FormHelperTest.php 305 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661
  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. * @since CakePHP(tm) v 1.2.0.4206
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\View\Helper;
  18. use Cake\Controller\Controller;
  19. use Cake\Core\App;
  20. use Cake\Core\Configure;
  21. use Cake\Core\Plugin;
  22. use Cake\Network\Request;
  23. use Cake\ORM\Table;
  24. use Cake\Routing\Router;
  25. use Cake\TestSuite\TestCase;
  26. use Cake\Utility\ClassRegistry;
  27. use Cake\Utility\Security;
  28. use Cake\View\Helper\FormHelper;
  29. use Cake\View\Helper\HtmlHelper;
  30. use Cake\View\View;
  31. /**
  32. * ContactTestController class
  33. *
  34. */
  35. class ContactTestController extends Controller {
  36. /**
  37. * uses property
  38. *
  39. * @var mixed null
  40. */
  41. public $uses = null;
  42. }
  43. /**
  44. * Contact class
  45. *
  46. */
  47. class ContactsTable extends Table {
  48. /**
  49. * Default schema
  50. *
  51. * @var array
  52. */
  53. protected $_schema = array(
  54. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  55. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  56. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  57. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  58. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  59. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  60. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  61. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  62. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  63. );
  64. /**
  65. * validate property
  66. *
  67. * @var array
  68. */
  69. public $validate = array(
  70. 'non_existing' => array(),
  71. 'idontexist' => array(),
  72. 'imrequired' => array('_allowEmpty' => false, array('rule' => array('between', 5, 30))),
  73. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  74. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  75. 'imrequiredonboth' => array(
  76. 'required' => array('rule' => 'alphaNumeric'),
  77. ),
  78. 'string_required' => 'notEmpty',
  79. 'imalsorequired' => array('_allowEmpty' => false, array('rule' => 'alphaNumeric')),
  80. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  81. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  82. 'imnotrequired' => array(
  83. '_allowEmpty' => true,
  84. array('rule' => 'alphaNumeric')
  85. ),
  86. 'imalsonotrequired' => array(
  87. '_allowEmpty' => true,
  88. 'alpha' => array('rule' => 'alphaNumeric'),
  89. 'between' => array('rule' => array('between', 5, 30)),
  90. ),
  91. 'imnotrequiredeither' => array(
  92. '_allowEmpty' => true,
  93. array('rule' => array('between', 5, 30))
  94. ),
  95. 'iamrequiredalways' => array(
  96. 'email' => array('rule' => 'email'),
  97. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  98. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  99. ),
  100. 'boolean_field' => array('rule' => 'boolean')
  101. );
  102. /**
  103. * schema method
  104. *
  105. * @return void
  106. */
  107. public function setSchema($schema) {
  108. $this->_schema = $schema;
  109. }
  110. /**
  111. * hasAndBelongsToMany property
  112. *
  113. * @var array
  114. */
  115. public $hasAndBelongsToMany = array(
  116. 'ContactTag' => array(
  117. 'className' => 'Cake\Test\TestCase\View\Helper\ContactTag',
  118. 'with' => 'Cake\Test\TestCase\View\Helper\ContactTagsContact'
  119. )
  120. );
  121. /**
  122. * hasAndBelongsToMany property
  123. *
  124. * @var array
  125. */
  126. public $belongsTo = array(
  127. 'User' => array('className' => 'UserForm'
  128. ));
  129. }
  130. /**
  131. * ContactTagsContact class
  132. *
  133. */
  134. class ContactTagsContactsTable extends Table {
  135. /**
  136. * Default schema
  137. *
  138. * @var array
  139. */
  140. protected $_schema = array(
  141. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  142. 'contact_tag_id' => array(
  143. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  144. )
  145. );
  146. /**
  147. * schema method
  148. *
  149. * @return void
  150. */
  151. public function setSchema($schema) {
  152. $this->_schema = $schema;
  153. }
  154. }
  155. /**
  156. * ContactNonStandardPk class
  157. *
  158. */
  159. class ContactNonStandardPk extends ContactsTable {
  160. /**
  161. * primaryKey property
  162. *
  163. * @var string
  164. */
  165. public $primaryKey = 'pk';
  166. /**
  167. * schema method
  168. *
  169. * @return void
  170. */
  171. public function schema($field = false) {
  172. $this->_schema = parent::schema();
  173. $this->_schema['pk'] = $this->_schema['id'];
  174. unset($this->_schema['id']);
  175. return $this->_schema;
  176. }
  177. }
  178. /**
  179. * ContactTag class
  180. *
  181. */
  182. class ContactTagsTable extends Table {
  183. /**
  184. * schema definition
  185. *
  186. * @var array
  187. */
  188. protected $_schema = array(
  189. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  190. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  191. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  192. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  193. );
  194. }
  195. /**
  196. * UserForm class
  197. *
  198. */
  199. class UserFormsTable extends Table {
  200. /**
  201. * hasMany property
  202. *
  203. * @var array
  204. */
  205. public $hasMany = array(
  206. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  207. ));
  208. /**
  209. * schema definition
  210. *
  211. * @var array
  212. */
  213. protected $_schema = array(
  214. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  215. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  216. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  217. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  218. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  219. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  220. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  221. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  222. );
  223. }
  224. /**
  225. * OpenidUrl class
  226. *
  227. */
  228. class OpenidUrlsTable extends Table {
  229. /**
  230. * belongsTo property
  231. *
  232. * @var array
  233. */
  234. public $belongsTo = array('UserForm' => array(
  235. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  236. ));
  237. /**
  238. * validate property
  239. *
  240. * @var array
  241. */
  242. public $validate = array('openid_not_registered' => array());
  243. /**
  244. * schema method
  245. *
  246. * @var array
  247. */
  248. protected $_schema = array(
  249. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  250. 'user_form_id' => array(
  251. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  252. ),
  253. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  254. );
  255. /**
  256. * beforeValidate method
  257. *
  258. * @return void
  259. */
  260. public function beforeValidate($options = array()) {
  261. $this->invalidate('openid_not_registered');
  262. return true;
  263. }
  264. }
  265. /**
  266. * ValidateUser class
  267. *
  268. */
  269. class ValidateUsersTable extends Table {
  270. /**
  271. * hasOne property
  272. *
  273. * @var array
  274. */
  275. public $hasOne = array('ValidateProfile' => array(
  276. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  277. ));
  278. /**
  279. * schema method
  280. *
  281. * @var array
  282. */
  283. protected $_schema = array(
  284. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  285. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  286. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  287. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  288. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => '6,3'),
  289. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  290. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  291. );
  292. /**
  293. * beforeValidate method
  294. *
  295. * @return void
  296. */
  297. public function beforeValidate($options = array()) {
  298. $this->invalidate('email');
  299. return false;
  300. }
  301. }
  302. /**
  303. * ValidateProfile class
  304. *
  305. */
  306. class ValidateProfilesTable extends Table {
  307. /**
  308. * useTable property
  309. *
  310. * @var boolean
  311. */
  312. public $useTable = false;
  313. /**
  314. * schema property
  315. *
  316. * @var array
  317. */
  318. protected $_schema = array(
  319. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  320. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  321. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  322. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  323. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  324. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  325. );
  326. /**
  327. * hasOne property
  328. *
  329. * @var array
  330. */
  331. public $hasOne = array('ValidateItem' => array(
  332. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  333. ));
  334. /**
  335. * belongsTo property
  336. *
  337. * @var array
  338. */
  339. public $belongsTo = array('ValidateUser' => array(
  340. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  341. ));
  342. /**
  343. * beforeValidate method
  344. *
  345. * @return void
  346. */
  347. public function beforeValidate($options = array()) {
  348. $this->invalidate('full_name');
  349. $this->invalidate('city');
  350. return false;
  351. }
  352. }
  353. /**
  354. * ValidateItem class
  355. *
  356. */
  357. class ValidateItemsTable extends Table {
  358. /**
  359. * schema property
  360. *
  361. * @var array
  362. */
  363. protected $_schema = array(
  364. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  365. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  366. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  367. 'description' => array(
  368. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  369. ),
  370. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  371. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  372. );
  373. /**
  374. * belongsTo property
  375. *
  376. * @var array
  377. */
  378. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  379. /**
  380. * beforeValidate method
  381. *
  382. * @return void
  383. */
  384. public function beforeValidate($options = array()) {
  385. $this->invalidate('description');
  386. return false;
  387. }
  388. }
  389. /**
  390. * TestMail class
  391. *
  392. */
  393. class TestMailsTable extends Table {
  394. }
  395. /**
  396. * FormHelperTest class
  397. *
  398. * @property FormHelper $Form
  399. */
  400. class FormHelperTest extends TestCase {
  401. /**
  402. * Fixtures to be used
  403. *
  404. * @var array
  405. */
  406. public $fixtures = array('core.post');
  407. /**
  408. * Do not load the fixtures by default
  409. *
  410. * @var boolean
  411. */
  412. public $autoFixtures = false;
  413. /**
  414. * setUp method
  415. *
  416. * @return void
  417. */
  418. public function setUp() {
  419. parent::setUp();
  420. $this->markTestIncomplete('Need to revisit once models work again.');
  421. Configure::write('Config.language', 'eng');
  422. Configure::write('App.base', '');
  423. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  424. Configure::delete('Asset');
  425. $this->Controller = new ContactTestController();
  426. $this->View = new View($this->Controller);
  427. $this->Form = new FormHelper($this->View);
  428. $this->Form->Html = new HtmlHelper($this->View);
  429. $this->Form->request = new Request('contacts/add');
  430. $this->Form->request->here = '/contacts/add';
  431. $this->Form->request['action'] = 'add';
  432. $this->Form->request->webroot = '';
  433. $this->Form->request->base = '';
  434. ClassRegistry::addObject('Contact', new Contact());
  435. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  436. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  437. ClassRegistry::addObject('User', new UserForm());
  438. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  439. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  440. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  441. $this->dateRegex = array(
  442. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  443. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  444. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  445. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  446. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  447. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  448. );
  449. Configure::write('Security.salt', 'foo!');
  450. Router::connect('/:controller', array('action' => 'index'));
  451. Router::connect('/:controller/:action/*');
  452. }
  453. /**
  454. * tearDown method
  455. *
  456. * @return void
  457. */
  458. public function tearDown() {
  459. parent::tearDown();
  460. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  461. }
  462. /**
  463. * testFormCreateWithSecurity method
  464. *
  465. * Test form->create() with security key.
  466. *
  467. * @return void
  468. */
  469. public function testCreateWithSecurity() {
  470. $this->Form->request->params['_csrfToken'] = 'testKey';
  471. $encoding = strtolower(Configure::read('App.encoding'));
  472. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  473. $expected = array(
  474. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  475. 'div' => array('style' => 'display:none;'),
  476. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  477. array('input' => array(
  478. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey', 'id'
  479. )),
  480. '/div'
  481. );
  482. $this->assertTags($result, $expected);
  483. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  484. $expected['form']['id'] = 'MyForm';
  485. $this->assertTags($result, $expected);
  486. }
  487. /**
  488. * testFormCreateGetNoSecurity method
  489. *
  490. * Test form->create() with no security key as its a get form
  491. *
  492. * @return void
  493. */
  494. public function testCreateEndGetNoSecurity() {
  495. $this->Form->request->params['_csrfToken'] = 'testKey';
  496. $encoding = strtolower(Configure::read('App.encoding'));
  497. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  498. $this->assertNotContains('testKey', $result);
  499. $result = $this->Form->end('Save');
  500. $this->assertNotContains('testKey', $result);
  501. }
  502. /**
  503. * test that create() clears the fields property so it starts fresh
  504. *
  505. * @return void
  506. */
  507. public function testCreateClearingFields() {
  508. $this->Form->fields = array('model_id');
  509. $this->Form->create('Contact');
  510. $this->assertEquals(array(), $this->Form->fields);
  511. }
  512. /**
  513. * Tests form hash generation with model-less data
  514. *
  515. * @return void
  516. */
  517. public function testValidateHashNoModel() {
  518. $this->Form->request->params['_csrfToken'] = 'foo';
  519. $result = $this->Form->secure(array('anything'));
  520. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  521. }
  522. /**
  523. * Tests that models with identical field names get resolved properly
  524. *
  525. * @return void
  526. */
  527. public function testDuplicateFieldNameResolution() {
  528. $result = $this->Form->create('ValidateUser');
  529. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  530. $result = $this->Form->input('ValidateItem.name');
  531. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  532. $result = $this->Form->input('ValidateUser.name');
  533. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  534. $this->assertContains('name="ValidateUser[name]"', $result);
  535. $this->assertContains('type="text"', $result);
  536. $result = $this->Form->input('ValidateItem.name');
  537. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  538. $this->assertContains('name="ValidateItem[name]"', $result);
  539. $this->assertContains('<textarea', $result);
  540. $result = $this->Form->input('name');
  541. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  542. $this->assertContains('name="ValidateUser[name]"', $result);
  543. $this->assertContains('type="text"', $result);
  544. }
  545. /**
  546. * Tests that hidden fields generated for checkboxes don't get locked
  547. *
  548. * @return void
  549. */
  550. public function testNoCheckboxLocking() {
  551. $this->Form->request->params['_csrfToken'] = 'foo';
  552. $this->assertSame(array(), $this->Form->fields);
  553. $this->Form->checkbox('check', array('value' => '1'));
  554. $this->assertSame($this->Form->fields, array('check'));
  555. }
  556. /**
  557. * testFormSecurityFields method
  558. *
  559. * Test generation of secure form hash generation.
  560. *
  561. * @return void
  562. */
  563. public function testFormSecurityFields() {
  564. $key = 'testKey';
  565. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  566. $this->Form->request->params['_csrfToken'] = $key;
  567. $result = $this->Form->secure($fields);
  568. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  569. $hash .= ':' . 'Model.valid';
  570. $hash = urlencode($hash);
  571. $expected = array(
  572. 'div' => array('style' => 'display:none;'),
  573. array('input' => array(
  574. 'type' => 'hidden', 'name' => '_Token[fields]',
  575. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  576. )),
  577. array('input' => array(
  578. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  579. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  580. )),
  581. '/div'
  582. );
  583. $this->assertTags($result, $expected);
  584. $path = CAKE . 'Test/TestApp/Config/';
  585. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  586. $result = $this->Form->secure($fields);
  587. $expected = array(
  588. 'div' => array('class' => 'hidden'),
  589. array('input' => array(
  590. 'type' => 'hidden', 'name' => '_Token[fields]',
  591. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  592. )),
  593. array('input' => array(
  594. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  595. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  596. )),
  597. '/div'
  598. );
  599. $this->assertTags($result, $expected);
  600. }
  601. /**
  602. * Tests correct generation of number fields for double and float fields
  603. *
  604. * @return void
  605. */
  606. public function testTextFieldGenerationForFloats() {
  607. $model = ClassRegistry::getObject('Contact');
  608. $model->setSchema(array('foo' => array(
  609. 'type' => 'float',
  610. 'null' => false,
  611. 'default' => null,
  612. 'length' => 10
  613. )));
  614. $this->Form->create('Contact');
  615. $result = $this->Form->input('foo');
  616. $expected = array(
  617. 'div' => array('class' => 'input number'),
  618. 'label' => array('for' => 'ContactFoo'),
  619. 'Foo',
  620. '/label',
  621. array('input' => array(
  622. 'type' => 'number',
  623. 'name' => 'Contact[foo]',
  624. 'id' => 'ContactFoo',
  625. 'step' => 'any'
  626. )),
  627. '/div'
  628. );
  629. $this->assertTags($result, $expected);
  630. $result = $this->Form->input('foo', array('step' => 0.5));
  631. $expected = array(
  632. 'div' => array('class' => 'input number'),
  633. 'label' => array('for' => 'ContactFoo'),
  634. 'Foo',
  635. '/label',
  636. array('input' => array(
  637. 'type' => 'number',
  638. 'name' => 'Contact[foo]',
  639. 'id' => 'ContactFoo',
  640. 'step' => '0.5'
  641. )),
  642. '/div'
  643. );
  644. $this->assertTags($result, $expected);
  645. }
  646. /**
  647. * Tests correct generation of number fields for integer fields
  648. *
  649. * @return void
  650. */
  651. public function testTextFieldTypeNumberGenerationForIntegers() {
  652. $model = ClassRegistry::getObject('Contact');
  653. $model->setSchema(array('foo' => array(
  654. 'type' => 'integer',
  655. 'null' => false,
  656. 'default' => null,
  657. 'length' => null
  658. )));
  659. $this->Form->create('Contact');
  660. $result = $this->Form->input('foo');
  661. $expected = array(
  662. 'div' => array('class' => 'input number'),
  663. 'label' => array('for' => 'ContactFoo'),
  664. 'Foo',
  665. '/label',
  666. array('input' => array(
  667. 'type' => 'number', 'name' => 'Contact[foo]',
  668. 'id' => 'ContactFoo'
  669. )),
  670. '/div'
  671. );
  672. $this->assertTags($result, $expected);
  673. }
  674. /**
  675. * Tests correct generation of file upload fields for binary fields
  676. *
  677. * @return void
  678. */
  679. public function testFileUploadFieldTypeGenerationForBinaries() {
  680. $model = ClassRegistry::getObject('Contact');
  681. $model->setSchema(array('foo' => array(
  682. 'type' => 'binary',
  683. 'null' => false,
  684. 'default' => null,
  685. 'length' => 1024
  686. )));
  687. $this->Form->create('Contact');
  688. $result = $this->Form->input('foo');
  689. $expected = array(
  690. 'div' => array('class' => 'input file'),
  691. 'label' => array('for' => 'ContactFoo'),
  692. 'Foo',
  693. '/label',
  694. array('input' => array(
  695. 'type' => 'file', 'name' => 'data[Contact][foo]',
  696. 'id' => 'ContactFoo'
  697. )),
  698. '/div'
  699. );
  700. $this->assertTags($result, $expected);
  701. }
  702. /**
  703. * testFormSecurityMultipleFields method
  704. *
  705. * Test secure() with multiple row form. Ensure hash is correct.
  706. *
  707. * @return void
  708. */
  709. public function testFormSecurityMultipleFields() {
  710. $key = 'testKey';
  711. $fields = array(
  712. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  713. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  714. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  715. );
  716. $this->Form->request->params['_csrfToken'] = $key;
  717. $result = $this->Form->secure($fields);
  718. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  719. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  720. $expected = array(
  721. 'div' => array('style' => 'display:none;'),
  722. array('input' => array(
  723. 'type' => 'hidden', 'name' => '_Token[fields]',
  724. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  725. )),
  726. array('input' => array(
  727. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  728. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  729. )),
  730. '/div'
  731. );
  732. $this->assertTags($result, $expected);
  733. }
  734. /**
  735. * testFormSecurityMultipleSubmitButtons
  736. *
  737. * test form submit generation and ensure that _Token is only created on end()
  738. *
  739. * @return void
  740. */
  741. public function testFormSecurityMultipleSubmitButtons() {
  742. $key = 'testKey';
  743. $this->Form->request->params['_csrfToken'] = $key;
  744. $this->Form->create('Addresses');
  745. $this->Form->input('Address.title');
  746. $this->Form->input('Address.first_name');
  747. $result = $this->Form->submit('Save', array('name' => 'save'));
  748. $expected = array(
  749. 'div' => array('class' => 'submit'),
  750. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  751. '/div',
  752. );
  753. $this->assertTags($result, $expected);
  754. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  755. $expected = array(
  756. 'div' => array('class' => 'submit'),
  757. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  758. '/div',
  759. );
  760. $this->assertTags($result, $expected);
  761. $result = $this->Form->end(null);
  762. $expected = array(
  763. 'div' => array('style' => 'display:none;'),
  764. array('input' => array(
  765. 'type' => 'hidden', 'name' => '_Token[fields]',
  766. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  767. )),
  768. array('input' => array(
  769. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  770. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  771. )),
  772. '/div'
  773. );
  774. $this->assertTags($result, $expected);
  775. }
  776. /**
  777. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  778. *
  779. * @return void
  780. */
  781. public function testSecurityButtonNestedNamed() {
  782. $key = 'testKey';
  783. $this->Form->request->params['_csrfToken'] = $key;
  784. $this->Form->create('Addresses');
  785. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  786. $result = $this->Form->unlockField();
  787. $this->assertEquals(array('Address.button'), $result);
  788. }
  789. /**
  790. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  791. *
  792. * @return void
  793. */
  794. public function testSecuritySubmitNestedNamed() {
  795. $key = 'testKey';
  796. $this->Form->request->params['_csrfToken'] = $key;
  797. $this->Form->create('Addresses');
  798. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  799. $result = $this->Form->unlockField();
  800. $this->assertEquals(array('Address.button'), $result);
  801. }
  802. /**
  803. * Test that the correct fields are unlocked for image submits with no names.
  804. *
  805. * @return void
  806. */
  807. public function testSecuritySubmitImageNoName() {
  808. $key = 'testKey';
  809. $this->Form->request->params['_csrfToken'] = $key;
  810. $this->Form->create('User');
  811. $result = $this->Form->submit('save.png');
  812. $expected = array(
  813. 'div' => array('class' => 'submit'),
  814. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  815. '/div'
  816. );
  817. $this->assertTags($result, $expected);
  818. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  819. }
  820. /**
  821. * Test that the correct fields are unlocked for image submits with names.
  822. *
  823. * @return void
  824. */
  825. public function testSecuritySubmitImageName() {
  826. $key = 'testKey';
  827. $this->Form->request->params['_csrfToken'] = $key;
  828. $this->Form->create('User');
  829. $result = $this->Form->submit('save.png', array('name' => 'test'));
  830. $expected = array(
  831. 'div' => array('class' => 'submit'),
  832. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  833. '/div'
  834. );
  835. $this->assertTags($result, $expected);
  836. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  837. }
  838. /**
  839. * testFormSecurityMultipleInputFields method
  840. *
  841. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  842. *
  843. * @return void
  844. */
  845. public function testFormSecurityMultipleInputFields() {
  846. $key = 'testKey';
  847. $this->Form->request->params['_csrfToken'] = $key;
  848. $this->Form->create('Addresses');
  849. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  850. $this->Form->input('Addresses.0.title');
  851. $this->Form->input('Addresses.0.first_name');
  852. $this->Form->input('Addresses.0.last_name');
  853. $this->Form->input('Addresses.0.address');
  854. $this->Form->input('Addresses.0.city');
  855. $this->Form->input('Addresses.0.phone');
  856. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  857. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  858. $this->Form->input('Addresses.1.title');
  859. $this->Form->input('Addresses.1.first_name');
  860. $this->Form->input('Addresses.1.last_name');
  861. $this->Form->input('Addresses.1.address');
  862. $this->Form->input('Addresses.1.city');
  863. $this->Form->input('Addresses.1.phone');
  864. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  865. $result = $this->Form->secure($this->Form->fields);
  866. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  867. $expected = array(
  868. 'div' => array('style' => 'display:none;'),
  869. array('input' => array(
  870. 'type' => 'hidden', 'name' => '_Token[fields]',
  871. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  872. )),
  873. array('input' => array(
  874. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  875. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  876. )),
  877. '/div'
  878. );
  879. $this->assertTags($result, $expected);
  880. }
  881. /**
  882. * Test form security with Model.field.0 style inputs
  883. *
  884. * @return void
  885. */
  886. public function testFormSecurityArrayFields() {
  887. $key = 'testKey';
  888. $this->Form->request->params['_csrfToken'] = $key;
  889. $this->Form->create('Address');
  890. $this->Form->input('Address.primary.1');
  891. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  892. $this->Form->input('Address.secondary.1.0');
  893. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  894. }
  895. /**
  896. * testFormSecurityMultipleInputDisabledFields method
  897. *
  898. * test secure form generation with multiple records and disabled fields.
  899. *
  900. * @return void
  901. */
  902. public function testFormSecurityMultipleInputDisabledFields() {
  903. $key = 'testKey';
  904. $this->Form->request->params['_csrfToken'] = $key;
  905. $this->Form->request->params['_Token'] = array(
  906. 'unlockedFields' => array('first_name', 'address')
  907. );
  908. $this->Form->create();
  909. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  910. $this->Form->input('Addresses.0.title');
  911. $this->Form->input('Addresses.0.first_name');
  912. $this->Form->input('Addresses.0.last_name');
  913. $this->Form->input('Addresses.0.address');
  914. $this->Form->input('Addresses.0.city');
  915. $this->Form->input('Addresses.0.phone');
  916. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  917. $this->Form->input('Addresses.1.title');
  918. $this->Form->input('Addresses.1.first_name');
  919. $this->Form->input('Addresses.1.last_name');
  920. $this->Form->input('Addresses.1.address');
  921. $this->Form->input('Addresses.1.city');
  922. $this->Form->input('Addresses.1.phone');
  923. $result = $this->Form->secure($this->Form->fields);
  924. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  925. $expected = array(
  926. 'div' => array('style' => 'display:none;'),
  927. array('input' => array(
  928. 'type' => 'hidden', 'name' => '_Token[fields]',
  929. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  930. )),
  931. array('input' => array(
  932. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  933. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  934. )),
  935. '/div'
  936. );
  937. $this->assertTags($result, $expected);
  938. }
  939. /**
  940. * testFormSecurityInputDisabledFields method
  941. *
  942. * Test single record form with disabled fields.
  943. *
  944. * @return void
  945. */
  946. public function testFormSecurityInputUnlockedFields() {
  947. $key = 'testKey';
  948. $this->Form->request->params['_csrfToken'] = $key;
  949. $this->Form->request['_Token'] = array(
  950. 'unlockedFields' => array('first_name', 'address')
  951. );
  952. $this->Form->create();
  953. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  954. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  955. $this->Form->input('Addresses.title');
  956. $this->Form->input('Addresses.first_name');
  957. $this->Form->input('Addresses.last_name');
  958. $this->Form->input('Addresses.address');
  959. $this->Form->input('Addresses.city');
  960. $this->Form->input('Addresses.phone');
  961. $result = $this->Form->fields;
  962. $expected = array(
  963. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  964. 'Addresses.city', 'Addresses.phone'
  965. );
  966. $this->assertEquals($expected, $result);
  967. $result = $this->Form->secure($expected);
  968. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  969. $expected = array(
  970. 'div' => array('style' => 'display:none;'),
  971. array('input' => array(
  972. 'type' => 'hidden', 'name' => '_Token[fields]',
  973. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  974. )),
  975. array('input' => array(
  976. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  977. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  978. )),
  979. '/div'
  980. );
  981. $this->assertTags($result, $expected);
  982. }
  983. /**
  984. * test securing inputs with custom name attributes.
  985. *
  986. * @return void
  987. */
  988. public function testFormSecureWithCustomNameAttribute() {
  989. $this->Form->request->params['_csrfToken'] = 'testKey';
  990. $this->Form->text('UserForm.published', array('name' => 'User[custom]'));
  991. $this->assertEquals('User.custom', $this->Form->fields[0]);
  992. $this->Form->text('UserForm.published', array('name' => 'User[custom][another][value]'));
  993. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  994. }
  995. /**
  996. * testFormSecuredInput method
  997. *
  998. * Test generation of entire secure form, assertions made on input() output.
  999. *
  1000. * @return void
  1001. */
  1002. public function testFormSecuredInput() {
  1003. $this->Form->request->params['_csrfToken'] = 'testKey';
  1004. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1005. $encoding = strtolower(Configure::read('App.encoding'));
  1006. $expected = array(
  1007. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1008. 'div' => array('style' => 'display:none;'),
  1009. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1010. array('input' => array(
  1011. 'type' => 'hidden', 'name' => '_csrfToken',
  1012. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1013. )),
  1014. '/div'
  1015. );
  1016. $this->assertTags($result, $expected);
  1017. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1018. $expected = array(
  1019. 'div' => array('class' => 'input text'),
  1020. 'label' => array('for' => 'UserFormPublished'),
  1021. 'Published',
  1022. '/label',
  1023. array('input' => array(
  1024. 'type' => 'text', 'name' => 'UserForm[published]',
  1025. 'id' => 'UserFormPublished'
  1026. )),
  1027. '/div'
  1028. );
  1029. $this->assertTags($result, $expected);
  1030. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1031. $expected = array(
  1032. 'div' => array('class' => 'input text'),
  1033. 'label' => array('for' => 'UserFormOther'),
  1034. 'Other',
  1035. '/label',
  1036. array('input' => array(
  1037. 'type' => 'text', 'name' => 'UserForm[other]',
  1038. 'id' => 'UserFormOther'
  1039. )),
  1040. '/div'
  1041. );
  1042. $this->assertTags($result, $expected);
  1043. $result = $this->Form->hidden('UserForm.stuff');
  1044. $expected = array(
  1045. 'input' => array(
  1046. 'type' => 'hidden', 'name' => 'UserForm[stuff]',
  1047. 'id' => 'UserFormStuff'
  1048. ));
  1049. $this->assertTags($result, $expected);
  1050. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1051. $expected = array('input' => array(
  1052. 'type' => 'hidden', 'name' => 'UserForm[hidden]',
  1053. 'value' => '0', 'id' => 'UserFormHidden'
  1054. ));
  1055. $this->assertTags($result, $expected);
  1056. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1057. $expected = array(
  1058. 'div' => array('class' => 'input checkbox'),
  1059. array('input' => array(
  1060. 'type' => 'hidden', 'name' => 'UserForm[something]',
  1061. 'value' => '0', 'id' => 'UserFormSomething_'
  1062. )),
  1063. array('input' => array(
  1064. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  1065. 'value' => '1', 'id' => 'UserFormSomething'
  1066. )),
  1067. 'label' => array('for' => 'UserFormSomething'),
  1068. 'Something',
  1069. '/label',
  1070. '/div'
  1071. );
  1072. $this->assertTags($result, $expected);
  1073. $result = $this->Form->fields;
  1074. $expected = array(
  1075. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1076. 'UserForm.hidden' => '0', 'UserForm.something'
  1077. );
  1078. $this->assertEquals($expected, $result);
  1079. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1080. $result = $this->Form->secure($this->Form->fields);
  1081. $expected = array(
  1082. 'div' => array('style' => 'display:none;'),
  1083. array('input' => array(
  1084. 'type' => 'hidden', 'name' => '_Token[fields]',
  1085. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1086. )),
  1087. array('input' => array(
  1088. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1089. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1090. )),
  1091. '/div'
  1092. );
  1093. $this->assertTags($result, $expected);
  1094. }
  1095. /**
  1096. * Test secured inputs with custom names.
  1097. *
  1098. * @return void
  1099. */
  1100. public function testSecuredInputCustomName() {
  1101. $this->Form->request->params['_csrfToken'] = 'testKey';
  1102. $this->assertEquals(array(), $this->Form->fields);
  1103. $this->Form->input('text_input', array(
  1104. 'name' => 'data[Option][General.default_role]',
  1105. ));
  1106. $expected = array('Option.General.default_role');
  1107. $this->assertEquals($expected, $this->Form->fields);
  1108. $this->Form->input('select_box', array(
  1109. 'name' => 'data[Option][General.select_role]',
  1110. 'type' => 'select',
  1111. 'options' => array(1, 2),
  1112. ));
  1113. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1114. $this->assertEquals($expected, $this->Form->fields);
  1115. }
  1116. /**
  1117. * Tests that the correct keys are added to the field hash index
  1118. *
  1119. * @return void
  1120. */
  1121. public function testFormSecuredFileInput() {
  1122. $this->Form->request->params['_csrfToken'] = 'testKey';
  1123. $this->assertEquals(array(), $this->Form->fields);
  1124. $this->Form->file('Attachment.file');
  1125. $expected = array(
  1126. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1127. 'Attachment.file.error', 'Attachment.file.size'
  1128. );
  1129. $this->assertEquals($expected, $this->Form->fields);
  1130. }
  1131. /**
  1132. * test that multiple selects keys are added to field hash
  1133. *
  1134. * @return void
  1135. */
  1136. public function testFormSecuredMultipleSelect() {
  1137. $this->Form->request->params['_csrfToken'] = 'testKey';
  1138. $this->assertEquals(array(), $this->Form->fields);
  1139. $options = array('1' => 'one', '2' => 'two');
  1140. $this->Form->select('Model.select', $options);
  1141. $expected = array('Model.select');
  1142. $this->assertEquals($expected, $this->Form->fields);
  1143. $this->Form->fields = array();
  1144. $this->Form->select('Model.select', $options, array('multiple' => true));
  1145. $this->assertEquals($expected, $this->Form->fields);
  1146. }
  1147. /**
  1148. * testFormSecuredRadio method
  1149. *
  1150. * @return void
  1151. */
  1152. public function testFormSecuredRadio() {
  1153. $this->Form->request->params['_csrfToken'] = 'testKey';
  1154. $this->assertEquals(array(), $this->Form->fields);
  1155. $options = array('1' => 'option1', '2' => 'option2');
  1156. $this->Form->radio('Test.test', $options);
  1157. $expected = array('Test.test');
  1158. $this->assertEquals($expected, $this->Form->fields);
  1159. }
  1160. /**
  1161. * Test that when disabled is in a list based attribute array it works.
  1162. *
  1163. * @return void
  1164. */
  1165. public function testFormSecuredAndDisabledNotAssoc() {
  1166. $this->Form->request->params['_csrfToken'] = 'testKey';
  1167. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1168. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1169. $this->Form->text('Model.text', array('disabled'));
  1170. $this->Form->textarea('Model.textarea', array('disabled'));
  1171. $this->Form->password('Model.password', array('disabled'));
  1172. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1173. $expected = array(
  1174. 'Model.radio' => ''
  1175. );
  1176. $this->assertEquals($expected, $this->Form->fields);
  1177. }
  1178. /**
  1179. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1180. * hashing.
  1181. *
  1182. * @return void
  1183. */
  1184. public function testFormSecuredAndDisabled() {
  1185. $this->Form->request->params['_csrfToken'] = 'testKey';
  1186. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1187. $this->Form->text('Model.text', array('disabled' => true));
  1188. $this->Form->password('Model.text', array('disabled' => true));
  1189. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1190. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1191. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1192. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1193. $this->Form->month('Model.month', array('disabled' => true));
  1194. $this->Form->day('Model.day', array('disabled' => true));
  1195. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1196. $this->Form->minute('Model.minute', array('disabled' => true));
  1197. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1198. $expected = array(
  1199. 'Model.radio' => ''
  1200. );
  1201. $this->assertEquals($expected, $this->Form->fields);
  1202. }
  1203. /**
  1204. * testDisableSecurityUsingForm method
  1205. *
  1206. * @return void
  1207. */
  1208. public function testDisableSecurityUsingForm() {
  1209. $this->Form->request->params['_csrfToken'] = 'testKey';
  1210. $this->Form->request['_Token'] = array(
  1211. 'disabledFields' => array()
  1212. );
  1213. $this->Form->create();
  1214. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1215. $this->Form->input('Addresses.title');
  1216. $this->Form->input('Addresses.first_name', array('secure' => false));
  1217. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1218. $this->Form->input('Addresses.zip', array(
  1219. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1220. ));
  1221. $result = $this->Form->fields;
  1222. $expected = array(
  1223. 'Addresses.id' => '123456', 'Addresses.title',
  1224. );
  1225. $this->assertEquals($expected, $result);
  1226. }
  1227. /**
  1228. * test disableField
  1229. *
  1230. * @return void
  1231. */
  1232. public function testUnlockFieldAddsToList() {
  1233. $this->Form->request->params['_csrfToken'] = 'testKey';
  1234. $this->Form->request['_Token'] = array(
  1235. 'unlockedFields' => array()
  1236. );
  1237. $this->Form->create('Contact');
  1238. $this->Form->unlockField('Contact.name');
  1239. $this->Form->text('Contact.name');
  1240. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1241. $this->assertEquals(array(), $this->Form->fields);
  1242. }
  1243. /**
  1244. * test unlockField removing from fields array.
  1245. *
  1246. * @return void
  1247. */
  1248. public function testUnlockFieldRemovingFromFields() {
  1249. $this->Form->request->params['_csrfToken'] = 'testKey';
  1250. $this->Form->request['_Token'] = array(
  1251. 'unlockedFields' => array()
  1252. );
  1253. $this->Form->create('Contact');
  1254. $this->Form->hidden('Contact.id', array('value' => 1));
  1255. $this->Form->text('Contact.name');
  1256. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1257. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1258. $this->Form->unlockField('Contact.name');
  1259. $this->Form->unlockField('Contact.id');
  1260. $this->assertEquals(array(), $this->Form->fields);
  1261. }
  1262. /**
  1263. * testTagIsInvalid method
  1264. *
  1265. * @return void
  1266. */
  1267. public function testTagIsInvalid() {
  1268. $Contact = ClassRegistry::getObject('Contact');
  1269. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1270. $this->Form->setEntity('Contact.0.email');
  1271. $result = $this->Form->tagIsInvalid();
  1272. $this->assertEquals($expected, $result);
  1273. $this->Form->setEntity('Contact.1.email');
  1274. $result = $this->Form->tagIsInvalid();
  1275. $this->assertFalse($result);
  1276. $this->Form->setEntity('Contact.0.name');
  1277. $result = $this->Form->tagIsInvalid();
  1278. $this->assertFalse($result);
  1279. }
  1280. /**
  1281. * Test tagIsInvalid with validation errors from a saveMany
  1282. *
  1283. * @return void
  1284. */
  1285. public function testTagIsInvalidSaveMany() {
  1286. $Contact = ClassRegistry::getObject('Contact');
  1287. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1288. $this->Form->create('Contact');
  1289. $this->Form->setEntity('0.email');
  1290. $result = $this->Form->tagIsInvalid();
  1291. $this->assertEquals($expected, $result);
  1292. $this->Form->setEntity('0.Contact.email');
  1293. $result = $this->Form->tagIsInvalid();
  1294. $this->assertEquals($expected, $result);
  1295. }
  1296. /**
  1297. * Test validation errors.
  1298. *
  1299. * @return void
  1300. */
  1301. public function testPasswordValidation() {
  1302. $Contact = ClassRegistry::getObject('Contact');
  1303. $Contact->validationErrors['password'] = array('Please provide a password');
  1304. $result = $this->Form->input('Contact.password');
  1305. $expected = array(
  1306. 'div' => array('class' => 'input password error'),
  1307. 'label' => array('for' => 'ContactPassword'),
  1308. 'Password',
  1309. '/label',
  1310. 'input' => array(
  1311. 'type' => 'password', 'name' => 'Contact[password]',
  1312. 'id' => 'ContactPassword', 'class' => 'form-error'
  1313. ),
  1314. array('div' => array('class' => 'error-message')),
  1315. 'Please provide a password',
  1316. '/div',
  1317. '/div'
  1318. );
  1319. $this->assertTags($result, $expected);
  1320. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1321. $expected = array(
  1322. 'div' => array('class' => 'input password error'),
  1323. 'label' => array('for' => 'ContactPassword'),
  1324. 'Password',
  1325. '/label',
  1326. 'input' => array(
  1327. 'type' => 'password', 'name' => 'Contact[password]',
  1328. 'id' => 'ContactPassword', 'class' => 'form-error'
  1329. ),
  1330. '/div'
  1331. );
  1332. $this->assertTags($result, $expected);
  1333. }
  1334. /**
  1335. * Test validation errors, when validation message is an empty string.
  1336. *
  1337. * @return void
  1338. */
  1339. public function testEmptyErrorValidation() {
  1340. $this->Form->validationErrors['Contact']['password'] = '';
  1341. $result = $this->Form->input('Contact.password');
  1342. $expected = array(
  1343. 'div' => array('class' => 'input password error'),
  1344. 'label' => array('for' => 'ContactPassword'),
  1345. 'Password',
  1346. '/label',
  1347. 'input' => array(
  1348. 'type' => 'password', 'name' => 'Contact[password]',
  1349. 'id' => 'ContactPassword', 'class' => 'form-error'
  1350. ),
  1351. array('div' => array('class' => 'error-message')),
  1352. array(),
  1353. '/div',
  1354. '/div'
  1355. );
  1356. $this->assertTags($result, $expected);
  1357. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1358. $expected = array(
  1359. 'div' => array('class' => 'input password error'),
  1360. 'label' => array('for' => 'ContactPassword'),
  1361. 'Password',
  1362. '/label',
  1363. 'input' => array(
  1364. 'type' => 'password', 'name' => 'Contact[password]',
  1365. 'id' => 'ContactPassword', 'class' => 'form-error'
  1366. ),
  1367. '/div'
  1368. );
  1369. $this->assertTags($result, $expected);
  1370. }
  1371. /**
  1372. * Test validation errors, when calling input() overriding validation message by an empty string.
  1373. *
  1374. * @return void
  1375. */
  1376. public function testEmptyInputErrorValidation() {
  1377. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1378. $result = $this->Form->input('Contact.password', array('error' => ''));
  1379. $expected = array(
  1380. 'div' => array('class' => 'input password error'),
  1381. 'label' => array('for' => 'ContactPassword'),
  1382. 'Password',
  1383. '/label',
  1384. 'input' => array(
  1385. 'type' => 'password', 'name' => 'Contact[password]',
  1386. 'id' => 'ContactPassword', 'class' => 'form-error'
  1387. ),
  1388. array('div' => array('class' => 'error-message')),
  1389. array(),
  1390. '/div',
  1391. '/div'
  1392. );
  1393. $this->assertTags($result, $expected);
  1394. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1395. $expected = array(
  1396. 'div' => array('class' => 'input password error'),
  1397. 'label' => array('for' => 'ContactPassword'),
  1398. 'Password',
  1399. '/label',
  1400. 'input' => array(
  1401. 'type' => 'password', 'name' => 'Contact[password]',
  1402. 'id' => 'ContactPassword', 'class' => 'form-error'
  1403. ),
  1404. '/div'
  1405. );
  1406. $this->assertTags($result, $expected);
  1407. }
  1408. /**
  1409. * testFormValidationAssociated method
  1410. *
  1411. * test display of form errors in conjunction with model::validates.
  1412. *
  1413. * @return void
  1414. */
  1415. public function testFormValidationAssociated() {
  1416. $this->UserForm = ClassRegistry::getObject('UserForm');
  1417. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1418. $data = array(
  1419. 'UserForm' => array('name' => 'user'),
  1420. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1421. );
  1422. $result = $this->UserForm->OpenidUrl->create($data);
  1423. $this->assertFalse(empty($result));
  1424. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1425. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1426. $encoding = strtolower(Configure::read('App.encoding'));
  1427. $expected = array(
  1428. 'form' => array(
  1429. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1430. 'accept-charset' => $encoding
  1431. ),
  1432. 'div' => array('style' => 'display:none;'),
  1433. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1434. '/div'
  1435. );
  1436. $this->assertTags($result, $expected);
  1437. $result = $this->Form->error(
  1438. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1439. );
  1440. $this->assertEquals('Error, not registered', $result);
  1441. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1442. }
  1443. /**
  1444. * testFormValidationAssociatedFirstLevel method
  1445. *
  1446. * test form error display with associated model.
  1447. *
  1448. * @return void
  1449. */
  1450. public function testFormValidationAssociatedFirstLevel() {
  1451. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1452. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1453. $data = array(
  1454. 'ValidateUser' => array('name' => 'mariano'),
  1455. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1456. );
  1457. $result = $this->ValidateUser->create($data);
  1458. $this->assertFalse(empty($result));
  1459. $this->assertFalse($this->ValidateUser->validates());
  1460. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1461. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1462. $encoding = strtolower(Configure::read('App.encoding'));
  1463. $expected = array(
  1464. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1465. 'div' => array('style' => 'display:none;'),
  1466. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1467. '/div'
  1468. );
  1469. $this->assertTags($result, $expected);
  1470. $result = $this->Form->error(
  1471. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1472. );
  1473. $this->assertEquals('Invalid email', $result);
  1474. $result = $this->Form->error(
  1475. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1476. );
  1477. $this->assertEquals('Invalid name', $result);
  1478. $result = $this->Form->error(
  1479. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1480. );
  1481. $this->assertEquals('Invalid city', $result);
  1482. unset($this->ValidateUser->ValidateProfile);
  1483. unset($this->ValidateUser);
  1484. }
  1485. /**
  1486. * testFormValidationAssociatedSecondLevel method
  1487. *
  1488. * test form error display with associated model.
  1489. *
  1490. * @return void
  1491. */
  1492. public function testFormValidationAssociatedSecondLevel() {
  1493. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1494. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1495. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1496. $data = array(
  1497. 'ValidateUser' => array('name' => 'mariano'),
  1498. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1499. 'ValidateItem' => array('name' => 'Item')
  1500. );
  1501. $result = $this->ValidateUser->create($data);
  1502. $this->assertFalse(empty($result));
  1503. $this->assertFalse($this->ValidateUser->validates());
  1504. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1505. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1506. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1507. $encoding = strtolower(Configure::read('App.encoding'));
  1508. $expected = array(
  1509. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1510. 'div' => array('style' => 'display:none;'),
  1511. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1512. '/div'
  1513. );
  1514. $this->assertTags($result, $expected);
  1515. $result = $this->Form->error(
  1516. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1517. );
  1518. $this->assertEquals('Invalid email', $result);
  1519. $result = $this->Form->error(
  1520. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1521. );
  1522. $this->assertEquals('Invalid name', $result);
  1523. $result = $this->Form->error(
  1524. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1525. );
  1526. $result = $this->Form->error(
  1527. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1528. );
  1529. $this->assertEquals('Invalid description', $result);
  1530. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1531. unset($this->ValidateUser->ValidateProfile);
  1532. unset($this->ValidateUser);
  1533. }
  1534. /**
  1535. * testFormValidationMultiRecord method
  1536. *
  1537. * test form error display with multiple records.
  1538. *
  1539. * @return void
  1540. */
  1541. public function testFormValidationMultiRecord() {
  1542. $Contact = ClassRegistry::getObject('Contact');
  1543. $Contact->validationErrors[2] = array(
  1544. 'name' => array('The provided value is invalid')
  1545. );
  1546. $result = $this->Form->input('Contact.2.name');
  1547. $expected = array(
  1548. 'div' => array('class' => 'input text error'),
  1549. 'label' => array('for' => 'Contact2Name'),
  1550. 'Name',
  1551. '/label',
  1552. 'input' => array(
  1553. 'type' => 'text', 'name' => 'Contact[2][name]', 'id' => 'Contact2Name',
  1554. 'class' => 'form-error', 'maxlength' => 255
  1555. ),
  1556. array('div' => array('class' => 'error-message')),
  1557. 'The provided value is invalid',
  1558. '/div',
  1559. '/div'
  1560. );
  1561. $this->assertTags($result, $expected);
  1562. }
  1563. /**
  1564. * testMultipleInputValidation method
  1565. *
  1566. * test multiple record form validation error display.
  1567. *
  1568. * @return void
  1569. */
  1570. public function testMultipleInputValidation() {
  1571. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1572. $Address->validationErrors[0] = array(
  1573. 'title' => array('This field cannot be empty'),
  1574. 'first_name' => array('This field cannot be empty')
  1575. );
  1576. $Address->validationErrors[1] = array(
  1577. 'last_name' => array('You must have a last name')
  1578. );
  1579. $this->Form->create();
  1580. $result = $this->Form->input('Address.0.title');
  1581. $expected = array(
  1582. 'div' => array('class'),
  1583. 'label' => array('for'),
  1584. 'preg:/[^<]+/',
  1585. '/label',
  1586. 'input' => array(
  1587. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1588. ),
  1589. array('div' => array('class' => 'error-message')),
  1590. 'This field cannot be empty',
  1591. '/div',
  1592. '/div'
  1593. );
  1594. $this->assertTags($result, $expected);
  1595. $result = $this->Form->input('Address.0.first_name');
  1596. $expected = array(
  1597. 'div' => array('class'),
  1598. 'label' => array('for'),
  1599. 'preg:/[^<]+/',
  1600. '/label',
  1601. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1602. array('div' => array('class' => 'error-message')),
  1603. 'This field cannot be empty',
  1604. '/div',
  1605. '/div'
  1606. );
  1607. $this->assertTags($result, $expected);
  1608. $result = $this->Form->input('Address.0.last_name');
  1609. $expected = array(
  1610. 'div' => array('class'),
  1611. 'label' => array('for'),
  1612. 'preg:/[^<]+/',
  1613. '/label',
  1614. 'input' => array('type' => 'text', 'name', 'id'),
  1615. '/div'
  1616. );
  1617. $this->assertTags($result, $expected);
  1618. $result = $this->Form->input('Address.1.last_name');
  1619. $expected = array(
  1620. 'div' => array('class'),
  1621. 'label' => array('for'),
  1622. 'preg:/[^<]+/',
  1623. '/label',
  1624. 'input' => array(
  1625. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1626. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1627. ),
  1628. array('div' => array('class' => 'error-message')),
  1629. 'You must have a last name',
  1630. '/div',
  1631. '/div'
  1632. );
  1633. $this->assertTags($result, $expected);
  1634. }
  1635. /**
  1636. * testInput method
  1637. *
  1638. * Test various incarnations of input().
  1639. *
  1640. * @return void
  1641. */
  1642. public function testInput() {
  1643. $result = $this->Form->input('ValidateUser.balance');
  1644. $expected = array(
  1645. 'div' => array('class'),
  1646. 'label' => array('for'),
  1647. 'Balance',
  1648. '/label',
  1649. 'input' => array('name', 'type' => 'number', 'id'),
  1650. '/div',
  1651. );
  1652. $this->assertTags($result, $expected);
  1653. $result = $this->Form->input('ValidateUser.cost_decimal');
  1654. $expected = array(
  1655. 'div' => array('class'),
  1656. 'label' => array('for'),
  1657. 'Cost Decimal',
  1658. '/label',
  1659. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  1660. '/div',
  1661. );
  1662. $this->assertTags($result, $expected);
  1663. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1664. $expected = array(
  1665. 'div' => array('class' => 'input email'),
  1666. 'label' => array('for' => 'custom'),
  1667. 'Email',
  1668. '/label',
  1669. array('input' => array(
  1670. 'type' => 'email', 'name' => 'Contact[email]',
  1671. 'id' => 'custom', 'maxlength' => 255
  1672. )),
  1673. '/div'
  1674. );
  1675. $this->assertTags($result, $expected);
  1676. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1677. $expected = array(
  1678. '<div',
  1679. 'label' => array('for' => 'ContactEmail'),
  1680. 'Email',
  1681. '/label',
  1682. array('input' => array(
  1683. 'type' => 'email', 'name' => 'Contact[email]',
  1684. 'id' => 'ContactEmail', 'maxlength' => 255
  1685. )),
  1686. '/div'
  1687. );
  1688. $this->assertTags($result, $expected);
  1689. $result = $this->Form->hidden('Contact.idontexist');
  1690. $expected = array('input' => array(
  1691. 'type' => 'hidden', 'name' => 'Contact[idontexist]',
  1692. 'id' => 'ContactIdontexist'
  1693. ));
  1694. $this->assertTags($result, $expected);
  1695. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1696. $expected = array(
  1697. 'div' => array('class' => 'input text'),
  1698. 'label' => array('for' => 'ContactEmail'),
  1699. 'Email',
  1700. '/label',
  1701. array('input' => array(
  1702. 'type' => 'text', 'name' => 'Contact[email]',
  1703. 'id' => 'ContactEmail'
  1704. )),
  1705. '/div'
  1706. );
  1707. $this->assertTags($result, $expected);
  1708. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1709. $expected = array(
  1710. 'div' => array('class' => 'input text'),
  1711. 'label' => array('for' => 'Contact5Email'),
  1712. 'Email',
  1713. '/label',
  1714. array('input' => array(
  1715. 'type' => 'text', 'name' => 'Contact[5][email]',
  1716. 'id' => 'Contact5Email'
  1717. )),
  1718. '/div'
  1719. );
  1720. $this->assertTags($result, $expected);
  1721. $result = $this->Form->input('Contact.password');
  1722. $expected = array(
  1723. 'div' => array('class' => 'input password'),
  1724. 'label' => array('for' => 'ContactPassword'),
  1725. 'Password',
  1726. '/label',
  1727. array('input' => array(
  1728. 'type' => 'password', 'name' => 'Contact[password]',
  1729. 'id' => 'ContactPassword'
  1730. )),
  1731. '/div'
  1732. );
  1733. $this->assertTags($result, $expected);
  1734. $result = $this->Form->input('Contact.email', array(
  1735. 'type' => 'file', 'class' => 'textbox'
  1736. ));
  1737. $expected = array(
  1738. 'div' => array('class' => 'input file'),
  1739. 'label' => array('for' => 'ContactEmail'),
  1740. 'Email',
  1741. '/label',
  1742. array('input' => array(
  1743. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  1744. 'id' => 'ContactEmail'
  1745. )),
  1746. '/div'
  1747. );
  1748. $this->assertTags($result, $expected);
  1749. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1750. $result = $this->Form->input('Contact.phone');
  1751. $expected = array(
  1752. 'div' => array('class' => 'input tel'),
  1753. 'label' => array('for' => 'ContactPhone'),
  1754. 'Phone',
  1755. '/label',
  1756. array('input' => array(
  1757. 'type' => 'tel', 'name' => 'Contact[phone]',
  1758. 'value' => 'Hello &amp; World &gt; weird chars',
  1759. 'id' => 'ContactPhone', 'maxlength' => 255
  1760. )),
  1761. '/div'
  1762. );
  1763. $this->assertTags($result, $expected);
  1764. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1765. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1766. $expected = array(
  1767. 'div' => array('class' => 'input text'),
  1768. 'label' => array('for' => 'myId'),
  1769. 'Field',
  1770. '/label',
  1771. 'input' => array(
  1772. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  1773. 'value' => 'My value', 'id' => 'myId'
  1774. ),
  1775. '/div'
  1776. );
  1777. $this->assertTags($result, $expected);
  1778. unset($this->Form->request->data);
  1779. $Contact = ClassRegistry::getObject('Contact');
  1780. $Contact->validationErrors['field'] = array('Badness!');
  1781. $result = $this->Form->input('Contact.field');
  1782. $expected = array(
  1783. 'div' => array('class' => 'input text error'),
  1784. 'label' => array('for' => 'ContactField'),
  1785. 'Field',
  1786. '/label',
  1787. 'input' => array(
  1788. 'type' => 'text', 'name' => 'Contact[field]',
  1789. 'id' => 'ContactField', 'class' => 'form-error'
  1790. ),
  1791. array('div' => array('class' => 'error-message')),
  1792. 'Badness!',
  1793. '/div',
  1794. '/div'
  1795. );
  1796. $this->assertTags($result, $expected);
  1797. $result = $this->Form->input('Contact.field', array(
  1798. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1799. ));
  1800. $expected = array(
  1801. 'label' => array('for' => 'ContactField'),
  1802. 'Field',
  1803. '/label',
  1804. 'input' => array(
  1805. 'type' => 'text', 'name' => 'Contact[field]',
  1806. 'id' => 'ContactField', 'class' => 'form-error'
  1807. ),
  1808. array('span' => array('class' => 'error-message')),
  1809. 'Badness!',
  1810. '/span'
  1811. );
  1812. $this->assertTags($result, $expected);
  1813. $result = $this->Form->input('Contact.field', array(
  1814. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1815. ));
  1816. $expected = array(
  1817. 'div' => array('class' => 'input text error'),
  1818. 'label' => array('for' => 'ContactField'),
  1819. 'Field',
  1820. '/label',
  1821. 'input' => array(
  1822. 'type' => 'text', 'name' => 'Contact[field]',
  1823. 'id' => 'ContactField', 'class' => 'form-error'
  1824. ),
  1825. array('div' => array('class' => 'error')),
  1826. 'Badness!',
  1827. '/div'
  1828. );
  1829. $this->assertTags($result, $expected);
  1830. $result = $this->Form->input('Contact.field', array(
  1831. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1832. ));
  1833. $expected = array(
  1834. 'span' => array('class' => 'input text error'),
  1835. 'label' => array('for' => 'ContactField'),
  1836. 'Field',
  1837. '/label',
  1838. 'input' => array(
  1839. 'type' => 'text', 'name' => 'Contact[field]',
  1840. 'id' => 'ContactField', 'class' => 'form-error'
  1841. ),
  1842. 'Badness!',
  1843. '/span'
  1844. );
  1845. $this->assertTags($result, $expected);
  1846. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1847. $expected = array(
  1848. 'div' => array('class' => 'input text error'),
  1849. 'label' => array('for' => 'ContactField'),
  1850. 'Field',
  1851. '/label',
  1852. 'input' => array(
  1853. 'type' => 'text', 'name' => 'Contact[field]',
  1854. 'id' => 'ContactField', 'class' => 'form-error'
  1855. ),
  1856. 'A message to you, Rudy',
  1857. array('div' => array('class' => 'error-message')),
  1858. 'Badness!',
  1859. '/div',
  1860. '/div'
  1861. );
  1862. $this->assertTags($result, $expected);
  1863. $this->Form->setEntity(null);
  1864. $this->Form->setEntity('Contact.field');
  1865. $result = $this->Form->input('Contact.field', array(
  1866. 'after' => 'A message to you, Rudy', 'error' => false
  1867. ));
  1868. $expected = array(
  1869. 'div' => array('class' => 'input text'),
  1870. 'label' => array('for' => 'ContactField'),
  1871. 'Field',
  1872. '/label',
  1873. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1874. 'A message to you, Rudy',
  1875. '/div'
  1876. );
  1877. $this->assertTags($result, $expected);
  1878. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1879. $expected = array(
  1880. 'div' => array('class' => 'input text'),
  1881. 'label' => array('for' => 'ObjectField'),
  1882. 'Field',
  1883. '/label',
  1884. 'input' => array('type' => 'text', 'name' => 'Object[field]', 'id' => 'ObjectField'),
  1885. 'A message to you, Rudy',
  1886. '/div'
  1887. );
  1888. $this->assertTags($result, $expected);
  1889. $Contact->validationErrors['field'] = array('minLength');
  1890. $result = $this->Form->input('Contact.field', array(
  1891. 'error' => array(
  1892. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1893. 'maxLength' => 'login too large'
  1894. )
  1895. ));
  1896. $expected = array(
  1897. 'div' => array('class' => 'input text error'),
  1898. 'label' => array('for' => 'ContactField'),
  1899. 'Field',
  1900. '/label',
  1901. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1902. array('div' => array('class' => 'error-message')),
  1903. 'Le login doit contenir au moins 2 caractères',
  1904. '/div',
  1905. '/div'
  1906. );
  1907. $this->assertTags($result, $expected);
  1908. $Contact->validationErrors['field'] = array('maxLength');
  1909. $result = $this->Form->input('Contact.field', array(
  1910. 'error' => array(
  1911. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1912. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1913. 'maxLength' => 'login too large',
  1914. )
  1915. ));
  1916. $expected = array(
  1917. 'div' => array('class' => 'input text error'),
  1918. 'label' => array('for' => 'ContactField'),
  1919. 'Field',
  1920. '/label',
  1921. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1922. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1923. 'login too large',
  1924. '/span',
  1925. '/div'
  1926. );
  1927. $this->assertTags($result, $expected);
  1928. }
  1929. /**
  1930. * Test that inputs with 0 can be created.
  1931. *
  1932. * @return void
  1933. */
  1934. public function testInputZero() {
  1935. $this->Form->create('User');
  1936. $result = $this->Form->input('0');
  1937. $expected = array(
  1938. 'div' => array('class' => 'input text'),
  1939. 'label' => array('for' => 'User0'), '/label',
  1940. 'input' => array('type' => 'text', 'name' => 'User[0]', 'id' => 'User0'),
  1941. '/div'
  1942. );
  1943. $this->assertTags($result, $expected);
  1944. }
  1945. /**
  1946. * test input() with checkbox creation
  1947. *
  1948. * @return void
  1949. */
  1950. public function testInputCheckbox() {
  1951. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1952. $expected = array(
  1953. 'div' => array('class' => 'input checkbox'),
  1954. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  1955. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1956. '/div'
  1957. );
  1958. $this->assertTags($result, $expected);
  1959. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1960. $expected = array(
  1961. 'div' => array('class' => 'input checkbox'),
  1962. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  1963. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1964. '/div'
  1965. );
  1966. $this->assertTags($result, $expected);
  1967. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1968. $expected = array(
  1969. 'div' => array('class' => 'input checkbox'),
  1970. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  1971. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1972. '/div'
  1973. );
  1974. $this->assertTags($result, $expected);
  1975. $result = $this->Form->input('User.disabled', array(
  1976. 'label' => 'Disabled',
  1977. 'type' => 'checkbox',
  1978. 'data-foo' => 'disabled'
  1979. ));
  1980. $expected = array(
  1981. 'div' => array('class' => 'input checkbox'),
  1982. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  1983. array('input' => array(
  1984. 'type' => 'checkbox',
  1985. 'name' => 'data[User][disabled]',
  1986. 'value' => '1',
  1987. 'id' => 'UserDisabled',
  1988. 'data-foo' => 'disabled'
  1989. )),
  1990. 'label' => array('for' => 'UserDisabled'),
  1991. 'Disabled',
  1992. '/label',
  1993. '/div'
  1994. );
  1995. $this->assertTags($result, $expected);
  1996. }
  1997. /**
  1998. * test form->input() with time types.
  1999. *
  2000. */
  2001. public function testInputTime() {
  2002. extract($this->dateRegex);
  2003. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2004. $result = explode(':', $result);
  2005. $this->assertRegExp('/option value="23"/', $result[0]);
  2006. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2007. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2008. $result = explode(':', $result);
  2009. $this->assertRegExp('/option value="23"/', $result[0]);
  2010. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2011. $result = $this->Form->input('Model.field', array(
  2012. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2013. ));
  2014. $result = explode(':', $result);
  2015. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2016. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2017. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2018. $result = $this->Form->input('Model.field', array(
  2019. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2020. ));
  2021. $result = explode(':', $result);
  2022. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2023. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2024. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2025. $result = $this->Form->input('prueba', array(
  2026. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2027. 'maxYear' => date('Y') + 1, 'interval' => 15
  2028. ));
  2029. $result = explode(':', $result);
  2030. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2031. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2032. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2033. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2034. $result = $this->Form->input('Random.start_time', array(
  2035. 'type' => 'time',
  2036. 'selected' => '18:15'
  2037. ));
  2038. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2039. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2040. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2041. $result = $this->Form->input('published', array('type' => 'time'));
  2042. $now = strtotime('now');
  2043. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2044. $now = strtotime('2013-03-09 00:42:21');
  2045. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2046. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2047. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2048. }
  2049. /**
  2050. * Test interval + selected near the hour roll over.
  2051. *
  2052. * @return void
  2053. */
  2054. public function testTimeSelectedWithInterval() {
  2055. $result = $this->Form->input('Model.start_time', array(
  2056. 'type' => 'time',
  2057. 'interval' => 15,
  2058. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2059. ));
  2060. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2061. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2062. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2063. $result = $this->Form->input('Model.start_time', array(
  2064. 'type' => 'time',
  2065. 'interval' => 15,
  2066. 'selected' => '2012-10-23 15:57:00'
  2067. ));
  2068. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2069. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2070. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2071. $result = $this->Form->input('Model.start_time', array(
  2072. 'timeFormat' => 24,
  2073. 'type' => 'time',
  2074. 'interval' => 15,
  2075. 'selected' => '15:57'
  2076. ));
  2077. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2078. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2079. $result = $this->Form->input('Model.start_time', array(
  2080. 'timeFormat' => 24,
  2081. 'type' => 'time',
  2082. 'interval' => 15,
  2083. 'selected' => '23:57'
  2084. ));
  2085. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2086. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2087. $result = $this->Form->input('Model.created', array(
  2088. 'timeFormat' => 24,
  2089. 'type' => 'datetime',
  2090. 'interval' => 15,
  2091. 'selected' => '2012-09-30 23:56'
  2092. ));
  2093. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2094. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2095. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2096. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2097. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2098. }
  2099. /**
  2100. * Test time with selected values around 12:xx:xx
  2101. *
  2102. * @return void
  2103. */
  2104. public function testTimeSelectedWithIntervalTwelve() {
  2105. $result = $this->Form->input('Model.start_time', array(
  2106. 'type' => 'time',
  2107. 'timeFormat' => 12,
  2108. 'interval' => 15,
  2109. 'selected' => '00:00:00'
  2110. ));
  2111. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2112. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2113. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2114. $result = $this->Form->input('Model.start_time', array(
  2115. 'type' => 'time',
  2116. 'timeFormat' => 12,
  2117. 'interval' => 15,
  2118. 'selected' => '12:00:00'
  2119. ));
  2120. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2121. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2122. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2123. $result = $this->Form->input('Model.start_time', array(
  2124. 'type' => 'time',
  2125. 'timeFormat' => 12,
  2126. 'interval' => 15,
  2127. 'selected' => '12:15:00'
  2128. ));
  2129. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2130. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2131. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2132. }
  2133. /**
  2134. * Test interval & timeFormat = 12
  2135. *
  2136. * @return void
  2137. */
  2138. public function testInputTimeWithIntervalAnd12HourFormat() {
  2139. $result = $this->Form->input('Model.start_time', array(
  2140. 'type' => 'time',
  2141. 'timeFormat' => 12,
  2142. 'interval' => 5,
  2143. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2144. ));
  2145. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2146. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2147. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2148. $result = $this->Form->input('Model.start_time', array(
  2149. 'type' => 'time',
  2150. 'timeFormat' => '12',
  2151. 'interval' => 5,
  2152. 'selected' => '2013-04-19 16:30:00'
  2153. ));
  2154. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2155. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2156. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2157. $result = $this->Form->input('Model.start_time', array(
  2158. 'type' => 'time',
  2159. 'timeFormat' => '12',
  2160. 'interval' => 10,
  2161. 'selected' => '2013-05-19 00:33:00'
  2162. ));
  2163. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2164. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2165. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2166. $result = $this->Form->input('Model.start_time', array(
  2167. 'type' => 'time',
  2168. 'timeFormat' => '12',
  2169. 'interval' => 10,
  2170. 'selected' => '2013-05-19 13:33:00'
  2171. ));
  2172. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2173. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2174. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2175. $result = $this->Form->input('Model.start_time', array(
  2176. 'type' => 'time',
  2177. 'timeFormat' => '12',
  2178. 'interval' => 10,
  2179. 'selected' => '2013-05-19 01:33:00'
  2180. ));
  2181. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2182. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2183. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2184. }
  2185. /**
  2186. * test form->input() with datetime, date and time types
  2187. *
  2188. * @return void
  2189. */
  2190. public function testInputDatetime() {
  2191. extract($this->dateRegex);
  2192. $result = $this->Form->input('prueba', array(
  2193. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2194. 'maxYear' => date('Y') + 1, 'interval' => 15
  2195. ));
  2196. $result = explode(':', $result);
  2197. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2198. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2199. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2200. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2201. //related to ticket #5013
  2202. $result = $this->Form->input('Contact.date', array(
  2203. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2204. ));
  2205. $this->assertRegExp('/class="customClass"/', $result);
  2206. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2207. $result = $this->Form->input('Contact.date', array(
  2208. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2209. ));
  2210. $this->assertRegExp('/id="customIdDay"/', $result);
  2211. $this->assertRegExp('/id="customIdMonth"/', $result);
  2212. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2213. $result = $this->Form->input('Model.field', array(
  2214. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2215. ));
  2216. $this->assertRegExp('/id="customIDDay"/', $result);
  2217. $this->assertRegExp('/id="customIDHour"/', $result);
  2218. $result = explode('</select><select', $result);
  2219. $result = explode(':', $result[1]);
  2220. $this->assertRegExp('/option value="23"/', $result[0]);
  2221. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2222. $result = $this->Form->input('Model.field', array(
  2223. 'type' => 'datetime', 'timeFormat' => 12
  2224. ));
  2225. $result = explode('</select><select', $result);
  2226. $result = explode(':', $result[1]);
  2227. $this->assertRegExp('/option value="12"/', $result[0]);
  2228. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2229. $this->Form->request->data = array('Contact' => array('created' => null));
  2230. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2231. $expected = array(
  2232. 'div' => array('class' => 'input date'),
  2233. 'label' => array('for' => 'ContactCreatedMonth'),
  2234. 'Created',
  2235. '/label',
  2236. array('select' => array('name' => 'Contact[created][month]', 'id' => 'ContactCreatedMonth')),
  2237. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2238. $monthsRegex,
  2239. '/select', '-',
  2240. array('select' => array('name' => 'Contact[created][day]', 'id' => 'ContactCreatedDay')),
  2241. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2242. $daysRegex,
  2243. '/select', '-',
  2244. array('select' => array('name' => 'Contact[created][year]', 'id' => 'ContactCreatedYear')),
  2245. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2246. $yearsRegex,
  2247. '/select',
  2248. '/div'
  2249. );
  2250. $this->assertTags($result, $expected);
  2251. $this->Form->request->data = array('Contact' => array('created' => null));
  2252. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2253. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2254. $this->Form->request->data = array('Contact' => array('created' => null));
  2255. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2256. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2257. $result = $this->Form->input('Contact.created', array(
  2258. 'type' => 'date',
  2259. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2260. 'timeFormat' => 'NONE'
  2261. ));
  2262. $this->assertRegExp('/for\="created-month"/', $result);
  2263. }
  2264. /**
  2265. * Test generating checkboxes in a loop.
  2266. *
  2267. * @return void
  2268. */
  2269. public function testInputCheckboxesInLoop() {
  2270. for ($i = 1; $i < 5; $i++) {
  2271. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2272. $expected = array(
  2273. 'div' => array('class' => 'input checkbox'),
  2274. 'input' => array('type' => 'hidden', 'name' => "Contact[{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2275. array('input' => array('type' => 'checkbox', 'name' => "Contact[{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2276. 'label' => array('for' => "Contact{$i}Email"),
  2277. 'Email',
  2278. '/label',
  2279. '/div'
  2280. );
  2281. $this->assertTags($result, $expected);
  2282. }
  2283. }
  2284. /**
  2285. * Test generating checkboxes with disabled elements.
  2286. *
  2287. * @return void
  2288. */
  2289. public function testInputCheckboxWithDisabledElements() {
  2290. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2291. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2292. $expected = array(
  2293. array('div' => array('class' => 'input select')),
  2294. array('label' => array('for' => "ContactMultiple")),
  2295. 'Multiple',
  2296. '/label',
  2297. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2298. array('div' => array('class' => 'checkbox')),
  2299. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2300. array('label' => array('for' => "ContactMultiple1")),
  2301. 'One',
  2302. '/label',
  2303. '/div',
  2304. array('div' => array('class' => 'checkbox')),
  2305. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2306. array('label' => array('for' => "ContactMultiple2")),
  2307. 'Two',
  2308. '/label',
  2309. '/div',
  2310. array('div' => array('class' => 'checkbox')),
  2311. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2312. array('label' => array('for' => "ContactMultiple3")),
  2313. 'Three',
  2314. '/label',
  2315. '/div',
  2316. '/div'
  2317. );
  2318. $this->assertTags($result, $expected);
  2319. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2320. $this->assertTags($result, $expected);
  2321. $disabled = array('2', 3);
  2322. $expected = array(
  2323. array('div' => array('class' => 'input select')),
  2324. array('label' => array('for' => "ContactMultiple")),
  2325. 'Multiple',
  2326. '/label',
  2327. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2328. array('div' => array('class' => 'checkbox')),
  2329. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2330. array('label' => array('for' => "ContactMultiple1")),
  2331. 'One',
  2332. '/label',
  2333. '/div',
  2334. array('div' => array('class' => 'checkbox')),
  2335. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2336. array('label' => array('for' => "ContactMultiple2")),
  2337. 'Two',
  2338. '/label',
  2339. '/div',
  2340. array('div' => array('class' => 'checkbox')),
  2341. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2342. array('label' => array('for' => "ContactMultiple3")),
  2343. 'Three',
  2344. '/label',
  2345. '/div',
  2346. '/div'
  2347. );
  2348. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2349. $this->assertTags($result, $expected);
  2350. // make sure 50 does only disable 50, and not 50f5c0cf
  2351. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2352. $disabled = array(50);
  2353. $expected = array(
  2354. array('div' => array('class' => 'input select')),
  2355. array('label' => array('for' => "ContactMultiple")),
  2356. 'Multiple',
  2357. '/label',
  2358. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2359. array('div' => array('class' => 'checkbox')),
  2360. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2361. array('label' => array('for' => "ContactMultiple50")),
  2362. 'Fifty',
  2363. '/label',
  2364. '/div',
  2365. array('div' => array('class' => 'checkbox')),
  2366. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2367. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2368. 'Stringy',
  2369. '/label',
  2370. '/div',
  2371. '/div'
  2372. );
  2373. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2374. $this->assertTags($result, $expected);
  2375. }
  2376. /**
  2377. * test input name with leading integer, ensure attributes are generated correctly.
  2378. *
  2379. * @return void
  2380. */
  2381. public function testInputWithLeadingInteger() {
  2382. $result = $this->Form->text('0.Node.title');
  2383. $expected = array(
  2384. 'input' => array('name' => '0[Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2385. );
  2386. $this->assertTags($result, $expected);
  2387. }
  2388. /**
  2389. * test form->input() with select type inputs.
  2390. *
  2391. * @return void
  2392. */
  2393. public function testInputSelectType() {
  2394. $result = $this->Form->input('email', array(
  2395. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2396. );
  2397. $expected = array(
  2398. 'div' => array('class' => 'input select'),
  2399. 'label' => array('for' => 'email'),
  2400. 'Email',
  2401. '/label',
  2402. array('select' => array('name' => 'email', 'id' => 'email')),
  2403. array('option' => array('value' => '')),
  2404. '/option',
  2405. array('option' => array('value' => 'è')),
  2406. 'Firést',
  2407. '/option',
  2408. array('option' => array('value' => 'é')),
  2409. 'Secoènd',
  2410. '/option',
  2411. '/select',
  2412. '/div'
  2413. );
  2414. $this->assertTags($result, $expected);
  2415. $result = $this->Form->input('email', array(
  2416. 'options' => array('First', 'Second'), 'empty' => true)
  2417. );
  2418. $expected = array(
  2419. 'div' => array('class' => 'input select'),
  2420. 'label' => array('for' => 'email'),
  2421. 'Email',
  2422. '/label',
  2423. array('select' => array('name' => 'email', 'id' => 'email')),
  2424. array('option' => array('value' => '')),
  2425. '/option',
  2426. array('option' => array('value' => '0')),
  2427. 'First',
  2428. '/option',
  2429. array('option' => array('value' => '1')),
  2430. 'Second',
  2431. '/option',
  2432. '/select',
  2433. '/div'
  2434. );
  2435. $this->assertTags($result, $expected);
  2436. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2437. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2438. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2439. $expected = array(
  2440. 'div' => array('class' => 'input select'),
  2441. 'label' => array('for' => 'ModelUserId'),
  2442. 'User',
  2443. '/label',
  2444. 'select' => array('name' => 'Model[user_id]', 'id' => 'ModelUserId'),
  2445. array('option' => array('value' => '')),
  2446. '/option',
  2447. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2448. 'good',
  2449. '/option',
  2450. array('option' => array('value' => 'other')),
  2451. 'bad',
  2452. '/option',
  2453. '/select',
  2454. '/div'
  2455. );
  2456. $this->assertTags($result, $expected);
  2457. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2458. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2459. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2460. $expected = array(
  2461. 'div' => array('class' => 'input select'),
  2462. 'label' => array('for' => 'ThingUserId'),
  2463. 'User',
  2464. '/label',
  2465. 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'),
  2466. array('option' => array('value' => '')),
  2467. 'Some Empty',
  2468. '/option',
  2469. array('option' => array('value' => 'value')),
  2470. 'good',
  2471. '/option',
  2472. array('option' => array('value' => 'other')),
  2473. 'bad',
  2474. '/option',
  2475. '/select',
  2476. '/div'
  2477. );
  2478. $this->assertTags($result, $expected);
  2479. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2480. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2481. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2482. $expected = array(
  2483. 'div' => array('class' => 'input select'),
  2484. 'label' => array('for' => 'ThingUserId'),
  2485. 'User',
  2486. '/label',
  2487. 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'),
  2488. array('option' => array('value' => '')),
  2489. 'Some Empty',
  2490. '/option',
  2491. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2492. 'good',
  2493. '/option',
  2494. array('option' => array('value' => 'other')),
  2495. 'bad',
  2496. '/option',
  2497. '/select',
  2498. '/div'
  2499. );
  2500. $this->assertTags($result, $expected);
  2501. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2502. $this->Form->request->data = array('User' => array('User' => array('value')));
  2503. $result = $this->Form->input('User.User', array('empty' => true));
  2504. $expected = array(
  2505. 'div' => array('class' => 'input select'),
  2506. 'label' => array('for' => 'UserUser'),
  2507. 'User',
  2508. '/label',
  2509. 'input' => array('type' => 'hidden', 'name' => 'User[User]', 'value' => '', 'id' => 'UserUser_'),
  2510. 'select' => array('name' => 'User[User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2511. array('option' => array('value' => '')),
  2512. '/option',
  2513. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2514. 'good',
  2515. '/option',
  2516. array('option' => array('value' => 'other')),
  2517. 'bad',
  2518. '/option',
  2519. '/select',
  2520. '/div'
  2521. );
  2522. $this->assertTags($result, $expected);
  2523. $this->Form->data = array();
  2524. $result = $this->Form->input('Publisher.id', array(
  2525. 'label' => 'Publisher',
  2526. 'type' => 'select',
  2527. 'multiple' => 'checkbox',
  2528. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2529. ));
  2530. $expected = array(
  2531. array('div' => array('class' => 'input select')),
  2532. array('label' => array('for' => 'PublisherId')),
  2533. 'Publisher',
  2534. '/label',
  2535. 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'PublisherId'),
  2536. array('div' => array('class' => 'checkbox')),
  2537. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2538. array('label' => array('for' => 'PublisherIdValue1')),
  2539. 'Label 1',
  2540. '/label',
  2541. '/div',
  2542. array('div' => array('class' => 'checkbox')),
  2543. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2544. array('label' => array('for' => 'PublisherIdValue2')),
  2545. 'Label 2',
  2546. '/label',
  2547. '/div',
  2548. '/div'
  2549. );
  2550. $this->assertTags($result, $expected);
  2551. }
  2552. /**
  2553. * test that input() and a non standard primary key makes a hidden input by default.
  2554. *
  2555. * @return void
  2556. */
  2557. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2558. $this->Form->create('User');
  2559. $this->Form->fieldset = array(
  2560. 'User' => array(
  2561. 'fields' => array(
  2562. 'model_id' => array('type' => 'integer')
  2563. ),
  2564. 'validates' => array(),
  2565. 'key' => 'model_id'
  2566. )
  2567. );
  2568. $result = $this->Form->input('model_id');
  2569. $expected = array(
  2570. 'input' => array('type' => 'hidden', 'name' => 'User[model_id]', 'id' => 'UserModelId'),
  2571. );
  2572. $this->assertTags($result, $expected);
  2573. }
  2574. /**
  2575. * test that overriding the magic select type widget is possible
  2576. *
  2577. * @return void
  2578. */
  2579. public function testInputOverridingMagicSelectType() {
  2580. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2581. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2582. $expected = array(
  2583. 'div' => array('class' => 'input text'),
  2584. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2585. 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2586. '/div'
  2587. );
  2588. $this->assertTags($result, $expected);
  2589. //Check that magic types still work for plural/singular vars
  2590. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2591. $result = $this->Form->input('Model.type');
  2592. $expected = array(
  2593. 'div' => array('class' => 'input select'),
  2594. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2595. 'select' => array('name' => 'Model[type]', 'id' => 'ModelType'),
  2596. array('option' => array('value' => 'value')), 'good', '/option',
  2597. array('option' => array('value' => 'other')), 'bad', '/option',
  2598. '/select',
  2599. '/div'
  2600. );
  2601. $this->assertTags($result, $expected);
  2602. }
  2603. /**
  2604. * Test that inferred types do not override developer input
  2605. *
  2606. * @return void
  2607. */
  2608. public function testInputMagicTypeDoesNotOverride() {
  2609. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2610. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2611. $expected = array(
  2612. 'div' => array('class' => 'input checkbox'),
  2613. array('input' => array(
  2614. 'type' => 'hidden',
  2615. 'name' => 'data[Model][user]',
  2616. 'id' => 'ModelUser_',
  2617. 'value' => 0,
  2618. )),
  2619. array('input' => array(
  2620. 'name' => 'data[Model][user]',
  2621. 'type' => 'checkbox',
  2622. 'id' => 'ModelUser',
  2623. 'value' => 1
  2624. )),
  2625. 'label' => array('for' => 'ModelUser'), 'User', '/label',
  2626. '/div'
  2627. );
  2628. $this->assertTags($result, $expected);
  2629. }
  2630. /**
  2631. * Test that magic input() selects are created for type=number
  2632. *
  2633. * @return void
  2634. */
  2635. public function testInputMagicSelectForTypeNumber() {
  2636. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2637. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2638. $result = $this->Form->input('ValidateUser.balance');
  2639. $expected = array(
  2640. 'div' => array('class' => 'input select'),
  2641. 'label' => array('for' => 'ValidateUserBalance'),
  2642. 'Balance',
  2643. '/label',
  2644. 'select' => array('name' => 'ValidateUser[balance]', 'id' => 'ValidateUserBalance'),
  2645. array('option' => array('value' => '0')),
  2646. 'nothing',
  2647. '/option',
  2648. array('option' => array('value' => '1', 'selected' => 'selected')),
  2649. 'some',
  2650. '/option',
  2651. array('option' => array('value' => '100')),
  2652. 'a lot',
  2653. '/option',
  2654. '/select',
  2655. '/div'
  2656. );
  2657. $this->assertTags($result, $expected);
  2658. }
  2659. /**
  2660. * Test that magic input() selects can easily be converted into radio types without error.
  2661. *
  2662. * @return void
  2663. */
  2664. public function testInputMagicSelectChangeToRadio() {
  2665. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2666. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2667. $this->assertRegExp('/input type="radio"/', $result);
  2668. }
  2669. /**
  2670. * fields with the same name as the model should work.
  2671. *
  2672. * @return void
  2673. */
  2674. public function testInputWithMatchingFieldAndModelName() {
  2675. $this->Form->create('User');
  2676. $this->Form->fieldset = array(
  2677. 'User' => array(
  2678. 'fields' => array(
  2679. 'User' => array('type' => 'text')
  2680. ),
  2681. 'validates' => array(),
  2682. 'key' => 'id'
  2683. )
  2684. );
  2685. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2686. $result = $this->Form->input('User', array('type' => 'text'));
  2687. $expected = array(
  2688. 'div' => array('class' => 'input text'),
  2689. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2690. 'input' => array('name' => 'User[User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2691. '/div'
  2692. );
  2693. $this->assertTags($result, $expected);
  2694. }
  2695. /**
  2696. * testFormInputs method
  2697. *
  2698. * test correct results from form::inputs().
  2699. *
  2700. * @return void
  2701. */
  2702. public function testFormInputs() {
  2703. $this->Form->create('Cake\Test\TestCase\View\Helper\Contact');
  2704. $result = $this->Form->inputs('The Legend');
  2705. $expected = array(
  2706. '<fieldset',
  2707. '<legend',
  2708. 'The Legend',
  2709. '/legend',
  2710. '*/fieldset',
  2711. );
  2712. $this->assertTags($result, $expected);
  2713. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2714. $expected = array(
  2715. 'fieldset' => array('class' => 'classy-stuff'),
  2716. '<legend',
  2717. 'Field of Dreams',
  2718. '/legend',
  2719. '*/fieldset'
  2720. );
  2721. $this->assertTags($result, $expected);
  2722. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2723. $this->assertTags($result, $expected);
  2724. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2725. $this->assertTags($result, $expected);
  2726. $this->Form->create('Contact');
  2727. $this->Form->request['prefix'] = 'admin';
  2728. $this->Form->request['action'] = 'admin_edit';
  2729. $result = $this->Form->inputs();
  2730. $expected = array(
  2731. '<fieldset',
  2732. '<legend',
  2733. 'Edit Contact',
  2734. '/legend',
  2735. '*/fieldset',
  2736. );
  2737. $this->assertTags($result, $expected);
  2738. $this->Form->create('Contact');
  2739. $result = $this->Form->inputs(false);
  2740. $expected = array(
  2741. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2742. array('div' => array('class' => 'input text')),
  2743. '*/div',
  2744. array('div' => array('class' => 'input email')),
  2745. '*/div',
  2746. array('div' => array('class' => 'input tel')),
  2747. '*/div',
  2748. array('div' => array('class' => 'input password')),
  2749. '*/div',
  2750. array('div' => array('class' => 'input date')),
  2751. '*/div',
  2752. array('div' => array('class' => 'input date')),
  2753. '*/div',
  2754. array('div' => array('class' => 'input datetime')),
  2755. '*/div',
  2756. array('div' => array('class' => 'input number')),
  2757. '*/div',
  2758. array('div' => array('class' => 'input select')),
  2759. '*/div',
  2760. );
  2761. $this->assertTags($result, $expected);
  2762. $this->Form->create('Contact');
  2763. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2764. $expected = array(
  2765. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2766. array('div' => array('class' => 'input text')),
  2767. '*/div',
  2768. array('div' => array('class' => 'input email')),
  2769. '*/div',
  2770. array('div' => array('class' => 'input tel')),
  2771. '*/div',
  2772. array('div' => array('class' => 'input password')),
  2773. '*/div',
  2774. array('div' => array('class' => 'input date')),
  2775. '*/div',
  2776. array('div' => array('class' => 'input date')),
  2777. '*/div',
  2778. array('div' => array('class' => 'input datetime')),
  2779. '*/div',
  2780. array('div' => array('class' => 'input number')),
  2781. '*/div',
  2782. array('div' => array('class' => 'input select')),
  2783. '*/div',
  2784. );
  2785. $this->assertTags($result, $expected);
  2786. $this->Form->create('Contact');
  2787. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2788. $this->assertTags($result, $expected);
  2789. $this->Form->create('Contact');
  2790. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2791. $expected = array(
  2792. 'fieldset' => array(),
  2793. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2794. array('div' => array('class' => 'input text')),
  2795. '*/div',
  2796. array('div' => array('class' => 'input email')),
  2797. '*/div',
  2798. array('div' => array('class' => 'input tel')),
  2799. '*/div',
  2800. array('div' => array('class' => 'input password')),
  2801. '*/div',
  2802. array('div' => array('class' => 'input date')),
  2803. '*/div',
  2804. array('div' => array('class' => 'input date')),
  2805. '*/div',
  2806. array('div' => array('class' => 'input datetime')),
  2807. '*/div',
  2808. array('div' => array('class' => 'input number')),
  2809. '*/div',
  2810. array('div' => array('class' => 'input select')),
  2811. '*/div',
  2812. '/fieldset'
  2813. );
  2814. $this->assertTags($result, $expected);
  2815. $this->Form->create('Contact');
  2816. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2817. $expected = array(
  2818. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2819. array('div' => array('class' => 'input text')),
  2820. '*/div',
  2821. array('div' => array('class' => 'input email')),
  2822. '*/div',
  2823. array('div' => array('class' => 'input tel')),
  2824. '*/div',
  2825. array('div' => array('class' => 'input password')),
  2826. '*/div',
  2827. array('div' => array('class' => 'input date')),
  2828. '*/div',
  2829. array('div' => array('class' => 'input date')),
  2830. '*/div',
  2831. array('div' => array('class' => 'input datetime')),
  2832. '*/div',
  2833. array('div' => array('class' => 'input number')),
  2834. '*/div',
  2835. array('div' => array('class' => 'input select')),
  2836. '*/div',
  2837. );
  2838. $this->assertTags($result, $expected);
  2839. $this->Form->create('Contact');
  2840. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2841. $this->assertTags($result, $expected);
  2842. $this->Form->create('Contact');
  2843. $result = $this->Form->inputs('Hello');
  2844. $expected = array(
  2845. 'fieldset' => array(),
  2846. 'legend' => array(),
  2847. 'Hello',
  2848. '/legend',
  2849. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2850. array('div' => array('class' => 'input text')),
  2851. '*/div',
  2852. array('div' => array('class' => 'input email')),
  2853. '*/div',
  2854. array('div' => array('class' => 'input tel')),
  2855. '*/div',
  2856. array('div' => array('class' => 'input password')),
  2857. '*/div',
  2858. array('div' => array('class' => 'input date')),
  2859. '*/div',
  2860. array('div' => array('class' => 'input date')),
  2861. '*/div',
  2862. array('div' => array('class' => 'input datetime')),
  2863. '*/div',
  2864. array('div' => array('class' => 'input number')),
  2865. '*/div',
  2866. array('div' => array('class' => 'input select')),
  2867. '*/div',
  2868. '/fieldset'
  2869. );
  2870. $this->assertTags($result, $expected);
  2871. $this->Form->create('Contact');
  2872. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2873. $expected = array(
  2874. 'fieldset' => array(),
  2875. 'legend' => array(),
  2876. 'Hello',
  2877. '/legend',
  2878. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2879. array('div' => array('class' => 'input text')),
  2880. '*/div',
  2881. array('div' => array('class' => 'input email')),
  2882. '*/div',
  2883. array('div' => array('class' => 'input tel')),
  2884. '*/div',
  2885. array('div' => array('class' => 'input password')),
  2886. '*/div',
  2887. array('div' => array('class' => 'input date')),
  2888. '*/div',
  2889. array('div' => array('class' => 'input date')),
  2890. '*/div',
  2891. array('div' => array('class' => 'input datetime')),
  2892. '*/div',
  2893. array('div' => array('class' => 'input number')),
  2894. '*/div',
  2895. array('div' => array('class' => 'input select')),
  2896. '*/div',
  2897. '/fieldset'
  2898. );
  2899. $this->assertTags($result, $expected);
  2900. $this->Form->create('Contact');
  2901. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2902. $this->assertTags($result, $expected);
  2903. $this->Form->end();
  2904. $this->Form->create(false);
  2905. $expected = array(
  2906. 'fieldset' => array(),
  2907. array('div' => array('class' => 'input text')),
  2908. 'label' => array('for' => 'foo'),
  2909. 'Foo',
  2910. '/label',
  2911. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  2912. '*/div',
  2913. '/fieldset'
  2914. );
  2915. $result = $this->Form->inputs(
  2916. array('foo' => array('type' => 'text')),
  2917. array(),
  2918. array('legend' => false)
  2919. );
  2920. $this->assertTags($result, $expected);
  2921. }
  2922. /**
  2923. * Tests inputs() works with plugin models
  2924. *
  2925. * @return void
  2926. */
  2927. public function testInputsPluginModel() {
  2928. $this->loadFixtures('Post');
  2929. Plugin::load('TestPlugin');
  2930. $this->Form->request['models'] = array(
  2931. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2932. );
  2933. $this->Form->create('TestPlugin.TestPluginPost');
  2934. $result = $this->Form->inputs();
  2935. $this->assertContains('TestPluginPost[id]', $result);
  2936. $this->assertContains('TestPluginPost[author_id]', $result);
  2937. $this->assertContains('TestPluginPost[title]', $result);
  2938. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2939. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2940. $this->assertEquals('TestPluginPost', $this->Form->model());
  2941. }
  2942. /**
  2943. * testSelectAsCheckbox method
  2944. *
  2945. * test multi-select widget with checkbox formatting.
  2946. *
  2947. * @return void
  2948. */
  2949. public function testSelectAsCheckbox() {
  2950. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2951. $expected = array(
  2952. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2953. array('div' => array('class' => 'checkbox')),
  2954. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2955. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2956. 'first',
  2957. '/label',
  2958. '/div',
  2959. array('div' => array('class' => 'checkbox')),
  2960. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2961. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2962. 'second',
  2963. '/label',
  2964. '/div',
  2965. array('div' => array('class' => 'checkbox')),
  2966. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2967. array('label' => array('for' => 'ModelMultiField2')),
  2968. 'third',
  2969. '/label',
  2970. '/div',
  2971. );
  2972. $this->assertTags($result, $expected);
  2973. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2974. $expected = array(
  2975. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2976. array('div' => array('class' => 'checkbox')),
  2977. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField1/22')),
  2978. array('label' => array('for' => 'ModelMultiField1/22')),
  2979. 'half',
  2980. '/label',
  2981. '/div',
  2982. );
  2983. $this->assertTags($result, $expected);
  2984. }
  2985. /**
  2986. * testLabel method
  2987. *
  2988. * test label generation.
  2989. *
  2990. * @return void
  2991. */
  2992. public function testLabel() {
  2993. $this->Form->text('Person.name');
  2994. $result = $this->Form->label();
  2995. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2996. $this->Form->text('Person.name');
  2997. $result = $this->Form->label();
  2998. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2999. $result = $this->Form->label('Person.first_name');
  3000. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  3001. $result = $this->Form->label('Person.first_name', 'Your first name');
  3002. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  3003. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  3004. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  3005. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  3006. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  3007. $result = $this->Form->label('Person.first_name', '');
  3008. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  3009. $result = $this->Form->label('Person.2.name', '');
  3010. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  3011. }
  3012. /**
  3013. * testTextbox method
  3014. *
  3015. * test textbox element generation
  3016. *
  3017. * @return void
  3018. */
  3019. public function testTextbox() {
  3020. $result = $this->Form->text('Model.field');
  3021. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'ModelField')));
  3022. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3023. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]', 'id' => 'ModelField')));
  3024. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3025. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID')));
  3026. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3027. $result = $this->Form->text('Model.text');
  3028. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  3029. $Contact = ClassRegistry::getObject('Contact');
  3030. $Contact->validationErrors['text'] = array(true);
  3031. $this->Form->request->data['Contact']['text'] = 'test';
  3032. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  3033. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Contact[text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3034. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  3035. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  3036. $expected = array(
  3037. 'input' => array('type' => 'text', 'name' => 'Model[0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  3038. );
  3039. $this->assertTags($result, $expected);
  3040. }
  3041. /**
  3042. * testDefaultValue method
  3043. *
  3044. * Test default value setting
  3045. *
  3046. * @return void
  3047. */
  3048. public function testDefaultValue() {
  3049. $this->Form->request->data['Model']['field'] = 'test';
  3050. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3051. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'value' => 'test', 'id' => 'ModelField')));
  3052. unset($this->Form->request->data['Model']['field']);
  3053. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3054. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'value' => 'default value', 'id' => 'ModelField')));
  3055. }
  3056. /**
  3057. * testCheckboxDefaultValue method
  3058. *
  3059. * Test default value setting on checkbox() method
  3060. *
  3061. * @return void
  3062. */
  3063. public function testCheckboxDefaultValue() {
  3064. $this->Form->request->data['Model']['field'] = false;
  3065. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3066. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField')));
  3067. unset($this->Form->request->data['Model']['field']);
  3068. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3069. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3070. $this->Form->request->data['Model']['field'] = true;
  3071. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3072. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3073. unset($this->Form->request->data['Model']['field']);
  3074. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3075. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField')));
  3076. }
  3077. /**
  3078. * testError method
  3079. *
  3080. * Test field error generation
  3081. *
  3082. * @return void
  3083. */
  3084. public function testError() {
  3085. $Contact = ClassRegistry::getObject('Contact');
  3086. $Contact->validationErrors['field'] = array(1);
  3087. $result = $this->Form->error('Contact.field');
  3088. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3089. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3090. $this->assertEquals('Error in field Field', $result);
  3091. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3092. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3093. $this->assertEquals('This field contains invalid input', $result);
  3094. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3095. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3096. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3097. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3098. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3099. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3100. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3101. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3102. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3103. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3104. $this->assertEquals('<strong>Badness!</strong>', $result);
  3105. $Contact->validationErrors['field'] = array("email");
  3106. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3107. $expected = array(
  3108. 'div' => array('class' => 'field-error'),
  3109. 'No good!',
  3110. '/div'
  3111. );
  3112. $this->assertTags($result, $expected);
  3113. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3114. $result = $this->Form->error('Contact.field', array(
  3115. 'notEmpty' => 'Cannot be empty',
  3116. 'email' => 'No good!'
  3117. ));
  3118. $expected = array(
  3119. 'div' => array('class' => 'error-message'),
  3120. 'ul' => array(),
  3121. '<li', 'Cannot be empty', '/li',
  3122. '<li', 'No good!', '/li',
  3123. '<li', 'Something else', '/li',
  3124. '/ul',
  3125. '/div'
  3126. );
  3127. $this->assertTags($result, $expected);
  3128. // Testing error messages list options
  3129. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3130. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3131. $expected = array(
  3132. 'div' => array('class' => 'error-message'),
  3133. 'ol' => array(),
  3134. '<li', 'notEmpty', '/li',
  3135. '<li', 'email', '/li',
  3136. '/ol',
  3137. '/div'
  3138. );
  3139. $this->assertTags($result, $expected);
  3140. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3141. $expected = array(
  3142. 'div' => array('class' => 'error-message'),
  3143. 'ol' => array(),
  3144. '<li', 'notEmpty', '/li',
  3145. '<li', 'email', '/li',
  3146. '/ol',
  3147. '/div'
  3148. );
  3149. $this->assertTags($result, $expected);
  3150. $result = $this->Form->error('Contact.field', null, array(
  3151. 'listOptions' => array(
  3152. 'class' => 'ul-class',
  3153. 'itemOptions' => array(
  3154. 'class' => 'li-class'
  3155. )
  3156. )
  3157. ));
  3158. $expected = array(
  3159. 'div' => array('class' => 'error-message'),
  3160. 'ul' => array('class' => 'ul-class'),
  3161. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3162. array('li' => array('class' => 'li-class')), 'email', '/li',
  3163. '/ul',
  3164. '/div'
  3165. );
  3166. $this->assertTags($result, $expected);
  3167. }
  3168. /**
  3169. * test error options when using form->input();
  3170. *
  3171. * @return void
  3172. */
  3173. public function testInputErrorEscape() {
  3174. $this->Form->create('ValidateProfile');
  3175. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3176. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3177. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3178. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3179. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3180. $this->assertRegExp('/required<br>/', $result);
  3181. }
  3182. /**
  3183. * testPassword method
  3184. *
  3185. * Test password element generation
  3186. *
  3187. * @return void
  3188. */
  3189. public function testPassword() {
  3190. $Contact = ClassRegistry::getObject('Contact');
  3191. $result = $this->Form->password('Contact.field');
  3192. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]', 'id' => 'ContactField')));
  3193. $Contact->validationErrors['passwd'] = 1;
  3194. $this->Form->request->data['Contact']['passwd'] = 'test';
  3195. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3196. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3197. }
  3198. /**
  3199. * testRadio method
  3200. *
  3201. * Test radio element set generation
  3202. *
  3203. * @return void
  3204. */
  3205. public function testRadio() {
  3206. $result = $this->Form->radio('Model.field', array('option A'));
  3207. $expected = array(
  3208. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  3209. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')),
  3210. 'label' => array('for' => 'ModelField0'),
  3211. 'option A',
  3212. '/label'
  3213. );
  3214. $this->assertTags($result, $expected);
  3215. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3216. $expected = array(
  3217. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  3218. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1/2', 'id' => 'ModelField1/2')),
  3219. 'label' => array('for' => 'ModelField1/2'),
  3220. 'half',
  3221. '/label'
  3222. );
  3223. $this->assertTags($result, $expected);
  3224. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3225. $expected = array(
  3226. 'fieldset' => array(),
  3227. 'legend' => array(),
  3228. 'Field',
  3229. '/legend',
  3230. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  3231. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')),
  3232. array('label' => array('for' => 'ModelField0')),
  3233. 'option A',
  3234. '/label',
  3235. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3236. array('label' => array('for' => 'ModelField1')),
  3237. 'option B',
  3238. '/label',
  3239. '/fieldset'
  3240. );
  3241. $this->assertTags($result, $expected);
  3242. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3243. $expected = array(
  3244. 'fieldset' => array(),
  3245. 'legend' => array(),
  3246. 'Field',
  3247. '/legend',
  3248. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  3249. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')),
  3250. array('label' => array('for' => 'ModelField0')),
  3251. 'option A',
  3252. '/label',
  3253. 'br' => array(),
  3254. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3255. array('label' => array('for' => 'ModelField1')),
  3256. 'option B',
  3257. '/label',
  3258. '/fieldset'
  3259. );
  3260. $this->assertTags($result, $expected);
  3261. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3262. $expected = array(
  3263. 'fieldset' => array(),
  3264. 'legend' => array(),
  3265. 'Gender',
  3266. '/legend',
  3267. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3268. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3269. array('label' => array('for' => 'EmployeeGenderMale')),
  3270. 'Male',
  3271. '/label',
  3272. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3273. array('label' => array('for' => 'EmployeeGenderFemale')),
  3274. 'Female',
  3275. '/label',
  3276. '/fieldset',
  3277. );
  3278. $this->assertTags($result, $expected);
  3279. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3280. $expected = array(
  3281. 'fieldset' => array(),
  3282. 'legend' => array(),
  3283. 'Gender',
  3284. '/legend',
  3285. 'input' => array('type' => 'hidden', 'name' => 'Officer[gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3286. array('input' => array('type' => 'radio', 'name' => 'Officer[gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3287. array('label' => array('for' => 'OfficerGenderMale')),
  3288. 'Male',
  3289. '/label',
  3290. array('input' => array('type' => 'radio', 'name' => 'Officer[gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3291. array('label' => array('for' => 'OfficerGenderFemale')),
  3292. 'Female',
  3293. '/label',
  3294. '/fieldset',
  3295. );
  3296. $this->assertTags($result, $expected);
  3297. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3298. $expected = array(
  3299. 'input' => array('type' => 'hidden', 'name' => 'Contact[1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3300. array('input' => array('type' => 'radio', 'name' => 'Contact[1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0', 'required' => 'required')),
  3301. 'label' => array('for' => 'Contact1Imrequired0'),
  3302. 'option A',
  3303. '/label'
  3304. );
  3305. $this->assertTags($result, $expected);
  3306. $result = $this->Form->radio('Model.1.field', array('option A'));
  3307. $expected = array(
  3308. 'input' => array('type' => 'hidden', 'name' => 'Model[1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3309. array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3310. 'label' => array('for' => 'Model1Field0'),
  3311. 'option A',
  3312. '/label'
  3313. );
  3314. $this->assertTags($result, $expected);
  3315. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  3316. $expected = array(
  3317. 'fieldset' => array(),
  3318. 'legend' => array(),
  3319. 'Field',
  3320. '/legend',
  3321. 'input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '', 'id' => 'ModelField_'),
  3322. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'ModelField0')),
  3323. array('label' => array('for' => 'ModelField0')),
  3324. 'option A',
  3325. '/label',
  3326. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'ModelField1')),
  3327. array('label' => array('for' => 'ModelField1')),
  3328. 'option B',
  3329. '/label',
  3330. '/fieldset'
  3331. );
  3332. $this->assertTags($result, $expected);
  3333. $result = $this->Form->radio(
  3334. 'Model.field',
  3335. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third')
  3336. );
  3337. $expected = array(
  3338. 'fieldset' => array(),
  3339. 'legend' => array(),
  3340. 'Field',
  3341. '/legend',
  3342. 'input' => array(
  3343. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3344. 'id' => 'ModelField_', 'value' => '',
  3345. ),
  3346. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3347. 'id' => 'ModelFieldAB', 'value' => 'a&gt;b')),
  3348. array('label' => array('for' => 'ModelFieldAB')),
  3349. 'first',
  3350. '/label',
  3351. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3352. 'id' => 'ModelFieldAB1', 'value' => 'a&lt;b')),
  3353. array('label' => array('for' => 'ModelFieldAB1')),
  3354. 'second',
  3355. '/label',
  3356. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3357. 'id' => 'ModelFieldAB2', 'value' => 'a&quot;b')),
  3358. array('label' => array('for' => 'ModelFieldAB2')),
  3359. 'third',
  3360. '/label',
  3361. '/fieldset'
  3362. );
  3363. $this->assertTags($result, $expected);
  3364. }
  3365. /**
  3366. * Test radio inputs with between as string or array. Also ensure
  3367. * that an array with less between elements works.
  3368. *
  3369. * @return void
  3370. */
  3371. public function testRadioBetween() {
  3372. $result = $this->Form->radio(
  3373. 'Model.field',
  3374. array('option A', 'option B'),
  3375. array('between' => 'I am between')
  3376. );
  3377. $expected = array(
  3378. 'fieldset' => array(),
  3379. 'legend' => array(),
  3380. 'Field',
  3381. '/legend',
  3382. 'I am between',
  3383. 'input' => array(
  3384. 'type' => 'hidden', 'name' => 'Model[field]',
  3385. 'value' => '', 'id' => 'ModelField_'
  3386. ),
  3387. array('input' => array(
  3388. 'type' => 'radio', 'name' => 'Model[field]',
  3389. 'value' => '0', 'id' => 'ModelField0'
  3390. )),
  3391. array('label' => array('for' => 'ModelField0')),
  3392. 'option A',
  3393. '/label',
  3394. array('input' => array(
  3395. 'type' => 'radio', 'name' => 'Model[field]',
  3396. 'value' => '1', 'id' => 'ModelField1'
  3397. )),
  3398. array('label' => array('for' => 'ModelField1')),
  3399. 'option B',
  3400. '/label',
  3401. '/fieldset'
  3402. );
  3403. $this->assertTags($result, $expected);
  3404. $result = $this->Form->radio(
  3405. 'Model.field',
  3406. array('option A', 'option B', 'option C'),
  3407. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3408. );
  3409. $expected = array(
  3410. 'fieldset' => array(),
  3411. 'legend' => array(),
  3412. 'Field',
  3413. '/legend',
  3414. 'input' => array(
  3415. 'type' => 'hidden', 'name' => 'Model[field]',
  3416. 'value' => '', 'id' => 'ModelField_'
  3417. ),
  3418. array('input' => array(
  3419. 'type' => 'radio', 'name' => 'Model[field]',
  3420. 'value' => '0', 'id' => 'ModelField0'
  3421. )),
  3422. array('label' => array('for' => 'ModelField0')),
  3423. 'option A',
  3424. '/label',
  3425. 'between A',
  3426. '--separator--',
  3427. array('input' => array(
  3428. 'type' => 'radio', 'name' => 'Model[field]',
  3429. 'value' => '1', 'id' => 'ModelField1'
  3430. )),
  3431. array('label' => array('for' => 'ModelField1')),
  3432. 'option B',
  3433. '/label',
  3434. 'between B',
  3435. '--separator--',
  3436. array('input' => array(
  3437. 'type' => 'radio', 'name' => 'Model[field]',
  3438. 'value' => '2', 'id' => 'ModelField2'
  3439. )),
  3440. array('label' => array('for' => 'ModelField2')),
  3441. 'option C',
  3442. '/label',
  3443. 'between C',
  3444. '/fieldset'
  3445. );
  3446. $this->assertTags($result, $expected);
  3447. $result = $this->Form->input('Model.field', array(
  3448. 'options' => array('1' => 'first', '2' => 'second'),
  3449. 'type' => 'radio',
  3450. 'before' => '--before--',
  3451. 'after' => '--after--',
  3452. 'separator' => '--separator--',
  3453. 'between' => array('--between first--', '--between second--')
  3454. ));
  3455. $expected = array(
  3456. 'div' => array('class' => 'input radio'),
  3457. '--before--',
  3458. 'fieldset' => array(),
  3459. 'legend' => array(),
  3460. 'Field',
  3461. '/legend',
  3462. array('input' => array('type' => 'hidden', 'name' => 'Model[field]', 'id' => 'ModelField_', 'value' => '')),
  3463. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3464. array('label' => array('for' => 'ModelField1')),
  3465. 'first',
  3466. '/label',
  3467. '--between first--',
  3468. '--separator--',
  3469. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '2', 'id' => 'ModelField2')),
  3470. array('label' => array('for' => 'ModelField2')),
  3471. 'second',
  3472. '/label',
  3473. '--between second--',
  3474. '/fieldset',
  3475. '--after--',
  3476. '/div'
  3477. );
  3478. $this->assertTags($result, $expected);
  3479. $result = $this->Form->input('Model.field', array(
  3480. 'options' => array('1' => 'first', '2' => 'second'),
  3481. 'type' => 'radio',
  3482. 'before' => '--before--',
  3483. 'after' => '--after--',
  3484. 'separator' => '--separator--',
  3485. 'between' => array('--between first--')
  3486. ));
  3487. $expected = array(
  3488. 'div' => array('class' => 'input radio'),
  3489. '--before--',
  3490. 'fieldset' => array(),
  3491. 'legend' => array(),
  3492. 'Field',
  3493. '/legend',
  3494. array('input' => array('type' => 'hidden', 'name' => 'Model[field]', 'id' => 'ModelField_', 'value' => '')),
  3495. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3496. array('label' => array('for' => 'ModelField1')),
  3497. 'first',
  3498. '/label',
  3499. '--between first--',
  3500. '--separator--',
  3501. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '2', 'id' => 'ModelField2')),
  3502. array('label' => array('for' => 'ModelField2')),
  3503. 'second',
  3504. '/label',
  3505. '/fieldset',
  3506. '--after--',
  3507. '/div'
  3508. );
  3509. $this->assertTags($result, $expected);
  3510. }
  3511. /**
  3512. * Test that radios with a 0 value are selected under the correct conditions.
  3513. * Also ensure that values that are booleanish are handled correctly.
  3514. *
  3515. * @return void
  3516. */
  3517. public function testRadioOptionWithBooleanishValues() {
  3518. $expected = array(
  3519. 'fieldset' => array(),
  3520. 'legend' => array(),
  3521. 'Field',
  3522. '/legend',
  3523. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3524. array('label' => array('for' => 'ModelField1')),
  3525. 'Yes',
  3526. '/label',
  3527. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3528. array('label' => array('for' => 'ModelField0')),
  3529. 'No',
  3530. '/label',
  3531. '/fieldset'
  3532. );
  3533. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3534. $this->assertTags($result, $expected);
  3535. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3536. $this->assertTags($result, $expected);
  3537. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3538. $this->assertTags($result, $expected);
  3539. $expected = array(
  3540. 'fieldset' => array(),
  3541. 'legend' => array(),
  3542. 'Field',
  3543. '/legend',
  3544. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  3545. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3546. array('label' => array('for' => 'ModelField1')),
  3547. 'Yes',
  3548. '/label',
  3549. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')),
  3550. array('label' => array('for' => 'ModelField0')),
  3551. 'No',
  3552. '/label',
  3553. '/fieldset'
  3554. );
  3555. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3556. $this->assertTags($result, $expected);
  3557. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3558. $this->assertTags($result, $expected);
  3559. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3560. $this->assertTags($result, $expected);
  3561. $expected = array(
  3562. 'fieldset' => array(),
  3563. 'legend' => array(),
  3564. 'Field',
  3565. '/legend',
  3566. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3567. array('label' => array('for' => 'ModelField1')),
  3568. 'Yes',
  3569. '/label',
  3570. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')),
  3571. array('label' => array('for' => 'ModelField0')),
  3572. 'No',
  3573. '/label',
  3574. '/fieldset'
  3575. );
  3576. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3577. $this->assertTags($result, $expected);
  3578. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3579. $this->assertTags($result, $expected);
  3580. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3581. $this->assertTags($result, $expected);
  3582. }
  3583. /**
  3584. * test disabled radio options
  3585. *
  3586. * @return void
  3587. */
  3588. public function testRadioDisabled() {
  3589. $result = $this->Form->radio(
  3590. 'Model.field',
  3591. array('option A', 'option B'),
  3592. array('disabled' => array(0), 'value' => '0')
  3593. );
  3594. $expected = array(
  3595. 'fieldset' => array(),
  3596. 'legend' => array(),
  3597. 'Field',
  3598. '/legend',
  3599. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3600. array('label' => array('for' => 'ModelField0')),
  3601. 'option A',
  3602. '/label',
  3603. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3604. array('label' => array('for' => 'ModelField1')),
  3605. 'option B',
  3606. '/label',
  3607. '/fieldset'
  3608. );
  3609. $this->assertTags($result, $expected);
  3610. $result = $this->Form->radio(
  3611. 'Model.field',
  3612. array('option A', 'option B'),
  3613. array('disabled' => true, 'value' => '0')
  3614. );
  3615. $expected = array(
  3616. 'fieldset' => array(),
  3617. 'legend' => array(),
  3618. 'Field',
  3619. '/legend',
  3620. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3621. array('label' => array('for' => 'ModelField0')),
  3622. 'option A',
  3623. '/label',
  3624. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3625. array('label' => array('for' => 'ModelField1')),
  3626. 'option B',
  3627. '/label',
  3628. '/fieldset'
  3629. );
  3630. $this->assertTags($result, $expected);
  3631. $result = $this->Form->radio(
  3632. 'Model.field',
  3633. array('option A', 'option B'),
  3634. array('disabled' => 'disabled', 'value' => '0')
  3635. );
  3636. $expected = array(
  3637. 'fieldset' => array(),
  3638. 'legend' => array(),
  3639. 'Field',
  3640. '/legend',
  3641. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3642. array('label' => array('for' => 'ModelField0')),
  3643. 'option A',
  3644. '/label',
  3645. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3646. array('label' => array('for' => 'ModelField1')),
  3647. 'option B',
  3648. '/label',
  3649. '/fieldset'
  3650. );
  3651. $this->assertTags($result, $expected);
  3652. $result = $this->Form->input('Model.field', array(
  3653. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3654. 'type' => 'radio',
  3655. 'disabled' => array(2, '3x'),
  3656. ));
  3657. $expected = array(
  3658. 'div' => array('class' => 'input radio'),
  3659. 'fieldset' => array(),
  3660. 'legend' => array(),
  3661. 'Field',
  3662. '/legend',
  3663. array('input' => array('type' => 'hidden', 'name' => 'Model[field]', 'id' => 'ModelField_', 'value' => '')),
  3664. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3665. array('label' => array('for' => 'ModelField1')),
  3666. 'first',
  3667. '/label',
  3668. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3669. array('label' => array('for' => 'ModelField2')),
  3670. 'second',
  3671. '/label',
  3672. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3673. array('label' => array('for' => 'ModelField2x')),
  3674. '2x',
  3675. '/label',
  3676. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '3', 'id' => 'ModelField3')),
  3677. array('label' => array('for' => 'ModelField3')),
  3678. 'third',
  3679. '/label',
  3680. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3681. array('label' => array('for' => 'ModelField3x')),
  3682. '3x',
  3683. '/label',
  3684. '/fieldset',
  3685. '/div'
  3686. );
  3687. $this->assertTags($result, $expected);
  3688. $result = $this->Form->input('Model.field', array(
  3689. 'type' => 'radio',
  3690. 'options' => array(
  3691. 1 => 'A',
  3692. 2 => 'B',
  3693. 3 => 'C'
  3694. ),
  3695. 'disabled' => array(1)
  3696. ));
  3697. $expected = array(
  3698. 'div' => array('class' => 'input radio'),
  3699. 'fieldset' => array(),
  3700. 'legend' => array(),
  3701. 'Field',
  3702. '/legend',
  3703. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3704. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
  3705. array('label' => array('for' => 'ModelField1')),
  3706. 'A',
  3707. '/label',
  3708. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
  3709. array('label' => array('for' => 'ModelField2')),
  3710. 'B',
  3711. '/label',
  3712. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
  3713. array('label' => array('for' => 'ModelField3')),
  3714. 'C',
  3715. '/label',
  3716. '/fieldset',
  3717. '/div'
  3718. );
  3719. $this->assertTags($result, $expected);
  3720. }
  3721. /**
  3722. * test disabling the hidden input for radio buttons
  3723. *
  3724. * @return void
  3725. */
  3726. public function testRadioHiddenInputDisabling() {
  3727. $result = $this->Form->input('Model.1.field', array(
  3728. 'type' => 'radio',
  3729. 'options' => array('option A'),
  3730. 'hiddenField' => false
  3731. )
  3732. );
  3733. $expected = array(
  3734. 'div' => array('class' => 'input radio'),
  3735. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3736. 'label' => array('for' => 'Model1Field0'),
  3737. 'option A',
  3738. '/label',
  3739. '/div'
  3740. );
  3741. $this->assertTags($result, $expected);
  3742. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3743. $expected = array(
  3744. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3745. 'label' => array('for' => 'Model1Field0'),
  3746. 'option A',
  3747. '/label'
  3748. );
  3749. $this->assertTags($result, $expected);
  3750. }
  3751. /**
  3752. * test adding an empty option for radio buttons
  3753. *
  3754. * @return void
  3755. */
  3756. public function testRadioAddEmptyOption() {
  3757. $result = $this->Form->input('Model.1.field', array(
  3758. 'type' => 'radio',
  3759. 'options' => array('option A'),
  3760. 'empty' => true,
  3761. 'hiddenField' => false
  3762. ));
  3763. $expected = array(
  3764. 'div' => array('class' => 'input radio'),
  3765. 'fieldset' => array(),
  3766. 'legend' => array(),
  3767. 'Field',
  3768. '/legend',
  3769. array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '', 'id' => 'Model1Field')),
  3770. array('label' => array('for' => 'Model1Field')),
  3771. __('empty'),
  3772. '/label',
  3773. array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3774. array('label' => array('for' => 'Model1Field0')),
  3775. 'option A',
  3776. '/label',
  3777. '/fieldset',
  3778. '/div'
  3779. );
  3780. $this->assertTags($result, $expected);
  3781. $result = $this->Form->input('Model.1.field', array(
  3782. 'type' => 'radio',
  3783. 'options' => array('option A'),
  3784. 'empty' => 'CustomEmptyLabel',
  3785. 'hiddenField' => false
  3786. ));
  3787. $expected = array(
  3788. 'div' => array('class' => 'input radio'),
  3789. 'fieldset' => array(),
  3790. 'legend' => array(),
  3791. 'Field',
  3792. '/legend',
  3793. array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '', 'id' => 'Model1Field')),
  3794. array('label' => array('for' => 'Model1Field')),
  3795. 'CustomEmptyLabel',
  3796. '/label',
  3797. array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3798. array('label' => array('for' => 'Model1Field0')),
  3799. 'option A',
  3800. '/label',
  3801. '/fieldset',
  3802. '/div'
  3803. );
  3804. $this->assertTags($result, $expected);
  3805. $result = $this->Form->input('Model.1.field', array(
  3806. 'type' => 'radio',
  3807. 'options' => array('option A'),
  3808. 'empty' => false,
  3809. 'hiddenField' => false
  3810. ));
  3811. $this->assertTextNotContains('"Model1Field"', $result);
  3812. }
  3813. /**
  3814. * Test that radio() accepts an array for label
  3815. *
  3816. * @return void
  3817. */
  3818. public function testRadioLabelArray() {
  3819. $result = $this->Form->input('Model.field', array(
  3820. 'type' => 'radio',
  3821. 'legend' => false,
  3822. 'label' => array(
  3823. 'class' => 'checkbox float-left',
  3824. ),
  3825. 'options' => array('1' => 'Option A', '2' => 'Option B.')
  3826. ));
  3827. $this->assertTextContains(
  3828. '<label for="ModelField1" class="checkbox float-left">Option A</label>',
  3829. $result
  3830. );
  3831. }
  3832. /**
  3833. * Test that label id's match the input element id's when radio is called after create().
  3834. *
  3835. * @return void
  3836. */
  3837. public function testRadioWithCreate() {
  3838. $this->Form->create('Model');
  3839. $result = $this->Form->radio('recipient',
  3840. array('1' => '1', '2' => '2', '3' => '3'),
  3841. array('legend' => false, 'value' => '1')
  3842. );
  3843. $this->assertTextNotContains(
  3844. '<label for="ModelModelRecipient1">1</label>',
  3845. $result
  3846. );
  3847. $this->assertTextContains(
  3848. '<label for="ModelRecipient1">1</label>',
  3849. $result
  3850. );
  3851. }
  3852. /**
  3853. * testDomIdSuffix method
  3854. *
  3855. * @return void
  3856. */
  3857. public function testDomIdSuffix() {
  3858. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs');
  3859. $this->assertEquals('1StringWith1$-dollarSigns', $result);
  3860. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>');
  3861. $this->assertEquals('AbcX=FooY=Bar', $result);
  3862. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'xhtml');
  3863. $this->assertEquals('1StringWith1-dollarSigns', $result);
  3864. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>', 'xhtml');
  3865. $this->assertEquals('AbcXFooYBar', $result);
  3866. }
  3867. /**
  3868. * testDomIdSuffixCollisionResolvement()
  3869. *
  3870. * @return void
  3871. */
  3872. public function testDomIdSuffixCollisionResolvement() {
  3873. $result = $this->Form->domIdSuffix('a>b');
  3874. $this->assertEquals('AB', $result);
  3875. $result = $this->Form->domIdSuffix('a<b');
  3876. $this->assertEquals('AB1', $result);
  3877. $result = $this->Form->domIdSuffix('a\'b');
  3878. $this->assertEquals('AB2', $result);
  3879. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3880. $this->assertEquals('1StringWith1-dollar', $result);
  3881. $result = $this->Form->domIdSuffix('1 string with 1€-dollar', 'xhtml');
  3882. $this->assertEquals('1StringWith1-dollar1', $result);
  3883. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3884. $this->assertEquals('1StringWith1-dollar2', $result);
  3885. }
  3886. /**
  3887. * testSelect method
  3888. *
  3889. * Test select element generation.
  3890. *
  3891. * @return void
  3892. */
  3893. public function testSelect() {
  3894. $result = $this->Form->select('Model.field', array());
  3895. $expected = array(
  3896. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3897. array('option' => array('value' => '')),
  3898. '/option',
  3899. '/select'
  3900. );
  3901. $this->assertTags($result, $expected);
  3902. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3903. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3904. $expected = array(
  3905. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3906. array('option' => array('value' => '')),
  3907. '/option',
  3908. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3909. 'good',
  3910. '/option',
  3911. array('option' => array('value' => 'other')),
  3912. 'bad',
  3913. '/option',
  3914. '/select'
  3915. );
  3916. $this->assertTags($result, $expected);
  3917. $this->Form->request->data = array();
  3918. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3919. $expected = array(
  3920. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3921. array('option' => array('value' => '')),
  3922. '/option',
  3923. array('option' => array('value' => 'value')),
  3924. 'good',
  3925. '/option',
  3926. array('option' => array('value' => 'other')),
  3927. 'bad',
  3928. '/option',
  3929. '/select'
  3930. );
  3931. $this->assertTags($result, $expected);
  3932. $result = $this->Form->select(
  3933. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3934. array('empty' => false)
  3935. );
  3936. $expected = array(
  3937. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3938. array('option' => array('value' => 'first')),
  3939. 'first &quot;html&quot; &lt;chars&gt;',
  3940. '/option',
  3941. array('option' => array('value' => 'second')),
  3942. 'value',
  3943. '/option',
  3944. '/select'
  3945. );
  3946. $this->assertTags($result, $expected);
  3947. $result = $this->Form->select(
  3948. 'Model.field',
  3949. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3950. array('escape' => false, 'empty' => false)
  3951. );
  3952. $expected = array(
  3953. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3954. array('option' => array('value' => 'first')),
  3955. 'first "html" <chars>',
  3956. '/option',
  3957. array('option' => array('value' => 'second')),
  3958. 'value',
  3959. '/option',
  3960. '/select'
  3961. );
  3962. $this->assertTags($result, $expected);
  3963. $options = array(
  3964. array('value' => 'first', 'name' => 'First'),
  3965. array('value' => 'first', 'name' => 'Another First'),
  3966. );
  3967. $result = $this->Form->select(
  3968. 'Model.field',
  3969. $options,
  3970. array('escape' => false, 'empty' => false)
  3971. );
  3972. $expected = array(
  3973. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3974. array('option' => array('value' => 'first')),
  3975. 'First',
  3976. '/option',
  3977. array('option' => array('value' => 'first')),
  3978. 'Another First',
  3979. '/option',
  3980. '/select'
  3981. );
  3982. $this->assertTags($result, $expected);
  3983. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3984. $result = $this->Form->select(
  3985. 'Model.contact_id',
  3986. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3987. array('escape' => false, 'empty' => 'pick something')
  3988. );
  3989. $expected = array(
  3990. 'select' => array('name' => 'Model[contact_id]', 'id' => 'ModelContactId'),
  3991. array('option' => array('value' => '')), 'pick something', '/option',
  3992. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3993. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3994. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3995. '/select'
  3996. );
  3997. $this->assertTags($result, $expected);
  3998. $this->Form->request->data['Model']['field'] = 0;
  3999. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  4000. $expected = array(
  4001. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  4002. array('option' => array('value' => '')), '/option',
  4003. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  4004. array('option' => array('value' => '1')), 'Yes', '/option',
  4005. '/select'
  4006. );
  4007. $this->assertTags($result, $expected);
  4008. $this->Form->request->data['Model']['field'] = 50;
  4009. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  4010. $expected = array(
  4011. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  4012. array('option' => array('value' => '')), '/option',
  4013. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  4014. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  4015. '/select'
  4016. );
  4017. $this->assertTags($result, $expected);
  4018. $result = $this->Form->select('Contact.required_one', array('option A'));
  4019. $expected = array(
  4020. 'select' => array(
  4021. 'name' => 'data[Contact][required_one]',
  4022. 'id' => 'ContactRequiredOne',
  4023. 'required' => 'required'
  4024. ),
  4025. array('option' => array('value' => '')), '/option',
  4026. array('option' => array('value' => '0')), 'option A', '/option',
  4027. '/select'
  4028. );
  4029. $this->assertTags($result, $expected);
  4030. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  4031. $expected = array(
  4032. 'select' => array(
  4033. 'name' => 'data[Contact][required_one]',
  4034. 'id' => 'ContactRequiredOne',
  4035. 'disabled' => 'disabled'
  4036. ),
  4037. array('option' => array('value' => '')), '/option',
  4038. array('option' => array('value' => '0')), 'option A', '/option',
  4039. '/select'
  4040. );
  4041. $this->assertTags($result, $expected);
  4042. }
  4043. /**
  4044. * test that select() with optiongroups listens to the escape param.
  4045. *
  4046. * @return void
  4047. */
  4048. public function testSelectOptionGroupEscaping() {
  4049. $options = array(
  4050. '>< Key' => array(
  4051. 1 => 'One',
  4052. 2 => 'Two'
  4053. )
  4054. );
  4055. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  4056. $expected = array(
  4057. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  4058. 'optgroup' => array('label' => '&gt;&lt; Key'),
  4059. array('option' => array('value' => '1')), 'One', '/option',
  4060. array('option' => array('value' => '2')), 'Two', '/option',
  4061. '/optgroup',
  4062. '/select'
  4063. );
  4064. $this->assertTags($result, $expected);
  4065. $options = array(
  4066. '>< Key' => array(
  4067. 1 => 'One',
  4068. 2 => 'Two'
  4069. )
  4070. );
  4071. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  4072. $expected = array(
  4073. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  4074. 'optgroup' => array('label' => '>< Key'),
  4075. array('option' => array('value' => '1')), 'One', '/option',
  4076. array('option' => array('value' => '2')), 'Two', '/option',
  4077. '/optgroup',
  4078. '/select'
  4079. );
  4080. $this->assertTags($result, $expected);
  4081. }
  4082. /**
  4083. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  4084. *
  4085. * @return void
  4086. */
  4087. public function testSelectWithNullAttributes() {
  4088. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  4089. $expected = array(
  4090. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  4091. array('option' => array('value' => '0')),
  4092. 'first',
  4093. '/option',
  4094. array('option' => array('value' => '1')),
  4095. 'second',
  4096. '/option',
  4097. '/select'
  4098. );
  4099. $this->assertTags($result, $expected);
  4100. }
  4101. /**
  4102. * testNestedSelect method
  4103. *
  4104. * test select element generation with optgroups
  4105. *
  4106. * @return void
  4107. */
  4108. public function testNestedSelect() {
  4109. $result = $this->Form->select(
  4110. 'Model.field',
  4111. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4112. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4113. )), array('empty' => false)
  4114. );
  4115. $expected = array(
  4116. 'select' => array('name' => 'Model[field]',
  4117. 'id' => 'ModelField'),
  4118. array('option' => array('value' => 1)),
  4119. 'One',
  4120. '/option',
  4121. array('option' => array('value' => 2)),
  4122. 'Two',
  4123. '/option',
  4124. array('optgroup' => array('label' => 'Three')),
  4125. array('option' => array('value' => 4)),
  4126. 'Four',
  4127. '/option',
  4128. array('option' => array('value' => 5)),
  4129. 'Five',
  4130. '/option',
  4131. '/optgroup',
  4132. '/select'
  4133. );
  4134. $this->assertTags($result, $expected);
  4135. $result = $this->Form->select(
  4136. 'Model.field',
  4137. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  4138. array('showParents' => true, 'empty' => false)
  4139. );
  4140. $expected = array(
  4141. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  4142. array('option' => array('value' => 1)),
  4143. 'One',
  4144. '/option',
  4145. array('option' => array('value' => 2)),
  4146. 'Two',
  4147. '/option',
  4148. array('optgroup' => array('label' => 'Three')),
  4149. array('option' => array('value' => 3)),
  4150. 'Three',
  4151. '/option',
  4152. array('option' => array('value' => 4)),
  4153. 'Four',
  4154. '/option',
  4155. '/optgroup',
  4156. '/select'
  4157. );
  4158. $this->assertTags($result, $expected);
  4159. }
  4160. /**
  4161. * testSelectMultiple method
  4162. *
  4163. * test generation of multiple select elements
  4164. *
  4165. * @return void
  4166. */
  4167. public function testSelectMultiple() {
  4168. $options = array('first', 'second', 'third');
  4169. $result = $this->Form->select(
  4170. 'Model.multi_field', $options, array('multiple' => true)
  4171. );
  4172. $expected = array(
  4173. 'input' => array(
  4174. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4175. ),
  4176. 'select' => array(
  4177. 'name' => 'Model[multi_field][]',
  4178. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4179. ),
  4180. array('option' => array('value' => '0')),
  4181. 'first',
  4182. '/option',
  4183. array('option' => array('value' => '1')),
  4184. 'second',
  4185. '/option',
  4186. array('option' => array('value' => '2')),
  4187. 'third',
  4188. '/option',
  4189. '/select'
  4190. );
  4191. $this->assertTags($result, $expected);
  4192. $result = $this->Form->select(
  4193. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4194. );
  4195. $expected = array(
  4196. 'input' => array(
  4197. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4198. ),
  4199. 'select' => array(
  4200. 'name' => 'Model[multi_field][]',
  4201. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4202. ),
  4203. array('option' => array('value' => '0')),
  4204. 'first',
  4205. '/option',
  4206. array('option' => array('value' => '1')),
  4207. 'second',
  4208. '/option',
  4209. array('option' => array('value' => '2')),
  4210. 'third',
  4211. '/option',
  4212. '/select'
  4213. );
  4214. $this->assertTags($result, $expected);
  4215. $result = $this->Form->select(
  4216. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4217. );
  4218. $expected = array(
  4219. 'input' => array(
  4220. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4221. ),
  4222. 'select' => array(
  4223. 'name' => 'Model[multi_field][]', 'id' => 'ModelMultiField',
  4224. 'multiple' => 'multiple'
  4225. ),
  4226. array('option' => array('value' => '0', 'selected' => 'selected')),
  4227. 'first',
  4228. '/option',
  4229. array('option' => array('value' => '1', 'selected' => 'selected')),
  4230. 'second',
  4231. '/option',
  4232. array('option' => array('value' => '2')),
  4233. 'third',
  4234. '/option',
  4235. '/select'
  4236. );
  4237. $this->assertTags($result, $expected);
  4238. $result = $this->Form->select(
  4239. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4240. );
  4241. $expected = array(
  4242. 'select' => array(
  4243. 'name' => 'Model[multi_field]', 'id' => 'ModelMultiField'
  4244. ),
  4245. array('option' => array('value' => '0', 'selected' => 'selected')),
  4246. 'first',
  4247. '/option',
  4248. array('option' => array('value' => '1', 'selected' => 'selected')),
  4249. 'second',
  4250. '/option',
  4251. array('option' => array('value' => '2')),
  4252. 'third',
  4253. '/option',
  4254. '/select'
  4255. );
  4256. $this->assertTags($result, $expected);
  4257. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4258. $selected = array('2', '3x');
  4259. $result = $this->Form->select(
  4260. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4261. );
  4262. $expected = array(
  4263. 'input' => array(
  4264. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4265. ),
  4266. 'select' => array(
  4267. 'name' => 'Model[multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4268. ),
  4269. array('option' => array('value' => '1')),
  4270. 'One',
  4271. '/option',
  4272. array('option' => array('value' => '2', 'selected' => 'selected')),
  4273. 'Two',
  4274. '/option',
  4275. array('option' => array('value' => '3')),
  4276. 'Three',
  4277. '/option',
  4278. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4279. 'Stringy',
  4280. '/option',
  4281. '/select'
  4282. );
  4283. $this->assertTags($result, $expected);
  4284. $result = $this->Form->select('Contact.required_one', array(
  4285. '1' => 'option A',
  4286. '2' => 'option B'
  4287. ), array('multiple' => true));
  4288. $expected = array(
  4289. 'input' => array(
  4290. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4291. ),
  4292. 'select' => array(
  4293. 'name' => 'data[Contact][required_one][]',
  4294. 'id' => 'ContactRequiredOne',
  4295. 'required' => 'required',
  4296. 'multiple' => 'multiple'
  4297. ),
  4298. array('option' => array('value' => '1')), 'option A', '/option',
  4299. array('option' => array('value' => '2')), 'option B', '/option',
  4300. '/select'
  4301. );
  4302. $this->assertTags($result, $expected);
  4303. $result = $this->Form->select(
  4304. 'Model.multi_field',
  4305. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4306. array('multiple' => true)
  4307. );
  4308. $expected = array(
  4309. 'input' => array(
  4310. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '',
  4311. 'id' => 'ModelMultiField_'
  4312. ),
  4313. array('select' => array('name' => 'data[Model][multi_field][]',
  4314. 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4315. )),
  4316. array('option' => array('value' => 'a&gt;b')),
  4317. 'first',
  4318. '/option',
  4319. array('option' => array('value' => 'a&lt;b')),
  4320. 'second',
  4321. '/option',
  4322. array('option' => array(
  4323. 'value' => 'a&quot;b'
  4324. )),
  4325. 'third',
  4326. '/option',
  4327. '/select'
  4328. );
  4329. $this->assertTags($result, $expected);
  4330. }
  4331. /**
  4332. * Test generating multiple select with disabled elements.
  4333. *
  4334. * @return void
  4335. */
  4336. public function testSelectMultipleWithDisabledElements() {
  4337. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4338. $disabled = array(2, 3);
  4339. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4340. $expected = array(
  4341. 'input' => array(
  4342. 'type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4343. ),
  4344. 'select' => array(
  4345. 'name' => 'Contact[multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4346. ),
  4347. array('option' => array('value' => '1')),
  4348. 'One',
  4349. '/option',
  4350. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4351. 'Two',
  4352. '/option',
  4353. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4354. 'Three',
  4355. '/option',
  4356. array('option' => array('value' => '3x')),
  4357. 'Stringy',
  4358. '/option',
  4359. '/select'
  4360. );
  4361. $this->assertTags($result, $expected);
  4362. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4363. $disabled = array('2', '3x');
  4364. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4365. $expected = array(
  4366. array('div' => array('class' => 'input select')),
  4367. array('label' => array('for' => 'ContactMultiple')),
  4368. 'Multiple',
  4369. '/label',
  4370. 'input' => array(
  4371. 'type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4372. ),
  4373. 'select' => array(
  4374. 'name' => 'Contact[multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4375. ),
  4376. array('option' => array('value' => '1')),
  4377. 'One',
  4378. '/option',
  4379. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4380. 'Two',
  4381. '/option',
  4382. array('option' => array('value' => '3')),
  4383. 'Three',
  4384. '/option',
  4385. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4386. 'Stringy',
  4387. '/option',
  4388. '/select',
  4389. '/div'
  4390. );
  4391. $this->assertTags($result, $expected);
  4392. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4393. $disabled = true;
  4394. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4395. $expected = array(
  4396. array('div' => array('class' => 'input select')),
  4397. array('label' => array('for' => 'ContactMultiple')),
  4398. 'Multiple',
  4399. '/label',
  4400. 'input' => array(
  4401. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4402. ),
  4403. 'select' => array(
  4404. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4405. ),
  4406. array('option' => array('value' => '1')),
  4407. 'One',
  4408. '/option',
  4409. array('option' => array('value' => '2')),
  4410. 'Two',
  4411. '/option',
  4412. array('option' => array('value' => '3')),
  4413. 'Three',
  4414. '/option',
  4415. array('option' => array('value' => '3x')),
  4416. 'Stringy',
  4417. '/option',
  4418. '/select',
  4419. '/div'
  4420. );
  4421. $this->assertTags($result, $expected);
  4422. }
  4423. /**
  4424. * Test generating select with disabled elements.
  4425. *
  4426. * @return void
  4427. */
  4428. public function testSelectWithDisabledElements() {
  4429. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4430. $disabled = array(2, 3);
  4431. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4432. $expected = array(
  4433. 'select' => array(
  4434. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4435. ),
  4436. array('option' => array('value' => '')),
  4437. '/option',
  4438. array('option' => array('value' => '1')),
  4439. 'One',
  4440. '/option',
  4441. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4442. 'Two',
  4443. '/option',
  4444. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4445. 'Three',
  4446. '/option',
  4447. array('option' => array('value' => '3x')),
  4448. 'Stringy',
  4449. '/option',
  4450. '/select'
  4451. );
  4452. $this->assertTags($result, $expected);
  4453. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4454. $disabled = array('2', '3x');
  4455. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4456. $expected = array(
  4457. array('div' => array('class' => 'input select')),
  4458. array('label' => array('for' => 'ModelField')),
  4459. 'Field',
  4460. '/label',
  4461. 'select' => array(
  4462. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4463. ),
  4464. array('option' => array('value' => '1')),
  4465. 'One',
  4466. '/option',
  4467. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4468. 'Two',
  4469. '/option',
  4470. array('option' => array('value' => '3')),
  4471. 'Three',
  4472. '/option',
  4473. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4474. 'Stringy',
  4475. '/option',
  4476. '/select',
  4477. '/div'
  4478. );
  4479. $this->assertTags($result, $expected);
  4480. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4481. $disabled = true;
  4482. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4483. $expected = array(
  4484. array('div' => array('class' => 'input select')),
  4485. array('label' => array('for' => 'ModelField')),
  4486. 'Field',
  4487. '/label',
  4488. 'select' => array(
  4489. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4490. ),
  4491. array('option' => array('value' => '1')),
  4492. 'One',
  4493. '/option',
  4494. array('option' => array('value' => '2')),
  4495. 'Two',
  4496. '/option',
  4497. array('option' => array('value' => '3')),
  4498. 'Three',
  4499. '/option',
  4500. array('option' => array('value' => '3x')),
  4501. 'Stringy',
  4502. '/option',
  4503. '/select',
  4504. '/div'
  4505. );
  4506. $this->assertTags($result, $expected);
  4507. }
  4508. /**
  4509. * test generation of habtm select boxes.
  4510. *
  4511. * @return void
  4512. */
  4513. public function testHabtmSelectBox() {
  4514. $this->View->viewVars['contactTags'] = array(
  4515. 1 => 'blue',
  4516. 2 => 'red',
  4517. 3 => 'green'
  4518. );
  4519. $this->Form->request->data = array(
  4520. 'Contact' => array(),
  4521. 'ContactTag' => array(
  4522. array(
  4523. 'id' => '1',
  4524. 'name' => 'blue'
  4525. ),
  4526. array(
  4527. 'id' => 3,
  4528. 'name' => 'green'
  4529. )
  4530. )
  4531. );
  4532. $this->Form->create('Contact');
  4533. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4534. $expected = array(
  4535. 'input' => array(
  4536. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4537. ),
  4538. 'select' => array(
  4539. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  4540. 'multiple' => 'multiple'
  4541. ),
  4542. array('option' => array('value' => '1', 'selected' => 'selected')),
  4543. 'blue',
  4544. '/option',
  4545. array('option' => array('value' => '2')),
  4546. 'red',
  4547. '/option',
  4548. array('option' => array('value' => '3', 'selected' => 'selected')),
  4549. 'green',
  4550. '/option',
  4551. '/select'
  4552. );
  4553. $this->assertTags($result, $expected);
  4554. // make sure only 50 is selected, and not 50f5c0cf
  4555. $this->View->viewVars['contactTags'] = array(
  4556. '1' => 'blue',
  4557. '50f5c0cf' => 'red',
  4558. '50' => 'green'
  4559. );
  4560. $this->Form->request->data = array(
  4561. 'Contact' => array(),
  4562. 'ContactTag' => array(
  4563. array(
  4564. 'id' => 1,
  4565. 'name' => 'blue'
  4566. ),
  4567. array(
  4568. 'id' => 50,
  4569. 'name' => 'green'
  4570. )
  4571. )
  4572. );
  4573. $this->Form->create('Contact');
  4574. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4575. $expected = array(
  4576. 'input' => array(
  4577. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4578. ),
  4579. 'select' => array(
  4580. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  4581. 'multiple' => 'multiple'
  4582. ),
  4583. array('option' => array('value' => '1', 'selected' => 'selected')),
  4584. 'blue',
  4585. '/option',
  4586. array('option' => array('value' => '50f5c0cf')),
  4587. 'red',
  4588. '/option',
  4589. array('option' => array('value' => '50', 'selected' => 'selected')),
  4590. 'green',
  4591. '/option',
  4592. '/select'
  4593. );
  4594. $this->assertTags($result, $expected);
  4595. }
  4596. /**
  4597. * test generation of multi select elements in checkbox format
  4598. *
  4599. * @return void
  4600. */
  4601. public function testSelectMultipleCheckboxes() {
  4602. $result = $this->Form->select(
  4603. 'Model.multi_field',
  4604. array('first', 'second', 'third'),
  4605. array('multiple' => 'checkbox')
  4606. );
  4607. $expected = array(
  4608. 'input' => array(
  4609. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4610. ),
  4611. array('div' => array('class' => 'checkbox')),
  4612. array('input' => array(
  4613. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4614. 'value' => '0', 'id' => 'ModelMultiField0'
  4615. )),
  4616. array('label' => array('for' => 'ModelMultiField0')),
  4617. 'first',
  4618. '/label',
  4619. '/div',
  4620. array('div' => array('class' => 'checkbox')),
  4621. array('input' => array(
  4622. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4623. 'value' => '1', 'id' => 'ModelMultiField1'
  4624. )),
  4625. array('label' => array('for' => 'ModelMultiField1')),
  4626. 'second',
  4627. '/label',
  4628. '/div',
  4629. array('div' => array('class' => 'checkbox')),
  4630. array('input' => array(
  4631. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4632. 'value' => '2', 'id' => 'ModelMultiField2'
  4633. )),
  4634. array('label' => array('for' => 'ModelMultiField2')),
  4635. 'third',
  4636. '/label',
  4637. '/div'
  4638. );
  4639. $this->assertTags($result, $expected);
  4640. $result = $this->Form->select(
  4641. 'Model.multi_field',
  4642. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4643. array('multiple' => 'checkbox')
  4644. );
  4645. $expected = array(
  4646. 'input' => array(
  4647. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4648. ),
  4649. array('div' => array('class' => 'checkbox')),
  4650. array('input' => array(
  4651. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4652. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4653. )),
  4654. array('label' => array('for' => 'ModelMultiFieldA')),
  4655. 'first',
  4656. '/label',
  4657. '/div',
  4658. array('div' => array('class' => 'checkbox')),
  4659. array('input' => array(
  4660. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4661. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4662. )),
  4663. array('label' => array('for' => 'ModelMultiFieldB')),
  4664. 'second',
  4665. '/label',
  4666. '/div',
  4667. array('div' => array('class' => 'checkbox')),
  4668. array('input' => array(
  4669. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4670. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4671. )),
  4672. array('label' => array('for' => 'ModelMultiFieldC')),
  4673. 'third',
  4674. '/label',
  4675. '/div'
  4676. );
  4677. $this->assertTags($result, $expected);
  4678. $result = $this->Form->select(
  4679. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4680. );
  4681. $expected = array(
  4682. 'input' => array(
  4683. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4684. ),
  4685. array('div' => array('class' => 'checkbox')),
  4686. array('input' => array(
  4687. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4688. 'value' => '1', 'id' => 'ModelMultiField1'
  4689. )),
  4690. array('label' => array('for' => 'ModelMultiField1')),
  4691. 'first',
  4692. '/label',
  4693. '/div'
  4694. );
  4695. $this->assertTags($result, $expected);
  4696. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4697. $result = $this->Form->select(
  4698. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4699. );
  4700. $expected = array(
  4701. 'input' => array(
  4702. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'ModelTags'
  4703. ),
  4704. array('div' => array('class' => 'checkbox')),
  4705. array('input' => array(
  4706. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4707. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4708. )),
  4709. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4710. 'first',
  4711. '/label',
  4712. '/div',
  4713. array('div' => array('class' => 'checkbox')),
  4714. array('input' => array(
  4715. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4716. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4717. )),
  4718. array('label' => array('for' => 'ModelTagsArray')),
  4719. 'Array',
  4720. '/label',
  4721. '/div'
  4722. );
  4723. $this->assertTags($result, $expected);
  4724. $result = $this->Form->select(
  4725. 'Model.multi_field',
  4726. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  4727. array('multiple' => 'checkbox')
  4728. );
  4729. $expected = array(
  4730. 'input' => array(
  4731. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4732. ),
  4733. array('div' => array('class' => 'checkbox')),
  4734. array('input' => array(
  4735. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4736. 'value' => 'a+', 'id' => 'ModelMultiFieldA+'
  4737. )),
  4738. array('label' => array('for' => 'ModelMultiFieldA+')),
  4739. 'first',
  4740. '/label',
  4741. '/div',
  4742. array('div' => array('class' => 'checkbox')),
  4743. array('input' => array(
  4744. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4745. 'value' => 'a++', 'id' => 'ModelMultiFieldA++'
  4746. )),
  4747. array('label' => array('for' => 'ModelMultiFieldA++')),
  4748. 'second',
  4749. '/label',
  4750. '/div',
  4751. array('div' => array('class' => 'checkbox')),
  4752. array('input' => array(
  4753. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4754. 'value' => 'a+++', 'id' => 'ModelMultiFieldA+++'
  4755. )),
  4756. array('label' => array('for' => 'ModelMultiFieldA+++')),
  4757. 'third',
  4758. '/label',
  4759. '/div'
  4760. );
  4761. $this->assertTags($result, $expected);
  4762. $result = $this->Form->select(
  4763. 'Model.multi_field',
  4764. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4765. array('multiple' => 'checkbox')
  4766. );
  4767. $expected = array(
  4768. 'input' => array(
  4769. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4770. ),
  4771. array('div' => array('class' => 'checkbox')),
  4772. array('input' => array(
  4773. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4774. 'value' => 'a&gt;b', 'id' => 'ModelMultiFieldAB2'
  4775. )),
  4776. array('label' => array('for' => 'ModelMultiFieldAB2')),
  4777. 'first',
  4778. '/label',
  4779. '/div',
  4780. array('div' => array('class' => 'checkbox')),
  4781. array('input' => array(
  4782. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4783. 'value' => 'a&lt;b', 'id' => 'ModelMultiFieldAB1'
  4784. )),
  4785. array('label' => array('for' => 'ModelMultiFieldAB1')),
  4786. 'second',
  4787. '/label',
  4788. '/div',
  4789. array('div' => array('class' => 'checkbox')),
  4790. array('input' => array(
  4791. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4792. 'value' => 'a&quot;b', 'id' => 'ModelMultiFieldAB'
  4793. )),
  4794. array('label' => array('for' => 'ModelMultiFieldAB')),
  4795. 'third',
  4796. '/label',
  4797. '/div'
  4798. );
  4799. $this->assertTags($result, $expected);
  4800. }
  4801. /**
  4802. * test multiple checkboxes with div styles.
  4803. *
  4804. * @return void
  4805. */
  4806. public function testSelectMultipleCheckboxDiv() {
  4807. $result = $this->Form->select(
  4808. 'Model.tags',
  4809. array('first', 'second'),
  4810. array('multiple' => 'checkbox', 'class' => 'my-class')
  4811. );
  4812. $expected = array(
  4813. 'input' => array(
  4814. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'ModelTags'
  4815. ),
  4816. array('div' => array('class' => 'my-class')),
  4817. array('input' => array(
  4818. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4819. 'value' => '0', 'id' => 'ModelTags0'
  4820. )),
  4821. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4822. '/div',
  4823. array('div' => array('class' => 'my-class')),
  4824. array('input' => array(
  4825. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4826. 'value' => '1', 'id' => 'ModelTags1'
  4827. )),
  4828. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4829. '/div'
  4830. );
  4831. $this->assertTags($result, $expected);
  4832. $result = $this->Form->input('Model.tags', array(
  4833. 'options' => array('first', 'second'),
  4834. 'multiple' => 'checkbox',
  4835. 'class' => 'my-class',
  4836. 'div' => false,
  4837. 'label' => false
  4838. ));
  4839. $this->assertTags($result, $expected);
  4840. $Contact = ClassRegistry::getObject('Contact');
  4841. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4842. $result = $this->Form->input('Contact.tags', array(
  4843. 'options' => array('one'),
  4844. 'multiple' => 'checkbox',
  4845. 'label' => false,
  4846. 'div' => false
  4847. ));
  4848. $expected = array(
  4849. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[tags]', 'value' => '', 'id' => 'ContactTags'),
  4850. array('div' => array('class' => 'checkbox form-error')),
  4851. array('input' => array('type' => 'checkbox', 'name' => 'Contact[tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4852. array('label' => array('for' => 'ContactTags0')),
  4853. 'one',
  4854. '/label',
  4855. '/div'
  4856. );
  4857. $this->assertTags($result, $expected);
  4858. $result = $this->Form->input('Contact.tags', array(
  4859. 'options' => array('one'),
  4860. 'multiple' => 'checkbox',
  4861. 'class' => 'mycheckbox',
  4862. 'label' => false,
  4863. 'div' => false
  4864. ));
  4865. $expected = array(
  4866. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[tags]', 'value' => '', 'id' => 'ContactTags'),
  4867. array('div' => array('class' => 'mycheckbox form-error')),
  4868. array('input' => array('type' => 'checkbox', 'name' => 'Contact[tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4869. array('label' => array('for' => 'ContactTags0')),
  4870. 'one',
  4871. '/label',
  4872. '/div'
  4873. );
  4874. $this->assertTags($result, $expected);
  4875. }
  4876. /**
  4877. * Checks the security hash array generated for multiple-input checkbox elements
  4878. *
  4879. * @return void
  4880. */
  4881. public function testSelectMultipleCheckboxSecurity() {
  4882. $this->Form->request->params['_csrfToken'] = 'testKey';
  4883. $this->assertEquals(array(), $this->Form->fields);
  4884. $result = $this->Form->select(
  4885. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4886. array('multiple' => 'checkbox')
  4887. );
  4888. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4889. $result = $this->Form->secure($this->Form->fields);
  4890. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4891. $this->assertRegExp('/"' . $key . '"/', $result);
  4892. }
  4893. /**
  4894. * Multiple select elements should always be secured as they always participate
  4895. * in the POST data.
  4896. *
  4897. * @return void
  4898. */
  4899. public function testSelectMultipleSecureWithNoOptions() {
  4900. $this->Form->request->params['_csrfToken'] = 'testkey';
  4901. $this->assertEquals(array(), $this->Form->fields);
  4902. $this->Form->select(
  4903. 'Model.select',
  4904. array(),
  4905. array('multiple' => true)
  4906. );
  4907. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4908. }
  4909. /**
  4910. * When a select box has no options it should not be added to the fields list
  4911. * as it always fail post validation.
  4912. *
  4913. * @return void
  4914. */
  4915. public function testSelectNoSecureWithNoOptions() {
  4916. $this->Form->request->params['_csrfToken'] = 'testkey';
  4917. $this->assertEquals(array(), $this->Form->fields);
  4918. $this->Form->select(
  4919. 'Model.select',
  4920. array()
  4921. );
  4922. $this->assertEquals(array(), $this->Form->fields);
  4923. $this->Form->select(
  4924. 'Model.select',
  4925. array(),
  4926. array('empty' => true)
  4927. );
  4928. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4929. }
  4930. /**
  4931. * testInputMultipleCheckboxes method
  4932. *
  4933. * test input() resulting in multi select elements being generated.
  4934. *
  4935. * @return void
  4936. */
  4937. public function testInputMultipleCheckboxes() {
  4938. $result = $this->Form->input('Model.multi_field', array(
  4939. 'options' => array('first', 'second', 'third'),
  4940. 'multiple' => 'checkbox'
  4941. ));
  4942. $expected = array(
  4943. array('div' => array('class' => 'input select')),
  4944. array('label' => array('for' => 'ModelMultiField')),
  4945. 'Multi Field',
  4946. '/label',
  4947. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4948. array('div' => array('class' => 'checkbox')),
  4949. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4950. array('label' => array('for' => 'ModelMultiField0')),
  4951. 'first',
  4952. '/label',
  4953. '/div',
  4954. array('div' => array('class' => 'checkbox')),
  4955. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4956. array('label' => array('for' => 'ModelMultiField1')),
  4957. 'second',
  4958. '/label',
  4959. '/div',
  4960. array('div' => array('class' => 'checkbox')),
  4961. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4962. array('label' => array('for' => 'ModelMultiField2')),
  4963. 'third',
  4964. '/label',
  4965. '/div',
  4966. '/div'
  4967. );
  4968. $this->assertTags($result, $expected);
  4969. $result = $this->Form->input('Model.multi_field', array(
  4970. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4971. 'multiple' => 'checkbox'
  4972. ));
  4973. $expected = array(
  4974. array('div' => array('class' => 'input select')),
  4975. array('label' => array('for' => 'ModelMultiField')),
  4976. 'Multi Field',
  4977. '/label',
  4978. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4979. array('div' => array('class' => 'checkbox')),
  4980. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4981. array('label' => array('for' => 'ModelMultiFieldA')),
  4982. 'first',
  4983. '/label',
  4984. '/div',
  4985. array('div' => array('class' => 'checkbox')),
  4986. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4987. array('label' => array('for' => 'ModelMultiFieldB')),
  4988. 'second',
  4989. '/label',
  4990. '/div',
  4991. array('div' => array('class' => 'checkbox')),
  4992. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4993. array('label' => array('for' => 'ModelMultiFieldC')),
  4994. 'third',
  4995. '/label',
  4996. '/div',
  4997. '/div'
  4998. );
  4999. $this->assertTags($result, $expected);
  5000. $result = $this->Form->input('Model.multi_field', array(
  5001. 'options' => array('1' => 'first'),
  5002. 'multiple' => 'checkbox',
  5003. 'label' => false,
  5004. 'div' => false
  5005. ));
  5006. $expected = array(
  5007. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5008. array('div' => array('class' => 'checkbox')),
  5009. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5010. array('label' => array('for' => 'ModelMultiField1')),
  5011. 'first',
  5012. '/label',
  5013. '/div'
  5014. );
  5015. $this->assertTags($result, $expected);
  5016. $result = $this->Form->input('Model.multi_field', array(
  5017. 'options' => array('2' => 'second'),
  5018. 'multiple' => 'checkbox',
  5019. 'label' => false,
  5020. 'div' => false
  5021. ));
  5022. $expected = array(
  5023. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5024. array('div' => array('class' => 'checkbox')),
  5025. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  5026. array('label' => array('for' => 'ModelMultiField2')),
  5027. 'second',
  5028. '/label',
  5029. '/div'
  5030. );
  5031. $this->assertTags($result, $expected);
  5032. }
  5033. /**
  5034. * testSelectHiddenFieldOmission method
  5035. *
  5036. * test that select() with 'hiddenField' => false omits the hidden field
  5037. *
  5038. * @return void
  5039. */
  5040. public function testSelectHiddenFieldOmission() {
  5041. $result = $this->Form->select('Model.multi_field',
  5042. array('first', 'second'),
  5043. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  5044. );
  5045. $expected = array(
  5046. array('div' => array('class' => 'checkbox')),
  5047. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5048. array('label' => array('for' => 'ModelMultiField0')),
  5049. 'first',
  5050. '/label',
  5051. '/div',
  5052. array('div' => array('class' => 'checkbox')),
  5053. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5054. array('label' => array('for' => 'ModelMultiField1')),
  5055. 'second',
  5056. '/label',
  5057. '/div'
  5058. );
  5059. $this->assertTags($result, $expected);
  5060. $result = $this->Form->input('Model.multi_field', array(
  5061. 'options' => array('first', 'second'),
  5062. 'multiple' => 'checkbox',
  5063. 'hiddenField' => false
  5064. ));
  5065. $expected = array(
  5066. array('div' => array('class' => 'input select')),
  5067. array('label' => array('for' => 'ModelMultiField')),
  5068. 'Multi Field',
  5069. '/label',
  5070. array('div' => array('class' => 'checkbox')),
  5071. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5072. array('label' => array('for' => 'ModelMultiField0')),
  5073. 'first',
  5074. '/label',
  5075. '/div',
  5076. array('div' => array('class' => 'checkbox')),
  5077. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5078. array('label' => array('for' => 'ModelMultiField1')),
  5079. 'second',
  5080. '/label',
  5081. '/div',
  5082. '/div'
  5083. );
  5084. $this->assertTags($result, $expected);
  5085. }
  5086. /**
  5087. * test that select() with multiple = checkbox works with overriding name attribute.
  5088. *
  5089. * @return void
  5090. */
  5091. public function testSelectCheckboxMultipleOverrideName() {
  5092. $result = $this->Form->input('category', array(
  5093. 'type' => 'select',
  5094. 'multiple' => 'checkbox',
  5095. 'name' => 'fish',
  5096. 'options' => array('1', '2'),
  5097. 'div' => false,
  5098. 'label' => false,
  5099. ));
  5100. $expected = array(
  5101. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'),
  5102. array('div' => array('class' => 'checkbox')),
  5103. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'Category0')),
  5104. array('label' => array('for' => 'Category0')), '1', '/label',
  5105. '/div',
  5106. array('div' => array('class' => 'checkbox')),
  5107. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'Category1')),
  5108. array('label' => array('for' => 'Category1')), '2', '/label',
  5109. '/div'
  5110. );
  5111. $this->assertTags($result, $expected);
  5112. }
  5113. /**
  5114. * Test that 'id' overrides all the checkbox id's as well.
  5115. *
  5116. * @return void
  5117. */
  5118. public function testSelectCheckboxMultipleId() {
  5119. $result = $this->Form->select(
  5120. 'Model.multi_field',
  5121. array('first', 'second', 'third'),
  5122. array('multiple' => 'checkbox', 'id' => 'CustomId')
  5123. );
  5124. $expected = array(
  5125. 'input' => array(
  5126. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'CustomId'
  5127. ),
  5128. array('div' => array('class' => 'checkbox')),
  5129. array('input' => array(
  5130. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5131. 'value' => '0', 'id' => 'CustomId0'
  5132. )),
  5133. array('label' => array('for' => 'CustomId0')),
  5134. 'first',
  5135. '/label',
  5136. '/div',
  5137. array('div' => array('class' => 'checkbox')),
  5138. array('input' => array(
  5139. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5140. 'value' => '1', 'id' => 'CustomId1'
  5141. )),
  5142. array('label' => array('for' => 'CustomId1')),
  5143. 'second',
  5144. '/label',
  5145. '/div',
  5146. array('div' => array('class' => 'checkbox')),
  5147. array('input' => array(
  5148. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5149. 'value' => '2', 'id' => 'CustomId2'
  5150. )),
  5151. array('label' => array('for' => 'CustomId2')),
  5152. 'third',
  5153. '/label',
  5154. '/div'
  5155. );
  5156. $this->assertTags($result, $expected);
  5157. }
  5158. /**
  5159. * testCheckbox method
  5160. *
  5161. * Test generation of checkboxes
  5162. *
  5163. * @return void
  5164. */
  5165. public function testCheckbox() {
  5166. $result = $this->Form->checkbox('Model.field');
  5167. $expected = array(
  5168. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5169. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField'))
  5170. );
  5171. $this->assertTags($result, $expected);
  5172. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  5173. $expected = array(
  5174. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'theID_'),
  5175. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => 'myvalue', 'id' => 'theID'))
  5176. );
  5177. $this->assertTags($result, $expected);
  5178. $Contact = ClassRegistry::getObject('Contact');
  5179. $Contact->validationErrors['field'] = 1;
  5180. $this->Form->request->data['Contact']['field'] = 'myvalue';
  5181. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  5182. $expected = array(
  5183. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'theID_'),
  5184. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  5185. );
  5186. $this->assertTags($result, $expected);
  5187. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5188. $expected = array(
  5189. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'ContactField_'),
  5190. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  5191. );
  5192. $this->assertTags($result, $expected);
  5193. $this->Form->request->data['Contact']['field'] = '';
  5194. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  5195. $expected = array(
  5196. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'theID_'),
  5197. array('input' => array('type' => 'checkbox', 'name' => 'Contact[field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  5198. );
  5199. $this->assertTags($result, $expected);
  5200. $Contact->validationErrors = array();
  5201. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5202. $expected = array(
  5203. 'input' => array('type' => 'hidden', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'ContactField_'),
  5204. array('input' => array('type' => 'checkbox', 'name' => 'Contact[field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  5205. );
  5206. $this->assertTags($result, $expected);
  5207. $this->Form->request->data['Contact']['published'] = 1;
  5208. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5209. $expected = array(
  5210. 'input' => array('type' => 'hidden', 'name' => 'Contact[published]', 'value' => '0', 'id' => 'theID_'),
  5211. array('input' => array('type' => 'checkbox', 'name' => 'Contact[published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  5212. );
  5213. $this->assertTags($result, $expected);
  5214. $this->Form->request->data['Contact']['published'] = 0;
  5215. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5216. $expected = array(
  5217. 'input' => array('type' => 'hidden', 'name' => 'Contact[published]', 'value' => '0', 'id' => 'theID_'),
  5218. array('input' => array('type' => 'checkbox', 'name' => 'Contact[published]', 'value' => '1', 'id' => 'theID'))
  5219. );
  5220. $this->assertTags($result, $expected);
  5221. $result = $this->Form->checkbox('Model.CustomField.1.value');
  5222. $expected = array(
  5223. 'input' => array('type' => 'hidden', 'name' => 'Model[CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  5224. array('input' => array('type' => 'checkbox', 'name' => 'Model[CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  5225. );
  5226. $this->assertTags($result, $expected);
  5227. $result = $this->Form->checkbox('CustomField.1.value');
  5228. $expected = array(
  5229. 'input' => array('type' => 'hidden', 'name' => 'CustomField[1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  5230. array('input' => array('type' => 'checkbox', 'name' => 'CustomField[1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  5231. );
  5232. $this->assertTags($result, $expected);
  5233. }
  5234. /**
  5235. * test checkbox() with a custom name attribute
  5236. *
  5237. * @return void
  5238. */
  5239. public function testCheckboxCustomNameAttribute() {
  5240. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  5241. $expected = array(
  5242. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  5243. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  5244. );
  5245. $this->assertTags($result, $expected);
  5246. }
  5247. /**
  5248. * test the checked option for checkboxes.
  5249. *
  5250. * @return void
  5251. */
  5252. public function testCheckboxCheckedOption() {
  5253. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  5254. $expected = array(
  5255. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5256. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5257. );
  5258. $this->assertTags($result, $expected);
  5259. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  5260. $expected = array(
  5261. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5262. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5263. );
  5264. $this->assertTags($result, $expected);
  5265. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  5266. $expected = array(
  5267. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5268. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5269. );
  5270. $this->assertTags($result, $expected);
  5271. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5272. $expected = array(
  5273. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5274. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField'))
  5275. );
  5276. $this->assertTags($result, $expected);
  5277. $this->Form->request->data['Model']['field'] = 1;
  5278. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5279. $expected = array(
  5280. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5281. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField'))
  5282. );
  5283. $this->assertTags($result, $expected);
  5284. }
  5285. /**
  5286. * Test that disabled attribute works on both the checkbox and hidden input.
  5287. *
  5288. * @return void
  5289. */
  5290. public function testCheckboxDisabling() {
  5291. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5292. $expected = array(
  5293. array('input' => array('type' => 'hidden', 'name' => 'Account[show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5294. array('input' => array('type' => 'checkbox', 'name' => 'Account[show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5295. );
  5296. $this->assertTags($result, $expected);
  5297. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5298. $expected = array(
  5299. array('input' => array('type' => 'hidden', 'name' => 'Account[show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5300. array('input' => array('type' => 'checkbox', 'name' => 'Account[show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5301. );
  5302. $this->assertTags($result, $expected);
  5303. }
  5304. /**
  5305. * Test that the hidden input for checkboxes can be omitted or set to a
  5306. * specific value.
  5307. *
  5308. * @return void
  5309. */
  5310. public function testCheckboxHiddenField() {
  5311. $result = $this->Form->input('UserForm.something', array(
  5312. 'type' => 'checkbox',
  5313. 'hiddenField' => false
  5314. ));
  5315. $expected = array(
  5316. 'div' => array('class' => 'input checkbox'),
  5317. array('input' => array(
  5318. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5319. 'value' => '1', 'id' => 'UserFormSomething'
  5320. )),
  5321. 'label' => array('for' => 'UserFormSomething'),
  5322. 'Something',
  5323. '/label',
  5324. '/div'
  5325. );
  5326. $this->assertTags($result, $expected);
  5327. $result = $this->Form->input('UserForm.something', array(
  5328. 'type' => 'checkbox',
  5329. 'value' => 'Y',
  5330. 'hiddenField' => 'N',
  5331. ));
  5332. $expected = array(
  5333. 'div' => array('class' => 'input checkbox'),
  5334. array('input' => array(
  5335. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5336. 'value' => 'N', 'id' => 'UserFormSomething_'
  5337. )),
  5338. array('input' => array(
  5339. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5340. 'value' => 'Y', 'id' => 'UserFormSomething'
  5341. )),
  5342. 'label' => array('for' => 'UserFormSomething'),
  5343. 'Something',
  5344. '/label',
  5345. '/div'
  5346. );
  5347. $this->assertTags($result, $expected);
  5348. }
  5349. /**
  5350. * testDateTime method
  5351. *
  5352. * Test generation of date/time select elements
  5353. *
  5354. * @return void
  5355. */
  5356. public function testDateTime() {
  5357. extract($this->dateRegex);
  5358. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5359. $now = strtotime('now');
  5360. $expected = array(
  5361. array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')),
  5362. $daysRegex,
  5363. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5364. date('j', $now),
  5365. '/option',
  5366. '*/select',
  5367. '-',
  5368. array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')),
  5369. $monthsRegex,
  5370. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5371. date('F', $now),
  5372. '/option',
  5373. '*/select',
  5374. '-',
  5375. array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')),
  5376. $yearsRegex,
  5377. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5378. date('Y', $now),
  5379. '/option',
  5380. '*/select',
  5381. array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')),
  5382. $hoursRegex,
  5383. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5384. date('g', $now),
  5385. '/option',
  5386. '*/select',
  5387. ':',
  5388. array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')),
  5389. $minutesRegex,
  5390. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5391. date('i', $now),
  5392. '/option',
  5393. '*/select',
  5394. ' ',
  5395. array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')),
  5396. $meridianRegex,
  5397. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5398. date('a', $now),
  5399. '/option',
  5400. '*/select'
  5401. );
  5402. $this->assertTags($result, $expected);
  5403. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5404. $expected = array(
  5405. array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')),
  5406. $daysRegex,
  5407. array('option' => array('value' => '')),
  5408. '/option',
  5409. '*/select',
  5410. '-',
  5411. array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')),
  5412. $monthsRegex,
  5413. array('option' => array('value' => '')),
  5414. '/option',
  5415. '*/select',
  5416. '-',
  5417. array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')),
  5418. $yearsRegex,
  5419. array('option' => array('value' => '')),
  5420. '/option',
  5421. '*/select',
  5422. array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')),
  5423. $hoursRegex,
  5424. array('option' => array('value' => '')),
  5425. '/option',
  5426. '*/select',
  5427. ':',
  5428. array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')),
  5429. $minutesRegex,
  5430. array('option' => array('value' => '')),
  5431. '/option',
  5432. '*/select',
  5433. ' ',
  5434. array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')),
  5435. $meridianRegex,
  5436. array('option' => array('value' => '')),
  5437. '/option',
  5438. '*/select'
  5439. );
  5440. $this->assertTags($result, $expected);
  5441. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5442. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5443. $this->assertTags($result, $expected);
  5444. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5445. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5446. $this->assertTags($result, $expected);
  5447. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5448. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5449. $expected = array(
  5450. array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')),
  5451. $daysRegex,
  5452. array('option' => array('value' => '')),
  5453. '/option',
  5454. '*/select',
  5455. '-',
  5456. array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')),
  5457. $monthsRegex,
  5458. array('option' => array('value' => '')),
  5459. '/option',
  5460. '*/select',
  5461. '-',
  5462. array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')),
  5463. $yearsRegex,
  5464. array('option' => array('value' => '')),
  5465. '/option',
  5466. '*/select',
  5467. array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')),
  5468. $hoursRegex,
  5469. array('option' => array('value' => '')),
  5470. '/option',
  5471. '*/select',
  5472. ':',
  5473. array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')),
  5474. $minutesRegex,
  5475. array('option' => array('value' => '')),
  5476. '/option',
  5477. array('option' => array('value' => '00')),
  5478. '00',
  5479. '/option',
  5480. array('option' => array('value' => '05')),
  5481. '05',
  5482. '/option',
  5483. array('option' => array('value' => '10')),
  5484. '10',
  5485. '/option',
  5486. '*/select',
  5487. ' ',
  5488. array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')),
  5489. $meridianRegex,
  5490. array('option' => array('value' => '')),
  5491. '/option',
  5492. '*/select'
  5493. );
  5494. $this->assertTags($result, $expected);
  5495. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5496. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5497. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5498. $this->Form->request->data['Contact']['data'] = null;
  5499. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5500. $expected = array(
  5501. array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')),
  5502. $daysRegex,
  5503. array('option' => array('value' => '')),
  5504. '/option',
  5505. '*/select',
  5506. '-',
  5507. array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')),
  5508. $monthsRegex,
  5509. array('option' => array('value' => '')),
  5510. '/option',
  5511. '*/select',
  5512. '-',
  5513. array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')),
  5514. $yearsRegex,
  5515. array('option' => array('value' => '')),
  5516. '/option',
  5517. '*/select',
  5518. array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')),
  5519. $hoursRegex,
  5520. array('option' => array('value' => '')),
  5521. '/option',
  5522. '*/select',
  5523. ':',
  5524. array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')),
  5525. $minutesRegex,
  5526. array('option' => array('value' => '')),
  5527. '/option',
  5528. '*/select',
  5529. ' ',
  5530. array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')),
  5531. $meridianRegex,
  5532. array('option' => array('value' => '')),
  5533. '/option',
  5534. '*/select'
  5535. );
  5536. $this->assertTags($result, $expected);
  5537. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5538. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5539. $now = strtotime($this->Form->data['Model']['field']);
  5540. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5541. $expected = array(
  5542. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  5543. $daysRegex,
  5544. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5545. date('j', $now),
  5546. '/option',
  5547. '*/select',
  5548. '-',
  5549. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5550. $monthsRegex,
  5551. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5552. date('F', $now),
  5553. '/option',
  5554. '*/select',
  5555. '-',
  5556. array('select' => array('name' => 'Model[field][year]', 'id' => 'ModelFieldYear')),
  5557. $yearsRegex,
  5558. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5559. date('Y', $now),
  5560. '/option',
  5561. '*/select',
  5562. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  5563. $hoursRegex,
  5564. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5565. date('g', $now),
  5566. '/option',
  5567. '*/select',
  5568. ':',
  5569. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5570. $minutesRegex,
  5571. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5572. date('i', $now),
  5573. '/option',
  5574. '*/select',
  5575. ' ',
  5576. array('select' => array('name' => 'Model[field][meridian]', 'id' => 'ModelFieldMeridian')),
  5577. $meridianRegex,
  5578. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5579. date('a', $now),
  5580. '/option',
  5581. '*/select'
  5582. );
  5583. $this->assertTags($result, $expected);
  5584. $selected = strtotime('2008-10-26 12:33:00');
  5585. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5586. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5587. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5588. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5589. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5590. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5591. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5592. $this->Form->create('Contact');
  5593. $result = $this->Form->input('published');
  5594. $now = strtotime('now');
  5595. $expected = array(
  5596. 'div' => array('class' => 'input date'),
  5597. 'label' => array('for' => 'ContactPublishedMonth'),
  5598. 'Published',
  5599. '/label',
  5600. array('select' => array('name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth')),
  5601. $monthsRegex,
  5602. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5603. date('F', $now),
  5604. '/option',
  5605. '*/select',
  5606. '-',
  5607. array('select' => array('name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay')),
  5608. $daysRegex,
  5609. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5610. date('j', $now),
  5611. '/option',
  5612. '*/select',
  5613. '-',
  5614. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  5615. $yearsRegex,
  5616. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5617. date('Y', $now),
  5618. '/option',
  5619. '*/select',
  5620. '/div'
  5621. );
  5622. $this->assertTags($result, $expected);
  5623. $result = $this->Form->input('published2', array('type' => 'date'));
  5624. $now = strtotime('now');
  5625. $expected = array(
  5626. 'div' => array('class' => 'input date'),
  5627. 'label' => array('for' => 'ContactPublished2Month'),
  5628. 'Published2',
  5629. '/label',
  5630. array('select' => array('name' => 'Contact[published2][month]', 'id' => 'ContactPublished2Month')),
  5631. $monthsRegex,
  5632. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5633. date('F', $now),
  5634. '/option',
  5635. '*/select',
  5636. '-',
  5637. array('select' => array('name' => 'Contact[published2][day]', 'id' => 'ContactPublished2Day')),
  5638. $daysRegex,
  5639. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5640. date('j', $now),
  5641. '/option',
  5642. '*/select',
  5643. '-',
  5644. array('select' => array('name' => 'Contact[published2][year]', 'id' => 'ContactPublished2Year')),
  5645. $yearsRegex,
  5646. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5647. date('Y', $now),
  5648. '/option',
  5649. '*/select',
  5650. '/div'
  5651. );
  5652. $this->assertTags($result, $expected);
  5653. $this->Form->create('Contact');
  5654. $result = $this->Form->input('published', array('monthNames' => false));
  5655. $now = strtotime('now');
  5656. $expected = array(
  5657. 'div' => array('class' => 'input date'),
  5658. 'label' => array('for' => 'ContactPublishedMonth'),
  5659. 'Published',
  5660. '/label',
  5661. array('select' => array('name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth')),
  5662. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5663. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5664. date('m', $now),
  5665. '/option',
  5666. '*/select',
  5667. '-',
  5668. array('select' => array('name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay')),
  5669. $daysRegex,
  5670. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5671. date('j', $now),
  5672. '/option',
  5673. '*/select',
  5674. '-',
  5675. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  5676. $yearsRegex,
  5677. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5678. date('Y', $now),
  5679. '/option',
  5680. '*/select',
  5681. '/div'
  5682. );
  5683. $this->assertTags($result, $expected);
  5684. $result = $this->Form->input('published', array('type' => 'time'));
  5685. $now = strtotime('now');
  5686. $expected = array(
  5687. 'div' => array('class' => 'input time'),
  5688. 'label' => array('for' => 'ContactPublishedHour'),
  5689. 'Published',
  5690. '/label',
  5691. array('select' => array('name' => 'Contact[published][hour]', 'id' => 'ContactPublishedHour')),
  5692. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5693. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5694. date('g', $now),
  5695. '/option',
  5696. '*/select',
  5697. ':',
  5698. );
  5699. $this->assertTags($result, $expected);
  5700. $result = $this->Form->input('published', array(
  5701. 'timeFormat' => 24,
  5702. 'interval' => 5,
  5703. 'selected' => strtotime('2009-09-03 13:37:00'),
  5704. 'type' => 'datetime'
  5705. ));
  5706. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5707. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5708. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5709. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5710. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5711. }
  5712. /**
  5713. * Test dateTime with rounding
  5714. *
  5715. * @return void
  5716. */
  5717. public function testDateTimeRounding() {
  5718. $this->Form->request->data['Contact'] = array(
  5719. 'date' => array(
  5720. 'day' => '13',
  5721. 'month' => '12',
  5722. 'year' => '2010',
  5723. 'hour' => '04',
  5724. 'min' => '19',
  5725. 'meridian' => 'AM'
  5726. )
  5727. );
  5728. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5729. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5730. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5731. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5732. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5733. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5734. }
  5735. /**
  5736. * Test that empty values don't trigger errors.
  5737. *
  5738. * @return void
  5739. */
  5740. public function testDateTimeNoErrorsOnEmptyData() {
  5741. $this->Form->request->data['Contact'] = array(
  5742. 'date' => array(
  5743. 'day' => '',
  5744. 'month' => '',
  5745. 'year' => '',
  5746. 'hour' => '',
  5747. 'min' => '',
  5748. 'meridian' => ''
  5749. )
  5750. );
  5751. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5752. $this->assertNotEmpty($result);
  5753. }
  5754. /**
  5755. * test that datetime() and default values work.
  5756. *
  5757. * @return void
  5758. */
  5759. public function testDatetimeWithDefault() {
  5760. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5761. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5762. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5763. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5764. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5765. 'default' => '2009-06-01 11:15:30'
  5766. ));
  5767. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5768. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5769. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5770. }
  5771. /**
  5772. * test that bogus non-date time data doesn't cause errors.
  5773. *
  5774. * @return void
  5775. */
  5776. public function testDateTimeWithBogusData() {
  5777. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5778. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5779. }
  5780. /**
  5781. * testDateTimeEmptyAsArray
  5782. *
  5783. * @return void
  5784. */
  5785. public function testDateTimeEmptyAsArray() {
  5786. $result = $this->Form->dateTime('Contact.date',
  5787. 'DMY',
  5788. '12',
  5789. array(
  5790. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5791. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5792. )
  5793. )
  5794. );
  5795. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5796. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5797. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5798. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5799. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5800. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5801. $result = $this->Form->dateTime('Contact.date',
  5802. 'DMY',
  5803. '12',
  5804. array(
  5805. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5806. )
  5807. );
  5808. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5809. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5810. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5811. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5812. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5813. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5814. }
  5815. /**
  5816. * testFormDateTimeMulti method
  5817. *
  5818. * test multiple datetime element generation
  5819. *
  5820. * @return void
  5821. */
  5822. public function testFormDateTimeMulti() {
  5823. extract($this->dateRegex);
  5824. $result = $this->Form->dateTime('Contact.1.updated');
  5825. $expected = array(
  5826. array('select' => array('name' => 'Contact[1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5827. $daysRegex,
  5828. array('option' => array('value' => '')),
  5829. '/option',
  5830. '*/select',
  5831. '-',
  5832. array('select' => array('name' => 'Contact[1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5833. $monthsRegex,
  5834. array('option' => array('value' => '')),
  5835. '/option',
  5836. '*/select',
  5837. '-',
  5838. array('select' => array('name' => 'Contact[1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5839. $yearsRegex,
  5840. array('option' => array('value' => '')),
  5841. '/option',
  5842. '*/select',
  5843. array('select' => array('name' => 'Contact[1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5844. $hoursRegex,
  5845. array('option' => array('value' => '')),
  5846. '/option',
  5847. '*/select',
  5848. ':',
  5849. array('select' => array('name' => 'Contact[1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5850. $minutesRegex,
  5851. array('option' => array('value' => '')),
  5852. '/option',
  5853. '*/select',
  5854. ' ',
  5855. array('select' => array('name' => 'Contact[1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5856. $meridianRegex,
  5857. array('option' => array('value' => '')),
  5858. '/option',
  5859. '*/select'
  5860. );
  5861. $this->assertTags($result, $expected);
  5862. $result = $this->Form->dateTime('Contact.2.updated');
  5863. $expected = array(
  5864. array('select' => array('name' => 'Contact[2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5865. $daysRegex,
  5866. array('option' => array('value' => '')),
  5867. '/option',
  5868. '*/select',
  5869. '-',
  5870. array('select' => array('name' => 'Contact[2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5871. $monthsRegex,
  5872. array('option' => array('value' => '')),
  5873. '/option',
  5874. '*/select',
  5875. '-',
  5876. array('select' => array('name' => 'Contact[2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5877. $yearsRegex,
  5878. array('option' => array('value' => '')),
  5879. '/option',
  5880. '*/select',
  5881. array('select' => array('name' => 'Contact[2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5882. $hoursRegex,
  5883. array('option' => array('value' => '')),
  5884. '/option',
  5885. '*/select',
  5886. ':',
  5887. array('select' => array('name' => 'Contact[2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5888. $minutesRegex,
  5889. array('option' => array('value' => '')),
  5890. '/option',
  5891. '*/select',
  5892. ' ',
  5893. array('select' => array('name' => 'Contact[2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5894. $meridianRegex,
  5895. array('option' => array('value' => '')),
  5896. '/option',
  5897. '*/select'
  5898. );
  5899. $this->assertTags($result, $expected);
  5900. }
  5901. /**
  5902. * When changing the date format, the label should always focus the first select box when
  5903. * clicked.
  5904. *
  5905. * @return void
  5906. */
  5907. public function testDateTimeLabelIdMatchesFirstInput() {
  5908. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5909. $this->assertContains('label for="ModelDateMonth"', $result);
  5910. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5911. $this->assertContains('label for="ModelDateDay"', $result);
  5912. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5913. $this->assertContains('label for="ModelDateYear"', $result);
  5914. }
  5915. /**
  5916. * testMonth method
  5917. *
  5918. * @return void
  5919. */
  5920. public function testMonth() {
  5921. $result = $this->Form->month('Model.field');
  5922. $expected = array(
  5923. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5924. array('option' => array('value' => '')),
  5925. '/option',
  5926. array('option' => array('value' => '01')),
  5927. date('F', strtotime('2008-01-01 00:00:00')),
  5928. '/option',
  5929. array('option' => array('value' => '02')),
  5930. date('F', strtotime('2008-02-01 00:00:00')),
  5931. '/option',
  5932. '*/select',
  5933. );
  5934. $this->assertTags($result, $expected);
  5935. $result = $this->Form->month('Model.field', array('empty' => true));
  5936. $expected = array(
  5937. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5938. array('option' => array('value' => '')),
  5939. '/option',
  5940. array('option' => array('value' => '01')),
  5941. date('F', strtotime('2008-01-01 00:00:00')),
  5942. '/option',
  5943. array('option' => array('value' => '02')),
  5944. date('F', strtotime('2008-02-01 00:00:00')),
  5945. '/option',
  5946. '*/select',
  5947. );
  5948. $this->assertTags($result, $expected);
  5949. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5950. $expected = array(
  5951. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5952. array('option' => array('value' => '')),
  5953. '/option',
  5954. array('option' => array('value' => '01')),
  5955. '01',
  5956. '/option',
  5957. array('option' => array('value' => '02')),
  5958. '02',
  5959. '/option',
  5960. '*/select',
  5961. );
  5962. $this->assertTags($result, $expected);
  5963. $monthNames = array(
  5964. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5965. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5966. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5967. $expected = array(
  5968. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5969. array('option' => array('value' => '')),
  5970. '/option',
  5971. array('option' => array('value' => '01')),
  5972. 'Jan',
  5973. '/option',
  5974. array('option' => array('value' => '02')),
  5975. 'Feb',
  5976. '/option',
  5977. '*/select',
  5978. );
  5979. $this->assertTags($result, $expected);
  5980. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5981. $result = $this->Form->month('Project.release');
  5982. $expected = array(
  5983. array('select' => array('name' => 'Project[release][month]', 'id' => 'ProjectReleaseMonth')),
  5984. array('option' => array('value' => '')),
  5985. '/option',
  5986. array('option' => array('value' => '01')),
  5987. 'January',
  5988. '/option',
  5989. array('option' => array('value' => '02', 'selected' => 'selected')),
  5990. 'February',
  5991. '/option',
  5992. '*/select',
  5993. );
  5994. $this->assertTags($result, $expected);
  5995. }
  5996. /**
  5997. * testDay method
  5998. *
  5999. * @return void
  6000. */
  6001. public function testDay() {
  6002. extract($this->dateRegex);
  6003. $result = $this->Form->day('Model.field', array('value' => false));
  6004. $expected = array(
  6005. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  6006. array('option' => array('value' => '')),
  6007. '/option',
  6008. array('option' => array('value' => '01')),
  6009. '1',
  6010. '/option',
  6011. array('option' => array('value' => '02')),
  6012. '2',
  6013. '/option',
  6014. $daysRegex,
  6015. '/select',
  6016. );
  6017. $this->assertTags($result, $expected);
  6018. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6019. $result = $this->Form->day('Model.field');
  6020. $expected = array(
  6021. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  6022. array('option' => array('value' => '')),
  6023. '/option',
  6024. array('option' => array('value' => '01')),
  6025. '1',
  6026. '/option',
  6027. array('option' => array('value' => '02')),
  6028. '2',
  6029. '/option',
  6030. $daysRegex,
  6031. array('option' => array('value' => '10', 'selected' => 'selected')),
  6032. '10',
  6033. '/option',
  6034. $daysRegex,
  6035. '/select',
  6036. );
  6037. $this->assertTags($result, $expected);
  6038. $this->Form->request->data['Model']['field'] = '';
  6039. $result = $this->Form->day('Model.field', array('value' => '10'));
  6040. $expected = array(
  6041. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  6042. array('option' => array('value' => '')),
  6043. '/option',
  6044. array('option' => array('value' => '01')),
  6045. '1',
  6046. '/option',
  6047. array('option' => array('value' => '02')),
  6048. '2',
  6049. '/option',
  6050. $daysRegex,
  6051. array('option' => array('value' => '10', 'selected' => 'selected')),
  6052. '10',
  6053. '/option',
  6054. $daysRegex,
  6055. '/select',
  6056. );
  6057. $this->assertTags($result, $expected);
  6058. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6059. $result = $this->Form->day('Model.field', array('value' => true));
  6060. $expected = array(
  6061. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  6062. array('option' => array('value' => '')),
  6063. '/option',
  6064. array('option' => array('value' => '01')),
  6065. '1',
  6066. '/option',
  6067. array('option' => array('value' => '02')),
  6068. '2',
  6069. '/option',
  6070. $daysRegex,
  6071. array('option' => array('value' => '10', 'selected' => 'selected')),
  6072. '10',
  6073. '/option',
  6074. $daysRegex,
  6075. '/select',
  6076. );
  6077. $this->assertTags($result, $expected);
  6078. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6079. $result = $this->Form->day('Project.release');
  6080. $expected = array(
  6081. array('select' => array('name' => 'Project[release][day]', 'id' => 'ProjectReleaseDay')),
  6082. array('option' => array('value' => '')),
  6083. '/option',
  6084. array('option' => array('value' => '01')),
  6085. '1',
  6086. '/option',
  6087. array('option' => array('value' => '02')),
  6088. '2',
  6089. '/option',
  6090. $daysRegex,
  6091. array('option' => array('value' => '10', 'selected' => 'selected')),
  6092. '10',
  6093. '/option',
  6094. $daysRegex,
  6095. '/select',
  6096. );
  6097. $this->assertTags($result, $expected);
  6098. }
  6099. /**
  6100. * testMinute method
  6101. *
  6102. * @return void
  6103. */
  6104. public function testMinute() {
  6105. extract($this->dateRegex);
  6106. $result = $this->Form->minute('Model.field');
  6107. $expected = array(
  6108. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  6109. array('option' => array('value' => '')),
  6110. '/option',
  6111. array('option' => array('value' => '00')),
  6112. '00',
  6113. '/option',
  6114. array('option' => array('value' => '01')),
  6115. '01',
  6116. '/option',
  6117. array('option' => array('value' => '02')),
  6118. '02',
  6119. '/option',
  6120. $minutesRegex,
  6121. '/select',
  6122. );
  6123. $this->assertTags($result, $expected);
  6124. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6125. $result = $this->Form->minute('Model.field');
  6126. $expected = array(
  6127. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  6128. array('option' => array('value' => '')),
  6129. '/option',
  6130. array('option' => array('value' => '00')),
  6131. '00',
  6132. '/option',
  6133. array('option' => array('value' => '01')),
  6134. '01',
  6135. '/option',
  6136. array('option' => array('value' => '02')),
  6137. '02',
  6138. '/option',
  6139. $minutesRegex,
  6140. array('option' => array('value' => '12', 'selected' => 'selected')),
  6141. '12',
  6142. '/option',
  6143. $minutesRegex,
  6144. '/select',
  6145. );
  6146. $this->assertTags($result, $expected);
  6147. $this->Form->request->data['Model']['field'] = '';
  6148. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6149. $expected = array(
  6150. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  6151. array('option' => array('value' => '')),
  6152. '/option',
  6153. array('option' => array('value' => '00')),
  6154. '00',
  6155. '/option',
  6156. array('option' => array('value' => '05')),
  6157. '05',
  6158. '/option',
  6159. array('option' => array('value' => '10')),
  6160. '10',
  6161. '/option',
  6162. $minutesRegex,
  6163. '/select',
  6164. );
  6165. $this->assertTags($result, $expected);
  6166. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6167. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6168. $expected = array(
  6169. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  6170. array('option' => array('value' => '')),
  6171. '/option',
  6172. array('option' => array('value' => '00')),
  6173. '00',
  6174. '/option',
  6175. array('option' => array('value' => '05')),
  6176. '05',
  6177. '/option',
  6178. array('option' => array('value' => '10', 'selected' => 'selected')),
  6179. '10',
  6180. '/option',
  6181. $minutesRegex,
  6182. '/select',
  6183. );
  6184. $this->assertTags($result, $expected);
  6185. }
  6186. /**
  6187. * testHour method
  6188. *
  6189. * @return void
  6190. */
  6191. public function testHour() {
  6192. extract($this->dateRegex);
  6193. $result = $this->Form->hour('Model.field', false);
  6194. $expected = array(
  6195. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  6196. array('option' => array('value' => '')),
  6197. '/option',
  6198. array('option' => array('value' => '01')),
  6199. '1',
  6200. '/option',
  6201. array('option' => array('value' => '02')),
  6202. '2',
  6203. '/option',
  6204. $hoursRegex,
  6205. '/select',
  6206. );
  6207. $this->assertTags($result, $expected);
  6208. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6209. $result = $this->Form->hour('Model.field', false);
  6210. $expected = array(
  6211. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  6212. array('option' => array('value' => '')),
  6213. '/option',
  6214. array('option' => array('value' => '01')),
  6215. '1',
  6216. '/option',
  6217. array('option' => array('value' => '02')),
  6218. '2',
  6219. '/option',
  6220. $hoursRegex,
  6221. array('option' => array('value' => '12', 'selected' => 'selected')),
  6222. '12',
  6223. '/option',
  6224. '/select',
  6225. );
  6226. $this->assertTags($result, $expected);
  6227. $this->Form->request->data['Model']['field'] = '';
  6228. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  6229. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6230. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  6231. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6232. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6233. $result = $this->Form->hour('Model.field', true);
  6234. $expected = array(
  6235. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  6236. array('option' => array('value' => '')),
  6237. '/option',
  6238. array('option' => array('value' => '00', 'selected' => 'selected')),
  6239. '0',
  6240. '/option',
  6241. array('option' => array('value' => '01')),
  6242. '1',
  6243. '/option',
  6244. array('option' => array('value' => '02')),
  6245. '2',
  6246. '/option',
  6247. $hoursRegex,
  6248. '/select',
  6249. );
  6250. $this->assertTags($result, $expected);
  6251. unset($this->Form->request->data['Model']['field']);
  6252. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  6253. $thisHour = date('H');
  6254. $optValue = date('G');
  6255. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6256. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6257. $result = $this->Form->hour('Model.field', true);
  6258. $expected = array(
  6259. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  6260. array('option' => array('value' => '')),
  6261. '/option',
  6262. array('option' => array('value' => '00')),
  6263. '0',
  6264. '/option',
  6265. array('option' => array('value' => '01', 'selected' => 'selected')),
  6266. '1',
  6267. '/option',
  6268. array('option' => array('value' => '02')),
  6269. '2',
  6270. '/option',
  6271. $hoursRegex,
  6272. '/select',
  6273. );
  6274. $this->assertTags($result, $expected);
  6275. }
  6276. /**
  6277. * testYear method
  6278. *
  6279. * @return void
  6280. */
  6281. public function testYear() {
  6282. $result = $this->Form->year('Model.field', 2006, 2007);
  6283. $expected = array(
  6284. array('select' => array('name' => 'Model[field][year]', 'id' => 'ModelFieldYear')),
  6285. array('option' => array('value' => '')),
  6286. '/option',
  6287. array('option' => array('value' => '2007')),
  6288. '2007',
  6289. '/option',
  6290. array('option' => array('value' => '2006')),
  6291. '2006',
  6292. '/option',
  6293. '/select',
  6294. );
  6295. $this->assertTags($result, $expected);
  6296. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  6297. $expected = array(
  6298. array('select' => array('name' => 'Model[field][year]', 'id' => 'ModelFieldYear')),
  6299. array('option' => array('value' => '')),
  6300. '/option',
  6301. array('option' => array('value' => '2006')),
  6302. '2006',
  6303. '/option',
  6304. array('option' => array('value' => '2007')),
  6305. '2007',
  6306. '/option',
  6307. '/select',
  6308. );
  6309. $this->assertTags($result, $expected);
  6310. $this->request->data['Contact']['published'] = '';
  6311. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6312. $expected = array(
  6313. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6314. array('option' => array('value' => '')),
  6315. '/option',
  6316. array('option' => array('value' => '2007')),
  6317. '2007',
  6318. '/option',
  6319. array('option' => array('value' => '2006')),
  6320. '2006',
  6321. '/option',
  6322. '/select',
  6323. );
  6324. $this->assertTags($result, $expected);
  6325. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6326. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6327. $expected = array(
  6328. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6329. array('option' => array('value' => '2007')),
  6330. '2007',
  6331. '/option',
  6332. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6333. '2006',
  6334. '/option',
  6335. '/select',
  6336. );
  6337. $this->assertTags($result, $expected);
  6338. $this->Form->request->data['Contact']['published'] = '';
  6339. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6340. $expected = array(
  6341. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6342. array('option' => array('value' => '')),
  6343. '/option',
  6344. array('option' => array('value' => '2007')),
  6345. '2007',
  6346. '/option',
  6347. array('option' => array('value' => '2006')),
  6348. '2006',
  6349. '/option',
  6350. '/select',
  6351. );
  6352. $this->assertTags($result, $expected);
  6353. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6354. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6355. $expected = array(
  6356. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6357. array('option' => array('value' => '2007')),
  6358. '2007',
  6359. '/option',
  6360. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6361. '2006',
  6362. '/option',
  6363. '/select',
  6364. );
  6365. $this->assertTags($result, $expected);
  6366. $this->Form->request->data['Contact']['published'] = '';
  6367. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6368. $expected = array(
  6369. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6370. array('option' => array('value' => '')),
  6371. '/option',
  6372. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6373. '2007',
  6374. '/option',
  6375. array('option' => array('value' => '2006')),
  6376. '2006',
  6377. '/option',
  6378. '/select',
  6379. );
  6380. $this->assertTags($result, $expected);
  6381. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6382. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6383. $expected = array(
  6384. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6385. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6386. '2007',
  6387. '/option',
  6388. array('option' => array('value' => '2006')),
  6389. '2006',
  6390. '/option',
  6391. '/select',
  6392. );
  6393. $this->assertTags($result, $expected);
  6394. $this->Form->request->data['Contact']['published'] = '';
  6395. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6396. $expected = array(
  6397. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6398. array('option' => array('value' => '2008')),
  6399. '2008',
  6400. '/option',
  6401. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6402. '2007',
  6403. '/option',
  6404. array('option' => array('value' => '2006')),
  6405. '2006',
  6406. '/option',
  6407. '/select',
  6408. );
  6409. $this->assertTags($result, $expected);
  6410. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6411. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6412. $expected = array(
  6413. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6414. array('option' => array('value' => '2008')),
  6415. '2008',
  6416. '/option',
  6417. array('option' => array('value' => '2007')),
  6418. '2007',
  6419. '/option',
  6420. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6421. '2006',
  6422. '/option',
  6423. '/select',
  6424. );
  6425. $this->assertTags($result, $expected);
  6426. $this->Form->request->data = array();
  6427. $this->Form->create('Contact');
  6428. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6429. $expected = array(
  6430. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6431. array('option' => array('value' => '2008')),
  6432. '2008',
  6433. '/option',
  6434. array('option' => array('value' => '2007')),
  6435. '2007',
  6436. '/option',
  6437. array('option' => array('value' => '2006')),
  6438. '2006',
  6439. '/option',
  6440. '/select',
  6441. );
  6442. $this->assertTags($result, $expected);
  6443. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6444. $this->assertContains('Contact[published][year]', $result);
  6445. }
  6446. /**
  6447. * testYearAutoExpandRange method
  6448. *
  6449. * @return void
  6450. */
  6451. public function testYearAutoExpandRange() {
  6452. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6453. $result = $this->Form->year('User.birthday');
  6454. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6455. $result = $matches[1];
  6456. $expected = range(date('Y') + 20, 1930);
  6457. $this->assertEquals($expected, $result);
  6458. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6459. $result = $this->Form->year('Project.release');
  6460. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6461. $result = $matches[1];
  6462. $expected = range(2050, date('Y') - 20);
  6463. $this->assertEquals($expected, $result);
  6464. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6465. $result = $this->Form->year('Project.release', 1890, 1900);
  6466. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6467. $result = $matches[1];
  6468. $expected = range(1900, 1881);
  6469. $this->assertEquals($expected, $result);
  6470. }
  6471. /**
  6472. * testInputDate method
  6473. *
  6474. * Test various inputs with type date and different dateFormat values.
  6475. * Failing to provide a dateFormat key should not error.
  6476. * It should simply not pre-select any value then.
  6477. *
  6478. * @return void
  6479. */
  6480. public function testInputDate() {
  6481. $this->Form->request->data = array(
  6482. 'User' => array(
  6483. 'month_year' => array('month' => date('m')),
  6484. 'just_year' => array('month' => date('m')),
  6485. 'just_month' => array('year' => date('Y')),
  6486. 'just_day' => array('month' => date('m')),
  6487. )
  6488. );
  6489. $this->Form->create('User');
  6490. $result = $this->Form->input('month_year',
  6491. array(
  6492. 'label' => false,
  6493. 'div' => false,
  6494. 'type' => 'date',
  6495. 'dateFormat' => 'MY',
  6496. 'minYear' => 2006,
  6497. 'maxYear' => 2008
  6498. )
  6499. );
  6500. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6501. $this->assertNotContains('value="2008" selected="selected"', $result);
  6502. $result = $this->Form->input('just_year',
  6503. array(
  6504. 'type' => 'date',
  6505. 'label' => false,
  6506. 'dateFormat' => 'Y',
  6507. 'minYear' => date('Y'),
  6508. 'maxYear' => date('Y', strtotime('+20 years'))
  6509. )
  6510. );
  6511. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6512. $result = $this->Form->input('just_month',
  6513. array(
  6514. 'type' => 'date',
  6515. 'label' => false,
  6516. 'dateFormat' => 'M',
  6517. 'empty' => false,
  6518. )
  6519. );
  6520. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6521. $result = $this->Form->input('just_day',
  6522. array(
  6523. 'type' => 'date',
  6524. 'label' => false,
  6525. 'dateFormat' => 'D',
  6526. 'empty' => false,
  6527. )
  6528. );
  6529. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6530. }
  6531. /**
  6532. * testInputDateMaxYear method
  6533. *
  6534. * Let's say we want to only allow users born from 2006 to 2008 to register
  6535. * This being the first singup page, we still don't have any data
  6536. *
  6537. * @return void
  6538. */
  6539. public function testInputDateMaxYear() {
  6540. $this->Form->request->data = array();
  6541. $this->Form->create('User');
  6542. $result = $this->Form->input('birthday',
  6543. array(
  6544. 'label' => false,
  6545. 'div' => false,
  6546. 'type' => 'date',
  6547. 'dateFormat' => 'DMY',
  6548. 'minYear' => 2006,
  6549. 'maxYear' => 2008
  6550. )
  6551. );
  6552. $this->assertContains('value="2008" selected="selected"', $result);
  6553. }
  6554. /**
  6555. * testTextArea method
  6556. *
  6557. * @return void
  6558. */
  6559. public function testTextArea() {
  6560. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6561. $result = $this->Form->textarea('Model.field');
  6562. $expected = array(
  6563. 'textarea' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  6564. 'some test data',
  6565. '/textarea',
  6566. );
  6567. $this->assertTags($result, $expected);
  6568. $result = $this->Form->textarea('Model.tmp');
  6569. $expected = array(
  6570. 'textarea' => array('name' => 'Model[tmp]', 'id' => 'ModelTmp'),
  6571. '/textarea',
  6572. );
  6573. $this->assertTags($result, $expected);
  6574. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6575. $result = $this->Form->textarea('Model.field');
  6576. $expected = array(
  6577. 'textarea' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  6578. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6579. '/textarea',
  6580. );
  6581. $this->assertTags($result, $expected);
  6582. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6583. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6584. $expected = array(
  6585. 'textarea' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  6586. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6587. '/textarea',
  6588. );
  6589. $this->assertTags($result, $expected);
  6590. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6591. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6592. $expected = array(
  6593. 'textarea' => array('name' => 'Model[0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6594. '/textarea'
  6595. );
  6596. $this->assertTags($result, $expected);
  6597. }
  6598. /**
  6599. * testTextAreaWithStupidCharacters method
  6600. *
  6601. * test text area with non-ascii characters
  6602. *
  6603. * @return void
  6604. */
  6605. public function testTextAreaWithStupidCharacters() {
  6606. $this->loadFixtures('Post');
  6607. $result = $this->Form->input('Post.content', array(
  6608. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6609. ));
  6610. $expected = array(
  6611. 'div' => array('class' => 'input textarea'),
  6612. 'label' => array('for' => 'PostContent'),
  6613. 'Current Text',
  6614. '/label',
  6615. 'textarea' => array('name' => 'Post[content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6616. 'GREAT®',
  6617. '/textarea',
  6618. '/div'
  6619. );
  6620. $this->assertTags($result, $expected);
  6621. }
  6622. /**
  6623. * testHiddenField method
  6624. *
  6625. * @return void
  6626. */
  6627. public function testHiddenField() {
  6628. $Contact = ClassRegistry::getObject('Contact');
  6629. $Contact->validationErrors['field'] = 1;
  6630. $this->Form->request->data['Contact']['field'] = 'test';
  6631. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6632. $this->assertTags($result, array(
  6633. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'id' => 'theID', 'value' => 'test'))
  6634. );
  6635. }
  6636. /**
  6637. * testFileUploadField method
  6638. *
  6639. * @return void
  6640. */
  6641. public function testFileUploadField() {
  6642. $result = $this->Form->file('Model.upload');
  6643. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload')));
  6644. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6645. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6646. $expected = array(
  6647. 'div' => array('class' => 'input file'),
  6648. 'label' => array('for' => 'ModelUpload'),
  6649. 'Upload',
  6650. '/label',
  6651. 'input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload'),
  6652. '/div'
  6653. );
  6654. $this->assertTags($result, $expected);
  6655. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6656. $result = $this->Form->file('Model.upload');
  6657. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload')));
  6658. }
  6659. /**
  6660. * test File upload input on a model not used in create();
  6661. *
  6662. * @return void
  6663. */
  6664. public function testFileUploadOnOtherModel() {
  6665. $this->Form->create('ValidateUser', array('type' => 'file'));
  6666. $result = $this->Form->file('ValidateProfile.city');
  6667. $expected = array(
  6668. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]', 'id' => 'ValidateProfileCity')
  6669. );
  6670. $this->assertTags($result, $expected);
  6671. }
  6672. /**
  6673. * testButton method
  6674. *
  6675. * @return void
  6676. */
  6677. public function testButton() {
  6678. $result = $this->Form->button('Hi');
  6679. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6680. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6681. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6682. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6683. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6684. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6685. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6686. $result = $this->Form->button('No type', array('type' => false));
  6687. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6688. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6689. $this->assertNotRegExp('/\&039/', $result);
  6690. }
  6691. /**
  6692. * Test that button() makes unlocked fields by default.
  6693. *
  6694. * @return void
  6695. */
  6696. public function testButtonUnlockedByDefault() {
  6697. $this->Form->request->params['_csrfToken'] = 'secured';
  6698. $this->Form->button('Save', array('name' => 'save'));
  6699. $this->Form->button('Clear');
  6700. $result = $this->Form->unlockField();
  6701. $this->assertEquals(array('save'), $result);
  6702. }
  6703. /**
  6704. * testPostButton method
  6705. *
  6706. * @return void
  6707. */
  6708. public function testPostButton() {
  6709. $result = $this->Form->postButton('Hi', '/controller/action');
  6710. $this->assertTags($result, array(
  6711. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6712. 'div' => array('style' => 'display:none;'),
  6713. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6714. '/div',
  6715. 'button' => array('type' => 'submit'),
  6716. 'Hi',
  6717. '/button',
  6718. '/form'
  6719. ));
  6720. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6721. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"/>') !== false);
  6722. }
  6723. /**
  6724. * Test that postButton adds _Token fields.
  6725. *
  6726. * @return void
  6727. */
  6728. public function testSecurePostButton() {
  6729. $this->Form->request->params['_csrfToken'] = 'testkey';
  6730. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6731. $expected = array(
  6732. 'form' => array(
  6733. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6734. ),
  6735. array('div' => array('style' => 'display:none;')),
  6736. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6737. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6738. '/div',
  6739. 'button' => array('type' => 'submit'),
  6740. 'Delete',
  6741. '/button',
  6742. array('div' => array('style' => 'display:none;')),
  6743. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6744. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6745. '/div',
  6746. '/form',
  6747. );
  6748. $this->assertTags($result, $expected);
  6749. }
  6750. /**
  6751. * testPostLink method
  6752. *
  6753. * @return void
  6754. */
  6755. public function testPostLink() {
  6756. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6757. $this->assertTags($result, array(
  6758. 'form' => array(
  6759. 'method' => 'post', 'action' => '/posts/delete/1',
  6760. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6761. ),
  6762. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6763. '/form',
  6764. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6765. 'Delete',
  6766. '/a'
  6767. ));
  6768. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6769. $this->assertTags($result, array(
  6770. 'form' => array(
  6771. 'method' => 'post', 'action' => '/posts/delete/1',
  6772. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6773. ),
  6774. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6775. '/form',
  6776. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6777. 'Delete',
  6778. '/a'
  6779. ));
  6780. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6781. $this->assertTags($result, array(
  6782. 'form' => array(
  6783. 'method' => 'post', 'action' => '/posts/delete/1',
  6784. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6785. ),
  6786. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6787. '/form',
  6788. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6789. 'Delete',
  6790. '/a'
  6791. ));
  6792. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  6793. $this->assertTags($result, array(
  6794. 'form' => array(
  6795. 'method' => 'post', 'action' => '/posts/delete/1',
  6796. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6797. ),
  6798. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6799. '/form',
  6800. '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;/'),
  6801. 'Delete',
  6802. '/a'
  6803. ));
  6804. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6805. $this->assertContains('<input type="hidden" name="id" value="1"/>', $result);
  6806. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6807. $this->assertTags($result, array(
  6808. 'form' => array(
  6809. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6810. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6811. ),
  6812. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6813. '/form',
  6814. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6815. 'Delete',
  6816. '/a'
  6817. ));
  6818. $result = $this->Form->postLink(
  6819. '',
  6820. array('controller' => 'items', 'action' => 'delete', 10),
  6821. array('class' => 'btn btn-danger', 'escape' => false),
  6822. 'Confirm thing'
  6823. );
  6824. $this->assertTags($result, array(
  6825. 'form' => array(
  6826. 'method' => 'post', 'action' => '/items/delete/10',
  6827. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6828. ),
  6829. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6830. '/form',
  6831. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6832. '/a'
  6833. ));
  6834. }
  6835. /**
  6836. * test creating postLinks after a GET form.
  6837. *
  6838. * @return void
  6839. */
  6840. public function testPostLinkAfterGetForm() {
  6841. $this->Form->request->params['_Token']['key'] = 'testkey';
  6842. $this->Form->create('User', array('type' => 'get'));
  6843. $this->Form->end();
  6844. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6845. $this->assertTags($result, array(
  6846. 'form' => array(
  6847. 'method' => 'post', 'action' => '/posts/delete/1',
  6848. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6849. ),
  6850. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6851. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6852. 'div' => array('style' => 'display:none;'),
  6853. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6854. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6855. '/div',
  6856. '/form',
  6857. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6858. 'Delete',
  6859. '/a'
  6860. ));
  6861. }
  6862. /**
  6863. * Test that postLink adds _Token fields.
  6864. *
  6865. * @return void
  6866. */
  6867. public function testSecurePostLink() {
  6868. $this->Form->request->params['_csrfToken'] = 'testkey';
  6869. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6870. $expected = array(
  6871. 'form' => array(
  6872. 'method' => 'post', 'action' => '/posts/delete/1',
  6873. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6874. ),
  6875. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6876. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6877. 'div' => array('style' => 'display:none;'),
  6878. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6879. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6880. '/div',
  6881. '/form',
  6882. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6883. 'Delete',
  6884. '/a'
  6885. );
  6886. $this->assertTags($result, $expected);
  6887. }
  6888. /**
  6889. * Test that postLink adds form tags to view block
  6890. *
  6891. * @return void
  6892. */
  6893. public function testPostLinkFormBuffer() {
  6894. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
  6895. $this->assertTags($result, array(
  6896. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6897. 'Delete',
  6898. '/a'
  6899. ));
  6900. $result = $this->View->fetch('postLink');
  6901. $this->assertTags($result, array(
  6902. 'form' => array(
  6903. 'method' => 'post', 'action' => '/posts/delete/1',
  6904. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6905. ),
  6906. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6907. '/form'
  6908. ));
  6909. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  6910. array('inline' => false, 'method' => 'DELETE')
  6911. );
  6912. $this->assertTags($result, array(
  6913. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6914. 'Delete',
  6915. '/a'
  6916. ));
  6917. $result = $this->View->fetch('postLink');
  6918. $this->assertTags($result, array(
  6919. 'form' => array(
  6920. 'method' => 'post', 'action' => '/posts/delete/1',
  6921. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6922. ),
  6923. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6924. '/form',
  6925. array(
  6926. 'form' => array(
  6927. 'method' => 'post', 'action' => '/posts/delete/2',
  6928. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6929. ),
  6930. ),
  6931. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  6932. '/form'
  6933. ));
  6934. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  6935. $this->assertTags($result, array(
  6936. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6937. 'Delete',
  6938. '/a'
  6939. ));
  6940. $result = $this->View->fetch('foobar');
  6941. $this->assertTags($result, array(
  6942. 'form' => array(
  6943. 'method' => 'post', 'action' => '/posts/delete/1',
  6944. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6945. ),
  6946. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6947. '/form'
  6948. ));
  6949. }
  6950. /**
  6951. * testSubmitButton method
  6952. *
  6953. * @return void
  6954. */
  6955. public function testSubmitButton() {
  6956. $result = $this->Form->submit('');
  6957. $expected = array(
  6958. 'div' => array('class' => 'submit'),
  6959. 'input' => array('type' => 'submit', 'value' => ''),
  6960. '/div'
  6961. );
  6962. $this->assertTags($result, $expected);
  6963. $result = $this->Form->submit('Test Submit');
  6964. $expected = array(
  6965. 'div' => array('class' => 'submit'),
  6966. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6967. '/div'
  6968. );
  6969. $this->assertTags($result, $expected);
  6970. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6971. $expected = array(
  6972. 'span' => array('class' => 'submit'),
  6973. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6974. '/span'
  6975. );
  6976. $this->assertTags($result, $expected);
  6977. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6978. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6979. $this->assertTags($result, $expected);
  6980. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6981. $expected = array(
  6982. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6983. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6984. '/div'
  6985. );
  6986. $this->assertTags($result, $expected);
  6987. $result = $this->Form->submit('Next >');
  6988. $expected = array(
  6989. 'div' => array('class' => 'submit'),
  6990. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6991. '/div'
  6992. );
  6993. $this->assertTags($result, $expected);
  6994. $result = $this->Form->submit('Next >', array('escape' => false));
  6995. $expected = array(
  6996. 'div' => array('class' => 'submit'),
  6997. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6998. '/div'
  6999. );
  7000. $this->assertTags($result, $expected);
  7001. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  7002. $expected = array(
  7003. 'div' => array('class' => 'submit'),
  7004. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  7005. '/div'
  7006. );
  7007. $this->assertTags($result, $expected);
  7008. $before = '--before--';
  7009. $after = '--after--';
  7010. $result = $this->Form->submit('Test', array('before' => $before));
  7011. $expected = array(
  7012. 'div' => array('class' => 'submit'),
  7013. '--before--',
  7014. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7015. '/div'
  7016. );
  7017. $this->assertTags($result, $expected);
  7018. $result = $this->Form->submit('Test', array('after' => $after));
  7019. $expected = array(
  7020. 'div' => array('class' => 'submit'),
  7021. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7022. '--after--',
  7023. '/div'
  7024. );
  7025. $this->assertTags($result, $expected);
  7026. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  7027. $expected = array(
  7028. 'div' => array('class' => 'submit'),
  7029. '--before--',
  7030. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7031. '--after--',
  7032. '/div'
  7033. );
  7034. $this->assertTags($result, $expected);
  7035. }
  7036. /**
  7037. * test image submit types.
  7038. *
  7039. * @return void
  7040. */
  7041. public function testSubmitImage() {
  7042. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7043. $expected = array(
  7044. 'div' => array('class' => 'submit'),
  7045. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  7046. '/div'
  7047. );
  7048. $this->assertTags($result, $expected);
  7049. $result = $this->Form->submit('/relative/cake.power.gif');
  7050. $expected = array(
  7051. 'div' => array('class' => 'submit'),
  7052. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  7053. '/div'
  7054. );
  7055. $this->assertTags($result, $expected);
  7056. $result = $this->Form->submit('cake.power.gif');
  7057. $expected = array(
  7058. 'div' => array('class' => 'submit'),
  7059. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7060. '/div'
  7061. );
  7062. $this->assertTags($result, $expected);
  7063. $result = $this->Form->submit('Not.an.image');
  7064. $expected = array(
  7065. 'div' => array('class' => 'submit'),
  7066. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7067. '/div'
  7068. );
  7069. $this->assertTags($result, $expected);
  7070. $after = '--after--';
  7071. $before = '--before--';
  7072. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  7073. $expected = array(
  7074. 'div' => array('class' => 'submit'),
  7075. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7076. '--after--',
  7077. '/div'
  7078. );
  7079. $this->assertTags($result, $expected);
  7080. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  7081. $expected = array(
  7082. 'div' => array('class' => 'submit'),
  7083. '--before--',
  7084. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7085. '/div'
  7086. );
  7087. $this->assertTags($result, $expected);
  7088. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  7089. $expected = array(
  7090. 'div' => array('class' => 'submit'),
  7091. '--before--',
  7092. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7093. '--after--',
  7094. '/div'
  7095. );
  7096. $this->assertTags($result, $expected);
  7097. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  7098. $expected = array(
  7099. 'div' => array('class' => 'submit'),
  7100. '--before--',
  7101. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7102. '--after--',
  7103. '/div'
  7104. );
  7105. $this->assertTags($result, $expected);
  7106. }
  7107. /**
  7108. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7109. * be submitted at a time.
  7110. *
  7111. * @return void
  7112. */
  7113. public function testSubmitUnlockedByDefault() {
  7114. $this->Form->request->params['_csrfToken'] = 'secured';
  7115. $this->Form->submit('Go go');
  7116. $this->Form->submit('Save', array('name' => 'save'));
  7117. $result = $this->Form->unlockField();
  7118. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  7119. }
  7120. /**
  7121. * Test submit image with timestamps.
  7122. *
  7123. * @return void
  7124. */
  7125. public function testSubmitImageTimestamp() {
  7126. Configure::write('Asset.timestamp', 'force');
  7127. $result = $this->Form->submit('cake.power.gif');
  7128. $expected = array(
  7129. 'div' => array('class' => 'submit'),
  7130. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  7131. '/div'
  7132. );
  7133. $this->assertTags($result, $expected);
  7134. }
  7135. /**
  7136. * test the create() method
  7137. *
  7138. * @return void
  7139. */
  7140. public function testCreate() {
  7141. $result = $this->Form->create('Contact');
  7142. $encoding = strtolower(Configure::read('App.encoding'));
  7143. $expected = array(
  7144. 'form' => array(
  7145. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7146. 'accept-charset' => $encoding
  7147. ),
  7148. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  7149. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7150. '/div'
  7151. );
  7152. $this->assertTags($result, $expected);
  7153. $result = $this->Form->create('Contact', array('type' => 'GET'));
  7154. $expected = array('form' => array(
  7155. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7156. 'accept-charset' => $encoding
  7157. ));
  7158. $this->assertTags($result, $expected);
  7159. $result = $this->Form->create('Contact', array('type' => 'get'));
  7160. $expected = array('form' => array(
  7161. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7162. 'accept-charset' => $encoding
  7163. ));
  7164. $this->assertTags($result, $expected);
  7165. $result = $this->Form->create('Contact', array('type' => 'put'));
  7166. $expected = array(
  7167. 'form' => array(
  7168. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7169. 'accept-charset' => $encoding
  7170. ),
  7171. 'div' => array('style' => 'display:none;'),
  7172. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7173. '/div'
  7174. );
  7175. $this->assertTags($result, $expected);
  7176. $result = $this->Form->create('Contact', array('type' => 'file'));
  7177. $expected = array(
  7178. 'form' => array(
  7179. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7180. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7181. ),
  7182. 'div' => array('style' => 'display:none;'),
  7183. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7184. '/div'
  7185. );
  7186. $this->assertTags($result, $expected);
  7187. $this->Form->request->data['Contact']['id'] = 1;
  7188. $this->Form->request->here = '/contacts/edit/1';
  7189. $this->Form->request['action'] = 'edit';
  7190. $result = $this->Form->create('Contact');
  7191. $expected = array(
  7192. 'form' => array(
  7193. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7194. 'accept-charset' => $encoding
  7195. ),
  7196. 'div' => array('style' => 'display:none;'),
  7197. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7198. '/div'
  7199. );
  7200. $this->assertTags($result, $expected);
  7201. $this->Form->request->data['Contact']['id'] = 1;
  7202. $this->Form->request->here = '/contacts/edit/1';
  7203. $this->Form->request['action'] = 'edit';
  7204. $result = $this->Form->create('Contact', array('type' => 'file'));
  7205. $expected = array(
  7206. 'form' => array(
  7207. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7208. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7209. ),
  7210. 'div' => array('style' => 'display:none;'),
  7211. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7212. '/div'
  7213. );
  7214. $this->assertTags($result, $expected);
  7215. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  7216. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  7217. $expected = array(
  7218. 'form' => array(
  7219. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  7220. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  7221. ),
  7222. 'div' => array('style' => 'display:none;'),
  7223. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7224. '/div'
  7225. );
  7226. $this->assertTags($result, $expected);
  7227. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  7228. $expected = array(
  7229. 'form' => array(
  7230. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  7231. 'accept-charset' => $encoding
  7232. ),
  7233. 'div' => array('style' => 'display:none;'),
  7234. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7235. '/div'
  7236. );
  7237. $this->assertTags($result, $expected);
  7238. $this->Form->request['action'] = 'add';
  7239. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  7240. $expected = array(
  7241. 'form' => array(
  7242. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  7243. 'accept-charset' => $encoding
  7244. ),
  7245. 'div' => array('style' => 'display:none;'),
  7246. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7247. '/div'
  7248. );
  7249. $this->assertTags($result, $expected);
  7250. $result = $this->Form->create('User', array('action' => 'login'));
  7251. $expected = array(
  7252. 'form' => array(
  7253. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  7254. 'accept-charset' => $encoding
  7255. ),
  7256. 'div' => array('style' => 'display:none;'),
  7257. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7258. '/div'
  7259. );
  7260. $this->assertTags($result, $expected);
  7261. $result = $this->Form->create('User', array('url' => '/users/login'));
  7262. $expected = array(
  7263. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  7264. 'div' => array('style' => 'display:none;'),
  7265. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7266. '/div'
  7267. );
  7268. $this->assertTags($result, $expected);
  7269. $this->Form->request['controller'] = 'pages';
  7270. $result = $this->Form->create('User', array('action' => 'signup'));
  7271. $expected = array(
  7272. 'form' => array(
  7273. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  7274. 'accept-charset' => $encoding
  7275. ),
  7276. 'div' => array('style' => 'display:none;'),
  7277. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7278. '/div'
  7279. );
  7280. $this->assertTags($result, $expected);
  7281. $this->Form->request->data = array();
  7282. $this->Form->request['controller'] = 'contacts';
  7283. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7284. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  7285. $expected = array(
  7286. 'form' => array(
  7287. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  7288. 'accept-charset' => 'utf-8'
  7289. ),
  7290. 'div' => array('style' => 'display:none;'),
  7291. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7292. '/div'
  7293. );
  7294. $this->assertTags($result, $expected);
  7295. }
  7296. /**
  7297. * Test the onsubmit option for create()
  7298. *
  7299. * @return void
  7300. */
  7301. public function testCreateOnSubmit() {
  7302. $this->Form->request->data = array();
  7303. $this->Form->request['controller'] = 'contacts';
  7304. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7305. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  7306. $expected = array(
  7307. 'form' => array(
  7308. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  7309. 'accept-charset' => 'utf-8'
  7310. ),
  7311. 'div' => array('style' => 'display:none;'),
  7312. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7313. '/div'
  7314. );
  7315. $this->assertTags($result, $expected);
  7316. $this->Form->request->data = array();
  7317. $this->Form->request['controller'] = 'contacts';
  7318. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7319. $result = $this->Form->create(array(
  7320. 'url' => array('action' => 'index', 'param'),
  7321. 'default' => false,
  7322. 'onsubmit' => 'someFunction();'
  7323. ));
  7324. $expected = array(
  7325. 'form' => array(
  7326. 'id' => 'ContactAddForm', 'method' => 'post',
  7327. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  7328. 'action' => '/contacts/index/param',
  7329. 'accept-charset' => 'utf-8'
  7330. ),
  7331. 'div' => array('style' => 'display:none;'),
  7332. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7333. '/div'
  7334. );
  7335. $this->assertTags($result, $expected);
  7336. }
  7337. /**
  7338. * test create() with automatic url generation
  7339. *
  7340. * @return void
  7341. */
  7342. public function testCreateAutoUrl() {
  7343. $this->Form->request['action'] = 'delete';
  7344. $this->Form->request->here = '/contacts/delete/10';
  7345. $this->Form->request->base = '';
  7346. $result = $this->Form->create('Contact');
  7347. $expected = array(
  7348. 'form' => array(
  7349. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/contacts/delete/10',
  7350. 'accept-charset' => 'utf-8'
  7351. ),
  7352. 'div' => array('style' => 'display:none;'),
  7353. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7354. '/div'
  7355. );
  7356. $this->assertTags($result, $expected);
  7357. }
  7358. /**
  7359. * test create() with a custom route
  7360. *
  7361. * @return void
  7362. */
  7363. public function testCreateCustomRoute() {
  7364. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  7365. $encoding = strtolower(Configure::read('App.encoding'));
  7366. $result = $this->Form->create('User', array('action' => 'login'));
  7367. $expected = array(
  7368. 'form' => array(
  7369. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  7370. 'accept-charset' => $encoding
  7371. ),
  7372. 'div' => array('style' => 'display:none;'),
  7373. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7374. '/div'
  7375. );
  7376. $this->assertTags($result, $expected);
  7377. }
  7378. /**
  7379. * test that inputDefaults are stored and used.
  7380. *
  7381. * @return void
  7382. */
  7383. public function testCreateWithInputDefaults() {
  7384. $this->Form->create('User', array(
  7385. 'inputDefaults' => array(
  7386. 'div' => false,
  7387. 'label' => false,
  7388. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  7389. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  7390. )
  7391. ));
  7392. $result = $this->Form->input('username');
  7393. $expected = array(
  7394. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername')
  7395. );
  7396. $this->assertTags($result, $expected);
  7397. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  7398. $expected = array(
  7399. 'div' => array('class' => 'input text'),
  7400. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  7401. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  7402. '/div'
  7403. );
  7404. $this->assertTags($result, $expected);
  7405. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7406. $expected = array(
  7407. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  7408. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7409. );
  7410. $this->assertTags($result, $expected);
  7411. $this->Form->create('User', array(
  7412. 'inputDefaults' => array(
  7413. 'div' => false,
  7414. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7415. )
  7416. ));
  7417. $result = $this->Form->input('username', array('div' => true));
  7418. $expected = array(
  7419. 'div' => array('class' => 'input text'),
  7420. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7421. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  7422. '/div'
  7423. );
  7424. $this->assertTags($result, $expected);
  7425. }
  7426. /**
  7427. * test automatic accept-charset overriding
  7428. *
  7429. * @return void
  7430. */
  7431. public function testCreateWithAcceptCharset() {
  7432. $result = $this->Form->create('UserForm', array(
  7433. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7434. )
  7435. );
  7436. $expected = array(
  7437. 'form' => array(
  7438. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7439. 'accept-charset' => 'iso-8859-1'
  7440. ),
  7441. 'div' => array('style' => 'display:none;'),
  7442. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7443. '/div'
  7444. );
  7445. $this->assertTags($result, $expected);
  7446. }
  7447. /**
  7448. * Test base form URL when url param is passed with multiple parameters (&)
  7449. *
  7450. */
  7451. public function testCreateQuerystringrequest() {
  7452. $encoding = strtolower(Configure::read('App.encoding'));
  7453. $result = $this->Form->create('Contact', array(
  7454. 'type' => 'post',
  7455. 'escape' => false,
  7456. 'url' => array(
  7457. 'controller' => 'controller',
  7458. 'action' => 'action',
  7459. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7460. )
  7461. ));
  7462. $expected = array(
  7463. 'form' => array(
  7464. 'id' => 'ContactAddForm',
  7465. 'method' => 'post',
  7466. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7467. 'accept-charset' => $encoding
  7468. ),
  7469. 'div' => array('style' => 'display:none;'),
  7470. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7471. '/div'
  7472. );
  7473. $this->assertTags($result, $expected);
  7474. $result = $this->Form->create('Contact', array(
  7475. 'type' => 'post',
  7476. 'url' => array(
  7477. 'controller' => 'controller',
  7478. 'action' => 'action',
  7479. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7480. )
  7481. ));
  7482. $expected = array(
  7483. 'form' => array(
  7484. 'id' => 'ContactAddForm',
  7485. 'method' => 'post',
  7486. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7487. 'accept-charset' => $encoding
  7488. ),
  7489. 'div' => array('style' => 'display:none;'),
  7490. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7491. '/div'
  7492. );
  7493. $this->assertTags($result, $expected);
  7494. }
  7495. /**
  7496. * test that create() doesn't cause errors by multiple id's being in the primary key
  7497. * as could happen with multiple select or checkboxes.
  7498. *
  7499. * @return void
  7500. */
  7501. public function testCreateWithMultipleIdInData() {
  7502. $encoding = strtolower(Configure::read('App.encoding'));
  7503. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7504. $result = $this->Form->create('Contact');
  7505. $expected = array(
  7506. 'form' => array(
  7507. 'id' => 'ContactAddForm',
  7508. 'method' => 'post',
  7509. 'action' => '/contacts/add',
  7510. 'accept-charset' => $encoding
  7511. ),
  7512. 'div' => array('style' => 'display:none;'),
  7513. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7514. '/div'
  7515. );
  7516. $this->assertTags($result, $expected);
  7517. }
  7518. /**
  7519. * test that create() doesn't add in extra passed params.
  7520. *
  7521. * @return void
  7522. */
  7523. public function testCreatePassedArgs() {
  7524. $encoding = strtolower(Configure::read('App.encoding'));
  7525. $this->Form->request->data['Contact']['id'] = 1;
  7526. $result = $this->Form->create('Contact', array(
  7527. 'type' => 'post',
  7528. 'escape' => false,
  7529. 'url' => array(
  7530. 'action' => 'edit',
  7531. 'myparam'
  7532. )
  7533. ));
  7534. $expected = array(
  7535. 'form' => array(
  7536. 'id' => 'ContactAddForm',
  7537. 'method' => 'post',
  7538. 'action' => '/contacts/edit/myparam',
  7539. 'accept-charset' => $encoding
  7540. ),
  7541. 'div' => array('style' => 'display:none;'),
  7542. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7543. '/div'
  7544. );
  7545. $this->assertTags($result, $expected);
  7546. }
  7547. /**
  7548. * test creating a get form, and get form inputs.
  7549. *
  7550. * @return void
  7551. */
  7552. public function testGetFormCreate() {
  7553. $encoding = strtolower(Configure::read('App.encoding'));
  7554. $result = $this->Form->create('Contact', array('type' => 'get'));
  7555. $this->assertTags($result, array('form' => array(
  7556. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7557. 'accept-charset' => $encoding
  7558. )));
  7559. $result = $this->Form->text('Contact.name');
  7560. $this->assertTags($result, array('input' => array(
  7561. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7562. )));
  7563. $result = $this->Form->password('password');
  7564. $this->assertTags($result, array('input' => array(
  7565. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7566. )));
  7567. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7568. $result = $this->Form->text('user_form');
  7569. $this->assertTags($result, array('input' => array(
  7570. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7571. )));
  7572. }
  7573. /**
  7574. * test get form, and inputs when the model param is false
  7575. *
  7576. * @return void
  7577. */
  7578. public function testGetFormWithFalseModel() {
  7579. $encoding = strtolower(Configure::read('App.encoding'));
  7580. $this->Form->request['controller'] = 'contact_test';
  7581. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7582. $expected = array('form' => array(
  7583. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7584. 'accept-charset' => $encoding
  7585. ));
  7586. $this->assertTags($result, $expected);
  7587. $result = $this->Form->text('reason');
  7588. $expected = array(
  7589. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7590. );
  7591. $this->assertTags($result, $expected);
  7592. }
  7593. /**
  7594. * test that datetime() works with GET style forms.
  7595. *
  7596. * @return void
  7597. */
  7598. public function testDateTimeWithGetForms() {
  7599. extract($this->dateRegex);
  7600. $this->Form->create('Contact', array('type' => 'get'));
  7601. $result = $this->Form->datetime('created');
  7602. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7603. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7604. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7605. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7606. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7607. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7608. }
  7609. /**
  7610. * testEditFormWithData method
  7611. *
  7612. * test auto populating form elements from submitted data.
  7613. *
  7614. * @return void
  7615. */
  7616. public function testEditFormWithData() {
  7617. $this->Form->request->data = array('Person' => array(
  7618. 'id' => 1,
  7619. 'first_name' => 'Nate',
  7620. 'last_name' => 'Abele',
  7621. 'email' => 'nate@example.com'
  7622. ));
  7623. $this->Form->request->addParams(array(
  7624. 'models' => array('Person'),
  7625. 'controller' => 'people',
  7626. 'action' => 'add'
  7627. ));
  7628. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7629. $this->Form->create();
  7630. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7631. $expected = array(
  7632. 'input' => array('type' => 'hidden', 'name' => 'People[People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7633. 'select' => array(
  7634. 'name' => 'People[People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7635. ),
  7636. array('option' => array('value' => 1)), 'Nate', '/option',
  7637. array('option' => array('value' => 2)), 'Garrett', '/option',
  7638. array('option' => array('value' => 3)), 'Larry', '/option',
  7639. '/select'
  7640. );
  7641. $this->assertTags($result, $expected);
  7642. }
  7643. /**
  7644. * Test that required fields are created for various types of validation.
  7645. *
  7646. * @return void
  7647. */
  7648. public function testFormInputRequiredDetection() {
  7649. $this->Form->create('Contact');
  7650. $result = $this->Form->input('Contact.non_existing');
  7651. $expected = array(
  7652. 'div' => array('class' => 'input text'),
  7653. 'label' => array('for' => 'ContactNonExisting'),
  7654. 'Non Existing',
  7655. '/label',
  7656. 'input' => array(
  7657. 'type' => 'text', 'name' => 'Contact[non_existing]',
  7658. 'id' => 'ContactNonExisting'
  7659. ),
  7660. '/div'
  7661. );
  7662. $this->assertTags($result, $expected);
  7663. $result = $this->Form->input('Contact.imrequired');
  7664. $expected = array(
  7665. 'div' => array('class' => 'input text required'),
  7666. 'label' => array('for' => 'ContactImrequired'),
  7667. 'Imrequired',
  7668. '/label',
  7669. 'input' => array(
  7670. 'type' => 'text', 'name' => 'Contact[imrequired]',
  7671. 'id' => 'ContactImrequired',
  7672. 'required' => 'required'
  7673. ),
  7674. '/div'
  7675. );
  7676. $this->assertTags($result, $expected);
  7677. $result = $this->Form->input('Contact.imalsorequired');
  7678. $expected = array(
  7679. 'div' => array('class' => 'input text required'),
  7680. 'label' => array('for' => 'ContactImalsorequired'),
  7681. 'Imalsorequired',
  7682. '/label',
  7683. 'input' => array(
  7684. 'type' => 'text', 'name' => 'Contact[imalsorequired]',
  7685. 'id' => 'ContactImalsorequired',
  7686. 'required' => 'required'
  7687. ),
  7688. '/div'
  7689. );
  7690. $this->assertTags($result, $expected);
  7691. $result = $this->Form->input('Contact.imrequiredtoo');
  7692. $expected = array(
  7693. 'div' => array('class' => 'input text required'),
  7694. 'label' => array('for' => 'ContactImrequiredtoo'),
  7695. 'Imrequiredtoo',
  7696. '/label',
  7697. 'input' => array(
  7698. 'type' => 'text', 'name' => 'Contact[imrequiredtoo]',
  7699. 'id' => 'ContactImrequiredtoo',
  7700. 'required' => 'required'
  7701. ),
  7702. '/div'
  7703. );
  7704. $this->assertTags($result, $expected);
  7705. $result = $this->Form->input('Contact.required_one');
  7706. $expected = array(
  7707. 'div' => array('class' => 'input text required'),
  7708. 'label' => array('for' => 'ContactRequiredOne'),
  7709. 'Required One',
  7710. '/label',
  7711. 'input' => array(
  7712. 'type' => 'text', 'name' => 'Contact[required_one]',
  7713. 'id' => 'ContactRequiredOne',
  7714. 'required' => 'required'
  7715. ),
  7716. '/div'
  7717. );
  7718. $this->assertTags($result, $expected);
  7719. $result = $this->Form->input('Contact.string_required');
  7720. $expected = array(
  7721. 'div' => array('class' => 'input text required'),
  7722. 'label' => array('for' => 'ContactStringRequired'),
  7723. 'String Required',
  7724. '/label',
  7725. 'input' => array(
  7726. 'type' => 'text', 'name' => 'Contact[string_required]',
  7727. 'id' => 'ContactStringRequired',
  7728. 'required' => 'required'
  7729. ),
  7730. '/div'
  7731. );
  7732. $this->assertTags($result, $expected);
  7733. $result = $this->Form->input('Contact.imnotrequired');
  7734. $expected = array(
  7735. 'div' => array('class' => 'input text'),
  7736. 'label' => array('for' => 'ContactImnotrequired'),
  7737. 'Imnotrequired',
  7738. '/label',
  7739. 'input' => array(
  7740. 'type' => 'text', 'name' => 'Contact[imnotrequired]',
  7741. 'id' => 'ContactImnotrequired'
  7742. ),
  7743. '/div'
  7744. );
  7745. $this->assertTags($result, $expected);
  7746. $result = $this->Form->input('Contact.imalsonotrequired');
  7747. $expected = array(
  7748. 'div' => array('class' => 'input text'),
  7749. 'label' => array('for' => 'ContactImalsonotrequired'),
  7750. 'Imalsonotrequired',
  7751. '/label',
  7752. 'input' => array(
  7753. 'type' => 'text', 'name' => 'Contact[imalsonotrequired]',
  7754. 'id' => 'ContactImalsonotrequired'
  7755. ),
  7756. '/div'
  7757. );
  7758. $this->assertTags($result, $expected);
  7759. $result = $this->Form->input('Contact.imalsonotrequired2');
  7760. $expected = array(
  7761. 'div' => array('class' => 'input text'),
  7762. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7763. 'Imalsonotrequired2',
  7764. '/label',
  7765. 'input' => array(
  7766. 'type' => 'text', 'name' => 'Contact[imalsonotrequired2]',
  7767. 'id' => 'ContactImalsonotrequired2'
  7768. ),
  7769. '/div'
  7770. );
  7771. $this->assertTags($result, $expected);
  7772. $result = $this->Form->input('Contact.imnotrequiredeither');
  7773. $expected = array(
  7774. 'div' => array('class' => 'input text'),
  7775. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7776. 'Imnotrequiredeither',
  7777. '/label',
  7778. 'input' => array(
  7779. 'type' => 'text', 'name' => 'Contact[imnotrequiredeither]',
  7780. 'id' => 'ContactImnotrequiredeither'
  7781. ),
  7782. '/div'
  7783. );
  7784. $this->assertTags($result, $expected);
  7785. $result = $this->Form->input('Contact.iamrequiredalways');
  7786. $expected = array(
  7787. 'div' => array('class' => 'input text required'),
  7788. 'label' => array('for' => 'ContactIamrequiredalways'),
  7789. 'Iamrequiredalways',
  7790. '/label',
  7791. 'input' => array(
  7792. 'type' => 'text', 'name' => 'Contact[iamrequiredalways]',
  7793. 'id' => 'ContactIamrequiredalways',
  7794. 'required' => 'required'
  7795. ),
  7796. '/div'
  7797. );
  7798. $this->assertTags($result, $expected);
  7799. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7800. $expected = array(
  7801. 'div' => array('class' => 'input checkbox required'),
  7802. array('input' => array(
  7803. 'type' => 'hidden',
  7804. 'name' => 'Contact[boolean_field]',
  7805. 'id' => 'ContactBooleanField_',
  7806. 'value' => '0'
  7807. )),
  7808. array('input' => array(
  7809. 'type' => 'checkbox',
  7810. 'name' => 'Contact[boolean_field]',
  7811. 'value' => '1',
  7812. 'id' => 'ContactBooleanField'
  7813. )),
  7814. 'label' => array('for' => 'ContactBooleanField'),
  7815. 'Boolean Field',
  7816. '/label',
  7817. '/div'
  7818. );
  7819. $this->assertTags($result, $expected);
  7820. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7821. $expected = array(
  7822. 'div' => array('class' => 'input checkbox required'),
  7823. array('input' => array(
  7824. 'type' => 'hidden',
  7825. 'name' => 'Contact[boolean_field]',
  7826. 'id' => 'ContactBooleanField_',
  7827. 'value' => '0'
  7828. )),
  7829. array('input' => array(
  7830. 'type' => 'checkbox',
  7831. 'name' => 'Contact[boolean_field]',
  7832. 'value' => '1',
  7833. 'id' => 'ContactBooleanField',
  7834. 'required' => 'required'
  7835. )),
  7836. 'label' => array('for' => 'ContactBooleanField'),
  7837. 'Boolean Field',
  7838. '/label',
  7839. '/div'
  7840. );
  7841. $this->assertTags($result, $expected);
  7842. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  7843. $expected = array(
  7844. 'div' => array('class' => 'input file required'),
  7845. 'label' => array('for' => 'ContactIamrequiredalways'),
  7846. 'Iamrequiredalways',
  7847. '/label',
  7848. 'input' => array(
  7849. 'type' => 'file',
  7850. 'name' => 'data[Contact][iamrequiredalways]',
  7851. 'id' => 'ContactIamrequiredalways',
  7852. 'required' => 'required'
  7853. ),
  7854. '/div'
  7855. );
  7856. $this->assertTags($result, $expected);
  7857. }
  7858. /**
  7859. * Test that required fields are created when only using ModelValidator::add().
  7860. *
  7861. * @return void
  7862. */
  7863. public function testFormInputRequiredDetectionModelValidator() {
  7864. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  7865. $this->Form->create('ContactTag');
  7866. $result = $this->Form->input('ContactTag.iwillberequired');
  7867. $expected = array(
  7868. 'div' => array('class' => 'input text required'),
  7869. 'label' => array('for' => 'ContactTagIwillberequired'),
  7870. 'Iwillberequired',
  7871. '/label',
  7872. 'input' => array(
  7873. 'name' => 'data[ContactTag][iwillberequired]',
  7874. 'type' => 'text',
  7875. 'id' => 'ContactTagIwillberequired',
  7876. 'required' => 'required'
  7877. ),
  7878. '/div'
  7879. );
  7880. $this->assertTags($result, $expected);
  7881. }
  7882. /**
  7883. * testFormMagicInput method
  7884. *
  7885. * @return void
  7886. */
  7887. public function testFormMagicInput() {
  7888. $encoding = strtolower(Configure::read('App.encoding'));
  7889. $result = $this->Form->create('Contact');
  7890. $expected = array(
  7891. 'form' => array(
  7892. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7893. 'accept-charset' => $encoding
  7894. ),
  7895. 'div' => array('style' => 'display:none;'),
  7896. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7897. '/div'
  7898. );
  7899. $this->assertTags($result, $expected);
  7900. $result = $this->Form->input('name');
  7901. $expected = array(
  7902. 'div' => array('class' => 'input text'),
  7903. 'label' => array('for' => 'ContactName'),
  7904. 'Name',
  7905. '/label',
  7906. 'input' => array(
  7907. 'type' => 'text', 'name' => 'Contact[name]',
  7908. 'id' => 'ContactName', 'maxlength' => '255'
  7909. ),
  7910. '/div'
  7911. );
  7912. $this->assertTags($result, $expected);
  7913. $result = $this->Form->input('non_existing_field_in_contact_model');
  7914. $expected = array(
  7915. 'div' => array('class' => 'input text'),
  7916. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7917. 'Non Existing Field In Contact Model',
  7918. '/label',
  7919. 'input' => array(
  7920. 'type' => 'text', 'name' => 'Contact[non_existing_field_in_contact_model]',
  7921. 'id' => 'ContactNonExistingFieldInContactModel'
  7922. ),
  7923. '/div'
  7924. );
  7925. $this->assertTags($result, $expected);
  7926. $result = $this->Form->input('Address.street');
  7927. $expected = array(
  7928. 'div' => array('class' => 'input text'),
  7929. 'label' => array('for' => 'AddressStreet'),
  7930. 'Street',
  7931. '/label',
  7932. 'input' => array(
  7933. 'type' => 'text', 'name' => 'Address[street]',
  7934. 'id' => 'AddressStreet'
  7935. ),
  7936. '/div'
  7937. );
  7938. $this->assertTags($result, $expected);
  7939. $result = $this->Form->input('Address.non_existing_field_in_model');
  7940. $expected = array(
  7941. 'div' => array('class' => 'input text'),
  7942. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7943. 'Non Existing Field In Model',
  7944. '/label',
  7945. 'input' => array(
  7946. 'type' => 'text', 'name' => 'Address[non_existing_field_in_model]',
  7947. 'id' => 'AddressNonExistingFieldInModel'
  7948. ),
  7949. '/div'
  7950. );
  7951. $this->assertTags($result, $expected);
  7952. $result = $this->Form->input('name', array('div' => false));
  7953. $expected = array(
  7954. 'label' => array('for' => 'ContactName'),
  7955. 'Name',
  7956. '/label',
  7957. 'input' => array(
  7958. 'type' => 'text', 'name' => 'Contact[name]',
  7959. 'id' => 'ContactName', 'maxlength' => '255'
  7960. )
  7961. );
  7962. $this->assertTags($result, $expected);
  7963. extract($this->dateRegex);
  7964. $now = strtotime('now');
  7965. $result = $this->Form->input('Contact.published', array('div' => false));
  7966. $expected = array(
  7967. 'label' => array('for' => 'ContactPublishedMonth'),
  7968. 'Published',
  7969. '/label',
  7970. array('select' => array(
  7971. 'name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth'
  7972. )),
  7973. $monthsRegex,
  7974. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7975. date('F', $now),
  7976. '/option',
  7977. '*/select',
  7978. '-',
  7979. array('select' => array(
  7980. 'name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay'
  7981. )),
  7982. $daysRegex,
  7983. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7984. date('j', $now),
  7985. '/option',
  7986. '*/select',
  7987. '-',
  7988. array('select' => array(
  7989. 'name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear'
  7990. )),
  7991. $yearsRegex,
  7992. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7993. date('Y', $now),
  7994. '*/select'
  7995. );
  7996. $this->assertTags($result, $expected);
  7997. $result = $this->Form->input('Contact.updated', array('div' => false));
  7998. $expected = array(
  7999. 'label' => array('for' => 'ContactUpdatedMonth'),
  8000. 'Updated',
  8001. '/label',
  8002. array('select' => array(
  8003. 'name' => 'Contact[updated][month]', 'id' => 'ContactUpdatedMonth'
  8004. )),
  8005. $monthsRegex,
  8006. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  8007. date('F', $now),
  8008. '/option',
  8009. '*/select',
  8010. '-',
  8011. array('select' => array(
  8012. 'name' => 'Contact[updated][day]', 'id' => 'ContactUpdatedDay'
  8013. )),
  8014. $daysRegex,
  8015. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  8016. date('j', $now),
  8017. '/option',
  8018. '*/select',
  8019. '-',
  8020. array('select' => array(
  8021. 'name' => 'Contact[updated][year]', 'id' => 'ContactUpdatedYear'
  8022. )),
  8023. $yearsRegex,
  8024. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  8025. date('Y', $now),
  8026. '*/select'
  8027. );
  8028. $this->assertTags($result, $expected);
  8029. $result = $this->Form->input('UserForm.stuff');
  8030. $expected = array(
  8031. 'div' => array('class' => 'input text'),
  8032. 'label' => array('for' => 'UserFormStuff'),
  8033. 'Stuff',
  8034. '/label',
  8035. 'input' => array(
  8036. 'type' => 'text', 'name' => 'UserForm[stuff]',
  8037. 'id' => 'UserFormStuff', 'maxlength' => 10
  8038. ),
  8039. '/div'
  8040. );
  8041. $this->assertTags($result, $expected);
  8042. }
  8043. /**
  8044. * testForMagicInputNonExistingNorValidated method
  8045. *
  8046. * @return void
  8047. */
  8048. public function testForMagicInputNonExistingNorValidated() {
  8049. $encoding = strtolower(Configure::read('App.encoding'));
  8050. $result = $this->Form->create('Contact');
  8051. $expected = array(
  8052. 'form' => array(
  8053. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8054. 'accept-charset' => $encoding
  8055. ),
  8056. 'div' => array('style' => 'display:none;'),
  8057. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8058. '/div'
  8059. );
  8060. $this->assertTags($result, $expected);
  8061. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8062. $expected = array(
  8063. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8064. 'Non Existing Nor Validated',
  8065. '/label',
  8066. 'input' => array(
  8067. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  8068. 'id' => 'ContactNonExistingNorValidated'
  8069. )
  8070. );
  8071. $this->assertTags($result, $expected);
  8072. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  8073. 'div' => false, 'value' => 'my value'
  8074. ));
  8075. $expected = array(
  8076. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8077. 'Non Existing Nor Validated',
  8078. '/label',
  8079. 'input' => array(
  8080. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  8081. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  8082. )
  8083. );
  8084. $this->assertTags($result, $expected);
  8085. $this->Form->request->data = array(
  8086. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  8087. ));
  8088. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8089. $expected = array(
  8090. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8091. 'Non Existing Nor Validated',
  8092. '/label',
  8093. 'input' => array(
  8094. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  8095. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  8096. )
  8097. );
  8098. $this->assertTags($result, $expected);
  8099. }
  8100. /**
  8101. * testFormMagicInputLabel method
  8102. *
  8103. * @return void
  8104. */
  8105. public function testFormMagicInputLabel() {
  8106. $encoding = strtolower(Configure::read('App.encoding'));
  8107. $result = $this->Form->create('Contact');
  8108. $expected = array(
  8109. 'form' => array(
  8110. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8111. 'accept-charset' => $encoding
  8112. ),
  8113. 'div' => array('style' => 'display:none;'),
  8114. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8115. '/div'
  8116. );
  8117. $this->assertTags($result, $expected);
  8118. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  8119. $this->assertTags($result, array('input' => array(
  8120. 'name' => 'Contact[name]', 'type' => 'text',
  8121. 'id' => 'ContactName', 'maxlength' => '255')
  8122. ));
  8123. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  8124. $expected = array(
  8125. 'label' => array('for' => 'ContactName'),
  8126. 'My label',
  8127. '/label',
  8128. 'input' => array(
  8129. 'type' => 'text', 'name' => 'Contact[name]',
  8130. 'id' => 'ContactName', 'maxlength' => '255'
  8131. )
  8132. );
  8133. $this->assertTags($result, $expected);
  8134. $result = $this->Form->input('Contact.name', array(
  8135. 'div' => false, 'label' => array('class' => 'mandatory')
  8136. ));
  8137. $expected = array(
  8138. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8139. 'Name',
  8140. '/label',
  8141. 'input' => array(
  8142. 'type' => 'text', 'name' => 'Contact[name]',
  8143. 'id' => 'ContactName', 'maxlength' => '255'
  8144. )
  8145. );
  8146. $this->assertTags($result, $expected);
  8147. $result = $this->Form->input('Contact.name', array(
  8148. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  8149. ));
  8150. $expected = array(
  8151. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8152. 'My label',
  8153. '/label',
  8154. 'input' => array(
  8155. 'type' => 'text', 'name' => 'Contact[name]',
  8156. 'id' => 'ContactName', 'maxlength' => '255'
  8157. )
  8158. );
  8159. $this->assertTags($result, $expected);
  8160. $result = $this->Form->input('Contact.name', array(
  8161. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  8162. ));
  8163. $expected = array(
  8164. 'label' => array('for' => 'my_id'),
  8165. 'Name',
  8166. '/label',
  8167. 'input' => array(
  8168. 'type' => 'text', 'name' => 'Contact[name]',
  8169. 'id' => 'my_id', 'maxlength' => '255'
  8170. )
  8171. );
  8172. $this->assertTags($result, $expected);
  8173. $result = $this->Form->input('1.id');
  8174. $this->assertTags($result, array('input' => array(
  8175. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  8176. 'id' => 'Contact1Id'
  8177. )));
  8178. $result = $this->Form->input("1.name");
  8179. $expected = array(
  8180. 'div' => array('class' => 'input text'),
  8181. 'label' => array('for' => 'Contact1Name'),
  8182. 'Name',
  8183. '/label',
  8184. 'input' => array(
  8185. 'type' => 'text', 'name' => 'Contact[1][name]',
  8186. 'id' => 'Contact1Name', 'maxlength' => '255'
  8187. ),
  8188. '/div'
  8189. );
  8190. $this->assertTags($result, $expected);
  8191. $result = $this->Form->input('Contact.1.id');
  8192. $this->assertTags($result, array(
  8193. 'input' => array(
  8194. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  8195. 'id' => 'Contact1Id'
  8196. )
  8197. ));
  8198. $result = $this->Form->input("Model.1.name");
  8199. $expected = array(
  8200. 'div' => array('class' => 'input text'),
  8201. 'label' => array('for' => 'Model1Name'),
  8202. 'Name',
  8203. '/label',
  8204. 'input' => array(
  8205. 'type' => 'text', 'name' => 'Model[1][name]',
  8206. 'id' => 'Model1Name'
  8207. ),
  8208. '/div'
  8209. );
  8210. $this->assertTags($result, $expected);
  8211. }
  8212. /**
  8213. * testFormEnd method
  8214. *
  8215. * @return void
  8216. */
  8217. public function testFormEnd() {
  8218. $this->assertEquals('</form>', $this->Form->end());
  8219. $result = $this->Form->end('');
  8220. $expected = array(
  8221. 'div' => array('class' => 'submit'),
  8222. 'input' => array('type' => 'submit', 'value' => ''),
  8223. '/div',
  8224. '/form'
  8225. );
  8226. $this->assertTags($result, $expected);
  8227. $result = $this->Form->end(array('label' => ''));
  8228. $expected = array(
  8229. 'div' => array('class' => 'submit'),
  8230. 'input' => array('type' => 'submit', 'value' => ''),
  8231. '/div',
  8232. '/form'
  8233. );
  8234. $this->assertTags($result, $expected);
  8235. $result = $this->Form->end('save');
  8236. $expected = array(
  8237. 'div' => array('class' => 'submit'),
  8238. 'input' => array('type' => 'submit', 'value' => 'save'),
  8239. '/div',
  8240. '/form'
  8241. );
  8242. $this->assertTags($result, $expected);
  8243. $result = $this->Form->end(array('label' => 'save'));
  8244. $expected = array(
  8245. 'div' => array('class' => 'submit'),
  8246. 'input' => array('type' => 'submit', 'value' => 'save'),
  8247. '/div',
  8248. '/form'
  8249. );
  8250. $this->assertTags($result, $expected);
  8251. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  8252. $expected = array(
  8253. 'div' => array('class' => 'submit'),
  8254. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8255. '/div',
  8256. '/form'
  8257. );
  8258. $this->assertTags($result, $expected);
  8259. $result = $this->Form->end(array('name' => 'Whatever'));
  8260. $expected = array(
  8261. 'div' => array('class' => 'submit'),
  8262. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  8263. '/div',
  8264. '/form'
  8265. );
  8266. $this->assertTags($result, $expected);
  8267. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  8268. $expected = array(
  8269. 'div' => array('class' => 'good'),
  8270. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8271. '/div',
  8272. '/form'
  8273. );
  8274. $this->assertTags($result, $expected);
  8275. $result = $this->Form->end(array(
  8276. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  8277. ));
  8278. $expected = array(
  8279. 'div' => array('class' => 'good'),
  8280. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8281. '/div',
  8282. '/form'
  8283. );
  8284. $this->assertTags($result, $expected);
  8285. }
  8286. /**
  8287. * testMultipleFormWithIdFields method
  8288. *
  8289. * @return void
  8290. */
  8291. public function testMultipleFormWithIdFields() {
  8292. $this->Form->create('UserForm');
  8293. $result = $this->Form->input('id');
  8294. $this->assertTags($result, array('input' => array(
  8295. 'type' => 'hidden', 'name' => 'UserForm[id]', 'id' => 'UserFormId'
  8296. )));
  8297. $result = $this->Form->input('ValidateItem.id');
  8298. $this->assertTags($result, array('input' => array(
  8299. 'type' => 'hidden', 'name' => 'ValidateItem[id]',
  8300. 'id' => 'ValidateItemId'
  8301. )));
  8302. $result = $this->Form->input('ValidateUser.id');
  8303. $this->assertTags($result, array('input' => array(
  8304. 'type' => 'hidden', 'name' => 'ValidateUser[id]',
  8305. 'id' => 'ValidateUserId'
  8306. )));
  8307. }
  8308. /**
  8309. * testDbLessModel method
  8310. *
  8311. * @return void
  8312. */
  8313. public function testDbLessModel() {
  8314. $this->Form->create('TestMail');
  8315. $result = $this->Form->input('name');
  8316. $expected = array(
  8317. 'div' => array('class' => 'input text'),
  8318. 'label' => array('for' => 'TestMailName'),
  8319. 'Name',
  8320. '/label',
  8321. 'input' => array(
  8322. 'name' => 'TestMail[name]', 'type' => 'text',
  8323. 'id' => 'TestMailName'
  8324. ),
  8325. '/div'
  8326. );
  8327. $this->assertTags($result, $expected);
  8328. ClassRegistry::init('TestMail');
  8329. $this->Form->create('TestMail');
  8330. $result = $this->Form->input('name');
  8331. $expected = array(
  8332. 'div' => array('class' => 'input text'),
  8333. 'label' => array('for' => 'TestMailName'),
  8334. 'Name',
  8335. '/label',
  8336. 'input' => array(
  8337. 'name' => 'TestMail[name]', 'type' => 'text',
  8338. 'id' => 'TestMailName'
  8339. ),
  8340. '/div'
  8341. );
  8342. $this->assertTags($result, $expected);
  8343. }
  8344. /**
  8345. * testBrokenness method
  8346. *
  8347. * @return void
  8348. */
  8349. public function testBrokenness() {
  8350. /*
  8351. * #4 This test has two parents and four children. By default (as of r7117) both
  8352. * parents are show but the first parent is missing a child. This is the inconsistency
  8353. * in the default behaviour - one parent has all children, the other does not - dependent
  8354. * on the data values.
  8355. */
  8356. $result = $this->Form->select('Model.field', array(
  8357. 'Fred' => array(
  8358. 'freds_son_1' => 'Fred',
  8359. 'freds_son_2' => 'Freddie'
  8360. ),
  8361. 'Bert' => array(
  8362. 'berts_son_1' => 'Albert',
  8363. 'berts_son_2' => 'Bertie')
  8364. ),
  8365. array('showParents' => true, 'empty' => false)
  8366. );
  8367. $expected = array(
  8368. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  8369. array('optgroup' => array('label' => 'Fred')),
  8370. array('option' => array('value' => 'freds_son_1')),
  8371. 'Fred',
  8372. '/option',
  8373. array('option' => array('value' => 'freds_son_2')),
  8374. 'Freddie',
  8375. '/option',
  8376. '/optgroup',
  8377. array('optgroup' => array('label' => 'Bert')),
  8378. array('option' => array('value' => 'berts_son_1')),
  8379. 'Albert',
  8380. '/option',
  8381. array('option' => array('value' => 'berts_son_2')),
  8382. 'Bertie',
  8383. '/option',
  8384. '/optgroup',
  8385. '/select'
  8386. );
  8387. $this->assertTags($result, $expected);
  8388. /*
  8389. * #2 This is structurally identical to the test above (#1) - only the parent name has
  8390. * changed, so we should expect the same select list data, just with a different name
  8391. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  8392. * This is where data corruption can occur, because when a select value is missing from
  8393. * a list a form will substitute the first value in the list - without the user knowing.
  8394. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  8395. * affect the availability of 3 => 'Three' as a valid option.
  8396. */
  8397. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  8398. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  8399. ));
  8400. $result = $this->Form->select(
  8401. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  8402. );
  8403. $expected = array(
  8404. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  8405. array('option' => array('value' => 1)),
  8406. 'One',
  8407. '/option',
  8408. array('option' => array('value' => 2)),
  8409. 'Two',
  8410. '/option',
  8411. array('optgroup' => array('label' => 'Three')),
  8412. array('option' => array('value' => 3)),
  8413. 'Three',
  8414. '/option',
  8415. array('option' => array('value' => 4)),
  8416. 'Four',
  8417. '/option',
  8418. array('option' => array('value' => 5)),
  8419. 'Five',
  8420. '/option',
  8421. '/optgroup',
  8422. '/select'
  8423. );
  8424. $this->assertTags($result, $expected);
  8425. }
  8426. /**
  8427. * Test the generation of fields for a multi record form.
  8428. *
  8429. * @return void
  8430. */
  8431. public function testMultiRecordForm() {
  8432. $this->Form->create('ValidateProfile');
  8433. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8434. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8435. $expected = array(
  8436. 'div' => array('class' => 'input textarea'),
  8437. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8438. 'Name',
  8439. '/label',
  8440. 'textarea' => array(
  8441. 'id' => 'ValidateProfile1ValidateItem2Name',
  8442. 'name' => 'ValidateProfile[1][ValidateItem][2][name]',
  8443. 'cols' => 30,
  8444. 'rows' => 6
  8445. ),
  8446. 'Value',
  8447. '/textarea',
  8448. '/div'
  8449. );
  8450. $this->assertTags($result, $expected);
  8451. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8452. $expected = array(
  8453. 'div' => array('class' => 'input date'),
  8454. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8455. 'Created',
  8456. '/label',
  8457. array('select' => array(
  8458. 'name' => 'ValidateProfile[1][ValidateItem][2][created][month]',
  8459. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8460. )
  8461. ),
  8462. array('option' => array('value' => '')), '/option',
  8463. $this->dateRegex['monthsRegex'],
  8464. '/select', '-',
  8465. array('select' => array(
  8466. 'name' => 'ValidateProfile[1][ValidateItem][2][created][day]',
  8467. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8468. )
  8469. ),
  8470. array('option' => array('value' => '')), '/option',
  8471. $this->dateRegex['daysRegex'],
  8472. '/select', '-',
  8473. array('select' => array(
  8474. 'name' => 'ValidateProfile[1][ValidateItem][2][created][year]',
  8475. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8476. )
  8477. ),
  8478. array('option' => array('value' => '')), '/option',
  8479. $this->dateRegex['yearsRegex'],
  8480. '/select',
  8481. '/div'
  8482. );
  8483. $this->assertTags($result, $expected);
  8484. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8485. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8486. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8487. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8488. $expected = array(
  8489. 'div' => array('class' => 'input select error'),
  8490. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8491. 'Profile',
  8492. '/label',
  8493. 'select' => array(
  8494. 'name' => 'ValidateProfile[1][ValidateItem][2][profile_id]',
  8495. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8496. 'class' => 'form-error'
  8497. ),
  8498. '/select',
  8499. array('div' => array('class' => 'error-message')),
  8500. 'Error',
  8501. '/div',
  8502. '/div'
  8503. );
  8504. $this->assertTags($result, $expected);
  8505. }
  8506. /**
  8507. * test the correct display of multi-record form validation errors.
  8508. *
  8509. * @return void
  8510. */
  8511. public function testMultiRecordFormValidationErrors() {
  8512. $this->Form->create('ValidateProfile');
  8513. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8514. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8515. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8516. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8517. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8518. $result = $this->Form->error('ValidateProfile.2.city');
  8519. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8520. $result = $this->Form->error('2.city');
  8521. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8522. }
  8523. /**
  8524. * test the correct display of multi-record form validation errors.
  8525. *
  8526. * @return void
  8527. */
  8528. public function testSaveManyRecordFormValidationErrors() {
  8529. $this->Form->create('ValidateUser');
  8530. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8531. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8532. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8533. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8534. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8535. $result = $this->Form->error('ValidateUser.0.city');
  8536. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8537. }
  8538. /**
  8539. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8540. *
  8541. * @return void
  8542. */
  8543. public function testInputTemplate() {
  8544. $result = $this->Form->input('Contact.email', array(
  8545. 'type' => 'text', 'format' => array('input')
  8546. ));
  8547. $expected = array(
  8548. 'div' => array('class' => 'input text'),
  8549. 'input' => array(
  8550. 'type' => 'text', 'name' => 'Contact[email]',
  8551. 'id' => 'ContactEmail'
  8552. ),
  8553. '/div'
  8554. );
  8555. $this->assertTags($result, $expected);
  8556. $result = $this->Form->input('Contact.email', array(
  8557. 'type' => 'text', 'format' => array('input', 'label'),
  8558. 'label' => '<em>Email (required)</em>'
  8559. ));
  8560. $expected = array(
  8561. 'div' => array('class' => 'input text'),
  8562. array('input' => array(
  8563. 'type' => 'text', 'name' => 'Contact[email]',
  8564. 'id' => 'ContactEmail'
  8565. )),
  8566. 'label' => array('for' => 'ContactEmail'),
  8567. 'em' => array(),
  8568. 'Email (required)',
  8569. '/em',
  8570. '/label',
  8571. '/div'
  8572. );
  8573. $this->assertTags($result, $expected);
  8574. $result = $this->Form->input('Contact.email', array(
  8575. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8576. 'between' => '<div>Something in the middle</div>',
  8577. 'after' => '<span>Some text at the end</span>'
  8578. ));
  8579. $expected = array(
  8580. 'div' => array('class' => 'input text'),
  8581. array('input' => array(
  8582. 'type' => 'text', 'name' => 'Contact[email]',
  8583. 'id' => 'ContactEmail'
  8584. )),
  8585. array('div' => array()),
  8586. 'Something in the middle',
  8587. '/div',
  8588. 'label' => array('for' => 'ContactEmail'),
  8589. 'Email',
  8590. '/label',
  8591. 'span' => array(),
  8592. 'Some text at the end',
  8593. '/span',
  8594. '/div'
  8595. );
  8596. $this->assertTags($result, $expected);
  8597. $result = $this->Form->input('Contact.method', array(
  8598. 'type' => 'radio',
  8599. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8600. 'between' => 'I am between',
  8601. ));
  8602. $expected = array(
  8603. 'div' => array('class' => 'input radio'),
  8604. 'fieldset' => array(),
  8605. 'legend' => array(),
  8606. 'Method',
  8607. '/legend',
  8608. 'I am between',
  8609. 'input' => array(
  8610. 'type' => 'hidden', 'name' => 'Contact[method]',
  8611. 'value' => '', 'id' => 'ContactMethod_'
  8612. ),
  8613. array('input' => array(
  8614. 'type' => 'radio', 'name' => 'Contact[method]',
  8615. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8616. )),
  8617. array('label' => array('for' => 'ContactMethodEmail')),
  8618. 'Email',
  8619. '/label',
  8620. array('input' => array(
  8621. 'type' => 'radio', 'name' => 'Contact[method]',
  8622. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8623. )),
  8624. array('label' => array('for' => 'ContactMethodPigeon')),
  8625. 'Pigeon',
  8626. '/label',
  8627. '/fieldset',
  8628. '/div',
  8629. );
  8630. $this->assertTags($result, $expected);
  8631. }
  8632. /**
  8633. * test that some html5 inputs + FormHelper::__call() work
  8634. *
  8635. * @return void
  8636. */
  8637. public function testHtml5Inputs() {
  8638. $result = $this->Form->email('User.email');
  8639. $expected = array(
  8640. 'input' => array('type' => 'email', 'name' => 'User[email]', 'id' => 'UserEmail')
  8641. );
  8642. $this->assertTags($result, $expected);
  8643. $result = $this->Form->search('User.query');
  8644. $expected = array(
  8645. 'input' => array('type' => 'search', 'name' => 'User[query]', 'id' => 'UserQuery')
  8646. );
  8647. $this->assertTags($result, $expected);
  8648. $result = $this->Form->search('User.query', array('value' => 'test'));
  8649. $expected = array(
  8650. 'input' => array('type' => 'search', 'name' => 'User[query]', 'id' => 'UserQuery', 'value' => 'test')
  8651. );
  8652. $this->assertTags($result, $expected);
  8653. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8654. $expected = array(
  8655. 'input' => array('type' => 'text', 'name' => 'User[query]', 'id' => 'UserQuery', 'value' => 'test')
  8656. );
  8657. $this->assertTags($result, $expected);
  8658. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8659. $expected = array(
  8660. 'input' => array('type' => 'url', 'name' => 'User[website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8661. );
  8662. $this->assertTags($result, $expected);
  8663. }
  8664. /**
  8665. *
  8666. * @expectedException Cake\Error\Exception
  8667. * @return void
  8668. */
  8669. public function testHtml5InputException() {
  8670. $this->Form->email();
  8671. }
  8672. /**
  8673. * Tests that a model can be loaded from the model names passed in the request object
  8674. *
  8675. * @return void
  8676. */
  8677. public function testIntrospectModelFromRequest() {
  8678. $this->loadFixtures('Post');
  8679. App::build(array(
  8680. 'Plugin' => array(CAKE . 'Test/TestApp/Plugin/')
  8681. ));
  8682. Plugin::load('TestPlugin');
  8683. $this->Form->request['models'] = array(
  8684. 'TestPluginPost' => array(
  8685. 'plugin' => 'TestPlugin',
  8686. 'className' => 'TestPluginPost'
  8687. )
  8688. );
  8689. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8690. $this->Form->create('TestPluginPost');
  8691. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8692. $this->assertInstanceOf('TestPlugin\Model\TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8693. Plugin::unload();
  8694. }
  8695. /**
  8696. * Tests that it is possible to set the validation errors directly in the helper for a field
  8697. *
  8698. * @return void
  8699. */
  8700. public function testCustomValidationErrors() {
  8701. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8702. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8703. $this->assertEquals('Badness!', $result);
  8704. }
  8705. /**
  8706. * Tests that the 'on' key validates as expected on create
  8707. *
  8708. * @return void
  8709. */
  8710. public function testRequiredOnCreate() {
  8711. $this->Form->create('Contact');
  8712. $result = $this->Form->input('Contact.imrequiredonupdate');
  8713. $expected = array(
  8714. 'div' => array('class' => 'input text'),
  8715. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8716. 'Imrequiredonupdate',
  8717. '/label',
  8718. 'input' => array(
  8719. 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
  8720. 'id' => 'ContactImrequiredonupdate'
  8721. ),
  8722. '/div'
  8723. );
  8724. $this->assertTags($result, $expected);
  8725. $result = $this->Form->input('Contact.imrequiredoncreate');
  8726. $expected = array(
  8727. 'div' => array('class' => 'input text required'),
  8728. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8729. 'Imrequiredoncreate',
  8730. '/label',
  8731. 'input' => array(
  8732. 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
  8733. 'id' => 'ContactImrequiredoncreate',
  8734. 'required' => 'required'
  8735. ),
  8736. '/div'
  8737. );
  8738. $this->assertTags($result, $expected);
  8739. $result = $this->Form->input('Contact.imrequiredonboth');
  8740. $expected = array(
  8741. 'div' => array('class' => 'input text required'),
  8742. 'label' => array('for' => 'ContactImrequiredonboth'),
  8743. 'Imrequiredonboth',
  8744. '/label',
  8745. 'input' => array(
  8746. 'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
  8747. 'id' => 'ContactImrequiredonboth',
  8748. 'required' => 'required'
  8749. ),
  8750. '/div'
  8751. );
  8752. $this->assertTags($result, $expected);
  8753. $this->Form->inputDefaults(array('required' => false));
  8754. $result = $this->Form->input('Contact.imrequired');
  8755. $expected = array(
  8756. 'div' => array('class' => 'input text'),
  8757. 'label' => array('for' => 'ContactImrequired'),
  8758. 'Imrequired',
  8759. '/label',
  8760. 'input' => array(
  8761. 'type' => 'text', 'name' => 'Contact[imrequired]',
  8762. 'id' => 'ContactImrequired'
  8763. ),
  8764. '/div'
  8765. );
  8766. $this->assertTags($result, $expected);
  8767. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8768. $this->assertTags($result, $expected);
  8769. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8770. $expected = array(
  8771. 'div' => array('class' => 'input text required'),
  8772. 'label' => array('for' => 'ContactImrequired'),
  8773. 'Imrequired',
  8774. '/label',
  8775. 'input' => array(
  8776. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8777. 'id' => 'ContactImrequired'
  8778. ),
  8779. '/div'
  8780. );
  8781. $this->assertTags($result, $expected);
  8782. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8783. $this->assertTags($result, $expected);
  8784. }
  8785. /**
  8786. * Tests that the 'on' key validates as expected on update
  8787. *
  8788. * @return void
  8789. */
  8790. public function testRequiredOnUpdate() {
  8791. $this->Form->request->data['Contact']['id'] = 1;
  8792. $this->Form->create('Contact');
  8793. $result = $this->Form->input('Contact.imrequiredonupdate');
  8794. $expected = array(
  8795. 'div' => array('class' => 'input text required'),
  8796. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8797. 'Imrequiredonupdate',
  8798. '/label',
  8799. 'input' => array(
  8800. 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
  8801. 'id' => 'ContactImrequiredonupdate',
  8802. 'required' => 'required'
  8803. ),
  8804. '/div'
  8805. );
  8806. $this->assertTags($result, $expected);
  8807. $result = $this->Form->input('Contact.imrequiredoncreate');
  8808. $expected = array(
  8809. 'div' => array('class' => 'input text'),
  8810. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8811. 'Imrequiredoncreate',
  8812. '/label',
  8813. 'input' => array(
  8814. 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
  8815. 'id' => 'ContactImrequiredoncreate'
  8816. ),
  8817. '/div'
  8818. );
  8819. $this->assertTags($result, $expected);
  8820. $result = $this->Form->input('Contact.imrequiredonboth');
  8821. $expected = array(
  8822. 'div' => array('class' => 'input text required'),
  8823. 'label' => array('for' => 'ContactImrequiredonboth'),
  8824. 'Imrequiredonboth',
  8825. '/label',
  8826. 'input' => array(
  8827. 'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
  8828. 'id' => 'ContactImrequiredonboth',
  8829. 'required' => 'required'
  8830. ),
  8831. '/div'
  8832. );
  8833. $this->assertTags($result, $expected);
  8834. $result = $this->Form->input('Contact.imrequired');
  8835. $expected = array(
  8836. 'div' => array('class' => 'input text required'),
  8837. 'label' => array('for' => 'ContactImrequired'),
  8838. 'Imrequired',
  8839. '/label',
  8840. 'input' => array(
  8841. 'type' => 'text', 'name' => 'Contact[imrequired]',
  8842. 'id' => 'ContactImrequired',
  8843. 'required' => 'required'
  8844. ),
  8845. '/div'
  8846. );
  8847. $this->assertTags($result, $expected);
  8848. }
  8849. /**
  8850. * Test inputDefaults setter and getter
  8851. *
  8852. * @return void
  8853. */
  8854. public function testInputDefaults() {
  8855. $this->Form->create('Contact');
  8856. $this->Form->inputDefaults(array(
  8857. 'label' => false,
  8858. 'div' => array(
  8859. 'style' => 'color: #000;'
  8860. )
  8861. ));
  8862. $result = $this->Form->input('Contact.field1');
  8863. $expected = array(
  8864. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8865. 'input' => array(
  8866. 'type' => 'text', 'name' => 'Contact[field1]',
  8867. 'id' => 'ContactField1'
  8868. ),
  8869. '/div'
  8870. );
  8871. $this->assertTags($result, $expected);
  8872. $this->Form->inputDefaults(array(
  8873. 'div' => false,
  8874. 'label' => 'Label',
  8875. ));
  8876. $result = $this->Form->input('Contact.field1');
  8877. $expected = array(
  8878. 'label' => array('for' => 'ContactField1'),
  8879. 'Label',
  8880. '/label',
  8881. 'input' => array(
  8882. 'type' => 'text', 'name' => 'Contact[field1]',
  8883. 'id' => 'ContactField1'
  8884. ),
  8885. );
  8886. $this->assertTags($result, $expected);
  8887. $this->Form->inputDefaults(array(
  8888. 'label' => false,
  8889. ), true);
  8890. $result = $this->Form->input('Contact.field1');
  8891. $expected = array(
  8892. 'input' => array(
  8893. 'type' => 'text', 'name' => 'Contact[field1]',
  8894. 'id' => 'ContactField1'
  8895. ),
  8896. );
  8897. $this->assertTags($result, $expected);
  8898. $result = $this->Form->inputDefaults();
  8899. $expected = array(
  8900. 'div' => false,
  8901. 'label' => false,
  8902. );
  8903. $this->assertEquals($expected, $result);
  8904. }
  8905. }