FormHelperTest.php 293 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since CakePHP(tm) v 1.2.0.4206
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Controller\Controller;
  17. use Cake\Core\App;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\Model\Model;
  21. use Cake\Network\Request;
  22. use Cake\Routing\Router;
  23. use Cake\TestSuite\Fixture\TestModel;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\ClassRegistry;
  26. use Cake\Utility\Security;
  27. use Cake\View\Helper\FormHelper;
  28. use Cake\View\Helper\HtmlHelper;
  29. use Cake\View\View;
  30. /**
  31. * ContactTestController class
  32. *
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * uses property
  37. *
  38. * @var mixed null
  39. */
  40. public $uses = null;
  41. }
  42. /**
  43. * Contact class
  44. *
  45. */
  46. class Contact extends TestModel {
  47. /**
  48. * useTable property
  49. *
  50. * @var boolean
  51. */
  52. public $useTable = false;
  53. /**
  54. * Default schema
  55. *
  56. * @var array
  57. */
  58. protected $_schema = array(
  59. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  60. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  61. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  65. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  66. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  67. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  68. );
  69. /**
  70. * validate property
  71. *
  72. * @var array
  73. */
  74. public $validate = array(
  75. 'non_existing' => array(),
  76. 'idontexist' => array(),
  77. 'imrequired' => array('_allowEmpty' => false, array('rule' => array('between', 5, 30))),
  78. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  79. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  80. 'imrequiredonboth' => array(
  81. 'required' => array('rule' => 'alphaNumeric'),
  82. ),
  83. 'string_required' => 'notEmpty',
  84. 'imalsorequired' => array('_allowEmpty' => false, array('rule' => 'alphaNumeric')),
  85. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  86. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  87. 'imnotrequired' => array(
  88. '_allowEmpty' => true,
  89. array('rule' => 'alphaNumeric')
  90. ),
  91. 'imalsonotrequired' => array(
  92. '_allowEmpty' => true,
  93. 'alpha' => array('rule' => 'alphaNumeric'),
  94. 'between' => array('rule' => array('between', 5, 30)),
  95. ),
  96. 'imnotrequiredeither' => array(
  97. '_allowEmpty' => true,
  98. array('rule' => array('between', 5, 30))
  99. ),
  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(
  121. 'ContactTag' => array(
  122. 'className' => 'Cake\Test\TestCase\View\Helper\ContactTag',
  123. 'with' => 'Cake\Test\TestCase\View\Helper\ContactTagsContact'
  124. )
  125. );
  126. /**
  127. * hasAndBelongsToMany property
  128. *
  129. * @var array
  130. */
  131. public $belongsTo = array(
  132. 'User' => array('className' => 'UserForm'
  133. ));
  134. }
  135. /**
  136. * ContactTagsContact class
  137. *
  138. */
  139. class ContactTagsContact extends TestModel {
  140. /**
  141. * useTable property
  142. *
  143. * @var boolean
  144. */
  145. public $useTable = false;
  146. /**
  147. * Default schema
  148. *
  149. * @var array
  150. */
  151. protected $_schema = array(
  152. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  153. 'contact_tag_id' => array(
  154. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  155. )
  156. );
  157. /**
  158. * schema method
  159. *
  160. * @return void
  161. */
  162. public function setSchema($schema) {
  163. $this->_schema = $schema;
  164. }
  165. }
  166. /**
  167. * ContactNonStandardPk class
  168. *
  169. */
  170. class ContactNonStandardPk extends Contact {
  171. /**
  172. * primaryKey property
  173. *
  174. * @var string
  175. */
  176. public $primaryKey = 'pk';
  177. /**
  178. * schema method
  179. *
  180. * @return void
  181. */
  182. public function schema($field = false) {
  183. $this->_schema = parent::schema();
  184. $this->_schema['pk'] = $this->_schema['id'];
  185. unset($this->_schema['id']);
  186. return $this->_schema;
  187. }
  188. }
  189. /**
  190. * ContactTag class
  191. *
  192. */
  193. class ContactTag extends Model {
  194. /**
  195. * useTable property
  196. *
  197. * @var boolean
  198. */
  199. public $useTable = false;
  200. /**
  201. * schema definition
  202. *
  203. * @var array
  204. */
  205. protected $_schema = array(
  206. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  207. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  208. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  209. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  210. );
  211. }
  212. /**
  213. * UserForm class
  214. *
  215. */
  216. class UserForm extends TestModel {
  217. /**
  218. * useTable property
  219. *
  220. * @var boolean
  221. */
  222. public $useTable = false;
  223. /**
  224. * hasMany property
  225. *
  226. * @var array
  227. */
  228. public $hasMany = array(
  229. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  230. ));
  231. /**
  232. * schema definition
  233. *
  234. * @var array
  235. */
  236. protected $_schema = array(
  237. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  238. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  239. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  240. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  241. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  242. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  243. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  244. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  245. );
  246. }
  247. /**
  248. * OpenidUrl class
  249. *
  250. */
  251. class OpenidUrl extends TestModel {
  252. /**
  253. * useTable property
  254. *
  255. * @var boolean
  256. */
  257. public $useTable = false;
  258. /**
  259. * belongsTo property
  260. *
  261. * @var array
  262. */
  263. public $belongsTo = array('UserForm' => array(
  264. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  265. ));
  266. /**
  267. * validate property
  268. *
  269. * @var array
  270. */
  271. public $validate = array('openid_not_registered' => array());
  272. /**
  273. * schema method
  274. *
  275. * @var array
  276. */
  277. protected $_schema = array(
  278. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  279. 'user_form_id' => array(
  280. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  281. ),
  282. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  283. );
  284. /**
  285. * beforeValidate method
  286. *
  287. * @return void
  288. */
  289. public function beforeValidate($options = array()) {
  290. $this->invalidate('openid_not_registered');
  291. return true;
  292. }
  293. }
  294. /**
  295. * ValidateUser class
  296. *
  297. */
  298. class ValidateUser extends TestModel {
  299. /**
  300. * useTable property
  301. *
  302. * @var boolean
  303. */
  304. public $useTable = false;
  305. /**
  306. * hasOne property
  307. *
  308. * @var array
  309. */
  310. public $hasOne = array('ValidateProfile' => array(
  311. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  312. ));
  313. /**
  314. * schema method
  315. *
  316. * @var array
  317. */
  318. protected $_schema = array(
  319. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  320. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  321. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  322. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  323. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => '6,3'),
  324. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  325. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  326. );
  327. /**
  328. * beforeValidate method
  329. *
  330. * @return void
  331. */
  332. public function beforeValidate($options = array()) {
  333. $this->invalidate('email');
  334. return false;
  335. }
  336. }
  337. /**
  338. * ValidateProfile class
  339. *
  340. */
  341. class ValidateProfile extends TestModel {
  342. /**
  343. * useTable property
  344. *
  345. * @var boolean
  346. */
  347. public $useTable = false;
  348. /**
  349. * schema property
  350. *
  351. * @var array
  352. */
  353. protected $_schema = array(
  354. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  355. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  356. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  357. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  358. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  359. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  360. );
  361. /**
  362. * hasOne property
  363. *
  364. * @var array
  365. */
  366. public $hasOne = array('ValidateItem' => array(
  367. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  368. ));
  369. /**
  370. * belongsTo property
  371. *
  372. * @var array
  373. */
  374. public $belongsTo = array('ValidateUser' => array(
  375. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  376. ));
  377. /**
  378. * beforeValidate method
  379. *
  380. * @return void
  381. */
  382. public function beforeValidate($options = array()) {
  383. $this->invalidate('full_name');
  384. $this->invalidate('city');
  385. return false;
  386. }
  387. }
  388. /**
  389. * ValidateItem class
  390. *
  391. */
  392. class ValidateItem extends TestModel {
  393. /**
  394. * useTable property
  395. *
  396. * @var boolean
  397. */
  398. public $useTable = false;
  399. /**
  400. * schema property
  401. *
  402. * @var array
  403. */
  404. protected $_schema = array(
  405. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  406. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  407. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  408. 'description' => array(
  409. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  410. ),
  411. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  412. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  413. );
  414. /**
  415. * belongsTo property
  416. *
  417. * @var array
  418. */
  419. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  420. /**
  421. * beforeValidate method
  422. *
  423. * @return void
  424. */
  425. public function beforeValidate($options = array()) {
  426. $this->invalidate('description');
  427. return false;
  428. }
  429. }
  430. /**
  431. * TestMail class
  432. *
  433. */
  434. class TestMail extends TestModel {
  435. /**
  436. * useTable property
  437. *
  438. * @var boolean
  439. */
  440. public $useTable = false;
  441. }
  442. /**
  443. * FormHelperTest class
  444. *
  445. * @property FormHelper $Form
  446. */
  447. class FormHelperTest extends TestCase {
  448. /**
  449. * Fixtures to be used
  450. *
  451. * @var array
  452. */
  453. public $fixtures = array('core.post');
  454. /**
  455. * Do not load the fixtures by default
  456. *
  457. * @var boolean
  458. */
  459. public $autoFixtures = false;
  460. /**
  461. * setUp method
  462. *
  463. * @return void
  464. */
  465. public function setUp() {
  466. parent::setUp();
  467. $this->markTestIncomplete('Need to revisit once models work again.');
  468. Configure::write('Config.language', 'eng');
  469. Configure::write('App.base', '');
  470. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  471. Configure::delete('Asset');
  472. $this->Controller = new ContactTestController();
  473. $this->View = new View($this->Controller);
  474. $this->Form = new FormHelper($this->View);
  475. $this->Form->Html = new HtmlHelper($this->View);
  476. $this->Form->request = new Request('contacts/add');
  477. $this->Form->request->here = '/contacts/add';
  478. $this->Form->request['action'] = 'add';
  479. $this->Form->request->webroot = '';
  480. $this->Form->request->base = '';
  481. ClassRegistry::addObject('Contact', new Contact());
  482. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  483. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  484. ClassRegistry::addObject('User', new UserForm());
  485. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  486. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  487. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  488. $this->dateRegex = array(
  489. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  490. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  491. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  492. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  493. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  494. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  495. );
  496. Configure::write('Security.salt', 'foo!');
  497. Router::connect('/:controller', array('action' => 'index'));
  498. Router::connect('/:controller/:action/*');
  499. }
  500. /**
  501. * tearDown method
  502. *
  503. * @return void
  504. */
  505. public function tearDown() {
  506. parent::tearDown();
  507. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  508. }
  509. /**
  510. * testFormCreateWithSecurity method
  511. *
  512. * Test form->create() with security key.
  513. *
  514. * @return void
  515. */
  516. public function testCreateWithSecurity() {
  517. $this->Form->request['_Token'] = array('key' => 'testKey');
  518. $encoding = strtolower(Configure::read('App.encoding'));
  519. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  520. $expected = array(
  521. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  522. 'div' => array('style' => 'display:none;'),
  523. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  524. array('input' => array(
  525. 'type' => 'hidden', 'name' => '_Token[key]', 'value' => 'testKey', 'id'
  526. )),
  527. '/div'
  528. );
  529. $this->assertTags($result, $expected);
  530. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  531. $expected['form']['id'] = 'MyForm';
  532. $this->assertTags($result, $expected);
  533. }
  534. /**
  535. * testFormCreateGetNoSecurity method
  536. *
  537. * Test form->create() with no security key as its a get form
  538. *
  539. * @return void
  540. */
  541. public function testCreateEndGetNoSecurity() {
  542. $this->Form->request['_Token'] = array('key' => 'testKey');
  543. $encoding = strtolower(Configure::read('App.encoding'));
  544. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  545. $this->assertNotContains('Token', $result);
  546. $result = $this->Form->end('Save');
  547. $this->assertNotContains('Token', $result);
  548. }
  549. /**
  550. * test that create() clears the fields property so it starts fresh
  551. *
  552. * @return void
  553. */
  554. public function testCreateClearingFields() {
  555. $this->Form->fields = array('model_id');
  556. $this->Form->create('Contact');
  557. $this->assertEquals(array(), $this->Form->fields);
  558. }
  559. /**
  560. * Tests form hash generation with model-less data
  561. *
  562. * @return void
  563. */
  564. public function testValidateHashNoModel() {
  565. $this->Form->request['_Token'] = array('key' => 'foo');
  566. $result = $this->Form->secure(array('anything'));
  567. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  568. }
  569. /**
  570. * Tests that models with identical field names get resolved properly
  571. *
  572. * @return void
  573. */
  574. public function testDuplicateFieldNameResolution() {
  575. $result = $this->Form->create('ValidateUser');
  576. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  577. $result = $this->Form->input('ValidateItem.name');
  578. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  579. $result = $this->Form->input('ValidateUser.name');
  580. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  581. $this->assertContains('name="ValidateUser[name]"', $result);
  582. $this->assertContains('type="text"', $result);
  583. $result = $this->Form->input('ValidateItem.name');
  584. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  585. $this->assertContains('name="ValidateItem[name]"', $result);
  586. $this->assertContains('<textarea', $result);
  587. $result = $this->Form->input('name');
  588. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  589. $this->assertContains('name="ValidateUser[name]"', $result);
  590. $this->assertContains('type="text"', $result);
  591. }
  592. /**
  593. * Tests that hidden fields generated for checkboxes don't get locked
  594. *
  595. * @return void
  596. */
  597. public function testNoCheckboxLocking() {
  598. $this->Form->request['_Token'] = array('key' => 'foo');
  599. $this->assertSame(array(), $this->Form->fields);
  600. $this->Form->checkbox('check', array('value' => '1'));
  601. $this->assertSame($this->Form->fields, array('check'));
  602. }
  603. /**
  604. * testFormSecurityFields method
  605. *
  606. * Test generation of secure form hash generation.
  607. *
  608. * @return void
  609. */
  610. public function testFormSecurityFields() {
  611. $key = 'testKey';
  612. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  613. $this->Form->request['_Token'] = array('key' => $key);
  614. $result = $this->Form->secure($fields);
  615. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  616. $hash .= ':' . 'Model.valid';
  617. $hash = urlencode($hash);
  618. $expected = array(
  619. 'div' => array('style' => 'display:none;'),
  620. array('input' => array(
  621. 'type' => 'hidden', 'name' => '_Token[fields]',
  622. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  623. )),
  624. array('input' => array(
  625. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  626. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  627. )),
  628. '/div'
  629. );
  630. $this->assertTags($result, $expected);
  631. $path = CAKE . 'Test/TestApp/Config/';
  632. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  633. $result = $this->Form->secure($fields);
  634. $expected = array(
  635. 'div' => array('class' => 'hidden'),
  636. array('input' => array(
  637. 'type' => 'hidden', 'name' => '_Token[fields]',
  638. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  639. )),
  640. array('input' => array(
  641. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  642. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  643. )),
  644. '/div'
  645. );
  646. $this->assertTags($result, $expected);
  647. }
  648. /**
  649. * Tests correct generation of number fields for double and float fields
  650. *
  651. * @return void
  652. */
  653. public function testTextFieldGenerationForFloats() {
  654. $model = ClassRegistry::getObject('Contact');
  655. $model->setSchema(array('foo' => array(
  656. 'type' => 'float',
  657. 'null' => false,
  658. 'default' => null,
  659. 'length' => 10
  660. )));
  661. $this->Form->create('Contact');
  662. $result = $this->Form->input('foo');
  663. $expected = array(
  664. 'div' => array('class' => 'input number'),
  665. 'label' => array('for' => 'ContactFoo'),
  666. 'Foo',
  667. '/label',
  668. array('input' => array(
  669. 'type' => 'number',
  670. 'name' => 'Contact[foo]',
  671. 'id' => 'ContactFoo',
  672. 'step' => 'any'
  673. )),
  674. '/div'
  675. );
  676. $this->assertTags($result, $expected);
  677. $result = $this->Form->input('foo', array('step' => 0.5));
  678. $expected = array(
  679. 'div' => array('class' => 'input number'),
  680. 'label' => array('for' => 'ContactFoo'),
  681. 'Foo',
  682. '/label',
  683. array('input' => array(
  684. 'type' => 'number',
  685. 'name' => 'Contact[foo]',
  686. 'id' => 'ContactFoo',
  687. 'step' => '0.5'
  688. )),
  689. '/div'
  690. );
  691. $this->assertTags($result, $expected);
  692. }
  693. /**
  694. * Tests correct generation of number fields for integer fields
  695. *
  696. * @return void
  697. */
  698. public function testTextFieldTypeNumberGenerationForIntegers() {
  699. $model = ClassRegistry::getObject('Contact');
  700. $model->setSchema(array('foo' => array(
  701. 'type' => 'integer',
  702. 'null' => false,
  703. 'default' => null,
  704. 'length' => null
  705. )));
  706. $this->Form->create('Contact');
  707. $result = $this->Form->input('foo');
  708. $expected = array(
  709. 'div' => array('class' => 'input number'),
  710. 'label' => array('for' => 'ContactFoo'),
  711. 'Foo',
  712. '/label',
  713. array('input' => array(
  714. 'type' => 'number', 'name' => 'Contact[foo]',
  715. 'id' => 'ContactFoo'
  716. )),
  717. '/div'
  718. );
  719. $this->assertTags($result, $expected);
  720. }
  721. /**
  722. * testFormSecurityMultipleFields method
  723. *
  724. * Test secure() with multiple row form. Ensure hash is correct.
  725. *
  726. * @return void
  727. */
  728. public function testFormSecurityMultipleFields() {
  729. $key = 'testKey';
  730. $fields = array(
  731. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  732. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  733. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  734. );
  735. $this->Form->request['_Token'] = array('key' => $key);
  736. $result = $this->Form->secure($fields);
  737. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  738. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  739. $expected = array(
  740. 'div' => array('style' => 'display:none;'),
  741. array('input' => array(
  742. 'type' => 'hidden', 'name' => '_Token[fields]',
  743. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  744. )),
  745. array('input' => array(
  746. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  747. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  748. )),
  749. '/div'
  750. );
  751. $this->assertTags($result, $expected);
  752. }
  753. /**
  754. * testFormSecurityMultipleSubmitButtons
  755. *
  756. * test form submit generation and ensure that _Token is only created on end()
  757. *
  758. * @return void
  759. */
  760. public function testFormSecurityMultipleSubmitButtons() {
  761. $key = 'testKey';
  762. $this->Form->request['_Token'] = array('key' => $key);
  763. $this->Form->create('Addresses');
  764. $this->Form->input('Address.title');
  765. $this->Form->input('Address.first_name');
  766. $result = $this->Form->submit('Save', array('name' => 'save'));
  767. $expected = array(
  768. 'div' => array('class' => 'submit'),
  769. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  770. '/div',
  771. );
  772. $this->assertTags($result, $expected);
  773. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  774. $expected = array(
  775. 'div' => array('class' => 'submit'),
  776. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  777. '/div',
  778. );
  779. $this->assertTags($result, $expected);
  780. $result = $this->Form->end(null);
  781. $expected = array(
  782. 'div' => array('style' => 'display:none;'),
  783. array('input' => array(
  784. 'type' => 'hidden', 'name' => '_Token[fields]',
  785. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  786. )),
  787. array('input' => array(
  788. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  789. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  790. )),
  791. '/div'
  792. );
  793. $this->assertTags($result, $expected);
  794. }
  795. /**
  796. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  797. *
  798. * @return void
  799. */
  800. public function testSecurityButtonNestedNamed() {
  801. $key = 'testKey';
  802. $this->Form->request['_Token'] = array('key' => $key);
  803. $this->Form->create('Addresses');
  804. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  805. $result = $this->Form->unlockField();
  806. $this->assertEquals(array('Address.button'), $result);
  807. }
  808. /**
  809. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  810. *
  811. * @return void
  812. */
  813. public function testSecuritySubmitNestedNamed() {
  814. $key = 'testKey';
  815. $this->Form->request['_Token'] = array('key' => $key);
  816. $this->Form->create('Addresses');
  817. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  818. $result = $this->Form->unlockField();
  819. $this->assertEquals(array('Address.button'), $result);
  820. }
  821. /**
  822. * Test that the correct fields are unlocked for image submits with no names.
  823. *
  824. * @return void
  825. */
  826. public function testSecuritySubmitImageNoName() {
  827. $key = 'testKey';
  828. $this->Form->request['_Token'] = array('key' => $key);
  829. $this->Form->create('User');
  830. $result = $this->Form->submit('save.png');
  831. $expected = array(
  832. 'div' => array('class' => 'submit'),
  833. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  834. '/div'
  835. );
  836. $this->assertTags($result, $expected);
  837. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  838. }
  839. /**
  840. * Test that the correct fields are unlocked for image submits with names.
  841. *
  842. * @return void
  843. */
  844. public function testSecuritySubmitImageName() {
  845. $key = 'testKey';
  846. $this->Form->request['_Token'] = array('key' => $key);
  847. $this->Form->create('User');
  848. $result = $this->Form->submit('save.png', array('name' => 'test'));
  849. $expected = array(
  850. 'div' => array('class' => 'submit'),
  851. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  852. '/div'
  853. );
  854. $this->assertTags($result, $expected);
  855. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  856. }
  857. /**
  858. * testFormSecurityMultipleInputFields method
  859. *
  860. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  861. *
  862. * @return void
  863. */
  864. public function testFormSecurityMultipleInputFields() {
  865. $key = 'testKey';
  866. $this->Form->request['_Token'] = array('key' => $key);
  867. $this->Form->create('Addresses');
  868. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  869. $this->Form->input('Addresses.0.title');
  870. $this->Form->input('Addresses.0.first_name');
  871. $this->Form->input('Addresses.0.last_name');
  872. $this->Form->input('Addresses.0.address');
  873. $this->Form->input('Addresses.0.city');
  874. $this->Form->input('Addresses.0.phone');
  875. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  876. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  877. $this->Form->input('Addresses.1.title');
  878. $this->Form->input('Addresses.1.first_name');
  879. $this->Form->input('Addresses.1.last_name');
  880. $this->Form->input('Addresses.1.address');
  881. $this->Form->input('Addresses.1.city');
  882. $this->Form->input('Addresses.1.phone');
  883. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  884. $result = $this->Form->secure($this->Form->fields);
  885. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  886. $expected = array(
  887. 'div' => array('style' => 'display:none;'),
  888. array('input' => array(
  889. 'type' => 'hidden', 'name' => '_Token[fields]',
  890. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  891. )),
  892. array('input' => array(
  893. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  894. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  895. )),
  896. '/div'
  897. );
  898. $this->assertTags($result, $expected);
  899. }
  900. /**
  901. * Test form security with Model.field.0 style inputs
  902. *
  903. * @return void
  904. */
  905. public function testFormSecurityArrayFields() {
  906. $key = 'testKey';
  907. $this->Form->request->params['_Token']['key'] = $key;
  908. $this->Form->create('Address');
  909. $this->Form->input('Address.primary.1');
  910. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  911. $this->Form->input('Address.secondary.1.0');
  912. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  913. }
  914. /**
  915. * testFormSecurityMultipleInputDisabledFields method
  916. *
  917. * test secure form generation with multiple records and disabled fields.
  918. *
  919. * @return void
  920. */
  921. public function testFormSecurityMultipleInputDisabledFields() {
  922. $key = 'testKey';
  923. $this->Form->request->params['_Token'] = array(
  924. 'key' => $key,
  925. 'unlockedFields' => array('first_name', 'address')
  926. );
  927. $this->Form->create();
  928. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  929. $this->Form->input('Addresses.0.title');
  930. $this->Form->input('Addresses.0.first_name');
  931. $this->Form->input('Addresses.0.last_name');
  932. $this->Form->input('Addresses.0.address');
  933. $this->Form->input('Addresses.0.city');
  934. $this->Form->input('Addresses.0.phone');
  935. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  936. $this->Form->input('Addresses.1.title');
  937. $this->Form->input('Addresses.1.first_name');
  938. $this->Form->input('Addresses.1.last_name');
  939. $this->Form->input('Addresses.1.address');
  940. $this->Form->input('Addresses.1.city');
  941. $this->Form->input('Addresses.1.phone');
  942. $result = $this->Form->secure($this->Form->fields);
  943. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  944. $expected = array(
  945. 'div' => array('style' => 'display:none;'),
  946. array('input' => array(
  947. 'type' => 'hidden', 'name' => '_Token[fields]',
  948. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  949. )),
  950. array('input' => array(
  951. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  952. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  953. )),
  954. '/div'
  955. );
  956. $this->assertTags($result, $expected);
  957. }
  958. /**
  959. * testFormSecurityInputDisabledFields method
  960. *
  961. * Test single record form with disabled fields.
  962. *
  963. * @return void
  964. */
  965. public function testFormSecurityInputUnlockedFields() {
  966. $key = 'testKey';
  967. $this->Form->request['_Token'] = array(
  968. 'key' => $key,
  969. 'unlockedFields' => array('first_name', 'address')
  970. );
  971. $this->Form->create();
  972. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  973. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  974. $this->Form->input('Addresses.title');
  975. $this->Form->input('Addresses.first_name');
  976. $this->Form->input('Addresses.last_name');
  977. $this->Form->input('Addresses.address');
  978. $this->Form->input('Addresses.city');
  979. $this->Form->input('Addresses.phone');
  980. $result = $this->Form->fields;
  981. $expected = array(
  982. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  983. 'Addresses.city', 'Addresses.phone'
  984. );
  985. $this->assertEquals($expected, $result);
  986. $result = $this->Form->secure($expected);
  987. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  988. $expected = array(
  989. 'div' => array('style' => 'display:none;'),
  990. array('input' => array(
  991. 'type' => 'hidden', 'name' => '_Token[fields]',
  992. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  993. )),
  994. array('input' => array(
  995. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  996. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  997. )),
  998. '/div'
  999. );
  1000. $this->assertTags($result, $expected);
  1001. }
  1002. /**
  1003. * test securing inputs with custom name attributes.
  1004. *
  1005. * @return void
  1006. */
  1007. public function testFormSecureWithCustomNameAttribute() {
  1008. $this->Form->request->params['_Token']['key'] = 'testKey';
  1009. $this->Form->text('UserForm.published', array('name' => 'User[custom]'));
  1010. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1011. $this->Form->text('UserForm.published', array('name' => 'User[custom][another][value]'));
  1012. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1013. }
  1014. /**
  1015. * testFormSecuredInput method
  1016. *
  1017. * Test generation of entire secure form, assertions made on input() output.
  1018. *
  1019. * @return void
  1020. */
  1021. public function testFormSecuredInput() {
  1022. $this->Form->request['_Token'] = array('key' => 'testKey');
  1023. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1024. $encoding = strtolower(Configure::read('App.encoding'));
  1025. $expected = array(
  1026. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1027. 'div' => array('style' => 'display:none;'),
  1028. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1029. array('input' => array(
  1030. 'type' => 'hidden', 'name' => '_Token[key]',
  1031. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1032. )),
  1033. '/div'
  1034. );
  1035. $this->assertTags($result, $expected);
  1036. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1037. $expected = array(
  1038. 'div' => array('class' => 'input text'),
  1039. 'label' => array('for' => 'UserFormPublished'),
  1040. 'Published',
  1041. '/label',
  1042. array('input' => array(
  1043. 'type' => 'text', 'name' => 'UserForm[published]',
  1044. 'id' => 'UserFormPublished'
  1045. )),
  1046. '/div'
  1047. );
  1048. $this->assertTags($result, $expected);
  1049. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1050. $expected = array(
  1051. 'div' => array('class' => 'input text'),
  1052. 'label' => array('for' => 'UserFormOther'),
  1053. 'Other',
  1054. '/label',
  1055. array('input' => array(
  1056. 'type' => 'text', 'name' => 'UserForm[other]',
  1057. 'id' => 'UserFormOther'
  1058. )),
  1059. '/div'
  1060. );
  1061. $this->assertTags($result, $expected);
  1062. $result = $this->Form->hidden('UserForm.stuff');
  1063. $expected = array(
  1064. 'input' => array(
  1065. 'type' => 'hidden', 'name' => 'UserForm[stuff]',
  1066. 'id' => 'UserFormStuff'
  1067. ));
  1068. $this->assertTags($result, $expected);
  1069. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1070. $expected = array('input' => array(
  1071. 'type' => 'hidden', 'name' => 'UserForm[hidden]',
  1072. 'value' => '0', 'id' => 'UserFormHidden'
  1073. ));
  1074. $this->assertTags($result, $expected);
  1075. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1076. $expected = array(
  1077. 'div' => array('class' => 'input checkbox'),
  1078. array('input' => array(
  1079. 'type' => 'hidden', 'name' => 'UserForm[something]',
  1080. 'value' => '0', 'id' => 'UserFormSomething_'
  1081. )),
  1082. array('input' => array(
  1083. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  1084. 'value' => '1', 'id' => 'UserFormSomething'
  1085. )),
  1086. 'label' => array('for' => 'UserFormSomething'),
  1087. 'Something',
  1088. '/label',
  1089. '/div'
  1090. );
  1091. $this->assertTags($result, $expected);
  1092. $result = $this->Form->fields;
  1093. $expected = array(
  1094. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1095. 'UserForm.hidden' => '0', 'UserForm.something'
  1096. );
  1097. $this->assertEquals($expected, $result);
  1098. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1099. $result = $this->Form->secure($this->Form->fields);
  1100. $expected = array(
  1101. 'div' => array('style' => 'display:none;'),
  1102. array('input' => array(
  1103. 'type' => 'hidden', 'name' => '_Token[fields]',
  1104. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1105. )),
  1106. array('input' => array(
  1107. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1108. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1109. )),
  1110. '/div'
  1111. );
  1112. $this->assertTags($result, $expected);
  1113. }
  1114. /**
  1115. * Test secured inputs with custom names.
  1116. *
  1117. * @return void
  1118. */
  1119. public function testSecuredInputCustomName() {
  1120. $this->Form->request['_Token'] = array('key' => 'testKey');
  1121. $this->assertEquals(array(), $this->Form->fields);
  1122. $this->Form->input('text_input', array(
  1123. 'name' => 'data[Option][General.default_role]',
  1124. ));
  1125. $expected = array('Option.General.default_role');
  1126. $this->assertEquals($expected, $this->Form->fields);
  1127. $this->Form->input('select_box', array(
  1128. 'name' => 'data[Option][General.select_role]',
  1129. 'type' => 'select',
  1130. 'options' => array(1, 2),
  1131. ));
  1132. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1133. $this->assertEquals($expected, $this->Form->fields);
  1134. }
  1135. /**
  1136. * Tests that the correct keys are added to the field hash index
  1137. *
  1138. * @return void
  1139. */
  1140. public function testFormSecuredFileInput() {
  1141. $this->Form->request['_Token'] = array('key' => 'testKey');
  1142. $this->assertEquals(array(), $this->Form->fields);
  1143. $this->Form->file('Attachment.file');
  1144. $expected = array(
  1145. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1146. 'Attachment.file.error', 'Attachment.file.size'
  1147. );
  1148. $this->assertEquals($expected, $this->Form->fields);
  1149. }
  1150. /**
  1151. * test that multiple selects keys are added to field hash
  1152. *
  1153. * @return void
  1154. */
  1155. public function testFormSecuredMultipleSelect() {
  1156. $this->Form->request['_Token'] = array('key' => 'testKey');
  1157. $this->assertEquals(array(), $this->Form->fields);
  1158. $options = array('1' => 'one', '2' => 'two');
  1159. $this->Form->select('Model.select', $options);
  1160. $expected = array('Model.select');
  1161. $this->assertEquals($expected, $this->Form->fields);
  1162. $this->Form->fields = array();
  1163. $this->Form->select('Model.select', $options, array('multiple' => true));
  1164. $this->assertEquals($expected, $this->Form->fields);
  1165. }
  1166. /**
  1167. * testFormSecuredRadio method
  1168. *
  1169. * @return void
  1170. */
  1171. public function testFormSecuredRadio() {
  1172. $this->Form->request['_Token'] = array('key' => 'testKey');
  1173. $this->assertEquals(array(), $this->Form->fields);
  1174. $options = array('1' => 'option1', '2' => 'option2');
  1175. $this->Form->radio('Test.test', $options);
  1176. $expected = array('Test.test');
  1177. $this->assertEquals($expected, $this->Form->fields);
  1178. }
  1179. /**
  1180. * Test that when disabled is in a list based attribute array it works.
  1181. *
  1182. * @return void
  1183. */
  1184. public function testFormSecuredAndDisabledNotAssoc() {
  1185. $this->Form->request['_Token'] = array('key' => 'testKey');
  1186. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1187. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1188. $this->Form->text('Model.text', array('disabled'));
  1189. $this->Form->textarea('Model.textarea', array('disabled'));
  1190. $this->Form->password('Model.password', array('disabled'));
  1191. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1192. $expected = array(
  1193. 'Model.radio' => ''
  1194. );
  1195. $this->assertEquals($expected, $this->Form->fields);
  1196. }
  1197. /**
  1198. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1199. * hashing.
  1200. *
  1201. * @return void
  1202. */
  1203. public function testFormSecuredAndDisabled() {
  1204. $this->Form->request['_Token'] = array('key' => 'testKey');
  1205. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1206. $this->Form->text('Model.text', array('disabled' => true));
  1207. $this->Form->password('Model.text', array('disabled' => true));
  1208. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1209. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1210. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1211. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1212. $this->Form->month('Model.month', array('disabled' => true));
  1213. $this->Form->day('Model.day', array('disabled' => true));
  1214. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1215. $this->Form->minute('Model.minute', array('disabled' => true));
  1216. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1217. $expected = array(
  1218. 'Model.radio' => ''
  1219. );
  1220. $this->assertEquals($expected, $this->Form->fields);
  1221. }
  1222. /**
  1223. * testDisableSecurityUsingForm method
  1224. *
  1225. * @return void
  1226. */
  1227. public function testDisableSecurityUsingForm() {
  1228. $this->Form->request['_Token'] = array(
  1229. 'key' => 'testKey',
  1230. 'disabledFields' => array()
  1231. );
  1232. $this->Form->create();
  1233. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1234. $this->Form->input('Addresses.title');
  1235. $this->Form->input('Addresses.first_name', array('secure' => false));
  1236. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1237. $this->Form->input('Addresses.zip', array(
  1238. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1239. ));
  1240. $result = $this->Form->fields;
  1241. $expected = array(
  1242. 'Addresses.id' => '123456', 'Addresses.title',
  1243. );
  1244. $this->assertEquals($expected, $result);
  1245. }
  1246. /**
  1247. * test disableField
  1248. *
  1249. * @return void
  1250. */
  1251. public function testUnlockFieldAddsToList() {
  1252. $this->Form->request['_Token'] = array(
  1253. 'key' => 'testKey',
  1254. 'unlockedFields' => array()
  1255. );
  1256. $this->Form->create('Contact');
  1257. $this->Form->unlockField('Contact.name');
  1258. $this->Form->text('Contact.name');
  1259. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1260. $this->assertEquals(array(), $this->Form->fields);
  1261. }
  1262. /**
  1263. * test unlockField removing from fields array.
  1264. *
  1265. * @return void
  1266. */
  1267. public function testUnlockFieldRemovingFromFields() {
  1268. $this->Form->request['_Token'] = array(
  1269. 'key' => 'testKey',
  1270. 'unlockedFields' => array()
  1271. );
  1272. $this->Form->create('Contact');
  1273. $this->Form->hidden('Contact.id', array('value' => 1));
  1274. $this->Form->text('Contact.name');
  1275. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1276. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1277. $this->Form->unlockField('Contact.name');
  1278. $this->Form->unlockField('Contact.id');
  1279. $this->assertEquals(array(), $this->Form->fields);
  1280. }
  1281. /**
  1282. * testTagIsInvalid method
  1283. *
  1284. * @return void
  1285. */
  1286. public function testTagIsInvalid() {
  1287. $Contact = ClassRegistry::getObject('Contact');
  1288. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1289. $this->Form->setEntity('Contact.0.email');
  1290. $result = $this->Form->tagIsInvalid();
  1291. $this->assertEquals($expected, $result);
  1292. $this->Form->setEntity('Contact.1.email');
  1293. $result = $this->Form->tagIsInvalid();
  1294. $this->assertFalse($result);
  1295. $this->Form->setEntity('Contact.0.name');
  1296. $result = $this->Form->tagIsInvalid();
  1297. $this->assertFalse($result);
  1298. }
  1299. /**
  1300. * Test tagIsInvalid with validation errors from a saveMany
  1301. *
  1302. * @return void
  1303. */
  1304. public function testTagIsInvalidSaveMany() {
  1305. $Contact = ClassRegistry::getObject('Contact');
  1306. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1307. $this->Form->create('Contact');
  1308. $this->Form->setEntity('0.email');
  1309. $result = $this->Form->tagIsInvalid();
  1310. $this->assertEquals($expected, $result);
  1311. $this->Form->setEntity('0.Contact.email');
  1312. $result = $this->Form->tagIsInvalid();
  1313. $this->assertEquals($expected, $result);
  1314. }
  1315. /**
  1316. * Test validation errors.
  1317. *
  1318. * @return void
  1319. */
  1320. public function testPasswordValidation() {
  1321. $Contact = ClassRegistry::getObject('Contact');
  1322. $Contact->validationErrors['password'] = array('Please provide a password');
  1323. $result = $this->Form->input('Contact.password');
  1324. $expected = array(
  1325. 'div' => array('class' => 'input password error'),
  1326. 'label' => array('for' => 'ContactPassword'),
  1327. 'Password',
  1328. '/label',
  1329. 'input' => array(
  1330. 'type' => 'password', 'name' => 'Contact[password]',
  1331. 'id' => 'ContactPassword', 'class' => 'form-error'
  1332. ),
  1333. array('div' => array('class' => 'error-message')),
  1334. 'Please provide a password',
  1335. '/div',
  1336. '/div'
  1337. );
  1338. $this->assertTags($result, $expected);
  1339. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1340. $expected = array(
  1341. 'div' => array('class' => 'input password error'),
  1342. 'label' => array('for' => 'ContactPassword'),
  1343. 'Password',
  1344. '/label',
  1345. 'input' => array(
  1346. 'type' => 'password', 'name' => 'Contact[password]',
  1347. 'id' => 'ContactPassword', 'class' => 'form-error'
  1348. ),
  1349. '/div'
  1350. );
  1351. $this->assertTags($result, $expected);
  1352. }
  1353. /**
  1354. * Test validation errors, when validation message is an empty string.
  1355. *
  1356. * @return void
  1357. */
  1358. public function testEmptyErrorValidation() {
  1359. $this->Form->validationErrors['Contact']['password'] = '';
  1360. $result = $this->Form->input('Contact.password');
  1361. $expected = array(
  1362. 'div' => array('class' => 'input password error'),
  1363. 'label' => array('for' => 'ContactPassword'),
  1364. 'Password',
  1365. '/label',
  1366. 'input' => array(
  1367. 'type' => 'password', 'name' => 'Contact[password]',
  1368. 'id' => 'ContactPassword', 'class' => 'form-error'
  1369. ),
  1370. array('div' => array('class' => 'error-message')),
  1371. array(),
  1372. '/div',
  1373. '/div'
  1374. );
  1375. $this->assertTags($result, $expected);
  1376. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1377. $expected = array(
  1378. 'div' => array('class' => 'input password error'),
  1379. 'label' => array('for' => 'ContactPassword'),
  1380. 'Password',
  1381. '/label',
  1382. 'input' => array(
  1383. 'type' => 'password', 'name' => 'Contact[password]',
  1384. 'id' => 'ContactPassword', 'class' => 'form-error'
  1385. ),
  1386. '/div'
  1387. );
  1388. $this->assertTags($result, $expected);
  1389. }
  1390. /**
  1391. * Test validation errors, when calling input() overriding validation message by an empty string.
  1392. *
  1393. * @return void
  1394. */
  1395. public function testEmptyInputErrorValidation() {
  1396. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1397. $result = $this->Form->input('Contact.password', array('error' => ''));
  1398. $expected = array(
  1399. 'div' => array('class' => 'input password error'),
  1400. 'label' => array('for' => 'ContactPassword'),
  1401. 'Password',
  1402. '/label',
  1403. 'input' => array(
  1404. 'type' => 'password', 'name' => 'Contact[password]',
  1405. 'id' => 'ContactPassword', 'class' => 'form-error'
  1406. ),
  1407. array('div' => array('class' => 'error-message')),
  1408. array(),
  1409. '/div',
  1410. '/div'
  1411. );
  1412. $this->assertTags($result, $expected);
  1413. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1414. $expected = array(
  1415. 'div' => array('class' => 'input password error'),
  1416. 'label' => array('for' => 'ContactPassword'),
  1417. 'Password',
  1418. '/label',
  1419. 'input' => array(
  1420. 'type' => 'password', 'name' => 'Contact[password]',
  1421. 'id' => 'ContactPassword', 'class' => 'form-error'
  1422. ),
  1423. '/div'
  1424. );
  1425. $this->assertTags($result, $expected);
  1426. }
  1427. /**
  1428. * testFormValidationAssociated method
  1429. *
  1430. * test display of form errors in conjunction with model::validates.
  1431. *
  1432. * @return void
  1433. */
  1434. public function testFormValidationAssociated() {
  1435. $this->UserForm = ClassRegistry::getObject('UserForm');
  1436. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1437. $data = array(
  1438. 'UserForm' => array('name' => 'user'),
  1439. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1440. );
  1441. $result = $this->UserForm->OpenidUrl->create($data);
  1442. $this->assertFalse(empty($result));
  1443. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1444. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1445. $encoding = strtolower(Configure::read('App.encoding'));
  1446. $expected = array(
  1447. 'form' => array(
  1448. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1449. 'accept-charset' => $encoding
  1450. ),
  1451. 'div' => array('style' => 'display:none;'),
  1452. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1453. '/div'
  1454. );
  1455. $this->assertTags($result, $expected);
  1456. $result = $this->Form->error(
  1457. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1458. );
  1459. $this->assertEquals('Error, not registered', $result);
  1460. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1461. }
  1462. /**
  1463. * testFormValidationAssociatedFirstLevel method
  1464. *
  1465. * test form error display with associated model.
  1466. *
  1467. * @return void
  1468. */
  1469. public function testFormValidationAssociatedFirstLevel() {
  1470. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1471. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1472. $data = array(
  1473. 'ValidateUser' => array('name' => 'mariano'),
  1474. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1475. );
  1476. $result = $this->ValidateUser->create($data);
  1477. $this->assertFalse(empty($result));
  1478. $this->assertFalse($this->ValidateUser->validates());
  1479. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1480. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1481. $encoding = strtolower(Configure::read('App.encoding'));
  1482. $expected = array(
  1483. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1484. 'div' => array('style' => 'display:none;'),
  1485. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1486. '/div'
  1487. );
  1488. $this->assertTags($result, $expected);
  1489. $result = $this->Form->error(
  1490. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1491. );
  1492. $this->assertEquals('Invalid email', $result);
  1493. $result = $this->Form->error(
  1494. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1495. );
  1496. $this->assertEquals('Invalid name', $result);
  1497. $result = $this->Form->error(
  1498. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1499. );
  1500. $this->assertEquals('Invalid city', $result);
  1501. unset($this->ValidateUser->ValidateProfile);
  1502. unset($this->ValidateUser);
  1503. }
  1504. /**
  1505. * testFormValidationAssociatedSecondLevel method
  1506. *
  1507. * test form error display with associated model.
  1508. *
  1509. * @return void
  1510. */
  1511. public function testFormValidationAssociatedSecondLevel() {
  1512. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1513. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1514. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1515. $data = array(
  1516. 'ValidateUser' => array('name' => 'mariano'),
  1517. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1518. 'ValidateItem' => array('name' => 'Item')
  1519. );
  1520. $result = $this->ValidateUser->create($data);
  1521. $this->assertFalse(empty($result));
  1522. $this->assertFalse($this->ValidateUser->validates());
  1523. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1524. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1525. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1526. $encoding = strtolower(Configure::read('App.encoding'));
  1527. $expected = array(
  1528. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1529. 'div' => array('style' => 'display:none;'),
  1530. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1531. '/div'
  1532. );
  1533. $this->assertTags($result, $expected);
  1534. $result = $this->Form->error(
  1535. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1536. );
  1537. $this->assertEquals('Invalid email', $result);
  1538. $result = $this->Form->error(
  1539. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1540. );
  1541. $this->assertEquals('Invalid name', $result);
  1542. $result = $this->Form->error(
  1543. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1544. );
  1545. $result = $this->Form->error(
  1546. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1547. );
  1548. $this->assertEquals('Invalid description', $result);
  1549. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1550. unset($this->ValidateUser->ValidateProfile);
  1551. unset($this->ValidateUser);
  1552. }
  1553. /**
  1554. * testFormValidationMultiRecord method
  1555. *
  1556. * test form error display with multiple records.
  1557. *
  1558. * @return void
  1559. */
  1560. public function testFormValidationMultiRecord() {
  1561. $Contact = ClassRegistry::getObject('Contact');
  1562. $Contact->validationErrors[2] = array(
  1563. 'name' => array('The provided value is invalid')
  1564. );
  1565. $result = $this->Form->input('Contact.2.name');
  1566. $expected = array(
  1567. 'div' => array('class' => 'input text error'),
  1568. 'label' => array('for' => 'Contact2Name'),
  1569. 'Name',
  1570. '/label',
  1571. 'input' => array(
  1572. 'type' => 'text', 'name' => 'Contact[2][name]', 'id' => 'Contact2Name',
  1573. 'class' => 'form-error', 'maxlength' => 255
  1574. ),
  1575. array('div' => array('class' => 'error-message')),
  1576. 'The provided value is invalid',
  1577. '/div',
  1578. '/div'
  1579. );
  1580. $this->assertTags($result, $expected);
  1581. }
  1582. /**
  1583. * testMultipleInputValidation method
  1584. *
  1585. * test multiple record form validation error display.
  1586. *
  1587. * @return void
  1588. */
  1589. public function testMultipleInputValidation() {
  1590. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1591. $Address->validationErrors[0] = array(
  1592. 'title' => array('This field cannot be empty'),
  1593. 'first_name' => array('This field cannot be empty')
  1594. );
  1595. $Address->validationErrors[1] = array(
  1596. 'last_name' => array('You must have a last name')
  1597. );
  1598. $this->Form->create();
  1599. $result = $this->Form->input('Address.0.title');
  1600. $expected = array(
  1601. 'div' => array('class'),
  1602. 'label' => array('for'),
  1603. 'preg:/[^<]+/',
  1604. '/label',
  1605. 'input' => array(
  1606. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1607. ),
  1608. array('div' => array('class' => 'error-message')),
  1609. 'This field cannot be empty',
  1610. '/div',
  1611. '/div'
  1612. );
  1613. $this->assertTags($result, $expected);
  1614. $result = $this->Form->input('Address.0.first_name');
  1615. $expected = array(
  1616. 'div' => array('class'),
  1617. 'label' => array('for'),
  1618. 'preg:/[^<]+/',
  1619. '/label',
  1620. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1621. array('div' => array('class' => 'error-message')),
  1622. 'This field cannot be empty',
  1623. '/div',
  1624. '/div'
  1625. );
  1626. $this->assertTags($result, $expected);
  1627. $result = $this->Form->input('Address.0.last_name');
  1628. $expected = array(
  1629. 'div' => array('class'),
  1630. 'label' => array('for'),
  1631. 'preg:/[^<]+/',
  1632. '/label',
  1633. 'input' => array('type' => 'text', 'name', 'id'),
  1634. '/div'
  1635. );
  1636. $this->assertTags($result, $expected);
  1637. $result = $this->Form->input('Address.1.last_name');
  1638. $expected = array(
  1639. 'div' => array('class'),
  1640. 'label' => array('for'),
  1641. 'preg:/[^<]+/',
  1642. '/label',
  1643. 'input' => array(
  1644. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1645. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1646. ),
  1647. array('div' => array('class' => 'error-message')),
  1648. 'You must have a last name',
  1649. '/div',
  1650. '/div'
  1651. );
  1652. $this->assertTags($result, $expected);
  1653. }
  1654. /**
  1655. * testInput method
  1656. *
  1657. * Test various incarnations of input().
  1658. *
  1659. * @return void
  1660. */
  1661. public function testInput() {
  1662. $result = $this->Form->input('ValidateUser.balance');
  1663. $expected = array(
  1664. 'div' => array('class'),
  1665. 'label' => array('for'),
  1666. 'Balance',
  1667. '/label',
  1668. 'input' => array('name', 'type' => 'number', 'id'),
  1669. '/div',
  1670. );
  1671. $this->assertTags($result, $expected);
  1672. $result = $this->Form->input('ValidateUser.cost_decimal');
  1673. $expected = array(
  1674. 'div' => array('class'),
  1675. 'label' => array('for'),
  1676. 'Cost Decimal',
  1677. '/label',
  1678. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  1679. '/div',
  1680. );
  1681. $this->assertTags($result, $expected);
  1682. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1683. $expected = array(
  1684. 'div' => array('class' => 'input email'),
  1685. 'label' => array('for' => 'custom'),
  1686. 'Email',
  1687. '/label',
  1688. array('input' => array(
  1689. 'type' => 'email', 'name' => 'Contact[email]',
  1690. 'id' => 'custom', 'maxlength' => 255
  1691. )),
  1692. '/div'
  1693. );
  1694. $this->assertTags($result, $expected);
  1695. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1696. $expected = array(
  1697. '<div',
  1698. 'label' => array('for' => 'ContactEmail'),
  1699. 'Email',
  1700. '/label',
  1701. array('input' => array(
  1702. 'type' => 'email', 'name' => 'Contact[email]',
  1703. 'id' => 'ContactEmail', 'maxlength' => 255
  1704. )),
  1705. '/div'
  1706. );
  1707. $this->assertTags($result, $expected);
  1708. $result = $this->Form->hidden('Contact.idontexist');
  1709. $expected = array('input' => array(
  1710. 'type' => 'hidden', 'name' => 'Contact[idontexist]',
  1711. 'id' => 'ContactIdontexist'
  1712. ));
  1713. $this->assertTags($result, $expected);
  1714. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1715. $expected = array(
  1716. 'div' => array('class' => 'input text'),
  1717. 'label' => array('for' => 'ContactEmail'),
  1718. 'Email',
  1719. '/label',
  1720. array('input' => array(
  1721. 'type' => 'text', 'name' => 'Contact[email]',
  1722. 'id' => 'ContactEmail'
  1723. )),
  1724. '/div'
  1725. );
  1726. $this->assertTags($result, $expected);
  1727. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1728. $expected = array(
  1729. 'div' => array('class' => 'input text'),
  1730. 'label' => array('for' => 'Contact5Email'),
  1731. 'Email',
  1732. '/label',
  1733. array('input' => array(
  1734. 'type' => 'text', 'name' => 'Contact[5][email]',
  1735. 'id' => 'Contact5Email'
  1736. )),
  1737. '/div'
  1738. );
  1739. $this->assertTags($result, $expected);
  1740. $result = $this->Form->input('Contact.password');
  1741. $expected = array(
  1742. 'div' => array('class' => 'input password'),
  1743. 'label' => array('for' => 'ContactPassword'),
  1744. 'Password',
  1745. '/label',
  1746. array('input' => array(
  1747. 'type' => 'password', 'name' => 'Contact[password]',
  1748. 'id' => 'ContactPassword'
  1749. )),
  1750. '/div'
  1751. );
  1752. $this->assertTags($result, $expected);
  1753. $result = $this->Form->input('Contact.email', array(
  1754. 'type' => 'file', 'class' => 'textbox'
  1755. ));
  1756. $expected = array(
  1757. 'div' => array('class' => 'input file'),
  1758. 'label' => array('for' => 'ContactEmail'),
  1759. 'Email',
  1760. '/label',
  1761. array('input' => array(
  1762. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  1763. 'id' => 'ContactEmail'
  1764. )),
  1765. '/div'
  1766. );
  1767. $this->assertTags($result, $expected);
  1768. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1769. $result = $this->Form->input('Contact.phone');
  1770. $expected = array(
  1771. 'div' => array('class' => 'input tel'),
  1772. 'label' => array('for' => 'ContactPhone'),
  1773. 'Phone',
  1774. '/label',
  1775. array('input' => array(
  1776. 'type' => 'tel', 'name' => 'Contact[phone]',
  1777. 'value' => 'Hello &amp; World &gt; weird chars',
  1778. 'id' => 'ContactPhone', 'maxlength' => 255
  1779. )),
  1780. '/div'
  1781. );
  1782. $this->assertTags($result, $expected);
  1783. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1784. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1785. $expected = array(
  1786. 'div' => array('class' => 'input text'),
  1787. 'label' => array('for' => 'myId'),
  1788. 'Field',
  1789. '/label',
  1790. 'input' => array(
  1791. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  1792. 'value' => 'My value', 'id' => 'myId'
  1793. ),
  1794. '/div'
  1795. );
  1796. $this->assertTags($result, $expected);
  1797. unset($this->Form->request->data);
  1798. $Contact = ClassRegistry::getObject('Contact');
  1799. $Contact->validationErrors['field'] = array('Badness!');
  1800. $result = $this->Form->input('Contact.field');
  1801. $expected = array(
  1802. 'div' => array('class' => 'input text error'),
  1803. 'label' => array('for' => 'ContactField'),
  1804. 'Field',
  1805. '/label',
  1806. 'input' => array(
  1807. 'type' => 'text', 'name' => 'Contact[field]',
  1808. 'id' => 'ContactField', 'class' => 'form-error'
  1809. ),
  1810. array('div' => array('class' => 'error-message')),
  1811. 'Badness!',
  1812. '/div',
  1813. '/div'
  1814. );
  1815. $this->assertTags($result, $expected);
  1816. $result = $this->Form->input('Contact.field', array(
  1817. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1818. ));
  1819. $expected = array(
  1820. 'label' => array('for' => 'ContactField'),
  1821. 'Field',
  1822. '/label',
  1823. 'input' => array(
  1824. 'type' => 'text', 'name' => 'Contact[field]',
  1825. 'id' => 'ContactField', 'class' => 'form-error'
  1826. ),
  1827. array('span' => array('class' => 'error-message')),
  1828. 'Badness!',
  1829. '/span'
  1830. );
  1831. $this->assertTags($result, $expected);
  1832. $result = $this->Form->input('Contact.field', array(
  1833. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1834. ));
  1835. $expected = array(
  1836. 'div' => array('class' => 'input text error'),
  1837. 'label' => array('for' => 'ContactField'),
  1838. 'Field',
  1839. '/label',
  1840. 'input' => array(
  1841. 'type' => 'text', 'name' => 'Contact[field]',
  1842. 'id' => 'ContactField', 'class' => 'form-error'
  1843. ),
  1844. array('div' => array('class' => 'error')),
  1845. 'Badness!',
  1846. '/div'
  1847. );
  1848. $this->assertTags($result, $expected);
  1849. $result = $this->Form->input('Contact.field', array(
  1850. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1851. ));
  1852. $expected = array(
  1853. 'span' => array('class' => 'input text error'),
  1854. 'label' => array('for' => 'ContactField'),
  1855. 'Field',
  1856. '/label',
  1857. 'input' => array(
  1858. 'type' => 'text', 'name' => 'Contact[field]',
  1859. 'id' => 'ContactField', 'class' => 'form-error'
  1860. ),
  1861. 'Badness!',
  1862. '/span'
  1863. );
  1864. $this->assertTags($result, $expected);
  1865. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1866. $expected = array(
  1867. 'div' => array('class' => 'input text error'),
  1868. 'label' => array('for' => 'ContactField'),
  1869. 'Field',
  1870. '/label',
  1871. 'input' => array(
  1872. 'type' => 'text', 'name' => 'Contact[field]',
  1873. 'id' => 'ContactField', 'class' => 'form-error'
  1874. ),
  1875. 'A message to you, Rudy',
  1876. array('div' => array('class' => 'error-message')),
  1877. 'Badness!',
  1878. '/div',
  1879. '/div'
  1880. );
  1881. $this->assertTags($result, $expected);
  1882. $this->Form->setEntity(null);
  1883. $this->Form->setEntity('Contact.field');
  1884. $result = $this->Form->input('Contact.field', array(
  1885. 'after' => 'A message to you, Rudy', 'error' => false
  1886. ));
  1887. $expected = array(
  1888. 'div' => array('class' => 'input text'),
  1889. 'label' => array('for' => 'ContactField'),
  1890. 'Field',
  1891. '/label',
  1892. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1893. 'A message to you, Rudy',
  1894. '/div'
  1895. );
  1896. $this->assertTags($result, $expected);
  1897. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1898. $expected = array(
  1899. 'div' => array('class' => 'input text'),
  1900. 'label' => array('for' => 'ObjectField'),
  1901. 'Field',
  1902. '/label',
  1903. 'input' => array('type' => 'text', 'name' => 'Object[field]', 'id' => 'ObjectField'),
  1904. 'A message to you, Rudy',
  1905. '/div'
  1906. );
  1907. $this->assertTags($result, $expected);
  1908. $Contact->validationErrors['field'] = array('minLength');
  1909. $result = $this->Form->input('Contact.field', array(
  1910. 'error' => array(
  1911. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1912. 'maxLength' => 'login too large'
  1913. )
  1914. ));
  1915. $expected = array(
  1916. 'div' => array('class' => 'input text error'),
  1917. 'label' => array('for' => 'ContactField'),
  1918. 'Field',
  1919. '/label',
  1920. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1921. array('div' => array('class' => 'error-message')),
  1922. 'Le login doit contenir au moins 2 caractères',
  1923. '/div',
  1924. '/div'
  1925. );
  1926. $this->assertTags($result, $expected);
  1927. $Contact->validationErrors['field'] = array('maxLength');
  1928. $result = $this->Form->input('Contact.field', array(
  1929. 'error' => array(
  1930. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1931. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1932. 'maxLength' => 'login too large',
  1933. )
  1934. ));
  1935. $expected = array(
  1936. 'div' => array('class' => 'input text error'),
  1937. 'label' => array('for' => 'ContactField'),
  1938. 'Field',
  1939. '/label',
  1940. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1941. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1942. 'login too large',
  1943. '/span',
  1944. '/div'
  1945. );
  1946. $this->assertTags($result, $expected);
  1947. }
  1948. /**
  1949. * Test that inputs with 0 can be created.
  1950. *
  1951. * @return void
  1952. */
  1953. public function testInputZero() {
  1954. $this->Form->create('User');
  1955. $result = $this->Form->input('0');
  1956. $expected = array(
  1957. 'div' => array('class' => 'input text'),
  1958. 'label' => array('for' => 'User0'), '/label',
  1959. 'input' => array('type' => 'text', 'name' => 'User[0]', 'id' => 'User0'),
  1960. '/div'
  1961. );
  1962. $this->assertTags($result, $expected);
  1963. }
  1964. /**
  1965. * test input() with checkbox creation
  1966. *
  1967. * @return void
  1968. */
  1969. public function testInputCheckbox() {
  1970. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1971. $expected = array(
  1972. 'div' => array('class' => 'input checkbox'),
  1973. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  1974. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1975. '/div'
  1976. );
  1977. $this->assertTags($result, $expected);
  1978. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1979. $expected = array(
  1980. 'div' => array('class' => 'input checkbox'),
  1981. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  1982. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1983. '/div'
  1984. );
  1985. $this->assertTags($result, $expected);
  1986. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1987. $expected = array(
  1988. 'div' => array('class' => 'input checkbox'),
  1989. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  1990. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1991. '/div'
  1992. );
  1993. $this->assertTags($result, $expected);
  1994. $result = $this->Form->input('User.disabled', array(
  1995. 'label' => 'Disabled',
  1996. 'type' => 'checkbox',
  1997. 'data-foo' => 'disabled'
  1998. ));
  1999. $expected = array(
  2000. 'div' => array('class' => 'input checkbox'),
  2001. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  2002. array('input' => array(
  2003. 'type' => 'checkbox',
  2004. 'name' => 'data[User][disabled]',
  2005. 'value' => '1',
  2006. 'id' => 'UserDisabled',
  2007. 'data-foo' => 'disabled'
  2008. )),
  2009. 'label' => array('for' => 'UserDisabled'),
  2010. 'Disabled',
  2011. '/label',
  2012. '/div'
  2013. );
  2014. $this->assertTags($result, $expected);
  2015. }
  2016. /**
  2017. * test form->input() with time types.
  2018. *
  2019. */
  2020. public function testInputTime() {
  2021. extract($this->dateRegex);
  2022. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2023. $result = explode(':', $result);
  2024. $this->assertRegExp('/option value="23"/', $result[0]);
  2025. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2026. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2027. $result = explode(':', $result);
  2028. $this->assertRegExp('/option value="23"/', $result[0]);
  2029. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2030. $result = $this->Form->input('Model.field', array(
  2031. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2032. ));
  2033. $result = explode(':', $result);
  2034. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2035. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2036. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2037. $result = $this->Form->input('Model.field', array(
  2038. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2039. ));
  2040. $result = explode(':', $result);
  2041. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2042. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2043. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2044. $result = $this->Form->input('prueba', array(
  2045. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2046. 'maxYear' => date('Y') + 1, 'interval' => 15
  2047. ));
  2048. $result = explode(':', $result);
  2049. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2050. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2051. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2052. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2053. $result = $this->Form->input('Random.start_time', array(
  2054. 'type' => 'time',
  2055. 'selected' => '18:15'
  2056. ));
  2057. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2058. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2059. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2060. $result = $this->Form->input('published', array('type' => 'time'));
  2061. $now = strtotime('now');
  2062. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2063. $now = strtotime('2013-03-09 00:42:21');
  2064. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2065. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2066. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2067. }
  2068. /**
  2069. * Test interval + selected near the hour roll over.
  2070. *
  2071. * @return void
  2072. */
  2073. public function testTimeSelectedWithInterval() {
  2074. $result = $this->Form->input('Model.start_time', array(
  2075. 'type' => 'time',
  2076. 'interval' => 15,
  2077. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2078. ));
  2079. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2080. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2081. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2082. $result = $this->Form->input('Model.start_time', array(
  2083. 'type' => 'time',
  2084. 'interval' => 15,
  2085. 'selected' => '2012-10-23 15:57:00'
  2086. ));
  2087. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2088. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2089. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2090. $result = $this->Form->input('Model.start_time', array(
  2091. 'timeFormat' => 24,
  2092. 'type' => 'time',
  2093. 'interval' => 15,
  2094. 'selected' => '15:57'
  2095. ));
  2096. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2097. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2098. $result = $this->Form->input('Model.start_time', array(
  2099. 'timeFormat' => 24,
  2100. 'type' => 'time',
  2101. 'interval' => 15,
  2102. 'selected' => '23:57'
  2103. ));
  2104. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2105. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2106. $result = $this->Form->input('Model.created', array(
  2107. 'timeFormat' => 24,
  2108. 'type' => 'datetime',
  2109. 'interval' => 15,
  2110. 'selected' => '2012-09-30 23:56'
  2111. ));
  2112. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2113. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2114. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2115. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2116. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2117. }
  2118. /**
  2119. * Test interval & timeFormat = 12
  2120. *
  2121. * @return void
  2122. */
  2123. public function testInputTimeWithIntervalAnd12HourFormat() {
  2124. $result = $this->Form->input('Model.start_time', array(
  2125. 'type' => 'time',
  2126. 'timeFormat' => 12,
  2127. 'interval' => 5,
  2128. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2129. ));
  2130. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2131. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2132. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2133. $result = $this->Form->input('Model.start_time', array(
  2134. 'type' => 'time',
  2135. 'timeFormat' => '12',
  2136. 'interval' => 5,
  2137. 'selected' => '2013-04-19 16:30:00'
  2138. ));
  2139. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2140. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2141. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2142. $result = $this->Form->input('Model.start_time', array(
  2143. 'type' => 'time',
  2144. 'timeFormat' => '12',
  2145. 'interval' => 10,
  2146. 'selected' => '2013-05-19 00:33:00'
  2147. ));
  2148. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2149. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2150. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2151. $result = $this->Form->input('Model.start_time', array(
  2152. 'type' => 'time',
  2153. 'timeFormat' => '12',
  2154. 'interval' => 10,
  2155. 'selected' => '2013-05-19 13:33:00'
  2156. ));
  2157. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2158. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2159. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2160. $result = $this->Form->input('Model.start_time', array(
  2161. 'type' => 'time',
  2162. 'timeFormat' => '12',
  2163. 'interval' => 10,
  2164. 'selected' => '2013-05-19 01:33:00'
  2165. ));
  2166. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2167. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2168. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2169. }
  2170. /**
  2171. * test form->input() with datetime, date and time types
  2172. *
  2173. * @return void
  2174. */
  2175. public function testInputDatetime() {
  2176. extract($this->dateRegex);
  2177. $result = $this->Form->input('prueba', array(
  2178. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2179. 'maxYear' => date('Y') + 1, 'interval' => 15
  2180. ));
  2181. $result = explode(':', $result);
  2182. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2183. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2184. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2185. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2186. //related to ticket #5013
  2187. $result = $this->Form->input('Contact.date', array(
  2188. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2189. ));
  2190. $this->assertRegExp('/class="customClass"/', $result);
  2191. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2192. $result = $this->Form->input('Contact.date', array(
  2193. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2194. ));
  2195. $this->assertRegExp('/id="customIdDay"/', $result);
  2196. $this->assertRegExp('/id="customIdMonth"/', $result);
  2197. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2198. $result = $this->Form->input('Model.field', array(
  2199. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2200. ));
  2201. $this->assertRegExp('/id="customIDDay"/', $result);
  2202. $this->assertRegExp('/id="customIDHour"/', $result);
  2203. $result = explode('</select><select', $result);
  2204. $result = explode(':', $result[1]);
  2205. $this->assertRegExp('/option value="23"/', $result[0]);
  2206. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2207. $result = $this->Form->input('Model.field', array(
  2208. 'type' => 'datetime', 'timeFormat' => 12
  2209. ));
  2210. $result = explode('</select><select', $result);
  2211. $result = explode(':', $result[1]);
  2212. $this->assertRegExp('/option value="12"/', $result[0]);
  2213. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2214. $this->Form->request->data = array('Contact' => array('created' => null));
  2215. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2216. $expected = array(
  2217. 'div' => array('class' => 'input date'),
  2218. 'label' => array('for' => 'ContactCreatedMonth'),
  2219. 'Created',
  2220. '/label',
  2221. array('select' => array('name' => 'Contact[created][month]', 'id' => 'ContactCreatedMonth')),
  2222. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2223. $monthsRegex,
  2224. '/select', '-',
  2225. array('select' => array('name' => 'Contact[created][day]', 'id' => 'ContactCreatedDay')),
  2226. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2227. $daysRegex,
  2228. '/select', '-',
  2229. array('select' => array('name' => 'Contact[created][year]', 'id' => 'ContactCreatedYear')),
  2230. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2231. $yearsRegex,
  2232. '/select',
  2233. '/div'
  2234. );
  2235. $this->assertTags($result, $expected);
  2236. $this->Form->request->data = array('Contact' => array('created' => null));
  2237. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2238. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2239. $this->Form->request->data = array('Contact' => array('created' => null));
  2240. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2241. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2242. $result = $this->Form->input('Contact.created', array(
  2243. 'type' => 'date',
  2244. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2245. 'timeFormat' => 'NONE'
  2246. ));
  2247. $this->assertRegExp('/for\="created-month"/', $result);
  2248. }
  2249. /**
  2250. * Test generating checkboxes in a loop.
  2251. *
  2252. * @return void
  2253. */
  2254. public function testInputCheckboxesInLoop() {
  2255. for ($i = 1; $i < 5; $i++) {
  2256. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2257. $expected = array(
  2258. 'div' => array('class' => 'input checkbox'),
  2259. 'input' => array('type' => 'hidden', 'name' => "Contact[{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2260. array('input' => array('type' => 'checkbox', 'name' => "Contact[{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2261. 'label' => array('for' => "Contact{$i}Email"),
  2262. 'Email',
  2263. '/label',
  2264. '/div'
  2265. );
  2266. $this->assertTags($result, $expected);
  2267. }
  2268. }
  2269. /**
  2270. * Test generating checkboxes with disabled elements.
  2271. *
  2272. * @return void
  2273. */
  2274. public function testInputCheckboxWithDisabledElements() {
  2275. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2276. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2277. $expected = array(
  2278. array('div' => array('class' => 'input select')),
  2279. array('label' => array('for' => "ContactMultiple")),
  2280. 'Multiple',
  2281. '/label',
  2282. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2283. array('div' => array('class' => 'checkbox')),
  2284. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2285. array('label' => array('for' => "ContactMultiple1")),
  2286. 'One',
  2287. '/label',
  2288. '/div',
  2289. array('div' => array('class' => 'checkbox')),
  2290. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2291. array('label' => array('for' => "ContactMultiple2")),
  2292. 'Two',
  2293. '/label',
  2294. '/div',
  2295. array('div' => array('class' => 'checkbox')),
  2296. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2297. array('label' => array('for' => "ContactMultiple3")),
  2298. 'Three',
  2299. '/label',
  2300. '/div',
  2301. '/div'
  2302. );
  2303. $this->assertTags($result, $expected);
  2304. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2305. $this->assertTags($result, $expected);
  2306. $disabled = array('2', 3);
  2307. $expected = array(
  2308. array('div' => array('class' => 'input select')),
  2309. array('label' => array('for' => "ContactMultiple")),
  2310. 'Multiple',
  2311. '/label',
  2312. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2313. array('div' => array('class' => 'checkbox')),
  2314. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2315. array('label' => array('for' => "ContactMultiple1")),
  2316. 'One',
  2317. '/label',
  2318. '/div',
  2319. array('div' => array('class' => 'checkbox')),
  2320. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2321. array('label' => array('for' => "ContactMultiple2")),
  2322. 'Two',
  2323. '/label',
  2324. '/div',
  2325. array('div' => array('class' => 'checkbox')),
  2326. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2327. array('label' => array('for' => "ContactMultiple3")),
  2328. 'Three',
  2329. '/label',
  2330. '/div',
  2331. '/div'
  2332. );
  2333. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2334. $this->assertTags($result, $expected);
  2335. // make sure 50 does only disable 50, and not 50f5c0cf
  2336. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2337. $disabled = array(50);
  2338. $expected = array(
  2339. array('div' => array('class' => 'input select')),
  2340. array('label' => array('for' => "ContactMultiple")),
  2341. 'Multiple',
  2342. '/label',
  2343. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2344. array('div' => array('class' => 'checkbox')),
  2345. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2346. array('label' => array('for' => "ContactMultiple50")),
  2347. 'Fifty',
  2348. '/label',
  2349. '/div',
  2350. array('div' => array('class' => 'checkbox')),
  2351. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2352. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2353. 'Stringy',
  2354. '/label',
  2355. '/div',
  2356. '/div'
  2357. );
  2358. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2359. $this->assertTags($result, $expected);
  2360. }
  2361. /**
  2362. * test input name with leading integer, ensure attributes are generated correctly.
  2363. *
  2364. * @return void
  2365. */
  2366. public function testInputWithLeadingInteger() {
  2367. $result = $this->Form->text('0.Node.title');
  2368. $expected = array(
  2369. 'input' => array('name' => '0[Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2370. );
  2371. $this->assertTags($result, $expected);
  2372. }
  2373. /**
  2374. * test form->input() with select type inputs.
  2375. *
  2376. * @return void
  2377. */
  2378. public function testInputSelectType() {
  2379. $result = $this->Form->input('email', array(
  2380. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2381. );
  2382. $expected = array(
  2383. 'div' => array('class' => 'input select'),
  2384. 'label' => array('for' => 'email'),
  2385. 'Email',
  2386. '/label',
  2387. array('select' => array('name' => 'email', 'id' => 'email')),
  2388. array('option' => array('value' => '')),
  2389. '/option',
  2390. array('option' => array('value' => 'è')),
  2391. 'Firést',
  2392. '/option',
  2393. array('option' => array('value' => 'é')),
  2394. 'Secoènd',
  2395. '/option',
  2396. '/select',
  2397. '/div'
  2398. );
  2399. $this->assertTags($result, $expected);
  2400. $result = $this->Form->input('email', array(
  2401. 'options' => array('First', 'Second'), 'empty' => true)
  2402. );
  2403. $expected = array(
  2404. 'div' => array('class' => 'input select'),
  2405. 'label' => array('for' => 'email'),
  2406. 'Email',
  2407. '/label',
  2408. array('select' => array('name' => 'email', 'id' => 'email')),
  2409. array('option' => array('value' => '')),
  2410. '/option',
  2411. array('option' => array('value' => '0')),
  2412. 'First',
  2413. '/option',
  2414. array('option' => array('value' => '1')),
  2415. 'Second',
  2416. '/option',
  2417. '/select',
  2418. '/div'
  2419. );
  2420. $this->assertTags($result, $expected);
  2421. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2422. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2423. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2424. $expected = array(
  2425. 'div' => array('class' => 'input select'),
  2426. 'label' => array('for' => 'ModelUserId'),
  2427. 'User',
  2428. '/label',
  2429. 'select' => array('name' => 'Model[user_id]', 'id' => 'ModelUserId'),
  2430. array('option' => array('value' => '')),
  2431. '/option',
  2432. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2433. 'good',
  2434. '/option',
  2435. array('option' => array('value' => 'other')),
  2436. 'bad',
  2437. '/option',
  2438. '/select',
  2439. '/div'
  2440. );
  2441. $this->assertTags($result, $expected);
  2442. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2443. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2444. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2445. $expected = array(
  2446. 'div' => array('class' => 'input select'),
  2447. 'label' => array('for' => 'ThingUserId'),
  2448. 'User',
  2449. '/label',
  2450. 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'),
  2451. array('option' => array('value' => '')),
  2452. 'Some Empty',
  2453. '/option',
  2454. array('option' => array('value' => 'value')),
  2455. 'good',
  2456. '/option',
  2457. array('option' => array('value' => 'other')),
  2458. 'bad',
  2459. '/option',
  2460. '/select',
  2461. '/div'
  2462. );
  2463. $this->assertTags($result, $expected);
  2464. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2465. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2466. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2467. $expected = array(
  2468. 'div' => array('class' => 'input select'),
  2469. 'label' => array('for' => 'ThingUserId'),
  2470. 'User',
  2471. '/label',
  2472. 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'),
  2473. array('option' => array('value' => '')),
  2474. 'Some Empty',
  2475. '/option',
  2476. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2477. 'good',
  2478. '/option',
  2479. array('option' => array('value' => 'other')),
  2480. 'bad',
  2481. '/option',
  2482. '/select',
  2483. '/div'
  2484. );
  2485. $this->assertTags($result, $expected);
  2486. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2487. $this->Form->request->data = array('User' => array('User' => array('value')));
  2488. $result = $this->Form->input('User.User', array('empty' => true));
  2489. $expected = array(
  2490. 'div' => array('class' => 'input select'),
  2491. 'label' => array('for' => 'UserUser'),
  2492. 'User',
  2493. '/label',
  2494. 'input' => array('type' => 'hidden', 'name' => 'User[User]', 'value' => '', 'id' => 'UserUser_'),
  2495. 'select' => array('name' => 'User[User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2496. array('option' => array('value' => '')),
  2497. '/option',
  2498. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2499. 'good',
  2500. '/option',
  2501. array('option' => array('value' => 'other')),
  2502. 'bad',
  2503. '/option',
  2504. '/select',
  2505. '/div'
  2506. );
  2507. $this->assertTags($result, $expected);
  2508. $this->Form->data = array();
  2509. $result = $this->Form->input('Publisher.id', array(
  2510. 'label' => 'Publisher',
  2511. 'type' => 'select',
  2512. 'multiple' => 'checkbox',
  2513. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2514. ));
  2515. $expected = array(
  2516. array('div' => array('class' => 'input select')),
  2517. array('label' => array('for' => 'PublisherId')),
  2518. 'Publisher',
  2519. '/label',
  2520. 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'PublisherId'),
  2521. array('div' => array('class' => 'checkbox')),
  2522. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2523. array('label' => array('for' => 'PublisherIdValue1')),
  2524. 'Label 1',
  2525. '/label',
  2526. '/div',
  2527. array('div' => array('class' => 'checkbox')),
  2528. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2529. array('label' => array('for' => 'PublisherIdValue2')),
  2530. 'Label 2',
  2531. '/label',
  2532. '/div',
  2533. '/div'
  2534. );
  2535. $this->assertTags($result, $expected);
  2536. }
  2537. /**
  2538. * test that input() and a non standard primary key makes a hidden input by default.
  2539. *
  2540. * @return void
  2541. */
  2542. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2543. $this->Form->create('User');
  2544. $this->Form->fieldset = array(
  2545. 'User' => array(
  2546. 'fields' => array(
  2547. 'model_id' => array('type' => 'integer')
  2548. ),
  2549. 'validates' => array(),
  2550. 'key' => 'model_id'
  2551. )
  2552. );
  2553. $result = $this->Form->input('model_id');
  2554. $expected = array(
  2555. 'input' => array('type' => 'hidden', 'name' => 'User[model_id]', 'id' => 'UserModelId'),
  2556. );
  2557. $this->assertTags($result, $expected);
  2558. }
  2559. /**
  2560. * test that overriding the magic select type widget is possible
  2561. *
  2562. * @return void
  2563. */
  2564. public function testInputOverridingMagicSelectType() {
  2565. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2566. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2567. $expected = array(
  2568. 'div' => array('class' => 'input text'),
  2569. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2570. 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2571. '/div'
  2572. );
  2573. $this->assertTags($result, $expected);
  2574. //Check that magic types still work for plural/singular vars
  2575. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2576. $result = $this->Form->input('Model.type');
  2577. $expected = array(
  2578. 'div' => array('class' => 'input select'),
  2579. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2580. 'select' => array('name' => 'Model[type]', 'id' => 'ModelType'),
  2581. array('option' => array('value' => 'value')), 'good', '/option',
  2582. array('option' => array('value' => 'other')), 'bad', '/option',
  2583. '/select',
  2584. '/div'
  2585. );
  2586. $this->assertTags($result, $expected);
  2587. }
  2588. /**
  2589. * Test that magic input() selects are created for type=number
  2590. *
  2591. * @return void
  2592. */
  2593. public function testInputMagicSelectForTypeNumber() {
  2594. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2595. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2596. $result = $this->Form->input('ValidateUser.balance');
  2597. $expected = array(
  2598. 'div' => array('class' => 'input select'),
  2599. 'label' => array('for' => 'ValidateUserBalance'),
  2600. 'Balance',
  2601. '/label',
  2602. 'select' => array('name' => 'ValidateUser[balance]', 'id' => 'ValidateUserBalance'),
  2603. array('option' => array('value' => '0')),
  2604. 'nothing',
  2605. '/option',
  2606. array('option' => array('value' => '1', 'selected' => 'selected')),
  2607. 'some',
  2608. '/option',
  2609. array('option' => array('value' => '100')),
  2610. 'a lot',
  2611. '/option',
  2612. '/select',
  2613. '/div'
  2614. );
  2615. $this->assertTags($result, $expected);
  2616. }
  2617. /**
  2618. * Test that magic input() selects can easily be converted into radio types without error.
  2619. *
  2620. * @return void
  2621. */
  2622. public function testInputMagicSelectChangeToRadio() {
  2623. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2624. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2625. $this->assertRegExp('/input type="radio"/', $result);
  2626. }
  2627. /**
  2628. * fields with the same name as the model should work.
  2629. *
  2630. * @return void
  2631. */
  2632. public function testInputWithMatchingFieldAndModelName() {
  2633. $this->Form->create('User');
  2634. $this->Form->fieldset = array(
  2635. 'User' => array(
  2636. 'fields' => array(
  2637. 'User' => array('type' => 'text')
  2638. ),
  2639. 'validates' => array(),
  2640. 'key' => 'id'
  2641. )
  2642. );
  2643. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2644. $result = $this->Form->input('User', array('type' => 'text'));
  2645. $expected = array(
  2646. 'div' => array('class' => 'input text'),
  2647. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2648. 'input' => array('name' => 'User[User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2649. '/div'
  2650. );
  2651. $this->assertTags($result, $expected);
  2652. }
  2653. /**
  2654. * testFormInputs method
  2655. *
  2656. * test correct results from form::inputs().
  2657. *
  2658. * @return void
  2659. */
  2660. public function testFormInputs() {
  2661. $this->Form->create('Cake\Test\TestCase\View\Helper\Contact');
  2662. $result = $this->Form->inputs('The Legend');
  2663. $expected = array(
  2664. '<fieldset',
  2665. '<legend',
  2666. 'The Legend',
  2667. '/legend',
  2668. '*/fieldset',
  2669. );
  2670. $this->assertTags($result, $expected);
  2671. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2672. $expected = array(
  2673. 'fieldset' => array('class' => 'classy-stuff'),
  2674. '<legend',
  2675. 'Field of Dreams',
  2676. '/legend',
  2677. '*/fieldset'
  2678. );
  2679. $this->assertTags($result, $expected);
  2680. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2681. $this->assertTags($result, $expected);
  2682. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2683. $this->assertTags($result, $expected);
  2684. $this->Form->create('Contact');
  2685. $this->Form->request['prefix'] = 'admin';
  2686. $this->Form->request['action'] = 'admin_edit';
  2687. $result = $this->Form->inputs();
  2688. $expected = array(
  2689. '<fieldset',
  2690. '<legend',
  2691. 'Edit Contact',
  2692. '/legend',
  2693. '*/fieldset',
  2694. );
  2695. $this->assertTags($result, $expected);
  2696. $this->Form->create('Contact');
  2697. $result = $this->Form->inputs(false);
  2698. $expected = array(
  2699. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2700. array('div' => array('class' => 'input text')),
  2701. '*/div',
  2702. array('div' => array('class' => 'input email')),
  2703. '*/div',
  2704. array('div' => array('class' => 'input tel')),
  2705. '*/div',
  2706. array('div' => array('class' => 'input password')),
  2707. '*/div',
  2708. array('div' => array('class' => 'input date')),
  2709. '*/div',
  2710. array('div' => array('class' => 'input date')),
  2711. '*/div',
  2712. array('div' => array('class' => 'input datetime')),
  2713. '*/div',
  2714. array('div' => array('class' => 'input number')),
  2715. '*/div',
  2716. array('div' => array('class' => 'input select')),
  2717. '*/div',
  2718. );
  2719. $this->assertTags($result, $expected);
  2720. $this->Form->create('Contact');
  2721. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2722. $expected = array(
  2723. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2724. array('div' => array('class' => 'input text')),
  2725. '*/div',
  2726. array('div' => array('class' => 'input email')),
  2727. '*/div',
  2728. array('div' => array('class' => 'input tel')),
  2729. '*/div',
  2730. array('div' => array('class' => 'input password')),
  2731. '*/div',
  2732. array('div' => array('class' => 'input date')),
  2733. '*/div',
  2734. array('div' => array('class' => 'input date')),
  2735. '*/div',
  2736. array('div' => array('class' => 'input datetime')),
  2737. '*/div',
  2738. array('div' => array('class' => 'input number')),
  2739. '*/div',
  2740. array('div' => array('class' => 'input select')),
  2741. '*/div',
  2742. );
  2743. $this->assertTags($result, $expected);
  2744. $this->Form->create('Contact');
  2745. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2746. $this->assertTags($result, $expected);
  2747. $this->Form->create('Contact');
  2748. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2749. $expected = array(
  2750. 'fieldset' => array(),
  2751. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2752. array('div' => array('class' => 'input text')),
  2753. '*/div',
  2754. array('div' => array('class' => 'input email')),
  2755. '*/div',
  2756. array('div' => array('class' => 'input tel')),
  2757. '*/div',
  2758. array('div' => array('class' => 'input password')),
  2759. '*/div',
  2760. array('div' => array('class' => 'input date')),
  2761. '*/div',
  2762. array('div' => array('class' => 'input date')),
  2763. '*/div',
  2764. array('div' => array('class' => 'input datetime')),
  2765. '*/div',
  2766. array('div' => array('class' => 'input number')),
  2767. '*/div',
  2768. array('div' => array('class' => 'input select')),
  2769. '*/div',
  2770. '/fieldset'
  2771. );
  2772. $this->assertTags($result, $expected);
  2773. $this->Form->create('Contact');
  2774. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2775. $expected = array(
  2776. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2777. array('div' => array('class' => 'input text')),
  2778. '*/div',
  2779. array('div' => array('class' => 'input email')),
  2780. '*/div',
  2781. array('div' => array('class' => 'input tel')),
  2782. '*/div',
  2783. array('div' => array('class' => 'input password')),
  2784. '*/div',
  2785. array('div' => array('class' => 'input date')),
  2786. '*/div',
  2787. array('div' => array('class' => 'input date')),
  2788. '*/div',
  2789. array('div' => array('class' => 'input datetime')),
  2790. '*/div',
  2791. array('div' => array('class' => 'input number')),
  2792. '*/div',
  2793. array('div' => array('class' => 'input select')),
  2794. '*/div',
  2795. );
  2796. $this->assertTags($result, $expected);
  2797. $this->Form->create('Contact');
  2798. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2799. $this->assertTags($result, $expected);
  2800. $this->Form->create('Contact');
  2801. $result = $this->Form->inputs('Hello');
  2802. $expected = array(
  2803. 'fieldset' => array(),
  2804. 'legend' => array(),
  2805. 'Hello',
  2806. '/legend',
  2807. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2808. array('div' => array('class' => 'input text')),
  2809. '*/div',
  2810. array('div' => array('class' => 'input email')),
  2811. '*/div',
  2812. array('div' => array('class' => 'input tel')),
  2813. '*/div',
  2814. array('div' => array('class' => 'input password')),
  2815. '*/div',
  2816. array('div' => array('class' => 'input date')),
  2817. '*/div',
  2818. array('div' => array('class' => 'input date')),
  2819. '*/div',
  2820. array('div' => array('class' => 'input datetime')),
  2821. '*/div',
  2822. array('div' => array('class' => 'input number')),
  2823. '*/div',
  2824. array('div' => array('class' => 'input select')),
  2825. '*/div',
  2826. '/fieldset'
  2827. );
  2828. $this->assertTags($result, $expected);
  2829. $this->Form->create('Contact');
  2830. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2831. $expected = array(
  2832. 'fieldset' => array(),
  2833. 'legend' => array(),
  2834. 'Hello',
  2835. '/legend',
  2836. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2837. array('div' => array('class' => 'input text')),
  2838. '*/div',
  2839. array('div' => array('class' => 'input email')),
  2840. '*/div',
  2841. array('div' => array('class' => 'input tel')),
  2842. '*/div',
  2843. array('div' => array('class' => 'input password')),
  2844. '*/div',
  2845. array('div' => array('class' => 'input date')),
  2846. '*/div',
  2847. array('div' => array('class' => 'input date')),
  2848. '*/div',
  2849. array('div' => array('class' => 'input datetime')),
  2850. '*/div',
  2851. array('div' => array('class' => 'input number')),
  2852. '*/div',
  2853. array('div' => array('class' => 'input select')),
  2854. '*/div',
  2855. '/fieldset'
  2856. );
  2857. $this->assertTags($result, $expected);
  2858. $this->Form->create('Contact');
  2859. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2860. $this->assertTags($result, $expected);
  2861. $this->Form->end();
  2862. $this->Form->create(false);
  2863. $expected = array(
  2864. 'fieldset' => array(),
  2865. array('div' => array('class' => 'input text')),
  2866. 'label' => array('for' => 'foo'),
  2867. 'Foo',
  2868. '/label',
  2869. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  2870. '*/div',
  2871. '/fieldset'
  2872. );
  2873. $result = $this->Form->inputs(
  2874. array('foo' => array('type' => 'text')),
  2875. array(),
  2876. array('legend' => false)
  2877. );
  2878. $this->assertTags($result, $expected);
  2879. }
  2880. /**
  2881. * Tests inputs() works with plugin models
  2882. *
  2883. * @return void
  2884. */
  2885. public function testInputsPluginModel() {
  2886. $this->loadFixtures('Post');
  2887. Plugin::load('TestPlugin');
  2888. $this->Form->request['models'] = array(
  2889. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2890. );
  2891. $this->Form->create('TestPlugin.TestPluginPost');
  2892. $result = $this->Form->inputs();
  2893. $this->assertContains('TestPluginPost[id]', $result);
  2894. $this->assertContains('TestPluginPost[author_id]', $result);
  2895. $this->assertContains('TestPluginPost[title]', $result);
  2896. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2897. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2898. $this->assertEquals('TestPluginPost', $this->Form->model());
  2899. }
  2900. /**
  2901. * testSelectAsCheckbox method
  2902. *
  2903. * test multi-select widget with checkbox formatting.
  2904. *
  2905. * @return void
  2906. */
  2907. public function testSelectAsCheckbox() {
  2908. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2909. $expected = array(
  2910. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2911. array('div' => array('class' => 'checkbox')),
  2912. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2913. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2914. 'first',
  2915. '/label',
  2916. '/div',
  2917. array('div' => array('class' => 'checkbox')),
  2918. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2919. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2920. 'second',
  2921. '/label',
  2922. '/div',
  2923. array('div' => array('class' => 'checkbox')),
  2924. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2925. array('label' => array('for' => 'ModelMultiField2')),
  2926. 'third',
  2927. '/label',
  2928. '/div',
  2929. );
  2930. $this->assertTags($result, $expected);
  2931. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2932. $expected = array(
  2933. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2934. array('div' => array('class' => 'checkbox')),
  2935. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2936. array('label' => array('for' => 'ModelMultiField12')),
  2937. 'half',
  2938. '/label',
  2939. '/div',
  2940. );
  2941. $this->assertTags($result, $expected);
  2942. }
  2943. /**
  2944. * testLabel method
  2945. *
  2946. * test label generation.
  2947. *
  2948. * @return void
  2949. */
  2950. public function testLabel() {
  2951. $this->Form->text('Person.name');
  2952. $result = $this->Form->label();
  2953. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2954. $this->Form->text('Person.name');
  2955. $result = $this->Form->label();
  2956. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2957. $result = $this->Form->label('Person.first_name');
  2958. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2959. $result = $this->Form->label('Person.first_name', 'Your first name');
  2960. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2961. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2962. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2963. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2964. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2965. $result = $this->Form->label('Person.first_name', '');
  2966. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2967. $result = $this->Form->label('Person.2.name', '');
  2968. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2969. }
  2970. /**
  2971. * testTextbox method
  2972. *
  2973. * test textbox element generation
  2974. *
  2975. * @return void
  2976. */
  2977. public function testTextbox() {
  2978. $result = $this->Form->text('Model.field');
  2979. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'ModelField')));
  2980. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2981. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]', 'id' => 'ModelField')));
  2982. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2983. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID')));
  2984. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2985. $result = $this->Form->text('Model.text');
  2986. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2987. $Contact = ClassRegistry::getObject('Contact');
  2988. $Contact->validationErrors['text'] = array(true);
  2989. $this->Form->request->data['Contact']['text'] = 'test';
  2990. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2991. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Contact[text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2992. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2993. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2994. $expected = array(
  2995. 'input' => array('type' => 'text', 'name' => 'Model[0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2996. );
  2997. $this->assertTags($result, $expected);
  2998. }
  2999. /**
  3000. * testDefaultValue method
  3001. *
  3002. * Test default value setting
  3003. *
  3004. * @return void
  3005. */
  3006. public function testDefaultValue() {
  3007. $this->Form->request->data['Model']['field'] = 'test';
  3008. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3009. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'value' => 'test', 'id' => 'ModelField')));
  3010. unset($this->Form->request->data['Model']['field']);
  3011. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3012. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'value' => 'default value', 'id' => 'ModelField')));
  3013. }
  3014. /**
  3015. * testCheckboxDefaultValue method
  3016. *
  3017. * Test default value setting on checkbox() method
  3018. *
  3019. * @return void
  3020. */
  3021. public function testCheckboxDefaultValue() {
  3022. $this->Form->request->data['Model']['field'] = false;
  3023. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3024. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField')));
  3025. unset($this->Form->request->data['Model']['field']);
  3026. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3027. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3028. $this->Form->request->data['Model']['field'] = true;
  3029. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3030. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3031. unset($this->Form->request->data['Model']['field']);
  3032. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3033. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField')));
  3034. }
  3035. /**
  3036. * testError method
  3037. *
  3038. * Test field error generation
  3039. *
  3040. * @return void
  3041. */
  3042. public function testError() {
  3043. $Contact = ClassRegistry::getObject('Contact');
  3044. $Contact->validationErrors['field'] = array(1);
  3045. $result = $this->Form->error('Contact.field');
  3046. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3047. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3048. $this->assertEquals('Error in field Field', $result);
  3049. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3050. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3051. $this->assertEquals('This field contains invalid input', $result);
  3052. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3053. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3054. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3055. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3056. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3057. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3058. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3059. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3060. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3061. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3062. $this->assertEquals('<strong>Badness!</strong>', $result);
  3063. $Contact->validationErrors['field'] = array("email");
  3064. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3065. $expected = array(
  3066. 'div' => array('class' => 'field-error'),
  3067. 'No good!',
  3068. '/div'
  3069. );
  3070. $this->assertTags($result, $expected);
  3071. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3072. $result = $this->Form->error('Contact.field', array(
  3073. 'notEmpty' => 'Cannot be empty',
  3074. 'email' => 'No good!'
  3075. ));
  3076. $expected = array(
  3077. 'div' => array('class' => 'error-message'),
  3078. 'ul' => array(),
  3079. '<li', 'Cannot be empty', '/li',
  3080. '<li', 'No good!', '/li',
  3081. '<li', 'Something else', '/li',
  3082. '/ul',
  3083. '/div'
  3084. );
  3085. $this->assertTags($result, $expected);
  3086. // Testing error messages list options
  3087. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3088. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3089. $expected = array(
  3090. 'div' => array('class' => 'error-message'),
  3091. 'ol' => array(),
  3092. '<li', 'notEmpty', '/li',
  3093. '<li', 'email', '/li',
  3094. '/ol',
  3095. '/div'
  3096. );
  3097. $this->assertTags($result, $expected);
  3098. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3099. $expected = array(
  3100. 'div' => array('class' => 'error-message'),
  3101. 'ol' => array(),
  3102. '<li', 'notEmpty', '/li',
  3103. '<li', 'email', '/li',
  3104. '/ol',
  3105. '/div'
  3106. );
  3107. $this->assertTags($result, $expected);
  3108. $result = $this->Form->error('Contact.field', null, array(
  3109. 'listOptions' => array(
  3110. 'class' => 'ul-class',
  3111. 'itemOptions' => array(
  3112. 'class' => 'li-class'
  3113. )
  3114. )
  3115. ));
  3116. $expected = array(
  3117. 'div' => array('class' => 'error-message'),
  3118. 'ul' => array('class' => 'ul-class'),
  3119. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3120. array('li' => array('class' => 'li-class')), 'email', '/li',
  3121. '/ul',
  3122. '/div'
  3123. );
  3124. $this->assertTags($result, $expected);
  3125. }
  3126. /**
  3127. * test error options when using form->input();
  3128. *
  3129. * @return void
  3130. */
  3131. public function testInputErrorEscape() {
  3132. $this->Form->create('ValidateProfile');
  3133. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3134. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3135. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3136. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3137. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3138. $this->assertRegExp('/required<br>/', $result);
  3139. }
  3140. /**
  3141. * testPassword method
  3142. *
  3143. * Test password element generation
  3144. *
  3145. * @return void
  3146. */
  3147. public function testPassword() {
  3148. $Contact = ClassRegistry::getObject('Contact');
  3149. $result = $this->Form->password('Contact.field');
  3150. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]', 'id' => 'ContactField')));
  3151. $Contact->validationErrors['passwd'] = 1;
  3152. $this->Form->request->data['Contact']['passwd'] = 'test';
  3153. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3154. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3155. }
  3156. /**
  3157. * testRadio method
  3158. *
  3159. * Test radio element set generation
  3160. *
  3161. * @return void
  3162. */
  3163. public function testRadio() {
  3164. $result = $this->Form->radio('Model.field', array('option A'));
  3165. $expected = array(
  3166. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  3167. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')),
  3168. 'label' => array('for' => 'ModelField0'),
  3169. 'option A',
  3170. '/label'
  3171. );
  3172. $this->assertTags($result, $expected);
  3173. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3174. $expected = array(
  3175. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  3176. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3177. 'label' => array('for' => 'ModelField12'),
  3178. 'half',
  3179. '/label'
  3180. );
  3181. $this->assertTags($result, $expected);
  3182. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3183. $expected = array(
  3184. 'fieldset' => array(),
  3185. 'legend' => array(),
  3186. 'Field',
  3187. '/legend',
  3188. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  3189. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')),
  3190. array('label' => array('for' => 'ModelField0')),
  3191. 'option A',
  3192. '/label',
  3193. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3194. array('label' => array('for' => 'ModelField1')),
  3195. 'option B',
  3196. '/label',
  3197. '/fieldset'
  3198. );
  3199. $this->assertTags($result, $expected);
  3200. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3201. $expected = array(
  3202. 'fieldset' => array(),
  3203. 'legend' => array(),
  3204. 'Field',
  3205. '/legend',
  3206. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  3207. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')),
  3208. array('label' => array('for' => 'ModelField0')),
  3209. 'option A',
  3210. '/label',
  3211. 'br' => array(),
  3212. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3213. array('label' => array('for' => 'ModelField1')),
  3214. 'option B',
  3215. '/label',
  3216. '/fieldset'
  3217. );
  3218. $this->assertTags($result, $expected);
  3219. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3220. $expected = array(
  3221. 'fieldset' => array(),
  3222. 'legend' => array(),
  3223. 'Gender',
  3224. '/legend',
  3225. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3226. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3227. array('label' => array('for' => 'EmployeeGenderMale')),
  3228. 'Male',
  3229. '/label',
  3230. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3231. array('label' => array('for' => 'EmployeeGenderFemale')),
  3232. 'Female',
  3233. '/label',
  3234. '/fieldset',
  3235. );
  3236. $this->assertTags($result, $expected);
  3237. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3238. $expected = array(
  3239. 'fieldset' => array(),
  3240. 'legend' => array(),
  3241. 'Gender',
  3242. '/legend',
  3243. 'input' => array('type' => 'hidden', 'name' => 'Officer[gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3244. array('input' => array('type' => 'radio', 'name' => 'Officer[gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3245. array('label' => array('for' => 'OfficerGenderMale')),
  3246. 'Male',
  3247. '/label',
  3248. array('input' => array('type' => 'radio', 'name' => 'Officer[gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3249. array('label' => array('for' => 'OfficerGenderFemale')),
  3250. 'Female',
  3251. '/label',
  3252. '/fieldset',
  3253. );
  3254. $this->assertTags($result, $expected);
  3255. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3256. $expected = array(
  3257. 'input' => array('type' => 'hidden', 'name' => 'Contact[1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3258. array('input' => array('type' => 'radio', 'name' => 'Contact[1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0', 'required' => 'required')),
  3259. 'label' => array('for' => 'Contact1Imrequired0'),
  3260. 'option A',
  3261. '/label'
  3262. );
  3263. $this->assertTags($result, $expected);
  3264. $result = $this->Form->radio('Model.1.field', array('option A'));
  3265. $expected = array(
  3266. 'input' => array('type' => 'hidden', 'name' => 'Model[1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3267. array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3268. 'label' => array('for' => 'Model1Field0'),
  3269. 'option A',
  3270. '/label'
  3271. );
  3272. $this->assertTags($result, $expected);
  3273. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  3274. $expected = array(
  3275. 'fieldset' => array(),
  3276. 'legend' => array(),
  3277. 'Field',
  3278. '/legend',
  3279. 'input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '', 'id' => 'ModelField_'),
  3280. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'ModelField0')),
  3281. array('label' => array('for' => 'ModelField0')),
  3282. 'option A',
  3283. '/label',
  3284. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'ModelField1')),
  3285. array('label' => array('for' => 'ModelField1')),
  3286. 'option B',
  3287. '/label',
  3288. '/fieldset'
  3289. );
  3290. $this->assertTags($result, $expected);
  3291. }
  3292. /**
  3293. * Test radio inputs with between as string or array. Also ensure
  3294. * that an array with less between elements works.
  3295. *
  3296. * @return void
  3297. */
  3298. public function testRadioBetween() {
  3299. $result = $this->Form->radio(
  3300. 'Model.field',
  3301. array('option A', 'option B'),
  3302. array('between' => 'I am between')
  3303. );
  3304. $expected = array(
  3305. 'fieldset' => array(),
  3306. 'legend' => array(),
  3307. 'Field',
  3308. '/legend',
  3309. 'I am between',
  3310. 'input' => array(
  3311. 'type' => 'hidden', 'name' => 'Model[field]',
  3312. 'value' => '', 'id' => 'ModelField_'
  3313. ),
  3314. array('input' => array(
  3315. 'type' => 'radio', 'name' => 'Model[field]',
  3316. 'value' => '0', 'id' => 'ModelField0'
  3317. )),
  3318. array('label' => array('for' => 'ModelField0')),
  3319. 'option A',
  3320. '/label',
  3321. array('input' => array(
  3322. 'type' => 'radio', 'name' => 'Model[field]',
  3323. 'value' => '1', 'id' => 'ModelField1'
  3324. )),
  3325. array('label' => array('for' => 'ModelField1')),
  3326. 'option B',
  3327. '/label',
  3328. '/fieldset'
  3329. );
  3330. $this->assertTags($result, $expected);
  3331. $result = $this->Form->radio(
  3332. 'Model.field',
  3333. array('option A', 'option B', 'option C'),
  3334. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3335. );
  3336. $expected = array(
  3337. 'fieldset' => array(),
  3338. 'legend' => array(),
  3339. 'Field',
  3340. '/legend',
  3341. 'input' => array(
  3342. 'type' => 'hidden', 'name' => 'Model[field]',
  3343. 'value' => '', 'id' => 'ModelField_'
  3344. ),
  3345. array('input' => array(
  3346. 'type' => 'radio', 'name' => 'Model[field]',
  3347. 'value' => '0', 'id' => 'ModelField0'
  3348. )),
  3349. array('label' => array('for' => 'ModelField0')),
  3350. 'option A',
  3351. '/label',
  3352. 'between A',
  3353. '--separator--',
  3354. array('input' => array(
  3355. 'type' => 'radio', 'name' => 'Model[field]',
  3356. 'value' => '1', 'id' => 'ModelField1'
  3357. )),
  3358. array('label' => array('for' => 'ModelField1')),
  3359. 'option B',
  3360. '/label',
  3361. 'between B',
  3362. '--separator--',
  3363. array('input' => array(
  3364. 'type' => 'radio', 'name' => 'Model[field]',
  3365. 'value' => '2', 'id' => 'ModelField2'
  3366. )),
  3367. array('label' => array('for' => 'ModelField2')),
  3368. 'option C',
  3369. '/label',
  3370. 'between C',
  3371. '/fieldset'
  3372. );
  3373. $this->assertTags($result, $expected);
  3374. $result = $this->Form->input('Model.field', array(
  3375. 'options' => array('1' => 'first', '2' => 'second'),
  3376. 'type' => 'radio',
  3377. 'before' => '--before--',
  3378. 'after' => '--after--',
  3379. 'separator' => '--separator--',
  3380. 'between' => array('--between first--', '--between second--')
  3381. ));
  3382. $expected = array(
  3383. 'div' => array('class' => 'input radio'),
  3384. '--before--',
  3385. 'fieldset' => array(),
  3386. 'legend' => array(),
  3387. 'Field',
  3388. '/legend',
  3389. array('input' => array('type' => 'hidden', 'name' => 'Model[field]', 'id' => 'ModelField_', 'value' => '')),
  3390. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3391. array('label' => array('for' => 'ModelField1')),
  3392. 'first',
  3393. '/label',
  3394. '--between first--',
  3395. '--separator--',
  3396. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '2', 'id' => 'ModelField2')),
  3397. array('label' => array('for' => 'ModelField2')),
  3398. 'second',
  3399. '/label',
  3400. '--between second--',
  3401. '/fieldset',
  3402. '--after--',
  3403. '/div'
  3404. );
  3405. $this->assertTags($result, $expected);
  3406. $result = $this->Form->input('Model.field', array(
  3407. 'options' => array('1' => 'first', '2' => 'second'),
  3408. 'type' => 'radio',
  3409. 'before' => '--before--',
  3410. 'after' => '--after--',
  3411. 'separator' => '--separator--',
  3412. 'between' => array('--between first--')
  3413. ));
  3414. $expected = array(
  3415. 'div' => array('class' => 'input radio'),
  3416. '--before--',
  3417. 'fieldset' => array(),
  3418. 'legend' => array(),
  3419. 'Field',
  3420. '/legend',
  3421. array('input' => array('type' => 'hidden', 'name' => 'Model[field]', 'id' => 'ModelField_', 'value' => '')),
  3422. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3423. array('label' => array('for' => 'ModelField1')),
  3424. 'first',
  3425. '/label',
  3426. '--between first--',
  3427. '--separator--',
  3428. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '2', 'id' => 'ModelField2')),
  3429. array('label' => array('for' => 'ModelField2')),
  3430. 'second',
  3431. '/label',
  3432. '/fieldset',
  3433. '--after--',
  3434. '/div'
  3435. );
  3436. $this->assertTags($result, $expected);
  3437. }
  3438. /**
  3439. * Test that radios with a 0 value are selected under the correct conditions.
  3440. * Also ensure that values that are booleanish are handled correctly.
  3441. *
  3442. * @return void
  3443. */
  3444. public function testRadioOptionWithBooleanishValues() {
  3445. $expected = array(
  3446. 'fieldset' => array(),
  3447. 'legend' => array(),
  3448. 'Field',
  3449. '/legend',
  3450. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3451. array('label' => array('for' => 'ModelField1')),
  3452. 'Yes',
  3453. '/label',
  3454. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3455. array('label' => array('for' => 'ModelField0')),
  3456. 'No',
  3457. '/label',
  3458. '/fieldset'
  3459. );
  3460. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3461. $this->assertTags($result, $expected);
  3462. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3463. $this->assertTags($result, $expected);
  3464. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3465. $this->assertTags($result, $expected);
  3466. $expected = array(
  3467. 'fieldset' => array(),
  3468. 'legend' => array(),
  3469. 'Field',
  3470. '/legend',
  3471. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  3472. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3473. array('label' => array('for' => 'ModelField1')),
  3474. 'Yes',
  3475. '/label',
  3476. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')),
  3477. array('label' => array('for' => 'ModelField0')),
  3478. 'No',
  3479. '/label',
  3480. '/fieldset'
  3481. );
  3482. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3483. $this->assertTags($result, $expected);
  3484. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3485. $this->assertTags($result, $expected);
  3486. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3487. $this->assertTags($result, $expected);
  3488. $expected = array(
  3489. 'fieldset' => array(),
  3490. 'legend' => array(),
  3491. 'Field',
  3492. '/legend',
  3493. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3494. array('label' => array('for' => 'ModelField1')),
  3495. 'Yes',
  3496. '/label',
  3497. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')),
  3498. array('label' => array('for' => 'ModelField0')),
  3499. 'No',
  3500. '/label',
  3501. '/fieldset'
  3502. );
  3503. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3504. $this->assertTags($result, $expected);
  3505. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3506. $this->assertTags($result, $expected);
  3507. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3508. $this->assertTags($result, $expected);
  3509. }
  3510. /**
  3511. * test disabled radio options
  3512. *
  3513. * @return void
  3514. */
  3515. public function testRadioDisabled() {
  3516. $result = $this->Form->radio(
  3517. 'Model.field',
  3518. array('option A', 'option B'),
  3519. array('disabled' => array(0), 'value' => '0')
  3520. );
  3521. $expected = array(
  3522. 'fieldset' => array(),
  3523. 'legend' => array(),
  3524. 'Field',
  3525. '/legend',
  3526. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3527. array('label' => array('for' => 'ModelField0')),
  3528. 'option A',
  3529. '/label',
  3530. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3531. array('label' => array('for' => 'ModelField1')),
  3532. 'option B',
  3533. '/label',
  3534. '/fieldset'
  3535. );
  3536. $this->assertTags($result, $expected);
  3537. $result = $this->Form->radio(
  3538. 'Model.field',
  3539. array('option A', 'option B'),
  3540. array('disabled' => true, 'value' => '0')
  3541. );
  3542. $expected = array(
  3543. 'fieldset' => array(),
  3544. 'legend' => array(),
  3545. 'Field',
  3546. '/legend',
  3547. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3548. array('label' => array('for' => 'ModelField0')),
  3549. 'option A',
  3550. '/label',
  3551. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3552. array('label' => array('for' => 'ModelField1')),
  3553. 'option B',
  3554. '/label',
  3555. '/fieldset'
  3556. );
  3557. $this->assertTags($result, $expected);
  3558. $result = $this->Form->radio(
  3559. 'Model.field',
  3560. array('option A', 'option B'),
  3561. array('disabled' => 'disabled', 'value' => '0')
  3562. );
  3563. $expected = array(
  3564. 'fieldset' => array(),
  3565. 'legend' => array(),
  3566. 'Field',
  3567. '/legend',
  3568. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3569. array('label' => array('for' => 'ModelField0')),
  3570. 'option A',
  3571. '/label',
  3572. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3573. array('label' => array('for' => 'ModelField1')),
  3574. 'option B',
  3575. '/label',
  3576. '/fieldset'
  3577. );
  3578. $this->assertTags($result, $expected);
  3579. $result = $this->Form->input('Model.field', array(
  3580. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3581. 'type' => 'radio',
  3582. 'disabled' => array(2, '3x'),
  3583. ));
  3584. $expected = array(
  3585. 'div' => array('class' => 'input radio'),
  3586. 'fieldset' => array(),
  3587. 'legend' => array(),
  3588. 'Field',
  3589. '/legend',
  3590. array('input' => array('type' => 'hidden', 'name' => 'Model[field]', 'id' => 'ModelField_', 'value' => '')),
  3591. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3592. array('label' => array('for' => 'ModelField1')),
  3593. 'first',
  3594. '/label',
  3595. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3596. array('label' => array('for' => 'ModelField2')),
  3597. 'second',
  3598. '/label',
  3599. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3600. array('label' => array('for' => 'ModelField2x')),
  3601. '2x',
  3602. '/label',
  3603. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '3', 'id' => 'ModelField3')),
  3604. array('label' => array('for' => 'ModelField3')),
  3605. 'third',
  3606. '/label',
  3607. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3608. array('label' => array('for' => 'ModelField3x')),
  3609. '3x',
  3610. '/label',
  3611. '/fieldset',
  3612. '/div'
  3613. );
  3614. $this->assertTags($result, $expected);
  3615. $result = $this->Form->input('Model.field', array(
  3616. 'type' => 'radio',
  3617. 'options' => array(
  3618. 1 => 'A',
  3619. 2 => 'B',
  3620. 3 => 'C'
  3621. ),
  3622. 'disabled' => array(1)
  3623. ));
  3624. $expected = array(
  3625. 'div' => array('class' => 'input radio'),
  3626. 'fieldset' => array(),
  3627. 'legend' => array(),
  3628. 'Field',
  3629. '/legend',
  3630. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3631. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
  3632. array('label' => array('for' => 'ModelField1')),
  3633. 'A',
  3634. '/label',
  3635. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
  3636. array('label' => array('for' => 'ModelField2')),
  3637. 'B',
  3638. '/label',
  3639. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
  3640. array('label' => array('for' => 'ModelField3')),
  3641. 'C',
  3642. '/label',
  3643. '/fieldset',
  3644. '/div'
  3645. );
  3646. $this->assertTags($result, $expected);
  3647. }
  3648. /**
  3649. * test disabling the hidden input for radio buttons
  3650. *
  3651. * @return void
  3652. */
  3653. public function testRadioHiddenInputDisabling() {
  3654. $result = $this->Form->input('Model.1.field', array(
  3655. 'type' => 'radio',
  3656. 'options' => array('option A'),
  3657. 'hiddenField' => false
  3658. )
  3659. );
  3660. $expected = array(
  3661. 'div' => array('class' => 'input radio'),
  3662. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3663. 'label' => array('for' => 'Model1Field0'),
  3664. 'option A',
  3665. '/label',
  3666. '/div'
  3667. );
  3668. $this->assertTags($result, $expected);
  3669. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3670. $expected = array(
  3671. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3672. 'label' => array('for' => 'Model1Field0'),
  3673. 'option A',
  3674. '/label'
  3675. );
  3676. $this->assertTags($result, $expected);
  3677. }
  3678. /**
  3679. * test adding an empty option for radio buttons
  3680. *
  3681. * @return void
  3682. */
  3683. public function testRadioAddEmptyOption() {
  3684. $result = $this->Form->input('Model.1.field', array(
  3685. 'type' => 'radio',
  3686. 'options' => array('option A'),
  3687. 'empty' => true,
  3688. 'hiddenField' => false
  3689. ));
  3690. $expected = array(
  3691. 'div' => array('class' => 'input radio'),
  3692. 'fieldset' => array(),
  3693. 'legend' => array(),
  3694. 'Field',
  3695. '/legend',
  3696. array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '', 'id' => 'Model1Field')),
  3697. array('label' => array('for' => 'Model1Field')),
  3698. __('empty'),
  3699. '/label',
  3700. array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3701. array('label' => array('for' => 'Model1Field0')),
  3702. 'option A',
  3703. '/label',
  3704. '/fieldset',
  3705. '/div'
  3706. );
  3707. $this->assertTags($result, $expected);
  3708. $result = $this->Form->input('Model.1.field', array(
  3709. 'type' => 'radio',
  3710. 'options' => array('option A'),
  3711. 'empty' => 'CustomEmptyLabel',
  3712. 'hiddenField' => false
  3713. ));
  3714. $expected = array(
  3715. 'div' => array('class' => 'input radio'),
  3716. 'fieldset' => array(),
  3717. 'legend' => array(),
  3718. 'Field',
  3719. '/legend',
  3720. array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '', 'id' => 'Model1Field')),
  3721. array('label' => array('for' => 'Model1Field')),
  3722. 'CustomEmptyLabel',
  3723. '/label',
  3724. array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3725. array('label' => array('for' => 'Model1Field0')),
  3726. 'option A',
  3727. '/label',
  3728. '/fieldset',
  3729. '/div'
  3730. );
  3731. $this->assertTags($result, $expected);
  3732. $result = $this->Form->input('Model.1.field', array(
  3733. 'type' => 'radio',
  3734. 'options' => array('option A'),
  3735. 'empty' => false,
  3736. 'hiddenField' => false
  3737. ));
  3738. $this->assertTextNotContains('"Model1Field"', $result);
  3739. }
  3740. /**
  3741. * Test that radio() accepts an array for label
  3742. *
  3743. * @return void
  3744. */
  3745. public function testRadioLabelArray() {
  3746. $result = $this->Form->input('Model.field', array(
  3747. 'type' => 'radio',
  3748. 'legend' => false,
  3749. 'label' => array(
  3750. 'class' => 'checkbox float-left',
  3751. ),
  3752. 'options' => array('1' => 'Option A', '2' => 'Option B.')
  3753. ));
  3754. $this->assertTextContains(
  3755. '<label for="ModelField1" class="checkbox float-left">Option A</label>',
  3756. $result
  3757. );
  3758. }
  3759. /**
  3760. * Test that label id's match the input element id's when radio is called after create().
  3761. *
  3762. * @return void
  3763. */
  3764. public function testRadioWithCreate() {
  3765. $this->Form->create('Model');
  3766. $result = $this->Form->radio('recipient',
  3767. array('1' => '1', '2' => '2', '3' => '3'),
  3768. array('legend' => false, 'value' => '1')
  3769. );
  3770. $this->assertTextNotContains(
  3771. '<label for="ModelModelRecipient1">1</label>',
  3772. $result
  3773. );
  3774. $this->assertTextContains(
  3775. '<label for="ModelRecipient1">1</label>',
  3776. $result
  3777. );
  3778. }
  3779. /**
  3780. * testSelect method
  3781. *
  3782. * Test select element generation.
  3783. *
  3784. * @return void
  3785. */
  3786. public function testSelect() {
  3787. $result = $this->Form->select('Model.field', array());
  3788. $expected = array(
  3789. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3790. array('option' => array('value' => '')),
  3791. '/option',
  3792. '/select'
  3793. );
  3794. $this->assertTags($result, $expected);
  3795. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3796. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3797. $expected = array(
  3798. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3799. array('option' => array('value' => '')),
  3800. '/option',
  3801. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3802. 'good',
  3803. '/option',
  3804. array('option' => array('value' => 'other')),
  3805. 'bad',
  3806. '/option',
  3807. '/select'
  3808. );
  3809. $this->assertTags($result, $expected);
  3810. $this->Form->request->data = array();
  3811. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3812. $expected = array(
  3813. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3814. array('option' => array('value' => '')),
  3815. '/option',
  3816. array('option' => array('value' => 'value')),
  3817. 'good',
  3818. '/option',
  3819. array('option' => array('value' => 'other')),
  3820. 'bad',
  3821. '/option',
  3822. '/select'
  3823. );
  3824. $this->assertTags($result, $expected);
  3825. $result = $this->Form->select(
  3826. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3827. array('empty' => false)
  3828. );
  3829. $expected = array(
  3830. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3831. array('option' => array('value' => 'first')),
  3832. 'first &quot;html&quot; &lt;chars&gt;',
  3833. '/option',
  3834. array('option' => array('value' => 'second')),
  3835. 'value',
  3836. '/option',
  3837. '/select'
  3838. );
  3839. $this->assertTags($result, $expected);
  3840. $result = $this->Form->select(
  3841. 'Model.field',
  3842. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3843. array('escape' => false, 'empty' => false)
  3844. );
  3845. $expected = array(
  3846. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3847. array('option' => array('value' => 'first')),
  3848. 'first "html" <chars>',
  3849. '/option',
  3850. array('option' => array('value' => 'second')),
  3851. 'value',
  3852. '/option',
  3853. '/select'
  3854. );
  3855. $this->assertTags($result, $expected);
  3856. $options = array(
  3857. array('value' => 'first', 'name' => 'First'),
  3858. array('value' => 'first', 'name' => 'Another First'),
  3859. );
  3860. $result = $this->Form->select(
  3861. 'Model.field',
  3862. $options,
  3863. array('escape' => false, 'empty' => false)
  3864. );
  3865. $expected = array(
  3866. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3867. array('option' => array('value' => 'first')),
  3868. 'First',
  3869. '/option',
  3870. array('option' => array('value' => 'first')),
  3871. 'Another First',
  3872. '/option',
  3873. '/select'
  3874. );
  3875. $this->assertTags($result, $expected);
  3876. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3877. $result = $this->Form->select(
  3878. 'Model.contact_id',
  3879. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3880. array('escape' => false, 'empty' => 'pick something')
  3881. );
  3882. $expected = array(
  3883. 'select' => array('name' => 'Model[contact_id]', 'id' => 'ModelContactId'),
  3884. array('option' => array('value' => '')), 'pick something', '/option',
  3885. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3886. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3887. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3888. '/select'
  3889. );
  3890. $this->assertTags($result, $expected);
  3891. $this->Form->request->data['Model']['field'] = 0;
  3892. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3893. $expected = array(
  3894. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3895. array('option' => array('value' => '')), '/option',
  3896. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3897. array('option' => array('value' => '1')), 'Yes', '/option',
  3898. '/select'
  3899. );
  3900. $this->assertTags($result, $expected);
  3901. $this->Form->request->data['Model']['field'] = 50;
  3902. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3903. $expected = array(
  3904. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3905. array('option' => array('value' => '')), '/option',
  3906. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3907. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3908. '/select'
  3909. );
  3910. $this->assertTags($result, $expected);
  3911. $result = $this->Form->select('Contact.required_one', array('option A'));
  3912. $expected = array(
  3913. 'select' => array(
  3914. 'name' => 'data[Contact][required_one]',
  3915. 'id' => 'ContactRequiredOne',
  3916. 'required' => 'required'
  3917. ),
  3918. array('option' => array('value' => '')), '/option',
  3919. array('option' => array('value' => '0')), 'option A', '/option',
  3920. '/select'
  3921. );
  3922. $this->assertTags($result, $expected);
  3923. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  3924. $expected = array(
  3925. 'select' => array(
  3926. 'name' => 'data[Contact][required_one]',
  3927. 'id' => 'ContactRequiredOne',
  3928. 'disabled' => 'disabled'
  3929. ),
  3930. array('option' => array('value' => '')), '/option',
  3931. array('option' => array('value' => '0')), 'option A', '/option',
  3932. '/select'
  3933. );
  3934. $this->assertTags($result, $expected);
  3935. }
  3936. /**
  3937. * test that select() with optiongroups listens to the escape param.
  3938. *
  3939. * @return void
  3940. */
  3941. public function testSelectOptionGroupEscaping() {
  3942. $options = array(
  3943. '>< Key' => array(
  3944. 1 => 'One',
  3945. 2 => 'Two'
  3946. )
  3947. );
  3948. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3949. $expected = array(
  3950. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3951. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3952. array('option' => array('value' => '1')), 'One', '/option',
  3953. array('option' => array('value' => '2')), 'Two', '/option',
  3954. '/optgroup',
  3955. '/select'
  3956. );
  3957. $this->assertTags($result, $expected);
  3958. $options = array(
  3959. '>< Key' => array(
  3960. 1 => 'One',
  3961. 2 => 'Two'
  3962. )
  3963. );
  3964. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3965. $expected = array(
  3966. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3967. 'optgroup' => array('label' => '>< Key'),
  3968. array('option' => array('value' => '1')), 'One', '/option',
  3969. array('option' => array('value' => '2')), 'Two', '/option',
  3970. '/optgroup',
  3971. '/select'
  3972. );
  3973. $this->assertTags($result, $expected);
  3974. }
  3975. /**
  3976. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3977. *
  3978. * @return void
  3979. */
  3980. public function testSelectWithNullAttributes() {
  3981. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3982. $expected = array(
  3983. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3984. array('option' => array('value' => '0')),
  3985. 'first',
  3986. '/option',
  3987. array('option' => array('value' => '1')),
  3988. 'second',
  3989. '/option',
  3990. '/select'
  3991. );
  3992. $this->assertTags($result, $expected);
  3993. }
  3994. /**
  3995. * testNestedSelect method
  3996. *
  3997. * test select element generation with optgroups
  3998. *
  3999. * @return void
  4000. */
  4001. public function testNestedSelect() {
  4002. $result = $this->Form->select(
  4003. 'Model.field',
  4004. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4005. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4006. )), array('empty' => false)
  4007. );
  4008. $expected = array(
  4009. 'select' => array('name' => 'Model[field]',
  4010. 'id' => 'ModelField'),
  4011. array('option' => array('value' => 1)),
  4012. 'One',
  4013. '/option',
  4014. array('option' => array('value' => 2)),
  4015. 'Two',
  4016. '/option',
  4017. array('optgroup' => array('label' => 'Three')),
  4018. array('option' => array('value' => 4)),
  4019. 'Four',
  4020. '/option',
  4021. array('option' => array('value' => 5)),
  4022. 'Five',
  4023. '/option',
  4024. '/optgroup',
  4025. '/select'
  4026. );
  4027. $this->assertTags($result, $expected);
  4028. $result = $this->Form->select(
  4029. 'Model.field',
  4030. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  4031. array('showParents' => true, 'empty' => false)
  4032. );
  4033. $expected = array(
  4034. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  4035. array('option' => array('value' => 1)),
  4036. 'One',
  4037. '/option',
  4038. array('option' => array('value' => 2)),
  4039. 'Two',
  4040. '/option',
  4041. array('optgroup' => array('label' => 'Three')),
  4042. array('option' => array('value' => 3)),
  4043. 'Three',
  4044. '/option',
  4045. array('option' => array('value' => 4)),
  4046. 'Four',
  4047. '/option',
  4048. '/optgroup',
  4049. '/select'
  4050. );
  4051. $this->assertTags($result, $expected);
  4052. }
  4053. /**
  4054. * testSelectMultiple method
  4055. *
  4056. * test generation of multiple select elements
  4057. *
  4058. * @return void
  4059. */
  4060. public function testSelectMultiple() {
  4061. $options = array('first', 'second', 'third');
  4062. $result = $this->Form->select(
  4063. 'Model.multi_field', $options, array('multiple' => true)
  4064. );
  4065. $expected = array(
  4066. 'input' => array(
  4067. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4068. ),
  4069. 'select' => array(
  4070. 'name' => 'Model[multi_field][]',
  4071. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4072. ),
  4073. array('option' => array('value' => '0')),
  4074. 'first',
  4075. '/option',
  4076. array('option' => array('value' => '1')),
  4077. 'second',
  4078. '/option',
  4079. array('option' => array('value' => '2')),
  4080. 'third',
  4081. '/option',
  4082. '/select'
  4083. );
  4084. $this->assertTags($result, $expected);
  4085. $result = $this->Form->select(
  4086. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4087. );
  4088. $expected = array(
  4089. 'input' => array(
  4090. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4091. ),
  4092. 'select' => array(
  4093. 'name' => 'Model[multi_field][]',
  4094. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4095. ),
  4096. array('option' => array('value' => '0')),
  4097. 'first',
  4098. '/option',
  4099. array('option' => array('value' => '1')),
  4100. 'second',
  4101. '/option',
  4102. array('option' => array('value' => '2')),
  4103. 'third',
  4104. '/option',
  4105. '/select'
  4106. );
  4107. $this->assertTags($result, $expected);
  4108. $result = $this->Form->select(
  4109. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4110. );
  4111. $expected = array(
  4112. 'input' => array(
  4113. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4114. ),
  4115. 'select' => array(
  4116. 'name' => 'Model[multi_field][]', 'id' => 'ModelMultiField',
  4117. 'multiple' => 'multiple'
  4118. ),
  4119. array('option' => array('value' => '0', 'selected' => 'selected')),
  4120. 'first',
  4121. '/option',
  4122. array('option' => array('value' => '1', 'selected' => 'selected')),
  4123. 'second',
  4124. '/option',
  4125. array('option' => array('value' => '2')),
  4126. 'third',
  4127. '/option',
  4128. '/select'
  4129. );
  4130. $this->assertTags($result, $expected);
  4131. $result = $this->Form->select(
  4132. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4133. );
  4134. $expected = array(
  4135. 'select' => array(
  4136. 'name' => 'Model[multi_field]', 'id' => 'ModelMultiField'
  4137. ),
  4138. array('option' => array('value' => '0', 'selected' => 'selected')),
  4139. 'first',
  4140. '/option',
  4141. array('option' => array('value' => '1', 'selected' => 'selected')),
  4142. 'second',
  4143. '/option',
  4144. array('option' => array('value' => '2')),
  4145. 'third',
  4146. '/option',
  4147. '/select'
  4148. );
  4149. $this->assertTags($result, $expected);
  4150. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4151. $selected = array('2', '3x');
  4152. $result = $this->Form->select(
  4153. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4154. );
  4155. $expected = array(
  4156. 'input' => array(
  4157. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4158. ),
  4159. 'select' => array(
  4160. 'name' => 'Model[multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4161. ),
  4162. array('option' => array('value' => '1')),
  4163. 'One',
  4164. '/option',
  4165. array('option' => array('value' => '2', 'selected' => 'selected')),
  4166. 'Two',
  4167. '/option',
  4168. array('option' => array('value' => '3')),
  4169. 'Three',
  4170. '/option',
  4171. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4172. 'Stringy',
  4173. '/option',
  4174. '/select'
  4175. );
  4176. $this->assertTags($result, $expected);
  4177. $result = $this->Form->select('Contact.required_one', array(
  4178. '1' => 'option A',
  4179. '2' => 'option B'
  4180. ), array('multiple' => true));
  4181. $expected = array(
  4182. 'input' => array(
  4183. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4184. ),
  4185. 'select' => array(
  4186. 'name' => 'data[Contact][required_one][]',
  4187. 'id' => 'ContactRequiredOne',
  4188. 'required' => 'required',
  4189. 'multiple' => 'multiple'
  4190. ),
  4191. array('option' => array('value' => '1')), 'option A', '/option',
  4192. array('option' => array('value' => '2')), 'option B', '/option',
  4193. '/select'
  4194. );
  4195. $this->assertTags($result, $expected);
  4196. }
  4197. /**
  4198. * Test generating multiple select with disabled elements.
  4199. *
  4200. * @return void
  4201. */
  4202. public function testSelectMultipleWithDisabledElements() {
  4203. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4204. $disabled = array(2, 3);
  4205. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4206. $expected = array(
  4207. 'input' => array(
  4208. 'type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4209. ),
  4210. 'select' => array(
  4211. 'name' => 'Contact[multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4212. ),
  4213. array('option' => array('value' => '1')),
  4214. 'One',
  4215. '/option',
  4216. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4217. 'Two',
  4218. '/option',
  4219. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4220. 'Three',
  4221. '/option',
  4222. array('option' => array('value' => '3x')),
  4223. 'Stringy',
  4224. '/option',
  4225. '/select'
  4226. );
  4227. $this->assertTags($result, $expected);
  4228. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4229. $disabled = array('2', '3x');
  4230. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4231. $expected = array(
  4232. array('div' => array('class' => 'input select')),
  4233. array('label' => array('for' => 'ContactMultiple')),
  4234. 'Multiple',
  4235. '/label',
  4236. 'input' => array(
  4237. 'type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4238. ),
  4239. 'select' => array(
  4240. 'name' => 'Contact[multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4241. ),
  4242. array('option' => array('value' => '1')),
  4243. 'One',
  4244. '/option',
  4245. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4246. 'Two',
  4247. '/option',
  4248. array('option' => array('value' => '3')),
  4249. 'Three',
  4250. '/option',
  4251. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4252. 'Stringy',
  4253. '/option',
  4254. '/select',
  4255. '/div'
  4256. );
  4257. $this->assertTags($result, $expected);
  4258. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4259. $disabled = true;
  4260. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4261. $expected = array(
  4262. array('div' => array('class' => 'input select')),
  4263. array('label' => array('for' => 'ContactMultiple')),
  4264. 'Multiple',
  4265. '/label',
  4266. 'input' => array(
  4267. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4268. ),
  4269. 'select' => array(
  4270. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4271. ),
  4272. array('option' => array('value' => '1')),
  4273. 'One',
  4274. '/option',
  4275. array('option' => array('value' => '2')),
  4276. 'Two',
  4277. '/option',
  4278. array('option' => array('value' => '3')),
  4279. 'Three',
  4280. '/option',
  4281. array('option' => array('value' => '3x')),
  4282. 'Stringy',
  4283. '/option',
  4284. '/select',
  4285. '/div'
  4286. );
  4287. $this->assertTags($result, $expected);
  4288. }
  4289. /**
  4290. * Test generating select with disabled elements.
  4291. *
  4292. * @return void
  4293. */
  4294. public function testSelectWithDisabledElements() {
  4295. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4296. $disabled = array(2, 3);
  4297. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4298. $expected = array(
  4299. 'select' => array(
  4300. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4301. ),
  4302. array('option' => array('value' => '')),
  4303. '/option',
  4304. array('option' => array('value' => '1')),
  4305. 'One',
  4306. '/option',
  4307. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4308. 'Two',
  4309. '/option',
  4310. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4311. 'Three',
  4312. '/option',
  4313. array('option' => array('value' => '3x')),
  4314. 'Stringy',
  4315. '/option',
  4316. '/select'
  4317. );
  4318. $this->assertTags($result, $expected);
  4319. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4320. $disabled = array('2', '3x');
  4321. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4322. $expected = array(
  4323. array('div' => array('class' => 'input select')),
  4324. array('label' => array('for' => 'ModelField')),
  4325. 'Field',
  4326. '/label',
  4327. 'select' => array(
  4328. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4329. ),
  4330. array('option' => array('value' => '1')),
  4331. 'One',
  4332. '/option',
  4333. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4334. 'Two',
  4335. '/option',
  4336. array('option' => array('value' => '3')),
  4337. 'Three',
  4338. '/option',
  4339. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4340. 'Stringy',
  4341. '/option',
  4342. '/select',
  4343. '/div'
  4344. );
  4345. $this->assertTags($result, $expected);
  4346. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4347. $disabled = true;
  4348. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4349. $expected = array(
  4350. array('div' => array('class' => 'input select')),
  4351. array('label' => array('for' => 'ModelField')),
  4352. 'Field',
  4353. '/label',
  4354. 'select' => array(
  4355. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4356. ),
  4357. array('option' => array('value' => '1')),
  4358. 'One',
  4359. '/option',
  4360. array('option' => array('value' => '2')),
  4361. 'Two',
  4362. '/option',
  4363. array('option' => array('value' => '3')),
  4364. 'Three',
  4365. '/option',
  4366. array('option' => array('value' => '3x')),
  4367. 'Stringy',
  4368. '/option',
  4369. '/select',
  4370. '/div'
  4371. );
  4372. $this->assertTags($result, $expected);
  4373. }
  4374. /**
  4375. * test generation of habtm select boxes.
  4376. *
  4377. * @return void
  4378. */
  4379. public function testHabtmSelectBox() {
  4380. $this->View->viewVars['contactTags'] = array(
  4381. 1 => 'blue',
  4382. 2 => 'red',
  4383. 3 => 'green'
  4384. );
  4385. $this->Form->request->data = array(
  4386. 'Contact' => array(),
  4387. 'ContactTag' => array(
  4388. array(
  4389. 'id' => '1',
  4390. 'name' => 'blue'
  4391. ),
  4392. array(
  4393. 'id' => 3,
  4394. 'name' => 'green'
  4395. )
  4396. )
  4397. );
  4398. $this->Form->create('Contact');
  4399. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4400. $expected = array(
  4401. 'input' => array(
  4402. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4403. ),
  4404. 'select' => array(
  4405. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  4406. 'multiple' => 'multiple'
  4407. ),
  4408. array('option' => array('value' => '1', 'selected' => 'selected')),
  4409. 'blue',
  4410. '/option',
  4411. array('option' => array('value' => '2')),
  4412. 'red',
  4413. '/option',
  4414. array('option' => array('value' => '3', 'selected' => 'selected')),
  4415. 'green',
  4416. '/option',
  4417. '/select'
  4418. );
  4419. $this->assertTags($result, $expected);
  4420. // make sure only 50 is selected, and not 50f5c0cf
  4421. $this->View->viewVars['contactTags'] = array(
  4422. '1' => 'blue',
  4423. '50f5c0cf' => 'red',
  4424. '50' => 'green'
  4425. );
  4426. $this->Form->request->data = array(
  4427. 'Contact' => array(),
  4428. 'ContactTag' => array(
  4429. array(
  4430. 'id' => 1,
  4431. 'name' => 'blue'
  4432. ),
  4433. array(
  4434. 'id' => 50,
  4435. 'name' => 'green'
  4436. )
  4437. )
  4438. );
  4439. $this->Form->create('Contact');
  4440. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4441. $expected = array(
  4442. 'input' => array(
  4443. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4444. ),
  4445. 'select' => array(
  4446. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  4447. 'multiple' => 'multiple'
  4448. ),
  4449. array('option' => array('value' => '1', 'selected' => 'selected')),
  4450. 'blue',
  4451. '/option',
  4452. array('option' => array('value' => '50f5c0cf')),
  4453. 'red',
  4454. '/option',
  4455. array('option' => array('value' => '50', 'selected' => 'selected')),
  4456. 'green',
  4457. '/option',
  4458. '/select'
  4459. );
  4460. $this->assertTags($result, $expected);
  4461. }
  4462. /**
  4463. * test generation of multi select elements in checkbox format
  4464. *
  4465. * @return void
  4466. */
  4467. public function testSelectMultipleCheckboxes() {
  4468. $result = $this->Form->select(
  4469. 'Model.multi_field',
  4470. array('first', 'second', 'third'),
  4471. array('multiple' => 'checkbox')
  4472. );
  4473. $expected = array(
  4474. 'input' => array(
  4475. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4476. ),
  4477. array('div' => array('class' => 'checkbox')),
  4478. array('input' => array(
  4479. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4480. 'value' => '0', 'id' => 'ModelMultiField0'
  4481. )),
  4482. array('label' => array('for' => 'ModelMultiField0')),
  4483. 'first',
  4484. '/label',
  4485. '/div',
  4486. array('div' => array('class' => 'checkbox')),
  4487. array('input' => array(
  4488. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4489. 'value' => '1', 'id' => 'ModelMultiField1'
  4490. )),
  4491. array('label' => array('for' => 'ModelMultiField1')),
  4492. 'second',
  4493. '/label',
  4494. '/div',
  4495. array('div' => array('class' => 'checkbox')),
  4496. array('input' => array(
  4497. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4498. 'value' => '2', 'id' => 'ModelMultiField2'
  4499. )),
  4500. array('label' => array('for' => 'ModelMultiField2')),
  4501. 'third',
  4502. '/label',
  4503. '/div'
  4504. );
  4505. $this->assertTags($result, $expected);
  4506. $result = $this->Form->select(
  4507. 'Model.multi_field',
  4508. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4509. array('multiple' => 'checkbox')
  4510. );
  4511. $expected = array(
  4512. 'input' => array(
  4513. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4514. ),
  4515. array('div' => array('class' => 'checkbox')),
  4516. array('input' => array(
  4517. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4518. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4519. )),
  4520. array('label' => array('for' => 'ModelMultiFieldA')),
  4521. 'first',
  4522. '/label',
  4523. '/div',
  4524. array('div' => array('class' => 'checkbox')),
  4525. array('input' => array(
  4526. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4527. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4528. )),
  4529. array('label' => array('for' => 'ModelMultiFieldB')),
  4530. 'second',
  4531. '/label',
  4532. '/div',
  4533. array('div' => array('class' => 'checkbox')),
  4534. array('input' => array(
  4535. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4536. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4537. )),
  4538. array('label' => array('for' => 'ModelMultiFieldC')),
  4539. 'third',
  4540. '/label',
  4541. '/div'
  4542. );
  4543. $this->assertTags($result, $expected);
  4544. $result = $this->Form->select(
  4545. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4546. );
  4547. $expected = array(
  4548. 'input' => array(
  4549. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4550. ),
  4551. array('div' => array('class' => 'checkbox')),
  4552. array('input' => array(
  4553. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4554. 'value' => '1', 'id' => 'ModelMultiField1'
  4555. )),
  4556. array('label' => array('for' => 'ModelMultiField1')),
  4557. 'first',
  4558. '/label',
  4559. '/div'
  4560. );
  4561. $this->assertTags($result, $expected);
  4562. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4563. $result = $this->Form->select(
  4564. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4565. );
  4566. $expected = array(
  4567. 'input' => array(
  4568. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'ModelTags'
  4569. ),
  4570. array('div' => array('class' => 'checkbox')),
  4571. array('input' => array(
  4572. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4573. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4574. )),
  4575. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4576. 'first',
  4577. '/label',
  4578. '/div',
  4579. array('div' => array('class' => 'checkbox')),
  4580. array('input' => array(
  4581. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4582. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4583. )),
  4584. array('label' => array('for' => 'ModelTagsArray')),
  4585. 'Array',
  4586. '/label',
  4587. '/div'
  4588. );
  4589. $this->assertTags($result, $expected);
  4590. }
  4591. /**
  4592. * test multiple checkboxes with div styles.
  4593. *
  4594. * @return void
  4595. */
  4596. public function testSelectMultipleCheckboxDiv() {
  4597. $result = $this->Form->select(
  4598. 'Model.tags',
  4599. array('first', 'second'),
  4600. array('multiple' => 'checkbox', 'class' => 'my-class')
  4601. );
  4602. $expected = array(
  4603. 'input' => array(
  4604. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'ModelTags'
  4605. ),
  4606. array('div' => array('class' => 'my-class')),
  4607. array('input' => array(
  4608. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4609. 'value' => '0', 'id' => 'ModelTags0'
  4610. )),
  4611. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4612. '/div',
  4613. array('div' => array('class' => 'my-class')),
  4614. array('input' => array(
  4615. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4616. 'value' => '1', 'id' => 'ModelTags1'
  4617. )),
  4618. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4619. '/div'
  4620. );
  4621. $this->assertTags($result, $expected);
  4622. $result = $this->Form->input('Model.tags', array(
  4623. 'options' => array('first', 'second'),
  4624. 'multiple' => 'checkbox',
  4625. 'class' => 'my-class',
  4626. 'div' => false,
  4627. 'label' => false
  4628. ));
  4629. $this->assertTags($result, $expected);
  4630. $Contact = ClassRegistry::getObject('Contact');
  4631. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4632. $result = $this->Form->input('Contact.tags', array(
  4633. 'options' => array('one'),
  4634. 'multiple' => 'checkbox',
  4635. 'label' => false,
  4636. 'div' => false
  4637. ));
  4638. $expected = array(
  4639. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[tags]', 'value' => '', 'id' => 'ContactTags'),
  4640. array('div' => array('class' => 'checkbox form-error')),
  4641. array('input' => array('type' => 'checkbox', 'name' => 'Contact[tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4642. array('label' => array('for' => 'ContactTags0')),
  4643. 'one',
  4644. '/label',
  4645. '/div'
  4646. );
  4647. $this->assertTags($result, $expected);
  4648. $result = $this->Form->input('Contact.tags', array(
  4649. 'options' => array('one'),
  4650. 'multiple' => 'checkbox',
  4651. 'class' => 'mycheckbox',
  4652. 'label' => false,
  4653. 'div' => false
  4654. ));
  4655. $expected = array(
  4656. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[tags]', 'value' => '', 'id' => 'ContactTags'),
  4657. array('div' => array('class' => 'mycheckbox form-error')),
  4658. array('input' => array('type' => 'checkbox', 'name' => 'Contact[tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4659. array('label' => array('for' => 'ContactTags0')),
  4660. 'one',
  4661. '/label',
  4662. '/div'
  4663. );
  4664. $this->assertTags($result, $expected);
  4665. }
  4666. /**
  4667. * Checks the security hash array generated for multiple-input checkbox elements
  4668. *
  4669. * @return void
  4670. */
  4671. public function testSelectMultipleCheckboxSecurity() {
  4672. $this->Form->request['_Token'] = array('key' => 'testKey');
  4673. $this->assertEquals(array(), $this->Form->fields);
  4674. $result = $this->Form->select(
  4675. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4676. array('multiple' => 'checkbox')
  4677. );
  4678. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4679. $result = $this->Form->secure($this->Form->fields);
  4680. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4681. $this->assertRegExp('/"' . $key . '"/', $result);
  4682. }
  4683. /**
  4684. * Multiple select elements should always be secured as they always participate
  4685. * in the POST data.
  4686. *
  4687. * @return void
  4688. */
  4689. public function testSelectMultipleSecureWithNoOptions() {
  4690. $this->Form->request['_Token'] = array('key' => 'testkey');
  4691. $this->assertEquals(array(), $this->Form->fields);
  4692. $this->Form->select(
  4693. 'Model.select',
  4694. array(),
  4695. array('multiple' => true)
  4696. );
  4697. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4698. }
  4699. /**
  4700. * When a select box has no options it should not be added to the fields list
  4701. * as it always fail post validation.
  4702. *
  4703. * @return void
  4704. */
  4705. public function testSelectNoSecureWithNoOptions() {
  4706. $this->Form->request['_Token'] = array('key' => 'testkey');
  4707. $this->assertEquals(array(), $this->Form->fields);
  4708. $this->Form->select(
  4709. 'Model.select',
  4710. array()
  4711. );
  4712. $this->assertEquals(array(), $this->Form->fields);
  4713. $this->Form->select(
  4714. 'Model.select',
  4715. array(),
  4716. array('empty' => true)
  4717. );
  4718. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4719. }
  4720. /**
  4721. * testInputMultipleCheckboxes method
  4722. *
  4723. * test input() resulting in multi select elements being generated.
  4724. *
  4725. * @return void
  4726. */
  4727. public function testInputMultipleCheckboxes() {
  4728. $result = $this->Form->input('Model.multi_field', array(
  4729. 'options' => array('first', 'second', 'third'),
  4730. 'multiple' => 'checkbox'
  4731. ));
  4732. $expected = array(
  4733. array('div' => array('class' => 'input select')),
  4734. array('label' => array('for' => 'ModelMultiField')),
  4735. 'Multi Field',
  4736. '/label',
  4737. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4738. array('div' => array('class' => 'checkbox')),
  4739. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4740. array('label' => array('for' => 'ModelMultiField0')),
  4741. 'first',
  4742. '/label',
  4743. '/div',
  4744. array('div' => array('class' => 'checkbox')),
  4745. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4746. array('label' => array('for' => 'ModelMultiField1')),
  4747. 'second',
  4748. '/label',
  4749. '/div',
  4750. array('div' => array('class' => 'checkbox')),
  4751. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4752. array('label' => array('for' => 'ModelMultiField2')),
  4753. 'third',
  4754. '/label',
  4755. '/div',
  4756. '/div'
  4757. );
  4758. $this->assertTags($result, $expected);
  4759. $result = $this->Form->input('Model.multi_field', array(
  4760. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4761. 'multiple' => 'checkbox'
  4762. ));
  4763. $expected = array(
  4764. array('div' => array('class' => 'input select')),
  4765. array('label' => array('for' => 'ModelMultiField')),
  4766. 'Multi Field',
  4767. '/label',
  4768. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4769. array('div' => array('class' => 'checkbox')),
  4770. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4771. array('label' => array('for' => 'ModelMultiFieldA')),
  4772. 'first',
  4773. '/label',
  4774. '/div',
  4775. array('div' => array('class' => 'checkbox')),
  4776. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4777. array('label' => array('for' => 'ModelMultiFieldB')),
  4778. 'second',
  4779. '/label',
  4780. '/div',
  4781. array('div' => array('class' => 'checkbox')),
  4782. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4783. array('label' => array('for' => 'ModelMultiFieldC')),
  4784. 'third',
  4785. '/label',
  4786. '/div',
  4787. '/div'
  4788. );
  4789. $this->assertTags($result, $expected);
  4790. $result = $this->Form->input('Model.multi_field', array(
  4791. 'options' => array('1' => 'first'),
  4792. 'multiple' => 'checkbox',
  4793. 'label' => false,
  4794. 'div' => false
  4795. ));
  4796. $expected = array(
  4797. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4798. array('div' => array('class' => 'checkbox')),
  4799. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4800. array('label' => array('for' => 'ModelMultiField1')),
  4801. 'first',
  4802. '/label',
  4803. '/div'
  4804. );
  4805. $this->assertTags($result, $expected);
  4806. $result = $this->Form->input('Model.multi_field', array(
  4807. 'options' => array('2' => 'second'),
  4808. 'multiple' => 'checkbox',
  4809. 'label' => false,
  4810. 'div' => false
  4811. ));
  4812. $expected = array(
  4813. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4814. array('div' => array('class' => 'checkbox')),
  4815. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4816. array('label' => array('for' => 'ModelMultiField2')),
  4817. 'second',
  4818. '/label',
  4819. '/div'
  4820. );
  4821. $this->assertTags($result, $expected);
  4822. }
  4823. /**
  4824. * testSelectHiddenFieldOmission method
  4825. *
  4826. * test that select() with 'hiddenField' => false omits the hidden field
  4827. *
  4828. * @return void
  4829. */
  4830. public function testSelectHiddenFieldOmission() {
  4831. $result = $this->Form->select('Model.multi_field',
  4832. array('first', 'second'),
  4833. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4834. );
  4835. $expected = array(
  4836. array('div' => array('class' => 'checkbox')),
  4837. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4838. array('label' => array('for' => 'ModelMultiField0')),
  4839. 'first',
  4840. '/label',
  4841. '/div',
  4842. array('div' => array('class' => 'checkbox')),
  4843. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4844. array('label' => array('for' => 'ModelMultiField1')),
  4845. 'second',
  4846. '/label',
  4847. '/div'
  4848. );
  4849. $this->assertTags($result, $expected);
  4850. $result = $this->Form->input('Model.multi_field', array(
  4851. 'options' => array('first', 'second'),
  4852. 'multiple' => 'checkbox',
  4853. 'hiddenField' => false
  4854. ));
  4855. $expected = array(
  4856. array('div' => array('class' => 'input select')),
  4857. array('label' => array('for' => 'ModelMultiField')),
  4858. 'Multi Field',
  4859. '/label',
  4860. array('div' => array('class' => 'checkbox')),
  4861. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4862. array('label' => array('for' => 'ModelMultiField0')),
  4863. 'first',
  4864. '/label',
  4865. '/div',
  4866. array('div' => array('class' => 'checkbox')),
  4867. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4868. array('label' => array('for' => 'ModelMultiField1')),
  4869. 'second',
  4870. '/label',
  4871. '/div',
  4872. '/div'
  4873. );
  4874. $this->assertTags($result, $expected);
  4875. }
  4876. /**
  4877. * test that select() with multiple = checkbox works with overriding name attribute.
  4878. *
  4879. * @return void
  4880. */
  4881. public function testSelectCheckboxMultipleOverrideName() {
  4882. $result = $this->Form->input('category', array(
  4883. 'type' => 'select',
  4884. 'multiple' => 'checkbox',
  4885. 'name' => 'fish',
  4886. 'options' => array('1', '2'),
  4887. 'div' => false,
  4888. 'label' => false,
  4889. ));
  4890. $expected = array(
  4891. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'),
  4892. array('div' => array('class' => 'checkbox')),
  4893. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'Category0')),
  4894. array('label' => array('for' => 'Category0')), '1', '/label',
  4895. '/div',
  4896. array('div' => array('class' => 'checkbox')),
  4897. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'Category1')),
  4898. array('label' => array('for' => 'Category1')), '2', '/label',
  4899. '/div'
  4900. );
  4901. $this->assertTags($result, $expected);
  4902. }
  4903. /**
  4904. * Test that 'id' overrides all the checkbox id's as well.
  4905. *
  4906. * @return void
  4907. */
  4908. public function testSelectCheckboxMultipleId() {
  4909. $result = $this->Form->select(
  4910. 'Model.multi_field',
  4911. array('first', 'second', 'third'),
  4912. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4913. );
  4914. $expected = array(
  4915. 'input' => array(
  4916. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'CustomId'
  4917. ),
  4918. array('div' => array('class' => 'checkbox')),
  4919. array('input' => array(
  4920. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4921. 'value' => '0', 'id' => 'CustomId0'
  4922. )),
  4923. array('label' => array('for' => 'CustomId0')),
  4924. 'first',
  4925. '/label',
  4926. '/div',
  4927. array('div' => array('class' => 'checkbox')),
  4928. array('input' => array(
  4929. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4930. 'value' => '1', 'id' => 'CustomId1'
  4931. )),
  4932. array('label' => array('for' => 'CustomId1')),
  4933. 'second',
  4934. '/label',
  4935. '/div',
  4936. array('div' => array('class' => 'checkbox')),
  4937. array('input' => array(
  4938. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4939. 'value' => '2', 'id' => 'CustomId2'
  4940. )),
  4941. array('label' => array('for' => 'CustomId2')),
  4942. 'third',
  4943. '/label',
  4944. '/div'
  4945. );
  4946. $this->assertTags($result, $expected);
  4947. }
  4948. /**
  4949. * testCheckbox method
  4950. *
  4951. * Test generation of checkboxes
  4952. *
  4953. * @return void
  4954. */
  4955. public function testCheckbox() {
  4956. $result = $this->Form->checkbox('Model.field');
  4957. $expected = array(
  4958. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  4959. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField'))
  4960. );
  4961. $this->assertTags($result, $expected);
  4962. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4963. $expected = array(
  4964. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'theID_'),
  4965. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => 'myvalue', 'id' => 'theID'))
  4966. );
  4967. $this->assertTags($result, $expected);
  4968. $Contact = ClassRegistry::getObject('Contact');
  4969. $Contact->validationErrors['field'] = 1;
  4970. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4971. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4972. $expected = array(
  4973. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'theID_'),
  4974. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4975. );
  4976. $this->assertTags($result, $expected);
  4977. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4978. $expected = array(
  4979. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'ContactField_'),
  4980. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4981. );
  4982. $this->assertTags($result, $expected);
  4983. $this->Form->request->data['Contact']['field'] = '';
  4984. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4985. $expected = array(
  4986. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'theID_'),
  4987. array('input' => array('type' => 'checkbox', 'name' => 'Contact[field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4988. );
  4989. $this->assertTags($result, $expected);
  4990. $Contact->validationErrors = array();
  4991. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4992. $expected = array(
  4993. 'input' => array('type' => 'hidden', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'ContactField_'),
  4994. array('input' => array('type' => 'checkbox', 'name' => 'Contact[field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4995. );
  4996. $this->assertTags($result, $expected);
  4997. $this->Form->request->data['Contact']['published'] = 1;
  4998. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4999. $expected = array(
  5000. 'input' => array('type' => 'hidden', 'name' => 'Contact[published]', 'value' => '0', 'id' => 'theID_'),
  5001. array('input' => array('type' => 'checkbox', 'name' => 'Contact[published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  5002. );
  5003. $this->assertTags($result, $expected);
  5004. $this->Form->request->data['Contact']['published'] = 0;
  5005. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5006. $expected = array(
  5007. 'input' => array('type' => 'hidden', 'name' => 'Contact[published]', 'value' => '0', 'id' => 'theID_'),
  5008. array('input' => array('type' => 'checkbox', 'name' => 'Contact[published]', 'value' => '1', 'id' => 'theID'))
  5009. );
  5010. $this->assertTags($result, $expected);
  5011. $result = $this->Form->checkbox('Model.CustomField.1.value');
  5012. $expected = array(
  5013. 'input' => array('type' => 'hidden', 'name' => 'Model[CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  5014. array('input' => array('type' => 'checkbox', 'name' => 'Model[CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  5015. );
  5016. $this->assertTags($result, $expected);
  5017. $result = $this->Form->checkbox('CustomField.1.value');
  5018. $expected = array(
  5019. 'input' => array('type' => 'hidden', 'name' => 'CustomField[1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  5020. array('input' => array('type' => 'checkbox', 'name' => 'CustomField[1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  5021. );
  5022. $this->assertTags($result, $expected);
  5023. }
  5024. /**
  5025. * test checkbox() with a custom name attribute
  5026. *
  5027. * @return void
  5028. */
  5029. public function testCheckboxCustomNameAttribute() {
  5030. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  5031. $expected = array(
  5032. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  5033. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  5034. );
  5035. $this->assertTags($result, $expected);
  5036. }
  5037. /**
  5038. * test the checked option for checkboxes.
  5039. *
  5040. * @return void
  5041. */
  5042. public function testCheckboxCheckedOption() {
  5043. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  5044. $expected = array(
  5045. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5046. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5047. );
  5048. $this->assertTags($result, $expected);
  5049. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  5050. $expected = array(
  5051. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5052. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5053. );
  5054. $this->assertTags($result, $expected);
  5055. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  5056. $expected = array(
  5057. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5058. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5059. );
  5060. $this->assertTags($result, $expected);
  5061. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5062. $expected = array(
  5063. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5064. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField'))
  5065. );
  5066. $this->assertTags($result, $expected);
  5067. $this->Form->request->data['Model']['field'] = 1;
  5068. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5069. $expected = array(
  5070. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5071. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField'))
  5072. );
  5073. $this->assertTags($result, $expected);
  5074. }
  5075. /**
  5076. * Test that disabled attribute works on both the checkbox and hidden input.
  5077. *
  5078. * @return void
  5079. */
  5080. public function testCheckboxDisabling() {
  5081. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5082. $expected = array(
  5083. array('input' => array('type' => 'hidden', 'name' => 'Account[show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5084. array('input' => array('type' => 'checkbox', 'name' => 'Account[show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5085. );
  5086. $this->assertTags($result, $expected);
  5087. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5088. $expected = array(
  5089. array('input' => array('type' => 'hidden', 'name' => 'Account[show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5090. array('input' => array('type' => 'checkbox', 'name' => 'Account[show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5091. );
  5092. $this->assertTags($result, $expected);
  5093. }
  5094. /**
  5095. * Test that the hidden input for checkboxes can be omitted or set to a
  5096. * specific value.
  5097. *
  5098. * @return void
  5099. */
  5100. public function testCheckboxHiddenField() {
  5101. $result = $this->Form->input('UserForm.something', array(
  5102. 'type' => 'checkbox',
  5103. 'hiddenField' => false
  5104. ));
  5105. $expected = array(
  5106. 'div' => array('class' => 'input checkbox'),
  5107. array('input' => array(
  5108. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5109. 'value' => '1', 'id' => 'UserFormSomething'
  5110. )),
  5111. 'label' => array('for' => 'UserFormSomething'),
  5112. 'Something',
  5113. '/label',
  5114. '/div'
  5115. );
  5116. $this->assertTags($result, $expected);
  5117. $result = $this->Form->input('UserForm.something', array(
  5118. 'type' => 'checkbox',
  5119. 'value' => 'Y',
  5120. 'hiddenField' => 'N',
  5121. ));
  5122. $expected = array(
  5123. 'div' => array('class' => 'input checkbox'),
  5124. array('input' => array(
  5125. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5126. 'value' => 'N', 'id' => 'UserFormSomething_'
  5127. )),
  5128. array('input' => array(
  5129. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5130. 'value' => 'Y', 'id' => 'UserFormSomething'
  5131. )),
  5132. 'label' => array('for' => 'UserFormSomething'),
  5133. 'Something',
  5134. '/label',
  5135. '/div'
  5136. );
  5137. $this->assertTags($result, $expected);
  5138. }
  5139. /**
  5140. * testDateTime method
  5141. *
  5142. * Test generation of date/time select elements
  5143. *
  5144. * @return void
  5145. */
  5146. public function testDateTime() {
  5147. extract($this->dateRegex);
  5148. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5149. $now = strtotime('now');
  5150. $expected = array(
  5151. array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')),
  5152. $daysRegex,
  5153. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5154. date('j', $now),
  5155. '/option',
  5156. '*/select',
  5157. '-',
  5158. array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')),
  5159. $monthsRegex,
  5160. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5161. date('F', $now),
  5162. '/option',
  5163. '*/select',
  5164. '-',
  5165. array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')),
  5166. $yearsRegex,
  5167. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5168. date('Y', $now),
  5169. '/option',
  5170. '*/select',
  5171. array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')),
  5172. $hoursRegex,
  5173. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5174. date('g', $now),
  5175. '/option',
  5176. '*/select',
  5177. ':',
  5178. array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')),
  5179. $minutesRegex,
  5180. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5181. date('i', $now),
  5182. '/option',
  5183. '*/select',
  5184. ' ',
  5185. array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')),
  5186. $meridianRegex,
  5187. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5188. date('a', $now),
  5189. '/option',
  5190. '*/select'
  5191. );
  5192. $this->assertTags($result, $expected);
  5193. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5194. $expected = array(
  5195. array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')),
  5196. $daysRegex,
  5197. array('option' => array('value' => '')),
  5198. '/option',
  5199. '*/select',
  5200. '-',
  5201. array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')),
  5202. $monthsRegex,
  5203. array('option' => array('value' => '')),
  5204. '/option',
  5205. '*/select',
  5206. '-',
  5207. array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')),
  5208. $yearsRegex,
  5209. array('option' => array('value' => '')),
  5210. '/option',
  5211. '*/select',
  5212. array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')),
  5213. $hoursRegex,
  5214. array('option' => array('value' => '')),
  5215. '/option',
  5216. '*/select',
  5217. ':',
  5218. array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')),
  5219. $minutesRegex,
  5220. array('option' => array('value' => '')),
  5221. '/option',
  5222. '*/select',
  5223. ' ',
  5224. array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')),
  5225. $meridianRegex,
  5226. array('option' => array('value' => '')),
  5227. '/option',
  5228. '*/select'
  5229. );
  5230. $this->assertTags($result, $expected);
  5231. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5232. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5233. $this->assertTags($result, $expected);
  5234. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5235. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5236. $this->assertTags($result, $expected);
  5237. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5238. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5239. $expected = array(
  5240. array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')),
  5241. $daysRegex,
  5242. array('option' => array('value' => '')),
  5243. '/option',
  5244. '*/select',
  5245. '-',
  5246. array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')),
  5247. $monthsRegex,
  5248. array('option' => array('value' => '')),
  5249. '/option',
  5250. '*/select',
  5251. '-',
  5252. array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')),
  5253. $yearsRegex,
  5254. array('option' => array('value' => '')),
  5255. '/option',
  5256. '*/select',
  5257. array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')),
  5258. $hoursRegex,
  5259. array('option' => array('value' => '')),
  5260. '/option',
  5261. '*/select',
  5262. ':',
  5263. array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')),
  5264. $minutesRegex,
  5265. array('option' => array('value' => '')),
  5266. '/option',
  5267. array('option' => array('value' => '00')),
  5268. '00',
  5269. '/option',
  5270. array('option' => array('value' => '05')),
  5271. '05',
  5272. '/option',
  5273. array('option' => array('value' => '10')),
  5274. '10',
  5275. '/option',
  5276. '*/select',
  5277. ' ',
  5278. array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')),
  5279. $meridianRegex,
  5280. array('option' => array('value' => '')),
  5281. '/option',
  5282. '*/select'
  5283. );
  5284. $this->assertTags($result, $expected);
  5285. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5286. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5287. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5288. $this->Form->request->data['Contact']['data'] = null;
  5289. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5290. $expected = array(
  5291. array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')),
  5292. $daysRegex,
  5293. array('option' => array('value' => '')),
  5294. '/option',
  5295. '*/select',
  5296. '-',
  5297. array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')),
  5298. $monthsRegex,
  5299. array('option' => array('value' => '')),
  5300. '/option',
  5301. '*/select',
  5302. '-',
  5303. array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')),
  5304. $yearsRegex,
  5305. array('option' => array('value' => '')),
  5306. '/option',
  5307. '*/select',
  5308. array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')),
  5309. $hoursRegex,
  5310. array('option' => array('value' => '')),
  5311. '/option',
  5312. '*/select',
  5313. ':',
  5314. array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')),
  5315. $minutesRegex,
  5316. array('option' => array('value' => '')),
  5317. '/option',
  5318. '*/select',
  5319. ' ',
  5320. array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')),
  5321. $meridianRegex,
  5322. array('option' => array('value' => '')),
  5323. '/option',
  5324. '*/select'
  5325. );
  5326. $this->assertTags($result, $expected);
  5327. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5328. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5329. $now = strtotime($this->Form->data['Model']['field']);
  5330. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5331. $expected = array(
  5332. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  5333. $daysRegex,
  5334. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5335. date('j', $now),
  5336. '/option',
  5337. '*/select',
  5338. '-',
  5339. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5340. $monthsRegex,
  5341. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5342. date('F', $now),
  5343. '/option',
  5344. '*/select',
  5345. '-',
  5346. array('select' => array('name' => 'Model[field][year]', 'id' => 'ModelFieldYear')),
  5347. $yearsRegex,
  5348. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5349. date('Y', $now),
  5350. '/option',
  5351. '*/select',
  5352. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  5353. $hoursRegex,
  5354. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5355. date('g', $now),
  5356. '/option',
  5357. '*/select',
  5358. ':',
  5359. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5360. $minutesRegex,
  5361. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5362. date('i', $now),
  5363. '/option',
  5364. '*/select',
  5365. ' ',
  5366. array('select' => array('name' => 'Model[field][meridian]', 'id' => 'ModelFieldMeridian')),
  5367. $meridianRegex,
  5368. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5369. date('a', $now),
  5370. '/option',
  5371. '*/select'
  5372. );
  5373. $this->assertTags($result, $expected);
  5374. $selected = strtotime('2008-10-26 12:33:00');
  5375. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5376. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5377. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5378. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5379. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5380. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5381. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5382. $this->Form->create('Contact');
  5383. $result = $this->Form->input('published');
  5384. $now = strtotime('now');
  5385. $expected = array(
  5386. 'div' => array('class' => 'input date'),
  5387. 'label' => array('for' => 'ContactPublishedMonth'),
  5388. 'Published',
  5389. '/label',
  5390. array('select' => array('name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth')),
  5391. $monthsRegex,
  5392. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5393. date('F', $now),
  5394. '/option',
  5395. '*/select',
  5396. '-',
  5397. array('select' => array('name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay')),
  5398. $daysRegex,
  5399. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5400. date('j', $now),
  5401. '/option',
  5402. '*/select',
  5403. '-',
  5404. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  5405. $yearsRegex,
  5406. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5407. date('Y', $now),
  5408. '/option',
  5409. '*/select',
  5410. '/div'
  5411. );
  5412. $this->assertTags($result, $expected);
  5413. $result = $this->Form->input('published2', array('type' => 'date'));
  5414. $now = strtotime('now');
  5415. $expected = array(
  5416. 'div' => array('class' => 'input date'),
  5417. 'label' => array('for' => 'ContactPublished2Month'),
  5418. 'Published2',
  5419. '/label',
  5420. array('select' => array('name' => 'Contact[published2][month]', 'id' => 'ContactPublished2Month')),
  5421. $monthsRegex,
  5422. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5423. date('F', $now),
  5424. '/option',
  5425. '*/select',
  5426. '-',
  5427. array('select' => array('name' => 'Contact[published2][day]', 'id' => 'ContactPublished2Day')),
  5428. $daysRegex,
  5429. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5430. date('j', $now),
  5431. '/option',
  5432. '*/select',
  5433. '-',
  5434. array('select' => array('name' => 'Contact[published2][year]', 'id' => 'ContactPublished2Year')),
  5435. $yearsRegex,
  5436. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5437. date('Y', $now),
  5438. '/option',
  5439. '*/select',
  5440. '/div'
  5441. );
  5442. $this->assertTags($result, $expected);
  5443. $this->Form->create('Contact');
  5444. $result = $this->Form->input('published', array('monthNames' => false));
  5445. $now = strtotime('now');
  5446. $expected = array(
  5447. 'div' => array('class' => 'input date'),
  5448. 'label' => array('for' => 'ContactPublishedMonth'),
  5449. 'Published',
  5450. '/label',
  5451. array('select' => array('name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth')),
  5452. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5453. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5454. date('m', $now),
  5455. '/option',
  5456. '*/select',
  5457. '-',
  5458. array('select' => array('name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay')),
  5459. $daysRegex,
  5460. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5461. date('j', $now),
  5462. '/option',
  5463. '*/select',
  5464. '-',
  5465. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  5466. $yearsRegex,
  5467. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5468. date('Y', $now),
  5469. '/option',
  5470. '*/select',
  5471. '/div'
  5472. );
  5473. $this->assertTags($result, $expected);
  5474. $result = $this->Form->input('published', array('type' => 'time'));
  5475. $now = strtotime('now');
  5476. $expected = array(
  5477. 'div' => array('class' => 'input time'),
  5478. 'label' => array('for' => 'ContactPublishedHour'),
  5479. 'Published',
  5480. '/label',
  5481. array('select' => array('name' => 'Contact[published][hour]', 'id' => 'ContactPublishedHour')),
  5482. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5483. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5484. date('g', $now),
  5485. '/option',
  5486. '*/select',
  5487. ':',
  5488. );
  5489. $this->assertTags($result, $expected);
  5490. $result = $this->Form->input('published', array(
  5491. 'timeFormat' => 24,
  5492. 'interval' => 5,
  5493. 'selected' => strtotime('2009-09-03 13:37:00'),
  5494. 'type' => 'datetime'
  5495. ));
  5496. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5497. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5498. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5499. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5500. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5501. }
  5502. /**
  5503. * Test dateTime with rounding
  5504. *
  5505. * @return void
  5506. */
  5507. public function testDateTimeRounding() {
  5508. $this->Form->request->data['Contact'] = array(
  5509. 'date' => array(
  5510. 'day' => '13',
  5511. 'month' => '12',
  5512. 'year' => '2010',
  5513. 'hour' => '04',
  5514. 'min' => '19',
  5515. 'meridian' => 'AM'
  5516. )
  5517. );
  5518. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5519. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5520. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5521. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5522. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5523. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5524. }
  5525. /**
  5526. * Test that empty values don't trigger errors.
  5527. *
  5528. * @return void
  5529. */
  5530. public function testDateTimeNoErrorsOnEmptyData() {
  5531. $this->Form->request->data['Contact'] = array(
  5532. 'date' => array(
  5533. 'day' => '',
  5534. 'month' => '',
  5535. 'year' => '',
  5536. 'hour' => '',
  5537. 'min' => '',
  5538. 'meridian' => ''
  5539. )
  5540. );
  5541. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5542. $this->assertNotEmpty($result);
  5543. }
  5544. /**
  5545. * test that datetime() and default values work.
  5546. *
  5547. * @return void
  5548. */
  5549. public function testDatetimeWithDefault() {
  5550. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5551. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5552. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5553. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5554. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5555. 'default' => '2009-06-01 11:15:30'
  5556. ));
  5557. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5558. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5559. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5560. }
  5561. /**
  5562. * test that bogus non-date time data doesn't cause errors.
  5563. *
  5564. * @return void
  5565. */
  5566. public function testDateTimeWithBogusData() {
  5567. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5568. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5569. }
  5570. /**
  5571. * testDateTimeEmptyAsArray
  5572. *
  5573. * @return void
  5574. */
  5575. public function testDateTimeEmptyAsArray() {
  5576. $result = $this->Form->dateTime('Contact.date',
  5577. 'DMY',
  5578. '12',
  5579. array(
  5580. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5581. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5582. )
  5583. )
  5584. );
  5585. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5586. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5587. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5588. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5589. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5590. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5591. $result = $this->Form->dateTime('Contact.date',
  5592. 'DMY',
  5593. '12',
  5594. array(
  5595. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5596. )
  5597. );
  5598. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5599. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5600. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5601. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5602. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5603. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5604. }
  5605. /**
  5606. * testFormDateTimeMulti method
  5607. *
  5608. * test multiple datetime element generation
  5609. *
  5610. * @return void
  5611. */
  5612. public function testFormDateTimeMulti() {
  5613. extract($this->dateRegex);
  5614. $result = $this->Form->dateTime('Contact.1.updated');
  5615. $expected = array(
  5616. array('select' => array('name' => 'Contact[1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5617. $daysRegex,
  5618. array('option' => array('value' => '')),
  5619. '/option',
  5620. '*/select',
  5621. '-',
  5622. array('select' => array('name' => 'Contact[1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5623. $monthsRegex,
  5624. array('option' => array('value' => '')),
  5625. '/option',
  5626. '*/select',
  5627. '-',
  5628. array('select' => array('name' => 'Contact[1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5629. $yearsRegex,
  5630. array('option' => array('value' => '')),
  5631. '/option',
  5632. '*/select',
  5633. array('select' => array('name' => 'Contact[1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5634. $hoursRegex,
  5635. array('option' => array('value' => '')),
  5636. '/option',
  5637. '*/select',
  5638. ':',
  5639. array('select' => array('name' => 'Contact[1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5640. $minutesRegex,
  5641. array('option' => array('value' => '')),
  5642. '/option',
  5643. '*/select',
  5644. ' ',
  5645. array('select' => array('name' => 'Contact[1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5646. $meridianRegex,
  5647. array('option' => array('value' => '')),
  5648. '/option',
  5649. '*/select'
  5650. );
  5651. $this->assertTags($result, $expected);
  5652. $result = $this->Form->dateTime('Contact.2.updated');
  5653. $expected = array(
  5654. array('select' => array('name' => 'Contact[2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5655. $daysRegex,
  5656. array('option' => array('value' => '')),
  5657. '/option',
  5658. '*/select',
  5659. '-',
  5660. array('select' => array('name' => 'Contact[2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5661. $monthsRegex,
  5662. array('option' => array('value' => '')),
  5663. '/option',
  5664. '*/select',
  5665. '-',
  5666. array('select' => array('name' => 'Contact[2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5667. $yearsRegex,
  5668. array('option' => array('value' => '')),
  5669. '/option',
  5670. '*/select',
  5671. array('select' => array('name' => 'Contact[2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5672. $hoursRegex,
  5673. array('option' => array('value' => '')),
  5674. '/option',
  5675. '*/select',
  5676. ':',
  5677. array('select' => array('name' => 'Contact[2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5678. $minutesRegex,
  5679. array('option' => array('value' => '')),
  5680. '/option',
  5681. '*/select',
  5682. ' ',
  5683. array('select' => array('name' => 'Contact[2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5684. $meridianRegex,
  5685. array('option' => array('value' => '')),
  5686. '/option',
  5687. '*/select'
  5688. );
  5689. $this->assertTags($result, $expected);
  5690. }
  5691. /**
  5692. * When changing the date format, the label should always focus the first select box when
  5693. * clicked.
  5694. *
  5695. * @return void
  5696. */
  5697. public function testDateTimeLabelIdMatchesFirstInput() {
  5698. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5699. $this->assertContains('label for="ModelDateMonth"', $result);
  5700. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5701. $this->assertContains('label for="ModelDateDay"', $result);
  5702. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5703. $this->assertContains('label for="ModelDateYear"', $result);
  5704. }
  5705. /**
  5706. * testMonth method
  5707. *
  5708. * @return void
  5709. */
  5710. public function testMonth() {
  5711. $result = $this->Form->month('Model.field');
  5712. $expected = array(
  5713. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5714. array('option' => array('value' => '')),
  5715. '/option',
  5716. array('option' => array('value' => '01')),
  5717. date('F', strtotime('2008-01-01 00:00:00')),
  5718. '/option',
  5719. array('option' => array('value' => '02')),
  5720. date('F', strtotime('2008-02-01 00:00:00')),
  5721. '/option',
  5722. '*/select',
  5723. );
  5724. $this->assertTags($result, $expected);
  5725. $result = $this->Form->month('Model.field', array('empty' => true));
  5726. $expected = array(
  5727. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5728. array('option' => array('value' => '')),
  5729. '/option',
  5730. array('option' => array('value' => '01')),
  5731. date('F', strtotime('2008-01-01 00:00:00')),
  5732. '/option',
  5733. array('option' => array('value' => '02')),
  5734. date('F', strtotime('2008-02-01 00:00:00')),
  5735. '/option',
  5736. '*/select',
  5737. );
  5738. $this->assertTags($result, $expected);
  5739. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5740. $expected = array(
  5741. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5742. array('option' => array('value' => '')),
  5743. '/option',
  5744. array('option' => array('value' => '01')),
  5745. '01',
  5746. '/option',
  5747. array('option' => array('value' => '02')),
  5748. '02',
  5749. '/option',
  5750. '*/select',
  5751. );
  5752. $this->assertTags($result, $expected);
  5753. $monthNames = array(
  5754. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5755. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5756. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5757. $expected = array(
  5758. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5759. array('option' => array('value' => '')),
  5760. '/option',
  5761. array('option' => array('value' => '01')),
  5762. 'Jan',
  5763. '/option',
  5764. array('option' => array('value' => '02')),
  5765. 'Feb',
  5766. '/option',
  5767. '*/select',
  5768. );
  5769. $this->assertTags($result, $expected);
  5770. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5771. $result = $this->Form->month('Project.release');
  5772. $expected = array(
  5773. array('select' => array('name' => 'Project[release][month]', 'id' => 'ProjectReleaseMonth')),
  5774. array('option' => array('value' => '')),
  5775. '/option',
  5776. array('option' => array('value' => '01')),
  5777. 'January',
  5778. '/option',
  5779. array('option' => array('value' => '02', 'selected' => 'selected')),
  5780. 'February',
  5781. '/option',
  5782. '*/select',
  5783. );
  5784. $this->assertTags($result, $expected);
  5785. }
  5786. /**
  5787. * testDay method
  5788. *
  5789. * @return void
  5790. */
  5791. public function testDay() {
  5792. extract($this->dateRegex);
  5793. $result = $this->Form->day('Model.field', array('value' => false));
  5794. $expected = array(
  5795. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  5796. array('option' => array('value' => '')),
  5797. '/option',
  5798. array('option' => array('value' => '01')),
  5799. '1',
  5800. '/option',
  5801. array('option' => array('value' => '02')),
  5802. '2',
  5803. '/option',
  5804. $daysRegex,
  5805. '/select',
  5806. );
  5807. $this->assertTags($result, $expected);
  5808. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5809. $result = $this->Form->day('Model.field');
  5810. $expected = array(
  5811. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  5812. array('option' => array('value' => '')),
  5813. '/option',
  5814. array('option' => array('value' => '01')),
  5815. '1',
  5816. '/option',
  5817. array('option' => array('value' => '02')),
  5818. '2',
  5819. '/option',
  5820. $daysRegex,
  5821. array('option' => array('value' => '10', 'selected' => 'selected')),
  5822. '10',
  5823. '/option',
  5824. $daysRegex,
  5825. '/select',
  5826. );
  5827. $this->assertTags($result, $expected);
  5828. $this->Form->request->data['Model']['field'] = '';
  5829. $result = $this->Form->day('Model.field', array('value' => '10'));
  5830. $expected = array(
  5831. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  5832. array('option' => array('value' => '')),
  5833. '/option',
  5834. array('option' => array('value' => '01')),
  5835. '1',
  5836. '/option',
  5837. array('option' => array('value' => '02')),
  5838. '2',
  5839. '/option',
  5840. $daysRegex,
  5841. array('option' => array('value' => '10', 'selected' => 'selected')),
  5842. '10',
  5843. '/option',
  5844. $daysRegex,
  5845. '/select',
  5846. );
  5847. $this->assertTags($result, $expected);
  5848. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5849. $result = $this->Form->day('Model.field', array('value' => true));
  5850. $expected = array(
  5851. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  5852. array('option' => array('value' => '')),
  5853. '/option',
  5854. array('option' => array('value' => '01')),
  5855. '1',
  5856. '/option',
  5857. array('option' => array('value' => '02')),
  5858. '2',
  5859. '/option',
  5860. $daysRegex,
  5861. array('option' => array('value' => '10', 'selected' => 'selected')),
  5862. '10',
  5863. '/option',
  5864. $daysRegex,
  5865. '/select',
  5866. );
  5867. $this->assertTags($result, $expected);
  5868. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5869. $result = $this->Form->day('Project.release');
  5870. $expected = array(
  5871. array('select' => array('name' => 'Project[release][day]', 'id' => 'ProjectReleaseDay')),
  5872. array('option' => array('value' => '')),
  5873. '/option',
  5874. array('option' => array('value' => '01')),
  5875. '1',
  5876. '/option',
  5877. array('option' => array('value' => '02')),
  5878. '2',
  5879. '/option',
  5880. $daysRegex,
  5881. array('option' => array('value' => '10', 'selected' => 'selected')),
  5882. '10',
  5883. '/option',
  5884. $daysRegex,
  5885. '/select',
  5886. );
  5887. $this->assertTags($result, $expected);
  5888. }
  5889. /**
  5890. * testMinute method
  5891. *
  5892. * @return void
  5893. */
  5894. public function testMinute() {
  5895. extract($this->dateRegex);
  5896. $result = $this->Form->minute('Model.field');
  5897. $expected = array(
  5898. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5899. array('option' => array('value' => '')),
  5900. '/option',
  5901. array('option' => array('value' => '00')),
  5902. '00',
  5903. '/option',
  5904. array('option' => array('value' => '01')),
  5905. '01',
  5906. '/option',
  5907. array('option' => array('value' => '02')),
  5908. '02',
  5909. '/option',
  5910. $minutesRegex,
  5911. '/select',
  5912. );
  5913. $this->assertTags($result, $expected);
  5914. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5915. $result = $this->Form->minute('Model.field');
  5916. $expected = array(
  5917. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5918. array('option' => array('value' => '')),
  5919. '/option',
  5920. array('option' => array('value' => '00')),
  5921. '00',
  5922. '/option',
  5923. array('option' => array('value' => '01')),
  5924. '01',
  5925. '/option',
  5926. array('option' => array('value' => '02')),
  5927. '02',
  5928. '/option',
  5929. $minutesRegex,
  5930. array('option' => array('value' => '12', 'selected' => 'selected')),
  5931. '12',
  5932. '/option',
  5933. $minutesRegex,
  5934. '/select',
  5935. );
  5936. $this->assertTags($result, $expected);
  5937. $this->Form->request->data['Model']['field'] = '';
  5938. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5939. $expected = array(
  5940. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5941. array('option' => array('value' => '')),
  5942. '/option',
  5943. array('option' => array('value' => '00')),
  5944. '00',
  5945. '/option',
  5946. array('option' => array('value' => '05')),
  5947. '05',
  5948. '/option',
  5949. array('option' => array('value' => '10')),
  5950. '10',
  5951. '/option',
  5952. $minutesRegex,
  5953. '/select',
  5954. );
  5955. $this->assertTags($result, $expected);
  5956. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5957. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5958. $expected = array(
  5959. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5960. array('option' => array('value' => '')),
  5961. '/option',
  5962. array('option' => array('value' => '00')),
  5963. '00',
  5964. '/option',
  5965. array('option' => array('value' => '05')),
  5966. '05',
  5967. '/option',
  5968. array('option' => array('value' => '10', 'selected' => 'selected')),
  5969. '10',
  5970. '/option',
  5971. $minutesRegex,
  5972. '/select',
  5973. );
  5974. $this->assertTags($result, $expected);
  5975. }
  5976. /**
  5977. * testHour method
  5978. *
  5979. * @return void
  5980. */
  5981. public function testHour() {
  5982. extract($this->dateRegex);
  5983. $result = $this->Form->hour('Model.field', false);
  5984. $expected = array(
  5985. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  5986. array('option' => array('value' => '')),
  5987. '/option',
  5988. array('option' => array('value' => '01')),
  5989. '1',
  5990. '/option',
  5991. array('option' => array('value' => '02')),
  5992. '2',
  5993. '/option',
  5994. $hoursRegex,
  5995. '/select',
  5996. );
  5997. $this->assertTags($result, $expected);
  5998. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5999. $result = $this->Form->hour('Model.field', false);
  6000. $expected = array(
  6001. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  6002. array('option' => array('value' => '')),
  6003. '/option',
  6004. array('option' => array('value' => '01')),
  6005. '1',
  6006. '/option',
  6007. array('option' => array('value' => '02')),
  6008. '2',
  6009. '/option',
  6010. $hoursRegex,
  6011. array('option' => array('value' => '12', 'selected' => 'selected')),
  6012. '12',
  6013. '/option',
  6014. '/select',
  6015. );
  6016. $this->assertTags($result, $expected);
  6017. $this->Form->request->data['Model']['field'] = '';
  6018. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  6019. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6020. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  6021. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6022. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6023. $result = $this->Form->hour('Model.field', true);
  6024. $expected = array(
  6025. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  6026. array('option' => array('value' => '')),
  6027. '/option',
  6028. array('option' => array('value' => '00', 'selected' => 'selected')),
  6029. '0',
  6030. '/option',
  6031. array('option' => array('value' => '01')),
  6032. '1',
  6033. '/option',
  6034. array('option' => array('value' => '02')),
  6035. '2',
  6036. '/option',
  6037. $hoursRegex,
  6038. '/select',
  6039. );
  6040. $this->assertTags($result, $expected);
  6041. unset($this->Form->request->data['Model']['field']);
  6042. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  6043. $thisHour = date('H');
  6044. $optValue = date('G');
  6045. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6046. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6047. $result = $this->Form->hour('Model.field', true);
  6048. $expected = array(
  6049. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  6050. array('option' => array('value' => '')),
  6051. '/option',
  6052. array('option' => array('value' => '00')),
  6053. '0',
  6054. '/option',
  6055. array('option' => array('value' => '01', 'selected' => 'selected')),
  6056. '1',
  6057. '/option',
  6058. array('option' => array('value' => '02')),
  6059. '2',
  6060. '/option',
  6061. $hoursRegex,
  6062. '/select',
  6063. );
  6064. $this->assertTags($result, $expected);
  6065. }
  6066. /**
  6067. * testYear method
  6068. *
  6069. * @return void
  6070. */
  6071. public function testYear() {
  6072. $result = $this->Form->year('Model.field', 2006, 2007);
  6073. $expected = array(
  6074. array('select' => array('name' => 'Model[field][year]', 'id' => 'ModelFieldYear')),
  6075. array('option' => array('value' => '')),
  6076. '/option',
  6077. array('option' => array('value' => '2007')),
  6078. '2007',
  6079. '/option',
  6080. array('option' => array('value' => '2006')),
  6081. '2006',
  6082. '/option',
  6083. '/select',
  6084. );
  6085. $this->assertTags($result, $expected);
  6086. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  6087. $expected = array(
  6088. array('select' => array('name' => 'Model[field][year]', 'id' => 'ModelFieldYear')),
  6089. array('option' => array('value' => '')),
  6090. '/option',
  6091. array('option' => array('value' => '2006')),
  6092. '2006',
  6093. '/option',
  6094. array('option' => array('value' => '2007')),
  6095. '2007',
  6096. '/option',
  6097. '/select',
  6098. );
  6099. $this->assertTags($result, $expected);
  6100. $this->request->data['Contact']['published'] = '';
  6101. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6102. $expected = array(
  6103. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6104. array('option' => array('value' => '')),
  6105. '/option',
  6106. array('option' => array('value' => '2007')),
  6107. '2007',
  6108. '/option',
  6109. array('option' => array('value' => '2006')),
  6110. '2006',
  6111. '/option',
  6112. '/select',
  6113. );
  6114. $this->assertTags($result, $expected);
  6115. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6116. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6117. $expected = array(
  6118. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6119. array('option' => array('value' => '2007')),
  6120. '2007',
  6121. '/option',
  6122. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6123. '2006',
  6124. '/option',
  6125. '/select',
  6126. );
  6127. $this->assertTags($result, $expected);
  6128. $this->Form->request->data['Contact']['published'] = '';
  6129. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6130. $expected = array(
  6131. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6132. array('option' => array('value' => '')),
  6133. '/option',
  6134. array('option' => array('value' => '2007')),
  6135. '2007',
  6136. '/option',
  6137. array('option' => array('value' => '2006')),
  6138. '2006',
  6139. '/option',
  6140. '/select',
  6141. );
  6142. $this->assertTags($result, $expected);
  6143. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6144. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6145. $expected = array(
  6146. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6147. array('option' => array('value' => '2007')),
  6148. '2007',
  6149. '/option',
  6150. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6151. '2006',
  6152. '/option',
  6153. '/select',
  6154. );
  6155. $this->assertTags($result, $expected);
  6156. $this->Form->request->data['Contact']['published'] = '';
  6157. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6158. $expected = array(
  6159. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6160. array('option' => array('value' => '')),
  6161. '/option',
  6162. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6163. '2007',
  6164. '/option',
  6165. array('option' => array('value' => '2006')),
  6166. '2006',
  6167. '/option',
  6168. '/select',
  6169. );
  6170. $this->assertTags($result, $expected);
  6171. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6172. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6173. $expected = array(
  6174. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6175. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6176. '2007',
  6177. '/option',
  6178. array('option' => array('value' => '2006')),
  6179. '2006',
  6180. '/option',
  6181. '/select',
  6182. );
  6183. $this->assertTags($result, $expected);
  6184. $this->Form->request->data['Contact']['published'] = '';
  6185. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6186. $expected = array(
  6187. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6188. array('option' => array('value' => '2008')),
  6189. '2008',
  6190. '/option',
  6191. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6192. '2007',
  6193. '/option',
  6194. array('option' => array('value' => '2006')),
  6195. '2006',
  6196. '/option',
  6197. '/select',
  6198. );
  6199. $this->assertTags($result, $expected);
  6200. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6201. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6202. $expected = array(
  6203. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6204. array('option' => array('value' => '2008')),
  6205. '2008',
  6206. '/option',
  6207. array('option' => array('value' => '2007')),
  6208. '2007',
  6209. '/option',
  6210. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6211. '2006',
  6212. '/option',
  6213. '/select',
  6214. );
  6215. $this->assertTags($result, $expected);
  6216. $this->Form->request->data = array();
  6217. $this->Form->create('Contact');
  6218. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6219. $expected = array(
  6220. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6221. array('option' => array('value' => '2008')),
  6222. '2008',
  6223. '/option',
  6224. array('option' => array('value' => '2007')),
  6225. '2007',
  6226. '/option',
  6227. array('option' => array('value' => '2006')),
  6228. '2006',
  6229. '/option',
  6230. '/select',
  6231. );
  6232. $this->assertTags($result, $expected);
  6233. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6234. $this->assertContains('Contact[published][year]', $result);
  6235. }
  6236. /**
  6237. * testYearAutoExpandRange method
  6238. *
  6239. * @return void
  6240. */
  6241. public function testYearAutoExpandRange() {
  6242. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6243. $result = $this->Form->year('User.birthday');
  6244. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6245. $result = $matches[1];
  6246. $expected = range(date('Y') + 20, 1930);
  6247. $this->assertEquals($expected, $result);
  6248. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6249. $result = $this->Form->year('Project.release');
  6250. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6251. $result = $matches[1];
  6252. $expected = range(2050, date('Y') - 20);
  6253. $this->assertEquals($expected, $result);
  6254. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6255. $result = $this->Form->year('Project.release', 1890, 1900);
  6256. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6257. $result = $matches[1];
  6258. $expected = range(1900, 1881);
  6259. $this->assertEquals($expected, $result);
  6260. }
  6261. /**
  6262. * testInputDate method
  6263. *
  6264. * Test various inputs with type date and different dateFormat values.
  6265. * Failing to provide a dateFormat key should not error.
  6266. * It should simply not pre-select any value then.
  6267. *
  6268. * @return void
  6269. */
  6270. public function testInputDate() {
  6271. $this->Form->request->data = array(
  6272. 'User' => array(
  6273. 'month_year' => array('month' => date('m')),
  6274. 'just_year' => array('month' => date('m')),
  6275. 'just_month' => array('year' => date('Y')),
  6276. 'just_day' => array('month' => date('m')),
  6277. )
  6278. );
  6279. $this->Form->create('User');
  6280. $result = $this->Form->input('month_year',
  6281. array(
  6282. 'label' => false,
  6283. 'div' => false,
  6284. 'type' => 'date',
  6285. 'dateFormat' => 'MY',
  6286. 'minYear' => 2006,
  6287. 'maxYear' => 2008
  6288. )
  6289. );
  6290. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6291. $this->assertNotContains('value="2008" selected="selected"', $result);
  6292. $result = $this->Form->input('just_year',
  6293. array(
  6294. 'type' => 'date',
  6295. 'label' => false,
  6296. 'dateFormat' => 'Y',
  6297. 'minYear' => date('Y'),
  6298. 'maxYear' => date('Y', strtotime('+20 years'))
  6299. )
  6300. );
  6301. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6302. $result = $this->Form->input('just_month',
  6303. array(
  6304. 'type' => 'date',
  6305. 'label' => false,
  6306. 'dateFormat' => 'M',
  6307. 'empty' => false,
  6308. )
  6309. );
  6310. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6311. $result = $this->Form->input('just_day',
  6312. array(
  6313. 'type' => 'date',
  6314. 'label' => false,
  6315. 'dateFormat' => 'D',
  6316. 'empty' => false,
  6317. )
  6318. );
  6319. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6320. }
  6321. /**
  6322. * testInputDateMaxYear method
  6323. *
  6324. * Let's say we want to only allow users born from 2006 to 2008 to register
  6325. * This being the first singup page, we still don't have any data
  6326. *
  6327. * @return void
  6328. */
  6329. public function testInputDateMaxYear() {
  6330. $this->Form->request->data = array();
  6331. $this->Form->create('User');
  6332. $result = $this->Form->input('birthday',
  6333. array(
  6334. 'label' => false,
  6335. 'div' => false,
  6336. 'type' => 'date',
  6337. 'dateFormat' => 'DMY',
  6338. 'minYear' => 2006,
  6339. 'maxYear' => 2008
  6340. )
  6341. );
  6342. $this->assertContains('value="2008" selected="selected"', $result);
  6343. }
  6344. /**
  6345. * testTextArea method
  6346. *
  6347. * @return void
  6348. */
  6349. public function testTextArea() {
  6350. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6351. $result = $this->Form->textarea('Model.field');
  6352. $expected = array(
  6353. 'textarea' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  6354. 'some test data',
  6355. '/textarea',
  6356. );
  6357. $this->assertTags($result, $expected);
  6358. $result = $this->Form->textarea('Model.tmp');
  6359. $expected = array(
  6360. 'textarea' => array('name' => 'Model[tmp]', 'id' => 'ModelTmp'),
  6361. '/textarea',
  6362. );
  6363. $this->assertTags($result, $expected);
  6364. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6365. $result = $this->Form->textarea('Model.field');
  6366. $expected = array(
  6367. 'textarea' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  6368. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6369. '/textarea',
  6370. );
  6371. $this->assertTags($result, $expected);
  6372. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6373. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6374. $expected = array(
  6375. 'textarea' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  6376. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6377. '/textarea',
  6378. );
  6379. $this->assertTags($result, $expected);
  6380. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6381. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6382. $expected = array(
  6383. 'textarea' => array('name' => 'Model[0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6384. '/textarea'
  6385. );
  6386. $this->assertTags($result, $expected);
  6387. }
  6388. /**
  6389. * testTextAreaWithStupidCharacters method
  6390. *
  6391. * test text area with non-ascii characters
  6392. *
  6393. * @return void
  6394. */
  6395. public function testTextAreaWithStupidCharacters() {
  6396. $this->loadFixtures('Post');
  6397. $result = $this->Form->input('Post.content', array(
  6398. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6399. ));
  6400. $expected = array(
  6401. 'div' => array('class' => 'input textarea'),
  6402. 'label' => array('for' => 'PostContent'),
  6403. 'Current Text',
  6404. '/label',
  6405. 'textarea' => array('name' => 'Post[content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6406. 'GREAT®',
  6407. '/textarea',
  6408. '/div'
  6409. );
  6410. $this->assertTags($result, $expected);
  6411. }
  6412. /**
  6413. * testHiddenField method
  6414. *
  6415. * @return void
  6416. */
  6417. public function testHiddenField() {
  6418. $Contact = ClassRegistry::getObject('Contact');
  6419. $Contact->validationErrors['field'] = 1;
  6420. $this->Form->request->data['Contact']['field'] = 'test';
  6421. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6422. $this->assertTags($result, array(
  6423. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'id' => 'theID', 'value' => 'test'))
  6424. );
  6425. }
  6426. /**
  6427. * testFileUploadField method
  6428. *
  6429. * @return void
  6430. */
  6431. public function testFileUploadField() {
  6432. $result = $this->Form->file('Model.upload');
  6433. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload')));
  6434. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6435. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6436. $expected = array(
  6437. 'div' => array('class' => 'input file'),
  6438. 'label' => array('for' => 'ModelUpload'),
  6439. 'Upload',
  6440. '/label',
  6441. 'input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload'),
  6442. '/div'
  6443. );
  6444. $this->assertTags($result, $expected);
  6445. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6446. $result = $this->Form->file('Model.upload');
  6447. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload')));
  6448. }
  6449. /**
  6450. * test File upload input on a model not used in create();
  6451. *
  6452. * @return void
  6453. */
  6454. public function testFileUploadOnOtherModel() {
  6455. $this->Form->create('ValidateUser', array('type' => 'file'));
  6456. $result = $this->Form->file('ValidateProfile.city');
  6457. $expected = array(
  6458. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]', 'id' => 'ValidateProfileCity')
  6459. );
  6460. $this->assertTags($result, $expected);
  6461. }
  6462. /**
  6463. * testButton method
  6464. *
  6465. * @return void
  6466. */
  6467. public function testButton() {
  6468. $result = $this->Form->button('Hi');
  6469. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6470. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6471. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6472. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6473. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6474. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6475. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6476. $result = $this->Form->button('No type', array('type' => false));
  6477. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6478. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6479. $this->assertNotRegExp('/\&039/', $result);
  6480. }
  6481. /**
  6482. * Test that button() makes unlocked fields by default.
  6483. *
  6484. * @return void
  6485. */
  6486. public function testButtonUnlockedByDefault() {
  6487. $this->Form->request->params['_Token']['key'] = 'secured';
  6488. $this->Form->button('Save', array('name' => 'save'));
  6489. $this->Form->button('Clear');
  6490. $result = $this->Form->unlockField();
  6491. $this->assertEquals(array('save'), $result);
  6492. }
  6493. /**
  6494. * testPostButton method
  6495. *
  6496. * @return void
  6497. */
  6498. public function testPostButton() {
  6499. $result = $this->Form->postButton('Hi', '/controller/action');
  6500. $this->assertTags($result, array(
  6501. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6502. 'div' => array('style' => 'display:none;'),
  6503. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6504. '/div',
  6505. 'button' => array('type' => 'submit'),
  6506. 'Hi',
  6507. '/button',
  6508. '/form'
  6509. ));
  6510. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6511. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"/>') !== false);
  6512. }
  6513. /**
  6514. * Test that postButton adds _Token fields.
  6515. *
  6516. * @return void
  6517. */
  6518. public function testSecurePostButton() {
  6519. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6520. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6521. $expected = array(
  6522. 'form' => array(
  6523. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6524. ),
  6525. array('div' => array('style' => 'display:none;')),
  6526. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6527. array('input' => array('type' => 'hidden', 'name' => '_Token[key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6528. '/div',
  6529. 'button' => array('type' => 'submit'),
  6530. 'Delete',
  6531. '/button',
  6532. array('div' => array('style' => 'display:none;')),
  6533. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6534. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6535. '/div',
  6536. '/form',
  6537. );
  6538. $this->assertTags($result, $expected);
  6539. }
  6540. /**
  6541. * testPostLink method
  6542. *
  6543. * @return void
  6544. */
  6545. public function testPostLink() {
  6546. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6547. $this->assertTags($result, array(
  6548. 'form' => array(
  6549. 'method' => 'post', 'action' => '/posts/delete/1',
  6550. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6551. ),
  6552. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6553. '/form',
  6554. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6555. 'Delete',
  6556. '/a'
  6557. ));
  6558. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6559. $this->assertTags($result, array(
  6560. 'form' => array(
  6561. 'method' => 'post', 'action' => '/posts/delete/1',
  6562. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6563. ),
  6564. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6565. '/form',
  6566. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6567. 'Delete',
  6568. '/a'
  6569. ));
  6570. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6571. $this->assertTags($result, array(
  6572. 'form' => array(
  6573. 'method' => 'post', 'action' => '/posts/delete/1',
  6574. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6575. ),
  6576. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6577. '/form',
  6578. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6579. 'Delete',
  6580. '/a'
  6581. ));
  6582. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  6583. $this->assertTags($result, array(
  6584. 'form' => array(
  6585. 'method' => 'post', 'action' => '/posts/delete/1',
  6586. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6587. ),
  6588. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6589. '/form',
  6590. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;&#039;Confirm&#039; this \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6591. 'Delete',
  6592. '/a'
  6593. ));
  6594. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6595. $this->assertContains('<input type="hidden" name="id" value="1"/>', $result);
  6596. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6597. $this->assertTags($result, array(
  6598. 'form' => array(
  6599. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6600. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6601. ),
  6602. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6603. '/form',
  6604. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6605. 'Delete',
  6606. '/a'
  6607. ));
  6608. $result = $this->Form->postLink(
  6609. '',
  6610. array('controller' => 'items', 'action' => 'delete', 10),
  6611. array('class' => 'btn btn-danger', 'escape' => false),
  6612. 'Confirm thing'
  6613. );
  6614. $this->assertTags($result, array(
  6615. 'form' => array(
  6616. 'method' => 'post', 'action' => '/items/delete/10',
  6617. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6618. ),
  6619. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6620. '/form',
  6621. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6622. '/a'
  6623. ));
  6624. }
  6625. /**
  6626. * Test that postLink adds _Token fields.
  6627. *
  6628. * @return void
  6629. */
  6630. public function testSecurePostLink() {
  6631. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6632. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6633. $expected = array(
  6634. 'form' => array(
  6635. 'method' => 'post', 'action' => '/posts/delete/1',
  6636. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6637. ),
  6638. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6639. array('input' => array('type' => 'hidden', 'name' => '_Token[key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6640. 'div' => array('style' => 'display:none;'),
  6641. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6642. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6643. '/div',
  6644. '/form',
  6645. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6646. 'Delete',
  6647. '/a'
  6648. );
  6649. $this->assertTags($result, $expected);
  6650. }
  6651. /**
  6652. * testSubmitButton method
  6653. *
  6654. * @return void
  6655. */
  6656. public function testSubmitButton() {
  6657. $result = $this->Form->submit('');
  6658. $expected = array(
  6659. 'div' => array('class' => 'submit'),
  6660. 'input' => array('type' => 'submit', 'value' => ''),
  6661. '/div'
  6662. );
  6663. $this->assertTags($result, $expected);
  6664. $result = $this->Form->submit('Test Submit');
  6665. $expected = array(
  6666. 'div' => array('class' => 'submit'),
  6667. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6668. '/div'
  6669. );
  6670. $this->assertTags($result, $expected);
  6671. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6672. $expected = array(
  6673. 'span' => array('class' => 'submit'),
  6674. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6675. '/span'
  6676. );
  6677. $this->assertTags($result, $expected);
  6678. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6679. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6680. $this->assertTags($result, $expected);
  6681. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6682. $expected = array(
  6683. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6684. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6685. '/div'
  6686. );
  6687. $this->assertTags($result, $expected);
  6688. $result = $this->Form->submit('Next >');
  6689. $expected = array(
  6690. 'div' => array('class' => 'submit'),
  6691. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6692. '/div'
  6693. );
  6694. $this->assertTags($result, $expected);
  6695. $result = $this->Form->submit('Next >', array('escape' => false));
  6696. $expected = array(
  6697. 'div' => array('class' => 'submit'),
  6698. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6699. '/div'
  6700. );
  6701. $this->assertTags($result, $expected);
  6702. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6703. $expected = array(
  6704. 'div' => array('class' => 'submit'),
  6705. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6706. '/div'
  6707. );
  6708. $this->assertTags($result, $expected);
  6709. $before = '--before--';
  6710. $after = '--after--';
  6711. $result = $this->Form->submit('Test', array('before' => $before));
  6712. $expected = array(
  6713. 'div' => array('class' => 'submit'),
  6714. '--before--',
  6715. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6716. '/div'
  6717. );
  6718. $this->assertTags($result, $expected);
  6719. $result = $this->Form->submit('Test', array('after' => $after));
  6720. $expected = array(
  6721. 'div' => array('class' => 'submit'),
  6722. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6723. '--after--',
  6724. '/div'
  6725. );
  6726. $this->assertTags($result, $expected);
  6727. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6728. $expected = array(
  6729. 'div' => array('class' => 'submit'),
  6730. '--before--',
  6731. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6732. '--after--',
  6733. '/div'
  6734. );
  6735. $this->assertTags($result, $expected);
  6736. }
  6737. /**
  6738. * test image submit types.
  6739. *
  6740. * @return void
  6741. */
  6742. public function testSubmitImage() {
  6743. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6744. $expected = array(
  6745. 'div' => array('class' => 'submit'),
  6746. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6747. '/div'
  6748. );
  6749. $this->assertTags($result, $expected);
  6750. $result = $this->Form->submit('/relative/cake.power.gif');
  6751. $expected = array(
  6752. 'div' => array('class' => 'submit'),
  6753. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  6754. '/div'
  6755. );
  6756. $this->assertTags($result, $expected);
  6757. $result = $this->Form->submit('cake.power.gif');
  6758. $expected = array(
  6759. 'div' => array('class' => 'submit'),
  6760. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6761. '/div'
  6762. );
  6763. $this->assertTags($result, $expected);
  6764. $result = $this->Form->submit('Not.an.image');
  6765. $expected = array(
  6766. 'div' => array('class' => 'submit'),
  6767. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6768. '/div'
  6769. );
  6770. $this->assertTags($result, $expected);
  6771. $after = '--after--';
  6772. $before = '--before--';
  6773. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6774. $expected = array(
  6775. 'div' => array('class' => 'submit'),
  6776. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6777. '--after--',
  6778. '/div'
  6779. );
  6780. $this->assertTags($result, $expected);
  6781. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6782. $expected = array(
  6783. 'div' => array('class' => 'submit'),
  6784. '--before--',
  6785. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6786. '/div'
  6787. );
  6788. $this->assertTags($result, $expected);
  6789. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6790. $expected = array(
  6791. 'div' => array('class' => 'submit'),
  6792. '--before--',
  6793. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6794. '--after--',
  6795. '/div'
  6796. );
  6797. $this->assertTags($result, $expected);
  6798. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6799. $expected = array(
  6800. 'div' => array('class' => 'submit'),
  6801. '--before--',
  6802. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6803. '--after--',
  6804. '/div'
  6805. );
  6806. $this->assertTags($result, $expected);
  6807. }
  6808. /**
  6809. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6810. * be submitted at a time.
  6811. *
  6812. * @return void
  6813. */
  6814. public function testSubmitUnlockedByDefault() {
  6815. $this->Form->request->params['_Token']['key'] = 'secured';
  6816. $this->Form->submit('Go go');
  6817. $this->Form->submit('Save', array('name' => 'save'));
  6818. $result = $this->Form->unlockField();
  6819. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6820. }
  6821. /**
  6822. * Test submit image with timestamps.
  6823. *
  6824. * @return void
  6825. */
  6826. public function testSubmitImageTimestamp() {
  6827. Configure::write('Asset.timestamp', 'force');
  6828. $result = $this->Form->submit('cake.power.gif');
  6829. $expected = array(
  6830. 'div' => array('class' => 'submit'),
  6831. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6832. '/div'
  6833. );
  6834. $this->assertTags($result, $expected);
  6835. }
  6836. /**
  6837. * test the create() method
  6838. *
  6839. * @return void
  6840. */
  6841. public function testCreate() {
  6842. $result = $this->Form->create('Contact');
  6843. $encoding = strtolower(Configure::read('App.encoding'));
  6844. $expected = array(
  6845. 'form' => array(
  6846. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6847. 'accept-charset' => $encoding
  6848. ),
  6849. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6850. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6851. '/div'
  6852. );
  6853. $this->assertTags($result, $expected);
  6854. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6855. $expected = array('form' => array(
  6856. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6857. 'accept-charset' => $encoding
  6858. ));
  6859. $this->assertTags($result, $expected);
  6860. $result = $this->Form->create('Contact', array('type' => 'get'));
  6861. $expected = array('form' => array(
  6862. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6863. 'accept-charset' => $encoding
  6864. ));
  6865. $this->assertTags($result, $expected);
  6866. $result = $this->Form->create('Contact', array('type' => 'put'));
  6867. $expected = array(
  6868. 'form' => array(
  6869. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6870. 'accept-charset' => $encoding
  6871. ),
  6872. 'div' => array('style' => 'display:none;'),
  6873. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6874. '/div'
  6875. );
  6876. $this->assertTags($result, $expected);
  6877. $result = $this->Form->create('Contact', array('type' => 'file'));
  6878. $expected = array(
  6879. 'form' => array(
  6880. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6881. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6882. ),
  6883. 'div' => array('style' => 'display:none;'),
  6884. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6885. '/div'
  6886. );
  6887. $this->assertTags($result, $expected);
  6888. $this->Form->request->data['Contact']['id'] = 1;
  6889. $this->Form->request->here = '/contacts/edit/1';
  6890. $this->Form->request['action'] = 'edit';
  6891. $result = $this->Form->create('Contact');
  6892. $expected = array(
  6893. 'form' => array(
  6894. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6895. 'accept-charset' => $encoding
  6896. ),
  6897. 'div' => array('style' => 'display:none;'),
  6898. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6899. '/div'
  6900. );
  6901. $this->assertTags($result, $expected);
  6902. $this->Form->request->data['Contact']['id'] = 1;
  6903. $this->Form->request->here = '/contacts/edit/1';
  6904. $this->Form->request['action'] = 'edit';
  6905. $result = $this->Form->create('Contact', array('type' => 'file'));
  6906. $expected = array(
  6907. 'form' => array(
  6908. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6909. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6910. ),
  6911. 'div' => array('style' => 'display:none;'),
  6912. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6913. '/div'
  6914. );
  6915. $this->assertTags($result, $expected);
  6916. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6917. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6918. $expected = array(
  6919. 'form' => array(
  6920. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6921. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  6922. ),
  6923. 'div' => array('style' => 'display:none;'),
  6924. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6925. '/div'
  6926. );
  6927. $this->assertTags($result, $expected);
  6928. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6929. $expected = array(
  6930. 'form' => array(
  6931. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6932. 'accept-charset' => $encoding
  6933. ),
  6934. 'div' => array('style' => 'display:none;'),
  6935. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6936. '/div'
  6937. );
  6938. $this->assertTags($result, $expected);
  6939. $this->Form->request['action'] = 'add';
  6940. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6941. $expected = array(
  6942. 'form' => array(
  6943. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6944. 'accept-charset' => $encoding
  6945. ),
  6946. 'div' => array('style' => 'display:none;'),
  6947. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6948. '/div'
  6949. );
  6950. $this->assertTags($result, $expected);
  6951. $result = $this->Form->create('User', array('action' => 'login'));
  6952. $expected = array(
  6953. 'form' => array(
  6954. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6955. 'accept-charset' => $encoding
  6956. ),
  6957. 'div' => array('style' => 'display:none;'),
  6958. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6959. '/div'
  6960. );
  6961. $this->assertTags($result, $expected);
  6962. $result = $this->Form->create('User', array('url' => '/users/login'));
  6963. $expected = array(
  6964. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6965. 'div' => array('style' => 'display:none;'),
  6966. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6967. '/div'
  6968. );
  6969. $this->assertTags($result, $expected);
  6970. $this->Form->request['controller'] = 'pages';
  6971. $result = $this->Form->create('User', array('action' => 'signup'));
  6972. $expected = array(
  6973. 'form' => array(
  6974. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6975. 'accept-charset' => $encoding
  6976. ),
  6977. 'div' => array('style' => 'display:none;'),
  6978. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6979. '/div'
  6980. );
  6981. $this->assertTags($result, $expected);
  6982. $this->Form->request->data = array();
  6983. $this->Form->request['controller'] = 'contacts';
  6984. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6985. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6986. $expected = array(
  6987. 'form' => array(
  6988. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6989. 'accept-charset' => 'utf-8'
  6990. ),
  6991. 'div' => array('style' => 'display:none;'),
  6992. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6993. '/div'
  6994. );
  6995. $this->assertTags($result, $expected);
  6996. }
  6997. /**
  6998. * Test the onsubmit option for create()
  6999. *
  7000. * @return void
  7001. */
  7002. public function testCreateOnSubmit() {
  7003. $this->Form->request->data = array();
  7004. $this->Form->request['controller'] = 'contacts';
  7005. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7006. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  7007. $expected = array(
  7008. 'form' => array(
  7009. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  7010. 'accept-charset' => 'utf-8'
  7011. ),
  7012. 'div' => array('style' => 'display:none;'),
  7013. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7014. '/div'
  7015. );
  7016. $this->assertTags($result, $expected);
  7017. $this->Form->request->data = array();
  7018. $this->Form->request['controller'] = 'contacts';
  7019. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7020. $result = $this->Form->create(array(
  7021. 'url' => array('action' => 'index', 'param'),
  7022. 'default' => false,
  7023. 'onsubmit' => 'someFunction();'
  7024. ));
  7025. $expected = array(
  7026. 'form' => array(
  7027. 'id' => 'ContactAddForm', 'method' => 'post',
  7028. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  7029. 'action' => '/contacts/index/param',
  7030. 'accept-charset' => 'utf-8'
  7031. ),
  7032. 'div' => array('style' => 'display:none;'),
  7033. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7034. '/div'
  7035. );
  7036. $this->assertTags($result, $expected);
  7037. }
  7038. /**
  7039. * test create() with automatic url generation
  7040. *
  7041. * @return void
  7042. */
  7043. public function testCreateAutoUrl() {
  7044. $this->Form->request['action'] = 'delete';
  7045. $this->Form->request->here = '/contacts/delete/10';
  7046. $this->Form->request->base = '';
  7047. $result = $this->Form->create('Contact');
  7048. $expected = array(
  7049. 'form' => array(
  7050. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/contacts/delete/10',
  7051. 'accept-charset' => 'utf-8'
  7052. ),
  7053. 'div' => array('style' => 'display:none;'),
  7054. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7055. '/div'
  7056. );
  7057. $this->assertTags($result, $expected);
  7058. }
  7059. /**
  7060. * test create() with a custom route
  7061. *
  7062. * @return void
  7063. */
  7064. public function testCreateCustomRoute() {
  7065. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  7066. $encoding = strtolower(Configure::read('App.encoding'));
  7067. $result = $this->Form->create('User', array('action' => 'login'));
  7068. $expected = array(
  7069. 'form' => array(
  7070. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  7071. 'accept-charset' => $encoding
  7072. ),
  7073. 'div' => array('style' => 'display:none;'),
  7074. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7075. '/div'
  7076. );
  7077. $this->assertTags($result, $expected);
  7078. }
  7079. /**
  7080. * test that inputDefaults are stored and used.
  7081. *
  7082. * @return void
  7083. */
  7084. public function testCreateWithInputDefaults() {
  7085. $this->Form->create('User', array(
  7086. 'inputDefaults' => array(
  7087. 'div' => false,
  7088. 'label' => false,
  7089. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  7090. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  7091. )
  7092. ));
  7093. $result = $this->Form->input('username');
  7094. $expected = array(
  7095. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername')
  7096. );
  7097. $this->assertTags($result, $expected);
  7098. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  7099. $expected = array(
  7100. 'div' => array('class' => 'input text'),
  7101. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  7102. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  7103. '/div'
  7104. );
  7105. $this->assertTags($result, $expected);
  7106. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7107. $expected = array(
  7108. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  7109. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7110. );
  7111. $this->assertTags($result, $expected);
  7112. $this->Form->create('User', array(
  7113. 'inputDefaults' => array(
  7114. 'div' => false,
  7115. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7116. )
  7117. ));
  7118. $result = $this->Form->input('username', array('div' => true));
  7119. $expected = array(
  7120. 'div' => array('class' => 'input text'),
  7121. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7122. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  7123. '/div'
  7124. );
  7125. $this->assertTags($result, $expected);
  7126. }
  7127. /**
  7128. * test automatic accept-charset overriding
  7129. *
  7130. * @return void
  7131. */
  7132. public function testCreateWithAcceptCharset() {
  7133. $result = $this->Form->create('UserForm', array(
  7134. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7135. )
  7136. );
  7137. $expected = array(
  7138. 'form' => array(
  7139. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7140. 'accept-charset' => 'iso-8859-1'
  7141. ),
  7142. 'div' => array('style' => 'display:none;'),
  7143. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7144. '/div'
  7145. );
  7146. $this->assertTags($result, $expected);
  7147. }
  7148. /**
  7149. * Test base form URL when url param is passed with multiple parameters (&)
  7150. *
  7151. */
  7152. public function testCreateQuerystringrequest() {
  7153. $encoding = strtolower(Configure::read('App.encoding'));
  7154. $result = $this->Form->create('Contact', array(
  7155. 'type' => 'post',
  7156. 'escape' => false,
  7157. 'url' => array(
  7158. 'controller' => 'controller',
  7159. 'action' => 'action',
  7160. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7161. )
  7162. ));
  7163. $expected = array(
  7164. 'form' => array(
  7165. 'id' => 'ContactAddForm',
  7166. 'method' => 'post',
  7167. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7168. 'accept-charset' => $encoding
  7169. ),
  7170. 'div' => array('style' => 'display:none;'),
  7171. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7172. '/div'
  7173. );
  7174. $this->assertTags($result, $expected);
  7175. $result = $this->Form->create('Contact', array(
  7176. 'type' => 'post',
  7177. 'url' => array(
  7178. 'controller' => 'controller',
  7179. 'action' => 'action',
  7180. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7181. )
  7182. ));
  7183. $expected = array(
  7184. 'form' => array(
  7185. 'id' => 'ContactAddForm',
  7186. 'method' => 'post',
  7187. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7188. 'accept-charset' => $encoding
  7189. ),
  7190. 'div' => array('style' => 'display:none;'),
  7191. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7192. '/div'
  7193. );
  7194. $this->assertTags($result, $expected);
  7195. }
  7196. /**
  7197. * test that create() doesn't cause errors by multiple id's being in the primary key
  7198. * as could happen with multiple select or checkboxes.
  7199. *
  7200. * @return void
  7201. */
  7202. public function testCreateWithMultipleIdInData() {
  7203. $encoding = strtolower(Configure::read('App.encoding'));
  7204. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7205. $result = $this->Form->create('Contact');
  7206. $expected = array(
  7207. 'form' => array(
  7208. 'id' => 'ContactAddForm',
  7209. 'method' => 'post',
  7210. 'action' => '/contacts/add',
  7211. 'accept-charset' => $encoding
  7212. ),
  7213. 'div' => array('style' => 'display:none;'),
  7214. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7215. '/div'
  7216. );
  7217. $this->assertTags($result, $expected);
  7218. }
  7219. /**
  7220. * test that create() doesn't add in extra passed params.
  7221. *
  7222. * @return void
  7223. */
  7224. public function testCreatePassedArgs() {
  7225. $encoding = strtolower(Configure::read('App.encoding'));
  7226. $this->Form->request->data['Contact']['id'] = 1;
  7227. $result = $this->Form->create('Contact', array(
  7228. 'type' => 'post',
  7229. 'escape' => false,
  7230. 'url' => array(
  7231. 'action' => 'edit',
  7232. 'myparam'
  7233. )
  7234. ));
  7235. $expected = array(
  7236. 'form' => array(
  7237. 'id' => 'ContactAddForm',
  7238. 'method' => 'post',
  7239. 'action' => '/contacts/edit/myparam',
  7240. 'accept-charset' => $encoding
  7241. ),
  7242. 'div' => array('style' => 'display:none;'),
  7243. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7244. '/div'
  7245. );
  7246. $this->assertTags($result, $expected);
  7247. }
  7248. /**
  7249. * test creating a get form, and get form inputs.
  7250. *
  7251. * @return void
  7252. */
  7253. public function testGetFormCreate() {
  7254. $encoding = strtolower(Configure::read('App.encoding'));
  7255. $result = $this->Form->create('Contact', array('type' => 'get'));
  7256. $this->assertTags($result, array('form' => array(
  7257. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7258. 'accept-charset' => $encoding
  7259. )));
  7260. $result = $this->Form->text('Contact.name');
  7261. $this->assertTags($result, array('input' => array(
  7262. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7263. )));
  7264. $result = $this->Form->password('password');
  7265. $this->assertTags($result, array('input' => array(
  7266. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7267. )));
  7268. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7269. $result = $this->Form->text('user_form');
  7270. $this->assertTags($result, array('input' => array(
  7271. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7272. )));
  7273. }
  7274. /**
  7275. * test get form, and inputs when the model param is false
  7276. *
  7277. * @return void
  7278. */
  7279. public function testGetFormWithFalseModel() {
  7280. $encoding = strtolower(Configure::read('App.encoding'));
  7281. $this->Form->request['controller'] = 'contact_test';
  7282. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7283. $expected = array('form' => array(
  7284. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7285. 'accept-charset' => $encoding
  7286. ));
  7287. $this->assertTags($result, $expected);
  7288. $result = $this->Form->text('reason');
  7289. $expected = array(
  7290. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7291. );
  7292. $this->assertTags($result, $expected);
  7293. }
  7294. /**
  7295. * test that datetime() works with GET style forms.
  7296. *
  7297. * @return void
  7298. */
  7299. public function testDateTimeWithGetForms() {
  7300. extract($this->dateRegex);
  7301. $this->Form->create('Contact', array('type' => 'get'));
  7302. $result = $this->Form->datetime('created');
  7303. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7304. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7305. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7306. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7307. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7308. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7309. }
  7310. /**
  7311. * testEditFormWithData method
  7312. *
  7313. * test auto populating form elements from submitted data.
  7314. *
  7315. * @return void
  7316. */
  7317. public function testEditFormWithData() {
  7318. $this->Form->request->data = array('Person' => array(
  7319. 'id' => 1,
  7320. 'first_name' => 'Nate',
  7321. 'last_name' => 'Abele',
  7322. 'email' => 'nate@example.com'
  7323. ));
  7324. $this->Form->request->addParams(array(
  7325. 'models' => array('Person'),
  7326. 'controller' => 'people',
  7327. 'action' => 'add'
  7328. ));
  7329. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7330. $this->Form->create();
  7331. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7332. $expected = array(
  7333. 'input' => array('type' => 'hidden', 'name' => 'People[People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7334. 'select' => array(
  7335. 'name' => 'People[People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7336. ),
  7337. array('option' => array('value' => 1)), 'Nate', '/option',
  7338. array('option' => array('value' => 2)), 'Garrett', '/option',
  7339. array('option' => array('value' => 3)), 'Larry', '/option',
  7340. '/select'
  7341. );
  7342. $this->assertTags($result, $expected);
  7343. }
  7344. /**
  7345. * Test that required fields are created for various types of validation.
  7346. *
  7347. * @return void
  7348. */
  7349. public function testFormInputRequiredDetection() {
  7350. $this->Form->create('Contact');
  7351. $result = $this->Form->input('Contact.non_existing');
  7352. $expected = array(
  7353. 'div' => array('class' => 'input text'),
  7354. 'label' => array('for' => 'ContactNonExisting'),
  7355. 'Non Existing',
  7356. '/label',
  7357. 'input' => array(
  7358. 'type' => 'text', 'name' => 'Contact[non_existing]',
  7359. 'id' => 'ContactNonExisting'
  7360. ),
  7361. '/div'
  7362. );
  7363. $this->assertTags($result, $expected);
  7364. $result = $this->Form->input('Contact.imrequired');
  7365. $expected = array(
  7366. 'div' => array('class' => 'input text required'),
  7367. 'label' => array('for' => 'ContactImrequired'),
  7368. 'Imrequired',
  7369. '/label',
  7370. 'input' => array(
  7371. 'type' => 'text', 'name' => 'Contact[imrequired]',
  7372. 'id' => 'ContactImrequired',
  7373. 'required' => 'required'
  7374. ),
  7375. '/div'
  7376. );
  7377. $this->assertTags($result, $expected);
  7378. $result = $this->Form->input('Contact.imalsorequired');
  7379. $expected = array(
  7380. 'div' => array('class' => 'input text required'),
  7381. 'label' => array('for' => 'ContactImalsorequired'),
  7382. 'Imalsorequired',
  7383. '/label',
  7384. 'input' => array(
  7385. 'type' => 'text', 'name' => 'Contact[imalsorequired]',
  7386. 'id' => 'ContactImalsorequired',
  7387. 'required' => 'required'
  7388. ),
  7389. '/div'
  7390. );
  7391. $this->assertTags($result, $expected);
  7392. $result = $this->Form->input('Contact.imrequiredtoo');
  7393. $expected = array(
  7394. 'div' => array('class' => 'input text required'),
  7395. 'label' => array('for' => 'ContactImrequiredtoo'),
  7396. 'Imrequiredtoo',
  7397. '/label',
  7398. 'input' => array(
  7399. 'type' => 'text', 'name' => 'Contact[imrequiredtoo]',
  7400. 'id' => 'ContactImrequiredtoo',
  7401. 'required' => 'required'
  7402. ),
  7403. '/div'
  7404. );
  7405. $this->assertTags($result, $expected);
  7406. $result = $this->Form->input('Contact.required_one');
  7407. $expected = array(
  7408. 'div' => array('class' => 'input text required'),
  7409. 'label' => array('for' => 'ContactRequiredOne'),
  7410. 'Required One',
  7411. '/label',
  7412. 'input' => array(
  7413. 'type' => 'text', 'name' => 'Contact[required_one]',
  7414. 'id' => 'ContactRequiredOne',
  7415. 'required' => 'required'
  7416. ),
  7417. '/div'
  7418. );
  7419. $this->assertTags($result, $expected);
  7420. $result = $this->Form->input('Contact.string_required');
  7421. $expected = array(
  7422. 'div' => array('class' => 'input text required'),
  7423. 'label' => array('for' => 'ContactStringRequired'),
  7424. 'String Required',
  7425. '/label',
  7426. 'input' => array(
  7427. 'type' => 'text', 'name' => 'Contact[string_required]',
  7428. 'id' => 'ContactStringRequired',
  7429. 'required' => 'required'
  7430. ),
  7431. '/div'
  7432. );
  7433. $this->assertTags($result, $expected);
  7434. $result = $this->Form->input('Contact.imnotrequired');
  7435. $expected = array(
  7436. 'div' => array('class' => 'input text'),
  7437. 'label' => array('for' => 'ContactImnotrequired'),
  7438. 'Imnotrequired',
  7439. '/label',
  7440. 'input' => array(
  7441. 'type' => 'text', 'name' => 'Contact[imnotrequired]',
  7442. 'id' => 'ContactImnotrequired'
  7443. ),
  7444. '/div'
  7445. );
  7446. $this->assertTags($result, $expected);
  7447. $result = $this->Form->input('Contact.imalsonotrequired');
  7448. $expected = array(
  7449. 'div' => array('class' => 'input text'),
  7450. 'label' => array('for' => 'ContactImalsonotrequired'),
  7451. 'Imalsonotrequired',
  7452. '/label',
  7453. 'input' => array(
  7454. 'type' => 'text', 'name' => 'Contact[imalsonotrequired]',
  7455. 'id' => 'ContactImalsonotrequired'
  7456. ),
  7457. '/div'
  7458. );
  7459. $this->assertTags($result, $expected);
  7460. $result = $this->Form->input('Contact.imalsonotrequired2');
  7461. $expected = array(
  7462. 'div' => array('class' => 'input text'),
  7463. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7464. 'Imalsonotrequired2',
  7465. '/label',
  7466. 'input' => array(
  7467. 'type' => 'text', 'name' => 'Contact[imalsonotrequired2]',
  7468. 'id' => 'ContactImalsonotrequired2'
  7469. ),
  7470. '/div'
  7471. );
  7472. $this->assertTags($result, $expected);
  7473. $result = $this->Form->input('Contact.imnotrequiredeither');
  7474. $expected = array(
  7475. 'div' => array('class' => 'input text'),
  7476. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7477. 'Imnotrequiredeither',
  7478. '/label',
  7479. 'input' => array(
  7480. 'type' => 'text', 'name' => 'Contact[imnotrequiredeither]',
  7481. 'id' => 'ContactImnotrequiredeither'
  7482. ),
  7483. '/div'
  7484. );
  7485. $this->assertTags($result, $expected);
  7486. $result = $this->Form->input('Contact.iamrequiredalways');
  7487. $expected = array(
  7488. 'div' => array('class' => 'input text required'),
  7489. 'label' => array('for' => 'ContactIamrequiredalways'),
  7490. 'Iamrequiredalways',
  7491. '/label',
  7492. 'input' => array(
  7493. 'type' => 'text', 'name' => 'Contact[iamrequiredalways]',
  7494. 'id' => 'ContactIamrequiredalways',
  7495. 'required' => 'required'
  7496. ),
  7497. '/div'
  7498. );
  7499. $this->assertTags($result, $expected);
  7500. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7501. $expected = array(
  7502. 'div' => array('class' => 'input checkbox required'),
  7503. array('input' => array(
  7504. 'type' => 'hidden',
  7505. 'name' => 'Contact[boolean_field]',
  7506. 'id' => 'ContactBooleanField_',
  7507. 'value' => '0'
  7508. )),
  7509. array('input' => array(
  7510. 'type' => 'checkbox',
  7511. 'name' => 'Contact[boolean_field]',
  7512. 'value' => '1',
  7513. 'id' => 'ContactBooleanField'
  7514. )),
  7515. 'label' => array('for' => 'ContactBooleanField'),
  7516. 'Boolean Field',
  7517. '/label',
  7518. '/div'
  7519. );
  7520. $this->assertTags($result, $expected);
  7521. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7522. $expected = array(
  7523. 'div' => array('class' => 'input checkbox required'),
  7524. array('input' => array(
  7525. 'type' => 'hidden',
  7526. 'name' => 'Contact[boolean_field]',
  7527. 'id' => 'ContactBooleanField_',
  7528. 'value' => '0'
  7529. )),
  7530. array('input' => array(
  7531. 'type' => 'checkbox',
  7532. 'name' => 'Contact[boolean_field]',
  7533. 'value' => '1',
  7534. 'id' => 'ContactBooleanField',
  7535. 'required' => 'required'
  7536. )),
  7537. 'label' => array('for' => 'ContactBooleanField'),
  7538. 'Boolean Field',
  7539. '/label',
  7540. '/div'
  7541. );
  7542. $this->assertTags($result, $expected);
  7543. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  7544. $expected = array(
  7545. 'div' => array('class' => 'input file required'),
  7546. 'label' => array('for' => 'ContactIamrequiredalways'),
  7547. 'Iamrequiredalways',
  7548. '/label',
  7549. 'input' => array(
  7550. 'type' => 'file',
  7551. 'name' => 'data[Contact][iamrequiredalways]',
  7552. 'id' => 'ContactIamrequiredalways',
  7553. 'required' => 'required'
  7554. ),
  7555. '/div'
  7556. );
  7557. $this->assertTags($result, $expected);
  7558. }
  7559. /**
  7560. * Test that required fields are created when only using ModelValidator::add().
  7561. *
  7562. * @return void
  7563. */
  7564. public function testFormInputRequiredDetectionModelValidator() {
  7565. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  7566. $this->Form->create('ContactTag');
  7567. $result = $this->Form->input('ContactTag.iwillberequired');
  7568. $expected = array(
  7569. 'div' => array('class' => 'input text required'),
  7570. 'label' => array('for' => 'ContactTagIwillberequired'),
  7571. 'Iwillberequired',
  7572. '/label',
  7573. 'input' => array(
  7574. 'name' => 'data[ContactTag][iwillberequired]',
  7575. 'type' => 'text',
  7576. 'id' => 'ContactTagIwillberequired',
  7577. 'required' => 'required'
  7578. ),
  7579. '/div'
  7580. );
  7581. $this->assertTags($result, $expected);
  7582. }
  7583. /**
  7584. * testFormMagicInput method
  7585. *
  7586. * @return void
  7587. */
  7588. public function testFormMagicInput() {
  7589. $encoding = strtolower(Configure::read('App.encoding'));
  7590. $result = $this->Form->create('Contact');
  7591. $expected = array(
  7592. 'form' => array(
  7593. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7594. 'accept-charset' => $encoding
  7595. ),
  7596. 'div' => array('style' => 'display:none;'),
  7597. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7598. '/div'
  7599. );
  7600. $this->assertTags($result, $expected);
  7601. $result = $this->Form->input('name');
  7602. $expected = array(
  7603. 'div' => array('class' => 'input text'),
  7604. 'label' => array('for' => 'ContactName'),
  7605. 'Name',
  7606. '/label',
  7607. 'input' => array(
  7608. 'type' => 'text', 'name' => 'Contact[name]',
  7609. 'id' => 'ContactName', 'maxlength' => '255'
  7610. ),
  7611. '/div'
  7612. );
  7613. $this->assertTags($result, $expected);
  7614. $result = $this->Form->input('non_existing_field_in_contact_model');
  7615. $expected = array(
  7616. 'div' => array('class' => 'input text'),
  7617. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7618. 'Non Existing Field In Contact Model',
  7619. '/label',
  7620. 'input' => array(
  7621. 'type' => 'text', 'name' => 'Contact[non_existing_field_in_contact_model]',
  7622. 'id' => 'ContactNonExistingFieldInContactModel'
  7623. ),
  7624. '/div'
  7625. );
  7626. $this->assertTags($result, $expected);
  7627. $result = $this->Form->input('Address.street');
  7628. $expected = array(
  7629. 'div' => array('class' => 'input text'),
  7630. 'label' => array('for' => 'AddressStreet'),
  7631. 'Street',
  7632. '/label',
  7633. 'input' => array(
  7634. 'type' => 'text', 'name' => 'Address[street]',
  7635. 'id' => 'AddressStreet'
  7636. ),
  7637. '/div'
  7638. );
  7639. $this->assertTags($result, $expected);
  7640. $result = $this->Form->input('Address.non_existing_field_in_model');
  7641. $expected = array(
  7642. 'div' => array('class' => 'input text'),
  7643. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7644. 'Non Existing Field In Model',
  7645. '/label',
  7646. 'input' => array(
  7647. 'type' => 'text', 'name' => 'Address[non_existing_field_in_model]',
  7648. 'id' => 'AddressNonExistingFieldInModel'
  7649. ),
  7650. '/div'
  7651. );
  7652. $this->assertTags($result, $expected);
  7653. $result = $this->Form->input('name', array('div' => false));
  7654. $expected = array(
  7655. 'label' => array('for' => 'ContactName'),
  7656. 'Name',
  7657. '/label',
  7658. 'input' => array(
  7659. 'type' => 'text', 'name' => 'Contact[name]',
  7660. 'id' => 'ContactName', 'maxlength' => '255'
  7661. )
  7662. );
  7663. $this->assertTags($result, $expected);
  7664. extract($this->dateRegex);
  7665. $now = strtotime('now');
  7666. $result = $this->Form->input('Contact.published', array('div' => false));
  7667. $expected = array(
  7668. 'label' => array('for' => 'ContactPublishedMonth'),
  7669. 'Published',
  7670. '/label',
  7671. array('select' => array(
  7672. 'name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth'
  7673. )),
  7674. $monthsRegex,
  7675. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7676. date('F', $now),
  7677. '/option',
  7678. '*/select',
  7679. '-',
  7680. array('select' => array(
  7681. 'name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay'
  7682. )),
  7683. $daysRegex,
  7684. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7685. date('j', $now),
  7686. '/option',
  7687. '*/select',
  7688. '-',
  7689. array('select' => array(
  7690. 'name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear'
  7691. )),
  7692. $yearsRegex,
  7693. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7694. date('Y', $now),
  7695. '*/select'
  7696. );
  7697. $this->assertTags($result, $expected);
  7698. $result = $this->Form->input('Contact.updated', array('div' => false));
  7699. $expected = array(
  7700. 'label' => array('for' => 'ContactUpdatedMonth'),
  7701. 'Updated',
  7702. '/label',
  7703. array('select' => array(
  7704. 'name' => 'Contact[updated][month]', 'id' => 'ContactUpdatedMonth'
  7705. )),
  7706. $monthsRegex,
  7707. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7708. date('F', $now),
  7709. '/option',
  7710. '*/select',
  7711. '-',
  7712. array('select' => array(
  7713. 'name' => 'Contact[updated][day]', 'id' => 'ContactUpdatedDay'
  7714. )),
  7715. $daysRegex,
  7716. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7717. date('j', $now),
  7718. '/option',
  7719. '*/select',
  7720. '-',
  7721. array('select' => array(
  7722. 'name' => 'Contact[updated][year]', 'id' => 'ContactUpdatedYear'
  7723. )),
  7724. $yearsRegex,
  7725. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7726. date('Y', $now),
  7727. '*/select'
  7728. );
  7729. $this->assertTags($result, $expected);
  7730. $result = $this->Form->input('UserForm.stuff');
  7731. $expected = array(
  7732. 'div' => array('class' => 'input text'),
  7733. 'label' => array('for' => 'UserFormStuff'),
  7734. 'Stuff',
  7735. '/label',
  7736. 'input' => array(
  7737. 'type' => 'text', 'name' => 'UserForm[stuff]',
  7738. 'id' => 'UserFormStuff', 'maxlength' => 10
  7739. ),
  7740. '/div'
  7741. );
  7742. $this->assertTags($result, $expected);
  7743. }
  7744. /**
  7745. * testForMagicInputNonExistingNorValidated method
  7746. *
  7747. * @return void
  7748. */
  7749. public function testForMagicInputNonExistingNorValidated() {
  7750. $encoding = strtolower(Configure::read('App.encoding'));
  7751. $result = $this->Form->create('Contact');
  7752. $expected = array(
  7753. 'form' => array(
  7754. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7755. 'accept-charset' => $encoding
  7756. ),
  7757. 'div' => array('style' => 'display:none;'),
  7758. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7759. '/div'
  7760. );
  7761. $this->assertTags($result, $expected);
  7762. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7763. $expected = array(
  7764. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7765. 'Non Existing Nor Validated',
  7766. '/label',
  7767. 'input' => array(
  7768. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  7769. 'id' => 'ContactNonExistingNorValidated'
  7770. )
  7771. );
  7772. $this->assertTags($result, $expected);
  7773. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  7774. 'div' => false, 'value' => 'my value'
  7775. ));
  7776. $expected = array(
  7777. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7778. 'Non Existing Nor Validated',
  7779. '/label',
  7780. 'input' => array(
  7781. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  7782. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  7783. )
  7784. );
  7785. $this->assertTags($result, $expected);
  7786. $this->Form->request->data = array(
  7787. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  7788. ));
  7789. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7790. $expected = array(
  7791. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7792. 'Non Existing Nor Validated',
  7793. '/label',
  7794. 'input' => array(
  7795. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  7796. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  7797. )
  7798. );
  7799. $this->assertTags($result, $expected);
  7800. }
  7801. /**
  7802. * testFormMagicInputLabel method
  7803. *
  7804. * @return void
  7805. */
  7806. public function testFormMagicInputLabel() {
  7807. $encoding = strtolower(Configure::read('App.encoding'));
  7808. $result = $this->Form->create('Contact');
  7809. $expected = array(
  7810. 'form' => array(
  7811. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7812. 'accept-charset' => $encoding
  7813. ),
  7814. 'div' => array('style' => 'display:none;'),
  7815. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7816. '/div'
  7817. );
  7818. $this->assertTags($result, $expected);
  7819. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  7820. $this->assertTags($result, array('input' => array(
  7821. 'name' => 'Contact[name]', 'type' => 'text',
  7822. 'id' => 'ContactName', 'maxlength' => '255')
  7823. ));
  7824. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  7825. $expected = array(
  7826. 'label' => array('for' => 'ContactName'),
  7827. 'My label',
  7828. '/label',
  7829. 'input' => array(
  7830. 'type' => 'text', 'name' => 'Contact[name]',
  7831. 'id' => 'ContactName', 'maxlength' => '255'
  7832. )
  7833. );
  7834. $this->assertTags($result, $expected);
  7835. $result = $this->Form->input('Contact.name', array(
  7836. 'div' => false, 'label' => array('class' => 'mandatory')
  7837. ));
  7838. $expected = array(
  7839. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7840. 'Name',
  7841. '/label',
  7842. 'input' => array(
  7843. 'type' => 'text', 'name' => 'Contact[name]',
  7844. 'id' => 'ContactName', 'maxlength' => '255'
  7845. )
  7846. );
  7847. $this->assertTags($result, $expected);
  7848. $result = $this->Form->input('Contact.name', array(
  7849. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7850. ));
  7851. $expected = array(
  7852. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7853. 'My label',
  7854. '/label',
  7855. 'input' => array(
  7856. 'type' => 'text', 'name' => 'Contact[name]',
  7857. 'id' => 'ContactName', 'maxlength' => '255'
  7858. )
  7859. );
  7860. $this->assertTags($result, $expected);
  7861. $result = $this->Form->input('Contact.name', array(
  7862. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7863. ));
  7864. $expected = array(
  7865. 'label' => array('for' => 'my_id'),
  7866. 'Name',
  7867. '/label',
  7868. 'input' => array(
  7869. 'type' => 'text', 'name' => 'Contact[name]',
  7870. 'id' => 'my_id', 'maxlength' => '255'
  7871. )
  7872. );
  7873. $this->assertTags($result, $expected);
  7874. $result = $this->Form->input('1.id');
  7875. $this->assertTags($result, array('input' => array(
  7876. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  7877. 'id' => 'Contact1Id'
  7878. )));
  7879. $result = $this->Form->input("1.name");
  7880. $expected = array(
  7881. 'div' => array('class' => 'input text'),
  7882. 'label' => array('for' => 'Contact1Name'),
  7883. 'Name',
  7884. '/label',
  7885. 'input' => array(
  7886. 'type' => 'text', 'name' => 'Contact[1][name]',
  7887. 'id' => 'Contact1Name', 'maxlength' => '255'
  7888. ),
  7889. '/div'
  7890. );
  7891. $this->assertTags($result, $expected);
  7892. $result = $this->Form->input('Contact.1.id');
  7893. $this->assertTags($result, array(
  7894. 'input' => array(
  7895. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  7896. 'id' => 'Contact1Id'
  7897. )
  7898. ));
  7899. $result = $this->Form->input("Model.1.name");
  7900. $expected = array(
  7901. 'div' => array('class' => 'input text'),
  7902. 'label' => array('for' => 'Model1Name'),
  7903. 'Name',
  7904. '/label',
  7905. 'input' => array(
  7906. 'type' => 'text', 'name' => 'Model[1][name]',
  7907. 'id' => 'Model1Name'
  7908. ),
  7909. '/div'
  7910. );
  7911. $this->assertTags($result, $expected);
  7912. }
  7913. /**
  7914. * testFormEnd method
  7915. *
  7916. * @return void
  7917. */
  7918. public function testFormEnd() {
  7919. $this->assertEquals('</form>', $this->Form->end());
  7920. $result = $this->Form->end('');
  7921. $expected = array(
  7922. 'div' => array('class' => 'submit'),
  7923. 'input' => array('type' => 'submit', 'value' => ''),
  7924. '/div',
  7925. '/form'
  7926. );
  7927. $this->assertTags($result, $expected);
  7928. $result = $this->Form->end(array('label' => ''));
  7929. $expected = array(
  7930. 'div' => array('class' => 'submit'),
  7931. 'input' => array('type' => 'submit', 'value' => ''),
  7932. '/div',
  7933. '/form'
  7934. );
  7935. $this->assertTags($result, $expected);
  7936. $result = $this->Form->end('save');
  7937. $expected = array(
  7938. 'div' => array('class' => 'submit'),
  7939. 'input' => array('type' => 'submit', 'value' => 'save'),
  7940. '/div',
  7941. '/form'
  7942. );
  7943. $this->assertTags($result, $expected);
  7944. $result = $this->Form->end(array('label' => 'save'));
  7945. $expected = array(
  7946. 'div' => array('class' => 'submit'),
  7947. 'input' => array('type' => 'submit', 'value' => 'save'),
  7948. '/div',
  7949. '/form'
  7950. );
  7951. $this->assertTags($result, $expected);
  7952. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7953. $expected = array(
  7954. 'div' => array('class' => 'submit'),
  7955. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7956. '/div',
  7957. '/form'
  7958. );
  7959. $this->assertTags($result, $expected);
  7960. $result = $this->Form->end(array('name' => 'Whatever'));
  7961. $expected = array(
  7962. 'div' => array('class' => 'submit'),
  7963. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7964. '/div',
  7965. '/form'
  7966. );
  7967. $this->assertTags($result, $expected);
  7968. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7969. $expected = array(
  7970. 'div' => array('class' => 'good'),
  7971. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7972. '/div',
  7973. '/form'
  7974. );
  7975. $this->assertTags($result, $expected);
  7976. $result = $this->Form->end(array(
  7977. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7978. ));
  7979. $expected = array(
  7980. 'div' => array('class' => 'good'),
  7981. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7982. '/div',
  7983. '/form'
  7984. );
  7985. $this->assertTags($result, $expected);
  7986. }
  7987. /**
  7988. * testMultipleFormWithIdFields method
  7989. *
  7990. * @return void
  7991. */
  7992. public function testMultipleFormWithIdFields() {
  7993. $this->Form->create('UserForm');
  7994. $result = $this->Form->input('id');
  7995. $this->assertTags($result, array('input' => array(
  7996. 'type' => 'hidden', 'name' => 'UserForm[id]', 'id' => 'UserFormId'
  7997. )));
  7998. $result = $this->Form->input('ValidateItem.id');
  7999. $this->assertTags($result, array('input' => array(
  8000. 'type' => 'hidden', 'name' => 'ValidateItem[id]',
  8001. 'id' => 'ValidateItemId'
  8002. )));
  8003. $result = $this->Form->input('ValidateUser.id');
  8004. $this->assertTags($result, array('input' => array(
  8005. 'type' => 'hidden', 'name' => 'ValidateUser[id]',
  8006. 'id' => 'ValidateUserId'
  8007. )));
  8008. }
  8009. /**
  8010. * testDbLessModel method
  8011. *
  8012. * @return void
  8013. */
  8014. public function testDbLessModel() {
  8015. $this->Form->create('TestMail');
  8016. $result = $this->Form->input('name');
  8017. $expected = array(
  8018. 'div' => array('class' => 'input text'),
  8019. 'label' => array('for' => 'TestMailName'),
  8020. 'Name',
  8021. '/label',
  8022. 'input' => array(
  8023. 'name' => 'TestMail[name]', 'type' => 'text',
  8024. 'id' => 'TestMailName'
  8025. ),
  8026. '/div'
  8027. );
  8028. $this->assertTags($result, $expected);
  8029. ClassRegistry::init('TestMail');
  8030. $this->Form->create('TestMail');
  8031. $result = $this->Form->input('name');
  8032. $expected = array(
  8033. 'div' => array('class' => 'input text'),
  8034. 'label' => array('for' => 'TestMailName'),
  8035. 'Name',
  8036. '/label',
  8037. 'input' => array(
  8038. 'name' => 'TestMail[name]', 'type' => 'text',
  8039. 'id' => 'TestMailName'
  8040. ),
  8041. '/div'
  8042. );
  8043. $this->assertTags($result, $expected);
  8044. }
  8045. /**
  8046. * testBrokenness method
  8047. *
  8048. * @return void
  8049. */
  8050. public function testBrokenness() {
  8051. /*
  8052. * #4 This test has two parents and four children. By default (as of r7117) both
  8053. * parents are show but the first parent is missing a child. This is the inconsistency
  8054. * in the default behaviour - one parent has all children, the other does not - dependent
  8055. * on the data values.
  8056. */
  8057. $result = $this->Form->select('Model.field', array(
  8058. 'Fred' => array(
  8059. 'freds_son_1' => 'Fred',
  8060. 'freds_son_2' => 'Freddie'
  8061. ),
  8062. 'Bert' => array(
  8063. 'berts_son_1' => 'Albert',
  8064. 'berts_son_2' => 'Bertie')
  8065. ),
  8066. array('showParents' => true, 'empty' => false)
  8067. );
  8068. $expected = array(
  8069. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  8070. array('optgroup' => array('label' => 'Fred')),
  8071. array('option' => array('value' => 'freds_son_1')),
  8072. 'Fred',
  8073. '/option',
  8074. array('option' => array('value' => 'freds_son_2')),
  8075. 'Freddie',
  8076. '/option',
  8077. '/optgroup',
  8078. array('optgroup' => array('label' => 'Bert')),
  8079. array('option' => array('value' => 'berts_son_1')),
  8080. 'Albert',
  8081. '/option',
  8082. array('option' => array('value' => 'berts_son_2')),
  8083. 'Bertie',
  8084. '/option',
  8085. '/optgroup',
  8086. '/select'
  8087. );
  8088. $this->assertTags($result, $expected);
  8089. /*
  8090. * #2 This is structurally identical to the test above (#1) - only the parent name has
  8091. * changed, so we should expect the same select list data, just with a different name
  8092. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  8093. * This is where data corruption can occur, because when a select value is missing from
  8094. * a list a form will substitute the first value in the list - without the user knowing.
  8095. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  8096. * affect the availability of 3 => 'Three' as a valid option.
  8097. */
  8098. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  8099. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  8100. ));
  8101. $result = $this->Form->select(
  8102. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  8103. );
  8104. $expected = array(
  8105. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  8106. array('option' => array('value' => 1)),
  8107. 'One',
  8108. '/option',
  8109. array('option' => array('value' => 2)),
  8110. 'Two',
  8111. '/option',
  8112. array('optgroup' => array('label' => 'Three')),
  8113. array('option' => array('value' => 3)),
  8114. 'Three',
  8115. '/option',
  8116. array('option' => array('value' => 4)),
  8117. 'Four',
  8118. '/option',
  8119. array('option' => array('value' => 5)),
  8120. 'Five',
  8121. '/option',
  8122. '/optgroup',
  8123. '/select'
  8124. );
  8125. $this->assertTags($result, $expected);
  8126. }
  8127. /**
  8128. * Test the generation of fields for a multi record form.
  8129. *
  8130. * @return void
  8131. */
  8132. public function testMultiRecordForm() {
  8133. $this->Form->create('ValidateProfile');
  8134. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8135. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8136. $expected = array(
  8137. 'div' => array('class' => 'input textarea'),
  8138. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8139. 'Name',
  8140. '/label',
  8141. 'textarea' => array(
  8142. 'id' => 'ValidateProfile1ValidateItem2Name',
  8143. 'name' => 'ValidateProfile[1][ValidateItem][2][name]',
  8144. 'cols' => 30,
  8145. 'rows' => 6
  8146. ),
  8147. 'Value',
  8148. '/textarea',
  8149. '/div'
  8150. );
  8151. $this->assertTags($result, $expected);
  8152. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8153. $expected = array(
  8154. 'div' => array('class' => 'input date'),
  8155. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8156. 'Created',
  8157. '/label',
  8158. array('select' => array(
  8159. 'name' => 'ValidateProfile[1][ValidateItem][2][created][month]',
  8160. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8161. )
  8162. ),
  8163. array('option' => array('value' => '')), '/option',
  8164. $this->dateRegex['monthsRegex'],
  8165. '/select', '-',
  8166. array('select' => array(
  8167. 'name' => 'ValidateProfile[1][ValidateItem][2][created][day]',
  8168. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8169. )
  8170. ),
  8171. array('option' => array('value' => '')), '/option',
  8172. $this->dateRegex['daysRegex'],
  8173. '/select', '-',
  8174. array('select' => array(
  8175. 'name' => 'ValidateProfile[1][ValidateItem][2][created][year]',
  8176. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8177. )
  8178. ),
  8179. array('option' => array('value' => '')), '/option',
  8180. $this->dateRegex['yearsRegex'],
  8181. '/select',
  8182. '/div'
  8183. );
  8184. $this->assertTags($result, $expected);
  8185. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8186. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8187. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8188. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8189. $expected = array(
  8190. 'div' => array('class' => 'input select error'),
  8191. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8192. 'Profile',
  8193. '/label',
  8194. 'select' => array(
  8195. 'name' => 'ValidateProfile[1][ValidateItem][2][profile_id]',
  8196. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8197. 'class' => 'form-error'
  8198. ),
  8199. '/select',
  8200. array('div' => array('class' => 'error-message')),
  8201. 'Error',
  8202. '/div',
  8203. '/div'
  8204. );
  8205. $this->assertTags($result, $expected);
  8206. }
  8207. /**
  8208. * test the correct display of multi-record form validation errors.
  8209. *
  8210. * @return void
  8211. */
  8212. public function testMultiRecordFormValidationErrors() {
  8213. $this->Form->create('ValidateProfile');
  8214. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8215. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8216. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8217. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8218. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8219. $result = $this->Form->error('ValidateProfile.2.city');
  8220. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8221. $result = $this->Form->error('2.city');
  8222. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8223. }
  8224. /**
  8225. * test the correct display of multi-record form validation errors.
  8226. *
  8227. * @return void
  8228. */
  8229. public function testSaveManyRecordFormValidationErrors() {
  8230. $this->Form->create('ValidateUser');
  8231. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8232. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8233. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8234. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8235. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8236. $result = $this->Form->error('ValidateUser.0.city');
  8237. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8238. }
  8239. /**
  8240. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8241. *
  8242. * @return void
  8243. */
  8244. public function testInputTemplate() {
  8245. $result = $this->Form->input('Contact.email', array(
  8246. 'type' => 'text', 'format' => array('input')
  8247. ));
  8248. $expected = array(
  8249. 'div' => array('class' => 'input text'),
  8250. 'input' => array(
  8251. 'type' => 'text', 'name' => 'Contact[email]',
  8252. 'id' => 'ContactEmail'
  8253. ),
  8254. '/div'
  8255. );
  8256. $this->assertTags($result, $expected);
  8257. $result = $this->Form->input('Contact.email', array(
  8258. 'type' => 'text', 'format' => array('input', 'label'),
  8259. 'label' => '<em>Email (required)</em>'
  8260. ));
  8261. $expected = array(
  8262. 'div' => array('class' => 'input text'),
  8263. array('input' => array(
  8264. 'type' => 'text', 'name' => 'Contact[email]',
  8265. 'id' => 'ContactEmail'
  8266. )),
  8267. 'label' => array('for' => 'ContactEmail'),
  8268. 'em' => array(),
  8269. 'Email (required)',
  8270. '/em',
  8271. '/label',
  8272. '/div'
  8273. );
  8274. $this->assertTags($result, $expected);
  8275. $result = $this->Form->input('Contact.email', array(
  8276. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8277. 'between' => '<div>Something in the middle</div>',
  8278. 'after' => '<span>Some text at the end</span>'
  8279. ));
  8280. $expected = array(
  8281. 'div' => array('class' => 'input text'),
  8282. array('input' => array(
  8283. 'type' => 'text', 'name' => 'Contact[email]',
  8284. 'id' => 'ContactEmail'
  8285. )),
  8286. array('div' => array()),
  8287. 'Something in the middle',
  8288. '/div',
  8289. 'label' => array('for' => 'ContactEmail'),
  8290. 'Email',
  8291. '/label',
  8292. 'span' => array(),
  8293. 'Some text at the end',
  8294. '/span',
  8295. '/div'
  8296. );
  8297. $this->assertTags($result, $expected);
  8298. $result = $this->Form->input('Contact.method', array(
  8299. 'type' => 'radio',
  8300. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8301. 'between' => 'I am between',
  8302. ));
  8303. $expected = array(
  8304. 'div' => array('class' => 'input radio'),
  8305. 'fieldset' => array(),
  8306. 'legend' => array(),
  8307. 'Method',
  8308. '/legend',
  8309. 'I am between',
  8310. 'input' => array(
  8311. 'type' => 'hidden', 'name' => 'Contact[method]',
  8312. 'value' => '', 'id' => 'ContactMethod_'
  8313. ),
  8314. array('input' => array(
  8315. 'type' => 'radio', 'name' => 'Contact[method]',
  8316. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8317. )),
  8318. array('label' => array('for' => 'ContactMethodEmail')),
  8319. 'Email',
  8320. '/label',
  8321. array('input' => array(
  8322. 'type' => 'radio', 'name' => 'Contact[method]',
  8323. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8324. )),
  8325. array('label' => array('for' => 'ContactMethodPigeon')),
  8326. 'Pigeon',
  8327. '/label',
  8328. '/fieldset',
  8329. '/div',
  8330. );
  8331. $this->assertTags($result, $expected);
  8332. }
  8333. /**
  8334. * test that some html5 inputs + FormHelper::__call() work
  8335. *
  8336. * @return void
  8337. */
  8338. public function testHtml5Inputs() {
  8339. $result = $this->Form->email('User.email');
  8340. $expected = array(
  8341. 'input' => array('type' => 'email', 'name' => 'User[email]', 'id' => 'UserEmail')
  8342. );
  8343. $this->assertTags($result, $expected);
  8344. $result = $this->Form->search('User.query');
  8345. $expected = array(
  8346. 'input' => array('type' => 'search', 'name' => 'User[query]', 'id' => 'UserQuery')
  8347. );
  8348. $this->assertTags($result, $expected);
  8349. $result = $this->Form->search('User.query', array('value' => 'test'));
  8350. $expected = array(
  8351. 'input' => array('type' => 'search', 'name' => 'User[query]', 'id' => 'UserQuery', 'value' => 'test')
  8352. );
  8353. $this->assertTags($result, $expected);
  8354. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8355. $expected = array(
  8356. 'input' => array('type' => 'text', 'name' => 'User[query]', 'id' => 'UserQuery', 'value' => 'test')
  8357. );
  8358. $this->assertTags($result, $expected);
  8359. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8360. $expected = array(
  8361. 'input' => array('type' => 'url', 'name' => 'User[website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8362. );
  8363. $this->assertTags($result, $expected);
  8364. }
  8365. /**
  8366. *
  8367. * @expectedException Cake\Error\Exception
  8368. * @return void
  8369. */
  8370. public function testHtml5InputException() {
  8371. $this->Form->email();
  8372. }
  8373. /**
  8374. * Tests that a model can be loaded from the model names passed in the request object
  8375. *
  8376. * @return void
  8377. */
  8378. public function testIntrospectModelFromRequest() {
  8379. $this->loadFixtures('Post');
  8380. App::build(array(
  8381. 'Plugin' => array(CAKE . 'Test/TestApp/Plugin/')
  8382. ));
  8383. Plugin::load('TestPlugin');
  8384. $this->Form->request['models'] = array(
  8385. 'TestPluginPost' => array(
  8386. 'plugin' => 'TestPlugin',
  8387. 'className' => 'TestPluginPost'
  8388. )
  8389. );
  8390. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8391. $this->Form->create('TestPluginPost');
  8392. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8393. $this->assertInstanceOf('TestPlugin\Model\TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8394. Plugin::unload();
  8395. }
  8396. /**
  8397. * Tests that it is possible to set the validation errors directly in the helper for a field
  8398. *
  8399. * @return void
  8400. */
  8401. public function testCustomValidationErrors() {
  8402. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8403. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8404. $this->assertEquals('Badness!', $result);
  8405. }
  8406. /**
  8407. * Tests that the 'on' key validates as expected on create
  8408. *
  8409. * @return void
  8410. */
  8411. public function testRequiredOnCreate() {
  8412. $this->Form->create('Contact');
  8413. $result = $this->Form->input('Contact.imrequiredonupdate');
  8414. $expected = array(
  8415. 'div' => array('class' => 'input text'),
  8416. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8417. 'Imrequiredonupdate',
  8418. '/label',
  8419. 'input' => array(
  8420. 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
  8421. 'id' => 'ContactImrequiredonupdate'
  8422. ),
  8423. '/div'
  8424. );
  8425. $this->assertTags($result, $expected);
  8426. $result = $this->Form->input('Contact.imrequiredoncreate');
  8427. $expected = array(
  8428. 'div' => array('class' => 'input text required'),
  8429. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8430. 'Imrequiredoncreate',
  8431. '/label',
  8432. 'input' => array(
  8433. 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
  8434. 'id' => 'ContactImrequiredoncreate',
  8435. 'required' => 'required'
  8436. ),
  8437. '/div'
  8438. );
  8439. $this->assertTags($result, $expected);
  8440. $result = $this->Form->input('Contact.imrequiredonboth');
  8441. $expected = array(
  8442. 'div' => array('class' => 'input text required'),
  8443. 'label' => array('for' => 'ContactImrequiredonboth'),
  8444. 'Imrequiredonboth',
  8445. '/label',
  8446. 'input' => array(
  8447. 'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
  8448. 'id' => 'ContactImrequiredonboth',
  8449. 'required' => 'required'
  8450. ),
  8451. '/div'
  8452. );
  8453. $this->assertTags($result, $expected);
  8454. $this->Form->inputDefaults(array('required' => false));
  8455. $result = $this->Form->input('Contact.imrequired');
  8456. $expected = array(
  8457. 'div' => array('class' => 'input text'),
  8458. 'label' => array('for' => 'ContactImrequired'),
  8459. 'Imrequired',
  8460. '/label',
  8461. 'input' => array(
  8462. 'type' => 'text', 'name' => 'Contact[imrequired]',
  8463. 'id' => 'ContactImrequired'
  8464. ),
  8465. '/div'
  8466. );
  8467. $this->assertTags($result, $expected);
  8468. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8469. $this->assertTags($result, $expected);
  8470. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8471. $expected = array(
  8472. 'div' => array('class' => 'input text required'),
  8473. 'label' => array('for' => 'ContactImrequired'),
  8474. 'Imrequired',
  8475. '/label',
  8476. 'input' => array(
  8477. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8478. 'id' => 'ContactImrequired'
  8479. ),
  8480. '/div'
  8481. );
  8482. $this->assertTags($result, $expected);
  8483. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8484. $this->assertTags($result, $expected);
  8485. }
  8486. /**
  8487. * Tests that the 'on' key validates as expected on update
  8488. *
  8489. * @return void
  8490. */
  8491. public function testRequiredOnUpdate() {
  8492. $this->Form->request->data['Contact']['id'] = 1;
  8493. $this->Form->create('Contact');
  8494. $result = $this->Form->input('Contact.imrequiredonupdate');
  8495. $expected = array(
  8496. 'div' => array('class' => 'input text required'),
  8497. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8498. 'Imrequiredonupdate',
  8499. '/label',
  8500. 'input' => array(
  8501. 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
  8502. 'id' => 'ContactImrequiredonupdate',
  8503. 'required' => 'required'
  8504. ),
  8505. '/div'
  8506. );
  8507. $this->assertTags($result, $expected);
  8508. $result = $this->Form->input('Contact.imrequiredoncreate');
  8509. $expected = array(
  8510. 'div' => array('class' => 'input text'),
  8511. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8512. 'Imrequiredoncreate',
  8513. '/label',
  8514. 'input' => array(
  8515. 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
  8516. 'id' => 'ContactImrequiredoncreate'
  8517. ),
  8518. '/div'
  8519. );
  8520. $this->assertTags($result, $expected);
  8521. $result = $this->Form->input('Contact.imrequiredonboth');
  8522. $expected = array(
  8523. 'div' => array('class' => 'input text required'),
  8524. 'label' => array('for' => 'ContactImrequiredonboth'),
  8525. 'Imrequiredonboth',
  8526. '/label',
  8527. 'input' => array(
  8528. 'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
  8529. 'id' => 'ContactImrequiredonboth',
  8530. 'required' => 'required'
  8531. ),
  8532. '/div'
  8533. );
  8534. $this->assertTags($result, $expected);
  8535. $result = $this->Form->input('Contact.imrequired');
  8536. $expected = array(
  8537. 'div' => array('class' => 'input text required'),
  8538. 'label' => array('for' => 'ContactImrequired'),
  8539. 'Imrequired',
  8540. '/label',
  8541. 'input' => array(
  8542. 'type' => 'text', 'name' => 'Contact[imrequired]',
  8543. 'id' => 'ContactImrequired',
  8544. 'required' => 'required'
  8545. ),
  8546. '/div'
  8547. );
  8548. $this->assertTags($result, $expected);
  8549. }
  8550. /**
  8551. * Test inputDefaults setter and getter
  8552. *
  8553. * @return void
  8554. */
  8555. public function testInputDefaults() {
  8556. $this->Form->create('Contact');
  8557. $this->Form->inputDefaults(array(
  8558. 'label' => false,
  8559. 'div' => array(
  8560. 'style' => 'color: #000;'
  8561. )
  8562. ));
  8563. $result = $this->Form->input('Contact.field1');
  8564. $expected = array(
  8565. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8566. 'input' => array(
  8567. 'type' => 'text', 'name' => 'Contact[field1]',
  8568. 'id' => 'ContactField1'
  8569. ),
  8570. '/div'
  8571. );
  8572. $this->assertTags($result, $expected);
  8573. $this->Form->inputDefaults(array(
  8574. 'div' => false,
  8575. 'label' => 'Label',
  8576. ));
  8577. $result = $this->Form->input('Contact.field1');
  8578. $expected = array(
  8579. 'label' => array('for' => 'ContactField1'),
  8580. 'Label',
  8581. '/label',
  8582. 'input' => array(
  8583. 'type' => 'text', 'name' => 'Contact[field1]',
  8584. 'id' => 'ContactField1'
  8585. ),
  8586. );
  8587. $this->assertTags($result, $expected);
  8588. $this->Form->inputDefaults(array(
  8589. 'label' => false,
  8590. ), true);
  8591. $result = $this->Form->input('Contact.field1');
  8592. $expected = array(
  8593. 'input' => array(
  8594. 'type' => 'text', 'name' => 'Contact[field1]',
  8595. 'id' => 'ContactField1'
  8596. ),
  8597. );
  8598. $this->assertTags($result, $expected);
  8599. $result = $this->Form->inputDefaults();
  8600. $expected = array(
  8601. 'div' => false,
  8602. 'label' => false,
  8603. );
  8604. $this->assertEquals($expected, $result);
  8605. }
  8606. }