FormHelperTest.php 283 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884
  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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @package Cake.Test.Case.View.Helper
  17. * @since CakePHP(tm) v 1.2.0.4206
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('ClassRegistry', 'Utility');
  21. App::uses('Controller', 'Controller');
  22. App::uses('View', 'View');
  23. App::uses('Model', 'Model');
  24. App::uses('Security', 'Utility');
  25. App::uses('CakeRequest', 'Network');
  26. App::uses('HtmlHelper', 'View/Helper');
  27. App::uses('FormHelper', 'View/Helper');
  28. App::uses('Router', 'Routing');
  29. /**
  30. * ContactTestController class
  31. *
  32. * @package cake
  33. * @package Cake.Test.Case.View.Helper
  34. */
  35. class ContactTestController extends Controller {
  36. /**
  37. * uses property
  38. *
  39. * @var mixed null
  40. */
  41. public $uses = null;
  42. }
  43. /**
  44. * Contact class
  45. *
  46. * @package cake
  47. * @package Cake.Test.Case.View.Helper
  48. */
  49. class Contact extends CakeTestModel {
  50. /**
  51. * useTable property
  52. *
  53. * @var bool false
  54. */
  55. public $useTable = false;
  56. /**
  57. * Default schema
  58. *
  59. * @var array
  60. */
  61. protected $_schema = array(
  62. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  63. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  65. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  66. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  67. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  68. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  69. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  70. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  71. );
  72. /**
  73. * validate property
  74. *
  75. * @var array
  76. */
  77. public $validate = array(
  78. 'non_existing' => array(),
  79. 'idontexist' => array(),
  80. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  81. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  82. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  83. 'imrequiredonboth' => array(
  84. 'required' => array('rule' => 'alphaNumeric'),
  85. ),
  86. 'string_required' => 'notEmpty',
  87. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  88. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  89. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  90. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  91. 'imalsonotrequired' => array(
  92. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  93. 'between' => array('rule' => array('between', 5, 30)),
  94. ),
  95. 'imalsonotrequired2' => array(
  96. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  97. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  98. ),
  99. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  100. 'iamrequiredalways' => array(
  101. 'email' => array('rule' => 'email'),
  102. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  103. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  104. ),
  105. 'boolean_field' => array('rule' => 'boolean')
  106. );
  107. /**
  108. * schema method
  109. *
  110. * @return void
  111. */
  112. public function setSchema($schema) {
  113. $this->_schema = $schema;
  114. }
  115. /**
  116. * hasAndBelongsToMany property
  117. *
  118. * @var array
  119. */
  120. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  121. /**
  122. * hasAndBelongsToMany property
  123. *
  124. * @var array
  125. */
  126. public $belongsTo = array('User' => array('className' => 'UserForm'));
  127. }
  128. /**
  129. * ContactTagsContact class
  130. *
  131. * @package cake
  132. * @package Cake.Test.Case.View.Helper
  133. */
  134. class ContactTagsContact extends CakeTestModel {
  135. /**
  136. * useTable property
  137. *
  138. * @var bool false
  139. */
  140. public $useTable = false;
  141. /**
  142. * Default schema
  143. *
  144. * @var array
  145. */
  146. protected $_schema = array(
  147. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  148. 'contact_tag_id' => array(
  149. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  150. )
  151. );
  152. /**
  153. * schema method
  154. *
  155. * @return void
  156. */
  157. public function setSchema($schema) {
  158. $this->_schema = $schema;
  159. }
  160. }
  161. /**
  162. * ContactNonStandardPk class
  163. *
  164. * @package cake
  165. * @package Cake.Test.Case.View.Helper
  166. */
  167. class ContactNonStandardPk extends Contact {
  168. /**
  169. * primaryKey property
  170. *
  171. * @var string 'pk'
  172. */
  173. public $primaryKey = 'pk';
  174. /**
  175. * schema method
  176. *
  177. * @return void
  178. */
  179. public function schema($field = false) {
  180. $this->_schema = parent::schema();
  181. $this->_schema['pk'] = $this->_schema['id'];
  182. unset($this->_schema['id']);
  183. return $this->_schema;
  184. }
  185. }
  186. /**
  187. * ContactTag class
  188. *
  189. * @package cake
  190. * @package Cake.Test.Case.View.Helper
  191. */
  192. class ContactTag extends Model {
  193. /**
  194. * useTable property
  195. *
  196. * @var bool false
  197. */
  198. public $useTable = false;
  199. /**
  200. * schema definition
  201. *
  202. * @var array
  203. */
  204. protected $_schema = array(
  205. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  206. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  207. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  208. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  209. );
  210. }
  211. /**
  212. * UserForm class
  213. *
  214. * @package cake
  215. * @package Cake.Test.Case.View.Helper
  216. */
  217. class UserForm extends CakeTestModel {
  218. /**
  219. * useTable property
  220. *
  221. * @var bool false
  222. */
  223. public $useTable = false;
  224. /**
  225. * hasMany property
  226. *
  227. * @var array
  228. */
  229. public $hasMany = array(
  230. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  231. ));
  232. /**
  233. * schema definition
  234. *
  235. * @var array
  236. */
  237. protected $_schema = array(
  238. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  239. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  240. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  241. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  242. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  243. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  244. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  245. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  246. );
  247. }
  248. /**
  249. * OpenidUrl class
  250. *
  251. * @package cake
  252. * @package Cake.Test.Case.View.Helper
  253. */
  254. class OpenidUrl extends CakeTestModel {
  255. /**
  256. * useTable property
  257. *
  258. * @var bool false
  259. */
  260. public $useTable = false;
  261. /**
  262. * belongsTo property
  263. *
  264. * @var array
  265. */
  266. public $belongsTo = array('UserForm' => array(
  267. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  268. ));
  269. /**
  270. * validate property
  271. *
  272. * @var array
  273. */
  274. public $validate = array('openid_not_registered' => array());
  275. /**
  276. * schema method
  277. *
  278. * @var array
  279. */
  280. protected $_schema = array(
  281. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  282. 'user_form_id' => array(
  283. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  284. ),
  285. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  286. );
  287. /**
  288. * beforeValidate method
  289. *
  290. * @return void
  291. */
  292. public function beforeValidate($options = array()) {
  293. $this->invalidate('openid_not_registered');
  294. return true;
  295. }
  296. }
  297. /**
  298. * ValidateUser class
  299. *
  300. * @package cake
  301. * @package Cake.Test.Case.View.Helper
  302. */
  303. class ValidateUser extends CakeTestModel {
  304. /**
  305. * useTable property
  306. *
  307. * @var bool false
  308. */
  309. public $useTable = false;
  310. /**
  311. * hasOne property
  312. *
  313. * @var array
  314. */
  315. public $hasOne = array('ValidateProfile' => array(
  316. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  317. ));
  318. /**
  319. * schema method
  320. *
  321. * @var array
  322. */
  323. protected $_schema = array(
  324. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  325. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  326. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  327. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  328. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  329. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  330. );
  331. /**
  332. * beforeValidate method
  333. *
  334. * @return void
  335. */
  336. public function beforeValidate($options = array()) {
  337. $this->invalidate('email');
  338. return false;
  339. }
  340. }
  341. /**
  342. * ValidateProfile class
  343. *
  344. * @package cake
  345. * @package Cake.Test.Case.View.Helper
  346. */
  347. class ValidateProfile extends CakeTestModel {
  348. /**
  349. * useTable property
  350. *
  351. * @var bool false
  352. */
  353. public $useTable = false;
  354. /**
  355. * schema property
  356. *
  357. * @var array
  358. */
  359. protected $_schema = array(
  360. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  361. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  362. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  363. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  364. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  365. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  366. );
  367. /**
  368. * hasOne property
  369. *
  370. * @var array
  371. */
  372. public $hasOne = array('ValidateItem' => array(
  373. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  374. ));
  375. /**
  376. * belongsTo property
  377. *
  378. * @var array
  379. */
  380. public $belongsTo = array('ValidateUser' => array(
  381. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  382. ));
  383. /**
  384. * beforeValidate method
  385. *
  386. * @return void
  387. */
  388. public function beforeValidate($options = array()) {
  389. $this->invalidate('full_name');
  390. $this->invalidate('city');
  391. return false;
  392. }
  393. }
  394. /**
  395. * ValidateItem class
  396. *
  397. * @package cake
  398. * @package Cake.Test.Case.View.Helper
  399. */
  400. class ValidateItem extends CakeTestModel {
  401. /**
  402. * useTable property
  403. *
  404. * @var bool false
  405. */
  406. public $useTable = false;
  407. /**
  408. * schema property
  409. *
  410. * @var array
  411. */
  412. protected $_schema = array(
  413. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  414. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  415. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  416. 'description' => array(
  417. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  418. ),
  419. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  420. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  421. );
  422. /**
  423. * belongsTo property
  424. *
  425. * @var array
  426. */
  427. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  428. /**
  429. * beforeValidate method
  430. *
  431. * @return void
  432. */
  433. public function beforeValidate($options = array()) {
  434. $this->invalidate('description');
  435. return false;
  436. }
  437. }
  438. /**
  439. * TestMail class
  440. *
  441. * @package cake
  442. * @package Cake.Test.Case.View.Helper
  443. */
  444. class TestMail extends CakeTestModel {
  445. /**
  446. * useTable property
  447. *
  448. * @var bool false
  449. */
  450. public $useTable = false;
  451. }
  452. /**
  453. * FormHelperTest class
  454. *
  455. * @package cake
  456. * @subpackage Cake.Test.Case.View.Helper
  457. * @property FormHelper $Form
  458. */
  459. class FormHelperTest extends CakeTestCase {
  460. /**
  461. * Fixtures to be used
  462. *
  463. * @var array
  464. */
  465. public $fixtures = array('core.post');
  466. /**
  467. * Do not load the fixtures by default
  468. *
  469. * @var boolean
  470. */
  471. public $autoFixtures = false;
  472. /**
  473. * setUp method
  474. *
  475. * @return void
  476. */
  477. public function setUp() {
  478. parent::setUp();
  479. Configure::write('App.base', '');
  480. $this->Controller = new ContactTestController();
  481. $this->View = new View($this->Controller);
  482. $this->Form = new FormHelper($this->View);
  483. $this->Form->Html = new HtmlHelper($this->View);
  484. $this->Form->request = new CakeRequest('contacts/add', false);
  485. $this->Form->request->here = '/contacts/add';
  486. $this->Form->request['action'] = 'add';
  487. $this->Form->request->webroot = '';
  488. $this->Form->request->base = '';
  489. ClassRegistry::addObject('Contact', new Contact());
  490. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  491. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  492. ClassRegistry::addObject('User', new UserForm());
  493. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  494. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  495. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  496. $this->oldSalt = Configure::read('Security.salt');
  497. $this->dateRegex = array(
  498. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  499. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  500. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  501. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  502. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  503. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  504. );
  505. Configure::write('Security.salt', 'foo!');
  506. }
  507. /**
  508. * tearDown method
  509. *
  510. * @return void
  511. */
  512. public function tearDown() {
  513. parent::tearDown();
  514. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  515. Configure::write('Security.salt', $this->oldSalt);
  516. }
  517. /**
  518. * testFormCreateWithSecurity method
  519. *
  520. * Test form->create() with security key.
  521. *
  522. * @return void
  523. */
  524. public function testCreateWithSecurity() {
  525. $this->Form->request['_Token'] = array('key' => 'testKey');
  526. $encoding = strtolower(Configure::read('App.encoding'));
  527. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  528. $expected = array(
  529. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  530. 'div' => array('style' => 'display:none;'),
  531. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  532. array('input' => array(
  533. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  534. )),
  535. '/div'
  536. );
  537. $this->assertTags($result, $expected);
  538. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  539. $expected['form']['id'] = 'MyForm';
  540. $this->assertTags($result, $expected);
  541. }
  542. /**
  543. * testFormCreateGetNoSecurity method
  544. *
  545. * Test form->create() with no security key as its a get form
  546. *
  547. * @return void
  548. */
  549. public function testCreateEndGetNoSecurity() {
  550. $this->Form->request['_Token'] = array('key' => 'testKey');
  551. $encoding = strtolower(Configure::read('App.encoding'));
  552. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  553. $this->assertNotContains('Token', $result);
  554. $result = $this->Form->end('Save');
  555. $this->assertNotContains('Token', $result);
  556. }
  557. /**
  558. * test that create() clears the fields property so it starts fresh
  559. *
  560. * @return void
  561. */
  562. public function testCreateClearingFields() {
  563. $this->Form->fields = array('model_id');
  564. $this->Form->create('Contact');
  565. $this->assertEquals(array(), $this->Form->fields);
  566. }
  567. /**
  568. * Tests form hash generation with model-less data
  569. *
  570. * @return void
  571. */
  572. public function testValidateHashNoModel() {
  573. $this->Form->request['_Token'] = array('key' => 'foo');
  574. $result = $this->Form->secure(array('anything'));
  575. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  576. }
  577. /**
  578. * Tests that models with identical field names get resolved properly
  579. *
  580. * @return void
  581. */
  582. public function testDuplicateFieldNameResolution() {
  583. $result = $this->Form->create('ValidateUser');
  584. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  585. $result = $this->Form->input('ValidateItem.name');
  586. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  587. $result = $this->Form->input('ValidateUser.name');
  588. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  589. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  590. $this->assertRegExp('/type="text"/', $result);
  591. $result = $this->Form->input('ValidateItem.name');
  592. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  593. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  594. $this->assertRegExp('/<textarea/', $result);
  595. $result = $this->Form->input('name');
  596. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  597. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  598. $this->assertRegExp('/type="text"/', $result);
  599. }
  600. /**
  601. * Tests that hidden fields generated for checkboxes don't get locked
  602. *
  603. * @return void
  604. */
  605. public function testNoCheckboxLocking() {
  606. $this->Form->request['_Token'] = array('key' => 'foo');
  607. $this->assertSame(array(), $this->Form->fields);
  608. $this->Form->checkbox('check', array('value' => '1'));
  609. $this->assertSame($this->Form->fields, array('check'));
  610. }
  611. /**
  612. * testFormSecurityFields method
  613. *
  614. * Test generation of secure form hash generation.
  615. *
  616. * @return void
  617. */
  618. public function testFormSecurityFields() {
  619. $key = 'testKey';
  620. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  621. $this->Form->request['_Token'] = array('key' => $key);
  622. $result = $this->Form->secure($fields);
  623. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  624. $hash .= ':' . 'Model.valid';
  625. $hash = urlencode($hash);
  626. $expected = array(
  627. 'div' => array('style' => 'display:none;'),
  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. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  640. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  641. $result = $this->Form->secure($fields);
  642. $expected = array(
  643. 'div' => array('class' => 'hidden'),
  644. array('input' => array(
  645. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  646. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  647. )),
  648. array('input' => array(
  649. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  650. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  651. )),
  652. '/div'
  653. );
  654. $this->assertTags($result, $expected);
  655. }
  656. /**
  657. * Tests correct generation of number fields for double and float fields
  658. *
  659. * @return void
  660. */
  661. public function testTextFieldGenerationForFloats() {
  662. $model = ClassRegistry::getObject('Contact');
  663. $model->setSchema(array('foo' => array(
  664. 'type' => 'float',
  665. 'null' => false,
  666. 'default' => null,
  667. 'length' => 10
  668. )));
  669. $this->Form->create('Contact');
  670. $result = $this->Form->input('foo');
  671. $expected = array(
  672. 'div' => array('class' => 'input number'),
  673. 'label' => array('for' => 'ContactFoo'),
  674. 'Foo',
  675. '/label',
  676. array('input' => array(
  677. 'type' => 'number',
  678. 'name' => 'data[Contact][foo]',
  679. 'id' => 'ContactFoo',
  680. 'step' => 'any'
  681. )),
  682. '/div'
  683. );
  684. $this->assertTags($result, $expected);
  685. $result = $this->Form->input('foo', array('step' => 0.5));
  686. $expected = array(
  687. 'div' => array('class' => 'input number'),
  688. 'label' => array('for' => 'ContactFoo'),
  689. 'Foo',
  690. '/label',
  691. array('input' => array(
  692. 'type' => 'number',
  693. 'name' => 'data[Contact][foo]',
  694. 'id' => 'ContactFoo',
  695. 'step' => '0.5'
  696. )),
  697. '/div'
  698. );
  699. $this->assertTags($result, $expected);
  700. }
  701. /**
  702. * Tests correct generation of number fields for integer fields
  703. *
  704. * @access public
  705. * @return void
  706. */
  707. public function testTextFieldTypeNumberGenerationForIntegers() {
  708. $model = ClassRegistry::getObject('Contact');
  709. $model->setSchema(array('foo' => array(
  710. 'type' => 'integer',
  711. 'null' => false,
  712. 'default' => null,
  713. 'length' => null
  714. )));
  715. $this->Form->create('Contact');
  716. $result = $this->Form->input('foo');
  717. $expected = array(
  718. 'div' => array('class' => 'input number'),
  719. 'label' => array('for' => 'ContactFoo'),
  720. 'Foo',
  721. '/label',
  722. array('input' => array(
  723. 'type' => 'number', 'name' => 'data[Contact][foo]',
  724. 'id' => 'ContactFoo'
  725. )),
  726. '/div'
  727. );
  728. $this->assertTags($result, $expected);
  729. }
  730. /**
  731. * testFormSecurityMultipleFields method
  732. *
  733. * Test secure() with multiple row form. Ensure hash is correct.
  734. *
  735. * @return void
  736. */
  737. public function testFormSecurityMultipleFields() {
  738. $key = 'testKey';
  739. $fields = array(
  740. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  741. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  742. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  743. );
  744. $this->Form->request['_Token'] = array('key' => $key);
  745. $result = $this->Form->secure($fields);
  746. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  747. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  748. $expected = array(
  749. 'div' => array('style' => 'display:none;'),
  750. array('input' => array(
  751. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  752. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  753. )),
  754. array('input' => array(
  755. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  756. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  757. )),
  758. '/div'
  759. );
  760. $this->assertTags($result, $expected);
  761. }
  762. /**
  763. * testFormSecurityMultipleSubmitButtons
  764. *
  765. * test form submit generation and ensure that _Token is only created on end()
  766. *
  767. * @return void
  768. */
  769. public function testFormSecurityMultipleSubmitButtons() {
  770. $key = 'testKey';
  771. $this->Form->request['_Token'] = array('key' => $key);
  772. $this->Form->create('Addresses');
  773. $this->Form->input('Address.title');
  774. $this->Form->input('Address.first_name');
  775. $result = $this->Form->submit('Save', array('name' => 'save'));
  776. $expected = array(
  777. 'div' => array('class' => 'submit'),
  778. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  779. '/div',
  780. );
  781. $this->assertTags($result, $expected);
  782. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  783. $expected = array(
  784. 'div' => array('class' => 'submit'),
  785. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  786. '/div',
  787. );
  788. $this->assertTags($result, $expected);
  789. $result = $this->Form->end(null);
  790. $expected = array(
  791. 'div' => array('style' => 'display:none;'),
  792. array('input' => array(
  793. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  794. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  795. )),
  796. array('input' => array(
  797. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  798. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  799. )),
  800. '/div'
  801. );
  802. $this->assertTags($result, $expected);
  803. }
  804. /**
  805. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  806. *
  807. * @return void
  808. */
  809. public function testSecurityButtonNestedNamed() {
  810. $key = 'testKey';
  811. $this->Form->request['_Token'] = array('key' => $key);
  812. $this->Form->create('Addresses');
  813. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  814. $result = $this->Form->unlockField();
  815. $this->assertEquals(array('Address.button'), $result);
  816. }
  817. /**
  818. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  819. *
  820. * @return void
  821. */
  822. public function testSecuritySubmitNestedNamed() {
  823. $key = 'testKey';
  824. $this->Form->request['_Token'] = array('key' => $key);
  825. $this->Form->create('Addresses');
  826. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  827. $result = $this->Form->unlockField();
  828. $this->assertEquals(array('Address.button'), $result);
  829. }
  830. /**
  831. * Test that the correct fields are unlocked for image submits with no names.
  832. *
  833. * @return void
  834. */
  835. public function testSecuritySubmitImageNoName() {
  836. $key = 'testKey';
  837. $this->Form->request['_Token'] = array('key' => $key);
  838. $this->Form->create('User');
  839. $result = $this->Form->submit('save.png');
  840. $expected = array(
  841. 'div' => array('class' => 'submit'),
  842. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  843. '/div'
  844. );
  845. $this->assertTags($result, $expected);
  846. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  847. }
  848. /**
  849. * Test that the correct fields are unlocked for image submits with names.
  850. *
  851. * @return void
  852. */
  853. public function testSecuritySubmitImageName() {
  854. $key = 'testKey';
  855. $this->Form->request['_Token'] = array('key' => $key);
  856. $this->Form->create('User');
  857. $result = $this->Form->submit('save.png', array('name' => 'test'));
  858. $expected = array(
  859. 'div' => array('class' => 'submit'),
  860. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  861. '/div'
  862. );
  863. $this->assertTags($result, $expected);
  864. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  865. }
  866. /**
  867. * testFormSecurityMultipleInputFields method
  868. *
  869. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  870. *
  871. * @return void
  872. */
  873. public function testFormSecurityMultipleInputFields() {
  874. $key = 'testKey';
  875. $this->Form->request['_Token'] = array('key' => $key);
  876. $this->Form->create('Addresses');
  877. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  878. $this->Form->input('Addresses.0.title');
  879. $this->Form->input('Addresses.0.first_name');
  880. $this->Form->input('Addresses.0.last_name');
  881. $this->Form->input('Addresses.0.address');
  882. $this->Form->input('Addresses.0.city');
  883. $this->Form->input('Addresses.0.phone');
  884. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  885. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  886. $this->Form->input('Addresses.1.title');
  887. $this->Form->input('Addresses.1.first_name');
  888. $this->Form->input('Addresses.1.last_name');
  889. $this->Form->input('Addresses.1.address');
  890. $this->Form->input('Addresses.1.city');
  891. $this->Form->input('Addresses.1.phone');
  892. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  893. $result = $this->Form->secure($this->Form->fields);
  894. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  895. $expected = array(
  896. 'div' => array('style' => 'display:none;'),
  897. array('input' => array(
  898. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  899. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  900. )),
  901. array('input' => array(
  902. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  903. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  904. )),
  905. '/div'
  906. );
  907. $this->assertTags($result, $expected);
  908. }
  909. /**
  910. * Test form security with Model.field.0 style inputs
  911. *
  912. * @return void
  913. */
  914. public function testFormSecurityArrayFields() {
  915. $key = 'testKey';
  916. $this->Form->request->params['_Token']['key'] = $key;
  917. $this->Form->create('Address');
  918. $this->Form->input('Address.primary.1');
  919. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  920. $this->Form->input('Address.secondary.1.0');
  921. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  922. }
  923. /**
  924. * testFormSecurityMultipleInputDisabledFields method
  925. *
  926. * test secure form generation with multiple records and disabled fields.
  927. *
  928. * @return void
  929. */
  930. public function testFormSecurityMultipleInputDisabledFields() {
  931. $key = 'testKey';
  932. $this->Form->request->params['_Token'] = array(
  933. 'key' => $key,
  934. 'unlockedFields' => array('first_name', 'address')
  935. );
  936. $this->Form->create();
  937. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  938. $this->Form->input('Addresses.0.title');
  939. $this->Form->input('Addresses.0.first_name');
  940. $this->Form->input('Addresses.0.last_name');
  941. $this->Form->input('Addresses.0.address');
  942. $this->Form->input('Addresses.0.city');
  943. $this->Form->input('Addresses.0.phone');
  944. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  945. $this->Form->input('Addresses.1.title');
  946. $this->Form->input('Addresses.1.first_name');
  947. $this->Form->input('Addresses.1.last_name');
  948. $this->Form->input('Addresses.1.address');
  949. $this->Form->input('Addresses.1.city');
  950. $this->Form->input('Addresses.1.phone');
  951. $result = $this->Form->secure($this->Form->fields);
  952. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  953. $expected = array(
  954. 'div' => array('style' => 'display:none;'),
  955. array('input' => array(
  956. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  957. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  958. )),
  959. array('input' => array(
  960. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  961. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  962. )),
  963. '/div'
  964. );
  965. $this->assertTags($result, $expected);
  966. }
  967. /**
  968. * testFormSecurityInputDisabledFields method
  969. *
  970. * Test single record form with disabled fields.
  971. *
  972. * @return void
  973. */
  974. public function testFormSecurityInputUnlockedFields() {
  975. $key = 'testKey';
  976. $this->Form->request['_Token'] = array(
  977. 'key' => $key,
  978. 'unlockedFields' => array('first_name', 'address')
  979. );
  980. $this->Form->create();
  981. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  982. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  983. $this->Form->input('Addresses.title');
  984. $this->Form->input('Addresses.first_name');
  985. $this->Form->input('Addresses.last_name');
  986. $this->Form->input('Addresses.address');
  987. $this->Form->input('Addresses.city');
  988. $this->Form->input('Addresses.phone');
  989. $result = $this->Form->fields;
  990. $expected = array(
  991. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  992. 'Addresses.city', 'Addresses.phone'
  993. );
  994. $this->assertEquals($expected, $result);
  995. $result = $this->Form->secure($expected);
  996. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  997. $expected = array(
  998. 'div' => array('style' => 'display:none;'),
  999. array('input' => array(
  1000. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1001. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1002. )),
  1003. array('input' => array(
  1004. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1005. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1006. )),
  1007. '/div'
  1008. );
  1009. $this->assertTags($result, $expected);
  1010. }
  1011. /**
  1012. * test securing inputs with custom name attributes.
  1013. *
  1014. * @return void
  1015. */
  1016. public function testFormSecureWithCustomNameAttribute() {
  1017. $this->Form->request->params['_Token']['key'] = 'testKey';
  1018. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1019. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1020. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1021. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1022. }
  1023. /**
  1024. * testFormSecuredInput method
  1025. *
  1026. * Test generation of entire secure form, assertions made on input() output.
  1027. *
  1028. * @return void
  1029. */
  1030. public function testFormSecuredInput() {
  1031. $this->Form->request['_Token'] = array('key' => 'testKey');
  1032. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1033. $encoding = strtolower(Configure::read('App.encoding'));
  1034. $expected = array(
  1035. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1036. 'div' => array('style' => 'display:none;'),
  1037. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1038. array('input' => array(
  1039. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1040. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1041. )),
  1042. '/div'
  1043. );
  1044. $this->assertTags($result, $expected);
  1045. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1046. $expected = array(
  1047. 'div' => array('class' => 'input text'),
  1048. 'label' => array('for' => 'UserFormPublished'),
  1049. 'Published',
  1050. '/label',
  1051. array('input' => array(
  1052. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1053. 'id' => 'UserFormPublished'
  1054. )),
  1055. '/div'
  1056. );
  1057. $this->assertTags($result, $expected);
  1058. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1059. $expected = array(
  1060. 'div' => array('class' => 'input text'),
  1061. 'label' => array('for' => 'UserFormOther'),
  1062. 'Other',
  1063. '/label',
  1064. array('input' => array(
  1065. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1066. 'id' => 'UserFormOther'
  1067. )),
  1068. '/div'
  1069. );
  1070. $this->assertTags($result, $expected);
  1071. $result = $this->Form->hidden('UserForm.stuff');
  1072. $expected = array(
  1073. 'input' => array(
  1074. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1075. 'id' => 'UserFormStuff'
  1076. ));
  1077. $this->assertTags($result, $expected);
  1078. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1079. $expected = array('input' => array(
  1080. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1081. 'value' => '0', 'id' => 'UserFormHidden'
  1082. ));
  1083. $this->assertTags($result, $expected);
  1084. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1085. $expected = array(
  1086. 'div' => array('class' => 'input checkbox'),
  1087. array('input' => array(
  1088. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1089. 'value' => '0', 'id' => 'UserFormSomething_'
  1090. )),
  1091. array('input' => array(
  1092. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1093. 'value' => '1', 'id' => 'UserFormSomething'
  1094. )),
  1095. 'label' => array('for' => 'UserFormSomething'),
  1096. 'Something',
  1097. '/label',
  1098. '/div'
  1099. );
  1100. $this->assertTags($result, $expected);
  1101. $result = $this->Form->fields;
  1102. $expected = array(
  1103. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1104. 'UserForm.hidden' => '0', 'UserForm.something'
  1105. );
  1106. $this->assertEquals($expected, $result);
  1107. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1108. $result = $this->Form->secure($this->Form->fields);
  1109. $expected = array(
  1110. 'div' => array('style' => 'display:none;'),
  1111. array('input' => array(
  1112. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1113. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1114. )),
  1115. array('input' => array(
  1116. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1117. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1118. )),
  1119. '/div'
  1120. );
  1121. $this->assertTags($result, $expected);
  1122. }
  1123. /**
  1124. * Test secured inputs with custom names.
  1125. *
  1126. * @return void
  1127. */
  1128. public function testSecuredInputCustomName() {
  1129. $this->Form->request['_Token'] = array('key' => 'testKey');
  1130. $this->assertEquals(array(), $this->Form->fields);
  1131. $this->Form->input('text_input', array(
  1132. 'name' => 'data[Option][General.default_role]',
  1133. ));
  1134. $expected = array('Option.General.default_role');
  1135. $this->assertEquals($expected, $this->Form->fields);
  1136. $this->Form->input('select_box', array(
  1137. 'name' => 'data[Option][General.select_role]',
  1138. 'type' => 'select',
  1139. 'options' => array(1, 2),
  1140. ));
  1141. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1142. $this->assertEquals($expected, $this->Form->fields);
  1143. }
  1144. /**
  1145. * Tests that the correct keys are added to the field hash index
  1146. *
  1147. * @return void
  1148. */
  1149. public function testFormSecuredFileInput() {
  1150. $this->Form->request['_Token'] = array('key' => 'testKey');
  1151. $this->assertEquals(array(), $this->Form->fields);
  1152. $this->Form->file('Attachment.file');
  1153. $expected = array(
  1154. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1155. 'Attachment.file.error', 'Attachment.file.size'
  1156. );
  1157. $this->assertEquals($expected, $this->Form->fields);
  1158. }
  1159. /**
  1160. * test that multiple selects keys are added to field hash
  1161. *
  1162. * @return void
  1163. */
  1164. public function testFormSecuredMultipleSelect() {
  1165. $this->Form->request['_Token'] = array('key' => 'testKey');
  1166. $this->assertEquals(array(), $this->Form->fields);
  1167. $options = array('1' => 'one', '2' => 'two');
  1168. $this->Form->select('Model.select', $options);
  1169. $expected = array('Model.select');
  1170. $this->assertEquals($expected, $this->Form->fields);
  1171. $this->Form->fields = array();
  1172. $this->Form->select('Model.select', $options, array('multiple' => true));
  1173. $this->assertEquals($expected, $this->Form->fields);
  1174. }
  1175. /**
  1176. * testFormSecuredRadio method
  1177. *
  1178. * @return void
  1179. */
  1180. public function testFormSecuredRadio() {
  1181. $this->Form->request['_Token'] = array('key' => 'testKey');
  1182. $this->assertEquals(array(), $this->Form->fields);
  1183. $options = array('1' => 'option1', '2' => 'option2');
  1184. $this->Form->radio('Test.test', $options);
  1185. $expected = array('Test.test');
  1186. $this->assertEquals($expected, $this->Form->fields);
  1187. }
  1188. /**
  1189. * Test that when disabled is in a list based attribute array it works.
  1190. *
  1191. * @return void
  1192. */
  1193. public function testFormSecuredAndDisabledNotAssoc() {
  1194. $this->Form->request['_Token'] = array('key' => 'testKey');
  1195. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1196. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1197. $this->Form->text('Model.text', array('disabled'));
  1198. $this->Form->textarea('Model.textarea', array('disabled'));
  1199. $this->Form->password('Model.password', array('disabled'));
  1200. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1201. $expected = array(
  1202. 'Model.radio' => ''
  1203. );
  1204. $this->assertEquals($expected, $this->Form->fields);
  1205. }
  1206. /**
  1207. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1208. * hashing.
  1209. *
  1210. * @return void
  1211. */
  1212. public function testFormSecuredAndDisabled() {
  1213. $this->Form->request['_Token'] = array('key' => 'testKey');
  1214. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1215. $this->Form->text('Model.text', array('disabled' => true));
  1216. $this->Form->password('Model.text', array('disabled' => true));
  1217. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1218. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1219. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1220. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1221. $this->Form->month('Model.month', array('disabled' => true));
  1222. $this->Form->day('Model.day', array('disabled' => true));
  1223. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1224. $this->Form->minute('Model.minute', array('disabled' => true));
  1225. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1226. $expected = array(
  1227. 'Model.radio' => ''
  1228. );
  1229. $this->assertEquals($expected, $this->Form->fields);
  1230. }
  1231. /**
  1232. * testDisableSecurityUsingForm method
  1233. *
  1234. * @return void
  1235. */
  1236. public function testDisableSecurityUsingForm() {
  1237. $this->Form->request['_Token'] = array(
  1238. 'key' => 'testKey',
  1239. 'disabledFields' => array()
  1240. );
  1241. $this->Form->create();
  1242. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1243. $this->Form->input('Addresses.title');
  1244. $this->Form->input('Addresses.first_name', array('secure' => false));
  1245. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1246. $this->Form->input('Addresses.zip', array(
  1247. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1248. ));
  1249. $result = $this->Form->fields;
  1250. $expected = array(
  1251. 'Addresses.id' => '123456', 'Addresses.title',
  1252. );
  1253. $this->assertEquals($expected, $result);
  1254. }
  1255. /**
  1256. * test disableField
  1257. *
  1258. * @return void
  1259. */
  1260. public function testUnlockFieldAddsToList() {
  1261. $this->Form->request['_Token'] = array(
  1262. 'key' => 'testKey',
  1263. 'unlockedFields' => array()
  1264. );
  1265. $this->Form->create('Contact');
  1266. $this->Form->unlockField('Contact.name');
  1267. $this->Form->text('Contact.name');
  1268. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1269. $this->assertEquals(array(), $this->Form->fields);
  1270. }
  1271. /**
  1272. * test unlockField removing from fields array.
  1273. *
  1274. * @return void
  1275. */
  1276. public function testUnlockFieldRemovingFromFields() {
  1277. $this->Form->request['_Token'] = array(
  1278. 'key' => 'testKey',
  1279. 'unlockedFields' => array()
  1280. );
  1281. $this->Form->create('Contact');
  1282. $this->Form->hidden('Contact.id', array('value' => 1));
  1283. $this->Form->text('Contact.name');
  1284. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1285. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1286. $this->Form->unlockField('Contact.name');
  1287. $this->Form->unlockField('Contact.id');
  1288. $this->assertEquals(array(), $this->Form->fields);
  1289. }
  1290. /**
  1291. * testTagIsInvalid method
  1292. *
  1293. * @return void
  1294. */
  1295. public function testTagIsInvalid() {
  1296. $Contact = ClassRegistry::getObject('Contact');
  1297. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1298. $this->Form->setEntity('Contact.0.email');
  1299. $result = $this->Form->tagIsInvalid();
  1300. $expected = array('Please provide an email');
  1301. $this->assertEquals($expected, $result);
  1302. $this->Form->setEntity('Contact.1.email');
  1303. $result = $this->Form->tagIsInvalid();
  1304. $expected = false;
  1305. $this->assertSame($expected, $result);
  1306. $this->Form->setEntity('Contact.0.name');
  1307. $result = $this->Form->tagIsInvalid();
  1308. $expected = false;
  1309. $this->assertSame($expected, $result);
  1310. }
  1311. /**
  1312. * Test validation errors.
  1313. *
  1314. * @return void
  1315. */
  1316. public function testPasswordValidation() {
  1317. $Contact = ClassRegistry::getObject('Contact');
  1318. $Contact->validationErrors['password'] = array('Please provide a password');
  1319. $result = $this->Form->input('Contact.password');
  1320. $expected = array(
  1321. 'div' => array('class' => 'input password error'),
  1322. 'label' => array('for' => 'ContactPassword'),
  1323. 'Password',
  1324. '/label',
  1325. 'input' => array(
  1326. 'type' => 'password', 'name' => 'data[Contact][password]',
  1327. 'id' => 'ContactPassword', 'class' => 'form-error'
  1328. ),
  1329. array('div' => array('class' => 'error-message')),
  1330. 'Please provide a password',
  1331. '/div',
  1332. '/div'
  1333. );
  1334. $this->assertTags($result, $expected);
  1335. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1336. $expected = array(
  1337. 'div' => array('class' => 'input password error'),
  1338. 'label' => array('for' => 'ContactPassword'),
  1339. 'Password',
  1340. '/label',
  1341. 'input' => array(
  1342. 'type' => 'password', 'name' => 'data[Contact][password]',
  1343. 'id' => 'ContactPassword', 'class' => 'form-error'
  1344. ),
  1345. '/div'
  1346. );
  1347. $this->assertTags($result, $expected);
  1348. }
  1349. /**
  1350. * Test validation errors, when validation message is an empty string.
  1351. *
  1352. * @access public
  1353. * @return void
  1354. */
  1355. public function testEmptyErrorValidation() {
  1356. $this->Form->validationErrors['Contact']['password'] = '';
  1357. $result = $this->Form->input('Contact.password');
  1358. $expected = array(
  1359. 'div' => array('class' => 'input password error'),
  1360. 'label' => array('for' => 'ContactPassword'),
  1361. 'Password',
  1362. '/label',
  1363. 'input' => array(
  1364. 'type' => 'password', 'name' => 'data[Contact][password]',
  1365. 'id' => 'ContactPassword', 'class' => 'form-error'
  1366. ),
  1367. array('div' => array('class' => 'error-message')),
  1368. array(),
  1369. '/div',
  1370. '/div'
  1371. );
  1372. $this->assertTags($result, $expected);
  1373. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1374. $expected = array(
  1375. 'div' => array('class' => 'input password error'),
  1376. 'label' => array('for' => 'ContactPassword'),
  1377. 'Password',
  1378. '/label',
  1379. 'input' => array(
  1380. 'type' => 'password', 'name' => 'data[Contact][password]',
  1381. 'id' => 'ContactPassword', 'class' => 'form-error'
  1382. ),
  1383. '/div'
  1384. );
  1385. $this->assertTags($result, $expected);
  1386. }
  1387. /**
  1388. * Test validation errors, when calling input() overriding validation message by an empty string.
  1389. *
  1390. * @access public
  1391. * @return void
  1392. */
  1393. public function testEmptyInputErrorValidation() {
  1394. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1395. $result = $this->Form->input('Contact.password', array('error' => ''));
  1396. $expected = array(
  1397. 'div' => array('class' => 'input password error'),
  1398. 'label' => array('for' => 'ContactPassword'),
  1399. 'Password',
  1400. '/label',
  1401. 'input' => array(
  1402. 'type' => 'password', 'name' => 'data[Contact][password]',
  1403. 'id' => 'ContactPassword', 'class' => 'form-error'
  1404. ),
  1405. array('div' => array('class' => 'error-message')),
  1406. array(),
  1407. '/div',
  1408. '/div'
  1409. );
  1410. $this->assertTags($result, $expected);
  1411. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1412. $expected = array(
  1413. 'div' => array('class' => 'input password error'),
  1414. 'label' => array('for' => 'ContactPassword'),
  1415. 'Password',
  1416. '/label',
  1417. 'input' => array(
  1418. 'type' => 'password', 'name' => 'data[Contact][password]',
  1419. 'id' => 'ContactPassword', 'class' => 'form-error'
  1420. ),
  1421. '/div'
  1422. );
  1423. $this->assertTags($result, $expected);
  1424. }
  1425. /**
  1426. * testFormValidationAssociated method
  1427. *
  1428. * test display of form errors in conjunction with model::validates.
  1429. *
  1430. * @return void
  1431. */
  1432. public function testFormValidationAssociated() {
  1433. $this->UserForm = ClassRegistry::getObject('UserForm');
  1434. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1435. $data = array(
  1436. 'UserForm' => array('name' => 'user'),
  1437. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1438. );
  1439. $result = $this->UserForm->OpenidUrl->create($data);
  1440. $this->assertFalse(empty($result));
  1441. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1442. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1443. $encoding = strtolower(Configure::read('App.encoding'));
  1444. $expected = array(
  1445. 'form' => array(
  1446. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1447. 'accept-charset' => $encoding
  1448. ),
  1449. 'div' => array('style' => 'display:none;'),
  1450. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1451. '/div'
  1452. );
  1453. $this->assertTags($result, $expected);
  1454. $result = $this->Form->error(
  1455. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1456. );
  1457. $this->assertEquals('Error, not registered', $result);
  1458. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1459. }
  1460. /**
  1461. * testFormValidationAssociatedFirstLevel method
  1462. *
  1463. * test form error display with associated model.
  1464. *
  1465. * @return void
  1466. */
  1467. public function testFormValidationAssociatedFirstLevel() {
  1468. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1469. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1470. $data = array(
  1471. 'ValidateUser' => array('name' => 'mariano'),
  1472. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1473. );
  1474. $result = $this->ValidateUser->create($data);
  1475. $this->assertFalse(empty($result));
  1476. $this->assertFalse($this->ValidateUser->validates());
  1477. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1478. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1479. $encoding = strtolower(Configure::read('App.encoding'));
  1480. $expected = array(
  1481. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1482. 'div' => array('style' => 'display:none;'),
  1483. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1484. '/div'
  1485. );
  1486. $this->assertTags($result, $expected);
  1487. $result = $this->Form->error(
  1488. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1489. );
  1490. $this->assertEquals('Invalid email', $result);
  1491. $result = $this->Form->error(
  1492. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1493. );
  1494. $this->assertEquals('Invalid name', $result);
  1495. $result = $this->Form->error(
  1496. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1497. );
  1498. $this->assertEquals('Invalid city', $result);
  1499. unset($this->ValidateUser->ValidateProfile);
  1500. unset($this->ValidateUser);
  1501. }
  1502. /**
  1503. * testFormValidationAssociatedSecondLevel method
  1504. *
  1505. * test form error display with associated model.
  1506. *
  1507. * @return void
  1508. */
  1509. public function testFormValidationAssociatedSecondLevel() {
  1510. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1511. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1512. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1513. $data = array(
  1514. 'ValidateUser' => array('name' => 'mariano'),
  1515. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1516. 'ValidateItem' => array('name' => 'Item')
  1517. );
  1518. $result = $this->ValidateUser->create($data);
  1519. $this->assertFalse(empty($result));
  1520. $this->assertFalse($this->ValidateUser->validates());
  1521. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1522. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1523. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1524. $encoding = strtolower(Configure::read('App.encoding'));
  1525. $expected = array(
  1526. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1527. 'div' => array('style' => 'display:none;'),
  1528. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1529. '/div'
  1530. );
  1531. $this->assertTags($result, $expected);
  1532. $result = $this->Form->error(
  1533. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1534. );
  1535. $this->assertEquals('Invalid email', $result);
  1536. $result = $this->Form->error(
  1537. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1538. );
  1539. $this->assertEquals('Invalid name', $result);
  1540. $result = $this->Form->error(
  1541. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1542. );
  1543. $result = $this->Form->error(
  1544. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1545. );
  1546. $this->assertEquals('Invalid description', $result);
  1547. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1548. unset($this->ValidateUser->ValidateProfile);
  1549. unset($this->ValidateUser);
  1550. }
  1551. /**
  1552. * testFormValidationMultiRecord method
  1553. *
  1554. * test form error display with multiple records.
  1555. *
  1556. * @return void
  1557. */
  1558. public function testFormValidationMultiRecord() {
  1559. $Contact = ClassRegistry::getObject('Contact');
  1560. $Contact->validationErrors[2] = array(
  1561. 'name' => array('This field cannot be left blank')
  1562. );
  1563. $result = $this->Form->input('Contact.2.name');
  1564. $expected = array(
  1565. 'div' => array('class' => 'input text error'),
  1566. 'label' => array('for' => 'Contact2Name'),
  1567. 'Name',
  1568. '/label',
  1569. 'input' => array(
  1570. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1571. 'class' => 'form-error', 'maxlength' => 255
  1572. ),
  1573. array('div' => array('class' => 'error-message')),
  1574. 'This field cannot be left blank',
  1575. '/div',
  1576. '/div'
  1577. );
  1578. $this->assertTags($result, $expected);
  1579. }
  1580. /**
  1581. * testMultipleInputValidation method
  1582. *
  1583. * test multiple record form validation error display.
  1584. *
  1585. * @return void
  1586. */
  1587. public function testMultipleInputValidation() {
  1588. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1589. $Address->validationErrors[0] = array(
  1590. 'title' => array('This field cannot be empty'),
  1591. 'first_name' => array('This field cannot be empty')
  1592. );
  1593. $Address->validationErrors[1] = array(
  1594. 'last_name' => array('You must have a last name')
  1595. );
  1596. $this->Form->create();
  1597. $result = $this->Form->input('Address.0.title');
  1598. $expected = array(
  1599. 'div' => array('class'),
  1600. 'label' => array('for'),
  1601. 'preg:/[^<]+/',
  1602. '/label',
  1603. 'input' => array(
  1604. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1605. ),
  1606. array('div' => array('class' => 'error-message')),
  1607. 'This field cannot be empty',
  1608. '/div',
  1609. '/div'
  1610. );
  1611. $this->assertTags($result, $expected);
  1612. $result = $this->Form->input('Address.0.first_name');
  1613. $expected = array(
  1614. 'div' => array('class'),
  1615. 'label' => array('for'),
  1616. 'preg:/[^<]+/',
  1617. '/label',
  1618. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1619. array('div' => array('class' => 'error-message')),
  1620. 'This field cannot be empty',
  1621. '/div',
  1622. '/div'
  1623. );
  1624. $this->assertTags($result, $expected);
  1625. $result = $this->Form->input('Address.0.last_name');
  1626. $expected = array(
  1627. 'div' => array('class'),
  1628. 'label' => array('for'),
  1629. 'preg:/[^<]+/',
  1630. '/label',
  1631. 'input' => array('type' => 'text', 'name', 'id'),
  1632. '/div'
  1633. );
  1634. $this->assertTags($result, $expected);
  1635. $result = $this->Form->input('Address.1.last_name');
  1636. $expected = array(
  1637. 'div' => array('class'),
  1638. 'label' => array('for'),
  1639. 'preg:/[^<]+/',
  1640. '/label',
  1641. 'input' => array(
  1642. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1643. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1644. ),
  1645. array('div' => array('class' => 'error-message')),
  1646. 'You must have a last name',
  1647. '/div',
  1648. '/div'
  1649. );
  1650. $this->assertTags($result, $expected);
  1651. }
  1652. /**
  1653. * testInput method
  1654. *
  1655. * Test various incarnations of input().
  1656. *
  1657. * @return void
  1658. */
  1659. public function testInput() {
  1660. $result = $this->Form->input('ValidateUser.balance');
  1661. $expected = array(
  1662. 'div' => array('class'),
  1663. 'label' => array('for'),
  1664. 'Balance',
  1665. '/label',
  1666. 'input' => array('name', 'type' => 'number', 'id'),
  1667. '/div',
  1668. );
  1669. $this->assertTags($result, $expected);
  1670. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1671. $expected = array(
  1672. 'div' => array('class' => 'input email'),
  1673. 'label' => array('for' => 'custom'),
  1674. 'Email',
  1675. '/label',
  1676. array('input' => array(
  1677. 'type' => 'email', 'name' => 'data[Contact][email]',
  1678. 'id' => 'custom', 'maxlength' => 255
  1679. )),
  1680. '/div'
  1681. );
  1682. $this->assertTags($result, $expected);
  1683. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1684. $expected = array(
  1685. '<div',
  1686. 'label' => array('for' => 'ContactEmail'),
  1687. 'Email',
  1688. '/label',
  1689. array('input' => array(
  1690. 'type' => 'email', 'name' => 'data[Contact][email]',
  1691. 'id' => 'ContactEmail', 'maxlength' => 255
  1692. )),
  1693. '/div'
  1694. );
  1695. $this->assertTags($result, $expected);
  1696. $result = $this->Form->hidden('Contact.idontexist');
  1697. $expected = array('input' => array(
  1698. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1699. 'id' => 'ContactIdontexist'
  1700. ));
  1701. $this->assertTags($result, $expected);
  1702. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1703. $expected = array(
  1704. 'div' => array('class' => 'input text'),
  1705. 'label' => array('for' => 'ContactEmail'),
  1706. 'Email',
  1707. '/label',
  1708. array('input' => array(
  1709. 'type' => 'text', 'name' => 'data[Contact][email]',
  1710. 'id' => 'ContactEmail'
  1711. )),
  1712. '/div'
  1713. );
  1714. $this->assertTags($result, $expected);
  1715. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1716. $expected = array(
  1717. 'div' => array('class' => 'input text'),
  1718. 'label' => array('for' => 'Contact5Email'),
  1719. 'Email',
  1720. '/label',
  1721. array('input' => array(
  1722. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1723. 'id' => 'Contact5Email'
  1724. )),
  1725. '/div'
  1726. );
  1727. $this->assertTags($result, $expected);
  1728. $result = $this->Form->input('Contact.password');
  1729. $expected = array(
  1730. 'div' => array('class' => 'input password'),
  1731. 'label' => array('for' => 'ContactPassword'),
  1732. 'Password',
  1733. '/label',
  1734. array('input' => array(
  1735. 'type' => 'password', 'name' => 'data[Contact][password]',
  1736. 'id' => 'ContactPassword'
  1737. )),
  1738. '/div'
  1739. );
  1740. $this->assertTags($result, $expected);
  1741. $result = $this->Form->input('Contact.email', array(
  1742. 'type' => 'file', 'class' => 'textbox'
  1743. ));
  1744. $expected = array(
  1745. 'div' => array('class' => 'input file'),
  1746. 'label' => array('for' => 'ContactEmail'),
  1747. 'Email',
  1748. '/label',
  1749. array('input' => array(
  1750. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1751. 'id' => 'ContactEmail'
  1752. )),
  1753. '/div'
  1754. );
  1755. $this->assertTags($result, $expected);
  1756. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1757. $result = $this->Form->input('Contact.phone');
  1758. $expected = array(
  1759. 'div' => array('class' => 'input tel'),
  1760. 'label' => array('for' => 'ContactPhone'),
  1761. 'Phone',
  1762. '/label',
  1763. array('input' => array(
  1764. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1765. 'value' => 'Hello &amp; World &gt; weird chars',
  1766. 'id' => 'ContactPhone', 'maxlength' => 255
  1767. )),
  1768. '/div'
  1769. );
  1770. $this->assertTags($result, $expected);
  1771. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1772. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1773. $expected = array(
  1774. 'div' => array('class' => 'input text'),
  1775. 'label' => array('for' => 'myId'),
  1776. 'Field',
  1777. '/label',
  1778. 'input' => array(
  1779. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1780. 'value' => 'My value', 'id' => 'myId'
  1781. ),
  1782. '/div'
  1783. );
  1784. $this->assertTags($result, $expected);
  1785. unset($this->Form->request->data);
  1786. $Contact = ClassRegistry::getObject('Contact');
  1787. $Contact->validationErrors['field'] = array('Badness!');
  1788. $result = $this->Form->input('Contact.field');
  1789. $expected = array(
  1790. 'div' => array('class' => 'input text error'),
  1791. 'label' => array('for' => 'ContactField'),
  1792. 'Field',
  1793. '/label',
  1794. 'input' => array(
  1795. 'type' => 'text', 'name' => 'data[Contact][field]',
  1796. 'id' => 'ContactField', 'class' => 'form-error'
  1797. ),
  1798. array('div' => array('class' => 'error-message')),
  1799. 'Badness!',
  1800. '/div',
  1801. '/div'
  1802. );
  1803. $this->assertTags($result, $expected);
  1804. $result = $this->Form->input('Contact.field', array(
  1805. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1806. ));
  1807. $expected = array(
  1808. 'label' => array('for' => 'ContactField'),
  1809. 'Field',
  1810. '/label',
  1811. 'input' => array(
  1812. 'type' => 'text', 'name' => 'data[Contact][field]',
  1813. 'id' => 'ContactField', 'class' => 'form-error'
  1814. ),
  1815. array('span' => array('class' => 'error-message')),
  1816. 'Badness!',
  1817. '/span'
  1818. );
  1819. $this->assertTags($result, $expected);
  1820. $result = $this->Form->input('Contact.field', array(
  1821. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1822. ));
  1823. $expected = array(
  1824. 'div' => array('class' => 'input text error'),
  1825. 'label' => array('for' => 'ContactField'),
  1826. 'Field',
  1827. '/label',
  1828. 'input' => array(
  1829. 'type' => 'text', 'name' => 'data[Contact][field]',
  1830. 'id' => 'ContactField', 'class' => 'form-error'
  1831. ),
  1832. array('div' => array('class' => 'error')),
  1833. 'Badness!',
  1834. '/div'
  1835. );
  1836. $this->assertTags($result, $expected);
  1837. $result = $this->Form->input('Contact.field', array(
  1838. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1839. ));
  1840. $expected = array(
  1841. 'span' => array('class' => 'input text error'),
  1842. 'label' => array('for' => 'ContactField'),
  1843. 'Field',
  1844. '/label',
  1845. 'input' => array(
  1846. 'type' => 'text', 'name' => 'data[Contact][field]',
  1847. 'id' => 'ContactField', 'class' => 'form-error'
  1848. ),
  1849. 'Badness!',
  1850. '/span'
  1851. );
  1852. $this->assertTags($result, $expected);
  1853. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1854. $expected = array(
  1855. 'div' => array('class' => 'input text error'),
  1856. 'label' => array('for' => 'ContactField'),
  1857. 'Field',
  1858. '/label',
  1859. 'input' => array(
  1860. 'type' => 'text', 'name' => 'data[Contact][field]',
  1861. 'id' => 'ContactField', 'class' => 'form-error'
  1862. ),
  1863. 'A message to you, Rudy',
  1864. array('div' => array('class' => 'error-message')),
  1865. 'Badness!',
  1866. '/div',
  1867. '/div'
  1868. );
  1869. $this->assertTags($result, $expected);
  1870. $this->Form->setEntity(null);
  1871. $this->Form->setEntity('Contact.field');
  1872. $result = $this->Form->input('Contact.field', array(
  1873. 'after' => 'A message to you, Rudy', 'error' => false
  1874. ));
  1875. $expected = array(
  1876. 'div' => array('class' => 'input text'),
  1877. 'label' => array('for' => 'ContactField'),
  1878. 'Field',
  1879. '/label',
  1880. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1881. 'A message to you, Rudy',
  1882. '/div'
  1883. );
  1884. $this->assertTags($result, $expected);
  1885. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1886. $expected = array(
  1887. 'div' => array('class' => 'input text'),
  1888. 'label' => array('for' => 'ObjectField'),
  1889. 'Field',
  1890. '/label',
  1891. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1892. 'A message to you, Rudy',
  1893. '/div'
  1894. );
  1895. $this->assertTags($result, $expected);
  1896. $Contact->validationErrors['field'] = array('minLength');
  1897. $result = $this->Form->input('Contact.field', array(
  1898. 'error' => array(
  1899. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1900. 'maxLength' => 'login too large'
  1901. )
  1902. ));
  1903. $expected = array(
  1904. 'div' => array('class' => 'input text error'),
  1905. 'label' => array('for' => 'ContactField'),
  1906. 'Field',
  1907. '/label',
  1908. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1909. array('div' => array('class' => 'error-message')),
  1910. 'Le login doit contenir au moins 2 caractères',
  1911. '/div',
  1912. '/div'
  1913. );
  1914. $this->assertTags($result, $expected);
  1915. $Contact->validationErrors['field'] = array('maxLength');
  1916. $result = $this->Form->input('Contact.field', array(
  1917. 'error' => array(
  1918. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1919. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1920. 'maxLength' => 'login too large',
  1921. )
  1922. ));
  1923. $expected = array(
  1924. 'div' => array('class' => 'input text error'),
  1925. 'label' => array('for' => 'ContactField'),
  1926. 'Field',
  1927. '/label',
  1928. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1929. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1930. 'login too large',
  1931. '/span',
  1932. '/div'
  1933. );
  1934. $this->assertTags($result, $expected);
  1935. }
  1936. /**
  1937. * Test that inputs with 0 can be created.
  1938. *
  1939. * @return void
  1940. */
  1941. public function testInputZero() {
  1942. $this->Form->create('User');
  1943. $result = $this->Form->input('0');
  1944. $expected = array(
  1945. 'div' => array('class' => 'input text'),
  1946. 'label' => array('for' => 'User0'), '/label',
  1947. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1948. '/div'
  1949. );
  1950. $this->assertTags($result, $expected);
  1951. }
  1952. /**
  1953. * test input() with checkbox creation
  1954. *
  1955. * @return void
  1956. */
  1957. public function testInputCheckbox() {
  1958. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1959. $expected = array(
  1960. 'div' => array('class' => 'input checkbox'),
  1961. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1962. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1963. '/div'
  1964. );
  1965. $this->assertTags($result, $expected);
  1966. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1967. $expected = array(
  1968. 'div' => array('class' => 'input checkbox'),
  1969. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1970. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1971. '/div'
  1972. );
  1973. $this->assertTags($result, $expected);
  1974. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1975. $expected = array(
  1976. 'div' => array('class' => 'input checkbox'),
  1977. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1978. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1979. '/div'
  1980. );
  1981. $this->assertTags($result, $expected);
  1982. $result = $this->Form->input('User.disabled', array(
  1983. 'label' => 'Disabled',
  1984. 'type' => 'checkbox',
  1985. 'data-foo' => 'disabled'
  1986. ));
  1987. $expected = array(
  1988. 'div' => array('class' => 'input checkbox'),
  1989. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  1990. array('input' => array(
  1991. 'type' => 'checkbox',
  1992. 'name' => 'data[User][disabled]',
  1993. 'value' => '1',
  1994. 'id' => 'UserDisabled',
  1995. 'data-foo' => 'disabled'
  1996. )),
  1997. 'label' => array('for' => 'UserDisabled'),
  1998. 'Disabled',
  1999. '/label',
  2000. '/div'
  2001. );
  2002. $this->assertTags($result, $expected);
  2003. }
  2004. /**
  2005. * test form->input() with time types.
  2006. *
  2007. */
  2008. public function testInputTime() {
  2009. extract($this->dateRegex);
  2010. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2011. $result = explode(':', $result);
  2012. $this->assertRegExp('/option value="23"/', $result[0]);
  2013. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2014. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2015. $result = explode(':', $result);
  2016. $this->assertRegExp('/option value="23"/', $result[0]);
  2017. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2018. $result = $this->Form->input('Model.field', array(
  2019. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2020. ));
  2021. $result = explode(':', $result);
  2022. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2023. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2024. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2025. $result = $this->Form->input('Model.field', array(
  2026. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2027. ));
  2028. $result = explode(':', $result);
  2029. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2030. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2031. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2032. $result = $this->Form->input('prueba', array(
  2033. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  2034. 'maxYear' => date('Y') + 1 ,'interval' => 15
  2035. ));
  2036. $result = explode(':', $result);
  2037. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2038. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2039. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2040. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2041. $result = $this->Form->input('Random.start_time', array(
  2042. 'type' => 'time',
  2043. 'selected' => '18:15'
  2044. ));
  2045. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2046. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2047. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2048. $result = $this->Form->input('published', array('type' => 'time'));
  2049. $now = strtotime('now');
  2050. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2051. $now = strtotime('2013-03-09 00:42:21');
  2052. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2053. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2054. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2055. }
  2056. /**
  2057. * Test interval + selected near the hour roll over.
  2058. *
  2059. * @return void
  2060. */
  2061. public function testTimeSelectedWithInterval() {
  2062. $result = $this->Form->input('Model.start_time', array(
  2063. 'type' => 'time',
  2064. 'interval' => 15,
  2065. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2066. ));
  2067. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2068. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2069. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2070. $result = $this->Form->input('Model.start_time', array(
  2071. 'type' => 'time',
  2072. 'interval' => 15,
  2073. 'selected' => '2012-10-23 15:57:00'
  2074. ));
  2075. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2076. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2077. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2078. $result = $this->Form->input('Model.start_time', array(
  2079. 'timeFormat' => 24,
  2080. 'type' => 'time',
  2081. 'interval' => 15,
  2082. 'selected' => '15:57'
  2083. ));
  2084. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2085. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2086. $result = $this->Form->input('Model.start_time', array(
  2087. 'timeFormat' => 24,
  2088. 'type' => 'time',
  2089. 'interval' => 15,
  2090. 'selected' => '23:57'
  2091. ));
  2092. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2093. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2094. $result = $this->Form->input('Model.created', array(
  2095. 'timeFormat' => 24,
  2096. 'type' => 'datetime',
  2097. 'interval' => 15,
  2098. 'selected' => '2012-09-30 23:56'
  2099. ));
  2100. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2101. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2102. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2103. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2104. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2105. }
  2106. /**
  2107. * Test interval & timeFormat = 12
  2108. *
  2109. * @return void
  2110. */
  2111. public function testInputTimeWithIntervalAnd12HourFormat() {
  2112. $result = $this->Form->input('Model.start_time', array(
  2113. 'type' => 'time',
  2114. 'timeFormat' => 12,
  2115. 'interval' => 5,
  2116. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2117. ));
  2118. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2119. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2120. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2121. $result = $this->Form->input('Model.start_time', array(
  2122. 'type' => 'time',
  2123. 'timeFormat' => '12',
  2124. 'interval' => 5,
  2125. 'selected' => '2013-04-19 16:30:00'
  2126. ));
  2127. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2128. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2129. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2130. $result = $this->Form->input('Model.start_time', array(
  2131. 'type' => 'time',
  2132. 'timeFormat' => '12',
  2133. 'interval' => 10,
  2134. 'selected' => '2013-05-19 00:33:00'
  2135. ));
  2136. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2137. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2138. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2139. $result = $this->Form->input('Model.start_time', array(
  2140. 'type' => 'time',
  2141. 'timeFormat' => '12',
  2142. 'interval' => 10,
  2143. 'selected' => '2013-05-19 13:33:00'
  2144. ));
  2145. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2146. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2147. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2148. $result = $this->Form->input('Model.start_time', array(
  2149. 'type' => 'time',
  2150. 'timeFormat' => '12',
  2151. 'interval' => 10,
  2152. 'selected' => '2013-05-19 01:33:00'
  2153. ));
  2154. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2155. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2156. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2157. }
  2158. /**
  2159. * test form->input() with datetime, date and time types
  2160. *
  2161. * @return void
  2162. */
  2163. public function testInputDatetime() {
  2164. extract($this->dateRegex);
  2165. $result = $this->Form->input('prueba', array(
  2166. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2167. 'maxYear' => date('Y') + 1, 'interval' => 15
  2168. ));
  2169. $result = explode(':', $result);
  2170. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2171. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2172. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2173. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2174. //related to ticket #5013
  2175. $result = $this->Form->input('Contact.date', array(
  2176. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2177. ));
  2178. $this->assertRegExp('/class="customClass"/', $result);
  2179. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2180. $result = $this->Form->input('Contact.date', array(
  2181. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2182. ));
  2183. $this->assertRegExp('/id="customIdDay"/', $result);
  2184. $this->assertRegExp('/id="customIdMonth"/', $result);
  2185. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2186. $result = $this->Form->input('Model.field', array(
  2187. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2188. ));
  2189. $this->assertRegExp('/id="customIDDay"/', $result);
  2190. $this->assertRegExp('/id="customIDHour"/', $result);
  2191. $result = explode('</select><select', $result);
  2192. $result = explode(':', $result[1]);
  2193. $this->assertRegExp('/option value="23"/', $result[0]);
  2194. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2195. $result = $this->Form->input('Model.field', array(
  2196. 'type' => 'datetime', 'timeFormat' => 12
  2197. ));
  2198. $result = explode('</select><select', $result);
  2199. $result = explode(':', $result[1]);
  2200. $this->assertRegExp('/option value="12"/', $result[0]);
  2201. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2202. $this->Form->request->data = array('Contact' => array('created' => null));
  2203. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2204. $expected = array(
  2205. 'div' => array('class' => 'input date'),
  2206. 'label' => array('for' => 'ContactCreatedMonth'),
  2207. 'Created',
  2208. '/label',
  2209. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2210. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2211. $monthsRegex,
  2212. '/select', '-',
  2213. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2214. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2215. $daysRegex,
  2216. '/select', '-',
  2217. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2218. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2219. $yearsRegex,
  2220. '/select',
  2221. '/div'
  2222. );
  2223. $this->assertTags($result, $expected);
  2224. $this->Form->request->data = array('Contact' => array('created' => null));
  2225. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2226. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2227. $this->Form->request->data = array('Contact' => array('created' => null));
  2228. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2229. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2230. $result = $this->Form->input('Contact.created', array(
  2231. 'type' => 'date',
  2232. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2233. 'timeFormat' => 'NONE'
  2234. ));
  2235. $this->assertRegExp('/for\="created-month"/', $result);
  2236. }
  2237. /**
  2238. * Test generating checkboxes in a loop.
  2239. *
  2240. * @return void
  2241. */
  2242. public function testInputCheckboxesInLoop() {
  2243. for ($i = 1; $i < 5; $i++) {
  2244. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2245. $expected = array(
  2246. 'div' => array('class' => 'input checkbox'),
  2247. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2248. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2249. 'label' => array('for' => "Contact{$i}Email"),
  2250. 'Email',
  2251. '/label',
  2252. '/div'
  2253. );
  2254. $this->assertTags($result, $expected);
  2255. }
  2256. }
  2257. /**
  2258. * Test generating checkboxes with disabled elements.
  2259. *
  2260. * @return void
  2261. */
  2262. public function testInputCheckboxWithDisabledElements() {
  2263. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2264. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2265. $expected = array(
  2266. array('div' => array('class' => 'input select')),
  2267. array('label' => array('for' => "ContactMultiple")),
  2268. 'Multiple',
  2269. '/label',
  2270. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2271. array('div' => array('class' => 'checkbox')),
  2272. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2273. array('label' => array('for' => "ContactMultiple1")),
  2274. 'One',
  2275. '/label',
  2276. '/div',
  2277. array('div' => array('class' => 'checkbox')),
  2278. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2279. array('label' => array('for' => "ContactMultiple2")),
  2280. 'Two',
  2281. '/label',
  2282. '/div',
  2283. array('div' => array('class' => 'checkbox')),
  2284. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2285. array('label' => array('for' => "ContactMultiple3")),
  2286. 'Three',
  2287. '/label',
  2288. '/div',
  2289. '/div'
  2290. );
  2291. $this->assertTags($result, $expected);
  2292. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2293. $this->assertTags($result, $expected);
  2294. $disabled = array('2', 3);
  2295. $expected = array(
  2296. array('div' => array('class' => 'input select')),
  2297. array('label' => array('for' => "ContactMultiple")),
  2298. 'Multiple',
  2299. '/label',
  2300. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2301. array('div' => array('class' => 'checkbox')),
  2302. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2303. array('label' => array('for' => "ContactMultiple1")),
  2304. 'One',
  2305. '/label',
  2306. '/div',
  2307. array('div' => array('class' => 'checkbox')),
  2308. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2309. array('label' => array('for' => "ContactMultiple2")),
  2310. 'Two',
  2311. '/label',
  2312. '/div',
  2313. array('div' => array('class' => 'checkbox')),
  2314. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2315. array('label' => array('for' => "ContactMultiple3")),
  2316. 'Three',
  2317. '/label',
  2318. '/div',
  2319. '/div'
  2320. );
  2321. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2322. $this->assertTags($result, $expected);
  2323. // make sure 50 does only disable 50, and not 50f5c0cf
  2324. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2325. $disabled = array(50);
  2326. $expected = array(
  2327. array('div' => array('class' => 'input select')),
  2328. array('label' => array('for' => "ContactMultiple")),
  2329. 'Multiple',
  2330. '/label',
  2331. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2332. array('div' => array('class' => 'checkbox')),
  2333. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2334. array('label' => array('for' => "ContactMultiple50")),
  2335. 'Fifty',
  2336. '/label',
  2337. '/div',
  2338. array('div' => array('class' => 'checkbox')),
  2339. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2340. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2341. 'Stringy',
  2342. '/label',
  2343. '/div',
  2344. '/div'
  2345. );
  2346. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2347. $this->assertTags($result, $expected);
  2348. }
  2349. /**
  2350. * test input name with leading integer, ensure attributes are generated correctly.
  2351. *
  2352. * @return void
  2353. */
  2354. public function testInputWithLeadingInteger() {
  2355. $result = $this->Form->text('0.Node.title');
  2356. $expected = array(
  2357. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2358. );
  2359. $this->assertTags($result, $expected);
  2360. }
  2361. /**
  2362. * test form->input() with select type inputs.
  2363. *
  2364. * @return void
  2365. */
  2366. public function testInputSelectType() {
  2367. $result = $this->Form->input('email', array(
  2368. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2369. );
  2370. $expected = array(
  2371. 'div' => array('class' => 'input select'),
  2372. 'label' => array('for' => 'email'),
  2373. 'Email',
  2374. '/label',
  2375. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2376. array('option' => array('value' => '')),
  2377. '/option',
  2378. array('option' => array('value' => 'è')),
  2379. 'Firést',
  2380. '/option',
  2381. array('option' => array('value' => 'é')),
  2382. 'Secoènd',
  2383. '/option',
  2384. '/select',
  2385. '/div'
  2386. );
  2387. $this->assertTags($result, $expected);
  2388. $result = $this->Form->input('email', array(
  2389. 'options' => array('First', 'Second'), 'empty' => true)
  2390. );
  2391. $expected = array(
  2392. 'div' => array('class' => 'input select'),
  2393. 'label' => array('for' => 'email'),
  2394. 'Email',
  2395. '/label',
  2396. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2397. array('option' => array('value' => '')),
  2398. '/option',
  2399. array('option' => array('value' => '0')),
  2400. 'First',
  2401. '/option',
  2402. array('option' => array('value' => '1')),
  2403. 'Second',
  2404. '/option',
  2405. '/select',
  2406. '/div'
  2407. );
  2408. $this->assertTags($result, $expected);
  2409. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2410. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2411. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2412. $expected = array(
  2413. 'div' => array('class' => 'input select'),
  2414. 'label' => array('for' => 'ModelUserId'),
  2415. 'User',
  2416. '/label',
  2417. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2418. array('option' => array('value' => '')),
  2419. '/option',
  2420. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2421. 'good',
  2422. '/option',
  2423. array('option' => array('value' => 'other')),
  2424. 'bad',
  2425. '/option',
  2426. '/select',
  2427. '/div'
  2428. );
  2429. $this->assertTags($result, $expected);
  2430. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2431. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2432. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2433. $expected = array(
  2434. 'div' => array('class' => 'input select'),
  2435. 'label' => array('for' => 'ThingUserId'),
  2436. 'User',
  2437. '/label',
  2438. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2439. array('option' => array('value' => '')),
  2440. 'Some Empty',
  2441. '/option',
  2442. array('option' => array('value' => 'value')),
  2443. 'good',
  2444. '/option',
  2445. array('option' => array('value' => 'other')),
  2446. 'bad',
  2447. '/option',
  2448. '/select',
  2449. '/div'
  2450. );
  2451. $this->assertTags($result, $expected);
  2452. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2453. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2454. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2455. $expected = array(
  2456. 'div' => array('class' => 'input select'),
  2457. 'label' => array('for' => 'ThingUserId'),
  2458. 'User',
  2459. '/label',
  2460. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2461. array('option' => array('value' => '')),
  2462. 'Some Empty',
  2463. '/option',
  2464. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2465. 'good',
  2466. '/option',
  2467. array('option' => array('value' => 'other')),
  2468. 'bad',
  2469. '/option',
  2470. '/select',
  2471. '/div'
  2472. );
  2473. $this->assertTags($result, $expected);
  2474. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2475. $this->Form->request->data = array('User' => array('User' => array('value')));
  2476. $result = $this->Form->input('User.User', array('empty' => true));
  2477. $expected = array(
  2478. 'div' => array('class' => 'input select'),
  2479. 'label' => array('for' => 'UserUser'),
  2480. 'User',
  2481. '/label',
  2482. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2483. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2484. array('option' => array('value' => '')),
  2485. '/option',
  2486. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2487. 'good',
  2488. '/option',
  2489. array('option' => array('value' => 'other')),
  2490. 'bad',
  2491. '/option',
  2492. '/select',
  2493. '/div'
  2494. );
  2495. $this->assertTags($result, $expected);
  2496. $this->Form->data = array();
  2497. $result = $this->Form->input('Publisher.id', array(
  2498. 'label' => 'Publisher',
  2499. 'type' => 'select',
  2500. 'multiple' => 'checkbox',
  2501. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2502. ));
  2503. $expected = array(
  2504. array('div' => array('class' => 'input select')),
  2505. array('label' => array('for' => 'PublisherId')),
  2506. 'Publisher',
  2507. '/label',
  2508. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2509. array('div' => array('class' => 'checkbox')),
  2510. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2511. array('label' => array('for' => 'PublisherIdValue1')),
  2512. 'Label 1',
  2513. '/label',
  2514. '/div',
  2515. array('div' => array('class' => 'checkbox')),
  2516. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2517. array('label' => array('for' => 'PublisherIdValue2')),
  2518. 'Label 2',
  2519. '/label',
  2520. '/div',
  2521. '/div'
  2522. );
  2523. $this->assertTags($result, $expected);
  2524. }
  2525. /**
  2526. * test that input() and a non standard primary key makes a hidden input by default.
  2527. *
  2528. * @return void
  2529. */
  2530. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2531. $this->Form->create('User');
  2532. $this->Form->fieldset = array(
  2533. 'User' => array(
  2534. 'fields' => array(
  2535. 'model_id' => array('type' => 'integer')
  2536. ),
  2537. 'validates' => array(),
  2538. 'key' => 'model_id'
  2539. )
  2540. );
  2541. $result = $this->Form->input('model_id');
  2542. $expected = array(
  2543. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2544. );
  2545. $this->assertTags($result, $expected);
  2546. }
  2547. /**
  2548. * test that overriding the magic select type widget is possible
  2549. *
  2550. * @return void
  2551. */
  2552. public function testInputOverridingMagicSelectType() {
  2553. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2554. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2555. $expected = array(
  2556. 'div' => array('class' => 'input text'),
  2557. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2558. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2559. '/div'
  2560. );
  2561. $this->assertTags($result, $expected);
  2562. //Check that magic types still work for plural/singular vars
  2563. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2564. $result = $this->Form->input('Model.type');
  2565. $expected = array(
  2566. 'div' => array('class' => 'input select'),
  2567. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2568. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2569. array('option' => array('value' => 'value')), 'good', '/option',
  2570. array('option' => array('value' => 'other')), 'bad', '/option',
  2571. '/select',
  2572. '/div'
  2573. );
  2574. $this->assertTags($result, $expected);
  2575. }
  2576. /**
  2577. * Test that magic input() selects are created for type=number
  2578. *
  2579. * @return void
  2580. */
  2581. public function testInputMagicSelectForTypeNumber() {
  2582. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2583. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2584. $result = $this->Form->input('ValidateUser.balance');
  2585. $expected = array(
  2586. 'div' => array('class' => 'input select'),
  2587. 'label' => array('for' => 'ValidateUserBalance'),
  2588. 'Balance',
  2589. '/label',
  2590. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2591. array('option' => array('value' => '0')),
  2592. 'nothing',
  2593. '/option',
  2594. array('option' => array('value' => '1', 'selected' => 'selected')),
  2595. 'some',
  2596. '/option',
  2597. array('option' => array('value' => '100')),
  2598. 'a lot',
  2599. '/option',
  2600. '/select',
  2601. '/div'
  2602. );
  2603. $this->assertTags($result, $expected);
  2604. }
  2605. /**
  2606. * Test that magic input() selects can easily be converted into radio types without error.
  2607. *
  2608. * @return void
  2609. */
  2610. public function testInputMagicSelectChangeToRadio() {
  2611. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2612. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2613. $this->assertRegExp('/input type="radio"/', $result);
  2614. }
  2615. /**
  2616. * fields with the same name as the model should work.
  2617. *
  2618. * @return void
  2619. */
  2620. public function testInputWithMatchingFieldAndModelName() {
  2621. $this->Form->create('User');
  2622. $this->Form->fieldset = array(
  2623. 'User' => array(
  2624. 'fields' => array(
  2625. 'User' => array('type' => 'text')
  2626. ),
  2627. 'validates' => array(),
  2628. 'key' => 'id'
  2629. )
  2630. );
  2631. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2632. $result = $this->Form->input('User', array('type' => 'text'));
  2633. $expected = array(
  2634. 'div' => array('class' => 'input text'),
  2635. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2636. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2637. '/div'
  2638. );
  2639. $this->assertTags($result, $expected);
  2640. }
  2641. /**
  2642. * testFormInputs method
  2643. *
  2644. * test correct results from form::inputs().
  2645. *
  2646. * @return void
  2647. */
  2648. public function testFormInputs() {
  2649. $this->Form->create('Contact');
  2650. $result = $this->Form->inputs('The Legend');
  2651. $expected = array(
  2652. '<fieldset',
  2653. '<legend',
  2654. 'The Legend',
  2655. '/legend',
  2656. '*/fieldset',
  2657. );
  2658. $this->assertTags($result, $expected);
  2659. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2660. $expected = array(
  2661. 'fieldset' => array('class' => 'classy-stuff'),
  2662. '<legend',
  2663. 'Field of Dreams',
  2664. '/legend',
  2665. '*/fieldset'
  2666. );
  2667. $this->assertTags($result, $expected);
  2668. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2669. $this->assertTags($result, $expected);
  2670. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2671. $this->assertTags($result, $expected);
  2672. $this->Form->create('Contact');
  2673. $this->Form->request['prefix'] = 'admin';
  2674. $this->Form->request['action'] = 'admin_edit';
  2675. $result = $this->Form->inputs();
  2676. $expected = array(
  2677. '<fieldset',
  2678. '<legend',
  2679. 'Edit Contact',
  2680. '/legend',
  2681. '*/fieldset',
  2682. );
  2683. $this->assertTags($result, $expected);
  2684. $this->Form->create('Contact');
  2685. $result = $this->Form->inputs(false);
  2686. $expected = array(
  2687. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2688. array('div' => array('class' => 'input text')),
  2689. '*/div',
  2690. array('div' => array('class' => 'input email')),
  2691. '*/div',
  2692. array('div' => array('class' => 'input tel')),
  2693. '*/div',
  2694. array('div' => array('class' => 'input password')),
  2695. '*/div',
  2696. array('div' => array('class' => 'input date')),
  2697. '*/div',
  2698. array('div' => array('class' => 'input date')),
  2699. '*/div',
  2700. array('div' => array('class' => 'input datetime')),
  2701. '*/div',
  2702. array('div' => array('class' => 'input number')),
  2703. '*/div',
  2704. array('div' => array('class' => 'input select')),
  2705. '*/div',
  2706. );
  2707. $this->assertTags($result, $expected);
  2708. $this->Form->create('Contact');
  2709. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2710. $expected = array(
  2711. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2712. array('div' => array('class' => 'input text')),
  2713. '*/div',
  2714. array('div' => array('class' => 'input email')),
  2715. '*/div',
  2716. array('div' => array('class' => 'input tel')),
  2717. '*/div',
  2718. array('div' => array('class' => 'input password')),
  2719. '*/div',
  2720. array('div' => array('class' => 'input date')),
  2721. '*/div',
  2722. array('div' => array('class' => 'input date')),
  2723. '*/div',
  2724. array('div' => array('class' => 'input datetime')),
  2725. '*/div',
  2726. array('div' => array('class' => 'input number')),
  2727. '*/div',
  2728. array('div' => array('class' => 'input select')),
  2729. '*/div',
  2730. );
  2731. $this->assertTags($result, $expected);
  2732. $this->Form->create('Contact');
  2733. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2734. $this->assertTags($result, $expected);
  2735. $this->Form->create('Contact');
  2736. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2737. $expected = array(
  2738. 'fieldset' => array(),
  2739. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2740. array('div' => array('class' => 'input text')),
  2741. '*/div',
  2742. array('div' => array('class' => 'input email')),
  2743. '*/div',
  2744. array('div' => array('class' => 'input tel')),
  2745. '*/div',
  2746. array('div' => array('class' => 'input password')),
  2747. '*/div',
  2748. array('div' => array('class' => 'input date')),
  2749. '*/div',
  2750. array('div' => array('class' => 'input date')),
  2751. '*/div',
  2752. array('div' => array('class' => 'input datetime')),
  2753. '*/div',
  2754. array('div' => array('class' => 'input number')),
  2755. '*/div',
  2756. array('div' => array('class' => 'input select')),
  2757. '*/div',
  2758. '/fieldset'
  2759. );
  2760. $this->assertTags($result, $expected);
  2761. $this->Form->create('Contact');
  2762. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2763. $expected = array(
  2764. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2765. array('div' => array('class' => 'input text')),
  2766. '*/div',
  2767. array('div' => array('class' => 'input email')),
  2768. '*/div',
  2769. array('div' => array('class' => 'input tel')),
  2770. '*/div',
  2771. array('div' => array('class' => 'input password')),
  2772. '*/div',
  2773. array('div' => array('class' => 'input date')),
  2774. '*/div',
  2775. array('div' => array('class' => 'input date')),
  2776. '*/div',
  2777. array('div' => array('class' => 'input datetime')),
  2778. '*/div',
  2779. array('div' => array('class' => 'input number')),
  2780. '*/div',
  2781. array('div' => array('class' => 'input select')),
  2782. '*/div',
  2783. );
  2784. $this->assertTags($result, $expected);
  2785. $this->Form->create('Contact');
  2786. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2787. $this->assertTags($result, $expected);
  2788. $this->Form->create('Contact');
  2789. $result = $this->Form->inputs('Hello');
  2790. $expected = array(
  2791. 'fieldset' => array(),
  2792. 'legend' => array(),
  2793. 'Hello',
  2794. '/legend',
  2795. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2796. array('div' => array('class' => 'input text')),
  2797. '*/div',
  2798. array('div' => array('class' => 'input email')),
  2799. '*/div',
  2800. array('div' => array('class' => 'input tel')),
  2801. '*/div',
  2802. array('div' => array('class' => 'input password')),
  2803. '*/div',
  2804. array('div' => array('class' => 'input date')),
  2805. '*/div',
  2806. array('div' => array('class' => 'input date')),
  2807. '*/div',
  2808. array('div' => array('class' => 'input datetime')),
  2809. '*/div',
  2810. array('div' => array('class' => 'input number')),
  2811. '*/div',
  2812. array('div' => array('class' => 'input select')),
  2813. '*/div',
  2814. '/fieldset'
  2815. );
  2816. $this->assertTags($result, $expected);
  2817. $this->Form->create('Contact');
  2818. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2819. $expected = array(
  2820. 'fieldset' => array(),
  2821. 'legend' => array(),
  2822. 'Hello',
  2823. '/legend',
  2824. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2825. array('div' => array('class' => 'input text')),
  2826. '*/div',
  2827. array('div' => array('class' => 'input email')),
  2828. '*/div',
  2829. array('div' => array('class' => 'input tel')),
  2830. '*/div',
  2831. array('div' => array('class' => 'input password')),
  2832. '*/div',
  2833. array('div' => array('class' => 'input date')),
  2834. '*/div',
  2835. array('div' => array('class' => 'input date')),
  2836. '*/div',
  2837. array('div' => array('class' => 'input datetime')),
  2838. '*/div',
  2839. array('div' => array('class' => 'input number')),
  2840. '*/div',
  2841. array('div' => array('class' => 'input select')),
  2842. '*/div',
  2843. '/fieldset'
  2844. );
  2845. $this->assertTags($result, $expected);
  2846. $this->Form->create('Contact');
  2847. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2848. $this->assertTags($result, $expected);
  2849. $this->Form->end();
  2850. $this->Form->create(false);
  2851. $expected = array(
  2852. 'fieldset' => array(),
  2853. array('div' => array('class' => 'input text')),
  2854. 'label' => array('for' => 'foo'),
  2855. 'Foo',
  2856. '/label',
  2857. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2858. '*/div',
  2859. '/fieldset'
  2860. );
  2861. $result = $this->Form->inputs(
  2862. array('foo' => array('type' => 'text')),
  2863. array(),
  2864. array('legend' => false)
  2865. );
  2866. $this->assertTags($result, $expected);
  2867. }
  2868. /**
  2869. * Tests inputs() works with plugin models
  2870. *
  2871. * @return void
  2872. */
  2873. public function testInputsPluginModel() {
  2874. $this->loadFixtures('Post');
  2875. App::build(array(
  2876. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2877. ));
  2878. CakePlugin::load('TestPlugin');
  2879. $this->Form->request['models'] = array(
  2880. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2881. );
  2882. $this->Form->create('TestPlugin.TestPluginPost');
  2883. $result = $this->Form->inputs();
  2884. $this->assertContains('data[TestPluginPost][id]', $result);
  2885. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2886. $this->assertContains('data[TestPluginPost][title]', $result);
  2887. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2888. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2889. $this->assertEquals('TestPluginPost', $this->Form->model());
  2890. }
  2891. /**
  2892. * testSelectAsCheckbox method
  2893. *
  2894. * test multi-select widget with checkbox formatting.
  2895. *
  2896. * @return void
  2897. */
  2898. public function testSelectAsCheckbox() {
  2899. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2900. $expected = array(
  2901. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2902. array('div' => array('class' => 'checkbox')),
  2903. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2904. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2905. 'first',
  2906. '/label',
  2907. '/div',
  2908. array('div' => array('class' => 'checkbox')),
  2909. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2910. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2911. 'second',
  2912. '/label',
  2913. '/div',
  2914. array('div' => array('class' => 'checkbox')),
  2915. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2916. array('label' => array('for' => 'ModelMultiField2')),
  2917. 'third',
  2918. '/label',
  2919. '/div',
  2920. );
  2921. $this->assertTags($result, $expected);
  2922. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2923. $expected = array(
  2924. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2925. array('div' => array('class' => 'checkbox')),
  2926. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2927. array('label' => array('for' => 'ModelMultiField12')),
  2928. 'half',
  2929. '/label',
  2930. '/div',
  2931. );
  2932. $this->assertTags($result, $expected);
  2933. }
  2934. /**
  2935. * testLabel method
  2936. *
  2937. * test label generation.
  2938. *
  2939. * @return void
  2940. */
  2941. public function testLabel() {
  2942. $this->Form->text('Person.name');
  2943. $result = $this->Form->label();
  2944. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2945. $this->Form->text('Person.name');
  2946. $result = $this->Form->label();
  2947. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2948. $result = $this->Form->label('Person.first_name');
  2949. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2950. $result = $this->Form->label('Person.first_name', 'Your first name');
  2951. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2952. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2953. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2954. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2955. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2956. $result = $this->Form->label('Person.first_name', '');
  2957. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2958. $result = $this->Form->label('Person.2.name', '');
  2959. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2960. }
  2961. /**
  2962. * testTextbox method
  2963. *
  2964. * test textbox element generation
  2965. *
  2966. * @return void
  2967. */
  2968. public function testTextbox() {
  2969. $result = $this->Form->text('Model.field');
  2970. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2971. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2972. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2973. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2974. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2975. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2976. $result = $this->Form->text('Model.text');
  2977. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2978. $Contact = ClassRegistry::getObject('Contact');
  2979. $Contact->validationErrors['text'] = array(true);
  2980. $this->Form->request->data['Contact']['text'] = 'test';
  2981. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2982. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2983. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2984. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2985. $expected = array(
  2986. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2987. );
  2988. $this->assertTags($result, $expected);
  2989. }
  2990. /**
  2991. * testDefaultValue method
  2992. *
  2993. * Test default value setting
  2994. *
  2995. * @return void
  2996. */
  2997. public function testDefaultValue() {
  2998. $this->Form->request->data['Model']['field'] = 'test';
  2999. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3000. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3001. unset($this->Form->request->data['Model']['field']);
  3002. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3003. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3004. }
  3005. /**
  3006. * testCheckboxDefaultValue method
  3007. *
  3008. * Test default value setting on checkbox() method
  3009. *
  3010. * @return void
  3011. */
  3012. public function testCheckboxDefaultValue() {
  3013. $this->Form->request->data['Model']['field'] = false;
  3014. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3015. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3016. unset($this->Form->request->data['Model']['field']);
  3017. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3018. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3019. $this->Form->request->data['Model']['field'] = true;
  3020. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3021. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3022. unset($this->Form->request->data['Model']['field']);
  3023. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3024. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3025. }
  3026. /**
  3027. * testError method
  3028. *
  3029. * Test field error generation
  3030. *
  3031. * @return void
  3032. */
  3033. public function testError() {
  3034. $Contact = ClassRegistry::getObject('Contact');
  3035. $Contact->validationErrors['field'] = array(1);
  3036. $result = $this->Form->error('Contact.field');
  3037. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3038. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3039. $this->assertEquals('Error in field Field', $result);
  3040. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3041. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3042. $this->assertEquals('This field contains invalid input', $result);
  3043. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3044. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3045. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3046. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3047. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3048. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3049. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3050. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3051. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3052. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3053. $this->assertEquals('<strong>Badness!</strong>', $result);
  3054. $Contact->validationErrors['field'] = array("email");
  3055. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3056. $expected = array(
  3057. 'div' => array('class' => 'field-error'),
  3058. 'No good!',
  3059. '/div'
  3060. );
  3061. $this->assertTags($result, $expected);
  3062. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3063. $result = $this->Form->error('Contact.field', array(
  3064. 'notEmpty' => 'Cannot be empty',
  3065. 'email' => 'No good!'
  3066. ));
  3067. $expected = array(
  3068. 'div' => array('class' => 'error-message'),
  3069. 'ul' => array(),
  3070. '<li', 'Cannot be empty', '/li',
  3071. '<li', 'No good!', '/li',
  3072. '<li', 'Something else', '/li',
  3073. '/ul',
  3074. '/div'
  3075. );
  3076. $this->assertTags($result, $expected);
  3077. // Testing error messages list options
  3078. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3079. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3080. $expected = array(
  3081. 'div' => array('class' => 'error-message'),
  3082. 'ol' => array(),
  3083. '<li', 'notEmpty', '/li',
  3084. '<li', 'email', '/li',
  3085. '/ol',
  3086. '/div'
  3087. );
  3088. $this->assertTags($result, $expected);
  3089. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3090. $expected = array(
  3091. 'div' => array('class' => 'error-message'),
  3092. 'ol' => array(),
  3093. '<li', 'notEmpty', '/li',
  3094. '<li', 'email', '/li',
  3095. '/ol',
  3096. '/div'
  3097. );
  3098. $this->assertTags($result, $expected);
  3099. $result = $this->Form->error('Contact.field', null, array(
  3100. 'listOptions' => array(
  3101. 'class' => 'ul-class',
  3102. 'itemOptions' => array(
  3103. 'class' => 'li-class'
  3104. )
  3105. )
  3106. ));
  3107. $expected = array(
  3108. 'div' => array('class' => 'error-message'),
  3109. 'ul' => array('class' => 'ul-class'),
  3110. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3111. array('li' => array('class' => 'li-class')), 'email', '/li',
  3112. '/ul',
  3113. '/div'
  3114. );
  3115. $this->assertTags($result, $expected);
  3116. }
  3117. /**
  3118. * test error options when using form->input();
  3119. *
  3120. * @return void
  3121. */
  3122. public function testInputErrorEscape() {
  3123. $this->Form->create('ValidateProfile');
  3124. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3125. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3126. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  3127. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3128. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  3129. $this->assertRegExp('/required<br>/', $result);
  3130. }
  3131. /**
  3132. * testPassword method
  3133. *
  3134. * Test password element generation
  3135. *
  3136. * @return void
  3137. */
  3138. public function testPassword() {
  3139. $Contact = ClassRegistry::getObject('Contact');
  3140. $result = $this->Form->password('Contact.field');
  3141. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3142. $Contact->validationErrors['passwd'] = 1;
  3143. $this->Form->request->data['Contact']['passwd'] = 'test';
  3144. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3145. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3146. }
  3147. /**
  3148. * testRadio method
  3149. *
  3150. * Test radio element set generation
  3151. *
  3152. * @return void
  3153. */
  3154. public function testRadio() {
  3155. $result = $this->Form->radio('Model.field', array('option A'));
  3156. $expected = array(
  3157. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3158. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3159. 'label' => array('for' => 'ModelField0'),
  3160. 'option A',
  3161. '/label'
  3162. );
  3163. $this->assertTags($result, $expected);
  3164. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3165. $expected = array(
  3166. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3167. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3168. 'label' => array('for' => 'ModelField12'),
  3169. 'half',
  3170. '/label'
  3171. );
  3172. $this->assertTags($result, $expected);
  3173. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3174. $expected = array(
  3175. 'fieldset' => array(),
  3176. 'legend' => array(),
  3177. 'Field',
  3178. '/legend',
  3179. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3180. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3181. array('label' => array('for' => 'ModelField0')),
  3182. 'option A',
  3183. '/label',
  3184. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3185. array('label' => array('for' => 'ModelField1')),
  3186. 'option B',
  3187. '/label',
  3188. '/fieldset'
  3189. );
  3190. $this->assertTags($result, $expected);
  3191. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3192. $expected = array(
  3193. 'fieldset' => array(),
  3194. 'legend' => array(),
  3195. 'Field',
  3196. '/legend',
  3197. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3198. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3199. array('label' => array('for' => 'ModelField0')),
  3200. 'option A',
  3201. '/label',
  3202. 'br' => array(),
  3203. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3204. array('label' => array('for' => 'ModelField1')),
  3205. 'option B',
  3206. '/label',
  3207. '/fieldset'
  3208. );
  3209. $this->assertTags($result, $expected);
  3210. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3211. $expected = array(
  3212. 'fieldset' => array(),
  3213. 'legend' => array(),
  3214. 'Gender',
  3215. '/legend',
  3216. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3217. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3218. array('label' => array('for' => 'EmployeeGenderMale')),
  3219. 'Male',
  3220. '/label',
  3221. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3222. array('label' => array('for' => 'EmployeeGenderFemale')),
  3223. 'Female',
  3224. '/label',
  3225. '/fieldset',
  3226. );
  3227. $this->assertTags($result, $expected);
  3228. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3229. $expected = array(
  3230. 'fieldset' => array(),
  3231. 'legend' => array(),
  3232. 'Gender',
  3233. '/legend',
  3234. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3235. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3236. array('label' => array('for' => 'OfficerGenderMale')),
  3237. 'Male',
  3238. '/label',
  3239. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3240. array('label' => array('for' => 'OfficerGenderFemale')),
  3241. 'Female',
  3242. '/label',
  3243. '/fieldset',
  3244. );
  3245. $this->assertTags($result, $expected);
  3246. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3247. $expected = array(
  3248. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3249. array('input' => array(
  3250. 'type' => 'radio',
  3251. 'name' => 'data[Contact][1][imrequired]',
  3252. 'value' => '0',
  3253. 'id' => 'Contact1Imrequired0',
  3254. 'required' => 'required'
  3255. )),
  3256. 'label' => array('for' => 'Contact1Imrequired0'),
  3257. 'option A',
  3258. '/label'
  3259. );
  3260. $this->assertTags($result, $expected);
  3261. $result = $this->Form->radio('Model.1.field', array('option A'));
  3262. $expected = array(
  3263. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3264. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3265. 'label' => array('for' => 'Model1Field0'),
  3266. 'option A',
  3267. '/label'
  3268. );
  3269. $this->assertTags($result, $expected);
  3270. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3271. $expected = array(
  3272. 'fieldset' => array(),
  3273. 'legend' => array(),
  3274. 'Field',
  3275. '/legend',
  3276. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3277. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3278. array('label' => array('for' => 'ModelField0')),
  3279. 'option A',
  3280. '/label',
  3281. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3282. array('label' => array('for' => 'ModelField1')),
  3283. 'option B',
  3284. '/label',
  3285. '/fieldset'
  3286. );
  3287. $this->assertTags($result, $expected);
  3288. }
  3289. /**
  3290. * Test radio inputs with between as string or array. Also ensure
  3291. * that an array with less between elements works.
  3292. *
  3293. * @return void
  3294. */
  3295. public function testRadioBetween() {
  3296. $result = $this->Form->radio(
  3297. 'Model.field',
  3298. array('option A', 'option B'),
  3299. array('between' => 'I am between')
  3300. );
  3301. $expected = array(
  3302. 'fieldset' => array(),
  3303. 'legend' => array(),
  3304. 'Field',
  3305. '/legend',
  3306. 'I am between',
  3307. 'input' => array(
  3308. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3309. 'value' => '', 'id' => 'ModelField_'
  3310. ),
  3311. array('input' => array(
  3312. 'type' => 'radio', 'name' => 'data[Model][field]',
  3313. 'value' => '0', 'id' => 'ModelField0'
  3314. )),
  3315. array('label' => array('for' => 'ModelField0')),
  3316. 'option A',
  3317. '/label',
  3318. array('input' => array(
  3319. 'type' => 'radio', 'name' => 'data[Model][field]',
  3320. 'value' => '1', 'id' => 'ModelField1'
  3321. )),
  3322. array('label' => array('for' => 'ModelField1')),
  3323. 'option B',
  3324. '/label',
  3325. '/fieldset'
  3326. );
  3327. $this->assertTags($result, $expected);
  3328. $result = $this->Form->radio(
  3329. 'Model.field',
  3330. array('option A', 'option B', 'option C'),
  3331. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3332. );
  3333. $expected = array(
  3334. 'fieldset' => array(),
  3335. 'legend' => array(),
  3336. 'Field',
  3337. '/legend',
  3338. 'input' => array(
  3339. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3340. 'value' => '', 'id' => 'ModelField_'
  3341. ),
  3342. array('input' => array(
  3343. 'type' => 'radio', 'name' => 'data[Model][field]',
  3344. 'value' => '0', 'id' => 'ModelField0'
  3345. )),
  3346. array('label' => array('for' => 'ModelField0')),
  3347. 'option A',
  3348. '/label',
  3349. 'between A',
  3350. '--separator--',
  3351. array('input' => array(
  3352. 'type' => 'radio', 'name' => 'data[Model][field]',
  3353. 'value' => '1', 'id' => 'ModelField1'
  3354. )),
  3355. array('label' => array('for' => 'ModelField1')),
  3356. 'option B',
  3357. '/label',
  3358. 'between B',
  3359. '--separator--',
  3360. array('input' => array(
  3361. 'type' => 'radio', 'name' => 'data[Model][field]',
  3362. 'value' => '2', 'id' => 'ModelField2'
  3363. )),
  3364. array('label' => array('for' => 'ModelField2')),
  3365. 'option C',
  3366. '/label',
  3367. 'between C',
  3368. '/fieldset'
  3369. );
  3370. $this->assertTags($result, $expected);
  3371. $result = $this->Form->input('Model.field', array(
  3372. 'options' => array('1' => 'first', '2' => 'second'),
  3373. 'type' => 'radio',
  3374. 'before' => '--before--',
  3375. 'after' => '--after--',
  3376. 'separator' => '--separator--',
  3377. 'between' => array('--between first--', '--between second--')
  3378. ));
  3379. $expected = array(
  3380. 'div' => array('class' => 'input radio'),
  3381. '--before--',
  3382. 'fieldset' => array(),
  3383. 'legend' => array(),
  3384. 'Field',
  3385. '/legend',
  3386. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3387. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3388. array('label' => array('for' => 'ModelField1')),
  3389. 'first',
  3390. '/label',
  3391. '--between first--',
  3392. '--separator--',
  3393. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3394. array('label' => array('for' => 'ModelField2')),
  3395. 'second',
  3396. '/label',
  3397. '--between second--',
  3398. '/fieldset',
  3399. '--after--',
  3400. '/div'
  3401. );
  3402. $this->assertTags($result, $expected);
  3403. $result = $this->Form->input('Model.field', array(
  3404. 'options' => array('1' => 'first', '2' => 'second'),
  3405. 'type' => 'radio',
  3406. 'before' => '--before--',
  3407. 'after' => '--after--',
  3408. 'separator' => '--separator--',
  3409. 'between' => array('--between first--')
  3410. ));
  3411. $expected = array(
  3412. 'div' => array('class' => 'input radio'),
  3413. '--before--',
  3414. 'fieldset' => array(),
  3415. 'legend' => array(),
  3416. 'Field',
  3417. '/legend',
  3418. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3419. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3420. array('label' => array('for' => 'ModelField1')),
  3421. 'first',
  3422. '/label',
  3423. '--between first--',
  3424. '--separator--',
  3425. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3426. array('label' => array('for' => 'ModelField2')),
  3427. 'second',
  3428. '/label',
  3429. '/fieldset',
  3430. '--after--',
  3431. '/div'
  3432. );
  3433. $this->assertTags($result, $expected);
  3434. }
  3435. /**
  3436. * Test that radios with a 0 value are selected under the correct conditions.
  3437. * Also ensure that values that are booleanish are handled correctly.
  3438. *
  3439. * @return void
  3440. */
  3441. public function testRadioOptionWithBooleanishValues() {
  3442. $expected = array(
  3443. 'fieldset' => array(),
  3444. 'legend' => array(),
  3445. 'Field',
  3446. '/legend',
  3447. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3448. array('label' => array('for' => 'ModelField1')),
  3449. 'Yes',
  3450. '/label',
  3451. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3452. array('label' => array('for' => 'ModelField0')),
  3453. 'No',
  3454. '/label',
  3455. '/fieldset'
  3456. );
  3457. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3458. $this->assertTags($result, $expected);
  3459. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3460. $this->assertTags($result, $expected);
  3461. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3462. $this->assertTags($result, $expected);
  3463. $expected = array(
  3464. 'fieldset' => array(),
  3465. 'legend' => array(),
  3466. 'Field',
  3467. '/legend',
  3468. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3469. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3470. array('label' => array('for' => 'ModelField1')),
  3471. 'Yes',
  3472. '/label',
  3473. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3474. array('label' => array('for' => 'ModelField0')),
  3475. 'No',
  3476. '/label',
  3477. '/fieldset'
  3478. );
  3479. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3480. $this->assertTags($result, $expected);
  3481. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3482. $this->assertTags($result, $expected);
  3483. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3484. $this->assertTags($result, $expected);
  3485. $expected = array(
  3486. 'fieldset' => array(),
  3487. 'legend' => array(),
  3488. 'Field',
  3489. '/legend',
  3490. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3491. array('label' => array('for' => 'ModelField1')),
  3492. 'Yes',
  3493. '/label',
  3494. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3495. array('label' => array('for' => 'ModelField0')),
  3496. 'No',
  3497. '/label',
  3498. '/fieldset'
  3499. );
  3500. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3501. $this->assertTags($result, $expected);
  3502. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3503. $this->assertTags($result, $expected);
  3504. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3505. $this->assertTags($result, $expected);
  3506. }
  3507. /**
  3508. * test disabled radio options
  3509. *
  3510. * @return void
  3511. */
  3512. public function testRadioDisabled() {
  3513. $result = $this->Form->radio(
  3514. 'Model.field',
  3515. array('option A', 'option B'),
  3516. array('disabled' => array(0), 'value' => '0')
  3517. );
  3518. $expected = array(
  3519. 'fieldset' => array(),
  3520. 'legend' => array(),
  3521. 'Field',
  3522. '/legend',
  3523. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3524. array('label' => array('for' => 'ModelField0')),
  3525. 'option A',
  3526. '/label',
  3527. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3528. array('label' => array('for' => 'ModelField1')),
  3529. 'option B',
  3530. '/label',
  3531. '/fieldset'
  3532. );
  3533. $this->assertTags($result, $expected);
  3534. $result = $this->Form->radio(
  3535. 'Model.field',
  3536. array('option A', 'option B'),
  3537. array('disabled' => true, 'value' => '0')
  3538. );
  3539. $expected = array(
  3540. 'fieldset' => array(),
  3541. 'legend' => array(),
  3542. 'Field',
  3543. '/legend',
  3544. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3545. array('label' => array('for' => 'ModelField0')),
  3546. 'option A',
  3547. '/label',
  3548. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3549. array('label' => array('for' => 'ModelField1')),
  3550. 'option B',
  3551. '/label',
  3552. '/fieldset'
  3553. );
  3554. $this->assertTags($result, $expected);
  3555. $result = $this->Form->radio(
  3556. 'Model.field',
  3557. array('option A', 'option B'),
  3558. array('disabled' => 'disabled', 'value' => '0')
  3559. );
  3560. $expected = array(
  3561. 'fieldset' => array(),
  3562. 'legend' => array(),
  3563. 'Field',
  3564. '/legend',
  3565. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3566. array('label' => array('for' => 'ModelField0')),
  3567. 'option A',
  3568. '/label',
  3569. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3570. array('label' => array('for' => 'ModelField1')),
  3571. 'option B',
  3572. '/label',
  3573. '/fieldset'
  3574. );
  3575. $this->assertTags($result, $expected);
  3576. $result = $this->Form->input('Model.field', array(
  3577. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3578. 'type' => 'radio',
  3579. 'disabled' => array(2, '3x'),
  3580. ));
  3581. $expected = array(
  3582. 'div' => array('class' => 'input radio'),
  3583. 'fieldset' => array(),
  3584. 'legend' => array(),
  3585. 'Field',
  3586. '/legend',
  3587. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3588. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3589. array('label' => array('for' => 'ModelField1')),
  3590. 'first',
  3591. '/label',
  3592. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3593. array('label' => array('for' => 'ModelField2')),
  3594. 'second',
  3595. '/label',
  3596. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3597. array('label' => array('for' => 'ModelField2x')),
  3598. '2x',
  3599. '/label',
  3600. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3601. array('label' => array('for' => 'ModelField3')),
  3602. 'third',
  3603. '/label',
  3604. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3605. array('label' => array('for' => 'ModelField3x')),
  3606. '3x',
  3607. '/label',
  3608. '/fieldset',
  3609. '/div'
  3610. );
  3611. $this->assertTags($result, $expected);
  3612. }
  3613. /**
  3614. * test disabling the hidden input for radio buttons
  3615. *
  3616. * @return void
  3617. */
  3618. public function testRadioHiddenInputDisabling() {
  3619. $result = $this->Form->input('Model.1.field', array(
  3620. 'type' => 'radio',
  3621. 'options' => array('option A'),
  3622. 'hiddenField' => false
  3623. )
  3624. );
  3625. $expected = array(
  3626. 'div' => array('class' => 'input radio'),
  3627. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3628. 'label' => array('for' => 'Model1Field0'),
  3629. 'option A',
  3630. '/label',
  3631. '/div'
  3632. );
  3633. $this->assertTags($result, $expected);
  3634. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3635. $expected = array(
  3636. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3637. 'label' => array('for' => 'Model1Field0'),
  3638. 'option A',
  3639. '/label'
  3640. );
  3641. $this->assertTags($result, $expected);
  3642. }
  3643. /**
  3644. * test adding an empty option for radio buttons
  3645. *
  3646. * @return void
  3647. */
  3648. public function testRadioAddEmptyOption() {
  3649. $result = $this->Form->input('Model.1.field', array(
  3650. 'type' => 'radio',
  3651. 'options' => array('option A'),
  3652. 'empty' => true,
  3653. 'hiddenField' => false
  3654. ));
  3655. $expected = array(
  3656. 'div' => array('class' => 'input radio'),
  3657. 'fieldset' => array(),
  3658. 'legend' => array(),
  3659. 'Field',
  3660. '/legend',
  3661. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3662. array('label' => array('for' => 'Model1Field')),
  3663. __('empty'),
  3664. '/label',
  3665. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3666. array('label' => array('for' => 'Model1Field0')),
  3667. 'option A',
  3668. '/label',
  3669. '/fieldset',
  3670. '/div'
  3671. );
  3672. $this->assertTags($result, $expected);
  3673. $result = $this->Form->input('Model.1.field', array(
  3674. 'type' => 'radio',
  3675. 'options' => array('option A'),
  3676. 'empty' => 'CustomEmptyLabel',
  3677. 'hiddenField' => false
  3678. ));
  3679. $expected = array(
  3680. 'div' => array('class' => 'input radio'),
  3681. 'fieldset' => array(),
  3682. 'legend' => array(),
  3683. 'Field',
  3684. '/legend',
  3685. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3686. array('label' => array('for' => 'Model1Field')),
  3687. 'CustomEmptyLabel',
  3688. '/label',
  3689. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3690. array('label' => array('for' => 'Model1Field0')),
  3691. 'option A',
  3692. '/label',
  3693. '/fieldset',
  3694. '/div'
  3695. );
  3696. $this->assertTags($result, $expected);
  3697. $result = $this->Form->input('Model.1.field', array(
  3698. 'type' => 'radio',
  3699. 'options' => array('option A'),
  3700. 'empty' => false,
  3701. 'hiddenField' => false
  3702. ));
  3703. $this->assertTextNotContains('"Model1Field"', $result);
  3704. }
  3705. /**
  3706. * testSelect method
  3707. *
  3708. * Test select element generation.
  3709. *
  3710. * @return void
  3711. */
  3712. public function testSelect() {
  3713. $result = $this->Form->select('Model.field', array());
  3714. $expected = array(
  3715. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3716. array('option' => array('value' => '')),
  3717. '/option',
  3718. '/select'
  3719. );
  3720. $this->assertTags($result, $expected);
  3721. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3722. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3723. $expected = array(
  3724. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3725. array('option' => array('value' => '')),
  3726. '/option',
  3727. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3728. 'good',
  3729. '/option',
  3730. array('option' => array('value' => 'other')),
  3731. 'bad',
  3732. '/option',
  3733. '/select'
  3734. );
  3735. $this->assertTags($result, $expected);
  3736. $this->Form->request->data = array();
  3737. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3738. $expected = array(
  3739. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3740. array('option' => array('value' => '')),
  3741. '/option',
  3742. array('option' => array('value' => 'value')),
  3743. 'good',
  3744. '/option',
  3745. array('option' => array('value' => 'other')),
  3746. 'bad',
  3747. '/option',
  3748. '/select'
  3749. );
  3750. $this->assertTags($result, $expected);
  3751. $result = $this->Form->select(
  3752. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3753. array('empty' => false)
  3754. );
  3755. $expected = array(
  3756. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3757. array('option' => array('value' => 'first')),
  3758. 'first &quot;html&quot; &lt;chars&gt;',
  3759. '/option',
  3760. array('option' => array('value' => 'second')),
  3761. 'value',
  3762. '/option',
  3763. '/select'
  3764. );
  3765. $this->assertTags($result, $expected);
  3766. $result = $this->Form->select(
  3767. 'Model.field',
  3768. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3769. array('escape' => false, 'empty' => false)
  3770. );
  3771. $expected = array(
  3772. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3773. array('option' => array('value' => 'first')),
  3774. 'first "html" <chars>',
  3775. '/option',
  3776. array('option' => array('value' => 'second')),
  3777. 'value',
  3778. '/option',
  3779. '/select'
  3780. );
  3781. $this->assertTags($result, $expected);
  3782. $options = array(
  3783. array('value' => 'first', 'name' => 'First'),
  3784. array('value' => 'first', 'name' => 'Another First'),
  3785. );
  3786. $result = $this->Form->select(
  3787. 'Model.field',
  3788. $options,
  3789. array('escape' => false, 'empty' => false)
  3790. );
  3791. $expected = array(
  3792. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3793. array('option' => array('value' => 'first')),
  3794. 'First',
  3795. '/option',
  3796. array('option' => array('value' => 'first')),
  3797. 'Another First',
  3798. '/option',
  3799. '/select'
  3800. );
  3801. $this->assertTags($result, $expected);
  3802. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3803. $result = $this->Form->select(
  3804. 'Model.contact_id',
  3805. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3806. array('escape' => false, 'empty' => 'pick something')
  3807. );
  3808. $expected = array(
  3809. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3810. array('option' => array('value' => '')), 'pick something', '/option',
  3811. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3812. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3813. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3814. '/select'
  3815. );
  3816. $this->assertTags($result, $expected);
  3817. $this->Form->request->data['Model']['field'] = 0;
  3818. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3819. $expected = array(
  3820. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3821. array('option' => array('value' => '')), '/option',
  3822. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3823. array('option' => array('value' => '1')), 'Yes', '/option',
  3824. '/select'
  3825. );
  3826. $this->assertTags($result, $expected);
  3827. $this->Form->request->data['Model']['field'] = 50;
  3828. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3829. $expected = array(
  3830. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3831. array('option' => array('value' => '')), '/option',
  3832. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3833. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3834. '/select'
  3835. );
  3836. $this->assertTags($result, $expected);
  3837. }
  3838. /**
  3839. * test that select() with optiongroups listens to the escape param.
  3840. *
  3841. * @return void
  3842. */
  3843. public function testSelectOptionGroupEscaping() {
  3844. $options = array(
  3845. '>< Key' => array(
  3846. 1 => 'One',
  3847. 2 => 'Two'
  3848. )
  3849. );
  3850. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3851. $expected = array(
  3852. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3853. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3854. array('option' => array('value' => '1')), 'One', '/option',
  3855. array('option' => array('value' => '2')), 'Two', '/option',
  3856. '/optgroup',
  3857. '/select'
  3858. );
  3859. $this->assertTags($result, $expected);
  3860. $options = array(
  3861. '>< Key' => array(
  3862. 1 => 'One',
  3863. 2 => 'Two'
  3864. )
  3865. );
  3866. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3867. $expected = array(
  3868. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3869. 'optgroup' => array('label' => '>< Key'),
  3870. array('option' => array('value' => '1')), 'One', '/option',
  3871. array('option' => array('value' => '2')), 'Two', '/option',
  3872. '/optgroup',
  3873. '/select'
  3874. );
  3875. $this->assertTags($result, $expected);
  3876. }
  3877. /**
  3878. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3879. *
  3880. * @return void
  3881. */
  3882. public function testSelectWithNullAttributes() {
  3883. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3884. $expected = array(
  3885. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3886. array('option' => array('value' => '0')),
  3887. 'first',
  3888. '/option',
  3889. array('option' => array('value' => '1')),
  3890. 'second',
  3891. '/option',
  3892. '/select'
  3893. );
  3894. $this->assertTags($result, $expected);
  3895. }
  3896. /**
  3897. * testNestedSelect method
  3898. *
  3899. * test select element generation with optgroups
  3900. *
  3901. * @return void
  3902. */
  3903. public function testNestedSelect() {
  3904. $result = $this->Form->select(
  3905. 'Model.field',
  3906. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3907. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3908. )), array('empty' => false)
  3909. );
  3910. $expected = array(
  3911. 'select' => array('name' => 'data[Model][field]',
  3912. 'id' => 'ModelField'),
  3913. array('option' => array('value' => 1)),
  3914. 'One',
  3915. '/option',
  3916. array('option' => array('value' => 2)),
  3917. 'Two',
  3918. '/option',
  3919. array('optgroup' => array('label' => 'Three')),
  3920. array('option' => array('value' => 4)),
  3921. 'Four',
  3922. '/option',
  3923. array('option' => array('value' => 5)),
  3924. 'Five',
  3925. '/option',
  3926. '/optgroup',
  3927. '/select'
  3928. );
  3929. $this->assertTags($result, $expected);
  3930. $result = $this->Form->select(
  3931. 'Model.field',
  3932. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3933. array('showParents' => true, 'empty' => false)
  3934. );
  3935. $expected = array(
  3936. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3937. array('option' => array('value' => 1)),
  3938. 'One',
  3939. '/option',
  3940. array('option' => array('value' => 2)),
  3941. 'Two',
  3942. '/option',
  3943. array('optgroup' => array('label' => 'Three')),
  3944. array('option' => array('value' => 3)),
  3945. 'Three',
  3946. '/option',
  3947. array('option' => array('value' => 4)),
  3948. 'Four',
  3949. '/option',
  3950. '/optgroup',
  3951. '/select'
  3952. );
  3953. $this->assertTags($result, $expected);
  3954. }
  3955. /**
  3956. * testSelectMultiple method
  3957. *
  3958. * test generation of multiple select elements
  3959. *
  3960. * @return void
  3961. */
  3962. public function testSelectMultiple() {
  3963. $options = array('first', 'second', 'third');
  3964. $result = $this->Form->select(
  3965. 'Model.multi_field', $options, array('multiple' => true)
  3966. );
  3967. $expected = array(
  3968. 'input' => array(
  3969. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3970. ),
  3971. 'select' => array(
  3972. 'name' => 'data[Model][multi_field][]',
  3973. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3974. ),
  3975. array('option' => array('value' => '0')),
  3976. 'first',
  3977. '/option',
  3978. array('option' => array('value' => '1')),
  3979. 'second',
  3980. '/option',
  3981. array('option' => array('value' => '2')),
  3982. 'third',
  3983. '/option',
  3984. '/select'
  3985. );
  3986. $this->assertTags($result, $expected);
  3987. $result = $this->Form->select(
  3988. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3989. );
  3990. $expected = array(
  3991. 'input' => array(
  3992. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3993. ),
  3994. 'select' => array(
  3995. 'name' => 'data[Model][multi_field][]',
  3996. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3997. ),
  3998. array('option' => array('value' => '0')),
  3999. 'first',
  4000. '/option',
  4001. array('option' => array('value' => '1')),
  4002. 'second',
  4003. '/option',
  4004. array('option' => array('value' => '2')),
  4005. 'third',
  4006. '/option',
  4007. '/select'
  4008. );
  4009. $this->assertTags($result, $expected);
  4010. $result = $this->Form->select(
  4011. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4012. );
  4013. $expected = array(
  4014. 'input' => array(
  4015. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4016. ),
  4017. 'select' => array(
  4018. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4019. 'multiple' => 'multiple'
  4020. ),
  4021. array('option' => array('value' => '0', 'selected' => 'selected')),
  4022. 'first',
  4023. '/option',
  4024. array('option' => array('value' => '1', 'selected' => 'selected')),
  4025. 'second',
  4026. '/option',
  4027. array('option' => array('value' => '2')),
  4028. 'third',
  4029. '/option',
  4030. '/select'
  4031. );
  4032. $this->assertTags($result, $expected);
  4033. $result = $this->Form->select(
  4034. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4035. );
  4036. $expected = array(
  4037. 'select' => array(
  4038. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4039. ),
  4040. array('option' => array('value' => '0', 'selected' => 'selected')),
  4041. 'first',
  4042. '/option',
  4043. array('option' => array('value' => '1', 'selected' => 'selected')),
  4044. 'second',
  4045. '/option',
  4046. array('option' => array('value' => '2')),
  4047. 'third',
  4048. '/option',
  4049. '/select'
  4050. );
  4051. $this->assertTags($result, $expected);
  4052. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4053. $selected = array('2', '3x');
  4054. $result = $this->Form->select(
  4055. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4056. );
  4057. $expected = array(
  4058. 'input' => array(
  4059. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4060. ),
  4061. 'select' => array(
  4062. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4063. ),
  4064. array('option' => array('value' => '1')),
  4065. 'One',
  4066. '/option',
  4067. array('option' => array('value' => '2', 'selected' => 'selected')),
  4068. 'Two',
  4069. '/option',
  4070. array('option' => array('value' => '3')),
  4071. 'Three',
  4072. '/option',
  4073. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4074. 'Stringy',
  4075. '/option',
  4076. '/select'
  4077. );
  4078. $this->assertTags($result, $expected);
  4079. }
  4080. /**
  4081. * Test generating multiple select with disabled elements.
  4082. *
  4083. * @return void
  4084. */
  4085. public function testSelectMultipleWithDisabledElements() {
  4086. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4087. $disabled = array(2, 3);
  4088. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4089. $expected = array(
  4090. 'input' => array(
  4091. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4092. ),
  4093. 'select' => array(
  4094. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4095. ),
  4096. array('option' => array('value' => '1')),
  4097. 'One',
  4098. '/option',
  4099. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4100. 'Two',
  4101. '/option',
  4102. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4103. 'Three',
  4104. '/option',
  4105. array('option' => array('value' => '3x')),
  4106. 'Stringy',
  4107. '/option',
  4108. '/select'
  4109. );
  4110. $this->assertTags($result, $expected);
  4111. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4112. $disabled = array('2', '3x');
  4113. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4114. $expected = array(
  4115. array('div' => array('class' => 'input select')),
  4116. array('label' => array('for' => 'ContactMultiple')),
  4117. 'Multiple',
  4118. '/label',
  4119. 'input' => array(
  4120. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4121. ),
  4122. 'select' => array(
  4123. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4124. ),
  4125. array('option' => array('value' => '1')),
  4126. 'One',
  4127. '/option',
  4128. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4129. 'Two',
  4130. '/option',
  4131. array('option' => array('value' => '3')),
  4132. 'Three',
  4133. '/option',
  4134. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4135. 'Stringy',
  4136. '/option',
  4137. '/select',
  4138. '/div'
  4139. );
  4140. $this->assertTags($result, $expected);
  4141. }
  4142. /**
  4143. * test generation of habtm select boxes.
  4144. *
  4145. * @return void
  4146. */
  4147. public function testHabtmSelectBox() {
  4148. $this->View->viewVars['contactTags'] = array(
  4149. 1 => 'blue',
  4150. 2 => 'red',
  4151. 3 => 'green'
  4152. );
  4153. $this->Form->request->data = array(
  4154. 'Contact' => array(),
  4155. 'ContactTag' => array(
  4156. array(
  4157. 'id' => '1',
  4158. 'name' => 'blue'
  4159. ),
  4160. array(
  4161. 'id' => 3,
  4162. 'name' => 'green'
  4163. )
  4164. )
  4165. );
  4166. $this->Form->create('Contact');
  4167. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4168. $expected = array(
  4169. 'input' => array(
  4170. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4171. ),
  4172. 'select' => array(
  4173. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4174. 'multiple' => 'multiple'
  4175. ),
  4176. array('option' => array('value' => '1', 'selected' => 'selected')),
  4177. 'blue',
  4178. '/option',
  4179. array('option' => array('value' => '2')),
  4180. 'red',
  4181. '/option',
  4182. array('option' => array('value' => '3', 'selected' => 'selected')),
  4183. 'green',
  4184. '/option',
  4185. '/select'
  4186. );
  4187. $this->assertTags($result, $expected);
  4188. // make sure only 50 is selected, and not 50f5c0cf
  4189. $this->View->viewVars['contactTags'] = array(
  4190. '1' => 'blue',
  4191. '50f5c0cf' => 'red',
  4192. '50' => 'green'
  4193. );
  4194. $this->Form->request->data = array(
  4195. 'Contact' => array(),
  4196. 'ContactTag' => array(
  4197. array(
  4198. 'id' => 1,
  4199. 'name' => 'blue'
  4200. ),
  4201. array(
  4202. 'id' => 50,
  4203. 'name' => 'green'
  4204. )
  4205. )
  4206. );
  4207. $this->Form->create('Contact');
  4208. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4209. $expected = array(
  4210. 'input' => array(
  4211. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4212. ),
  4213. 'select' => array(
  4214. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4215. 'multiple' => 'multiple'
  4216. ),
  4217. array('option' => array('value' => '1', 'selected' => 'selected')),
  4218. 'blue',
  4219. '/option',
  4220. array('option' => array('value' => '50f5c0cf')),
  4221. 'red',
  4222. '/option',
  4223. array('option' => array('value' => '50', 'selected' => 'selected')),
  4224. 'green',
  4225. '/option',
  4226. '/select'
  4227. );
  4228. $this->assertTags($result, $expected);
  4229. }
  4230. /**
  4231. * test generation of multi select elements in checkbox format
  4232. *
  4233. * @return void
  4234. */
  4235. public function testSelectMultipleCheckboxes() {
  4236. $result = $this->Form->select(
  4237. 'Model.multi_field',
  4238. array('first', 'second', 'third'),
  4239. array('multiple' => 'checkbox')
  4240. );
  4241. $expected = array(
  4242. 'input' => array(
  4243. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4244. ),
  4245. array('div' => array('class' => 'checkbox')),
  4246. array('input' => array(
  4247. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4248. 'value' => '0', 'id' => 'ModelMultiField0'
  4249. )),
  4250. array('label' => array('for' => 'ModelMultiField0')),
  4251. 'first',
  4252. '/label',
  4253. '/div',
  4254. array('div' => array('class' => 'checkbox')),
  4255. array('input' => array(
  4256. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4257. 'value' => '1', 'id' => 'ModelMultiField1'
  4258. )),
  4259. array('label' => array('for' => 'ModelMultiField1')),
  4260. 'second',
  4261. '/label',
  4262. '/div',
  4263. array('div' => array('class' => 'checkbox')),
  4264. array('input' => array(
  4265. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4266. 'value' => '2', 'id' => 'ModelMultiField2'
  4267. )),
  4268. array('label' => array('for' => 'ModelMultiField2')),
  4269. 'third',
  4270. '/label',
  4271. '/div'
  4272. );
  4273. $this->assertTags($result, $expected);
  4274. $result = $this->Form->select(
  4275. 'Model.multi_field',
  4276. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4277. array('multiple' => 'checkbox')
  4278. );
  4279. $expected = array(
  4280. 'input' => array(
  4281. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4282. ),
  4283. array('div' => array('class' => 'checkbox')),
  4284. array('input' => array(
  4285. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4286. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4287. )),
  4288. array('label' => array('for' => 'ModelMultiFieldA')),
  4289. 'first',
  4290. '/label',
  4291. '/div',
  4292. array('div' => array('class' => 'checkbox')),
  4293. array('input' => array(
  4294. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4295. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4296. )),
  4297. array('label' => array('for' => 'ModelMultiFieldB')),
  4298. 'second',
  4299. '/label',
  4300. '/div',
  4301. array('div' => array('class' => 'checkbox')),
  4302. array('input' => array(
  4303. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4304. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4305. )),
  4306. array('label' => array('for' => 'ModelMultiFieldC')),
  4307. 'third',
  4308. '/label',
  4309. '/div'
  4310. );
  4311. $this->assertTags($result, $expected);
  4312. $result = $this->Form->select(
  4313. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4314. );
  4315. $expected = array(
  4316. 'input' => array(
  4317. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4318. ),
  4319. array('div' => array('class' => 'checkbox')),
  4320. array('input' => array(
  4321. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4322. 'value' => '1', 'id' => 'ModelMultiField1'
  4323. )),
  4324. array('label' => array('for' => 'ModelMultiField1')),
  4325. 'first',
  4326. '/label',
  4327. '/div'
  4328. );
  4329. $this->assertTags($result, $expected);
  4330. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4331. $result = $this->Form->select(
  4332. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4333. );
  4334. $expected = array(
  4335. 'input' => array(
  4336. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4337. ),
  4338. array('div' => array('class' => 'checkbox')),
  4339. array('input' => array(
  4340. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4341. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4342. )),
  4343. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4344. 'first',
  4345. '/label',
  4346. '/div',
  4347. array('div' => array('class' => 'checkbox')),
  4348. array('input' => array(
  4349. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4350. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4351. )),
  4352. array('label' => array('for' => 'ModelTagsArray')),
  4353. 'Array',
  4354. '/label',
  4355. '/div'
  4356. );
  4357. $this->assertTags($result, $expected);
  4358. }
  4359. /**
  4360. * test multiple checkboxes with div styles.
  4361. *
  4362. * @return void
  4363. */
  4364. public function testSelectMultipleCheckboxDiv() {
  4365. $result = $this->Form->select(
  4366. 'Model.tags',
  4367. array('first', 'second'),
  4368. array('multiple' => 'checkbox', 'class' => 'my-class')
  4369. );
  4370. $expected = array(
  4371. 'input' => array(
  4372. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4373. ),
  4374. array('div' => array('class' => 'my-class')),
  4375. array('input' => array(
  4376. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4377. 'value' => '0', 'id' => 'ModelTags0'
  4378. )),
  4379. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4380. '/div',
  4381. array('div' => array('class' => 'my-class')),
  4382. array('input' => array(
  4383. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4384. 'value' => '1', 'id' => 'ModelTags1'
  4385. )),
  4386. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4387. '/div'
  4388. );
  4389. $this->assertTags($result, $expected);
  4390. $result = $this->Form->input('Model.tags', array(
  4391. 'options' => array('first', 'second'),
  4392. 'multiple' => 'checkbox',
  4393. 'class' => 'my-class',
  4394. 'div' => false,
  4395. 'label' => false
  4396. ));
  4397. $this->assertTags($result, $expected);
  4398. $Contact = ClassRegistry::getObject('Contact');
  4399. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4400. $result = $this->Form->input('Contact.tags', array(
  4401. 'options' => array('one'),
  4402. 'multiple' => 'checkbox',
  4403. 'label' => false,
  4404. 'div' => false
  4405. ));
  4406. $expected = array(
  4407. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4408. array('div' => array('class' => 'checkbox form-error')),
  4409. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4410. array('label' => array('for' => 'ContactTags0')),
  4411. 'one',
  4412. '/label',
  4413. '/div'
  4414. );
  4415. $this->assertTags($result, $expected);
  4416. $result = $this->Form->input('Contact.tags', array(
  4417. 'options' => array('one'),
  4418. 'multiple' => 'checkbox',
  4419. 'class' => 'mycheckbox',
  4420. 'label' => false,
  4421. 'div' => false
  4422. ));
  4423. $expected = array(
  4424. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4425. array('div' => array('class' => 'mycheckbox form-error')),
  4426. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4427. array('label' => array('for' => 'ContactTags0')),
  4428. 'one',
  4429. '/label',
  4430. '/div'
  4431. );
  4432. $this->assertTags($result, $expected);
  4433. }
  4434. /**
  4435. * Checks the security hash array generated for multiple-input checkbox elements
  4436. *
  4437. * @return void
  4438. */
  4439. public function testSelectMultipleCheckboxSecurity() {
  4440. $this->Form->request['_Token'] = array('key' => 'testKey');
  4441. $this->assertEquals(array(), $this->Form->fields);
  4442. $result = $this->Form->select(
  4443. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4444. array('multiple' => 'checkbox')
  4445. );
  4446. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4447. $result = $this->Form->secure($this->Form->fields);
  4448. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4449. $this->assertRegExp('/"' . $key . '"/', $result);
  4450. }
  4451. /**
  4452. * Multiple select elements should always be secured as they always participate
  4453. * in the POST data.
  4454. *
  4455. * @return void
  4456. */
  4457. public function testSelectMultipleSecureWithNoOptions() {
  4458. $this->Form->request['_Token'] = array('key' => 'testkey');
  4459. $this->assertEquals(array(), $this->Form->fields);
  4460. $this->Form->select(
  4461. 'Model.select',
  4462. array(),
  4463. array('multiple' => true)
  4464. );
  4465. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4466. }
  4467. /**
  4468. * When a select box has no options it should not be added to the fields list
  4469. * as it always fail post validation.
  4470. *
  4471. * @return void
  4472. */
  4473. public function testSelectNoSecureWithNoOptions() {
  4474. $this->Form->request['_Token'] = array('key' => 'testkey');
  4475. $this->assertEquals(array(), $this->Form->fields);
  4476. $this->Form->select(
  4477. 'Model.select',
  4478. array()
  4479. );
  4480. $this->assertEquals(array(), $this->Form->fields);
  4481. $this->Form->select(
  4482. 'Model.select',
  4483. array(),
  4484. array('empty' => true)
  4485. );
  4486. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4487. }
  4488. /**
  4489. * testInputMultipleCheckboxes method
  4490. *
  4491. * test input() resulting in multi select elements being generated.
  4492. *
  4493. * @return void
  4494. */
  4495. public function testInputMultipleCheckboxes() {
  4496. $result = $this->Form->input('Model.multi_field', array(
  4497. 'options' => array('first', 'second', 'third'),
  4498. 'multiple' => 'checkbox'
  4499. ));
  4500. $expected = array(
  4501. array('div' => array('class' => 'input select')),
  4502. array('label' => array('for' => 'ModelMultiField')),
  4503. 'Multi Field',
  4504. '/label',
  4505. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4506. array('div' => array('class' => 'checkbox')),
  4507. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4508. array('label' => array('for' => 'ModelMultiField0')),
  4509. 'first',
  4510. '/label',
  4511. '/div',
  4512. array('div' => array('class' => 'checkbox')),
  4513. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4514. array('label' => array('for' => 'ModelMultiField1')),
  4515. 'second',
  4516. '/label',
  4517. '/div',
  4518. array('div' => array('class' => 'checkbox')),
  4519. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4520. array('label' => array('for' => 'ModelMultiField2')),
  4521. 'third',
  4522. '/label',
  4523. '/div',
  4524. '/div'
  4525. );
  4526. $this->assertTags($result, $expected);
  4527. $result = $this->Form->input('Model.multi_field', array(
  4528. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4529. 'multiple' => 'checkbox'
  4530. ));
  4531. $expected = array(
  4532. array('div' => array('class' => 'input select')),
  4533. array('label' => array('for' => 'ModelMultiField')),
  4534. 'Multi Field',
  4535. '/label',
  4536. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4537. array('div' => array('class' => 'checkbox')),
  4538. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4539. array('label' => array('for' => 'ModelMultiFieldA')),
  4540. 'first',
  4541. '/label',
  4542. '/div',
  4543. array('div' => array('class' => 'checkbox')),
  4544. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4545. array('label' => array('for' => 'ModelMultiFieldB')),
  4546. 'second',
  4547. '/label',
  4548. '/div',
  4549. array('div' => array('class' => 'checkbox')),
  4550. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4551. array('label' => array('for' => 'ModelMultiFieldC')),
  4552. 'third',
  4553. '/label',
  4554. '/div',
  4555. '/div'
  4556. );
  4557. $this->assertTags($result, $expected);
  4558. $result = $this->Form->input('Model.multi_field', array(
  4559. 'options' => array('1' => 'first'),
  4560. 'multiple' => 'checkbox',
  4561. 'label' => false,
  4562. 'div' => false
  4563. ));
  4564. $expected = array(
  4565. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4566. array('div' => array('class' => 'checkbox')),
  4567. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4568. array('label' => array('for' => 'ModelMultiField1')),
  4569. 'first',
  4570. '/label',
  4571. '/div'
  4572. );
  4573. $this->assertTags($result, $expected);
  4574. $result = $this->Form->input('Model.multi_field', array(
  4575. 'options' => array('2' => 'second'),
  4576. 'multiple' => 'checkbox',
  4577. 'label' => false,
  4578. 'div' => false
  4579. ));
  4580. $expected = array(
  4581. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4582. array('div' => array('class' => 'checkbox')),
  4583. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4584. array('label' => array('for' => 'ModelMultiField2')),
  4585. 'second',
  4586. '/label',
  4587. '/div'
  4588. );
  4589. $this->assertTags($result, $expected);
  4590. }
  4591. /**
  4592. * testSelectHiddenFieldOmission method
  4593. *
  4594. * test that select() with 'hiddenField' => false omits the hidden field
  4595. *
  4596. * @return void
  4597. */
  4598. public function testSelectHiddenFieldOmission() {
  4599. $result = $this->Form->select('Model.multi_field',
  4600. array('first', 'second'),
  4601. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4602. );
  4603. $expected = array(
  4604. array('div' => array('class' => 'checkbox')),
  4605. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4606. array('label' => array('for' => 'ModelMultiField0')),
  4607. 'first',
  4608. '/label',
  4609. '/div',
  4610. array('div' => array('class' => 'checkbox')),
  4611. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4612. array('label' => array('for' => 'ModelMultiField1')),
  4613. 'second',
  4614. '/label',
  4615. '/div'
  4616. );
  4617. $this->assertTags($result, $expected);
  4618. $result = $this->Form->input('Model.multi_field', array(
  4619. 'options' => array('first', 'second'),
  4620. 'multiple' => 'checkbox',
  4621. 'hiddenField' => false
  4622. ));
  4623. $expected = array(
  4624. array('div' => array('class' => 'input select')),
  4625. array('label' => array('for' => 'ModelMultiField')),
  4626. 'Multi Field',
  4627. '/label',
  4628. array('div' => array('class' => 'checkbox')),
  4629. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4630. array('label' => array('for' => 'ModelMultiField0')),
  4631. 'first',
  4632. '/label',
  4633. '/div',
  4634. array('div' => array('class' => 'checkbox')),
  4635. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4636. array('label' => array('for' => 'ModelMultiField1')),
  4637. 'second',
  4638. '/label',
  4639. '/div',
  4640. '/div'
  4641. );
  4642. $this->assertTags($result, $expected);
  4643. }
  4644. /**
  4645. * test that select() with multiple = checkbox works with overriding name attribute.
  4646. *
  4647. * @return void
  4648. */
  4649. public function testSelectCheckboxMultipleOverrideName() {
  4650. $result = $this->Form->input('category', array(
  4651. 'type' => 'select',
  4652. 'multiple' => 'checkbox',
  4653. 'name' => 'data[fish]',
  4654. 'options' => array('1', '2'),
  4655. 'div' => false,
  4656. 'label' => false,
  4657. ));
  4658. $expected = array(
  4659. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4660. array('div' => array('class' => 'checkbox')),
  4661. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4662. array('label' => array('for' => 'Category0')), '1', '/label',
  4663. '/div',
  4664. array('div' => array('class' => 'checkbox')),
  4665. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4666. array('label' => array('for' => 'Category1')), '2', '/label',
  4667. '/div'
  4668. );
  4669. $this->assertTags($result, $expected);
  4670. }
  4671. /**
  4672. * Test that 'id' overrides all the checkbox id's as well.
  4673. *
  4674. * @return void
  4675. */
  4676. public function testSelectCheckboxMultipleId() {
  4677. $result = $this->Form->select(
  4678. 'Model.multi_field',
  4679. array('first', 'second', 'third'),
  4680. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4681. );
  4682. $expected = array(
  4683. 'input' => array(
  4684. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4685. ),
  4686. array('div' => array('class' => 'checkbox')),
  4687. array('input' => array(
  4688. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4689. 'value' => '0', 'id' => 'CustomId0'
  4690. )),
  4691. array('label' => array('for' => 'CustomId0')),
  4692. 'first',
  4693. '/label',
  4694. '/div',
  4695. array('div' => array('class' => 'checkbox')),
  4696. array('input' => array(
  4697. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4698. 'value' => '1', 'id' => 'CustomId1'
  4699. )),
  4700. array('label' => array('for' => 'CustomId1')),
  4701. 'second',
  4702. '/label',
  4703. '/div',
  4704. array('div' => array('class' => 'checkbox')),
  4705. array('input' => array(
  4706. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4707. 'value' => '2', 'id' => 'CustomId2'
  4708. )),
  4709. array('label' => array('for' => 'CustomId2')),
  4710. 'third',
  4711. '/label',
  4712. '/div'
  4713. );
  4714. $this->assertTags($result, $expected);
  4715. }
  4716. /**
  4717. * testCheckbox method
  4718. *
  4719. * Test generation of checkboxes
  4720. *
  4721. * @return void
  4722. */
  4723. public function testCheckbox() {
  4724. $result = $this->Form->checkbox('Model.field');
  4725. $expected = array(
  4726. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4727. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4728. );
  4729. $this->assertTags($result, $expected);
  4730. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4731. $expected = array(
  4732. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4733. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4734. );
  4735. $this->assertTags($result, $expected);
  4736. $Contact = ClassRegistry::getObject('Contact');
  4737. $Contact->validationErrors['field'] = 1;
  4738. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4739. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4740. $expected = array(
  4741. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4742. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4743. );
  4744. $this->assertTags($result, $expected);
  4745. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4746. $expected = array(
  4747. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4748. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4749. );
  4750. $this->assertTags($result, $expected);
  4751. $this->Form->request->data['Contact']['field'] = '';
  4752. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4753. $expected = array(
  4754. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4755. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4756. );
  4757. $this->assertTags($result, $expected);
  4758. $Contact->validationErrors = array();
  4759. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4760. $expected = array(
  4761. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4762. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4763. );
  4764. $this->assertTags($result, $expected);
  4765. $this->Form->request->data['Contact']['published'] = 1;
  4766. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4767. $expected = array(
  4768. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4769. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4770. );
  4771. $this->assertTags($result, $expected);
  4772. $this->Form->request->data['Contact']['published'] = 0;
  4773. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4774. $expected = array(
  4775. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4776. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4777. );
  4778. $this->assertTags($result, $expected);
  4779. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4780. $expected = array(
  4781. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4782. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4783. );
  4784. $this->assertTags($result, $expected);
  4785. $result = $this->Form->checkbox('CustomField.1.value');
  4786. $expected = array(
  4787. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4788. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4789. );
  4790. $this->assertTags($result, $expected);
  4791. }
  4792. /**
  4793. * test checkbox() with a custom name attribute
  4794. *
  4795. * @return void
  4796. */
  4797. public function testCheckboxCustomNameAttribute() {
  4798. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4799. $expected = array(
  4800. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4801. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4802. );
  4803. $this->assertTags($result, $expected);
  4804. }
  4805. /**
  4806. * test the checked option for checkboxes.
  4807. *
  4808. * @return void
  4809. */
  4810. public function testCheckboxCheckedOption() {
  4811. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4812. $expected = array(
  4813. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4814. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4815. );
  4816. $this->assertTags($result, $expected);
  4817. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4818. $expected = array(
  4819. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4820. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4821. );
  4822. $this->assertTags($result, $expected);
  4823. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4824. $expected = array(
  4825. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4826. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4827. );
  4828. $this->assertTags($result, $expected);
  4829. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4830. $expected = array(
  4831. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4832. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4833. );
  4834. $this->assertTags($result, $expected);
  4835. $this->Form->request->data['Model']['field'] = 1;
  4836. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4837. $expected = array(
  4838. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4839. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4840. );
  4841. $this->assertTags($result, $expected);
  4842. }
  4843. /**
  4844. * Test that disabled attribute works on both the checkbox and hidden input.
  4845. *
  4846. * @return void
  4847. */
  4848. public function testCheckboxDisabling() {
  4849. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4850. $expected = array(
  4851. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4852. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4853. );
  4854. $this->assertTags($result, $expected);
  4855. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4856. $expected = array(
  4857. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4858. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4859. );
  4860. $this->assertTags($result, $expected);
  4861. }
  4862. /**
  4863. * Test that the hidden input for checkboxes can be omitted or set to a
  4864. * specific value.
  4865. *
  4866. * @return void
  4867. */
  4868. public function testCheckboxHiddenField() {
  4869. $result = $this->Form->input('UserForm.something', array(
  4870. 'type' => 'checkbox',
  4871. 'hiddenField' => false
  4872. ));
  4873. $expected = array(
  4874. 'div' => array('class' => 'input checkbox'),
  4875. array('input' => array(
  4876. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4877. 'value' => '1', 'id' => 'UserFormSomething'
  4878. )),
  4879. 'label' => array('for' => 'UserFormSomething'),
  4880. 'Something',
  4881. '/label',
  4882. '/div'
  4883. );
  4884. $this->assertTags($result, $expected);
  4885. $result = $this->Form->input('UserForm.something', array(
  4886. 'type' => 'checkbox',
  4887. 'value' => 'Y',
  4888. 'hiddenField' => 'N',
  4889. ));
  4890. $expected = array(
  4891. 'div' => array('class' => 'input checkbox'),
  4892. array('input' => array(
  4893. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4894. 'value' => 'N', 'id' => 'UserFormSomething_'
  4895. )),
  4896. array('input' => array(
  4897. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4898. 'value' => 'Y', 'id' => 'UserFormSomething'
  4899. )),
  4900. 'label' => array('for' => 'UserFormSomething'),
  4901. 'Something',
  4902. '/label',
  4903. '/div'
  4904. );
  4905. $this->assertTags($result, $expected);
  4906. }
  4907. /**
  4908. * testDateTime method
  4909. *
  4910. * Test generation of date/time select elements
  4911. *
  4912. * @return void
  4913. */
  4914. public function testDateTime() {
  4915. extract($this->dateRegex);
  4916. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4917. $now = strtotime('now');
  4918. $expected = array(
  4919. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4920. $daysRegex,
  4921. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4922. date('j', $now),
  4923. '/option',
  4924. '*/select',
  4925. '-',
  4926. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4927. $monthsRegex,
  4928. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4929. date('F', $now),
  4930. '/option',
  4931. '*/select',
  4932. '-',
  4933. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4934. $yearsRegex,
  4935. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4936. date('Y', $now),
  4937. '/option',
  4938. '*/select',
  4939. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4940. $hoursRegex,
  4941. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4942. date('g', $now),
  4943. '/option',
  4944. '*/select',
  4945. ':',
  4946. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4947. $minutesRegex,
  4948. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4949. date('i', $now),
  4950. '/option',
  4951. '*/select',
  4952. ' ',
  4953. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4954. $meridianRegex,
  4955. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4956. date('a', $now),
  4957. '/option',
  4958. '*/select'
  4959. );
  4960. $this->assertTags($result, $expected);
  4961. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4962. $expected = array(
  4963. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4964. $daysRegex,
  4965. array('option' => array('value' => '')),
  4966. '/option',
  4967. '*/select',
  4968. '-',
  4969. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4970. $monthsRegex,
  4971. array('option' => array('value' => '')),
  4972. '/option',
  4973. '*/select',
  4974. '-',
  4975. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4976. $yearsRegex,
  4977. array('option' => array('value' => '')),
  4978. '/option',
  4979. '*/select',
  4980. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4981. $hoursRegex,
  4982. array('option' => array('value' => '')),
  4983. '/option',
  4984. '*/select',
  4985. ':',
  4986. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4987. $minutesRegex,
  4988. array('option' => array('value' => '')),
  4989. '/option',
  4990. '*/select',
  4991. ' ',
  4992. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4993. $meridianRegex,
  4994. array('option' => array('value' => '')),
  4995. '/option',
  4996. '*/select'
  4997. );
  4998. $this->assertTags($result, $expected);
  4999. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5000. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5001. $this->assertTags($result, $expected);
  5002. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5003. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5004. $this->assertTags($result, $expected);
  5005. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5006. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5007. $expected = array(
  5008. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5009. $daysRegex,
  5010. array('option' => array('value' => '')),
  5011. '/option',
  5012. '*/select',
  5013. '-',
  5014. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5015. $monthsRegex,
  5016. array('option' => array('value' => '')),
  5017. '/option',
  5018. '*/select',
  5019. '-',
  5020. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5021. $yearsRegex,
  5022. array('option' => array('value' => '')),
  5023. '/option',
  5024. '*/select',
  5025. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5026. $hoursRegex,
  5027. array('option' => array('value' => '')),
  5028. '/option',
  5029. '*/select',
  5030. ':',
  5031. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5032. $minutesRegex,
  5033. array('option' => array('value' => '')),
  5034. '/option',
  5035. array('option' => array('value' => '00')),
  5036. '00',
  5037. '/option',
  5038. array('option' => array('value' => '05')),
  5039. '05',
  5040. '/option',
  5041. array('option' => array('value' => '10')),
  5042. '10',
  5043. '/option',
  5044. '*/select',
  5045. ' ',
  5046. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5047. $meridianRegex,
  5048. array('option' => array('value' => '')),
  5049. '/option',
  5050. '*/select'
  5051. );
  5052. $this->assertTags($result, $expected);
  5053. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5054. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5055. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5056. $this->Form->request->data['Contact']['data'] = null;
  5057. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5058. $expected = array(
  5059. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5060. $daysRegex,
  5061. array('option' => array('value' => '')),
  5062. '/option',
  5063. '*/select',
  5064. '-',
  5065. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5066. $monthsRegex,
  5067. array('option' => array('value' => '')),
  5068. '/option',
  5069. '*/select',
  5070. '-',
  5071. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5072. $yearsRegex,
  5073. array('option' => array('value' => '')),
  5074. '/option',
  5075. '*/select',
  5076. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5077. $hoursRegex,
  5078. array('option' => array('value' => '')),
  5079. '/option',
  5080. '*/select',
  5081. ':',
  5082. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5083. $minutesRegex,
  5084. array('option' => array('value' => '')),
  5085. '/option',
  5086. '*/select',
  5087. ' ',
  5088. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5089. $meridianRegex,
  5090. array('option' => array('value' => '')),
  5091. '/option',
  5092. '*/select'
  5093. );
  5094. $this->assertTags($result, $expected);
  5095. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5096. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5097. $now = strtotime($this->Form->data['Model']['field']);
  5098. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5099. $expected = array(
  5100. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5101. $daysRegex,
  5102. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5103. date('j', $now),
  5104. '/option',
  5105. '*/select',
  5106. '-',
  5107. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5108. $monthsRegex,
  5109. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5110. date('F', $now),
  5111. '/option',
  5112. '*/select',
  5113. '-',
  5114. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5115. $yearsRegex,
  5116. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5117. date('Y', $now),
  5118. '/option',
  5119. '*/select',
  5120. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5121. $hoursRegex,
  5122. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5123. date('g', $now),
  5124. '/option',
  5125. '*/select',
  5126. ':',
  5127. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5128. $minutesRegex,
  5129. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5130. date('i', $now),
  5131. '/option',
  5132. '*/select',
  5133. ' ',
  5134. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5135. $meridianRegex,
  5136. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5137. date('a', $now),
  5138. '/option',
  5139. '*/select'
  5140. );
  5141. $this->assertTags($result, $expected);
  5142. $selected = strtotime('2008-10-26 12:33:00');
  5143. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5144. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5145. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5146. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5147. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5148. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5149. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5150. $this->Form->create('Contact');
  5151. $result = $this->Form->input('published');
  5152. $now = strtotime('now');
  5153. $expected = array(
  5154. 'div' => array('class' => 'input date'),
  5155. 'label' => array('for' => 'ContactPublishedMonth'),
  5156. 'Published',
  5157. '/label',
  5158. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5159. $monthsRegex,
  5160. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5161. date('F', $now),
  5162. '/option',
  5163. '*/select',
  5164. '-',
  5165. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5166. $daysRegex,
  5167. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5168. date('j', $now),
  5169. '/option',
  5170. '*/select',
  5171. '-',
  5172. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5173. $yearsRegex,
  5174. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5175. date('Y', $now),
  5176. '/option',
  5177. '*/select',
  5178. '/div'
  5179. );
  5180. $this->assertTags($result, $expected);
  5181. $result = $this->Form->input('published2', array('type' => 'date'));
  5182. $now = strtotime('now');
  5183. $expected = array(
  5184. 'div' => array('class' => 'input date'),
  5185. 'label' => array('for' => 'ContactPublished2Month'),
  5186. 'Published2',
  5187. '/label',
  5188. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5189. $monthsRegex,
  5190. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5191. date('F', $now),
  5192. '/option',
  5193. '*/select',
  5194. '-',
  5195. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5196. $daysRegex,
  5197. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5198. date('j', $now),
  5199. '/option',
  5200. '*/select',
  5201. '-',
  5202. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5203. $yearsRegex,
  5204. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5205. date('Y', $now),
  5206. '/option',
  5207. '*/select',
  5208. '/div'
  5209. );
  5210. $this->assertTags($result, $expected);
  5211. $this->Form->create('Contact');
  5212. $result = $this->Form->input('published', array('monthNames' => false));
  5213. $now = strtotime('now');
  5214. $expected = array(
  5215. 'div' => array('class' => 'input date'),
  5216. 'label' => array('for' => 'ContactPublishedMonth'),
  5217. 'Published',
  5218. '/label',
  5219. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5220. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5221. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5222. date('m', $now),
  5223. '/option',
  5224. '*/select',
  5225. '-',
  5226. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5227. $daysRegex,
  5228. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5229. date('j', $now),
  5230. '/option',
  5231. '*/select',
  5232. '-',
  5233. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5234. $yearsRegex,
  5235. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5236. date('Y', $now),
  5237. '/option',
  5238. '*/select',
  5239. '/div'
  5240. );
  5241. $this->assertTags($result, $expected);
  5242. $result = $this->Form->input('published', array(
  5243. 'timeFormat' => 24,
  5244. 'interval' => 5,
  5245. 'selected' => strtotime('2009-09-03 13:37:00'),
  5246. 'type' => 'datetime'
  5247. ));
  5248. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5249. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5250. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5251. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5252. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5253. }
  5254. /**
  5255. * Test that empty values don't trigger errors.
  5256. *
  5257. * @return void
  5258. */
  5259. public function testDateTimeNoErrorsOnEmptyData() {
  5260. $this->Form->request->data['Contact'] = array(
  5261. 'date' => array(
  5262. 'day' => '',
  5263. 'month' => '',
  5264. 'year' => '',
  5265. 'hour' => '',
  5266. 'min' => '',
  5267. 'meridian' => ''
  5268. )
  5269. );
  5270. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5271. $this->assertNotEmpty($result);
  5272. }
  5273. /**
  5274. * test that datetime() and default values work.
  5275. *
  5276. * @return void
  5277. */
  5278. public function testDatetimeWithDefault() {
  5279. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5280. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5281. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5282. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5283. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5284. 'default' => '2009-06-01 11:15:30'
  5285. ));
  5286. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5287. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5288. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5289. }
  5290. /**
  5291. * test that bogus non-date time data doesn't cause errors.
  5292. *
  5293. * @return void
  5294. */
  5295. public function testDateTimeWithBogusData() {
  5296. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5297. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5298. }
  5299. /**
  5300. * testDateTimeEmptyAsArray
  5301. *
  5302. * @return void
  5303. */
  5304. public function testDateTimeEmptyAsArray() {
  5305. $result = $this->Form->dateTime('Contact.date',
  5306. 'DMY',
  5307. '12',
  5308. array(
  5309. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5310. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5311. )
  5312. )
  5313. );
  5314. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5315. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5316. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5317. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5318. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5319. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5320. $result = $this->Form->dateTime('Contact.date',
  5321. 'DMY',
  5322. '12',
  5323. array(
  5324. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5325. )
  5326. );
  5327. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5328. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5329. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5330. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5331. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5332. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5333. }
  5334. /**
  5335. * testFormDateTimeMulti method
  5336. *
  5337. * test multiple datetime element generation
  5338. *
  5339. * @return void
  5340. */
  5341. public function testFormDateTimeMulti() {
  5342. extract($this->dateRegex);
  5343. $result = $this->Form->dateTime('Contact.1.updated');
  5344. $expected = array(
  5345. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5346. $daysRegex,
  5347. array('option' => array('value' => '')),
  5348. '/option',
  5349. '*/select',
  5350. '-',
  5351. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5352. $monthsRegex,
  5353. array('option' => array('value' => '')),
  5354. '/option',
  5355. '*/select',
  5356. '-',
  5357. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5358. $yearsRegex,
  5359. array('option' => array('value' => '')),
  5360. '/option',
  5361. '*/select',
  5362. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5363. $hoursRegex,
  5364. array('option' => array('value' => '')),
  5365. '/option',
  5366. '*/select',
  5367. ':',
  5368. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5369. $minutesRegex,
  5370. array('option' => array('value' => '')),
  5371. '/option',
  5372. '*/select',
  5373. ' ',
  5374. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5375. $meridianRegex,
  5376. array('option' => array('value' => '')),
  5377. '/option',
  5378. '*/select'
  5379. );
  5380. $this->assertTags($result, $expected);
  5381. $result = $this->Form->dateTime('Contact.2.updated');
  5382. $expected = array(
  5383. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5384. $daysRegex,
  5385. array('option' => array('value' => '')),
  5386. '/option',
  5387. '*/select',
  5388. '-',
  5389. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5390. $monthsRegex,
  5391. array('option' => array('value' => '')),
  5392. '/option',
  5393. '*/select',
  5394. '-',
  5395. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5396. $yearsRegex,
  5397. array('option' => array('value' => '')),
  5398. '/option',
  5399. '*/select',
  5400. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5401. $hoursRegex,
  5402. array('option' => array('value' => '')),
  5403. '/option',
  5404. '*/select',
  5405. ':',
  5406. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5407. $minutesRegex,
  5408. array('option' => array('value' => '')),
  5409. '/option',
  5410. '*/select',
  5411. ' ',
  5412. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5413. $meridianRegex,
  5414. array('option' => array('value' => '')),
  5415. '/option',
  5416. '*/select'
  5417. );
  5418. $this->assertTags($result, $expected);
  5419. }
  5420. /**
  5421. * When changing the date format, the label should always focus the first select box when
  5422. * clicked.
  5423. *
  5424. * @return void
  5425. */
  5426. public function testDateTimeLabelIdMatchesFirstInput() {
  5427. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5428. $this->assertContains('label for="ModelDateMonth"', $result);
  5429. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5430. $this->assertContains('label for="ModelDateDay"', $result);
  5431. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5432. $this->assertContains('label for="ModelDateYear"', $result);
  5433. }
  5434. /**
  5435. * testMonth method
  5436. *
  5437. * @return void
  5438. */
  5439. public function testMonth() {
  5440. $result = $this->Form->month('Model.field');
  5441. $expected = array(
  5442. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5443. array('option' => array('value' => '')),
  5444. '/option',
  5445. array('option' => array('value' => '01')),
  5446. date('F', strtotime('2008-01-01 00:00:00')),
  5447. '/option',
  5448. array('option' => array('value' => '02')),
  5449. date('F', strtotime('2008-02-01 00:00:00')),
  5450. '/option',
  5451. '*/select',
  5452. );
  5453. $this->assertTags($result, $expected);
  5454. $result = $this->Form->month('Model.field', array('empty' => true));
  5455. $expected = array(
  5456. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5457. array('option' => array('value' => '')),
  5458. '/option',
  5459. array('option' => array('value' => '01')),
  5460. date('F', strtotime('2008-01-01 00:00:00')),
  5461. '/option',
  5462. array('option' => array('value' => '02')),
  5463. date('F', strtotime('2008-02-01 00:00:00')),
  5464. '/option',
  5465. '*/select',
  5466. );
  5467. $this->assertTags($result, $expected);
  5468. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5469. $expected = array(
  5470. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5471. array('option' => array('value' => '')),
  5472. '/option',
  5473. array('option' => array('value' => '01')),
  5474. '01',
  5475. '/option',
  5476. array('option' => array('value' => '02')),
  5477. '02',
  5478. '/option',
  5479. '*/select',
  5480. );
  5481. $this->assertTags($result, $expected);
  5482. $monthNames = array(
  5483. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5484. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5485. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5486. $expected = array(
  5487. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5488. array('option' => array('value' => '')),
  5489. '/option',
  5490. array('option' => array('value' => '01')),
  5491. 'Jan',
  5492. '/option',
  5493. array('option' => array('value' => '02')),
  5494. 'Feb',
  5495. '/option',
  5496. '*/select',
  5497. );
  5498. $this->assertTags($result, $expected);
  5499. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5500. $result = $this->Form->month('Project.release');
  5501. $expected = array(
  5502. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  5503. array('option' => array('value' => '')),
  5504. '/option',
  5505. array('option' => array('value' => '01')),
  5506. 'January',
  5507. '/option',
  5508. array('option' => array('value' => '02', 'selected' => 'selected')),
  5509. 'February',
  5510. '/option',
  5511. '*/select',
  5512. );
  5513. $this->assertTags($result, $expected);
  5514. }
  5515. /**
  5516. * testDay method
  5517. *
  5518. * @return void
  5519. */
  5520. public function testDay() {
  5521. extract($this->dateRegex);
  5522. $result = $this->Form->day('Model.field', array('value' => false));
  5523. $expected = array(
  5524. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5525. array('option' => array('value' => '')),
  5526. '/option',
  5527. array('option' => array('value' => '01')),
  5528. '1',
  5529. '/option',
  5530. array('option' => array('value' => '02')),
  5531. '2',
  5532. '/option',
  5533. $daysRegex,
  5534. '/select',
  5535. );
  5536. $this->assertTags($result, $expected);
  5537. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5538. $result = $this->Form->day('Model.field');
  5539. $expected = array(
  5540. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5541. array('option' => array('value' => '')),
  5542. '/option',
  5543. array('option' => array('value' => '01')),
  5544. '1',
  5545. '/option',
  5546. array('option' => array('value' => '02')),
  5547. '2',
  5548. '/option',
  5549. $daysRegex,
  5550. array('option' => array('value' => '10', 'selected' => 'selected')),
  5551. '10',
  5552. '/option',
  5553. $daysRegex,
  5554. '/select',
  5555. );
  5556. $this->assertTags($result, $expected);
  5557. $this->Form->request->data['Model']['field'] = '';
  5558. $result = $this->Form->day('Model.field', array('value' => '10'));
  5559. $expected = array(
  5560. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5561. array('option' => array('value' => '')),
  5562. '/option',
  5563. array('option' => array('value' => '01')),
  5564. '1',
  5565. '/option',
  5566. array('option' => array('value' => '02')),
  5567. '2',
  5568. '/option',
  5569. $daysRegex,
  5570. array('option' => array('value' => '10', 'selected' => 'selected')),
  5571. '10',
  5572. '/option',
  5573. $daysRegex,
  5574. '/select',
  5575. );
  5576. $this->assertTags($result, $expected);
  5577. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5578. $result = $this->Form->day('Model.field', array('value' => true));
  5579. $expected = array(
  5580. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5581. array('option' => array('value' => '')),
  5582. '/option',
  5583. array('option' => array('value' => '01')),
  5584. '1',
  5585. '/option',
  5586. array('option' => array('value' => '02')),
  5587. '2',
  5588. '/option',
  5589. $daysRegex,
  5590. array('option' => array('value' => '10', 'selected' => 'selected')),
  5591. '10',
  5592. '/option',
  5593. $daysRegex,
  5594. '/select',
  5595. );
  5596. $this->assertTags($result, $expected);
  5597. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5598. $result = $this->Form->day('Project.release');
  5599. $expected = array(
  5600. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  5601. array('option' => array('value' => '')),
  5602. '/option',
  5603. array('option' => array('value' => '01')),
  5604. '1',
  5605. '/option',
  5606. array('option' => array('value' => '02')),
  5607. '2',
  5608. '/option',
  5609. $daysRegex,
  5610. array('option' => array('value' => '10', 'selected' => 'selected')),
  5611. '10',
  5612. '/option',
  5613. $daysRegex,
  5614. '/select',
  5615. );
  5616. $this->assertTags($result, $expected);
  5617. }
  5618. /**
  5619. * testMinute method
  5620. *
  5621. * @return void
  5622. */
  5623. public function testMinute() {
  5624. extract($this->dateRegex);
  5625. $result = $this->Form->minute('Model.field');
  5626. $expected = array(
  5627. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5628. array('option' => array('value' => '')),
  5629. '/option',
  5630. array('option' => array('value' => '00')),
  5631. '00',
  5632. '/option',
  5633. array('option' => array('value' => '01')),
  5634. '01',
  5635. '/option',
  5636. array('option' => array('value' => '02')),
  5637. '02',
  5638. '/option',
  5639. $minutesRegex,
  5640. '/select',
  5641. );
  5642. $this->assertTags($result, $expected);
  5643. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5644. $result = $this->Form->minute('Model.field');
  5645. $expected = array(
  5646. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5647. array('option' => array('value' => '')),
  5648. '/option',
  5649. array('option' => array('value' => '00')),
  5650. '00',
  5651. '/option',
  5652. array('option' => array('value' => '01')),
  5653. '01',
  5654. '/option',
  5655. array('option' => array('value' => '02')),
  5656. '02',
  5657. '/option',
  5658. $minutesRegex,
  5659. array('option' => array('value' => '12', 'selected' => 'selected')),
  5660. '12',
  5661. '/option',
  5662. $minutesRegex,
  5663. '/select',
  5664. );
  5665. $this->assertTags($result, $expected);
  5666. $this->Form->request->data['Model']['field'] = '';
  5667. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5668. $expected = array(
  5669. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5670. array('option' => array('value' => '')),
  5671. '/option',
  5672. array('option' => array('value' => '00')),
  5673. '00',
  5674. '/option',
  5675. array('option' => array('value' => '05')),
  5676. '05',
  5677. '/option',
  5678. array('option' => array('value' => '10')),
  5679. '10',
  5680. '/option',
  5681. $minutesRegex,
  5682. '/select',
  5683. );
  5684. $this->assertTags($result, $expected);
  5685. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5686. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5687. $expected = array(
  5688. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5689. array('option' => array('value' => '')),
  5690. '/option',
  5691. array('option' => array('value' => '00')),
  5692. '00',
  5693. '/option',
  5694. array('option' => array('value' => '05')),
  5695. '05',
  5696. '/option',
  5697. array('option' => array('value' => '10', 'selected' => 'selected')),
  5698. '10',
  5699. '/option',
  5700. $minutesRegex,
  5701. '/select',
  5702. );
  5703. $this->assertTags($result, $expected);
  5704. }
  5705. /**
  5706. * testHour method
  5707. *
  5708. * @return void
  5709. */
  5710. public function testHour() {
  5711. extract($this->dateRegex);
  5712. $result = $this->Form->hour('Model.field', false);
  5713. $expected = array(
  5714. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5715. array('option' => array('value' => '')),
  5716. '/option',
  5717. array('option' => array('value' => '01')),
  5718. '1',
  5719. '/option',
  5720. array('option' => array('value' => '02')),
  5721. '2',
  5722. '/option',
  5723. $hoursRegex,
  5724. '/select',
  5725. );
  5726. $this->assertTags($result, $expected);
  5727. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5728. $result = $this->Form->hour('Model.field', false);
  5729. $expected = array(
  5730. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5731. array('option' => array('value' => '')),
  5732. '/option',
  5733. array('option' => array('value' => '01')),
  5734. '1',
  5735. '/option',
  5736. array('option' => array('value' => '02')),
  5737. '2',
  5738. '/option',
  5739. $hoursRegex,
  5740. array('option' => array('value' => '12', 'selected' => 'selected')),
  5741. '12',
  5742. '/option',
  5743. '/select',
  5744. );
  5745. $this->assertTags($result, $expected);
  5746. $this->Form->request->data['Model']['field'] = '';
  5747. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5748. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5749. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5750. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5751. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5752. $result = $this->Form->hour('Model.field', true);
  5753. $expected = array(
  5754. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5755. array('option' => array('value' => '')),
  5756. '/option',
  5757. array('option' => array('value' => '00', 'selected' => 'selected')),
  5758. '0',
  5759. '/option',
  5760. array('option' => array('value' => '01')),
  5761. '1',
  5762. '/option',
  5763. array('option' => array('value' => '02')),
  5764. '2',
  5765. '/option',
  5766. $hoursRegex,
  5767. '/select',
  5768. );
  5769. $this->assertTags($result, $expected);
  5770. unset($this->Form->request->data['Model']['field']);
  5771. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5772. $thisHour = date('H');
  5773. $optValue = date('G');
  5774. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5775. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  5776. $result = $this->Form->hour('Model.field', true);
  5777. $expected = array(
  5778. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5779. array('option' => array('value' => '')),
  5780. '/option',
  5781. array('option' => array('value' => '00')),
  5782. '0',
  5783. '/option',
  5784. array('option' => array('value' => '01', 'selected' => 'selected')),
  5785. '1',
  5786. '/option',
  5787. array('option' => array('value' => '02')),
  5788. '2',
  5789. '/option',
  5790. $hoursRegex,
  5791. '/select',
  5792. );
  5793. $this->assertTags($result, $expected);
  5794. }
  5795. /**
  5796. * testYear method
  5797. *
  5798. * @return void
  5799. */
  5800. public function testYear() {
  5801. $result = $this->Form->year('Model.field', 2006, 2007);
  5802. $expected = array(
  5803. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5804. array('option' => array('value' => '')),
  5805. '/option',
  5806. array('option' => array('value' => '2007')),
  5807. '2007',
  5808. '/option',
  5809. array('option' => array('value' => '2006')),
  5810. '2006',
  5811. '/option',
  5812. '/select',
  5813. );
  5814. $this->assertTags($result, $expected);
  5815. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5816. $expected = array(
  5817. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5818. array('option' => array('value' => '')),
  5819. '/option',
  5820. array('option' => array('value' => '2006')),
  5821. '2006',
  5822. '/option',
  5823. array('option' => array('value' => '2007')),
  5824. '2007',
  5825. '/option',
  5826. '/select',
  5827. );
  5828. $this->assertTags($result, $expected);
  5829. $this->request->data['Contact']['published'] = '';
  5830. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5831. $expected = array(
  5832. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5833. array('option' => array('value' => '')),
  5834. '/option',
  5835. array('option' => array('value' => '2007')),
  5836. '2007',
  5837. '/option',
  5838. array('option' => array('value' => '2006')),
  5839. '2006',
  5840. '/option',
  5841. '/select',
  5842. );
  5843. $this->assertTags($result, $expected);
  5844. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5845. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5846. $expected = array(
  5847. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5848. array('option' => array('value' => '2007')),
  5849. '2007',
  5850. '/option',
  5851. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5852. '2006',
  5853. '/option',
  5854. '/select',
  5855. );
  5856. $this->assertTags($result, $expected);
  5857. $this->Form->request->data['Contact']['published'] = '';
  5858. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5859. $expected = array(
  5860. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5861. array('option' => array('value' => '')),
  5862. '/option',
  5863. array('option' => array('value' => '2007')),
  5864. '2007',
  5865. '/option',
  5866. array('option' => array('value' => '2006')),
  5867. '2006',
  5868. '/option',
  5869. '/select',
  5870. );
  5871. $this->assertTags($result, $expected);
  5872. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5873. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5874. $expected = array(
  5875. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5876. array('option' => array('value' => '2007')),
  5877. '2007',
  5878. '/option',
  5879. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5880. '2006',
  5881. '/option',
  5882. '/select',
  5883. );
  5884. $this->assertTags($result, $expected);
  5885. $this->Form->request->data['Contact']['published'] = '';
  5886. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5887. $expected = array(
  5888. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5889. array('option' => array('value' => '')),
  5890. '/option',
  5891. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5892. '2007',
  5893. '/option',
  5894. array('option' => array('value' => '2006')),
  5895. '2006',
  5896. '/option',
  5897. '/select',
  5898. );
  5899. $this->assertTags($result, $expected);
  5900. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5901. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5902. $expected = array(
  5903. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5904. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5905. '2007',
  5906. '/option',
  5907. array('option' => array('value' => '2006')),
  5908. '2006',
  5909. '/option',
  5910. '/select',
  5911. );
  5912. $this->assertTags($result, $expected);
  5913. $this->Form->request->data['Contact']['published'] = '';
  5914. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5915. $expected = array(
  5916. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5917. array('option' => array('value' => '2008')),
  5918. '2008',
  5919. '/option',
  5920. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5921. '2007',
  5922. '/option',
  5923. array('option' => array('value' => '2006')),
  5924. '2006',
  5925. '/option',
  5926. '/select',
  5927. );
  5928. $this->assertTags($result, $expected);
  5929. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5930. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5931. $expected = array(
  5932. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5933. array('option' => array('value' => '2008')),
  5934. '2008',
  5935. '/option',
  5936. array('option' => array('value' => '2007')),
  5937. '2007',
  5938. '/option',
  5939. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5940. '2006',
  5941. '/option',
  5942. '/select',
  5943. );
  5944. $this->assertTags($result, $expected);
  5945. $this->Form->request->data = array();
  5946. $this->Form->create('Contact');
  5947. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5948. $expected = array(
  5949. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5950. array('option' => array('value' => '2008')),
  5951. '2008',
  5952. '/option',
  5953. array('option' => array('value' => '2007')),
  5954. '2007',
  5955. '/option',
  5956. array('option' => array('value' => '2006')),
  5957. '2006',
  5958. '/option',
  5959. '/select',
  5960. );
  5961. $this->assertTags($result, $expected);
  5962. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5963. $this->assertContains('data[Contact][published][year]', $result);
  5964. }
  5965. /**
  5966. * testYearAutoExpandRange method
  5967. *
  5968. * @return void
  5969. */
  5970. public function testYearAutoExpandRange() {
  5971. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  5972. $result = $this->Form->year('User.birthday');
  5973. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5974. $result = $matches[1];
  5975. $expected = range(date('Y') + 20, 1930);
  5976. $this->assertEquals($result, $expected);
  5977. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5978. $result = $this->Form->year('Project.release');
  5979. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5980. $result = $matches[1];
  5981. $expected = range(2050, date('Y') - 20);
  5982. $this->assertEquals($result, $expected);
  5983. $this->Form->request->data['Project']['release'] = '1881-10-10';
  5984. $result = $this->Form->year('Project.release', 1890, 1900);
  5985. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5986. $result = $matches[1];
  5987. $expected = range(1900, 1881);
  5988. $this->assertEquals($result, $expected);
  5989. }
  5990. /**
  5991. * testTextArea method
  5992. *
  5993. * @return void
  5994. */
  5995. public function testTextArea() {
  5996. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5997. $result = $this->Form->textarea('Model.field');
  5998. $expected = array(
  5999. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6000. 'some test data',
  6001. '/textarea',
  6002. );
  6003. $this->assertTags($result, $expected);
  6004. $result = $this->Form->textarea('Model.tmp');
  6005. $expected = array(
  6006. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6007. '/textarea',
  6008. );
  6009. $this->assertTags($result, $expected);
  6010. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6011. $result = $this->Form->textarea('Model.field');
  6012. $expected = array(
  6013. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6014. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6015. '/textarea',
  6016. );
  6017. $this->assertTags($result, $expected);
  6018. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6019. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6020. $expected = array(
  6021. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6022. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6023. '/textarea',
  6024. );
  6025. $this->assertTags($result, $expected);
  6026. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6027. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6028. $expected = array(
  6029. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6030. '/textarea'
  6031. );
  6032. $this->assertTags($result, $expected);
  6033. }
  6034. /**
  6035. * testTextAreaWithStupidCharacters method
  6036. *
  6037. * test text area with non-ascii characters
  6038. *
  6039. * @return void
  6040. */
  6041. public function testTextAreaWithStupidCharacters() {
  6042. $this->loadFixtures('Post');
  6043. $result = $this->Form->input('Post.content', array(
  6044. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6045. ));
  6046. $expected = array(
  6047. 'div' => array('class' => 'input textarea'),
  6048. 'label' => array('for' => 'PostContent'),
  6049. 'Current Text',
  6050. '/label',
  6051. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6052. 'GREAT®',
  6053. '/textarea',
  6054. '/div'
  6055. );
  6056. $this->assertTags($result, $expected);
  6057. }
  6058. /**
  6059. * testHiddenField method
  6060. *
  6061. * @return void
  6062. */
  6063. public function testHiddenField() {
  6064. $Contact = ClassRegistry::getObject('Contact');
  6065. $Contact->validationErrors['field'] = 1;
  6066. $this->Form->request->data['Contact']['field'] = 'test';
  6067. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6068. $this->assertTags($result, array(
  6069. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6070. );
  6071. }
  6072. /**
  6073. * testFileUploadField method
  6074. *
  6075. * @return void
  6076. */
  6077. public function testFileUploadField() {
  6078. $result = $this->Form->file('Model.upload');
  6079. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6080. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6081. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6082. $expected = array(
  6083. 'div' => array('class' => 'input file'),
  6084. 'label' => array('for' => 'ModelUpload'),
  6085. 'Upload',
  6086. '/label',
  6087. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6088. '/div'
  6089. );
  6090. $this->assertTags($result, $expected);
  6091. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6092. $result = $this->Form->file('Model.upload');
  6093. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6094. }
  6095. /**
  6096. * test File upload input on a model not used in create();
  6097. *
  6098. * @return void
  6099. */
  6100. public function testFileUploadOnOtherModel() {
  6101. $this->Form->create('ValidateUser', array('type' => 'file'));
  6102. $result = $this->Form->file('ValidateProfile.city');
  6103. $expected = array(
  6104. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6105. );
  6106. $this->assertTags($result, $expected);
  6107. }
  6108. /**
  6109. * testButton method
  6110. *
  6111. * @return void
  6112. */
  6113. public function testButton() {
  6114. $result = $this->Form->button('Hi');
  6115. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6116. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6117. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6118. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6119. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6120. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6121. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6122. $result = $this->Form->button('No type', array('type' => false));
  6123. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6124. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6125. $this->assertNotRegExp('/\&039/', $result);
  6126. }
  6127. /**
  6128. * Test that button() makes unlocked fields by default.
  6129. *
  6130. * @return void
  6131. */
  6132. public function testButtonUnlockedByDefault() {
  6133. $this->Form->request->params['_Token']['key'] = 'secured';
  6134. $this->Form->button('Save', array('name' => 'save'));
  6135. $this->Form->button('Clear');
  6136. $result = $this->Form->unlockField();
  6137. $this->assertEquals(array('save'), $result);
  6138. }
  6139. /**
  6140. * testPostButton method
  6141. *
  6142. * @return void
  6143. */
  6144. public function testPostButton() {
  6145. $result = $this->Form->postButton('Hi', '/controller/action');
  6146. $this->assertTags($result, array(
  6147. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6148. 'div' => array('style' => 'display:none;'),
  6149. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6150. '/div',
  6151. 'button' => array('type' => 'submit'),
  6152. 'Hi',
  6153. '/button',
  6154. '/form'
  6155. ));
  6156. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6157. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6158. }
  6159. /**
  6160. * Test that postButton adds _Token fields.
  6161. *
  6162. * @return void
  6163. */
  6164. public function testSecurePostButton() {
  6165. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6166. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6167. $expected = array(
  6168. 'form' => array(
  6169. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6170. ),
  6171. array('div' => array('style' => 'display:none;')),
  6172. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6173. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6174. '/div',
  6175. 'button' => array('type' => 'submit'),
  6176. 'Delete',
  6177. '/button',
  6178. array('div' => array('style' => 'display:none;')),
  6179. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6180. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6181. '/div',
  6182. '/form',
  6183. );
  6184. $this->assertTags($result, $expected);
  6185. }
  6186. /**
  6187. * testPostLink method
  6188. *
  6189. * @return void
  6190. */
  6191. public function testPostLink() {
  6192. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6193. $this->assertTags($result, array(
  6194. 'form' => array(
  6195. 'method' => 'post', 'action' => '/posts/delete/1',
  6196. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6197. ),
  6198. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6199. '/form',
  6200. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6201. 'Delete',
  6202. '/a'
  6203. ));
  6204. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6205. $this->assertTags($result, array(
  6206. 'form' => array(
  6207. 'method' => 'post', 'action' => '/posts/delete/1',
  6208. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6209. ),
  6210. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6211. '/form',
  6212. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6213. 'Delete',
  6214. '/a'
  6215. ));
  6216. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6217. $this->assertTags($result, array(
  6218. 'form' => array(
  6219. 'method' => 'post', 'action' => '/posts/delete/1',
  6220. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6221. ),
  6222. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6223. '/form',
  6224. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6225. 'Delete',
  6226. '/a'
  6227. ));
  6228. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6229. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6230. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6231. $this->assertTags($result, array(
  6232. 'form' => array(
  6233. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6234. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6235. ),
  6236. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6237. '/form',
  6238. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6239. 'Delete',
  6240. '/a'
  6241. ));
  6242. }
  6243. /**
  6244. * Test that postLink adds _Token fields.
  6245. *
  6246. * @return void
  6247. */
  6248. public function testSecurePostLink() {
  6249. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6250. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6251. $expected = array(
  6252. 'form' => array(
  6253. 'method' => 'post', 'action' => '/posts/delete/1',
  6254. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6255. ),
  6256. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6257. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6258. 'div' => array('style' => 'display:none;'),
  6259. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6260. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6261. '/div',
  6262. '/form',
  6263. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6264. 'Delete',
  6265. '/a'
  6266. );
  6267. $this->assertTags($result, $expected);
  6268. }
  6269. /**
  6270. * testSubmitButton method
  6271. *
  6272. * @return void
  6273. */
  6274. public function testSubmitButton() {
  6275. $result = $this->Form->submit('');
  6276. $expected = array(
  6277. 'div' => array('class' => 'submit'),
  6278. 'input' => array('type' => 'submit', 'value' => ''),
  6279. '/div'
  6280. );
  6281. $this->assertTags($result, $expected);
  6282. $result = $this->Form->submit('Test Submit');
  6283. $expected = array(
  6284. 'div' => array('class' => 'submit'),
  6285. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6286. '/div'
  6287. );
  6288. $this->assertTags($result, $expected);
  6289. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6290. $expected = array(
  6291. 'span' => array('class' => 'submit'),
  6292. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6293. '/span'
  6294. );
  6295. $this->assertTags($result, $expected);
  6296. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6297. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6298. $this->assertTags($result, $expected);
  6299. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6300. $expected = array(
  6301. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6302. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6303. '/div'
  6304. );
  6305. $this->assertTags($result, $expected);
  6306. $result = $this->Form->submit('Next >');
  6307. $expected = array(
  6308. 'div' => array('class' => 'submit'),
  6309. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6310. '/div'
  6311. );
  6312. $this->assertTags($result, $expected);
  6313. $result = $this->Form->submit('Next >', array('escape' => false));
  6314. $expected = array(
  6315. 'div' => array('class' => 'submit'),
  6316. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6317. '/div'
  6318. );
  6319. $this->assertTags($result, $expected);
  6320. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6321. $expected = array(
  6322. 'div' => array('class' => 'submit'),
  6323. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6324. '/div'
  6325. );
  6326. $this->assertTags($result, $expected);
  6327. $before = '--before--';
  6328. $after = '--after--';
  6329. $result = $this->Form->submit('Test', array('before' => $before));
  6330. $expected = array(
  6331. 'div' => array('class' => 'submit'),
  6332. '--before--',
  6333. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6334. '/div'
  6335. );
  6336. $this->assertTags($result, $expected);
  6337. $result = $this->Form->submit('Test', array('after' => $after));
  6338. $expected = array(
  6339. 'div' => array('class' => 'submit'),
  6340. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6341. '--after--',
  6342. '/div'
  6343. );
  6344. $this->assertTags($result, $expected);
  6345. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6346. $expected = array(
  6347. 'div' => array('class' => 'submit'),
  6348. '--before--',
  6349. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6350. '--after--',
  6351. '/div'
  6352. );
  6353. $this->assertTags($result, $expected);
  6354. }
  6355. /**
  6356. * test image submit types.
  6357. *
  6358. * @return void
  6359. */
  6360. public function testSubmitImage() {
  6361. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6362. $expected = array(
  6363. 'div' => array('class' => 'submit'),
  6364. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6365. '/div'
  6366. );
  6367. $this->assertTags($result, $expected);
  6368. $result = $this->Form->submit('/relative/cake.power.gif');
  6369. $expected = array(
  6370. 'div' => array('class' => 'submit'),
  6371. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  6372. '/div'
  6373. );
  6374. $this->assertTags($result, $expected);
  6375. $result = $this->Form->submit('cake.power.gif');
  6376. $expected = array(
  6377. 'div' => array('class' => 'submit'),
  6378. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6379. '/div'
  6380. );
  6381. $this->assertTags($result, $expected);
  6382. $result = $this->Form->submit('Not.an.image');
  6383. $expected = array(
  6384. 'div' => array('class' => 'submit'),
  6385. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6386. '/div'
  6387. );
  6388. $this->assertTags($result, $expected);
  6389. $after = '--after--';
  6390. $before = '--before--';
  6391. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6392. $expected = array(
  6393. 'div' => array('class' => 'submit'),
  6394. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6395. '--after--',
  6396. '/div'
  6397. );
  6398. $this->assertTags($result, $expected);
  6399. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6400. $expected = array(
  6401. 'div' => array('class' => 'submit'),
  6402. '--before--',
  6403. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6404. '/div'
  6405. );
  6406. $this->assertTags($result, $expected);
  6407. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6408. $expected = array(
  6409. 'div' => array('class' => 'submit'),
  6410. '--before--',
  6411. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6412. '--after--',
  6413. '/div'
  6414. );
  6415. $this->assertTags($result, $expected);
  6416. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6417. $expected = array(
  6418. 'div' => array('class' => 'submit'),
  6419. '--before--',
  6420. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6421. '--after--',
  6422. '/div'
  6423. );
  6424. $this->assertTags($result, $expected);
  6425. }
  6426. /**
  6427. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6428. * be submitted at a time.
  6429. *
  6430. * @return void
  6431. */
  6432. public function testSubmitUnlockedByDefault() {
  6433. $this->Form->request->params['_Token']['key'] = 'secured';
  6434. $this->Form->submit('Go go');
  6435. $this->Form->submit('Save', array('name' => 'save'));
  6436. $result = $this->Form->unlockField();
  6437. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6438. }
  6439. /**
  6440. * Test submit image with timestamps.
  6441. *
  6442. * @return void
  6443. */
  6444. public function testSubmitImageTimestamp() {
  6445. Configure::write('Asset.timestamp', 'force');
  6446. $result = $this->Form->submit('cake.power.gif');
  6447. $expected = array(
  6448. 'div' => array('class' => 'submit'),
  6449. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6450. '/div'
  6451. );
  6452. $this->assertTags($result, $expected);
  6453. }
  6454. /**
  6455. * test the create() method
  6456. *
  6457. * @return void
  6458. */
  6459. public function testCreate() {
  6460. $result = $this->Form->create('Contact');
  6461. $encoding = strtolower(Configure::read('App.encoding'));
  6462. $expected = array(
  6463. 'form' => array(
  6464. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6465. 'accept-charset' => $encoding
  6466. ),
  6467. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6468. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6469. '/div'
  6470. );
  6471. $this->assertTags($result, $expected);
  6472. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6473. $expected = array('form' => array(
  6474. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6475. 'accept-charset' => $encoding
  6476. ));
  6477. $this->assertTags($result, $expected);
  6478. $result = $this->Form->create('Contact', array('type' => 'get'));
  6479. $expected = array('form' => array(
  6480. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6481. 'accept-charset' => $encoding
  6482. ));
  6483. $this->assertTags($result, $expected);
  6484. $result = $this->Form->create('Contact', array('type' => 'put'));
  6485. $expected = array(
  6486. 'form' => array(
  6487. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6488. 'accept-charset' => $encoding
  6489. ),
  6490. 'div' => array('style' => 'display:none;'),
  6491. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6492. '/div'
  6493. );
  6494. $this->assertTags($result, $expected);
  6495. $result = $this->Form->create('Contact', array('type' => 'file'));
  6496. $expected = array(
  6497. 'form' => array(
  6498. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6499. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6500. ),
  6501. 'div' => array('style' => 'display:none;'),
  6502. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6503. '/div'
  6504. );
  6505. $this->assertTags($result, $expected);
  6506. $this->Form->request->data['Contact']['id'] = 1;
  6507. $this->Form->request->here = '/contacts/edit/1';
  6508. $this->Form->request['action'] = 'edit';
  6509. $result = $this->Form->create('Contact');
  6510. $expected = array(
  6511. 'form' => array(
  6512. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6513. 'accept-charset' => $encoding
  6514. ),
  6515. 'div' => array('style' => 'display:none;'),
  6516. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6517. '/div'
  6518. );
  6519. $this->assertTags($result, $expected);
  6520. $this->Form->request->data['Contact']['id'] = 1;
  6521. $this->Form->request->here = '/contacts/edit/1';
  6522. $this->Form->request['action'] = 'edit';
  6523. $result = $this->Form->create('Contact', array('type' => 'file'));
  6524. $expected = array(
  6525. 'form' => array(
  6526. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6527. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6528. ),
  6529. 'div' => array('style' => 'display:none;'),
  6530. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6531. '/div'
  6532. );
  6533. $this->assertTags($result, $expected);
  6534. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6535. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6536. $expected = array(
  6537. 'form' => array(
  6538. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6539. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  6540. ),
  6541. 'div' => array('style' => 'display:none;'),
  6542. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6543. '/div'
  6544. );
  6545. $this->assertTags($result, $expected);
  6546. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6547. $expected = array(
  6548. 'form' => array(
  6549. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6550. 'accept-charset' => $encoding
  6551. ),
  6552. 'div' => array('style' => 'display:none;'),
  6553. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6554. '/div'
  6555. );
  6556. $this->assertTags($result, $expected);
  6557. $this->Form->request['action'] = 'add';
  6558. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6559. $expected = array(
  6560. 'form' => array(
  6561. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6562. 'accept-charset' => $encoding
  6563. ),
  6564. 'div' => array('style' => 'display:none;'),
  6565. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6566. '/div'
  6567. );
  6568. $this->assertTags($result, $expected);
  6569. $result = $this->Form->create('User', array('action' => 'login'));
  6570. $expected = array(
  6571. 'form' => array(
  6572. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6573. 'accept-charset' => $encoding
  6574. ),
  6575. 'div' => array('style' => 'display:none;'),
  6576. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6577. '/div'
  6578. );
  6579. $this->assertTags($result, $expected);
  6580. $result = $this->Form->create('User', array('url' => '/users/login'));
  6581. $expected = array(
  6582. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6583. 'div' => array('style' => 'display:none;'),
  6584. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6585. '/div'
  6586. );
  6587. $this->assertTags($result, $expected);
  6588. $this->Form->request['controller'] = 'pages';
  6589. $result = $this->Form->create('User', array('action' => 'signup'));
  6590. $expected = array(
  6591. 'form' => array(
  6592. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6593. 'accept-charset' => $encoding
  6594. ),
  6595. 'div' => array('style' => 'display:none;'),
  6596. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6597. '/div'
  6598. );
  6599. $this->assertTags($result, $expected);
  6600. $this->Form->request->data = array();
  6601. $this->Form->request['controller'] = 'contacts';
  6602. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6603. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6604. $expected = array(
  6605. 'form' => array(
  6606. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6607. 'accept-charset' => 'utf-8'
  6608. ),
  6609. 'div' => array('style' => 'display:none;'),
  6610. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6611. '/div'
  6612. );
  6613. $this->assertTags($result, $expected);
  6614. }
  6615. /**
  6616. * Test the onsubmit option for create()
  6617. *
  6618. * @return void
  6619. */
  6620. public function testCreateOnSubmit() {
  6621. $this->Form->request->data = array();
  6622. $this->Form->request['controller'] = 'contacts';
  6623. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6624. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6625. $expected = array(
  6626. 'form' => array(
  6627. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6628. 'accept-charset' => 'utf-8'
  6629. ),
  6630. 'div' => array('style' => 'display:none;'),
  6631. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6632. '/div'
  6633. );
  6634. $this->assertTags($result, $expected);
  6635. $this->Form->request->data = array();
  6636. $this->Form->request['controller'] = 'contacts';
  6637. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6638. $result = $this->Form->create(array(
  6639. 'url' => array('action' => 'index', 'param'),
  6640. 'default' => false,
  6641. 'onsubmit' => 'someFunction();'
  6642. ));
  6643. $expected = array(
  6644. 'form' => array(
  6645. 'id' => 'ContactAddForm', 'method' => 'post',
  6646. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6647. 'action' => '/contacts/index/param',
  6648. 'accept-charset' => 'utf-8'
  6649. ),
  6650. 'div' => array('style' => 'display:none;'),
  6651. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6652. '/div'
  6653. );
  6654. $this->assertTags($result, $expected);
  6655. }
  6656. /**
  6657. * test create() with automatic url generation
  6658. *
  6659. * @return void
  6660. */
  6661. public function testCreateAutoUrl() {
  6662. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6663. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6664. $this->Form->request->base = '/base_url';
  6665. $result = $this->Form->create('Contact');
  6666. $expected = array(
  6667. 'form' => array(
  6668. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6669. 'accept-charset' => 'utf-8'
  6670. ),
  6671. 'div' => array('style' => 'display:none;'),
  6672. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6673. '/div'
  6674. );
  6675. $this->assertTags($result, $expected);
  6676. $this->Form->request['action'] = 'delete';
  6677. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6678. $this->Form->request->base = '/base_url';
  6679. $result = $this->Form->create('Contact');
  6680. $expected = array(
  6681. 'form' => array(
  6682. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6683. 'accept-charset' => 'utf-8'
  6684. ),
  6685. 'div' => array('style' => 'display:none;'),
  6686. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6687. '/div'
  6688. );
  6689. $this->assertTags($result, $expected);
  6690. }
  6691. /**
  6692. * test create() with a custom route
  6693. *
  6694. * @return void
  6695. */
  6696. public function testCreateCustomRoute() {
  6697. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6698. $encoding = strtolower(Configure::read('App.encoding'));
  6699. $result = $this->Form->create('User', array('action' => 'login'));
  6700. $expected = array(
  6701. 'form' => array(
  6702. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6703. 'accept-charset' => $encoding
  6704. ),
  6705. 'div' => array('style' => 'display:none;'),
  6706. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6707. '/div'
  6708. );
  6709. $this->assertTags($result, $expected);
  6710. }
  6711. /**
  6712. * test that inputDefaults are stored and used.
  6713. *
  6714. * @return void
  6715. */
  6716. public function testCreateWithInputDefaults() {
  6717. $this->Form->create('User', array(
  6718. 'inputDefaults' => array(
  6719. 'div' => false,
  6720. 'label' => false,
  6721. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6722. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6723. )
  6724. ));
  6725. $result = $this->Form->input('username');
  6726. $expected = array(
  6727. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6728. );
  6729. $this->assertTags($result, $expected);
  6730. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6731. $expected = array(
  6732. 'div' => array('class' => 'input text'),
  6733. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6734. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6735. '/div'
  6736. );
  6737. $this->assertTags($result, $expected);
  6738. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6739. $expected = array(
  6740. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6741. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6742. );
  6743. $this->assertTags($result, $expected);
  6744. $this->Form->create('User', array(
  6745. 'inputDefaults' => array(
  6746. 'div' => false,
  6747. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6748. )
  6749. ));
  6750. $result = $this->Form->input('username', array('div' => true));
  6751. $expected = array(
  6752. 'div' => array('class' => 'input text'),
  6753. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6754. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6755. '/div'
  6756. );
  6757. $this->assertTags($result, $expected);
  6758. }
  6759. /**
  6760. * test automatic accept-charset overriding
  6761. *
  6762. * @return void
  6763. */
  6764. public function testCreateWithAcceptCharset() {
  6765. $result = $this->Form->create('UserForm', array(
  6766. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6767. )
  6768. );
  6769. $expected = array(
  6770. 'form' => array(
  6771. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6772. 'accept-charset' => 'iso-8859-1'
  6773. ),
  6774. 'div' => array('style' => 'display:none;'),
  6775. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6776. '/div'
  6777. );
  6778. $this->assertTags($result, $expected);
  6779. }
  6780. /**
  6781. * Test base form url when url param is passed with multiple parameters (&)
  6782. *
  6783. */
  6784. public function testCreateQuerystringrequest() {
  6785. $encoding = strtolower(Configure::read('App.encoding'));
  6786. $result = $this->Form->create('Contact', array(
  6787. 'type' => 'post',
  6788. 'escape' => false,
  6789. 'url' => array(
  6790. 'controller' => 'controller',
  6791. 'action' => 'action',
  6792. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6793. )
  6794. ));
  6795. $expected = array(
  6796. 'form' => array(
  6797. 'id' => 'ContactAddForm',
  6798. 'method' => 'post',
  6799. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6800. 'accept-charset' => $encoding
  6801. ),
  6802. 'div' => array('style' => 'display:none;'),
  6803. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6804. '/div'
  6805. );
  6806. $this->assertTags($result, $expected);
  6807. $result = $this->Form->create('Contact', array(
  6808. 'type' => 'post',
  6809. 'url' => array(
  6810. 'controller' => 'controller',
  6811. 'action' => 'action',
  6812. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6813. )
  6814. ));
  6815. $expected = array(
  6816. 'form' => array(
  6817. 'id' => 'ContactAddForm',
  6818. 'method' => 'post',
  6819. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6820. 'accept-charset' => $encoding
  6821. ),
  6822. 'div' => array('style' => 'display:none;'),
  6823. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6824. '/div'
  6825. );
  6826. $this->assertTags($result, $expected);
  6827. }
  6828. /**
  6829. * test that create() doesn't cause errors by multiple id's being in the primary key
  6830. * as could happen with multiple select or checkboxes.
  6831. *
  6832. * @return void
  6833. */
  6834. public function testCreateWithMultipleIdInData() {
  6835. $encoding = strtolower(Configure::read('App.encoding'));
  6836. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6837. $result = $this->Form->create('Contact');
  6838. $expected = array(
  6839. 'form' => array(
  6840. 'id' => 'ContactAddForm',
  6841. 'method' => 'post',
  6842. 'action' => '/contacts/add',
  6843. 'accept-charset' => $encoding
  6844. ),
  6845. 'div' => array('style' => 'display:none;'),
  6846. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6847. '/div'
  6848. );
  6849. $this->assertTags($result, $expected);
  6850. }
  6851. /**
  6852. * test that create() doesn't add in extra passed params.
  6853. *
  6854. * @return void
  6855. */
  6856. public function testCreatePassedArgs() {
  6857. $encoding = strtolower(Configure::read('App.encoding'));
  6858. $this->Form->request->data['Contact']['id'] = 1;
  6859. $result = $this->Form->create('Contact', array(
  6860. 'type' => 'post',
  6861. 'escape' => false,
  6862. 'url' => array(
  6863. 'action' => 'edit',
  6864. 'myparam'
  6865. )
  6866. ));
  6867. $expected = array(
  6868. 'form' => array(
  6869. 'id' => 'ContactAddForm',
  6870. 'method' => 'post',
  6871. 'action' => '/contacts/edit/myparam',
  6872. 'accept-charset' => $encoding
  6873. ),
  6874. 'div' => array('style' => 'display:none;'),
  6875. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6876. '/div'
  6877. );
  6878. $this->assertTags($result, $expected);
  6879. }
  6880. /**
  6881. * test creating a get form, and get form inputs.
  6882. *
  6883. * @return void
  6884. */
  6885. public function testGetFormCreate() {
  6886. $encoding = strtolower(Configure::read('App.encoding'));
  6887. $result = $this->Form->create('Contact', array('type' => 'get'));
  6888. $this->assertTags($result, array('form' => array(
  6889. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6890. 'accept-charset' => $encoding
  6891. )));
  6892. $result = $this->Form->text('Contact.name');
  6893. $this->assertTags($result, array('input' => array(
  6894. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6895. )));
  6896. $result = $this->Form->password('password');
  6897. $this->assertTags($result, array('input' => array(
  6898. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6899. )));
  6900. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6901. $result = $this->Form->text('user_form');
  6902. $this->assertTags($result, array('input' => array(
  6903. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6904. )));
  6905. }
  6906. /**
  6907. * test get form, and inputs when the model param is false
  6908. *
  6909. * @return void
  6910. */
  6911. public function testGetFormWithFalseModel() {
  6912. $encoding = strtolower(Configure::read('App.encoding'));
  6913. $this->Form->request['controller'] = 'contact_test';
  6914. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6915. $expected = array('form' => array(
  6916. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6917. 'accept-charset' => $encoding
  6918. ));
  6919. $this->assertTags($result, $expected);
  6920. $result = $this->Form->text('reason');
  6921. $expected = array(
  6922. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6923. );
  6924. $this->assertTags($result, $expected);
  6925. }
  6926. /**
  6927. * test that datetime() works with GET style forms.
  6928. *
  6929. * @return void
  6930. */
  6931. public function testDateTimeWithGetForms() {
  6932. extract($this->dateRegex);
  6933. $this->Form->create('Contact', array('type' => 'get'));
  6934. $result = $this->Form->datetime('created');
  6935. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6936. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6937. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6938. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6939. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6940. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6941. }
  6942. /**
  6943. * testEditFormWithData method
  6944. *
  6945. * test auto populating form elements from submitted data.
  6946. *
  6947. * @return void
  6948. */
  6949. public function testEditFormWithData() {
  6950. $this->Form->request->data = array('Person' => array(
  6951. 'id' => 1,
  6952. 'first_name' => 'Nate',
  6953. 'last_name' => 'Abele',
  6954. 'email' => 'nate@example.com'
  6955. ));
  6956. $this->Form->request->addParams(array(
  6957. 'models' => array('Person'),
  6958. 'controller' => 'people',
  6959. 'action' => 'add'
  6960. ));
  6961. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6962. $this->Form->create();
  6963. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6964. $expected = array(
  6965. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6966. 'select' => array(
  6967. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6968. ),
  6969. array('option' => array('value' => 1)), 'Nate', '/option',
  6970. array('option' => array('value' => 2)), 'Garrett', '/option',
  6971. array('option' => array('value' => 3)), 'Larry', '/option',
  6972. '/select'
  6973. );
  6974. $this->assertTags($result, $expected);
  6975. }
  6976. /**
  6977. * Test that required fields are created for various types of validation.
  6978. *
  6979. * @return void
  6980. */
  6981. public function testFormInputRequiredDetection() {
  6982. $this->Form->create('Contact');
  6983. $result = $this->Form->input('Contact.non_existing');
  6984. $expected = array(
  6985. 'div' => array('class' => 'input text'),
  6986. 'label' => array('for' => 'ContactNonExisting'),
  6987. 'Non Existing',
  6988. '/label',
  6989. 'input' => array(
  6990. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6991. 'id' => 'ContactNonExisting'
  6992. ),
  6993. '/div'
  6994. );
  6995. $this->assertTags($result, $expected);
  6996. $result = $this->Form->input('Contact.imrequired');
  6997. $expected = array(
  6998. 'div' => array('class' => 'input text required'),
  6999. 'label' => array('for' => 'ContactImrequired'),
  7000. 'Imrequired',
  7001. '/label',
  7002. 'input' => array(
  7003. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7004. 'id' => 'ContactImrequired',
  7005. 'required' => 'required'
  7006. ),
  7007. '/div'
  7008. );
  7009. $this->assertTags($result, $expected);
  7010. $result = $this->Form->input('Contact.imalsorequired');
  7011. $expected = array(
  7012. 'div' => array('class' => 'input text required'),
  7013. 'label' => array('for' => 'ContactImalsorequired'),
  7014. 'Imalsorequired',
  7015. '/label',
  7016. 'input' => array(
  7017. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7018. 'id' => 'ContactImalsorequired',
  7019. 'required' => 'required'
  7020. ),
  7021. '/div'
  7022. );
  7023. $this->assertTags($result, $expected);
  7024. $result = $this->Form->input('Contact.imrequiredtoo');
  7025. $expected = array(
  7026. 'div' => array('class' => 'input text required'),
  7027. 'label' => array('for' => 'ContactImrequiredtoo'),
  7028. 'Imrequiredtoo',
  7029. '/label',
  7030. 'input' => array(
  7031. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7032. 'id' => 'ContactImrequiredtoo',
  7033. 'required' => 'required'
  7034. ),
  7035. '/div'
  7036. );
  7037. $this->assertTags($result, $expected);
  7038. $result = $this->Form->input('Contact.required_one');
  7039. $expected = array(
  7040. 'div' => array('class' => 'input text required'),
  7041. 'label' => array('for' => 'ContactRequiredOne'),
  7042. 'Required One',
  7043. '/label',
  7044. 'input' => array(
  7045. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7046. 'id' => 'ContactRequiredOne',
  7047. 'required' => 'required'
  7048. ),
  7049. '/div'
  7050. );
  7051. $this->assertTags($result, $expected);
  7052. $result = $this->Form->input('Contact.string_required');
  7053. $expected = array(
  7054. 'div' => array('class' => 'input text required'),
  7055. 'label' => array('for' => 'ContactStringRequired'),
  7056. 'String Required',
  7057. '/label',
  7058. 'input' => array(
  7059. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7060. 'id' => 'ContactStringRequired',
  7061. 'required' => 'required'
  7062. ),
  7063. '/div'
  7064. );
  7065. $this->assertTags($result, $expected);
  7066. $result = $this->Form->input('Contact.imnotrequired');
  7067. $expected = array(
  7068. 'div' => array('class' => 'input text'),
  7069. 'label' => array('for' => 'ContactImnotrequired'),
  7070. 'Imnotrequired',
  7071. '/label',
  7072. 'input' => array(
  7073. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7074. 'id' => 'ContactImnotrequired'
  7075. ),
  7076. '/div'
  7077. );
  7078. $this->assertTags($result, $expected);
  7079. $result = $this->Form->input('Contact.imalsonotrequired');
  7080. $expected = array(
  7081. 'div' => array('class' => 'input text'),
  7082. 'label' => array('for' => 'ContactImalsonotrequired'),
  7083. 'Imalsonotrequired',
  7084. '/label',
  7085. 'input' => array(
  7086. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7087. 'id' => 'ContactImalsonotrequired'
  7088. ),
  7089. '/div'
  7090. );
  7091. $this->assertTags($result, $expected);
  7092. $result = $this->Form->input('Contact.imalsonotrequired2');
  7093. $expected = array(
  7094. 'div' => array('class' => 'input text'),
  7095. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7096. 'Imalsonotrequired2',
  7097. '/label',
  7098. 'input' => array(
  7099. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7100. 'id' => 'ContactImalsonotrequired2'
  7101. ),
  7102. '/div'
  7103. );
  7104. $this->assertTags($result, $expected);
  7105. $result = $this->Form->input('Contact.imnotrequiredeither');
  7106. $expected = array(
  7107. 'div' => array('class' => 'input text'),
  7108. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7109. 'Imnotrequiredeither',
  7110. '/label',
  7111. 'input' => array(
  7112. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7113. 'id' => 'ContactImnotrequiredeither'
  7114. ),
  7115. '/div'
  7116. );
  7117. $this->assertTags($result, $expected);
  7118. $result = $this->Form->input('Contact.iamrequiredalways');
  7119. $expected = array(
  7120. 'div' => array('class' => 'input text required'),
  7121. 'label' => array('for' => 'ContactIamrequiredalways'),
  7122. 'Iamrequiredalways',
  7123. '/label',
  7124. 'input' => array(
  7125. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7126. 'id' => 'ContactIamrequiredalways',
  7127. 'required' => 'required'
  7128. ),
  7129. '/div'
  7130. );
  7131. $this->assertTags($result, $expected);
  7132. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7133. $expected = array(
  7134. 'div' => array('class' => 'input checkbox required'),
  7135. array('input' => array(
  7136. 'type' => 'hidden',
  7137. 'name' => 'data[Contact][boolean_field]',
  7138. 'id' => 'ContactBooleanField_',
  7139. 'value' => '0'
  7140. )),
  7141. array('input' => array(
  7142. 'type' => 'checkbox',
  7143. 'name' => 'data[Contact][boolean_field]',
  7144. 'value' => '1',
  7145. 'id' => 'ContactBooleanField'
  7146. )),
  7147. 'label' => array('for' => 'ContactBooleanField'),
  7148. 'Boolean Field',
  7149. '/label',
  7150. '/div'
  7151. );
  7152. $this->assertTags($result, $expected);
  7153. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7154. $expected = array(
  7155. 'div' => array('class' => 'input checkbox required'),
  7156. array('input' => array(
  7157. 'type' => 'hidden',
  7158. 'name' => 'data[Contact][boolean_field]',
  7159. 'id' => 'ContactBooleanField_',
  7160. 'value' => '0'
  7161. )),
  7162. array('input' => array(
  7163. 'type' => 'checkbox',
  7164. 'name' => 'data[Contact][boolean_field]',
  7165. 'value' => '1',
  7166. 'id' => 'ContactBooleanField',
  7167. 'required' => 'required'
  7168. )),
  7169. 'label' => array('for' => 'ContactBooleanField'),
  7170. 'Boolean Field',
  7171. '/label',
  7172. '/div'
  7173. );
  7174. $this->assertTags($result, $expected);
  7175. }
  7176. /**
  7177. * testFormMagicInput method
  7178. *
  7179. * @return void
  7180. */
  7181. public function testFormMagicInput() {
  7182. $encoding = strtolower(Configure::read('App.encoding'));
  7183. $result = $this->Form->create('Contact');
  7184. $expected = array(
  7185. 'form' => array(
  7186. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7187. 'accept-charset' => $encoding
  7188. ),
  7189. 'div' => array('style' => 'display:none;'),
  7190. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7191. '/div'
  7192. );
  7193. $this->assertTags($result, $expected);
  7194. $result = $this->Form->input('name');
  7195. $expected = array(
  7196. 'div' => array('class' => 'input text'),
  7197. 'label' => array('for' => 'ContactName'),
  7198. 'Name',
  7199. '/label',
  7200. 'input' => array(
  7201. 'type' => 'text', 'name' => 'data[Contact][name]',
  7202. 'id' => 'ContactName', 'maxlength' => '255'
  7203. ),
  7204. '/div'
  7205. );
  7206. $this->assertTags($result, $expected);
  7207. $result = $this->Form->input('non_existing_field_in_contact_model');
  7208. $expected = array(
  7209. 'div' => array('class' => 'input text'),
  7210. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7211. 'Non Existing Field In Contact Model',
  7212. '/label',
  7213. 'input' => array(
  7214. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  7215. 'id' => 'ContactNonExistingFieldInContactModel'
  7216. ),
  7217. '/div'
  7218. );
  7219. $this->assertTags($result, $expected);
  7220. $result = $this->Form->input('Address.street');
  7221. $expected = array(
  7222. 'div' => array('class' => 'input text'),
  7223. 'label' => array('for' => 'AddressStreet'),
  7224. 'Street',
  7225. '/label',
  7226. 'input' => array(
  7227. 'type' => 'text', 'name' => 'data[Address][street]',
  7228. 'id' => 'AddressStreet'
  7229. ),
  7230. '/div'
  7231. );
  7232. $this->assertTags($result, $expected);
  7233. $result = $this->Form->input('Address.non_existing_field_in_model');
  7234. $expected = array(
  7235. 'div' => array('class' => 'input text'),
  7236. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7237. 'Non Existing Field In Model',
  7238. '/label',
  7239. 'input' => array(
  7240. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  7241. 'id' => 'AddressNonExistingFieldInModel'
  7242. ),
  7243. '/div'
  7244. );
  7245. $this->assertTags($result, $expected);
  7246. $result = $this->Form->input('name', array('div' => false));
  7247. $expected = array(
  7248. 'label' => array('for' => 'ContactName'),
  7249. 'Name',
  7250. '/label',
  7251. 'input' => array(
  7252. 'type' => 'text', 'name' => 'data[Contact][name]',
  7253. 'id' => 'ContactName', 'maxlength' => '255'
  7254. )
  7255. );
  7256. $this->assertTags($result, $expected);
  7257. extract($this->dateRegex);
  7258. $now = strtotime('now');
  7259. $result = $this->Form->input('Contact.published', array('div' => false));
  7260. $expected = array(
  7261. 'label' => array('for' => 'ContactPublishedMonth'),
  7262. 'Published',
  7263. '/label',
  7264. array('select' => array(
  7265. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  7266. )),
  7267. $monthsRegex,
  7268. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7269. date('F', $now),
  7270. '/option',
  7271. '*/select',
  7272. '-',
  7273. array('select' => array(
  7274. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  7275. )),
  7276. $daysRegex,
  7277. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7278. date('j', $now),
  7279. '/option',
  7280. '*/select',
  7281. '-',
  7282. array('select' => array(
  7283. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  7284. )),
  7285. $yearsRegex,
  7286. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7287. date('Y', $now),
  7288. '*/select'
  7289. );
  7290. $this->assertTags($result, $expected);
  7291. $result = $this->Form->input('Contact.updated', array('div' => false));
  7292. $expected = array(
  7293. 'label' => array('for' => 'ContactUpdatedMonth'),
  7294. 'Updated',
  7295. '/label',
  7296. array('select' => array(
  7297. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  7298. )),
  7299. $monthsRegex,
  7300. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7301. date('F', $now),
  7302. '/option',
  7303. '*/select',
  7304. '-',
  7305. array('select' => array(
  7306. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  7307. )),
  7308. $daysRegex,
  7309. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7310. date('j', $now),
  7311. '/option',
  7312. '*/select',
  7313. '-',
  7314. array('select' => array(
  7315. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  7316. )),
  7317. $yearsRegex,
  7318. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7319. date('Y', $now),
  7320. '*/select'
  7321. );
  7322. $this->assertTags($result, $expected);
  7323. $result = $this->Form->input('UserForm.stuff');
  7324. $expected = array(
  7325. 'div' => array('class' => 'input text'),
  7326. 'label' => array('for' => 'UserFormStuff'),
  7327. 'Stuff',
  7328. '/label',
  7329. 'input' => array(
  7330. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  7331. 'id' => 'UserFormStuff', 'maxlength' => 10
  7332. ),
  7333. '/div'
  7334. );
  7335. $this->assertTags($result, $expected);
  7336. }
  7337. /**
  7338. * testForMagicInputNonExistingNorValidated method
  7339. *
  7340. * @return void
  7341. */
  7342. public function testForMagicInputNonExistingNorValidated() {
  7343. $encoding = strtolower(Configure::read('App.encoding'));
  7344. $result = $this->Form->create('Contact');
  7345. $expected = array(
  7346. 'form' => array(
  7347. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7348. 'accept-charset' => $encoding
  7349. ),
  7350. 'div' => array('style' => 'display:none;'),
  7351. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7352. '/div'
  7353. );
  7354. $this->assertTags($result, $expected);
  7355. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7356. $expected = array(
  7357. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7358. 'Non Existing Nor Validated',
  7359. '/label',
  7360. 'input' => array(
  7361. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7362. 'id' => 'ContactNonExistingNorValidated'
  7363. )
  7364. );
  7365. $this->assertTags($result, $expected);
  7366. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  7367. 'div' => false, 'value' => 'my value'
  7368. ));
  7369. $expected = array(
  7370. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7371. 'Non Existing Nor Validated',
  7372. '/label',
  7373. 'input' => array(
  7374. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7375. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  7376. )
  7377. );
  7378. $this->assertTags($result, $expected);
  7379. $this->Form->request->data = array(
  7380. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  7381. ));
  7382. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7383. $expected = array(
  7384. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7385. 'Non Existing Nor Validated',
  7386. '/label',
  7387. 'input' => array(
  7388. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7389. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  7390. )
  7391. );
  7392. $this->assertTags($result, $expected);
  7393. }
  7394. /**
  7395. * testFormMagicInputLabel method
  7396. *
  7397. * @return void
  7398. */
  7399. public function testFormMagicInputLabel() {
  7400. $encoding = strtolower(Configure::read('App.encoding'));
  7401. $result = $this->Form->create('Contact');
  7402. $expected = array(
  7403. 'form' => array(
  7404. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7405. 'accept-charset' => $encoding
  7406. ),
  7407. 'div' => array('style' => 'display:none;'),
  7408. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7409. '/div'
  7410. );
  7411. $this->assertTags($result, $expected);
  7412. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  7413. $this->assertTags($result, array('input' => array(
  7414. 'name' => 'data[Contact][name]', 'type' => 'text',
  7415. 'id' => 'ContactName', 'maxlength' => '255')
  7416. ));
  7417. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  7418. $expected = array(
  7419. 'label' => array('for' => 'ContactName'),
  7420. 'My label',
  7421. '/label',
  7422. 'input' => array(
  7423. 'type' => 'text', 'name' => 'data[Contact][name]',
  7424. 'id' => 'ContactName', 'maxlength' => '255'
  7425. )
  7426. );
  7427. $this->assertTags($result, $expected);
  7428. $result = $this->Form->input('Contact.name', array(
  7429. 'div' => false, 'label' => array('class' => 'mandatory')
  7430. ));
  7431. $expected = array(
  7432. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7433. 'Name',
  7434. '/label',
  7435. 'input' => array(
  7436. 'type' => 'text', 'name' => 'data[Contact][name]',
  7437. 'id' => 'ContactName', 'maxlength' => '255'
  7438. )
  7439. );
  7440. $this->assertTags($result, $expected);
  7441. $result = $this->Form->input('Contact.name', array(
  7442. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7443. ));
  7444. $expected = array(
  7445. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7446. 'My label',
  7447. '/label',
  7448. 'input' => array(
  7449. 'type' => 'text', 'name' => 'data[Contact][name]',
  7450. 'id' => 'ContactName', 'maxlength' => '255'
  7451. )
  7452. );
  7453. $this->assertTags($result, $expected);
  7454. $result = $this->Form->input('Contact.name', array(
  7455. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7456. ));
  7457. $expected = array(
  7458. 'label' => array('for' => 'my_id'),
  7459. 'Name',
  7460. '/label',
  7461. 'input' => array(
  7462. 'type' => 'text', 'name' => 'data[Contact][name]',
  7463. 'id' => 'my_id', 'maxlength' => '255'
  7464. )
  7465. );
  7466. $this->assertTags($result, $expected);
  7467. $result = $this->Form->input('1.id');
  7468. $this->assertTags($result, array('input' => array(
  7469. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7470. 'id' => 'Contact1Id'
  7471. )));
  7472. $result = $this->Form->input("1.name");
  7473. $expected = array(
  7474. 'div' => array('class' => 'input text'),
  7475. 'label' => array('for' => 'Contact1Name'),
  7476. 'Name',
  7477. '/label',
  7478. 'input' => array(
  7479. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  7480. 'id' => 'Contact1Name', 'maxlength' => '255'
  7481. ),
  7482. '/div'
  7483. );
  7484. $this->assertTags($result, $expected);
  7485. $result = $this->Form->input('Contact.1.id');
  7486. $this->assertTags($result, array(
  7487. 'input' => array(
  7488. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7489. 'id' => 'Contact1Id'
  7490. )
  7491. ));
  7492. $result = $this->Form->input("Model.1.name");
  7493. $expected = array(
  7494. 'div' => array('class' => 'input text'),
  7495. 'label' => array('for' => 'Model1Name'),
  7496. 'Name',
  7497. '/label',
  7498. 'input' => array(
  7499. 'type' => 'text', 'name' => 'data[Model][1][name]',
  7500. 'id' => 'Model1Name'
  7501. ),
  7502. '/div'
  7503. );
  7504. $this->assertTags($result, $expected);
  7505. }
  7506. /**
  7507. * testFormEnd method
  7508. *
  7509. * @return void
  7510. */
  7511. public function testFormEnd() {
  7512. $this->assertEquals('</form>', $this->Form->end());
  7513. $result = $this->Form->end('');
  7514. $expected = array(
  7515. 'div' => array('class' => 'submit'),
  7516. 'input' => array('type' => 'submit', 'value' => ''),
  7517. '/div',
  7518. '/form'
  7519. );
  7520. $this->assertTags($result, $expected);
  7521. $result = $this->Form->end(array('label' => ''));
  7522. $expected = array(
  7523. 'div' => array('class' => 'submit'),
  7524. 'input' => array('type' => 'submit', 'value' => ''),
  7525. '/div',
  7526. '/form'
  7527. );
  7528. $this->assertTags($result, $expected);
  7529. $result = $this->Form->end('save');
  7530. $expected = array(
  7531. 'div' => array('class' => 'submit'),
  7532. 'input' => array('type' => 'submit', 'value' => 'save'),
  7533. '/div',
  7534. '/form'
  7535. );
  7536. $this->assertTags($result, $expected);
  7537. $result = $this->Form->end(array('label' => 'save'));
  7538. $expected = array(
  7539. 'div' => array('class' => 'submit'),
  7540. 'input' => array('type' => 'submit', 'value' => 'save'),
  7541. '/div',
  7542. '/form'
  7543. );
  7544. $this->assertTags($result, $expected);
  7545. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7546. $expected = array(
  7547. 'div' => array('class' => 'submit'),
  7548. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7549. '/div',
  7550. '/form'
  7551. );
  7552. $this->assertTags($result, $expected);
  7553. $result = $this->Form->end(array('name' => 'Whatever'));
  7554. $expected = array(
  7555. 'div' => array('class' => 'submit'),
  7556. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7557. '/div',
  7558. '/form'
  7559. );
  7560. $this->assertTags($result, $expected);
  7561. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7562. $expected = array(
  7563. 'div' => array('class' => 'good'),
  7564. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7565. '/div',
  7566. '/form'
  7567. );
  7568. $this->assertTags($result, $expected);
  7569. $result = $this->Form->end(array(
  7570. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7571. ));
  7572. $expected = array(
  7573. 'div' => array('class' => 'good'),
  7574. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7575. '/div',
  7576. '/form'
  7577. );
  7578. $this->assertTags($result, $expected);
  7579. }
  7580. /**
  7581. * testMultipleFormWithIdFields method
  7582. *
  7583. * @return void
  7584. */
  7585. public function testMultipleFormWithIdFields() {
  7586. $this->Form->create('UserForm');
  7587. $result = $this->Form->input('id');
  7588. $this->assertTags($result, array('input' => array(
  7589. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7590. )));
  7591. $result = $this->Form->input('ValidateItem.id');
  7592. $this->assertTags($result, array('input' => array(
  7593. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7594. 'id' => 'ValidateItemId'
  7595. )));
  7596. $result = $this->Form->input('ValidateUser.id');
  7597. $this->assertTags($result, array('input' => array(
  7598. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7599. 'id' => 'ValidateUserId'
  7600. )));
  7601. }
  7602. /**
  7603. * testDbLessModel method
  7604. *
  7605. * @return void
  7606. */
  7607. public function testDbLessModel() {
  7608. $this->Form->create('TestMail');
  7609. $result = $this->Form->input('name');
  7610. $expected = array(
  7611. 'div' => array('class' => 'input text'),
  7612. 'label' => array('for' => 'TestMailName'),
  7613. 'Name',
  7614. '/label',
  7615. 'input' => array(
  7616. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7617. 'id' => 'TestMailName'
  7618. ),
  7619. '/div'
  7620. );
  7621. $this->assertTags($result, $expected);
  7622. ClassRegistry::init('TestMail');
  7623. $this->Form->create('TestMail');
  7624. $result = $this->Form->input('name');
  7625. $expected = array(
  7626. 'div' => array('class' => 'input text'),
  7627. 'label' => array('for' => 'TestMailName'),
  7628. 'Name',
  7629. '/label',
  7630. 'input' => array(
  7631. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7632. 'id' => 'TestMailName'
  7633. ),
  7634. '/div'
  7635. );
  7636. $this->assertTags($result, $expected);
  7637. }
  7638. /**
  7639. * testBrokenness method
  7640. *
  7641. * @return void
  7642. */
  7643. public function testBrokenness() {
  7644. /*
  7645. * #4 This test has two parents and four children. By default (as of r7117) both
  7646. * parents are show but the first parent is missing a child. This is the inconsistency
  7647. * in the default behaviour - one parent has all children, the other does not - dependent
  7648. * on the data values.
  7649. */
  7650. $result = $this->Form->select('Model.field', array(
  7651. 'Fred' => array(
  7652. 'freds_son_1' => 'Fred',
  7653. 'freds_son_2' => 'Freddie'
  7654. ),
  7655. 'Bert' => array(
  7656. 'berts_son_1' => 'Albert',
  7657. 'berts_son_2' => 'Bertie')
  7658. ),
  7659. array('showParents' => true, 'empty' => false)
  7660. );
  7661. $expected = array(
  7662. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7663. array('optgroup' => array('label' => 'Fred')),
  7664. array('option' => array('value' => 'freds_son_1')),
  7665. 'Fred',
  7666. '/option',
  7667. array('option' => array('value' => 'freds_son_2')),
  7668. 'Freddie',
  7669. '/option',
  7670. '/optgroup',
  7671. array('optgroup' => array('label' => 'Bert')),
  7672. array('option' => array('value' => 'berts_son_1')),
  7673. 'Albert',
  7674. '/option',
  7675. array('option' => array('value' => 'berts_son_2')),
  7676. 'Bertie',
  7677. '/option',
  7678. '/optgroup',
  7679. '/select'
  7680. );
  7681. $this->assertTags($result, $expected);
  7682. /*
  7683. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7684. * changed, so we should expect the same select list data, just with a different name
  7685. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7686. * This is where data corruption can occur, because when a select value is missing from
  7687. * a list a form will substitute the first value in the list - without the user knowing.
  7688. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7689. * affect the availability of 3 => 'Three' as a valid option.
  7690. */
  7691. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7692. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7693. ));
  7694. $result = $this->Form->select(
  7695. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7696. );
  7697. $expected = array(
  7698. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7699. array('option' => array('value' => 1)),
  7700. 'One',
  7701. '/option',
  7702. array('option' => array('value' => 2)),
  7703. 'Two',
  7704. '/option',
  7705. array('optgroup' => array('label' => 'Three')),
  7706. array('option' => array('value' => 3)),
  7707. 'Three',
  7708. '/option',
  7709. array('option' => array('value' => 4)),
  7710. 'Four',
  7711. '/option',
  7712. array('option' => array('value' => 5)),
  7713. 'Five',
  7714. '/option',
  7715. '/optgroup',
  7716. '/select'
  7717. );
  7718. $this->assertTags($result, $expected);
  7719. }
  7720. /**
  7721. * Test the generation of fields for a multi record form.
  7722. *
  7723. * @return void
  7724. */
  7725. public function testMultiRecordForm() {
  7726. $this->Form->create('ValidateProfile');
  7727. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7728. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7729. $expected = array(
  7730. 'div' => array('class' => 'input textarea'),
  7731. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7732. 'Name',
  7733. '/label',
  7734. 'textarea' => array(
  7735. 'id' => 'ValidateProfile1ValidateItem2Name',
  7736. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7737. 'cols' => 30,
  7738. 'rows' => 6
  7739. ),
  7740. 'Value',
  7741. '/textarea',
  7742. '/div'
  7743. );
  7744. $this->assertTags($result, $expected);
  7745. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7746. $expected = array(
  7747. 'div' => array('class' => 'input date'),
  7748. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7749. 'Created',
  7750. '/label',
  7751. array('select' => array(
  7752. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7753. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7754. )
  7755. ),
  7756. array('option' => array('value' => '')), '/option',
  7757. $this->dateRegex['monthsRegex'],
  7758. '/select', '-',
  7759. array('select' => array(
  7760. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7761. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7762. )
  7763. ),
  7764. array('option' => array('value' => '')), '/option',
  7765. $this->dateRegex['daysRegex'],
  7766. '/select', '-',
  7767. array('select' => array(
  7768. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7769. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7770. )
  7771. ),
  7772. array('option' => array('value' => '')), '/option',
  7773. $this->dateRegex['yearsRegex'],
  7774. '/select',
  7775. '/div'
  7776. );
  7777. $this->assertTags($result, $expected);
  7778. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7779. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7780. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7781. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7782. $expected = array(
  7783. 'div' => array('class' => 'input select error'),
  7784. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7785. 'Profile',
  7786. '/label',
  7787. 'select' => array(
  7788. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7789. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7790. 'class' => 'form-error'
  7791. ),
  7792. '/select',
  7793. array('div' => array('class' => 'error-message')),
  7794. 'Error',
  7795. '/div',
  7796. '/div'
  7797. );
  7798. $this->assertTags($result, $expected);
  7799. }
  7800. /**
  7801. * test the correct display of multi-record form validation errors.
  7802. *
  7803. * @return void
  7804. */
  7805. public function testMultiRecordFormValidationErrors() {
  7806. $this->Form->create('ValidateProfile');
  7807. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7808. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7809. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7810. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7811. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7812. $result = $this->Form->error('ValidateProfile.2.city');
  7813. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7814. $result = $this->Form->error('2.city');
  7815. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7816. }
  7817. /**
  7818. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7819. *
  7820. * @return void
  7821. */
  7822. public function testInputTemplate() {
  7823. $result = $this->Form->input('Contact.email', array(
  7824. 'type' => 'text', 'format' => array('input')
  7825. ));
  7826. $expected = array(
  7827. 'div' => array('class' => 'input text'),
  7828. 'input' => array(
  7829. 'type' => 'text', 'name' => 'data[Contact][email]',
  7830. 'id' => 'ContactEmail'
  7831. ),
  7832. '/div'
  7833. );
  7834. $this->assertTags($result, $expected);
  7835. $result = $this->Form->input('Contact.email', array(
  7836. 'type' => 'text', 'format' => array('input', 'label'),
  7837. 'label' => '<em>Email (required)</em>'
  7838. ));
  7839. $expected = array(
  7840. 'div' => array('class' => 'input text'),
  7841. array('input' => array(
  7842. 'type' => 'text', 'name' => 'data[Contact][email]',
  7843. 'id' => 'ContactEmail'
  7844. )),
  7845. 'label' => array('for' => 'ContactEmail'),
  7846. 'em' => array(),
  7847. 'Email (required)',
  7848. '/em',
  7849. '/label',
  7850. '/div'
  7851. );
  7852. $this->assertTags($result, $expected);
  7853. $result = $this->Form->input('Contact.email', array(
  7854. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7855. 'between' => '<div>Something in the middle</div>',
  7856. 'after' => '<span>Some text at the end</span>'
  7857. ));
  7858. $expected = array(
  7859. 'div' => array('class' => 'input text'),
  7860. array('input' => array(
  7861. 'type' => 'text', 'name' => 'data[Contact][email]',
  7862. 'id' => 'ContactEmail'
  7863. )),
  7864. array('div' => array()),
  7865. 'Something in the middle',
  7866. '/div',
  7867. 'label' => array('for' => 'ContactEmail'),
  7868. 'Email',
  7869. '/label',
  7870. 'span' => array(),
  7871. 'Some text at the end',
  7872. '/span',
  7873. '/div'
  7874. );
  7875. $this->assertTags($result, $expected);
  7876. $result = $this->Form->input('Contact.method', array(
  7877. 'type' => 'radio',
  7878. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7879. 'between' => 'I am between',
  7880. ));
  7881. $expected = array(
  7882. 'div' => array('class' => 'input radio'),
  7883. 'fieldset' => array(),
  7884. 'legend' => array(),
  7885. 'Method',
  7886. '/legend',
  7887. 'I am between',
  7888. 'input' => array(
  7889. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7890. 'value' => '', 'id' => 'ContactMethod_'
  7891. ),
  7892. array('input' => array(
  7893. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7894. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7895. )),
  7896. array('label' => array('for' => 'ContactMethodEmail')),
  7897. 'Email',
  7898. '/label',
  7899. array('input' => array(
  7900. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7901. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7902. )),
  7903. array('label' => array('for' => 'ContactMethodPigeon')),
  7904. 'Pigeon',
  7905. '/label',
  7906. '/fieldset',
  7907. '/div',
  7908. );
  7909. $this->assertTags($result, $expected);
  7910. }
  7911. /**
  7912. * test that some html5 inputs + FormHelper::__call() work
  7913. *
  7914. * @return void
  7915. */
  7916. public function testHtml5Inputs() {
  7917. $result = $this->Form->email('User.email');
  7918. $expected = array(
  7919. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7920. );
  7921. $this->assertTags($result, $expected);
  7922. $result = $this->Form->search('User.query');
  7923. $expected = array(
  7924. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7925. );
  7926. $this->assertTags($result, $expected);
  7927. $result = $this->Form->search('User.query', array('value' => 'test'));
  7928. $expected = array(
  7929. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7930. );
  7931. $this->assertTags($result, $expected);
  7932. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7933. $expected = array(
  7934. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7935. );
  7936. $this->assertTags($result, $expected);
  7937. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7938. $expected = array(
  7939. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7940. );
  7941. $this->assertTags($result, $expected);
  7942. }
  7943. /**
  7944. *
  7945. * @expectedException CakeException
  7946. * @return void
  7947. */
  7948. public function testHtml5InputException() {
  7949. $this->Form->email();
  7950. }
  7951. /**
  7952. * Tests that a model can be loaded from the model names passed in the request object
  7953. *
  7954. * @return void
  7955. */
  7956. public function testIntrospectModelFromRequest() {
  7957. $this->loadFixtures('Post');
  7958. App::build(array(
  7959. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7960. ));
  7961. CakePlugin::load('TestPlugin');
  7962. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7963. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7964. $this->Form->create('TestPluginPost');
  7965. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7966. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7967. CakePlugin::unload();
  7968. App::build();
  7969. }
  7970. /**
  7971. * Tests that it is possible to set the validation errors directly in the helper for a field
  7972. *
  7973. * @return void
  7974. */
  7975. public function testCustomValidationErrors() {
  7976. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7977. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7978. $this->assertEquals('Badness!', $result);
  7979. }
  7980. /**
  7981. * Tests that the 'on' key validates as expected on create
  7982. *
  7983. * @return void
  7984. */
  7985. public function testRequiredOnCreate() {
  7986. $this->Form->create('Contact');
  7987. $result = $this->Form->input('Contact.imrequiredonupdate');
  7988. $expected = array(
  7989. 'div' => array('class' => 'input text'),
  7990. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7991. 'Imrequiredonupdate',
  7992. '/label',
  7993. 'input' => array(
  7994. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7995. 'id' => 'ContactImrequiredonupdate'
  7996. ),
  7997. '/div'
  7998. );
  7999. $this->assertTags($result, $expected);
  8000. $result = $this->Form->input('Contact.imrequiredoncreate');
  8001. $expected = array(
  8002. 'div' => array('class' => 'input text required'),
  8003. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8004. 'Imrequiredoncreate',
  8005. '/label',
  8006. 'input' => array(
  8007. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8008. 'id' => 'ContactImrequiredoncreate',
  8009. 'required' => 'required'
  8010. ),
  8011. '/div'
  8012. );
  8013. $this->assertTags($result, $expected);
  8014. $result = $this->Form->input('Contact.imrequiredonboth');
  8015. $expected = array(
  8016. 'div' => array('class' => 'input text required'),
  8017. 'label' => array('for' => 'ContactImrequiredonboth'),
  8018. 'Imrequiredonboth',
  8019. '/label',
  8020. 'input' => array(
  8021. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8022. 'id' => 'ContactImrequiredonboth',
  8023. 'required' => 'required'
  8024. ),
  8025. '/div'
  8026. );
  8027. $this->assertTags($result, $expected);
  8028. $this->Form->inputDefaults(array('required' => false));
  8029. $result = $this->Form->input('Contact.imrequired');
  8030. $expected = array(
  8031. 'div' => array('class' => 'input text'),
  8032. 'label' => array('for' => 'ContactImrequired'),
  8033. 'Imrequired',
  8034. '/label',
  8035. 'input' => array(
  8036. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8037. 'id' => 'ContactImrequired'
  8038. ),
  8039. '/div'
  8040. );
  8041. $this->assertTags($result, $expected);
  8042. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8043. $this->assertTags($result, $expected);
  8044. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8045. $expected = array(
  8046. 'div' => array('class' => 'input text required'),
  8047. 'label' => array('for' => 'ContactImrequired'),
  8048. 'Imrequired',
  8049. '/label',
  8050. 'input' => array(
  8051. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8052. 'id' => 'ContactImrequired'
  8053. ),
  8054. '/div'
  8055. );
  8056. $this->assertTags($result, $expected);
  8057. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8058. $this->assertTags($result, $expected);
  8059. }
  8060. /**
  8061. * Tests that the 'on' key validates as expected on update
  8062. *
  8063. * @return void
  8064. */
  8065. public function testRequiredOnUpdate() {
  8066. $this->Form->request->data['Contact']['id'] = 1;
  8067. $this->Form->create('Contact');
  8068. $result = $this->Form->input('Contact.imrequiredonupdate');
  8069. $expected = array(
  8070. 'div' => array('class' => 'input text required'),
  8071. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8072. 'Imrequiredonupdate',
  8073. '/label',
  8074. 'input' => array(
  8075. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8076. 'id' => 'ContactImrequiredonupdate',
  8077. 'required' => 'required'
  8078. ),
  8079. '/div'
  8080. );
  8081. $this->assertTags($result, $expected);
  8082. $result = $this->Form->input('Contact.imrequiredoncreate');
  8083. $expected = array(
  8084. 'div' => array('class' => 'input text'),
  8085. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8086. 'Imrequiredoncreate',
  8087. '/label',
  8088. 'input' => array(
  8089. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8090. 'id' => 'ContactImrequiredoncreate'
  8091. ),
  8092. '/div'
  8093. );
  8094. $this->assertTags($result, $expected);
  8095. $result = $this->Form->input('Contact.imrequiredonboth');
  8096. $expected = array(
  8097. 'div' => array('class' => 'input text required'),
  8098. 'label' => array('for' => 'ContactImrequiredonboth'),
  8099. 'Imrequiredonboth',
  8100. '/label',
  8101. 'input' => array(
  8102. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8103. 'id' => 'ContactImrequiredonboth',
  8104. 'required' => 'required'
  8105. ),
  8106. '/div'
  8107. );
  8108. $this->assertTags($result, $expected);
  8109. $result = $this->Form->input('Contact.imrequired');
  8110. $expected = array(
  8111. 'div' => array('class' => 'input text required'),
  8112. 'label' => array('for' => 'ContactImrequired'),
  8113. 'Imrequired',
  8114. '/label',
  8115. 'input' => array(
  8116. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8117. 'id' => 'ContactImrequired',
  8118. 'required' => 'required'
  8119. ),
  8120. '/div'
  8121. );
  8122. $this->assertTags($result, $expected);
  8123. }
  8124. /**
  8125. * Test inputDefaults setter and getter
  8126. *
  8127. * @return void
  8128. */
  8129. public function testInputDefaults() {
  8130. $this->Form->create('Contact');
  8131. $this->Form->inputDefaults(array(
  8132. 'label' => false,
  8133. 'div' => array(
  8134. 'style' => 'color: #000;'
  8135. )
  8136. ));
  8137. $result = $this->Form->input('Contact.field1');
  8138. $expected = array(
  8139. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8140. 'input' => array(
  8141. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8142. 'id' => 'ContactField1'
  8143. ),
  8144. '/div'
  8145. );
  8146. $this->assertTags($result, $expected);
  8147. $this->Form->inputDefaults(array(
  8148. 'div' => false,
  8149. 'label' => 'Label',
  8150. ));
  8151. $result = $this->Form->input('Contact.field1');
  8152. $expected = array(
  8153. 'label' => array('for' => 'ContactField1'),
  8154. 'Label',
  8155. '/label',
  8156. 'input' => array(
  8157. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8158. 'id' => 'ContactField1'
  8159. ),
  8160. );
  8161. $this->assertTags($result, $expected);
  8162. $this->Form->inputDefaults(array(
  8163. 'label' => false,
  8164. ), true);
  8165. $result = $this->Form->input('Contact.field1');
  8166. $expected = array(
  8167. 'input' => array(
  8168. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8169. 'id' => 'ContactField1'
  8170. ),
  8171. );
  8172. $this->assertTags($result, $expected);
  8173. $result = $this->Form->inputDefaults();
  8174. $expected = array(
  8175. 'div' => false,
  8176. 'label' => false,
  8177. );
  8178. $this->assertEqual($result, $expected);
  8179. }
  8180. }