FormHelperTest.php 297 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @package Cake.Test.Case.View.Helper
  17. * @since CakePHP(tm) v 1.2.0.4206
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('ClassRegistry', 'Utility');
  21. App::uses('Controller', 'Controller');
  22. App::uses('View', 'View');
  23. App::uses('Model', 'Model');
  24. App::uses('Security', 'Utility');
  25. App::uses('CakeRequest', 'Network');
  26. App::uses('HtmlHelper', 'View/Helper');
  27. App::uses('FormHelper', 'View/Helper');
  28. App::uses('Router', 'Routing');
  29. /**
  30. * ContactTestController class
  31. *
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * uses property
  37. *
  38. * @var mixed null
  39. */
  40. public $uses = null;
  41. }
  42. /**
  43. * Contact class
  44. *
  45. * @package Cake.Test.Case.View.Helper
  46. */
  47. class Contact extends CakeTestModel {
  48. /**
  49. * useTable property
  50. *
  51. * @var boolean
  52. */
  53. public $useTable = false;
  54. /**
  55. * Default schema
  56. *
  57. * @var array
  58. */
  59. protected $_schema = array(
  60. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  61. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  65. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  66. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  67. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  68. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  69. );
  70. /**
  71. * validate property
  72. *
  73. * @var array
  74. */
  75. public $validate = array(
  76. 'non_existing' => array(),
  77. 'idontexist' => array(),
  78. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  79. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  80. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  81. 'imrequiredonboth' => array(
  82. 'required' => array('rule' => 'alphaNumeric'),
  83. ),
  84. 'string_required' => 'notEmpty',
  85. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  86. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  87. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  88. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  89. 'imalsonotrequired' => array(
  90. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  91. 'between' => array('rule' => array('between', 5, 30)),
  92. ),
  93. 'imalsonotrequired2' => array(
  94. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  95. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  96. ),
  97. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  98. 'iamrequiredalways' => array(
  99. 'email' => array('rule' => 'email'),
  100. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  101. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  102. ),
  103. 'boolean_field' => array('rule' => 'boolean')
  104. );
  105. /**
  106. * schema method
  107. *
  108. * @return void
  109. */
  110. public function setSchema($schema) {
  111. $this->_schema = $schema;
  112. }
  113. /**
  114. * hasAndBelongsToMany property
  115. *
  116. * @var array
  117. */
  118. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  119. /**
  120. * hasAndBelongsToMany property
  121. *
  122. * @var array
  123. */
  124. public $belongsTo = array('User' => array('className' => 'UserForm'));
  125. }
  126. /**
  127. * ContactTagsContact class
  128. *
  129. * @package Cake.Test.Case.View.Helper
  130. */
  131. class ContactTagsContact extends CakeTestModel {
  132. /**
  133. * useTable property
  134. *
  135. * @var boolean
  136. */
  137. public $useTable = false;
  138. /**
  139. * Default schema
  140. *
  141. * @var array
  142. */
  143. protected $_schema = array(
  144. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  145. 'contact_tag_id' => array(
  146. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  147. )
  148. );
  149. /**
  150. * schema method
  151. *
  152. * @return void
  153. */
  154. public function setSchema($schema) {
  155. $this->_schema = $schema;
  156. }
  157. }
  158. /**
  159. * ContactNonStandardPk class
  160. *
  161. * @package Cake.Test.Case.View.Helper
  162. */
  163. class ContactNonStandardPk extends Contact {
  164. /**
  165. * primaryKey property
  166. *
  167. * @var string
  168. */
  169. public $primaryKey = 'pk';
  170. /**
  171. * schema method
  172. *
  173. * @return void
  174. */
  175. public function schema($field = false) {
  176. $this->_schema = parent::schema();
  177. $this->_schema['pk'] = $this->_schema['id'];
  178. unset($this->_schema['id']);
  179. return $this->_schema;
  180. }
  181. }
  182. /**
  183. * ContactTag class
  184. *
  185. * @package Cake.Test.Case.View.Helper
  186. */
  187. class ContactTag extends Model {
  188. /**
  189. * useTable property
  190. *
  191. * @var boolean
  192. */
  193. public $useTable = false;
  194. /**
  195. * schema definition
  196. *
  197. * @var array
  198. */
  199. protected $_schema = array(
  200. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  201. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  202. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  203. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  204. );
  205. }
  206. /**
  207. * UserForm class
  208. *
  209. * @package Cake.Test.Case.View.Helper
  210. */
  211. class UserForm extends CakeTestModel {
  212. /**
  213. * useTable property
  214. *
  215. * @var boolean
  216. */
  217. public $useTable = false;
  218. /**
  219. * hasMany property
  220. *
  221. * @var array
  222. */
  223. public $hasMany = array(
  224. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  225. ));
  226. /**
  227. * schema definition
  228. *
  229. * @var array
  230. */
  231. protected $_schema = array(
  232. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  233. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  234. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  235. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  236. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  237. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  238. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  239. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  240. );
  241. }
  242. /**
  243. * OpenidUrl class
  244. *
  245. * @package Cake.Test.Case.View.Helper
  246. */
  247. class OpenidUrl extends CakeTestModel {
  248. /**
  249. * useTable property
  250. *
  251. * @var boolean
  252. */
  253. public $useTable = false;
  254. /**
  255. * belongsTo property
  256. *
  257. * @var array
  258. */
  259. public $belongsTo = array('UserForm' => array(
  260. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  261. ));
  262. /**
  263. * validate property
  264. *
  265. * @var array
  266. */
  267. public $validate = array('openid_not_registered' => array());
  268. /**
  269. * schema method
  270. *
  271. * @var array
  272. */
  273. protected $_schema = array(
  274. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  275. 'user_form_id' => array(
  276. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  277. ),
  278. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  279. );
  280. /**
  281. * beforeValidate method
  282. *
  283. * @return void
  284. */
  285. public function beforeValidate($options = array()) {
  286. $this->invalidate('openid_not_registered');
  287. return true;
  288. }
  289. }
  290. /**
  291. * ValidateUser class
  292. *
  293. * @package Cake.Test.Case.View.Helper
  294. */
  295. class ValidateUser extends CakeTestModel {
  296. /**
  297. * useTable property
  298. *
  299. * @var boolean
  300. */
  301. public $useTable = false;
  302. /**
  303. * hasOne property
  304. *
  305. * @var array
  306. */
  307. public $hasOne = array('ValidateProfile' => array(
  308. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  309. ));
  310. /**
  311. * schema method
  312. *
  313. * @var array
  314. */
  315. protected $_schema = array(
  316. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  317. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  318. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  319. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  320. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => '6,3'),
  321. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  322. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  323. );
  324. /**
  325. * beforeValidate method
  326. *
  327. * @return void
  328. */
  329. public function beforeValidate($options = array()) {
  330. $this->invalidate('email');
  331. return false;
  332. }
  333. }
  334. /**
  335. * ValidateProfile class
  336. *
  337. * @package Cake.Test.Case.View.Helper
  338. */
  339. class ValidateProfile extends CakeTestModel {
  340. /**
  341. * useTable property
  342. *
  343. * @var boolean
  344. */
  345. public $useTable = false;
  346. /**
  347. * schema property
  348. *
  349. * @var array
  350. */
  351. protected $_schema = array(
  352. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  353. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  354. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  355. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  356. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  357. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  358. );
  359. /**
  360. * hasOne property
  361. *
  362. * @var array
  363. */
  364. public $hasOne = array('ValidateItem' => array(
  365. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  366. ));
  367. /**
  368. * belongsTo property
  369. *
  370. * @var array
  371. */
  372. public $belongsTo = array('ValidateUser' => array(
  373. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  374. ));
  375. /**
  376. * beforeValidate method
  377. *
  378. * @return void
  379. */
  380. public function beforeValidate($options = array()) {
  381. $this->invalidate('full_name');
  382. $this->invalidate('city');
  383. return false;
  384. }
  385. }
  386. /**
  387. * ValidateItem class
  388. *
  389. * @package Cake.Test.Case.View.Helper
  390. */
  391. class ValidateItem extends CakeTestModel {
  392. /**
  393. * useTable property
  394. *
  395. * @var boolean
  396. */
  397. public $useTable = false;
  398. /**
  399. * schema property
  400. *
  401. * @var array
  402. */
  403. protected $_schema = array(
  404. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  405. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  406. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  407. 'description' => array(
  408. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  409. ),
  410. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  411. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  412. );
  413. /**
  414. * belongsTo property
  415. *
  416. * @var array
  417. */
  418. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  419. /**
  420. * beforeValidate method
  421. *
  422. * @return void
  423. */
  424. public function beforeValidate($options = array()) {
  425. $this->invalidate('description');
  426. return false;
  427. }
  428. }
  429. /**
  430. * TestMail class
  431. *
  432. * @package Cake.Test.Case.View.Helper
  433. */
  434. class TestMail extends CakeTestModel {
  435. /**
  436. * useTable property
  437. *
  438. * @var boolean
  439. */
  440. public $useTable = false;
  441. }
  442. /**
  443. * FormHelperTest class
  444. *
  445. * @package Cake.Test.Case.View.Helper
  446. * @property FormHelper $Form
  447. */
  448. class FormHelperTest extends CakeTestCase {
  449. /**
  450. * Fixtures to be used
  451. *
  452. * @var array
  453. */
  454. public $fixtures = array('core.post');
  455. /**
  456. * Do not load the fixtures by default
  457. *
  458. * @var boolean
  459. */
  460. public $autoFixtures = false;
  461. /**
  462. * setUp method
  463. *
  464. * @return void
  465. */
  466. public function setUp() {
  467. parent::setUp();
  468. Configure::write('Config.language', 'eng');
  469. Configure::write('App.base', '');
  470. Configure::delete('Asset');
  471. $this->Controller = new ContactTestController();
  472. $this->View = new View($this->Controller);
  473. $this->Form = new FormHelper($this->View);
  474. $this->Form->Html = new HtmlHelper($this->View);
  475. $this->Form->request = new CakeRequest('contacts/add', false);
  476. $this->Form->request->here = '/contacts/add';
  477. $this->Form->request['action'] = 'add';
  478. $this->Form->request->webroot = '';
  479. $this->Form->request->base = '';
  480. ClassRegistry::addObject('Contact', new Contact());
  481. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  482. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  483. ClassRegistry::addObject('User', new UserForm());
  484. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  485. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  486. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  487. $this->oldSalt = Configure::read('Security.salt');
  488. $this->dateRegex = array(
  489. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  490. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  491. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  492. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  493. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  494. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  495. );
  496. Configure::write('Security.salt', 'foo!');
  497. }
  498. /**
  499. * tearDown method
  500. *
  501. * @return void
  502. */
  503. public function tearDown() {
  504. parent::tearDown();
  505. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  506. Configure::write('Security.salt', $this->oldSalt);
  507. }
  508. /**
  509. * testFormCreateWithSecurity method
  510. *
  511. * Test form->create() with security key.
  512. *
  513. * @return void
  514. */
  515. public function testCreateWithSecurity() {
  516. $this->Form->request['_Token'] = array('key' => 'testKey');
  517. $encoding = strtolower(Configure::read('App.encoding'));
  518. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  519. $expected = array(
  520. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  521. 'div' => array('style' => 'display:none;'),
  522. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  523. array('input' => array(
  524. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  525. )),
  526. '/div'
  527. );
  528. $this->assertTags($result, $expected);
  529. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  530. $expected['form']['id'] = 'MyForm';
  531. $this->assertTags($result, $expected);
  532. }
  533. /**
  534. * testFormCreateGetNoSecurity method
  535. *
  536. * Test form->create() with no security key as its a get form
  537. *
  538. * @return void
  539. */
  540. public function testCreateEndGetNoSecurity() {
  541. $this->Form->request['_Token'] = array('key' => 'testKey');
  542. $encoding = strtolower(Configure::read('App.encoding'));
  543. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  544. $this->assertNotContains('Token', $result);
  545. $result = $this->Form->end('Save');
  546. $this->assertNotContains('Token', $result);
  547. }
  548. /**
  549. * test that create() clears the fields property so it starts fresh
  550. *
  551. * @return void
  552. */
  553. public function testCreateClearingFields() {
  554. $this->Form->fields = array('model_id');
  555. $this->Form->create('Contact');
  556. $this->assertEquals(array(), $this->Form->fields);
  557. }
  558. /**
  559. * Tests form hash generation with model-less data
  560. *
  561. * @return void
  562. */
  563. public function testValidateHashNoModel() {
  564. $this->Form->request['_Token'] = array('key' => 'foo');
  565. $result = $this->Form->secure(array('anything'));
  566. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  567. }
  568. /**
  569. * Tests that models with identical field names get resolved properly
  570. *
  571. * @return void
  572. */
  573. public function testDuplicateFieldNameResolution() {
  574. $result = $this->Form->create('ValidateUser');
  575. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  576. $result = $this->Form->input('ValidateItem.name');
  577. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  578. $result = $this->Form->input('ValidateUser.name');
  579. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  580. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  581. $this->assertRegExp('/type="text"/', $result);
  582. $result = $this->Form->input('ValidateItem.name');
  583. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  584. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  585. $this->assertRegExp('/<textarea/', $result);
  586. $result = $this->Form->input('name');
  587. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  588. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  589. $this->assertRegExp('/type="text"/', $result);
  590. }
  591. /**
  592. * Tests that hidden fields generated for checkboxes don't get locked
  593. *
  594. * @return void
  595. */
  596. public function testNoCheckboxLocking() {
  597. $this->Form->request['_Token'] = array('key' => 'foo');
  598. $this->assertSame(array(), $this->Form->fields);
  599. $this->Form->checkbox('check', array('value' => '1'));
  600. $this->assertSame($this->Form->fields, array('check'));
  601. }
  602. /**
  603. * testFormSecurityFields method
  604. *
  605. * Test generation of secure form hash generation.
  606. *
  607. * @return void
  608. */
  609. public function testFormSecurityFields() {
  610. $key = 'testKey';
  611. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  612. $this->Form->request['_Token'] = array('key' => $key);
  613. $result = $this->Form->secure($fields);
  614. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  615. $hash .= ':' . 'Model.valid';
  616. $hash = urlencode($hash);
  617. $expected = array(
  618. 'div' => array('style' => 'display:none;'),
  619. array('input' => array(
  620. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  621. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  622. )),
  623. array('input' => array(
  624. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  625. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  626. )),
  627. '/div'
  628. );
  629. $this->assertTags($result, $expected);
  630. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  631. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  632. $result = $this->Form->secure($fields);
  633. $expected = array(
  634. 'div' => array('class' => 'hidden'),
  635. array('input' => array(
  636. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  637. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  638. )),
  639. array('input' => array(
  640. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  641. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  642. )),
  643. '/div'
  644. );
  645. $this->assertTags($result, $expected);
  646. }
  647. /**
  648. * Tests correct generation of number fields for double and float fields
  649. *
  650. * @return void
  651. */
  652. public function testTextFieldGenerationForFloats() {
  653. $model = ClassRegistry::getObject('Contact');
  654. $model->setSchema(array('foo' => array(
  655. 'type' => 'float',
  656. 'null' => false,
  657. 'default' => null,
  658. 'length' => 10
  659. )));
  660. $this->Form->create('Contact');
  661. $result = $this->Form->input('foo');
  662. $expected = array(
  663. 'div' => array('class' => 'input number'),
  664. 'label' => array('for' => 'ContactFoo'),
  665. 'Foo',
  666. '/label',
  667. array('input' => array(
  668. 'type' => 'number',
  669. 'name' => 'data[Contact][foo]',
  670. 'id' => 'ContactFoo',
  671. 'step' => 'any'
  672. )),
  673. '/div'
  674. );
  675. $this->assertTags($result, $expected);
  676. $result = $this->Form->input('foo', array('step' => 0.5));
  677. $expected = array(
  678. 'div' => array('class' => 'input number'),
  679. 'label' => array('for' => 'ContactFoo'),
  680. 'Foo',
  681. '/label',
  682. array('input' => array(
  683. 'type' => 'number',
  684. 'name' => 'data[Contact][foo]',
  685. 'id' => 'ContactFoo',
  686. 'step' => '0.5'
  687. )),
  688. '/div'
  689. );
  690. $this->assertTags($result, $expected);
  691. }
  692. /**
  693. * Tests correct generation of number fields for integer fields
  694. *
  695. * @return void
  696. */
  697. public function testTextFieldTypeNumberGenerationForIntegers() {
  698. $model = ClassRegistry::getObject('Contact');
  699. $model->setSchema(array('foo' => array(
  700. 'type' => 'integer',
  701. 'null' => false,
  702. 'default' => null,
  703. 'length' => null
  704. )));
  705. $this->Form->create('Contact');
  706. $result = $this->Form->input('foo');
  707. $expected = array(
  708. 'div' => array('class' => 'input number'),
  709. 'label' => array('for' => 'ContactFoo'),
  710. 'Foo',
  711. '/label',
  712. array('input' => array(
  713. 'type' => 'number', 'name' => 'data[Contact][foo]',
  714. 'id' => 'ContactFoo'
  715. )),
  716. '/div'
  717. );
  718. $this->assertTags($result, $expected);
  719. }
  720. /**
  721. * Tests correct generation of file upload fields for binary fields
  722. *
  723. * @return void
  724. */
  725. public function testFileUploadFieldTypeGenerationForBinaries() {
  726. $model = ClassRegistry::getObject('Contact');
  727. $model->setSchema(array('foo' => array(
  728. 'type' => 'binary',
  729. 'null' => false,
  730. 'default' => null,
  731. 'length' => 1024
  732. )));
  733. $this->Form->create('Contact');
  734. $result = $this->Form->input('foo');
  735. $expected = array(
  736. 'div' => array('class' => 'input file'),
  737. 'label' => array('for' => 'ContactFoo'),
  738. 'Foo',
  739. '/label',
  740. array('input' => array(
  741. 'type' => 'file', 'name' => 'data[Contact][foo]',
  742. 'id' => 'ContactFoo'
  743. )),
  744. '/div'
  745. );
  746. $this->assertTags($result, $expected);
  747. }
  748. /**
  749. * testFormSecurityMultipleFields method
  750. *
  751. * Test secure() with multiple row form. Ensure hash is correct.
  752. *
  753. * @return void
  754. */
  755. public function testFormSecurityMultipleFields() {
  756. $key = 'testKey';
  757. $fields = array(
  758. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  759. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  760. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  761. );
  762. $this->Form->request['_Token'] = array('key' => $key);
  763. $result = $this->Form->secure($fields);
  764. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  765. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  766. $expected = array(
  767. 'div' => array('style' => 'display:none;'),
  768. array('input' => array(
  769. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  770. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  771. )),
  772. array('input' => array(
  773. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  774. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  775. )),
  776. '/div'
  777. );
  778. $this->assertTags($result, $expected);
  779. }
  780. /**
  781. * testFormSecurityMultipleSubmitButtons
  782. *
  783. * test form submit generation and ensure that _Token is only created on end()
  784. *
  785. * @return void
  786. */
  787. public function testFormSecurityMultipleSubmitButtons() {
  788. $key = 'testKey';
  789. $this->Form->request['_Token'] = array('key' => $key);
  790. $this->Form->create('Addresses');
  791. $this->Form->input('Address.title');
  792. $this->Form->input('Address.first_name');
  793. $result = $this->Form->submit('Save', array('name' => 'save'));
  794. $expected = array(
  795. 'div' => array('class' => 'submit'),
  796. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  797. '/div',
  798. );
  799. $this->assertTags($result, $expected);
  800. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  801. $expected = array(
  802. 'div' => array('class' => 'submit'),
  803. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  804. '/div',
  805. );
  806. $this->assertTags($result, $expected);
  807. $result = $this->Form->end(null);
  808. $expected = array(
  809. 'div' => array('style' => 'display:none;'),
  810. array('input' => array(
  811. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  812. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  813. )),
  814. array('input' => array(
  815. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  816. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  817. )),
  818. '/div'
  819. );
  820. $this->assertTags($result, $expected);
  821. }
  822. /**
  823. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  824. *
  825. * @return void
  826. */
  827. public function testSecurityButtonNestedNamed() {
  828. $key = 'testKey';
  829. $this->Form->request['_Token'] = array('key' => $key);
  830. $this->Form->create('Addresses');
  831. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  832. $result = $this->Form->unlockField();
  833. $this->assertEquals(array('Address.button'), $result);
  834. }
  835. /**
  836. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  837. *
  838. * @return void
  839. */
  840. public function testSecuritySubmitNestedNamed() {
  841. $key = 'testKey';
  842. $this->Form->request['_Token'] = array('key' => $key);
  843. $this->Form->create('Addresses');
  844. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  845. $result = $this->Form->unlockField();
  846. $this->assertEquals(array('Address.button'), $result);
  847. }
  848. /**
  849. * Test that the correct fields are unlocked for image submits with no names.
  850. *
  851. * @return void
  852. */
  853. public function testSecuritySubmitImageNoName() {
  854. $key = 'testKey';
  855. $this->Form->request['_Token'] = array('key' => $key);
  856. $this->Form->create('User');
  857. $result = $this->Form->submit('save.png');
  858. $expected = array(
  859. 'div' => array('class' => 'submit'),
  860. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  861. '/div'
  862. );
  863. $this->assertTags($result, $expected);
  864. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  865. }
  866. /**
  867. * Test that the correct fields are unlocked for image submits with names.
  868. *
  869. * @return void
  870. */
  871. public function testSecuritySubmitImageName() {
  872. $key = 'testKey';
  873. $this->Form->request['_Token'] = array('key' => $key);
  874. $this->Form->create('User');
  875. $result = $this->Form->submit('save.png', array('name' => 'test'));
  876. $expected = array(
  877. 'div' => array('class' => 'submit'),
  878. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  879. '/div'
  880. );
  881. $this->assertTags($result, $expected);
  882. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  883. }
  884. /**
  885. * testFormSecurityMultipleInputFields method
  886. *
  887. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  888. *
  889. * @return void
  890. */
  891. public function testFormSecurityMultipleInputFields() {
  892. $key = 'testKey';
  893. $this->Form->request['_Token'] = array('key' => $key);
  894. $this->Form->create('Addresses');
  895. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  896. $this->Form->input('Addresses.0.title');
  897. $this->Form->input('Addresses.0.first_name');
  898. $this->Form->input('Addresses.0.last_name');
  899. $this->Form->input('Addresses.0.address');
  900. $this->Form->input('Addresses.0.city');
  901. $this->Form->input('Addresses.0.phone');
  902. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  903. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  904. $this->Form->input('Addresses.1.title');
  905. $this->Form->input('Addresses.1.first_name');
  906. $this->Form->input('Addresses.1.last_name');
  907. $this->Form->input('Addresses.1.address');
  908. $this->Form->input('Addresses.1.city');
  909. $this->Form->input('Addresses.1.phone');
  910. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  911. $result = $this->Form->secure($this->Form->fields);
  912. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  913. $expected = array(
  914. 'div' => array('style' => 'display:none;'),
  915. array('input' => array(
  916. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  917. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  918. )),
  919. array('input' => array(
  920. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  921. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  922. )),
  923. '/div'
  924. );
  925. $this->assertTags($result, $expected);
  926. }
  927. /**
  928. * Test form security with Model.field.0 style inputs
  929. *
  930. * @return void
  931. */
  932. public function testFormSecurityArrayFields() {
  933. $key = 'testKey';
  934. $this->Form->request->params['_Token']['key'] = $key;
  935. $this->Form->create('Address');
  936. $this->Form->input('Address.primary.1');
  937. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  938. $this->Form->input('Address.secondary.1.0');
  939. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  940. }
  941. /**
  942. * testFormSecurityMultipleInputDisabledFields method
  943. *
  944. * test secure form generation with multiple records and disabled fields.
  945. *
  946. * @return void
  947. */
  948. public function testFormSecurityMultipleInputDisabledFields() {
  949. $key = 'testKey';
  950. $this->Form->request->params['_Token'] = array(
  951. 'key' => $key,
  952. 'unlockedFields' => array('first_name', 'address')
  953. );
  954. $this->Form->create();
  955. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  956. $this->Form->input('Addresses.0.title');
  957. $this->Form->input('Addresses.0.first_name');
  958. $this->Form->input('Addresses.0.last_name');
  959. $this->Form->input('Addresses.0.address');
  960. $this->Form->input('Addresses.0.city');
  961. $this->Form->input('Addresses.0.phone');
  962. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  963. $this->Form->input('Addresses.1.title');
  964. $this->Form->input('Addresses.1.first_name');
  965. $this->Form->input('Addresses.1.last_name');
  966. $this->Form->input('Addresses.1.address');
  967. $this->Form->input('Addresses.1.city');
  968. $this->Form->input('Addresses.1.phone');
  969. $result = $this->Form->secure($this->Form->fields);
  970. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  971. $expected = array(
  972. 'div' => array('style' => 'display:none;'),
  973. array('input' => array(
  974. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  975. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  976. )),
  977. array('input' => array(
  978. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  979. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  980. )),
  981. '/div'
  982. );
  983. $this->assertTags($result, $expected);
  984. }
  985. /**
  986. * testFormSecurityInputDisabledFields method
  987. *
  988. * Test single record form with disabled fields.
  989. *
  990. * @return void
  991. */
  992. public function testFormSecurityInputUnlockedFields() {
  993. $key = 'testKey';
  994. $this->Form->request['_Token'] = array(
  995. 'key' => $key,
  996. 'unlockedFields' => array('first_name', 'address')
  997. );
  998. $this->Form->create();
  999. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1000. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1001. $this->Form->input('Addresses.title');
  1002. $this->Form->input('Addresses.first_name');
  1003. $this->Form->input('Addresses.last_name');
  1004. $this->Form->input('Addresses.address');
  1005. $this->Form->input('Addresses.city');
  1006. $this->Form->input('Addresses.phone');
  1007. $result = $this->Form->fields;
  1008. $expected = array(
  1009. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1010. 'Addresses.city', 'Addresses.phone'
  1011. );
  1012. $this->assertEquals($expected, $result);
  1013. $result = $this->Form->secure($expected);
  1014. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1015. $expected = array(
  1016. 'div' => array('style' => 'display:none;'),
  1017. array('input' => array(
  1018. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1019. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1020. )),
  1021. array('input' => array(
  1022. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1023. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1024. )),
  1025. '/div'
  1026. );
  1027. $this->assertTags($result, $expected);
  1028. }
  1029. /**
  1030. * test securing inputs with custom name attributes.
  1031. *
  1032. * @return void
  1033. */
  1034. public function testFormSecureWithCustomNameAttribute() {
  1035. $this->Form->request->params['_Token']['key'] = 'testKey';
  1036. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1037. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1038. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1039. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1040. }
  1041. /**
  1042. * testFormSecuredInput method
  1043. *
  1044. * Test generation of entire secure form, assertions made on input() output.
  1045. *
  1046. * @return void
  1047. */
  1048. public function testFormSecuredInput() {
  1049. $this->Form->request['_Token'] = array('key' => 'testKey');
  1050. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1051. $encoding = strtolower(Configure::read('App.encoding'));
  1052. $expected = array(
  1053. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1054. 'div' => array('style' => 'display:none;'),
  1055. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1056. array('input' => array(
  1057. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1058. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1059. )),
  1060. '/div'
  1061. );
  1062. $this->assertTags($result, $expected);
  1063. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1064. $expected = array(
  1065. 'div' => array('class' => 'input text'),
  1066. 'label' => array('for' => 'UserFormPublished'),
  1067. 'Published',
  1068. '/label',
  1069. array('input' => array(
  1070. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1071. 'id' => 'UserFormPublished'
  1072. )),
  1073. '/div'
  1074. );
  1075. $this->assertTags($result, $expected);
  1076. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1077. $expected = array(
  1078. 'div' => array('class' => 'input text'),
  1079. 'label' => array('for' => 'UserFormOther'),
  1080. 'Other',
  1081. '/label',
  1082. array('input' => array(
  1083. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1084. 'id' => 'UserFormOther'
  1085. )),
  1086. '/div'
  1087. );
  1088. $this->assertTags($result, $expected);
  1089. $result = $this->Form->hidden('UserForm.stuff');
  1090. $expected = array(
  1091. 'input' => array(
  1092. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1093. 'id' => 'UserFormStuff'
  1094. ));
  1095. $this->assertTags($result, $expected);
  1096. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1097. $expected = array('input' => array(
  1098. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1099. 'value' => '0', 'id' => 'UserFormHidden'
  1100. ));
  1101. $this->assertTags($result, $expected);
  1102. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1103. $expected = array(
  1104. 'div' => array('class' => 'input checkbox'),
  1105. array('input' => array(
  1106. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1107. 'value' => '0', 'id' => 'UserFormSomething_'
  1108. )),
  1109. array('input' => array(
  1110. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1111. 'value' => '1', 'id' => 'UserFormSomething'
  1112. )),
  1113. 'label' => array('for' => 'UserFormSomething'),
  1114. 'Something',
  1115. '/label',
  1116. '/div'
  1117. );
  1118. $this->assertTags($result, $expected);
  1119. $result = $this->Form->fields;
  1120. $expected = array(
  1121. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1122. 'UserForm.hidden' => '0', 'UserForm.something'
  1123. );
  1124. $this->assertEquals($expected, $result);
  1125. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1126. $result = $this->Form->secure($this->Form->fields);
  1127. $expected = array(
  1128. 'div' => array('style' => 'display:none;'),
  1129. array('input' => array(
  1130. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1131. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1132. )),
  1133. array('input' => array(
  1134. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1135. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1136. )),
  1137. '/div'
  1138. );
  1139. $this->assertTags($result, $expected);
  1140. }
  1141. /**
  1142. * Test secured inputs with custom names.
  1143. *
  1144. * @return void
  1145. */
  1146. public function testSecuredInputCustomName() {
  1147. $this->Form->request['_Token'] = array('key' => 'testKey');
  1148. $this->assertEquals(array(), $this->Form->fields);
  1149. $this->Form->input('text_input', array(
  1150. 'name' => 'data[Option][General.default_role]',
  1151. ));
  1152. $expected = array('Option.General.default_role');
  1153. $this->assertEquals($expected, $this->Form->fields);
  1154. $this->Form->input('select_box', array(
  1155. 'name' => 'data[Option][General.select_role]',
  1156. 'type' => 'select',
  1157. 'options' => array(1, 2),
  1158. ));
  1159. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1160. $this->assertEquals($expected, $this->Form->fields);
  1161. }
  1162. /**
  1163. * Tests that the correct keys are added to the field hash index
  1164. *
  1165. * @return void
  1166. */
  1167. public function testFormSecuredFileInput() {
  1168. $this->Form->request['_Token'] = array('key' => 'testKey');
  1169. $this->assertEquals(array(), $this->Form->fields);
  1170. $this->Form->file('Attachment.file');
  1171. $expected = array(
  1172. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1173. 'Attachment.file.error', 'Attachment.file.size'
  1174. );
  1175. $this->assertEquals($expected, $this->Form->fields);
  1176. }
  1177. /**
  1178. * test that multiple selects keys are added to field hash
  1179. *
  1180. * @return void
  1181. */
  1182. public function testFormSecuredMultipleSelect() {
  1183. $this->Form->request['_Token'] = array('key' => 'testKey');
  1184. $this->assertEquals(array(), $this->Form->fields);
  1185. $options = array('1' => 'one', '2' => 'two');
  1186. $this->Form->select('Model.select', $options);
  1187. $expected = array('Model.select');
  1188. $this->assertEquals($expected, $this->Form->fields);
  1189. $this->Form->fields = array();
  1190. $this->Form->select('Model.select', $options, array('multiple' => true));
  1191. $this->assertEquals($expected, $this->Form->fields);
  1192. }
  1193. /**
  1194. * testFormSecuredRadio method
  1195. *
  1196. * @return void
  1197. */
  1198. public function testFormSecuredRadio() {
  1199. $this->Form->request['_Token'] = array('key' => 'testKey');
  1200. $this->assertEquals(array(), $this->Form->fields);
  1201. $options = array('1' => 'option1', '2' => 'option2');
  1202. $this->Form->radio('Test.test', $options);
  1203. $expected = array('Test.test');
  1204. $this->assertEquals($expected, $this->Form->fields);
  1205. }
  1206. /**
  1207. * Test that when disabled is in a list based attribute array it works.
  1208. *
  1209. * @return void
  1210. */
  1211. public function testFormSecuredAndDisabledNotAssoc() {
  1212. $this->Form->request['_Token'] = array('key' => 'testKey');
  1213. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1214. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1215. $this->Form->text('Model.text', array('disabled'));
  1216. $this->Form->textarea('Model.textarea', array('disabled'));
  1217. $this->Form->password('Model.password', array('disabled'));
  1218. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1219. $expected = array(
  1220. 'Model.radio' => ''
  1221. );
  1222. $this->assertEquals($expected, $this->Form->fields);
  1223. }
  1224. /**
  1225. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1226. * hashing.
  1227. *
  1228. * @return void
  1229. */
  1230. public function testFormSecuredAndDisabled() {
  1231. $this->Form->request['_Token'] = array('key' => 'testKey');
  1232. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1233. $this->Form->text('Model.text', array('disabled' => true));
  1234. $this->Form->password('Model.text', array('disabled' => true));
  1235. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1236. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1237. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1238. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1239. $this->Form->month('Model.month', array('disabled' => true));
  1240. $this->Form->day('Model.day', array('disabled' => true));
  1241. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1242. $this->Form->minute('Model.minute', array('disabled' => true));
  1243. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1244. $expected = array(
  1245. 'Model.radio' => ''
  1246. );
  1247. $this->assertEquals($expected, $this->Form->fields);
  1248. }
  1249. /**
  1250. * testDisableSecurityUsingForm method
  1251. *
  1252. * @return void
  1253. */
  1254. public function testDisableSecurityUsingForm() {
  1255. $this->Form->request['_Token'] = array(
  1256. 'key' => 'testKey',
  1257. 'disabledFields' => array()
  1258. );
  1259. $this->Form->create();
  1260. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1261. $this->Form->input('Addresses.title');
  1262. $this->Form->input('Addresses.first_name', array('secure' => false));
  1263. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1264. $this->Form->input('Addresses.zip', array(
  1265. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1266. ));
  1267. $result = $this->Form->fields;
  1268. $expected = array(
  1269. 'Addresses.id' => '123456', 'Addresses.title',
  1270. );
  1271. $this->assertEquals($expected, $result);
  1272. }
  1273. /**
  1274. * test disableField
  1275. *
  1276. * @return void
  1277. */
  1278. public function testUnlockFieldAddsToList() {
  1279. $this->Form->request['_Token'] = array(
  1280. 'key' => 'testKey',
  1281. 'unlockedFields' => array()
  1282. );
  1283. $this->Form->create('Contact');
  1284. $this->Form->unlockField('Contact.name');
  1285. $this->Form->text('Contact.name');
  1286. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1287. $this->assertEquals(array(), $this->Form->fields);
  1288. }
  1289. /**
  1290. * test unlockField removing from fields array.
  1291. *
  1292. * @return void
  1293. */
  1294. public function testUnlockFieldRemovingFromFields() {
  1295. $this->Form->request['_Token'] = array(
  1296. 'key' => 'testKey',
  1297. 'unlockedFields' => array()
  1298. );
  1299. $this->Form->create('Contact');
  1300. $this->Form->hidden('Contact.id', array('value' => 1));
  1301. $this->Form->text('Contact.name');
  1302. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1303. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1304. $this->Form->unlockField('Contact.name');
  1305. $this->Form->unlockField('Contact.id');
  1306. $this->assertEquals(array(), $this->Form->fields);
  1307. }
  1308. /**
  1309. * testTagIsInvalid method
  1310. *
  1311. * @return void
  1312. */
  1313. public function testTagIsInvalid() {
  1314. $Contact = ClassRegistry::getObject('Contact');
  1315. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1316. $this->Form->setEntity('Contact.0.email');
  1317. $result = $this->Form->tagIsInvalid();
  1318. $this->assertEquals($expected, $result);
  1319. $this->Form->setEntity('Contact.1.email');
  1320. $result = $this->Form->tagIsInvalid();
  1321. $this->assertFalse($result);
  1322. $this->Form->setEntity('Contact.0.name');
  1323. $result = $this->Form->tagIsInvalid();
  1324. $this->assertFalse($result);
  1325. }
  1326. /**
  1327. * Test tagIsInvalid with validation errors from a saveMany
  1328. *
  1329. * @return void
  1330. */
  1331. public function testTagIsInvalidSaveMany() {
  1332. $Contact = ClassRegistry::getObject('Contact');
  1333. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1334. $this->Form->create('Contact');
  1335. $this->Form->setEntity('0.email');
  1336. $result = $this->Form->tagIsInvalid();
  1337. $this->assertEquals($expected, $result);
  1338. $this->Form->setEntity('0.Contact.email');
  1339. $result = $this->Form->tagIsInvalid();
  1340. $this->assertEquals($expected, $result);
  1341. }
  1342. /**
  1343. * Test validation errors.
  1344. *
  1345. * @return void
  1346. */
  1347. public function testPasswordValidation() {
  1348. $Contact = ClassRegistry::getObject('Contact');
  1349. $Contact->validationErrors['password'] = array('Please provide a password');
  1350. $result = $this->Form->input('Contact.password');
  1351. $expected = array(
  1352. 'div' => array('class' => 'input password error'),
  1353. 'label' => array('for' => 'ContactPassword'),
  1354. 'Password',
  1355. '/label',
  1356. 'input' => array(
  1357. 'type' => 'password', 'name' => 'data[Contact][password]',
  1358. 'id' => 'ContactPassword', 'class' => 'form-error'
  1359. ),
  1360. array('div' => array('class' => 'error-message')),
  1361. 'Please provide a password',
  1362. '/div',
  1363. '/div'
  1364. );
  1365. $this->assertTags($result, $expected);
  1366. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1367. $expected = array(
  1368. 'div' => array('class' => 'input password error'),
  1369. 'label' => array('for' => 'ContactPassword'),
  1370. 'Password',
  1371. '/label',
  1372. 'input' => array(
  1373. 'type' => 'password', 'name' => 'data[Contact][password]',
  1374. 'id' => 'ContactPassword', 'class' => 'form-error'
  1375. ),
  1376. '/div'
  1377. );
  1378. $this->assertTags($result, $expected);
  1379. }
  1380. /**
  1381. * Test validation errors, when validation message is an empty string.
  1382. *
  1383. * @return void
  1384. */
  1385. public function testEmptyErrorValidation() {
  1386. $this->Form->validationErrors['Contact']['password'] = '';
  1387. $result = $this->Form->input('Contact.password');
  1388. $expected = array(
  1389. 'div' => array('class' => 'input password error'),
  1390. 'label' => array('for' => 'ContactPassword'),
  1391. 'Password',
  1392. '/label',
  1393. 'input' => array(
  1394. 'type' => 'password', 'name' => 'data[Contact][password]',
  1395. 'id' => 'ContactPassword', 'class' => 'form-error'
  1396. ),
  1397. array('div' => array('class' => 'error-message')),
  1398. array(),
  1399. '/div',
  1400. '/div'
  1401. );
  1402. $this->assertTags($result, $expected);
  1403. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1404. $expected = array(
  1405. 'div' => array('class' => 'input password error'),
  1406. 'label' => array('for' => 'ContactPassword'),
  1407. 'Password',
  1408. '/label',
  1409. 'input' => array(
  1410. 'type' => 'password', 'name' => 'data[Contact][password]',
  1411. 'id' => 'ContactPassword', 'class' => 'form-error'
  1412. ),
  1413. '/div'
  1414. );
  1415. $this->assertTags($result, $expected);
  1416. }
  1417. /**
  1418. * Test validation errors, when calling input() overriding validation message by an empty string.
  1419. *
  1420. * @return void
  1421. */
  1422. public function testEmptyInputErrorValidation() {
  1423. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1424. $result = $this->Form->input('Contact.password', array('error' => ''));
  1425. $expected = array(
  1426. 'div' => array('class' => 'input password error'),
  1427. 'label' => array('for' => 'ContactPassword'),
  1428. 'Password',
  1429. '/label',
  1430. 'input' => array(
  1431. 'type' => 'password', 'name' => 'data[Contact][password]',
  1432. 'id' => 'ContactPassword', 'class' => 'form-error'
  1433. ),
  1434. array('div' => array('class' => 'error-message')),
  1435. array(),
  1436. '/div',
  1437. '/div'
  1438. );
  1439. $this->assertTags($result, $expected);
  1440. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1441. $expected = array(
  1442. 'div' => array('class' => 'input password error'),
  1443. 'label' => array('for' => 'ContactPassword'),
  1444. 'Password',
  1445. '/label',
  1446. 'input' => array(
  1447. 'type' => 'password', 'name' => 'data[Contact][password]',
  1448. 'id' => 'ContactPassword', 'class' => 'form-error'
  1449. ),
  1450. '/div'
  1451. );
  1452. $this->assertTags($result, $expected);
  1453. }
  1454. /**
  1455. * testFormValidationAssociated method
  1456. *
  1457. * test display of form errors in conjunction with model::validates.
  1458. *
  1459. * @return void
  1460. */
  1461. public function testFormValidationAssociated() {
  1462. $this->UserForm = ClassRegistry::getObject('UserForm');
  1463. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1464. $data = array(
  1465. 'UserForm' => array('name' => 'user'),
  1466. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1467. );
  1468. $result = $this->UserForm->OpenidUrl->create($data);
  1469. $this->assertFalse(empty($result));
  1470. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1471. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1472. $encoding = strtolower(Configure::read('App.encoding'));
  1473. $expected = array(
  1474. 'form' => array(
  1475. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1476. 'accept-charset' => $encoding
  1477. ),
  1478. 'div' => array('style' => 'display:none;'),
  1479. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1480. '/div'
  1481. );
  1482. $this->assertTags($result, $expected);
  1483. $result = $this->Form->error(
  1484. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1485. );
  1486. $this->assertEquals('Error, not registered', $result);
  1487. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1488. }
  1489. /**
  1490. * testFormValidationAssociatedFirstLevel method
  1491. *
  1492. * test form error display with associated model.
  1493. *
  1494. * @return void
  1495. */
  1496. public function testFormValidationAssociatedFirstLevel() {
  1497. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1498. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1499. $data = array(
  1500. 'ValidateUser' => array('name' => 'mariano'),
  1501. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1502. );
  1503. $result = $this->ValidateUser->create($data);
  1504. $this->assertFalse(empty($result));
  1505. $this->assertFalse($this->ValidateUser->validates());
  1506. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1507. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1508. $encoding = strtolower(Configure::read('App.encoding'));
  1509. $expected = array(
  1510. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1511. 'div' => array('style' => 'display:none;'),
  1512. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1513. '/div'
  1514. );
  1515. $this->assertTags($result, $expected);
  1516. $result = $this->Form->error(
  1517. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1518. );
  1519. $this->assertEquals('Invalid email', $result);
  1520. $result = $this->Form->error(
  1521. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1522. );
  1523. $this->assertEquals('Invalid name', $result);
  1524. $result = $this->Form->error(
  1525. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1526. );
  1527. $this->assertEquals('Invalid city', $result);
  1528. unset($this->ValidateUser->ValidateProfile);
  1529. unset($this->ValidateUser);
  1530. }
  1531. /**
  1532. * testFormValidationAssociatedSecondLevel method
  1533. *
  1534. * test form error display with associated model.
  1535. *
  1536. * @return void
  1537. */
  1538. public function testFormValidationAssociatedSecondLevel() {
  1539. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1540. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1541. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1542. $data = array(
  1543. 'ValidateUser' => array('name' => 'mariano'),
  1544. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1545. 'ValidateItem' => array('name' => 'Item')
  1546. );
  1547. $result = $this->ValidateUser->create($data);
  1548. $this->assertFalse(empty($result));
  1549. $this->assertFalse($this->ValidateUser->validates());
  1550. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1551. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1552. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1553. $encoding = strtolower(Configure::read('App.encoding'));
  1554. $expected = array(
  1555. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1556. 'div' => array('style' => 'display:none;'),
  1557. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1558. '/div'
  1559. );
  1560. $this->assertTags($result, $expected);
  1561. $result = $this->Form->error(
  1562. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1563. );
  1564. $this->assertEquals('Invalid email', $result);
  1565. $result = $this->Form->error(
  1566. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1567. );
  1568. $this->assertEquals('Invalid name', $result);
  1569. $result = $this->Form->error(
  1570. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1571. );
  1572. $result = $this->Form->error(
  1573. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1574. );
  1575. $this->assertEquals('Invalid description', $result);
  1576. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1577. unset($this->ValidateUser->ValidateProfile);
  1578. unset($this->ValidateUser);
  1579. }
  1580. /**
  1581. * testFormValidationMultiRecord method
  1582. *
  1583. * test form error display with multiple records.
  1584. *
  1585. * @return void
  1586. */
  1587. public function testFormValidationMultiRecord() {
  1588. $Contact = ClassRegistry::getObject('Contact');
  1589. $Contact->validationErrors[2] = array(
  1590. 'name' => array('This field cannot be left blank')
  1591. );
  1592. $result = $this->Form->input('Contact.2.name');
  1593. $expected = array(
  1594. 'div' => array('class' => 'input text error'),
  1595. 'label' => array('for' => 'Contact2Name'),
  1596. 'Name',
  1597. '/label',
  1598. 'input' => array(
  1599. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1600. 'class' => 'form-error', 'maxlength' => 255
  1601. ),
  1602. array('div' => array('class' => 'error-message')),
  1603. 'This field cannot be left blank',
  1604. '/div',
  1605. '/div'
  1606. );
  1607. $this->assertTags($result, $expected);
  1608. }
  1609. /**
  1610. * testMultipleInputValidation method
  1611. *
  1612. * test multiple record form validation error display.
  1613. *
  1614. * @return void
  1615. */
  1616. public function testMultipleInputValidation() {
  1617. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1618. $Address->validationErrors[0] = array(
  1619. 'title' => array('This field cannot be empty'),
  1620. 'first_name' => array('This field cannot be empty')
  1621. );
  1622. $Address->validationErrors[1] = array(
  1623. 'last_name' => array('You must have a last name')
  1624. );
  1625. $this->Form->create();
  1626. $result = $this->Form->input('Address.0.title');
  1627. $expected = array(
  1628. 'div' => array('class'),
  1629. 'label' => array('for'),
  1630. 'preg:/[^<]+/',
  1631. '/label',
  1632. 'input' => array(
  1633. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1634. ),
  1635. array('div' => array('class' => 'error-message')),
  1636. 'This field cannot be empty',
  1637. '/div',
  1638. '/div'
  1639. );
  1640. $this->assertTags($result, $expected);
  1641. $result = $this->Form->input('Address.0.first_name');
  1642. $expected = array(
  1643. 'div' => array('class'),
  1644. 'label' => array('for'),
  1645. 'preg:/[^<]+/',
  1646. '/label',
  1647. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1648. array('div' => array('class' => 'error-message')),
  1649. 'This field cannot be empty',
  1650. '/div',
  1651. '/div'
  1652. );
  1653. $this->assertTags($result, $expected);
  1654. $result = $this->Form->input('Address.0.last_name');
  1655. $expected = array(
  1656. 'div' => array('class'),
  1657. 'label' => array('for'),
  1658. 'preg:/[^<]+/',
  1659. '/label',
  1660. 'input' => array('type' => 'text', 'name', 'id'),
  1661. '/div'
  1662. );
  1663. $this->assertTags($result, $expected);
  1664. $result = $this->Form->input('Address.1.last_name');
  1665. $expected = array(
  1666. 'div' => array('class'),
  1667. 'label' => array('for'),
  1668. 'preg:/[^<]+/',
  1669. '/label',
  1670. 'input' => array(
  1671. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1672. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1673. ),
  1674. array('div' => array('class' => 'error-message')),
  1675. 'You must have a last name',
  1676. '/div',
  1677. '/div'
  1678. );
  1679. $this->assertTags($result, $expected);
  1680. }
  1681. /**
  1682. * testInput method
  1683. *
  1684. * Test various incarnations of input().
  1685. *
  1686. * @return void
  1687. */
  1688. public function testInput() {
  1689. $result = $this->Form->input('ValidateUser.balance');
  1690. $expected = array(
  1691. 'div' => array('class'),
  1692. 'label' => array('for'),
  1693. 'Balance',
  1694. '/label',
  1695. 'input' => array('name', 'type' => 'number', 'id'),
  1696. '/div',
  1697. );
  1698. $this->assertTags($result, $expected);
  1699. $result = $this->Form->input('ValidateUser.cost_decimal');
  1700. $expected = array(
  1701. 'div' => array('class'),
  1702. 'label' => array('for'),
  1703. 'Cost Decimal',
  1704. '/label',
  1705. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  1706. '/div',
  1707. );
  1708. $this->assertTags($result, $expected);
  1709. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1710. $expected = array(
  1711. 'div' => array('class' => 'input email'),
  1712. 'label' => array('for' => 'custom'),
  1713. 'Email',
  1714. '/label',
  1715. array('input' => array(
  1716. 'type' => 'email', 'name' => 'data[Contact][email]',
  1717. 'id' => 'custom', 'maxlength' => 255
  1718. )),
  1719. '/div'
  1720. );
  1721. $this->assertTags($result, $expected);
  1722. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1723. $expected = array(
  1724. '<div',
  1725. 'label' => array('for' => 'ContactEmail'),
  1726. 'Email',
  1727. '/label',
  1728. array('input' => array(
  1729. 'type' => 'email', 'name' => 'data[Contact][email]',
  1730. 'id' => 'ContactEmail', 'maxlength' => 255
  1731. )),
  1732. '/div'
  1733. );
  1734. $this->assertTags($result, $expected);
  1735. $result = $this->Form->hidden('Contact.idontexist');
  1736. $expected = array('input' => array(
  1737. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1738. 'id' => 'ContactIdontexist'
  1739. ));
  1740. $this->assertTags($result, $expected);
  1741. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1742. $expected = array(
  1743. 'div' => array('class' => 'input text'),
  1744. 'label' => array('for' => 'ContactEmail'),
  1745. 'Email',
  1746. '/label',
  1747. array('input' => array(
  1748. 'type' => 'text', 'name' => 'data[Contact][email]',
  1749. 'id' => 'ContactEmail'
  1750. )),
  1751. '/div'
  1752. );
  1753. $this->assertTags($result, $expected);
  1754. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1755. $expected = array(
  1756. 'div' => array('class' => 'input text'),
  1757. 'label' => array('for' => 'Contact5Email'),
  1758. 'Email',
  1759. '/label',
  1760. array('input' => array(
  1761. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1762. 'id' => 'Contact5Email'
  1763. )),
  1764. '/div'
  1765. );
  1766. $this->assertTags($result, $expected);
  1767. $result = $this->Form->input('Contact.password');
  1768. $expected = array(
  1769. 'div' => array('class' => 'input password'),
  1770. 'label' => array('for' => 'ContactPassword'),
  1771. 'Password',
  1772. '/label',
  1773. array('input' => array(
  1774. 'type' => 'password', 'name' => 'data[Contact][password]',
  1775. 'id' => 'ContactPassword'
  1776. )),
  1777. '/div'
  1778. );
  1779. $this->assertTags($result, $expected);
  1780. $result = $this->Form->input('Contact.email', array(
  1781. 'type' => 'file', 'class' => 'textbox'
  1782. ));
  1783. $expected = array(
  1784. 'div' => array('class' => 'input file'),
  1785. 'label' => array('for' => 'ContactEmail'),
  1786. 'Email',
  1787. '/label',
  1788. array('input' => array(
  1789. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1790. 'id' => 'ContactEmail'
  1791. )),
  1792. '/div'
  1793. );
  1794. $this->assertTags($result, $expected);
  1795. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1796. $result = $this->Form->input('Contact.phone');
  1797. $expected = array(
  1798. 'div' => array('class' => 'input tel'),
  1799. 'label' => array('for' => 'ContactPhone'),
  1800. 'Phone',
  1801. '/label',
  1802. array('input' => array(
  1803. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1804. 'value' => 'Hello &amp; World &gt; weird chars',
  1805. 'id' => 'ContactPhone', 'maxlength' => 255
  1806. )),
  1807. '/div'
  1808. );
  1809. $this->assertTags($result, $expected);
  1810. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1811. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1812. $expected = array(
  1813. 'div' => array('class' => 'input text'),
  1814. 'label' => array('for' => 'myId'),
  1815. 'Field',
  1816. '/label',
  1817. 'input' => array(
  1818. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1819. 'value' => 'My value', 'id' => 'myId'
  1820. ),
  1821. '/div'
  1822. );
  1823. $this->assertTags($result, $expected);
  1824. unset($this->Form->request->data);
  1825. $Contact = ClassRegistry::getObject('Contact');
  1826. $Contact->validationErrors['field'] = array('Badness!');
  1827. $result = $this->Form->input('Contact.field');
  1828. $expected = array(
  1829. 'div' => array('class' => 'input text error'),
  1830. 'label' => array('for' => 'ContactField'),
  1831. 'Field',
  1832. '/label',
  1833. 'input' => array(
  1834. 'type' => 'text', 'name' => 'data[Contact][field]',
  1835. 'id' => 'ContactField', 'class' => 'form-error'
  1836. ),
  1837. array('div' => array('class' => 'error-message')),
  1838. 'Badness!',
  1839. '/div',
  1840. '/div'
  1841. );
  1842. $this->assertTags($result, $expected);
  1843. $result = $this->Form->input('Contact.field', array(
  1844. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1845. ));
  1846. $expected = array(
  1847. 'label' => array('for' => 'ContactField'),
  1848. 'Field',
  1849. '/label',
  1850. 'input' => array(
  1851. 'type' => 'text', 'name' => 'data[Contact][field]',
  1852. 'id' => 'ContactField', 'class' => 'form-error'
  1853. ),
  1854. array('span' => array('class' => 'error-message')),
  1855. 'Badness!',
  1856. '/span'
  1857. );
  1858. $this->assertTags($result, $expected);
  1859. $result = $this->Form->input('Contact.field', array(
  1860. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1861. ));
  1862. $expected = array(
  1863. 'div' => array('class' => 'input text error'),
  1864. 'label' => array('for' => 'ContactField'),
  1865. 'Field',
  1866. '/label',
  1867. 'input' => array(
  1868. 'type' => 'text', 'name' => 'data[Contact][field]',
  1869. 'id' => 'ContactField', 'class' => 'form-error'
  1870. ),
  1871. array('div' => array('class' => 'error')),
  1872. 'Badness!',
  1873. '/div'
  1874. );
  1875. $this->assertTags($result, $expected);
  1876. $result = $this->Form->input('Contact.field', array(
  1877. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1878. ));
  1879. $expected = array(
  1880. 'span' => array('class' => 'input text error'),
  1881. 'label' => array('for' => 'ContactField'),
  1882. 'Field',
  1883. '/label',
  1884. 'input' => array(
  1885. 'type' => 'text', 'name' => 'data[Contact][field]',
  1886. 'id' => 'ContactField', 'class' => 'form-error'
  1887. ),
  1888. 'Badness!',
  1889. '/span'
  1890. );
  1891. $this->assertTags($result, $expected);
  1892. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1893. $expected = array(
  1894. 'div' => array('class' => 'input text error'),
  1895. 'label' => array('for' => 'ContactField'),
  1896. 'Field',
  1897. '/label',
  1898. 'input' => array(
  1899. 'type' => 'text', 'name' => 'data[Contact][field]',
  1900. 'id' => 'ContactField', 'class' => 'form-error'
  1901. ),
  1902. 'A message to you, Rudy',
  1903. array('div' => array('class' => 'error-message')),
  1904. 'Badness!',
  1905. '/div',
  1906. '/div'
  1907. );
  1908. $this->assertTags($result, $expected);
  1909. $this->Form->setEntity(null);
  1910. $this->Form->setEntity('Contact.field');
  1911. $result = $this->Form->input('Contact.field', array(
  1912. 'after' => 'A message to you, Rudy', 'error' => false
  1913. ));
  1914. $expected = array(
  1915. 'div' => array('class' => 'input text'),
  1916. 'label' => array('for' => 'ContactField'),
  1917. 'Field',
  1918. '/label',
  1919. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1920. 'A message to you, Rudy',
  1921. '/div'
  1922. );
  1923. $this->assertTags($result, $expected);
  1924. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1925. $expected = array(
  1926. 'div' => array('class' => 'input text'),
  1927. 'label' => array('for' => 'ObjectField'),
  1928. 'Field',
  1929. '/label',
  1930. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1931. 'A message to you, Rudy',
  1932. '/div'
  1933. );
  1934. $this->assertTags($result, $expected);
  1935. $Contact->validationErrors['field'] = array('minLength');
  1936. $result = $this->Form->input('Contact.field', array(
  1937. 'error' => array(
  1938. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1939. 'maxLength' => 'login too large'
  1940. )
  1941. ));
  1942. $expected = array(
  1943. 'div' => array('class' => 'input text error'),
  1944. 'label' => array('for' => 'ContactField'),
  1945. 'Field',
  1946. '/label',
  1947. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1948. array('div' => array('class' => 'error-message')),
  1949. 'Le login doit contenir au moins 2 caractères',
  1950. '/div',
  1951. '/div'
  1952. );
  1953. $this->assertTags($result, $expected);
  1954. $Contact->validationErrors['field'] = array('maxLength');
  1955. $result = $this->Form->input('Contact.field', array(
  1956. 'error' => array(
  1957. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1958. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1959. 'maxLength' => 'login too large',
  1960. )
  1961. ));
  1962. $expected = array(
  1963. 'div' => array('class' => 'input text error'),
  1964. 'label' => array('for' => 'ContactField'),
  1965. 'Field',
  1966. '/label',
  1967. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1968. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1969. 'login too large',
  1970. '/span',
  1971. '/div'
  1972. );
  1973. $this->assertTags($result, $expected);
  1974. }
  1975. /**
  1976. * Test that inputs with 0 can be created.
  1977. *
  1978. * @return void
  1979. */
  1980. public function testInputZero() {
  1981. $this->Form->create('User');
  1982. $result = $this->Form->input('0');
  1983. $expected = array(
  1984. 'div' => array('class' => 'input text'),
  1985. 'label' => array('for' => 'User0'), '/label',
  1986. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1987. '/div'
  1988. );
  1989. $this->assertTags($result, $expected);
  1990. }
  1991. /**
  1992. * test input() with checkbox creation
  1993. *
  1994. * @return void
  1995. */
  1996. public function testInputCheckbox() {
  1997. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1998. $expected = array(
  1999. 'div' => array('class' => 'input checkbox'),
  2000. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2001. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2002. '/div'
  2003. );
  2004. $this->assertTags($result, $expected);
  2005. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2006. $expected = array(
  2007. 'div' => array('class' => 'input checkbox'),
  2008. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2009. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2010. '/div'
  2011. );
  2012. $this->assertTags($result, $expected);
  2013. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2014. $expected = array(
  2015. 'div' => array('class' => 'input checkbox'),
  2016. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2017. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2018. '/div'
  2019. );
  2020. $this->assertTags($result, $expected);
  2021. $result = $this->Form->input('User.disabled', array(
  2022. 'label' => 'Disabled',
  2023. 'type' => 'checkbox',
  2024. 'data-foo' => 'disabled'
  2025. ));
  2026. $expected = array(
  2027. 'div' => array('class' => 'input checkbox'),
  2028. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  2029. array('input' => array(
  2030. 'type' => 'checkbox',
  2031. 'name' => 'data[User][disabled]',
  2032. 'value' => '1',
  2033. 'id' => 'UserDisabled',
  2034. 'data-foo' => 'disabled'
  2035. )),
  2036. 'label' => array('for' => 'UserDisabled'),
  2037. 'Disabled',
  2038. '/label',
  2039. '/div'
  2040. );
  2041. $this->assertTags($result, $expected);
  2042. }
  2043. /**
  2044. * test form->input() with time types.
  2045. *
  2046. */
  2047. public function testInputTime() {
  2048. extract($this->dateRegex);
  2049. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2050. $result = explode(':', $result);
  2051. $this->assertRegExp('/option value="23"/', $result[0]);
  2052. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2053. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2054. $result = explode(':', $result);
  2055. $this->assertRegExp('/option value="23"/', $result[0]);
  2056. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2057. $result = $this->Form->input('Model.field', array(
  2058. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2059. ));
  2060. $result = explode(':', $result);
  2061. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2062. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2063. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2064. $result = $this->Form->input('Model.field', array(
  2065. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2066. ));
  2067. $result = explode(':', $result);
  2068. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2069. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2070. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2071. $result = $this->Form->input('prueba', array(
  2072. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2073. 'maxYear' => date('Y') + 1, 'interval' => 15
  2074. ));
  2075. $result = explode(':', $result);
  2076. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2077. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2078. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2079. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2080. $result = $this->Form->input('Random.start_time', array(
  2081. 'type' => 'time',
  2082. 'selected' => '18:15'
  2083. ));
  2084. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2085. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2086. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2087. $result = $this->Form->input('published', array('type' => 'time'));
  2088. $now = strtotime('now');
  2089. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2090. $now = strtotime('2013-03-09 00:42:21');
  2091. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2092. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2093. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2094. }
  2095. /**
  2096. * Test interval + selected near the hour roll over.
  2097. *
  2098. * @return void
  2099. */
  2100. public function testTimeSelectedWithInterval() {
  2101. $result = $this->Form->input('Model.start_time', array(
  2102. 'type' => 'time',
  2103. 'interval' => 15,
  2104. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2105. ));
  2106. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2107. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2108. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2109. $result = $this->Form->input('Model.start_time', array(
  2110. 'type' => 'time',
  2111. 'interval' => 15,
  2112. 'selected' => '2012-10-23 15:57:00'
  2113. ));
  2114. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2115. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2116. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2117. $result = $this->Form->input('Model.start_time', array(
  2118. 'timeFormat' => 24,
  2119. 'type' => 'time',
  2120. 'interval' => 15,
  2121. 'selected' => '15:57'
  2122. ));
  2123. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2124. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2125. $result = $this->Form->input('Model.start_time', array(
  2126. 'timeFormat' => 24,
  2127. 'type' => 'time',
  2128. 'interval' => 15,
  2129. 'selected' => '23:57'
  2130. ));
  2131. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2132. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2133. $result = $this->Form->input('Model.created', array(
  2134. 'timeFormat' => 24,
  2135. 'type' => 'datetime',
  2136. 'interval' => 15,
  2137. 'selected' => '2012-09-30 23:56'
  2138. ));
  2139. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2140. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2141. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2142. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2143. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2144. }
  2145. /**
  2146. * Test interval & timeFormat = 12
  2147. *
  2148. * @return void
  2149. */
  2150. public function testInputTimeWithIntervalAnd12HourFormat() {
  2151. $result = $this->Form->input('Model.start_time', array(
  2152. 'type' => 'time',
  2153. 'timeFormat' => 12,
  2154. 'interval' => 5,
  2155. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2156. ));
  2157. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2158. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2159. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2160. $result = $this->Form->input('Model.start_time', array(
  2161. 'type' => 'time',
  2162. 'timeFormat' => '12',
  2163. 'interval' => 5,
  2164. 'selected' => '2013-04-19 16:30:00'
  2165. ));
  2166. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2167. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2168. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2169. $result = $this->Form->input('Model.start_time', array(
  2170. 'type' => 'time',
  2171. 'timeFormat' => '12',
  2172. 'interval' => 10,
  2173. 'selected' => '2013-05-19 00:33:00'
  2174. ));
  2175. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2176. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2177. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2178. $result = $this->Form->input('Model.start_time', array(
  2179. 'type' => 'time',
  2180. 'timeFormat' => '12',
  2181. 'interval' => 10,
  2182. 'selected' => '2013-05-19 13:33:00'
  2183. ));
  2184. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2185. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2186. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2187. $result = $this->Form->input('Model.start_time', array(
  2188. 'type' => 'time',
  2189. 'timeFormat' => '12',
  2190. 'interval' => 10,
  2191. 'selected' => '2013-05-19 01:33:00'
  2192. ));
  2193. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2194. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2195. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2196. }
  2197. /**
  2198. * test form->input() with datetime, date and time types
  2199. *
  2200. * @return void
  2201. */
  2202. public function testInputDatetime() {
  2203. extract($this->dateRegex);
  2204. $result = $this->Form->input('prueba', array(
  2205. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2206. 'maxYear' => date('Y') + 1, 'interval' => 15
  2207. ));
  2208. $result = explode(':', $result);
  2209. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2210. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2211. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2212. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2213. //related to ticket #5013
  2214. $result = $this->Form->input('Contact.date', array(
  2215. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2216. ));
  2217. $this->assertRegExp('/class="customClass"/', $result);
  2218. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2219. $result = $this->Form->input('Contact.date', array(
  2220. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2221. ));
  2222. $this->assertRegExp('/id="customIdDay"/', $result);
  2223. $this->assertRegExp('/id="customIdMonth"/', $result);
  2224. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2225. $result = $this->Form->input('Model.field', array(
  2226. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2227. ));
  2228. $this->assertRegExp('/id="customIDDay"/', $result);
  2229. $this->assertRegExp('/id="customIDHour"/', $result);
  2230. $result = explode('</select><select', $result);
  2231. $result = explode(':', $result[1]);
  2232. $this->assertRegExp('/option value="23"/', $result[0]);
  2233. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2234. $result = $this->Form->input('Model.field', array(
  2235. 'type' => 'datetime', 'timeFormat' => 12
  2236. ));
  2237. $result = explode('</select><select', $result);
  2238. $result = explode(':', $result[1]);
  2239. $this->assertRegExp('/option value="12"/', $result[0]);
  2240. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2241. $this->Form->request->data = array('Contact' => array('created' => null));
  2242. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2243. $expected = array(
  2244. 'div' => array('class' => 'input date'),
  2245. 'label' => array('for' => 'ContactCreatedMonth'),
  2246. 'Created',
  2247. '/label',
  2248. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2249. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2250. $monthsRegex,
  2251. '/select', '-',
  2252. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2253. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2254. $daysRegex,
  2255. '/select', '-',
  2256. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2257. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2258. $yearsRegex,
  2259. '/select',
  2260. '/div'
  2261. );
  2262. $this->assertTags($result, $expected);
  2263. $this->Form->request->data = array('Contact' => array('created' => null));
  2264. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2265. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2266. $this->Form->request->data = array('Contact' => array('created' => null));
  2267. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2268. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2269. $result = $this->Form->input('Contact.created', array(
  2270. 'type' => 'date',
  2271. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2272. 'timeFormat' => 'NONE'
  2273. ));
  2274. $this->assertRegExp('/for\="created-month"/', $result);
  2275. }
  2276. /**
  2277. * Test generating checkboxes in a loop.
  2278. *
  2279. * @return void
  2280. */
  2281. public function testInputCheckboxesInLoop() {
  2282. for ($i = 1; $i < 5; $i++) {
  2283. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2284. $expected = array(
  2285. 'div' => array('class' => 'input checkbox'),
  2286. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2287. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2288. 'label' => array('for' => "Contact{$i}Email"),
  2289. 'Email',
  2290. '/label',
  2291. '/div'
  2292. );
  2293. $this->assertTags($result, $expected);
  2294. }
  2295. }
  2296. /**
  2297. * Test generating checkboxes with disabled elements.
  2298. *
  2299. * @return void
  2300. */
  2301. public function testInputCheckboxWithDisabledElements() {
  2302. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2303. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2304. $expected = array(
  2305. array('div' => array('class' => 'input select')),
  2306. array('label' => array('for' => "ContactMultiple")),
  2307. 'Multiple',
  2308. '/label',
  2309. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2310. array('div' => array('class' => 'checkbox')),
  2311. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2312. array('label' => array('for' => "ContactMultiple1")),
  2313. 'One',
  2314. '/label',
  2315. '/div',
  2316. array('div' => array('class' => 'checkbox')),
  2317. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2318. array('label' => array('for' => "ContactMultiple2")),
  2319. 'Two',
  2320. '/label',
  2321. '/div',
  2322. array('div' => array('class' => 'checkbox')),
  2323. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2324. array('label' => array('for' => "ContactMultiple3")),
  2325. 'Three',
  2326. '/label',
  2327. '/div',
  2328. '/div'
  2329. );
  2330. $this->assertTags($result, $expected);
  2331. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2332. $this->assertTags($result, $expected);
  2333. $disabled = array('2', 3);
  2334. $expected = array(
  2335. array('div' => array('class' => 'input select')),
  2336. array('label' => array('for' => "ContactMultiple")),
  2337. 'Multiple',
  2338. '/label',
  2339. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2340. array('div' => array('class' => 'checkbox')),
  2341. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2342. array('label' => array('for' => "ContactMultiple1")),
  2343. 'One',
  2344. '/label',
  2345. '/div',
  2346. array('div' => array('class' => 'checkbox')),
  2347. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2348. array('label' => array('for' => "ContactMultiple2")),
  2349. 'Two',
  2350. '/label',
  2351. '/div',
  2352. array('div' => array('class' => 'checkbox')),
  2353. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2354. array('label' => array('for' => "ContactMultiple3")),
  2355. 'Three',
  2356. '/label',
  2357. '/div',
  2358. '/div'
  2359. );
  2360. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2361. $this->assertTags($result, $expected);
  2362. // make sure 50 does only disable 50, and not 50f5c0cf
  2363. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2364. $disabled = array(50);
  2365. $expected = array(
  2366. array('div' => array('class' => 'input select')),
  2367. array('label' => array('for' => "ContactMultiple")),
  2368. 'Multiple',
  2369. '/label',
  2370. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2371. array('div' => array('class' => 'checkbox')),
  2372. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2373. array('label' => array('for' => "ContactMultiple50")),
  2374. 'Fifty',
  2375. '/label',
  2376. '/div',
  2377. array('div' => array('class' => 'checkbox')),
  2378. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2379. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2380. 'Stringy',
  2381. '/label',
  2382. '/div',
  2383. '/div'
  2384. );
  2385. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2386. $this->assertTags($result, $expected);
  2387. }
  2388. /**
  2389. * test input name with leading integer, ensure attributes are generated correctly.
  2390. *
  2391. * @return void
  2392. */
  2393. public function testInputWithLeadingInteger() {
  2394. $result = $this->Form->text('0.Node.title');
  2395. $expected = array(
  2396. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2397. );
  2398. $this->assertTags($result, $expected);
  2399. }
  2400. /**
  2401. * test form->input() with select type inputs.
  2402. *
  2403. * @return void
  2404. */
  2405. public function testInputSelectType() {
  2406. $result = $this->Form->input('email', array(
  2407. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2408. );
  2409. $expected = array(
  2410. 'div' => array('class' => 'input select'),
  2411. 'label' => array('for' => 'email'),
  2412. 'Email',
  2413. '/label',
  2414. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2415. array('option' => array('value' => '')),
  2416. '/option',
  2417. array('option' => array('value' => 'è')),
  2418. 'Firést',
  2419. '/option',
  2420. array('option' => array('value' => 'é')),
  2421. 'Secoènd',
  2422. '/option',
  2423. '/select',
  2424. '/div'
  2425. );
  2426. $this->assertTags($result, $expected);
  2427. $result = $this->Form->input('email', array(
  2428. 'options' => array('First', 'Second'), 'empty' => true)
  2429. );
  2430. $expected = array(
  2431. 'div' => array('class' => 'input select'),
  2432. 'label' => array('for' => 'email'),
  2433. 'Email',
  2434. '/label',
  2435. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2436. array('option' => array('value' => '')),
  2437. '/option',
  2438. array('option' => array('value' => '0')),
  2439. 'First',
  2440. '/option',
  2441. array('option' => array('value' => '1')),
  2442. 'Second',
  2443. '/option',
  2444. '/select',
  2445. '/div'
  2446. );
  2447. $this->assertTags($result, $expected);
  2448. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2449. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2450. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2451. $expected = array(
  2452. 'div' => array('class' => 'input select'),
  2453. 'label' => array('for' => 'ModelUserId'),
  2454. 'User',
  2455. '/label',
  2456. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2457. array('option' => array('value' => '')),
  2458. '/option',
  2459. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2460. 'good',
  2461. '/option',
  2462. array('option' => array('value' => 'other')),
  2463. 'bad',
  2464. '/option',
  2465. '/select',
  2466. '/div'
  2467. );
  2468. $this->assertTags($result, $expected);
  2469. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2470. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2471. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2472. $expected = array(
  2473. 'div' => array('class' => 'input select'),
  2474. 'label' => array('for' => 'ThingUserId'),
  2475. 'User',
  2476. '/label',
  2477. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2478. array('option' => array('value' => '')),
  2479. 'Some Empty',
  2480. '/option',
  2481. array('option' => array('value' => 'value')),
  2482. 'good',
  2483. '/option',
  2484. array('option' => array('value' => 'other')),
  2485. 'bad',
  2486. '/option',
  2487. '/select',
  2488. '/div'
  2489. );
  2490. $this->assertTags($result, $expected);
  2491. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2492. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2493. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2494. $expected = array(
  2495. 'div' => array('class' => 'input select'),
  2496. 'label' => array('for' => 'ThingUserId'),
  2497. 'User',
  2498. '/label',
  2499. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2500. array('option' => array('value' => '')),
  2501. 'Some Empty',
  2502. '/option',
  2503. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2504. 'good',
  2505. '/option',
  2506. array('option' => array('value' => 'other')),
  2507. 'bad',
  2508. '/option',
  2509. '/select',
  2510. '/div'
  2511. );
  2512. $this->assertTags($result, $expected);
  2513. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2514. $this->Form->request->data = array('User' => array('User' => array('value')));
  2515. $result = $this->Form->input('User.User', array('empty' => true));
  2516. $expected = array(
  2517. 'div' => array('class' => 'input select'),
  2518. 'label' => array('for' => 'UserUser'),
  2519. 'User',
  2520. '/label',
  2521. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2522. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2523. array('option' => array('value' => '')),
  2524. '/option',
  2525. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2526. 'good',
  2527. '/option',
  2528. array('option' => array('value' => 'other')),
  2529. 'bad',
  2530. '/option',
  2531. '/select',
  2532. '/div'
  2533. );
  2534. $this->assertTags($result, $expected);
  2535. $this->Form->data = array();
  2536. $result = $this->Form->input('Publisher.id', array(
  2537. 'label' => 'Publisher',
  2538. 'type' => 'select',
  2539. 'multiple' => 'checkbox',
  2540. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2541. ));
  2542. $expected = array(
  2543. array('div' => array('class' => 'input select')),
  2544. array('label' => array('for' => 'PublisherId')),
  2545. 'Publisher',
  2546. '/label',
  2547. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2548. array('div' => array('class' => 'checkbox')),
  2549. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2550. array('label' => array('for' => 'PublisherIdValue1')),
  2551. 'Label 1',
  2552. '/label',
  2553. '/div',
  2554. array('div' => array('class' => 'checkbox')),
  2555. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2556. array('label' => array('for' => 'PublisherIdValue2')),
  2557. 'Label 2',
  2558. '/label',
  2559. '/div',
  2560. '/div'
  2561. );
  2562. $this->assertTags($result, $expected);
  2563. }
  2564. /**
  2565. * test that input() and a non standard primary key makes a hidden input by default.
  2566. *
  2567. * @return void
  2568. */
  2569. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2570. $this->Form->create('User');
  2571. $this->Form->fieldset = array(
  2572. 'User' => array(
  2573. 'fields' => array(
  2574. 'model_id' => array('type' => 'integer')
  2575. ),
  2576. 'validates' => array(),
  2577. 'key' => 'model_id'
  2578. )
  2579. );
  2580. $result = $this->Form->input('model_id');
  2581. $expected = array(
  2582. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2583. );
  2584. $this->assertTags($result, $expected);
  2585. }
  2586. /**
  2587. * test that overriding the magic select type widget is possible
  2588. *
  2589. * @return void
  2590. */
  2591. public function testInputOverridingMagicSelectType() {
  2592. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2593. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2594. $expected = array(
  2595. 'div' => array('class' => 'input text'),
  2596. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2597. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2598. '/div'
  2599. );
  2600. $this->assertTags($result, $expected);
  2601. //Check that magic types still work for plural/singular vars
  2602. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2603. $result = $this->Form->input('Model.type');
  2604. $expected = array(
  2605. 'div' => array('class' => 'input select'),
  2606. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2607. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2608. array('option' => array('value' => 'value')), 'good', '/option',
  2609. array('option' => array('value' => 'other')), 'bad', '/option',
  2610. '/select',
  2611. '/div'
  2612. );
  2613. $this->assertTags($result, $expected);
  2614. }
  2615. /**
  2616. * Test that magic input() selects are created for type=number
  2617. *
  2618. * @return void
  2619. */
  2620. public function testInputMagicSelectForTypeNumber() {
  2621. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2622. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2623. $result = $this->Form->input('ValidateUser.balance');
  2624. $expected = array(
  2625. 'div' => array('class' => 'input select'),
  2626. 'label' => array('for' => 'ValidateUserBalance'),
  2627. 'Balance',
  2628. '/label',
  2629. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2630. array('option' => array('value' => '0')),
  2631. 'nothing',
  2632. '/option',
  2633. array('option' => array('value' => '1', 'selected' => 'selected')),
  2634. 'some',
  2635. '/option',
  2636. array('option' => array('value' => '100')),
  2637. 'a lot',
  2638. '/option',
  2639. '/select',
  2640. '/div'
  2641. );
  2642. $this->assertTags($result, $expected);
  2643. }
  2644. /**
  2645. * Test that magic input() selects can easily be converted into radio types without error.
  2646. *
  2647. * @return void
  2648. */
  2649. public function testInputMagicSelectChangeToRadio() {
  2650. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2651. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2652. $this->assertRegExp('/input type="radio"/', $result);
  2653. }
  2654. /**
  2655. * fields with the same name as the model should work.
  2656. *
  2657. * @return void
  2658. */
  2659. public function testInputWithMatchingFieldAndModelName() {
  2660. $this->Form->create('User');
  2661. $this->Form->fieldset = array(
  2662. 'User' => array(
  2663. 'fields' => array(
  2664. 'User' => array('type' => 'text')
  2665. ),
  2666. 'validates' => array(),
  2667. 'key' => 'id'
  2668. )
  2669. );
  2670. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2671. $result = $this->Form->input('User', array('type' => 'text'));
  2672. $expected = array(
  2673. 'div' => array('class' => 'input text'),
  2674. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2675. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2676. '/div'
  2677. );
  2678. $this->assertTags($result, $expected);
  2679. }
  2680. /**
  2681. * testFormInputs method
  2682. *
  2683. * test correct results from form::inputs().
  2684. *
  2685. * @return void
  2686. */
  2687. public function testFormInputs() {
  2688. $this->Form->create('Contact');
  2689. $result = $this->Form->inputs('The Legend');
  2690. $expected = array(
  2691. '<fieldset',
  2692. '<legend',
  2693. 'The Legend',
  2694. '/legend',
  2695. '*/fieldset',
  2696. );
  2697. $this->assertTags($result, $expected);
  2698. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2699. $expected = array(
  2700. 'fieldset' => array('class' => 'classy-stuff'),
  2701. '<legend',
  2702. 'Field of Dreams',
  2703. '/legend',
  2704. '*/fieldset'
  2705. );
  2706. $this->assertTags($result, $expected);
  2707. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2708. $this->assertTags($result, $expected);
  2709. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2710. $this->assertTags($result, $expected);
  2711. $this->Form->create('Contact');
  2712. $this->Form->request['prefix'] = 'admin';
  2713. $this->Form->request['action'] = 'admin_edit';
  2714. $result = $this->Form->inputs();
  2715. $expected = array(
  2716. '<fieldset',
  2717. '<legend',
  2718. 'Edit Contact',
  2719. '/legend',
  2720. '*/fieldset',
  2721. );
  2722. $this->assertTags($result, $expected);
  2723. $this->Form->create('Contact');
  2724. $result = $this->Form->inputs(false);
  2725. $expected = array(
  2726. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2727. array('div' => array('class' => 'input text')),
  2728. '*/div',
  2729. array('div' => array('class' => 'input email')),
  2730. '*/div',
  2731. array('div' => array('class' => 'input tel')),
  2732. '*/div',
  2733. array('div' => array('class' => 'input password')),
  2734. '*/div',
  2735. array('div' => array('class' => 'input date')),
  2736. '*/div',
  2737. array('div' => array('class' => 'input date')),
  2738. '*/div',
  2739. array('div' => array('class' => 'input datetime')),
  2740. '*/div',
  2741. array('div' => array('class' => 'input number')),
  2742. '*/div',
  2743. array('div' => array('class' => 'input select')),
  2744. '*/div',
  2745. );
  2746. $this->assertTags($result, $expected);
  2747. $this->Form->create('Contact');
  2748. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2749. $expected = array(
  2750. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2751. array('div' => array('class' => 'input text')),
  2752. '*/div',
  2753. array('div' => array('class' => 'input email')),
  2754. '*/div',
  2755. array('div' => array('class' => 'input tel')),
  2756. '*/div',
  2757. array('div' => array('class' => 'input password')),
  2758. '*/div',
  2759. array('div' => array('class' => 'input date')),
  2760. '*/div',
  2761. array('div' => array('class' => 'input date')),
  2762. '*/div',
  2763. array('div' => array('class' => 'input datetime')),
  2764. '*/div',
  2765. array('div' => array('class' => 'input number')),
  2766. '*/div',
  2767. array('div' => array('class' => 'input select')),
  2768. '*/div',
  2769. );
  2770. $this->assertTags($result, $expected);
  2771. $this->Form->create('Contact');
  2772. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2773. $this->assertTags($result, $expected);
  2774. $this->Form->create('Contact');
  2775. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2776. $expected = array(
  2777. 'fieldset' => array(),
  2778. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2779. array('div' => array('class' => 'input text')),
  2780. '*/div',
  2781. array('div' => array('class' => 'input email')),
  2782. '*/div',
  2783. array('div' => array('class' => 'input tel')),
  2784. '*/div',
  2785. array('div' => array('class' => 'input password')),
  2786. '*/div',
  2787. array('div' => array('class' => 'input date')),
  2788. '*/div',
  2789. array('div' => array('class' => 'input date')),
  2790. '*/div',
  2791. array('div' => array('class' => 'input datetime')),
  2792. '*/div',
  2793. array('div' => array('class' => 'input number')),
  2794. '*/div',
  2795. array('div' => array('class' => 'input select')),
  2796. '*/div',
  2797. '/fieldset'
  2798. );
  2799. $this->assertTags($result, $expected);
  2800. $this->Form->create('Contact');
  2801. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2802. $expected = array(
  2803. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2804. array('div' => array('class' => 'input text')),
  2805. '*/div',
  2806. array('div' => array('class' => 'input email')),
  2807. '*/div',
  2808. array('div' => array('class' => 'input tel')),
  2809. '*/div',
  2810. array('div' => array('class' => 'input password')),
  2811. '*/div',
  2812. array('div' => array('class' => 'input date')),
  2813. '*/div',
  2814. array('div' => array('class' => 'input date')),
  2815. '*/div',
  2816. array('div' => array('class' => 'input datetime')),
  2817. '*/div',
  2818. array('div' => array('class' => 'input number')),
  2819. '*/div',
  2820. array('div' => array('class' => 'input select')),
  2821. '*/div',
  2822. );
  2823. $this->assertTags($result, $expected);
  2824. $this->Form->create('Contact');
  2825. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2826. $this->assertTags($result, $expected);
  2827. $this->Form->create('Contact');
  2828. $result = $this->Form->inputs('Hello');
  2829. $expected = array(
  2830. 'fieldset' => array(),
  2831. 'legend' => array(),
  2832. 'Hello',
  2833. '/legend',
  2834. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2835. array('div' => array('class' => 'input text')),
  2836. '*/div',
  2837. array('div' => array('class' => 'input email')),
  2838. '*/div',
  2839. array('div' => array('class' => 'input tel')),
  2840. '*/div',
  2841. array('div' => array('class' => 'input password')),
  2842. '*/div',
  2843. array('div' => array('class' => 'input date')),
  2844. '*/div',
  2845. array('div' => array('class' => 'input date')),
  2846. '*/div',
  2847. array('div' => array('class' => 'input datetime')),
  2848. '*/div',
  2849. array('div' => array('class' => 'input number')),
  2850. '*/div',
  2851. array('div' => array('class' => 'input select')),
  2852. '*/div',
  2853. '/fieldset'
  2854. );
  2855. $this->assertTags($result, $expected);
  2856. $this->Form->create('Contact');
  2857. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2858. $expected = array(
  2859. 'fieldset' => array(),
  2860. 'legend' => array(),
  2861. 'Hello',
  2862. '/legend',
  2863. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2864. array('div' => array('class' => 'input text')),
  2865. '*/div',
  2866. array('div' => array('class' => 'input email')),
  2867. '*/div',
  2868. array('div' => array('class' => 'input tel')),
  2869. '*/div',
  2870. array('div' => array('class' => 'input password')),
  2871. '*/div',
  2872. array('div' => array('class' => 'input date')),
  2873. '*/div',
  2874. array('div' => array('class' => 'input date')),
  2875. '*/div',
  2876. array('div' => array('class' => 'input datetime')),
  2877. '*/div',
  2878. array('div' => array('class' => 'input number')),
  2879. '*/div',
  2880. array('div' => array('class' => 'input select')),
  2881. '*/div',
  2882. '/fieldset'
  2883. );
  2884. $this->assertTags($result, $expected);
  2885. $this->Form->create('Contact');
  2886. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2887. $this->assertTags($result, $expected);
  2888. $this->Form->end();
  2889. $this->Form->create(false);
  2890. $expected = array(
  2891. 'fieldset' => array(),
  2892. array('div' => array('class' => 'input text')),
  2893. 'label' => array('for' => 'foo'),
  2894. 'Foo',
  2895. '/label',
  2896. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2897. '*/div',
  2898. '/fieldset'
  2899. );
  2900. $result = $this->Form->inputs(
  2901. array('foo' => array('type' => 'text')),
  2902. array(),
  2903. array('legend' => false)
  2904. );
  2905. $this->assertTags($result, $expected);
  2906. }
  2907. /**
  2908. * Tests inputs() works with plugin models
  2909. *
  2910. * @return void
  2911. */
  2912. public function testInputsPluginModel() {
  2913. $this->loadFixtures('Post');
  2914. App::build(array(
  2915. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2916. ));
  2917. CakePlugin::load('TestPlugin');
  2918. $this->Form->request['models'] = array(
  2919. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2920. );
  2921. $this->Form->create('TestPlugin.TestPluginPost');
  2922. $result = $this->Form->inputs();
  2923. $this->assertContains('data[TestPluginPost][id]', $result);
  2924. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2925. $this->assertContains('data[TestPluginPost][title]', $result);
  2926. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2927. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2928. $this->assertEquals('TestPluginPost', $this->Form->model());
  2929. }
  2930. /**
  2931. * testSelectAsCheckbox method
  2932. *
  2933. * test multi-select widget with checkbox formatting.
  2934. *
  2935. * @return void
  2936. */
  2937. public function testSelectAsCheckbox() {
  2938. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2939. $expected = array(
  2940. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2941. array('div' => array('class' => 'checkbox')),
  2942. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2943. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2944. 'first',
  2945. '/label',
  2946. '/div',
  2947. array('div' => array('class' => 'checkbox')),
  2948. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2949. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2950. 'second',
  2951. '/label',
  2952. '/div',
  2953. array('div' => array('class' => 'checkbox')),
  2954. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2955. array('label' => array('for' => 'ModelMultiField2')),
  2956. 'third',
  2957. '/label',
  2958. '/div',
  2959. );
  2960. $this->assertTags($result, $expected);
  2961. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2962. $expected = array(
  2963. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2964. array('div' => array('class' => 'checkbox')),
  2965. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2966. array('label' => array('for' => 'ModelMultiField12')),
  2967. 'half',
  2968. '/label',
  2969. '/div',
  2970. );
  2971. $this->assertTags($result, $expected);
  2972. }
  2973. /**
  2974. * testLabel method
  2975. *
  2976. * test label generation.
  2977. *
  2978. * @return void
  2979. */
  2980. public function testLabel() {
  2981. $this->Form->text('Person.name');
  2982. $result = $this->Form->label();
  2983. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2984. $this->Form->text('Person.name');
  2985. $result = $this->Form->label();
  2986. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2987. $result = $this->Form->label('Person.first_name');
  2988. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2989. $result = $this->Form->label('Person.first_name', 'Your first name');
  2990. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2991. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2992. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2993. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2994. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2995. $result = $this->Form->label('Person.first_name', '');
  2996. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2997. $result = $this->Form->label('Person.2.name', '');
  2998. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2999. }
  3000. /**
  3001. * testTextbox method
  3002. *
  3003. * test textbox element generation
  3004. *
  3005. * @return void
  3006. */
  3007. public function testTextbox() {
  3008. $result = $this->Form->text('Model.field');
  3009. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3010. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3011. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3012. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3013. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  3014. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3015. $result = $this->Form->text('Model.text');
  3016. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  3017. $Contact = ClassRegistry::getObject('Contact');
  3018. $Contact->validationErrors['text'] = array(true);
  3019. $this->Form->request->data['Contact']['text'] = 'test';
  3020. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  3021. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3022. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  3023. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  3024. $expected = array(
  3025. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  3026. );
  3027. $this->assertTags($result, $expected);
  3028. }
  3029. /**
  3030. * testDefaultValue method
  3031. *
  3032. * Test default value setting
  3033. *
  3034. * @return void
  3035. */
  3036. public function testDefaultValue() {
  3037. $this->Form->request->data['Model']['field'] = 'test';
  3038. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3039. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3040. unset($this->Form->request->data['Model']['field']);
  3041. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3042. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3043. }
  3044. /**
  3045. * testCheckboxDefaultValue method
  3046. *
  3047. * Test default value setting on checkbox() method
  3048. *
  3049. * @return void
  3050. */
  3051. public function testCheckboxDefaultValue() {
  3052. $this->Form->request->data['Model']['field'] = false;
  3053. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3054. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3055. unset($this->Form->request->data['Model']['field']);
  3056. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3057. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3058. $this->Form->request->data['Model']['field'] = true;
  3059. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3060. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3061. unset($this->Form->request->data['Model']['field']);
  3062. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3063. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3064. }
  3065. /**
  3066. * testError method
  3067. *
  3068. * Test field error generation
  3069. *
  3070. * @return void
  3071. */
  3072. public function testError() {
  3073. $Contact = ClassRegistry::getObject('Contact');
  3074. $Contact->validationErrors['field'] = array(1);
  3075. $result = $this->Form->error('Contact.field');
  3076. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3077. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3078. $this->assertEquals('Error in field Field', $result);
  3079. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3080. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3081. $this->assertEquals('This field contains invalid input', $result);
  3082. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3083. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3084. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3085. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3086. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3087. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3088. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3089. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3090. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3091. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3092. $this->assertEquals('<strong>Badness!</strong>', $result);
  3093. $Contact->validationErrors['field'] = array("email");
  3094. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3095. $expected = array(
  3096. 'div' => array('class' => 'field-error'),
  3097. 'No good!',
  3098. '/div'
  3099. );
  3100. $this->assertTags($result, $expected);
  3101. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3102. $result = $this->Form->error('Contact.field', array(
  3103. 'notEmpty' => 'Cannot be empty',
  3104. 'email' => 'No good!'
  3105. ));
  3106. $expected = array(
  3107. 'div' => array('class' => 'error-message'),
  3108. 'ul' => array(),
  3109. '<li', 'Cannot be empty', '/li',
  3110. '<li', 'No good!', '/li',
  3111. '<li', 'Something else', '/li',
  3112. '/ul',
  3113. '/div'
  3114. );
  3115. $this->assertTags($result, $expected);
  3116. // Testing error messages list options
  3117. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3118. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3119. $expected = array(
  3120. 'div' => array('class' => 'error-message'),
  3121. 'ol' => array(),
  3122. '<li', 'notEmpty', '/li',
  3123. '<li', 'email', '/li',
  3124. '/ol',
  3125. '/div'
  3126. );
  3127. $this->assertTags($result, $expected);
  3128. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3129. $expected = array(
  3130. 'div' => array('class' => 'error-message'),
  3131. 'ol' => array(),
  3132. '<li', 'notEmpty', '/li',
  3133. '<li', 'email', '/li',
  3134. '/ol',
  3135. '/div'
  3136. );
  3137. $this->assertTags($result, $expected);
  3138. $result = $this->Form->error('Contact.field', null, array(
  3139. 'listOptions' => array(
  3140. 'class' => 'ul-class',
  3141. 'itemOptions' => array(
  3142. 'class' => 'li-class'
  3143. )
  3144. )
  3145. ));
  3146. $expected = array(
  3147. 'div' => array('class' => 'error-message'),
  3148. 'ul' => array('class' => 'ul-class'),
  3149. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3150. array('li' => array('class' => 'li-class')), 'email', '/li',
  3151. '/ul',
  3152. '/div'
  3153. );
  3154. $this->assertTags($result, $expected);
  3155. }
  3156. /**
  3157. * test error options when using form->input();
  3158. *
  3159. * @return void
  3160. */
  3161. public function testInputErrorEscape() {
  3162. $this->Form->create('ValidateProfile');
  3163. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3164. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3165. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3166. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3167. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3168. $this->assertRegExp('/required<br>/', $result);
  3169. }
  3170. /**
  3171. * testPassword method
  3172. *
  3173. * Test password element generation
  3174. *
  3175. * @return void
  3176. */
  3177. public function testPassword() {
  3178. $Contact = ClassRegistry::getObject('Contact');
  3179. $result = $this->Form->password('Contact.field');
  3180. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3181. $Contact->validationErrors['passwd'] = 1;
  3182. $this->Form->request->data['Contact']['passwd'] = 'test';
  3183. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3184. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3185. }
  3186. /**
  3187. * testRadio method
  3188. *
  3189. * Test radio element set generation
  3190. *
  3191. * @return void
  3192. */
  3193. public function testRadio() {
  3194. $result = $this->Form->radio('Model.field', array('option A'));
  3195. $expected = array(
  3196. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3197. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3198. 'label' => array('for' => 'ModelField0'),
  3199. 'option A',
  3200. '/label'
  3201. );
  3202. $this->assertTags($result, $expected);
  3203. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3204. $expected = array(
  3205. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3206. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3207. 'label' => array('for' => 'ModelField12'),
  3208. 'half',
  3209. '/label'
  3210. );
  3211. $this->assertTags($result, $expected);
  3212. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3213. $expected = array(
  3214. 'fieldset' => array(),
  3215. 'legend' => array(),
  3216. 'Field',
  3217. '/legend',
  3218. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3219. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3220. array('label' => array('for' => 'ModelField0')),
  3221. 'option A',
  3222. '/label',
  3223. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3224. array('label' => array('for' => 'ModelField1')),
  3225. 'option B',
  3226. '/label',
  3227. '/fieldset'
  3228. );
  3229. $this->assertTags($result, $expected);
  3230. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3231. $expected = array(
  3232. 'fieldset' => array(),
  3233. 'legend' => array(),
  3234. 'Field',
  3235. '/legend',
  3236. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3237. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3238. array('label' => array('for' => 'ModelField0')),
  3239. 'option A',
  3240. '/label',
  3241. 'br' => array(),
  3242. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3243. array('label' => array('for' => 'ModelField1')),
  3244. 'option B',
  3245. '/label',
  3246. '/fieldset'
  3247. );
  3248. $this->assertTags($result, $expected);
  3249. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3250. $expected = array(
  3251. 'fieldset' => array(),
  3252. 'legend' => array(),
  3253. 'Gender',
  3254. '/legend',
  3255. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3256. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3257. array('label' => array('for' => 'EmployeeGenderMale')),
  3258. 'Male',
  3259. '/label',
  3260. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3261. array('label' => array('for' => 'EmployeeGenderFemale')),
  3262. 'Female',
  3263. '/label',
  3264. '/fieldset',
  3265. );
  3266. $this->assertTags($result, $expected);
  3267. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3268. $expected = array(
  3269. 'fieldset' => array(),
  3270. 'legend' => array(),
  3271. 'Gender',
  3272. '/legend',
  3273. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3274. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3275. array('label' => array('for' => 'OfficerGenderMale')),
  3276. 'Male',
  3277. '/label',
  3278. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3279. array('label' => array('for' => 'OfficerGenderFemale')),
  3280. 'Female',
  3281. '/label',
  3282. '/fieldset',
  3283. );
  3284. $this->assertTags($result, $expected);
  3285. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3286. $expected = array(
  3287. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3288. array('input' => array(
  3289. 'type' => 'radio',
  3290. 'name' => 'data[Contact][1][imrequired]',
  3291. 'value' => '0',
  3292. 'id' => 'Contact1Imrequired0',
  3293. 'required' => 'required'
  3294. )),
  3295. 'label' => array('for' => 'Contact1Imrequired0'),
  3296. 'option A',
  3297. '/label'
  3298. );
  3299. $this->assertTags($result, $expected);
  3300. $result = $this->Form->radio('Model.1.field', array('option A'));
  3301. $expected = array(
  3302. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3303. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3304. 'label' => array('for' => 'Model1Field0'),
  3305. 'option A',
  3306. '/label'
  3307. );
  3308. $this->assertTags($result, $expected);
  3309. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3310. $expected = array(
  3311. 'fieldset' => array(),
  3312. 'legend' => array(),
  3313. 'Field',
  3314. '/legend',
  3315. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3316. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3317. array('label' => array('for' => 'ModelField0')),
  3318. 'option A',
  3319. '/label',
  3320. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3321. array('label' => array('for' => 'ModelField1')),
  3322. 'option B',
  3323. '/label',
  3324. '/fieldset'
  3325. );
  3326. $this->assertTags($result, $expected);
  3327. }
  3328. /**
  3329. * Test radio inputs with between as string or array. Also ensure
  3330. * that an array with less between elements works.
  3331. *
  3332. * @return void
  3333. */
  3334. public function testRadioBetween() {
  3335. $result = $this->Form->radio(
  3336. 'Model.field',
  3337. array('option A', 'option B'),
  3338. array('between' => 'I am between')
  3339. );
  3340. $expected = array(
  3341. 'fieldset' => array(),
  3342. 'legend' => array(),
  3343. 'Field',
  3344. '/legend',
  3345. 'I am between',
  3346. 'input' => array(
  3347. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3348. 'value' => '', 'id' => 'ModelField_'
  3349. ),
  3350. array('input' => array(
  3351. 'type' => 'radio', 'name' => 'data[Model][field]',
  3352. 'value' => '0', 'id' => 'ModelField0'
  3353. )),
  3354. array('label' => array('for' => 'ModelField0')),
  3355. 'option A',
  3356. '/label',
  3357. array('input' => array(
  3358. 'type' => 'radio', 'name' => 'data[Model][field]',
  3359. 'value' => '1', 'id' => 'ModelField1'
  3360. )),
  3361. array('label' => array('for' => 'ModelField1')),
  3362. 'option B',
  3363. '/label',
  3364. '/fieldset'
  3365. );
  3366. $this->assertTags($result, $expected);
  3367. $result = $this->Form->radio(
  3368. 'Model.field',
  3369. array('option A', 'option B', 'option C'),
  3370. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3371. );
  3372. $expected = array(
  3373. 'fieldset' => array(),
  3374. 'legend' => array(),
  3375. 'Field',
  3376. '/legend',
  3377. 'input' => array(
  3378. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3379. 'value' => '', 'id' => 'ModelField_'
  3380. ),
  3381. array('input' => array(
  3382. 'type' => 'radio', 'name' => 'data[Model][field]',
  3383. 'value' => '0', 'id' => 'ModelField0'
  3384. )),
  3385. array('label' => array('for' => 'ModelField0')),
  3386. 'option A',
  3387. '/label',
  3388. 'between A',
  3389. '--separator--',
  3390. array('input' => array(
  3391. 'type' => 'radio', 'name' => 'data[Model][field]',
  3392. 'value' => '1', 'id' => 'ModelField1'
  3393. )),
  3394. array('label' => array('for' => 'ModelField1')),
  3395. 'option B',
  3396. '/label',
  3397. 'between B',
  3398. '--separator--',
  3399. array('input' => array(
  3400. 'type' => 'radio', 'name' => 'data[Model][field]',
  3401. 'value' => '2', 'id' => 'ModelField2'
  3402. )),
  3403. array('label' => array('for' => 'ModelField2')),
  3404. 'option C',
  3405. '/label',
  3406. 'between C',
  3407. '/fieldset'
  3408. );
  3409. $this->assertTags($result, $expected);
  3410. $result = $this->Form->input('Model.field', array(
  3411. 'options' => array('1' => 'first', '2' => 'second'),
  3412. 'type' => 'radio',
  3413. 'before' => '--before--',
  3414. 'after' => '--after--',
  3415. 'separator' => '--separator--',
  3416. 'between' => array('--between first--', '--between second--')
  3417. ));
  3418. $expected = array(
  3419. 'div' => array('class' => 'input radio'),
  3420. '--before--',
  3421. 'fieldset' => array(),
  3422. 'legend' => array(),
  3423. 'Field',
  3424. '/legend',
  3425. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3426. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3427. array('label' => array('for' => 'ModelField1')),
  3428. 'first',
  3429. '/label',
  3430. '--between first--',
  3431. '--separator--',
  3432. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3433. array('label' => array('for' => 'ModelField2')),
  3434. 'second',
  3435. '/label',
  3436. '--between second--',
  3437. '/fieldset',
  3438. '--after--',
  3439. '/div'
  3440. );
  3441. $this->assertTags($result, $expected);
  3442. $result = $this->Form->input('Model.field', array(
  3443. 'options' => array('1' => 'first', '2' => 'second'),
  3444. 'type' => 'radio',
  3445. 'before' => '--before--',
  3446. 'after' => '--after--',
  3447. 'separator' => '--separator--',
  3448. 'between' => array('--between first--')
  3449. ));
  3450. $expected = array(
  3451. 'div' => array('class' => 'input radio'),
  3452. '--before--',
  3453. 'fieldset' => array(),
  3454. 'legend' => array(),
  3455. 'Field',
  3456. '/legend',
  3457. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3458. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3459. array('label' => array('for' => 'ModelField1')),
  3460. 'first',
  3461. '/label',
  3462. '--between first--',
  3463. '--separator--',
  3464. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3465. array('label' => array('for' => 'ModelField2')),
  3466. 'second',
  3467. '/label',
  3468. '/fieldset',
  3469. '--after--',
  3470. '/div'
  3471. );
  3472. $this->assertTags($result, $expected);
  3473. }
  3474. /**
  3475. * Test that radios with a 0 value are selected under the correct conditions.
  3476. * Also ensure that values that are booleanish are handled correctly.
  3477. *
  3478. * @return void
  3479. */
  3480. public function testRadioOptionWithBooleanishValues() {
  3481. $expected = array(
  3482. 'fieldset' => array(),
  3483. 'legend' => array(),
  3484. 'Field',
  3485. '/legend',
  3486. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3487. array('label' => array('for' => 'ModelField1')),
  3488. 'Yes',
  3489. '/label',
  3490. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3491. array('label' => array('for' => 'ModelField0')),
  3492. 'No',
  3493. '/label',
  3494. '/fieldset'
  3495. );
  3496. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3497. $this->assertTags($result, $expected);
  3498. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3499. $this->assertTags($result, $expected);
  3500. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3501. $this->assertTags($result, $expected);
  3502. $expected = array(
  3503. 'fieldset' => array(),
  3504. 'legend' => array(),
  3505. 'Field',
  3506. '/legend',
  3507. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3508. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3509. array('label' => array('for' => 'ModelField1')),
  3510. 'Yes',
  3511. '/label',
  3512. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3513. array('label' => array('for' => 'ModelField0')),
  3514. 'No',
  3515. '/label',
  3516. '/fieldset'
  3517. );
  3518. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3519. $this->assertTags($result, $expected);
  3520. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3521. $this->assertTags($result, $expected);
  3522. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3523. $this->assertTags($result, $expected);
  3524. $expected = array(
  3525. 'fieldset' => array(),
  3526. 'legend' => array(),
  3527. 'Field',
  3528. '/legend',
  3529. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3530. array('label' => array('for' => 'ModelField1')),
  3531. 'Yes',
  3532. '/label',
  3533. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3534. array('label' => array('for' => 'ModelField0')),
  3535. 'No',
  3536. '/label',
  3537. '/fieldset'
  3538. );
  3539. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3540. $this->assertTags($result, $expected);
  3541. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3542. $this->assertTags($result, $expected);
  3543. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3544. $this->assertTags($result, $expected);
  3545. }
  3546. /**
  3547. * test disabled radio options
  3548. *
  3549. * @return void
  3550. */
  3551. public function testRadioDisabled() {
  3552. $result = $this->Form->radio(
  3553. 'Model.field',
  3554. array('option A', 'option B'),
  3555. array('disabled' => array(0), 'value' => '0')
  3556. );
  3557. $expected = array(
  3558. 'fieldset' => array(),
  3559. 'legend' => array(),
  3560. 'Field',
  3561. '/legend',
  3562. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3563. array('label' => array('for' => 'ModelField0')),
  3564. 'option A',
  3565. '/label',
  3566. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3567. array('label' => array('for' => 'ModelField1')),
  3568. 'option B',
  3569. '/label',
  3570. '/fieldset'
  3571. );
  3572. $this->assertTags($result, $expected);
  3573. $result = $this->Form->radio(
  3574. 'Model.field',
  3575. array('option A', 'option B'),
  3576. array('disabled' => true, 'value' => '0')
  3577. );
  3578. $expected = array(
  3579. 'fieldset' => array(),
  3580. 'legend' => array(),
  3581. 'Field',
  3582. '/legend',
  3583. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3584. array('label' => array('for' => 'ModelField0')),
  3585. 'option A',
  3586. '/label',
  3587. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3588. array('label' => array('for' => 'ModelField1')),
  3589. 'option B',
  3590. '/label',
  3591. '/fieldset'
  3592. );
  3593. $this->assertTags($result, $expected);
  3594. $result = $this->Form->radio(
  3595. 'Model.field',
  3596. array('option A', 'option B'),
  3597. array('disabled' => 'disabled', 'value' => '0')
  3598. );
  3599. $expected = array(
  3600. 'fieldset' => array(),
  3601. 'legend' => array(),
  3602. 'Field',
  3603. '/legend',
  3604. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3605. array('label' => array('for' => 'ModelField0')),
  3606. 'option A',
  3607. '/label',
  3608. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3609. array('label' => array('for' => 'ModelField1')),
  3610. 'option B',
  3611. '/label',
  3612. '/fieldset'
  3613. );
  3614. $this->assertTags($result, $expected);
  3615. $result = $this->Form->input('Model.field', array(
  3616. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3617. 'type' => 'radio',
  3618. 'disabled' => array(2, '3x'),
  3619. ));
  3620. $expected = array(
  3621. 'div' => array('class' => 'input radio'),
  3622. 'fieldset' => array(),
  3623. 'legend' => array(),
  3624. 'Field',
  3625. '/legend',
  3626. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3627. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3628. array('label' => array('for' => 'ModelField1')),
  3629. 'first',
  3630. '/label',
  3631. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3632. array('label' => array('for' => 'ModelField2')),
  3633. 'second',
  3634. '/label',
  3635. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3636. array('label' => array('for' => 'ModelField2x')),
  3637. '2x',
  3638. '/label',
  3639. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3640. array('label' => array('for' => 'ModelField3')),
  3641. 'third',
  3642. '/label',
  3643. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3644. array('label' => array('for' => 'ModelField3x')),
  3645. '3x',
  3646. '/label',
  3647. '/fieldset',
  3648. '/div'
  3649. );
  3650. $this->assertTags($result, $expected);
  3651. $result = $this->Form->input('Model.field', array(
  3652. 'type' => 'radio',
  3653. 'options' => array(
  3654. 1 => 'A',
  3655. 2 => 'B',
  3656. 3 => 'C'
  3657. ),
  3658. 'disabled' => array(1)
  3659. ));
  3660. $expected = array(
  3661. 'div' => array('class' => 'input radio'),
  3662. 'fieldset' => array(),
  3663. 'legend' => array(),
  3664. 'Field',
  3665. '/legend',
  3666. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3667. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
  3668. array('label' => array('for' => 'ModelField1')),
  3669. 'A',
  3670. '/label',
  3671. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
  3672. array('label' => array('for' => 'ModelField2')),
  3673. 'B',
  3674. '/label',
  3675. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
  3676. array('label' => array('for' => 'ModelField3')),
  3677. 'C',
  3678. '/label',
  3679. '/fieldset',
  3680. '/div'
  3681. );
  3682. $this->assertTags($result, $expected);
  3683. }
  3684. /**
  3685. * test disabling the hidden input for radio buttons
  3686. *
  3687. * @return void
  3688. */
  3689. public function testRadioHiddenInputDisabling() {
  3690. $result = $this->Form->input('Model.1.field', array(
  3691. 'type' => 'radio',
  3692. 'options' => array('option A'),
  3693. 'hiddenField' => false
  3694. )
  3695. );
  3696. $expected = array(
  3697. 'div' => array('class' => 'input radio'),
  3698. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3699. 'label' => array('for' => 'Model1Field0'),
  3700. 'option A',
  3701. '/label',
  3702. '/div'
  3703. );
  3704. $this->assertTags($result, $expected);
  3705. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3706. $expected = array(
  3707. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3708. 'label' => array('for' => 'Model1Field0'),
  3709. 'option A',
  3710. '/label'
  3711. );
  3712. $this->assertTags($result, $expected);
  3713. }
  3714. /**
  3715. * test adding an empty option for radio buttons
  3716. *
  3717. * @return void
  3718. */
  3719. public function testRadioAddEmptyOption() {
  3720. $result = $this->Form->input('Model.1.field', array(
  3721. 'type' => 'radio',
  3722. 'options' => array('option A'),
  3723. 'empty' => true,
  3724. 'hiddenField' => false
  3725. ));
  3726. $expected = array(
  3727. 'div' => array('class' => 'input radio'),
  3728. 'fieldset' => array(),
  3729. 'legend' => array(),
  3730. 'Field',
  3731. '/legend',
  3732. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3733. array('label' => array('for' => 'Model1Field')),
  3734. __('empty'),
  3735. '/label',
  3736. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3737. array('label' => array('for' => 'Model1Field0')),
  3738. 'option A',
  3739. '/label',
  3740. '/fieldset',
  3741. '/div'
  3742. );
  3743. $this->assertTags($result, $expected);
  3744. $result = $this->Form->input('Model.1.field', array(
  3745. 'type' => 'radio',
  3746. 'options' => array('option A'),
  3747. 'empty' => 'CustomEmptyLabel',
  3748. 'hiddenField' => false
  3749. ));
  3750. $expected = array(
  3751. 'div' => array('class' => 'input radio'),
  3752. 'fieldset' => array(),
  3753. 'legend' => array(),
  3754. 'Field',
  3755. '/legend',
  3756. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3757. array('label' => array('for' => 'Model1Field')),
  3758. 'CustomEmptyLabel',
  3759. '/label',
  3760. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3761. array('label' => array('for' => 'Model1Field0')),
  3762. 'option A',
  3763. '/label',
  3764. '/fieldset',
  3765. '/div'
  3766. );
  3767. $this->assertTags($result, $expected);
  3768. $result = $this->Form->input('Model.1.field', array(
  3769. 'type' => 'radio',
  3770. 'options' => array('option A'),
  3771. 'empty' => false,
  3772. 'hiddenField' => false
  3773. ));
  3774. $this->assertTextNotContains('"Model1Field"', $result);
  3775. }
  3776. /**
  3777. * Test that radio() accepts an array for label
  3778. *
  3779. * @return void
  3780. */
  3781. public function testRadioLabelArray() {
  3782. $result = $this->Form->input('Model.field', array(
  3783. 'type' => 'radio',
  3784. 'legend' => false,
  3785. 'label' => array(
  3786. 'class' => 'checkbox float-left',
  3787. ),
  3788. 'options' => array('1' => 'Option A', '2' => 'Option B.')
  3789. ));
  3790. $this->assertTextContains(
  3791. '<label for="ModelField1" class="checkbox float-left">Option A</label>',
  3792. $result
  3793. );
  3794. }
  3795. /**
  3796. * Test that label id's match the input element id's when radio is called after create().
  3797. *
  3798. * @return void
  3799. */
  3800. public function testRadioWithCreate() {
  3801. $this->Form->create('Model');
  3802. $result = $this->Form->radio('recipient',
  3803. array('1' => '1', '2' => '2', '3' => '3'),
  3804. array('legend' => false, 'value' => '1')
  3805. );
  3806. $this->assertTextNotContains(
  3807. '<label for="ModelModelRecipient1">1</label>',
  3808. $result
  3809. );
  3810. $this->assertTextContains(
  3811. '<label for="ModelRecipient1">1</label>',
  3812. $result
  3813. );
  3814. }
  3815. /**
  3816. * testSelect method
  3817. *
  3818. * Test select element generation.
  3819. *
  3820. * @return void
  3821. */
  3822. public function testSelect() {
  3823. $result = $this->Form->select('Model.field', array());
  3824. $expected = array(
  3825. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3826. array('option' => array('value' => '')),
  3827. '/option',
  3828. '/select'
  3829. );
  3830. $this->assertTags($result, $expected);
  3831. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3832. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3833. $expected = array(
  3834. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3835. array('option' => array('value' => '')),
  3836. '/option',
  3837. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3838. 'good',
  3839. '/option',
  3840. array('option' => array('value' => 'other')),
  3841. 'bad',
  3842. '/option',
  3843. '/select'
  3844. );
  3845. $this->assertTags($result, $expected);
  3846. $this->Form->request->data = array();
  3847. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3848. $expected = array(
  3849. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3850. array('option' => array('value' => '')),
  3851. '/option',
  3852. array('option' => array('value' => 'value')),
  3853. 'good',
  3854. '/option',
  3855. array('option' => array('value' => 'other')),
  3856. 'bad',
  3857. '/option',
  3858. '/select'
  3859. );
  3860. $this->assertTags($result, $expected);
  3861. $result = $this->Form->select(
  3862. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3863. array('empty' => false)
  3864. );
  3865. $expected = array(
  3866. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3867. array('option' => array('value' => 'first')),
  3868. 'first &quot;html&quot; &lt;chars&gt;',
  3869. '/option',
  3870. array('option' => array('value' => 'second')),
  3871. 'value',
  3872. '/option',
  3873. '/select'
  3874. );
  3875. $this->assertTags($result, $expected);
  3876. $result = $this->Form->select(
  3877. 'Model.field',
  3878. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3879. array('escape' => false, 'empty' => false)
  3880. );
  3881. $expected = array(
  3882. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3883. array('option' => array('value' => 'first')),
  3884. 'first "html" <chars>',
  3885. '/option',
  3886. array('option' => array('value' => 'second')),
  3887. 'value',
  3888. '/option',
  3889. '/select'
  3890. );
  3891. $this->assertTags($result, $expected);
  3892. $options = array(
  3893. array('value' => 'first', 'name' => 'First'),
  3894. array('value' => 'first', 'name' => 'Another First'),
  3895. );
  3896. $result = $this->Form->select(
  3897. 'Model.field',
  3898. $options,
  3899. array('escape' => false, 'empty' => false)
  3900. );
  3901. $expected = array(
  3902. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3903. array('option' => array('value' => 'first')),
  3904. 'First',
  3905. '/option',
  3906. array('option' => array('value' => 'first')),
  3907. 'Another First',
  3908. '/option',
  3909. '/select'
  3910. );
  3911. $this->assertTags($result, $expected);
  3912. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3913. $result = $this->Form->select(
  3914. 'Model.contact_id',
  3915. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3916. array('escape' => false, 'empty' => 'pick something')
  3917. );
  3918. $expected = array(
  3919. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3920. array('option' => array('value' => '')), 'pick something', '/option',
  3921. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3922. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3923. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3924. '/select'
  3925. );
  3926. $this->assertTags($result, $expected);
  3927. $this->Form->request->data['Model']['field'] = 0;
  3928. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3929. $expected = array(
  3930. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3931. array('option' => array('value' => '')), '/option',
  3932. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3933. array('option' => array('value' => '1')), 'Yes', '/option',
  3934. '/select'
  3935. );
  3936. $this->assertTags($result, $expected);
  3937. $this->Form->request->data['Model']['field'] = 50;
  3938. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3939. $expected = array(
  3940. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3941. array('option' => array('value' => '')), '/option',
  3942. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3943. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3944. '/select'
  3945. );
  3946. $this->assertTags($result, $expected);
  3947. $result = $this->Form->select('Contact.required_one', array('option A'));
  3948. $expected = array(
  3949. 'select' => array(
  3950. 'name' => 'data[Contact][required_one]',
  3951. 'id' => 'ContactRequiredOne',
  3952. 'required' => 'required'
  3953. ),
  3954. array('option' => array('value' => '')), '/option',
  3955. array('option' => array('value' => '0')), 'option A', '/option',
  3956. '/select'
  3957. );
  3958. $this->assertTags($result, $expected);
  3959. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  3960. $expected = array(
  3961. 'select' => array(
  3962. 'name' => 'data[Contact][required_one]',
  3963. 'id' => 'ContactRequiredOne',
  3964. 'disabled' => 'disabled'
  3965. ),
  3966. array('option' => array('value' => '')), '/option',
  3967. array('option' => array('value' => '0')), 'option A', '/option',
  3968. '/select'
  3969. );
  3970. $this->assertTags($result, $expected);
  3971. }
  3972. /**
  3973. * test that select() with optiongroups listens to the escape param.
  3974. *
  3975. * @return void
  3976. */
  3977. public function testSelectOptionGroupEscaping() {
  3978. $options = array(
  3979. '>< Key' => array(
  3980. 1 => 'One',
  3981. 2 => 'Two'
  3982. )
  3983. );
  3984. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3985. $expected = array(
  3986. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3987. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3988. array('option' => array('value' => '1')), 'One', '/option',
  3989. array('option' => array('value' => '2')), 'Two', '/option',
  3990. '/optgroup',
  3991. '/select'
  3992. );
  3993. $this->assertTags($result, $expected);
  3994. $options = array(
  3995. '>< Key' => array(
  3996. 1 => 'One',
  3997. 2 => 'Two'
  3998. )
  3999. );
  4000. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  4001. $expected = array(
  4002. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4003. 'optgroup' => array('label' => '>< Key'),
  4004. array('option' => array('value' => '1')), 'One', '/option',
  4005. array('option' => array('value' => '2')), 'Two', '/option',
  4006. '/optgroup',
  4007. '/select'
  4008. );
  4009. $this->assertTags($result, $expected);
  4010. }
  4011. /**
  4012. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  4013. *
  4014. * @return void
  4015. */
  4016. public function testSelectWithNullAttributes() {
  4017. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  4018. $expected = array(
  4019. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4020. array('option' => array('value' => '0')),
  4021. 'first',
  4022. '/option',
  4023. array('option' => array('value' => '1')),
  4024. 'second',
  4025. '/option',
  4026. '/select'
  4027. );
  4028. $this->assertTags($result, $expected);
  4029. }
  4030. /**
  4031. * testNestedSelect method
  4032. *
  4033. * test select element generation with optgroups
  4034. *
  4035. * @return void
  4036. */
  4037. public function testNestedSelect() {
  4038. $result = $this->Form->select(
  4039. 'Model.field',
  4040. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4041. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4042. )), array('empty' => false)
  4043. );
  4044. $expected = array(
  4045. 'select' => array('name' => 'data[Model][field]',
  4046. 'id' => 'ModelField'),
  4047. array('option' => array('value' => 1)),
  4048. 'One',
  4049. '/option',
  4050. array('option' => array('value' => 2)),
  4051. 'Two',
  4052. '/option',
  4053. array('optgroup' => array('label' => 'Three')),
  4054. array('option' => array('value' => 4)),
  4055. 'Four',
  4056. '/option',
  4057. array('option' => array('value' => 5)),
  4058. 'Five',
  4059. '/option',
  4060. '/optgroup',
  4061. '/select'
  4062. );
  4063. $this->assertTags($result, $expected);
  4064. $result = $this->Form->select(
  4065. 'Model.field',
  4066. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  4067. array('showParents' => true, 'empty' => false)
  4068. );
  4069. $expected = array(
  4070. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4071. array('option' => array('value' => 1)),
  4072. 'One',
  4073. '/option',
  4074. array('option' => array('value' => 2)),
  4075. 'Two',
  4076. '/option',
  4077. array('optgroup' => array('label' => 'Three')),
  4078. array('option' => array('value' => 3)),
  4079. 'Three',
  4080. '/option',
  4081. array('option' => array('value' => 4)),
  4082. 'Four',
  4083. '/option',
  4084. '/optgroup',
  4085. '/select'
  4086. );
  4087. $this->assertTags($result, $expected);
  4088. }
  4089. /**
  4090. * testSelectMultiple method
  4091. *
  4092. * test generation of multiple select elements
  4093. *
  4094. * @return void
  4095. */
  4096. public function testSelectMultiple() {
  4097. $options = array('first', 'second', 'third');
  4098. $result = $this->Form->select(
  4099. 'Model.multi_field', $options, array('multiple' => true)
  4100. );
  4101. $expected = array(
  4102. 'input' => array(
  4103. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4104. ),
  4105. 'select' => array(
  4106. 'name' => 'data[Model][multi_field][]',
  4107. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4108. ),
  4109. array('option' => array('value' => '0')),
  4110. 'first',
  4111. '/option',
  4112. array('option' => array('value' => '1')),
  4113. 'second',
  4114. '/option',
  4115. array('option' => array('value' => '2')),
  4116. 'third',
  4117. '/option',
  4118. '/select'
  4119. );
  4120. $this->assertTags($result, $expected);
  4121. $result = $this->Form->select(
  4122. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4123. );
  4124. $expected = array(
  4125. 'input' => array(
  4126. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4127. ),
  4128. 'select' => array(
  4129. 'name' => 'data[Model][multi_field][]',
  4130. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4131. ),
  4132. array('option' => array('value' => '0')),
  4133. 'first',
  4134. '/option',
  4135. array('option' => array('value' => '1')),
  4136. 'second',
  4137. '/option',
  4138. array('option' => array('value' => '2')),
  4139. 'third',
  4140. '/option',
  4141. '/select'
  4142. );
  4143. $this->assertTags($result, $expected);
  4144. $result = $this->Form->select(
  4145. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4146. );
  4147. $expected = array(
  4148. 'input' => array(
  4149. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4150. ),
  4151. 'select' => array(
  4152. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4153. 'multiple' => 'multiple'
  4154. ),
  4155. array('option' => array('value' => '0', 'selected' => 'selected')),
  4156. 'first',
  4157. '/option',
  4158. array('option' => array('value' => '1', 'selected' => 'selected')),
  4159. 'second',
  4160. '/option',
  4161. array('option' => array('value' => '2')),
  4162. 'third',
  4163. '/option',
  4164. '/select'
  4165. );
  4166. $this->assertTags($result, $expected);
  4167. $result = $this->Form->select(
  4168. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4169. );
  4170. $expected = array(
  4171. 'select' => array(
  4172. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4173. ),
  4174. array('option' => array('value' => '0', 'selected' => 'selected')),
  4175. 'first',
  4176. '/option',
  4177. array('option' => array('value' => '1', 'selected' => 'selected')),
  4178. 'second',
  4179. '/option',
  4180. array('option' => array('value' => '2')),
  4181. 'third',
  4182. '/option',
  4183. '/select'
  4184. );
  4185. $this->assertTags($result, $expected);
  4186. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4187. $selected = array('2', '3x');
  4188. $result = $this->Form->select(
  4189. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4190. );
  4191. $expected = array(
  4192. 'input' => array(
  4193. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4194. ),
  4195. 'select' => array(
  4196. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4197. ),
  4198. array('option' => array('value' => '1')),
  4199. 'One',
  4200. '/option',
  4201. array('option' => array('value' => '2', 'selected' => 'selected')),
  4202. 'Two',
  4203. '/option',
  4204. array('option' => array('value' => '3')),
  4205. 'Three',
  4206. '/option',
  4207. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4208. 'Stringy',
  4209. '/option',
  4210. '/select'
  4211. );
  4212. $this->assertTags($result, $expected);
  4213. $result = $this->Form->select('Contact.required_one', array(
  4214. '1' => 'option A',
  4215. '2' => 'option B'
  4216. ), array('multiple' => true));
  4217. $expected = array(
  4218. 'input' => array(
  4219. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4220. ),
  4221. 'select' => array(
  4222. 'name' => 'data[Contact][required_one][]',
  4223. 'id' => 'ContactRequiredOne',
  4224. 'required' => 'required',
  4225. 'multiple' => 'multiple'
  4226. ),
  4227. array('option' => array('value' => '1')), 'option A', '/option',
  4228. array('option' => array('value' => '2')), 'option B', '/option',
  4229. '/select'
  4230. );
  4231. $this->assertTags($result, $expected);
  4232. }
  4233. /**
  4234. * Test generating multiple select with disabled elements.
  4235. *
  4236. * @return void
  4237. */
  4238. public function testSelectMultipleWithDisabledElements() {
  4239. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4240. $disabled = array(2, 3);
  4241. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4242. $expected = array(
  4243. 'input' => array(
  4244. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4245. ),
  4246. 'select' => array(
  4247. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4248. ),
  4249. array('option' => array('value' => '1')),
  4250. 'One',
  4251. '/option',
  4252. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4253. 'Two',
  4254. '/option',
  4255. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4256. 'Three',
  4257. '/option',
  4258. array('option' => array('value' => '3x')),
  4259. 'Stringy',
  4260. '/option',
  4261. '/select'
  4262. );
  4263. $this->assertTags($result, $expected);
  4264. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4265. $disabled = array('2', '3x');
  4266. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4267. $expected = array(
  4268. array('div' => array('class' => 'input select')),
  4269. array('label' => array('for' => 'ContactMultiple')),
  4270. 'Multiple',
  4271. '/label',
  4272. 'input' => array(
  4273. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4274. ),
  4275. 'select' => array(
  4276. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4277. ),
  4278. array('option' => array('value' => '1')),
  4279. 'One',
  4280. '/option',
  4281. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4282. 'Two',
  4283. '/option',
  4284. array('option' => array('value' => '3')),
  4285. 'Three',
  4286. '/option',
  4287. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4288. 'Stringy',
  4289. '/option',
  4290. '/select',
  4291. '/div'
  4292. );
  4293. $this->assertTags($result, $expected);
  4294. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4295. $disabled = true;
  4296. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4297. $expected = array(
  4298. array('div' => array('class' => 'input select')),
  4299. array('label' => array('for' => 'ContactMultiple')),
  4300. 'Multiple',
  4301. '/label',
  4302. 'input' => array(
  4303. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4304. ),
  4305. 'select' => array(
  4306. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4307. ),
  4308. array('option' => array('value' => '1')),
  4309. 'One',
  4310. '/option',
  4311. array('option' => array('value' => '2')),
  4312. 'Two',
  4313. '/option',
  4314. array('option' => array('value' => '3')),
  4315. 'Three',
  4316. '/option',
  4317. array('option' => array('value' => '3x')),
  4318. 'Stringy',
  4319. '/option',
  4320. '/select',
  4321. '/div'
  4322. );
  4323. $this->assertTags($result, $expected);
  4324. }
  4325. /**
  4326. * Test generating select with disabled elements.
  4327. *
  4328. * @return void
  4329. */
  4330. public function testSelectWithDisabledElements() {
  4331. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4332. $disabled = array(2, 3);
  4333. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4334. $expected = array(
  4335. 'select' => array(
  4336. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4337. ),
  4338. array('option' => array('value' => '')),
  4339. '/option',
  4340. array('option' => array('value' => '1')),
  4341. 'One',
  4342. '/option',
  4343. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4344. 'Two',
  4345. '/option',
  4346. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4347. 'Three',
  4348. '/option',
  4349. array('option' => array('value' => '3x')),
  4350. 'Stringy',
  4351. '/option',
  4352. '/select'
  4353. );
  4354. $this->assertTags($result, $expected);
  4355. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4356. $disabled = array('2', '3x');
  4357. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4358. $expected = array(
  4359. array('div' => array('class' => 'input select')),
  4360. array('label' => array('for' => 'ModelField')),
  4361. 'Field',
  4362. '/label',
  4363. 'select' => array(
  4364. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4365. ),
  4366. array('option' => array('value' => '1')),
  4367. 'One',
  4368. '/option',
  4369. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4370. 'Two',
  4371. '/option',
  4372. array('option' => array('value' => '3')),
  4373. 'Three',
  4374. '/option',
  4375. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4376. 'Stringy',
  4377. '/option',
  4378. '/select',
  4379. '/div'
  4380. );
  4381. $this->assertTags($result, $expected);
  4382. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4383. $disabled = true;
  4384. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4385. $expected = array(
  4386. array('div' => array('class' => 'input select')),
  4387. array('label' => array('for' => 'ModelField')),
  4388. 'Field',
  4389. '/label',
  4390. 'select' => array(
  4391. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4392. ),
  4393. array('option' => array('value' => '1')),
  4394. 'One',
  4395. '/option',
  4396. array('option' => array('value' => '2')),
  4397. 'Two',
  4398. '/option',
  4399. array('option' => array('value' => '3')),
  4400. 'Three',
  4401. '/option',
  4402. array('option' => array('value' => '3x')),
  4403. 'Stringy',
  4404. '/option',
  4405. '/select',
  4406. '/div'
  4407. );
  4408. $this->assertTags($result, $expected);
  4409. }
  4410. /**
  4411. * test generation of habtm select boxes.
  4412. *
  4413. * @return void
  4414. */
  4415. public function testHabtmSelectBox() {
  4416. $this->View->viewVars['contactTags'] = array(
  4417. 1 => 'blue',
  4418. 2 => 'red',
  4419. 3 => 'green'
  4420. );
  4421. $this->Form->request->data = array(
  4422. 'Contact' => array(),
  4423. 'ContactTag' => array(
  4424. array(
  4425. 'id' => '1',
  4426. 'name' => 'blue'
  4427. ),
  4428. array(
  4429. 'id' => 3,
  4430. 'name' => 'green'
  4431. )
  4432. )
  4433. );
  4434. $this->Form->create('Contact');
  4435. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4436. $expected = array(
  4437. 'input' => array(
  4438. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4439. ),
  4440. 'select' => array(
  4441. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4442. 'multiple' => 'multiple'
  4443. ),
  4444. array('option' => array('value' => '1', 'selected' => 'selected')),
  4445. 'blue',
  4446. '/option',
  4447. array('option' => array('value' => '2')),
  4448. 'red',
  4449. '/option',
  4450. array('option' => array('value' => '3', 'selected' => 'selected')),
  4451. 'green',
  4452. '/option',
  4453. '/select'
  4454. );
  4455. $this->assertTags($result, $expected);
  4456. // make sure only 50 is selected, and not 50f5c0cf
  4457. $this->View->viewVars['contactTags'] = array(
  4458. '1' => 'blue',
  4459. '50f5c0cf' => 'red',
  4460. '50' => 'green'
  4461. );
  4462. $this->Form->request->data = array(
  4463. 'Contact' => array(),
  4464. 'ContactTag' => array(
  4465. array(
  4466. 'id' => 1,
  4467. 'name' => 'blue'
  4468. ),
  4469. array(
  4470. 'id' => 50,
  4471. 'name' => 'green'
  4472. )
  4473. )
  4474. );
  4475. $this->Form->create('Contact');
  4476. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4477. $expected = array(
  4478. 'input' => array(
  4479. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4480. ),
  4481. 'select' => array(
  4482. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4483. 'multiple' => 'multiple'
  4484. ),
  4485. array('option' => array('value' => '1', 'selected' => 'selected')),
  4486. 'blue',
  4487. '/option',
  4488. array('option' => array('value' => '50f5c0cf')),
  4489. 'red',
  4490. '/option',
  4491. array('option' => array('value' => '50', 'selected' => 'selected')),
  4492. 'green',
  4493. '/option',
  4494. '/select'
  4495. );
  4496. $this->assertTags($result, $expected);
  4497. }
  4498. /**
  4499. * test generation of multi select elements in checkbox format
  4500. *
  4501. * @return void
  4502. */
  4503. public function testSelectMultipleCheckboxes() {
  4504. $result = $this->Form->select(
  4505. 'Model.multi_field',
  4506. array('first', 'second', 'third'),
  4507. array('multiple' => 'checkbox')
  4508. );
  4509. $expected = array(
  4510. 'input' => array(
  4511. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4512. ),
  4513. array('div' => array('class' => 'checkbox')),
  4514. array('input' => array(
  4515. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4516. 'value' => '0', 'id' => 'ModelMultiField0'
  4517. )),
  4518. array('label' => array('for' => 'ModelMultiField0')),
  4519. 'first',
  4520. '/label',
  4521. '/div',
  4522. array('div' => array('class' => 'checkbox')),
  4523. array('input' => array(
  4524. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4525. 'value' => '1', 'id' => 'ModelMultiField1'
  4526. )),
  4527. array('label' => array('for' => 'ModelMultiField1')),
  4528. 'second',
  4529. '/label',
  4530. '/div',
  4531. array('div' => array('class' => 'checkbox')),
  4532. array('input' => array(
  4533. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4534. 'value' => '2', 'id' => 'ModelMultiField2'
  4535. )),
  4536. array('label' => array('for' => 'ModelMultiField2')),
  4537. 'third',
  4538. '/label',
  4539. '/div'
  4540. );
  4541. $this->assertTags($result, $expected);
  4542. $result = $this->Form->select(
  4543. 'Model.multi_field',
  4544. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4545. array('multiple' => 'checkbox')
  4546. );
  4547. $expected = array(
  4548. 'input' => array(
  4549. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4550. ),
  4551. array('div' => array('class' => 'checkbox')),
  4552. array('input' => array(
  4553. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4554. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4555. )),
  4556. array('label' => array('for' => 'ModelMultiFieldA')),
  4557. 'first',
  4558. '/label',
  4559. '/div',
  4560. array('div' => array('class' => 'checkbox')),
  4561. array('input' => array(
  4562. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4563. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4564. )),
  4565. array('label' => array('for' => 'ModelMultiFieldB')),
  4566. 'second',
  4567. '/label',
  4568. '/div',
  4569. array('div' => array('class' => 'checkbox')),
  4570. array('input' => array(
  4571. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4572. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4573. )),
  4574. array('label' => array('for' => 'ModelMultiFieldC')),
  4575. 'third',
  4576. '/label',
  4577. '/div'
  4578. );
  4579. $this->assertTags($result, $expected);
  4580. $result = $this->Form->select(
  4581. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4582. );
  4583. $expected = array(
  4584. 'input' => array(
  4585. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4586. ),
  4587. array('div' => array('class' => 'checkbox')),
  4588. array('input' => array(
  4589. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4590. 'value' => '1', 'id' => 'ModelMultiField1'
  4591. )),
  4592. array('label' => array('for' => 'ModelMultiField1')),
  4593. 'first',
  4594. '/label',
  4595. '/div'
  4596. );
  4597. $this->assertTags($result, $expected);
  4598. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4599. $result = $this->Form->select(
  4600. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4601. );
  4602. $expected = array(
  4603. 'input' => array(
  4604. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4605. ),
  4606. array('div' => array('class' => 'checkbox')),
  4607. array('input' => array(
  4608. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4609. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4610. )),
  4611. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4612. 'first',
  4613. '/label',
  4614. '/div',
  4615. array('div' => array('class' => 'checkbox')),
  4616. array('input' => array(
  4617. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4618. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4619. )),
  4620. array('label' => array('for' => 'ModelTagsArray')),
  4621. 'Array',
  4622. '/label',
  4623. '/div'
  4624. );
  4625. $this->assertTags($result, $expected);
  4626. }
  4627. /**
  4628. * test multiple checkboxes with div styles.
  4629. *
  4630. * @return void
  4631. */
  4632. public function testSelectMultipleCheckboxDiv() {
  4633. $result = $this->Form->select(
  4634. 'Model.tags',
  4635. array('first', 'second'),
  4636. array('multiple' => 'checkbox', 'class' => 'my-class')
  4637. );
  4638. $expected = array(
  4639. 'input' => array(
  4640. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4641. ),
  4642. array('div' => array('class' => 'my-class')),
  4643. array('input' => array(
  4644. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4645. 'value' => '0', 'id' => 'ModelTags0'
  4646. )),
  4647. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4648. '/div',
  4649. array('div' => array('class' => 'my-class')),
  4650. array('input' => array(
  4651. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4652. 'value' => '1', 'id' => 'ModelTags1'
  4653. )),
  4654. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4655. '/div'
  4656. );
  4657. $this->assertTags($result, $expected);
  4658. $result = $this->Form->input('Model.tags', array(
  4659. 'options' => array('first', 'second'),
  4660. 'multiple' => 'checkbox',
  4661. 'class' => 'my-class',
  4662. 'div' => false,
  4663. 'label' => false
  4664. ));
  4665. $this->assertTags($result, $expected);
  4666. $Contact = ClassRegistry::getObject('Contact');
  4667. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4668. $result = $this->Form->input('Contact.tags', array(
  4669. 'options' => array('one'),
  4670. 'multiple' => 'checkbox',
  4671. 'label' => false,
  4672. 'div' => false
  4673. ));
  4674. $expected = array(
  4675. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4676. array('div' => array('class' => 'checkbox form-error')),
  4677. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4678. array('label' => array('for' => 'ContactTags0')),
  4679. 'one',
  4680. '/label',
  4681. '/div'
  4682. );
  4683. $this->assertTags($result, $expected);
  4684. $result = $this->Form->input('Contact.tags', array(
  4685. 'options' => array('one'),
  4686. 'multiple' => 'checkbox',
  4687. 'class' => 'mycheckbox',
  4688. 'label' => false,
  4689. 'div' => false
  4690. ));
  4691. $expected = array(
  4692. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4693. array('div' => array('class' => 'mycheckbox form-error')),
  4694. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4695. array('label' => array('for' => 'ContactTags0')),
  4696. 'one',
  4697. '/label',
  4698. '/div'
  4699. );
  4700. $this->assertTags($result, $expected);
  4701. }
  4702. /**
  4703. * Checks the security hash array generated for multiple-input checkbox elements
  4704. *
  4705. * @return void
  4706. */
  4707. public function testSelectMultipleCheckboxSecurity() {
  4708. $this->Form->request['_Token'] = array('key' => 'testKey');
  4709. $this->assertEquals(array(), $this->Form->fields);
  4710. $result = $this->Form->select(
  4711. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4712. array('multiple' => 'checkbox')
  4713. );
  4714. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4715. $result = $this->Form->secure($this->Form->fields);
  4716. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4717. $this->assertRegExp('/"' . $key . '"/', $result);
  4718. }
  4719. /**
  4720. * Multiple select elements should always be secured as they always participate
  4721. * in the POST data.
  4722. *
  4723. * @return void
  4724. */
  4725. public function testSelectMultipleSecureWithNoOptions() {
  4726. $this->Form->request['_Token'] = array('key' => 'testkey');
  4727. $this->assertEquals(array(), $this->Form->fields);
  4728. $this->Form->select(
  4729. 'Model.select',
  4730. array(),
  4731. array('multiple' => true)
  4732. );
  4733. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4734. }
  4735. /**
  4736. * When a select box has no options it should not be added to the fields list
  4737. * as it always fail post validation.
  4738. *
  4739. * @return void
  4740. */
  4741. public function testSelectNoSecureWithNoOptions() {
  4742. $this->Form->request['_Token'] = array('key' => 'testkey');
  4743. $this->assertEquals(array(), $this->Form->fields);
  4744. $this->Form->select(
  4745. 'Model.select',
  4746. array()
  4747. );
  4748. $this->assertEquals(array(), $this->Form->fields);
  4749. $this->Form->select(
  4750. 'Model.select',
  4751. array(),
  4752. array('empty' => true)
  4753. );
  4754. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4755. }
  4756. /**
  4757. * testInputMultipleCheckboxes method
  4758. *
  4759. * test input() resulting in multi select elements being generated.
  4760. *
  4761. * @return void
  4762. */
  4763. public function testInputMultipleCheckboxes() {
  4764. $result = $this->Form->input('Model.multi_field', array(
  4765. 'options' => array('first', 'second', 'third'),
  4766. 'multiple' => 'checkbox'
  4767. ));
  4768. $expected = array(
  4769. array('div' => array('class' => 'input select')),
  4770. array('label' => array('for' => 'ModelMultiField')),
  4771. 'Multi Field',
  4772. '/label',
  4773. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4774. array('div' => array('class' => 'checkbox')),
  4775. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4776. array('label' => array('for' => 'ModelMultiField0')),
  4777. 'first',
  4778. '/label',
  4779. '/div',
  4780. array('div' => array('class' => 'checkbox')),
  4781. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4782. array('label' => array('for' => 'ModelMultiField1')),
  4783. 'second',
  4784. '/label',
  4785. '/div',
  4786. array('div' => array('class' => 'checkbox')),
  4787. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4788. array('label' => array('for' => 'ModelMultiField2')),
  4789. 'third',
  4790. '/label',
  4791. '/div',
  4792. '/div'
  4793. );
  4794. $this->assertTags($result, $expected);
  4795. $result = $this->Form->input('Model.multi_field', array(
  4796. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4797. 'multiple' => 'checkbox'
  4798. ));
  4799. $expected = array(
  4800. array('div' => array('class' => 'input select')),
  4801. array('label' => array('for' => 'ModelMultiField')),
  4802. 'Multi Field',
  4803. '/label',
  4804. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4805. array('div' => array('class' => 'checkbox')),
  4806. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4807. array('label' => array('for' => 'ModelMultiFieldA')),
  4808. 'first',
  4809. '/label',
  4810. '/div',
  4811. array('div' => array('class' => 'checkbox')),
  4812. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4813. array('label' => array('for' => 'ModelMultiFieldB')),
  4814. 'second',
  4815. '/label',
  4816. '/div',
  4817. array('div' => array('class' => 'checkbox')),
  4818. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4819. array('label' => array('for' => 'ModelMultiFieldC')),
  4820. 'third',
  4821. '/label',
  4822. '/div',
  4823. '/div'
  4824. );
  4825. $this->assertTags($result, $expected);
  4826. $result = $this->Form->input('Model.multi_field', array(
  4827. 'options' => array('1' => 'first'),
  4828. 'multiple' => 'checkbox',
  4829. 'label' => false,
  4830. 'div' => false
  4831. ));
  4832. $expected = array(
  4833. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4834. array('div' => array('class' => 'checkbox')),
  4835. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4836. array('label' => array('for' => 'ModelMultiField1')),
  4837. 'first',
  4838. '/label',
  4839. '/div'
  4840. );
  4841. $this->assertTags($result, $expected);
  4842. $result = $this->Form->input('Model.multi_field', array(
  4843. 'options' => array('2' => 'second'),
  4844. 'multiple' => 'checkbox',
  4845. 'label' => false,
  4846. 'div' => false
  4847. ));
  4848. $expected = array(
  4849. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4850. array('div' => array('class' => 'checkbox')),
  4851. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4852. array('label' => array('for' => 'ModelMultiField2')),
  4853. 'second',
  4854. '/label',
  4855. '/div'
  4856. );
  4857. $this->assertTags($result, $expected);
  4858. }
  4859. /**
  4860. * testSelectHiddenFieldOmission method
  4861. *
  4862. * test that select() with 'hiddenField' => false omits the hidden field
  4863. *
  4864. * @return void
  4865. */
  4866. public function testSelectHiddenFieldOmission() {
  4867. $result = $this->Form->select('Model.multi_field',
  4868. array('first', 'second'),
  4869. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4870. );
  4871. $expected = array(
  4872. array('div' => array('class' => 'checkbox')),
  4873. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4874. array('label' => array('for' => 'ModelMultiField0')),
  4875. 'first',
  4876. '/label',
  4877. '/div',
  4878. array('div' => array('class' => 'checkbox')),
  4879. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4880. array('label' => array('for' => 'ModelMultiField1')),
  4881. 'second',
  4882. '/label',
  4883. '/div'
  4884. );
  4885. $this->assertTags($result, $expected);
  4886. $result = $this->Form->input('Model.multi_field', array(
  4887. 'options' => array('first', 'second'),
  4888. 'multiple' => 'checkbox',
  4889. 'hiddenField' => false
  4890. ));
  4891. $expected = array(
  4892. array('div' => array('class' => 'input select')),
  4893. array('label' => array('for' => 'ModelMultiField')),
  4894. 'Multi Field',
  4895. '/label',
  4896. array('div' => array('class' => 'checkbox')),
  4897. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4898. array('label' => array('for' => 'ModelMultiField0')),
  4899. 'first',
  4900. '/label',
  4901. '/div',
  4902. array('div' => array('class' => 'checkbox')),
  4903. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4904. array('label' => array('for' => 'ModelMultiField1')),
  4905. 'second',
  4906. '/label',
  4907. '/div',
  4908. '/div'
  4909. );
  4910. $this->assertTags($result, $expected);
  4911. }
  4912. /**
  4913. * test that select() with multiple = checkbox works with overriding name attribute.
  4914. *
  4915. * @return void
  4916. */
  4917. public function testSelectCheckboxMultipleOverrideName() {
  4918. $result = $this->Form->input('category', array(
  4919. 'type' => 'select',
  4920. 'multiple' => 'checkbox',
  4921. 'name' => 'data[fish]',
  4922. 'options' => array('1', '2'),
  4923. 'div' => false,
  4924. 'label' => false,
  4925. ));
  4926. $expected = array(
  4927. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4928. array('div' => array('class' => 'checkbox')),
  4929. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4930. array('label' => array('for' => 'Category0')), '1', '/label',
  4931. '/div',
  4932. array('div' => array('class' => 'checkbox')),
  4933. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4934. array('label' => array('for' => 'Category1')), '2', '/label',
  4935. '/div'
  4936. );
  4937. $this->assertTags($result, $expected);
  4938. }
  4939. /**
  4940. * Test that 'id' overrides all the checkbox id's as well.
  4941. *
  4942. * @return void
  4943. */
  4944. public function testSelectCheckboxMultipleId() {
  4945. $result = $this->Form->select(
  4946. 'Model.multi_field',
  4947. array('first', 'second', 'third'),
  4948. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4949. );
  4950. $expected = array(
  4951. 'input' => array(
  4952. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4953. ),
  4954. array('div' => array('class' => 'checkbox')),
  4955. array('input' => array(
  4956. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4957. 'value' => '0', 'id' => 'CustomId0'
  4958. )),
  4959. array('label' => array('for' => 'CustomId0')),
  4960. 'first',
  4961. '/label',
  4962. '/div',
  4963. array('div' => array('class' => 'checkbox')),
  4964. array('input' => array(
  4965. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4966. 'value' => '1', 'id' => 'CustomId1'
  4967. )),
  4968. array('label' => array('for' => 'CustomId1')),
  4969. 'second',
  4970. '/label',
  4971. '/div',
  4972. array('div' => array('class' => 'checkbox')),
  4973. array('input' => array(
  4974. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4975. 'value' => '2', 'id' => 'CustomId2'
  4976. )),
  4977. array('label' => array('for' => 'CustomId2')),
  4978. 'third',
  4979. '/label',
  4980. '/div'
  4981. );
  4982. $this->assertTags($result, $expected);
  4983. }
  4984. /**
  4985. * testCheckbox method
  4986. *
  4987. * Test generation of checkboxes
  4988. *
  4989. * @return void
  4990. */
  4991. public function testCheckbox() {
  4992. $result = $this->Form->checkbox('Model.field');
  4993. $expected = array(
  4994. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4995. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4996. );
  4997. $this->assertTags($result, $expected);
  4998. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4999. $expected = array(
  5000. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  5001. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  5002. );
  5003. $this->assertTags($result, $expected);
  5004. $Contact = ClassRegistry::getObject('Contact');
  5005. $Contact->validationErrors['field'] = 1;
  5006. $this->Form->request->data['Contact']['field'] = 'myvalue';
  5007. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  5008. $expected = array(
  5009. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5010. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  5011. );
  5012. $this->assertTags($result, $expected);
  5013. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5014. $expected = array(
  5015. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5016. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  5017. );
  5018. $this->assertTags($result, $expected);
  5019. $this->Form->request->data['Contact']['field'] = '';
  5020. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  5021. $expected = array(
  5022. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5023. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  5024. );
  5025. $this->assertTags($result, $expected);
  5026. $Contact->validationErrors = array();
  5027. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5028. $expected = array(
  5029. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5030. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  5031. );
  5032. $this->assertTags($result, $expected);
  5033. $this->Form->request->data['Contact']['published'] = 1;
  5034. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5035. $expected = array(
  5036. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5037. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  5038. );
  5039. $this->assertTags($result, $expected);
  5040. $this->Form->request->data['Contact']['published'] = 0;
  5041. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5042. $expected = array(
  5043. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5044. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  5045. );
  5046. $this->assertTags($result, $expected);
  5047. $result = $this->Form->checkbox('Model.CustomField.1.value');
  5048. $expected = array(
  5049. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  5050. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  5051. );
  5052. $this->assertTags($result, $expected);
  5053. $result = $this->Form->checkbox('CustomField.1.value');
  5054. $expected = array(
  5055. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  5056. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  5057. );
  5058. $this->assertTags($result, $expected);
  5059. }
  5060. /**
  5061. * test checkbox() with a custom name attribute
  5062. *
  5063. * @return void
  5064. */
  5065. public function testCheckboxCustomNameAttribute() {
  5066. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  5067. $expected = array(
  5068. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  5069. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  5070. );
  5071. $this->assertTags($result, $expected);
  5072. }
  5073. /**
  5074. * test the checked option for checkboxes.
  5075. *
  5076. * @return void
  5077. */
  5078. public function testCheckboxCheckedOption() {
  5079. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  5080. $expected = array(
  5081. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5082. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5083. );
  5084. $this->assertTags($result, $expected);
  5085. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  5086. $expected = array(
  5087. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5088. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5089. );
  5090. $this->assertTags($result, $expected);
  5091. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  5092. $expected = array(
  5093. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5094. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5095. );
  5096. $this->assertTags($result, $expected);
  5097. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5098. $expected = array(
  5099. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5100. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5101. );
  5102. $this->assertTags($result, $expected);
  5103. $this->Form->request->data['Model']['field'] = 1;
  5104. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5105. $expected = array(
  5106. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5107. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5108. );
  5109. $this->assertTags($result, $expected);
  5110. }
  5111. /**
  5112. * Test that disabled attribute works on both the checkbox and hidden input.
  5113. *
  5114. * @return void
  5115. */
  5116. public function testCheckboxDisabling() {
  5117. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5118. $expected = array(
  5119. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5120. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5121. );
  5122. $this->assertTags($result, $expected);
  5123. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5124. $expected = array(
  5125. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5126. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5127. );
  5128. $this->assertTags($result, $expected);
  5129. }
  5130. /**
  5131. * Test that the hidden input for checkboxes can be omitted or set to a
  5132. * specific value.
  5133. *
  5134. * @return void
  5135. */
  5136. public function testCheckboxHiddenField() {
  5137. $result = $this->Form->input('UserForm.something', array(
  5138. 'type' => 'checkbox',
  5139. 'hiddenField' => false
  5140. ));
  5141. $expected = array(
  5142. 'div' => array('class' => 'input checkbox'),
  5143. array('input' => array(
  5144. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5145. 'value' => '1', 'id' => 'UserFormSomething'
  5146. )),
  5147. 'label' => array('for' => 'UserFormSomething'),
  5148. 'Something',
  5149. '/label',
  5150. '/div'
  5151. );
  5152. $this->assertTags($result, $expected);
  5153. $result = $this->Form->input('UserForm.something', array(
  5154. 'type' => 'checkbox',
  5155. 'value' => 'Y',
  5156. 'hiddenField' => 'N',
  5157. ));
  5158. $expected = array(
  5159. 'div' => array('class' => 'input checkbox'),
  5160. array('input' => array(
  5161. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  5162. 'value' => 'N', 'id' => 'UserFormSomething_'
  5163. )),
  5164. array('input' => array(
  5165. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5166. 'value' => 'Y', 'id' => 'UserFormSomething'
  5167. )),
  5168. 'label' => array('for' => 'UserFormSomething'),
  5169. 'Something',
  5170. '/label',
  5171. '/div'
  5172. );
  5173. $this->assertTags($result, $expected);
  5174. }
  5175. /**
  5176. * testDateTime method
  5177. *
  5178. * Test generation of date/time select elements
  5179. *
  5180. * @return void
  5181. */
  5182. public function testDateTime() {
  5183. extract($this->dateRegex);
  5184. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5185. $now = strtotime('now');
  5186. $expected = array(
  5187. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5188. $daysRegex,
  5189. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5190. date('j', $now),
  5191. '/option',
  5192. '*/select',
  5193. '-',
  5194. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5195. $monthsRegex,
  5196. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5197. date('F', $now),
  5198. '/option',
  5199. '*/select',
  5200. '-',
  5201. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5202. $yearsRegex,
  5203. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5204. date('Y', $now),
  5205. '/option',
  5206. '*/select',
  5207. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5208. $hoursRegex,
  5209. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5210. date('g', $now),
  5211. '/option',
  5212. '*/select',
  5213. ':',
  5214. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5215. $minutesRegex,
  5216. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5217. date('i', $now),
  5218. '/option',
  5219. '*/select',
  5220. ' ',
  5221. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5222. $meridianRegex,
  5223. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5224. date('a', $now),
  5225. '/option',
  5226. '*/select'
  5227. );
  5228. $this->assertTags($result, $expected);
  5229. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5230. $expected = array(
  5231. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5232. $daysRegex,
  5233. array('option' => array('value' => '')),
  5234. '/option',
  5235. '*/select',
  5236. '-',
  5237. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5238. $monthsRegex,
  5239. array('option' => array('value' => '')),
  5240. '/option',
  5241. '*/select',
  5242. '-',
  5243. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5244. $yearsRegex,
  5245. array('option' => array('value' => '')),
  5246. '/option',
  5247. '*/select',
  5248. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5249. $hoursRegex,
  5250. array('option' => array('value' => '')),
  5251. '/option',
  5252. '*/select',
  5253. ':',
  5254. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5255. $minutesRegex,
  5256. array('option' => array('value' => '')),
  5257. '/option',
  5258. '*/select',
  5259. ' ',
  5260. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5261. $meridianRegex,
  5262. array('option' => array('value' => '')),
  5263. '/option',
  5264. '*/select'
  5265. );
  5266. $this->assertTags($result, $expected);
  5267. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5268. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5269. $this->assertTags($result, $expected);
  5270. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5271. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5272. $this->assertTags($result, $expected);
  5273. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5274. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5275. $expected = array(
  5276. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5277. $daysRegex,
  5278. array('option' => array('value' => '')),
  5279. '/option',
  5280. '*/select',
  5281. '-',
  5282. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5283. $monthsRegex,
  5284. array('option' => array('value' => '')),
  5285. '/option',
  5286. '*/select',
  5287. '-',
  5288. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5289. $yearsRegex,
  5290. array('option' => array('value' => '')),
  5291. '/option',
  5292. '*/select',
  5293. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5294. $hoursRegex,
  5295. array('option' => array('value' => '')),
  5296. '/option',
  5297. '*/select',
  5298. ':',
  5299. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5300. $minutesRegex,
  5301. array('option' => array('value' => '')),
  5302. '/option',
  5303. array('option' => array('value' => '00')),
  5304. '00',
  5305. '/option',
  5306. array('option' => array('value' => '05')),
  5307. '05',
  5308. '/option',
  5309. array('option' => array('value' => '10')),
  5310. '10',
  5311. '/option',
  5312. '*/select',
  5313. ' ',
  5314. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5315. $meridianRegex,
  5316. array('option' => array('value' => '')),
  5317. '/option',
  5318. '*/select'
  5319. );
  5320. $this->assertTags($result, $expected);
  5321. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5322. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5323. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5324. $this->Form->request->data['Contact']['data'] = null;
  5325. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5326. $expected = array(
  5327. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5328. $daysRegex,
  5329. array('option' => array('value' => '')),
  5330. '/option',
  5331. '*/select',
  5332. '-',
  5333. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5334. $monthsRegex,
  5335. array('option' => array('value' => '')),
  5336. '/option',
  5337. '*/select',
  5338. '-',
  5339. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5340. $yearsRegex,
  5341. array('option' => array('value' => '')),
  5342. '/option',
  5343. '*/select',
  5344. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5345. $hoursRegex,
  5346. array('option' => array('value' => '')),
  5347. '/option',
  5348. '*/select',
  5349. ':',
  5350. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5351. $minutesRegex,
  5352. array('option' => array('value' => '')),
  5353. '/option',
  5354. '*/select',
  5355. ' ',
  5356. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5357. $meridianRegex,
  5358. array('option' => array('value' => '')),
  5359. '/option',
  5360. '*/select'
  5361. );
  5362. $this->assertTags($result, $expected);
  5363. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5364. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5365. $now = strtotime($this->Form->data['Model']['field']);
  5366. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5367. $expected = array(
  5368. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5369. $daysRegex,
  5370. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5371. date('j', $now),
  5372. '/option',
  5373. '*/select',
  5374. '-',
  5375. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5376. $monthsRegex,
  5377. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5378. date('F', $now),
  5379. '/option',
  5380. '*/select',
  5381. '-',
  5382. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5383. $yearsRegex,
  5384. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5385. date('Y', $now),
  5386. '/option',
  5387. '*/select',
  5388. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5389. $hoursRegex,
  5390. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5391. date('g', $now),
  5392. '/option',
  5393. '*/select',
  5394. ':',
  5395. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5396. $minutesRegex,
  5397. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5398. date('i', $now),
  5399. '/option',
  5400. '*/select',
  5401. ' ',
  5402. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5403. $meridianRegex,
  5404. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5405. date('a', $now),
  5406. '/option',
  5407. '*/select'
  5408. );
  5409. $this->assertTags($result, $expected);
  5410. $selected = strtotime('2008-10-26 12:33:00');
  5411. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5412. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5413. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5414. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5415. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5416. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5417. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5418. $this->Form->create('Contact');
  5419. $result = $this->Form->input('published');
  5420. $now = strtotime('now');
  5421. $expected = array(
  5422. 'div' => array('class' => 'input date'),
  5423. 'label' => array('for' => 'ContactPublishedMonth'),
  5424. 'Published',
  5425. '/label',
  5426. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5427. $monthsRegex,
  5428. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5429. date('F', $now),
  5430. '/option',
  5431. '*/select',
  5432. '-',
  5433. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5434. $daysRegex,
  5435. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5436. date('j', $now),
  5437. '/option',
  5438. '*/select',
  5439. '-',
  5440. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5441. $yearsRegex,
  5442. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5443. date('Y', $now),
  5444. '/option',
  5445. '*/select',
  5446. '/div'
  5447. );
  5448. $this->assertTags($result, $expected);
  5449. $result = $this->Form->input('published2', array('type' => 'date'));
  5450. $now = strtotime('now');
  5451. $expected = array(
  5452. 'div' => array('class' => 'input date'),
  5453. 'label' => array('for' => 'ContactPublished2Month'),
  5454. 'Published2',
  5455. '/label',
  5456. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5457. $monthsRegex,
  5458. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5459. date('F', $now),
  5460. '/option',
  5461. '*/select',
  5462. '-',
  5463. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5464. $daysRegex,
  5465. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5466. date('j', $now),
  5467. '/option',
  5468. '*/select',
  5469. '-',
  5470. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5471. $yearsRegex,
  5472. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5473. date('Y', $now),
  5474. '/option',
  5475. '*/select',
  5476. '/div'
  5477. );
  5478. $this->assertTags($result, $expected);
  5479. $this->Form->create('Contact');
  5480. $result = $this->Form->input('published', array('monthNames' => false));
  5481. $now = strtotime('now');
  5482. $expected = array(
  5483. 'div' => array('class' => 'input date'),
  5484. 'label' => array('for' => 'ContactPublishedMonth'),
  5485. 'Published',
  5486. '/label',
  5487. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5488. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5489. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5490. date('m', $now),
  5491. '/option',
  5492. '*/select',
  5493. '-',
  5494. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5495. $daysRegex,
  5496. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5497. date('j', $now),
  5498. '/option',
  5499. '*/select',
  5500. '-',
  5501. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5502. $yearsRegex,
  5503. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5504. date('Y', $now),
  5505. '/option',
  5506. '*/select',
  5507. '/div'
  5508. );
  5509. $this->assertTags($result, $expected);
  5510. $result = $this->Form->input('published', array(
  5511. 'timeFormat' => 24,
  5512. 'interval' => 5,
  5513. 'selected' => strtotime('2009-09-03 13:37:00'),
  5514. 'type' => 'datetime'
  5515. ));
  5516. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5517. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5518. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5519. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5520. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5521. }
  5522. /**
  5523. * Test dateTime with rounding
  5524. *
  5525. * @return void
  5526. */
  5527. public function testDateTimeRounding() {
  5528. $this->Form->request->data['Contact'] = array(
  5529. 'date' => array(
  5530. 'day' => '13',
  5531. 'month' => '12',
  5532. 'year' => '2010',
  5533. 'hour' => '04',
  5534. 'min' => '19',
  5535. 'meridian' => 'AM'
  5536. )
  5537. );
  5538. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5539. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5540. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5541. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5542. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5543. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5544. }
  5545. /**
  5546. * Test that empty values don't trigger errors.
  5547. *
  5548. * @return void
  5549. */
  5550. public function testDateTimeNoErrorsOnEmptyData() {
  5551. $this->Form->request->data['Contact'] = array(
  5552. 'date' => array(
  5553. 'day' => '',
  5554. 'month' => '',
  5555. 'year' => '',
  5556. 'hour' => '',
  5557. 'min' => '',
  5558. 'meridian' => ''
  5559. )
  5560. );
  5561. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5562. $this->assertNotEmpty($result);
  5563. }
  5564. /**
  5565. * test that datetime() and default values work.
  5566. *
  5567. * @return void
  5568. */
  5569. public function testDatetimeWithDefault() {
  5570. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5571. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5572. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5573. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5574. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5575. 'default' => '2009-06-01 11:15:30'
  5576. ));
  5577. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5578. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5579. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5580. }
  5581. /**
  5582. * test that bogus non-date time data doesn't cause errors.
  5583. *
  5584. * @return void
  5585. */
  5586. public function testDateTimeWithBogusData() {
  5587. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5588. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5589. }
  5590. /**
  5591. * testDateTimeEmptyAsArray
  5592. *
  5593. * @return void
  5594. */
  5595. public function testDateTimeEmptyAsArray() {
  5596. $result = $this->Form->dateTime('Contact.date',
  5597. 'DMY',
  5598. '12',
  5599. array(
  5600. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5601. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5602. )
  5603. )
  5604. );
  5605. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5606. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5607. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5608. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5609. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5610. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5611. $result = $this->Form->dateTime('Contact.date',
  5612. 'DMY',
  5613. '12',
  5614. array(
  5615. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5616. )
  5617. );
  5618. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5619. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5620. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5621. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5622. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5623. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5624. }
  5625. /**
  5626. * testFormDateTimeMulti method
  5627. *
  5628. * test multiple datetime element generation
  5629. *
  5630. * @return void
  5631. */
  5632. public function testFormDateTimeMulti() {
  5633. extract($this->dateRegex);
  5634. $result = $this->Form->dateTime('Contact.1.updated');
  5635. $expected = array(
  5636. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5637. $daysRegex,
  5638. array('option' => array('value' => '')),
  5639. '/option',
  5640. '*/select',
  5641. '-',
  5642. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5643. $monthsRegex,
  5644. array('option' => array('value' => '')),
  5645. '/option',
  5646. '*/select',
  5647. '-',
  5648. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5649. $yearsRegex,
  5650. array('option' => array('value' => '')),
  5651. '/option',
  5652. '*/select',
  5653. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5654. $hoursRegex,
  5655. array('option' => array('value' => '')),
  5656. '/option',
  5657. '*/select',
  5658. ':',
  5659. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5660. $minutesRegex,
  5661. array('option' => array('value' => '')),
  5662. '/option',
  5663. '*/select',
  5664. ' ',
  5665. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5666. $meridianRegex,
  5667. array('option' => array('value' => '')),
  5668. '/option',
  5669. '*/select'
  5670. );
  5671. $this->assertTags($result, $expected);
  5672. $result = $this->Form->dateTime('Contact.2.updated');
  5673. $expected = array(
  5674. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5675. $daysRegex,
  5676. array('option' => array('value' => '')),
  5677. '/option',
  5678. '*/select',
  5679. '-',
  5680. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5681. $monthsRegex,
  5682. array('option' => array('value' => '')),
  5683. '/option',
  5684. '*/select',
  5685. '-',
  5686. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5687. $yearsRegex,
  5688. array('option' => array('value' => '')),
  5689. '/option',
  5690. '*/select',
  5691. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5692. $hoursRegex,
  5693. array('option' => array('value' => '')),
  5694. '/option',
  5695. '*/select',
  5696. ':',
  5697. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5698. $minutesRegex,
  5699. array('option' => array('value' => '')),
  5700. '/option',
  5701. '*/select',
  5702. ' ',
  5703. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5704. $meridianRegex,
  5705. array('option' => array('value' => '')),
  5706. '/option',
  5707. '*/select'
  5708. );
  5709. $this->assertTags($result, $expected);
  5710. }
  5711. /**
  5712. * When changing the date format, the label should always focus the first select box when
  5713. * clicked.
  5714. *
  5715. * @return void
  5716. */
  5717. public function testDateTimeLabelIdMatchesFirstInput() {
  5718. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5719. $this->assertContains('label for="ModelDateMonth"', $result);
  5720. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5721. $this->assertContains('label for="ModelDateDay"', $result);
  5722. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5723. $this->assertContains('label for="ModelDateYear"', $result);
  5724. }
  5725. /**
  5726. * testMonth method
  5727. *
  5728. * @return void
  5729. */
  5730. public function testMonth() {
  5731. $result = $this->Form->month('Model.field');
  5732. $expected = array(
  5733. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5734. array('option' => array('value' => '')),
  5735. '/option',
  5736. array('option' => array('value' => '01')),
  5737. date('F', strtotime('2008-01-01 00:00:00')),
  5738. '/option',
  5739. array('option' => array('value' => '02')),
  5740. date('F', strtotime('2008-02-01 00:00:00')),
  5741. '/option',
  5742. '*/select',
  5743. );
  5744. $this->assertTags($result, $expected);
  5745. $result = $this->Form->month('Model.field', array('empty' => true));
  5746. $expected = array(
  5747. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5748. array('option' => array('value' => '')),
  5749. '/option',
  5750. array('option' => array('value' => '01')),
  5751. date('F', strtotime('2008-01-01 00:00:00')),
  5752. '/option',
  5753. array('option' => array('value' => '02')),
  5754. date('F', strtotime('2008-02-01 00:00:00')),
  5755. '/option',
  5756. '*/select',
  5757. );
  5758. $this->assertTags($result, $expected);
  5759. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5760. $expected = array(
  5761. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5762. array('option' => array('value' => '')),
  5763. '/option',
  5764. array('option' => array('value' => '01')),
  5765. '01',
  5766. '/option',
  5767. array('option' => array('value' => '02')),
  5768. '02',
  5769. '/option',
  5770. '*/select',
  5771. );
  5772. $this->assertTags($result, $expected);
  5773. $monthNames = array(
  5774. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5775. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5776. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5777. $expected = array(
  5778. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5779. array('option' => array('value' => '')),
  5780. '/option',
  5781. array('option' => array('value' => '01')),
  5782. 'Jan',
  5783. '/option',
  5784. array('option' => array('value' => '02')),
  5785. 'Feb',
  5786. '/option',
  5787. '*/select',
  5788. );
  5789. $this->assertTags($result, $expected);
  5790. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5791. $result = $this->Form->month('Project.release');
  5792. $expected = array(
  5793. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  5794. array('option' => array('value' => '')),
  5795. '/option',
  5796. array('option' => array('value' => '01')),
  5797. 'January',
  5798. '/option',
  5799. array('option' => array('value' => '02', 'selected' => 'selected')),
  5800. 'February',
  5801. '/option',
  5802. '*/select',
  5803. );
  5804. $this->assertTags($result, $expected);
  5805. }
  5806. /**
  5807. * testDay method
  5808. *
  5809. * @return void
  5810. */
  5811. public function testDay() {
  5812. extract($this->dateRegex);
  5813. $result = $this->Form->day('Model.field', array('value' => false));
  5814. $expected = array(
  5815. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5816. array('option' => array('value' => '')),
  5817. '/option',
  5818. array('option' => array('value' => '01')),
  5819. '1',
  5820. '/option',
  5821. array('option' => array('value' => '02')),
  5822. '2',
  5823. '/option',
  5824. $daysRegex,
  5825. '/select',
  5826. );
  5827. $this->assertTags($result, $expected);
  5828. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5829. $result = $this->Form->day('Model.field');
  5830. $expected = array(
  5831. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5832. array('option' => array('value' => '')),
  5833. '/option',
  5834. array('option' => array('value' => '01')),
  5835. '1',
  5836. '/option',
  5837. array('option' => array('value' => '02')),
  5838. '2',
  5839. '/option',
  5840. $daysRegex,
  5841. array('option' => array('value' => '10', 'selected' => 'selected')),
  5842. '10',
  5843. '/option',
  5844. $daysRegex,
  5845. '/select',
  5846. );
  5847. $this->assertTags($result, $expected);
  5848. $this->Form->request->data['Model']['field'] = '';
  5849. $result = $this->Form->day('Model.field', array('value' => '10'));
  5850. $expected = array(
  5851. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5852. array('option' => array('value' => '')),
  5853. '/option',
  5854. array('option' => array('value' => '01')),
  5855. '1',
  5856. '/option',
  5857. array('option' => array('value' => '02')),
  5858. '2',
  5859. '/option',
  5860. $daysRegex,
  5861. array('option' => array('value' => '10', 'selected' => 'selected')),
  5862. '10',
  5863. '/option',
  5864. $daysRegex,
  5865. '/select',
  5866. );
  5867. $this->assertTags($result, $expected);
  5868. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5869. $result = $this->Form->day('Model.field', array('value' => true));
  5870. $expected = array(
  5871. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5872. array('option' => array('value' => '')),
  5873. '/option',
  5874. array('option' => array('value' => '01')),
  5875. '1',
  5876. '/option',
  5877. array('option' => array('value' => '02')),
  5878. '2',
  5879. '/option',
  5880. $daysRegex,
  5881. array('option' => array('value' => '10', 'selected' => 'selected')),
  5882. '10',
  5883. '/option',
  5884. $daysRegex,
  5885. '/select',
  5886. );
  5887. $this->assertTags($result, $expected);
  5888. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5889. $result = $this->Form->day('Project.release');
  5890. $expected = array(
  5891. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  5892. array('option' => array('value' => '')),
  5893. '/option',
  5894. array('option' => array('value' => '01')),
  5895. '1',
  5896. '/option',
  5897. array('option' => array('value' => '02')),
  5898. '2',
  5899. '/option',
  5900. $daysRegex,
  5901. array('option' => array('value' => '10', 'selected' => 'selected')),
  5902. '10',
  5903. '/option',
  5904. $daysRegex,
  5905. '/select',
  5906. );
  5907. $this->assertTags($result, $expected);
  5908. }
  5909. /**
  5910. * testMinute method
  5911. *
  5912. * @return void
  5913. */
  5914. public function testMinute() {
  5915. extract($this->dateRegex);
  5916. $result = $this->Form->minute('Model.field');
  5917. $expected = array(
  5918. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5919. array('option' => array('value' => '')),
  5920. '/option',
  5921. array('option' => array('value' => '00')),
  5922. '00',
  5923. '/option',
  5924. array('option' => array('value' => '01')),
  5925. '01',
  5926. '/option',
  5927. array('option' => array('value' => '02')),
  5928. '02',
  5929. '/option',
  5930. $minutesRegex,
  5931. '/select',
  5932. );
  5933. $this->assertTags($result, $expected);
  5934. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5935. $result = $this->Form->minute('Model.field');
  5936. $expected = array(
  5937. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5938. array('option' => array('value' => '')),
  5939. '/option',
  5940. array('option' => array('value' => '00')),
  5941. '00',
  5942. '/option',
  5943. array('option' => array('value' => '01')),
  5944. '01',
  5945. '/option',
  5946. array('option' => array('value' => '02')),
  5947. '02',
  5948. '/option',
  5949. $minutesRegex,
  5950. array('option' => array('value' => '12', 'selected' => 'selected')),
  5951. '12',
  5952. '/option',
  5953. $minutesRegex,
  5954. '/select',
  5955. );
  5956. $this->assertTags($result, $expected);
  5957. $this->Form->request->data['Model']['field'] = '';
  5958. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5959. $expected = array(
  5960. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5961. array('option' => array('value' => '')),
  5962. '/option',
  5963. array('option' => array('value' => '00')),
  5964. '00',
  5965. '/option',
  5966. array('option' => array('value' => '05')),
  5967. '05',
  5968. '/option',
  5969. array('option' => array('value' => '10')),
  5970. '10',
  5971. '/option',
  5972. $minutesRegex,
  5973. '/select',
  5974. );
  5975. $this->assertTags($result, $expected);
  5976. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5977. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5978. $expected = array(
  5979. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5980. array('option' => array('value' => '')),
  5981. '/option',
  5982. array('option' => array('value' => '00')),
  5983. '00',
  5984. '/option',
  5985. array('option' => array('value' => '05')),
  5986. '05',
  5987. '/option',
  5988. array('option' => array('value' => '10', 'selected' => 'selected')),
  5989. '10',
  5990. '/option',
  5991. $minutesRegex,
  5992. '/select',
  5993. );
  5994. $this->assertTags($result, $expected);
  5995. }
  5996. /**
  5997. * testHour method
  5998. *
  5999. * @return void
  6000. */
  6001. public function testHour() {
  6002. extract($this->dateRegex);
  6003. $result = $this->Form->hour('Model.field', false);
  6004. $expected = array(
  6005. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  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. $hoursRegex,
  6015. '/select',
  6016. );
  6017. $this->assertTags($result, $expected);
  6018. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6019. $result = $this->Form->hour('Model.field', false);
  6020. $expected = array(
  6021. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  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. $hoursRegex,
  6031. array('option' => array('value' => '12', 'selected' => 'selected')),
  6032. '12',
  6033. '/option',
  6034. '/select',
  6035. );
  6036. $this->assertTags($result, $expected);
  6037. $this->Form->request->data['Model']['field'] = '';
  6038. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  6039. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6040. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  6041. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6042. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6043. $result = $this->Form->hour('Model.field', true);
  6044. $expected = array(
  6045. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6046. array('option' => array('value' => '')),
  6047. '/option',
  6048. array('option' => array('value' => '00', 'selected' => 'selected')),
  6049. '0',
  6050. '/option',
  6051. array('option' => array('value' => '01')),
  6052. '1',
  6053. '/option',
  6054. array('option' => array('value' => '02')),
  6055. '2',
  6056. '/option',
  6057. $hoursRegex,
  6058. '/select',
  6059. );
  6060. $this->assertTags($result, $expected);
  6061. unset($this->Form->request->data['Model']['field']);
  6062. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  6063. $thisHour = date('H');
  6064. $optValue = date('G');
  6065. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6066. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6067. $result = $this->Form->hour('Model.field', true);
  6068. $expected = array(
  6069. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6070. array('option' => array('value' => '')),
  6071. '/option',
  6072. array('option' => array('value' => '00')),
  6073. '0',
  6074. '/option',
  6075. array('option' => array('value' => '01', 'selected' => 'selected')),
  6076. '1',
  6077. '/option',
  6078. array('option' => array('value' => '02')),
  6079. '2',
  6080. '/option',
  6081. $hoursRegex,
  6082. '/select',
  6083. );
  6084. $this->assertTags($result, $expected);
  6085. }
  6086. /**
  6087. * testYear method
  6088. *
  6089. * @return void
  6090. */
  6091. public function testYear() {
  6092. $result = $this->Form->year('Model.field', 2006, 2007);
  6093. $expected = array(
  6094. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6095. array('option' => array('value' => '')),
  6096. '/option',
  6097. array('option' => array('value' => '2007')),
  6098. '2007',
  6099. '/option',
  6100. array('option' => array('value' => '2006')),
  6101. '2006',
  6102. '/option',
  6103. '/select',
  6104. );
  6105. $this->assertTags($result, $expected);
  6106. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  6107. $expected = array(
  6108. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6109. array('option' => array('value' => '')),
  6110. '/option',
  6111. array('option' => array('value' => '2006')),
  6112. '2006',
  6113. '/option',
  6114. array('option' => array('value' => '2007')),
  6115. '2007',
  6116. '/option',
  6117. '/select',
  6118. );
  6119. $this->assertTags($result, $expected);
  6120. $this->request->data['Contact']['published'] = '';
  6121. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6122. $expected = array(
  6123. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6124. array('option' => array('value' => '')),
  6125. '/option',
  6126. array('option' => array('value' => '2007')),
  6127. '2007',
  6128. '/option',
  6129. array('option' => array('value' => '2006')),
  6130. '2006',
  6131. '/option',
  6132. '/select',
  6133. );
  6134. $this->assertTags($result, $expected);
  6135. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6136. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6137. $expected = array(
  6138. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6139. array('option' => array('value' => '2007')),
  6140. '2007',
  6141. '/option',
  6142. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6143. '2006',
  6144. '/option',
  6145. '/select',
  6146. );
  6147. $this->assertTags($result, $expected);
  6148. $this->Form->request->data['Contact']['published'] = '';
  6149. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6150. $expected = array(
  6151. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6152. array('option' => array('value' => '')),
  6153. '/option',
  6154. array('option' => array('value' => '2007')),
  6155. '2007',
  6156. '/option',
  6157. array('option' => array('value' => '2006')),
  6158. '2006',
  6159. '/option',
  6160. '/select',
  6161. );
  6162. $this->assertTags($result, $expected);
  6163. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6164. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6165. $expected = array(
  6166. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6167. array('option' => array('value' => '2007')),
  6168. '2007',
  6169. '/option',
  6170. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6171. '2006',
  6172. '/option',
  6173. '/select',
  6174. );
  6175. $this->assertTags($result, $expected);
  6176. $this->Form->request->data['Contact']['published'] = '';
  6177. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6178. $expected = array(
  6179. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6180. array('option' => array('value' => '')),
  6181. '/option',
  6182. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6183. '2007',
  6184. '/option',
  6185. array('option' => array('value' => '2006')),
  6186. '2006',
  6187. '/option',
  6188. '/select',
  6189. );
  6190. $this->assertTags($result, $expected);
  6191. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6192. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6193. $expected = array(
  6194. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6195. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6196. '2007',
  6197. '/option',
  6198. array('option' => array('value' => '2006')),
  6199. '2006',
  6200. '/option',
  6201. '/select',
  6202. );
  6203. $this->assertTags($result, $expected);
  6204. $this->Form->request->data['Contact']['published'] = '';
  6205. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6206. $expected = array(
  6207. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6208. array('option' => array('value' => '2008')),
  6209. '2008',
  6210. '/option',
  6211. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6212. '2007',
  6213. '/option',
  6214. array('option' => array('value' => '2006')),
  6215. '2006',
  6216. '/option',
  6217. '/select',
  6218. );
  6219. $this->assertTags($result, $expected);
  6220. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6221. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6222. $expected = array(
  6223. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6224. array('option' => array('value' => '2008')),
  6225. '2008',
  6226. '/option',
  6227. array('option' => array('value' => '2007')),
  6228. '2007',
  6229. '/option',
  6230. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6231. '2006',
  6232. '/option',
  6233. '/select',
  6234. );
  6235. $this->assertTags($result, $expected);
  6236. $this->Form->request->data = array();
  6237. $this->Form->create('Contact');
  6238. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6239. $expected = array(
  6240. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6241. array('option' => array('value' => '2008')),
  6242. '2008',
  6243. '/option',
  6244. array('option' => array('value' => '2007')),
  6245. '2007',
  6246. '/option',
  6247. array('option' => array('value' => '2006')),
  6248. '2006',
  6249. '/option',
  6250. '/select',
  6251. );
  6252. $this->assertTags($result, $expected);
  6253. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6254. $this->assertContains('data[Contact][published][year]', $result);
  6255. }
  6256. /**
  6257. * testYearAutoExpandRange method
  6258. *
  6259. * @return void
  6260. */
  6261. public function testYearAutoExpandRange() {
  6262. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6263. $result = $this->Form->year('User.birthday');
  6264. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6265. $result = $matches[1];
  6266. $expected = range(date('Y') + 20, 1930);
  6267. $this->assertEquals($expected, $result);
  6268. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6269. $result = $this->Form->year('Project.release');
  6270. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6271. $result = $matches[1];
  6272. $expected = range(2050, date('Y') - 20);
  6273. $this->assertEquals($expected, $result);
  6274. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6275. $result = $this->Form->year('Project.release', 1890, 1900);
  6276. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6277. $result = $matches[1];
  6278. $expected = range(1900, 1881);
  6279. $this->assertEquals($expected, $result);
  6280. }
  6281. /**
  6282. * testInputDate method
  6283. *
  6284. * Test various inputs with type date and different dateFormat values.
  6285. * Failing to provide a dateFormat key should not error.
  6286. * It should simply not pre-select any value then.
  6287. *
  6288. * @return void
  6289. */
  6290. public function testInputDate() {
  6291. $this->Form->request->data = array(
  6292. 'User' => array(
  6293. 'month_year' => array('month' => date('m')),
  6294. 'just_year' => array('month' => date('m')),
  6295. 'just_month' => array('year' => date('Y')),
  6296. 'just_day' => array('month' => date('m')),
  6297. )
  6298. );
  6299. $this->Form->create('User');
  6300. $result = $this->Form->input('month_year',
  6301. array(
  6302. 'label' => false,
  6303. 'div' => false,
  6304. 'type' => 'date',
  6305. 'dateFormat' => 'MY',
  6306. 'minYear' => 2006,
  6307. 'maxYear' => 2008
  6308. )
  6309. );
  6310. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6311. $this->assertNotContains('value="2008" selected="selected"', $result);
  6312. $result = $this->Form->input('just_year',
  6313. array(
  6314. 'type' => 'date',
  6315. 'label' => false,
  6316. 'dateFormat' => 'Y',
  6317. 'minYear' => date('Y'),
  6318. 'maxYear' => date('Y', strtotime('+20 years'))
  6319. )
  6320. );
  6321. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6322. $result = $this->Form->input('just_month',
  6323. array(
  6324. 'type' => 'date',
  6325. 'label' => false,
  6326. 'dateFormat' => 'M',
  6327. 'empty' => false,
  6328. )
  6329. );
  6330. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6331. $result = $this->Form->input('just_day',
  6332. array(
  6333. 'type' => 'date',
  6334. 'label' => false,
  6335. 'dateFormat' => 'D',
  6336. 'empty' => false,
  6337. )
  6338. );
  6339. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6340. }
  6341. /**
  6342. * testInputDateMaxYear method
  6343. *
  6344. * Let's say we want to only allow users born from 2006 to 2008 to register
  6345. * This being the first singup page, we still don't have any data
  6346. *
  6347. * @return void
  6348. */
  6349. public function testInputDateMaxYear() {
  6350. $this->Form->request->data = array();
  6351. $this->Form->create('User');
  6352. $result = $this->Form->input('birthday',
  6353. array(
  6354. 'label' => false,
  6355. 'div' => false,
  6356. 'type' => 'date',
  6357. 'dateFormat' => 'DMY',
  6358. 'minYear' => 2006,
  6359. 'maxYear' => 2008
  6360. )
  6361. );
  6362. $this->assertContains('value="2008" selected="selected"', $result);
  6363. }
  6364. /**
  6365. * testTextArea method
  6366. *
  6367. * @return void
  6368. */
  6369. public function testTextArea() {
  6370. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6371. $result = $this->Form->textarea('Model.field');
  6372. $expected = array(
  6373. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6374. 'some test data',
  6375. '/textarea',
  6376. );
  6377. $this->assertTags($result, $expected);
  6378. $result = $this->Form->textarea('Model.tmp');
  6379. $expected = array(
  6380. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6381. '/textarea',
  6382. );
  6383. $this->assertTags($result, $expected);
  6384. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6385. $result = $this->Form->textarea('Model.field');
  6386. $expected = array(
  6387. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6388. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6389. '/textarea',
  6390. );
  6391. $this->assertTags($result, $expected);
  6392. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6393. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6394. $expected = array(
  6395. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6396. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6397. '/textarea',
  6398. );
  6399. $this->assertTags($result, $expected);
  6400. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6401. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6402. $expected = array(
  6403. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6404. '/textarea'
  6405. );
  6406. $this->assertTags($result, $expected);
  6407. }
  6408. /**
  6409. * testTextAreaWithStupidCharacters method
  6410. *
  6411. * test text area with non-ascii characters
  6412. *
  6413. * @return void
  6414. */
  6415. public function testTextAreaWithStupidCharacters() {
  6416. $this->loadFixtures('Post');
  6417. $result = $this->Form->input('Post.content', array(
  6418. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6419. ));
  6420. $expected = array(
  6421. 'div' => array('class' => 'input textarea'),
  6422. 'label' => array('for' => 'PostContent'),
  6423. 'Current Text',
  6424. '/label',
  6425. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6426. 'GREAT®',
  6427. '/textarea',
  6428. '/div'
  6429. );
  6430. $this->assertTags($result, $expected);
  6431. }
  6432. /**
  6433. * testHiddenField method
  6434. *
  6435. * @return void
  6436. */
  6437. public function testHiddenField() {
  6438. $Contact = ClassRegistry::getObject('Contact');
  6439. $Contact->validationErrors['field'] = 1;
  6440. $this->Form->request->data['Contact']['field'] = 'test';
  6441. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6442. $this->assertTags($result, array(
  6443. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6444. );
  6445. }
  6446. /**
  6447. * testFileUploadField method
  6448. *
  6449. * @return void
  6450. */
  6451. public function testFileUploadField() {
  6452. $result = $this->Form->file('Model.upload');
  6453. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6454. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6455. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6456. $expected = array(
  6457. 'div' => array('class' => 'input file'),
  6458. 'label' => array('for' => 'ModelUpload'),
  6459. 'Upload',
  6460. '/label',
  6461. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6462. '/div'
  6463. );
  6464. $this->assertTags($result, $expected);
  6465. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6466. $result = $this->Form->file('Model.upload');
  6467. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6468. }
  6469. /**
  6470. * test File upload input on a model not used in create();
  6471. *
  6472. * @return void
  6473. */
  6474. public function testFileUploadOnOtherModel() {
  6475. $this->Form->create('ValidateUser', array('type' => 'file'));
  6476. $result = $this->Form->file('ValidateProfile.city');
  6477. $expected = array(
  6478. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6479. );
  6480. $this->assertTags($result, $expected);
  6481. }
  6482. /**
  6483. * testButton method
  6484. *
  6485. * @return void
  6486. */
  6487. public function testButton() {
  6488. $result = $this->Form->button('Hi');
  6489. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6490. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6491. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6492. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6493. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6494. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6495. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6496. $result = $this->Form->button('No type', array('type' => false));
  6497. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6498. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6499. $this->assertNotRegExp('/\&039/', $result);
  6500. }
  6501. /**
  6502. * Test that button() makes unlocked fields by default.
  6503. *
  6504. * @return void
  6505. */
  6506. public function testButtonUnlockedByDefault() {
  6507. $this->Form->request->params['_Token']['key'] = 'secured';
  6508. $this->Form->button('Save', array('name' => 'save'));
  6509. $this->Form->button('Clear');
  6510. $result = $this->Form->unlockField();
  6511. $this->assertEquals(array('save'), $result);
  6512. }
  6513. /**
  6514. * testPostButton method
  6515. *
  6516. * @return void
  6517. */
  6518. public function testPostButton() {
  6519. $result = $this->Form->postButton('Hi', '/controller/action');
  6520. $this->assertTags($result, array(
  6521. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6522. 'div' => array('style' => 'display:none;'),
  6523. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6524. '/div',
  6525. 'button' => array('type' => 'submit'),
  6526. 'Hi',
  6527. '/button',
  6528. '/form'
  6529. ));
  6530. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6531. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6532. }
  6533. /**
  6534. * Test that postButton adds _Token fields.
  6535. *
  6536. * @return void
  6537. */
  6538. public function testSecurePostButton() {
  6539. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6540. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6541. $expected = array(
  6542. 'form' => array(
  6543. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6544. ),
  6545. array('div' => array('style' => 'display:none;')),
  6546. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6547. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6548. '/div',
  6549. 'button' => array('type' => 'submit'),
  6550. 'Delete',
  6551. '/button',
  6552. array('div' => array('style' => 'display:none;')),
  6553. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6554. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6555. '/div',
  6556. '/form',
  6557. );
  6558. $this->assertTags($result, $expected);
  6559. }
  6560. /**
  6561. * testPostLink method
  6562. *
  6563. * @return void
  6564. */
  6565. public function testPostLink() {
  6566. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6567. $this->assertTags($result, array(
  6568. 'form' => array(
  6569. 'method' => 'post', 'action' => '/posts/delete/1',
  6570. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6571. ),
  6572. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6573. '/form',
  6574. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6575. 'Delete',
  6576. '/a'
  6577. ));
  6578. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6579. $this->assertTags($result, array(
  6580. 'form' => array(
  6581. 'method' => 'post', 'action' => '/posts/delete/1',
  6582. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6583. ),
  6584. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6585. '/form',
  6586. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6587. 'Delete',
  6588. '/a'
  6589. ));
  6590. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6591. $this->assertTags($result, array(
  6592. 'form' => array(
  6593. 'method' => 'post', 'action' => '/posts/delete/1',
  6594. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6595. ),
  6596. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6597. '/form',
  6598. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6599. 'Delete',
  6600. '/a'
  6601. ));
  6602. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  6603. $this->assertTags($result, array(
  6604. 'form' => array(
  6605. 'method' => 'post', 'action' => '/posts/delete/1',
  6606. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6607. ),
  6608. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6609. '/form',
  6610. '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;/'),
  6611. 'Delete',
  6612. '/a'
  6613. ));
  6614. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6615. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6616. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6617. $this->assertTags($result, array(
  6618. 'form' => array(
  6619. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6620. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6621. ),
  6622. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6623. '/form',
  6624. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6625. 'Delete',
  6626. '/a'
  6627. ));
  6628. $result = $this->Form->postLink(
  6629. '',
  6630. array('controller' => 'items', 'action' => 'delete', 10),
  6631. array('class' => 'btn btn-danger', 'escape' => false),
  6632. 'Confirm thing'
  6633. );
  6634. $this->assertTags($result, array(
  6635. 'form' => array(
  6636. 'method' => 'post', 'action' => '/items/delete/10',
  6637. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6638. ),
  6639. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6640. '/form',
  6641. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6642. '/a'
  6643. ));
  6644. }
  6645. /**
  6646. * Test that postLink adds _Token fields.
  6647. *
  6648. * @return void
  6649. */
  6650. public function testSecurePostLink() {
  6651. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6652. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6653. $expected = array(
  6654. 'form' => array(
  6655. 'method' => 'post', 'action' => '/posts/delete/1',
  6656. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6657. ),
  6658. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6659. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6660. 'div' => array('style' => 'display:none;'),
  6661. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6662. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6663. '/div',
  6664. '/form',
  6665. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6666. 'Delete',
  6667. '/a'
  6668. );
  6669. $this->assertTags($result, $expected);
  6670. }
  6671. /**
  6672. * testSubmitButton method
  6673. *
  6674. * @return void
  6675. */
  6676. public function testSubmitButton() {
  6677. $result = $this->Form->submit('');
  6678. $expected = array(
  6679. 'div' => array('class' => 'submit'),
  6680. 'input' => array('type' => 'submit', 'value' => ''),
  6681. '/div'
  6682. );
  6683. $this->assertTags($result, $expected);
  6684. $result = $this->Form->submit('Test Submit');
  6685. $expected = array(
  6686. 'div' => array('class' => 'submit'),
  6687. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6688. '/div'
  6689. );
  6690. $this->assertTags($result, $expected);
  6691. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6692. $expected = array(
  6693. 'span' => array('class' => 'submit'),
  6694. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6695. '/span'
  6696. );
  6697. $this->assertTags($result, $expected);
  6698. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6699. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6700. $this->assertTags($result, $expected);
  6701. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6702. $expected = array(
  6703. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6704. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6705. '/div'
  6706. );
  6707. $this->assertTags($result, $expected);
  6708. $result = $this->Form->submit('Next >');
  6709. $expected = array(
  6710. 'div' => array('class' => 'submit'),
  6711. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6712. '/div'
  6713. );
  6714. $this->assertTags($result, $expected);
  6715. $result = $this->Form->submit('Next >', array('escape' => false));
  6716. $expected = array(
  6717. 'div' => array('class' => 'submit'),
  6718. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6719. '/div'
  6720. );
  6721. $this->assertTags($result, $expected);
  6722. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6723. $expected = array(
  6724. 'div' => array('class' => 'submit'),
  6725. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6726. '/div'
  6727. );
  6728. $this->assertTags($result, $expected);
  6729. $before = '--before--';
  6730. $after = '--after--';
  6731. $result = $this->Form->submit('Test', array('before' => $before));
  6732. $expected = array(
  6733. 'div' => array('class' => 'submit'),
  6734. '--before--',
  6735. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6736. '/div'
  6737. );
  6738. $this->assertTags($result, $expected);
  6739. $result = $this->Form->submit('Test', array('after' => $after));
  6740. $expected = array(
  6741. 'div' => array('class' => 'submit'),
  6742. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6743. '--after--',
  6744. '/div'
  6745. );
  6746. $this->assertTags($result, $expected);
  6747. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6748. $expected = array(
  6749. 'div' => array('class' => 'submit'),
  6750. '--before--',
  6751. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6752. '--after--',
  6753. '/div'
  6754. );
  6755. $this->assertTags($result, $expected);
  6756. }
  6757. /**
  6758. * test image submit types.
  6759. *
  6760. * @return void
  6761. */
  6762. public function testSubmitImage() {
  6763. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6764. $expected = array(
  6765. 'div' => array('class' => 'submit'),
  6766. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6767. '/div'
  6768. );
  6769. $this->assertTags($result, $expected);
  6770. $result = $this->Form->submit('/relative/cake.power.gif');
  6771. $expected = array(
  6772. 'div' => array('class' => 'submit'),
  6773. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  6774. '/div'
  6775. );
  6776. $this->assertTags($result, $expected);
  6777. $result = $this->Form->submit('cake.power.gif');
  6778. $expected = array(
  6779. 'div' => array('class' => 'submit'),
  6780. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6781. '/div'
  6782. );
  6783. $this->assertTags($result, $expected);
  6784. $result = $this->Form->submit('Not.an.image');
  6785. $expected = array(
  6786. 'div' => array('class' => 'submit'),
  6787. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6788. '/div'
  6789. );
  6790. $this->assertTags($result, $expected);
  6791. $after = '--after--';
  6792. $before = '--before--';
  6793. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6794. $expected = array(
  6795. 'div' => array('class' => 'submit'),
  6796. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6797. '--after--',
  6798. '/div'
  6799. );
  6800. $this->assertTags($result, $expected);
  6801. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6802. $expected = array(
  6803. 'div' => array('class' => 'submit'),
  6804. '--before--',
  6805. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6806. '/div'
  6807. );
  6808. $this->assertTags($result, $expected);
  6809. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6810. $expected = array(
  6811. 'div' => array('class' => 'submit'),
  6812. '--before--',
  6813. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6814. '--after--',
  6815. '/div'
  6816. );
  6817. $this->assertTags($result, $expected);
  6818. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6819. $expected = array(
  6820. 'div' => array('class' => 'submit'),
  6821. '--before--',
  6822. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6823. '--after--',
  6824. '/div'
  6825. );
  6826. $this->assertTags($result, $expected);
  6827. }
  6828. /**
  6829. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6830. * be submitted at a time.
  6831. *
  6832. * @return void
  6833. */
  6834. public function testSubmitUnlockedByDefault() {
  6835. $this->Form->request->params['_Token']['key'] = 'secured';
  6836. $this->Form->submit('Go go');
  6837. $this->Form->submit('Save', array('name' => 'save'));
  6838. $result = $this->Form->unlockField();
  6839. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6840. }
  6841. /**
  6842. * Test submit image with timestamps.
  6843. *
  6844. * @return void
  6845. */
  6846. public function testSubmitImageTimestamp() {
  6847. Configure::write('Asset.timestamp', 'force');
  6848. $result = $this->Form->submit('cake.power.gif');
  6849. $expected = array(
  6850. 'div' => array('class' => 'submit'),
  6851. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6852. '/div'
  6853. );
  6854. $this->assertTags($result, $expected);
  6855. }
  6856. /**
  6857. * test the create() method
  6858. *
  6859. * @return void
  6860. */
  6861. public function testCreate() {
  6862. $result = $this->Form->create('Contact');
  6863. $encoding = strtolower(Configure::read('App.encoding'));
  6864. $expected = array(
  6865. 'form' => array(
  6866. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6867. 'accept-charset' => $encoding
  6868. ),
  6869. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6870. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6871. '/div'
  6872. );
  6873. $this->assertTags($result, $expected);
  6874. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6875. $expected = array('form' => array(
  6876. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6877. 'accept-charset' => $encoding
  6878. ));
  6879. $this->assertTags($result, $expected);
  6880. $result = $this->Form->create('Contact', array('type' => 'get'));
  6881. $expected = array('form' => array(
  6882. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6883. 'accept-charset' => $encoding
  6884. ));
  6885. $this->assertTags($result, $expected);
  6886. $result = $this->Form->create('Contact', array('type' => 'put'));
  6887. $expected = array(
  6888. 'form' => array(
  6889. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6890. 'accept-charset' => $encoding
  6891. ),
  6892. 'div' => array('style' => 'display:none;'),
  6893. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6894. '/div'
  6895. );
  6896. $this->assertTags($result, $expected);
  6897. $result = $this->Form->create('Contact', array('type' => 'file'));
  6898. $expected = array(
  6899. 'form' => array(
  6900. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6901. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6902. ),
  6903. 'div' => array('style' => 'display:none;'),
  6904. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6905. '/div'
  6906. );
  6907. $this->assertTags($result, $expected);
  6908. $this->Form->request->data['Contact']['id'] = 1;
  6909. $this->Form->request->here = '/contacts/edit/1';
  6910. $this->Form->request['action'] = 'edit';
  6911. $result = $this->Form->create('Contact');
  6912. $expected = array(
  6913. 'form' => array(
  6914. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6915. 'accept-charset' => $encoding
  6916. ),
  6917. 'div' => array('style' => 'display:none;'),
  6918. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6919. '/div'
  6920. );
  6921. $this->assertTags($result, $expected);
  6922. $this->Form->request->data['Contact']['id'] = 1;
  6923. $this->Form->request->here = '/contacts/edit/1';
  6924. $this->Form->request['action'] = 'edit';
  6925. $result = $this->Form->create('Contact', array('type' => 'file'));
  6926. $expected = array(
  6927. 'form' => array(
  6928. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6929. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6930. ),
  6931. 'div' => array('style' => 'display:none;'),
  6932. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6933. '/div'
  6934. );
  6935. $this->assertTags($result, $expected);
  6936. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6937. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6938. $expected = array(
  6939. 'form' => array(
  6940. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6941. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  6942. ),
  6943. 'div' => array('style' => 'display:none;'),
  6944. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6945. '/div'
  6946. );
  6947. $this->assertTags($result, $expected);
  6948. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6949. $expected = array(
  6950. 'form' => array(
  6951. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6952. 'accept-charset' => $encoding
  6953. ),
  6954. 'div' => array('style' => 'display:none;'),
  6955. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6956. '/div'
  6957. );
  6958. $this->assertTags($result, $expected);
  6959. $this->Form->request['action'] = 'add';
  6960. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6961. $expected = array(
  6962. 'form' => array(
  6963. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6964. 'accept-charset' => $encoding
  6965. ),
  6966. 'div' => array('style' => 'display:none;'),
  6967. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6968. '/div'
  6969. );
  6970. $this->assertTags($result, $expected);
  6971. $result = $this->Form->create('User', array('action' => 'login'));
  6972. $expected = array(
  6973. 'form' => array(
  6974. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6975. 'accept-charset' => $encoding
  6976. ),
  6977. 'div' => array('style' => 'display:none;'),
  6978. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6979. '/div'
  6980. );
  6981. $this->assertTags($result, $expected);
  6982. $result = $this->Form->create('User', array('url' => '/users/login'));
  6983. $expected = array(
  6984. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6985. 'div' => array('style' => 'display:none;'),
  6986. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6987. '/div'
  6988. );
  6989. $this->assertTags($result, $expected);
  6990. $this->Form->request['controller'] = 'pages';
  6991. $result = $this->Form->create('User', array('action' => 'signup'));
  6992. $expected = array(
  6993. 'form' => array(
  6994. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6995. 'accept-charset' => $encoding
  6996. ),
  6997. 'div' => array('style' => 'display:none;'),
  6998. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6999. '/div'
  7000. );
  7001. $this->assertTags($result, $expected);
  7002. $this->Form->request->data = array();
  7003. $this->Form->request['controller'] = 'contacts';
  7004. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7005. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  7006. $expected = array(
  7007. 'form' => array(
  7008. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  7009. 'accept-charset' => 'utf-8'
  7010. ),
  7011. 'div' => array('style' => 'display:none;'),
  7012. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7013. '/div'
  7014. );
  7015. $this->assertTags($result, $expected);
  7016. }
  7017. /**
  7018. * Test the onsubmit option for create()
  7019. *
  7020. * @return void
  7021. */
  7022. public function testCreateOnSubmit() {
  7023. $this->Form->request->data = array();
  7024. $this->Form->request['controller'] = 'contacts';
  7025. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7026. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  7027. $expected = array(
  7028. 'form' => array(
  7029. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  7030. 'accept-charset' => 'utf-8'
  7031. ),
  7032. 'div' => array('style' => 'display:none;'),
  7033. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7034. '/div'
  7035. );
  7036. $this->assertTags($result, $expected);
  7037. $this->Form->request->data = array();
  7038. $this->Form->request['controller'] = 'contacts';
  7039. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7040. $result = $this->Form->create(array(
  7041. 'url' => array('action' => 'index', 'param'),
  7042. 'default' => false,
  7043. 'onsubmit' => 'someFunction();'
  7044. ));
  7045. $expected = array(
  7046. 'form' => array(
  7047. 'id' => 'ContactAddForm', 'method' => 'post',
  7048. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  7049. 'action' => '/contacts/index/param',
  7050. 'accept-charset' => 'utf-8'
  7051. ),
  7052. 'div' => array('style' => 'display:none;'),
  7053. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7054. '/div'
  7055. );
  7056. $this->assertTags($result, $expected);
  7057. }
  7058. /**
  7059. * test create() with automatic url generation
  7060. *
  7061. * @return void
  7062. */
  7063. public function testCreateAutoUrl() {
  7064. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  7065. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  7066. $this->Form->request->base = '/base_url';
  7067. $result = $this->Form->create('Contact');
  7068. $expected = array(
  7069. 'form' => array(
  7070. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  7071. 'accept-charset' => 'utf-8'
  7072. ),
  7073. 'div' => array('style' => 'display:none;'),
  7074. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7075. '/div'
  7076. );
  7077. $this->assertTags($result, $expected);
  7078. $this->Form->request['action'] = 'delete';
  7079. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  7080. $this->Form->request->base = '/base_url';
  7081. $result = $this->Form->create('Contact');
  7082. $expected = array(
  7083. 'form' => array(
  7084. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  7085. 'accept-charset' => 'utf-8'
  7086. ),
  7087. 'div' => array('style' => 'display:none;'),
  7088. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7089. '/div'
  7090. );
  7091. $this->assertTags($result, $expected);
  7092. }
  7093. /**
  7094. * test create() with a custom route
  7095. *
  7096. * @return void
  7097. */
  7098. public function testCreateCustomRoute() {
  7099. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  7100. $encoding = strtolower(Configure::read('App.encoding'));
  7101. $result = $this->Form->create('User', array('action' => 'login'));
  7102. $expected = array(
  7103. 'form' => array(
  7104. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  7105. 'accept-charset' => $encoding
  7106. ),
  7107. 'div' => array('style' => 'display:none;'),
  7108. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7109. '/div'
  7110. );
  7111. $this->assertTags($result, $expected);
  7112. }
  7113. /**
  7114. * test that inputDefaults are stored and used.
  7115. *
  7116. * @return void
  7117. */
  7118. public function testCreateWithInputDefaults() {
  7119. $this->Form->create('User', array(
  7120. 'inputDefaults' => array(
  7121. 'div' => false,
  7122. 'label' => false,
  7123. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  7124. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  7125. )
  7126. ));
  7127. $result = $this->Form->input('username');
  7128. $expected = array(
  7129. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  7130. );
  7131. $this->assertTags($result, $expected);
  7132. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  7133. $expected = array(
  7134. 'div' => array('class' => 'input text'),
  7135. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  7136. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7137. '/div'
  7138. );
  7139. $this->assertTags($result, $expected);
  7140. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7141. $expected = array(
  7142. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7143. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7144. );
  7145. $this->assertTags($result, $expected);
  7146. $this->Form->create('User', array(
  7147. 'inputDefaults' => array(
  7148. 'div' => false,
  7149. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7150. )
  7151. ));
  7152. $result = $this->Form->input('username', array('div' => true));
  7153. $expected = array(
  7154. 'div' => array('class' => 'input text'),
  7155. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7156. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7157. '/div'
  7158. );
  7159. $this->assertTags($result, $expected);
  7160. }
  7161. /**
  7162. * test automatic accept-charset overriding
  7163. *
  7164. * @return void
  7165. */
  7166. public function testCreateWithAcceptCharset() {
  7167. $result = $this->Form->create('UserForm', array(
  7168. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7169. )
  7170. );
  7171. $expected = array(
  7172. 'form' => array(
  7173. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7174. 'accept-charset' => 'iso-8859-1'
  7175. ),
  7176. 'div' => array('style' => 'display:none;'),
  7177. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7178. '/div'
  7179. );
  7180. $this->assertTags($result, $expected);
  7181. }
  7182. /**
  7183. * Test base form URL when url param is passed with multiple parameters (&)
  7184. *
  7185. */
  7186. public function testCreateQuerystringrequest() {
  7187. $encoding = strtolower(Configure::read('App.encoding'));
  7188. $result = $this->Form->create('Contact', array(
  7189. 'type' => 'post',
  7190. 'escape' => false,
  7191. 'url' => array(
  7192. 'controller' => 'controller',
  7193. 'action' => 'action',
  7194. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7195. )
  7196. ));
  7197. $expected = array(
  7198. 'form' => array(
  7199. 'id' => 'ContactAddForm',
  7200. 'method' => 'post',
  7201. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7202. 'accept-charset' => $encoding
  7203. ),
  7204. 'div' => array('style' => 'display:none;'),
  7205. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7206. '/div'
  7207. );
  7208. $this->assertTags($result, $expected);
  7209. $result = $this->Form->create('Contact', array(
  7210. 'type' => 'post',
  7211. 'url' => array(
  7212. 'controller' => 'controller',
  7213. 'action' => 'action',
  7214. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7215. )
  7216. ));
  7217. $expected = array(
  7218. 'form' => array(
  7219. 'id' => 'ContactAddForm',
  7220. 'method' => 'post',
  7221. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7222. 'accept-charset' => $encoding
  7223. ),
  7224. 'div' => array('style' => 'display:none;'),
  7225. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7226. '/div'
  7227. );
  7228. $this->assertTags($result, $expected);
  7229. }
  7230. /**
  7231. * test that create() doesn't cause errors by multiple id's being in the primary key
  7232. * as could happen with multiple select or checkboxes.
  7233. *
  7234. * @return void
  7235. */
  7236. public function testCreateWithMultipleIdInData() {
  7237. $encoding = strtolower(Configure::read('App.encoding'));
  7238. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7239. $result = $this->Form->create('Contact');
  7240. $expected = array(
  7241. 'form' => array(
  7242. 'id' => 'ContactAddForm',
  7243. 'method' => 'post',
  7244. 'action' => '/contacts/add',
  7245. 'accept-charset' => $encoding
  7246. ),
  7247. 'div' => array('style' => 'display:none;'),
  7248. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7249. '/div'
  7250. );
  7251. $this->assertTags($result, $expected);
  7252. }
  7253. /**
  7254. * test that create() doesn't add in extra passed params.
  7255. *
  7256. * @return void
  7257. */
  7258. public function testCreatePassedArgs() {
  7259. $encoding = strtolower(Configure::read('App.encoding'));
  7260. $this->Form->request->data['Contact']['id'] = 1;
  7261. $result = $this->Form->create('Contact', array(
  7262. 'type' => 'post',
  7263. 'escape' => false,
  7264. 'url' => array(
  7265. 'action' => 'edit',
  7266. 'myparam'
  7267. )
  7268. ));
  7269. $expected = array(
  7270. 'form' => array(
  7271. 'id' => 'ContactAddForm',
  7272. 'method' => 'post',
  7273. 'action' => '/contacts/edit/myparam',
  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. }
  7282. /**
  7283. * test creating a get form, and get form inputs.
  7284. *
  7285. * @return void
  7286. */
  7287. public function testGetFormCreate() {
  7288. $encoding = strtolower(Configure::read('App.encoding'));
  7289. $result = $this->Form->create('Contact', array('type' => 'get'));
  7290. $this->assertTags($result, array('form' => array(
  7291. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7292. 'accept-charset' => $encoding
  7293. )));
  7294. $result = $this->Form->text('Contact.name');
  7295. $this->assertTags($result, array('input' => array(
  7296. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7297. )));
  7298. $result = $this->Form->password('password');
  7299. $this->assertTags($result, array('input' => array(
  7300. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7301. )));
  7302. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7303. $result = $this->Form->text('user_form');
  7304. $this->assertTags($result, array('input' => array(
  7305. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7306. )));
  7307. }
  7308. /**
  7309. * test get form, and inputs when the model param is false
  7310. *
  7311. * @return void
  7312. */
  7313. public function testGetFormWithFalseModel() {
  7314. $encoding = strtolower(Configure::read('App.encoding'));
  7315. $this->Form->request['controller'] = 'contact_test';
  7316. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7317. $expected = array('form' => array(
  7318. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7319. 'accept-charset' => $encoding
  7320. ));
  7321. $this->assertTags($result, $expected);
  7322. $result = $this->Form->text('reason');
  7323. $expected = array(
  7324. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7325. );
  7326. $this->assertTags($result, $expected);
  7327. }
  7328. /**
  7329. * test that datetime() works with GET style forms.
  7330. *
  7331. * @return void
  7332. */
  7333. public function testDateTimeWithGetForms() {
  7334. extract($this->dateRegex);
  7335. $this->Form->create('Contact', array('type' => 'get'));
  7336. $result = $this->Form->datetime('created');
  7337. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7338. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7339. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7340. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7341. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7342. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7343. }
  7344. /**
  7345. * testEditFormWithData method
  7346. *
  7347. * test auto populating form elements from submitted data.
  7348. *
  7349. * @return void
  7350. */
  7351. public function testEditFormWithData() {
  7352. $this->Form->request->data = array('Person' => array(
  7353. 'id' => 1,
  7354. 'first_name' => 'Nate',
  7355. 'last_name' => 'Abele',
  7356. 'email' => 'nate@example.com'
  7357. ));
  7358. $this->Form->request->addParams(array(
  7359. 'models' => array('Person'),
  7360. 'controller' => 'people',
  7361. 'action' => 'add'
  7362. ));
  7363. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7364. $this->Form->create();
  7365. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7366. $expected = array(
  7367. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7368. 'select' => array(
  7369. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7370. ),
  7371. array('option' => array('value' => 1)), 'Nate', '/option',
  7372. array('option' => array('value' => 2)), 'Garrett', '/option',
  7373. array('option' => array('value' => 3)), 'Larry', '/option',
  7374. '/select'
  7375. );
  7376. $this->assertTags($result, $expected);
  7377. }
  7378. /**
  7379. * Test that required fields are created for various types of validation.
  7380. *
  7381. * @return void
  7382. */
  7383. public function testFormInputRequiredDetection() {
  7384. $this->Form->create('Contact');
  7385. $result = $this->Form->input('Contact.non_existing');
  7386. $expected = array(
  7387. 'div' => array('class' => 'input text'),
  7388. 'label' => array('for' => 'ContactNonExisting'),
  7389. 'Non Existing',
  7390. '/label',
  7391. 'input' => array(
  7392. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7393. 'id' => 'ContactNonExisting'
  7394. ),
  7395. '/div'
  7396. );
  7397. $this->assertTags($result, $expected);
  7398. $result = $this->Form->input('Contact.imrequired');
  7399. $expected = array(
  7400. 'div' => array('class' => 'input text required'),
  7401. 'label' => array('for' => 'ContactImrequired'),
  7402. 'Imrequired',
  7403. '/label',
  7404. 'input' => array(
  7405. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7406. 'id' => 'ContactImrequired',
  7407. 'required' => 'required'
  7408. ),
  7409. '/div'
  7410. );
  7411. $this->assertTags($result, $expected);
  7412. $result = $this->Form->input('Contact.imalsorequired');
  7413. $expected = array(
  7414. 'div' => array('class' => 'input text required'),
  7415. 'label' => array('for' => 'ContactImalsorequired'),
  7416. 'Imalsorequired',
  7417. '/label',
  7418. 'input' => array(
  7419. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7420. 'id' => 'ContactImalsorequired',
  7421. 'required' => 'required'
  7422. ),
  7423. '/div'
  7424. );
  7425. $this->assertTags($result, $expected);
  7426. $result = $this->Form->input('Contact.imrequiredtoo');
  7427. $expected = array(
  7428. 'div' => array('class' => 'input text required'),
  7429. 'label' => array('for' => 'ContactImrequiredtoo'),
  7430. 'Imrequiredtoo',
  7431. '/label',
  7432. 'input' => array(
  7433. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7434. 'id' => 'ContactImrequiredtoo',
  7435. 'required' => 'required'
  7436. ),
  7437. '/div'
  7438. );
  7439. $this->assertTags($result, $expected);
  7440. $result = $this->Form->input('Contact.required_one');
  7441. $expected = array(
  7442. 'div' => array('class' => 'input text required'),
  7443. 'label' => array('for' => 'ContactRequiredOne'),
  7444. 'Required One',
  7445. '/label',
  7446. 'input' => array(
  7447. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7448. 'id' => 'ContactRequiredOne',
  7449. 'required' => 'required'
  7450. ),
  7451. '/div'
  7452. );
  7453. $this->assertTags($result, $expected);
  7454. $result = $this->Form->input('Contact.string_required');
  7455. $expected = array(
  7456. 'div' => array('class' => 'input text required'),
  7457. 'label' => array('for' => 'ContactStringRequired'),
  7458. 'String Required',
  7459. '/label',
  7460. 'input' => array(
  7461. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7462. 'id' => 'ContactStringRequired',
  7463. 'required' => 'required'
  7464. ),
  7465. '/div'
  7466. );
  7467. $this->assertTags($result, $expected);
  7468. $result = $this->Form->input('Contact.imnotrequired');
  7469. $expected = array(
  7470. 'div' => array('class' => 'input text'),
  7471. 'label' => array('for' => 'ContactImnotrequired'),
  7472. 'Imnotrequired',
  7473. '/label',
  7474. 'input' => array(
  7475. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7476. 'id' => 'ContactImnotrequired'
  7477. ),
  7478. '/div'
  7479. );
  7480. $this->assertTags($result, $expected);
  7481. $result = $this->Form->input('Contact.imalsonotrequired');
  7482. $expected = array(
  7483. 'div' => array('class' => 'input text'),
  7484. 'label' => array('for' => 'ContactImalsonotrequired'),
  7485. 'Imalsonotrequired',
  7486. '/label',
  7487. 'input' => array(
  7488. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7489. 'id' => 'ContactImalsonotrequired'
  7490. ),
  7491. '/div'
  7492. );
  7493. $this->assertTags($result, $expected);
  7494. $result = $this->Form->input('Contact.imalsonotrequired2');
  7495. $expected = array(
  7496. 'div' => array('class' => 'input text'),
  7497. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7498. 'Imalsonotrequired2',
  7499. '/label',
  7500. 'input' => array(
  7501. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7502. 'id' => 'ContactImalsonotrequired2'
  7503. ),
  7504. '/div'
  7505. );
  7506. $this->assertTags($result, $expected);
  7507. $result = $this->Form->input('Contact.imnotrequiredeither');
  7508. $expected = array(
  7509. 'div' => array('class' => 'input text'),
  7510. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7511. 'Imnotrequiredeither',
  7512. '/label',
  7513. 'input' => array(
  7514. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7515. 'id' => 'ContactImnotrequiredeither'
  7516. ),
  7517. '/div'
  7518. );
  7519. $this->assertTags($result, $expected);
  7520. $result = $this->Form->input('Contact.iamrequiredalways');
  7521. $expected = array(
  7522. 'div' => array('class' => 'input text required'),
  7523. 'label' => array('for' => 'ContactIamrequiredalways'),
  7524. 'Iamrequiredalways',
  7525. '/label',
  7526. 'input' => array(
  7527. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7528. 'id' => 'ContactIamrequiredalways',
  7529. 'required' => 'required'
  7530. ),
  7531. '/div'
  7532. );
  7533. $this->assertTags($result, $expected);
  7534. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7535. $expected = array(
  7536. 'div' => array('class' => 'input checkbox required'),
  7537. array('input' => array(
  7538. 'type' => 'hidden',
  7539. 'name' => 'data[Contact][boolean_field]',
  7540. 'id' => 'ContactBooleanField_',
  7541. 'value' => '0'
  7542. )),
  7543. array('input' => array(
  7544. 'type' => 'checkbox',
  7545. 'name' => 'data[Contact][boolean_field]',
  7546. 'value' => '1',
  7547. 'id' => 'ContactBooleanField'
  7548. )),
  7549. 'label' => array('for' => 'ContactBooleanField'),
  7550. 'Boolean Field',
  7551. '/label',
  7552. '/div'
  7553. );
  7554. $this->assertTags($result, $expected);
  7555. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7556. $expected = array(
  7557. 'div' => array('class' => 'input checkbox required'),
  7558. array('input' => array(
  7559. 'type' => 'hidden',
  7560. 'name' => 'data[Contact][boolean_field]',
  7561. 'id' => 'ContactBooleanField_',
  7562. 'value' => '0'
  7563. )),
  7564. array('input' => array(
  7565. 'type' => 'checkbox',
  7566. 'name' => 'data[Contact][boolean_field]',
  7567. 'value' => '1',
  7568. 'id' => 'ContactBooleanField',
  7569. 'required' => 'required'
  7570. )),
  7571. 'label' => array('for' => 'ContactBooleanField'),
  7572. 'Boolean Field',
  7573. '/label',
  7574. '/div'
  7575. );
  7576. $this->assertTags($result, $expected);
  7577. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  7578. $expected = array(
  7579. 'div' => array('class' => 'input file required'),
  7580. 'label' => array('for' => 'ContactIamrequiredalways'),
  7581. 'Iamrequiredalways',
  7582. '/label',
  7583. 'input' => array(
  7584. 'type' => 'file',
  7585. 'name' => 'data[Contact][iamrequiredalways]',
  7586. 'id' => 'ContactIamrequiredalways',
  7587. 'required' => 'required'
  7588. ),
  7589. '/div'
  7590. );
  7591. $this->assertTags($result, $expected);
  7592. }
  7593. /**
  7594. * Test that required fields are created when only using ModelValidator::add().
  7595. *
  7596. * @return void
  7597. */
  7598. public function testFormInputRequiredDetectionModelValidator() {
  7599. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  7600. $this->Form->create('ContactTag');
  7601. $result = $this->Form->input('ContactTag.iwillberequired');
  7602. $expected = array(
  7603. 'div' => array('class' => 'input text required'),
  7604. 'label' => array('for' => 'ContactTagIwillberequired'),
  7605. 'Iwillberequired',
  7606. '/label',
  7607. 'input' => array(
  7608. 'name' => 'data[ContactTag][iwillberequired]',
  7609. 'type' => 'text',
  7610. 'id' => 'ContactTagIwillberequired',
  7611. 'required' => 'required'
  7612. ),
  7613. '/div'
  7614. );
  7615. $this->assertTags($result, $expected);
  7616. }
  7617. /**
  7618. * testFormMagicInput method
  7619. *
  7620. * @return void
  7621. */
  7622. public function testFormMagicInput() {
  7623. $encoding = strtolower(Configure::read('App.encoding'));
  7624. $result = $this->Form->create('Contact');
  7625. $expected = array(
  7626. 'form' => array(
  7627. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7628. 'accept-charset' => $encoding
  7629. ),
  7630. 'div' => array('style' => 'display:none;'),
  7631. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7632. '/div'
  7633. );
  7634. $this->assertTags($result, $expected);
  7635. $result = $this->Form->input('name');
  7636. $expected = array(
  7637. 'div' => array('class' => 'input text'),
  7638. 'label' => array('for' => 'ContactName'),
  7639. 'Name',
  7640. '/label',
  7641. 'input' => array(
  7642. 'type' => 'text', 'name' => 'data[Contact][name]',
  7643. 'id' => 'ContactName', 'maxlength' => '255'
  7644. ),
  7645. '/div'
  7646. );
  7647. $this->assertTags($result, $expected);
  7648. $result = $this->Form->input('non_existing_field_in_contact_model');
  7649. $expected = array(
  7650. 'div' => array('class' => 'input text'),
  7651. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7652. 'Non Existing Field In Contact Model',
  7653. '/label',
  7654. 'input' => array(
  7655. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  7656. 'id' => 'ContactNonExistingFieldInContactModel'
  7657. ),
  7658. '/div'
  7659. );
  7660. $this->assertTags($result, $expected);
  7661. $result = $this->Form->input('Address.street');
  7662. $expected = array(
  7663. 'div' => array('class' => 'input text'),
  7664. 'label' => array('for' => 'AddressStreet'),
  7665. 'Street',
  7666. '/label',
  7667. 'input' => array(
  7668. 'type' => 'text', 'name' => 'data[Address][street]',
  7669. 'id' => 'AddressStreet'
  7670. ),
  7671. '/div'
  7672. );
  7673. $this->assertTags($result, $expected);
  7674. $result = $this->Form->input('Address.non_existing_field_in_model');
  7675. $expected = array(
  7676. 'div' => array('class' => 'input text'),
  7677. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7678. 'Non Existing Field In Model',
  7679. '/label',
  7680. 'input' => array(
  7681. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  7682. 'id' => 'AddressNonExistingFieldInModel'
  7683. ),
  7684. '/div'
  7685. );
  7686. $this->assertTags($result, $expected);
  7687. $result = $this->Form->input('name', array('div' => false));
  7688. $expected = array(
  7689. 'label' => array('for' => 'ContactName'),
  7690. 'Name',
  7691. '/label',
  7692. 'input' => array(
  7693. 'type' => 'text', 'name' => 'data[Contact][name]',
  7694. 'id' => 'ContactName', 'maxlength' => '255'
  7695. )
  7696. );
  7697. $this->assertTags($result, $expected);
  7698. extract($this->dateRegex);
  7699. $now = strtotime('now');
  7700. $result = $this->Form->input('Contact.published', array('div' => false));
  7701. $expected = array(
  7702. 'label' => array('for' => 'ContactPublishedMonth'),
  7703. 'Published',
  7704. '/label',
  7705. array('select' => array(
  7706. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  7707. )),
  7708. $monthsRegex,
  7709. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7710. date('F', $now),
  7711. '/option',
  7712. '*/select',
  7713. '-',
  7714. array('select' => array(
  7715. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  7716. )),
  7717. $daysRegex,
  7718. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7719. date('j', $now),
  7720. '/option',
  7721. '*/select',
  7722. '-',
  7723. array('select' => array(
  7724. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  7725. )),
  7726. $yearsRegex,
  7727. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7728. date('Y', $now),
  7729. '*/select'
  7730. );
  7731. $this->assertTags($result, $expected);
  7732. $result = $this->Form->input('Contact.updated', array('div' => false));
  7733. $expected = array(
  7734. 'label' => array('for' => 'ContactUpdatedMonth'),
  7735. 'Updated',
  7736. '/label',
  7737. array('select' => array(
  7738. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  7739. )),
  7740. $monthsRegex,
  7741. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7742. date('F', $now),
  7743. '/option',
  7744. '*/select',
  7745. '-',
  7746. array('select' => array(
  7747. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  7748. )),
  7749. $daysRegex,
  7750. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7751. date('j', $now),
  7752. '/option',
  7753. '*/select',
  7754. '-',
  7755. array('select' => array(
  7756. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  7757. )),
  7758. $yearsRegex,
  7759. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7760. date('Y', $now),
  7761. '*/select'
  7762. );
  7763. $this->assertTags($result, $expected);
  7764. $result = $this->Form->input('UserForm.stuff');
  7765. $expected = array(
  7766. 'div' => array('class' => 'input text'),
  7767. 'label' => array('for' => 'UserFormStuff'),
  7768. 'Stuff',
  7769. '/label',
  7770. 'input' => array(
  7771. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  7772. 'id' => 'UserFormStuff', 'maxlength' => 10
  7773. ),
  7774. '/div'
  7775. );
  7776. $this->assertTags($result, $expected);
  7777. }
  7778. /**
  7779. * testForMagicInputNonExistingNorValidated method
  7780. *
  7781. * @return void
  7782. */
  7783. public function testForMagicInputNonExistingNorValidated() {
  7784. $encoding = strtolower(Configure::read('App.encoding'));
  7785. $result = $this->Form->create('Contact');
  7786. $expected = array(
  7787. 'form' => array(
  7788. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7789. 'accept-charset' => $encoding
  7790. ),
  7791. 'div' => array('style' => 'display:none;'),
  7792. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7793. '/div'
  7794. );
  7795. $this->assertTags($result, $expected);
  7796. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7797. $expected = array(
  7798. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7799. 'Non Existing Nor Validated',
  7800. '/label',
  7801. 'input' => array(
  7802. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7803. 'id' => 'ContactNonExistingNorValidated'
  7804. )
  7805. );
  7806. $this->assertTags($result, $expected);
  7807. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  7808. 'div' => false, 'value' => 'my value'
  7809. ));
  7810. $expected = array(
  7811. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7812. 'Non Existing Nor Validated',
  7813. '/label',
  7814. 'input' => array(
  7815. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7816. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  7817. )
  7818. );
  7819. $this->assertTags($result, $expected);
  7820. $this->Form->request->data = array(
  7821. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  7822. ));
  7823. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7824. $expected = array(
  7825. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7826. 'Non Existing Nor Validated',
  7827. '/label',
  7828. 'input' => array(
  7829. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7830. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  7831. )
  7832. );
  7833. $this->assertTags($result, $expected);
  7834. }
  7835. /**
  7836. * testFormMagicInputLabel method
  7837. *
  7838. * @return void
  7839. */
  7840. public function testFormMagicInputLabel() {
  7841. $encoding = strtolower(Configure::read('App.encoding'));
  7842. $result = $this->Form->create('Contact');
  7843. $expected = array(
  7844. 'form' => array(
  7845. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7846. 'accept-charset' => $encoding
  7847. ),
  7848. 'div' => array('style' => 'display:none;'),
  7849. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7850. '/div'
  7851. );
  7852. $this->assertTags($result, $expected);
  7853. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  7854. $this->assertTags($result, array('input' => array(
  7855. 'name' => 'data[Contact][name]', 'type' => 'text',
  7856. 'id' => 'ContactName', 'maxlength' => '255')
  7857. ));
  7858. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  7859. $expected = array(
  7860. 'label' => array('for' => 'ContactName'),
  7861. 'My label',
  7862. '/label',
  7863. 'input' => array(
  7864. 'type' => 'text', 'name' => 'data[Contact][name]',
  7865. 'id' => 'ContactName', 'maxlength' => '255'
  7866. )
  7867. );
  7868. $this->assertTags($result, $expected);
  7869. $result = $this->Form->input('Contact.name', array(
  7870. 'div' => false, 'label' => array('class' => 'mandatory')
  7871. ));
  7872. $expected = array(
  7873. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7874. 'Name',
  7875. '/label',
  7876. 'input' => array(
  7877. 'type' => 'text', 'name' => 'data[Contact][name]',
  7878. 'id' => 'ContactName', 'maxlength' => '255'
  7879. )
  7880. );
  7881. $this->assertTags($result, $expected);
  7882. $result = $this->Form->input('Contact.name', array(
  7883. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7884. ));
  7885. $expected = array(
  7886. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7887. 'My label',
  7888. '/label',
  7889. 'input' => array(
  7890. 'type' => 'text', 'name' => 'data[Contact][name]',
  7891. 'id' => 'ContactName', 'maxlength' => '255'
  7892. )
  7893. );
  7894. $this->assertTags($result, $expected);
  7895. $result = $this->Form->input('Contact.name', array(
  7896. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7897. ));
  7898. $expected = array(
  7899. 'label' => array('for' => 'my_id'),
  7900. 'Name',
  7901. '/label',
  7902. 'input' => array(
  7903. 'type' => 'text', 'name' => 'data[Contact][name]',
  7904. 'id' => 'my_id', 'maxlength' => '255'
  7905. )
  7906. );
  7907. $this->assertTags($result, $expected);
  7908. $result = $this->Form->input('1.id');
  7909. $this->assertTags($result, array('input' => array(
  7910. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7911. 'id' => 'Contact1Id'
  7912. )));
  7913. $result = $this->Form->input("1.name");
  7914. $expected = array(
  7915. 'div' => array('class' => 'input text'),
  7916. 'label' => array('for' => 'Contact1Name'),
  7917. 'Name',
  7918. '/label',
  7919. 'input' => array(
  7920. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  7921. 'id' => 'Contact1Name', 'maxlength' => '255'
  7922. ),
  7923. '/div'
  7924. );
  7925. $this->assertTags($result, $expected);
  7926. $result = $this->Form->input('Contact.1.id');
  7927. $this->assertTags($result, array(
  7928. 'input' => array(
  7929. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7930. 'id' => 'Contact1Id'
  7931. )
  7932. ));
  7933. $result = $this->Form->input("Model.1.name");
  7934. $expected = array(
  7935. 'div' => array('class' => 'input text'),
  7936. 'label' => array('for' => 'Model1Name'),
  7937. 'Name',
  7938. '/label',
  7939. 'input' => array(
  7940. 'type' => 'text', 'name' => 'data[Model][1][name]',
  7941. 'id' => 'Model1Name'
  7942. ),
  7943. '/div'
  7944. );
  7945. $this->assertTags($result, $expected);
  7946. }
  7947. /**
  7948. * testFormEnd method
  7949. *
  7950. * @return void
  7951. */
  7952. public function testFormEnd() {
  7953. $this->assertEquals('</form>', $this->Form->end());
  7954. $result = $this->Form->end('');
  7955. $expected = array(
  7956. 'div' => array('class' => 'submit'),
  7957. 'input' => array('type' => 'submit', 'value' => ''),
  7958. '/div',
  7959. '/form'
  7960. );
  7961. $this->assertTags($result, $expected);
  7962. $result = $this->Form->end(array('label' => ''));
  7963. $expected = array(
  7964. 'div' => array('class' => 'submit'),
  7965. 'input' => array('type' => 'submit', 'value' => ''),
  7966. '/div',
  7967. '/form'
  7968. );
  7969. $this->assertTags($result, $expected);
  7970. $result = $this->Form->end('save');
  7971. $expected = array(
  7972. 'div' => array('class' => 'submit'),
  7973. 'input' => array('type' => 'submit', 'value' => 'save'),
  7974. '/div',
  7975. '/form'
  7976. );
  7977. $this->assertTags($result, $expected);
  7978. $result = $this->Form->end(array('label' => 'save'));
  7979. $expected = array(
  7980. 'div' => array('class' => 'submit'),
  7981. 'input' => array('type' => 'submit', 'value' => 'save'),
  7982. '/div',
  7983. '/form'
  7984. );
  7985. $this->assertTags($result, $expected);
  7986. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7987. $expected = array(
  7988. 'div' => array('class' => 'submit'),
  7989. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7990. '/div',
  7991. '/form'
  7992. );
  7993. $this->assertTags($result, $expected);
  7994. $result = $this->Form->end(array('name' => 'Whatever'));
  7995. $expected = array(
  7996. 'div' => array('class' => 'submit'),
  7997. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7998. '/div',
  7999. '/form'
  8000. );
  8001. $this->assertTags($result, $expected);
  8002. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  8003. $expected = array(
  8004. 'div' => array('class' => 'good'),
  8005. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8006. '/div',
  8007. '/form'
  8008. );
  8009. $this->assertTags($result, $expected);
  8010. $result = $this->Form->end(array(
  8011. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  8012. ));
  8013. $expected = array(
  8014. 'div' => array('class' => 'good'),
  8015. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8016. '/div',
  8017. '/form'
  8018. );
  8019. $this->assertTags($result, $expected);
  8020. }
  8021. /**
  8022. * testMultipleFormWithIdFields method
  8023. *
  8024. * @return void
  8025. */
  8026. public function testMultipleFormWithIdFields() {
  8027. $this->Form->create('UserForm');
  8028. $result = $this->Form->input('id');
  8029. $this->assertTags($result, array('input' => array(
  8030. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  8031. )));
  8032. $result = $this->Form->input('ValidateItem.id');
  8033. $this->assertTags($result, array('input' => array(
  8034. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  8035. 'id' => 'ValidateItemId'
  8036. )));
  8037. $result = $this->Form->input('ValidateUser.id');
  8038. $this->assertTags($result, array('input' => array(
  8039. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  8040. 'id' => 'ValidateUserId'
  8041. )));
  8042. }
  8043. /**
  8044. * testDbLessModel method
  8045. *
  8046. * @return void
  8047. */
  8048. public function testDbLessModel() {
  8049. $this->Form->create('TestMail');
  8050. $result = $this->Form->input('name');
  8051. $expected = array(
  8052. 'div' => array('class' => 'input text'),
  8053. 'label' => array('for' => 'TestMailName'),
  8054. 'Name',
  8055. '/label',
  8056. 'input' => array(
  8057. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8058. 'id' => 'TestMailName'
  8059. ),
  8060. '/div'
  8061. );
  8062. $this->assertTags($result, $expected);
  8063. ClassRegistry::init('TestMail');
  8064. $this->Form->create('TestMail');
  8065. $result = $this->Form->input('name');
  8066. $expected = array(
  8067. 'div' => array('class' => 'input text'),
  8068. 'label' => array('for' => 'TestMailName'),
  8069. 'Name',
  8070. '/label',
  8071. 'input' => array(
  8072. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8073. 'id' => 'TestMailName'
  8074. ),
  8075. '/div'
  8076. );
  8077. $this->assertTags($result, $expected);
  8078. }
  8079. /**
  8080. * testBrokenness method
  8081. *
  8082. * @return void
  8083. */
  8084. public function testBrokenness() {
  8085. /*
  8086. * #4 This test has two parents and four children. By default (as of r7117) both
  8087. * parents are show but the first parent is missing a child. This is the inconsistency
  8088. * in the default behaviour - one parent has all children, the other does not - dependent
  8089. * on the data values.
  8090. */
  8091. $result = $this->Form->select('Model.field', array(
  8092. 'Fred' => array(
  8093. 'freds_son_1' => 'Fred',
  8094. 'freds_son_2' => 'Freddie'
  8095. ),
  8096. 'Bert' => array(
  8097. 'berts_son_1' => 'Albert',
  8098. 'berts_son_2' => 'Bertie')
  8099. ),
  8100. array('showParents' => true, 'empty' => false)
  8101. );
  8102. $expected = array(
  8103. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8104. array('optgroup' => array('label' => 'Fred')),
  8105. array('option' => array('value' => 'freds_son_1')),
  8106. 'Fred',
  8107. '/option',
  8108. array('option' => array('value' => 'freds_son_2')),
  8109. 'Freddie',
  8110. '/option',
  8111. '/optgroup',
  8112. array('optgroup' => array('label' => 'Bert')),
  8113. array('option' => array('value' => 'berts_son_1')),
  8114. 'Albert',
  8115. '/option',
  8116. array('option' => array('value' => 'berts_son_2')),
  8117. 'Bertie',
  8118. '/option',
  8119. '/optgroup',
  8120. '/select'
  8121. );
  8122. $this->assertTags($result, $expected);
  8123. /*
  8124. * #2 This is structurally identical to the test above (#1) - only the parent name has
  8125. * changed, so we should expect the same select list data, just with a different name
  8126. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  8127. * This is where data corruption can occur, because when a select value is missing from
  8128. * a list a form will substitute the first value in the list - without the user knowing.
  8129. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  8130. * affect the availability of 3 => 'Three' as a valid option.
  8131. */
  8132. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  8133. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  8134. ));
  8135. $result = $this->Form->select(
  8136. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  8137. );
  8138. $expected = array(
  8139. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8140. array('option' => array('value' => 1)),
  8141. 'One',
  8142. '/option',
  8143. array('option' => array('value' => 2)),
  8144. 'Two',
  8145. '/option',
  8146. array('optgroup' => array('label' => 'Three')),
  8147. array('option' => array('value' => 3)),
  8148. 'Three',
  8149. '/option',
  8150. array('option' => array('value' => 4)),
  8151. 'Four',
  8152. '/option',
  8153. array('option' => array('value' => 5)),
  8154. 'Five',
  8155. '/option',
  8156. '/optgroup',
  8157. '/select'
  8158. );
  8159. $this->assertTags($result, $expected);
  8160. }
  8161. /**
  8162. * Test the generation of fields for a multi record form.
  8163. *
  8164. * @return void
  8165. */
  8166. public function testMultiRecordForm() {
  8167. $this->Form->create('ValidateProfile');
  8168. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8169. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8170. $expected = array(
  8171. 'div' => array('class' => 'input textarea'),
  8172. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8173. 'Name',
  8174. '/label',
  8175. 'textarea' => array(
  8176. 'id' => 'ValidateProfile1ValidateItem2Name',
  8177. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  8178. 'cols' => 30,
  8179. 'rows' => 6
  8180. ),
  8181. 'Value',
  8182. '/textarea',
  8183. '/div'
  8184. );
  8185. $this->assertTags($result, $expected);
  8186. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8187. $expected = array(
  8188. 'div' => array('class' => 'input date'),
  8189. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8190. 'Created',
  8191. '/label',
  8192. array('select' => array(
  8193. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  8194. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8195. )
  8196. ),
  8197. array('option' => array('value' => '')), '/option',
  8198. $this->dateRegex['monthsRegex'],
  8199. '/select', '-',
  8200. array('select' => array(
  8201. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  8202. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8203. )
  8204. ),
  8205. array('option' => array('value' => '')), '/option',
  8206. $this->dateRegex['daysRegex'],
  8207. '/select', '-',
  8208. array('select' => array(
  8209. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  8210. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8211. )
  8212. ),
  8213. array('option' => array('value' => '')), '/option',
  8214. $this->dateRegex['yearsRegex'],
  8215. '/select',
  8216. '/div'
  8217. );
  8218. $this->assertTags($result, $expected);
  8219. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8220. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8221. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8222. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8223. $expected = array(
  8224. 'div' => array('class' => 'input select error'),
  8225. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8226. 'Profile',
  8227. '/label',
  8228. 'select' => array(
  8229. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  8230. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8231. 'class' => 'form-error'
  8232. ),
  8233. '/select',
  8234. array('div' => array('class' => 'error-message')),
  8235. 'Error',
  8236. '/div',
  8237. '/div'
  8238. );
  8239. $this->assertTags($result, $expected);
  8240. }
  8241. /**
  8242. * test the correct display of multi-record form validation errors.
  8243. *
  8244. * @return void
  8245. */
  8246. public function testMultiRecordFormValidationErrors() {
  8247. $this->Form->create('ValidateProfile');
  8248. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8249. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8250. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8251. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8252. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8253. $result = $this->Form->error('ValidateProfile.2.city');
  8254. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8255. $result = $this->Form->error('2.city');
  8256. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8257. }
  8258. /**
  8259. * test the correct display of multi-record form validation errors.
  8260. *
  8261. * @return void
  8262. */
  8263. public function testSaveManyRecordFormValidationErrors() {
  8264. $this->Form->create('ValidateUser');
  8265. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8266. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8267. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8268. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8269. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8270. $result = $this->Form->error('ValidateUser.0.city');
  8271. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8272. }
  8273. /**
  8274. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8275. *
  8276. * @return void
  8277. */
  8278. public function testInputTemplate() {
  8279. $result = $this->Form->input('Contact.email', array(
  8280. 'type' => 'text', 'format' => array('input')
  8281. ));
  8282. $expected = array(
  8283. 'div' => array('class' => 'input text'),
  8284. 'input' => array(
  8285. 'type' => 'text', 'name' => 'data[Contact][email]',
  8286. 'id' => 'ContactEmail'
  8287. ),
  8288. '/div'
  8289. );
  8290. $this->assertTags($result, $expected);
  8291. $result = $this->Form->input('Contact.email', array(
  8292. 'type' => 'text', 'format' => array('input', 'label'),
  8293. 'label' => '<em>Email (required)</em>'
  8294. ));
  8295. $expected = array(
  8296. 'div' => array('class' => 'input text'),
  8297. array('input' => array(
  8298. 'type' => 'text', 'name' => 'data[Contact][email]',
  8299. 'id' => 'ContactEmail'
  8300. )),
  8301. 'label' => array('for' => 'ContactEmail'),
  8302. 'em' => array(),
  8303. 'Email (required)',
  8304. '/em',
  8305. '/label',
  8306. '/div'
  8307. );
  8308. $this->assertTags($result, $expected);
  8309. $result = $this->Form->input('Contact.email', array(
  8310. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8311. 'between' => '<div>Something in the middle</div>',
  8312. 'after' => '<span>Some text at the end</span>'
  8313. ));
  8314. $expected = array(
  8315. 'div' => array('class' => 'input text'),
  8316. array('input' => array(
  8317. 'type' => 'text', 'name' => 'data[Contact][email]',
  8318. 'id' => 'ContactEmail'
  8319. )),
  8320. array('div' => array()),
  8321. 'Something in the middle',
  8322. '/div',
  8323. 'label' => array('for' => 'ContactEmail'),
  8324. 'Email',
  8325. '/label',
  8326. 'span' => array(),
  8327. 'Some text at the end',
  8328. '/span',
  8329. '/div'
  8330. );
  8331. $this->assertTags($result, $expected);
  8332. $result = $this->Form->input('Contact.method', array(
  8333. 'type' => 'radio',
  8334. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8335. 'between' => 'I am between',
  8336. ));
  8337. $expected = array(
  8338. 'div' => array('class' => 'input radio'),
  8339. 'fieldset' => array(),
  8340. 'legend' => array(),
  8341. 'Method',
  8342. '/legend',
  8343. 'I am between',
  8344. 'input' => array(
  8345. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  8346. 'value' => '', 'id' => 'ContactMethod_'
  8347. ),
  8348. array('input' => array(
  8349. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8350. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8351. )),
  8352. array('label' => array('for' => 'ContactMethodEmail')),
  8353. 'Email',
  8354. '/label',
  8355. array('input' => array(
  8356. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8357. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8358. )),
  8359. array('label' => array('for' => 'ContactMethodPigeon')),
  8360. 'Pigeon',
  8361. '/label',
  8362. '/fieldset',
  8363. '/div',
  8364. );
  8365. $this->assertTags($result, $expected);
  8366. }
  8367. /**
  8368. * test that some html5 inputs + FormHelper::__call() work
  8369. *
  8370. * @return void
  8371. */
  8372. public function testHtml5Inputs() {
  8373. $result = $this->Form->email('User.email');
  8374. $expected = array(
  8375. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  8376. );
  8377. $this->assertTags($result, $expected);
  8378. $result = $this->Form->search('User.query');
  8379. $expected = array(
  8380. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  8381. );
  8382. $this->assertTags($result, $expected);
  8383. $result = $this->Form->search('User.query', array('value' => 'test'));
  8384. $expected = array(
  8385. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8386. );
  8387. $this->assertTags($result, $expected);
  8388. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8389. $expected = array(
  8390. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8391. );
  8392. $this->assertTags($result, $expected);
  8393. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8394. $expected = array(
  8395. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8396. );
  8397. $this->assertTags($result, $expected);
  8398. }
  8399. /**
  8400. *
  8401. * @expectedException CakeException
  8402. * @return void
  8403. */
  8404. public function testHtml5InputException() {
  8405. $this->Form->email();
  8406. }
  8407. /**
  8408. * Tests that a model can be loaded from the model names passed in the request object
  8409. *
  8410. * @return void
  8411. */
  8412. public function testIntrospectModelFromRequest() {
  8413. $this->loadFixtures('Post');
  8414. App::build(array(
  8415. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  8416. ));
  8417. CakePlugin::load('TestPlugin');
  8418. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  8419. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8420. $this->Form->create('TestPluginPost');
  8421. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8422. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8423. CakePlugin::unload();
  8424. App::build();
  8425. }
  8426. /**
  8427. * Tests that it is possible to set the validation errors directly in the helper for a field
  8428. *
  8429. * @return void
  8430. */
  8431. public function testCustomValidationErrors() {
  8432. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8433. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8434. $this->assertEquals('Badness!', $result);
  8435. }
  8436. /**
  8437. * Tests that the 'on' key validates as expected on create
  8438. *
  8439. * @return void
  8440. */
  8441. public function testRequiredOnCreate() {
  8442. $this->Form->create('Contact');
  8443. $result = $this->Form->input('Contact.imrequiredonupdate');
  8444. $expected = array(
  8445. 'div' => array('class' => 'input text'),
  8446. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8447. 'Imrequiredonupdate',
  8448. '/label',
  8449. 'input' => array(
  8450. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8451. 'id' => 'ContactImrequiredonupdate'
  8452. ),
  8453. '/div'
  8454. );
  8455. $this->assertTags($result, $expected);
  8456. $result = $this->Form->input('Contact.imrequiredoncreate');
  8457. $expected = array(
  8458. 'div' => array('class' => 'input text required'),
  8459. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8460. 'Imrequiredoncreate',
  8461. '/label',
  8462. 'input' => array(
  8463. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8464. 'id' => 'ContactImrequiredoncreate',
  8465. 'required' => 'required'
  8466. ),
  8467. '/div'
  8468. );
  8469. $this->assertTags($result, $expected);
  8470. $result = $this->Form->input('Contact.imrequiredonboth');
  8471. $expected = array(
  8472. 'div' => array('class' => 'input text required'),
  8473. 'label' => array('for' => 'ContactImrequiredonboth'),
  8474. 'Imrequiredonboth',
  8475. '/label',
  8476. 'input' => array(
  8477. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8478. 'id' => 'ContactImrequiredonboth',
  8479. 'required' => 'required'
  8480. ),
  8481. '/div'
  8482. );
  8483. $this->assertTags($result, $expected);
  8484. $this->Form->inputDefaults(array('required' => false));
  8485. $result = $this->Form->input('Contact.imrequired');
  8486. $expected = array(
  8487. 'div' => array('class' => 'input text'),
  8488. 'label' => array('for' => 'ContactImrequired'),
  8489. 'Imrequired',
  8490. '/label',
  8491. 'input' => array(
  8492. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8493. 'id' => 'ContactImrequired'
  8494. ),
  8495. '/div'
  8496. );
  8497. $this->assertTags($result, $expected);
  8498. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8499. $this->assertTags($result, $expected);
  8500. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8501. $expected = array(
  8502. 'div' => array('class' => 'input text required'),
  8503. 'label' => array('for' => 'ContactImrequired'),
  8504. 'Imrequired',
  8505. '/label',
  8506. 'input' => array(
  8507. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8508. 'id' => 'ContactImrequired'
  8509. ),
  8510. '/div'
  8511. );
  8512. $this->assertTags($result, $expected);
  8513. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8514. $this->assertTags($result, $expected);
  8515. }
  8516. /**
  8517. * Tests that the 'on' key validates as expected on update
  8518. *
  8519. * @return void
  8520. */
  8521. public function testRequiredOnUpdate() {
  8522. $this->Form->request->data['Contact']['id'] = 1;
  8523. $this->Form->create('Contact');
  8524. $result = $this->Form->input('Contact.imrequiredonupdate');
  8525. $expected = array(
  8526. 'div' => array('class' => 'input text required'),
  8527. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8528. 'Imrequiredonupdate',
  8529. '/label',
  8530. 'input' => array(
  8531. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8532. 'id' => 'ContactImrequiredonupdate',
  8533. 'required' => 'required'
  8534. ),
  8535. '/div'
  8536. );
  8537. $this->assertTags($result, $expected);
  8538. $result = $this->Form->input('Contact.imrequiredoncreate');
  8539. $expected = array(
  8540. 'div' => array('class' => 'input text'),
  8541. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8542. 'Imrequiredoncreate',
  8543. '/label',
  8544. 'input' => array(
  8545. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8546. 'id' => 'ContactImrequiredoncreate'
  8547. ),
  8548. '/div'
  8549. );
  8550. $this->assertTags($result, $expected);
  8551. $result = $this->Form->input('Contact.imrequiredonboth');
  8552. $expected = array(
  8553. 'div' => array('class' => 'input text required'),
  8554. 'label' => array('for' => 'ContactImrequiredonboth'),
  8555. 'Imrequiredonboth',
  8556. '/label',
  8557. 'input' => array(
  8558. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8559. 'id' => 'ContactImrequiredonboth',
  8560. 'required' => 'required'
  8561. ),
  8562. '/div'
  8563. );
  8564. $this->assertTags($result, $expected);
  8565. $result = $this->Form->input('Contact.imrequired');
  8566. $expected = array(
  8567. 'div' => array('class' => 'input text required'),
  8568. 'label' => array('for' => 'ContactImrequired'),
  8569. 'Imrequired',
  8570. '/label',
  8571. 'input' => array(
  8572. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8573. 'id' => 'ContactImrequired',
  8574. 'required' => 'required'
  8575. ),
  8576. '/div'
  8577. );
  8578. $this->assertTags($result, $expected);
  8579. }
  8580. /**
  8581. * Test inputDefaults setter and getter
  8582. *
  8583. * @return void
  8584. */
  8585. public function testInputDefaults() {
  8586. $this->Form->create('Contact');
  8587. $this->Form->inputDefaults(array(
  8588. 'label' => false,
  8589. 'div' => array(
  8590. 'style' => 'color: #000;'
  8591. )
  8592. ));
  8593. $result = $this->Form->input('Contact.field1');
  8594. $expected = array(
  8595. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8596. 'input' => array(
  8597. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8598. 'id' => 'ContactField1'
  8599. ),
  8600. '/div'
  8601. );
  8602. $this->assertTags($result, $expected);
  8603. $this->Form->inputDefaults(array(
  8604. 'div' => false,
  8605. 'label' => 'Label',
  8606. ));
  8607. $result = $this->Form->input('Contact.field1');
  8608. $expected = array(
  8609. 'label' => array('for' => 'ContactField1'),
  8610. 'Label',
  8611. '/label',
  8612. 'input' => array(
  8613. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8614. 'id' => 'ContactField1'
  8615. ),
  8616. );
  8617. $this->assertTags($result, $expected);
  8618. $this->Form->inputDefaults(array(
  8619. 'label' => false,
  8620. ), true);
  8621. $result = $this->Form->input('Contact.field1');
  8622. $expected = array(
  8623. 'input' => array(
  8624. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8625. 'id' => 'ContactField1'
  8626. ),
  8627. );
  8628. $this->assertTags($result, $expected);
  8629. $result = $this->Form->inputDefaults();
  8630. $expected = array(
  8631. 'div' => false,
  8632. 'label' => false,
  8633. );
  8634. $this->assertEquals($expected, $result);
  8635. }
  8636. }