FormHelperTest.php 284 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920
  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. Configure::delete('Asset');
  481. $this->Controller = new ContactTestController();
  482. $this->View = new View($this->Controller);
  483. $this->Form = new FormHelper($this->View);
  484. $this->Form->Html = new HtmlHelper($this->View);
  485. $this->Form->request = new CakeRequest('contacts/add', false);
  486. $this->Form->request->here = '/contacts/add';
  487. $this->Form->request['action'] = 'add';
  488. $this->Form->request->webroot = '';
  489. $this->Form->request->base = '';
  490. ClassRegistry::addObject('Contact', new Contact());
  491. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  492. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  493. ClassRegistry::addObject('User', new UserForm());
  494. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  495. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  496. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  497. $this->oldSalt = Configure::read('Security.salt');
  498. $this->dateRegex = array(
  499. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  500. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  501. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  502. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  503. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  504. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  505. );
  506. Configure::write('Security.salt', 'foo!');
  507. }
  508. /**
  509. * tearDown method
  510. *
  511. * @return void
  512. */
  513. public function tearDown() {
  514. parent::tearDown();
  515. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  516. Configure::write('Security.salt', $this->oldSalt);
  517. }
  518. /**
  519. * testFormCreateWithSecurity method
  520. *
  521. * Test form->create() with security key.
  522. *
  523. * @return void
  524. */
  525. public function testCreateWithSecurity() {
  526. $this->Form->request['_Token'] = array('key' => 'testKey');
  527. $encoding = strtolower(Configure::read('App.encoding'));
  528. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  529. $expected = array(
  530. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  531. 'div' => array('style' => 'display:none;'),
  532. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  533. array('input' => array(
  534. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  535. )),
  536. '/div'
  537. );
  538. $this->assertTags($result, $expected);
  539. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  540. $expected['form']['id'] = 'MyForm';
  541. $this->assertTags($result, $expected);
  542. }
  543. /**
  544. * testFormCreateGetNoSecurity method
  545. *
  546. * Test form->create() with no security key as its a get form
  547. *
  548. * @return void
  549. */
  550. public function testCreateEndGetNoSecurity() {
  551. $this->Form->request['_Token'] = array('key' => 'testKey');
  552. $encoding = strtolower(Configure::read('App.encoding'));
  553. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  554. $this->assertNotContains('Token', $result);
  555. $result = $this->Form->end('Save');
  556. $this->assertNotContains('Token', $result);
  557. }
  558. /**
  559. * test that create() clears the fields property so it starts fresh
  560. *
  561. * @return void
  562. */
  563. public function testCreateClearingFields() {
  564. $this->Form->fields = array('model_id');
  565. $this->Form->create('Contact');
  566. $this->assertEquals(array(), $this->Form->fields);
  567. }
  568. /**
  569. * Tests form hash generation with model-less data
  570. *
  571. * @return void
  572. */
  573. public function testValidateHashNoModel() {
  574. $this->Form->request['_Token'] = array('key' => 'foo');
  575. $result = $this->Form->secure(array('anything'));
  576. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  577. }
  578. /**
  579. * Tests that models with identical field names get resolved properly
  580. *
  581. * @return void
  582. */
  583. public function testDuplicateFieldNameResolution() {
  584. $result = $this->Form->create('ValidateUser');
  585. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  586. $result = $this->Form->input('ValidateItem.name');
  587. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  588. $result = $this->Form->input('ValidateUser.name');
  589. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  590. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  591. $this->assertRegExp('/type="text"/', $result);
  592. $result = $this->Form->input('ValidateItem.name');
  593. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  594. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  595. $this->assertRegExp('/<textarea/', $result);
  596. $result = $this->Form->input('name');
  597. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  598. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  599. $this->assertRegExp('/type="text"/', $result);
  600. }
  601. /**
  602. * Tests that hidden fields generated for checkboxes don't get locked
  603. *
  604. * @return void
  605. */
  606. public function testNoCheckboxLocking() {
  607. $this->Form->request['_Token'] = array('key' => 'foo');
  608. $this->assertSame(array(), $this->Form->fields);
  609. $this->Form->checkbox('check', array('value' => '1'));
  610. $this->assertSame($this->Form->fields, array('check'));
  611. }
  612. /**
  613. * testFormSecurityFields method
  614. *
  615. * Test generation of secure form hash generation.
  616. *
  617. * @return void
  618. */
  619. public function testFormSecurityFields() {
  620. $key = 'testKey';
  621. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  622. $this->Form->request['_Token'] = array('key' => $key);
  623. $result = $this->Form->secure($fields);
  624. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  625. $hash .= ':' . 'Model.valid';
  626. $hash = urlencode($hash);
  627. $expected = array(
  628. 'div' => array('style' => 'display:none;'),
  629. array('input' => array(
  630. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  631. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  632. )),
  633. array('input' => array(
  634. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  635. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  636. )),
  637. '/div'
  638. );
  639. $this->assertTags($result, $expected);
  640. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  641. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  642. $result = $this->Form->secure($fields);
  643. $expected = array(
  644. 'div' => array('class' => 'hidden'),
  645. array('input' => array(
  646. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  647. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  648. )),
  649. array('input' => array(
  650. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  651. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  652. )),
  653. '/div'
  654. );
  655. $this->assertTags($result, $expected);
  656. }
  657. /**
  658. * Tests correct generation of number fields for double and float fields
  659. *
  660. * @return void
  661. */
  662. public function testTextFieldGenerationForFloats() {
  663. $model = ClassRegistry::getObject('Contact');
  664. $model->setSchema(array('foo' => array(
  665. 'type' => 'float',
  666. 'null' => false,
  667. 'default' => null,
  668. 'length' => 10
  669. )));
  670. $this->Form->create('Contact');
  671. $result = $this->Form->input('foo');
  672. $expected = array(
  673. 'div' => array('class' => 'input number'),
  674. 'label' => array('for' => 'ContactFoo'),
  675. 'Foo',
  676. '/label',
  677. array('input' => array(
  678. 'type' => 'number',
  679. 'name' => 'data[Contact][foo]',
  680. 'id' => 'ContactFoo',
  681. 'step' => 'any'
  682. )),
  683. '/div'
  684. );
  685. $this->assertTags($result, $expected);
  686. $result = $this->Form->input('foo', array('step' => 0.5));
  687. $expected = array(
  688. 'div' => array('class' => 'input number'),
  689. 'label' => array('for' => 'ContactFoo'),
  690. 'Foo',
  691. '/label',
  692. array('input' => array(
  693. 'type' => 'number',
  694. 'name' => 'data[Contact][foo]',
  695. 'id' => 'ContactFoo',
  696. 'step' => '0.5'
  697. )),
  698. '/div'
  699. );
  700. $this->assertTags($result, $expected);
  701. }
  702. /**
  703. * Tests correct generation of number fields for integer fields
  704. *
  705. * @access public
  706. * @return void
  707. */
  708. public function testTextFieldTypeNumberGenerationForIntegers() {
  709. $model = ClassRegistry::getObject('Contact');
  710. $model->setSchema(array('foo' => array(
  711. 'type' => 'integer',
  712. 'null' => false,
  713. 'default' => null,
  714. 'length' => null
  715. )));
  716. $this->Form->create('Contact');
  717. $result = $this->Form->input('foo');
  718. $expected = array(
  719. 'div' => array('class' => 'input number'),
  720. 'label' => array('for' => 'ContactFoo'),
  721. 'Foo',
  722. '/label',
  723. array('input' => array(
  724. 'type' => 'number', 'name' => 'data[Contact][foo]',
  725. 'id' => 'ContactFoo'
  726. )),
  727. '/div'
  728. );
  729. $this->assertTags($result, $expected);
  730. }
  731. /**
  732. * testFormSecurityMultipleFields method
  733. *
  734. * Test secure() with multiple row form. Ensure hash is correct.
  735. *
  736. * @return void
  737. */
  738. public function testFormSecurityMultipleFields() {
  739. $key = 'testKey';
  740. $fields = array(
  741. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  742. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  743. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  744. );
  745. $this->Form->request['_Token'] = array('key' => $key);
  746. $result = $this->Form->secure($fields);
  747. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  748. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  749. $expected = array(
  750. 'div' => array('style' => 'display:none;'),
  751. array('input' => array(
  752. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  753. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  754. )),
  755. array('input' => array(
  756. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  757. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  758. )),
  759. '/div'
  760. );
  761. $this->assertTags($result, $expected);
  762. }
  763. /**
  764. * testFormSecurityMultipleSubmitButtons
  765. *
  766. * test form submit generation and ensure that _Token is only created on end()
  767. *
  768. * @return void
  769. */
  770. public function testFormSecurityMultipleSubmitButtons() {
  771. $key = 'testKey';
  772. $this->Form->request['_Token'] = array('key' => $key);
  773. $this->Form->create('Addresses');
  774. $this->Form->input('Address.title');
  775. $this->Form->input('Address.first_name');
  776. $result = $this->Form->submit('Save', array('name' => 'save'));
  777. $expected = array(
  778. 'div' => array('class' => 'submit'),
  779. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  780. '/div',
  781. );
  782. $this->assertTags($result, $expected);
  783. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  784. $expected = array(
  785. 'div' => array('class' => 'submit'),
  786. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  787. '/div',
  788. );
  789. $this->assertTags($result, $expected);
  790. $result = $this->Form->end(null);
  791. $expected = array(
  792. 'div' => array('style' => 'display:none;'),
  793. array('input' => array(
  794. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  795. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  796. )),
  797. array('input' => array(
  798. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  799. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  800. )),
  801. '/div'
  802. );
  803. $this->assertTags($result, $expected);
  804. }
  805. /**
  806. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  807. *
  808. * @return void
  809. */
  810. public function testSecurityButtonNestedNamed() {
  811. $key = 'testKey';
  812. $this->Form->request['_Token'] = array('key' => $key);
  813. $this->Form->create('Addresses');
  814. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  815. $result = $this->Form->unlockField();
  816. $this->assertEquals(array('Address.button'), $result);
  817. }
  818. /**
  819. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  820. *
  821. * @return void
  822. */
  823. public function testSecuritySubmitNestedNamed() {
  824. $key = 'testKey';
  825. $this->Form->request['_Token'] = array('key' => $key);
  826. $this->Form->create('Addresses');
  827. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  828. $result = $this->Form->unlockField();
  829. $this->assertEquals(array('Address.button'), $result);
  830. }
  831. /**
  832. * Test that the correct fields are unlocked for image submits with no names.
  833. *
  834. * @return void
  835. */
  836. public function testSecuritySubmitImageNoName() {
  837. $key = 'testKey';
  838. $this->Form->request['_Token'] = array('key' => $key);
  839. $this->Form->create('User');
  840. $result = $this->Form->submit('save.png');
  841. $expected = array(
  842. 'div' => array('class' => 'submit'),
  843. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  844. '/div'
  845. );
  846. $this->assertTags($result, $expected);
  847. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  848. }
  849. /**
  850. * Test that the correct fields are unlocked for image submits with names.
  851. *
  852. * @return void
  853. */
  854. public function testSecuritySubmitImageName() {
  855. $key = 'testKey';
  856. $this->Form->request['_Token'] = array('key' => $key);
  857. $this->Form->create('User');
  858. $result = $this->Form->submit('save.png', array('name' => 'test'));
  859. $expected = array(
  860. 'div' => array('class' => 'submit'),
  861. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  862. '/div'
  863. );
  864. $this->assertTags($result, $expected);
  865. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  866. }
  867. /**
  868. * testFormSecurityMultipleInputFields method
  869. *
  870. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  871. *
  872. * @return void
  873. */
  874. public function testFormSecurityMultipleInputFields() {
  875. $key = 'testKey';
  876. $this->Form->request['_Token'] = array('key' => $key);
  877. $this->Form->create('Addresses');
  878. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  879. $this->Form->input('Addresses.0.title');
  880. $this->Form->input('Addresses.0.first_name');
  881. $this->Form->input('Addresses.0.last_name');
  882. $this->Form->input('Addresses.0.address');
  883. $this->Form->input('Addresses.0.city');
  884. $this->Form->input('Addresses.0.phone');
  885. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  886. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  887. $this->Form->input('Addresses.1.title');
  888. $this->Form->input('Addresses.1.first_name');
  889. $this->Form->input('Addresses.1.last_name');
  890. $this->Form->input('Addresses.1.address');
  891. $this->Form->input('Addresses.1.city');
  892. $this->Form->input('Addresses.1.phone');
  893. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  894. $result = $this->Form->secure($this->Form->fields);
  895. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  896. $expected = array(
  897. 'div' => array('style' => 'display:none;'),
  898. array('input' => array(
  899. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  900. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  901. )),
  902. array('input' => array(
  903. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  904. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  905. )),
  906. '/div'
  907. );
  908. $this->assertTags($result, $expected);
  909. }
  910. /**
  911. * Test form security with Model.field.0 style inputs
  912. *
  913. * @return void
  914. */
  915. public function testFormSecurityArrayFields() {
  916. $key = 'testKey';
  917. $this->Form->request->params['_Token']['key'] = $key;
  918. $this->Form->create('Address');
  919. $this->Form->input('Address.primary.1');
  920. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  921. $this->Form->input('Address.secondary.1.0');
  922. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  923. }
  924. /**
  925. * testFormSecurityMultipleInputDisabledFields method
  926. *
  927. * test secure form generation with multiple records and disabled fields.
  928. *
  929. * @return void
  930. */
  931. public function testFormSecurityMultipleInputDisabledFields() {
  932. $key = 'testKey';
  933. $this->Form->request->params['_Token'] = array(
  934. 'key' => $key,
  935. 'unlockedFields' => array('first_name', 'address')
  936. );
  937. $this->Form->create();
  938. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  939. $this->Form->input('Addresses.0.title');
  940. $this->Form->input('Addresses.0.first_name');
  941. $this->Form->input('Addresses.0.last_name');
  942. $this->Form->input('Addresses.0.address');
  943. $this->Form->input('Addresses.0.city');
  944. $this->Form->input('Addresses.0.phone');
  945. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  946. $this->Form->input('Addresses.1.title');
  947. $this->Form->input('Addresses.1.first_name');
  948. $this->Form->input('Addresses.1.last_name');
  949. $this->Form->input('Addresses.1.address');
  950. $this->Form->input('Addresses.1.city');
  951. $this->Form->input('Addresses.1.phone');
  952. $result = $this->Form->secure($this->Form->fields);
  953. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  954. $expected = array(
  955. 'div' => array('style' => 'display:none;'),
  956. array('input' => array(
  957. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  958. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  959. )),
  960. array('input' => array(
  961. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  962. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  963. )),
  964. '/div'
  965. );
  966. $this->assertTags($result, $expected);
  967. }
  968. /**
  969. * testFormSecurityInputDisabledFields method
  970. *
  971. * Test single record form with disabled fields.
  972. *
  973. * @return void
  974. */
  975. public function testFormSecurityInputUnlockedFields() {
  976. $key = 'testKey';
  977. $this->Form->request['_Token'] = array(
  978. 'key' => $key,
  979. 'unlockedFields' => array('first_name', 'address')
  980. );
  981. $this->Form->create();
  982. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  983. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  984. $this->Form->input('Addresses.title');
  985. $this->Form->input('Addresses.first_name');
  986. $this->Form->input('Addresses.last_name');
  987. $this->Form->input('Addresses.address');
  988. $this->Form->input('Addresses.city');
  989. $this->Form->input('Addresses.phone');
  990. $result = $this->Form->fields;
  991. $expected = array(
  992. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  993. 'Addresses.city', 'Addresses.phone'
  994. );
  995. $this->assertEquals($expected, $result);
  996. $result = $this->Form->secure($expected);
  997. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  998. $expected = array(
  999. 'div' => array('style' => 'display:none;'),
  1000. array('input' => array(
  1001. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1002. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1003. )),
  1004. array('input' => array(
  1005. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1006. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1007. )),
  1008. '/div'
  1009. );
  1010. $this->assertTags($result, $expected);
  1011. }
  1012. /**
  1013. * test securing inputs with custom name attributes.
  1014. *
  1015. * @return void
  1016. */
  1017. public function testFormSecureWithCustomNameAttribute() {
  1018. $this->Form->request->params['_Token']['key'] = 'testKey';
  1019. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1020. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1021. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1022. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1023. }
  1024. /**
  1025. * testFormSecuredInput method
  1026. *
  1027. * Test generation of entire secure form, assertions made on input() output.
  1028. *
  1029. * @return void
  1030. */
  1031. public function testFormSecuredInput() {
  1032. $this->Form->request['_Token'] = array('key' => 'testKey');
  1033. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1034. $encoding = strtolower(Configure::read('App.encoding'));
  1035. $expected = array(
  1036. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1037. 'div' => array('style' => 'display:none;'),
  1038. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1039. array('input' => array(
  1040. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1041. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1042. )),
  1043. '/div'
  1044. );
  1045. $this->assertTags($result, $expected);
  1046. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1047. $expected = array(
  1048. 'div' => array('class' => 'input text'),
  1049. 'label' => array('for' => 'UserFormPublished'),
  1050. 'Published',
  1051. '/label',
  1052. array('input' => array(
  1053. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1054. 'id' => 'UserFormPublished'
  1055. )),
  1056. '/div'
  1057. );
  1058. $this->assertTags($result, $expected);
  1059. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1060. $expected = array(
  1061. 'div' => array('class' => 'input text'),
  1062. 'label' => array('for' => 'UserFormOther'),
  1063. 'Other',
  1064. '/label',
  1065. array('input' => array(
  1066. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1067. 'id' => 'UserFormOther'
  1068. )),
  1069. '/div'
  1070. );
  1071. $this->assertTags($result, $expected);
  1072. $result = $this->Form->hidden('UserForm.stuff');
  1073. $expected = array(
  1074. 'input' => array(
  1075. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1076. 'id' => 'UserFormStuff'
  1077. ));
  1078. $this->assertTags($result, $expected);
  1079. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1080. $expected = array('input' => array(
  1081. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1082. 'value' => '0', 'id' => 'UserFormHidden'
  1083. ));
  1084. $this->assertTags($result, $expected);
  1085. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1086. $expected = array(
  1087. 'div' => array('class' => 'input checkbox'),
  1088. array('input' => array(
  1089. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1090. 'value' => '0', 'id' => 'UserFormSomething_'
  1091. )),
  1092. array('input' => array(
  1093. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1094. 'value' => '1', 'id' => 'UserFormSomething'
  1095. )),
  1096. 'label' => array('for' => 'UserFormSomething'),
  1097. 'Something',
  1098. '/label',
  1099. '/div'
  1100. );
  1101. $this->assertTags($result, $expected);
  1102. $result = $this->Form->fields;
  1103. $expected = array(
  1104. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1105. 'UserForm.hidden' => '0', 'UserForm.something'
  1106. );
  1107. $this->assertEquals($expected, $result);
  1108. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1109. $result = $this->Form->secure($this->Form->fields);
  1110. $expected = array(
  1111. 'div' => array('style' => 'display:none;'),
  1112. array('input' => array(
  1113. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1114. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1115. )),
  1116. array('input' => array(
  1117. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1118. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1119. )),
  1120. '/div'
  1121. );
  1122. $this->assertTags($result, $expected);
  1123. }
  1124. /**
  1125. * Test secured inputs with custom names.
  1126. *
  1127. * @return void
  1128. */
  1129. public function testSecuredInputCustomName() {
  1130. $this->Form->request['_Token'] = array('key' => 'testKey');
  1131. $this->assertEquals(array(), $this->Form->fields);
  1132. $this->Form->input('text_input', array(
  1133. 'name' => 'data[Option][General.default_role]',
  1134. ));
  1135. $expected = array('Option.General.default_role');
  1136. $this->assertEquals($expected, $this->Form->fields);
  1137. $this->Form->input('select_box', array(
  1138. 'name' => 'data[Option][General.select_role]',
  1139. 'type' => 'select',
  1140. 'options' => array(1, 2),
  1141. ));
  1142. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1143. $this->assertEquals($expected, $this->Form->fields);
  1144. }
  1145. /**
  1146. * Tests that the correct keys are added to the field hash index
  1147. *
  1148. * @return void
  1149. */
  1150. public function testFormSecuredFileInput() {
  1151. $this->Form->request['_Token'] = array('key' => 'testKey');
  1152. $this->assertEquals(array(), $this->Form->fields);
  1153. $this->Form->file('Attachment.file');
  1154. $expected = array(
  1155. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1156. 'Attachment.file.error', 'Attachment.file.size'
  1157. );
  1158. $this->assertEquals($expected, $this->Form->fields);
  1159. }
  1160. /**
  1161. * test that multiple selects keys are added to field hash
  1162. *
  1163. * @return void
  1164. */
  1165. public function testFormSecuredMultipleSelect() {
  1166. $this->Form->request['_Token'] = array('key' => 'testKey');
  1167. $this->assertEquals(array(), $this->Form->fields);
  1168. $options = array('1' => 'one', '2' => 'two');
  1169. $this->Form->select('Model.select', $options);
  1170. $expected = array('Model.select');
  1171. $this->assertEquals($expected, $this->Form->fields);
  1172. $this->Form->fields = array();
  1173. $this->Form->select('Model.select', $options, array('multiple' => true));
  1174. $this->assertEquals($expected, $this->Form->fields);
  1175. }
  1176. /**
  1177. * testFormSecuredRadio method
  1178. *
  1179. * @return void
  1180. */
  1181. public function testFormSecuredRadio() {
  1182. $this->Form->request['_Token'] = array('key' => 'testKey');
  1183. $this->assertEquals(array(), $this->Form->fields);
  1184. $options = array('1' => 'option1', '2' => 'option2');
  1185. $this->Form->radio('Test.test', $options);
  1186. $expected = array('Test.test');
  1187. $this->assertEquals($expected, $this->Form->fields);
  1188. }
  1189. /**
  1190. * Test that when disabled is in a list based attribute array it works.
  1191. *
  1192. * @return void
  1193. */
  1194. public function testFormSecuredAndDisabledNotAssoc() {
  1195. $this->Form->request['_Token'] = array('key' => 'testKey');
  1196. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1197. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1198. $this->Form->text('Model.text', array('disabled'));
  1199. $this->Form->textarea('Model.textarea', array('disabled'));
  1200. $this->Form->password('Model.password', array('disabled'));
  1201. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1202. $expected = array(
  1203. 'Model.radio' => ''
  1204. );
  1205. $this->assertEquals($expected, $this->Form->fields);
  1206. }
  1207. /**
  1208. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1209. * hashing.
  1210. *
  1211. * @return void
  1212. */
  1213. public function testFormSecuredAndDisabled() {
  1214. $this->Form->request['_Token'] = array('key' => 'testKey');
  1215. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1216. $this->Form->text('Model.text', array('disabled' => true));
  1217. $this->Form->password('Model.text', array('disabled' => true));
  1218. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1219. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1220. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1221. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1222. $this->Form->month('Model.month', array('disabled' => true));
  1223. $this->Form->day('Model.day', array('disabled' => true));
  1224. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1225. $this->Form->minute('Model.minute', array('disabled' => true));
  1226. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1227. $expected = array(
  1228. 'Model.radio' => ''
  1229. );
  1230. $this->assertEquals($expected, $this->Form->fields);
  1231. }
  1232. /**
  1233. * testDisableSecurityUsingForm method
  1234. *
  1235. * @return void
  1236. */
  1237. public function testDisableSecurityUsingForm() {
  1238. $this->Form->request['_Token'] = array(
  1239. 'key' => 'testKey',
  1240. 'disabledFields' => array()
  1241. );
  1242. $this->Form->create();
  1243. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1244. $this->Form->input('Addresses.title');
  1245. $this->Form->input('Addresses.first_name', array('secure' => false));
  1246. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1247. $this->Form->input('Addresses.zip', array(
  1248. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1249. ));
  1250. $result = $this->Form->fields;
  1251. $expected = array(
  1252. 'Addresses.id' => '123456', 'Addresses.title',
  1253. );
  1254. $this->assertEquals($expected, $result);
  1255. }
  1256. /**
  1257. * test disableField
  1258. *
  1259. * @return void
  1260. */
  1261. public function testUnlockFieldAddsToList() {
  1262. $this->Form->request['_Token'] = array(
  1263. 'key' => 'testKey',
  1264. 'unlockedFields' => array()
  1265. );
  1266. $this->Form->create('Contact');
  1267. $this->Form->unlockField('Contact.name');
  1268. $this->Form->text('Contact.name');
  1269. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1270. $this->assertEquals(array(), $this->Form->fields);
  1271. }
  1272. /**
  1273. * test unlockField removing from fields array.
  1274. *
  1275. * @return void
  1276. */
  1277. public function testUnlockFieldRemovingFromFields() {
  1278. $this->Form->request['_Token'] = array(
  1279. 'key' => 'testKey',
  1280. 'unlockedFields' => array()
  1281. );
  1282. $this->Form->create('Contact');
  1283. $this->Form->hidden('Contact.id', array('value' => 1));
  1284. $this->Form->text('Contact.name');
  1285. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1286. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1287. $this->Form->unlockField('Contact.name');
  1288. $this->Form->unlockField('Contact.id');
  1289. $this->assertEquals(array(), $this->Form->fields);
  1290. }
  1291. /**
  1292. * testTagIsInvalid method
  1293. *
  1294. * @return void
  1295. */
  1296. public function testTagIsInvalid() {
  1297. $Contact = ClassRegistry::getObject('Contact');
  1298. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1299. $this->Form->setEntity('Contact.0.email');
  1300. $result = $this->Form->tagIsInvalid();
  1301. $this->assertEquals($expected, $result);
  1302. $this->Form->setEntity('Contact.1.email');
  1303. $result = $this->Form->tagIsInvalid();
  1304. $this->assertFalse($result);
  1305. $this->Form->setEntity('Contact.0.name');
  1306. $result = $this->Form->tagIsInvalid();
  1307. $this->assertFalse($result);
  1308. }
  1309. /**
  1310. * Test tagIsInvalid with validation errors from a saveMany
  1311. *
  1312. * @return void
  1313. */
  1314. public function testTagIsInvalidSaveMany() {
  1315. $Contact = ClassRegistry::getObject('Contact');
  1316. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1317. $this->Form->create('Contact');
  1318. $this->Form->setEntity('0.email');
  1319. $result = $this->Form->tagIsInvalid();
  1320. $this->assertEquals($expected, $result);
  1321. $this->Form->setEntity('0.Contact.email');
  1322. $result = $this->Form->tagIsInvalid();
  1323. $this->assertEquals($expected, $result);
  1324. }
  1325. /**
  1326. * Test validation errors.
  1327. *
  1328. * @return void
  1329. */
  1330. public function testPasswordValidation() {
  1331. $Contact = ClassRegistry::getObject('Contact');
  1332. $Contact->validationErrors['password'] = array('Please provide a password');
  1333. $result = $this->Form->input('Contact.password');
  1334. $expected = array(
  1335. 'div' => array('class' => 'input password error'),
  1336. 'label' => array('for' => 'ContactPassword'),
  1337. 'Password',
  1338. '/label',
  1339. 'input' => array(
  1340. 'type' => 'password', 'name' => 'data[Contact][password]',
  1341. 'id' => 'ContactPassword', 'class' => 'form-error'
  1342. ),
  1343. array('div' => array('class' => 'error-message')),
  1344. 'Please provide a password',
  1345. '/div',
  1346. '/div'
  1347. );
  1348. $this->assertTags($result, $expected);
  1349. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1350. $expected = array(
  1351. 'div' => array('class' => 'input password error'),
  1352. 'label' => array('for' => 'ContactPassword'),
  1353. 'Password',
  1354. '/label',
  1355. 'input' => array(
  1356. 'type' => 'password', 'name' => 'data[Contact][password]',
  1357. 'id' => 'ContactPassword', 'class' => 'form-error'
  1358. ),
  1359. '/div'
  1360. );
  1361. $this->assertTags($result, $expected);
  1362. }
  1363. /**
  1364. * Test validation errors, when validation message is an empty string.
  1365. *
  1366. * @access public
  1367. * @return void
  1368. */
  1369. public function testEmptyErrorValidation() {
  1370. $this->Form->validationErrors['Contact']['password'] = '';
  1371. $result = $this->Form->input('Contact.password');
  1372. $expected = array(
  1373. 'div' => array('class' => 'input password error'),
  1374. 'label' => array('for' => 'ContactPassword'),
  1375. 'Password',
  1376. '/label',
  1377. 'input' => array(
  1378. 'type' => 'password', 'name' => 'data[Contact][password]',
  1379. 'id' => 'ContactPassword', 'class' => 'form-error'
  1380. ),
  1381. array('div' => array('class' => 'error-message')),
  1382. array(),
  1383. '/div',
  1384. '/div'
  1385. );
  1386. $this->assertTags($result, $expected);
  1387. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1388. $expected = array(
  1389. 'div' => array('class' => 'input password error'),
  1390. 'label' => array('for' => 'ContactPassword'),
  1391. 'Password',
  1392. '/label',
  1393. 'input' => array(
  1394. 'type' => 'password', 'name' => 'data[Contact][password]',
  1395. 'id' => 'ContactPassword', 'class' => 'form-error'
  1396. ),
  1397. '/div'
  1398. );
  1399. $this->assertTags($result, $expected);
  1400. }
  1401. /**
  1402. * Test validation errors, when calling input() overriding validation message by an empty string.
  1403. *
  1404. * @access public
  1405. * @return void
  1406. */
  1407. public function testEmptyInputErrorValidation() {
  1408. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1409. $result = $this->Form->input('Contact.password', array('error' => ''));
  1410. $expected = array(
  1411. 'div' => array('class' => 'input password error'),
  1412. 'label' => array('for' => 'ContactPassword'),
  1413. 'Password',
  1414. '/label',
  1415. 'input' => array(
  1416. 'type' => 'password', 'name' => 'data[Contact][password]',
  1417. 'id' => 'ContactPassword', 'class' => 'form-error'
  1418. ),
  1419. array('div' => array('class' => 'error-message')),
  1420. array(),
  1421. '/div',
  1422. '/div'
  1423. );
  1424. $this->assertTags($result, $expected);
  1425. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1426. $expected = array(
  1427. 'div' => array('class' => 'input password error'),
  1428. 'label' => array('for' => 'ContactPassword'),
  1429. 'Password',
  1430. '/label',
  1431. 'input' => array(
  1432. 'type' => 'password', 'name' => 'data[Contact][password]',
  1433. 'id' => 'ContactPassword', 'class' => 'form-error'
  1434. ),
  1435. '/div'
  1436. );
  1437. $this->assertTags($result, $expected);
  1438. }
  1439. /**
  1440. * testFormValidationAssociated method
  1441. *
  1442. * test display of form errors in conjunction with model::validates.
  1443. *
  1444. * @return void
  1445. */
  1446. public function testFormValidationAssociated() {
  1447. $this->UserForm = ClassRegistry::getObject('UserForm');
  1448. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1449. $data = array(
  1450. 'UserForm' => array('name' => 'user'),
  1451. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1452. );
  1453. $result = $this->UserForm->OpenidUrl->create($data);
  1454. $this->assertFalse(empty($result));
  1455. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1456. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1457. $encoding = strtolower(Configure::read('App.encoding'));
  1458. $expected = array(
  1459. 'form' => array(
  1460. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1461. 'accept-charset' => $encoding
  1462. ),
  1463. 'div' => array('style' => 'display:none;'),
  1464. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1465. '/div'
  1466. );
  1467. $this->assertTags($result, $expected);
  1468. $result = $this->Form->error(
  1469. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1470. );
  1471. $this->assertEquals('Error, not registered', $result);
  1472. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1473. }
  1474. /**
  1475. * testFormValidationAssociatedFirstLevel method
  1476. *
  1477. * test form error display with associated model.
  1478. *
  1479. * @return void
  1480. */
  1481. public function testFormValidationAssociatedFirstLevel() {
  1482. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1483. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1484. $data = array(
  1485. 'ValidateUser' => array('name' => 'mariano'),
  1486. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1487. );
  1488. $result = $this->ValidateUser->create($data);
  1489. $this->assertFalse(empty($result));
  1490. $this->assertFalse($this->ValidateUser->validates());
  1491. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1492. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1493. $encoding = strtolower(Configure::read('App.encoding'));
  1494. $expected = array(
  1495. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1496. 'div' => array('style' => 'display:none;'),
  1497. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1498. '/div'
  1499. );
  1500. $this->assertTags($result, $expected);
  1501. $result = $this->Form->error(
  1502. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1503. );
  1504. $this->assertEquals('Invalid email', $result);
  1505. $result = $this->Form->error(
  1506. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1507. );
  1508. $this->assertEquals('Invalid name', $result);
  1509. $result = $this->Form->error(
  1510. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1511. );
  1512. $this->assertEquals('Invalid city', $result);
  1513. unset($this->ValidateUser->ValidateProfile);
  1514. unset($this->ValidateUser);
  1515. }
  1516. /**
  1517. * testFormValidationAssociatedSecondLevel method
  1518. *
  1519. * test form error display with associated model.
  1520. *
  1521. * @return void
  1522. */
  1523. public function testFormValidationAssociatedSecondLevel() {
  1524. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1525. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1526. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1527. $data = array(
  1528. 'ValidateUser' => array('name' => 'mariano'),
  1529. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1530. 'ValidateItem' => array('name' => 'Item')
  1531. );
  1532. $result = $this->ValidateUser->create($data);
  1533. $this->assertFalse(empty($result));
  1534. $this->assertFalse($this->ValidateUser->validates());
  1535. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1536. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1537. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1538. $encoding = strtolower(Configure::read('App.encoding'));
  1539. $expected = array(
  1540. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1541. 'div' => array('style' => 'display:none;'),
  1542. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1543. '/div'
  1544. );
  1545. $this->assertTags($result, $expected);
  1546. $result = $this->Form->error(
  1547. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1548. );
  1549. $this->assertEquals('Invalid email', $result);
  1550. $result = $this->Form->error(
  1551. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1552. );
  1553. $this->assertEquals('Invalid name', $result);
  1554. $result = $this->Form->error(
  1555. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1556. );
  1557. $result = $this->Form->error(
  1558. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1559. );
  1560. $this->assertEquals('Invalid description', $result);
  1561. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1562. unset($this->ValidateUser->ValidateProfile);
  1563. unset($this->ValidateUser);
  1564. }
  1565. /**
  1566. * testFormValidationMultiRecord method
  1567. *
  1568. * test form error display with multiple records.
  1569. *
  1570. * @return void
  1571. */
  1572. public function testFormValidationMultiRecord() {
  1573. $Contact = ClassRegistry::getObject('Contact');
  1574. $Contact->validationErrors[2] = array(
  1575. 'name' => array('This field cannot be left blank')
  1576. );
  1577. $result = $this->Form->input('Contact.2.name');
  1578. $expected = array(
  1579. 'div' => array('class' => 'input text error'),
  1580. 'label' => array('for' => 'Contact2Name'),
  1581. 'Name',
  1582. '/label',
  1583. 'input' => array(
  1584. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1585. 'class' => 'form-error', 'maxlength' => 255
  1586. ),
  1587. array('div' => array('class' => 'error-message')),
  1588. 'This field cannot be left blank',
  1589. '/div',
  1590. '/div'
  1591. );
  1592. $this->assertTags($result, $expected);
  1593. }
  1594. /**
  1595. * testMultipleInputValidation method
  1596. *
  1597. * test multiple record form validation error display.
  1598. *
  1599. * @return void
  1600. */
  1601. public function testMultipleInputValidation() {
  1602. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1603. $Address->validationErrors[0] = array(
  1604. 'title' => array('This field cannot be empty'),
  1605. 'first_name' => array('This field cannot be empty')
  1606. );
  1607. $Address->validationErrors[1] = array(
  1608. 'last_name' => array('You must have a last name')
  1609. );
  1610. $this->Form->create();
  1611. $result = $this->Form->input('Address.0.title');
  1612. $expected = array(
  1613. 'div' => array('class'),
  1614. 'label' => array('for'),
  1615. 'preg:/[^<]+/',
  1616. '/label',
  1617. 'input' => array(
  1618. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1619. ),
  1620. array('div' => array('class' => 'error-message')),
  1621. 'This field cannot be empty',
  1622. '/div',
  1623. '/div'
  1624. );
  1625. $this->assertTags($result, $expected);
  1626. $result = $this->Form->input('Address.0.first_name');
  1627. $expected = array(
  1628. 'div' => array('class'),
  1629. 'label' => array('for'),
  1630. 'preg:/[^<]+/',
  1631. '/label',
  1632. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1633. array('div' => array('class' => 'error-message')),
  1634. 'This field cannot be empty',
  1635. '/div',
  1636. '/div'
  1637. );
  1638. $this->assertTags($result, $expected);
  1639. $result = $this->Form->input('Address.0.last_name');
  1640. $expected = array(
  1641. 'div' => array('class'),
  1642. 'label' => array('for'),
  1643. 'preg:/[^<]+/',
  1644. '/label',
  1645. 'input' => array('type' => 'text', 'name', 'id'),
  1646. '/div'
  1647. );
  1648. $this->assertTags($result, $expected);
  1649. $result = $this->Form->input('Address.1.last_name');
  1650. $expected = array(
  1651. 'div' => array('class'),
  1652. 'label' => array('for'),
  1653. 'preg:/[^<]+/',
  1654. '/label',
  1655. 'input' => array(
  1656. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1657. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1658. ),
  1659. array('div' => array('class' => 'error-message')),
  1660. 'You must have a last name',
  1661. '/div',
  1662. '/div'
  1663. );
  1664. $this->assertTags($result, $expected);
  1665. }
  1666. /**
  1667. * testInput method
  1668. *
  1669. * Test various incarnations of input().
  1670. *
  1671. * @return void
  1672. */
  1673. public function testInput() {
  1674. $result = $this->Form->input('ValidateUser.balance');
  1675. $expected = array(
  1676. 'div' => array('class'),
  1677. 'label' => array('for'),
  1678. 'Balance',
  1679. '/label',
  1680. 'input' => array('name', 'type' => 'number', 'id'),
  1681. '/div',
  1682. );
  1683. $this->assertTags($result, $expected);
  1684. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1685. $expected = array(
  1686. 'div' => array('class' => 'input email'),
  1687. 'label' => array('for' => 'custom'),
  1688. 'Email',
  1689. '/label',
  1690. array('input' => array(
  1691. 'type' => 'email', 'name' => 'data[Contact][email]',
  1692. 'id' => 'custom', 'maxlength' => 255
  1693. )),
  1694. '/div'
  1695. );
  1696. $this->assertTags($result, $expected);
  1697. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1698. $expected = array(
  1699. '<div',
  1700. 'label' => array('for' => 'ContactEmail'),
  1701. 'Email',
  1702. '/label',
  1703. array('input' => array(
  1704. 'type' => 'email', 'name' => 'data[Contact][email]',
  1705. 'id' => 'ContactEmail', 'maxlength' => 255
  1706. )),
  1707. '/div'
  1708. );
  1709. $this->assertTags($result, $expected);
  1710. $result = $this->Form->hidden('Contact.idontexist');
  1711. $expected = array('input' => array(
  1712. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1713. 'id' => 'ContactIdontexist'
  1714. ));
  1715. $this->assertTags($result, $expected);
  1716. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1717. $expected = array(
  1718. 'div' => array('class' => 'input text'),
  1719. 'label' => array('for' => 'ContactEmail'),
  1720. 'Email',
  1721. '/label',
  1722. array('input' => array(
  1723. 'type' => 'text', 'name' => 'data[Contact][email]',
  1724. 'id' => 'ContactEmail'
  1725. )),
  1726. '/div'
  1727. );
  1728. $this->assertTags($result, $expected);
  1729. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1730. $expected = array(
  1731. 'div' => array('class' => 'input text'),
  1732. 'label' => array('for' => 'Contact5Email'),
  1733. 'Email',
  1734. '/label',
  1735. array('input' => array(
  1736. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1737. 'id' => 'Contact5Email'
  1738. )),
  1739. '/div'
  1740. );
  1741. $this->assertTags($result, $expected);
  1742. $result = $this->Form->input('Contact.password');
  1743. $expected = array(
  1744. 'div' => array('class' => 'input password'),
  1745. 'label' => array('for' => 'ContactPassword'),
  1746. 'Password',
  1747. '/label',
  1748. array('input' => array(
  1749. 'type' => 'password', 'name' => 'data[Contact][password]',
  1750. 'id' => 'ContactPassword'
  1751. )),
  1752. '/div'
  1753. );
  1754. $this->assertTags($result, $expected);
  1755. $result = $this->Form->input('Contact.email', array(
  1756. 'type' => 'file', 'class' => 'textbox'
  1757. ));
  1758. $expected = array(
  1759. 'div' => array('class' => 'input file'),
  1760. 'label' => array('for' => 'ContactEmail'),
  1761. 'Email',
  1762. '/label',
  1763. array('input' => array(
  1764. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1765. 'id' => 'ContactEmail'
  1766. )),
  1767. '/div'
  1768. );
  1769. $this->assertTags($result, $expected);
  1770. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1771. $result = $this->Form->input('Contact.phone');
  1772. $expected = array(
  1773. 'div' => array('class' => 'input tel'),
  1774. 'label' => array('for' => 'ContactPhone'),
  1775. 'Phone',
  1776. '/label',
  1777. array('input' => array(
  1778. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1779. 'value' => 'Hello &amp; World &gt; weird chars',
  1780. 'id' => 'ContactPhone', 'maxlength' => 255
  1781. )),
  1782. '/div'
  1783. );
  1784. $this->assertTags($result, $expected);
  1785. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1786. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1787. $expected = array(
  1788. 'div' => array('class' => 'input text'),
  1789. 'label' => array('for' => 'myId'),
  1790. 'Field',
  1791. '/label',
  1792. 'input' => array(
  1793. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1794. 'value' => 'My value', 'id' => 'myId'
  1795. ),
  1796. '/div'
  1797. );
  1798. $this->assertTags($result, $expected);
  1799. unset($this->Form->request->data);
  1800. $Contact = ClassRegistry::getObject('Contact');
  1801. $Contact->validationErrors['field'] = array('Badness!');
  1802. $result = $this->Form->input('Contact.field');
  1803. $expected = array(
  1804. 'div' => array('class' => 'input text error'),
  1805. 'label' => array('for' => 'ContactField'),
  1806. 'Field',
  1807. '/label',
  1808. 'input' => array(
  1809. 'type' => 'text', 'name' => 'data[Contact][field]',
  1810. 'id' => 'ContactField', 'class' => 'form-error'
  1811. ),
  1812. array('div' => array('class' => 'error-message')),
  1813. 'Badness!',
  1814. '/div',
  1815. '/div'
  1816. );
  1817. $this->assertTags($result, $expected);
  1818. $result = $this->Form->input('Contact.field', array(
  1819. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1820. ));
  1821. $expected = array(
  1822. 'label' => array('for' => 'ContactField'),
  1823. 'Field',
  1824. '/label',
  1825. 'input' => array(
  1826. 'type' => 'text', 'name' => 'data[Contact][field]',
  1827. 'id' => 'ContactField', 'class' => 'form-error'
  1828. ),
  1829. array('span' => array('class' => 'error-message')),
  1830. 'Badness!',
  1831. '/span'
  1832. );
  1833. $this->assertTags($result, $expected);
  1834. $result = $this->Form->input('Contact.field', array(
  1835. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1836. ));
  1837. $expected = array(
  1838. 'div' => array('class' => 'input text error'),
  1839. 'label' => array('for' => 'ContactField'),
  1840. 'Field',
  1841. '/label',
  1842. 'input' => array(
  1843. 'type' => 'text', 'name' => 'data[Contact][field]',
  1844. 'id' => 'ContactField', 'class' => 'form-error'
  1845. ),
  1846. array('div' => array('class' => 'error')),
  1847. 'Badness!',
  1848. '/div'
  1849. );
  1850. $this->assertTags($result, $expected);
  1851. $result = $this->Form->input('Contact.field', array(
  1852. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1853. ));
  1854. $expected = array(
  1855. 'span' => 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. 'Badness!',
  1864. '/span'
  1865. );
  1866. $this->assertTags($result, $expected);
  1867. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1868. $expected = array(
  1869. 'div' => array('class' => 'input text error'),
  1870. 'label' => array('for' => 'ContactField'),
  1871. 'Field',
  1872. '/label',
  1873. 'input' => array(
  1874. 'type' => 'text', 'name' => 'data[Contact][field]',
  1875. 'id' => 'ContactField', 'class' => 'form-error'
  1876. ),
  1877. 'A message to you, Rudy',
  1878. array('div' => array('class' => 'error-message')),
  1879. 'Badness!',
  1880. '/div',
  1881. '/div'
  1882. );
  1883. $this->assertTags($result, $expected);
  1884. $this->Form->setEntity(null);
  1885. $this->Form->setEntity('Contact.field');
  1886. $result = $this->Form->input('Contact.field', array(
  1887. 'after' => 'A message to you, Rudy', 'error' => false
  1888. ));
  1889. $expected = array(
  1890. 'div' => array('class' => 'input text'),
  1891. 'label' => array('for' => 'ContactField'),
  1892. 'Field',
  1893. '/label',
  1894. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1895. 'A message to you, Rudy',
  1896. '/div'
  1897. );
  1898. $this->assertTags($result, $expected);
  1899. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1900. $expected = array(
  1901. 'div' => array('class' => 'input text'),
  1902. 'label' => array('for' => 'ObjectField'),
  1903. 'Field',
  1904. '/label',
  1905. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1906. 'A message to you, Rudy',
  1907. '/div'
  1908. );
  1909. $this->assertTags($result, $expected);
  1910. $Contact->validationErrors['field'] = array('minLength');
  1911. $result = $this->Form->input('Contact.field', array(
  1912. 'error' => array(
  1913. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1914. 'maxLength' => 'login too large'
  1915. )
  1916. ));
  1917. $expected = array(
  1918. 'div' => array('class' => 'input text error'),
  1919. 'label' => array('for' => 'ContactField'),
  1920. 'Field',
  1921. '/label',
  1922. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1923. array('div' => array('class' => 'error-message')),
  1924. 'Le login doit contenir au moins 2 caractères',
  1925. '/div',
  1926. '/div'
  1927. );
  1928. $this->assertTags($result, $expected);
  1929. $Contact->validationErrors['field'] = array('maxLength');
  1930. $result = $this->Form->input('Contact.field', array(
  1931. 'error' => array(
  1932. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1933. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1934. 'maxLength' => 'login too large',
  1935. )
  1936. ));
  1937. $expected = array(
  1938. 'div' => array('class' => 'input text error'),
  1939. 'label' => array('for' => 'ContactField'),
  1940. 'Field',
  1941. '/label',
  1942. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1943. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1944. 'login too large',
  1945. '/span',
  1946. '/div'
  1947. );
  1948. $this->assertTags($result, $expected);
  1949. }
  1950. /**
  1951. * Test that inputs with 0 can be created.
  1952. *
  1953. * @return void
  1954. */
  1955. public function testInputZero() {
  1956. $this->Form->create('User');
  1957. $result = $this->Form->input('0');
  1958. $expected = array(
  1959. 'div' => array('class' => 'input text'),
  1960. 'label' => array('for' => 'User0'), '/label',
  1961. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1962. '/div'
  1963. );
  1964. $this->assertTags($result, $expected);
  1965. }
  1966. /**
  1967. * test input() with checkbox creation
  1968. *
  1969. * @return void
  1970. */
  1971. public function testInputCheckbox() {
  1972. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1973. $expected = array(
  1974. 'div' => array('class' => 'input checkbox'),
  1975. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1976. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1977. '/div'
  1978. );
  1979. $this->assertTags($result, $expected);
  1980. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1981. $expected = array(
  1982. 'div' => array('class' => 'input checkbox'),
  1983. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1984. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1985. '/div'
  1986. );
  1987. $this->assertTags($result, $expected);
  1988. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1989. $expected = array(
  1990. 'div' => array('class' => 'input checkbox'),
  1991. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1992. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1993. '/div'
  1994. );
  1995. $this->assertTags($result, $expected);
  1996. $result = $this->Form->input('User.disabled', array(
  1997. 'label' => 'Disabled',
  1998. 'type' => 'checkbox',
  1999. 'data-foo' => 'disabled'
  2000. ));
  2001. $expected = array(
  2002. 'div' => array('class' => 'input checkbox'),
  2003. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  2004. array('input' => array(
  2005. 'type' => 'checkbox',
  2006. 'name' => 'data[User][disabled]',
  2007. 'value' => '1',
  2008. 'id' => 'UserDisabled',
  2009. 'data-foo' => 'disabled'
  2010. )),
  2011. 'label' => array('for' => 'UserDisabled'),
  2012. 'Disabled',
  2013. '/label',
  2014. '/div'
  2015. );
  2016. $this->assertTags($result, $expected);
  2017. }
  2018. /**
  2019. * test form->input() with time types.
  2020. *
  2021. */
  2022. public function testInputTime() {
  2023. extract($this->dateRegex);
  2024. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2025. $result = explode(':', $result);
  2026. $this->assertRegExp('/option value="23"/', $result[0]);
  2027. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2028. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2029. $result = explode(':', $result);
  2030. $this->assertRegExp('/option value="23"/', $result[0]);
  2031. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2032. $result = $this->Form->input('Model.field', array(
  2033. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2034. ));
  2035. $result = explode(':', $result);
  2036. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2037. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2038. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2039. $result = $this->Form->input('Model.field', array(
  2040. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2041. ));
  2042. $result = explode(':', $result);
  2043. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2044. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2045. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2046. $result = $this->Form->input('prueba', array(
  2047. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  2048. 'maxYear' => date('Y') + 1 ,'interval' => 15
  2049. ));
  2050. $result = explode(':', $result);
  2051. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2052. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2053. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2054. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2055. $result = $this->Form->input('Random.start_time', array(
  2056. 'type' => 'time',
  2057. 'selected' => '18:15'
  2058. ));
  2059. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2060. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2061. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2062. $result = $this->Form->input('published', array('type' => 'time'));
  2063. $now = strtotime('now');
  2064. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2065. $now = strtotime('2013-03-09 00:42:21');
  2066. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2067. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2068. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2069. }
  2070. /**
  2071. * Test interval + selected near the hour roll over.
  2072. *
  2073. * @return void
  2074. */
  2075. public function testTimeSelectedWithInterval() {
  2076. $result = $this->Form->input('Model.start_time', array(
  2077. 'type' => 'time',
  2078. 'interval' => 15,
  2079. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2080. ));
  2081. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2082. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2083. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2084. $result = $this->Form->input('Model.start_time', array(
  2085. 'type' => 'time',
  2086. 'interval' => 15,
  2087. 'selected' => '2012-10-23 15:57:00'
  2088. ));
  2089. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2090. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2091. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2092. $result = $this->Form->input('Model.start_time', array(
  2093. 'timeFormat' => 24,
  2094. 'type' => 'time',
  2095. 'interval' => 15,
  2096. 'selected' => '15:57'
  2097. ));
  2098. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2099. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2100. $result = $this->Form->input('Model.start_time', array(
  2101. 'timeFormat' => 24,
  2102. 'type' => 'time',
  2103. 'interval' => 15,
  2104. 'selected' => '23:57'
  2105. ));
  2106. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2107. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2108. $result = $this->Form->input('Model.created', array(
  2109. 'timeFormat' => 24,
  2110. 'type' => 'datetime',
  2111. 'interval' => 15,
  2112. 'selected' => '2012-09-30 23:56'
  2113. ));
  2114. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2115. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2116. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2117. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2118. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2119. }
  2120. /**
  2121. * Test interval & timeFormat = 12
  2122. *
  2123. * @return void
  2124. */
  2125. public function testInputTimeWithIntervalAnd12HourFormat() {
  2126. $result = $this->Form->input('Model.start_time', array(
  2127. 'type' => 'time',
  2128. 'timeFormat' => 12,
  2129. 'interval' => 5,
  2130. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2131. ));
  2132. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2133. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2134. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2135. $result = $this->Form->input('Model.start_time', array(
  2136. 'type' => 'time',
  2137. 'timeFormat' => '12',
  2138. 'interval' => 5,
  2139. 'selected' => '2013-04-19 16:30:00'
  2140. ));
  2141. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2142. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2143. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2144. $result = $this->Form->input('Model.start_time', array(
  2145. 'type' => 'time',
  2146. 'timeFormat' => '12',
  2147. 'interval' => 10,
  2148. 'selected' => '2013-05-19 00:33:00'
  2149. ));
  2150. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2151. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2152. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2153. $result = $this->Form->input('Model.start_time', array(
  2154. 'type' => 'time',
  2155. 'timeFormat' => '12',
  2156. 'interval' => 10,
  2157. 'selected' => '2013-05-19 13:33:00'
  2158. ));
  2159. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2160. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2161. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2162. $result = $this->Form->input('Model.start_time', array(
  2163. 'type' => 'time',
  2164. 'timeFormat' => '12',
  2165. 'interval' => 10,
  2166. 'selected' => '2013-05-19 01:33:00'
  2167. ));
  2168. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2169. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2170. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2171. }
  2172. /**
  2173. * test form->input() with datetime, date and time types
  2174. *
  2175. * @return void
  2176. */
  2177. public function testInputDatetime() {
  2178. extract($this->dateRegex);
  2179. $result = $this->Form->input('prueba', array(
  2180. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2181. 'maxYear' => date('Y') + 1, 'interval' => 15
  2182. ));
  2183. $result = explode(':', $result);
  2184. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2185. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2186. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2187. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2188. //related to ticket #5013
  2189. $result = $this->Form->input('Contact.date', array(
  2190. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2191. ));
  2192. $this->assertRegExp('/class="customClass"/', $result);
  2193. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2194. $result = $this->Form->input('Contact.date', array(
  2195. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2196. ));
  2197. $this->assertRegExp('/id="customIdDay"/', $result);
  2198. $this->assertRegExp('/id="customIdMonth"/', $result);
  2199. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2200. $result = $this->Form->input('Model.field', array(
  2201. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2202. ));
  2203. $this->assertRegExp('/id="customIDDay"/', $result);
  2204. $this->assertRegExp('/id="customIDHour"/', $result);
  2205. $result = explode('</select><select', $result);
  2206. $result = explode(':', $result[1]);
  2207. $this->assertRegExp('/option value="23"/', $result[0]);
  2208. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2209. $result = $this->Form->input('Model.field', array(
  2210. 'type' => 'datetime', 'timeFormat' => 12
  2211. ));
  2212. $result = explode('</select><select', $result);
  2213. $result = explode(':', $result[1]);
  2214. $this->assertRegExp('/option value="12"/', $result[0]);
  2215. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2216. $this->Form->request->data = array('Contact' => array('created' => null));
  2217. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2218. $expected = array(
  2219. 'div' => array('class' => 'input date'),
  2220. 'label' => array('for' => 'ContactCreatedMonth'),
  2221. 'Created',
  2222. '/label',
  2223. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2224. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2225. $monthsRegex,
  2226. '/select', '-',
  2227. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2228. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2229. $daysRegex,
  2230. '/select', '-',
  2231. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2232. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2233. $yearsRegex,
  2234. '/select',
  2235. '/div'
  2236. );
  2237. $this->assertTags($result, $expected);
  2238. $this->Form->request->data = array('Contact' => array('created' => null));
  2239. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2240. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2241. $this->Form->request->data = array('Contact' => array('created' => null));
  2242. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2243. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2244. $result = $this->Form->input('Contact.created', array(
  2245. 'type' => 'date',
  2246. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2247. 'timeFormat' => 'NONE'
  2248. ));
  2249. $this->assertRegExp('/for\="created-month"/', $result);
  2250. }
  2251. /**
  2252. * Test generating checkboxes in a loop.
  2253. *
  2254. * @return void
  2255. */
  2256. public function testInputCheckboxesInLoop() {
  2257. for ($i = 1; $i < 5; $i++) {
  2258. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2259. $expected = array(
  2260. 'div' => array('class' => 'input checkbox'),
  2261. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2262. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2263. 'label' => array('for' => "Contact{$i}Email"),
  2264. 'Email',
  2265. '/label',
  2266. '/div'
  2267. );
  2268. $this->assertTags($result, $expected);
  2269. }
  2270. }
  2271. /**
  2272. * Test generating checkboxes with disabled elements.
  2273. *
  2274. * @return void
  2275. */
  2276. public function testInputCheckboxWithDisabledElements() {
  2277. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2278. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2279. $expected = array(
  2280. array('div' => array('class' => 'input select')),
  2281. array('label' => array('for' => "ContactMultiple")),
  2282. 'Multiple',
  2283. '/label',
  2284. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2285. array('div' => array('class' => 'checkbox')),
  2286. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2287. array('label' => array('for' => "ContactMultiple1")),
  2288. 'One',
  2289. '/label',
  2290. '/div',
  2291. array('div' => array('class' => 'checkbox')),
  2292. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2293. array('label' => array('for' => "ContactMultiple2")),
  2294. 'Two',
  2295. '/label',
  2296. '/div',
  2297. array('div' => array('class' => 'checkbox')),
  2298. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2299. array('label' => array('for' => "ContactMultiple3")),
  2300. 'Three',
  2301. '/label',
  2302. '/div',
  2303. '/div'
  2304. );
  2305. $this->assertTags($result, $expected);
  2306. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2307. $this->assertTags($result, $expected);
  2308. $disabled = array('2', 3);
  2309. $expected = array(
  2310. array('div' => array('class' => 'input select')),
  2311. array('label' => array('for' => "ContactMultiple")),
  2312. 'Multiple',
  2313. '/label',
  2314. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2315. array('div' => array('class' => 'checkbox')),
  2316. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2317. array('label' => array('for' => "ContactMultiple1")),
  2318. 'One',
  2319. '/label',
  2320. '/div',
  2321. array('div' => array('class' => 'checkbox')),
  2322. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2323. array('label' => array('for' => "ContactMultiple2")),
  2324. 'Two',
  2325. '/label',
  2326. '/div',
  2327. array('div' => array('class' => 'checkbox')),
  2328. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2329. array('label' => array('for' => "ContactMultiple3")),
  2330. 'Three',
  2331. '/label',
  2332. '/div',
  2333. '/div'
  2334. );
  2335. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2336. $this->assertTags($result, $expected);
  2337. // make sure 50 does only disable 50, and not 50f5c0cf
  2338. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2339. $disabled = array(50);
  2340. $expected = array(
  2341. array('div' => array('class' => 'input select')),
  2342. array('label' => array('for' => "ContactMultiple")),
  2343. 'Multiple',
  2344. '/label',
  2345. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2346. array('div' => array('class' => 'checkbox')),
  2347. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2348. array('label' => array('for' => "ContactMultiple50")),
  2349. 'Fifty',
  2350. '/label',
  2351. '/div',
  2352. array('div' => array('class' => 'checkbox')),
  2353. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2354. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2355. 'Stringy',
  2356. '/label',
  2357. '/div',
  2358. '/div'
  2359. );
  2360. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2361. $this->assertTags($result, $expected);
  2362. }
  2363. /**
  2364. * test input name with leading integer, ensure attributes are generated correctly.
  2365. *
  2366. * @return void
  2367. */
  2368. public function testInputWithLeadingInteger() {
  2369. $result = $this->Form->text('0.Node.title');
  2370. $expected = array(
  2371. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2372. );
  2373. $this->assertTags($result, $expected);
  2374. }
  2375. /**
  2376. * test form->input() with select type inputs.
  2377. *
  2378. * @return void
  2379. */
  2380. public function testInputSelectType() {
  2381. $result = $this->Form->input('email', array(
  2382. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2383. );
  2384. $expected = array(
  2385. 'div' => array('class' => 'input select'),
  2386. 'label' => array('for' => 'email'),
  2387. 'Email',
  2388. '/label',
  2389. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2390. array('option' => array('value' => '')),
  2391. '/option',
  2392. array('option' => array('value' => 'è')),
  2393. 'Firést',
  2394. '/option',
  2395. array('option' => array('value' => 'é')),
  2396. 'Secoènd',
  2397. '/option',
  2398. '/select',
  2399. '/div'
  2400. );
  2401. $this->assertTags($result, $expected);
  2402. $result = $this->Form->input('email', array(
  2403. 'options' => array('First', 'Second'), 'empty' => true)
  2404. );
  2405. $expected = array(
  2406. 'div' => array('class' => 'input select'),
  2407. 'label' => array('for' => 'email'),
  2408. 'Email',
  2409. '/label',
  2410. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2411. array('option' => array('value' => '')),
  2412. '/option',
  2413. array('option' => array('value' => '0')),
  2414. 'First',
  2415. '/option',
  2416. array('option' => array('value' => '1')),
  2417. 'Second',
  2418. '/option',
  2419. '/select',
  2420. '/div'
  2421. );
  2422. $this->assertTags($result, $expected);
  2423. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2424. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2425. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2426. $expected = array(
  2427. 'div' => array('class' => 'input select'),
  2428. 'label' => array('for' => 'ModelUserId'),
  2429. 'User',
  2430. '/label',
  2431. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2432. array('option' => array('value' => '')),
  2433. '/option',
  2434. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2435. 'good',
  2436. '/option',
  2437. array('option' => array('value' => 'other')),
  2438. 'bad',
  2439. '/option',
  2440. '/select',
  2441. '/div'
  2442. );
  2443. $this->assertTags($result, $expected);
  2444. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2445. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2446. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2447. $expected = array(
  2448. 'div' => array('class' => 'input select'),
  2449. 'label' => array('for' => 'ThingUserId'),
  2450. 'User',
  2451. '/label',
  2452. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2453. array('option' => array('value' => '')),
  2454. 'Some Empty',
  2455. '/option',
  2456. array('option' => array('value' => 'value')),
  2457. 'good',
  2458. '/option',
  2459. array('option' => array('value' => 'other')),
  2460. 'bad',
  2461. '/option',
  2462. '/select',
  2463. '/div'
  2464. );
  2465. $this->assertTags($result, $expected);
  2466. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2467. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2468. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2469. $expected = array(
  2470. 'div' => array('class' => 'input select'),
  2471. 'label' => array('for' => 'ThingUserId'),
  2472. 'User',
  2473. '/label',
  2474. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2475. array('option' => array('value' => '')),
  2476. 'Some Empty',
  2477. '/option',
  2478. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2479. 'good',
  2480. '/option',
  2481. array('option' => array('value' => 'other')),
  2482. 'bad',
  2483. '/option',
  2484. '/select',
  2485. '/div'
  2486. );
  2487. $this->assertTags($result, $expected);
  2488. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2489. $this->Form->request->data = array('User' => array('User' => array('value')));
  2490. $result = $this->Form->input('User.User', array('empty' => true));
  2491. $expected = array(
  2492. 'div' => array('class' => 'input select'),
  2493. 'label' => array('for' => 'UserUser'),
  2494. 'User',
  2495. '/label',
  2496. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2497. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2498. array('option' => array('value' => '')),
  2499. '/option',
  2500. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2501. 'good',
  2502. '/option',
  2503. array('option' => array('value' => 'other')),
  2504. 'bad',
  2505. '/option',
  2506. '/select',
  2507. '/div'
  2508. );
  2509. $this->assertTags($result, $expected);
  2510. $this->Form->data = array();
  2511. $result = $this->Form->input('Publisher.id', array(
  2512. 'label' => 'Publisher',
  2513. 'type' => 'select',
  2514. 'multiple' => 'checkbox',
  2515. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2516. ));
  2517. $expected = array(
  2518. array('div' => array('class' => 'input select')),
  2519. array('label' => array('for' => 'PublisherId')),
  2520. 'Publisher',
  2521. '/label',
  2522. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2523. array('div' => array('class' => 'checkbox')),
  2524. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2525. array('label' => array('for' => 'PublisherIdValue1')),
  2526. 'Label 1',
  2527. '/label',
  2528. '/div',
  2529. array('div' => array('class' => 'checkbox')),
  2530. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2531. array('label' => array('for' => 'PublisherIdValue2')),
  2532. 'Label 2',
  2533. '/label',
  2534. '/div',
  2535. '/div'
  2536. );
  2537. $this->assertTags($result, $expected);
  2538. }
  2539. /**
  2540. * test that input() and a non standard primary key makes a hidden input by default.
  2541. *
  2542. * @return void
  2543. */
  2544. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2545. $this->Form->create('User');
  2546. $this->Form->fieldset = array(
  2547. 'User' => array(
  2548. 'fields' => array(
  2549. 'model_id' => array('type' => 'integer')
  2550. ),
  2551. 'validates' => array(),
  2552. 'key' => 'model_id'
  2553. )
  2554. );
  2555. $result = $this->Form->input('model_id');
  2556. $expected = array(
  2557. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2558. );
  2559. $this->assertTags($result, $expected);
  2560. }
  2561. /**
  2562. * test that overriding the magic select type widget is possible
  2563. *
  2564. * @return void
  2565. */
  2566. public function testInputOverridingMagicSelectType() {
  2567. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2568. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2569. $expected = array(
  2570. 'div' => array('class' => 'input text'),
  2571. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2572. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2573. '/div'
  2574. );
  2575. $this->assertTags($result, $expected);
  2576. //Check that magic types still work for plural/singular vars
  2577. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2578. $result = $this->Form->input('Model.type');
  2579. $expected = array(
  2580. 'div' => array('class' => 'input select'),
  2581. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2582. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2583. array('option' => array('value' => 'value')), 'good', '/option',
  2584. array('option' => array('value' => 'other')), 'bad', '/option',
  2585. '/select',
  2586. '/div'
  2587. );
  2588. $this->assertTags($result, $expected);
  2589. }
  2590. /**
  2591. * Test that magic input() selects are created for type=number
  2592. *
  2593. * @return void
  2594. */
  2595. public function testInputMagicSelectForTypeNumber() {
  2596. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2597. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2598. $result = $this->Form->input('ValidateUser.balance');
  2599. $expected = array(
  2600. 'div' => array('class' => 'input select'),
  2601. 'label' => array('for' => 'ValidateUserBalance'),
  2602. 'Balance',
  2603. '/label',
  2604. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2605. array('option' => array('value' => '0')),
  2606. 'nothing',
  2607. '/option',
  2608. array('option' => array('value' => '1', 'selected' => 'selected')),
  2609. 'some',
  2610. '/option',
  2611. array('option' => array('value' => '100')),
  2612. 'a lot',
  2613. '/option',
  2614. '/select',
  2615. '/div'
  2616. );
  2617. $this->assertTags($result, $expected);
  2618. }
  2619. /**
  2620. * Test that magic input() selects can easily be converted into radio types without error.
  2621. *
  2622. * @return void
  2623. */
  2624. public function testInputMagicSelectChangeToRadio() {
  2625. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2626. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2627. $this->assertRegExp('/input type="radio"/', $result);
  2628. }
  2629. /**
  2630. * fields with the same name as the model should work.
  2631. *
  2632. * @return void
  2633. */
  2634. public function testInputWithMatchingFieldAndModelName() {
  2635. $this->Form->create('User');
  2636. $this->Form->fieldset = array(
  2637. 'User' => array(
  2638. 'fields' => array(
  2639. 'User' => array('type' => 'text')
  2640. ),
  2641. 'validates' => array(),
  2642. 'key' => 'id'
  2643. )
  2644. );
  2645. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2646. $result = $this->Form->input('User', array('type' => 'text'));
  2647. $expected = array(
  2648. 'div' => array('class' => 'input text'),
  2649. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2650. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2651. '/div'
  2652. );
  2653. $this->assertTags($result, $expected);
  2654. }
  2655. /**
  2656. * testFormInputs method
  2657. *
  2658. * test correct results from form::inputs().
  2659. *
  2660. * @return void
  2661. */
  2662. public function testFormInputs() {
  2663. $this->Form->create('Contact');
  2664. $result = $this->Form->inputs('The Legend');
  2665. $expected = array(
  2666. '<fieldset',
  2667. '<legend',
  2668. 'The Legend',
  2669. '/legend',
  2670. '*/fieldset',
  2671. );
  2672. $this->assertTags($result, $expected);
  2673. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2674. $expected = array(
  2675. 'fieldset' => array('class' => 'classy-stuff'),
  2676. '<legend',
  2677. 'Field of Dreams',
  2678. '/legend',
  2679. '*/fieldset'
  2680. );
  2681. $this->assertTags($result, $expected);
  2682. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2683. $this->assertTags($result, $expected);
  2684. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2685. $this->assertTags($result, $expected);
  2686. $this->Form->create('Contact');
  2687. $this->Form->request['prefix'] = 'admin';
  2688. $this->Form->request['action'] = 'admin_edit';
  2689. $result = $this->Form->inputs();
  2690. $expected = array(
  2691. '<fieldset',
  2692. '<legend',
  2693. 'Edit Contact',
  2694. '/legend',
  2695. '*/fieldset',
  2696. );
  2697. $this->assertTags($result, $expected);
  2698. $this->Form->create('Contact');
  2699. $result = $this->Form->inputs(false);
  2700. $expected = array(
  2701. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2702. array('div' => array('class' => 'input text')),
  2703. '*/div',
  2704. array('div' => array('class' => 'input email')),
  2705. '*/div',
  2706. array('div' => array('class' => 'input tel')),
  2707. '*/div',
  2708. array('div' => array('class' => 'input password')),
  2709. '*/div',
  2710. array('div' => array('class' => 'input date')),
  2711. '*/div',
  2712. array('div' => array('class' => 'input date')),
  2713. '*/div',
  2714. array('div' => array('class' => 'input datetime')),
  2715. '*/div',
  2716. array('div' => array('class' => 'input number')),
  2717. '*/div',
  2718. array('div' => array('class' => 'input select')),
  2719. '*/div',
  2720. );
  2721. $this->assertTags($result, $expected);
  2722. $this->Form->create('Contact');
  2723. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2724. $expected = array(
  2725. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2726. array('div' => array('class' => 'input text')),
  2727. '*/div',
  2728. array('div' => array('class' => 'input email')),
  2729. '*/div',
  2730. array('div' => array('class' => 'input tel')),
  2731. '*/div',
  2732. array('div' => array('class' => 'input password')),
  2733. '*/div',
  2734. array('div' => array('class' => 'input date')),
  2735. '*/div',
  2736. array('div' => array('class' => 'input date')),
  2737. '*/div',
  2738. array('div' => array('class' => 'input datetime')),
  2739. '*/div',
  2740. array('div' => array('class' => 'input number')),
  2741. '*/div',
  2742. array('div' => array('class' => 'input select')),
  2743. '*/div',
  2744. );
  2745. $this->assertTags($result, $expected);
  2746. $this->Form->create('Contact');
  2747. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2748. $this->assertTags($result, $expected);
  2749. $this->Form->create('Contact');
  2750. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2751. $expected = array(
  2752. 'fieldset' => array(),
  2753. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2754. array('div' => array('class' => 'input text')),
  2755. '*/div',
  2756. array('div' => array('class' => 'input email')),
  2757. '*/div',
  2758. array('div' => array('class' => 'input tel')),
  2759. '*/div',
  2760. array('div' => array('class' => 'input password')),
  2761. '*/div',
  2762. array('div' => array('class' => 'input date')),
  2763. '*/div',
  2764. array('div' => array('class' => 'input date')),
  2765. '*/div',
  2766. array('div' => array('class' => 'input datetime')),
  2767. '*/div',
  2768. array('div' => array('class' => 'input number')),
  2769. '*/div',
  2770. array('div' => array('class' => 'input select')),
  2771. '*/div',
  2772. '/fieldset'
  2773. );
  2774. $this->assertTags($result, $expected);
  2775. $this->Form->create('Contact');
  2776. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2777. $expected = array(
  2778. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2779. array('div' => array('class' => 'input text')),
  2780. '*/div',
  2781. array('div' => array('class' => 'input email')),
  2782. '*/div',
  2783. array('div' => array('class' => 'input tel')),
  2784. '*/div',
  2785. array('div' => array('class' => 'input password')),
  2786. '*/div',
  2787. array('div' => array('class' => 'input date')),
  2788. '*/div',
  2789. array('div' => array('class' => 'input date')),
  2790. '*/div',
  2791. array('div' => array('class' => 'input datetime')),
  2792. '*/div',
  2793. array('div' => array('class' => 'input number')),
  2794. '*/div',
  2795. array('div' => array('class' => 'input select')),
  2796. '*/div',
  2797. );
  2798. $this->assertTags($result, $expected);
  2799. $this->Form->create('Contact');
  2800. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2801. $this->assertTags($result, $expected);
  2802. $this->Form->create('Contact');
  2803. $result = $this->Form->inputs('Hello');
  2804. $expected = array(
  2805. 'fieldset' => array(),
  2806. 'legend' => array(),
  2807. 'Hello',
  2808. '/legend',
  2809. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2810. array('div' => array('class' => 'input text')),
  2811. '*/div',
  2812. array('div' => array('class' => 'input email')),
  2813. '*/div',
  2814. array('div' => array('class' => 'input tel')),
  2815. '*/div',
  2816. array('div' => array('class' => 'input password')),
  2817. '*/div',
  2818. array('div' => array('class' => 'input date')),
  2819. '*/div',
  2820. array('div' => array('class' => 'input date')),
  2821. '*/div',
  2822. array('div' => array('class' => 'input datetime')),
  2823. '*/div',
  2824. array('div' => array('class' => 'input number')),
  2825. '*/div',
  2826. array('div' => array('class' => 'input select')),
  2827. '*/div',
  2828. '/fieldset'
  2829. );
  2830. $this->assertTags($result, $expected);
  2831. $this->Form->create('Contact');
  2832. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2833. $expected = array(
  2834. 'fieldset' => array(),
  2835. 'legend' => array(),
  2836. 'Hello',
  2837. '/legend',
  2838. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2839. array('div' => array('class' => 'input text')),
  2840. '*/div',
  2841. array('div' => array('class' => 'input email')),
  2842. '*/div',
  2843. array('div' => array('class' => 'input tel')),
  2844. '*/div',
  2845. array('div' => array('class' => 'input password')),
  2846. '*/div',
  2847. array('div' => array('class' => 'input date')),
  2848. '*/div',
  2849. array('div' => array('class' => 'input date')),
  2850. '*/div',
  2851. array('div' => array('class' => 'input datetime')),
  2852. '*/div',
  2853. array('div' => array('class' => 'input number')),
  2854. '*/div',
  2855. array('div' => array('class' => 'input select')),
  2856. '*/div',
  2857. '/fieldset'
  2858. );
  2859. $this->assertTags($result, $expected);
  2860. $this->Form->create('Contact');
  2861. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2862. $this->assertTags($result, $expected);
  2863. $this->Form->end();
  2864. $this->Form->create(false);
  2865. $expected = array(
  2866. 'fieldset' => array(),
  2867. array('div' => array('class' => 'input text')),
  2868. 'label' => array('for' => 'foo'),
  2869. 'Foo',
  2870. '/label',
  2871. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2872. '*/div',
  2873. '/fieldset'
  2874. );
  2875. $result = $this->Form->inputs(
  2876. array('foo' => array('type' => 'text')),
  2877. array(),
  2878. array('legend' => false)
  2879. );
  2880. $this->assertTags($result, $expected);
  2881. }
  2882. /**
  2883. * Tests inputs() works with plugin models
  2884. *
  2885. * @return void
  2886. */
  2887. public function testInputsPluginModel() {
  2888. $this->loadFixtures('Post');
  2889. App::build(array(
  2890. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2891. ));
  2892. CakePlugin::load('TestPlugin');
  2893. $this->Form->request['models'] = array(
  2894. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2895. );
  2896. $this->Form->create('TestPlugin.TestPluginPost');
  2897. $result = $this->Form->inputs();
  2898. $this->assertContains('data[TestPluginPost][id]', $result);
  2899. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2900. $this->assertContains('data[TestPluginPost][title]', $result);
  2901. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2902. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2903. $this->assertEquals('TestPluginPost', $this->Form->model());
  2904. }
  2905. /**
  2906. * testSelectAsCheckbox method
  2907. *
  2908. * test multi-select widget with checkbox formatting.
  2909. *
  2910. * @return void
  2911. */
  2912. public function testSelectAsCheckbox() {
  2913. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2914. $expected = array(
  2915. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2916. array('div' => array('class' => 'checkbox')),
  2917. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2918. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2919. 'first',
  2920. '/label',
  2921. '/div',
  2922. array('div' => array('class' => 'checkbox')),
  2923. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2924. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2925. 'second',
  2926. '/label',
  2927. '/div',
  2928. array('div' => array('class' => 'checkbox')),
  2929. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2930. array('label' => array('for' => 'ModelMultiField2')),
  2931. 'third',
  2932. '/label',
  2933. '/div',
  2934. );
  2935. $this->assertTags($result, $expected);
  2936. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2937. $expected = array(
  2938. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2939. array('div' => array('class' => 'checkbox')),
  2940. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2941. array('label' => array('for' => 'ModelMultiField12')),
  2942. 'half',
  2943. '/label',
  2944. '/div',
  2945. );
  2946. $this->assertTags($result, $expected);
  2947. }
  2948. /**
  2949. * testLabel method
  2950. *
  2951. * test label generation.
  2952. *
  2953. * @return void
  2954. */
  2955. public function testLabel() {
  2956. $this->Form->text('Person.name');
  2957. $result = $this->Form->label();
  2958. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2959. $this->Form->text('Person.name');
  2960. $result = $this->Form->label();
  2961. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2962. $result = $this->Form->label('Person.first_name');
  2963. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2964. $result = $this->Form->label('Person.first_name', 'Your first name');
  2965. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2966. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2967. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2968. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2969. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2970. $result = $this->Form->label('Person.first_name', '');
  2971. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2972. $result = $this->Form->label('Person.2.name', '');
  2973. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2974. }
  2975. /**
  2976. * testTextbox method
  2977. *
  2978. * test textbox element generation
  2979. *
  2980. * @return void
  2981. */
  2982. public function testTextbox() {
  2983. $result = $this->Form->text('Model.field');
  2984. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2985. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2986. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2987. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2988. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2989. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2990. $result = $this->Form->text('Model.text');
  2991. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2992. $Contact = ClassRegistry::getObject('Contact');
  2993. $Contact->validationErrors['text'] = array(true);
  2994. $this->Form->request->data['Contact']['text'] = 'test';
  2995. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2996. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2997. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2998. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2999. $expected = array(
  3000. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  3001. );
  3002. $this->assertTags($result, $expected);
  3003. }
  3004. /**
  3005. * testDefaultValue method
  3006. *
  3007. * Test default value setting
  3008. *
  3009. * @return void
  3010. */
  3011. public function testDefaultValue() {
  3012. $this->Form->request->data['Model']['field'] = 'test';
  3013. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3014. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3015. unset($this->Form->request->data['Model']['field']);
  3016. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3017. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3018. }
  3019. /**
  3020. * testCheckboxDefaultValue method
  3021. *
  3022. * Test default value setting on checkbox() method
  3023. *
  3024. * @return void
  3025. */
  3026. public function testCheckboxDefaultValue() {
  3027. $this->Form->request->data['Model']['field'] = false;
  3028. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3029. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3030. unset($this->Form->request->data['Model']['field']);
  3031. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3032. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3033. $this->Form->request->data['Model']['field'] = true;
  3034. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3035. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3036. unset($this->Form->request->data['Model']['field']);
  3037. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3038. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3039. }
  3040. /**
  3041. * testError method
  3042. *
  3043. * Test field error generation
  3044. *
  3045. * @return void
  3046. */
  3047. public function testError() {
  3048. $Contact = ClassRegistry::getObject('Contact');
  3049. $Contact->validationErrors['field'] = array(1);
  3050. $result = $this->Form->error('Contact.field');
  3051. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3052. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3053. $this->assertEquals('Error in field Field', $result);
  3054. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3055. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3056. $this->assertEquals('This field contains invalid input', $result);
  3057. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3058. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3059. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3060. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3061. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3062. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3063. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3064. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3065. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3066. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3067. $this->assertEquals('<strong>Badness!</strong>', $result);
  3068. $Contact->validationErrors['field'] = array("email");
  3069. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3070. $expected = array(
  3071. 'div' => array('class' => 'field-error'),
  3072. 'No good!',
  3073. '/div'
  3074. );
  3075. $this->assertTags($result, $expected);
  3076. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3077. $result = $this->Form->error('Contact.field', array(
  3078. 'notEmpty' => 'Cannot be empty',
  3079. 'email' => 'No good!'
  3080. ));
  3081. $expected = array(
  3082. 'div' => array('class' => 'error-message'),
  3083. 'ul' => array(),
  3084. '<li', 'Cannot be empty', '/li',
  3085. '<li', 'No good!', '/li',
  3086. '<li', 'Something else', '/li',
  3087. '/ul',
  3088. '/div'
  3089. );
  3090. $this->assertTags($result, $expected);
  3091. // Testing error messages list options
  3092. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3093. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3094. $expected = array(
  3095. 'div' => array('class' => 'error-message'),
  3096. 'ol' => array(),
  3097. '<li', 'notEmpty', '/li',
  3098. '<li', 'email', '/li',
  3099. '/ol',
  3100. '/div'
  3101. );
  3102. $this->assertTags($result, $expected);
  3103. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3104. $expected = array(
  3105. 'div' => array('class' => 'error-message'),
  3106. 'ol' => array(),
  3107. '<li', 'notEmpty', '/li',
  3108. '<li', 'email', '/li',
  3109. '/ol',
  3110. '/div'
  3111. );
  3112. $this->assertTags($result, $expected);
  3113. $result = $this->Form->error('Contact.field', null, array(
  3114. 'listOptions' => array(
  3115. 'class' => 'ul-class',
  3116. 'itemOptions' => array(
  3117. 'class' => 'li-class'
  3118. )
  3119. )
  3120. ));
  3121. $expected = array(
  3122. 'div' => array('class' => 'error-message'),
  3123. 'ul' => array('class' => 'ul-class'),
  3124. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3125. array('li' => array('class' => 'li-class')), 'email', '/li',
  3126. '/ul',
  3127. '/div'
  3128. );
  3129. $this->assertTags($result, $expected);
  3130. }
  3131. /**
  3132. * test error options when using form->input();
  3133. *
  3134. * @return void
  3135. */
  3136. public function testInputErrorEscape() {
  3137. $this->Form->create('ValidateProfile');
  3138. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3139. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3140. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3141. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3142. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3143. $this->assertRegExp('/required<br>/', $result);
  3144. }
  3145. /**
  3146. * testPassword method
  3147. *
  3148. * Test password element generation
  3149. *
  3150. * @return void
  3151. */
  3152. public function testPassword() {
  3153. $Contact = ClassRegistry::getObject('Contact');
  3154. $result = $this->Form->password('Contact.field');
  3155. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3156. $Contact->validationErrors['passwd'] = 1;
  3157. $this->Form->request->data['Contact']['passwd'] = 'test';
  3158. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3159. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3160. }
  3161. /**
  3162. * testRadio method
  3163. *
  3164. * Test radio element set generation
  3165. *
  3166. * @return void
  3167. */
  3168. public function testRadio() {
  3169. $result = $this->Form->radio('Model.field', array('option A'));
  3170. $expected = array(
  3171. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3172. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3173. 'label' => array('for' => 'ModelField0'),
  3174. 'option A',
  3175. '/label'
  3176. );
  3177. $this->assertTags($result, $expected);
  3178. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3179. $expected = array(
  3180. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3181. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3182. 'label' => array('for' => 'ModelField12'),
  3183. 'half',
  3184. '/label'
  3185. );
  3186. $this->assertTags($result, $expected);
  3187. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3188. $expected = array(
  3189. 'fieldset' => array(),
  3190. 'legend' => array(),
  3191. 'Field',
  3192. '/legend',
  3193. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3194. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3195. array('label' => array('for' => 'ModelField0')),
  3196. 'option A',
  3197. '/label',
  3198. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3199. array('label' => array('for' => 'ModelField1')),
  3200. 'option B',
  3201. '/label',
  3202. '/fieldset'
  3203. );
  3204. $this->assertTags($result, $expected);
  3205. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3206. $expected = array(
  3207. 'fieldset' => array(),
  3208. 'legend' => array(),
  3209. 'Field',
  3210. '/legend',
  3211. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3212. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3213. array('label' => array('for' => 'ModelField0')),
  3214. 'option A',
  3215. '/label',
  3216. 'br' => array(),
  3217. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3218. array('label' => array('for' => 'ModelField1')),
  3219. 'option B',
  3220. '/label',
  3221. '/fieldset'
  3222. );
  3223. $this->assertTags($result, $expected);
  3224. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3225. $expected = array(
  3226. 'fieldset' => array(),
  3227. 'legend' => array(),
  3228. 'Gender',
  3229. '/legend',
  3230. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3231. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3232. array('label' => array('for' => 'EmployeeGenderMale')),
  3233. 'Male',
  3234. '/label',
  3235. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3236. array('label' => array('for' => 'EmployeeGenderFemale')),
  3237. 'Female',
  3238. '/label',
  3239. '/fieldset',
  3240. );
  3241. $this->assertTags($result, $expected);
  3242. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3243. $expected = array(
  3244. 'fieldset' => array(),
  3245. 'legend' => array(),
  3246. 'Gender',
  3247. '/legend',
  3248. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3249. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3250. array('label' => array('for' => 'OfficerGenderMale')),
  3251. 'Male',
  3252. '/label',
  3253. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3254. array('label' => array('for' => 'OfficerGenderFemale')),
  3255. 'Female',
  3256. '/label',
  3257. '/fieldset',
  3258. );
  3259. $this->assertTags($result, $expected);
  3260. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3261. $expected = array(
  3262. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3263. array('input' => array(
  3264. 'type' => 'radio',
  3265. 'name' => 'data[Contact][1][imrequired]',
  3266. 'value' => '0',
  3267. 'id' => 'Contact1Imrequired0',
  3268. 'required' => 'required'
  3269. )),
  3270. 'label' => array('for' => 'Contact1Imrequired0'),
  3271. 'option A',
  3272. '/label'
  3273. );
  3274. $this->assertTags($result, $expected);
  3275. $result = $this->Form->radio('Model.1.field', array('option A'));
  3276. $expected = array(
  3277. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3278. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3279. 'label' => array('for' => 'Model1Field0'),
  3280. 'option A',
  3281. '/label'
  3282. );
  3283. $this->assertTags($result, $expected);
  3284. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3285. $expected = array(
  3286. 'fieldset' => array(),
  3287. 'legend' => array(),
  3288. 'Field',
  3289. '/legend',
  3290. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3291. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3292. array('label' => array('for' => 'ModelField0')),
  3293. 'option A',
  3294. '/label',
  3295. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3296. array('label' => array('for' => 'ModelField1')),
  3297. 'option B',
  3298. '/label',
  3299. '/fieldset'
  3300. );
  3301. $this->assertTags($result, $expected);
  3302. }
  3303. /**
  3304. * Test radio inputs with between as string or array. Also ensure
  3305. * that an array with less between elements works.
  3306. *
  3307. * @return void
  3308. */
  3309. public function testRadioBetween() {
  3310. $result = $this->Form->radio(
  3311. 'Model.field',
  3312. array('option A', 'option B'),
  3313. array('between' => 'I am between')
  3314. );
  3315. $expected = array(
  3316. 'fieldset' => array(),
  3317. 'legend' => array(),
  3318. 'Field',
  3319. '/legend',
  3320. 'I am between',
  3321. 'input' => array(
  3322. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3323. 'value' => '', 'id' => 'ModelField_'
  3324. ),
  3325. array('input' => array(
  3326. 'type' => 'radio', 'name' => 'data[Model][field]',
  3327. 'value' => '0', 'id' => 'ModelField0'
  3328. )),
  3329. array('label' => array('for' => 'ModelField0')),
  3330. 'option A',
  3331. '/label',
  3332. array('input' => array(
  3333. 'type' => 'radio', 'name' => 'data[Model][field]',
  3334. 'value' => '1', 'id' => 'ModelField1'
  3335. )),
  3336. array('label' => array('for' => 'ModelField1')),
  3337. 'option B',
  3338. '/label',
  3339. '/fieldset'
  3340. );
  3341. $this->assertTags($result, $expected);
  3342. $result = $this->Form->radio(
  3343. 'Model.field',
  3344. array('option A', 'option B', 'option C'),
  3345. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3346. );
  3347. $expected = array(
  3348. 'fieldset' => array(),
  3349. 'legend' => array(),
  3350. 'Field',
  3351. '/legend',
  3352. 'input' => array(
  3353. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3354. 'value' => '', 'id' => 'ModelField_'
  3355. ),
  3356. array('input' => array(
  3357. 'type' => 'radio', 'name' => 'data[Model][field]',
  3358. 'value' => '0', 'id' => 'ModelField0'
  3359. )),
  3360. array('label' => array('for' => 'ModelField0')),
  3361. 'option A',
  3362. '/label',
  3363. 'between A',
  3364. '--separator--',
  3365. array('input' => array(
  3366. 'type' => 'radio', 'name' => 'data[Model][field]',
  3367. 'value' => '1', 'id' => 'ModelField1'
  3368. )),
  3369. array('label' => array('for' => 'ModelField1')),
  3370. 'option B',
  3371. '/label',
  3372. 'between B',
  3373. '--separator--',
  3374. array('input' => array(
  3375. 'type' => 'radio', 'name' => 'data[Model][field]',
  3376. 'value' => '2', 'id' => 'ModelField2'
  3377. )),
  3378. array('label' => array('for' => 'ModelField2')),
  3379. 'option C',
  3380. '/label',
  3381. 'between C',
  3382. '/fieldset'
  3383. );
  3384. $this->assertTags($result, $expected);
  3385. $result = $this->Form->input('Model.field', array(
  3386. 'options' => array('1' => 'first', '2' => 'second'),
  3387. 'type' => 'radio',
  3388. 'before' => '--before--',
  3389. 'after' => '--after--',
  3390. 'separator' => '--separator--',
  3391. 'between' => array('--between first--', '--between second--')
  3392. ));
  3393. $expected = array(
  3394. 'div' => array('class' => 'input radio'),
  3395. '--before--',
  3396. 'fieldset' => array(),
  3397. 'legend' => array(),
  3398. 'Field',
  3399. '/legend',
  3400. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3401. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3402. array('label' => array('for' => 'ModelField1')),
  3403. 'first',
  3404. '/label',
  3405. '--between first--',
  3406. '--separator--',
  3407. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3408. array('label' => array('for' => 'ModelField2')),
  3409. 'second',
  3410. '/label',
  3411. '--between second--',
  3412. '/fieldset',
  3413. '--after--',
  3414. '/div'
  3415. );
  3416. $this->assertTags($result, $expected);
  3417. $result = $this->Form->input('Model.field', array(
  3418. 'options' => array('1' => 'first', '2' => 'second'),
  3419. 'type' => 'radio',
  3420. 'before' => '--before--',
  3421. 'after' => '--after--',
  3422. 'separator' => '--separator--',
  3423. 'between' => array('--between first--')
  3424. ));
  3425. $expected = array(
  3426. 'div' => array('class' => 'input radio'),
  3427. '--before--',
  3428. 'fieldset' => array(),
  3429. 'legend' => array(),
  3430. 'Field',
  3431. '/legend',
  3432. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3433. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3434. array('label' => array('for' => 'ModelField1')),
  3435. 'first',
  3436. '/label',
  3437. '--between first--',
  3438. '--separator--',
  3439. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3440. array('label' => array('for' => 'ModelField2')),
  3441. 'second',
  3442. '/label',
  3443. '/fieldset',
  3444. '--after--',
  3445. '/div'
  3446. );
  3447. $this->assertTags($result, $expected);
  3448. }
  3449. /**
  3450. * Test that radios with a 0 value are selected under the correct conditions.
  3451. * Also ensure that values that are booleanish are handled correctly.
  3452. *
  3453. * @return void
  3454. */
  3455. public function testRadioOptionWithBooleanishValues() {
  3456. $expected = array(
  3457. 'fieldset' => array(),
  3458. 'legend' => array(),
  3459. 'Field',
  3460. '/legend',
  3461. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3462. array('label' => array('for' => 'ModelField1')),
  3463. 'Yes',
  3464. '/label',
  3465. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3466. array('label' => array('for' => 'ModelField0')),
  3467. 'No',
  3468. '/label',
  3469. '/fieldset'
  3470. );
  3471. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3472. $this->assertTags($result, $expected);
  3473. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3474. $this->assertTags($result, $expected);
  3475. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3476. $this->assertTags($result, $expected);
  3477. $expected = array(
  3478. 'fieldset' => array(),
  3479. 'legend' => array(),
  3480. 'Field',
  3481. '/legend',
  3482. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3483. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3484. array('label' => array('for' => 'ModelField1')),
  3485. 'Yes',
  3486. '/label',
  3487. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3488. array('label' => array('for' => 'ModelField0')),
  3489. 'No',
  3490. '/label',
  3491. '/fieldset'
  3492. );
  3493. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3494. $this->assertTags($result, $expected);
  3495. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3496. $this->assertTags($result, $expected);
  3497. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3498. $this->assertTags($result, $expected);
  3499. $expected = array(
  3500. 'fieldset' => array(),
  3501. 'legend' => array(),
  3502. 'Field',
  3503. '/legend',
  3504. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3505. array('label' => array('for' => 'ModelField1')),
  3506. 'Yes',
  3507. '/label',
  3508. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3509. array('label' => array('for' => 'ModelField0')),
  3510. 'No',
  3511. '/label',
  3512. '/fieldset'
  3513. );
  3514. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3515. $this->assertTags($result, $expected);
  3516. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3517. $this->assertTags($result, $expected);
  3518. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3519. $this->assertTags($result, $expected);
  3520. }
  3521. /**
  3522. * test disabled radio options
  3523. *
  3524. * @return void
  3525. */
  3526. public function testRadioDisabled() {
  3527. $result = $this->Form->radio(
  3528. 'Model.field',
  3529. array('option A', 'option B'),
  3530. array('disabled' => array(0), 'value' => '0')
  3531. );
  3532. $expected = array(
  3533. 'fieldset' => array(),
  3534. 'legend' => array(),
  3535. 'Field',
  3536. '/legend',
  3537. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3538. array('label' => array('for' => 'ModelField0')),
  3539. 'option A',
  3540. '/label',
  3541. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3542. array('label' => array('for' => 'ModelField1')),
  3543. 'option B',
  3544. '/label',
  3545. '/fieldset'
  3546. );
  3547. $this->assertTags($result, $expected);
  3548. $result = $this->Form->radio(
  3549. 'Model.field',
  3550. array('option A', 'option B'),
  3551. array('disabled' => true, 'value' => '0')
  3552. );
  3553. $expected = array(
  3554. 'fieldset' => array(),
  3555. 'legend' => array(),
  3556. 'Field',
  3557. '/legend',
  3558. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3559. array('label' => array('for' => 'ModelField0')),
  3560. 'option A',
  3561. '/label',
  3562. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3563. array('label' => array('for' => 'ModelField1')),
  3564. 'option B',
  3565. '/label',
  3566. '/fieldset'
  3567. );
  3568. $this->assertTags($result, $expected);
  3569. $result = $this->Form->radio(
  3570. 'Model.field',
  3571. array('option A', 'option B'),
  3572. array('disabled' => 'disabled', 'value' => '0')
  3573. );
  3574. $expected = array(
  3575. 'fieldset' => array(),
  3576. 'legend' => array(),
  3577. 'Field',
  3578. '/legend',
  3579. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3580. array('label' => array('for' => 'ModelField0')),
  3581. 'option A',
  3582. '/label',
  3583. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3584. array('label' => array('for' => 'ModelField1')),
  3585. 'option B',
  3586. '/label',
  3587. '/fieldset'
  3588. );
  3589. $this->assertTags($result, $expected);
  3590. $result = $this->Form->input('Model.field', array(
  3591. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3592. 'type' => 'radio',
  3593. 'disabled' => array(2, '3x'),
  3594. ));
  3595. $expected = array(
  3596. 'div' => array('class' => 'input radio'),
  3597. 'fieldset' => array(),
  3598. 'legend' => array(),
  3599. 'Field',
  3600. '/legend',
  3601. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3602. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3603. array('label' => array('for' => 'ModelField1')),
  3604. 'first',
  3605. '/label',
  3606. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3607. array('label' => array('for' => 'ModelField2')),
  3608. 'second',
  3609. '/label',
  3610. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3611. array('label' => array('for' => 'ModelField2x')),
  3612. '2x',
  3613. '/label',
  3614. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3615. array('label' => array('for' => 'ModelField3')),
  3616. 'third',
  3617. '/label',
  3618. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3619. array('label' => array('for' => 'ModelField3x')),
  3620. '3x',
  3621. '/label',
  3622. '/fieldset',
  3623. '/div'
  3624. );
  3625. $this->assertTags($result, $expected);
  3626. }
  3627. /**
  3628. * test disabling the hidden input for radio buttons
  3629. *
  3630. * @return void
  3631. */
  3632. public function testRadioHiddenInputDisabling() {
  3633. $result = $this->Form->input('Model.1.field', array(
  3634. 'type' => 'radio',
  3635. 'options' => array('option A'),
  3636. 'hiddenField' => false
  3637. )
  3638. );
  3639. $expected = array(
  3640. 'div' => array('class' => 'input radio'),
  3641. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3642. 'label' => array('for' => 'Model1Field0'),
  3643. 'option A',
  3644. '/label',
  3645. '/div'
  3646. );
  3647. $this->assertTags($result, $expected);
  3648. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3649. $expected = array(
  3650. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3651. 'label' => array('for' => 'Model1Field0'),
  3652. 'option A',
  3653. '/label'
  3654. );
  3655. $this->assertTags($result, $expected);
  3656. }
  3657. /**
  3658. * test adding an empty option for radio buttons
  3659. *
  3660. * @return void
  3661. */
  3662. public function testRadioAddEmptyOption() {
  3663. $result = $this->Form->input('Model.1.field', array(
  3664. 'type' => 'radio',
  3665. 'options' => array('option A'),
  3666. 'empty' => true,
  3667. 'hiddenField' => false
  3668. ));
  3669. $expected = array(
  3670. 'div' => array('class' => 'input radio'),
  3671. 'fieldset' => array(),
  3672. 'legend' => array(),
  3673. 'Field',
  3674. '/legend',
  3675. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3676. array('label' => array('for' => 'Model1Field')),
  3677. __('empty'),
  3678. '/label',
  3679. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3680. array('label' => array('for' => 'Model1Field0')),
  3681. 'option A',
  3682. '/label',
  3683. '/fieldset',
  3684. '/div'
  3685. );
  3686. $this->assertTags($result, $expected);
  3687. $result = $this->Form->input('Model.1.field', array(
  3688. 'type' => 'radio',
  3689. 'options' => array('option A'),
  3690. 'empty' => 'CustomEmptyLabel',
  3691. 'hiddenField' => false
  3692. ));
  3693. $expected = array(
  3694. 'div' => array('class' => 'input radio'),
  3695. 'fieldset' => array(),
  3696. 'legend' => array(),
  3697. 'Field',
  3698. '/legend',
  3699. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3700. array('label' => array('for' => 'Model1Field')),
  3701. 'CustomEmptyLabel',
  3702. '/label',
  3703. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3704. array('label' => array('for' => 'Model1Field0')),
  3705. 'option A',
  3706. '/label',
  3707. '/fieldset',
  3708. '/div'
  3709. );
  3710. $this->assertTags($result, $expected);
  3711. $result = $this->Form->input('Model.1.field', array(
  3712. 'type' => 'radio',
  3713. 'options' => array('option A'),
  3714. 'empty' => false,
  3715. 'hiddenField' => false
  3716. ));
  3717. $this->assertTextNotContains('"Model1Field"', $result);
  3718. }
  3719. /**
  3720. * testSelect method
  3721. *
  3722. * Test select element generation.
  3723. *
  3724. * @return void
  3725. */
  3726. public function testSelect() {
  3727. $result = $this->Form->select('Model.field', array());
  3728. $expected = array(
  3729. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3730. array('option' => array('value' => '')),
  3731. '/option',
  3732. '/select'
  3733. );
  3734. $this->assertTags($result, $expected);
  3735. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3736. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3737. $expected = array(
  3738. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3739. array('option' => array('value' => '')),
  3740. '/option',
  3741. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3742. 'good',
  3743. '/option',
  3744. array('option' => array('value' => 'other')),
  3745. 'bad',
  3746. '/option',
  3747. '/select'
  3748. );
  3749. $this->assertTags($result, $expected);
  3750. $this->Form->request->data = array();
  3751. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3752. $expected = array(
  3753. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3754. array('option' => array('value' => '')),
  3755. '/option',
  3756. array('option' => array('value' => 'value')),
  3757. 'good',
  3758. '/option',
  3759. array('option' => array('value' => 'other')),
  3760. 'bad',
  3761. '/option',
  3762. '/select'
  3763. );
  3764. $this->assertTags($result, $expected);
  3765. $result = $this->Form->select(
  3766. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3767. array('empty' => false)
  3768. );
  3769. $expected = array(
  3770. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3771. array('option' => array('value' => 'first')),
  3772. 'first &quot;html&quot; &lt;chars&gt;',
  3773. '/option',
  3774. array('option' => array('value' => 'second')),
  3775. 'value',
  3776. '/option',
  3777. '/select'
  3778. );
  3779. $this->assertTags($result, $expected);
  3780. $result = $this->Form->select(
  3781. 'Model.field',
  3782. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3783. array('escape' => false, 'empty' => false)
  3784. );
  3785. $expected = array(
  3786. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3787. array('option' => array('value' => 'first')),
  3788. 'first "html" <chars>',
  3789. '/option',
  3790. array('option' => array('value' => 'second')),
  3791. 'value',
  3792. '/option',
  3793. '/select'
  3794. );
  3795. $this->assertTags($result, $expected);
  3796. $options = array(
  3797. array('value' => 'first', 'name' => 'First'),
  3798. array('value' => 'first', 'name' => 'Another First'),
  3799. );
  3800. $result = $this->Form->select(
  3801. 'Model.field',
  3802. $options,
  3803. array('escape' => false, 'empty' => false)
  3804. );
  3805. $expected = array(
  3806. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3807. array('option' => array('value' => 'first')),
  3808. 'First',
  3809. '/option',
  3810. array('option' => array('value' => 'first')),
  3811. 'Another First',
  3812. '/option',
  3813. '/select'
  3814. );
  3815. $this->assertTags($result, $expected);
  3816. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3817. $result = $this->Form->select(
  3818. 'Model.contact_id',
  3819. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3820. array('escape' => false, 'empty' => 'pick something')
  3821. );
  3822. $expected = array(
  3823. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3824. array('option' => array('value' => '')), 'pick something', '/option',
  3825. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3826. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3827. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3828. '/select'
  3829. );
  3830. $this->assertTags($result, $expected);
  3831. $this->Form->request->data['Model']['field'] = 0;
  3832. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3833. $expected = array(
  3834. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3835. array('option' => array('value' => '')), '/option',
  3836. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3837. array('option' => array('value' => '1')), 'Yes', '/option',
  3838. '/select'
  3839. );
  3840. $this->assertTags($result, $expected);
  3841. $this->Form->request->data['Model']['field'] = 50;
  3842. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3843. $expected = array(
  3844. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3845. array('option' => array('value' => '')), '/option',
  3846. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3847. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3848. '/select'
  3849. );
  3850. $this->assertTags($result, $expected);
  3851. }
  3852. /**
  3853. * test that select() with optiongroups listens to the escape param.
  3854. *
  3855. * @return void
  3856. */
  3857. public function testSelectOptionGroupEscaping() {
  3858. $options = array(
  3859. '>< Key' => array(
  3860. 1 => 'One',
  3861. 2 => 'Two'
  3862. )
  3863. );
  3864. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3865. $expected = array(
  3866. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3867. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3868. array('option' => array('value' => '1')), 'One', '/option',
  3869. array('option' => array('value' => '2')), 'Two', '/option',
  3870. '/optgroup',
  3871. '/select'
  3872. );
  3873. $this->assertTags($result, $expected);
  3874. $options = array(
  3875. '>< Key' => array(
  3876. 1 => 'One',
  3877. 2 => 'Two'
  3878. )
  3879. );
  3880. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3881. $expected = array(
  3882. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3883. 'optgroup' => array('label' => '>< Key'),
  3884. array('option' => array('value' => '1')), 'One', '/option',
  3885. array('option' => array('value' => '2')), 'Two', '/option',
  3886. '/optgroup',
  3887. '/select'
  3888. );
  3889. $this->assertTags($result, $expected);
  3890. }
  3891. /**
  3892. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3893. *
  3894. * @return void
  3895. */
  3896. public function testSelectWithNullAttributes() {
  3897. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3898. $expected = array(
  3899. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3900. array('option' => array('value' => '0')),
  3901. 'first',
  3902. '/option',
  3903. array('option' => array('value' => '1')),
  3904. 'second',
  3905. '/option',
  3906. '/select'
  3907. );
  3908. $this->assertTags($result, $expected);
  3909. }
  3910. /**
  3911. * testNestedSelect method
  3912. *
  3913. * test select element generation with optgroups
  3914. *
  3915. * @return void
  3916. */
  3917. public function testNestedSelect() {
  3918. $result = $this->Form->select(
  3919. 'Model.field',
  3920. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3921. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3922. )), array('empty' => false)
  3923. );
  3924. $expected = array(
  3925. 'select' => array('name' => 'data[Model][field]',
  3926. 'id' => 'ModelField'),
  3927. array('option' => array('value' => 1)),
  3928. 'One',
  3929. '/option',
  3930. array('option' => array('value' => 2)),
  3931. 'Two',
  3932. '/option',
  3933. array('optgroup' => array('label' => 'Three')),
  3934. array('option' => array('value' => 4)),
  3935. 'Four',
  3936. '/option',
  3937. array('option' => array('value' => 5)),
  3938. 'Five',
  3939. '/option',
  3940. '/optgroup',
  3941. '/select'
  3942. );
  3943. $this->assertTags($result, $expected);
  3944. $result = $this->Form->select(
  3945. 'Model.field',
  3946. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3947. array('showParents' => true, 'empty' => false)
  3948. );
  3949. $expected = array(
  3950. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3951. array('option' => array('value' => 1)),
  3952. 'One',
  3953. '/option',
  3954. array('option' => array('value' => 2)),
  3955. 'Two',
  3956. '/option',
  3957. array('optgroup' => array('label' => 'Three')),
  3958. array('option' => array('value' => 3)),
  3959. 'Three',
  3960. '/option',
  3961. array('option' => array('value' => 4)),
  3962. 'Four',
  3963. '/option',
  3964. '/optgroup',
  3965. '/select'
  3966. );
  3967. $this->assertTags($result, $expected);
  3968. }
  3969. /**
  3970. * testSelectMultiple method
  3971. *
  3972. * test generation of multiple select elements
  3973. *
  3974. * @return void
  3975. */
  3976. public function testSelectMultiple() {
  3977. $options = array('first', 'second', 'third');
  3978. $result = $this->Form->select(
  3979. 'Model.multi_field', $options, array('multiple' => true)
  3980. );
  3981. $expected = array(
  3982. 'input' => array(
  3983. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3984. ),
  3985. 'select' => array(
  3986. 'name' => 'data[Model][multi_field][]',
  3987. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3988. ),
  3989. array('option' => array('value' => '0')),
  3990. 'first',
  3991. '/option',
  3992. array('option' => array('value' => '1')),
  3993. 'second',
  3994. '/option',
  3995. array('option' => array('value' => '2')),
  3996. 'third',
  3997. '/option',
  3998. '/select'
  3999. );
  4000. $this->assertTags($result, $expected);
  4001. $result = $this->Form->select(
  4002. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4003. );
  4004. $expected = array(
  4005. 'input' => array(
  4006. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4007. ),
  4008. 'select' => array(
  4009. 'name' => 'data[Model][multi_field][]',
  4010. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4011. ),
  4012. array('option' => array('value' => '0')),
  4013. 'first',
  4014. '/option',
  4015. array('option' => array('value' => '1')),
  4016. 'second',
  4017. '/option',
  4018. array('option' => array('value' => '2')),
  4019. 'third',
  4020. '/option',
  4021. '/select'
  4022. );
  4023. $this->assertTags($result, $expected);
  4024. $result = $this->Form->select(
  4025. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4026. );
  4027. $expected = array(
  4028. 'input' => array(
  4029. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4030. ),
  4031. 'select' => array(
  4032. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4033. 'multiple' => 'multiple'
  4034. ),
  4035. array('option' => array('value' => '0', 'selected' => 'selected')),
  4036. 'first',
  4037. '/option',
  4038. array('option' => array('value' => '1', 'selected' => 'selected')),
  4039. 'second',
  4040. '/option',
  4041. array('option' => array('value' => '2')),
  4042. 'third',
  4043. '/option',
  4044. '/select'
  4045. );
  4046. $this->assertTags($result, $expected);
  4047. $result = $this->Form->select(
  4048. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4049. );
  4050. $expected = array(
  4051. 'select' => array(
  4052. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4053. ),
  4054. array('option' => array('value' => '0', 'selected' => 'selected')),
  4055. 'first',
  4056. '/option',
  4057. array('option' => array('value' => '1', 'selected' => 'selected')),
  4058. 'second',
  4059. '/option',
  4060. array('option' => array('value' => '2')),
  4061. 'third',
  4062. '/option',
  4063. '/select'
  4064. );
  4065. $this->assertTags($result, $expected);
  4066. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4067. $selected = array('2', '3x');
  4068. $result = $this->Form->select(
  4069. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4070. );
  4071. $expected = array(
  4072. 'input' => array(
  4073. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4074. ),
  4075. 'select' => array(
  4076. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4077. ),
  4078. array('option' => array('value' => '1')),
  4079. 'One',
  4080. '/option',
  4081. array('option' => array('value' => '2', 'selected' => 'selected')),
  4082. 'Two',
  4083. '/option',
  4084. array('option' => array('value' => '3')),
  4085. 'Three',
  4086. '/option',
  4087. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4088. 'Stringy',
  4089. '/option',
  4090. '/select'
  4091. );
  4092. $this->assertTags($result, $expected);
  4093. }
  4094. /**
  4095. * Test generating multiple select with disabled elements.
  4096. *
  4097. * @return void
  4098. */
  4099. public function testSelectMultipleWithDisabledElements() {
  4100. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4101. $disabled = array(2, 3);
  4102. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4103. $expected = array(
  4104. 'input' => array(
  4105. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4106. ),
  4107. 'select' => array(
  4108. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4109. ),
  4110. array('option' => array('value' => '1')),
  4111. 'One',
  4112. '/option',
  4113. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4114. 'Two',
  4115. '/option',
  4116. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4117. 'Three',
  4118. '/option',
  4119. array('option' => array('value' => '3x')),
  4120. 'Stringy',
  4121. '/option',
  4122. '/select'
  4123. );
  4124. $this->assertTags($result, $expected);
  4125. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4126. $disabled = array('2', '3x');
  4127. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4128. $expected = array(
  4129. array('div' => array('class' => 'input select')),
  4130. array('label' => array('for' => 'ContactMultiple')),
  4131. 'Multiple',
  4132. '/label',
  4133. 'input' => array(
  4134. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4135. ),
  4136. 'select' => array(
  4137. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4138. ),
  4139. array('option' => array('value' => '1')),
  4140. 'One',
  4141. '/option',
  4142. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4143. 'Two',
  4144. '/option',
  4145. array('option' => array('value' => '3')),
  4146. 'Three',
  4147. '/option',
  4148. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4149. 'Stringy',
  4150. '/option',
  4151. '/select',
  4152. '/div'
  4153. );
  4154. $this->assertTags($result, $expected);
  4155. }
  4156. /**
  4157. * test generation of habtm select boxes.
  4158. *
  4159. * @return void
  4160. */
  4161. public function testHabtmSelectBox() {
  4162. $this->View->viewVars['contactTags'] = array(
  4163. 1 => 'blue',
  4164. 2 => 'red',
  4165. 3 => 'green'
  4166. );
  4167. $this->Form->request->data = array(
  4168. 'Contact' => array(),
  4169. 'ContactTag' => array(
  4170. array(
  4171. 'id' => '1',
  4172. 'name' => 'blue'
  4173. ),
  4174. array(
  4175. 'id' => 3,
  4176. 'name' => 'green'
  4177. )
  4178. )
  4179. );
  4180. $this->Form->create('Contact');
  4181. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4182. $expected = array(
  4183. 'input' => array(
  4184. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4185. ),
  4186. 'select' => array(
  4187. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4188. 'multiple' => 'multiple'
  4189. ),
  4190. array('option' => array('value' => '1', 'selected' => 'selected')),
  4191. 'blue',
  4192. '/option',
  4193. array('option' => array('value' => '2')),
  4194. 'red',
  4195. '/option',
  4196. array('option' => array('value' => '3', 'selected' => 'selected')),
  4197. 'green',
  4198. '/option',
  4199. '/select'
  4200. );
  4201. $this->assertTags($result, $expected);
  4202. // make sure only 50 is selected, and not 50f5c0cf
  4203. $this->View->viewVars['contactTags'] = array(
  4204. '1' => 'blue',
  4205. '50f5c0cf' => 'red',
  4206. '50' => 'green'
  4207. );
  4208. $this->Form->request->data = array(
  4209. 'Contact' => array(),
  4210. 'ContactTag' => array(
  4211. array(
  4212. 'id' => 1,
  4213. 'name' => 'blue'
  4214. ),
  4215. array(
  4216. 'id' => 50,
  4217. 'name' => 'green'
  4218. )
  4219. )
  4220. );
  4221. $this->Form->create('Contact');
  4222. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4223. $expected = array(
  4224. 'input' => array(
  4225. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4226. ),
  4227. 'select' => array(
  4228. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4229. 'multiple' => 'multiple'
  4230. ),
  4231. array('option' => array('value' => '1', 'selected' => 'selected')),
  4232. 'blue',
  4233. '/option',
  4234. array('option' => array('value' => '50f5c0cf')),
  4235. 'red',
  4236. '/option',
  4237. array('option' => array('value' => '50', 'selected' => 'selected')),
  4238. 'green',
  4239. '/option',
  4240. '/select'
  4241. );
  4242. $this->assertTags($result, $expected);
  4243. }
  4244. /**
  4245. * test generation of multi select elements in checkbox format
  4246. *
  4247. * @return void
  4248. */
  4249. public function testSelectMultipleCheckboxes() {
  4250. $result = $this->Form->select(
  4251. 'Model.multi_field',
  4252. array('first', 'second', 'third'),
  4253. array('multiple' => 'checkbox')
  4254. );
  4255. $expected = array(
  4256. 'input' => array(
  4257. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4258. ),
  4259. array('div' => array('class' => 'checkbox')),
  4260. array('input' => array(
  4261. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4262. 'value' => '0', 'id' => 'ModelMultiField0'
  4263. )),
  4264. array('label' => array('for' => 'ModelMultiField0')),
  4265. 'first',
  4266. '/label',
  4267. '/div',
  4268. array('div' => array('class' => 'checkbox')),
  4269. array('input' => array(
  4270. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4271. 'value' => '1', 'id' => 'ModelMultiField1'
  4272. )),
  4273. array('label' => array('for' => 'ModelMultiField1')),
  4274. 'second',
  4275. '/label',
  4276. '/div',
  4277. array('div' => array('class' => 'checkbox')),
  4278. array('input' => array(
  4279. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4280. 'value' => '2', 'id' => 'ModelMultiField2'
  4281. )),
  4282. array('label' => array('for' => 'ModelMultiField2')),
  4283. 'third',
  4284. '/label',
  4285. '/div'
  4286. );
  4287. $this->assertTags($result, $expected);
  4288. $result = $this->Form->select(
  4289. 'Model.multi_field',
  4290. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4291. array('multiple' => 'checkbox')
  4292. );
  4293. $expected = array(
  4294. 'input' => array(
  4295. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4296. ),
  4297. array('div' => array('class' => 'checkbox')),
  4298. array('input' => array(
  4299. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4300. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4301. )),
  4302. array('label' => array('for' => 'ModelMultiFieldA')),
  4303. 'first',
  4304. '/label',
  4305. '/div',
  4306. array('div' => array('class' => 'checkbox')),
  4307. array('input' => array(
  4308. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4309. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4310. )),
  4311. array('label' => array('for' => 'ModelMultiFieldB')),
  4312. 'second',
  4313. '/label',
  4314. '/div',
  4315. array('div' => array('class' => 'checkbox')),
  4316. array('input' => array(
  4317. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4318. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4319. )),
  4320. array('label' => array('for' => 'ModelMultiFieldC')),
  4321. 'third',
  4322. '/label',
  4323. '/div'
  4324. );
  4325. $this->assertTags($result, $expected);
  4326. $result = $this->Form->select(
  4327. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4328. );
  4329. $expected = array(
  4330. 'input' => array(
  4331. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4332. ),
  4333. array('div' => array('class' => 'checkbox')),
  4334. array('input' => array(
  4335. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4336. 'value' => '1', 'id' => 'ModelMultiField1'
  4337. )),
  4338. array('label' => array('for' => 'ModelMultiField1')),
  4339. 'first',
  4340. '/label',
  4341. '/div'
  4342. );
  4343. $this->assertTags($result, $expected);
  4344. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4345. $result = $this->Form->select(
  4346. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4347. );
  4348. $expected = array(
  4349. 'input' => array(
  4350. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4351. ),
  4352. array('div' => array('class' => 'checkbox')),
  4353. array('input' => array(
  4354. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4355. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4356. )),
  4357. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4358. 'first',
  4359. '/label',
  4360. '/div',
  4361. array('div' => array('class' => 'checkbox')),
  4362. array('input' => array(
  4363. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4364. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4365. )),
  4366. array('label' => array('for' => 'ModelTagsArray')),
  4367. 'Array',
  4368. '/label',
  4369. '/div'
  4370. );
  4371. $this->assertTags($result, $expected);
  4372. }
  4373. /**
  4374. * test multiple checkboxes with div styles.
  4375. *
  4376. * @return void
  4377. */
  4378. public function testSelectMultipleCheckboxDiv() {
  4379. $result = $this->Form->select(
  4380. 'Model.tags',
  4381. array('first', 'second'),
  4382. array('multiple' => 'checkbox', 'class' => 'my-class')
  4383. );
  4384. $expected = array(
  4385. 'input' => array(
  4386. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4387. ),
  4388. array('div' => array('class' => 'my-class')),
  4389. array('input' => array(
  4390. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4391. 'value' => '0', 'id' => 'ModelTags0'
  4392. )),
  4393. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4394. '/div',
  4395. array('div' => array('class' => 'my-class')),
  4396. array('input' => array(
  4397. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4398. 'value' => '1', 'id' => 'ModelTags1'
  4399. )),
  4400. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4401. '/div'
  4402. );
  4403. $this->assertTags($result, $expected);
  4404. $result = $this->Form->input('Model.tags', array(
  4405. 'options' => array('first', 'second'),
  4406. 'multiple' => 'checkbox',
  4407. 'class' => 'my-class',
  4408. 'div' => false,
  4409. 'label' => false
  4410. ));
  4411. $this->assertTags($result, $expected);
  4412. $Contact = ClassRegistry::getObject('Contact');
  4413. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4414. $result = $this->Form->input('Contact.tags', array(
  4415. 'options' => array('one'),
  4416. 'multiple' => 'checkbox',
  4417. 'label' => false,
  4418. 'div' => false
  4419. ));
  4420. $expected = array(
  4421. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4422. array('div' => array('class' => 'checkbox form-error')),
  4423. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4424. array('label' => array('for' => 'ContactTags0')),
  4425. 'one',
  4426. '/label',
  4427. '/div'
  4428. );
  4429. $this->assertTags($result, $expected);
  4430. $result = $this->Form->input('Contact.tags', array(
  4431. 'options' => array('one'),
  4432. 'multiple' => 'checkbox',
  4433. 'class' => 'mycheckbox',
  4434. 'label' => false,
  4435. 'div' => false
  4436. ));
  4437. $expected = array(
  4438. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4439. array('div' => array('class' => 'mycheckbox form-error')),
  4440. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4441. array('label' => array('for' => 'ContactTags0')),
  4442. 'one',
  4443. '/label',
  4444. '/div'
  4445. );
  4446. $this->assertTags($result, $expected);
  4447. }
  4448. /**
  4449. * Checks the security hash array generated for multiple-input checkbox elements
  4450. *
  4451. * @return void
  4452. */
  4453. public function testSelectMultipleCheckboxSecurity() {
  4454. $this->Form->request['_Token'] = array('key' => 'testKey');
  4455. $this->assertEquals(array(), $this->Form->fields);
  4456. $result = $this->Form->select(
  4457. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4458. array('multiple' => 'checkbox')
  4459. );
  4460. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4461. $result = $this->Form->secure($this->Form->fields);
  4462. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4463. $this->assertRegExp('/"' . $key . '"/', $result);
  4464. }
  4465. /**
  4466. * Multiple select elements should always be secured as they always participate
  4467. * in the POST data.
  4468. *
  4469. * @return void
  4470. */
  4471. public function testSelectMultipleSecureWithNoOptions() {
  4472. $this->Form->request['_Token'] = array('key' => 'testkey');
  4473. $this->assertEquals(array(), $this->Form->fields);
  4474. $this->Form->select(
  4475. 'Model.select',
  4476. array(),
  4477. array('multiple' => true)
  4478. );
  4479. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4480. }
  4481. /**
  4482. * When a select box has no options it should not be added to the fields list
  4483. * as it always fail post validation.
  4484. *
  4485. * @return void
  4486. */
  4487. public function testSelectNoSecureWithNoOptions() {
  4488. $this->Form->request['_Token'] = array('key' => 'testkey');
  4489. $this->assertEquals(array(), $this->Form->fields);
  4490. $this->Form->select(
  4491. 'Model.select',
  4492. array()
  4493. );
  4494. $this->assertEquals(array(), $this->Form->fields);
  4495. $this->Form->select(
  4496. 'Model.select',
  4497. array(),
  4498. array('empty' => true)
  4499. );
  4500. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4501. }
  4502. /**
  4503. * testInputMultipleCheckboxes method
  4504. *
  4505. * test input() resulting in multi select elements being generated.
  4506. *
  4507. * @return void
  4508. */
  4509. public function testInputMultipleCheckboxes() {
  4510. $result = $this->Form->input('Model.multi_field', array(
  4511. 'options' => array('first', 'second', 'third'),
  4512. 'multiple' => 'checkbox'
  4513. ));
  4514. $expected = array(
  4515. array('div' => array('class' => 'input select')),
  4516. array('label' => array('for' => 'ModelMultiField')),
  4517. 'Multi Field',
  4518. '/label',
  4519. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4520. array('div' => array('class' => 'checkbox')),
  4521. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4522. array('label' => array('for' => 'ModelMultiField0')),
  4523. 'first',
  4524. '/label',
  4525. '/div',
  4526. array('div' => array('class' => 'checkbox')),
  4527. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4528. array('label' => array('for' => 'ModelMultiField1')),
  4529. 'second',
  4530. '/label',
  4531. '/div',
  4532. array('div' => array('class' => 'checkbox')),
  4533. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4534. array('label' => array('for' => 'ModelMultiField2')),
  4535. 'third',
  4536. '/label',
  4537. '/div',
  4538. '/div'
  4539. );
  4540. $this->assertTags($result, $expected);
  4541. $result = $this->Form->input('Model.multi_field', array(
  4542. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4543. 'multiple' => 'checkbox'
  4544. ));
  4545. $expected = array(
  4546. array('div' => array('class' => 'input select')),
  4547. array('label' => array('for' => 'ModelMultiField')),
  4548. 'Multi Field',
  4549. '/label',
  4550. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4551. array('div' => array('class' => 'checkbox')),
  4552. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4553. array('label' => array('for' => 'ModelMultiFieldA')),
  4554. 'first',
  4555. '/label',
  4556. '/div',
  4557. array('div' => array('class' => 'checkbox')),
  4558. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4559. array('label' => array('for' => 'ModelMultiFieldB')),
  4560. 'second',
  4561. '/label',
  4562. '/div',
  4563. array('div' => array('class' => 'checkbox')),
  4564. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4565. array('label' => array('for' => 'ModelMultiFieldC')),
  4566. 'third',
  4567. '/label',
  4568. '/div',
  4569. '/div'
  4570. );
  4571. $this->assertTags($result, $expected);
  4572. $result = $this->Form->input('Model.multi_field', array(
  4573. 'options' => array('1' => 'first'),
  4574. 'multiple' => 'checkbox',
  4575. 'label' => false,
  4576. 'div' => false
  4577. ));
  4578. $expected = array(
  4579. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4580. array('div' => array('class' => 'checkbox')),
  4581. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4582. array('label' => array('for' => 'ModelMultiField1')),
  4583. 'first',
  4584. '/label',
  4585. '/div'
  4586. );
  4587. $this->assertTags($result, $expected);
  4588. $result = $this->Form->input('Model.multi_field', array(
  4589. 'options' => array('2' => 'second'),
  4590. 'multiple' => 'checkbox',
  4591. 'label' => false,
  4592. 'div' => false
  4593. ));
  4594. $expected = array(
  4595. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4596. array('div' => array('class' => 'checkbox')),
  4597. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4598. array('label' => array('for' => 'ModelMultiField2')),
  4599. 'second',
  4600. '/label',
  4601. '/div'
  4602. );
  4603. $this->assertTags($result, $expected);
  4604. }
  4605. /**
  4606. * testSelectHiddenFieldOmission method
  4607. *
  4608. * test that select() with 'hiddenField' => false omits the hidden field
  4609. *
  4610. * @return void
  4611. */
  4612. public function testSelectHiddenFieldOmission() {
  4613. $result = $this->Form->select('Model.multi_field',
  4614. array('first', 'second'),
  4615. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4616. );
  4617. $expected = array(
  4618. array('div' => array('class' => 'checkbox')),
  4619. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4620. array('label' => array('for' => 'ModelMultiField0')),
  4621. 'first',
  4622. '/label',
  4623. '/div',
  4624. array('div' => array('class' => 'checkbox')),
  4625. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4626. array('label' => array('for' => 'ModelMultiField1')),
  4627. 'second',
  4628. '/label',
  4629. '/div'
  4630. );
  4631. $this->assertTags($result, $expected);
  4632. $result = $this->Form->input('Model.multi_field', array(
  4633. 'options' => array('first', 'second'),
  4634. 'multiple' => 'checkbox',
  4635. 'hiddenField' => false
  4636. ));
  4637. $expected = array(
  4638. array('div' => array('class' => 'input select')),
  4639. array('label' => array('for' => 'ModelMultiField')),
  4640. 'Multi Field',
  4641. '/label',
  4642. array('div' => array('class' => 'checkbox')),
  4643. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4644. array('label' => array('for' => 'ModelMultiField0')),
  4645. 'first',
  4646. '/label',
  4647. '/div',
  4648. array('div' => array('class' => 'checkbox')),
  4649. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4650. array('label' => array('for' => 'ModelMultiField1')),
  4651. 'second',
  4652. '/label',
  4653. '/div',
  4654. '/div'
  4655. );
  4656. $this->assertTags($result, $expected);
  4657. }
  4658. /**
  4659. * test that select() with multiple = checkbox works with overriding name attribute.
  4660. *
  4661. * @return void
  4662. */
  4663. public function testSelectCheckboxMultipleOverrideName() {
  4664. $result = $this->Form->input('category', array(
  4665. 'type' => 'select',
  4666. 'multiple' => 'checkbox',
  4667. 'name' => 'data[fish]',
  4668. 'options' => array('1', '2'),
  4669. 'div' => false,
  4670. 'label' => false,
  4671. ));
  4672. $expected = array(
  4673. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4674. array('div' => array('class' => 'checkbox')),
  4675. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4676. array('label' => array('for' => 'Category0')), '1', '/label',
  4677. '/div',
  4678. array('div' => array('class' => 'checkbox')),
  4679. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4680. array('label' => array('for' => 'Category1')), '2', '/label',
  4681. '/div'
  4682. );
  4683. $this->assertTags($result, $expected);
  4684. }
  4685. /**
  4686. * Test that 'id' overrides all the checkbox id's as well.
  4687. *
  4688. * @return void
  4689. */
  4690. public function testSelectCheckboxMultipleId() {
  4691. $result = $this->Form->select(
  4692. 'Model.multi_field',
  4693. array('first', 'second', 'third'),
  4694. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4695. );
  4696. $expected = array(
  4697. 'input' => array(
  4698. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4699. ),
  4700. array('div' => array('class' => 'checkbox')),
  4701. array('input' => array(
  4702. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4703. 'value' => '0', 'id' => 'CustomId0'
  4704. )),
  4705. array('label' => array('for' => 'CustomId0')),
  4706. 'first',
  4707. '/label',
  4708. '/div',
  4709. array('div' => array('class' => 'checkbox')),
  4710. array('input' => array(
  4711. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4712. 'value' => '1', 'id' => 'CustomId1'
  4713. )),
  4714. array('label' => array('for' => 'CustomId1')),
  4715. 'second',
  4716. '/label',
  4717. '/div',
  4718. array('div' => array('class' => 'checkbox')),
  4719. array('input' => array(
  4720. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4721. 'value' => '2', 'id' => 'CustomId2'
  4722. )),
  4723. array('label' => array('for' => 'CustomId2')),
  4724. 'third',
  4725. '/label',
  4726. '/div'
  4727. );
  4728. $this->assertTags($result, $expected);
  4729. }
  4730. /**
  4731. * testCheckbox method
  4732. *
  4733. * Test generation of checkboxes
  4734. *
  4735. * @return void
  4736. */
  4737. public function testCheckbox() {
  4738. $result = $this->Form->checkbox('Model.field');
  4739. $expected = array(
  4740. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4741. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4742. );
  4743. $this->assertTags($result, $expected);
  4744. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4745. $expected = array(
  4746. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4747. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4748. );
  4749. $this->assertTags($result, $expected);
  4750. $Contact = ClassRegistry::getObject('Contact');
  4751. $Contact->validationErrors['field'] = 1;
  4752. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4753. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4754. $expected = array(
  4755. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4756. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4757. );
  4758. $this->assertTags($result, $expected);
  4759. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4760. $expected = array(
  4761. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4762. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4763. );
  4764. $this->assertTags($result, $expected);
  4765. $this->Form->request->data['Contact']['field'] = '';
  4766. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4767. $expected = array(
  4768. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4769. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4770. );
  4771. $this->assertTags($result, $expected);
  4772. $Contact->validationErrors = array();
  4773. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4774. $expected = array(
  4775. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4776. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4777. );
  4778. $this->assertTags($result, $expected);
  4779. $this->Form->request->data['Contact']['published'] = 1;
  4780. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4781. $expected = array(
  4782. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4783. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4784. );
  4785. $this->assertTags($result, $expected);
  4786. $this->Form->request->data['Contact']['published'] = 0;
  4787. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4788. $expected = array(
  4789. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4790. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4791. );
  4792. $this->assertTags($result, $expected);
  4793. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4794. $expected = array(
  4795. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4796. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4797. );
  4798. $this->assertTags($result, $expected);
  4799. $result = $this->Form->checkbox('CustomField.1.value');
  4800. $expected = array(
  4801. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4802. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4803. );
  4804. $this->assertTags($result, $expected);
  4805. }
  4806. /**
  4807. * test checkbox() with a custom name attribute
  4808. *
  4809. * @return void
  4810. */
  4811. public function testCheckboxCustomNameAttribute() {
  4812. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4813. $expected = array(
  4814. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4815. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4816. );
  4817. $this->assertTags($result, $expected);
  4818. }
  4819. /**
  4820. * test the checked option for checkboxes.
  4821. *
  4822. * @return void
  4823. */
  4824. public function testCheckboxCheckedOption() {
  4825. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4826. $expected = array(
  4827. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4828. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4829. );
  4830. $this->assertTags($result, $expected);
  4831. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4832. $expected = array(
  4833. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4834. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4835. );
  4836. $this->assertTags($result, $expected);
  4837. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4838. $expected = array(
  4839. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4840. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4841. );
  4842. $this->assertTags($result, $expected);
  4843. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4844. $expected = array(
  4845. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4846. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4847. );
  4848. $this->assertTags($result, $expected);
  4849. $this->Form->request->data['Model']['field'] = 1;
  4850. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4851. $expected = array(
  4852. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4853. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4854. );
  4855. $this->assertTags($result, $expected);
  4856. }
  4857. /**
  4858. * Test that disabled attribute works on both the checkbox and hidden input.
  4859. *
  4860. * @return void
  4861. */
  4862. public function testCheckboxDisabling() {
  4863. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4864. $expected = array(
  4865. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4866. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4867. );
  4868. $this->assertTags($result, $expected);
  4869. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4870. $expected = array(
  4871. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4872. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4873. );
  4874. $this->assertTags($result, $expected);
  4875. }
  4876. /**
  4877. * Test that the hidden input for checkboxes can be omitted or set to a
  4878. * specific value.
  4879. *
  4880. * @return void
  4881. */
  4882. public function testCheckboxHiddenField() {
  4883. $result = $this->Form->input('UserForm.something', array(
  4884. 'type' => 'checkbox',
  4885. 'hiddenField' => false
  4886. ));
  4887. $expected = array(
  4888. 'div' => array('class' => 'input checkbox'),
  4889. array('input' => array(
  4890. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4891. 'value' => '1', 'id' => 'UserFormSomething'
  4892. )),
  4893. 'label' => array('for' => 'UserFormSomething'),
  4894. 'Something',
  4895. '/label',
  4896. '/div'
  4897. );
  4898. $this->assertTags($result, $expected);
  4899. $result = $this->Form->input('UserForm.something', array(
  4900. 'type' => 'checkbox',
  4901. 'value' => 'Y',
  4902. 'hiddenField' => 'N',
  4903. ));
  4904. $expected = array(
  4905. 'div' => array('class' => 'input checkbox'),
  4906. array('input' => array(
  4907. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4908. 'value' => 'N', 'id' => 'UserFormSomething_'
  4909. )),
  4910. array('input' => array(
  4911. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4912. 'value' => 'Y', 'id' => 'UserFormSomething'
  4913. )),
  4914. 'label' => array('for' => 'UserFormSomething'),
  4915. 'Something',
  4916. '/label',
  4917. '/div'
  4918. );
  4919. $this->assertTags($result, $expected);
  4920. }
  4921. /**
  4922. * testDateTime method
  4923. *
  4924. * Test generation of date/time select elements
  4925. *
  4926. * @return void
  4927. */
  4928. public function testDateTime() {
  4929. extract($this->dateRegex);
  4930. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4931. $now = strtotime('now');
  4932. $expected = array(
  4933. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4934. $daysRegex,
  4935. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4936. date('j', $now),
  4937. '/option',
  4938. '*/select',
  4939. '-',
  4940. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4941. $monthsRegex,
  4942. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4943. date('F', $now),
  4944. '/option',
  4945. '*/select',
  4946. '-',
  4947. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4948. $yearsRegex,
  4949. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4950. date('Y', $now),
  4951. '/option',
  4952. '*/select',
  4953. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4954. $hoursRegex,
  4955. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4956. date('g', $now),
  4957. '/option',
  4958. '*/select',
  4959. ':',
  4960. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4961. $minutesRegex,
  4962. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4963. date('i', $now),
  4964. '/option',
  4965. '*/select',
  4966. ' ',
  4967. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4968. $meridianRegex,
  4969. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4970. date('a', $now),
  4971. '/option',
  4972. '*/select'
  4973. );
  4974. $this->assertTags($result, $expected);
  4975. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4976. $expected = array(
  4977. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4978. $daysRegex,
  4979. array('option' => array('value' => '')),
  4980. '/option',
  4981. '*/select',
  4982. '-',
  4983. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4984. $monthsRegex,
  4985. array('option' => array('value' => '')),
  4986. '/option',
  4987. '*/select',
  4988. '-',
  4989. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4990. $yearsRegex,
  4991. array('option' => array('value' => '')),
  4992. '/option',
  4993. '*/select',
  4994. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4995. $hoursRegex,
  4996. array('option' => array('value' => '')),
  4997. '/option',
  4998. '*/select',
  4999. ':',
  5000. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5001. $minutesRegex,
  5002. array('option' => array('value' => '')),
  5003. '/option',
  5004. '*/select',
  5005. ' ',
  5006. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5007. $meridianRegex,
  5008. array('option' => array('value' => '')),
  5009. '/option',
  5010. '*/select'
  5011. );
  5012. $this->assertTags($result, $expected);
  5013. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5014. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5015. $this->assertTags($result, $expected);
  5016. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5017. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5018. $this->assertTags($result, $expected);
  5019. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5020. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5021. $expected = array(
  5022. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5023. $daysRegex,
  5024. array('option' => array('value' => '')),
  5025. '/option',
  5026. '*/select',
  5027. '-',
  5028. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5029. $monthsRegex,
  5030. array('option' => array('value' => '')),
  5031. '/option',
  5032. '*/select',
  5033. '-',
  5034. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5035. $yearsRegex,
  5036. array('option' => array('value' => '')),
  5037. '/option',
  5038. '*/select',
  5039. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5040. $hoursRegex,
  5041. array('option' => array('value' => '')),
  5042. '/option',
  5043. '*/select',
  5044. ':',
  5045. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5046. $minutesRegex,
  5047. array('option' => array('value' => '')),
  5048. '/option',
  5049. array('option' => array('value' => '00')),
  5050. '00',
  5051. '/option',
  5052. array('option' => array('value' => '05')),
  5053. '05',
  5054. '/option',
  5055. array('option' => array('value' => '10')),
  5056. '10',
  5057. '/option',
  5058. '*/select',
  5059. ' ',
  5060. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5061. $meridianRegex,
  5062. array('option' => array('value' => '')),
  5063. '/option',
  5064. '*/select'
  5065. );
  5066. $this->assertTags($result, $expected);
  5067. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5068. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5069. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5070. $this->Form->request->data['Contact']['data'] = null;
  5071. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5072. $expected = array(
  5073. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5074. $daysRegex,
  5075. array('option' => array('value' => '')),
  5076. '/option',
  5077. '*/select',
  5078. '-',
  5079. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5080. $monthsRegex,
  5081. array('option' => array('value' => '')),
  5082. '/option',
  5083. '*/select',
  5084. '-',
  5085. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5086. $yearsRegex,
  5087. array('option' => array('value' => '')),
  5088. '/option',
  5089. '*/select',
  5090. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5091. $hoursRegex,
  5092. array('option' => array('value' => '')),
  5093. '/option',
  5094. '*/select',
  5095. ':',
  5096. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5097. $minutesRegex,
  5098. array('option' => array('value' => '')),
  5099. '/option',
  5100. '*/select',
  5101. ' ',
  5102. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5103. $meridianRegex,
  5104. array('option' => array('value' => '')),
  5105. '/option',
  5106. '*/select'
  5107. );
  5108. $this->assertTags($result, $expected);
  5109. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5110. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5111. $now = strtotime($this->Form->data['Model']['field']);
  5112. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5113. $expected = array(
  5114. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5115. $daysRegex,
  5116. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5117. date('j', $now),
  5118. '/option',
  5119. '*/select',
  5120. '-',
  5121. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5122. $monthsRegex,
  5123. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5124. date('F', $now),
  5125. '/option',
  5126. '*/select',
  5127. '-',
  5128. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5129. $yearsRegex,
  5130. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5131. date('Y', $now),
  5132. '/option',
  5133. '*/select',
  5134. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5135. $hoursRegex,
  5136. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5137. date('g', $now),
  5138. '/option',
  5139. '*/select',
  5140. ':',
  5141. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5142. $minutesRegex,
  5143. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5144. date('i', $now),
  5145. '/option',
  5146. '*/select',
  5147. ' ',
  5148. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5149. $meridianRegex,
  5150. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5151. date('a', $now),
  5152. '/option',
  5153. '*/select'
  5154. );
  5155. $this->assertTags($result, $expected);
  5156. $selected = strtotime('2008-10-26 12:33:00');
  5157. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5158. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5159. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5160. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5161. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5162. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5163. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5164. $this->Form->create('Contact');
  5165. $result = $this->Form->input('published');
  5166. $now = strtotime('now');
  5167. $expected = array(
  5168. 'div' => array('class' => 'input date'),
  5169. 'label' => array('for' => 'ContactPublishedMonth'),
  5170. 'Published',
  5171. '/label',
  5172. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5173. $monthsRegex,
  5174. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5175. date('F', $now),
  5176. '/option',
  5177. '*/select',
  5178. '-',
  5179. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5180. $daysRegex,
  5181. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5182. date('j', $now),
  5183. '/option',
  5184. '*/select',
  5185. '-',
  5186. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5187. $yearsRegex,
  5188. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5189. date('Y', $now),
  5190. '/option',
  5191. '*/select',
  5192. '/div'
  5193. );
  5194. $this->assertTags($result, $expected);
  5195. $result = $this->Form->input('published2', array('type' => 'date'));
  5196. $now = strtotime('now');
  5197. $expected = array(
  5198. 'div' => array('class' => 'input date'),
  5199. 'label' => array('for' => 'ContactPublished2Month'),
  5200. 'Published2',
  5201. '/label',
  5202. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5203. $monthsRegex,
  5204. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5205. date('F', $now),
  5206. '/option',
  5207. '*/select',
  5208. '-',
  5209. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5210. $daysRegex,
  5211. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5212. date('j', $now),
  5213. '/option',
  5214. '*/select',
  5215. '-',
  5216. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5217. $yearsRegex,
  5218. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5219. date('Y', $now),
  5220. '/option',
  5221. '*/select',
  5222. '/div'
  5223. );
  5224. $this->assertTags($result, $expected);
  5225. $this->Form->create('Contact');
  5226. $result = $this->Form->input('published', array('monthNames' => false));
  5227. $now = strtotime('now');
  5228. $expected = array(
  5229. 'div' => array('class' => 'input date'),
  5230. 'label' => array('for' => 'ContactPublishedMonth'),
  5231. 'Published',
  5232. '/label',
  5233. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5234. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5235. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5236. date('m', $now),
  5237. '/option',
  5238. '*/select',
  5239. '-',
  5240. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5241. $daysRegex,
  5242. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5243. date('j', $now),
  5244. '/option',
  5245. '*/select',
  5246. '-',
  5247. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5248. $yearsRegex,
  5249. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5250. date('Y', $now),
  5251. '/option',
  5252. '*/select',
  5253. '/div'
  5254. );
  5255. $this->assertTags($result, $expected);
  5256. $result = $this->Form->input('published', array(
  5257. 'timeFormat' => 24,
  5258. 'interval' => 5,
  5259. 'selected' => strtotime('2009-09-03 13:37:00'),
  5260. 'type' => 'datetime'
  5261. ));
  5262. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5263. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5264. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5265. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5266. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5267. }
  5268. /**
  5269. * Test that empty values don't trigger errors.
  5270. *
  5271. * @return void
  5272. */
  5273. public function testDateTimeNoErrorsOnEmptyData() {
  5274. $this->Form->request->data['Contact'] = array(
  5275. 'date' => array(
  5276. 'day' => '',
  5277. 'month' => '',
  5278. 'year' => '',
  5279. 'hour' => '',
  5280. 'min' => '',
  5281. 'meridian' => ''
  5282. )
  5283. );
  5284. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5285. $this->assertNotEmpty($result);
  5286. }
  5287. /**
  5288. * test that datetime() and default values work.
  5289. *
  5290. * @return void
  5291. */
  5292. public function testDatetimeWithDefault() {
  5293. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5294. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5295. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5296. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5297. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5298. 'default' => '2009-06-01 11:15:30'
  5299. ));
  5300. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5301. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5302. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5303. }
  5304. /**
  5305. * test that bogus non-date time data doesn't cause errors.
  5306. *
  5307. * @return void
  5308. */
  5309. public function testDateTimeWithBogusData() {
  5310. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5311. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5312. }
  5313. /**
  5314. * testDateTimeEmptyAsArray
  5315. *
  5316. * @return void
  5317. */
  5318. public function testDateTimeEmptyAsArray() {
  5319. $result = $this->Form->dateTime('Contact.date',
  5320. 'DMY',
  5321. '12',
  5322. array(
  5323. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5324. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5325. )
  5326. )
  5327. );
  5328. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5329. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5330. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5331. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5332. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5333. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5334. $result = $this->Form->dateTime('Contact.date',
  5335. 'DMY',
  5336. '12',
  5337. array(
  5338. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5339. )
  5340. );
  5341. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5342. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5343. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5344. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5345. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5346. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5347. }
  5348. /**
  5349. * testFormDateTimeMulti method
  5350. *
  5351. * test multiple datetime element generation
  5352. *
  5353. * @return void
  5354. */
  5355. public function testFormDateTimeMulti() {
  5356. extract($this->dateRegex);
  5357. $result = $this->Form->dateTime('Contact.1.updated');
  5358. $expected = array(
  5359. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5360. $daysRegex,
  5361. array('option' => array('value' => '')),
  5362. '/option',
  5363. '*/select',
  5364. '-',
  5365. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5366. $monthsRegex,
  5367. array('option' => array('value' => '')),
  5368. '/option',
  5369. '*/select',
  5370. '-',
  5371. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5372. $yearsRegex,
  5373. array('option' => array('value' => '')),
  5374. '/option',
  5375. '*/select',
  5376. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5377. $hoursRegex,
  5378. array('option' => array('value' => '')),
  5379. '/option',
  5380. '*/select',
  5381. ':',
  5382. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5383. $minutesRegex,
  5384. array('option' => array('value' => '')),
  5385. '/option',
  5386. '*/select',
  5387. ' ',
  5388. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5389. $meridianRegex,
  5390. array('option' => array('value' => '')),
  5391. '/option',
  5392. '*/select'
  5393. );
  5394. $this->assertTags($result, $expected);
  5395. $result = $this->Form->dateTime('Contact.2.updated');
  5396. $expected = array(
  5397. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5398. $daysRegex,
  5399. array('option' => array('value' => '')),
  5400. '/option',
  5401. '*/select',
  5402. '-',
  5403. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5404. $monthsRegex,
  5405. array('option' => array('value' => '')),
  5406. '/option',
  5407. '*/select',
  5408. '-',
  5409. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5410. $yearsRegex,
  5411. array('option' => array('value' => '')),
  5412. '/option',
  5413. '*/select',
  5414. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5415. $hoursRegex,
  5416. array('option' => array('value' => '')),
  5417. '/option',
  5418. '*/select',
  5419. ':',
  5420. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5421. $minutesRegex,
  5422. array('option' => array('value' => '')),
  5423. '/option',
  5424. '*/select',
  5425. ' ',
  5426. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5427. $meridianRegex,
  5428. array('option' => array('value' => '')),
  5429. '/option',
  5430. '*/select'
  5431. );
  5432. $this->assertTags($result, $expected);
  5433. }
  5434. /**
  5435. * When changing the date format, the label should always focus the first select box when
  5436. * clicked.
  5437. *
  5438. * @return void
  5439. */
  5440. public function testDateTimeLabelIdMatchesFirstInput() {
  5441. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5442. $this->assertContains('label for="ModelDateMonth"', $result);
  5443. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5444. $this->assertContains('label for="ModelDateDay"', $result);
  5445. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5446. $this->assertContains('label for="ModelDateYear"', $result);
  5447. }
  5448. /**
  5449. * testMonth method
  5450. *
  5451. * @return void
  5452. */
  5453. public function testMonth() {
  5454. $result = $this->Form->month('Model.field');
  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('empty' => true));
  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. date('F', strtotime('2008-01-01 00:00:00')),
  5475. '/option',
  5476. array('option' => array('value' => '02')),
  5477. date('F', strtotime('2008-02-01 00:00:00')),
  5478. '/option',
  5479. '*/select',
  5480. );
  5481. $this->assertTags($result, $expected);
  5482. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5483. $expected = array(
  5484. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5485. array('option' => array('value' => '')),
  5486. '/option',
  5487. array('option' => array('value' => '01')),
  5488. '01',
  5489. '/option',
  5490. array('option' => array('value' => '02')),
  5491. '02',
  5492. '/option',
  5493. '*/select',
  5494. );
  5495. $this->assertTags($result, $expected);
  5496. $monthNames = array(
  5497. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5498. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5499. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5500. $expected = array(
  5501. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5502. array('option' => array('value' => '')),
  5503. '/option',
  5504. array('option' => array('value' => '01')),
  5505. 'Jan',
  5506. '/option',
  5507. array('option' => array('value' => '02')),
  5508. 'Feb',
  5509. '/option',
  5510. '*/select',
  5511. );
  5512. $this->assertTags($result, $expected);
  5513. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5514. $result = $this->Form->month('Project.release');
  5515. $expected = array(
  5516. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  5517. array('option' => array('value' => '')),
  5518. '/option',
  5519. array('option' => array('value' => '01')),
  5520. 'January',
  5521. '/option',
  5522. array('option' => array('value' => '02', 'selected' => 'selected')),
  5523. 'February',
  5524. '/option',
  5525. '*/select',
  5526. );
  5527. $this->assertTags($result, $expected);
  5528. }
  5529. /**
  5530. * testDay method
  5531. *
  5532. * @return void
  5533. */
  5534. public function testDay() {
  5535. extract($this->dateRegex);
  5536. $result = $this->Form->day('Model.field', array('value' => false));
  5537. $expected = array(
  5538. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5539. array('option' => array('value' => '')),
  5540. '/option',
  5541. array('option' => array('value' => '01')),
  5542. '1',
  5543. '/option',
  5544. array('option' => array('value' => '02')),
  5545. '2',
  5546. '/option',
  5547. $daysRegex,
  5548. '/select',
  5549. );
  5550. $this->assertTags($result, $expected);
  5551. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5552. $result = $this->Form->day('Model.field');
  5553. $expected = array(
  5554. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5555. array('option' => array('value' => '')),
  5556. '/option',
  5557. array('option' => array('value' => '01')),
  5558. '1',
  5559. '/option',
  5560. array('option' => array('value' => '02')),
  5561. '2',
  5562. '/option',
  5563. $daysRegex,
  5564. array('option' => array('value' => '10', 'selected' => 'selected')),
  5565. '10',
  5566. '/option',
  5567. $daysRegex,
  5568. '/select',
  5569. );
  5570. $this->assertTags($result, $expected);
  5571. $this->Form->request->data['Model']['field'] = '';
  5572. $result = $this->Form->day('Model.field', array('value' => '10'));
  5573. $expected = array(
  5574. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5575. array('option' => array('value' => '')),
  5576. '/option',
  5577. array('option' => array('value' => '01')),
  5578. '1',
  5579. '/option',
  5580. array('option' => array('value' => '02')),
  5581. '2',
  5582. '/option',
  5583. $daysRegex,
  5584. array('option' => array('value' => '10', 'selected' => 'selected')),
  5585. '10',
  5586. '/option',
  5587. $daysRegex,
  5588. '/select',
  5589. );
  5590. $this->assertTags($result, $expected);
  5591. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5592. $result = $this->Form->day('Model.field', array('value' => true));
  5593. $expected = array(
  5594. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5595. array('option' => array('value' => '')),
  5596. '/option',
  5597. array('option' => array('value' => '01')),
  5598. '1',
  5599. '/option',
  5600. array('option' => array('value' => '02')),
  5601. '2',
  5602. '/option',
  5603. $daysRegex,
  5604. array('option' => array('value' => '10', 'selected' => 'selected')),
  5605. '10',
  5606. '/option',
  5607. $daysRegex,
  5608. '/select',
  5609. );
  5610. $this->assertTags($result, $expected);
  5611. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5612. $result = $this->Form->day('Project.release');
  5613. $expected = array(
  5614. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  5615. array('option' => array('value' => '')),
  5616. '/option',
  5617. array('option' => array('value' => '01')),
  5618. '1',
  5619. '/option',
  5620. array('option' => array('value' => '02')),
  5621. '2',
  5622. '/option',
  5623. $daysRegex,
  5624. array('option' => array('value' => '10', 'selected' => 'selected')),
  5625. '10',
  5626. '/option',
  5627. $daysRegex,
  5628. '/select',
  5629. );
  5630. $this->assertTags($result, $expected);
  5631. }
  5632. /**
  5633. * testMinute method
  5634. *
  5635. * @return void
  5636. */
  5637. public function testMinute() {
  5638. extract($this->dateRegex);
  5639. $result = $this->Form->minute('Model.field');
  5640. $expected = array(
  5641. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5642. array('option' => array('value' => '')),
  5643. '/option',
  5644. array('option' => array('value' => '00')),
  5645. '00',
  5646. '/option',
  5647. array('option' => array('value' => '01')),
  5648. '01',
  5649. '/option',
  5650. array('option' => array('value' => '02')),
  5651. '02',
  5652. '/option',
  5653. $minutesRegex,
  5654. '/select',
  5655. );
  5656. $this->assertTags($result, $expected);
  5657. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5658. $result = $this->Form->minute('Model.field');
  5659. $expected = array(
  5660. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5661. array('option' => array('value' => '')),
  5662. '/option',
  5663. array('option' => array('value' => '00')),
  5664. '00',
  5665. '/option',
  5666. array('option' => array('value' => '01')),
  5667. '01',
  5668. '/option',
  5669. array('option' => array('value' => '02')),
  5670. '02',
  5671. '/option',
  5672. $minutesRegex,
  5673. array('option' => array('value' => '12', 'selected' => 'selected')),
  5674. '12',
  5675. '/option',
  5676. $minutesRegex,
  5677. '/select',
  5678. );
  5679. $this->assertTags($result, $expected);
  5680. $this->Form->request->data['Model']['field'] = '';
  5681. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5682. $expected = array(
  5683. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5684. array('option' => array('value' => '')),
  5685. '/option',
  5686. array('option' => array('value' => '00')),
  5687. '00',
  5688. '/option',
  5689. array('option' => array('value' => '05')),
  5690. '05',
  5691. '/option',
  5692. array('option' => array('value' => '10')),
  5693. '10',
  5694. '/option',
  5695. $minutesRegex,
  5696. '/select',
  5697. );
  5698. $this->assertTags($result, $expected);
  5699. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5700. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5701. $expected = array(
  5702. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5703. array('option' => array('value' => '')),
  5704. '/option',
  5705. array('option' => array('value' => '00')),
  5706. '00',
  5707. '/option',
  5708. array('option' => array('value' => '05')),
  5709. '05',
  5710. '/option',
  5711. array('option' => array('value' => '10', 'selected' => 'selected')),
  5712. '10',
  5713. '/option',
  5714. $minutesRegex,
  5715. '/select',
  5716. );
  5717. $this->assertTags($result, $expected);
  5718. }
  5719. /**
  5720. * testHour method
  5721. *
  5722. * @return void
  5723. */
  5724. public function testHour() {
  5725. extract($this->dateRegex);
  5726. $result = $this->Form->hour('Model.field', false);
  5727. $expected = array(
  5728. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5729. array('option' => array('value' => '')),
  5730. '/option',
  5731. array('option' => array('value' => '01')),
  5732. '1',
  5733. '/option',
  5734. array('option' => array('value' => '02')),
  5735. '2',
  5736. '/option',
  5737. $hoursRegex,
  5738. '/select',
  5739. );
  5740. $this->assertTags($result, $expected);
  5741. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5742. $result = $this->Form->hour('Model.field', false);
  5743. $expected = array(
  5744. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5745. array('option' => array('value' => '')),
  5746. '/option',
  5747. array('option' => array('value' => '01')),
  5748. '1',
  5749. '/option',
  5750. array('option' => array('value' => '02')),
  5751. '2',
  5752. '/option',
  5753. $hoursRegex,
  5754. array('option' => array('value' => '12', 'selected' => 'selected')),
  5755. '12',
  5756. '/option',
  5757. '/select',
  5758. );
  5759. $this->assertTags($result, $expected);
  5760. $this->Form->request->data['Model']['field'] = '';
  5761. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5762. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5763. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5764. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5765. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5766. $result = $this->Form->hour('Model.field', true);
  5767. $expected = array(
  5768. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5769. array('option' => array('value' => '')),
  5770. '/option',
  5771. array('option' => array('value' => '00', 'selected' => 'selected')),
  5772. '0',
  5773. '/option',
  5774. array('option' => array('value' => '01')),
  5775. '1',
  5776. '/option',
  5777. array('option' => array('value' => '02')),
  5778. '2',
  5779. '/option',
  5780. $hoursRegex,
  5781. '/select',
  5782. );
  5783. $this->assertTags($result, $expected);
  5784. unset($this->Form->request->data['Model']['field']);
  5785. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5786. $thisHour = date('H');
  5787. $optValue = date('G');
  5788. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5789. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  5790. $result = $this->Form->hour('Model.field', true);
  5791. $expected = array(
  5792. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5793. array('option' => array('value' => '')),
  5794. '/option',
  5795. array('option' => array('value' => '00')),
  5796. '0',
  5797. '/option',
  5798. array('option' => array('value' => '01', 'selected' => 'selected')),
  5799. '1',
  5800. '/option',
  5801. array('option' => array('value' => '02')),
  5802. '2',
  5803. '/option',
  5804. $hoursRegex,
  5805. '/select',
  5806. );
  5807. $this->assertTags($result, $expected);
  5808. }
  5809. /**
  5810. * testYear method
  5811. *
  5812. * @return void
  5813. */
  5814. public function testYear() {
  5815. $result = $this->Form->year('Model.field', 2006, 2007);
  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' => '2007')),
  5821. '2007',
  5822. '/option',
  5823. array('option' => array('value' => '2006')),
  5824. '2006',
  5825. '/option',
  5826. '/select',
  5827. );
  5828. $this->assertTags($result, $expected);
  5829. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5830. $expected = array(
  5831. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5832. array('option' => array('value' => '')),
  5833. '/option',
  5834. array('option' => array('value' => '2006')),
  5835. '2006',
  5836. '/option',
  5837. array('option' => array('value' => '2007')),
  5838. '2007',
  5839. '/option',
  5840. '/select',
  5841. );
  5842. $this->assertTags($result, $expected);
  5843. $this->request->data['Contact']['published'] = '';
  5844. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5845. $expected = array(
  5846. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5847. array('option' => array('value' => '')),
  5848. '/option',
  5849. array('option' => array('value' => '2007')),
  5850. '2007',
  5851. '/option',
  5852. array('option' => array('value' => '2006')),
  5853. '2006',
  5854. '/option',
  5855. '/select',
  5856. );
  5857. $this->assertTags($result, $expected);
  5858. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5859. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5860. $expected = array(
  5861. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5862. array('option' => array('value' => '2007')),
  5863. '2007',
  5864. '/option',
  5865. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5866. '2006',
  5867. '/option',
  5868. '/select',
  5869. );
  5870. $this->assertTags($result, $expected);
  5871. $this->Form->request->data['Contact']['published'] = '';
  5872. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5873. $expected = array(
  5874. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5875. array('option' => array('value' => '')),
  5876. '/option',
  5877. array('option' => array('value' => '2007')),
  5878. '2007',
  5879. '/option',
  5880. array('option' => array('value' => '2006')),
  5881. '2006',
  5882. '/option',
  5883. '/select',
  5884. );
  5885. $this->assertTags($result, $expected);
  5886. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5887. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5888. $expected = array(
  5889. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5890. array('option' => array('value' => '2007')),
  5891. '2007',
  5892. '/option',
  5893. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5894. '2006',
  5895. '/option',
  5896. '/select',
  5897. );
  5898. $this->assertTags($result, $expected);
  5899. $this->Form->request->data['Contact']['published'] = '';
  5900. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5901. $expected = array(
  5902. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5903. array('option' => array('value' => '')),
  5904. '/option',
  5905. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5906. '2007',
  5907. '/option',
  5908. array('option' => array('value' => '2006')),
  5909. '2006',
  5910. '/option',
  5911. '/select',
  5912. );
  5913. $this->assertTags($result, $expected);
  5914. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5915. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5916. $expected = array(
  5917. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5918. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5919. '2007',
  5920. '/option',
  5921. array('option' => array('value' => '2006')),
  5922. '2006',
  5923. '/option',
  5924. '/select',
  5925. );
  5926. $this->assertTags($result, $expected);
  5927. $this->Form->request->data['Contact']['published'] = '';
  5928. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5929. $expected = array(
  5930. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5931. array('option' => array('value' => '2008')),
  5932. '2008',
  5933. '/option',
  5934. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5935. '2007',
  5936. '/option',
  5937. array('option' => array('value' => '2006')),
  5938. '2006',
  5939. '/option',
  5940. '/select',
  5941. );
  5942. $this->assertTags($result, $expected);
  5943. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5944. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5945. $expected = array(
  5946. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5947. array('option' => array('value' => '2008')),
  5948. '2008',
  5949. '/option',
  5950. array('option' => array('value' => '2007')),
  5951. '2007',
  5952. '/option',
  5953. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5954. '2006',
  5955. '/option',
  5956. '/select',
  5957. );
  5958. $this->assertTags($result, $expected);
  5959. $this->Form->request->data = array();
  5960. $this->Form->create('Contact');
  5961. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5962. $expected = array(
  5963. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5964. array('option' => array('value' => '2008')),
  5965. '2008',
  5966. '/option',
  5967. array('option' => array('value' => '2007')),
  5968. '2007',
  5969. '/option',
  5970. array('option' => array('value' => '2006')),
  5971. '2006',
  5972. '/option',
  5973. '/select',
  5974. );
  5975. $this->assertTags($result, $expected);
  5976. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5977. $this->assertContains('data[Contact][published][year]', $result);
  5978. }
  5979. /**
  5980. * testYearAutoExpandRange method
  5981. *
  5982. * @return void
  5983. */
  5984. public function testYearAutoExpandRange() {
  5985. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  5986. $result = $this->Form->year('User.birthday');
  5987. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5988. $result = $matches[1];
  5989. $expected = range(date('Y') + 20, 1930);
  5990. $this->assertEquals($result, $expected);
  5991. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5992. $result = $this->Form->year('Project.release');
  5993. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5994. $result = $matches[1];
  5995. $expected = range(2050, date('Y') - 20);
  5996. $this->assertEquals($result, $expected);
  5997. $this->Form->request->data['Project']['release'] = '1881-10-10';
  5998. $result = $this->Form->year('Project.release', 1890, 1900);
  5999. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6000. $result = $matches[1];
  6001. $expected = range(1900, 1881);
  6002. $this->assertEquals($result, $expected);
  6003. }
  6004. /**
  6005. * testTextArea method
  6006. *
  6007. * @return void
  6008. */
  6009. public function testTextArea() {
  6010. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6011. $result = $this->Form->textarea('Model.field');
  6012. $expected = array(
  6013. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6014. 'some test data',
  6015. '/textarea',
  6016. );
  6017. $this->assertTags($result, $expected);
  6018. $result = $this->Form->textarea('Model.tmp');
  6019. $expected = array(
  6020. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6021. '/textarea',
  6022. );
  6023. $this->assertTags($result, $expected);
  6024. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6025. $result = $this->Form->textarea('Model.field');
  6026. $expected = array(
  6027. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6028. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6029. '/textarea',
  6030. );
  6031. $this->assertTags($result, $expected);
  6032. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6033. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6034. $expected = array(
  6035. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6036. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6037. '/textarea',
  6038. );
  6039. $this->assertTags($result, $expected);
  6040. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6041. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6042. $expected = array(
  6043. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6044. '/textarea'
  6045. );
  6046. $this->assertTags($result, $expected);
  6047. }
  6048. /**
  6049. * testTextAreaWithStupidCharacters method
  6050. *
  6051. * test text area with non-ascii characters
  6052. *
  6053. * @return void
  6054. */
  6055. public function testTextAreaWithStupidCharacters() {
  6056. $this->loadFixtures('Post');
  6057. $result = $this->Form->input('Post.content', array(
  6058. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6059. ));
  6060. $expected = array(
  6061. 'div' => array('class' => 'input textarea'),
  6062. 'label' => array('for' => 'PostContent'),
  6063. 'Current Text',
  6064. '/label',
  6065. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6066. 'GREAT®',
  6067. '/textarea',
  6068. '/div'
  6069. );
  6070. $this->assertTags($result, $expected);
  6071. }
  6072. /**
  6073. * testHiddenField method
  6074. *
  6075. * @return void
  6076. */
  6077. public function testHiddenField() {
  6078. $Contact = ClassRegistry::getObject('Contact');
  6079. $Contact->validationErrors['field'] = 1;
  6080. $this->Form->request->data['Contact']['field'] = 'test';
  6081. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6082. $this->assertTags($result, array(
  6083. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6084. );
  6085. }
  6086. /**
  6087. * testFileUploadField method
  6088. *
  6089. * @return void
  6090. */
  6091. public function testFileUploadField() {
  6092. $result = $this->Form->file('Model.upload');
  6093. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6094. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6095. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6096. $expected = array(
  6097. 'div' => array('class' => 'input file'),
  6098. 'label' => array('for' => 'ModelUpload'),
  6099. 'Upload',
  6100. '/label',
  6101. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6102. '/div'
  6103. );
  6104. $this->assertTags($result, $expected);
  6105. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6106. $result = $this->Form->file('Model.upload');
  6107. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6108. }
  6109. /**
  6110. * test File upload input on a model not used in create();
  6111. *
  6112. * @return void
  6113. */
  6114. public function testFileUploadOnOtherModel() {
  6115. $this->Form->create('ValidateUser', array('type' => 'file'));
  6116. $result = $this->Form->file('ValidateProfile.city');
  6117. $expected = array(
  6118. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6119. );
  6120. $this->assertTags($result, $expected);
  6121. }
  6122. /**
  6123. * testButton method
  6124. *
  6125. * @return void
  6126. */
  6127. public function testButton() {
  6128. $result = $this->Form->button('Hi');
  6129. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6130. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6131. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6132. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6133. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6134. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6135. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6136. $result = $this->Form->button('No type', array('type' => false));
  6137. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6138. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6139. $this->assertNotRegExp('/\&039/', $result);
  6140. }
  6141. /**
  6142. * Test that button() makes unlocked fields by default.
  6143. *
  6144. * @return void
  6145. */
  6146. public function testButtonUnlockedByDefault() {
  6147. $this->Form->request->params['_Token']['key'] = 'secured';
  6148. $this->Form->button('Save', array('name' => 'save'));
  6149. $this->Form->button('Clear');
  6150. $result = $this->Form->unlockField();
  6151. $this->assertEquals(array('save'), $result);
  6152. }
  6153. /**
  6154. * testPostButton method
  6155. *
  6156. * @return void
  6157. */
  6158. public function testPostButton() {
  6159. $result = $this->Form->postButton('Hi', '/controller/action');
  6160. $this->assertTags($result, array(
  6161. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6162. 'div' => array('style' => 'display:none;'),
  6163. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6164. '/div',
  6165. 'button' => array('type' => 'submit'),
  6166. 'Hi',
  6167. '/button',
  6168. '/form'
  6169. ));
  6170. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6171. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6172. }
  6173. /**
  6174. * Test that postButton adds _Token fields.
  6175. *
  6176. * @return void
  6177. */
  6178. public function testSecurePostButton() {
  6179. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6180. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6181. $expected = array(
  6182. 'form' => array(
  6183. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6184. ),
  6185. array('div' => array('style' => 'display:none;')),
  6186. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6187. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6188. '/div',
  6189. 'button' => array('type' => 'submit'),
  6190. 'Delete',
  6191. '/button',
  6192. array('div' => array('style' => 'display:none;')),
  6193. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6194. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6195. '/div',
  6196. '/form',
  6197. );
  6198. $this->assertTags($result, $expected);
  6199. }
  6200. /**
  6201. * testPostLink method
  6202. *
  6203. * @return void
  6204. */
  6205. public function testPostLink() {
  6206. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6207. $this->assertTags($result, array(
  6208. 'form' => array(
  6209. 'method' => 'post', 'action' => '/posts/delete/1',
  6210. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6211. ),
  6212. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6213. '/form',
  6214. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6215. 'Delete',
  6216. '/a'
  6217. ));
  6218. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6219. $this->assertTags($result, array(
  6220. 'form' => array(
  6221. 'method' => 'post', 'action' => '/posts/delete/1',
  6222. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6223. ),
  6224. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6225. '/form',
  6226. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6227. 'Delete',
  6228. '/a'
  6229. ));
  6230. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6231. $this->assertTags($result, array(
  6232. 'form' => array(
  6233. 'method' => 'post', '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:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6239. 'Delete',
  6240. '/a'
  6241. ));
  6242. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6243. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6244. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6245. $this->assertTags($result, array(
  6246. 'form' => array(
  6247. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6248. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6249. ),
  6250. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6251. '/form',
  6252. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6253. 'Delete',
  6254. '/a'
  6255. ));
  6256. }
  6257. /**
  6258. * Test that postLink adds _Token fields.
  6259. *
  6260. * @return void
  6261. */
  6262. public function testSecurePostLink() {
  6263. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6264. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6265. $expected = array(
  6266. 'form' => array(
  6267. 'method' => 'post', 'action' => '/posts/delete/1',
  6268. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6269. ),
  6270. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6271. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6272. 'div' => array('style' => 'display:none;'),
  6273. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6274. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6275. '/div',
  6276. '/form',
  6277. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6278. 'Delete',
  6279. '/a'
  6280. );
  6281. $this->assertTags($result, $expected);
  6282. }
  6283. /**
  6284. * testSubmitButton method
  6285. *
  6286. * @return void
  6287. */
  6288. public function testSubmitButton() {
  6289. $result = $this->Form->submit('');
  6290. $expected = array(
  6291. 'div' => array('class' => 'submit'),
  6292. 'input' => array('type' => 'submit', 'value' => ''),
  6293. '/div'
  6294. );
  6295. $this->assertTags($result, $expected);
  6296. $result = $this->Form->submit('Test Submit');
  6297. $expected = array(
  6298. 'div' => array('class' => 'submit'),
  6299. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6300. '/div'
  6301. );
  6302. $this->assertTags($result, $expected);
  6303. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6304. $expected = array(
  6305. 'span' => array('class' => 'submit'),
  6306. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6307. '/span'
  6308. );
  6309. $this->assertTags($result, $expected);
  6310. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6311. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6312. $this->assertTags($result, $expected);
  6313. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6314. $expected = array(
  6315. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6316. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6317. '/div'
  6318. );
  6319. $this->assertTags($result, $expected);
  6320. $result = $this->Form->submit('Next >');
  6321. $expected = array(
  6322. 'div' => array('class' => 'submit'),
  6323. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6324. '/div'
  6325. );
  6326. $this->assertTags($result, $expected);
  6327. $result = $this->Form->submit('Next >', array('escape' => false));
  6328. $expected = array(
  6329. 'div' => array('class' => 'submit'),
  6330. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6331. '/div'
  6332. );
  6333. $this->assertTags($result, $expected);
  6334. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6335. $expected = array(
  6336. 'div' => array('class' => 'submit'),
  6337. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6338. '/div'
  6339. );
  6340. $this->assertTags($result, $expected);
  6341. $before = '--before--';
  6342. $after = '--after--';
  6343. $result = $this->Form->submit('Test', array('before' => $before));
  6344. $expected = array(
  6345. 'div' => array('class' => 'submit'),
  6346. '--before--',
  6347. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6348. '/div'
  6349. );
  6350. $this->assertTags($result, $expected);
  6351. $result = $this->Form->submit('Test', array('after' => $after));
  6352. $expected = array(
  6353. 'div' => array('class' => 'submit'),
  6354. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6355. '--after--',
  6356. '/div'
  6357. );
  6358. $this->assertTags($result, $expected);
  6359. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6360. $expected = array(
  6361. 'div' => array('class' => 'submit'),
  6362. '--before--',
  6363. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6364. '--after--',
  6365. '/div'
  6366. );
  6367. $this->assertTags($result, $expected);
  6368. }
  6369. /**
  6370. * test image submit types.
  6371. *
  6372. * @return void
  6373. */
  6374. public function testSubmitImage() {
  6375. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6376. $expected = array(
  6377. 'div' => array('class' => 'submit'),
  6378. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6379. '/div'
  6380. );
  6381. $this->assertTags($result, $expected);
  6382. $result = $this->Form->submit('/relative/cake.power.gif');
  6383. $expected = array(
  6384. 'div' => array('class' => 'submit'),
  6385. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  6386. '/div'
  6387. );
  6388. $this->assertTags($result, $expected);
  6389. $result = $this->Form->submit('cake.power.gif');
  6390. $expected = array(
  6391. 'div' => array('class' => 'submit'),
  6392. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6393. '/div'
  6394. );
  6395. $this->assertTags($result, $expected);
  6396. $result = $this->Form->submit('Not.an.image');
  6397. $expected = array(
  6398. 'div' => array('class' => 'submit'),
  6399. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6400. '/div'
  6401. );
  6402. $this->assertTags($result, $expected);
  6403. $after = '--after--';
  6404. $before = '--before--';
  6405. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6406. $expected = array(
  6407. 'div' => array('class' => 'submit'),
  6408. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6409. '--after--',
  6410. '/div'
  6411. );
  6412. $this->assertTags($result, $expected);
  6413. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6414. $expected = array(
  6415. 'div' => array('class' => 'submit'),
  6416. '--before--',
  6417. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6418. '/div'
  6419. );
  6420. $this->assertTags($result, $expected);
  6421. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6422. $expected = array(
  6423. 'div' => array('class' => 'submit'),
  6424. '--before--',
  6425. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6426. '--after--',
  6427. '/div'
  6428. );
  6429. $this->assertTags($result, $expected);
  6430. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6431. $expected = array(
  6432. 'div' => array('class' => 'submit'),
  6433. '--before--',
  6434. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6435. '--after--',
  6436. '/div'
  6437. );
  6438. $this->assertTags($result, $expected);
  6439. }
  6440. /**
  6441. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6442. * be submitted at a time.
  6443. *
  6444. * @return void
  6445. */
  6446. public function testSubmitUnlockedByDefault() {
  6447. $this->Form->request->params['_Token']['key'] = 'secured';
  6448. $this->Form->submit('Go go');
  6449. $this->Form->submit('Save', array('name' => 'save'));
  6450. $result = $this->Form->unlockField();
  6451. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6452. }
  6453. /**
  6454. * Test submit image with timestamps.
  6455. *
  6456. * @return void
  6457. */
  6458. public function testSubmitImageTimestamp() {
  6459. Configure::write('Asset.timestamp', 'force');
  6460. $result = $this->Form->submit('cake.power.gif');
  6461. $expected = array(
  6462. 'div' => array('class' => 'submit'),
  6463. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6464. '/div'
  6465. );
  6466. $this->assertTags($result, $expected);
  6467. }
  6468. /**
  6469. * test the create() method
  6470. *
  6471. * @return void
  6472. */
  6473. public function testCreate() {
  6474. $result = $this->Form->create('Contact');
  6475. $encoding = strtolower(Configure::read('App.encoding'));
  6476. $expected = array(
  6477. 'form' => array(
  6478. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6479. 'accept-charset' => $encoding
  6480. ),
  6481. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6482. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6483. '/div'
  6484. );
  6485. $this->assertTags($result, $expected);
  6486. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6487. $expected = array('form' => array(
  6488. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6489. 'accept-charset' => $encoding
  6490. ));
  6491. $this->assertTags($result, $expected);
  6492. $result = $this->Form->create('Contact', array('type' => 'get'));
  6493. $expected = array('form' => array(
  6494. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6495. 'accept-charset' => $encoding
  6496. ));
  6497. $this->assertTags($result, $expected);
  6498. $result = $this->Form->create('Contact', array('type' => 'put'));
  6499. $expected = array(
  6500. 'form' => array(
  6501. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6502. 'accept-charset' => $encoding
  6503. ),
  6504. 'div' => array('style' => 'display:none;'),
  6505. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6506. '/div'
  6507. );
  6508. $this->assertTags($result, $expected);
  6509. $result = $this->Form->create('Contact', array('type' => 'file'));
  6510. $expected = array(
  6511. 'form' => array(
  6512. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6513. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6514. ),
  6515. 'div' => array('style' => 'display:none;'),
  6516. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  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');
  6524. $expected = array(
  6525. 'form' => array(
  6526. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6527. 'accept-charset' => $encoding
  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['Contact']['id'] = 1;
  6535. $this->Form->request->here = '/contacts/edit/1';
  6536. $this->Form->request['action'] = 'edit';
  6537. $result = $this->Form->create('Contact', array('type' => 'file'));
  6538. $expected = array(
  6539. 'form' => array(
  6540. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6541. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6542. ),
  6543. 'div' => array('style' => 'display:none;'),
  6544. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6545. '/div'
  6546. );
  6547. $this->assertTags($result, $expected);
  6548. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6549. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6550. $expected = array(
  6551. 'form' => array(
  6552. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6553. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  6554. ),
  6555. 'div' => array('style' => 'display:none;'),
  6556. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6557. '/div'
  6558. );
  6559. $this->assertTags($result, $expected);
  6560. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6561. $expected = array(
  6562. 'form' => array(
  6563. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6564. 'accept-charset' => $encoding
  6565. ),
  6566. 'div' => array('style' => 'display:none;'),
  6567. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6568. '/div'
  6569. );
  6570. $this->assertTags($result, $expected);
  6571. $this->Form->request['action'] = 'add';
  6572. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6573. $expected = array(
  6574. 'form' => array(
  6575. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6576. 'accept-charset' => $encoding
  6577. ),
  6578. 'div' => array('style' => 'display:none;'),
  6579. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6580. '/div'
  6581. );
  6582. $this->assertTags($result, $expected);
  6583. $result = $this->Form->create('User', array('action' => 'login'));
  6584. $expected = array(
  6585. 'form' => array(
  6586. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6587. 'accept-charset' => $encoding
  6588. ),
  6589. 'div' => array('style' => 'display:none;'),
  6590. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6591. '/div'
  6592. );
  6593. $this->assertTags($result, $expected);
  6594. $result = $this->Form->create('User', array('url' => '/users/login'));
  6595. $expected = array(
  6596. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6597. 'div' => array('style' => 'display:none;'),
  6598. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6599. '/div'
  6600. );
  6601. $this->assertTags($result, $expected);
  6602. $this->Form->request['controller'] = 'pages';
  6603. $result = $this->Form->create('User', array('action' => 'signup'));
  6604. $expected = array(
  6605. 'form' => array(
  6606. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6607. 'accept-charset' => $encoding
  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. $this->Form->request->data = array();
  6615. $this->Form->request['controller'] = 'contacts';
  6616. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6617. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6618. $expected = array(
  6619. 'form' => array(
  6620. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6621. 'accept-charset' => 'utf-8'
  6622. ),
  6623. 'div' => array('style' => 'display:none;'),
  6624. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6625. '/div'
  6626. );
  6627. $this->assertTags($result, $expected);
  6628. }
  6629. /**
  6630. * Test the onsubmit option for create()
  6631. *
  6632. * @return void
  6633. */
  6634. public function testCreateOnSubmit() {
  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('url' => array('action' => 'index', 'param'), 'default' => false));
  6639. $expected = array(
  6640. 'form' => array(
  6641. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6642. 'accept-charset' => 'utf-8'
  6643. ),
  6644. 'div' => array('style' => 'display:none;'),
  6645. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6646. '/div'
  6647. );
  6648. $this->assertTags($result, $expected);
  6649. $this->Form->request->data = array();
  6650. $this->Form->request['controller'] = 'contacts';
  6651. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6652. $result = $this->Form->create(array(
  6653. 'url' => array('action' => 'index', 'param'),
  6654. 'default' => false,
  6655. 'onsubmit' => 'someFunction();'
  6656. ));
  6657. $expected = array(
  6658. 'form' => array(
  6659. 'id' => 'ContactAddForm', 'method' => 'post',
  6660. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6661. 'action' => '/contacts/index/param',
  6662. 'accept-charset' => 'utf-8'
  6663. ),
  6664. 'div' => array('style' => 'display:none;'),
  6665. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6666. '/div'
  6667. );
  6668. $this->assertTags($result, $expected);
  6669. }
  6670. /**
  6671. * test create() with automatic url generation
  6672. *
  6673. * @return void
  6674. */
  6675. public function testCreateAutoUrl() {
  6676. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6677. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6678. $this->Form->request->base = '/base_url';
  6679. $result = $this->Form->create('Contact');
  6680. $expected = array(
  6681. 'form' => array(
  6682. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  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. $this->Form->request['action'] = 'delete';
  6691. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6692. $this->Form->request->base = '/base_url';
  6693. $result = $this->Form->create('Contact');
  6694. $expected = array(
  6695. 'form' => array(
  6696. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6697. 'accept-charset' => 'utf-8'
  6698. ),
  6699. 'div' => array('style' => 'display:none;'),
  6700. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6701. '/div'
  6702. );
  6703. $this->assertTags($result, $expected);
  6704. }
  6705. /**
  6706. * test create() with a custom route
  6707. *
  6708. * @return void
  6709. */
  6710. public function testCreateCustomRoute() {
  6711. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6712. $encoding = strtolower(Configure::read('App.encoding'));
  6713. $result = $this->Form->create('User', array('action' => 'login'));
  6714. $expected = array(
  6715. 'form' => array(
  6716. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6717. 'accept-charset' => $encoding
  6718. ),
  6719. 'div' => array('style' => 'display:none;'),
  6720. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6721. '/div'
  6722. );
  6723. $this->assertTags($result, $expected);
  6724. }
  6725. /**
  6726. * test that inputDefaults are stored and used.
  6727. *
  6728. * @return void
  6729. */
  6730. public function testCreateWithInputDefaults() {
  6731. $this->Form->create('User', array(
  6732. 'inputDefaults' => array(
  6733. 'div' => false,
  6734. 'label' => false,
  6735. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6736. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6737. )
  6738. ));
  6739. $result = $this->Form->input('username');
  6740. $expected = array(
  6741. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6742. );
  6743. $this->assertTags($result, $expected);
  6744. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6745. $expected = array(
  6746. 'div' => array('class' => 'input text'),
  6747. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6748. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6749. '/div'
  6750. );
  6751. $this->assertTags($result, $expected);
  6752. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6753. $expected = array(
  6754. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6755. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6756. );
  6757. $this->assertTags($result, $expected);
  6758. $this->Form->create('User', array(
  6759. 'inputDefaults' => array(
  6760. 'div' => false,
  6761. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6762. )
  6763. ));
  6764. $result = $this->Form->input('username', array('div' => true));
  6765. $expected = array(
  6766. 'div' => array('class' => 'input text'),
  6767. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6768. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6769. '/div'
  6770. );
  6771. $this->assertTags($result, $expected);
  6772. }
  6773. /**
  6774. * test automatic accept-charset overriding
  6775. *
  6776. * @return void
  6777. */
  6778. public function testCreateWithAcceptCharset() {
  6779. $result = $this->Form->create('UserForm', array(
  6780. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6781. )
  6782. );
  6783. $expected = array(
  6784. 'form' => array(
  6785. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6786. 'accept-charset' => 'iso-8859-1'
  6787. ),
  6788. 'div' => array('style' => 'display:none;'),
  6789. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6790. '/div'
  6791. );
  6792. $this->assertTags($result, $expected);
  6793. }
  6794. /**
  6795. * Test base form url when url param is passed with multiple parameters (&)
  6796. *
  6797. */
  6798. public function testCreateQuerystringrequest() {
  6799. $encoding = strtolower(Configure::read('App.encoding'));
  6800. $result = $this->Form->create('Contact', array(
  6801. 'type' => 'post',
  6802. 'escape' => false,
  6803. 'url' => array(
  6804. 'controller' => 'controller',
  6805. 'action' => 'action',
  6806. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6807. )
  6808. ));
  6809. $expected = array(
  6810. 'form' => array(
  6811. 'id' => 'ContactAddForm',
  6812. 'method' => 'post',
  6813. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6814. 'accept-charset' => $encoding
  6815. ),
  6816. 'div' => array('style' => 'display:none;'),
  6817. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6818. '/div'
  6819. );
  6820. $this->assertTags($result, $expected);
  6821. $result = $this->Form->create('Contact', array(
  6822. 'type' => 'post',
  6823. 'url' => array(
  6824. 'controller' => 'controller',
  6825. 'action' => 'action',
  6826. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6827. )
  6828. ));
  6829. $expected = array(
  6830. 'form' => array(
  6831. 'id' => 'ContactAddForm',
  6832. 'method' => 'post',
  6833. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6834. 'accept-charset' => $encoding
  6835. ),
  6836. 'div' => array('style' => 'display:none;'),
  6837. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6838. '/div'
  6839. );
  6840. $this->assertTags($result, $expected);
  6841. }
  6842. /**
  6843. * test that create() doesn't cause errors by multiple id's being in the primary key
  6844. * as could happen with multiple select or checkboxes.
  6845. *
  6846. * @return void
  6847. */
  6848. public function testCreateWithMultipleIdInData() {
  6849. $encoding = strtolower(Configure::read('App.encoding'));
  6850. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6851. $result = $this->Form->create('Contact');
  6852. $expected = array(
  6853. 'form' => array(
  6854. 'id' => 'ContactAddForm',
  6855. 'method' => 'post',
  6856. 'action' => '/contacts/add',
  6857. 'accept-charset' => $encoding
  6858. ),
  6859. 'div' => array('style' => 'display:none;'),
  6860. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6861. '/div'
  6862. );
  6863. $this->assertTags($result, $expected);
  6864. }
  6865. /**
  6866. * test that create() doesn't add in extra passed params.
  6867. *
  6868. * @return void
  6869. */
  6870. public function testCreatePassedArgs() {
  6871. $encoding = strtolower(Configure::read('App.encoding'));
  6872. $this->Form->request->data['Contact']['id'] = 1;
  6873. $result = $this->Form->create('Contact', array(
  6874. 'type' => 'post',
  6875. 'escape' => false,
  6876. 'url' => array(
  6877. 'action' => 'edit',
  6878. 'myparam'
  6879. )
  6880. ));
  6881. $expected = array(
  6882. 'form' => array(
  6883. 'id' => 'ContactAddForm',
  6884. 'method' => 'post',
  6885. 'action' => '/contacts/edit/myparam',
  6886. 'accept-charset' => $encoding
  6887. ),
  6888. 'div' => array('style' => 'display:none;'),
  6889. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6890. '/div'
  6891. );
  6892. $this->assertTags($result, $expected);
  6893. }
  6894. /**
  6895. * test creating a get form, and get form inputs.
  6896. *
  6897. * @return void
  6898. */
  6899. public function testGetFormCreate() {
  6900. $encoding = strtolower(Configure::read('App.encoding'));
  6901. $result = $this->Form->create('Contact', array('type' => 'get'));
  6902. $this->assertTags($result, array('form' => array(
  6903. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6904. 'accept-charset' => $encoding
  6905. )));
  6906. $result = $this->Form->text('Contact.name');
  6907. $this->assertTags($result, array('input' => array(
  6908. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6909. )));
  6910. $result = $this->Form->password('password');
  6911. $this->assertTags($result, array('input' => array(
  6912. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6913. )));
  6914. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6915. $result = $this->Form->text('user_form');
  6916. $this->assertTags($result, array('input' => array(
  6917. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6918. )));
  6919. }
  6920. /**
  6921. * test get form, and inputs when the model param is false
  6922. *
  6923. * @return void
  6924. */
  6925. public function testGetFormWithFalseModel() {
  6926. $encoding = strtolower(Configure::read('App.encoding'));
  6927. $this->Form->request['controller'] = 'contact_test';
  6928. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6929. $expected = array('form' => array(
  6930. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6931. 'accept-charset' => $encoding
  6932. ));
  6933. $this->assertTags($result, $expected);
  6934. $result = $this->Form->text('reason');
  6935. $expected = array(
  6936. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6937. );
  6938. $this->assertTags($result, $expected);
  6939. }
  6940. /**
  6941. * test that datetime() works with GET style forms.
  6942. *
  6943. * @return void
  6944. */
  6945. public function testDateTimeWithGetForms() {
  6946. extract($this->dateRegex);
  6947. $this->Form->create('Contact', array('type' => 'get'));
  6948. $result = $this->Form->datetime('created');
  6949. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6950. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6951. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6952. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6953. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6954. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6955. }
  6956. /**
  6957. * testEditFormWithData method
  6958. *
  6959. * test auto populating form elements from submitted data.
  6960. *
  6961. * @return void
  6962. */
  6963. public function testEditFormWithData() {
  6964. $this->Form->request->data = array('Person' => array(
  6965. 'id' => 1,
  6966. 'first_name' => 'Nate',
  6967. 'last_name' => 'Abele',
  6968. 'email' => 'nate@example.com'
  6969. ));
  6970. $this->Form->request->addParams(array(
  6971. 'models' => array('Person'),
  6972. 'controller' => 'people',
  6973. 'action' => 'add'
  6974. ));
  6975. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6976. $this->Form->create();
  6977. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6978. $expected = array(
  6979. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6980. 'select' => array(
  6981. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6982. ),
  6983. array('option' => array('value' => 1)), 'Nate', '/option',
  6984. array('option' => array('value' => 2)), 'Garrett', '/option',
  6985. array('option' => array('value' => 3)), 'Larry', '/option',
  6986. '/select'
  6987. );
  6988. $this->assertTags($result, $expected);
  6989. }
  6990. /**
  6991. * Test that required fields are created for various types of validation.
  6992. *
  6993. * @return void
  6994. */
  6995. public function testFormInputRequiredDetection() {
  6996. $this->Form->create('Contact');
  6997. $result = $this->Form->input('Contact.non_existing');
  6998. $expected = array(
  6999. 'div' => array('class' => 'input text'),
  7000. 'label' => array('for' => 'ContactNonExisting'),
  7001. 'Non Existing',
  7002. '/label',
  7003. 'input' => array(
  7004. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7005. 'id' => 'ContactNonExisting'
  7006. ),
  7007. '/div'
  7008. );
  7009. $this->assertTags($result, $expected);
  7010. $result = $this->Form->input('Contact.imrequired');
  7011. $expected = array(
  7012. 'div' => array('class' => 'input text required'),
  7013. 'label' => array('for' => 'ContactImrequired'),
  7014. 'Imrequired',
  7015. '/label',
  7016. 'input' => array(
  7017. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7018. 'id' => 'ContactImrequired',
  7019. 'required' => 'required'
  7020. ),
  7021. '/div'
  7022. );
  7023. $this->assertTags($result, $expected);
  7024. $result = $this->Form->input('Contact.imalsorequired');
  7025. $expected = array(
  7026. 'div' => array('class' => 'input text required'),
  7027. 'label' => array('for' => 'ContactImalsorequired'),
  7028. 'Imalsorequired',
  7029. '/label',
  7030. 'input' => array(
  7031. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7032. 'id' => 'ContactImalsorequired',
  7033. 'required' => 'required'
  7034. ),
  7035. '/div'
  7036. );
  7037. $this->assertTags($result, $expected);
  7038. $result = $this->Form->input('Contact.imrequiredtoo');
  7039. $expected = array(
  7040. 'div' => array('class' => 'input text required'),
  7041. 'label' => array('for' => 'ContactImrequiredtoo'),
  7042. 'Imrequiredtoo',
  7043. '/label',
  7044. 'input' => array(
  7045. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7046. 'id' => 'ContactImrequiredtoo',
  7047. 'required' => 'required'
  7048. ),
  7049. '/div'
  7050. );
  7051. $this->assertTags($result, $expected);
  7052. $result = $this->Form->input('Contact.required_one');
  7053. $expected = array(
  7054. 'div' => array('class' => 'input text required'),
  7055. 'label' => array('for' => 'ContactRequiredOne'),
  7056. 'Required One',
  7057. '/label',
  7058. 'input' => array(
  7059. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7060. 'id' => 'ContactRequiredOne',
  7061. 'required' => 'required'
  7062. ),
  7063. '/div'
  7064. );
  7065. $this->assertTags($result, $expected);
  7066. $result = $this->Form->input('Contact.string_required');
  7067. $expected = array(
  7068. 'div' => array('class' => 'input text required'),
  7069. 'label' => array('for' => 'ContactStringRequired'),
  7070. 'String Required',
  7071. '/label',
  7072. 'input' => array(
  7073. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7074. 'id' => 'ContactStringRequired',
  7075. 'required' => 'required'
  7076. ),
  7077. '/div'
  7078. );
  7079. $this->assertTags($result, $expected);
  7080. $result = $this->Form->input('Contact.imnotrequired');
  7081. $expected = array(
  7082. 'div' => array('class' => 'input text'),
  7083. 'label' => array('for' => 'ContactImnotrequired'),
  7084. 'Imnotrequired',
  7085. '/label',
  7086. 'input' => array(
  7087. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7088. 'id' => 'ContactImnotrequired'
  7089. ),
  7090. '/div'
  7091. );
  7092. $this->assertTags($result, $expected);
  7093. $result = $this->Form->input('Contact.imalsonotrequired');
  7094. $expected = array(
  7095. 'div' => array('class' => 'input text'),
  7096. 'label' => array('for' => 'ContactImalsonotrequired'),
  7097. 'Imalsonotrequired',
  7098. '/label',
  7099. 'input' => array(
  7100. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7101. 'id' => 'ContactImalsonotrequired'
  7102. ),
  7103. '/div'
  7104. );
  7105. $this->assertTags($result, $expected);
  7106. $result = $this->Form->input('Contact.imalsonotrequired2');
  7107. $expected = array(
  7108. 'div' => array('class' => 'input text'),
  7109. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7110. 'Imalsonotrequired2',
  7111. '/label',
  7112. 'input' => array(
  7113. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7114. 'id' => 'ContactImalsonotrequired2'
  7115. ),
  7116. '/div'
  7117. );
  7118. $this->assertTags($result, $expected);
  7119. $result = $this->Form->input('Contact.imnotrequiredeither');
  7120. $expected = array(
  7121. 'div' => array('class' => 'input text'),
  7122. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7123. 'Imnotrequiredeither',
  7124. '/label',
  7125. 'input' => array(
  7126. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7127. 'id' => 'ContactImnotrequiredeither'
  7128. ),
  7129. '/div'
  7130. );
  7131. $this->assertTags($result, $expected);
  7132. $result = $this->Form->input('Contact.iamrequiredalways');
  7133. $expected = array(
  7134. 'div' => array('class' => 'input text required'),
  7135. 'label' => array('for' => 'ContactIamrequiredalways'),
  7136. 'Iamrequiredalways',
  7137. '/label',
  7138. 'input' => array(
  7139. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7140. 'id' => 'ContactIamrequiredalways',
  7141. 'required' => 'required'
  7142. ),
  7143. '/div'
  7144. );
  7145. $this->assertTags($result, $expected);
  7146. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7147. $expected = array(
  7148. 'div' => array('class' => 'input checkbox required'),
  7149. array('input' => array(
  7150. 'type' => 'hidden',
  7151. 'name' => 'data[Contact][boolean_field]',
  7152. 'id' => 'ContactBooleanField_',
  7153. 'value' => '0'
  7154. )),
  7155. array('input' => array(
  7156. 'type' => 'checkbox',
  7157. 'name' => 'data[Contact][boolean_field]',
  7158. 'value' => '1',
  7159. 'id' => 'ContactBooleanField'
  7160. )),
  7161. 'label' => array('for' => 'ContactBooleanField'),
  7162. 'Boolean Field',
  7163. '/label',
  7164. '/div'
  7165. );
  7166. $this->assertTags($result, $expected);
  7167. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7168. $expected = array(
  7169. 'div' => array('class' => 'input checkbox required'),
  7170. array('input' => array(
  7171. 'type' => 'hidden',
  7172. 'name' => 'data[Contact][boolean_field]',
  7173. 'id' => 'ContactBooleanField_',
  7174. 'value' => '0'
  7175. )),
  7176. array('input' => array(
  7177. 'type' => 'checkbox',
  7178. 'name' => 'data[Contact][boolean_field]',
  7179. 'value' => '1',
  7180. 'id' => 'ContactBooleanField',
  7181. 'required' => 'required'
  7182. )),
  7183. 'label' => array('for' => 'ContactBooleanField'),
  7184. 'Boolean Field',
  7185. '/label',
  7186. '/div'
  7187. );
  7188. $this->assertTags($result, $expected);
  7189. }
  7190. /**
  7191. * testFormMagicInput method
  7192. *
  7193. * @return void
  7194. */
  7195. public function testFormMagicInput() {
  7196. $encoding = strtolower(Configure::read('App.encoding'));
  7197. $result = $this->Form->create('Contact');
  7198. $expected = array(
  7199. 'form' => array(
  7200. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7201. 'accept-charset' => $encoding
  7202. ),
  7203. 'div' => array('style' => 'display:none;'),
  7204. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7205. '/div'
  7206. );
  7207. $this->assertTags($result, $expected);
  7208. $result = $this->Form->input('name');
  7209. $expected = array(
  7210. 'div' => array('class' => 'input text'),
  7211. 'label' => array('for' => 'ContactName'),
  7212. 'Name',
  7213. '/label',
  7214. 'input' => array(
  7215. 'type' => 'text', 'name' => 'data[Contact][name]',
  7216. 'id' => 'ContactName', 'maxlength' => '255'
  7217. ),
  7218. '/div'
  7219. );
  7220. $this->assertTags($result, $expected);
  7221. $result = $this->Form->input('non_existing_field_in_contact_model');
  7222. $expected = array(
  7223. 'div' => array('class' => 'input text'),
  7224. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7225. 'Non Existing Field In Contact Model',
  7226. '/label',
  7227. 'input' => array(
  7228. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  7229. 'id' => 'ContactNonExistingFieldInContactModel'
  7230. ),
  7231. '/div'
  7232. );
  7233. $this->assertTags($result, $expected);
  7234. $result = $this->Form->input('Address.street');
  7235. $expected = array(
  7236. 'div' => array('class' => 'input text'),
  7237. 'label' => array('for' => 'AddressStreet'),
  7238. 'Street',
  7239. '/label',
  7240. 'input' => array(
  7241. 'type' => 'text', 'name' => 'data[Address][street]',
  7242. 'id' => 'AddressStreet'
  7243. ),
  7244. '/div'
  7245. );
  7246. $this->assertTags($result, $expected);
  7247. $result = $this->Form->input('Address.non_existing_field_in_model');
  7248. $expected = array(
  7249. 'div' => array('class' => 'input text'),
  7250. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7251. 'Non Existing Field In Model',
  7252. '/label',
  7253. 'input' => array(
  7254. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  7255. 'id' => 'AddressNonExistingFieldInModel'
  7256. ),
  7257. '/div'
  7258. );
  7259. $this->assertTags($result, $expected);
  7260. $result = $this->Form->input('name', array('div' => false));
  7261. $expected = array(
  7262. 'label' => array('for' => 'ContactName'),
  7263. 'Name',
  7264. '/label',
  7265. 'input' => array(
  7266. 'type' => 'text', 'name' => 'data[Contact][name]',
  7267. 'id' => 'ContactName', 'maxlength' => '255'
  7268. )
  7269. );
  7270. $this->assertTags($result, $expected);
  7271. extract($this->dateRegex);
  7272. $now = strtotime('now');
  7273. $result = $this->Form->input('Contact.published', array('div' => false));
  7274. $expected = array(
  7275. 'label' => array('for' => 'ContactPublishedMonth'),
  7276. 'Published',
  7277. '/label',
  7278. array('select' => array(
  7279. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  7280. )),
  7281. $monthsRegex,
  7282. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7283. date('F', $now),
  7284. '/option',
  7285. '*/select',
  7286. '-',
  7287. array('select' => array(
  7288. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  7289. )),
  7290. $daysRegex,
  7291. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7292. date('j', $now),
  7293. '/option',
  7294. '*/select',
  7295. '-',
  7296. array('select' => array(
  7297. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  7298. )),
  7299. $yearsRegex,
  7300. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7301. date('Y', $now),
  7302. '*/select'
  7303. );
  7304. $this->assertTags($result, $expected);
  7305. $result = $this->Form->input('Contact.updated', array('div' => false));
  7306. $expected = array(
  7307. 'label' => array('for' => 'ContactUpdatedMonth'),
  7308. 'Updated',
  7309. '/label',
  7310. array('select' => array(
  7311. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  7312. )),
  7313. $monthsRegex,
  7314. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7315. date('F', $now),
  7316. '/option',
  7317. '*/select',
  7318. '-',
  7319. array('select' => array(
  7320. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  7321. )),
  7322. $daysRegex,
  7323. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7324. date('j', $now),
  7325. '/option',
  7326. '*/select',
  7327. '-',
  7328. array('select' => array(
  7329. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  7330. )),
  7331. $yearsRegex,
  7332. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7333. date('Y', $now),
  7334. '*/select'
  7335. );
  7336. $this->assertTags($result, $expected);
  7337. $result = $this->Form->input('UserForm.stuff');
  7338. $expected = array(
  7339. 'div' => array('class' => 'input text'),
  7340. 'label' => array('for' => 'UserFormStuff'),
  7341. 'Stuff',
  7342. '/label',
  7343. 'input' => array(
  7344. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  7345. 'id' => 'UserFormStuff', 'maxlength' => 10
  7346. ),
  7347. '/div'
  7348. );
  7349. $this->assertTags($result, $expected);
  7350. }
  7351. /**
  7352. * testForMagicInputNonExistingNorValidated method
  7353. *
  7354. * @return void
  7355. */
  7356. public function testForMagicInputNonExistingNorValidated() {
  7357. $encoding = strtolower(Configure::read('App.encoding'));
  7358. $result = $this->Form->create('Contact');
  7359. $expected = array(
  7360. 'form' => array(
  7361. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7362. 'accept-charset' => $encoding
  7363. ),
  7364. 'div' => array('style' => 'display:none;'),
  7365. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7366. '/div'
  7367. );
  7368. $this->assertTags($result, $expected);
  7369. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7370. $expected = array(
  7371. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7372. 'Non Existing Nor Validated',
  7373. '/label',
  7374. 'input' => array(
  7375. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7376. 'id' => 'ContactNonExistingNorValidated'
  7377. )
  7378. );
  7379. $this->assertTags($result, $expected);
  7380. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  7381. 'div' => false, 'value' => 'my value'
  7382. ));
  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' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  7390. )
  7391. );
  7392. $this->assertTags($result, $expected);
  7393. $this->Form->request->data = array(
  7394. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  7395. ));
  7396. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7397. $expected = array(
  7398. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7399. 'Non Existing Nor Validated',
  7400. '/label',
  7401. 'input' => array(
  7402. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7403. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  7404. )
  7405. );
  7406. $this->assertTags($result, $expected);
  7407. }
  7408. /**
  7409. * testFormMagicInputLabel method
  7410. *
  7411. * @return void
  7412. */
  7413. public function testFormMagicInputLabel() {
  7414. $encoding = strtolower(Configure::read('App.encoding'));
  7415. $result = $this->Form->create('Contact');
  7416. $expected = array(
  7417. 'form' => array(
  7418. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7419. 'accept-charset' => $encoding
  7420. ),
  7421. 'div' => array('style' => 'display:none;'),
  7422. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7423. '/div'
  7424. );
  7425. $this->assertTags($result, $expected);
  7426. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  7427. $this->assertTags($result, array('input' => array(
  7428. 'name' => 'data[Contact][name]', 'type' => 'text',
  7429. 'id' => 'ContactName', 'maxlength' => '255')
  7430. ));
  7431. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  7432. $expected = array(
  7433. 'label' => array('for' => 'ContactName'),
  7434. 'My label',
  7435. '/label',
  7436. 'input' => array(
  7437. 'type' => 'text', 'name' => 'data[Contact][name]',
  7438. 'id' => 'ContactName', 'maxlength' => '255'
  7439. )
  7440. );
  7441. $this->assertTags($result, $expected);
  7442. $result = $this->Form->input('Contact.name', array(
  7443. 'div' => false, 'label' => array('class' => 'mandatory')
  7444. ));
  7445. $expected = array(
  7446. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7447. 'Name',
  7448. '/label',
  7449. 'input' => array(
  7450. 'type' => 'text', 'name' => 'data[Contact][name]',
  7451. 'id' => 'ContactName', 'maxlength' => '255'
  7452. )
  7453. );
  7454. $this->assertTags($result, $expected);
  7455. $result = $this->Form->input('Contact.name', array(
  7456. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7457. ));
  7458. $expected = array(
  7459. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7460. 'My label',
  7461. '/label',
  7462. 'input' => array(
  7463. 'type' => 'text', 'name' => 'data[Contact][name]',
  7464. 'id' => 'ContactName', 'maxlength' => '255'
  7465. )
  7466. );
  7467. $this->assertTags($result, $expected);
  7468. $result = $this->Form->input('Contact.name', array(
  7469. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7470. ));
  7471. $expected = array(
  7472. 'label' => array('for' => 'my_id'),
  7473. 'Name',
  7474. '/label',
  7475. 'input' => array(
  7476. 'type' => 'text', 'name' => 'data[Contact][name]',
  7477. 'id' => 'my_id', 'maxlength' => '255'
  7478. )
  7479. );
  7480. $this->assertTags($result, $expected);
  7481. $result = $this->Form->input('1.id');
  7482. $this->assertTags($result, array('input' => array(
  7483. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7484. 'id' => 'Contact1Id'
  7485. )));
  7486. $result = $this->Form->input("1.name");
  7487. $expected = array(
  7488. 'div' => array('class' => 'input text'),
  7489. 'label' => array('for' => 'Contact1Name'),
  7490. 'Name',
  7491. '/label',
  7492. 'input' => array(
  7493. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  7494. 'id' => 'Contact1Name', 'maxlength' => '255'
  7495. ),
  7496. '/div'
  7497. );
  7498. $this->assertTags($result, $expected);
  7499. $result = $this->Form->input('Contact.1.id');
  7500. $this->assertTags($result, array(
  7501. 'input' => array(
  7502. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7503. 'id' => 'Contact1Id'
  7504. )
  7505. ));
  7506. $result = $this->Form->input("Model.1.name");
  7507. $expected = array(
  7508. 'div' => array('class' => 'input text'),
  7509. 'label' => array('for' => 'Model1Name'),
  7510. 'Name',
  7511. '/label',
  7512. 'input' => array(
  7513. 'type' => 'text', 'name' => 'data[Model][1][name]',
  7514. 'id' => 'Model1Name'
  7515. ),
  7516. '/div'
  7517. );
  7518. $this->assertTags($result, $expected);
  7519. }
  7520. /**
  7521. * testFormEnd method
  7522. *
  7523. * @return void
  7524. */
  7525. public function testFormEnd() {
  7526. $this->assertEquals('</form>', $this->Form->end());
  7527. $result = $this->Form->end('');
  7528. $expected = array(
  7529. 'div' => array('class' => 'submit'),
  7530. 'input' => array('type' => 'submit', 'value' => ''),
  7531. '/div',
  7532. '/form'
  7533. );
  7534. $this->assertTags($result, $expected);
  7535. $result = $this->Form->end(array('label' => ''));
  7536. $expected = array(
  7537. 'div' => array('class' => 'submit'),
  7538. 'input' => array('type' => 'submit', 'value' => ''),
  7539. '/div',
  7540. '/form'
  7541. );
  7542. $this->assertTags($result, $expected);
  7543. $result = $this->Form->end('save');
  7544. $expected = array(
  7545. 'div' => array('class' => 'submit'),
  7546. 'input' => array('type' => 'submit', 'value' => 'save'),
  7547. '/div',
  7548. '/form'
  7549. );
  7550. $this->assertTags($result, $expected);
  7551. $result = $this->Form->end(array('label' => 'save'));
  7552. $expected = array(
  7553. 'div' => array('class' => 'submit'),
  7554. 'input' => array('type' => 'submit', 'value' => 'save'),
  7555. '/div',
  7556. '/form'
  7557. );
  7558. $this->assertTags($result, $expected);
  7559. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7560. $expected = array(
  7561. 'div' => array('class' => 'submit'),
  7562. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7563. '/div',
  7564. '/form'
  7565. );
  7566. $this->assertTags($result, $expected);
  7567. $result = $this->Form->end(array('name' => 'Whatever'));
  7568. $expected = array(
  7569. 'div' => array('class' => 'submit'),
  7570. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7571. '/div',
  7572. '/form'
  7573. );
  7574. $this->assertTags($result, $expected);
  7575. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7576. $expected = array(
  7577. 'div' => array('class' => 'good'),
  7578. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7579. '/div',
  7580. '/form'
  7581. );
  7582. $this->assertTags($result, $expected);
  7583. $result = $this->Form->end(array(
  7584. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7585. ));
  7586. $expected = array(
  7587. 'div' => array('class' => 'good'),
  7588. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7589. '/div',
  7590. '/form'
  7591. );
  7592. $this->assertTags($result, $expected);
  7593. }
  7594. /**
  7595. * testMultipleFormWithIdFields method
  7596. *
  7597. * @return void
  7598. */
  7599. public function testMultipleFormWithIdFields() {
  7600. $this->Form->create('UserForm');
  7601. $result = $this->Form->input('id');
  7602. $this->assertTags($result, array('input' => array(
  7603. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7604. )));
  7605. $result = $this->Form->input('ValidateItem.id');
  7606. $this->assertTags($result, array('input' => array(
  7607. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7608. 'id' => 'ValidateItemId'
  7609. )));
  7610. $result = $this->Form->input('ValidateUser.id');
  7611. $this->assertTags($result, array('input' => array(
  7612. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7613. 'id' => 'ValidateUserId'
  7614. )));
  7615. }
  7616. /**
  7617. * testDbLessModel method
  7618. *
  7619. * @return void
  7620. */
  7621. public function testDbLessModel() {
  7622. $this->Form->create('TestMail');
  7623. $result = $this->Form->input('name');
  7624. $expected = array(
  7625. 'div' => array('class' => 'input text'),
  7626. 'label' => array('for' => 'TestMailName'),
  7627. 'Name',
  7628. '/label',
  7629. 'input' => array(
  7630. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7631. 'id' => 'TestMailName'
  7632. ),
  7633. '/div'
  7634. );
  7635. $this->assertTags($result, $expected);
  7636. ClassRegistry::init('TestMail');
  7637. $this->Form->create('TestMail');
  7638. $result = $this->Form->input('name');
  7639. $expected = array(
  7640. 'div' => array('class' => 'input text'),
  7641. 'label' => array('for' => 'TestMailName'),
  7642. 'Name',
  7643. '/label',
  7644. 'input' => array(
  7645. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7646. 'id' => 'TestMailName'
  7647. ),
  7648. '/div'
  7649. );
  7650. $this->assertTags($result, $expected);
  7651. }
  7652. /**
  7653. * testBrokenness method
  7654. *
  7655. * @return void
  7656. */
  7657. public function testBrokenness() {
  7658. /*
  7659. * #4 This test has two parents and four children. By default (as of r7117) both
  7660. * parents are show but the first parent is missing a child. This is the inconsistency
  7661. * in the default behaviour - one parent has all children, the other does not - dependent
  7662. * on the data values.
  7663. */
  7664. $result = $this->Form->select('Model.field', array(
  7665. 'Fred' => array(
  7666. 'freds_son_1' => 'Fred',
  7667. 'freds_son_2' => 'Freddie'
  7668. ),
  7669. 'Bert' => array(
  7670. 'berts_son_1' => 'Albert',
  7671. 'berts_son_2' => 'Bertie')
  7672. ),
  7673. array('showParents' => true, 'empty' => false)
  7674. );
  7675. $expected = array(
  7676. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7677. array('optgroup' => array('label' => 'Fred')),
  7678. array('option' => array('value' => 'freds_son_1')),
  7679. 'Fred',
  7680. '/option',
  7681. array('option' => array('value' => 'freds_son_2')),
  7682. 'Freddie',
  7683. '/option',
  7684. '/optgroup',
  7685. array('optgroup' => array('label' => 'Bert')),
  7686. array('option' => array('value' => 'berts_son_1')),
  7687. 'Albert',
  7688. '/option',
  7689. array('option' => array('value' => 'berts_son_2')),
  7690. 'Bertie',
  7691. '/option',
  7692. '/optgroup',
  7693. '/select'
  7694. );
  7695. $this->assertTags($result, $expected);
  7696. /*
  7697. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7698. * changed, so we should expect the same select list data, just with a different name
  7699. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7700. * This is where data corruption can occur, because when a select value is missing from
  7701. * a list a form will substitute the first value in the list - without the user knowing.
  7702. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7703. * affect the availability of 3 => 'Three' as a valid option.
  7704. */
  7705. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7706. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7707. ));
  7708. $result = $this->Form->select(
  7709. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7710. );
  7711. $expected = array(
  7712. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7713. array('option' => array('value' => 1)),
  7714. 'One',
  7715. '/option',
  7716. array('option' => array('value' => 2)),
  7717. 'Two',
  7718. '/option',
  7719. array('optgroup' => array('label' => 'Three')),
  7720. array('option' => array('value' => 3)),
  7721. 'Three',
  7722. '/option',
  7723. array('option' => array('value' => 4)),
  7724. 'Four',
  7725. '/option',
  7726. array('option' => array('value' => 5)),
  7727. 'Five',
  7728. '/option',
  7729. '/optgroup',
  7730. '/select'
  7731. );
  7732. $this->assertTags($result, $expected);
  7733. }
  7734. /**
  7735. * Test the generation of fields for a multi record form.
  7736. *
  7737. * @return void
  7738. */
  7739. public function testMultiRecordForm() {
  7740. $this->Form->create('ValidateProfile');
  7741. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7742. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7743. $expected = array(
  7744. 'div' => array('class' => 'input textarea'),
  7745. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7746. 'Name',
  7747. '/label',
  7748. 'textarea' => array(
  7749. 'id' => 'ValidateProfile1ValidateItem2Name',
  7750. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7751. 'cols' => 30,
  7752. 'rows' => 6
  7753. ),
  7754. 'Value',
  7755. '/textarea',
  7756. '/div'
  7757. );
  7758. $this->assertTags($result, $expected);
  7759. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  7760. $expected = array(
  7761. 'div' => array('class' => 'input date'),
  7762. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7763. 'Created',
  7764. '/label',
  7765. array('select' => array(
  7766. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7767. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7768. )
  7769. ),
  7770. array('option' => array('value' => '')), '/option',
  7771. $this->dateRegex['monthsRegex'],
  7772. '/select', '-',
  7773. array('select' => array(
  7774. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7775. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7776. )
  7777. ),
  7778. array('option' => array('value' => '')), '/option',
  7779. $this->dateRegex['daysRegex'],
  7780. '/select', '-',
  7781. array('select' => array(
  7782. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7783. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7784. )
  7785. ),
  7786. array('option' => array('value' => '')), '/option',
  7787. $this->dateRegex['yearsRegex'],
  7788. '/select',
  7789. '/div'
  7790. );
  7791. $this->assertTags($result, $expected);
  7792. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7793. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7794. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7795. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7796. $expected = array(
  7797. 'div' => array('class' => 'input select error'),
  7798. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7799. 'Profile',
  7800. '/label',
  7801. 'select' => array(
  7802. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7803. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7804. 'class' => 'form-error'
  7805. ),
  7806. '/select',
  7807. array('div' => array('class' => 'error-message')),
  7808. 'Error',
  7809. '/div',
  7810. '/div'
  7811. );
  7812. $this->assertTags($result, $expected);
  7813. }
  7814. /**
  7815. * test the correct display of multi-record form validation errors.
  7816. *
  7817. * @return void
  7818. */
  7819. public function testMultiRecordFormValidationErrors() {
  7820. $this->Form->create('ValidateProfile');
  7821. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7822. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7823. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7824. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7825. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7826. $result = $this->Form->error('ValidateProfile.2.city');
  7827. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7828. $result = $this->Form->error('2.city');
  7829. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7830. }
  7831. /**
  7832. * test the correct display of multi-record form validation errors.
  7833. *
  7834. * @return void
  7835. */
  7836. public function testSaveManyRecordFormValidationErrors() {
  7837. $this->Form->create('ValidateUser');
  7838. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  7839. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  7840. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  7841. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7842. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  7843. $result = $this->Form->error('ValidateUser.0.city');
  7844. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7845. }
  7846. /**
  7847. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7848. *
  7849. * @return void
  7850. */
  7851. public function testInputTemplate() {
  7852. $result = $this->Form->input('Contact.email', array(
  7853. 'type' => 'text', 'format' => array('input')
  7854. ));
  7855. $expected = array(
  7856. 'div' => array('class' => 'input text'),
  7857. 'input' => array(
  7858. 'type' => 'text', 'name' => 'data[Contact][email]',
  7859. 'id' => 'ContactEmail'
  7860. ),
  7861. '/div'
  7862. );
  7863. $this->assertTags($result, $expected);
  7864. $result = $this->Form->input('Contact.email', array(
  7865. 'type' => 'text', 'format' => array('input', 'label'),
  7866. 'label' => '<em>Email (required)</em>'
  7867. ));
  7868. $expected = array(
  7869. 'div' => array('class' => 'input text'),
  7870. array('input' => array(
  7871. 'type' => 'text', 'name' => 'data[Contact][email]',
  7872. 'id' => 'ContactEmail'
  7873. )),
  7874. 'label' => array('for' => 'ContactEmail'),
  7875. 'em' => array(),
  7876. 'Email (required)',
  7877. '/em',
  7878. '/label',
  7879. '/div'
  7880. );
  7881. $this->assertTags($result, $expected);
  7882. $result = $this->Form->input('Contact.email', array(
  7883. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7884. 'between' => '<div>Something in the middle</div>',
  7885. 'after' => '<span>Some text at the end</span>'
  7886. ));
  7887. $expected = array(
  7888. 'div' => array('class' => 'input text'),
  7889. array('input' => array(
  7890. 'type' => 'text', 'name' => 'data[Contact][email]',
  7891. 'id' => 'ContactEmail'
  7892. )),
  7893. array('div' => array()),
  7894. 'Something in the middle',
  7895. '/div',
  7896. 'label' => array('for' => 'ContactEmail'),
  7897. 'Email',
  7898. '/label',
  7899. 'span' => array(),
  7900. 'Some text at the end',
  7901. '/span',
  7902. '/div'
  7903. );
  7904. $this->assertTags($result, $expected);
  7905. $result = $this->Form->input('Contact.method', array(
  7906. 'type' => 'radio',
  7907. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7908. 'between' => 'I am between',
  7909. ));
  7910. $expected = array(
  7911. 'div' => array('class' => 'input radio'),
  7912. 'fieldset' => array(),
  7913. 'legend' => array(),
  7914. 'Method',
  7915. '/legend',
  7916. 'I am between',
  7917. 'input' => array(
  7918. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7919. 'value' => '', 'id' => 'ContactMethod_'
  7920. ),
  7921. array('input' => array(
  7922. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7923. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7924. )),
  7925. array('label' => array('for' => 'ContactMethodEmail')),
  7926. 'Email',
  7927. '/label',
  7928. array('input' => array(
  7929. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7930. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7931. )),
  7932. array('label' => array('for' => 'ContactMethodPigeon')),
  7933. 'Pigeon',
  7934. '/label',
  7935. '/fieldset',
  7936. '/div',
  7937. );
  7938. $this->assertTags($result, $expected);
  7939. }
  7940. /**
  7941. * test that some html5 inputs + FormHelper::__call() work
  7942. *
  7943. * @return void
  7944. */
  7945. public function testHtml5Inputs() {
  7946. $result = $this->Form->email('User.email');
  7947. $expected = array(
  7948. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7949. );
  7950. $this->assertTags($result, $expected);
  7951. $result = $this->Form->search('User.query');
  7952. $expected = array(
  7953. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7954. );
  7955. $this->assertTags($result, $expected);
  7956. $result = $this->Form->search('User.query', array('value' => 'test'));
  7957. $expected = array(
  7958. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7959. );
  7960. $this->assertTags($result, $expected);
  7961. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7962. $expected = array(
  7963. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7964. );
  7965. $this->assertTags($result, $expected);
  7966. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7967. $expected = array(
  7968. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7969. );
  7970. $this->assertTags($result, $expected);
  7971. }
  7972. /**
  7973. *
  7974. * @expectedException CakeException
  7975. * @return void
  7976. */
  7977. public function testHtml5InputException() {
  7978. $this->Form->email();
  7979. }
  7980. /**
  7981. * Tests that a model can be loaded from the model names passed in the request object
  7982. *
  7983. * @return void
  7984. */
  7985. public function testIntrospectModelFromRequest() {
  7986. $this->loadFixtures('Post');
  7987. App::build(array(
  7988. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7989. ));
  7990. CakePlugin::load('TestPlugin');
  7991. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7992. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7993. $this->Form->create('TestPluginPost');
  7994. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7995. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7996. CakePlugin::unload();
  7997. App::build();
  7998. }
  7999. /**
  8000. * Tests that it is possible to set the validation errors directly in the helper for a field
  8001. *
  8002. * @return void
  8003. */
  8004. public function testCustomValidationErrors() {
  8005. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8006. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8007. $this->assertEquals('Badness!', $result);
  8008. }
  8009. /**
  8010. * Tests that the 'on' key validates as expected on create
  8011. *
  8012. * @return void
  8013. */
  8014. public function testRequiredOnCreate() {
  8015. $this->Form->create('Contact');
  8016. $result = $this->Form->input('Contact.imrequiredonupdate');
  8017. $expected = array(
  8018. 'div' => array('class' => 'input text'),
  8019. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8020. 'Imrequiredonupdate',
  8021. '/label',
  8022. 'input' => array(
  8023. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8024. 'id' => 'ContactImrequiredonupdate'
  8025. ),
  8026. '/div'
  8027. );
  8028. $this->assertTags($result, $expected);
  8029. $result = $this->Form->input('Contact.imrequiredoncreate');
  8030. $expected = array(
  8031. 'div' => array('class' => 'input text required'),
  8032. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8033. 'Imrequiredoncreate',
  8034. '/label',
  8035. 'input' => array(
  8036. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8037. 'id' => 'ContactImrequiredoncreate',
  8038. 'required' => 'required'
  8039. ),
  8040. '/div'
  8041. );
  8042. $this->assertTags($result, $expected);
  8043. $result = $this->Form->input('Contact.imrequiredonboth');
  8044. $expected = array(
  8045. 'div' => array('class' => 'input text required'),
  8046. 'label' => array('for' => 'ContactImrequiredonboth'),
  8047. 'Imrequiredonboth',
  8048. '/label',
  8049. 'input' => array(
  8050. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8051. 'id' => 'ContactImrequiredonboth',
  8052. 'required' => 'required'
  8053. ),
  8054. '/div'
  8055. );
  8056. $this->assertTags($result, $expected);
  8057. $this->Form->inputDefaults(array('required' => false));
  8058. $result = $this->Form->input('Contact.imrequired');
  8059. $expected = array(
  8060. 'div' => array('class' => 'input text'),
  8061. 'label' => array('for' => 'ContactImrequired'),
  8062. 'Imrequired',
  8063. '/label',
  8064. 'input' => array(
  8065. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8066. 'id' => 'ContactImrequired'
  8067. ),
  8068. '/div'
  8069. );
  8070. $this->assertTags($result, $expected);
  8071. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8072. $this->assertTags($result, $expected);
  8073. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8074. $expected = array(
  8075. 'div' => array('class' => 'input text required'),
  8076. 'label' => array('for' => 'ContactImrequired'),
  8077. 'Imrequired',
  8078. '/label',
  8079. 'input' => array(
  8080. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8081. 'id' => 'ContactImrequired'
  8082. ),
  8083. '/div'
  8084. );
  8085. $this->assertTags($result, $expected);
  8086. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8087. $this->assertTags($result, $expected);
  8088. }
  8089. /**
  8090. * Tests that the 'on' key validates as expected on update
  8091. *
  8092. * @return void
  8093. */
  8094. public function testRequiredOnUpdate() {
  8095. $this->Form->request->data['Contact']['id'] = 1;
  8096. $this->Form->create('Contact');
  8097. $result = $this->Form->input('Contact.imrequiredonupdate');
  8098. $expected = array(
  8099. 'div' => array('class' => 'input text required'),
  8100. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8101. 'Imrequiredonupdate',
  8102. '/label',
  8103. 'input' => array(
  8104. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8105. 'id' => 'ContactImrequiredonupdate',
  8106. 'required' => 'required'
  8107. ),
  8108. '/div'
  8109. );
  8110. $this->assertTags($result, $expected);
  8111. $result = $this->Form->input('Contact.imrequiredoncreate');
  8112. $expected = array(
  8113. 'div' => array('class' => 'input text'),
  8114. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8115. 'Imrequiredoncreate',
  8116. '/label',
  8117. 'input' => array(
  8118. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8119. 'id' => 'ContactImrequiredoncreate'
  8120. ),
  8121. '/div'
  8122. );
  8123. $this->assertTags($result, $expected);
  8124. $result = $this->Form->input('Contact.imrequiredonboth');
  8125. $expected = array(
  8126. 'div' => array('class' => 'input text required'),
  8127. 'label' => array('for' => 'ContactImrequiredonboth'),
  8128. 'Imrequiredonboth',
  8129. '/label',
  8130. 'input' => array(
  8131. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8132. 'id' => 'ContactImrequiredonboth',
  8133. 'required' => 'required'
  8134. ),
  8135. '/div'
  8136. );
  8137. $this->assertTags($result, $expected);
  8138. $result = $this->Form->input('Contact.imrequired');
  8139. $expected = array(
  8140. 'div' => array('class' => 'input text required'),
  8141. 'label' => array('for' => 'ContactImrequired'),
  8142. 'Imrequired',
  8143. '/label',
  8144. 'input' => array(
  8145. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8146. 'id' => 'ContactImrequired',
  8147. 'required' => 'required'
  8148. ),
  8149. '/div'
  8150. );
  8151. $this->assertTags($result, $expected);
  8152. }
  8153. /**
  8154. * Test inputDefaults setter and getter
  8155. *
  8156. * @return void
  8157. */
  8158. public function testInputDefaults() {
  8159. $this->Form->create('Contact');
  8160. $this->Form->inputDefaults(array(
  8161. 'label' => false,
  8162. 'div' => array(
  8163. 'style' => 'color: #000;'
  8164. )
  8165. ));
  8166. $result = $this->Form->input('Contact.field1');
  8167. $expected = array(
  8168. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8169. 'input' => array(
  8170. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8171. 'id' => 'ContactField1'
  8172. ),
  8173. '/div'
  8174. );
  8175. $this->assertTags($result, $expected);
  8176. $this->Form->inputDefaults(array(
  8177. 'div' => false,
  8178. 'label' => 'Label',
  8179. ));
  8180. $result = $this->Form->input('Contact.field1');
  8181. $expected = array(
  8182. 'label' => array('for' => 'ContactField1'),
  8183. 'Label',
  8184. '/label',
  8185. 'input' => array(
  8186. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8187. 'id' => 'ContactField1'
  8188. ),
  8189. );
  8190. $this->assertTags($result, $expected);
  8191. $this->Form->inputDefaults(array(
  8192. 'label' => false,
  8193. ), true);
  8194. $result = $this->Form->input('Contact.field1');
  8195. $expected = array(
  8196. 'input' => array(
  8197. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8198. 'id' => 'ContactField1'
  8199. ),
  8200. );
  8201. $this->assertTags($result, $expected);
  8202. $result = $this->Form->inputDefaults();
  8203. $expected = array(
  8204. 'div' => false,
  8205. 'label' => false,
  8206. );
  8207. $this->assertEqual($result, $expected);
  8208. }
  8209. }