FormHelperTest.php 311 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @package Cake.Test.Case.View.Helper
  15. * @since CakePHP(tm) v 1.2.0.4206
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('ClassRegistry', 'Utility');
  19. App::uses('Controller', 'Controller');
  20. App::uses('View', 'View');
  21. App::uses('Model', 'Model');
  22. App::uses('Security', 'Utility');
  23. App::uses('CakeRequest', 'Network');
  24. App::uses('HtmlHelper', 'View/Helper');
  25. App::uses('FormHelper', 'View/Helper');
  26. App::uses('Router', 'Routing');
  27. /**
  28. * ContactTestController class
  29. *
  30. * @package Cake.Test.Case.View.Helper
  31. */
  32. class ContactTestController extends Controller {
  33. /**
  34. * uses property
  35. *
  36. * @var mixed null
  37. */
  38. public $uses = null;
  39. }
  40. /**
  41. * Contact class
  42. *
  43. * @package Cake.Test.Case.View.Helper
  44. */
  45. class Contact extends CakeTestModel {
  46. /**
  47. * useTable property
  48. *
  49. * @var boolean
  50. */
  51. public $useTable = false;
  52. /**
  53. * Default schema
  54. *
  55. * @var array
  56. */
  57. protected $_schema = array(
  58. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  59. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  60. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  61. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  64. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  65. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  66. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  67. );
  68. /**
  69. * validate property
  70. *
  71. * @var array
  72. */
  73. public $validate = array(
  74. 'non_existing' => array(),
  75. 'idontexist' => array(),
  76. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  77. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  78. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  79. 'imrequiredonboth' => array(
  80. 'required' => array('rule' => 'alphaNumeric'),
  81. ),
  82. 'string_required' => 'notEmpty',
  83. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  84. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  85. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  86. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  87. 'imalsonotrequired' => array(
  88. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  89. 'between' => array('rule' => array('between', 5, 30)),
  90. ),
  91. 'imalsonotrequired2' => array(
  92. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  93. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  94. ),
  95. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  96. 'iamrequiredalways' => array(
  97. 'email' => array('rule' => 'email'),
  98. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  99. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  100. ),
  101. 'boolean_field' => array('rule' => 'boolean')
  102. );
  103. /**
  104. * schema method
  105. *
  106. * @return void
  107. */
  108. public function setSchema($schema) {
  109. $this->_schema = $schema;
  110. }
  111. /**
  112. * hasAndBelongsToMany property
  113. *
  114. * @var array
  115. */
  116. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  117. /**
  118. * hasAndBelongsToMany property
  119. *
  120. * @var array
  121. */
  122. public $belongsTo = array('User' => array('className' => 'UserForm'));
  123. }
  124. /**
  125. * ContactTagsContact class
  126. *
  127. * @package Cake.Test.Case.View.Helper
  128. */
  129. class ContactTagsContact extends CakeTestModel {
  130. /**
  131. * useTable property
  132. *
  133. * @var boolean
  134. */
  135. public $useTable = false;
  136. /**
  137. * Default schema
  138. *
  139. * @var array
  140. */
  141. protected $_schema = array(
  142. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  143. 'contact_tag_id' => array(
  144. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  145. )
  146. );
  147. /**
  148. * schema method
  149. *
  150. * @return void
  151. */
  152. public function setSchema($schema) {
  153. $this->_schema = $schema;
  154. }
  155. }
  156. /**
  157. * ContactNonStandardPk class
  158. *
  159. * @package Cake.Test.Case.View.Helper
  160. */
  161. class ContactNonStandardPk extends Contact {
  162. /**
  163. * primaryKey property
  164. *
  165. * @var string
  166. */
  167. public $primaryKey = 'pk';
  168. /**
  169. * schema method
  170. *
  171. * @return void
  172. */
  173. public function schema($field = false) {
  174. $this->_schema = parent::schema();
  175. $this->_schema['pk'] = $this->_schema['id'];
  176. unset($this->_schema['id']);
  177. return $this->_schema;
  178. }
  179. }
  180. /**
  181. * ContactTag class
  182. *
  183. * @package Cake.Test.Case.View.Helper
  184. */
  185. class ContactTag extends Model {
  186. /**
  187. * useTable property
  188. *
  189. * @var boolean
  190. */
  191. public $useTable = false;
  192. /**
  193. * schema definition
  194. *
  195. * @var array
  196. */
  197. protected $_schema = array(
  198. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  199. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  200. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  201. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  202. );
  203. }
  204. /**
  205. * UserForm class
  206. *
  207. * @package Cake.Test.Case.View.Helper
  208. */
  209. class UserForm extends CakeTestModel {
  210. /**
  211. * useTable property
  212. *
  213. * @var boolean
  214. */
  215. public $useTable = false;
  216. /**
  217. * hasMany property
  218. *
  219. * @var array
  220. */
  221. public $hasMany = array(
  222. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  223. ));
  224. /**
  225. * schema definition
  226. *
  227. * @var array
  228. */
  229. protected $_schema = array(
  230. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  231. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  232. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  233. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  234. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  235. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  236. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  237. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  238. );
  239. }
  240. /**
  241. * OpenidUrl class
  242. *
  243. * @package Cake.Test.Case.View.Helper
  244. */
  245. class OpenidUrl extends CakeTestModel {
  246. /**
  247. * useTable property
  248. *
  249. * @var boolean
  250. */
  251. public $useTable = false;
  252. /**
  253. * belongsTo property
  254. *
  255. * @var array
  256. */
  257. public $belongsTo = array('UserForm' => array(
  258. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  259. ));
  260. /**
  261. * validate property
  262. *
  263. * @var array
  264. */
  265. public $validate = array('openid_not_registered' => array());
  266. /**
  267. * schema method
  268. *
  269. * @var array
  270. */
  271. protected $_schema = array(
  272. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  273. 'user_form_id' => array(
  274. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  275. ),
  276. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  277. );
  278. /**
  279. * beforeValidate method
  280. *
  281. * @return void
  282. */
  283. public function beforeValidate($options = array()) {
  284. $this->invalidate('openid_not_registered');
  285. return true;
  286. }
  287. }
  288. /**
  289. * ValidateUser class
  290. *
  291. * @package Cake.Test.Case.View.Helper
  292. */
  293. class ValidateUser extends CakeTestModel {
  294. /**
  295. * useTable property
  296. *
  297. * @var boolean
  298. */
  299. public $useTable = false;
  300. /**
  301. * hasOne property
  302. *
  303. * @var array
  304. */
  305. public $hasOne = array('ValidateProfile' => array(
  306. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  307. ));
  308. /**
  309. * schema method
  310. *
  311. * @var array
  312. */
  313. protected $_schema = array(
  314. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  315. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  316. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  317. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  318. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => '6,3'),
  319. 'ratio' => array('type' => 'decimal', 'null' => false, 'length' => '10,6'),
  320. 'population' => array('type' => 'decimal', 'null' => false, 'length' => '15,0'),
  321. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  322. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  323. );
  324. /**
  325. * beforeValidate method
  326. *
  327. * @return void
  328. */
  329. public function beforeValidate($options = array()) {
  330. $this->invalidate('email');
  331. return false;
  332. }
  333. }
  334. /**
  335. * ValidateProfile class
  336. *
  337. * @package Cake.Test.Case.View.Helper
  338. */
  339. class ValidateProfile extends CakeTestModel {
  340. /**
  341. * useTable property
  342. *
  343. * @var boolean
  344. */
  345. public $useTable = false;
  346. /**
  347. * schema property
  348. *
  349. * @var array
  350. */
  351. protected $_schema = array(
  352. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  353. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  354. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  355. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  356. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  357. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  358. );
  359. /**
  360. * hasOne property
  361. *
  362. * @var array
  363. */
  364. public $hasOne = array('ValidateItem' => array(
  365. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  366. ));
  367. /**
  368. * belongsTo property
  369. *
  370. * @var array
  371. */
  372. public $belongsTo = array('ValidateUser' => array(
  373. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  374. ));
  375. /**
  376. * beforeValidate method
  377. *
  378. * @return void
  379. */
  380. public function beforeValidate($options = array()) {
  381. $this->invalidate('full_name');
  382. $this->invalidate('city');
  383. return false;
  384. }
  385. }
  386. /**
  387. * ValidateItem class
  388. *
  389. * @package Cake.Test.Case.View.Helper
  390. */
  391. class ValidateItem extends CakeTestModel {
  392. /**
  393. * useTable property
  394. *
  395. * @var boolean
  396. */
  397. public $useTable = false;
  398. /**
  399. * schema property
  400. *
  401. * @var array
  402. */
  403. protected $_schema = array(
  404. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  405. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  406. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  407. 'description' => array(
  408. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  409. ),
  410. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  411. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  412. );
  413. /**
  414. * belongsTo property
  415. *
  416. * @var array
  417. */
  418. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  419. /**
  420. * beforeValidate method
  421. *
  422. * @return void
  423. */
  424. public function beforeValidate($options = array()) {
  425. $this->invalidate('description');
  426. return false;
  427. }
  428. }
  429. /**
  430. * TestMail class
  431. *
  432. * @package Cake.Test.Case.View.Helper
  433. */
  434. class TestMail extends CakeTestModel {
  435. /**
  436. * useTable property
  437. *
  438. * @var boolean
  439. */
  440. public $useTable = false;
  441. }
  442. /**
  443. * FormHelperTest class
  444. *
  445. * @package Cake.Test.Case.View.Helper
  446. * @property FormHelper $Form
  447. */
  448. class FormHelperTest extends CakeTestCase {
  449. /**
  450. * Fixtures to be used
  451. *
  452. * @var array
  453. */
  454. public $fixtures = array('core.post');
  455. /**
  456. * Do not load the fixtures by default
  457. *
  458. * @var boolean
  459. */
  460. public $autoFixtures = false;
  461. /**
  462. * setUp method
  463. *
  464. * @return void
  465. */
  466. public function setUp() {
  467. parent::setUp();
  468. Configure::write('Config.language', 'eng');
  469. Configure::write('App.base', '');
  470. Configure::delete('Asset');
  471. $this->Controller = new ContactTestController();
  472. $this->View = new View($this->Controller);
  473. $this->Form = new FormHelper($this->View);
  474. $this->Form->Html = new HtmlHelper($this->View);
  475. $this->Form->request = new CakeRequest('contacts/add', false);
  476. $this->Form->request->here = '/contacts/add';
  477. $this->Form->request['action'] = 'add';
  478. $this->Form->request->webroot = '';
  479. $this->Form->request->base = '';
  480. ClassRegistry::addObject('Contact', new Contact());
  481. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  482. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  483. ClassRegistry::addObject('User', new UserForm());
  484. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  485. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  486. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  487. $this->oldSalt = Configure::read('Security.salt');
  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. }
  498. /**
  499. * tearDown method
  500. *
  501. * @return void
  502. */
  503. public function tearDown() {
  504. parent::tearDown();
  505. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  506. Configure::write('Security.salt', $this->oldSalt);
  507. }
  508. /**
  509. * testFormCreateWithSecurity method
  510. *
  511. * Test form->create() with security key.
  512. *
  513. * @return void
  514. */
  515. public function testCreateWithSecurity() {
  516. $this->Form->request['_Token'] = array('key' => 'testKey');
  517. $encoding = strtolower(Configure::read('App.encoding'));
  518. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  519. $expected = array(
  520. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  521. 'div' => array('style' => 'display:none;'),
  522. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  523. array('input' => array(
  524. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  525. )),
  526. '/div'
  527. );
  528. $this->assertTags($result, $expected);
  529. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  530. $expected['form']['id'] = 'MyForm';
  531. $this->assertTags($result, $expected);
  532. }
  533. /**
  534. * testFormCreateGetNoSecurity method
  535. *
  536. * Test form->create() with no security key as its a get form
  537. *
  538. * @return void
  539. */
  540. public function testCreateEndGetNoSecurity() {
  541. $this->Form->request['_Token'] = array('key' => 'testKey');
  542. $encoding = strtolower(Configure::read('App.encoding'));
  543. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  544. $this->assertNotContains('Token', $result);
  545. $result = $this->Form->end('Save');
  546. $this->assertNotContains('Token', $result);
  547. }
  548. /**
  549. * test that create() clears the fields property so it starts fresh
  550. *
  551. * @return void
  552. */
  553. public function testCreateClearingFields() {
  554. $this->Form->fields = array('model_id');
  555. $this->Form->create('Contact');
  556. $this->assertEquals(array(), $this->Form->fields);
  557. }
  558. /**
  559. * Tests form hash generation with model-less data
  560. *
  561. * @return void
  562. */
  563. public function testValidateHashNoModel() {
  564. $this->Form->request['_Token'] = array('key' => 'foo');
  565. $result = $this->Form->secure(array('anything'));
  566. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  567. }
  568. /**
  569. * Tests that models with identical field names get resolved properly
  570. *
  571. * @return void
  572. */
  573. public function testDuplicateFieldNameResolution() {
  574. $result = $this->Form->create('ValidateUser');
  575. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  576. $result = $this->Form->input('ValidateItem.name');
  577. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  578. $result = $this->Form->input('ValidateUser.name');
  579. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  580. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  581. $this->assertRegExp('/type="text"/', $result);
  582. $result = $this->Form->input('ValidateItem.name');
  583. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  584. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  585. $this->assertRegExp('/<textarea/', $result);
  586. $result = $this->Form->input('name');
  587. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  588. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  589. $this->assertRegExp('/type="text"/', $result);
  590. }
  591. /**
  592. * Tests that hidden fields generated for checkboxes don't get locked
  593. *
  594. * @return void
  595. */
  596. public function testNoCheckboxLocking() {
  597. $this->Form->request['_Token'] = array('key' => 'foo');
  598. $this->assertSame(array(), $this->Form->fields);
  599. $this->Form->checkbox('check', array('value' => '1'));
  600. $this->assertSame($this->Form->fields, array('check'));
  601. }
  602. /**
  603. * testFormSecurityFields method
  604. *
  605. * Test generation of secure form hash generation.
  606. *
  607. * @return void
  608. */
  609. public function testFormSecurityFields() {
  610. $key = 'testKey';
  611. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  612. $secureAttributes = array('form' => 'MyTestForm');
  613. $this->Form->request['_Token'] = array('key' => $key);
  614. $result = $this->Form->secure($fields, $secureAttributes);
  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' => 'data[_Token][fields]',
  622. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/',
  623. 'form' => 'MyTestForm',
  624. )),
  625. array('input' => array(
  626. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  627. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/',
  628. 'form' => 'MyTestForm',
  629. )),
  630. '/div'
  631. );
  632. $this->assertTags($result, $expected);
  633. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  634. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  635. $result = $this->Form->secure($fields, $secureAttributes);
  636. $expected = array(
  637. 'div' => array('class' => 'hidden'),
  638. array('input' => array(
  639. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  640. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/',
  641. 'form' => 'MyTestForm',
  642. )),
  643. array('input' => array(
  644. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  645. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/',
  646. 'form' => 'MyTestForm',
  647. )),
  648. '/div'
  649. );
  650. $this->assertTags($result, $expected);
  651. }
  652. /**
  653. * Tests correct generation of number fields for double and float fields
  654. *
  655. * @return void
  656. */
  657. public function testTextFieldGenerationForFloats() {
  658. $model = ClassRegistry::getObject('Contact');
  659. $model->setSchema(array('foo' => array(
  660. 'type' => 'float',
  661. 'null' => false,
  662. 'default' => null,
  663. 'length' => 10
  664. )));
  665. $this->Form->create('Contact');
  666. $result = $this->Form->input('foo');
  667. $expected = array(
  668. 'div' => array('class' => 'input number'),
  669. 'label' => array('for' => 'ContactFoo'),
  670. 'Foo',
  671. '/label',
  672. array('input' => array(
  673. 'type' => 'number',
  674. 'name' => 'data[Contact][foo]',
  675. 'id' => 'ContactFoo',
  676. 'step' => 'any'
  677. )),
  678. '/div'
  679. );
  680. $this->assertTags($result, $expected);
  681. $result = $this->Form->input('foo', array('step' => 0.5));
  682. $expected = array(
  683. 'div' => array('class' => 'input number'),
  684. 'label' => array('for' => 'ContactFoo'),
  685. 'Foo',
  686. '/label',
  687. array('input' => array(
  688. 'type' => 'number',
  689. 'name' => 'data[Contact][foo]',
  690. 'id' => 'ContactFoo',
  691. 'step' => '0.5'
  692. )),
  693. '/div'
  694. );
  695. $this->assertTags($result, $expected);
  696. }
  697. /**
  698. * Tests correct generation of number fields for integer fields
  699. *
  700. * @return void
  701. */
  702. public function testTextFieldTypeNumberGenerationForIntegers() {
  703. $model = ClassRegistry::getObject('Contact');
  704. $model->setSchema(array('foo' => array(
  705. 'type' => 'integer',
  706. 'null' => false,
  707. 'default' => null,
  708. 'length' => null
  709. )));
  710. $this->Form->create('Contact');
  711. $result = $this->Form->input('foo');
  712. $expected = array(
  713. 'div' => array('class' => 'input number'),
  714. 'label' => array('for' => 'ContactFoo'),
  715. 'Foo',
  716. '/label',
  717. array('input' => array(
  718. 'type' => 'number', 'name' => 'data[Contact][foo]',
  719. 'id' => 'ContactFoo'
  720. )),
  721. '/div'
  722. );
  723. $this->assertTags($result, $expected);
  724. }
  725. /**
  726. * Tests correct generation of file upload fields for binary fields
  727. *
  728. * @return void
  729. */
  730. public function testFileUploadFieldTypeGenerationForBinaries() {
  731. $model = ClassRegistry::getObject('Contact');
  732. $model->setSchema(array('foo' => array(
  733. 'type' => 'binary',
  734. 'null' => false,
  735. 'default' => null,
  736. 'length' => 1024
  737. )));
  738. $this->Form->create('Contact');
  739. $result = $this->Form->input('foo');
  740. $expected = array(
  741. 'div' => array('class' => 'input file'),
  742. 'label' => array('for' => 'ContactFoo'),
  743. 'Foo',
  744. '/label',
  745. array('input' => array(
  746. 'type' => 'file', 'name' => 'data[Contact][foo]',
  747. 'id' => 'ContactFoo'
  748. )),
  749. '/div'
  750. );
  751. $this->assertTags($result, $expected);
  752. }
  753. /**
  754. * testFormSecurityMultipleFields method
  755. *
  756. * Test secure() with multiple row form. Ensure hash is correct.
  757. *
  758. * @return void
  759. */
  760. public function testFormSecurityMultipleFields() {
  761. $key = 'testKey';
  762. $fields = array(
  763. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  764. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  765. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  766. );
  767. $this->Form->request['_Token'] = array('key' => $key);
  768. $result = $this->Form->secure($fields);
  769. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  770. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  771. $expected = array(
  772. 'div' => array('style' => 'display:none;'),
  773. array('input' => array(
  774. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  775. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  776. )),
  777. array('input' => array(
  778. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  779. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  780. )),
  781. '/div'
  782. );
  783. $this->assertTags($result, $expected);
  784. }
  785. /**
  786. * testFormSecurityMultipleSubmitButtons
  787. *
  788. * test form submit generation and ensure that _Token is only created on end()
  789. *
  790. * @return void
  791. */
  792. public function testFormSecurityMultipleSubmitButtons() {
  793. $key = 'testKey';
  794. $this->Form->request['_Token'] = array('key' => $key);
  795. $this->Form->create('Addresses');
  796. $this->Form->input('Address.title');
  797. $this->Form->input('Address.first_name');
  798. $result = $this->Form->submit('Save', array('name' => 'save'));
  799. $expected = array(
  800. 'div' => array('class' => 'submit'),
  801. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  802. '/div',
  803. );
  804. $this->assertTags($result, $expected);
  805. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  806. $expected = array(
  807. 'div' => array('class' => 'submit'),
  808. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  809. '/div',
  810. );
  811. $this->assertTags($result, $expected);
  812. $result = $this->Form->end(null);
  813. $expected = array(
  814. 'div' => array('style' => 'display:none;'),
  815. array('input' => array(
  816. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  817. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  818. )),
  819. array('input' => array(
  820. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  821. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  822. )),
  823. '/div'
  824. );
  825. $this->assertTags($result, $expected);
  826. }
  827. /**
  828. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  829. *
  830. * @return void
  831. */
  832. public function testSecurityButtonNestedNamed() {
  833. $key = 'testKey';
  834. $this->Form->request['_Token'] = array('key' => $key);
  835. $this->Form->create('Addresses');
  836. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  837. $result = $this->Form->unlockField();
  838. $this->assertEquals(array('Address.button'), $result);
  839. }
  840. /**
  841. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  842. *
  843. * @return void
  844. */
  845. public function testSecuritySubmitNestedNamed() {
  846. $key = 'testKey';
  847. $this->Form->request['_Token'] = array('key' => $key);
  848. $this->Form->create('Addresses');
  849. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  850. $result = $this->Form->unlockField();
  851. $this->assertEquals(array('Address.button'), $result);
  852. }
  853. /**
  854. * Test that the correct fields are unlocked for image submits with no names.
  855. *
  856. * @return void
  857. */
  858. public function testSecuritySubmitImageNoName() {
  859. $key = 'testKey';
  860. $this->Form->request['_Token'] = array('key' => $key);
  861. $this->Form->create('User');
  862. $result = $this->Form->submit('save.png');
  863. $expected = array(
  864. 'div' => array('class' => 'submit'),
  865. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  866. '/div'
  867. );
  868. $this->assertTags($result, $expected);
  869. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  870. }
  871. /**
  872. * Test that the correct fields are unlocked for image submits with names.
  873. *
  874. * @return void
  875. */
  876. public function testSecuritySubmitImageName() {
  877. $key = 'testKey';
  878. $this->Form->request['_Token'] = array('key' => $key);
  879. $this->Form->create('User');
  880. $result = $this->Form->submit('save.png', array('name' => 'test'));
  881. $expected = array(
  882. 'div' => array('class' => 'submit'),
  883. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  884. '/div'
  885. );
  886. $this->assertTags($result, $expected);
  887. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  888. }
  889. /**
  890. * testFormSecurityMultipleInputFields method
  891. *
  892. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  893. *
  894. * @return void
  895. */
  896. public function testFormSecurityMultipleInputFields() {
  897. $key = 'testKey';
  898. $this->Form->request['_Token'] = array('key' => $key);
  899. $this->Form->create('Addresses');
  900. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  901. $this->Form->input('Addresses.0.title');
  902. $this->Form->input('Addresses.0.first_name');
  903. $this->Form->input('Addresses.0.last_name');
  904. $this->Form->input('Addresses.0.address');
  905. $this->Form->input('Addresses.0.city');
  906. $this->Form->input('Addresses.0.phone');
  907. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  908. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  909. $this->Form->input('Addresses.1.title');
  910. $this->Form->input('Addresses.1.first_name');
  911. $this->Form->input('Addresses.1.last_name');
  912. $this->Form->input('Addresses.1.address');
  913. $this->Form->input('Addresses.1.city');
  914. $this->Form->input('Addresses.1.phone');
  915. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  916. $result = $this->Form->secure($this->Form->fields);
  917. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  918. $expected = array(
  919. 'div' => array('style' => 'display:none;'),
  920. array('input' => array(
  921. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  922. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  923. )),
  924. array('input' => array(
  925. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  926. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  927. )),
  928. '/div'
  929. );
  930. $this->assertTags($result, $expected);
  931. }
  932. /**
  933. * Test form security with Model.field.0 style inputs
  934. *
  935. * @return void
  936. */
  937. public function testFormSecurityArrayFields() {
  938. $key = 'testKey';
  939. $this->Form->request->params['_Token']['key'] = $key;
  940. $this->Form->create('Address');
  941. $this->Form->input('Address.primary.1');
  942. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  943. $this->Form->input('Address.secondary.1.0');
  944. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  945. }
  946. /**
  947. * testFormSecurityMultipleInputDisabledFields method
  948. *
  949. * test secure form generation with multiple records and disabled fields.
  950. *
  951. * @return void
  952. */
  953. public function testFormSecurityMultipleInputDisabledFields() {
  954. $key = 'testKey';
  955. $this->Form->request->params['_Token'] = array(
  956. 'key' => $key,
  957. 'unlockedFields' => array('first_name', 'address')
  958. );
  959. $this->Form->create();
  960. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  961. $this->Form->input('Addresses.0.title');
  962. $this->Form->input('Addresses.0.first_name');
  963. $this->Form->input('Addresses.0.last_name');
  964. $this->Form->input('Addresses.0.address');
  965. $this->Form->input('Addresses.0.city');
  966. $this->Form->input('Addresses.0.phone');
  967. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  968. $this->Form->input('Addresses.1.title');
  969. $this->Form->input('Addresses.1.first_name');
  970. $this->Form->input('Addresses.1.last_name');
  971. $this->Form->input('Addresses.1.address');
  972. $this->Form->input('Addresses.1.city');
  973. $this->Form->input('Addresses.1.phone');
  974. $result = $this->Form->secure($this->Form->fields);
  975. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  976. $expected = array(
  977. 'div' => array('style' => 'display:none;'),
  978. array('input' => array(
  979. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  980. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  981. )),
  982. array('input' => array(
  983. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  984. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  985. )),
  986. '/div'
  987. );
  988. $this->assertTags($result, $expected);
  989. }
  990. /**
  991. * testFormSecurityInputDisabledFields method
  992. *
  993. * Test single record form with disabled fields.
  994. *
  995. * @return void
  996. */
  997. public function testFormSecurityInputUnlockedFields() {
  998. $key = 'testKey';
  999. $this->Form->request['_Token'] = array(
  1000. 'key' => $key,
  1001. 'unlockedFields' => array('first_name', 'address')
  1002. );
  1003. $this->Form->create();
  1004. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1005. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1006. $this->Form->input('Addresses.title');
  1007. $this->Form->input('Addresses.first_name');
  1008. $this->Form->input('Addresses.last_name');
  1009. $this->Form->input('Addresses.address');
  1010. $this->Form->input('Addresses.city');
  1011. $this->Form->input('Addresses.phone');
  1012. $result = $this->Form->fields;
  1013. $expected = array(
  1014. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1015. 'Addresses.city', 'Addresses.phone'
  1016. );
  1017. $this->assertEquals($expected, $result);
  1018. $result = $this->Form->secure($expected);
  1019. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1020. $expected = array(
  1021. 'div' => array('style' => 'display:none;'),
  1022. array('input' => array(
  1023. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1024. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1025. )),
  1026. array('input' => array(
  1027. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1028. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1029. )),
  1030. '/div'
  1031. );
  1032. $this->assertTags($result, $expected);
  1033. }
  1034. /**
  1035. * test securing inputs with custom name attributes.
  1036. *
  1037. * @return void
  1038. */
  1039. public function testFormSecureWithCustomNameAttribute() {
  1040. $this->Form->request->params['_Token']['key'] = 'testKey';
  1041. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1042. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1043. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1044. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1045. }
  1046. /**
  1047. * testFormSecuredInput method
  1048. *
  1049. * Test generation of entire secure form, assertions made on input() output.
  1050. *
  1051. * @return void
  1052. */
  1053. public function testFormSecuredInput() {
  1054. $this->Form->request['_Token'] = array('key' => 'testKey');
  1055. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1056. $encoding = strtolower(Configure::read('App.encoding'));
  1057. $expected = array(
  1058. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1059. 'div' => array('style' => 'display:none;'),
  1060. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1061. array('input' => array(
  1062. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1063. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1064. )),
  1065. '/div'
  1066. );
  1067. $this->assertTags($result, $expected);
  1068. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1069. $expected = array(
  1070. 'div' => array('class' => 'input text'),
  1071. 'label' => array('for' => 'UserFormPublished'),
  1072. 'Published',
  1073. '/label',
  1074. array('input' => array(
  1075. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1076. 'id' => 'UserFormPublished'
  1077. )),
  1078. '/div'
  1079. );
  1080. $this->assertTags($result, $expected);
  1081. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1082. $expected = array(
  1083. 'div' => array('class' => 'input text'),
  1084. 'label' => array('for' => 'UserFormOther'),
  1085. 'Other',
  1086. '/label',
  1087. array('input' => array(
  1088. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1089. 'id' => 'UserFormOther'
  1090. )),
  1091. '/div'
  1092. );
  1093. $this->assertTags($result, $expected);
  1094. $result = $this->Form->hidden('UserForm.stuff');
  1095. $expected = array(
  1096. 'input' => array(
  1097. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1098. 'id' => 'UserFormStuff'
  1099. ));
  1100. $this->assertTags($result, $expected);
  1101. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1102. $expected = array('input' => array(
  1103. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1104. 'value' => '0', 'id' => 'UserFormHidden'
  1105. ));
  1106. $this->assertTags($result, $expected);
  1107. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1108. $expected = array(
  1109. 'div' => array('class' => 'input checkbox'),
  1110. array('input' => array(
  1111. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1112. 'value' => '0', 'id' => 'UserFormSomething_'
  1113. )),
  1114. array('input' => array(
  1115. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1116. 'value' => '1', 'id' => 'UserFormSomething'
  1117. )),
  1118. 'label' => array('for' => 'UserFormSomething'),
  1119. 'Something',
  1120. '/label',
  1121. '/div'
  1122. );
  1123. $this->assertTags($result, $expected);
  1124. $result = $this->Form->fields;
  1125. $expected = array(
  1126. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1127. 'UserForm.hidden' => '0', 'UserForm.something'
  1128. );
  1129. $this->assertEquals($expected, $result);
  1130. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1131. $result = $this->Form->secure($this->Form->fields);
  1132. $expected = array(
  1133. 'div' => array('style' => 'display:none;'),
  1134. array('input' => array(
  1135. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1136. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1137. )),
  1138. array('input' => array(
  1139. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1140. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1141. )),
  1142. '/div'
  1143. );
  1144. $this->assertTags($result, $expected);
  1145. }
  1146. /**
  1147. * Test secured inputs with custom names.
  1148. *
  1149. * @return void
  1150. */
  1151. public function testSecuredInputCustomName() {
  1152. $this->Form->request['_Token'] = array('key' => 'testKey');
  1153. $this->assertEquals(array(), $this->Form->fields);
  1154. $this->Form->input('text_input', array(
  1155. 'name' => 'data[Option][General.default_role]',
  1156. ));
  1157. $expected = array('Option.General.default_role');
  1158. $this->assertEquals($expected, $this->Form->fields);
  1159. $this->Form->input('select_box', array(
  1160. 'name' => 'data[Option][General.select_role]',
  1161. 'type' => 'select',
  1162. 'options' => array(1, 2),
  1163. ));
  1164. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1165. $this->assertEquals($expected, $this->Form->fields);
  1166. }
  1167. /**
  1168. * Tests that the correct keys are added to the field hash index
  1169. *
  1170. * @return void
  1171. */
  1172. public function testFormSecuredFileInput() {
  1173. $this->Form->request['_Token'] = array('key' => 'testKey');
  1174. $this->assertEquals(array(), $this->Form->fields);
  1175. $this->Form->file('Attachment.file');
  1176. $expected = array(
  1177. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1178. 'Attachment.file.error', 'Attachment.file.size'
  1179. );
  1180. $this->assertEquals($expected, $this->Form->fields);
  1181. }
  1182. /**
  1183. * test that multiple selects keys are added to field hash
  1184. *
  1185. * @return void
  1186. */
  1187. public function testFormSecuredMultipleSelect() {
  1188. $this->Form->request['_Token'] = array('key' => 'testKey');
  1189. $this->assertEquals(array(), $this->Form->fields);
  1190. $options = array('1' => 'one', '2' => 'two');
  1191. $this->Form->select('Model.select', $options);
  1192. $expected = array('Model.select');
  1193. $this->assertEquals($expected, $this->Form->fields);
  1194. $this->Form->fields = array();
  1195. $this->Form->select('Model.select', $options, array('multiple' => true));
  1196. $this->assertEquals($expected, $this->Form->fields);
  1197. }
  1198. /**
  1199. * testFormSecuredRadio method
  1200. *
  1201. * @return void
  1202. */
  1203. public function testFormSecuredRadio() {
  1204. $this->Form->request['_Token'] = array('key' => 'testKey');
  1205. $this->assertEquals(array(), $this->Form->fields);
  1206. $options = array('1' => 'option1', '2' => 'option2');
  1207. $this->Form->radio('Test.test', $options);
  1208. $expected = array('Test.test');
  1209. $this->assertEquals($expected, $this->Form->fields);
  1210. }
  1211. /**
  1212. * Test that when disabled is in a list based attribute array it works.
  1213. *
  1214. * @return void
  1215. */
  1216. public function testFormSecuredAndDisabledNotAssoc() {
  1217. $this->Form->request['_Token'] = array('key' => 'testKey');
  1218. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1219. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1220. $this->Form->text('Model.text', array('disabled'));
  1221. $this->Form->textarea('Model.textarea', array('disabled'));
  1222. $this->Form->password('Model.password', array('disabled'));
  1223. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1224. $expected = array(
  1225. 'Model.radio' => ''
  1226. );
  1227. $this->assertEquals($expected, $this->Form->fields);
  1228. }
  1229. /**
  1230. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1231. * hashing.
  1232. *
  1233. * @return void
  1234. */
  1235. public function testFormSecuredAndDisabled() {
  1236. $this->Form->request['_Token'] = array('key' => 'testKey');
  1237. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1238. $this->Form->text('Model.text', array('disabled' => true));
  1239. $this->Form->password('Model.text', array('disabled' => true));
  1240. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1241. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1242. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1243. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1244. $this->Form->month('Model.month', array('disabled' => true));
  1245. $this->Form->day('Model.day', array('disabled' => true));
  1246. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1247. $this->Form->minute('Model.minute', array('disabled' => true));
  1248. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1249. $expected = array(
  1250. 'Model.radio' => ''
  1251. );
  1252. $this->assertEquals($expected, $this->Form->fields);
  1253. }
  1254. /**
  1255. * testDisableSecurityUsingForm method
  1256. *
  1257. * @return void
  1258. */
  1259. public function testDisableSecurityUsingForm() {
  1260. $this->Form->request['_Token'] = array(
  1261. 'key' => 'testKey',
  1262. 'disabledFields' => array()
  1263. );
  1264. $this->Form->create();
  1265. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1266. $this->Form->input('Addresses.title');
  1267. $this->Form->input('Addresses.first_name', array('secure' => false));
  1268. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1269. $this->Form->input('Addresses.zip', array(
  1270. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1271. ));
  1272. $result = $this->Form->fields;
  1273. $expected = array(
  1274. 'Addresses.id' => '123456', 'Addresses.title',
  1275. );
  1276. $this->assertEquals($expected, $result);
  1277. }
  1278. /**
  1279. * test disableField
  1280. *
  1281. * @return void
  1282. */
  1283. public function testUnlockFieldAddsToList() {
  1284. $this->Form->request['_Token'] = array(
  1285. 'key' => 'testKey',
  1286. 'unlockedFields' => array()
  1287. );
  1288. $this->Form->create('Contact');
  1289. $this->Form->unlockField('Contact.name');
  1290. $this->Form->text('Contact.name');
  1291. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1292. $this->assertEquals(array(), $this->Form->fields);
  1293. }
  1294. /**
  1295. * test unlockField removing from fields array.
  1296. *
  1297. * @return void
  1298. */
  1299. public function testUnlockFieldRemovingFromFields() {
  1300. $this->Form->request['_Token'] = array(
  1301. 'key' => 'testKey',
  1302. 'unlockedFields' => array()
  1303. );
  1304. $this->Form->create('Contact');
  1305. $this->Form->hidden('Contact.id', array('value' => 1));
  1306. $this->Form->text('Contact.name');
  1307. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1308. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1309. $this->Form->unlockField('Contact.name');
  1310. $this->Form->unlockField('Contact.id');
  1311. $this->assertEquals(array(), $this->Form->fields);
  1312. }
  1313. /**
  1314. * testTagIsInvalid method
  1315. *
  1316. * @return void
  1317. */
  1318. public function testTagIsInvalid() {
  1319. $Contact = ClassRegistry::getObject('Contact');
  1320. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1321. $this->Form->setEntity('Contact.0.email');
  1322. $result = $this->Form->tagIsInvalid();
  1323. $this->assertEquals($expected, $result);
  1324. $this->Form->setEntity('Contact.1.email');
  1325. $result = $this->Form->tagIsInvalid();
  1326. $this->assertFalse($result);
  1327. $this->Form->setEntity('Contact.0.name');
  1328. $result = $this->Form->tagIsInvalid();
  1329. $this->assertFalse($result);
  1330. }
  1331. /**
  1332. * Test tagIsInvalid with validation errors from a saveMany
  1333. *
  1334. * @return void
  1335. */
  1336. public function testTagIsInvalidSaveMany() {
  1337. $Contact = ClassRegistry::getObject('Contact');
  1338. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1339. $this->Form->create('Contact');
  1340. $this->Form->setEntity('0.email');
  1341. $result = $this->Form->tagIsInvalid();
  1342. $this->assertEquals($expected, $result);
  1343. $this->Form->setEntity('0.Contact.email');
  1344. $result = $this->Form->tagIsInvalid();
  1345. $this->assertEquals($expected, $result);
  1346. }
  1347. /**
  1348. * Test validation errors.
  1349. *
  1350. * @return void
  1351. */
  1352. public function testPasswordValidation() {
  1353. $Contact = ClassRegistry::getObject('Contact');
  1354. $Contact->validationErrors['password'] = array('Please provide a password');
  1355. $result = $this->Form->input('Contact.password');
  1356. $expected = array(
  1357. 'div' => array('class' => 'input password error'),
  1358. 'label' => array('for' => 'ContactPassword'),
  1359. 'Password',
  1360. '/label',
  1361. 'input' => array(
  1362. 'type' => 'password', 'name' => 'data[Contact][password]',
  1363. 'id' => 'ContactPassword', 'class' => 'form-error'
  1364. ),
  1365. array('div' => array('class' => 'error-message')),
  1366. 'Please provide a password',
  1367. '/div',
  1368. '/div'
  1369. );
  1370. $this->assertTags($result, $expected);
  1371. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1372. $expected = array(
  1373. 'div' => array('class' => 'input password error'),
  1374. 'label' => array('for' => 'ContactPassword'),
  1375. 'Password',
  1376. '/label',
  1377. 'input' => array(
  1378. 'type' => 'password', 'name' => 'data[Contact][password]',
  1379. 'id' => 'ContactPassword', 'class' => 'form-error'
  1380. ),
  1381. '/div'
  1382. );
  1383. $this->assertTags($result, $expected);
  1384. }
  1385. /**
  1386. * Test validation errors, when validation message is an empty string.
  1387. *
  1388. * @return void
  1389. */
  1390. public function testEmptyErrorValidation() {
  1391. $this->Form->validationErrors['Contact']['password'] = '';
  1392. $result = $this->Form->input('Contact.password');
  1393. $expected = array(
  1394. 'div' => array('class' => 'input password error'),
  1395. 'label' => array('for' => 'ContactPassword'),
  1396. 'Password',
  1397. '/label',
  1398. 'input' => array(
  1399. 'type' => 'password', 'name' => 'data[Contact][password]',
  1400. 'id' => 'ContactPassword', 'class' => 'form-error'
  1401. ),
  1402. array('div' => array('class' => 'error-message')),
  1403. array(),
  1404. '/div',
  1405. '/div'
  1406. );
  1407. $this->assertTags($result, $expected);
  1408. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1409. $expected = array(
  1410. 'div' => array('class' => 'input password error'),
  1411. 'label' => array('for' => 'ContactPassword'),
  1412. 'Password',
  1413. '/label',
  1414. 'input' => array(
  1415. 'type' => 'password', 'name' => 'data[Contact][password]',
  1416. 'id' => 'ContactPassword', 'class' => 'form-error'
  1417. ),
  1418. '/div'
  1419. );
  1420. $this->assertTags($result, $expected);
  1421. }
  1422. /**
  1423. * Test validation errors, when calling input() overriding validation message by an empty string.
  1424. *
  1425. * @return void
  1426. */
  1427. public function testEmptyInputErrorValidation() {
  1428. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1429. $result = $this->Form->input('Contact.password', array('error' => ''));
  1430. $expected = array(
  1431. 'div' => array('class' => 'input password error'),
  1432. 'label' => array('for' => 'ContactPassword'),
  1433. 'Password',
  1434. '/label',
  1435. 'input' => array(
  1436. 'type' => 'password', 'name' => 'data[Contact][password]',
  1437. 'id' => 'ContactPassword', 'class' => 'form-error'
  1438. ),
  1439. array('div' => array('class' => 'error-message')),
  1440. array(),
  1441. '/div',
  1442. '/div'
  1443. );
  1444. $this->assertTags($result, $expected);
  1445. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1446. $expected = array(
  1447. 'div' => array('class' => 'input password error'),
  1448. 'label' => array('for' => 'ContactPassword'),
  1449. 'Password',
  1450. '/label',
  1451. 'input' => array(
  1452. 'type' => 'password', 'name' => 'data[Contact][password]',
  1453. 'id' => 'ContactPassword', 'class' => 'form-error'
  1454. ),
  1455. '/div'
  1456. );
  1457. $this->assertTags($result, $expected);
  1458. }
  1459. /**
  1460. * testFormValidationAssociated method
  1461. *
  1462. * test display of form errors in conjunction with model::validates.
  1463. *
  1464. * @return void
  1465. */
  1466. public function testFormValidationAssociated() {
  1467. $this->UserForm = ClassRegistry::getObject('UserForm');
  1468. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1469. $data = array(
  1470. 'UserForm' => array('name' => 'user'),
  1471. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1472. );
  1473. $result = $this->UserForm->OpenidUrl->create($data);
  1474. $this->assertFalse(empty($result));
  1475. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1476. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1477. $encoding = strtolower(Configure::read('App.encoding'));
  1478. $expected = array(
  1479. 'form' => array(
  1480. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1481. 'accept-charset' => $encoding
  1482. ),
  1483. 'div' => array('style' => 'display:none;'),
  1484. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1485. '/div'
  1486. );
  1487. $this->assertTags($result, $expected);
  1488. $result = $this->Form->error(
  1489. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1490. );
  1491. $this->assertEquals('Error, not registered', $result);
  1492. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1493. }
  1494. /**
  1495. * testFormValidationAssociatedFirstLevel method
  1496. *
  1497. * test form error display with associated model.
  1498. *
  1499. * @return void
  1500. */
  1501. public function testFormValidationAssociatedFirstLevel() {
  1502. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1503. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1504. $data = array(
  1505. 'ValidateUser' => array('name' => 'mariano'),
  1506. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1507. );
  1508. $result = $this->ValidateUser->create($data);
  1509. $this->assertFalse(empty($result));
  1510. $this->assertFalse($this->ValidateUser->validates());
  1511. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1512. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1513. $encoding = strtolower(Configure::read('App.encoding'));
  1514. $expected = array(
  1515. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1516. 'div' => array('style' => 'display:none;'),
  1517. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1518. '/div'
  1519. );
  1520. $this->assertTags($result, $expected);
  1521. $result = $this->Form->error(
  1522. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1523. );
  1524. $this->assertEquals('Invalid email', $result);
  1525. $result = $this->Form->error(
  1526. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1527. );
  1528. $this->assertEquals('Invalid name', $result);
  1529. $result = $this->Form->error(
  1530. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1531. );
  1532. $this->assertEquals('Invalid city', $result);
  1533. unset($this->ValidateUser->ValidateProfile);
  1534. unset($this->ValidateUser);
  1535. }
  1536. /**
  1537. * testFormValidationAssociatedSecondLevel method
  1538. *
  1539. * test form error display with associated model.
  1540. *
  1541. * @return void
  1542. */
  1543. public function testFormValidationAssociatedSecondLevel() {
  1544. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1545. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1546. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1547. $data = array(
  1548. 'ValidateUser' => array('name' => 'mariano'),
  1549. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1550. 'ValidateItem' => array('name' => 'Item')
  1551. );
  1552. $result = $this->ValidateUser->create($data);
  1553. $this->assertFalse(empty($result));
  1554. $this->assertFalse($this->ValidateUser->validates());
  1555. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1556. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1557. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1558. $encoding = strtolower(Configure::read('App.encoding'));
  1559. $expected = array(
  1560. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1561. 'div' => array('style' => 'display:none;'),
  1562. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1563. '/div'
  1564. );
  1565. $this->assertTags($result, $expected);
  1566. $result = $this->Form->error(
  1567. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1568. );
  1569. $this->assertEquals('Invalid email', $result);
  1570. $result = $this->Form->error(
  1571. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1572. );
  1573. $this->assertEquals('Invalid name', $result);
  1574. $result = $this->Form->error(
  1575. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1576. );
  1577. $result = $this->Form->error(
  1578. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1579. );
  1580. $this->assertEquals('Invalid description', $result);
  1581. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1582. unset($this->ValidateUser->ValidateProfile);
  1583. unset($this->ValidateUser);
  1584. }
  1585. /**
  1586. * testFormValidationMultiRecord method
  1587. *
  1588. * test form error display with multiple records.
  1589. *
  1590. * @return void
  1591. */
  1592. public function testFormValidationMultiRecord() {
  1593. $Contact = ClassRegistry::getObject('Contact');
  1594. $Contact->validationErrors[2] = array(
  1595. 'name' => array('This field cannot be left blank')
  1596. );
  1597. $result = $this->Form->input('Contact.2.name');
  1598. $expected = array(
  1599. 'div' => array('class' => 'input text error'),
  1600. 'label' => array('for' => 'Contact2Name'),
  1601. 'Name',
  1602. '/label',
  1603. 'input' => array(
  1604. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1605. 'class' => 'form-error', 'maxlength' => 255
  1606. ),
  1607. array('div' => array('class' => 'error-message')),
  1608. 'This field cannot be left blank',
  1609. '/div',
  1610. '/div'
  1611. );
  1612. $this->assertTags($result, $expected);
  1613. }
  1614. /**
  1615. * testMultipleInputValidation method
  1616. *
  1617. * test multiple record form validation error display.
  1618. *
  1619. * @return void
  1620. */
  1621. public function testMultipleInputValidation() {
  1622. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1623. $Address->validationErrors[0] = array(
  1624. 'title' => array('This field cannot be empty'),
  1625. 'first_name' => array('This field cannot be empty')
  1626. );
  1627. $Address->validationErrors[1] = array(
  1628. 'last_name' => array('You must have a last name')
  1629. );
  1630. $this->Form->create();
  1631. $result = $this->Form->input('Address.0.title');
  1632. $expected = array(
  1633. 'div' => array('class'),
  1634. 'label' => array('for'),
  1635. 'preg:/[^<]+/',
  1636. '/label',
  1637. 'input' => array(
  1638. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1639. ),
  1640. array('div' => array('class' => 'error-message')),
  1641. 'This field cannot be empty',
  1642. '/div',
  1643. '/div'
  1644. );
  1645. $this->assertTags($result, $expected);
  1646. $result = $this->Form->input('Address.0.first_name');
  1647. $expected = array(
  1648. 'div' => array('class'),
  1649. 'label' => array('for'),
  1650. 'preg:/[^<]+/',
  1651. '/label',
  1652. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1653. array('div' => array('class' => 'error-message')),
  1654. 'This field cannot be empty',
  1655. '/div',
  1656. '/div'
  1657. );
  1658. $this->assertTags($result, $expected);
  1659. $result = $this->Form->input('Address.0.last_name');
  1660. $expected = array(
  1661. 'div' => array('class'),
  1662. 'label' => array('for'),
  1663. 'preg:/[^<]+/',
  1664. '/label',
  1665. 'input' => array('type' => 'text', 'name', 'id'),
  1666. '/div'
  1667. );
  1668. $this->assertTags($result, $expected);
  1669. $result = $this->Form->input('Address.1.last_name');
  1670. $expected = array(
  1671. 'div' => array('class'),
  1672. 'label' => array('for'),
  1673. 'preg:/[^<]+/',
  1674. '/label',
  1675. 'input' => array(
  1676. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1677. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1678. ),
  1679. array('div' => array('class' => 'error-message')),
  1680. 'You must have a last name',
  1681. '/div',
  1682. '/div'
  1683. );
  1684. $this->assertTags($result, $expected);
  1685. }
  1686. /**
  1687. * testInput method
  1688. *
  1689. * Test various incarnations of input().
  1690. *
  1691. * @return void
  1692. */
  1693. public function testInput() {
  1694. $result = $this->Form->input('ValidateUser.balance');
  1695. $expected = array(
  1696. 'div' => array('class'),
  1697. 'label' => array('for'),
  1698. 'Balance',
  1699. '/label',
  1700. 'input' => array('name', 'type' => 'number', 'id'),
  1701. '/div',
  1702. );
  1703. $this->assertTags($result, $expected);
  1704. $result = $this->Form->input('ValidateUser.cost_decimal');
  1705. $expected = array(
  1706. 'div' => array('class'),
  1707. 'label' => array('for'),
  1708. 'Cost Decimal',
  1709. '/label',
  1710. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  1711. '/div',
  1712. );
  1713. $this->assertTags($result, $expected);
  1714. $result = $this->Form->input('ValidateUser.ratio');
  1715. $expected = array(
  1716. 'div' => array('class'),
  1717. 'label' => array('for'),
  1718. 'Ratio',
  1719. '/label',
  1720. 'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
  1721. '/div',
  1722. );
  1723. $this->assertTags($result, $expected);
  1724. $result = $this->Form->input('ValidateUser.population');
  1725. $expected = array(
  1726. 'div' => array('class'),
  1727. 'label' => array('for'),
  1728. 'Population',
  1729. '/label',
  1730. 'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
  1731. '/div',
  1732. );
  1733. $this->assertTags($result, $expected);
  1734. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1735. $expected = array(
  1736. 'div' => array('class' => 'input email'),
  1737. 'label' => array('for' => 'custom'),
  1738. 'Email',
  1739. '/label',
  1740. array('input' => array(
  1741. 'type' => 'email', 'name' => 'data[Contact][email]',
  1742. 'id' => 'custom', 'maxlength' => 255
  1743. )),
  1744. '/div'
  1745. );
  1746. $this->assertTags($result, $expected);
  1747. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1748. $expected = array(
  1749. '<div',
  1750. 'label' => array('for' => 'ContactEmail'),
  1751. 'Email',
  1752. '/label',
  1753. array('input' => array(
  1754. 'type' => 'email', 'name' => 'data[Contact][email]',
  1755. 'id' => 'ContactEmail', 'maxlength' => 255
  1756. )),
  1757. '/div'
  1758. );
  1759. $this->assertTags($result, $expected);
  1760. $result = $this->Form->hidden('Contact.idontexist');
  1761. $expected = array('input' => array(
  1762. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1763. 'id' => 'ContactIdontexist'
  1764. ));
  1765. $this->assertTags($result, $expected);
  1766. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1767. $expected = array(
  1768. 'div' => array('class' => 'input text'),
  1769. 'label' => array('for' => 'ContactEmail'),
  1770. 'Email',
  1771. '/label',
  1772. array('input' => array(
  1773. 'type' => 'text', 'name' => 'data[Contact][email]',
  1774. 'id' => 'ContactEmail'
  1775. )),
  1776. '/div'
  1777. );
  1778. $this->assertTags($result, $expected);
  1779. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1780. $expected = array(
  1781. 'div' => array('class' => 'input text'),
  1782. 'label' => array('for' => 'Contact5Email'),
  1783. 'Email',
  1784. '/label',
  1785. array('input' => array(
  1786. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1787. 'id' => 'Contact5Email'
  1788. )),
  1789. '/div'
  1790. );
  1791. $this->assertTags($result, $expected);
  1792. $result = $this->Form->input('Contact.password');
  1793. $expected = array(
  1794. 'div' => array('class' => 'input password'),
  1795. 'label' => array('for' => 'ContactPassword'),
  1796. 'Password',
  1797. '/label',
  1798. array('input' => array(
  1799. 'type' => 'password', 'name' => 'data[Contact][password]',
  1800. 'id' => 'ContactPassword'
  1801. )),
  1802. '/div'
  1803. );
  1804. $this->assertTags($result, $expected);
  1805. $result = $this->Form->input('Contact.email', array(
  1806. 'type' => 'file', 'class' => 'textbox'
  1807. ));
  1808. $expected = array(
  1809. 'div' => array('class' => 'input file'),
  1810. 'label' => array('for' => 'ContactEmail'),
  1811. 'Email',
  1812. '/label',
  1813. array('input' => array(
  1814. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1815. 'id' => 'ContactEmail'
  1816. )),
  1817. '/div'
  1818. );
  1819. $this->assertTags($result, $expected);
  1820. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1821. $result = $this->Form->input('Contact.phone');
  1822. $expected = array(
  1823. 'div' => array('class' => 'input tel'),
  1824. 'label' => array('for' => 'ContactPhone'),
  1825. 'Phone',
  1826. '/label',
  1827. array('input' => array(
  1828. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1829. 'value' => 'Hello &amp; World &gt; weird chars',
  1830. 'id' => 'ContactPhone', 'maxlength' => 255
  1831. )),
  1832. '/div'
  1833. );
  1834. $this->assertTags($result, $expected);
  1835. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1836. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1837. $expected = array(
  1838. 'div' => array('class' => 'input text'),
  1839. 'label' => array('for' => 'myId'),
  1840. 'Field',
  1841. '/label',
  1842. 'input' => array(
  1843. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1844. 'value' => 'My value', 'id' => 'myId'
  1845. ),
  1846. '/div'
  1847. );
  1848. $this->assertTags($result, $expected);
  1849. unset($this->Form->request->data);
  1850. $Contact = ClassRegistry::getObject('Contact');
  1851. $Contact->validationErrors['field'] = array('Badness!');
  1852. $result = $this->Form->input('Contact.field');
  1853. $expected = array(
  1854. 'div' => array('class' => 'input text error'),
  1855. 'label' => array('for' => 'ContactField'),
  1856. 'Field',
  1857. '/label',
  1858. 'input' => array(
  1859. 'type' => 'text', 'name' => 'data[Contact][field]',
  1860. 'id' => 'ContactField', 'class' => 'form-error'
  1861. ),
  1862. array('div' => array('class' => 'error-message')),
  1863. 'Badness!',
  1864. '/div',
  1865. '/div'
  1866. );
  1867. $this->assertTags($result, $expected);
  1868. $result = $this->Form->input('Contact.field', array(
  1869. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1870. ));
  1871. $expected = array(
  1872. 'label' => array('for' => 'ContactField'),
  1873. 'Field',
  1874. '/label',
  1875. 'input' => array(
  1876. 'type' => 'text', 'name' => 'data[Contact][field]',
  1877. 'id' => 'ContactField', 'class' => 'form-error'
  1878. ),
  1879. array('span' => array('class' => 'error-message')),
  1880. 'Badness!',
  1881. '/span'
  1882. );
  1883. $this->assertTags($result, $expected);
  1884. $result = $this->Form->input('Contact.field', array(
  1885. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1886. ));
  1887. $expected = array(
  1888. 'div' => array('class' => 'input text error'),
  1889. 'label' => array('for' => 'ContactField'),
  1890. 'Field',
  1891. '/label',
  1892. 'input' => array(
  1893. 'type' => 'text', 'name' => 'data[Contact][field]',
  1894. 'id' => 'ContactField', 'class' => 'form-error'
  1895. ),
  1896. array('div' => array('class' => 'error')),
  1897. 'Badness!',
  1898. '/div'
  1899. );
  1900. $this->assertTags($result, $expected);
  1901. $result = $this->Form->input('Contact.field', array(
  1902. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1903. ));
  1904. $expected = array(
  1905. 'span' => array('class' => 'input text error'),
  1906. 'label' => array('for' => 'ContactField'),
  1907. 'Field',
  1908. '/label',
  1909. 'input' => array(
  1910. 'type' => 'text', 'name' => 'data[Contact][field]',
  1911. 'id' => 'ContactField', 'class' => 'form-error'
  1912. ),
  1913. 'Badness!',
  1914. '/span'
  1915. );
  1916. $this->assertTags($result, $expected);
  1917. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1918. $expected = array(
  1919. 'div' => array('class' => 'input text error'),
  1920. 'label' => array('for' => 'ContactField'),
  1921. 'Field',
  1922. '/label',
  1923. 'input' => array(
  1924. 'type' => 'text', 'name' => 'data[Contact][field]',
  1925. 'id' => 'ContactField', 'class' => 'form-error'
  1926. ),
  1927. 'A message to you, Rudy',
  1928. array('div' => array('class' => 'error-message')),
  1929. 'Badness!',
  1930. '/div',
  1931. '/div'
  1932. );
  1933. $this->assertTags($result, $expected);
  1934. $this->Form->setEntity(null);
  1935. $this->Form->setEntity('Contact.field');
  1936. $result = $this->Form->input('Contact.field', array(
  1937. 'after' => 'A message to you, Rudy', 'error' => false
  1938. ));
  1939. $expected = array(
  1940. 'div' => array('class' => 'input text'),
  1941. 'label' => array('for' => 'ContactField'),
  1942. 'Field',
  1943. '/label',
  1944. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1945. 'A message to you, Rudy',
  1946. '/div'
  1947. );
  1948. $this->assertTags($result, $expected);
  1949. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1950. $expected = array(
  1951. 'div' => array('class' => 'input text'),
  1952. 'label' => array('for' => 'ObjectField'),
  1953. 'Field',
  1954. '/label',
  1955. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1956. 'A message to you, Rudy',
  1957. '/div'
  1958. );
  1959. $this->assertTags($result, $expected);
  1960. $Contact->validationErrors['field'] = array('minLength');
  1961. $result = $this->Form->input('Contact.field', array(
  1962. 'error' => array(
  1963. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1964. 'maxLength' => 'login too large'
  1965. )
  1966. ));
  1967. $expected = array(
  1968. 'div' => array('class' => 'input text error'),
  1969. 'label' => array('for' => 'ContactField'),
  1970. 'Field',
  1971. '/label',
  1972. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1973. array('div' => array('class' => 'error-message')),
  1974. 'Le login doit contenir au moins 2 caractères',
  1975. '/div',
  1976. '/div'
  1977. );
  1978. $this->assertTags($result, $expected);
  1979. $Contact->validationErrors['field'] = array('maxLength');
  1980. $result = $this->Form->input('Contact.field', array(
  1981. 'error' => array(
  1982. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1983. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1984. 'maxLength' => 'login too large',
  1985. )
  1986. ));
  1987. $expected = array(
  1988. 'div' => array('class' => 'input text error'),
  1989. 'label' => array('for' => 'ContactField'),
  1990. 'Field',
  1991. '/label',
  1992. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1993. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1994. 'login too large',
  1995. '/span',
  1996. '/div'
  1997. );
  1998. $this->assertTags($result, $expected);
  1999. }
  2000. /**
  2001. * Test that inputs with 0 can be created.
  2002. *
  2003. * @return void
  2004. */
  2005. public function testInputZero() {
  2006. $this->Form->create('User');
  2007. $result = $this->Form->input('0');
  2008. $expected = array(
  2009. 'div' => array('class' => 'input text'),
  2010. 'label' => array('for' => 'User0'), '/label',
  2011. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  2012. '/div'
  2013. );
  2014. $this->assertTags($result, $expected);
  2015. }
  2016. /**
  2017. * test input() with checkbox creation
  2018. *
  2019. * @return void
  2020. */
  2021. public function testInputCheckbox() {
  2022. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  2023. $expected = array(
  2024. 'div' => array('class' => 'input checkbox'),
  2025. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2026. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2027. '/div'
  2028. );
  2029. $this->assertTags($result, $expected);
  2030. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2031. $expected = array(
  2032. 'div' => array('class' => 'input checkbox'),
  2033. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2034. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2035. '/div'
  2036. );
  2037. $this->assertTags($result, $expected);
  2038. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2039. $expected = array(
  2040. 'div' => array('class' => 'input checkbox'),
  2041. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2042. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2043. '/div'
  2044. );
  2045. $this->assertTags($result, $expected);
  2046. $result = $this->Form->input('User.disabled', array(
  2047. 'label' => 'Disabled',
  2048. 'type' => 'checkbox',
  2049. 'data-foo' => 'disabled'
  2050. ));
  2051. $expected = array(
  2052. 'div' => array('class' => 'input checkbox'),
  2053. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  2054. array('input' => array(
  2055. 'type' => 'checkbox',
  2056. 'name' => 'data[User][disabled]',
  2057. 'value' => '1',
  2058. 'id' => 'UserDisabled',
  2059. 'data-foo' => 'disabled'
  2060. )),
  2061. 'label' => array('for' => 'UserDisabled'),
  2062. 'Disabled',
  2063. '/label',
  2064. '/div'
  2065. );
  2066. $this->assertTags($result, $expected);
  2067. }
  2068. /**
  2069. * test form->input() with time types.
  2070. *
  2071. */
  2072. public function testInputTime() {
  2073. extract($this->dateRegex);
  2074. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2075. $result = explode(':', $result);
  2076. $this->assertRegExp('/option value="23"/', $result[0]);
  2077. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2078. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2079. $result = explode(':', $result);
  2080. $this->assertRegExp('/option value="23"/', $result[0]);
  2081. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2082. $result = $this->Form->input('Model.field', array(
  2083. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2084. ));
  2085. $result = explode(':', $result);
  2086. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2087. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2088. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2089. $result = $this->Form->input('Model.field', array(
  2090. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2091. ));
  2092. $result = explode(':', $result);
  2093. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2094. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2095. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2096. $result = $this->Form->input('prueba', array(
  2097. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2098. 'maxYear' => date('Y') + 1, 'interval' => 15
  2099. ));
  2100. $result = explode(':', $result);
  2101. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2102. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2103. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2104. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2105. $result = $this->Form->input('Random.start_time', array(
  2106. 'type' => 'time',
  2107. 'selected' => '18:15'
  2108. ));
  2109. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2110. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2111. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2112. $result = $this->Form->input('published', array('type' => 'time'));
  2113. $now = strtotime('now');
  2114. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2115. $now = strtotime('2013-03-09 00:42:21');
  2116. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2117. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2118. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2119. }
  2120. /**
  2121. * Test interval + selected near the hour roll over.
  2122. *
  2123. * @return void
  2124. */
  2125. public function testTimeSelectedWithInterval() {
  2126. $result = $this->Form->input('Model.start_time', array(
  2127. 'type' => 'time',
  2128. 'interval' => 15,
  2129. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2130. ));
  2131. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2132. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2133. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2134. $result = $this->Form->input('Model.start_time', array(
  2135. 'type' => 'time',
  2136. 'interval' => 15,
  2137. 'selected' => '2012-10-23 15:57:00'
  2138. ));
  2139. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2140. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2141. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2142. $result = $this->Form->input('Model.start_time', array(
  2143. 'timeFormat' => 24,
  2144. 'type' => 'time',
  2145. 'interval' => 15,
  2146. 'selected' => '15:57'
  2147. ));
  2148. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2149. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2150. $result = $this->Form->input('Model.start_time', array(
  2151. 'timeFormat' => 24,
  2152. 'type' => 'time',
  2153. 'interval' => 15,
  2154. 'selected' => '23:57'
  2155. ));
  2156. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2157. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2158. $result = $this->Form->input('Model.created', array(
  2159. 'timeFormat' => 24,
  2160. 'type' => 'datetime',
  2161. 'interval' => 15,
  2162. 'selected' => '2012-09-30 23:56'
  2163. ));
  2164. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2165. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2166. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2167. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2168. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2169. }
  2170. /**
  2171. * Test time with selected values around 12:xx:xx
  2172. *
  2173. * @return void
  2174. */
  2175. public function testTimeSelectedWithIntervalTwelve() {
  2176. $result = $this->Form->input('Model.start_time', array(
  2177. 'type' => 'time',
  2178. 'timeFormat' => 12,
  2179. 'interval' => 15,
  2180. 'selected' => '00:00:00'
  2181. ));
  2182. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2183. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2184. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2185. $result = $this->Form->input('Model.start_time', array(
  2186. 'type' => 'time',
  2187. 'timeFormat' => 12,
  2188. 'interval' => 15,
  2189. 'selected' => '12:00:00'
  2190. ));
  2191. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2192. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2193. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2194. $result = $this->Form->input('Model.start_time', array(
  2195. 'type' => 'time',
  2196. 'timeFormat' => 12,
  2197. 'interval' => 15,
  2198. 'selected' => '12:15:00'
  2199. ));
  2200. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2201. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2202. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2203. }
  2204. /**
  2205. * Test interval & timeFormat = 12
  2206. *
  2207. * @return void
  2208. */
  2209. public function testInputTimeWithIntervalAnd12HourFormat() {
  2210. $result = $this->Form->input('Model.start_time', array(
  2211. 'type' => 'time',
  2212. 'timeFormat' => 12,
  2213. 'interval' => 5,
  2214. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2215. ));
  2216. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2217. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2218. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2219. $result = $this->Form->input('Model.start_time', array(
  2220. 'type' => 'time',
  2221. 'timeFormat' => '12',
  2222. 'interval' => 5,
  2223. 'selected' => '2013-04-19 16:30:00'
  2224. ));
  2225. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2226. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2227. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2228. $result = $this->Form->input('Model.start_time', array(
  2229. 'type' => 'time',
  2230. 'timeFormat' => '12',
  2231. 'interval' => 10,
  2232. 'selected' => '2013-05-19 00:33:00'
  2233. ));
  2234. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2235. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2236. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2237. $result = $this->Form->input('Model.start_time', array(
  2238. 'type' => 'time',
  2239. 'timeFormat' => '12',
  2240. 'interval' => 10,
  2241. 'selected' => '2013-05-19 13:33:00'
  2242. ));
  2243. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2244. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2245. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2246. $result = $this->Form->input('Model.start_time', array(
  2247. 'type' => 'time',
  2248. 'timeFormat' => '12',
  2249. 'interval' => 10,
  2250. 'selected' => '2013-05-19 01:33:00'
  2251. ));
  2252. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2253. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2254. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2255. }
  2256. /**
  2257. * test form->input() with datetime, date and time types
  2258. *
  2259. * @return void
  2260. */
  2261. public function testInputDatetime() {
  2262. extract($this->dateRegex);
  2263. $result = $this->Form->input('prueba', array(
  2264. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2265. 'maxYear' => date('Y') + 1, 'interval' => 15
  2266. ));
  2267. $result = explode(':', $result);
  2268. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2269. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2270. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2271. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2272. //related to ticket #5013
  2273. $result = $this->Form->input('Contact.date', array(
  2274. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2275. ));
  2276. $this->assertRegExp('/class="customClass"/', $result);
  2277. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2278. $result = $this->Form->input('Contact.date', array(
  2279. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2280. ));
  2281. $this->assertRegExp('/id="customIdDay"/', $result);
  2282. $this->assertRegExp('/id="customIdMonth"/', $result);
  2283. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2284. $result = $this->Form->input('Model.field', array(
  2285. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2286. ));
  2287. $this->assertRegExp('/id="customIDDay"/', $result);
  2288. $this->assertRegExp('/id="customIDHour"/', $result);
  2289. $result = explode('</select><select', $result);
  2290. $result = explode(':', $result[1]);
  2291. $this->assertRegExp('/option value="23"/', $result[0]);
  2292. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2293. $result = $this->Form->input('Model.field', array(
  2294. 'type' => 'datetime', 'timeFormat' => 12
  2295. ));
  2296. $result = explode('</select><select', $result);
  2297. $result = explode(':', $result[1]);
  2298. $this->assertRegExp('/option value="12"/', $result[0]);
  2299. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2300. $this->Form->request->data = array('Contact' => array('created' => null));
  2301. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2302. $expected = array(
  2303. 'div' => array('class' => 'input date'),
  2304. 'label' => array('for' => 'ContactCreatedMonth'),
  2305. 'Created',
  2306. '/label',
  2307. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2308. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2309. $monthsRegex,
  2310. '/select', '-',
  2311. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2312. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2313. $daysRegex,
  2314. '/select', '-',
  2315. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2316. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2317. $yearsRegex,
  2318. '/select',
  2319. '/div'
  2320. );
  2321. $this->assertTags($result, $expected);
  2322. $this->Form->request->data = array('Contact' => array('created' => null));
  2323. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2324. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2325. $this->Form->request->data = array('Contact' => array('created' => null));
  2326. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2327. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2328. $result = $this->Form->input('Contact.created', array(
  2329. 'type' => 'date',
  2330. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2331. 'timeFormat' => 'NONE'
  2332. ));
  2333. $this->assertRegExp('/for\="created-month"/', $result);
  2334. }
  2335. /**
  2336. * Test generating checkboxes in a loop.
  2337. *
  2338. * @return void
  2339. */
  2340. public function testInputCheckboxesInLoop() {
  2341. for ($i = 1; $i < 5; $i++) {
  2342. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2343. $expected = array(
  2344. 'div' => array('class' => 'input checkbox'),
  2345. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2346. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2347. 'label' => array('for' => "Contact{$i}Email"),
  2348. 'Email',
  2349. '/label',
  2350. '/div'
  2351. );
  2352. $this->assertTags($result, $expected);
  2353. }
  2354. }
  2355. /**
  2356. * Test generating checkboxes with disabled elements.
  2357. *
  2358. * @return void
  2359. */
  2360. public function testInputCheckboxWithDisabledElements() {
  2361. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2362. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2363. $expected = array(
  2364. array('div' => array('class' => 'input select')),
  2365. array('label' => array('for' => "ContactMultiple")),
  2366. 'Multiple',
  2367. '/label',
  2368. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2369. array('div' => array('class' => 'checkbox')),
  2370. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2371. array('label' => array('for' => "ContactMultiple1")),
  2372. 'One',
  2373. '/label',
  2374. '/div',
  2375. array('div' => array('class' => 'checkbox')),
  2376. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2377. array('label' => array('for' => "ContactMultiple2")),
  2378. 'Two',
  2379. '/label',
  2380. '/div',
  2381. array('div' => array('class' => 'checkbox')),
  2382. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2383. array('label' => array('for' => "ContactMultiple3")),
  2384. 'Three',
  2385. '/label',
  2386. '/div',
  2387. '/div'
  2388. );
  2389. $this->assertTags($result, $expected);
  2390. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2391. $this->assertTags($result, $expected);
  2392. $disabled = array('2', 3);
  2393. $expected = array(
  2394. array('div' => array('class' => 'input select')),
  2395. array('label' => array('for' => "ContactMultiple")),
  2396. 'Multiple',
  2397. '/label',
  2398. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2399. array('div' => array('class' => 'checkbox')),
  2400. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2401. array('label' => array('for' => "ContactMultiple1")),
  2402. 'One',
  2403. '/label',
  2404. '/div',
  2405. array('div' => array('class' => 'checkbox')),
  2406. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2407. array('label' => array('for' => "ContactMultiple2")),
  2408. 'Two',
  2409. '/label',
  2410. '/div',
  2411. array('div' => array('class' => 'checkbox')),
  2412. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2413. array('label' => array('for' => "ContactMultiple3")),
  2414. 'Three',
  2415. '/label',
  2416. '/div',
  2417. '/div'
  2418. );
  2419. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2420. $this->assertTags($result, $expected);
  2421. // make sure 50 does only disable 50, and not 50f5c0cf
  2422. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2423. $disabled = array(50);
  2424. $expected = array(
  2425. array('div' => array('class' => 'input select')),
  2426. array('label' => array('for' => "ContactMultiple")),
  2427. 'Multiple',
  2428. '/label',
  2429. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2430. array('div' => array('class' => 'checkbox')),
  2431. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2432. array('label' => array('for' => "ContactMultiple50")),
  2433. 'Fifty',
  2434. '/label',
  2435. '/div',
  2436. array('div' => array('class' => 'checkbox')),
  2437. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2438. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2439. 'Stringy',
  2440. '/label',
  2441. '/div',
  2442. '/div'
  2443. );
  2444. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2445. $this->assertTags($result, $expected);
  2446. }
  2447. /**
  2448. * test input name with leading integer, ensure attributes are generated correctly.
  2449. *
  2450. * @return void
  2451. */
  2452. public function testInputWithLeadingInteger() {
  2453. $result = $this->Form->text('0.Node.title');
  2454. $expected = array(
  2455. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2456. );
  2457. $this->assertTags($result, $expected);
  2458. }
  2459. /**
  2460. * test form->input() with select type inputs.
  2461. *
  2462. * @return void
  2463. */
  2464. public function testInputSelectType() {
  2465. $result = $this->Form->input('email', array(
  2466. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2467. );
  2468. $expected = array(
  2469. 'div' => array('class' => 'input select'),
  2470. 'label' => array('for' => 'email'),
  2471. 'Email',
  2472. '/label',
  2473. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2474. array('option' => array('value' => '')),
  2475. '/option',
  2476. array('option' => array('value' => 'è')),
  2477. 'Firést',
  2478. '/option',
  2479. array('option' => array('value' => 'é')),
  2480. 'Secoènd',
  2481. '/option',
  2482. '/select',
  2483. '/div'
  2484. );
  2485. $this->assertTags($result, $expected);
  2486. $result = $this->Form->input('email', array(
  2487. 'options' => array('First', 'Second'), 'empty' => true)
  2488. );
  2489. $expected = array(
  2490. 'div' => array('class' => 'input select'),
  2491. 'label' => array('for' => 'email'),
  2492. 'Email',
  2493. '/label',
  2494. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2495. array('option' => array('value' => '')),
  2496. '/option',
  2497. array('option' => array('value' => '0')),
  2498. 'First',
  2499. '/option',
  2500. array('option' => array('value' => '1')),
  2501. 'Second',
  2502. '/option',
  2503. '/select',
  2504. '/div'
  2505. );
  2506. $this->assertTags($result, $expected);
  2507. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2508. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2509. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2510. $expected = array(
  2511. 'div' => array('class' => 'input select'),
  2512. 'label' => array('for' => 'ModelUserId'),
  2513. 'User',
  2514. '/label',
  2515. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2516. array('option' => array('value' => '')),
  2517. '/option',
  2518. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2519. 'good',
  2520. '/option',
  2521. array('option' => array('value' => 'other')),
  2522. 'bad',
  2523. '/option',
  2524. '/select',
  2525. '/div'
  2526. );
  2527. $this->assertTags($result, $expected);
  2528. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2529. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2530. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2531. $expected = array(
  2532. 'div' => array('class' => 'input select'),
  2533. 'label' => array('for' => 'ThingUserId'),
  2534. 'User',
  2535. '/label',
  2536. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2537. array('option' => array('value' => '')),
  2538. 'Some Empty',
  2539. '/option',
  2540. array('option' => array('value' => 'value')),
  2541. 'good',
  2542. '/option',
  2543. array('option' => array('value' => 'other')),
  2544. 'bad',
  2545. '/option',
  2546. '/select',
  2547. '/div'
  2548. );
  2549. $this->assertTags($result, $expected);
  2550. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2551. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2552. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2553. $expected = array(
  2554. 'div' => array('class' => 'input select'),
  2555. 'label' => array('for' => 'ThingUserId'),
  2556. 'User',
  2557. '/label',
  2558. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2559. array('option' => array('value' => '')),
  2560. 'Some Empty',
  2561. '/option',
  2562. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2563. 'good',
  2564. '/option',
  2565. array('option' => array('value' => 'other')),
  2566. 'bad',
  2567. '/option',
  2568. '/select',
  2569. '/div'
  2570. );
  2571. $this->assertTags($result, $expected);
  2572. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2573. $this->Form->request->data = array('User' => array('User' => array('value')));
  2574. $result = $this->Form->input('User.User', array('empty' => true));
  2575. $expected = array(
  2576. 'div' => array('class' => 'input select'),
  2577. 'label' => array('for' => 'UserUser'),
  2578. 'User',
  2579. '/label',
  2580. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2581. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2582. array('option' => array('value' => '')),
  2583. '/option',
  2584. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2585. 'good',
  2586. '/option',
  2587. array('option' => array('value' => 'other')),
  2588. 'bad',
  2589. '/option',
  2590. '/select',
  2591. '/div'
  2592. );
  2593. $this->assertTags($result, $expected);
  2594. $this->Form->data = array();
  2595. $result = $this->Form->input('Publisher.id', array(
  2596. 'label' => 'Publisher',
  2597. 'type' => 'select',
  2598. 'multiple' => 'checkbox',
  2599. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2600. ));
  2601. $expected = array(
  2602. array('div' => array('class' => 'input select')),
  2603. array('label' => array('for' => 'PublisherId')),
  2604. 'Publisher',
  2605. '/label',
  2606. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2607. array('div' => array('class' => 'checkbox')),
  2608. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2609. array('label' => array('for' => 'PublisherIdValue1')),
  2610. 'Label 1',
  2611. '/label',
  2612. '/div',
  2613. array('div' => array('class' => 'checkbox')),
  2614. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2615. array('label' => array('for' => 'PublisherIdValue2')),
  2616. 'Label 2',
  2617. '/label',
  2618. '/div',
  2619. '/div'
  2620. );
  2621. $this->assertTags($result, $expected);
  2622. }
  2623. /**
  2624. * test that input() and a non standard primary key makes a hidden input by default.
  2625. *
  2626. * @return void
  2627. */
  2628. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2629. $this->Form->create('User');
  2630. $this->Form->fieldset = array(
  2631. 'User' => array(
  2632. 'fields' => array(
  2633. 'model_id' => array('type' => 'integer')
  2634. ),
  2635. 'validates' => array(),
  2636. 'key' => 'model_id'
  2637. )
  2638. );
  2639. $result = $this->Form->input('model_id');
  2640. $expected = array(
  2641. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2642. );
  2643. $this->assertTags($result, $expected);
  2644. }
  2645. /**
  2646. * test that overriding the magic select type widget is possible
  2647. *
  2648. * @return void
  2649. */
  2650. public function testInputOverridingMagicSelectType() {
  2651. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2652. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2653. $expected = array(
  2654. 'div' => array('class' => 'input text'),
  2655. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2656. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2657. '/div'
  2658. );
  2659. $this->assertTags($result, $expected);
  2660. //Check that magic types still work for plural/singular vars
  2661. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2662. $result = $this->Form->input('Model.type');
  2663. $expected = array(
  2664. 'div' => array('class' => 'input select'),
  2665. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2666. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2667. array('option' => array('value' => 'value')), 'good', '/option',
  2668. array('option' => array('value' => 'other')), 'bad', '/option',
  2669. '/select',
  2670. '/div'
  2671. );
  2672. $this->assertTags($result, $expected);
  2673. }
  2674. /**
  2675. * Test that inferred types do not override developer input
  2676. *
  2677. * @return void
  2678. */
  2679. public function testInputMagicTypeDoesNotOverride() {
  2680. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2681. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2682. $expected = array(
  2683. 'div' => array('class' => 'input checkbox'),
  2684. array('input' => array(
  2685. 'type' => 'hidden',
  2686. 'name' => 'data[Model][user]',
  2687. 'id' => 'ModelUser_',
  2688. 'value' => 0,
  2689. )),
  2690. array('input' => array(
  2691. 'name' => 'data[Model][user]',
  2692. 'type' => 'checkbox',
  2693. 'id' => 'ModelUser',
  2694. 'value' => 1
  2695. )),
  2696. 'label' => array('for' => 'ModelUser'), 'User', '/label',
  2697. '/div'
  2698. );
  2699. $this->assertTags($result, $expected);
  2700. }
  2701. /**
  2702. * Test that magic input() selects are created for type=number
  2703. *
  2704. * @return void
  2705. */
  2706. public function testInputMagicSelectForTypeNumber() {
  2707. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2708. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2709. $result = $this->Form->input('ValidateUser.balance');
  2710. $expected = array(
  2711. 'div' => array('class' => 'input select'),
  2712. 'label' => array('for' => 'ValidateUserBalance'),
  2713. 'Balance',
  2714. '/label',
  2715. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2716. array('option' => array('value' => '0')),
  2717. 'nothing',
  2718. '/option',
  2719. array('option' => array('value' => '1', 'selected' => 'selected')),
  2720. 'some',
  2721. '/option',
  2722. array('option' => array('value' => '100')),
  2723. 'a lot',
  2724. '/option',
  2725. '/select',
  2726. '/div'
  2727. );
  2728. $this->assertTags($result, $expected);
  2729. }
  2730. /**
  2731. * Test that magic input() selects can easily be converted into radio types without error.
  2732. *
  2733. * @return void
  2734. */
  2735. public function testInputMagicSelectChangeToRadio() {
  2736. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2737. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2738. $this->assertRegExp('/input type="radio"/', $result);
  2739. }
  2740. /**
  2741. * fields with the same name as the model should work.
  2742. *
  2743. * @return void
  2744. */
  2745. public function testInputWithMatchingFieldAndModelName() {
  2746. $this->Form->create('User');
  2747. $this->Form->fieldset = array(
  2748. 'User' => array(
  2749. 'fields' => array(
  2750. 'User' => array('type' => 'text')
  2751. ),
  2752. 'validates' => array(),
  2753. 'key' => 'id'
  2754. )
  2755. );
  2756. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2757. $result = $this->Form->input('User', array('type' => 'text'));
  2758. $expected = array(
  2759. 'div' => array('class' => 'input text'),
  2760. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2761. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2762. '/div'
  2763. );
  2764. $this->assertTags($result, $expected);
  2765. }
  2766. /**
  2767. * testFormInputs method
  2768. *
  2769. * test correct results from form::inputs().
  2770. *
  2771. * @return void
  2772. */
  2773. public function testFormInputs() {
  2774. $this->Form->create('Contact');
  2775. $result = $this->Form->inputs('The Legend');
  2776. $expected = array(
  2777. '<fieldset',
  2778. '<legend',
  2779. 'The Legend',
  2780. '/legend',
  2781. '*/fieldset',
  2782. );
  2783. $this->assertTags($result, $expected);
  2784. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2785. $expected = array(
  2786. 'fieldset' => array('class' => 'classy-stuff'),
  2787. '<legend',
  2788. 'Field of Dreams',
  2789. '/legend',
  2790. '*/fieldset'
  2791. );
  2792. $this->assertTags($result, $expected);
  2793. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2794. $this->assertTags($result, $expected);
  2795. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2796. $this->assertTags($result, $expected);
  2797. $this->Form->create('Contact');
  2798. $this->Form->request['prefix'] = 'admin';
  2799. $this->Form->request['action'] = 'admin_edit';
  2800. $result = $this->Form->inputs();
  2801. $expected = array(
  2802. '<fieldset',
  2803. '<legend',
  2804. 'Edit Contact',
  2805. '/legend',
  2806. '*/fieldset',
  2807. );
  2808. $this->assertTags($result, $expected);
  2809. $this->Form->create('Contact');
  2810. $result = $this->Form->inputs(false);
  2811. $expected = array(
  2812. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2813. array('div' => array('class' => 'input text')),
  2814. '*/div',
  2815. array('div' => array('class' => 'input email')),
  2816. '*/div',
  2817. array('div' => array('class' => 'input tel')),
  2818. '*/div',
  2819. array('div' => array('class' => 'input password')),
  2820. '*/div',
  2821. array('div' => array('class' => 'input date')),
  2822. '*/div',
  2823. array('div' => array('class' => 'input date')),
  2824. '*/div',
  2825. array('div' => array('class' => 'input datetime')),
  2826. '*/div',
  2827. array('div' => array('class' => 'input number')),
  2828. '*/div',
  2829. array('div' => array('class' => 'input select')),
  2830. '*/div',
  2831. );
  2832. $this->assertTags($result, $expected);
  2833. $this->Form->create('Contact');
  2834. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2835. $expected = array(
  2836. 'input' => array('type' => 'hidden', 'name' => 'data[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. );
  2856. $this->assertTags($result, $expected);
  2857. $this->Form->create('Contact');
  2858. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2859. $this->assertTags($result, $expected);
  2860. $this->Form->create('Contact');
  2861. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2862. $expected = array(
  2863. 'fieldset' => array(),
  2864. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2865. array('div' => array('class' => 'input text')),
  2866. '*/div',
  2867. array('div' => array('class' => 'input email')),
  2868. '*/div',
  2869. array('div' => array('class' => 'input tel')),
  2870. '*/div',
  2871. array('div' => array('class' => 'input password')),
  2872. '*/div',
  2873. array('div' => array('class' => 'input date')),
  2874. '*/div',
  2875. array('div' => array('class' => 'input date')),
  2876. '*/div',
  2877. array('div' => array('class' => 'input datetime')),
  2878. '*/div',
  2879. array('div' => array('class' => 'input number')),
  2880. '*/div',
  2881. array('div' => array('class' => 'input select')),
  2882. '*/div',
  2883. '/fieldset'
  2884. );
  2885. $this->assertTags($result, $expected);
  2886. $this->Form->create('Contact');
  2887. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2888. $expected = array(
  2889. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2890. array('div' => array('class' => 'input text')),
  2891. '*/div',
  2892. array('div' => array('class' => 'input email')),
  2893. '*/div',
  2894. array('div' => array('class' => 'input tel')),
  2895. '*/div',
  2896. array('div' => array('class' => 'input password')),
  2897. '*/div',
  2898. array('div' => array('class' => 'input date')),
  2899. '*/div',
  2900. array('div' => array('class' => 'input date')),
  2901. '*/div',
  2902. array('div' => array('class' => 'input datetime')),
  2903. '*/div',
  2904. array('div' => array('class' => 'input number')),
  2905. '*/div',
  2906. array('div' => array('class' => 'input select')),
  2907. '*/div',
  2908. );
  2909. $this->assertTags($result, $expected);
  2910. $this->Form->create('Contact');
  2911. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2912. $this->assertTags($result, $expected);
  2913. $this->Form->create('Contact');
  2914. $result = $this->Form->inputs('Hello');
  2915. $expected = array(
  2916. 'fieldset' => array(),
  2917. 'legend' => array(),
  2918. 'Hello',
  2919. '/legend',
  2920. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2921. array('div' => array('class' => 'input text')),
  2922. '*/div',
  2923. array('div' => array('class' => 'input email')),
  2924. '*/div',
  2925. array('div' => array('class' => 'input tel')),
  2926. '*/div',
  2927. array('div' => array('class' => 'input password')),
  2928. '*/div',
  2929. array('div' => array('class' => 'input date')),
  2930. '*/div',
  2931. array('div' => array('class' => 'input date')),
  2932. '*/div',
  2933. array('div' => array('class' => 'input datetime')),
  2934. '*/div',
  2935. array('div' => array('class' => 'input number')),
  2936. '*/div',
  2937. array('div' => array('class' => 'input select')),
  2938. '*/div',
  2939. '/fieldset'
  2940. );
  2941. $this->assertTags($result, $expected);
  2942. $this->Form->create('Contact');
  2943. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2944. $expected = array(
  2945. 'fieldset' => array(),
  2946. 'legend' => array(),
  2947. 'Hello',
  2948. '/legend',
  2949. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2950. array('div' => array('class' => 'input text')),
  2951. '*/div',
  2952. array('div' => array('class' => 'input email')),
  2953. '*/div',
  2954. array('div' => array('class' => 'input tel')),
  2955. '*/div',
  2956. array('div' => array('class' => 'input password')),
  2957. '*/div',
  2958. array('div' => array('class' => 'input date')),
  2959. '*/div',
  2960. array('div' => array('class' => 'input date')),
  2961. '*/div',
  2962. array('div' => array('class' => 'input datetime')),
  2963. '*/div',
  2964. array('div' => array('class' => 'input number')),
  2965. '*/div',
  2966. array('div' => array('class' => 'input select')),
  2967. '*/div',
  2968. '/fieldset'
  2969. );
  2970. $this->assertTags($result, $expected);
  2971. $this->Form->create('Contact');
  2972. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2973. $this->assertTags($result, $expected);
  2974. $this->Form->end();
  2975. $this->Form->create(false);
  2976. $expected = array(
  2977. 'fieldset' => array(),
  2978. array('div' => array('class' => 'input text')),
  2979. 'label' => array('for' => 'foo'),
  2980. 'Foo',
  2981. '/label',
  2982. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2983. '*/div',
  2984. '/fieldset'
  2985. );
  2986. $result = $this->Form->inputs(
  2987. array('foo' => array('type' => 'text')),
  2988. array(),
  2989. array('legend' => false)
  2990. );
  2991. $this->assertTags($result, $expected);
  2992. }
  2993. /**
  2994. * Tests inputs() works with plugin models
  2995. *
  2996. * @return void
  2997. */
  2998. public function testInputsPluginModel() {
  2999. $this->loadFixtures('Post');
  3000. App::build(array(
  3001. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  3002. ));
  3003. CakePlugin::load('TestPlugin');
  3004. $this->Form->request['models'] = array(
  3005. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  3006. );
  3007. $this->Form->create('TestPlugin.TestPluginPost');
  3008. $result = $this->Form->inputs();
  3009. $this->assertContains('data[TestPluginPost][id]', $result);
  3010. $this->assertContains('data[TestPluginPost][author_id]', $result);
  3011. $this->assertContains('data[TestPluginPost][title]', $result);
  3012. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  3013. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  3014. $this->assertEquals('TestPluginPost', $this->Form->model());
  3015. }
  3016. /**
  3017. * testSelectAsCheckbox method
  3018. *
  3019. * test multi-select widget with checkbox formatting.
  3020. *
  3021. * @return void
  3022. */
  3023. public function testSelectAsCheckbox() {
  3024. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  3025. $expected = array(
  3026. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3027. array('div' => array('class' => 'checkbox')),
  3028. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  3029. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  3030. 'first',
  3031. '/label',
  3032. '/div',
  3033. array('div' => array('class' => 'checkbox')),
  3034. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  3035. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  3036. 'second',
  3037. '/label',
  3038. '/div',
  3039. array('div' => array('class' => 'checkbox')),
  3040. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3041. array('label' => array('for' => 'ModelMultiField2')),
  3042. 'third',
  3043. '/label',
  3044. '/div',
  3045. );
  3046. $this->assertTags($result, $expected);
  3047. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  3048. $expected = array(
  3049. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3050. array('div' => array('class' => 'checkbox')),
  3051. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  3052. array('label' => array('for' => 'ModelMultiField12')),
  3053. 'half',
  3054. '/label',
  3055. '/div',
  3056. );
  3057. $this->assertTags($result, $expected);
  3058. }
  3059. /**
  3060. * testLabel method
  3061. *
  3062. * test label generation.
  3063. *
  3064. * @return void
  3065. */
  3066. public function testLabel() {
  3067. $this->Form->text('Person.name');
  3068. $result = $this->Form->label();
  3069. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  3070. $this->Form->text('Person.name');
  3071. $result = $this->Form->label();
  3072. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  3073. $result = $this->Form->label('Person.first_name');
  3074. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  3075. $result = $this->Form->label('Person.first_name', 'Your first name');
  3076. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  3077. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  3078. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  3079. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  3080. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  3081. $result = $this->Form->label('Person.first_name', '');
  3082. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  3083. $result = $this->Form->label('Person.2.name', '');
  3084. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  3085. }
  3086. /**
  3087. * testTextbox method
  3088. *
  3089. * test textbox element generation
  3090. *
  3091. * @return void
  3092. */
  3093. public function testTextbox() {
  3094. $result = $this->Form->text('Model.field');
  3095. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3096. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3097. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3098. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3099. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  3100. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3101. $result = $this->Form->text('Model.text');
  3102. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  3103. $Contact = ClassRegistry::getObject('Contact');
  3104. $Contact->validationErrors['text'] = array(true);
  3105. $this->Form->request->data['Contact']['text'] = 'test';
  3106. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  3107. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3108. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  3109. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  3110. $expected = array(
  3111. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  3112. );
  3113. $this->assertTags($result, $expected);
  3114. }
  3115. /**
  3116. * testDefaultValue method
  3117. *
  3118. * Test default value setting
  3119. *
  3120. * @return void
  3121. */
  3122. public function testDefaultValue() {
  3123. $this->Form->request->data['Model']['field'] = 'test';
  3124. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3125. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3126. unset($this->Form->request->data['Model']['field']);
  3127. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3128. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3129. }
  3130. /**
  3131. * testCheckboxDefaultValue method
  3132. *
  3133. * Test default value setting on checkbox() method
  3134. *
  3135. * @return void
  3136. */
  3137. public function testCheckboxDefaultValue() {
  3138. $this->Form->request->data['Model']['field'] = false;
  3139. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3140. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3141. unset($this->Form->request->data['Model']['field']);
  3142. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3143. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3144. $this->Form->request->data['Model']['field'] = true;
  3145. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3146. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3147. unset($this->Form->request->data['Model']['field']);
  3148. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3149. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3150. }
  3151. /**
  3152. * testError method
  3153. *
  3154. * Test field error generation
  3155. *
  3156. * @return void
  3157. */
  3158. public function testError() {
  3159. $Contact = ClassRegistry::getObject('Contact');
  3160. $Contact->validationErrors['field'] = array(1);
  3161. $result = $this->Form->error('Contact.field');
  3162. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3163. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3164. $this->assertEquals('Error in field Field', $result);
  3165. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3166. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3167. $this->assertEquals('This field contains invalid input', $result);
  3168. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3169. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3170. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3171. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3172. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3173. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3174. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3175. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3176. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3177. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3178. $this->assertEquals('<strong>Badness!</strong>', $result);
  3179. $Contact->validationErrors['field'] = array("email");
  3180. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3181. $expected = array(
  3182. 'div' => array('class' => 'field-error'),
  3183. 'No good!',
  3184. '/div'
  3185. );
  3186. $this->assertTags($result, $expected);
  3187. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3188. $result = $this->Form->error('Contact.field', array(
  3189. 'notEmpty' => 'Cannot be empty',
  3190. 'email' => 'No good!'
  3191. ));
  3192. $expected = array(
  3193. 'div' => array('class' => 'error-message'),
  3194. 'ul' => array(),
  3195. '<li', 'Cannot be empty', '/li',
  3196. '<li', 'No good!', '/li',
  3197. '<li', 'Something else', '/li',
  3198. '/ul',
  3199. '/div'
  3200. );
  3201. $this->assertTags($result, $expected);
  3202. // Testing error messages list options
  3203. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3204. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3205. $expected = array(
  3206. 'div' => array('class' => 'error-message'),
  3207. 'ol' => array(),
  3208. '<li', 'notEmpty', '/li',
  3209. '<li', 'email', '/li',
  3210. '/ol',
  3211. '/div'
  3212. );
  3213. $this->assertTags($result, $expected);
  3214. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3215. $expected = array(
  3216. 'div' => array('class' => 'error-message'),
  3217. 'ol' => array(),
  3218. '<li', 'notEmpty', '/li',
  3219. '<li', 'email', '/li',
  3220. '/ol',
  3221. '/div'
  3222. );
  3223. $this->assertTags($result, $expected);
  3224. $result = $this->Form->error('Contact.field', null, array(
  3225. 'listOptions' => array(
  3226. 'class' => 'ul-class',
  3227. 'itemOptions' => array(
  3228. 'class' => 'li-class'
  3229. )
  3230. )
  3231. ));
  3232. $expected = array(
  3233. 'div' => array('class' => 'error-message'),
  3234. 'ul' => array('class' => 'ul-class'),
  3235. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3236. array('li' => array('class' => 'li-class')), 'email', '/li',
  3237. '/ul',
  3238. '/div'
  3239. );
  3240. $this->assertTags($result, $expected);
  3241. }
  3242. /**
  3243. * test error options when using form->input();
  3244. *
  3245. * @return void
  3246. */
  3247. public function testInputErrorEscape() {
  3248. $this->Form->create('ValidateProfile');
  3249. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3250. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3251. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3252. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3253. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3254. $this->assertRegExp('/required<br>/', $result);
  3255. }
  3256. /**
  3257. * testPassword method
  3258. *
  3259. * Test password element generation
  3260. *
  3261. * @return void
  3262. */
  3263. public function testPassword() {
  3264. $Contact = ClassRegistry::getObject('Contact');
  3265. $result = $this->Form->password('Contact.field');
  3266. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3267. $Contact->validationErrors['passwd'] = 1;
  3268. $this->Form->request->data['Contact']['passwd'] = 'test';
  3269. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3270. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3271. }
  3272. /**
  3273. * testRadio method
  3274. *
  3275. * Test radio element set generation
  3276. *
  3277. * @return void
  3278. */
  3279. public function testRadio() {
  3280. $result = $this->Form->radio('Model.field', array('option A'));
  3281. $expected = array(
  3282. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3283. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3284. 'label' => array('for' => 'ModelField0'),
  3285. 'option A',
  3286. '/label'
  3287. );
  3288. $this->assertTags($result, $expected);
  3289. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3290. $expected = array(
  3291. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3292. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3293. 'label' => array('for' => 'ModelField12'),
  3294. 'half',
  3295. '/label'
  3296. );
  3297. $this->assertTags($result, $expected);
  3298. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3299. $expected = array(
  3300. 'fieldset' => array(),
  3301. 'legend' => array(),
  3302. 'Field',
  3303. '/legend',
  3304. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3305. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3306. array('label' => array('for' => 'ModelField0')),
  3307. 'option A',
  3308. '/label',
  3309. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3310. array('label' => array('for' => 'ModelField1')),
  3311. 'option B',
  3312. '/label',
  3313. '/fieldset'
  3314. );
  3315. $this->assertTags($result, $expected);
  3316. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3317. $expected = array(
  3318. 'fieldset' => array(),
  3319. 'legend' => array(),
  3320. 'Field',
  3321. '/legend',
  3322. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3323. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3324. array('label' => array('for' => 'ModelField0')),
  3325. 'option A',
  3326. '/label',
  3327. 'br' => array(),
  3328. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3329. array('label' => array('for' => 'ModelField1')),
  3330. 'option B',
  3331. '/label',
  3332. '/fieldset'
  3333. );
  3334. $this->assertTags($result, $expected);
  3335. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3336. $expected = array(
  3337. 'fieldset' => array(),
  3338. 'legend' => array(),
  3339. 'Gender',
  3340. '/legend',
  3341. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3342. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3343. array('label' => array('for' => 'EmployeeGenderMale')),
  3344. 'Male',
  3345. '/label',
  3346. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3347. array('label' => array('for' => 'EmployeeGenderFemale')),
  3348. 'Female',
  3349. '/label',
  3350. '/fieldset',
  3351. );
  3352. $this->assertTags($result, $expected);
  3353. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3354. $expected = array(
  3355. 'fieldset' => array(),
  3356. 'legend' => array(),
  3357. 'Gender',
  3358. '/legend',
  3359. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3360. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3361. array('label' => array('for' => 'OfficerGenderMale')),
  3362. 'Male',
  3363. '/label',
  3364. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3365. array('label' => array('for' => 'OfficerGenderFemale')),
  3366. 'Female',
  3367. '/label',
  3368. '/fieldset',
  3369. );
  3370. $this->assertTags($result, $expected);
  3371. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3372. $expected = array(
  3373. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3374. array('input' => array(
  3375. 'type' => 'radio',
  3376. 'name' => 'data[Contact][1][imrequired]',
  3377. 'value' => '0',
  3378. 'id' => 'Contact1Imrequired0',
  3379. 'required' => 'required'
  3380. )),
  3381. 'label' => array('for' => 'Contact1Imrequired0'),
  3382. 'option A',
  3383. '/label'
  3384. );
  3385. $this->assertTags($result, $expected);
  3386. $result = $this->Form->radio('Model.1.field', array('option A'));
  3387. $expected = array(
  3388. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3389. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3390. 'label' => array('for' => 'Model1Field0'),
  3391. 'option A',
  3392. '/label'
  3393. );
  3394. $this->assertTags($result, $expected);
  3395. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3396. $expected = array(
  3397. 'fieldset' => array(),
  3398. 'legend' => array(),
  3399. 'Field',
  3400. '/legend',
  3401. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3402. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3403. array('label' => array('for' => 'ModelField0')),
  3404. 'option A',
  3405. '/label',
  3406. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3407. array('label' => array('for' => 'ModelField1')),
  3408. 'option B',
  3409. '/label',
  3410. '/fieldset'
  3411. );
  3412. $this->assertTags($result, $expected);
  3413. $result = $this->Form->radio(
  3414. 'Model.field',
  3415. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third')
  3416. );
  3417. $expected = array(
  3418. 'fieldset' => array(),
  3419. 'legend' => array(),
  3420. 'Field',
  3421. '/legend',
  3422. 'input' => array(
  3423. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3424. 'id' => 'ModelField_', 'value' => '',
  3425. ),
  3426. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3427. 'id' => 'ModelFieldAB', 'value' => 'a&gt;b')),
  3428. array('label' => array('for' => 'ModelFieldAB')),
  3429. 'first',
  3430. '/label',
  3431. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3432. 'id' => 'ModelFieldAB1', 'value' => 'a&lt;b')),
  3433. array('label' => array('for' => 'ModelFieldAB1')),
  3434. 'second',
  3435. '/label',
  3436. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3437. 'id' => 'ModelFieldAB2', 'value' => 'a&quot;b')),
  3438. array('label' => array('for' => 'ModelFieldAB2')),
  3439. 'third',
  3440. '/label',
  3441. '/fieldset'
  3442. );
  3443. $this->assertTags($result, $expected);
  3444. }
  3445. /**
  3446. * Test radio inputs with between as string or array. Also ensure
  3447. * that an array with less between elements works.
  3448. *
  3449. * @return void
  3450. */
  3451. public function testRadioBetween() {
  3452. $result = $this->Form->radio(
  3453. 'Model.field',
  3454. array('option A', 'option B'),
  3455. array('between' => 'I am between')
  3456. );
  3457. $expected = array(
  3458. 'fieldset' => array(),
  3459. 'legend' => array(),
  3460. 'Field',
  3461. '/legend',
  3462. 'I am between',
  3463. 'input' => array(
  3464. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3465. 'value' => '', 'id' => 'ModelField_'
  3466. ),
  3467. array('input' => array(
  3468. 'type' => 'radio', 'name' => 'data[Model][field]',
  3469. 'value' => '0', 'id' => 'ModelField0'
  3470. )),
  3471. array('label' => array('for' => 'ModelField0')),
  3472. 'option A',
  3473. '/label',
  3474. array('input' => array(
  3475. 'type' => 'radio', 'name' => 'data[Model][field]',
  3476. 'value' => '1', 'id' => 'ModelField1'
  3477. )),
  3478. array('label' => array('for' => 'ModelField1')),
  3479. 'option B',
  3480. '/label',
  3481. '/fieldset'
  3482. );
  3483. $this->assertTags($result, $expected);
  3484. $result = $this->Form->radio(
  3485. 'Model.field',
  3486. array('option A', 'option B', 'option C'),
  3487. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3488. );
  3489. $expected = array(
  3490. 'fieldset' => array(),
  3491. 'legend' => array(),
  3492. 'Field',
  3493. '/legend',
  3494. 'input' => array(
  3495. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3496. 'value' => '', 'id' => 'ModelField_'
  3497. ),
  3498. array('input' => array(
  3499. 'type' => 'radio', 'name' => 'data[Model][field]',
  3500. 'value' => '0', 'id' => 'ModelField0'
  3501. )),
  3502. array('label' => array('for' => 'ModelField0')),
  3503. 'option A',
  3504. '/label',
  3505. 'between A',
  3506. '--separator--',
  3507. array('input' => array(
  3508. 'type' => 'radio', 'name' => 'data[Model][field]',
  3509. 'value' => '1', 'id' => 'ModelField1'
  3510. )),
  3511. array('label' => array('for' => 'ModelField1')),
  3512. 'option B',
  3513. '/label',
  3514. 'between B',
  3515. '--separator--',
  3516. array('input' => array(
  3517. 'type' => 'radio', 'name' => 'data[Model][field]',
  3518. 'value' => '2', 'id' => 'ModelField2'
  3519. )),
  3520. array('label' => array('for' => 'ModelField2')),
  3521. 'option C',
  3522. '/label',
  3523. 'between C',
  3524. '/fieldset'
  3525. );
  3526. $this->assertTags($result, $expected);
  3527. $result = $this->Form->input('Model.field', array(
  3528. 'options' => array('1' => 'first', '2' => 'second'),
  3529. 'type' => 'radio',
  3530. 'before' => '--before--',
  3531. 'after' => '--after--',
  3532. 'separator' => '--separator--',
  3533. 'between' => array('--between first--', '--between second--')
  3534. ));
  3535. $expected = array(
  3536. 'div' => array('class' => 'input radio'),
  3537. '--before--',
  3538. 'fieldset' => array(),
  3539. 'legend' => array(),
  3540. 'Field',
  3541. '/legend',
  3542. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3543. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3544. array('label' => array('for' => 'ModelField1')),
  3545. 'first',
  3546. '/label',
  3547. '--between first--',
  3548. '--separator--',
  3549. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3550. array('label' => array('for' => 'ModelField2')),
  3551. 'second',
  3552. '/label',
  3553. '--between second--',
  3554. '/fieldset',
  3555. '--after--',
  3556. '/div'
  3557. );
  3558. $this->assertTags($result, $expected);
  3559. $result = $this->Form->input('Model.field', array(
  3560. 'options' => array('1' => 'first', '2' => 'second'),
  3561. 'type' => 'radio',
  3562. 'before' => '--before--',
  3563. 'after' => '--after--',
  3564. 'separator' => '--separator--',
  3565. 'between' => array('--between first--')
  3566. ));
  3567. $expected = array(
  3568. 'div' => array('class' => 'input radio'),
  3569. '--before--',
  3570. 'fieldset' => array(),
  3571. 'legend' => array(),
  3572. 'Field',
  3573. '/legend',
  3574. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3575. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3576. array('label' => array('for' => 'ModelField1')),
  3577. 'first',
  3578. '/label',
  3579. '--between first--',
  3580. '--separator--',
  3581. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3582. array('label' => array('for' => 'ModelField2')),
  3583. 'second',
  3584. '/label',
  3585. '/fieldset',
  3586. '--after--',
  3587. '/div'
  3588. );
  3589. $this->assertTags($result, $expected);
  3590. }
  3591. /**
  3592. * Test that radios with a 0 value are selected under the correct conditions.
  3593. * Also ensure that values that are booleanish are handled correctly.
  3594. *
  3595. * @return void
  3596. */
  3597. public function testRadioOptionWithBooleanishValues() {
  3598. $expected = array(
  3599. 'fieldset' => array(),
  3600. 'legend' => array(),
  3601. 'Field',
  3602. '/legend',
  3603. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3604. array('label' => array('for' => 'ModelField1')),
  3605. 'Yes',
  3606. '/label',
  3607. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3608. array('label' => array('for' => 'ModelField0')),
  3609. 'No',
  3610. '/label',
  3611. '/fieldset'
  3612. );
  3613. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3614. $this->assertTags($result, $expected);
  3615. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3616. $this->assertTags($result, $expected);
  3617. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3618. $this->assertTags($result, $expected);
  3619. $expected = array(
  3620. 'fieldset' => array(),
  3621. 'legend' => array(),
  3622. 'Field',
  3623. '/legend',
  3624. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3625. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3626. array('label' => array('for' => 'ModelField1')),
  3627. 'Yes',
  3628. '/label',
  3629. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3630. array('label' => array('for' => 'ModelField0')),
  3631. 'No',
  3632. '/label',
  3633. '/fieldset'
  3634. );
  3635. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3636. $this->assertTags($result, $expected);
  3637. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3638. $this->assertTags($result, $expected);
  3639. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3640. $this->assertTags($result, $expected);
  3641. $expected = array(
  3642. 'fieldset' => array(),
  3643. 'legend' => array(),
  3644. 'Field',
  3645. '/legend',
  3646. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3647. array('label' => array('for' => 'ModelField1')),
  3648. 'Yes',
  3649. '/label',
  3650. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3651. array('label' => array('for' => 'ModelField0')),
  3652. 'No',
  3653. '/label',
  3654. '/fieldset'
  3655. );
  3656. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3657. $this->assertTags($result, $expected);
  3658. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3659. $this->assertTags($result, $expected);
  3660. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3661. $this->assertTags($result, $expected);
  3662. }
  3663. /**
  3664. * test disabled radio options
  3665. *
  3666. * @return void
  3667. */
  3668. public function testRadioDisabled() {
  3669. $result = $this->Form->radio(
  3670. 'Model.field',
  3671. array('option A', 'option B'),
  3672. array('disabled' => array(0), 'value' => '0')
  3673. );
  3674. $expected = array(
  3675. 'fieldset' => array(),
  3676. 'legend' => array(),
  3677. 'Field',
  3678. '/legend',
  3679. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3680. array('label' => array('for' => 'ModelField0')),
  3681. 'option A',
  3682. '/label',
  3683. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3684. array('label' => array('for' => 'ModelField1')),
  3685. 'option B',
  3686. '/label',
  3687. '/fieldset'
  3688. );
  3689. $this->assertTags($result, $expected);
  3690. $result = $this->Form->radio(
  3691. 'Model.field',
  3692. array('option A', 'option B'),
  3693. array('disabled' => true, 'value' => '0')
  3694. );
  3695. $expected = array(
  3696. 'fieldset' => array(),
  3697. 'legend' => array(),
  3698. 'Field',
  3699. '/legend',
  3700. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3701. array('label' => array('for' => 'ModelField0')),
  3702. 'option A',
  3703. '/label',
  3704. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3705. array('label' => array('for' => 'ModelField1')),
  3706. 'option B',
  3707. '/label',
  3708. '/fieldset'
  3709. );
  3710. $this->assertTags($result, $expected);
  3711. $result = $this->Form->radio(
  3712. 'Model.field',
  3713. array('option A', 'option B'),
  3714. array('disabled' => 'disabled', 'value' => '0')
  3715. );
  3716. $expected = array(
  3717. 'fieldset' => array(),
  3718. 'legend' => array(),
  3719. 'Field',
  3720. '/legend',
  3721. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3722. array('label' => array('for' => 'ModelField0')),
  3723. 'option A',
  3724. '/label',
  3725. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3726. array('label' => array('for' => 'ModelField1')),
  3727. 'option B',
  3728. '/label',
  3729. '/fieldset'
  3730. );
  3731. $this->assertTags($result, $expected);
  3732. $result = $this->Form->input('Model.field', array(
  3733. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3734. 'type' => 'radio',
  3735. 'disabled' => array(2, '3x'),
  3736. ));
  3737. $expected = array(
  3738. 'div' => array('class' => 'input radio'),
  3739. 'fieldset' => array(),
  3740. 'legend' => array(),
  3741. 'Field',
  3742. '/legend',
  3743. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3744. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3745. array('label' => array('for' => 'ModelField1')),
  3746. 'first',
  3747. '/label',
  3748. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3749. array('label' => array('for' => 'ModelField2')),
  3750. 'second',
  3751. '/label',
  3752. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3753. array('label' => array('for' => 'ModelField2x')),
  3754. '2x',
  3755. '/label',
  3756. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3757. array('label' => array('for' => 'ModelField3')),
  3758. 'third',
  3759. '/label',
  3760. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3761. array('label' => array('for' => 'ModelField3x')),
  3762. '3x',
  3763. '/label',
  3764. '/fieldset',
  3765. '/div'
  3766. );
  3767. $this->assertTags($result, $expected);
  3768. $result = $this->Form->input('Model.field', array(
  3769. 'type' => 'radio',
  3770. 'options' => array(
  3771. 1 => 'A',
  3772. 2 => 'B',
  3773. 3 => 'C'
  3774. ),
  3775. 'disabled' => array(1)
  3776. ));
  3777. $expected = array(
  3778. 'div' => array('class' => 'input radio'),
  3779. 'fieldset' => array(),
  3780. 'legend' => array(),
  3781. 'Field',
  3782. '/legend',
  3783. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3784. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
  3785. array('label' => array('for' => 'ModelField1')),
  3786. 'A',
  3787. '/label',
  3788. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
  3789. array('label' => array('for' => 'ModelField2')),
  3790. 'B',
  3791. '/label',
  3792. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
  3793. array('label' => array('for' => 'ModelField3')),
  3794. 'C',
  3795. '/label',
  3796. '/fieldset',
  3797. '/div'
  3798. );
  3799. $this->assertTags($result, $expected);
  3800. }
  3801. /**
  3802. * test disabling the hidden input for radio buttons
  3803. *
  3804. * @return void
  3805. */
  3806. public function testRadioHiddenInputDisabling() {
  3807. $result = $this->Form->input('Model.1.field', array(
  3808. 'type' => 'radio',
  3809. 'options' => array('option A'),
  3810. 'hiddenField' => false
  3811. )
  3812. );
  3813. $expected = array(
  3814. 'div' => array('class' => 'input radio'),
  3815. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3816. 'label' => array('for' => 'Model1Field0'),
  3817. 'option A',
  3818. '/label',
  3819. '/div'
  3820. );
  3821. $this->assertTags($result, $expected);
  3822. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3823. $expected = array(
  3824. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3825. 'label' => array('for' => 'Model1Field0'),
  3826. 'option A',
  3827. '/label'
  3828. );
  3829. $this->assertTags($result, $expected);
  3830. }
  3831. /**
  3832. * test adding an empty option for radio buttons
  3833. *
  3834. * @return void
  3835. */
  3836. public function testRadioAddEmptyOption() {
  3837. $result = $this->Form->input('Model.1.field', array(
  3838. 'type' => 'radio',
  3839. 'options' => array('option A'),
  3840. 'empty' => true,
  3841. 'hiddenField' => false
  3842. ));
  3843. $expected = array(
  3844. 'div' => array('class' => 'input radio'),
  3845. 'fieldset' => array(),
  3846. 'legend' => array(),
  3847. 'Field',
  3848. '/legend',
  3849. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3850. array('label' => array('for' => 'Model1Field')),
  3851. __('empty'),
  3852. '/label',
  3853. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3854. array('label' => array('for' => 'Model1Field0')),
  3855. 'option A',
  3856. '/label',
  3857. '/fieldset',
  3858. '/div'
  3859. );
  3860. $this->assertTags($result, $expected);
  3861. $result = $this->Form->input('Model.1.field', array(
  3862. 'type' => 'radio',
  3863. 'options' => array('option A'),
  3864. 'empty' => 'CustomEmptyLabel',
  3865. 'hiddenField' => false
  3866. ));
  3867. $expected = array(
  3868. 'div' => array('class' => 'input radio'),
  3869. 'fieldset' => array(),
  3870. 'legend' => array(),
  3871. 'Field',
  3872. '/legend',
  3873. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3874. array('label' => array('for' => 'Model1Field')),
  3875. 'CustomEmptyLabel',
  3876. '/label',
  3877. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3878. array('label' => array('for' => 'Model1Field0')),
  3879. 'option A',
  3880. '/label',
  3881. '/fieldset',
  3882. '/div'
  3883. );
  3884. $this->assertTags($result, $expected);
  3885. $result = $this->Form->input('Model.1.field', array(
  3886. 'type' => 'radio',
  3887. 'options' => array('option A'),
  3888. 'empty' => false,
  3889. 'hiddenField' => false
  3890. ));
  3891. $this->assertTextNotContains('"Model1Field"', $result);
  3892. }
  3893. /**
  3894. * Test that radio() accepts an array for label
  3895. *
  3896. * @return void
  3897. */
  3898. public function testRadioLabelArray() {
  3899. $result = $this->Form->input('Model.field', array(
  3900. 'type' => 'radio',
  3901. 'legend' => false,
  3902. 'label' => array(
  3903. 'class' => 'checkbox float-left',
  3904. ),
  3905. 'options' => array('1' => 'Option A', '2' => 'Option B.')
  3906. ));
  3907. $this->assertTextContains(
  3908. '<label for="ModelField1" class="checkbox float-left">Option A</label>',
  3909. $result
  3910. );
  3911. }
  3912. /**
  3913. * Test that label id's match the input element id's when radio is called after create().
  3914. *
  3915. * @return void
  3916. */
  3917. public function testRadioWithCreate() {
  3918. $this->Form->create('Model');
  3919. $result = $this->Form->radio('recipient',
  3920. array('1' => '1', '2' => '2', '3' => '3'),
  3921. array('legend' => false, 'value' => '1')
  3922. );
  3923. $this->assertTextNotContains(
  3924. '<label for="ModelModelRecipient1">1</label>',
  3925. $result
  3926. );
  3927. $this->assertTextContains(
  3928. '<label for="ModelRecipient1">1</label>',
  3929. $result
  3930. );
  3931. }
  3932. /**
  3933. * testDomIdSuffix method
  3934. *
  3935. * @return void
  3936. */
  3937. public function testDomIdSuffix() {
  3938. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs');
  3939. $this->assertEquals('1StringWith1DollarSigns', $result);
  3940. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>');
  3941. $this->assertEquals('AbcXFooYBar', $result);
  3942. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'html5');
  3943. $this->assertEquals('1StringWith1$-dollarSigns', $result);
  3944. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>', 'html5');
  3945. $this->assertEquals('AbcX=FooY=Bar', $result);
  3946. }
  3947. /**
  3948. * testDomIdSuffixCollisionResolvement()
  3949. *
  3950. * @return void
  3951. */
  3952. public function testDomIdSuffixCollisionResolvement() {
  3953. $result = $this->Form->domIdSuffix('a>b');
  3954. $this->assertEquals('AB', $result);
  3955. $result = $this->Form->domIdSuffix('a<b');
  3956. $this->assertEquals('AB1', $result);
  3957. $result = $this->Form->domIdSuffix('a\'b');
  3958. $this->assertEquals('AB2', $result);
  3959. $result = $this->Form->domIdSuffix('1 string with 1$-dollar');
  3960. $this->assertEquals('1StringWith1Dollar', $result);
  3961. $result = $this->Form->domIdSuffix('1 string with 1$-dollar');
  3962. $this->assertEquals('1StringWith1Dollar1', $result);
  3963. $result = $this->Form->domIdSuffix('1 string with 1$-dollar');
  3964. $this->assertEquals('1StringWith1Dollar2', $result);
  3965. }
  3966. /**
  3967. * testSelect method
  3968. *
  3969. * Test select element generation.
  3970. *
  3971. * @return void
  3972. */
  3973. public function testSelect() {
  3974. $result = $this->Form->select('Model.field', array());
  3975. $expected = array(
  3976. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3977. array('option' => array('value' => '')),
  3978. '/option',
  3979. '/select'
  3980. );
  3981. $this->assertTags($result, $expected);
  3982. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3983. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3984. $expected = array(
  3985. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3986. array('option' => array('value' => '')),
  3987. '/option',
  3988. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3989. 'good',
  3990. '/option',
  3991. array('option' => array('value' => 'other')),
  3992. 'bad',
  3993. '/option',
  3994. '/select'
  3995. );
  3996. $this->assertTags($result, $expected);
  3997. $this->Form->request->data = array();
  3998. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3999. $expected = array(
  4000. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4001. array('option' => array('value' => '')),
  4002. '/option',
  4003. array('option' => array('value' => 'value')),
  4004. 'good',
  4005. '/option',
  4006. array('option' => array('value' => 'other')),
  4007. 'bad',
  4008. '/option',
  4009. '/select'
  4010. );
  4011. $this->assertTags($result, $expected);
  4012. $result = $this->Form->select(
  4013. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  4014. array('empty' => false)
  4015. );
  4016. $expected = array(
  4017. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4018. array('option' => array('value' => 'first')),
  4019. 'first &quot;html&quot; &lt;chars&gt;',
  4020. '/option',
  4021. array('option' => array('value' => 'second')),
  4022. 'value',
  4023. '/option',
  4024. '/select'
  4025. );
  4026. $this->assertTags($result, $expected);
  4027. $result = $this->Form->select(
  4028. 'Model.field',
  4029. array('first' => 'first "html" <chars>', 'second' => 'value'),
  4030. array('escape' => false, 'empty' => false)
  4031. );
  4032. $expected = array(
  4033. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4034. array('option' => array('value' => 'first')),
  4035. 'first "html" <chars>',
  4036. '/option',
  4037. array('option' => array('value' => 'second')),
  4038. 'value',
  4039. '/option',
  4040. '/select'
  4041. );
  4042. $this->assertTags($result, $expected);
  4043. $options = array(
  4044. array('value' => 'first', 'name' => 'First'),
  4045. array('value' => 'first', 'name' => 'Another First'),
  4046. );
  4047. $result = $this->Form->select(
  4048. 'Model.field',
  4049. $options,
  4050. array('escape' => false, 'empty' => false)
  4051. );
  4052. $expected = array(
  4053. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4054. array('option' => array('value' => 'first')),
  4055. 'First',
  4056. '/option',
  4057. array('option' => array('value' => 'first')),
  4058. 'Another First',
  4059. '/option',
  4060. '/select'
  4061. );
  4062. $this->assertTags($result, $expected);
  4063. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  4064. $result = $this->Form->select(
  4065. 'Model.contact_id',
  4066. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  4067. array('escape' => false, 'empty' => 'pick something')
  4068. );
  4069. $expected = array(
  4070. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  4071. array('option' => array('value' => '')), 'pick something', '/option',
  4072. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  4073. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  4074. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  4075. '/select'
  4076. );
  4077. $this->assertTags($result, $expected);
  4078. $this->Form->request->data['Model']['field'] = 0;
  4079. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  4080. $expected = array(
  4081. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4082. array('option' => array('value' => '')), '/option',
  4083. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  4084. array('option' => array('value' => '1')), 'Yes', '/option',
  4085. '/select'
  4086. );
  4087. $this->assertTags($result, $expected);
  4088. $this->Form->request->data['Model']['field'] = 50;
  4089. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  4090. $expected = array(
  4091. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4092. array('option' => array('value' => '')), '/option',
  4093. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  4094. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  4095. '/select'
  4096. );
  4097. $this->assertTags($result, $expected);
  4098. $result = $this->Form->select('Contact.required_one', array('option A'));
  4099. $expected = array(
  4100. 'select' => array(
  4101. 'name' => 'data[Contact][required_one]',
  4102. 'id' => 'ContactRequiredOne',
  4103. 'required' => 'required'
  4104. ),
  4105. array('option' => array('value' => '')), '/option',
  4106. array('option' => array('value' => '0')), 'option A', '/option',
  4107. '/select'
  4108. );
  4109. $this->assertTags($result, $expected);
  4110. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  4111. $expected = array(
  4112. 'select' => array(
  4113. 'name' => 'data[Contact][required_one]',
  4114. 'id' => 'ContactRequiredOne',
  4115. 'disabled' => 'disabled'
  4116. ),
  4117. array('option' => array('value' => '')), '/option',
  4118. array('option' => array('value' => '0')), 'option A', '/option',
  4119. '/select'
  4120. );
  4121. $this->assertTags($result, $expected);
  4122. }
  4123. /**
  4124. * test that select() with optiongroups listens to the escape param.
  4125. *
  4126. * @return void
  4127. */
  4128. public function testSelectOptionGroupEscaping() {
  4129. $options = array(
  4130. '>< Key' => array(
  4131. 1 => 'One',
  4132. 2 => 'Two'
  4133. )
  4134. );
  4135. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  4136. $expected = array(
  4137. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4138. 'optgroup' => array('label' => '&gt;&lt; Key'),
  4139. array('option' => array('value' => '1')), 'One', '/option',
  4140. array('option' => array('value' => '2')), 'Two', '/option',
  4141. '/optgroup',
  4142. '/select'
  4143. );
  4144. $this->assertTags($result, $expected);
  4145. $options = array(
  4146. '>< Key' => array(
  4147. 1 => 'One',
  4148. 2 => 'Two'
  4149. )
  4150. );
  4151. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  4152. $expected = array(
  4153. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4154. 'optgroup' => array('label' => '>< Key'),
  4155. array('option' => array('value' => '1')), 'One', '/option',
  4156. array('option' => array('value' => '2')), 'Two', '/option',
  4157. '/optgroup',
  4158. '/select'
  4159. );
  4160. $this->assertTags($result, $expected);
  4161. }
  4162. /**
  4163. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  4164. *
  4165. * @return void
  4166. */
  4167. public function testSelectWithNullAttributes() {
  4168. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  4169. $expected = array(
  4170. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4171. array('option' => array('value' => '0')),
  4172. 'first',
  4173. '/option',
  4174. array('option' => array('value' => '1')),
  4175. 'second',
  4176. '/option',
  4177. '/select'
  4178. );
  4179. $this->assertTags($result, $expected);
  4180. }
  4181. /**
  4182. * testNestedSelect method
  4183. *
  4184. * test select element generation with optgroups
  4185. *
  4186. * @return void
  4187. */
  4188. public function testNestedSelect() {
  4189. $result = $this->Form->select(
  4190. 'Model.field',
  4191. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4192. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4193. )), array('empty' => false)
  4194. );
  4195. $expected = array(
  4196. 'select' => array('name' => 'data[Model][field]',
  4197. 'id' => 'ModelField'),
  4198. array('option' => array('value' => 1)),
  4199. 'One',
  4200. '/option',
  4201. array('option' => array('value' => 2)),
  4202. 'Two',
  4203. '/option',
  4204. array('optgroup' => array('label' => 'Three')),
  4205. array('option' => array('value' => 4)),
  4206. 'Four',
  4207. '/option',
  4208. array('option' => array('value' => 5)),
  4209. 'Five',
  4210. '/option',
  4211. '/optgroup',
  4212. '/select'
  4213. );
  4214. $this->assertTags($result, $expected);
  4215. $result = $this->Form->select(
  4216. 'Model.field',
  4217. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  4218. array('showParents' => true, 'empty' => false)
  4219. );
  4220. $expected = array(
  4221. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4222. array('option' => array('value' => 1)),
  4223. 'One',
  4224. '/option',
  4225. array('option' => array('value' => 2)),
  4226. 'Two',
  4227. '/option',
  4228. array('optgroup' => array('label' => 'Three')),
  4229. array('option' => array('value' => 3)),
  4230. 'Three',
  4231. '/option',
  4232. array('option' => array('value' => 4)),
  4233. 'Four',
  4234. '/option',
  4235. '/optgroup',
  4236. '/select'
  4237. );
  4238. $this->assertTags($result, $expected);
  4239. }
  4240. /**
  4241. * testSelectMultiple method
  4242. *
  4243. * test generation of multiple select elements
  4244. *
  4245. * @return void
  4246. */
  4247. public function testSelectMultiple() {
  4248. $options = array('first', 'second', 'third');
  4249. $result = $this->Form->select(
  4250. 'Model.multi_field', $options, array('multiple' => true)
  4251. );
  4252. $expected = array(
  4253. 'input' => array(
  4254. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4255. ),
  4256. 'select' => array(
  4257. 'name' => 'data[Model][multi_field][]',
  4258. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4259. ),
  4260. array('option' => array('value' => '0')),
  4261. 'first',
  4262. '/option',
  4263. array('option' => array('value' => '1')),
  4264. 'second',
  4265. '/option',
  4266. array('option' => array('value' => '2')),
  4267. 'third',
  4268. '/option',
  4269. '/select'
  4270. );
  4271. $this->assertTags($result, $expected);
  4272. $result = $this->Form->select(
  4273. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4274. );
  4275. $expected = array(
  4276. 'input' => array(
  4277. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4278. ),
  4279. 'select' => array(
  4280. 'name' => 'data[Model][multi_field][]',
  4281. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4282. ),
  4283. array('option' => array('value' => '0')),
  4284. 'first',
  4285. '/option',
  4286. array('option' => array('value' => '1')),
  4287. 'second',
  4288. '/option',
  4289. array('option' => array('value' => '2')),
  4290. 'third',
  4291. '/option',
  4292. '/select'
  4293. );
  4294. $this->assertTags($result, $expected);
  4295. $result = $this->Form->select(
  4296. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4297. );
  4298. $expected = array(
  4299. 'input' => array(
  4300. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4301. ),
  4302. 'select' => array(
  4303. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4304. 'multiple' => 'multiple'
  4305. ),
  4306. array('option' => array('value' => '0', 'selected' => 'selected')),
  4307. 'first',
  4308. '/option',
  4309. array('option' => array('value' => '1', 'selected' => 'selected')),
  4310. 'second',
  4311. '/option',
  4312. array('option' => array('value' => '2')),
  4313. 'third',
  4314. '/option',
  4315. '/select'
  4316. );
  4317. $this->assertTags($result, $expected);
  4318. $result = $this->Form->select(
  4319. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4320. );
  4321. $expected = array(
  4322. 'select' => array(
  4323. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4324. ),
  4325. array('option' => array('value' => '0', 'selected' => 'selected')),
  4326. 'first',
  4327. '/option',
  4328. array('option' => array('value' => '1', 'selected' => 'selected')),
  4329. 'second',
  4330. '/option',
  4331. array('option' => array('value' => '2')),
  4332. 'third',
  4333. '/option',
  4334. '/select'
  4335. );
  4336. $this->assertTags($result, $expected);
  4337. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4338. $selected = array('2', '3x');
  4339. $result = $this->Form->select(
  4340. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4341. );
  4342. $expected = array(
  4343. 'input' => array(
  4344. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4345. ),
  4346. 'select' => array(
  4347. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4348. ),
  4349. array('option' => array('value' => '1')),
  4350. 'One',
  4351. '/option',
  4352. array('option' => array('value' => '2', 'selected' => 'selected')),
  4353. 'Two',
  4354. '/option',
  4355. array('option' => array('value' => '3')),
  4356. 'Three',
  4357. '/option',
  4358. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4359. 'Stringy',
  4360. '/option',
  4361. '/select'
  4362. );
  4363. $this->assertTags($result, $expected);
  4364. $result = $this->Form->select('Contact.required_one', array(
  4365. '1' => 'option A',
  4366. '2' => 'option B'
  4367. ), array('multiple' => true));
  4368. $expected = array(
  4369. 'input' => array(
  4370. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4371. ),
  4372. 'select' => array(
  4373. 'name' => 'data[Contact][required_one][]',
  4374. 'id' => 'ContactRequiredOne',
  4375. 'required' => 'required',
  4376. 'multiple' => 'multiple'
  4377. ),
  4378. array('option' => array('value' => '1')), 'option A', '/option',
  4379. array('option' => array('value' => '2')), 'option B', '/option',
  4380. '/select'
  4381. );
  4382. $this->assertTags($result, $expected);
  4383. $result = $this->Form->select(
  4384. 'Model.multi_field',
  4385. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4386. array('multiple' => true)
  4387. );
  4388. $expected = array(
  4389. 'input' => array(
  4390. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '',
  4391. 'id' => 'ModelMultiField_'
  4392. ),
  4393. array('select' => array('name' => 'data[Model][multi_field][]',
  4394. 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4395. )),
  4396. array('option' => array('value' => 'a&gt;b')),
  4397. 'first',
  4398. '/option',
  4399. array('option' => array('value' => 'a&lt;b')),
  4400. 'second',
  4401. '/option',
  4402. array('option' => array(
  4403. 'value' => 'a&quot;b'
  4404. )),
  4405. 'third',
  4406. '/option',
  4407. '/select'
  4408. );
  4409. $this->assertTags($result, $expected);
  4410. }
  4411. /**
  4412. * Test generating multiple select with disabled elements.
  4413. *
  4414. * @return void
  4415. */
  4416. public function testSelectMultipleWithDisabledElements() {
  4417. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4418. $disabled = array(2, 3);
  4419. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4420. $expected = array(
  4421. 'input' => array(
  4422. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4423. ),
  4424. 'select' => array(
  4425. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4426. ),
  4427. array('option' => array('value' => '1')),
  4428. 'One',
  4429. '/option',
  4430. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4431. 'Two',
  4432. '/option',
  4433. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4434. 'Three',
  4435. '/option',
  4436. array('option' => array('value' => '3x')),
  4437. 'Stringy',
  4438. '/option',
  4439. '/select'
  4440. );
  4441. $this->assertTags($result, $expected);
  4442. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4443. $disabled = array('2', '3x');
  4444. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4445. $expected = array(
  4446. array('div' => array('class' => 'input select')),
  4447. array('label' => array('for' => 'ContactMultiple')),
  4448. 'Multiple',
  4449. '/label',
  4450. 'input' => array(
  4451. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4452. ),
  4453. 'select' => array(
  4454. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4455. ),
  4456. array('option' => array('value' => '1')),
  4457. 'One',
  4458. '/option',
  4459. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4460. 'Two',
  4461. '/option',
  4462. array('option' => array('value' => '3')),
  4463. 'Three',
  4464. '/option',
  4465. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4466. 'Stringy',
  4467. '/option',
  4468. '/select',
  4469. '/div'
  4470. );
  4471. $this->assertTags($result, $expected);
  4472. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4473. $disabled = true;
  4474. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4475. $expected = array(
  4476. array('div' => array('class' => 'input select')),
  4477. array('label' => array('for' => 'ContactMultiple')),
  4478. 'Multiple',
  4479. '/label',
  4480. 'input' => array(
  4481. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4482. ),
  4483. 'select' => array(
  4484. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4485. ),
  4486. array('option' => array('value' => '1')),
  4487. 'One',
  4488. '/option',
  4489. array('option' => array('value' => '2')),
  4490. 'Two',
  4491. '/option',
  4492. array('option' => array('value' => '3')),
  4493. 'Three',
  4494. '/option',
  4495. array('option' => array('value' => '3x')),
  4496. 'Stringy',
  4497. '/option',
  4498. '/select',
  4499. '/div'
  4500. );
  4501. $this->assertTags($result, $expected);
  4502. }
  4503. /**
  4504. * Test generating select with disabled elements.
  4505. *
  4506. * @return void
  4507. */
  4508. public function testSelectWithDisabledElements() {
  4509. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4510. $disabled = array(2, 3);
  4511. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4512. $expected = array(
  4513. 'select' => array(
  4514. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4515. ),
  4516. array('option' => array('value' => '')),
  4517. '/option',
  4518. array('option' => array('value' => '1')),
  4519. 'One',
  4520. '/option',
  4521. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4522. 'Two',
  4523. '/option',
  4524. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4525. 'Three',
  4526. '/option',
  4527. array('option' => array('value' => '3x')),
  4528. 'Stringy',
  4529. '/option',
  4530. '/select'
  4531. );
  4532. $this->assertTags($result, $expected);
  4533. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4534. $disabled = array('2', '3x');
  4535. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4536. $expected = array(
  4537. array('div' => array('class' => 'input select')),
  4538. array('label' => array('for' => 'ModelField')),
  4539. 'Field',
  4540. '/label',
  4541. 'select' => array(
  4542. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4543. ),
  4544. array('option' => array('value' => '1')),
  4545. 'One',
  4546. '/option',
  4547. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4548. 'Two',
  4549. '/option',
  4550. array('option' => array('value' => '3')),
  4551. 'Three',
  4552. '/option',
  4553. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4554. 'Stringy',
  4555. '/option',
  4556. '/select',
  4557. '/div'
  4558. );
  4559. $this->assertTags($result, $expected);
  4560. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4561. $disabled = true;
  4562. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4563. $expected = array(
  4564. array('div' => array('class' => 'input select')),
  4565. array('label' => array('for' => 'ModelField')),
  4566. 'Field',
  4567. '/label',
  4568. 'select' => array(
  4569. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4570. ),
  4571. array('option' => array('value' => '1')),
  4572. 'One',
  4573. '/option',
  4574. array('option' => array('value' => '2')),
  4575. 'Two',
  4576. '/option',
  4577. array('option' => array('value' => '3')),
  4578. 'Three',
  4579. '/option',
  4580. array('option' => array('value' => '3x')),
  4581. 'Stringy',
  4582. '/option',
  4583. '/select',
  4584. '/div'
  4585. );
  4586. $this->assertTags($result, $expected);
  4587. }
  4588. /**
  4589. * test generation of habtm select boxes.
  4590. *
  4591. * @return void
  4592. */
  4593. public function testHabtmSelectBox() {
  4594. $this->View->viewVars['contactTags'] = array(
  4595. 1 => 'blue',
  4596. 2 => 'red',
  4597. 3 => 'green'
  4598. );
  4599. $this->Form->request->data = array(
  4600. 'Contact' => array(),
  4601. 'ContactTag' => array(
  4602. array(
  4603. 'id' => '1',
  4604. 'name' => 'blue'
  4605. ),
  4606. array(
  4607. 'id' => 3,
  4608. 'name' => 'green'
  4609. )
  4610. )
  4611. );
  4612. $this->Form->create('Contact');
  4613. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4614. $expected = array(
  4615. 'input' => array(
  4616. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4617. ),
  4618. 'select' => array(
  4619. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4620. 'multiple' => 'multiple'
  4621. ),
  4622. array('option' => array('value' => '1', 'selected' => 'selected')),
  4623. 'blue',
  4624. '/option',
  4625. array('option' => array('value' => '2')),
  4626. 'red',
  4627. '/option',
  4628. array('option' => array('value' => '3', 'selected' => 'selected')),
  4629. 'green',
  4630. '/option',
  4631. '/select'
  4632. );
  4633. $this->assertTags($result, $expected);
  4634. // make sure only 50 is selected, and not 50f5c0cf
  4635. $this->View->viewVars['contactTags'] = array(
  4636. '1' => 'blue',
  4637. '50f5c0cf' => 'red',
  4638. '50' => 'green'
  4639. );
  4640. $this->Form->request->data = array(
  4641. 'Contact' => array(),
  4642. 'ContactTag' => array(
  4643. array(
  4644. 'id' => 1,
  4645. 'name' => 'blue'
  4646. ),
  4647. array(
  4648. 'id' => 50,
  4649. 'name' => 'green'
  4650. )
  4651. )
  4652. );
  4653. $this->Form->create('Contact');
  4654. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4655. $expected = array(
  4656. 'input' => array(
  4657. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4658. ),
  4659. 'select' => array(
  4660. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4661. 'multiple' => 'multiple'
  4662. ),
  4663. array('option' => array('value' => '1', 'selected' => 'selected')),
  4664. 'blue',
  4665. '/option',
  4666. array('option' => array('value' => '50f5c0cf')),
  4667. 'red',
  4668. '/option',
  4669. array('option' => array('value' => '50', 'selected' => 'selected')),
  4670. 'green',
  4671. '/option',
  4672. '/select'
  4673. );
  4674. $this->assertTags($result, $expected);
  4675. }
  4676. /**
  4677. * test generation of multi select elements in checkbox format
  4678. *
  4679. * @return void
  4680. */
  4681. public function testSelectMultipleCheckboxes() {
  4682. $result = $this->Form->select(
  4683. 'Model.multi_field',
  4684. array('first', 'second', 'third'),
  4685. array('multiple' => 'checkbox')
  4686. );
  4687. $expected = array(
  4688. 'input' => array(
  4689. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4690. ),
  4691. array('div' => array('class' => 'checkbox')),
  4692. array('input' => array(
  4693. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4694. 'value' => '0', 'id' => 'ModelMultiField0'
  4695. )),
  4696. array('label' => array('for' => 'ModelMultiField0')),
  4697. 'first',
  4698. '/label',
  4699. '/div',
  4700. array('div' => array('class' => 'checkbox')),
  4701. array('input' => array(
  4702. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4703. 'value' => '1', 'id' => 'ModelMultiField1'
  4704. )),
  4705. array('label' => array('for' => 'ModelMultiField1')),
  4706. 'second',
  4707. '/label',
  4708. '/div',
  4709. array('div' => array('class' => 'checkbox')),
  4710. array('input' => array(
  4711. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4712. 'value' => '2', 'id' => 'ModelMultiField2'
  4713. )),
  4714. array('label' => array('for' => 'ModelMultiField2')),
  4715. 'third',
  4716. '/label',
  4717. '/div'
  4718. );
  4719. $this->assertTags($result, $expected);
  4720. $result = $this->Form->select(
  4721. 'Model.multi_field',
  4722. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4723. array('multiple' => 'checkbox')
  4724. );
  4725. $expected = array(
  4726. 'input' => array(
  4727. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4728. ),
  4729. array('div' => array('class' => 'checkbox')),
  4730. array('input' => array(
  4731. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4732. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4733. )),
  4734. array('label' => array('for' => 'ModelMultiFieldA')),
  4735. 'first',
  4736. '/label',
  4737. '/div',
  4738. array('div' => array('class' => 'checkbox')),
  4739. array('input' => array(
  4740. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4741. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4742. )),
  4743. array('label' => array('for' => 'ModelMultiFieldB')),
  4744. 'second',
  4745. '/label',
  4746. '/div',
  4747. array('div' => array('class' => 'checkbox')),
  4748. array('input' => array(
  4749. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4750. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4751. )),
  4752. array('label' => array('for' => 'ModelMultiFieldC')),
  4753. 'third',
  4754. '/label',
  4755. '/div'
  4756. );
  4757. $this->assertTags($result, $expected);
  4758. $result = $this->Form->select(
  4759. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4760. );
  4761. $expected = array(
  4762. 'input' => array(
  4763. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4764. ),
  4765. array('div' => array('class' => 'checkbox')),
  4766. array('input' => array(
  4767. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4768. 'value' => '1', 'id' => 'ModelMultiField1'
  4769. )),
  4770. array('label' => array('for' => 'ModelMultiField1')),
  4771. 'first',
  4772. '/label',
  4773. '/div'
  4774. );
  4775. $this->assertTags($result, $expected);
  4776. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4777. $result = $this->Form->select(
  4778. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4779. );
  4780. $expected = array(
  4781. 'input' => array(
  4782. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4783. ),
  4784. array('div' => array('class' => 'checkbox')),
  4785. array('input' => array(
  4786. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4787. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4788. )),
  4789. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4790. 'first',
  4791. '/label',
  4792. '/div',
  4793. array('div' => array('class' => 'checkbox')),
  4794. array('input' => array(
  4795. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4796. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4797. )),
  4798. array('label' => array('for' => 'ModelTagsArray')),
  4799. 'Array',
  4800. '/label',
  4801. '/div'
  4802. );
  4803. $this->assertTags($result, $expected);
  4804. $result = $this->Form->select(
  4805. 'Model.multi_field',
  4806. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  4807. array('multiple' => 'checkbox')
  4808. );
  4809. $expected = array(
  4810. 'input' => array(
  4811. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4812. ),
  4813. array('div' => array('class' => 'checkbox')),
  4814. array('input' => array(
  4815. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4816. 'value' => 'a+', 'id' => 'ModelMultiFieldA2'
  4817. )),
  4818. array('label' => array('for' => 'ModelMultiFieldA2')),
  4819. 'first',
  4820. '/label',
  4821. '/div',
  4822. array('div' => array('class' => 'checkbox')),
  4823. array('input' => array(
  4824. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4825. 'value' => 'a++', 'id' => 'ModelMultiFieldA1'
  4826. )),
  4827. array('label' => array('for' => 'ModelMultiFieldA1')),
  4828. 'second',
  4829. '/label',
  4830. '/div',
  4831. array('div' => array('class' => 'checkbox')),
  4832. array('input' => array(
  4833. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4834. 'value' => 'a+++', 'id' => 'ModelMultiFieldA'
  4835. )),
  4836. array('label' => array('for' => 'ModelMultiFieldA')),
  4837. 'third',
  4838. '/label',
  4839. '/div'
  4840. );
  4841. $this->assertTags($result, $expected);
  4842. $result = $this->Form->select(
  4843. 'Model.multi_field',
  4844. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4845. array('multiple' => 'checkbox')
  4846. );
  4847. $expected = array(
  4848. 'input' => array(
  4849. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4850. ),
  4851. array('div' => array('class' => 'checkbox')),
  4852. array('input' => array(
  4853. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4854. 'value' => 'a&gt;b', 'id' => 'ModelMultiFieldAB2'
  4855. )),
  4856. array('label' => array('for' => 'ModelMultiFieldAB2')),
  4857. 'first',
  4858. '/label',
  4859. '/div',
  4860. array('div' => array('class' => 'checkbox')),
  4861. array('input' => array(
  4862. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4863. 'value' => 'a&lt;b', 'id' => 'ModelMultiFieldAB1'
  4864. )),
  4865. array('label' => array('for' => 'ModelMultiFieldAB1')),
  4866. 'second',
  4867. '/label',
  4868. '/div',
  4869. array('div' => array('class' => 'checkbox')),
  4870. array('input' => array(
  4871. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4872. 'value' => 'a&quot;b', 'id' => 'ModelMultiFieldAB'
  4873. )),
  4874. array('label' => array('for' => 'ModelMultiFieldAB')),
  4875. 'third',
  4876. '/label',
  4877. '/div'
  4878. );
  4879. $this->assertTags($result, $expected);
  4880. }
  4881. /**
  4882. * test multiple checkboxes with div styles.
  4883. *
  4884. * @return void
  4885. */
  4886. public function testSelectMultipleCheckboxDiv() {
  4887. $result = $this->Form->select(
  4888. 'Model.tags',
  4889. array('first', 'second'),
  4890. array('multiple' => 'checkbox', 'class' => 'my-class')
  4891. );
  4892. $expected = array(
  4893. 'input' => array(
  4894. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4895. ),
  4896. array('div' => array('class' => 'my-class')),
  4897. array('input' => array(
  4898. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4899. 'value' => '0', 'id' => 'ModelTags0'
  4900. )),
  4901. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4902. '/div',
  4903. array('div' => array('class' => 'my-class')),
  4904. array('input' => array(
  4905. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4906. 'value' => '1', 'id' => 'ModelTags1'
  4907. )),
  4908. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4909. '/div'
  4910. );
  4911. $this->assertTags($result, $expected);
  4912. $result = $this->Form->input('Model.tags', array(
  4913. 'options' => array('first', 'second'),
  4914. 'multiple' => 'checkbox',
  4915. 'class' => 'my-class',
  4916. 'div' => false,
  4917. 'label' => false
  4918. ));
  4919. $this->assertTags($result, $expected);
  4920. $Contact = ClassRegistry::getObject('Contact');
  4921. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4922. $result = $this->Form->input('Contact.tags', array(
  4923. 'options' => array('one'),
  4924. 'multiple' => 'checkbox',
  4925. 'label' => false,
  4926. 'div' => false
  4927. ));
  4928. $expected = array(
  4929. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4930. array('div' => array('class' => 'checkbox form-error')),
  4931. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4932. array('label' => array('for' => 'ContactTags0')),
  4933. 'one',
  4934. '/label',
  4935. '/div'
  4936. );
  4937. $this->assertTags($result, $expected);
  4938. $result = $this->Form->input('Contact.tags', array(
  4939. 'options' => array('one'),
  4940. 'multiple' => 'checkbox',
  4941. 'class' => 'mycheckbox',
  4942. 'label' => false,
  4943. 'div' => false
  4944. ));
  4945. $expected = array(
  4946. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4947. array('div' => array('class' => 'mycheckbox form-error')),
  4948. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4949. array('label' => array('for' => 'ContactTags0')),
  4950. 'one',
  4951. '/label',
  4952. '/div'
  4953. );
  4954. $this->assertTags($result, $expected);
  4955. }
  4956. /**
  4957. * Checks the security hash array generated for multiple-input checkbox elements
  4958. *
  4959. * @return void
  4960. */
  4961. public function testSelectMultipleCheckboxSecurity() {
  4962. $this->Form->request['_Token'] = array('key' => 'testKey');
  4963. $this->assertEquals(array(), $this->Form->fields);
  4964. $result = $this->Form->select(
  4965. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4966. array('multiple' => 'checkbox')
  4967. );
  4968. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4969. $result = $this->Form->secure($this->Form->fields);
  4970. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4971. $this->assertRegExp('/"' . $key . '"/', $result);
  4972. }
  4973. /**
  4974. * Multiple select elements should always be secured as they always participate
  4975. * in the POST data.
  4976. *
  4977. * @return void
  4978. */
  4979. public function testSelectMultipleSecureWithNoOptions() {
  4980. $this->Form->request['_Token'] = array('key' => 'testkey');
  4981. $this->assertEquals(array(), $this->Form->fields);
  4982. $this->Form->select(
  4983. 'Model.select',
  4984. array(),
  4985. array('multiple' => true)
  4986. );
  4987. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4988. }
  4989. /**
  4990. * When a select box has no options it should not be added to the fields list
  4991. * as it always fail post validation.
  4992. *
  4993. * @return void
  4994. */
  4995. public function testSelectNoSecureWithNoOptions() {
  4996. $this->Form->request['_Token'] = array('key' => 'testkey');
  4997. $this->assertEquals(array(), $this->Form->fields);
  4998. $this->Form->select(
  4999. 'Model.select',
  5000. array()
  5001. );
  5002. $this->assertEquals(array(), $this->Form->fields);
  5003. $this->Form->select(
  5004. 'Model.select',
  5005. array(),
  5006. array('empty' => true)
  5007. );
  5008. $this->assertEquals(array('Model.select'), $this->Form->fields);
  5009. }
  5010. /**
  5011. * testInputMultipleCheckboxes method
  5012. *
  5013. * test input() resulting in multi select elements being generated.
  5014. *
  5015. * @return void
  5016. */
  5017. public function testInputMultipleCheckboxes() {
  5018. $result = $this->Form->input('Model.multi_field', array(
  5019. 'options' => array('first', 'second', 'third'),
  5020. 'multiple' => 'checkbox'
  5021. ));
  5022. $expected = array(
  5023. array('div' => array('class' => 'input select')),
  5024. array('label' => array('for' => 'ModelMultiField')),
  5025. 'Multi Field',
  5026. '/label',
  5027. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5028. array('div' => array('class' => 'checkbox')),
  5029. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5030. array('label' => array('for' => 'ModelMultiField0')),
  5031. 'first',
  5032. '/label',
  5033. '/div',
  5034. array('div' => array('class' => 'checkbox')),
  5035. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5036. array('label' => array('for' => 'ModelMultiField1')),
  5037. 'second',
  5038. '/label',
  5039. '/div',
  5040. array('div' => array('class' => 'checkbox')),
  5041. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  5042. array('label' => array('for' => 'ModelMultiField2')),
  5043. 'third',
  5044. '/label',
  5045. '/div',
  5046. '/div'
  5047. );
  5048. $this->assertTags($result, $expected);
  5049. $result = $this->Form->input('Model.multi_field', array(
  5050. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  5051. 'multiple' => 'checkbox'
  5052. ));
  5053. $expected = array(
  5054. array('div' => array('class' => 'input select')),
  5055. array('label' => array('for' => 'ModelMultiField')),
  5056. 'Multi Field',
  5057. '/label',
  5058. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5059. array('div' => array('class' => 'checkbox')),
  5060. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  5061. array('label' => array('for' => 'ModelMultiFieldA')),
  5062. 'first',
  5063. '/label',
  5064. '/div',
  5065. array('div' => array('class' => 'checkbox')),
  5066. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  5067. array('label' => array('for' => 'ModelMultiFieldB')),
  5068. 'second',
  5069. '/label',
  5070. '/div',
  5071. array('div' => array('class' => 'checkbox')),
  5072. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  5073. array('label' => array('for' => 'ModelMultiFieldC')),
  5074. 'third',
  5075. '/label',
  5076. '/div',
  5077. '/div'
  5078. );
  5079. $this->assertTags($result, $expected);
  5080. $result = $this->Form->input('Model.multi_field', array(
  5081. 'options' => array('1' => 'first'),
  5082. 'multiple' => 'checkbox',
  5083. 'label' => false,
  5084. 'div' => false
  5085. ));
  5086. $expected = array(
  5087. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5088. array('div' => array('class' => 'checkbox')),
  5089. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5090. array('label' => array('for' => 'ModelMultiField1')),
  5091. 'first',
  5092. '/label',
  5093. '/div'
  5094. );
  5095. $this->assertTags($result, $expected);
  5096. $result = $this->Form->input('Model.multi_field', array(
  5097. 'options' => array('2' => 'second'),
  5098. 'multiple' => 'checkbox',
  5099. 'label' => false,
  5100. 'div' => false
  5101. ));
  5102. $expected = array(
  5103. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5104. array('div' => array('class' => 'checkbox')),
  5105. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  5106. array('label' => array('for' => 'ModelMultiField2')),
  5107. 'second',
  5108. '/label',
  5109. '/div'
  5110. );
  5111. $this->assertTags($result, $expected);
  5112. }
  5113. /**
  5114. * testSelectHiddenFieldOmission method
  5115. *
  5116. * test that select() with 'hiddenField' => false omits the hidden field
  5117. *
  5118. * @return void
  5119. */
  5120. public function testSelectHiddenFieldOmission() {
  5121. $result = $this->Form->select('Model.multi_field',
  5122. array('first', 'second'),
  5123. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  5124. );
  5125. $expected = array(
  5126. array('div' => array('class' => 'checkbox')),
  5127. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5128. array('label' => array('for' => 'ModelMultiField0')),
  5129. 'first',
  5130. '/label',
  5131. '/div',
  5132. array('div' => array('class' => 'checkbox')),
  5133. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5134. array('label' => array('for' => 'ModelMultiField1')),
  5135. 'second',
  5136. '/label',
  5137. '/div'
  5138. );
  5139. $this->assertTags($result, $expected);
  5140. $result = $this->Form->input('Model.multi_field', array(
  5141. 'options' => array('first', 'second'),
  5142. 'multiple' => 'checkbox',
  5143. 'hiddenField' => false
  5144. ));
  5145. $expected = array(
  5146. array('div' => array('class' => 'input select')),
  5147. array('label' => array('for' => 'ModelMultiField')),
  5148. 'Multi Field',
  5149. '/label',
  5150. array('div' => array('class' => 'checkbox')),
  5151. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5152. array('label' => array('for' => 'ModelMultiField0')),
  5153. 'first',
  5154. '/label',
  5155. '/div',
  5156. array('div' => array('class' => 'checkbox')),
  5157. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5158. array('label' => array('for' => 'ModelMultiField1')),
  5159. 'second',
  5160. '/label',
  5161. '/div',
  5162. '/div'
  5163. );
  5164. $this->assertTags($result, $expected);
  5165. }
  5166. /**
  5167. * test that select() with multiple = checkbox works with overriding name attribute.
  5168. *
  5169. * @return void
  5170. */
  5171. public function testSelectCheckboxMultipleOverrideName() {
  5172. $result = $this->Form->input('category', array(
  5173. 'type' => 'select',
  5174. 'multiple' => 'checkbox',
  5175. 'name' => 'data[fish]',
  5176. 'options' => array('1', '2'),
  5177. 'div' => false,
  5178. 'label' => false,
  5179. ));
  5180. $expected = array(
  5181. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  5182. array('div' => array('class' => 'checkbox')),
  5183. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  5184. array('label' => array('for' => 'Category0')), '1', '/label',
  5185. '/div',
  5186. array('div' => array('class' => 'checkbox')),
  5187. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  5188. array('label' => array('for' => 'Category1')), '2', '/label',
  5189. '/div'
  5190. );
  5191. $this->assertTags($result, $expected);
  5192. }
  5193. /**
  5194. * Test that 'id' overrides all the checkbox id's as well.
  5195. *
  5196. * @return void
  5197. */
  5198. public function testSelectCheckboxMultipleId() {
  5199. $result = $this->Form->select(
  5200. 'Model.multi_field',
  5201. array('first', 'second', 'third'),
  5202. array('multiple' => 'checkbox', 'id' => 'CustomId')
  5203. );
  5204. $expected = array(
  5205. 'input' => array(
  5206. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  5207. ),
  5208. array('div' => array('class' => 'checkbox')),
  5209. array('input' => array(
  5210. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5211. 'value' => '0', 'id' => 'CustomId0'
  5212. )),
  5213. array('label' => array('for' => 'CustomId0')),
  5214. 'first',
  5215. '/label',
  5216. '/div',
  5217. array('div' => array('class' => 'checkbox')),
  5218. array('input' => array(
  5219. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5220. 'value' => '1', 'id' => 'CustomId1'
  5221. )),
  5222. array('label' => array('for' => 'CustomId1')),
  5223. 'second',
  5224. '/label',
  5225. '/div',
  5226. array('div' => array('class' => 'checkbox')),
  5227. array('input' => array(
  5228. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5229. 'value' => '2', 'id' => 'CustomId2'
  5230. )),
  5231. array('label' => array('for' => 'CustomId2')),
  5232. 'third',
  5233. '/label',
  5234. '/div'
  5235. );
  5236. $this->assertTags($result, $expected);
  5237. }
  5238. /**
  5239. * testCheckbox method
  5240. *
  5241. * Test generation of checkboxes
  5242. *
  5243. * @return void
  5244. */
  5245. public function testCheckbox() {
  5246. $result = $this->Form->checkbox('Model.field');
  5247. $expected = array(
  5248. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5249. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5250. );
  5251. $this->assertTags($result, $expected);
  5252. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  5253. $expected = array(
  5254. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  5255. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  5256. );
  5257. $this->assertTags($result, $expected);
  5258. $Contact = ClassRegistry::getObject('Contact');
  5259. $Contact->validationErrors['field'] = 1;
  5260. $this->Form->request->data['Contact']['field'] = 'myvalue';
  5261. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  5262. $expected = array(
  5263. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5264. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  5265. );
  5266. $this->assertTags($result, $expected);
  5267. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5268. $expected = array(
  5269. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5270. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  5271. );
  5272. $this->assertTags($result, $expected);
  5273. $this->Form->request->data['Contact']['field'] = '';
  5274. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  5275. $expected = array(
  5276. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5277. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  5278. );
  5279. $this->assertTags($result, $expected);
  5280. $Contact->validationErrors = array();
  5281. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5282. $expected = array(
  5283. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5284. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  5285. );
  5286. $this->assertTags($result, $expected);
  5287. $this->Form->request->data['Contact']['published'] = 1;
  5288. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5289. $expected = array(
  5290. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5291. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  5292. );
  5293. $this->assertTags($result, $expected);
  5294. $this->Form->request->data['Contact']['published'] = 0;
  5295. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5296. $expected = array(
  5297. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5298. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  5299. );
  5300. $this->assertTags($result, $expected);
  5301. $result = $this->Form->checkbox('Model.CustomField.1.value');
  5302. $expected = array(
  5303. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  5304. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  5305. );
  5306. $this->assertTags($result, $expected);
  5307. $result = $this->Form->checkbox('CustomField.1.value');
  5308. $expected = array(
  5309. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  5310. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  5311. );
  5312. $this->assertTags($result, $expected);
  5313. }
  5314. /**
  5315. * test checkbox() with a custom name attribute
  5316. *
  5317. * @return void
  5318. */
  5319. public function testCheckboxCustomNameAttribute() {
  5320. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  5321. $expected = array(
  5322. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  5323. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  5324. );
  5325. $this->assertTags($result, $expected);
  5326. }
  5327. /**
  5328. * test the checked option for checkboxes.
  5329. *
  5330. * @return void
  5331. */
  5332. public function testCheckboxCheckedOption() {
  5333. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  5334. $expected = array(
  5335. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5336. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5337. );
  5338. $this->assertTags($result, $expected);
  5339. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  5340. $expected = array(
  5341. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5342. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5343. );
  5344. $this->assertTags($result, $expected);
  5345. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  5346. $expected = array(
  5347. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5348. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5349. );
  5350. $this->assertTags($result, $expected);
  5351. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5352. $expected = array(
  5353. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5354. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5355. );
  5356. $this->assertTags($result, $expected);
  5357. $this->Form->request->data['Model']['field'] = 1;
  5358. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5359. $expected = array(
  5360. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5361. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5362. );
  5363. $this->assertTags($result, $expected);
  5364. }
  5365. /**
  5366. * Test that disabled attribute works on both the checkbox and hidden input.
  5367. *
  5368. * @return void
  5369. */
  5370. public function testCheckboxDisabling() {
  5371. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5372. $expected = array(
  5373. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5374. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5375. );
  5376. $this->assertTags($result, $expected);
  5377. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5378. $expected = array(
  5379. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5380. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5381. );
  5382. $this->assertTags($result, $expected);
  5383. }
  5384. /**
  5385. * Test that the hidden input for checkboxes can be omitted or set to a
  5386. * specific value.
  5387. *
  5388. * @return void
  5389. */
  5390. public function testCheckboxHiddenField() {
  5391. $result = $this->Form->input('UserForm.something', array(
  5392. 'type' => 'checkbox',
  5393. 'hiddenField' => false
  5394. ));
  5395. $expected = array(
  5396. 'div' => array('class' => 'input checkbox'),
  5397. array('input' => array(
  5398. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5399. 'value' => '1', 'id' => 'UserFormSomething'
  5400. )),
  5401. 'label' => array('for' => 'UserFormSomething'),
  5402. 'Something',
  5403. '/label',
  5404. '/div'
  5405. );
  5406. $this->assertTags($result, $expected);
  5407. $result = $this->Form->input('UserForm.something', array(
  5408. 'type' => 'checkbox',
  5409. 'value' => 'Y',
  5410. 'hiddenField' => 'N',
  5411. ));
  5412. $expected = array(
  5413. 'div' => array('class' => 'input checkbox'),
  5414. array('input' => array(
  5415. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  5416. 'value' => 'N', 'id' => 'UserFormSomething_'
  5417. )),
  5418. array('input' => array(
  5419. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5420. 'value' => 'Y', 'id' => 'UserFormSomething'
  5421. )),
  5422. 'label' => array('for' => 'UserFormSomething'),
  5423. 'Something',
  5424. '/label',
  5425. '/div'
  5426. );
  5427. $this->assertTags($result, $expected);
  5428. }
  5429. /**
  5430. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  5431. *
  5432. * @return void
  5433. */
  5434. public function testCheckboxZeroValue() {
  5435. $result = $this->Form->input('User.get_spam', array(
  5436. 'type' => 'checkbox',
  5437. 'value' => '0',
  5438. 'hiddenField' => '1',
  5439. ));
  5440. $expected = array(
  5441. 'div' => array('class' => 'input checkbox'),
  5442. array('input' => array(
  5443. 'type' => 'hidden', 'name' => 'data[User][get_spam]',
  5444. 'value' => '1', 'id' => 'UserGetSpam_'
  5445. )),
  5446. array('input' => array(
  5447. 'type' => 'checkbox', 'name' => 'data[User][get_spam]',
  5448. 'value' => '0', 'id' => 'UserGetSpam'
  5449. )),
  5450. 'label' => array('for' => 'UserGetSpam'),
  5451. 'Get Spam',
  5452. '/label',
  5453. '/div'
  5454. );
  5455. $this->assertTags($result, $expected);
  5456. }
  5457. /**
  5458. * testDateTime method
  5459. *
  5460. * Test generation of date/time select elements
  5461. *
  5462. * @return void
  5463. */
  5464. public function testDateTime() {
  5465. extract($this->dateRegex);
  5466. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5467. $now = strtotime('now');
  5468. $expected = array(
  5469. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5470. $daysRegex,
  5471. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5472. date('j', $now),
  5473. '/option',
  5474. '*/select',
  5475. '-',
  5476. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5477. $monthsRegex,
  5478. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5479. date('F', $now),
  5480. '/option',
  5481. '*/select',
  5482. '-',
  5483. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5484. $yearsRegex,
  5485. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5486. date('Y', $now),
  5487. '/option',
  5488. '*/select',
  5489. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5490. $hoursRegex,
  5491. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5492. date('g', $now),
  5493. '/option',
  5494. '*/select',
  5495. ':',
  5496. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5497. $minutesRegex,
  5498. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5499. date('i', $now),
  5500. '/option',
  5501. '*/select',
  5502. ' ',
  5503. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5504. $meridianRegex,
  5505. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5506. date('a', $now),
  5507. '/option',
  5508. '*/select'
  5509. );
  5510. $this->assertTags($result, $expected);
  5511. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5512. $expected = array(
  5513. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5514. $daysRegex,
  5515. array('option' => array('value' => '')),
  5516. '/option',
  5517. '*/select',
  5518. '-',
  5519. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5520. $monthsRegex,
  5521. array('option' => array('value' => '')),
  5522. '/option',
  5523. '*/select',
  5524. '-',
  5525. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5526. $yearsRegex,
  5527. array('option' => array('value' => '')),
  5528. '/option',
  5529. '*/select',
  5530. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5531. $hoursRegex,
  5532. array('option' => array('value' => '')),
  5533. '/option',
  5534. '*/select',
  5535. ':',
  5536. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5537. $minutesRegex,
  5538. array('option' => array('value' => '')),
  5539. '/option',
  5540. '*/select',
  5541. ' ',
  5542. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5543. $meridianRegex,
  5544. array('option' => array('value' => '')),
  5545. '/option',
  5546. '*/select'
  5547. );
  5548. $this->assertTags($result, $expected);
  5549. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5550. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5551. $this->assertTags($result, $expected);
  5552. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5553. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5554. $this->assertTags($result, $expected);
  5555. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5556. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5557. $expected = array(
  5558. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5559. $daysRegex,
  5560. array('option' => array('value' => '')),
  5561. '/option',
  5562. '*/select',
  5563. '-',
  5564. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5565. $monthsRegex,
  5566. array('option' => array('value' => '')),
  5567. '/option',
  5568. '*/select',
  5569. '-',
  5570. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5571. $yearsRegex,
  5572. array('option' => array('value' => '')),
  5573. '/option',
  5574. '*/select',
  5575. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5576. $hoursRegex,
  5577. array('option' => array('value' => '')),
  5578. '/option',
  5579. '*/select',
  5580. ':',
  5581. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5582. $minutesRegex,
  5583. array('option' => array('value' => '')),
  5584. '/option',
  5585. array('option' => array('value' => '00')),
  5586. '00',
  5587. '/option',
  5588. array('option' => array('value' => '05')),
  5589. '05',
  5590. '/option',
  5591. array('option' => array('value' => '10')),
  5592. '10',
  5593. '/option',
  5594. '*/select',
  5595. ' ',
  5596. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5597. $meridianRegex,
  5598. array('option' => array('value' => '')),
  5599. '/option',
  5600. '*/select'
  5601. );
  5602. $this->assertTags($result, $expected);
  5603. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5604. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5605. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5606. $this->Form->request->data['Contact']['data'] = null;
  5607. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5608. $expected = array(
  5609. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5610. $daysRegex,
  5611. array('option' => array('value' => '')),
  5612. '/option',
  5613. '*/select',
  5614. '-',
  5615. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5616. $monthsRegex,
  5617. array('option' => array('value' => '')),
  5618. '/option',
  5619. '*/select',
  5620. '-',
  5621. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5622. $yearsRegex,
  5623. array('option' => array('value' => '')),
  5624. '/option',
  5625. '*/select',
  5626. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5627. $hoursRegex,
  5628. array('option' => array('value' => '')),
  5629. '/option',
  5630. '*/select',
  5631. ':',
  5632. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5633. $minutesRegex,
  5634. array('option' => array('value' => '')),
  5635. '/option',
  5636. '*/select',
  5637. ' ',
  5638. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5639. $meridianRegex,
  5640. array('option' => array('value' => '')),
  5641. '/option',
  5642. '*/select'
  5643. );
  5644. $this->assertTags($result, $expected);
  5645. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5646. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5647. $now = strtotime($this->Form->data['Model']['field']);
  5648. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5649. $expected = array(
  5650. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5651. $daysRegex,
  5652. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5653. date('j', $now),
  5654. '/option',
  5655. '*/select',
  5656. '-',
  5657. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5658. $monthsRegex,
  5659. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5660. date('F', $now),
  5661. '/option',
  5662. '*/select',
  5663. '-',
  5664. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5665. $yearsRegex,
  5666. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5667. date('Y', $now),
  5668. '/option',
  5669. '*/select',
  5670. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5671. $hoursRegex,
  5672. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5673. date('g', $now),
  5674. '/option',
  5675. '*/select',
  5676. ':',
  5677. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5678. $minutesRegex,
  5679. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5680. date('i', $now),
  5681. '/option',
  5682. '*/select',
  5683. ' ',
  5684. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5685. $meridianRegex,
  5686. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5687. date('a', $now),
  5688. '/option',
  5689. '*/select'
  5690. );
  5691. $this->assertTags($result, $expected);
  5692. $selected = strtotime('2008-10-26 12:33:00');
  5693. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5694. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5695. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5696. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5697. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5698. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5699. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5700. $this->Form->create('Contact');
  5701. $result = $this->Form->input('published');
  5702. $now = strtotime('now');
  5703. $expected = array(
  5704. 'div' => array('class' => 'input date'),
  5705. 'label' => array('for' => 'ContactPublishedMonth'),
  5706. 'Published',
  5707. '/label',
  5708. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5709. $monthsRegex,
  5710. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5711. date('F', $now),
  5712. '/option',
  5713. '*/select',
  5714. '-',
  5715. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5716. $daysRegex,
  5717. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5718. date('j', $now),
  5719. '/option',
  5720. '*/select',
  5721. '-',
  5722. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5723. $yearsRegex,
  5724. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5725. date('Y', $now),
  5726. '/option',
  5727. '*/select',
  5728. '/div'
  5729. );
  5730. $this->assertTags($result, $expected);
  5731. $result = $this->Form->input('published2', array('type' => 'date'));
  5732. $now = strtotime('now');
  5733. $expected = array(
  5734. 'div' => array('class' => 'input date'),
  5735. 'label' => array('for' => 'ContactPublished2Month'),
  5736. 'Published2',
  5737. '/label',
  5738. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5739. $monthsRegex,
  5740. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5741. date('F', $now),
  5742. '/option',
  5743. '*/select',
  5744. '-',
  5745. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5746. $daysRegex,
  5747. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5748. date('j', $now),
  5749. '/option',
  5750. '*/select',
  5751. '-',
  5752. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5753. $yearsRegex,
  5754. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5755. date('Y', $now),
  5756. '/option',
  5757. '*/select',
  5758. '/div'
  5759. );
  5760. $this->assertTags($result, $expected);
  5761. $this->Form->create('Contact');
  5762. $result = $this->Form->input('published', array('monthNames' => false));
  5763. $now = strtotime('now');
  5764. $expected = array(
  5765. 'div' => array('class' => 'input date'),
  5766. 'label' => array('for' => 'ContactPublishedMonth'),
  5767. 'Published',
  5768. '/label',
  5769. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5770. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5771. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5772. date('m', $now),
  5773. '/option',
  5774. '*/select',
  5775. '-',
  5776. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5777. $daysRegex,
  5778. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5779. date('j', $now),
  5780. '/option',
  5781. '*/select',
  5782. '-',
  5783. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5784. $yearsRegex,
  5785. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5786. date('Y', $now),
  5787. '/option',
  5788. '*/select',
  5789. '/div'
  5790. );
  5791. $this->assertTags($result, $expected);
  5792. $result = $this->Form->input('published', array(
  5793. 'timeFormat' => 24,
  5794. 'interval' => 5,
  5795. 'selected' => strtotime('2009-09-03 13:37:00'),
  5796. 'type' => 'datetime'
  5797. ));
  5798. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5799. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5800. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5801. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5802. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5803. }
  5804. /**
  5805. * Test dateTime with rounding
  5806. *
  5807. * @return void
  5808. */
  5809. public function testDateTimeRounding() {
  5810. $this->Form->request->data['Contact'] = array(
  5811. 'date' => array(
  5812. 'day' => '13',
  5813. 'month' => '12',
  5814. 'year' => '2010',
  5815. 'hour' => '04',
  5816. 'min' => '19',
  5817. 'meridian' => 'AM'
  5818. )
  5819. );
  5820. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5821. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5822. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5823. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5824. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5825. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5826. }
  5827. /**
  5828. * Test that empty values don't trigger errors.
  5829. *
  5830. * @return void
  5831. */
  5832. public function testDateTimeNoErrorsOnEmptyData() {
  5833. $this->Form->request->data['Contact'] = array(
  5834. 'date' => array(
  5835. 'day' => '',
  5836. 'month' => '',
  5837. 'year' => '',
  5838. 'hour' => '',
  5839. 'min' => '',
  5840. 'meridian' => ''
  5841. )
  5842. );
  5843. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5844. $this->assertNotEmpty($result);
  5845. }
  5846. /**
  5847. * test that datetime() and default values work.
  5848. *
  5849. * @return void
  5850. */
  5851. public function testDatetimeWithDefault() {
  5852. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5853. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5854. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5855. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5856. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5857. 'default' => '2009-06-01 11:15:30'
  5858. ));
  5859. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5860. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5861. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5862. }
  5863. /**
  5864. * test that bogus non-date time data doesn't cause errors.
  5865. *
  5866. * @return void
  5867. */
  5868. public function testDateTimeWithBogusData() {
  5869. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5870. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5871. }
  5872. /**
  5873. * testDateTime all zeros
  5874. *
  5875. * @return void
  5876. */
  5877. public function testDateTimeAllZeros() {
  5878. $result = $this->Form->dateTime('Contact.date',
  5879. 'DMY',
  5880. false,
  5881. array(
  5882. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  5883. 'value' => '0000-00-00'
  5884. )
  5885. );
  5886. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5887. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5888. }
  5889. /**
  5890. * testDateTimeEmptyAsArray
  5891. *
  5892. * @return void
  5893. */
  5894. public function testDateTimeEmptyAsArray() {
  5895. $result = $this->Form->dateTime('Contact.date',
  5896. 'DMY',
  5897. '12',
  5898. array(
  5899. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5900. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5901. )
  5902. )
  5903. );
  5904. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5905. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5906. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5907. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5908. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5909. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5910. $result = $this->Form->dateTime('Contact.date',
  5911. 'DMY',
  5912. '12',
  5913. array(
  5914. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5915. )
  5916. );
  5917. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5918. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5919. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5920. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5921. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5922. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5923. }
  5924. /**
  5925. * testFormDateTimeMulti method
  5926. *
  5927. * test multiple datetime element generation
  5928. *
  5929. * @return void
  5930. */
  5931. public function testFormDateTimeMulti() {
  5932. extract($this->dateRegex);
  5933. $result = $this->Form->dateTime('Contact.1.updated');
  5934. $expected = array(
  5935. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5936. $daysRegex,
  5937. array('option' => array('value' => '')),
  5938. '/option',
  5939. '*/select',
  5940. '-',
  5941. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5942. $monthsRegex,
  5943. array('option' => array('value' => '')),
  5944. '/option',
  5945. '*/select',
  5946. '-',
  5947. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5948. $yearsRegex,
  5949. array('option' => array('value' => '')),
  5950. '/option',
  5951. '*/select',
  5952. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5953. $hoursRegex,
  5954. array('option' => array('value' => '')),
  5955. '/option',
  5956. '*/select',
  5957. ':',
  5958. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5959. $minutesRegex,
  5960. array('option' => array('value' => '')),
  5961. '/option',
  5962. '*/select',
  5963. ' ',
  5964. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5965. $meridianRegex,
  5966. array('option' => array('value' => '')),
  5967. '/option',
  5968. '*/select'
  5969. );
  5970. $this->assertTags($result, $expected);
  5971. $result = $this->Form->dateTime('Contact.2.updated');
  5972. $expected = array(
  5973. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5974. $daysRegex,
  5975. array('option' => array('value' => '')),
  5976. '/option',
  5977. '*/select',
  5978. '-',
  5979. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5980. $monthsRegex,
  5981. array('option' => array('value' => '')),
  5982. '/option',
  5983. '*/select',
  5984. '-',
  5985. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5986. $yearsRegex,
  5987. array('option' => array('value' => '')),
  5988. '/option',
  5989. '*/select',
  5990. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5991. $hoursRegex,
  5992. array('option' => array('value' => '')),
  5993. '/option',
  5994. '*/select',
  5995. ':',
  5996. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5997. $minutesRegex,
  5998. array('option' => array('value' => '')),
  5999. '/option',
  6000. '*/select',
  6001. ' ',
  6002. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  6003. $meridianRegex,
  6004. array('option' => array('value' => '')),
  6005. '/option',
  6006. '*/select'
  6007. );
  6008. $this->assertTags($result, $expected);
  6009. }
  6010. /**
  6011. * When changing the date format, the label should always focus the first select box when
  6012. * clicked.
  6013. *
  6014. * @return void
  6015. */
  6016. public function testDateTimeLabelIdMatchesFirstInput() {
  6017. $result = $this->Form->input('Model.date', array('type' => 'date'));
  6018. $this->assertContains('label for="ModelDateMonth"', $result);
  6019. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  6020. $this->assertContains('label for="ModelDateDay"', $result);
  6021. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  6022. $this->assertContains('label for="ModelDateYear"', $result);
  6023. }
  6024. /**
  6025. * testMonth method
  6026. *
  6027. * @return void
  6028. */
  6029. public function testMonth() {
  6030. $result = $this->Form->month('Model.field');
  6031. $expected = array(
  6032. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6033. array('option' => array('value' => '')),
  6034. '/option',
  6035. array('option' => array('value' => '01')),
  6036. date('F', strtotime('2008-01-01 00:00:00')),
  6037. '/option',
  6038. array('option' => array('value' => '02')),
  6039. date('F', strtotime('2008-02-01 00:00:00')),
  6040. '/option',
  6041. '*/select',
  6042. );
  6043. $this->assertTags($result, $expected);
  6044. $result = $this->Form->month('Model.field', array('empty' => true));
  6045. $expected = array(
  6046. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6047. array('option' => array('value' => '')),
  6048. '/option',
  6049. array('option' => array('value' => '01')),
  6050. date('F', strtotime('2008-01-01 00:00:00')),
  6051. '/option',
  6052. array('option' => array('value' => '02')),
  6053. date('F', strtotime('2008-02-01 00:00:00')),
  6054. '/option',
  6055. '*/select',
  6056. );
  6057. $this->assertTags($result, $expected);
  6058. $result = $this->Form->month('Model.field', array('monthNames' => false));
  6059. $expected = array(
  6060. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6061. array('option' => array('value' => '')),
  6062. '/option',
  6063. array('option' => array('value' => '01')),
  6064. '01',
  6065. '/option',
  6066. array('option' => array('value' => '02')),
  6067. '02',
  6068. '/option',
  6069. '*/select',
  6070. );
  6071. $this->assertTags($result, $expected);
  6072. $monthNames = array(
  6073. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6074. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  6075. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  6076. $expected = array(
  6077. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6078. array('option' => array('value' => '')),
  6079. '/option',
  6080. array('option' => array('value' => '01')),
  6081. 'Jan',
  6082. '/option',
  6083. array('option' => array('value' => '02')),
  6084. 'Feb',
  6085. '/option',
  6086. '*/select',
  6087. );
  6088. $this->assertTags($result, $expected);
  6089. $this->Form->request->data['Project']['release'] = '2050-02-10';
  6090. $result = $this->Form->month('Project.release');
  6091. $expected = array(
  6092. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  6093. array('option' => array('value' => '')),
  6094. '/option',
  6095. array('option' => array('value' => '01')),
  6096. 'January',
  6097. '/option',
  6098. array('option' => array('value' => '02', 'selected' => 'selected')),
  6099. 'February',
  6100. '/option',
  6101. '*/select',
  6102. );
  6103. $this->assertTags($result, $expected);
  6104. }
  6105. /**
  6106. * testDay method
  6107. *
  6108. * @return void
  6109. */
  6110. public function testDay() {
  6111. extract($this->dateRegex);
  6112. $result = $this->Form->day('Model.field', array('value' => false));
  6113. $expected = array(
  6114. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6115. array('option' => array('value' => '')),
  6116. '/option',
  6117. array('option' => array('value' => '01')),
  6118. '1',
  6119. '/option',
  6120. array('option' => array('value' => '02')),
  6121. '2',
  6122. '/option',
  6123. $daysRegex,
  6124. '/select',
  6125. );
  6126. $this->assertTags($result, $expected);
  6127. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6128. $result = $this->Form->day('Model.field');
  6129. $expected = array(
  6130. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6131. array('option' => array('value' => '')),
  6132. '/option',
  6133. array('option' => array('value' => '01')),
  6134. '1',
  6135. '/option',
  6136. array('option' => array('value' => '02')),
  6137. '2',
  6138. '/option',
  6139. $daysRegex,
  6140. array('option' => array('value' => '10', 'selected' => 'selected')),
  6141. '10',
  6142. '/option',
  6143. $daysRegex,
  6144. '/select',
  6145. );
  6146. $this->assertTags($result, $expected);
  6147. $this->Form->request->data['Model']['field'] = '';
  6148. $result = $this->Form->day('Model.field', array('value' => '10'));
  6149. $expected = array(
  6150. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6151. array('option' => array('value' => '')),
  6152. '/option',
  6153. array('option' => array('value' => '01')),
  6154. '1',
  6155. '/option',
  6156. array('option' => array('value' => '02')),
  6157. '2',
  6158. '/option',
  6159. $daysRegex,
  6160. array('option' => array('value' => '10', 'selected' => 'selected')),
  6161. '10',
  6162. '/option',
  6163. $daysRegex,
  6164. '/select',
  6165. );
  6166. $this->assertTags($result, $expected);
  6167. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6168. $result = $this->Form->day('Model.field', array('value' => true));
  6169. $expected = array(
  6170. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6171. array('option' => array('value' => '')),
  6172. '/option',
  6173. array('option' => array('value' => '01')),
  6174. '1',
  6175. '/option',
  6176. array('option' => array('value' => '02')),
  6177. '2',
  6178. '/option',
  6179. $daysRegex,
  6180. array('option' => array('value' => '10', 'selected' => 'selected')),
  6181. '10',
  6182. '/option',
  6183. $daysRegex,
  6184. '/select',
  6185. );
  6186. $this->assertTags($result, $expected);
  6187. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6188. $result = $this->Form->day('Project.release');
  6189. $expected = array(
  6190. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  6191. array('option' => array('value' => '')),
  6192. '/option',
  6193. array('option' => array('value' => '01')),
  6194. '1',
  6195. '/option',
  6196. array('option' => array('value' => '02')),
  6197. '2',
  6198. '/option',
  6199. $daysRegex,
  6200. array('option' => array('value' => '10', 'selected' => 'selected')),
  6201. '10',
  6202. '/option',
  6203. $daysRegex,
  6204. '/select',
  6205. );
  6206. $this->assertTags($result, $expected);
  6207. }
  6208. /**
  6209. * testMinute method
  6210. *
  6211. * @return void
  6212. */
  6213. public function testMinute() {
  6214. extract($this->dateRegex);
  6215. $result = $this->Form->minute('Model.field');
  6216. $expected = array(
  6217. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6218. array('option' => array('value' => '')),
  6219. '/option',
  6220. array('option' => array('value' => '00')),
  6221. '00',
  6222. '/option',
  6223. array('option' => array('value' => '01')),
  6224. '01',
  6225. '/option',
  6226. array('option' => array('value' => '02')),
  6227. '02',
  6228. '/option',
  6229. $minutesRegex,
  6230. '/select',
  6231. );
  6232. $this->assertTags($result, $expected);
  6233. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6234. $result = $this->Form->minute('Model.field');
  6235. $expected = array(
  6236. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6237. array('option' => array('value' => '')),
  6238. '/option',
  6239. array('option' => array('value' => '00')),
  6240. '00',
  6241. '/option',
  6242. array('option' => array('value' => '01')),
  6243. '01',
  6244. '/option',
  6245. array('option' => array('value' => '02')),
  6246. '02',
  6247. '/option',
  6248. $minutesRegex,
  6249. array('option' => array('value' => '12', 'selected' => 'selected')),
  6250. '12',
  6251. '/option',
  6252. $minutesRegex,
  6253. '/select',
  6254. );
  6255. $this->assertTags($result, $expected);
  6256. $this->Form->request->data['Model']['field'] = '';
  6257. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6258. $expected = array(
  6259. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6260. array('option' => array('value' => '')),
  6261. '/option',
  6262. array('option' => array('value' => '00')),
  6263. '00',
  6264. '/option',
  6265. array('option' => array('value' => '05')),
  6266. '05',
  6267. '/option',
  6268. array('option' => array('value' => '10')),
  6269. '10',
  6270. '/option',
  6271. $minutesRegex,
  6272. '/select',
  6273. );
  6274. $this->assertTags($result, $expected);
  6275. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6276. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6277. $expected = array(
  6278. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6279. array('option' => array('value' => '')),
  6280. '/option',
  6281. array('option' => array('value' => '00')),
  6282. '00',
  6283. '/option',
  6284. array('option' => array('value' => '05')),
  6285. '05',
  6286. '/option',
  6287. array('option' => array('value' => '10', 'selected' => 'selected')),
  6288. '10',
  6289. '/option',
  6290. $minutesRegex,
  6291. '/select',
  6292. );
  6293. $this->assertTags($result, $expected);
  6294. }
  6295. /**
  6296. * testHour method
  6297. *
  6298. * @return void
  6299. */
  6300. public function testHour() {
  6301. extract($this->dateRegex);
  6302. $result = $this->Form->hour('Model.field', false);
  6303. $expected = array(
  6304. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6305. array('option' => array('value' => '')),
  6306. '/option',
  6307. array('option' => array('value' => '01')),
  6308. '1',
  6309. '/option',
  6310. array('option' => array('value' => '02')),
  6311. '2',
  6312. '/option',
  6313. $hoursRegex,
  6314. '/select',
  6315. );
  6316. $this->assertTags($result, $expected);
  6317. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6318. $result = $this->Form->hour('Model.field', false);
  6319. $expected = array(
  6320. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6321. array('option' => array('value' => '')),
  6322. '/option',
  6323. array('option' => array('value' => '01')),
  6324. '1',
  6325. '/option',
  6326. array('option' => array('value' => '02')),
  6327. '2',
  6328. '/option',
  6329. $hoursRegex,
  6330. array('option' => array('value' => '12', 'selected' => 'selected')),
  6331. '12',
  6332. '/option',
  6333. '/select',
  6334. );
  6335. $this->assertTags($result, $expected);
  6336. $this->Form->request->data['Model']['field'] = '';
  6337. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  6338. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6339. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  6340. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6341. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6342. $result = $this->Form->hour('Model.field', true);
  6343. $expected = array(
  6344. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6345. array('option' => array('value' => '')),
  6346. '/option',
  6347. array('option' => array('value' => '00', 'selected' => 'selected')),
  6348. '0',
  6349. '/option',
  6350. array('option' => array('value' => '01')),
  6351. '1',
  6352. '/option',
  6353. array('option' => array('value' => '02')),
  6354. '2',
  6355. '/option',
  6356. $hoursRegex,
  6357. '/select',
  6358. );
  6359. $this->assertTags($result, $expected);
  6360. unset($this->Form->request->data['Model']['field']);
  6361. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  6362. $thisHour = date('H');
  6363. $optValue = date('G');
  6364. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6365. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6366. $result = $this->Form->hour('Model.field', true);
  6367. $expected = array(
  6368. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6369. array('option' => array('value' => '')),
  6370. '/option',
  6371. array('option' => array('value' => '00')),
  6372. '0',
  6373. '/option',
  6374. array('option' => array('value' => '01', 'selected' => 'selected')),
  6375. '1',
  6376. '/option',
  6377. array('option' => array('value' => '02')),
  6378. '2',
  6379. '/option',
  6380. $hoursRegex,
  6381. '/select',
  6382. );
  6383. $this->assertTags($result, $expected);
  6384. }
  6385. /**
  6386. * testYear method
  6387. *
  6388. * @return void
  6389. */
  6390. public function testYear() {
  6391. $result = $this->Form->year('Model.field', 2006, 2007);
  6392. $expected = array(
  6393. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6394. array('option' => array('value' => '')),
  6395. '/option',
  6396. array('option' => array('value' => '2007')),
  6397. '2007',
  6398. '/option',
  6399. array('option' => array('value' => '2006')),
  6400. '2006',
  6401. '/option',
  6402. '/select',
  6403. );
  6404. $this->assertTags($result, $expected);
  6405. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  6406. $expected = array(
  6407. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6408. array('option' => array('value' => '')),
  6409. '/option',
  6410. array('option' => array('value' => '2006')),
  6411. '2006',
  6412. '/option',
  6413. array('option' => array('value' => '2007')),
  6414. '2007',
  6415. '/option',
  6416. '/select',
  6417. );
  6418. $this->assertTags($result, $expected);
  6419. $this->request->data['Contact']['published'] = '';
  6420. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6421. $expected = array(
  6422. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6423. array('option' => array('value' => '')),
  6424. '/option',
  6425. array('option' => array('value' => '2007')),
  6426. '2007',
  6427. '/option',
  6428. array('option' => array('value' => '2006')),
  6429. '2006',
  6430. '/option',
  6431. '/select',
  6432. );
  6433. $this->assertTags($result, $expected);
  6434. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6435. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6436. $expected = array(
  6437. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6438. array('option' => array('value' => '2007')),
  6439. '2007',
  6440. '/option',
  6441. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6442. '2006',
  6443. '/option',
  6444. '/select',
  6445. );
  6446. $this->assertTags($result, $expected);
  6447. $this->Form->request->data['Contact']['published'] = '';
  6448. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6449. $expected = array(
  6450. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6451. array('option' => array('value' => '')),
  6452. '/option',
  6453. array('option' => array('value' => '2007')),
  6454. '2007',
  6455. '/option',
  6456. array('option' => array('value' => '2006')),
  6457. '2006',
  6458. '/option',
  6459. '/select',
  6460. );
  6461. $this->assertTags($result, $expected);
  6462. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6463. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6464. $expected = array(
  6465. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6466. array('option' => array('value' => '2007')),
  6467. '2007',
  6468. '/option',
  6469. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6470. '2006',
  6471. '/option',
  6472. '/select',
  6473. );
  6474. $this->assertTags($result, $expected);
  6475. $this->Form->request->data['Contact']['published'] = '';
  6476. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6477. $expected = array(
  6478. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6479. array('option' => array('value' => '')),
  6480. '/option',
  6481. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6482. '2007',
  6483. '/option',
  6484. array('option' => array('value' => '2006')),
  6485. '2006',
  6486. '/option',
  6487. '/select',
  6488. );
  6489. $this->assertTags($result, $expected);
  6490. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6491. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6492. $expected = array(
  6493. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6494. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6495. '2007',
  6496. '/option',
  6497. array('option' => array('value' => '2006')),
  6498. '2006',
  6499. '/option',
  6500. '/select',
  6501. );
  6502. $this->assertTags($result, $expected);
  6503. $this->Form->request->data['Contact']['published'] = '';
  6504. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6505. $expected = array(
  6506. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6507. array('option' => array('value' => '2008')),
  6508. '2008',
  6509. '/option',
  6510. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6511. '2007',
  6512. '/option',
  6513. array('option' => array('value' => '2006')),
  6514. '2006',
  6515. '/option',
  6516. '/select',
  6517. );
  6518. $this->assertTags($result, $expected);
  6519. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6520. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6521. $expected = array(
  6522. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6523. array('option' => array('value' => '2008')),
  6524. '2008',
  6525. '/option',
  6526. array('option' => array('value' => '2007')),
  6527. '2007',
  6528. '/option',
  6529. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6530. '2006',
  6531. '/option',
  6532. '/select',
  6533. );
  6534. $this->assertTags($result, $expected);
  6535. $this->Form->request->data = array();
  6536. $this->Form->create('Contact');
  6537. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6538. $expected = array(
  6539. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6540. array('option' => array('value' => '2008')),
  6541. '2008',
  6542. '/option',
  6543. array('option' => array('value' => '2007')),
  6544. '2007',
  6545. '/option',
  6546. array('option' => array('value' => '2006')),
  6547. '2006',
  6548. '/option',
  6549. '/select',
  6550. );
  6551. $this->assertTags($result, $expected);
  6552. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6553. $this->assertContains('data[Contact][published][year]', $result);
  6554. }
  6555. /**
  6556. * testYearAutoExpandRange method
  6557. *
  6558. * @return void
  6559. */
  6560. public function testYearAutoExpandRange() {
  6561. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6562. $result = $this->Form->year('User.birthday');
  6563. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6564. $result = $matches[1];
  6565. $expected = range(date('Y') + 20, 1930);
  6566. $this->assertEquals($expected, $result);
  6567. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6568. $result = $this->Form->year('Project.release');
  6569. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6570. $result = $matches[1];
  6571. $expected = range(2050, date('Y') - 20);
  6572. $this->assertEquals($expected, $result);
  6573. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6574. $result = $this->Form->year('Project.release', 1890, 1900);
  6575. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6576. $result = $matches[1];
  6577. $expected = range(1900, 1881);
  6578. $this->assertEquals($expected, $result);
  6579. }
  6580. /**
  6581. * testInputDate method
  6582. *
  6583. * Test various inputs with type date and different dateFormat values.
  6584. * Failing to provide a dateFormat key should not error.
  6585. * It should simply not pre-select any value then.
  6586. *
  6587. * @return void
  6588. */
  6589. public function testInputDate() {
  6590. $this->Form->request->data = array(
  6591. 'User' => array(
  6592. 'month_year' => array('month' => date('m')),
  6593. 'just_year' => array('month' => date('m')),
  6594. 'just_month' => array('year' => date('Y')),
  6595. 'just_day' => array('month' => date('m')),
  6596. )
  6597. );
  6598. $this->Form->create('User');
  6599. $result = $this->Form->input('month_year',
  6600. array(
  6601. 'label' => false,
  6602. 'div' => false,
  6603. 'type' => 'date',
  6604. 'dateFormat' => 'MY',
  6605. 'minYear' => 2006,
  6606. 'maxYear' => 2008
  6607. )
  6608. );
  6609. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6610. $this->assertNotContains('value="2008" selected="selected"', $result);
  6611. $result = $this->Form->input('just_year',
  6612. array(
  6613. 'type' => 'date',
  6614. 'label' => false,
  6615. 'dateFormat' => 'Y',
  6616. 'minYear' => date('Y'),
  6617. 'maxYear' => date('Y', strtotime('+20 years'))
  6618. )
  6619. );
  6620. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6621. $result = $this->Form->input('just_month',
  6622. array(
  6623. 'type' => 'date',
  6624. 'label' => false,
  6625. 'dateFormat' => 'M',
  6626. 'empty' => false,
  6627. )
  6628. );
  6629. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6630. $result = $this->Form->input('just_day',
  6631. array(
  6632. 'type' => 'date',
  6633. 'label' => false,
  6634. 'dateFormat' => 'D',
  6635. 'empty' => false,
  6636. )
  6637. );
  6638. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6639. }
  6640. /**
  6641. * testInputDateMaxYear method
  6642. *
  6643. * Let's say we want to only allow users born from 2006 to 2008 to register
  6644. * This being the first singup page, we still don't have any data
  6645. *
  6646. * @return void
  6647. */
  6648. public function testInputDateMaxYear() {
  6649. $this->Form->request->data = array();
  6650. $this->Form->create('User');
  6651. $result = $this->Form->input('birthday',
  6652. array(
  6653. 'label' => false,
  6654. 'div' => false,
  6655. 'type' => 'date',
  6656. 'dateFormat' => 'DMY',
  6657. 'minYear' => 2006,
  6658. 'maxYear' => 2008
  6659. )
  6660. );
  6661. $this->assertContains('value="2008" selected="selected"', $result);
  6662. }
  6663. /**
  6664. * testTextArea method
  6665. *
  6666. * @return void
  6667. */
  6668. public function testTextArea() {
  6669. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6670. $result = $this->Form->textarea('Model.field');
  6671. $expected = array(
  6672. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6673. 'some test data',
  6674. '/textarea',
  6675. );
  6676. $this->assertTags($result, $expected);
  6677. $result = $this->Form->textarea('Model.tmp');
  6678. $expected = array(
  6679. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6680. '/textarea',
  6681. );
  6682. $this->assertTags($result, $expected);
  6683. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6684. $result = $this->Form->textarea('Model.field');
  6685. $expected = array(
  6686. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6687. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6688. '/textarea',
  6689. );
  6690. $this->assertTags($result, $expected);
  6691. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6692. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6693. $expected = array(
  6694. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6695. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6696. '/textarea',
  6697. );
  6698. $this->assertTags($result, $expected);
  6699. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6700. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6701. $expected = array(
  6702. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6703. '/textarea'
  6704. );
  6705. $this->assertTags($result, $expected);
  6706. }
  6707. /**
  6708. * testTextAreaWithStupidCharacters method
  6709. *
  6710. * test text area with non-ascii characters
  6711. *
  6712. * @return void
  6713. */
  6714. public function testTextAreaWithStupidCharacters() {
  6715. $this->loadFixtures('Post');
  6716. $result = $this->Form->input('Post.content', array(
  6717. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6718. ));
  6719. $expected = array(
  6720. 'div' => array('class' => 'input textarea'),
  6721. 'label' => array('for' => 'PostContent'),
  6722. 'Current Text',
  6723. '/label',
  6724. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6725. 'GREAT®',
  6726. '/textarea',
  6727. '/div'
  6728. );
  6729. $this->assertTags($result, $expected);
  6730. }
  6731. /**
  6732. * testHiddenField method
  6733. *
  6734. * @return void
  6735. */
  6736. public function testHiddenField() {
  6737. $Contact = ClassRegistry::getObject('Contact');
  6738. $Contact->validationErrors['field'] = 1;
  6739. $this->Form->request->data['Contact']['field'] = 'test';
  6740. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6741. $this->assertTags($result, array(
  6742. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6743. );
  6744. }
  6745. /**
  6746. * testFileUploadField method
  6747. *
  6748. * @return void
  6749. */
  6750. public function testFileUploadField() {
  6751. $result = $this->Form->file('Model.upload');
  6752. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6753. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6754. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6755. $expected = array(
  6756. 'div' => array('class' => 'input file'),
  6757. 'label' => array('for' => 'ModelUpload'),
  6758. 'Upload',
  6759. '/label',
  6760. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6761. '/div'
  6762. );
  6763. $this->assertTags($result, $expected);
  6764. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6765. $result = $this->Form->file('Model.upload');
  6766. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6767. }
  6768. /**
  6769. * test File upload input on a model not used in create();
  6770. *
  6771. * @return void
  6772. */
  6773. public function testFileUploadOnOtherModel() {
  6774. $this->Form->create('ValidateUser', array('type' => 'file'));
  6775. $result = $this->Form->file('ValidateProfile.city');
  6776. $expected = array(
  6777. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6778. );
  6779. $this->assertTags($result, $expected);
  6780. }
  6781. /**
  6782. * testButton method
  6783. *
  6784. * @return void
  6785. */
  6786. public function testButton() {
  6787. $result = $this->Form->button('Hi');
  6788. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6789. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6790. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6791. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6792. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6793. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6794. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6795. $result = $this->Form->button('No type', array('type' => false));
  6796. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6797. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6798. $this->assertNotRegExp('/\&039/', $result);
  6799. }
  6800. /**
  6801. * Test that button() makes unlocked fields by default.
  6802. *
  6803. * @return void
  6804. */
  6805. public function testButtonUnlockedByDefault() {
  6806. $this->Form->request->params['_Token']['key'] = 'secured';
  6807. $this->Form->button('Save', array('name' => 'save'));
  6808. $this->Form->button('Clear');
  6809. $result = $this->Form->unlockField();
  6810. $this->assertEquals(array('save'), $result);
  6811. }
  6812. /**
  6813. * testPostButton method
  6814. *
  6815. * @return void
  6816. */
  6817. public function testPostButton() {
  6818. $result = $this->Form->postButton('Hi', '/controller/action');
  6819. $this->assertTags($result, array(
  6820. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6821. 'div' => array('style' => 'display:none;'),
  6822. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6823. '/div',
  6824. 'button' => array('type' => 'submit'),
  6825. 'Hi',
  6826. '/button',
  6827. '/form'
  6828. ));
  6829. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6830. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6831. }
  6832. /**
  6833. * Test that postButton adds _Token fields.
  6834. *
  6835. * @return void
  6836. */
  6837. public function testSecurePostButton() {
  6838. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6839. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6840. $expected = array(
  6841. 'form' => array(
  6842. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6843. ),
  6844. array('div' => array('style' => 'display:none;')),
  6845. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6846. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6847. '/div',
  6848. 'button' => array('type' => 'submit'),
  6849. 'Delete',
  6850. '/button',
  6851. array('div' => array('style' => 'display:none;')),
  6852. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6853. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6854. '/div',
  6855. '/form',
  6856. );
  6857. $this->assertTags($result, $expected);
  6858. }
  6859. /**
  6860. * testPostLink method
  6861. *
  6862. * @return void
  6863. */
  6864. public function testPostLink() {
  6865. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6866. $this->assertTags($result, array(
  6867. 'form' => array(
  6868. 'method' => 'post', 'action' => '/posts/delete/1',
  6869. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6870. ),
  6871. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6872. '/form',
  6873. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6874. 'Delete',
  6875. '/a'
  6876. ));
  6877. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6878. $this->assertTags($result, array(
  6879. 'form' => array(
  6880. 'method' => 'post', 'action' => '/posts/delete/1',
  6881. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6882. ),
  6883. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6884. '/form',
  6885. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6886. 'Delete',
  6887. '/a'
  6888. ));
  6889. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6890. $this->assertTags($result, array(
  6891. 'form' => array(
  6892. 'method' => 'post', 'action' => '/posts/delete/1',
  6893. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6894. ),
  6895. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6896. '/form',
  6897. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6898. 'Delete',
  6899. '/a'
  6900. ));
  6901. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  6902. $this->assertTags($result, array(
  6903. 'form' => array(
  6904. 'method' => 'post', 'action' => '/posts/delete/1',
  6905. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6906. ),
  6907. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6908. '/form',
  6909. '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;/'),
  6910. 'Delete',
  6911. '/a'
  6912. ));
  6913. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6914. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6915. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6916. $this->assertTags($result, array(
  6917. 'form' => array(
  6918. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6919. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6920. ),
  6921. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6922. '/form',
  6923. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6924. 'Delete',
  6925. '/a'
  6926. ));
  6927. $result = $this->Form->postLink(
  6928. '',
  6929. array('controller' => 'items', 'action' => 'delete', 10),
  6930. array('class' => 'btn btn-danger', 'escape' => false),
  6931. 'Confirm thing'
  6932. );
  6933. $this->assertTags($result, array(
  6934. 'form' => array(
  6935. 'method' => 'post', 'action' => '/items/delete/10',
  6936. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6937. ),
  6938. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6939. '/form',
  6940. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6941. '/a'
  6942. ));
  6943. }
  6944. /**
  6945. * test creating postLinks after a GET form.
  6946. *
  6947. * @return void
  6948. */
  6949. public function testPostLinkAfterGetForm() {
  6950. $this->Form->request->params['_Token']['key'] = 'testkey';
  6951. $this->Form->create('User', array('type' => 'get'));
  6952. $this->Form->end();
  6953. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6954. $this->assertTags($result, array(
  6955. 'form' => array(
  6956. 'method' => 'post', 'action' => '/posts/delete/1',
  6957. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6958. ),
  6959. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6960. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6961. 'div' => array('style' => 'display:none;'),
  6962. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6963. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6964. '/div',
  6965. '/form',
  6966. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6967. 'Delete',
  6968. '/a'
  6969. ));
  6970. }
  6971. /**
  6972. * Test that postLink adds _Token fields.
  6973. *
  6974. * @return void
  6975. */
  6976. public function testSecurePostLink() {
  6977. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6978. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6979. $expected = array(
  6980. 'form' => array(
  6981. 'method' => 'post', 'action' => '/posts/delete/1',
  6982. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6983. ),
  6984. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6985. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6986. 'div' => array('style' => 'display:none;'),
  6987. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6988. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6989. '/div',
  6990. '/form',
  6991. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6992. 'Delete',
  6993. '/a'
  6994. );
  6995. $this->assertTags($result, $expected);
  6996. }
  6997. /**
  6998. * Test that postLink adds form tags to view block
  6999. *
  7000. * @return void
  7001. */
  7002. public function testPostLinkFormBuffer() {
  7003. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
  7004. $this->assertTags($result, array(
  7005. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7006. 'Delete',
  7007. '/a'
  7008. ));
  7009. $result = $this->View->fetch('postLink');
  7010. $this->assertTags($result, array(
  7011. 'form' => array(
  7012. 'method' => 'post', 'action' => '/posts/delete/1',
  7013. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7014. ),
  7015. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7016. '/form'
  7017. ));
  7018. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  7019. array('inline' => false, 'method' => 'DELETE')
  7020. );
  7021. $this->assertTags($result, array(
  7022. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7023. 'Delete',
  7024. '/a'
  7025. ));
  7026. $result = $this->View->fetch('postLink');
  7027. $this->assertTags($result, array(
  7028. 'form' => array(
  7029. 'method' => 'post', 'action' => '/posts/delete/1',
  7030. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7031. ),
  7032. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7033. '/form',
  7034. array(
  7035. 'form' => array(
  7036. 'method' => 'post', 'action' => '/posts/delete/2',
  7037. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7038. ),
  7039. ),
  7040. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  7041. '/form'
  7042. ));
  7043. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  7044. $this->assertTags($result, array(
  7045. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7046. 'Delete',
  7047. '/a'
  7048. ));
  7049. $result = $this->View->fetch('foobar');
  7050. $this->assertTags($result, array(
  7051. 'form' => array(
  7052. 'method' => 'post', 'action' => '/posts/delete/1',
  7053. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7054. ),
  7055. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7056. '/form'
  7057. ));
  7058. }
  7059. /**
  7060. * testSubmitButton method
  7061. *
  7062. * @return void
  7063. */
  7064. public function testSubmitButton() {
  7065. $result = $this->Form->submit('');
  7066. $expected = array(
  7067. 'div' => array('class' => 'submit'),
  7068. 'input' => array('type' => 'submit', 'value' => ''),
  7069. '/div'
  7070. );
  7071. $this->assertTags($result, $expected);
  7072. $result = $this->Form->submit('Test Submit');
  7073. $expected = array(
  7074. 'div' => array('class' => 'submit'),
  7075. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  7076. '/div'
  7077. );
  7078. $this->assertTags($result, $expected);
  7079. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  7080. $expected = array(
  7081. 'span' => array('class' => 'submit'),
  7082. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  7083. '/span'
  7084. );
  7085. $this->assertTags($result, $expected);
  7086. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  7087. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  7088. $this->assertTags($result, $expected);
  7089. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  7090. $expected = array(
  7091. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  7092. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  7093. '/div'
  7094. );
  7095. $this->assertTags($result, $expected);
  7096. $result = $this->Form->submit('Next >');
  7097. $expected = array(
  7098. 'div' => array('class' => 'submit'),
  7099. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  7100. '/div'
  7101. );
  7102. $this->assertTags($result, $expected);
  7103. $result = $this->Form->submit('Next >', array('escape' => false));
  7104. $expected = array(
  7105. 'div' => array('class' => 'submit'),
  7106. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  7107. '/div'
  7108. );
  7109. $this->assertTags($result, $expected);
  7110. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  7111. $expected = array(
  7112. 'div' => array('class' => 'submit'),
  7113. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  7114. '/div'
  7115. );
  7116. $this->assertTags($result, $expected);
  7117. $before = '--before--';
  7118. $after = '--after--';
  7119. $result = $this->Form->submit('Test', array('before' => $before));
  7120. $expected = array(
  7121. 'div' => array('class' => 'submit'),
  7122. '--before--',
  7123. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7124. '/div'
  7125. );
  7126. $this->assertTags($result, $expected);
  7127. $result = $this->Form->submit('Test', array('after' => $after));
  7128. $expected = array(
  7129. 'div' => array('class' => 'submit'),
  7130. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7131. '--after--',
  7132. '/div'
  7133. );
  7134. $this->assertTags($result, $expected);
  7135. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  7136. $expected = array(
  7137. 'div' => array('class' => 'submit'),
  7138. '--before--',
  7139. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7140. '--after--',
  7141. '/div'
  7142. );
  7143. $this->assertTags($result, $expected);
  7144. }
  7145. /**
  7146. * test image submit types.
  7147. *
  7148. * @return void
  7149. */
  7150. public function testSubmitImage() {
  7151. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7152. $expected = array(
  7153. 'div' => array('class' => 'submit'),
  7154. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  7155. '/div'
  7156. );
  7157. $this->assertTags($result, $expected);
  7158. $result = $this->Form->submit('/relative/cake.power.gif');
  7159. $expected = array(
  7160. 'div' => array('class' => 'submit'),
  7161. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  7162. '/div'
  7163. );
  7164. $this->assertTags($result, $expected);
  7165. $result = $this->Form->submit('cake.power.gif');
  7166. $expected = array(
  7167. 'div' => array('class' => 'submit'),
  7168. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7169. '/div'
  7170. );
  7171. $this->assertTags($result, $expected);
  7172. $result = $this->Form->submit('Not.an.image');
  7173. $expected = array(
  7174. 'div' => array('class' => 'submit'),
  7175. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7176. '/div'
  7177. );
  7178. $this->assertTags($result, $expected);
  7179. $after = '--after--';
  7180. $before = '--before--';
  7181. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  7182. $expected = array(
  7183. 'div' => array('class' => 'submit'),
  7184. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7185. '--after--',
  7186. '/div'
  7187. );
  7188. $this->assertTags($result, $expected);
  7189. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  7190. $expected = array(
  7191. 'div' => array('class' => 'submit'),
  7192. '--before--',
  7193. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7194. '/div'
  7195. );
  7196. $this->assertTags($result, $expected);
  7197. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  7198. $expected = array(
  7199. 'div' => array('class' => 'submit'),
  7200. '--before--',
  7201. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7202. '--after--',
  7203. '/div'
  7204. );
  7205. $this->assertTags($result, $expected);
  7206. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  7207. $expected = array(
  7208. 'div' => array('class' => 'submit'),
  7209. '--before--',
  7210. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7211. '--after--',
  7212. '/div'
  7213. );
  7214. $this->assertTags($result, $expected);
  7215. }
  7216. /**
  7217. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7218. * be submitted at a time.
  7219. *
  7220. * @return void
  7221. */
  7222. public function testSubmitUnlockedByDefault() {
  7223. $this->Form->request->params['_Token']['key'] = 'secured';
  7224. $this->Form->submit('Go go');
  7225. $this->Form->submit('Save', array('name' => 'save'));
  7226. $result = $this->Form->unlockField();
  7227. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  7228. }
  7229. /**
  7230. * Test submit image with timestamps.
  7231. *
  7232. * @return void
  7233. */
  7234. public function testSubmitImageTimestamp() {
  7235. Configure::write('Asset.timestamp', 'force');
  7236. $result = $this->Form->submit('cake.power.gif');
  7237. $expected = array(
  7238. 'div' => array('class' => 'submit'),
  7239. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  7240. '/div'
  7241. );
  7242. $this->assertTags($result, $expected);
  7243. }
  7244. /**
  7245. * test the create() method
  7246. *
  7247. * @return void
  7248. */
  7249. public function testCreate() {
  7250. $result = $this->Form->create('Contact');
  7251. $encoding = strtolower(Configure::read('App.encoding'));
  7252. $expected = array(
  7253. 'form' => array(
  7254. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7255. 'accept-charset' => $encoding
  7256. ),
  7257. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  7258. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7259. '/div'
  7260. );
  7261. $this->assertTags($result, $expected);
  7262. $result = $this->Form->create('Contact', array('type' => 'GET'));
  7263. $expected = array('form' => array(
  7264. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7265. 'accept-charset' => $encoding
  7266. ));
  7267. $this->assertTags($result, $expected);
  7268. $result = $this->Form->create('Contact', array('type' => 'get'));
  7269. $expected = array('form' => array(
  7270. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7271. 'accept-charset' => $encoding
  7272. ));
  7273. $this->assertTags($result, $expected);
  7274. $result = $this->Form->create('Contact', array('type' => 'put'));
  7275. $expected = array(
  7276. 'form' => array(
  7277. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7278. 'accept-charset' => $encoding
  7279. ),
  7280. 'div' => array('style' => 'display:none;'),
  7281. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7282. '/div'
  7283. );
  7284. $this->assertTags($result, $expected);
  7285. $result = $this->Form->create('Contact', array('type' => 'file'));
  7286. $expected = array(
  7287. 'form' => array(
  7288. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7289. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7290. ),
  7291. 'div' => array('style' => 'display:none;'),
  7292. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7293. '/div'
  7294. );
  7295. $this->assertTags($result, $expected);
  7296. $this->Form->request->data['Contact']['id'] = 1;
  7297. $this->Form->request->here = '/contacts/edit/1';
  7298. $this->Form->request['action'] = 'edit';
  7299. $result = $this->Form->create('Contact');
  7300. $expected = array(
  7301. 'form' => array(
  7302. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7303. 'accept-charset' => $encoding
  7304. ),
  7305. 'div' => array('style' => 'display:none;'),
  7306. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7307. '/div'
  7308. );
  7309. $this->assertTags($result, $expected);
  7310. $this->Form->request->data['Contact']['id'] = 1;
  7311. $this->Form->request->here = '/contacts/edit/1';
  7312. $this->Form->request['action'] = 'edit';
  7313. $result = $this->Form->create('Contact', array('type' => 'file'));
  7314. $expected = array(
  7315. 'form' => array(
  7316. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7317. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7318. ),
  7319. 'div' => array('style' => 'display:none;'),
  7320. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7321. '/div'
  7322. );
  7323. $this->assertTags($result, $expected);
  7324. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  7325. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  7326. $expected = array(
  7327. 'form' => array(
  7328. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  7329. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  7330. ),
  7331. 'div' => array('style' => 'display:none;'),
  7332. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7333. '/div'
  7334. );
  7335. $this->assertTags($result, $expected);
  7336. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  7337. $expected = array(
  7338. 'form' => array(
  7339. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  7340. 'accept-charset' => $encoding
  7341. ),
  7342. 'div' => array('style' => 'display:none;'),
  7343. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7344. '/div'
  7345. );
  7346. $this->assertTags($result, $expected);
  7347. $this->Form->request['action'] = 'add';
  7348. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  7349. $expected = array(
  7350. 'form' => array(
  7351. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  7352. 'accept-charset' => $encoding
  7353. ),
  7354. 'div' => array('style' => 'display:none;'),
  7355. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7356. '/div'
  7357. );
  7358. $this->assertTags($result, $expected);
  7359. $result = $this->Form->create('User', array('action' => 'login'));
  7360. $expected = array(
  7361. 'form' => array(
  7362. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  7363. 'accept-charset' => $encoding
  7364. ),
  7365. 'div' => array('style' => 'display:none;'),
  7366. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7367. '/div'
  7368. );
  7369. $this->assertTags($result, $expected);
  7370. $result = $this->Form->create('User', array('url' => '/users/login'));
  7371. $expected = array(
  7372. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  7373. 'div' => array('style' => 'display:none;'),
  7374. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7375. '/div'
  7376. );
  7377. $this->assertTags($result, $expected);
  7378. $this->Form->request['controller'] = 'pages';
  7379. $result = $this->Form->create('User', array('action' => 'signup'));
  7380. $expected = array(
  7381. 'form' => array(
  7382. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  7383. 'accept-charset' => $encoding
  7384. ),
  7385. 'div' => array('style' => 'display:none;'),
  7386. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7387. '/div'
  7388. );
  7389. $this->assertTags($result, $expected);
  7390. $this->Form->request->data = array();
  7391. $this->Form->request['controller'] = 'contacts';
  7392. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7393. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  7394. $expected = array(
  7395. 'form' => array(
  7396. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  7397. 'accept-charset' => 'utf-8'
  7398. ),
  7399. 'div' => array('style' => 'display:none;'),
  7400. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7401. '/div'
  7402. );
  7403. $this->assertTags($result, $expected);
  7404. }
  7405. /**
  7406. * Test the onsubmit option for create()
  7407. *
  7408. * @return void
  7409. */
  7410. public function testCreateOnSubmit() {
  7411. $this->Form->request->data = array();
  7412. $this->Form->request['controller'] = 'contacts';
  7413. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7414. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  7415. $expected = array(
  7416. 'form' => array(
  7417. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  7418. 'accept-charset' => 'utf-8'
  7419. ),
  7420. 'div' => array('style' => 'display:none;'),
  7421. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7422. '/div'
  7423. );
  7424. $this->assertTags($result, $expected);
  7425. $this->Form->request->data = array();
  7426. $this->Form->request['controller'] = 'contacts';
  7427. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7428. $result = $this->Form->create(array(
  7429. 'url' => array('action' => 'index', 'param'),
  7430. 'default' => false,
  7431. 'onsubmit' => 'someFunction();'
  7432. ));
  7433. $expected = array(
  7434. 'form' => array(
  7435. 'id' => 'ContactAddForm', 'method' => 'post',
  7436. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  7437. 'action' => '/contacts/index/param',
  7438. 'accept-charset' => 'utf-8'
  7439. ),
  7440. 'div' => array('style' => 'display:none;'),
  7441. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7442. '/div'
  7443. );
  7444. $this->assertTags($result, $expected);
  7445. }
  7446. /**
  7447. * test create() with automatic url generation
  7448. *
  7449. * @return void
  7450. */
  7451. public function testCreateAutoUrl() {
  7452. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  7453. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  7454. $this->Form->request->base = '/base_url';
  7455. $result = $this->Form->create('Contact');
  7456. $expected = array(
  7457. 'form' => array(
  7458. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  7459. 'accept-charset' => 'utf-8'
  7460. ),
  7461. 'div' => array('style' => 'display:none;'),
  7462. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7463. '/div'
  7464. );
  7465. $this->assertTags($result, $expected);
  7466. $this->Form->request['action'] = 'delete';
  7467. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  7468. $this->Form->request->base = '/base_url';
  7469. $result = $this->Form->create('Contact');
  7470. $expected = array(
  7471. 'form' => array(
  7472. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  7473. 'accept-charset' => 'utf-8'
  7474. ),
  7475. 'div' => array('style' => 'display:none;'),
  7476. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7477. '/div'
  7478. );
  7479. $this->assertTags($result, $expected);
  7480. }
  7481. /**
  7482. * test create() with a custom route
  7483. *
  7484. * @return void
  7485. */
  7486. public function testCreateCustomRoute() {
  7487. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  7488. $encoding = strtolower(Configure::read('App.encoding'));
  7489. $result = $this->Form->create('User', array('action' => 'login'));
  7490. $expected = array(
  7491. 'form' => array(
  7492. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  7493. 'accept-charset' => $encoding
  7494. ),
  7495. 'div' => array('style' => 'display:none;'),
  7496. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7497. '/div'
  7498. );
  7499. $this->assertTags($result, $expected);
  7500. }
  7501. /**
  7502. * test that inputDefaults are stored and used.
  7503. *
  7504. * @return void
  7505. */
  7506. public function testCreateWithInputDefaults() {
  7507. $this->Form->create('User', array(
  7508. 'inputDefaults' => array(
  7509. 'div' => false,
  7510. 'label' => false,
  7511. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  7512. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  7513. )
  7514. ));
  7515. $result = $this->Form->input('username');
  7516. $expected = array(
  7517. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  7518. );
  7519. $this->assertTags($result, $expected);
  7520. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  7521. $expected = array(
  7522. 'div' => array('class' => 'input text'),
  7523. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  7524. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7525. '/div'
  7526. );
  7527. $this->assertTags($result, $expected);
  7528. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7529. $expected = array(
  7530. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7531. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7532. );
  7533. $this->assertTags($result, $expected);
  7534. $this->Form->create('User', array(
  7535. 'inputDefaults' => array(
  7536. 'div' => false,
  7537. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7538. )
  7539. ));
  7540. $result = $this->Form->input('username', array('div' => true));
  7541. $expected = array(
  7542. 'div' => array('class' => 'input text'),
  7543. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7544. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7545. '/div'
  7546. );
  7547. $this->assertTags($result, $expected);
  7548. }
  7549. /**
  7550. * test automatic accept-charset overriding
  7551. *
  7552. * @return void
  7553. */
  7554. public function testCreateWithAcceptCharset() {
  7555. $result = $this->Form->create('UserForm', array(
  7556. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7557. )
  7558. );
  7559. $expected = array(
  7560. 'form' => array(
  7561. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7562. 'accept-charset' => 'iso-8859-1'
  7563. ),
  7564. 'div' => array('style' => 'display:none;'),
  7565. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7566. '/div'
  7567. );
  7568. $this->assertTags($result, $expected);
  7569. }
  7570. /**
  7571. * Test base form URL when url param is passed with multiple parameters (&)
  7572. *
  7573. */
  7574. public function testCreateQuerystringrequest() {
  7575. $encoding = strtolower(Configure::read('App.encoding'));
  7576. $result = $this->Form->create('Contact', array(
  7577. 'type' => 'post',
  7578. 'escape' => false,
  7579. 'url' => array(
  7580. 'controller' => 'controller',
  7581. 'action' => 'action',
  7582. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7583. )
  7584. ));
  7585. $expected = array(
  7586. 'form' => array(
  7587. 'id' => 'ContactAddForm',
  7588. 'method' => 'post',
  7589. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7590. 'accept-charset' => $encoding
  7591. ),
  7592. 'div' => array('style' => 'display:none;'),
  7593. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7594. '/div'
  7595. );
  7596. $this->assertTags($result, $expected);
  7597. $result = $this->Form->create('Contact', array(
  7598. 'type' => 'post',
  7599. 'url' => array(
  7600. 'controller' => 'controller',
  7601. 'action' => 'action',
  7602. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7603. )
  7604. ));
  7605. $expected = array(
  7606. 'form' => array(
  7607. 'id' => 'ContactAddForm',
  7608. 'method' => 'post',
  7609. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7610. 'accept-charset' => $encoding
  7611. ),
  7612. 'div' => array('style' => 'display:none;'),
  7613. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7614. '/div'
  7615. );
  7616. $this->assertTags($result, $expected);
  7617. }
  7618. /**
  7619. * test that create() doesn't cause errors by multiple id's being in the primary key
  7620. * as could happen with multiple select or checkboxes.
  7621. *
  7622. * @return void
  7623. */
  7624. public function testCreateWithMultipleIdInData() {
  7625. $encoding = strtolower(Configure::read('App.encoding'));
  7626. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7627. $result = $this->Form->create('Contact');
  7628. $expected = array(
  7629. 'form' => array(
  7630. 'id' => 'ContactAddForm',
  7631. 'method' => 'post',
  7632. 'action' => '/contacts/add',
  7633. 'accept-charset' => $encoding
  7634. ),
  7635. 'div' => array('style' => 'display:none;'),
  7636. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7637. '/div'
  7638. );
  7639. $this->assertTags($result, $expected);
  7640. }
  7641. /**
  7642. * test that create() doesn't add in extra passed params.
  7643. *
  7644. * @return void
  7645. */
  7646. public function testCreatePassedArgs() {
  7647. $encoding = strtolower(Configure::read('App.encoding'));
  7648. $this->Form->request->data['Contact']['id'] = 1;
  7649. $result = $this->Form->create('Contact', array(
  7650. 'type' => 'post',
  7651. 'escape' => false,
  7652. 'url' => array(
  7653. 'action' => 'edit',
  7654. 'myparam'
  7655. )
  7656. ));
  7657. $expected = array(
  7658. 'form' => array(
  7659. 'id' => 'ContactAddForm',
  7660. 'method' => 'post',
  7661. 'action' => '/contacts/edit/myparam',
  7662. 'accept-charset' => $encoding
  7663. ),
  7664. 'div' => array('style' => 'display:none;'),
  7665. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7666. '/div'
  7667. );
  7668. $this->assertTags($result, $expected);
  7669. }
  7670. /**
  7671. * test creating a get form, and get form inputs.
  7672. *
  7673. * @return void
  7674. */
  7675. public function testGetFormCreate() {
  7676. $encoding = strtolower(Configure::read('App.encoding'));
  7677. $result = $this->Form->create('Contact', array('type' => 'get'));
  7678. $this->assertTags($result, array('form' => array(
  7679. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7680. 'accept-charset' => $encoding
  7681. )));
  7682. $result = $this->Form->text('Contact.name');
  7683. $this->assertTags($result, array('input' => array(
  7684. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7685. )));
  7686. $result = $this->Form->password('password');
  7687. $this->assertTags($result, array('input' => array(
  7688. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7689. )));
  7690. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7691. $result = $this->Form->text('user_form');
  7692. $this->assertTags($result, array('input' => array(
  7693. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7694. )));
  7695. }
  7696. /**
  7697. * test get form, and inputs when the model param is false
  7698. *
  7699. * @return void
  7700. */
  7701. public function testGetFormWithFalseModel() {
  7702. $encoding = strtolower(Configure::read('App.encoding'));
  7703. $this->Form->request['controller'] = 'contact_test';
  7704. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7705. $expected = array('form' => array(
  7706. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7707. 'accept-charset' => $encoding
  7708. ));
  7709. $this->assertTags($result, $expected);
  7710. $result = $this->Form->text('reason');
  7711. $expected = array(
  7712. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7713. );
  7714. $this->assertTags($result, $expected);
  7715. }
  7716. /**
  7717. * test that datetime() works with GET style forms.
  7718. *
  7719. * @return void
  7720. */
  7721. public function testDateTimeWithGetForms() {
  7722. extract($this->dateRegex);
  7723. $this->Form->create('Contact', array('type' => 'get'));
  7724. $result = $this->Form->datetime('created');
  7725. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7726. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7727. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7728. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7729. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7730. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7731. }
  7732. /**
  7733. * testEditFormWithData method
  7734. *
  7735. * test auto populating form elements from submitted data.
  7736. *
  7737. * @return void
  7738. */
  7739. public function testEditFormWithData() {
  7740. $this->Form->request->data = array('Person' => array(
  7741. 'id' => 1,
  7742. 'first_name' => 'Nate',
  7743. 'last_name' => 'Abele',
  7744. 'email' => 'nate@example.com'
  7745. ));
  7746. $this->Form->request->addParams(array(
  7747. 'models' => array('Person'),
  7748. 'controller' => 'people',
  7749. 'action' => 'add'
  7750. ));
  7751. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7752. $this->Form->create();
  7753. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7754. $expected = array(
  7755. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7756. 'select' => array(
  7757. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7758. ),
  7759. array('option' => array('value' => 1)), 'Nate', '/option',
  7760. array('option' => array('value' => 2)), 'Garrett', '/option',
  7761. array('option' => array('value' => 3)), 'Larry', '/option',
  7762. '/select'
  7763. );
  7764. $this->assertTags($result, $expected);
  7765. }
  7766. /**
  7767. * Test that required fields are created for various types of validation.
  7768. *
  7769. * @return void
  7770. */
  7771. public function testFormInputRequiredDetection() {
  7772. $this->Form->create('Contact');
  7773. $result = $this->Form->input('Contact.non_existing');
  7774. $expected = array(
  7775. 'div' => array('class' => 'input text'),
  7776. 'label' => array('for' => 'ContactNonExisting'),
  7777. 'Non Existing',
  7778. '/label',
  7779. 'input' => array(
  7780. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7781. 'id' => 'ContactNonExisting'
  7782. ),
  7783. '/div'
  7784. );
  7785. $this->assertTags($result, $expected);
  7786. $result = $this->Form->input('Contact.imrequired');
  7787. $expected = array(
  7788. 'div' => array('class' => 'input text required'),
  7789. 'label' => array('for' => 'ContactImrequired'),
  7790. 'Imrequired',
  7791. '/label',
  7792. 'input' => array(
  7793. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7794. 'id' => 'ContactImrequired',
  7795. 'required' => 'required'
  7796. ),
  7797. '/div'
  7798. );
  7799. $this->assertTags($result, $expected);
  7800. $result = $this->Form->input('Contact.imalsorequired');
  7801. $expected = array(
  7802. 'div' => array('class' => 'input text required'),
  7803. 'label' => array('for' => 'ContactImalsorequired'),
  7804. 'Imalsorequired',
  7805. '/label',
  7806. 'input' => array(
  7807. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7808. 'id' => 'ContactImalsorequired',
  7809. 'required' => 'required'
  7810. ),
  7811. '/div'
  7812. );
  7813. $this->assertTags($result, $expected);
  7814. $result = $this->Form->input('Contact.imrequiredtoo');
  7815. $expected = array(
  7816. 'div' => array('class' => 'input text required'),
  7817. 'label' => array('for' => 'ContactImrequiredtoo'),
  7818. 'Imrequiredtoo',
  7819. '/label',
  7820. 'input' => array(
  7821. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7822. 'id' => 'ContactImrequiredtoo',
  7823. 'required' => 'required'
  7824. ),
  7825. '/div'
  7826. );
  7827. $this->assertTags($result, $expected);
  7828. $result = $this->Form->input('Contact.required_one');
  7829. $expected = array(
  7830. 'div' => array('class' => 'input text required'),
  7831. 'label' => array('for' => 'ContactRequiredOne'),
  7832. 'Required One',
  7833. '/label',
  7834. 'input' => array(
  7835. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7836. 'id' => 'ContactRequiredOne',
  7837. 'required' => 'required'
  7838. ),
  7839. '/div'
  7840. );
  7841. $this->assertTags($result, $expected);
  7842. $result = $this->Form->input('Contact.string_required');
  7843. $expected = array(
  7844. 'div' => array('class' => 'input text required'),
  7845. 'label' => array('for' => 'ContactStringRequired'),
  7846. 'String Required',
  7847. '/label',
  7848. 'input' => array(
  7849. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7850. 'id' => 'ContactStringRequired',
  7851. 'required' => 'required'
  7852. ),
  7853. '/div'
  7854. );
  7855. $this->assertTags($result, $expected);
  7856. $result = $this->Form->input('Contact.imnotrequired');
  7857. $expected = array(
  7858. 'div' => array('class' => 'input text'),
  7859. 'label' => array('for' => 'ContactImnotrequired'),
  7860. 'Imnotrequired',
  7861. '/label',
  7862. 'input' => array(
  7863. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7864. 'id' => 'ContactImnotrequired'
  7865. ),
  7866. '/div'
  7867. );
  7868. $this->assertTags($result, $expected);
  7869. $result = $this->Form->input('Contact.imalsonotrequired');
  7870. $expected = array(
  7871. 'div' => array('class' => 'input text'),
  7872. 'label' => array('for' => 'ContactImalsonotrequired'),
  7873. 'Imalsonotrequired',
  7874. '/label',
  7875. 'input' => array(
  7876. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7877. 'id' => 'ContactImalsonotrequired'
  7878. ),
  7879. '/div'
  7880. );
  7881. $this->assertTags($result, $expected);
  7882. $result = $this->Form->input('Contact.imalsonotrequired2');
  7883. $expected = array(
  7884. 'div' => array('class' => 'input text'),
  7885. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7886. 'Imalsonotrequired2',
  7887. '/label',
  7888. 'input' => array(
  7889. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7890. 'id' => 'ContactImalsonotrequired2'
  7891. ),
  7892. '/div'
  7893. );
  7894. $this->assertTags($result, $expected);
  7895. $result = $this->Form->input('Contact.imnotrequiredeither');
  7896. $expected = array(
  7897. 'div' => array('class' => 'input text'),
  7898. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7899. 'Imnotrequiredeither',
  7900. '/label',
  7901. 'input' => array(
  7902. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7903. 'id' => 'ContactImnotrequiredeither'
  7904. ),
  7905. '/div'
  7906. );
  7907. $this->assertTags($result, $expected);
  7908. $result = $this->Form->input('Contact.iamrequiredalways');
  7909. $expected = array(
  7910. 'div' => array('class' => 'input text required'),
  7911. 'label' => array('for' => 'ContactIamrequiredalways'),
  7912. 'Iamrequiredalways',
  7913. '/label',
  7914. 'input' => array(
  7915. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7916. 'id' => 'ContactIamrequiredalways',
  7917. 'required' => 'required'
  7918. ),
  7919. '/div'
  7920. );
  7921. $this->assertTags($result, $expected);
  7922. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7923. $expected = array(
  7924. 'div' => array('class' => 'input checkbox required'),
  7925. array('input' => array(
  7926. 'type' => 'hidden',
  7927. 'name' => 'data[Contact][boolean_field]',
  7928. 'id' => 'ContactBooleanField_',
  7929. 'value' => '0'
  7930. )),
  7931. array('input' => array(
  7932. 'type' => 'checkbox',
  7933. 'name' => 'data[Contact][boolean_field]',
  7934. 'value' => '1',
  7935. 'id' => 'ContactBooleanField'
  7936. )),
  7937. 'label' => array('for' => 'ContactBooleanField'),
  7938. 'Boolean Field',
  7939. '/label',
  7940. '/div'
  7941. );
  7942. $this->assertTags($result, $expected);
  7943. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7944. $expected = array(
  7945. 'div' => array('class' => 'input checkbox required'),
  7946. array('input' => array(
  7947. 'type' => 'hidden',
  7948. 'name' => 'data[Contact][boolean_field]',
  7949. 'id' => 'ContactBooleanField_',
  7950. 'value' => '0'
  7951. )),
  7952. array('input' => array(
  7953. 'type' => 'checkbox',
  7954. 'name' => 'data[Contact][boolean_field]',
  7955. 'value' => '1',
  7956. 'id' => 'ContactBooleanField',
  7957. 'required' => 'required'
  7958. )),
  7959. 'label' => array('for' => 'ContactBooleanField'),
  7960. 'Boolean Field',
  7961. '/label',
  7962. '/div'
  7963. );
  7964. $this->assertTags($result, $expected);
  7965. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  7966. $expected = array(
  7967. 'div' => array('class' => 'input file required'),
  7968. 'label' => array('for' => 'ContactIamrequiredalways'),
  7969. 'Iamrequiredalways',
  7970. '/label',
  7971. 'input' => array(
  7972. 'type' => 'file',
  7973. 'name' => 'data[Contact][iamrequiredalways]',
  7974. 'id' => 'ContactIamrequiredalways',
  7975. 'required' => 'required'
  7976. ),
  7977. '/div'
  7978. );
  7979. $this->assertTags($result, $expected);
  7980. }
  7981. /**
  7982. * Test that required fields are created when only using ModelValidator::add().
  7983. *
  7984. * @return void
  7985. */
  7986. public function testFormInputRequiredDetectionModelValidator() {
  7987. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  7988. $this->Form->create('ContactTag');
  7989. $result = $this->Form->input('ContactTag.iwillberequired');
  7990. $expected = array(
  7991. 'div' => array('class' => 'input text required'),
  7992. 'label' => array('for' => 'ContactTagIwillberequired'),
  7993. 'Iwillberequired',
  7994. '/label',
  7995. 'input' => array(
  7996. 'name' => 'data[ContactTag][iwillberequired]',
  7997. 'type' => 'text',
  7998. 'id' => 'ContactTagIwillberequired',
  7999. 'required' => 'required'
  8000. ),
  8001. '/div'
  8002. );
  8003. $this->assertTags($result, $expected);
  8004. }
  8005. /**
  8006. * testFormMagicInput method
  8007. *
  8008. * @return void
  8009. */
  8010. public function testFormMagicInput() {
  8011. $encoding = strtolower(Configure::read('App.encoding'));
  8012. $result = $this->Form->create('Contact');
  8013. $expected = array(
  8014. 'form' => array(
  8015. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8016. 'accept-charset' => $encoding
  8017. ),
  8018. 'div' => array('style' => 'display:none;'),
  8019. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8020. '/div'
  8021. );
  8022. $this->assertTags($result, $expected);
  8023. $result = $this->Form->input('name');
  8024. $expected = array(
  8025. 'div' => array('class' => 'input text'),
  8026. 'label' => array('for' => 'ContactName'),
  8027. 'Name',
  8028. '/label',
  8029. 'input' => array(
  8030. 'type' => 'text', 'name' => 'data[Contact][name]',
  8031. 'id' => 'ContactName', 'maxlength' => '255'
  8032. ),
  8033. '/div'
  8034. );
  8035. $this->assertTags($result, $expected);
  8036. $result = $this->Form->input('non_existing_field_in_contact_model');
  8037. $expected = array(
  8038. 'div' => array('class' => 'input text'),
  8039. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  8040. 'Non Existing Field In Contact Model',
  8041. '/label',
  8042. 'input' => array(
  8043. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  8044. 'id' => 'ContactNonExistingFieldInContactModel'
  8045. ),
  8046. '/div'
  8047. );
  8048. $this->assertTags($result, $expected);
  8049. $result = $this->Form->input('Address.street');
  8050. $expected = array(
  8051. 'div' => array('class' => 'input text'),
  8052. 'label' => array('for' => 'AddressStreet'),
  8053. 'Street',
  8054. '/label',
  8055. 'input' => array(
  8056. 'type' => 'text', 'name' => 'data[Address][street]',
  8057. 'id' => 'AddressStreet'
  8058. ),
  8059. '/div'
  8060. );
  8061. $this->assertTags($result, $expected);
  8062. $result = $this->Form->input('Address.non_existing_field_in_model');
  8063. $expected = array(
  8064. 'div' => array('class' => 'input text'),
  8065. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  8066. 'Non Existing Field In Model',
  8067. '/label',
  8068. 'input' => array(
  8069. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  8070. 'id' => 'AddressNonExistingFieldInModel'
  8071. ),
  8072. '/div'
  8073. );
  8074. $this->assertTags($result, $expected);
  8075. $result = $this->Form->input('name', array('div' => false));
  8076. $expected = array(
  8077. 'label' => array('for' => 'ContactName'),
  8078. 'Name',
  8079. '/label',
  8080. 'input' => array(
  8081. 'type' => 'text', 'name' => 'data[Contact][name]',
  8082. 'id' => 'ContactName', 'maxlength' => '255'
  8083. )
  8084. );
  8085. $this->assertTags($result, $expected);
  8086. extract($this->dateRegex);
  8087. $now = strtotime('now');
  8088. $result = $this->Form->input('Contact.published', array('div' => false));
  8089. $expected = array(
  8090. 'label' => array('for' => 'ContactPublishedMonth'),
  8091. 'Published',
  8092. '/label',
  8093. array('select' => array(
  8094. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  8095. )),
  8096. $monthsRegex,
  8097. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  8098. date('F', $now),
  8099. '/option',
  8100. '*/select',
  8101. '-',
  8102. array('select' => array(
  8103. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  8104. )),
  8105. $daysRegex,
  8106. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  8107. date('j', $now),
  8108. '/option',
  8109. '*/select',
  8110. '-',
  8111. array('select' => array(
  8112. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  8113. )),
  8114. $yearsRegex,
  8115. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  8116. date('Y', $now),
  8117. '*/select'
  8118. );
  8119. $this->assertTags($result, $expected);
  8120. $result = $this->Form->input('Contact.updated', array('div' => false));
  8121. $expected = array(
  8122. 'label' => array('for' => 'ContactUpdatedMonth'),
  8123. 'Updated',
  8124. '/label',
  8125. array('select' => array(
  8126. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  8127. )),
  8128. $monthsRegex,
  8129. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  8130. date('F', $now),
  8131. '/option',
  8132. '*/select',
  8133. '-',
  8134. array('select' => array(
  8135. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  8136. )),
  8137. $daysRegex,
  8138. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  8139. date('j', $now),
  8140. '/option',
  8141. '*/select',
  8142. '-',
  8143. array('select' => array(
  8144. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  8145. )),
  8146. $yearsRegex,
  8147. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  8148. date('Y', $now),
  8149. '*/select'
  8150. );
  8151. $this->assertTags($result, $expected);
  8152. $result = $this->Form->input('UserForm.stuff');
  8153. $expected = array(
  8154. 'div' => array('class' => 'input text'),
  8155. 'label' => array('for' => 'UserFormStuff'),
  8156. 'Stuff',
  8157. '/label',
  8158. 'input' => array(
  8159. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  8160. 'id' => 'UserFormStuff', 'maxlength' => 10
  8161. ),
  8162. '/div'
  8163. );
  8164. $this->assertTags($result, $expected);
  8165. }
  8166. /**
  8167. * testForMagicInputNonExistingNorValidated method
  8168. *
  8169. * @return void
  8170. */
  8171. public function testForMagicInputNonExistingNorValidated() {
  8172. $encoding = strtolower(Configure::read('App.encoding'));
  8173. $result = $this->Form->create('Contact');
  8174. $expected = array(
  8175. 'form' => array(
  8176. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8177. 'accept-charset' => $encoding
  8178. ),
  8179. 'div' => array('style' => 'display:none;'),
  8180. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8181. '/div'
  8182. );
  8183. $this->assertTags($result, $expected);
  8184. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8185. $expected = array(
  8186. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8187. 'Non Existing Nor Validated',
  8188. '/label',
  8189. 'input' => array(
  8190. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8191. 'id' => 'ContactNonExistingNorValidated'
  8192. )
  8193. );
  8194. $this->assertTags($result, $expected);
  8195. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  8196. 'div' => false, 'value' => 'my value'
  8197. ));
  8198. $expected = array(
  8199. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8200. 'Non Existing Nor Validated',
  8201. '/label',
  8202. 'input' => array(
  8203. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8204. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  8205. )
  8206. );
  8207. $this->assertTags($result, $expected);
  8208. $this->Form->request->data = array(
  8209. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  8210. ));
  8211. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8212. $expected = array(
  8213. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8214. 'Non Existing Nor Validated',
  8215. '/label',
  8216. 'input' => array(
  8217. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8218. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  8219. )
  8220. );
  8221. $this->assertTags($result, $expected);
  8222. }
  8223. /**
  8224. * testFormMagicInputLabel method
  8225. *
  8226. * @return void
  8227. */
  8228. public function testFormMagicInputLabel() {
  8229. $encoding = strtolower(Configure::read('App.encoding'));
  8230. $result = $this->Form->create('Contact');
  8231. $expected = array(
  8232. 'form' => array(
  8233. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8234. 'accept-charset' => $encoding
  8235. ),
  8236. 'div' => array('style' => 'display:none;'),
  8237. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8238. '/div'
  8239. );
  8240. $this->assertTags($result, $expected);
  8241. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  8242. $this->assertTags($result, array('input' => array(
  8243. 'name' => 'data[Contact][name]', 'type' => 'text',
  8244. 'id' => 'ContactName', 'maxlength' => '255')
  8245. ));
  8246. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  8247. $expected = array(
  8248. 'label' => array('for' => 'ContactName'),
  8249. 'My label',
  8250. '/label',
  8251. 'input' => array(
  8252. 'type' => 'text', 'name' => 'data[Contact][name]',
  8253. 'id' => 'ContactName', 'maxlength' => '255'
  8254. )
  8255. );
  8256. $this->assertTags($result, $expected);
  8257. $result = $this->Form->input('Contact.name', array(
  8258. 'div' => false, 'label' => array('class' => 'mandatory')
  8259. ));
  8260. $expected = array(
  8261. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8262. 'Name',
  8263. '/label',
  8264. 'input' => array(
  8265. 'type' => 'text', 'name' => 'data[Contact][name]',
  8266. 'id' => 'ContactName', 'maxlength' => '255'
  8267. )
  8268. );
  8269. $this->assertTags($result, $expected);
  8270. $result = $this->Form->input('Contact.name', array(
  8271. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  8272. ));
  8273. $expected = array(
  8274. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8275. 'My label',
  8276. '/label',
  8277. 'input' => array(
  8278. 'type' => 'text', 'name' => 'data[Contact][name]',
  8279. 'id' => 'ContactName', 'maxlength' => '255'
  8280. )
  8281. );
  8282. $this->assertTags($result, $expected);
  8283. $result = $this->Form->input('Contact.name', array(
  8284. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  8285. ));
  8286. $expected = array(
  8287. 'label' => array('for' => 'my_id'),
  8288. 'Name',
  8289. '/label',
  8290. 'input' => array(
  8291. 'type' => 'text', 'name' => 'data[Contact][name]',
  8292. 'id' => 'my_id', 'maxlength' => '255'
  8293. )
  8294. );
  8295. $this->assertTags($result, $expected);
  8296. $result = $this->Form->input('1.id');
  8297. $this->assertTags($result, array('input' => array(
  8298. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  8299. 'id' => 'Contact1Id'
  8300. )));
  8301. $result = $this->Form->input("1.name");
  8302. $expected = array(
  8303. 'div' => array('class' => 'input text'),
  8304. 'label' => array('for' => 'Contact1Name'),
  8305. 'Name',
  8306. '/label',
  8307. 'input' => array(
  8308. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  8309. 'id' => 'Contact1Name', 'maxlength' => '255'
  8310. ),
  8311. '/div'
  8312. );
  8313. $this->assertTags($result, $expected);
  8314. $result = $this->Form->input('Contact.1.id');
  8315. $this->assertTags($result, array(
  8316. 'input' => array(
  8317. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  8318. 'id' => 'Contact1Id'
  8319. )
  8320. ));
  8321. $result = $this->Form->input("Model.1.name");
  8322. $expected = array(
  8323. 'div' => array('class' => 'input text'),
  8324. 'label' => array('for' => 'Model1Name'),
  8325. 'Name',
  8326. '/label',
  8327. 'input' => array(
  8328. 'type' => 'text', 'name' => 'data[Model][1][name]',
  8329. 'id' => 'Model1Name'
  8330. ),
  8331. '/div'
  8332. );
  8333. $this->assertTags($result, $expected);
  8334. }
  8335. /**
  8336. * testFormEnd method
  8337. *
  8338. * @return void
  8339. */
  8340. public function testFormEnd() {
  8341. $this->assertEquals('</form>', $this->Form->end());
  8342. $result = $this->Form->end('', array('form' => 'MyTestFormWithSupportForSecurityComponent'));
  8343. $expected = array(
  8344. 'div' => array('class' => 'submit'),
  8345. 'input' => array('type' => 'submit', 'value' => '', 'form' => 'MyTestFormWithSupportForSecurityComponent'),
  8346. '/div',
  8347. '/form'
  8348. );
  8349. $this->assertTags($result, $expected);
  8350. $result = $this->Form->end('');
  8351. $expected = array(
  8352. 'div' => array('class' => 'submit'),
  8353. 'input' => array('type' => 'submit', 'value' => ''),
  8354. '/div',
  8355. '/form'
  8356. );
  8357. $this->assertTags($result, $expected);
  8358. $result = $this->Form->end(array('label' => ''));
  8359. $expected = array(
  8360. 'div' => array('class' => 'submit'),
  8361. 'input' => array('type' => 'submit', 'value' => ''),
  8362. '/div',
  8363. '/form'
  8364. );
  8365. $this->assertTags($result, $expected);
  8366. $result = $this->Form->end('save');
  8367. $expected = array(
  8368. 'div' => array('class' => 'submit'),
  8369. 'input' => array('type' => 'submit', 'value' => 'save'),
  8370. '/div',
  8371. '/form'
  8372. );
  8373. $this->assertTags($result, $expected);
  8374. $result = $this->Form->end(array('label' => 'save'));
  8375. $expected = array(
  8376. 'div' => array('class' => 'submit'),
  8377. 'input' => array('type' => 'submit', 'value' => 'save'),
  8378. '/div',
  8379. '/form'
  8380. );
  8381. $this->assertTags($result, $expected);
  8382. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  8383. $expected = array(
  8384. 'div' => array('class' => 'submit'),
  8385. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8386. '/div',
  8387. '/form'
  8388. );
  8389. $this->assertTags($result, $expected);
  8390. $result = $this->Form->end(array('name' => 'Whatever'));
  8391. $expected = array(
  8392. 'div' => array('class' => 'submit'),
  8393. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  8394. '/div',
  8395. '/form'
  8396. );
  8397. $this->assertTags($result, $expected);
  8398. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  8399. $expected = array(
  8400. 'div' => array('class' => 'good'),
  8401. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8402. '/div',
  8403. '/form'
  8404. );
  8405. $this->assertTags($result, $expected);
  8406. $result = $this->Form->end(array(
  8407. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  8408. ));
  8409. $expected = array(
  8410. 'div' => array('class' => 'good'),
  8411. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8412. '/div',
  8413. '/form'
  8414. );
  8415. $this->assertTags($result, $expected);
  8416. }
  8417. /**
  8418. * testMultipleFormWithIdFields method
  8419. *
  8420. * @return void
  8421. */
  8422. public function testMultipleFormWithIdFields() {
  8423. $this->Form->create('UserForm');
  8424. $result = $this->Form->input('id');
  8425. $this->assertTags($result, array('input' => array(
  8426. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  8427. )));
  8428. $result = $this->Form->input('ValidateItem.id');
  8429. $this->assertTags($result, array('input' => array(
  8430. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  8431. 'id' => 'ValidateItemId'
  8432. )));
  8433. $result = $this->Form->input('ValidateUser.id');
  8434. $this->assertTags($result, array('input' => array(
  8435. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  8436. 'id' => 'ValidateUserId'
  8437. )));
  8438. }
  8439. /**
  8440. * testDbLessModel method
  8441. *
  8442. * @return void
  8443. */
  8444. public function testDbLessModel() {
  8445. $this->Form->create('TestMail');
  8446. $result = $this->Form->input('name');
  8447. $expected = array(
  8448. 'div' => array('class' => 'input text'),
  8449. 'label' => array('for' => 'TestMailName'),
  8450. 'Name',
  8451. '/label',
  8452. 'input' => array(
  8453. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8454. 'id' => 'TestMailName'
  8455. ),
  8456. '/div'
  8457. );
  8458. $this->assertTags($result, $expected);
  8459. ClassRegistry::init('TestMail');
  8460. $this->Form->create('TestMail');
  8461. $result = $this->Form->input('name');
  8462. $expected = array(
  8463. 'div' => array('class' => 'input text'),
  8464. 'label' => array('for' => 'TestMailName'),
  8465. 'Name',
  8466. '/label',
  8467. 'input' => array(
  8468. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8469. 'id' => 'TestMailName'
  8470. ),
  8471. '/div'
  8472. );
  8473. $this->assertTags($result, $expected);
  8474. }
  8475. /**
  8476. * testBrokenness method
  8477. *
  8478. * @return void
  8479. */
  8480. public function testBrokenness() {
  8481. /*
  8482. * #4 This test has two parents and four children. By default (as of r7117) both
  8483. * parents are show but the first parent is missing a child. This is the inconsistency
  8484. * in the default behaviour - one parent has all children, the other does not - dependent
  8485. * on the data values.
  8486. */
  8487. $result = $this->Form->select('Model.field', array(
  8488. 'Fred' => array(
  8489. 'freds_son_1' => 'Fred',
  8490. 'freds_son_2' => 'Freddie'
  8491. ),
  8492. 'Bert' => array(
  8493. 'berts_son_1' => 'Albert',
  8494. 'berts_son_2' => 'Bertie')
  8495. ),
  8496. array('showParents' => true, 'empty' => false)
  8497. );
  8498. $expected = array(
  8499. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8500. array('optgroup' => array('label' => 'Fred')),
  8501. array('option' => array('value' => 'freds_son_1')),
  8502. 'Fred',
  8503. '/option',
  8504. array('option' => array('value' => 'freds_son_2')),
  8505. 'Freddie',
  8506. '/option',
  8507. '/optgroup',
  8508. array('optgroup' => array('label' => 'Bert')),
  8509. array('option' => array('value' => 'berts_son_1')),
  8510. 'Albert',
  8511. '/option',
  8512. array('option' => array('value' => 'berts_son_2')),
  8513. 'Bertie',
  8514. '/option',
  8515. '/optgroup',
  8516. '/select'
  8517. );
  8518. $this->assertTags($result, $expected);
  8519. /*
  8520. * #2 This is structurally identical to the test above (#1) - only the parent name has
  8521. * changed, so we should expect the same select list data, just with a different name
  8522. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  8523. * This is where data corruption can occur, because when a select value is missing from
  8524. * a list a form will substitute the first value in the list - without the user knowing.
  8525. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  8526. * affect the availability of 3 => 'Three' as a valid option.
  8527. */
  8528. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  8529. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  8530. ));
  8531. $result = $this->Form->select(
  8532. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  8533. );
  8534. $expected = array(
  8535. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8536. array('option' => array('value' => 1)),
  8537. 'One',
  8538. '/option',
  8539. array('option' => array('value' => 2)),
  8540. 'Two',
  8541. '/option',
  8542. array('optgroup' => array('label' => 'Three')),
  8543. array('option' => array('value' => 3)),
  8544. 'Three',
  8545. '/option',
  8546. array('option' => array('value' => 4)),
  8547. 'Four',
  8548. '/option',
  8549. array('option' => array('value' => 5)),
  8550. 'Five',
  8551. '/option',
  8552. '/optgroup',
  8553. '/select'
  8554. );
  8555. $this->assertTags($result, $expected);
  8556. }
  8557. /**
  8558. * Test the generation of fields for a multi record form.
  8559. *
  8560. * @return void
  8561. */
  8562. public function testMultiRecordForm() {
  8563. $this->Form->create('ValidateProfile');
  8564. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8565. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8566. $expected = array(
  8567. 'div' => array('class' => 'input textarea'),
  8568. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8569. 'Name',
  8570. '/label',
  8571. 'textarea' => array(
  8572. 'id' => 'ValidateProfile1ValidateItem2Name',
  8573. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  8574. 'cols' => 30,
  8575. 'rows' => 6
  8576. ),
  8577. 'Value',
  8578. '/textarea',
  8579. '/div'
  8580. );
  8581. $this->assertTags($result, $expected);
  8582. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8583. $expected = array(
  8584. 'div' => array('class' => 'input date'),
  8585. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8586. 'Created',
  8587. '/label',
  8588. array('select' => array(
  8589. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  8590. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8591. )
  8592. ),
  8593. array('option' => array('value' => '')), '/option',
  8594. $this->dateRegex['monthsRegex'],
  8595. '/select', '-',
  8596. array('select' => array(
  8597. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  8598. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8599. )
  8600. ),
  8601. array('option' => array('value' => '')), '/option',
  8602. $this->dateRegex['daysRegex'],
  8603. '/select', '-',
  8604. array('select' => array(
  8605. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  8606. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8607. )
  8608. ),
  8609. array('option' => array('value' => '')), '/option',
  8610. $this->dateRegex['yearsRegex'],
  8611. '/select',
  8612. '/div'
  8613. );
  8614. $this->assertTags($result, $expected);
  8615. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8616. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8617. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8618. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8619. $expected = array(
  8620. 'div' => array('class' => 'input select error'),
  8621. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8622. 'Profile',
  8623. '/label',
  8624. 'select' => array(
  8625. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  8626. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8627. 'class' => 'form-error'
  8628. ),
  8629. '/select',
  8630. array('div' => array('class' => 'error-message')),
  8631. 'Error',
  8632. '/div',
  8633. '/div'
  8634. );
  8635. $this->assertTags($result, $expected);
  8636. }
  8637. /**
  8638. * test the correct display of multi-record form validation errors.
  8639. *
  8640. * @return void
  8641. */
  8642. public function testMultiRecordFormValidationErrors() {
  8643. $this->Form->create('ValidateProfile');
  8644. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8645. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8646. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8647. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8648. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8649. $result = $this->Form->error('ValidateProfile.2.city');
  8650. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8651. $result = $this->Form->error('2.city');
  8652. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8653. }
  8654. /**
  8655. * test the correct display of multi-record form validation errors.
  8656. *
  8657. * @return void
  8658. */
  8659. public function testSaveManyRecordFormValidationErrors() {
  8660. $this->Form->create('ValidateUser');
  8661. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8662. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8663. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8664. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8665. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8666. $result = $this->Form->error('ValidateUser.0.city');
  8667. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8668. }
  8669. /**
  8670. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8671. *
  8672. * @return void
  8673. */
  8674. public function testInputTemplate() {
  8675. $result = $this->Form->input('Contact.email', array(
  8676. 'type' => 'text', 'format' => array('input')
  8677. ));
  8678. $expected = array(
  8679. 'div' => array('class' => 'input text'),
  8680. 'input' => array(
  8681. 'type' => 'text', 'name' => 'data[Contact][email]',
  8682. 'id' => 'ContactEmail'
  8683. ),
  8684. '/div'
  8685. );
  8686. $this->assertTags($result, $expected);
  8687. $result = $this->Form->input('Contact.email', array(
  8688. 'type' => 'text', 'format' => array('input', 'label'),
  8689. 'label' => '<em>Email (required)</em>'
  8690. ));
  8691. $expected = array(
  8692. 'div' => array('class' => 'input text'),
  8693. array('input' => array(
  8694. 'type' => 'text', 'name' => 'data[Contact][email]',
  8695. 'id' => 'ContactEmail'
  8696. )),
  8697. 'label' => array('for' => 'ContactEmail'),
  8698. 'em' => array(),
  8699. 'Email (required)',
  8700. '/em',
  8701. '/label',
  8702. '/div'
  8703. );
  8704. $this->assertTags($result, $expected);
  8705. $result = $this->Form->input('Contact.email', array(
  8706. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8707. 'between' => '<div>Something in the middle</div>',
  8708. 'after' => '<span>Some text at the end</span>'
  8709. ));
  8710. $expected = array(
  8711. 'div' => array('class' => 'input text'),
  8712. array('input' => array(
  8713. 'type' => 'text', 'name' => 'data[Contact][email]',
  8714. 'id' => 'ContactEmail'
  8715. )),
  8716. array('div' => array()),
  8717. 'Something in the middle',
  8718. '/div',
  8719. 'label' => array('for' => 'ContactEmail'),
  8720. 'Email',
  8721. '/label',
  8722. 'span' => array(),
  8723. 'Some text at the end',
  8724. '/span',
  8725. '/div'
  8726. );
  8727. $this->assertTags($result, $expected);
  8728. $result = $this->Form->input('Contact.method', array(
  8729. 'type' => 'radio',
  8730. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8731. 'between' => 'I am between',
  8732. ));
  8733. $expected = array(
  8734. 'div' => array('class' => 'input radio'),
  8735. 'fieldset' => array(),
  8736. 'legend' => array(),
  8737. 'Method',
  8738. '/legend',
  8739. 'I am between',
  8740. 'input' => array(
  8741. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  8742. 'value' => '', 'id' => 'ContactMethod_'
  8743. ),
  8744. array('input' => array(
  8745. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8746. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8747. )),
  8748. array('label' => array('for' => 'ContactMethodEmail')),
  8749. 'Email',
  8750. '/label',
  8751. array('input' => array(
  8752. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8753. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8754. )),
  8755. array('label' => array('for' => 'ContactMethodPigeon')),
  8756. 'Pigeon',
  8757. '/label',
  8758. '/fieldset',
  8759. '/div',
  8760. );
  8761. $this->assertTags($result, $expected);
  8762. }
  8763. /**
  8764. * test that some html5 inputs + FormHelper::__call() work
  8765. *
  8766. * @return void
  8767. */
  8768. public function testHtml5Inputs() {
  8769. $result = $this->Form->email('User.email');
  8770. $expected = array(
  8771. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  8772. );
  8773. $this->assertTags($result, $expected);
  8774. $result = $this->Form->search('User.query');
  8775. $expected = array(
  8776. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  8777. );
  8778. $this->assertTags($result, $expected);
  8779. $result = $this->Form->search('User.query', array('value' => 'test'));
  8780. $expected = array(
  8781. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8782. );
  8783. $this->assertTags($result, $expected);
  8784. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8785. $expected = array(
  8786. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8787. );
  8788. $this->assertTags($result, $expected);
  8789. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8790. $expected = array(
  8791. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8792. );
  8793. $this->assertTags($result, $expected);
  8794. }
  8795. /**
  8796. *
  8797. * @expectedException CakeException
  8798. * @return void
  8799. */
  8800. public function testHtml5InputException() {
  8801. $this->Form->email();
  8802. }
  8803. /**
  8804. * Tests that a model can be loaded from the model names passed in the request object
  8805. *
  8806. * @return void
  8807. */
  8808. public function testIntrospectModelFromRequest() {
  8809. $this->loadFixtures('Post');
  8810. App::build(array(
  8811. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  8812. ));
  8813. CakePlugin::load('TestPlugin');
  8814. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  8815. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8816. $this->Form->create('TestPluginPost');
  8817. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8818. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8819. CakePlugin::unload();
  8820. App::build();
  8821. }
  8822. /**
  8823. * Tests that it is possible to set the validation errors directly in the helper for a field
  8824. *
  8825. * @return void
  8826. */
  8827. public function testCustomValidationErrors() {
  8828. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8829. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8830. $this->assertEquals('Badness!', $result);
  8831. }
  8832. /**
  8833. * Tests that the 'on' key validates as expected on create
  8834. *
  8835. * @return void
  8836. */
  8837. public function testRequiredOnCreate() {
  8838. $this->Form->create('Contact');
  8839. $result = $this->Form->input('Contact.imrequiredonupdate');
  8840. $expected = array(
  8841. 'div' => array('class' => 'input text'),
  8842. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8843. 'Imrequiredonupdate',
  8844. '/label',
  8845. 'input' => array(
  8846. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8847. 'id' => 'ContactImrequiredonupdate'
  8848. ),
  8849. '/div'
  8850. );
  8851. $this->assertTags($result, $expected);
  8852. $result = $this->Form->input('Contact.imrequiredoncreate');
  8853. $expected = array(
  8854. 'div' => array('class' => 'input text required'),
  8855. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8856. 'Imrequiredoncreate',
  8857. '/label',
  8858. 'input' => array(
  8859. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8860. 'id' => 'ContactImrequiredoncreate',
  8861. 'required' => 'required'
  8862. ),
  8863. '/div'
  8864. );
  8865. $this->assertTags($result, $expected);
  8866. $result = $this->Form->input('Contact.imrequiredonboth');
  8867. $expected = array(
  8868. 'div' => array('class' => 'input text required'),
  8869. 'label' => array('for' => 'ContactImrequiredonboth'),
  8870. 'Imrequiredonboth',
  8871. '/label',
  8872. 'input' => array(
  8873. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8874. 'id' => 'ContactImrequiredonboth',
  8875. 'required' => 'required'
  8876. ),
  8877. '/div'
  8878. );
  8879. $this->assertTags($result, $expected);
  8880. $this->Form->inputDefaults(array('required' => false));
  8881. $result = $this->Form->input('Contact.imrequired');
  8882. $expected = array(
  8883. 'div' => array('class' => 'input text'),
  8884. 'label' => array('for' => 'ContactImrequired'),
  8885. 'Imrequired',
  8886. '/label',
  8887. 'input' => array(
  8888. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8889. 'id' => 'ContactImrequired'
  8890. ),
  8891. '/div'
  8892. );
  8893. $this->assertTags($result, $expected);
  8894. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8895. $this->assertTags($result, $expected);
  8896. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8897. $expected = array(
  8898. 'div' => array('class' => 'input text required'),
  8899. 'label' => array('for' => 'ContactImrequired'),
  8900. 'Imrequired',
  8901. '/label',
  8902. 'input' => array(
  8903. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8904. 'id' => 'ContactImrequired'
  8905. ),
  8906. '/div'
  8907. );
  8908. $this->assertTags($result, $expected);
  8909. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8910. $this->assertTags($result, $expected);
  8911. }
  8912. /**
  8913. * Tests that the 'on' key validates as expected on update
  8914. *
  8915. * @return void
  8916. */
  8917. public function testRequiredOnUpdate() {
  8918. $this->Form->request->data['Contact']['id'] = 1;
  8919. $this->Form->create('Contact');
  8920. $result = $this->Form->input('Contact.imrequiredonupdate');
  8921. $expected = array(
  8922. 'div' => array('class' => 'input text required'),
  8923. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8924. 'Imrequiredonupdate',
  8925. '/label',
  8926. 'input' => array(
  8927. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8928. 'id' => 'ContactImrequiredonupdate',
  8929. 'required' => 'required'
  8930. ),
  8931. '/div'
  8932. );
  8933. $this->assertTags($result, $expected);
  8934. $result = $this->Form->input('Contact.imrequiredoncreate');
  8935. $expected = array(
  8936. 'div' => array('class' => 'input text'),
  8937. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8938. 'Imrequiredoncreate',
  8939. '/label',
  8940. 'input' => array(
  8941. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8942. 'id' => 'ContactImrequiredoncreate'
  8943. ),
  8944. '/div'
  8945. );
  8946. $this->assertTags($result, $expected);
  8947. $result = $this->Form->input('Contact.imrequiredonboth');
  8948. $expected = array(
  8949. 'div' => array('class' => 'input text required'),
  8950. 'label' => array('for' => 'ContactImrequiredonboth'),
  8951. 'Imrequiredonboth',
  8952. '/label',
  8953. 'input' => array(
  8954. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8955. 'id' => 'ContactImrequiredonboth',
  8956. 'required' => 'required'
  8957. ),
  8958. '/div'
  8959. );
  8960. $this->assertTags($result, $expected);
  8961. $result = $this->Form->input('Contact.imrequired');
  8962. $expected = array(
  8963. 'div' => array('class' => 'input text required'),
  8964. 'label' => array('for' => 'ContactImrequired'),
  8965. 'Imrequired',
  8966. '/label',
  8967. 'input' => array(
  8968. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8969. 'id' => 'ContactImrequired',
  8970. 'required' => 'required'
  8971. ),
  8972. '/div'
  8973. );
  8974. $this->assertTags($result, $expected);
  8975. }
  8976. /**
  8977. * Test inputDefaults setter and getter
  8978. *
  8979. * @return void
  8980. */
  8981. public function testInputDefaults() {
  8982. $this->Form->create('Contact');
  8983. $this->Form->inputDefaults(array(
  8984. 'label' => false,
  8985. 'div' => array(
  8986. 'style' => 'color: #000;'
  8987. )
  8988. ));
  8989. $result = $this->Form->input('Contact.field1');
  8990. $expected = array(
  8991. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8992. 'input' => array(
  8993. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8994. 'id' => 'ContactField1'
  8995. ),
  8996. '/div'
  8997. );
  8998. $this->assertTags($result, $expected);
  8999. $this->Form->inputDefaults(array(
  9000. 'div' => false,
  9001. 'label' => 'Label',
  9002. ));
  9003. $result = $this->Form->input('Contact.field1');
  9004. $expected = array(
  9005. 'label' => array('for' => 'ContactField1'),
  9006. 'Label',
  9007. '/label',
  9008. 'input' => array(
  9009. 'type' => 'text', 'name' => 'data[Contact][field1]',
  9010. 'id' => 'ContactField1'
  9011. ),
  9012. );
  9013. $this->assertTags($result, $expected);
  9014. $this->Form->inputDefaults(array(
  9015. 'label' => false,
  9016. ), true);
  9017. $result = $this->Form->input('Contact.field1');
  9018. $expected = array(
  9019. 'input' => array(
  9020. 'type' => 'text', 'name' => 'data[Contact][field1]',
  9021. 'id' => 'ContactField1'
  9022. ),
  9023. );
  9024. $this->assertTags($result, $expected);
  9025. $result = $this->Form->inputDefaults();
  9026. $expected = array(
  9027. 'div' => false,
  9028. 'label' => false,
  9029. );
  9030. $this->assertEquals($expected, $result);
  9031. }
  9032. }