FormHelperTest.php 293 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226
  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 bool false
  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 bool false
  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 bool false
  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 bool false
  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 bool false
  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 bool false
  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. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  321. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  322. );
  323. /**
  324. * beforeValidate method
  325. *
  326. * @return void
  327. */
  328. public function beforeValidate($options = array()) {
  329. $this->invalidate('email');
  330. return false;
  331. }
  332. }
  333. /**
  334. * ValidateProfile class
  335. *
  336. * @package Cake.Test.Case.View.Helper
  337. */
  338. class ValidateProfile extends CakeTestModel {
  339. /**
  340. * useTable property
  341. *
  342. * @var bool false
  343. */
  344. public $useTable = false;
  345. /**
  346. * schema property
  347. *
  348. * @var array
  349. */
  350. protected $_schema = array(
  351. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  352. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  353. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  354. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  355. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  356. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  357. );
  358. /**
  359. * hasOne property
  360. *
  361. * @var array
  362. */
  363. public $hasOne = array('ValidateItem' => array(
  364. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  365. ));
  366. /**
  367. * belongsTo property
  368. *
  369. * @var array
  370. */
  371. public $belongsTo = array('ValidateUser' => array(
  372. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  373. ));
  374. /**
  375. * beforeValidate method
  376. *
  377. * @return void
  378. */
  379. public function beforeValidate($options = array()) {
  380. $this->invalidate('full_name');
  381. $this->invalidate('city');
  382. return false;
  383. }
  384. }
  385. /**
  386. * ValidateItem class
  387. *
  388. * @package Cake.Test.Case.View.Helper
  389. */
  390. class ValidateItem extends CakeTestModel {
  391. /**
  392. * useTable property
  393. *
  394. * @var bool false
  395. */
  396. public $useTable = false;
  397. /**
  398. * schema property
  399. *
  400. * @var array
  401. */
  402. protected $_schema = array(
  403. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  404. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  405. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  406. 'description' => array(
  407. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  408. ),
  409. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  410. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  411. );
  412. /**
  413. * belongsTo property
  414. *
  415. * @var array
  416. */
  417. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  418. /**
  419. * beforeValidate method
  420. *
  421. * @return void
  422. */
  423. public function beforeValidate($options = array()) {
  424. $this->invalidate('description');
  425. return false;
  426. }
  427. }
  428. /**
  429. * TestMail class
  430. *
  431. * @package Cake.Test.Case.View.Helper
  432. */
  433. class TestMail extends CakeTestModel {
  434. /**
  435. * useTable property
  436. *
  437. * @var bool false
  438. */
  439. public $useTable = false;
  440. }
  441. /**
  442. * FormHelperTest class
  443. *
  444. * @package Cake.Test.Case.View.Helper
  445. * @property FormHelper $Form
  446. */
  447. class FormHelperTest extends CakeTestCase {
  448. /**
  449. * Fixtures to be used
  450. *
  451. * @var array
  452. */
  453. public $fixtures = array('core.post');
  454. /**
  455. * Do not load the fixtures by default
  456. *
  457. * @var boolean
  458. */
  459. public $autoFixtures = false;
  460. /**
  461. * setUp method
  462. *
  463. * @return void
  464. */
  465. public function setUp() {
  466. parent::setUp();
  467. Configure::write('Config.language', 'eng');
  468. Configure::write('App.base', '');
  469. Configure::delete('Asset');
  470. $this->Controller = new ContactTestController();
  471. $this->View = new View($this->Controller);
  472. $this->Form = new FormHelper($this->View);
  473. $this->Form->Html = new HtmlHelper($this->View);
  474. $this->Form->request = new CakeRequest('contacts/add', false);
  475. $this->Form->request->here = '/contacts/add';
  476. $this->Form->request['action'] = 'add';
  477. $this->Form->request->webroot = '';
  478. $this->Form->request->base = '';
  479. ClassRegistry::addObject('Contact', new Contact());
  480. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  481. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  482. ClassRegistry::addObject('User', new UserForm());
  483. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  484. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  485. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  486. $this->oldSalt = Configure::read('Security.salt');
  487. $this->dateRegex = array(
  488. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  489. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  490. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  491. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  492. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  493. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  494. );
  495. Configure::write('Security.salt', 'foo!');
  496. }
  497. /**
  498. * tearDown method
  499. *
  500. * @return void
  501. */
  502. public function tearDown() {
  503. parent::tearDown();
  504. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  505. Configure::write('Security.salt', $this->oldSalt);
  506. }
  507. /**
  508. * testFormCreateWithSecurity method
  509. *
  510. * Test form->create() with security key.
  511. *
  512. * @return void
  513. */
  514. public function testCreateWithSecurity() {
  515. $this->Form->request['_Token'] = array('key' => 'testKey');
  516. $encoding = strtolower(Configure::read('App.encoding'));
  517. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  518. $expected = array(
  519. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  520. 'div' => array('style' => 'display:none;'),
  521. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  522. array('input' => array(
  523. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  524. )),
  525. '/div'
  526. );
  527. $this->assertTags($result, $expected);
  528. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  529. $expected['form']['id'] = 'MyForm';
  530. $this->assertTags($result, $expected);
  531. }
  532. /**
  533. * testFormCreateGetNoSecurity method
  534. *
  535. * Test form->create() with no security key as its a get form
  536. *
  537. * @return void
  538. */
  539. public function testCreateEndGetNoSecurity() {
  540. $this->Form->request['_Token'] = array('key' => 'testKey');
  541. $encoding = strtolower(Configure::read('App.encoding'));
  542. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  543. $this->assertNotContains('Token', $result);
  544. $result = $this->Form->end('Save');
  545. $this->assertNotContains('Token', $result);
  546. }
  547. /**
  548. * test that create() clears the fields property so it starts fresh
  549. *
  550. * @return void
  551. */
  552. public function testCreateClearingFields() {
  553. $this->Form->fields = array('model_id');
  554. $this->Form->create('Contact');
  555. $this->assertEquals(array(), $this->Form->fields);
  556. }
  557. /**
  558. * Tests form hash generation with model-less data
  559. *
  560. * @return void
  561. */
  562. public function testValidateHashNoModel() {
  563. $this->Form->request['_Token'] = array('key' => 'foo');
  564. $result = $this->Form->secure(array('anything'));
  565. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  566. }
  567. /**
  568. * Tests that models with identical field names get resolved properly
  569. *
  570. * @return void
  571. */
  572. public function testDuplicateFieldNameResolution() {
  573. $result = $this->Form->create('ValidateUser');
  574. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  575. $result = $this->Form->input('ValidateItem.name');
  576. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  577. $result = $this->Form->input('ValidateUser.name');
  578. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  579. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  580. $this->assertRegExp('/type="text"/', $result);
  581. $result = $this->Form->input('ValidateItem.name');
  582. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  583. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  584. $this->assertRegExp('/<textarea/', $result);
  585. $result = $this->Form->input('name');
  586. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  587. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  588. $this->assertRegExp('/type="text"/', $result);
  589. }
  590. /**
  591. * Tests that hidden fields generated for checkboxes don't get locked
  592. *
  593. * @return void
  594. */
  595. public function testNoCheckboxLocking() {
  596. $this->Form->request['_Token'] = array('key' => 'foo');
  597. $this->assertSame(array(), $this->Form->fields);
  598. $this->Form->checkbox('check', array('value' => '1'));
  599. $this->assertSame($this->Form->fields, array('check'));
  600. }
  601. /**
  602. * testFormSecurityFields method
  603. *
  604. * Test generation of secure form hash generation.
  605. *
  606. * @return void
  607. */
  608. public function testFormSecurityFields() {
  609. $key = 'testKey';
  610. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  611. $this->Form->request['_Token'] = array('key' => $key);
  612. $result = $this->Form->secure($fields);
  613. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  614. $hash .= ':' . 'Model.valid';
  615. $hash = urlencode($hash);
  616. $expected = array(
  617. 'div' => array('style' => 'display:none;'),
  618. array('input' => array(
  619. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  620. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  621. )),
  622. array('input' => array(
  623. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  624. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  625. )),
  626. '/div'
  627. );
  628. $this->assertTags($result, $expected);
  629. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  630. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  631. $result = $this->Form->secure($fields);
  632. $expected = array(
  633. 'div' => array('class' => 'hidden'),
  634. array('input' => array(
  635. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  636. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  637. )),
  638. array('input' => array(
  639. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  640. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  641. )),
  642. '/div'
  643. );
  644. $this->assertTags($result, $expected);
  645. }
  646. /**
  647. * Tests correct generation of number fields for double and float fields
  648. *
  649. * @return void
  650. */
  651. public function testTextFieldGenerationForFloats() {
  652. $model = ClassRegistry::getObject('Contact');
  653. $model->setSchema(array('foo' => array(
  654. 'type' => 'float',
  655. 'null' => false,
  656. 'default' => null,
  657. 'length' => 10
  658. )));
  659. $this->Form->create('Contact');
  660. $result = $this->Form->input('foo');
  661. $expected = array(
  662. 'div' => array('class' => 'input number'),
  663. 'label' => array('for' => 'ContactFoo'),
  664. 'Foo',
  665. '/label',
  666. array('input' => array(
  667. 'type' => 'number',
  668. 'name' => 'data[Contact][foo]',
  669. 'id' => 'ContactFoo',
  670. 'step' => 'any'
  671. )),
  672. '/div'
  673. );
  674. $this->assertTags($result, $expected);
  675. $result = $this->Form->input('foo', array('step' => 0.5));
  676. $expected = array(
  677. 'div' => array('class' => 'input number'),
  678. 'label' => array('for' => 'ContactFoo'),
  679. 'Foo',
  680. '/label',
  681. array('input' => array(
  682. 'type' => 'number',
  683. 'name' => 'data[Contact][foo]',
  684. 'id' => 'ContactFoo',
  685. 'step' => '0.5'
  686. )),
  687. '/div'
  688. );
  689. $this->assertTags($result, $expected);
  690. }
  691. /**
  692. * Tests correct generation of number fields for integer fields
  693. *
  694. * @return void
  695. */
  696. public function testTextFieldTypeNumberGenerationForIntegers() {
  697. $model = ClassRegistry::getObject('Contact');
  698. $model->setSchema(array('foo' => array(
  699. 'type' => 'integer',
  700. 'null' => false,
  701. 'default' => null,
  702. 'length' => null
  703. )));
  704. $this->Form->create('Contact');
  705. $result = $this->Form->input('foo');
  706. $expected = array(
  707. 'div' => array('class' => 'input number'),
  708. 'label' => array('for' => 'ContactFoo'),
  709. 'Foo',
  710. '/label',
  711. array('input' => array(
  712. 'type' => 'number', 'name' => 'data[Contact][foo]',
  713. 'id' => 'ContactFoo'
  714. )),
  715. '/div'
  716. );
  717. $this->assertTags($result, $expected);
  718. }
  719. /**
  720. * testFormSecurityMultipleFields method
  721. *
  722. * Test secure() with multiple row form. Ensure hash is correct.
  723. *
  724. * @return void
  725. */
  726. public function testFormSecurityMultipleFields() {
  727. $key = 'testKey';
  728. $fields = array(
  729. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  730. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  731. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  732. );
  733. $this->Form->request['_Token'] = array('key' => $key);
  734. $result = $this->Form->secure($fields);
  735. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  736. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  737. $expected = array(
  738. 'div' => array('style' => 'display:none;'),
  739. array('input' => array(
  740. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  741. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  742. )),
  743. array('input' => array(
  744. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  745. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  746. )),
  747. '/div'
  748. );
  749. $this->assertTags($result, $expected);
  750. }
  751. /**
  752. * testFormSecurityMultipleSubmitButtons
  753. *
  754. * test form submit generation and ensure that _Token is only created on end()
  755. *
  756. * @return void
  757. */
  758. public function testFormSecurityMultipleSubmitButtons() {
  759. $key = 'testKey';
  760. $this->Form->request['_Token'] = array('key' => $key);
  761. $this->Form->create('Addresses');
  762. $this->Form->input('Address.title');
  763. $this->Form->input('Address.first_name');
  764. $result = $this->Form->submit('Save', array('name' => 'save'));
  765. $expected = array(
  766. 'div' => array('class' => 'submit'),
  767. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  768. '/div',
  769. );
  770. $this->assertTags($result, $expected);
  771. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  772. $expected = array(
  773. 'div' => array('class' => 'submit'),
  774. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  775. '/div',
  776. );
  777. $this->assertTags($result, $expected);
  778. $result = $this->Form->end(null);
  779. $expected = array(
  780. 'div' => array('style' => 'display:none;'),
  781. array('input' => array(
  782. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  783. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  784. )),
  785. array('input' => array(
  786. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  787. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  788. )),
  789. '/div'
  790. );
  791. $this->assertTags($result, $expected);
  792. }
  793. /**
  794. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  795. *
  796. * @return void
  797. */
  798. public function testSecurityButtonNestedNamed() {
  799. $key = 'testKey';
  800. $this->Form->request['_Token'] = array('key' => $key);
  801. $this->Form->create('Addresses');
  802. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  803. $result = $this->Form->unlockField();
  804. $this->assertEquals(array('Address.button'), $result);
  805. }
  806. /**
  807. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  808. *
  809. * @return void
  810. */
  811. public function testSecuritySubmitNestedNamed() {
  812. $key = 'testKey';
  813. $this->Form->request['_Token'] = array('key' => $key);
  814. $this->Form->create('Addresses');
  815. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  816. $result = $this->Form->unlockField();
  817. $this->assertEquals(array('Address.button'), $result);
  818. }
  819. /**
  820. * Test that the correct fields are unlocked for image submits with no names.
  821. *
  822. * @return void
  823. */
  824. public function testSecuritySubmitImageNoName() {
  825. $key = 'testKey';
  826. $this->Form->request['_Token'] = array('key' => $key);
  827. $this->Form->create('User');
  828. $result = $this->Form->submit('save.png');
  829. $expected = array(
  830. 'div' => array('class' => 'submit'),
  831. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  832. '/div'
  833. );
  834. $this->assertTags($result, $expected);
  835. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  836. }
  837. /**
  838. * Test that the correct fields are unlocked for image submits with names.
  839. *
  840. * @return void
  841. */
  842. public function testSecuritySubmitImageName() {
  843. $key = 'testKey';
  844. $this->Form->request['_Token'] = array('key' => $key);
  845. $this->Form->create('User');
  846. $result = $this->Form->submit('save.png', array('name' => 'test'));
  847. $expected = array(
  848. 'div' => array('class' => 'submit'),
  849. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  850. '/div'
  851. );
  852. $this->assertTags($result, $expected);
  853. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  854. }
  855. /**
  856. * testFormSecurityMultipleInputFields method
  857. *
  858. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  859. *
  860. * @return void
  861. */
  862. public function testFormSecurityMultipleInputFields() {
  863. $key = 'testKey';
  864. $this->Form->request['_Token'] = array('key' => $key);
  865. $this->Form->create('Addresses');
  866. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  867. $this->Form->input('Addresses.0.title');
  868. $this->Form->input('Addresses.0.first_name');
  869. $this->Form->input('Addresses.0.last_name');
  870. $this->Form->input('Addresses.0.address');
  871. $this->Form->input('Addresses.0.city');
  872. $this->Form->input('Addresses.0.phone');
  873. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  874. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  875. $this->Form->input('Addresses.1.title');
  876. $this->Form->input('Addresses.1.first_name');
  877. $this->Form->input('Addresses.1.last_name');
  878. $this->Form->input('Addresses.1.address');
  879. $this->Form->input('Addresses.1.city');
  880. $this->Form->input('Addresses.1.phone');
  881. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  882. $result = $this->Form->secure($this->Form->fields);
  883. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  884. $expected = array(
  885. 'div' => array('style' => 'display:none;'),
  886. array('input' => array(
  887. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  888. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  889. )),
  890. array('input' => array(
  891. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  892. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  893. )),
  894. '/div'
  895. );
  896. $this->assertTags($result, $expected);
  897. }
  898. /**
  899. * Test form security with Model.field.0 style inputs
  900. *
  901. * @return void
  902. */
  903. public function testFormSecurityArrayFields() {
  904. $key = 'testKey';
  905. $this->Form->request->params['_Token']['key'] = $key;
  906. $this->Form->create('Address');
  907. $this->Form->input('Address.primary.1');
  908. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  909. $this->Form->input('Address.secondary.1.0');
  910. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  911. }
  912. /**
  913. * testFormSecurityMultipleInputDisabledFields method
  914. *
  915. * test secure form generation with multiple records and disabled fields.
  916. *
  917. * @return void
  918. */
  919. public function testFormSecurityMultipleInputDisabledFields() {
  920. $key = 'testKey';
  921. $this->Form->request->params['_Token'] = array(
  922. 'key' => $key,
  923. 'unlockedFields' => array('first_name', 'address')
  924. );
  925. $this->Form->create();
  926. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  927. $this->Form->input('Addresses.0.title');
  928. $this->Form->input('Addresses.0.first_name');
  929. $this->Form->input('Addresses.0.last_name');
  930. $this->Form->input('Addresses.0.address');
  931. $this->Form->input('Addresses.0.city');
  932. $this->Form->input('Addresses.0.phone');
  933. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  934. $this->Form->input('Addresses.1.title');
  935. $this->Form->input('Addresses.1.first_name');
  936. $this->Form->input('Addresses.1.last_name');
  937. $this->Form->input('Addresses.1.address');
  938. $this->Form->input('Addresses.1.city');
  939. $this->Form->input('Addresses.1.phone');
  940. $result = $this->Form->secure($this->Form->fields);
  941. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  942. $expected = array(
  943. 'div' => array('style' => 'display:none;'),
  944. array('input' => array(
  945. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  946. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  947. )),
  948. array('input' => array(
  949. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  950. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  951. )),
  952. '/div'
  953. );
  954. $this->assertTags($result, $expected);
  955. }
  956. /**
  957. * testFormSecurityInputDisabledFields method
  958. *
  959. * Test single record form with disabled fields.
  960. *
  961. * @return void
  962. */
  963. public function testFormSecurityInputUnlockedFields() {
  964. $key = 'testKey';
  965. $this->Form->request['_Token'] = array(
  966. 'key' => $key,
  967. 'unlockedFields' => array('first_name', 'address')
  968. );
  969. $this->Form->create();
  970. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  971. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  972. $this->Form->input('Addresses.title');
  973. $this->Form->input('Addresses.first_name');
  974. $this->Form->input('Addresses.last_name');
  975. $this->Form->input('Addresses.address');
  976. $this->Form->input('Addresses.city');
  977. $this->Form->input('Addresses.phone');
  978. $result = $this->Form->fields;
  979. $expected = array(
  980. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  981. 'Addresses.city', 'Addresses.phone'
  982. );
  983. $this->assertEquals($expected, $result);
  984. $result = $this->Form->secure($expected);
  985. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  986. $expected = array(
  987. 'div' => array('style' => 'display:none;'),
  988. array('input' => array(
  989. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  990. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  991. )),
  992. array('input' => array(
  993. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  994. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  995. )),
  996. '/div'
  997. );
  998. $this->assertTags($result, $expected);
  999. }
  1000. /**
  1001. * test securing inputs with custom name attributes.
  1002. *
  1003. * @return void
  1004. */
  1005. public function testFormSecureWithCustomNameAttribute() {
  1006. $this->Form->request->params['_Token']['key'] = 'testKey';
  1007. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1008. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1009. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1010. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1011. }
  1012. /**
  1013. * testFormSecuredInput method
  1014. *
  1015. * Test generation of entire secure form, assertions made on input() output.
  1016. *
  1017. * @return void
  1018. */
  1019. public function testFormSecuredInput() {
  1020. $this->Form->request['_Token'] = array('key' => 'testKey');
  1021. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1022. $encoding = strtolower(Configure::read('App.encoding'));
  1023. $expected = array(
  1024. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1025. 'div' => array('style' => 'display:none;'),
  1026. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1027. array('input' => array(
  1028. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1029. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1030. )),
  1031. '/div'
  1032. );
  1033. $this->assertTags($result, $expected);
  1034. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1035. $expected = array(
  1036. 'div' => array('class' => 'input text'),
  1037. 'label' => array('for' => 'UserFormPublished'),
  1038. 'Published',
  1039. '/label',
  1040. array('input' => array(
  1041. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1042. 'id' => 'UserFormPublished'
  1043. )),
  1044. '/div'
  1045. );
  1046. $this->assertTags($result, $expected);
  1047. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1048. $expected = array(
  1049. 'div' => array('class' => 'input text'),
  1050. 'label' => array('for' => 'UserFormOther'),
  1051. 'Other',
  1052. '/label',
  1053. array('input' => array(
  1054. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1055. 'id' => 'UserFormOther'
  1056. )),
  1057. '/div'
  1058. );
  1059. $this->assertTags($result, $expected);
  1060. $result = $this->Form->hidden('UserForm.stuff');
  1061. $expected = array(
  1062. 'input' => array(
  1063. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1064. 'id' => 'UserFormStuff'
  1065. ));
  1066. $this->assertTags($result, $expected);
  1067. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1068. $expected = array('input' => array(
  1069. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1070. 'value' => '0', 'id' => 'UserFormHidden'
  1071. ));
  1072. $this->assertTags($result, $expected);
  1073. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1074. $expected = array(
  1075. 'div' => array('class' => 'input checkbox'),
  1076. array('input' => array(
  1077. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1078. 'value' => '0', 'id' => 'UserFormSomething_'
  1079. )),
  1080. array('input' => array(
  1081. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1082. 'value' => '1', 'id' => 'UserFormSomething'
  1083. )),
  1084. 'label' => array('for' => 'UserFormSomething'),
  1085. 'Something',
  1086. '/label',
  1087. '/div'
  1088. );
  1089. $this->assertTags($result, $expected);
  1090. $result = $this->Form->fields;
  1091. $expected = array(
  1092. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1093. 'UserForm.hidden' => '0', 'UserForm.something'
  1094. );
  1095. $this->assertEquals($expected, $result);
  1096. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1097. $result = $this->Form->secure($this->Form->fields);
  1098. $expected = array(
  1099. 'div' => array('style' => 'display:none;'),
  1100. array('input' => array(
  1101. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1102. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1103. )),
  1104. array('input' => array(
  1105. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1106. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1107. )),
  1108. '/div'
  1109. );
  1110. $this->assertTags($result, $expected);
  1111. }
  1112. /**
  1113. * Test secured inputs with custom names.
  1114. *
  1115. * @return void
  1116. */
  1117. public function testSecuredInputCustomName() {
  1118. $this->Form->request['_Token'] = array('key' => 'testKey');
  1119. $this->assertEquals(array(), $this->Form->fields);
  1120. $this->Form->input('text_input', array(
  1121. 'name' => 'data[Option][General.default_role]',
  1122. ));
  1123. $expected = array('Option.General.default_role');
  1124. $this->assertEquals($expected, $this->Form->fields);
  1125. $this->Form->input('select_box', array(
  1126. 'name' => 'data[Option][General.select_role]',
  1127. 'type' => 'select',
  1128. 'options' => array(1, 2),
  1129. ));
  1130. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1131. $this->assertEquals($expected, $this->Form->fields);
  1132. }
  1133. /**
  1134. * Tests that the correct keys are added to the field hash index
  1135. *
  1136. * @return void
  1137. */
  1138. public function testFormSecuredFileInput() {
  1139. $this->Form->request['_Token'] = array('key' => 'testKey');
  1140. $this->assertEquals(array(), $this->Form->fields);
  1141. $this->Form->file('Attachment.file');
  1142. $expected = array(
  1143. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1144. 'Attachment.file.error', 'Attachment.file.size'
  1145. );
  1146. $this->assertEquals($expected, $this->Form->fields);
  1147. }
  1148. /**
  1149. * test that multiple selects keys are added to field hash
  1150. *
  1151. * @return void
  1152. */
  1153. public function testFormSecuredMultipleSelect() {
  1154. $this->Form->request['_Token'] = array('key' => 'testKey');
  1155. $this->assertEquals(array(), $this->Form->fields);
  1156. $options = array('1' => 'one', '2' => 'two');
  1157. $this->Form->select('Model.select', $options);
  1158. $expected = array('Model.select');
  1159. $this->assertEquals($expected, $this->Form->fields);
  1160. $this->Form->fields = array();
  1161. $this->Form->select('Model.select', $options, array('multiple' => true));
  1162. $this->assertEquals($expected, $this->Form->fields);
  1163. }
  1164. /**
  1165. * testFormSecuredRadio method
  1166. *
  1167. * @return void
  1168. */
  1169. public function testFormSecuredRadio() {
  1170. $this->Form->request['_Token'] = array('key' => 'testKey');
  1171. $this->assertEquals(array(), $this->Form->fields);
  1172. $options = array('1' => 'option1', '2' => 'option2');
  1173. $this->Form->radio('Test.test', $options);
  1174. $expected = array('Test.test');
  1175. $this->assertEquals($expected, $this->Form->fields);
  1176. }
  1177. /**
  1178. * Test that when disabled is in a list based attribute array it works.
  1179. *
  1180. * @return void
  1181. */
  1182. public function testFormSecuredAndDisabledNotAssoc() {
  1183. $this->Form->request['_Token'] = array('key' => 'testKey');
  1184. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1185. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1186. $this->Form->text('Model.text', array('disabled'));
  1187. $this->Form->textarea('Model.textarea', array('disabled'));
  1188. $this->Form->password('Model.password', array('disabled'));
  1189. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1190. $expected = array(
  1191. 'Model.radio' => ''
  1192. );
  1193. $this->assertEquals($expected, $this->Form->fields);
  1194. }
  1195. /**
  1196. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1197. * hashing.
  1198. *
  1199. * @return void
  1200. */
  1201. public function testFormSecuredAndDisabled() {
  1202. $this->Form->request['_Token'] = array('key' => 'testKey');
  1203. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1204. $this->Form->text('Model.text', array('disabled' => true));
  1205. $this->Form->password('Model.text', array('disabled' => true));
  1206. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1207. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1208. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1209. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1210. $this->Form->month('Model.month', array('disabled' => true));
  1211. $this->Form->day('Model.day', array('disabled' => true));
  1212. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1213. $this->Form->minute('Model.minute', array('disabled' => true));
  1214. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1215. $expected = array(
  1216. 'Model.radio' => ''
  1217. );
  1218. $this->assertEquals($expected, $this->Form->fields);
  1219. }
  1220. /**
  1221. * testDisableSecurityUsingForm method
  1222. *
  1223. * @return void
  1224. */
  1225. public function testDisableSecurityUsingForm() {
  1226. $this->Form->request['_Token'] = array(
  1227. 'key' => 'testKey',
  1228. 'disabledFields' => array()
  1229. );
  1230. $this->Form->create();
  1231. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1232. $this->Form->input('Addresses.title');
  1233. $this->Form->input('Addresses.first_name', array('secure' => false));
  1234. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1235. $this->Form->input('Addresses.zip', array(
  1236. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1237. ));
  1238. $result = $this->Form->fields;
  1239. $expected = array(
  1240. 'Addresses.id' => '123456', 'Addresses.title',
  1241. );
  1242. $this->assertEquals($expected, $result);
  1243. }
  1244. /**
  1245. * test disableField
  1246. *
  1247. * @return void
  1248. */
  1249. public function testUnlockFieldAddsToList() {
  1250. $this->Form->request['_Token'] = array(
  1251. 'key' => 'testKey',
  1252. 'unlockedFields' => array()
  1253. );
  1254. $this->Form->create('Contact');
  1255. $this->Form->unlockField('Contact.name');
  1256. $this->Form->text('Contact.name');
  1257. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1258. $this->assertEquals(array(), $this->Form->fields);
  1259. }
  1260. /**
  1261. * test unlockField removing from fields array.
  1262. *
  1263. * @return void
  1264. */
  1265. public function testUnlockFieldRemovingFromFields() {
  1266. $this->Form->request['_Token'] = array(
  1267. 'key' => 'testKey',
  1268. 'unlockedFields' => array()
  1269. );
  1270. $this->Form->create('Contact');
  1271. $this->Form->hidden('Contact.id', array('value' => 1));
  1272. $this->Form->text('Contact.name');
  1273. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1274. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1275. $this->Form->unlockField('Contact.name');
  1276. $this->Form->unlockField('Contact.id');
  1277. $this->assertEquals(array(), $this->Form->fields);
  1278. }
  1279. /**
  1280. * testTagIsInvalid method
  1281. *
  1282. * @return void
  1283. */
  1284. public function testTagIsInvalid() {
  1285. $Contact = ClassRegistry::getObject('Contact');
  1286. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1287. $this->Form->setEntity('Contact.0.email');
  1288. $result = $this->Form->tagIsInvalid();
  1289. $this->assertEquals($expected, $result);
  1290. $this->Form->setEntity('Contact.1.email');
  1291. $result = $this->Form->tagIsInvalid();
  1292. $this->assertFalse($result);
  1293. $this->Form->setEntity('Contact.0.name');
  1294. $result = $this->Form->tagIsInvalid();
  1295. $this->assertFalse($result);
  1296. }
  1297. /**
  1298. * Test tagIsInvalid with validation errors from a saveMany
  1299. *
  1300. * @return void
  1301. */
  1302. public function testTagIsInvalidSaveMany() {
  1303. $Contact = ClassRegistry::getObject('Contact');
  1304. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1305. $this->Form->create('Contact');
  1306. $this->Form->setEntity('0.email');
  1307. $result = $this->Form->tagIsInvalid();
  1308. $this->assertEquals($expected, $result);
  1309. $this->Form->setEntity('0.Contact.email');
  1310. $result = $this->Form->tagIsInvalid();
  1311. $this->assertEquals($expected, $result);
  1312. }
  1313. /**
  1314. * Test validation errors.
  1315. *
  1316. * @return void
  1317. */
  1318. public function testPasswordValidation() {
  1319. $Contact = ClassRegistry::getObject('Contact');
  1320. $Contact->validationErrors['password'] = array('Please provide a password');
  1321. $result = $this->Form->input('Contact.password');
  1322. $expected = array(
  1323. 'div' => array('class' => 'input password error'),
  1324. 'label' => array('for' => 'ContactPassword'),
  1325. 'Password',
  1326. '/label',
  1327. 'input' => array(
  1328. 'type' => 'password', 'name' => 'data[Contact][password]',
  1329. 'id' => 'ContactPassword', 'class' => 'form-error'
  1330. ),
  1331. array('div' => array('class' => 'error-message')),
  1332. 'Please provide a password',
  1333. '/div',
  1334. '/div'
  1335. );
  1336. $this->assertTags($result, $expected);
  1337. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1338. $expected = array(
  1339. 'div' => array('class' => 'input password error'),
  1340. 'label' => array('for' => 'ContactPassword'),
  1341. 'Password',
  1342. '/label',
  1343. 'input' => array(
  1344. 'type' => 'password', 'name' => 'data[Contact][password]',
  1345. 'id' => 'ContactPassword', 'class' => 'form-error'
  1346. ),
  1347. '/div'
  1348. );
  1349. $this->assertTags($result, $expected);
  1350. }
  1351. /**
  1352. * Test validation errors, when validation message is an empty string.
  1353. *
  1354. * @return void
  1355. */
  1356. public function testEmptyErrorValidation() {
  1357. $this->Form->validationErrors['Contact']['password'] = '';
  1358. $result = $this->Form->input('Contact.password');
  1359. $expected = array(
  1360. 'div' => array('class' => 'input password error'),
  1361. 'label' => array('for' => 'ContactPassword'),
  1362. 'Password',
  1363. '/label',
  1364. 'input' => array(
  1365. 'type' => 'password', 'name' => 'data[Contact][password]',
  1366. 'id' => 'ContactPassword', 'class' => 'form-error'
  1367. ),
  1368. array('div' => array('class' => 'error-message')),
  1369. array(),
  1370. '/div',
  1371. '/div'
  1372. );
  1373. $this->assertTags($result, $expected);
  1374. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1375. $expected = array(
  1376. 'div' => array('class' => 'input password error'),
  1377. 'label' => array('for' => 'ContactPassword'),
  1378. 'Password',
  1379. '/label',
  1380. 'input' => array(
  1381. 'type' => 'password', 'name' => 'data[Contact][password]',
  1382. 'id' => 'ContactPassword', 'class' => 'form-error'
  1383. ),
  1384. '/div'
  1385. );
  1386. $this->assertTags($result, $expected);
  1387. }
  1388. /**
  1389. * Test validation errors, when calling input() overriding validation message by an empty string.
  1390. *
  1391. * @return void
  1392. */
  1393. public function testEmptyInputErrorValidation() {
  1394. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1395. $result = $this->Form->input('Contact.password', array('error' => ''));
  1396. $expected = array(
  1397. 'div' => array('class' => 'input password error'),
  1398. 'label' => array('for' => 'ContactPassword'),
  1399. 'Password',
  1400. '/label',
  1401. 'input' => array(
  1402. 'type' => 'password', 'name' => 'data[Contact][password]',
  1403. 'id' => 'ContactPassword', 'class' => 'form-error'
  1404. ),
  1405. array('div' => array('class' => 'error-message')),
  1406. array(),
  1407. '/div',
  1408. '/div'
  1409. );
  1410. $this->assertTags($result, $expected);
  1411. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1412. $expected = array(
  1413. 'div' => array('class' => 'input password error'),
  1414. 'label' => array('for' => 'ContactPassword'),
  1415. 'Password',
  1416. '/label',
  1417. 'input' => array(
  1418. 'type' => 'password', 'name' => 'data[Contact][password]',
  1419. 'id' => 'ContactPassword', 'class' => 'form-error'
  1420. ),
  1421. '/div'
  1422. );
  1423. $this->assertTags($result, $expected);
  1424. }
  1425. /**
  1426. * testFormValidationAssociated method
  1427. *
  1428. * test display of form errors in conjunction with model::validates.
  1429. *
  1430. * @return void
  1431. */
  1432. public function testFormValidationAssociated() {
  1433. $this->UserForm = ClassRegistry::getObject('UserForm');
  1434. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1435. $data = array(
  1436. 'UserForm' => array('name' => 'user'),
  1437. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1438. );
  1439. $result = $this->UserForm->OpenidUrl->create($data);
  1440. $this->assertFalse(empty($result));
  1441. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1442. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1443. $encoding = strtolower(Configure::read('App.encoding'));
  1444. $expected = array(
  1445. 'form' => array(
  1446. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1447. 'accept-charset' => $encoding
  1448. ),
  1449. 'div' => array('style' => 'display:none;'),
  1450. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1451. '/div'
  1452. );
  1453. $this->assertTags($result, $expected);
  1454. $result = $this->Form->error(
  1455. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1456. );
  1457. $this->assertEquals('Error, not registered', $result);
  1458. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1459. }
  1460. /**
  1461. * testFormValidationAssociatedFirstLevel method
  1462. *
  1463. * test form error display with associated model.
  1464. *
  1465. * @return void
  1466. */
  1467. public function testFormValidationAssociatedFirstLevel() {
  1468. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1469. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1470. $data = array(
  1471. 'ValidateUser' => array('name' => 'mariano'),
  1472. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1473. );
  1474. $result = $this->ValidateUser->create($data);
  1475. $this->assertFalse(empty($result));
  1476. $this->assertFalse($this->ValidateUser->validates());
  1477. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1478. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1479. $encoding = strtolower(Configure::read('App.encoding'));
  1480. $expected = array(
  1481. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1482. 'div' => array('style' => 'display:none;'),
  1483. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1484. '/div'
  1485. );
  1486. $this->assertTags($result, $expected);
  1487. $result = $this->Form->error(
  1488. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1489. );
  1490. $this->assertEquals('Invalid email', $result);
  1491. $result = $this->Form->error(
  1492. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1493. );
  1494. $this->assertEquals('Invalid name', $result);
  1495. $result = $this->Form->error(
  1496. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1497. );
  1498. $this->assertEquals('Invalid city', $result);
  1499. unset($this->ValidateUser->ValidateProfile);
  1500. unset($this->ValidateUser);
  1501. }
  1502. /**
  1503. * testFormValidationAssociatedSecondLevel method
  1504. *
  1505. * test form error display with associated model.
  1506. *
  1507. * @return void
  1508. */
  1509. public function testFormValidationAssociatedSecondLevel() {
  1510. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1511. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1512. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1513. $data = array(
  1514. 'ValidateUser' => array('name' => 'mariano'),
  1515. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1516. 'ValidateItem' => array('name' => 'Item')
  1517. );
  1518. $result = $this->ValidateUser->create($data);
  1519. $this->assertFalse(empty($result));
  1520. $this->assertFalse($this->ValidateUser->validates());
  1521. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1522. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1523. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1524. $encoding = strtolower(Configure::read('App.encoding'));
  1525. $expected = array(
  1526. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1527. 'div' => array('style' => 'display:none;'),
  1528. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1529. '/div'
  1530. );
  1531. $this->assertTags($result, $expected);
  1532. $result = $this->Form->error(
  1533. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1534. );
  1535. $this->assertEquals('Invalid email', $result);
  1536. $result = $this->Form->error(
  1537. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1538. );
  1539. $this->assertEquals('Invalid name', $result);
  1540. $result = $this->Form->error(
  1541. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1542. );
  1543. $result = $this->Form->error(
  1544. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1545. );
  1546. $this->assertEquals('Invalid description', $result);
  1547. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1548. unset($this->ValidateUser->ValidateProfile);
  1549. unset($this->ValidateUser);
  1550. }
  1551. /**
  1552. * testFormValidationMultiRecord method
  1553. *
  1554. * test form error display with multiple records.
  1555. *
  1556. * @return void
  1557. */
  1558. public function testFormValidationMultiRecord() {
  1559. $Contact = ClassRegistry::getObject('Contact');
  1560. $Contact->validationErrors[2] = array(
  1561. 'name' => array('This field cannot be left blank')
  1562. );
  1563. $result = $this->Form->input('Contact.2.name');
  1564. $expected = array(
  1565. 'div' => array('class' => 'input text error'),
  1566. 'label' => array('for' => 'Contact2Name'),
  1567. 'Name',
  1568. '/label',
  1569. 'input' => array(
  1570. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1571. 'class' => 'form-error', 'maxlength' => 255
  1572. ),
  1573. array('div' => array('class' => 'error-message')),
  1574. 'This field cannot be left blank',
  1575. '/div',
  1576. '/div'
  1577. );
  1578. $this->assertTags($result, $expected);
  1579. }
  1580. /**
  1581. * testMultipleInputValidation method
  1582. *
  1583. * test multiple record form validation error display.
  1584. *
  1585. * @return void
  1586. */
  1587. public function testMultipleInputValidation() {
  1588. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1589. $Address->validationErrors[0] = array(
  1590. 'title' => array('This field cannot be empty'),
  1591. 'first_name' => array('This field cannot be empty')
  1592. );
  1593. $Address->validationErrors[1] = array(
  1594. 'last_name' => array('You must have a last name')
  1595. );
  1596. $this->Form->create();
  1597. $result = $this->Form->input('Address.0.title');
  1598. $expected = array(
  1599. 'div' => array('class'),
  1600. 'label' => array('for'),
  1601. 'preg:/[^<]+/',
  1602. '/label',
  1603. 'input' => array(
  1604. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1605. ),
  1606. array('div' => array('class' => 'error-message')),
  1607. 'This field cannot be empty',
  1608. '/div',
  1609. '/div'
  1610. );
  1611. $this->assertTags($result, $expected);
  1612. $result = $this->Form->input('Address.0.first_name');
  1613. $expected = array(
  1614. 'div' => array('class'),
  1615. 'label' => array('for'),
  1616. 'preg:/[^<]+/',
  1617. '/label',
  1618. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1619. array('div' => array('class' => 'error-message')),
  1620. 'This field cannot be empty',
  1621. '/div',
  1622. '/div'
  1623. );
  1624. $this->assertTags($result, $expected);
  1625. $result = $this->Form->input('Address.0.last_name');
  1626. $expected = array(
  1627. 'div' => array('class'),
  1628. 'label' => array('for'),
  1629. 'preg:/[^<]+/',
  1630. '/label',
  1631. 'input' => array('type' => 'text', 'name', 'id'),
  1632. '/div'
  1633. );
  1634. $this->assertTags($result, $expected);
  1635. $result = $this->Form->input('Address.1.last_name');
  1636. $expected = array(
  1637. 'div' => array('class'),
  1638. 'label' => array('for'),
  1639. 'preg:/[^<]+/',
  1640. '/label',
  1641. 'input' => array(
  1642. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1643. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1644. ),
  1645. array('div' => array('class' => 'error-message')),
  1646. 'You must have a last name',
  1647. '/div',
  1648. '/div'
  1649. );
  1650. $this->assertTags($result, $expected);
  1651. }
  1652. /**
  1653. * testInput method
  1654. *
  1655. * Test various incarnations of input().
  1656. *
  1657. * @return void
  1658. */
  1659. public function testInput() {
  1660. $result = $this->Form->input('ValidateUser.balance');
  1661. $expected = array(
  1662. 'div' => array('class'),
  1663. 'label' => array('for'),
  1664. 'Balance',
  1665. '/label',
  1666. 'input' => array('name', 'type' => 'number', 'id'),
  1667. '/div',
  1668. );
  1669. $this->assertTags($result, $expected);
  1670. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1671. $expected = array(
  1672. 'div' => array('class' => 'input email'),
  1673. 'label' => array('for' => 'custom'),
  1674. 'Email',
  1675. '/label',
  1676. array('input' => array(
  1677. 'type' => 'email', 'name' => 'data[Contact][email]',
  1678. 'id' => 'custom', 'maxlength' => 255
  1679. )),
  1680. '/div'
  1681. );
  1682. $this->assertTags($result, $expected);
  1683. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1684. $expected = array(
  1685. '<div',
  1686. 'label' => array('for' => 'ContactEmail'),
  1687. 'Email',
  1688. '/label',
  1689. array('input' => array(
  1690. 'type' => 'email', 'name' => 'data[Contact][email]',
  1691. 'id' => 'ContactEmail', 'maxlength' => 255
  1692. )),
  1693. '/div'
  1694. );
  1695. $this->assertTags($result, $expected);
  1696. $result = $this->Form->hidden('Contact.idontexist');
  1697. $expected = array('input' => array(
  1698. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1699. 'id' => 'ContactIdontexist'
  1700. ));
  1701. $this->assertTags($result, $expected);
  1702. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1703. $expected = array(
  1704. 'div' => array('class' => 'input text'),
  1705. 'label' => array('for' => 'ContactEmail'),
  1706. 'Email',
  1707. '/label',
  1708. array('input' => array(
  1709. 'type' => 'text', 'name' => 'data[Contact][email]',
  1710. 'id' => 'ContactEmail'
  1711. )),
  1712. '/div'
  1713. );
  1714. $this->assertTags($result, $expected);
  1715. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1716. $expected = array(
  1717. 'div' => array('class' => 'input text'),
  1718. 'label' => array('for' => 'Contact5Email'),
  1719. 'Email',
  1720. '/label',
  1721. array('input' => array(
  1722. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1723. 'id' => 'Contact5Email'
  1724. )),
  1725. '/div'
  1726. );
  1727. $this->assertTags($result, $expected);
  1728. $result = $this->Form->input('Contact.password');
  1729. $expected = array(
  1730. 'div' => array('class' => 'input password'),
  1731. 'label' => array('for' => 'ContactPassword'),
  1732. 'Password',
  1733. '/label',
  1734. array('input' => array(
  1735. 'type' => 'password', 'name' => 'data[Contact][password]',
  1736. 'id' => 'ContactPassword'
  1737. )),
  1738. '/div'
  1739. );
  1740. $this->assertTags($result, $expected);
  1741. $result = $this->Form->input('Contact.email', array(
  1742. 'type' => 'file', 'class' => 'textbox'
  1743. ));
  1744. $expected = array(
  1745. 'div' => array('class' => 'input file'),
  1746. 'label' => array('for' => 'ContactEmail'),
  1747. 'Email',
  1748. '/label',
  1749. array('input' => array(
  1750. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1751. 'id' => 'ContactEmail'
  1752. )),
  1753. '/div'
  1754. );
  1755. $this->assertTags($result, $expected);
  1756. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1757. $result = $this->Form->input('Contact.phone');
  1758. $expected = array(
  1759. 'div' => array('class' => 'input tel'),
  1760. 'label' => array('for' => 'ContactPhone'),
  1761. 'Phone',
  1762. '/label',
  1763. array('input' => array(
  1764. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1765. 'value' => 'Hello &amp; World &gt; weird chars',
  1766. 'id' => 'ContactPhone', 'maxlength' => 255
  1767. )),
  1768. '/div'
  1769. );
  1770. $this->assertTags($result, $expected);
  1771. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1772. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1773. $expected = array(
  1774. 'div' => array('class' => 'input text'),
  1775. 'label' => array('for' => 'myId'),
  1776. 'Field',
  1777. '/label',
  1778. 'input' => array(
  1779. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1780. 'value' => 'My value', 'id' => 'myId'
  1781. ),
  1782. '/div'
  1783. );
  1784. $this->assertTags($result, $expected);
  1785. unset($this->Form->request->data);
  1786. $Contact = ClassRegistry::getObject('Contact');
  1787. $Contact->validationErrors['field'] = array('Badness!');
  1788. $result = $this->Form->input('Contact.field');
  1789. $expected = array(
  1790. 'div' => array('class' => 'input text error'),
  1791. 'label' => array('for' => 'ContactField'),
  1792. 'Field',
  1793. '/label',
  1794. 'input' => array(
  1795. 'type' => 'text', 'name' => 'data[Contact][field]',
  1796. 'id' => 'ContactField', 'class' => 'form-error'
  1797. ),
  1798. array('div' => array('class' => 'error-message')),
  1799. 'Badness!',
  1800. '/div',
  1801. '/div'
  1802. );
  1803. $this->assertTags($result, $expected);
  1804. $result = $this->Form->input('Contact.field', array(
  1805. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1806. ));
  1807. $expected = array(
  1808. 'label' => array('for' => 'ContactField'),
  1809. 'Field',
  1810. '/label',
  1811. 'input' => array(
  1812. 'type' => 'text', 'name' => 'data[Contact][field]',
  1813. 'id' => 'ContactField', 'class' => 'form-error'
  1814. ),
  1815. array('span' => array('class' => 'error-message')),
  1816. 'Badness!',
  1817. '/span'
  1818. );
  1819. $this->assertTags($result, $expected);
  1820. $result = $this->Form->input('Contact.field', array(
  1821. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1822. ));
  1823. $expected = array(
  1824. 'div' => array('class' => 'input text error'),
  1825. 'label' => array('for' => 'ContactField'),
  1826. 'Field',
  1827. '/label',
  1828. 'input' => array(
  1829. 'type' => 'text', 'name' => 'data[Contact][field]',
  1830. 'id' => 'ContactField', 'class' => 'form-error'
  1831. ),
  1832. array('div' => array('class' => 'error')),
  1833. 'Badness!',
  1834. '/div'
  1835. );
  1836. $this->assertTags($result, $expected);
  1837. $result = $this->Form->input('Contact.field', array(
  1838. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1839. ));
  1840. $expected = array(
  1841. 'span' => array('class' => 'input text error'),
  1842. 'label' => array('for' => 'ContactField'),
  1843. 'Field',
  1844. '/label',
  1845. 'input' => array(
  1846. 'type' => 'text', 'name' => 'data[Contact][field]',
  1847. 'id' => 'ContactField', 'class' => 'form-error'
  1848. ),
  1849. 'Badness!',
  1850. '/span'
  1851. );
  1852. $this->assertTags($result, $expected);
  1853. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1854. $expected = array(
  1855. 'div' => array('class' => 'input text error'),
  1856. 'label' => array('for' => 'ContactField'),
  1857. 'Field',
  1858. '/label',
  1859. 'input' => array(
  1860. 'type' => 'text', 'name' => 'data[Contact][field]',
  1861. 'id' => 'ContactField', 'class' => 'form-error'
  1862. ),
  1863. 'A message to you, Rudy',
  1864. array('div' => array('class' => 'error-message')),
  1865. 'Badness!',
  1866. '/div',
  1867. '/div'
  1868. );
  1869. $this->assertTags($result, $expected);
  1870. $this->Form->setEntity(null);
  1871. $this->Form->setEntity('Contact.field');
  1872. $result = $this->Form->input('Contact.field', array(
  1873. 'after' => 'A message to you, Rudy', 'error' => false
  1874. ));
  1875. $expected = array(
  1876. 'div' => array('class' => 'input text'),
  1877. 'label' => array('for' => 'ContactField'),
  1878. 'Field',
  1879. '/label',
  1880. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1881. 'A message to you, Rudy',
  1882. '/div'
  1883. );
  1884. $this->assertTags($result, $expected);
  1885. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1886. $expected = array(
  1887. 'div' => array('class' => 'input text'),
  1888. 'label' => array('for' => 'ObjectField'),
  1889. 'Field',
  1890. '/label',
  1891. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1892. 'A message to you, Rudy',
  1893. '/div'
  1894. );
  1895. $this->assertTags($result, $expected);
  1896. $Contact->validationErrors['field'] = array('minLength');
  1897. $result = $this->Form->input('Contact.field', array(
  1898. 'error' => array(
  1899. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1900. 'maxLength' => 'login too large'
  1901. )
  1902. ));
  1903. $expected = array(
  1904. 'div' => array('class' => 'input text error'),
  1905. 'label' => array('for' => 'ContactField'),
  1906. 'Field',
  1907. '/label',
  1908. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1909. array('div' => array('class' => 'error-message')),
  1910. 'Le login doit contenir au moins 2 caractères',
  1911. '/div',
  1912. '/div'
  1913. );
  1914. $this->assertTags($result, $expected);
  1915. $Contact->validationErrors['field'] = array('maxLength');
  1916. $result = $this->Form->input('Contact.field', array(
  1917. 'error' => array(
  1918. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1919. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1920. 'maxLength' => 'login too large',
  1921. )
  1922. ));
  1923. $expected = array(
  1924. 'div' => array('class' => 'input text error'),
  1925. 'label' => array('for' => 'ContactField'),
  1926. 'Field',
  1927. '/label',
  1928. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1929. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1930. 'login too large',
  1931. '/span',
  1932. '/div'
  1933. );
  1934. $this->assertTags($result, $expected);
  1935. }
  1936. /**
  1937. * Test that inputs with 0 can be created.
  1938. *
  1939. * @return void
  1940. */
  1941. public function testInputZero() {
  1942. $this->Form->create('User');
  1943. $result = $this->Form->input('0');
  1944. $expected = array(
  1945. 'div' => array('class' => 'input text'),
  1946. 'label' => array('for' => 'User0'), '/label',
  1947. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1948. '/div'
  1949. );
  1950. $this->assertTags($result, $expected);
  1951. }
  1952. /**
  1953. * test input() with checkbox creation
  1954. *
  1955. * @return void
  1956. */
  1957. public function testInputCheckbox() {
  1958. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1959. $expected = array(
  1960. 'div' => array('class' => 'input checkbox'),
  1961. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1962. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1963. '/div'
  1964. );
  1965. $this->assertTags($result, $expected);
  1966. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1967. $expected = array(
  1968. 'div' => array('class' => 'input checkbox'),
  1969. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1970. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1971. '/div'
  1972. );
  1973. $this->assertTags($result, $expected);
  1974. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1975. $expected = array(
  1976. 'div' => array('class' => 'input checkbox'),
  1977. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1978. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1979. '/div'
  1980. );
  1981. $this->assertTags($result, $expected);
  1982. $result = $this->Form->input('User.disabled', array(
  1983. 'label' => 'Disabled',
  1984. 'type' => 'checkbox',
  1985. 'data-foo' => 'disabled'
  1986. ));
  1987. $expected = array(
  1988. 'div' => array('class' => 'input checkbox'),
  1989. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  1990. array('input' => array(
  1991. 'type' => 'checkbox',
  1992. 'name' => 'data[User][disabled]',
  1993. 'value' => '1',
  1994. 'id' => 'UserDisabled',
  1995. 'data-foo' => 'disabled'
  1996. )),
  1997. 'label' => array('for' => 'UserDisabled'),
  1998. 'Disabled',
  1999. '/label',
  2000. '/div'
  2001. );
  2002. $this->assertTags($result, $expected);
  2003. }
  2004. /**
  2005. * test form->input() with time types.
  2006. *
  2007. */
  2008. public function testInputTime() {
  2009. extract($this->dateRegex);
  2010. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2011. $result = explode(':', $result);
  2012. $this->assertRegExp('/option value="23"/', $result[0]);
  2013. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2014. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2015. $result = explode(':', $result);
  2016. $this->assertRegExp('/option value="23"/', $result[0]);
  2017. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2018. $result = $this->Form->input('Model.field', array(
  2019. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2020. ));
  2021. $result = explode(':', $result);
  2022. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2023. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2024. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2025. $result = $this->Form->input('Model.field', array(
  2026. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2027. ));
  2028. $result = explode(':', $result);
  2029. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2030. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2031. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2032. $result = $this->Form->input('prueba', array(
  2033. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2034. 'maxYear' => date('Y') + 1, 'interval' => 15
  2035. ));
  2036. $result = explode(':', $result);
  2037. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2038. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2039. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2040. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2041. $result = $this->Form->input('Random.start_time', array(
  2042. 'type' => 'time',
  2043. 'selected' => '18:15'
  2044. ));
  2045. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2046. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2047. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2048. $result = $this->Form->input('published', array('type' => 'time'));
  2049. $now = strtotime('now');
  2050. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2051. $now = strtotime('2013-03-09 00:42:21');
  2052. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2053. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2054. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2055. }
  2056. /**
  2057. * Test interval + selected near the hour roll over.
  2058. *
  2059. * @return void
  2060. */
  2061. public function testTimeSelectedWithInterval() {
  2062. $result = $this->Form->input('Model.start_time', array(
  2063. 'type' => 'time',
  2064. 'interval' => 15,
  2065. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2066. ));
  2067. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2068. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2069. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2070. $result = $this->Form->input('Model.start_time', array(
  2071. 'type' => 'time',
  2072. 'interval' => 15,
  2073. 'selected' => '2012-10-23 15:57:00'
  2074. ));
  2075. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2076. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2077. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2078. $result = $this->Form->input('Model.start_time', array(
  2079. 'timeFormat' => 24,
  2080. 'type' => 'time',
  2081. 'interval' => 15,
  2082. 'selected' => '15:57'
  2083. ));
  2084. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2085. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2086. $result = $this->Form->input('Model.start_time', array(
  2087. 'timeFormat' => 24,
  2088. 'type' => 'time',
  2089. 'interval' => 15,
  2090. 'selected' => '23:57'
  2091. ));
  2092. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2093. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2094. $result = $this->Form->input('Model.created', array(
  2095. 'timeFormat' => 24,
  2096. 'type' => 'datetime',
  2097. 'interval' => 15,
  2098. 'selected' => '2012-09-30 23:56'
  2099. ));
  2100. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2101. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2102. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2103. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2104. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2105. }
  2106. /**
  2107. * Test interval & timeFormat = 12
  2108. *
  2109. * @return void
  2110. */
  2111. public function testInputTimeWithIntervalAnd12HourFormat() {
  2112. $result = $this->Form->input('Model.start_time', array(
  2113. 'type' => 'time',
  2114. 'timeFormat' => 12,
  2115. 'interval' => 5,
  2116. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2117. ));
  2118. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2119. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2120. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2121. $result = $this->Form->input('Model.start_time', array(
  2122. 'type' => 'time',
  2123. 'timeFormat' => '12',
  2124. 'interval' => 5,
  2125. 'selected' => '2013-04-19 16:30:00'
  2126. ));
  2127. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2128. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2129. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2130. $result = $this->Form->input('Model.start_time', array(
  2131. 'type' => 'time',
  2132. 'timeFormat' => '12',
  2133. 'interval' => 10,
  2134. 'selected' => '2013-05-19 00:33:00'
  2135. ));
  2136. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2137. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2138. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2139. $result = $this->Form->input('Model.start_time', array(
  2140. 'type' => 'time',
  2141. 'timeFormat' => '12',
  2142. 'interval' => 10,
  2143. 'selected' => '2013-05-19 13:33:00'
  2144. ));
  2145. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2146. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2147. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2148. $result = $this->Form->input('Model.start_time', array(
  2149. 'type' => 'time',
  2150. 'timeFormat' => '12',
  2151. 'interval' => 10,
  2152. 'selected' => '2013-05-19 01:33:00'
  2153. ));
  2154. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2155. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2156. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2157. }
  2158. /**
  2159. * test form->input() with datetime, date and time types
  2160. *
  2161. * @return void
  2162. */
  2163. public function testInputDatetime() {
  2164. extract($this->dateRegex);
  2165. $result = $this->Form->input('prueba', array(
  2166. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2167. 'maxYear' => date('Y') + 1, 'interval' => 15
  2168. ));
  2169. $result = explode(':', $result);
  2170. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2171. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2172. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2173. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2174. //related to ticket #5013
  2175. $result = $this->Form->input('Contact.date', array(
  2176. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2177. ));
  2178. $this->assertRegExp('/class="customClass"/', $result);
  2179. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2180. $result = $this->Form->input('Contact.date', array(
  2181. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2182. ));
  2183. $this->assertRegExp('/id="customIdDay"/', $result);
  2184. $this->assertRegExp('/id="customIdMonth"/', $result);
  2185. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2186. $result = $this->Form->input('Model.field', array(
  2187. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2188. ));
  2189. $this->assertRegExp('/id="customIDDay"/', $result);
  2190. $this->assertRegExp('/id="customIDHour"/', $result);
  2191. $result = explode('</select><select', $result);
  2192. $result = explode(':', $result[1]);
  2193. $this->assertRegExp('/option value="23"/', $result[0]);
  2194. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2195. $result = $this->Form->input('Model.field', array(
  2196. 'type' => 'datetime', 'timeFormat' => 12
  2197. ));
  2198. $result = explode('</select><select', $result);
  2199. $result = explode(':', $result[1]);
  2200. $this->assertRegExp('/option value="12"/', $result[0]);
  2201. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2202. $this->Form->request->data = array('Contact' => array('created' => null));
  2203. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2204. $expected = array(
  2205. 'div' => array('class' => 'input date'),
  2206. 'label' => array('for' => 'ContactCreatedMonth'),
  2207. 'Created',
  2208. '/label',
  2209. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2210. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2211. $monthsRegex,
  2212. '/select', '-',
  2213. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2214. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2215. $daysRegex,
  2216. '/select', '-',
  2217. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2218. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2219. $yearsRegex,
  2220. '/select',
  2221. '/div'
  2222. );
  2223. $this->assertTags($result, $expected);
  2224. $this->Form->request->data = array('Contact' => array('created' => null));
  2225. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2226. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2227. $this->Form->request->data = array('Contact' => array('created' => null));
  2228. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2229. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2230. $result = $this->Form->input('Contact.created', array(
  2231. 'type' => 'date',
  2232. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2233. 'timeFormat' => 'NONE'
  2234. ));
  2235. $this->assertRegExp('/for\="created-month"/', $result);
  2236. }
  2237. /**
  2238. * Test generating checkboxes in a loop.
  2239. *
  2240. * @return void
  2241. */
  2242. public function testInputCheckboxesInLoop() {
  2243. for ($i = 1; $i < 5; $i++) {
  2244. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2245. $expected = array(
  2246. 'div' => array('class' => 'input checkbox'),
  2247. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2248. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2249. 'label' => array('for' => "Contact{$i}Email"),
  2250. 'Email',
  2251. '/label',
  2252. '/div'
  2253. );
  2254. $this->assertTags($result, $expected);
  2255. }
  2256. }
  2257. /**
  2258. * Test generating checkboxes with disabled elements.
  2259. *
  2260. * @return void
  2261. */
  2262. public function testInputCheckboxWithDisabledElements() {
  2263. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2264. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2265. $expected = array(
  2266. array('div' => array('class' => 'input select')),
  2267. array('label' => array('for' => "ContactMultiple")),
  2268. 'Multiple',
  2269. '/label',
  2270. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2271. array('div' => array('class' => 'checkbox')),
  2272. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2273. array('label' => array('for' => "ContactMultiple1")),
  2274. 'One',
  2275. '/label',
  2276. '/div',
  2277. array('div' => array('class' => 'checkbox')),
  2278. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2279. array('label' => array('for' => "ContactMultiple2")),
  2280. 'Two',
  2281. '/label',
  2282. '/div',
  2283. array('div' => array('class' => 'checkbox')),
  2284. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2285. array('label' => array('for' => "ContactMultiple3")),
  2286. 'Three',
  2287. '/label',
  2288. '/div',
  2289. '/div'
  2290. );
  2291. $this->assertTags($result, $expected);
  2292. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2293. $this->assertTags($result, $expected);
  2294. $disabled = array('2', 3);
  2295. $expected = array(
  2296. array('div' => array('class' => 'input select')),
  2297. array('label' => array('for' => "ContactMultiple")),
  2298. 'Multiple',
  2299. '/label',
  2300. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2301. array('div' => array('class' => 'checkbox')),
  2302. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2303. array('label' => array('for' => "ContactMultiple1")),
  2304. 'One',
  2305. '/label',
  2306. '/div',
  2307. array('div' => array('class' => 'checkbox')),
  2308. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2309. array('label' => array('for' => "ContactMultiple2")),
  2310. 'Two',
  2311. '/label',
  2312. '/div',
  2313. array('div' => array('class' => 'checkbox')),
  2314. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2315. array('label' => array('for' => "ContactMultiple3")),
  2316. 'Three',
  2317. '/label',
  2318. '/div',
  2319. '/div'
  2320. );
  2321. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2322. $this->assertTags($result, $expected);
  2323. // make sure 50 does only disable 50, and not 50f5c0cf
  2324. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2325. $disabled = array(50);
  2326. $expected = array(
  2327. array('div' => array('class' => 'input select')),
  2328. array('label' => array('for' => "ContactMultiple")),
  2329. 'Multiple',
  2330. '/label',
  2331. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2332. array('div' => array('class' => 'checkbox')),
  2333. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2334. array('label' => array('for' => "ContactMultiple50")),
  2335. 'Fifty',
  2336. '/label',
  2337. '/div',
  2338. array('div' => array('class' => 'checkbox')),
  2339. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2340. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2341. 'Stringy',
  2342. '/label',
  2343. '/div',
  2344. '/div'
  2345. );
  2346. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2347. $this->assertTags($result, $expected);
  2348. }
  2349. /**
  2350. * test input name with leading integer, ensure attributes are generated correctly.
  2351. *
  2352. * @return void
  2353. */
  2354. public function testInputWithLeadingInteger() {
  2355. $result = $this->Form->text('0.Node.title');
  2356. $expected = array(
  2357. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2358. );
  2359. $this->assertTags($result, $expected);
  2360. }
  2361. /**
  2362. * test form->input() with select type inputs.
  2363. *
  2364. * @return void
  2365. */
  2366. public function testInputSelectType() {
  2367. $result = $this->Form->input('email', array(
  2368. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2369. );
  2370. $expected = array(
  2371. 'div' => array('class' => 'input select'),
  2372. 'label' => array('for' => 'email'),
  2373. 'Email',
  2374. '/label',
  2375. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2376. array('option' => array('value' => '')),
  2377. '/option',
  2378. array('option' => array('value' => 'è')),
  2379. 'Firést',
  2380. '/option',
  2381. array('option' => array('value' => 'é')),
  2382. 'Secoènd',
  2383. '/option',
  2384. '/select',
  2385. '/div'
  2386. );
  2387. $this->assertTags($result, $expected);
  2388. $result = $this->Form->input('email', array(
  2389. 'options' => array('First', 'Second'), 'empty' => true)
  2390. );
  2391. $expected = array(
  2392. 'div' => array('class' => 'input select'),
  2393. 'label' => array('for' => 'email'),
  2394. 'Email',
  2395. '/label',
  2396. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2397. array('option' => array('value' => '')),
  2398. '/option',
  2399. array('option' => array('value' => '0')),
  2400. 'First',
  2401. '/option',
  2402. array('option' => array('value' => '1')),
  2403. 'Second',
  2404. '/option',
  2405. '/select',
  2406. '/div'
  2407. );
  2408. $this->assertTags($result, $expected);
  2409. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2410. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2411. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2412. $expected = array(
  2413. 'div' => array('class' => 'input select'),
  2414. 'label' => array('for' => 'ModelUserId'),
  2415. 'User',
  2416. '/label',
  2417. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2418. array('option' => array('value' => '')),
  2419. '/option',
  2420. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2421. 'good',
  2422. '/option',
  2423. array('option' => array('value' => 'other')),
  2424. 'bad',
  2425. '/option',
  2426. '/select',
  2427. '/div'
  2428. );
  2429. $this->assertTags($result, $expected);
  2430. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2431. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2432. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2433. $expected = array(
  2434. 'div' => array('class' => 'input select'),
  2435. 'label' => array('for' => 'ThingUserId'),
  2436. 'User',
  2437. '/label',
  2438. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2439. array('option' => array('value' => '')),
  2440. 'Some Empty',
  2441. '/option',
  2442. array('option' => array('value' => 'value')),
  2443. 'good',
  2444. '/option',
  2445. array('option' => array('value' => 'other')),
  2446. 'bad',
  2447. '/option',
  2448. '/select',
  2449. '/div'
  2450. );
  2451. $this->assertTags($result, $expected);
  2452. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2453. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2454. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2455. $expected = array(
  2456. 'div' => array('class' => 'input select'),
  2457. 'label' => array('for' => 'ThingUserId'),
  2458. 'User',
  2459. '/label',
  2460. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2461. array('option' => array('value' => '')),
  2462. 'Some Empty',
  2463. '/option',
  2464. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2465. 'good',
  2466. '/option',
  2467. array('option' => array('value' => 'other')),
  2468. 'bad',
  2469. '/option',
  2470. '/select',
  2471. '/div'
  2472. );
  2473. $this->assertTags($result, $expected);
  2474. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2475. $this->Form->request->data = array('User' => array('User' => array('value')));
  2476. $result = $this->Form->input('User.User', array('empty' => true));
  2477. $expected = array(
  2478. 'div' => array('class' => 'input select'),
  2479. 'label' => array('for' => 'UserUser'),
  2480. 'User',
  2481. '/label',
  2482. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2483. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2484. array('option' => array('value' => '')),
  2485. '/option',
  2486. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2487. 'good',
  2488. '/option',
  2489. array('option' => array('value' => 'other')),
  2490. 'bad',
  2491. '/option',
  2492. '/select',
  2493. '/div'
  2494. );
  2495. $this->assertTags($result, $expected);
  2496. $this->Form->data = array();
  2497. $result = $this->Form->input('Publisher.id', array(
  2498. 'label' => 'Publisher',
  2499. 'type' => 'select',
  2500. 'multiple' => 'checkbox',
  2501. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2502. ));
  2503. $expected = array(
  2504. array('div' => array('class' => 'input select')),
  2505. array('label' => array('for' => 'PublisherId')),
  2506. 'Publisher',
  2507. '/label',
  2508. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2509. array('div' => array('class' => 'checkbox')),
  2510. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2511. array('label' => array('for' => 'PublisherIdValue1')),
  2512. 'Label 1',
  2513. '/label',
  2514. '/div',
  2515. array('div' => array('class' => 'checkbox')),
  2516. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2517. array('label' => array('for' => 'PublisherIdValue2')),
  2518. 'Label 2',
  2519. '/label',
  2520. '/div',
  2521. '/div'
  2522. );
  2523. $this->assertTags($result, $expected);
  2524. }
  2525. /**
  2526. * test that input() and a non standard primary key makes a hidden input by default.
  2527. *
  2528. * @return void
  2529. */
  2530. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2531. $this->Form->create('User');
  2532. $this->Form->fieldset = array(
  2533. 'User' => array(
  2534. 'fields' => array(
  2535. 'model_id' => array('type' => 'integer')
  2536. ),
  2537. 'validates' => array(),
  2538. 'key' => 'model_id'
  2539. )
  2540. );
  2541. $result = $this->Form->input('model_id');
  2542. $expected = array(
  2543. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2544. );
  2545. $this->assertTags($result, $expected);
  2546. }
  2547. /**
  2548. * test that overriding the magic select type widget is possible
  2549. *
  2550. * @return void
  2551. */
  2552. public function testInputOverridingMagicSelectType() {
  2553. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2554. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2555. $expected = array(
  2556. 'div' => array('class' => 'input text'),
  2557. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2558. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2559. '/div'
  2560. );
  2561. $this->assertTags($result, $expected);
  2562. //Check that magic types still work for plural/singular vars
  2563. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2564. $result = $this->Form->input('Model.type');
  2565. $expected = array(
  2566. 'div' => array('class' => 'input select'),
  2567. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2568. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2569. array('option' => array('value' => 'value')), 'good', '/option',
  2570. array('option' => array('value' => 'other')), 'bad', '/option',
  2571. '/select',
  2572. '/div'
  2573. );
  2574. $this->assertTags($result, $expected);
  2575. }
  2576. /**
  2577. * Test that magic input() selects are created for type=number
  2578. *
  2579. * @return void
  2580. */
  2581. public function testInputMagicSelectForTypeNumber() {
  2582. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2583. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2584. $result = $this->Form->input('ValidateUser.balance');
  2585. $expected = array(
  2586. 'div' => array('class' => 'input select'),
  2587. 'label' => array('for' => 'ValidateUserBalance'),
  2588. 'Balance',
  2589. '/label',
  2590. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2591. array('option' => array('value' => '0')),
  2592. 'nothing',
  2593. '/option',
  2594. array('option' => array('value' => '1', 'selected' => 'selected')),
  2595. 'some',
  2596. '/option',
  2597. array('option' => array('value' => '100')),
  2598. 'a lot',
  2599. '/option',
  2600. '/select',
  2601. '/div'
  2602. );
  2603. $this->assertTags($result, $expected);
  2604. }
  2605. /**
  2606. * Test that magic input() selects can easily be converted into radio types without error.
  2607. *
  2608. * @return void
  2609. */
  2610. public function testInputMagicSelectChangeToRadio() {
  2611. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2612. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2613. $this->assertRegExp('/input type="radio"/', $result);
  2614. }
  2615. /**
  2616. * fields with the same name as the model should work.
  2617. *
  2618. * @return void
  2619. */
  2620. public function testInputWithMatchingFieldAndModelName() {
  2621. $this->Form->create('User');
  2622. $this->Form->fieldset = array(
  2623. 'User' => array(
  2624. 'fields' => array(
  2625. 'User' => array('type' => 'text')
  2626. ),
  2627. 'validates' => array(),
  2628. 'key' => 'id'
  2629. )
  2630. );
  2631. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2632. $result = $this->Form->input('User', array('type' => 'text'));
  2633. $expected = array(
  2634. 'div' => array('class' => 'input text'),
  2635. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2636. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2637. '/div'
  2638. );
  2639. $this->assertTags($result, $expected);
  2640. }
  2641. /**
  2642. * testFormInputs method
  2643. *
  2644. * test correct results from form::inputs().
  2645. *
  2646. * @return void
  2647. */
  2648. public function testFormInputs() {
  2649. $this->Form->create('Contact');
  2650. $result = $this->Form->inputs('The Legend');
  2651. $expected = array(
  2652. '<fieldset',
  2653. '<legend',
  2654. 'The Legend',
  2655. '/legend',
  2656. '*/fieldset',
  2657. );
  2658. $this->assertTags($result, $expected);
  2659. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2660. $expected = array(
  2661. 'fieldset' => array('class' => 'classy-stuff'),
  2662. '<legend',
  2663. 'Field of Dreams',
  2664. '/legend',
  2665. '*/fieldset'
  2666. );
  2667. $this->assertTags($result, $expected);
  2668. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2669. $this->assertTags($result, $expected);
  2670. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2671. $this->assertTags($result, $expected);
  2672. $this->Form->create('Contact');
  2673. $this->Form->request['prefix'] = 'admin';
  2674. $this->Form->request['action'] = 'admin_edit';
  2675. $result = $this->Form->inputs();
  2676. $expected = array(
  2677. '<fieldset',
  2678. '<legend',
  2679. 'Edit Contact',
  2680. '/legend',
  2681. '*/fieldset',
  2682. );
  2683. $this->assertTags($result, $expected);
  2684. $this->Form->create('Contact');
  2685. $result = $this->Form->inputs(false);
  2686. $expected = array(
  2687. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2688. array('div' => array('class' => 'input text')),
  2689. '*/div',
  2690. array('div' => array('class' => 'input email')),
  2691. '*/div',
  2692. array('div' => array('class' => 'input tel')),
  2693. '*/div',
  2694. array('div' => array('class' => 'input password')),
  2695. '*/div',
  2696. array('div' => array('class' => 'input date')),
  2697. '*/div',
  2698. array('div' => array('class' => 'input date')),
  2699. '*/div',
  2700. array('div' => array('class' => 'input datetime')),
  2701. '*/div',
  2702. array('div' => array('class' => 'input number')),
  2703. '*/div',
  2704. array('div' => array('class' => 'input select')),
  2705. '*/div',
  2706. );
  2707. $this->assertTags($result, $expected);
  2708. $this->Form->create('Contact');
  2709. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2710. $expected = array(
  2711. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2712. array('div' => array('class' => 'input text')),
  2713. '*/div',
  2714. array('div' => array('class' => 'input email')),
  2715. '*/div',
  2716. array('div' => array('class' => 'input tel')),
  2717. '*/div',
  2718. array('div' => array('class' => 'input password')),
  2719. '*/div',
  2720. array('div' => array('class' => 'input date')),
  2721. '*/div',
  2722. array('div' => array('class' => 'input date')),
  2723. '*/div',
  2724. array('div' => array('class' => 'input datetime')),
  2725. '*/div',
  2726. array('div' => array('class' => 'input number')),
  2727. '*/div',
  2728. array('div' => array('class' => 'input select')),
  2729. '*/div',
  2730. );
  2731. $this->assertTags($result, $expected);
  2732. $this->Form->create('Contact');
  2733. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2734. $this->assertTags($result, $expected);
  2735. $this->Form->create('Contact');
  2736. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2737. $expected = array(
  2738. 'fieldset' => array(),
  2739. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2740. array('div' => array('class' => 'input text')),
  2741. '*/div',
  2742. array('div' => array('class' => 'input email')),
  2743. '*/div',
  2744. array('div' => array('class' => 'input tel')),
  2745. '*/div',
  2746. array('div' => array('class' => 'input password')),
  2747. '*/div',
  2748. array('div' => array('class' => 'input date')),
  2749. '*/div',
  2750. array('div' => array('class' => 'input date')),
  2751. '*/div',
  2752. array('div' => array('class' => 'input datetime')),
  2753. '*/div',
  2754. array('div' => array('class' => 'input number')),
  2755. '*/div',
  2756. array('div' => array('class' => 'input select')),
  2757. '*/div',
  2758. '/fieldset'
  2759. );
  2760. $this->assertTags($result, $expected);
  2761. $this->Form->create('Contact');
  2762. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2763. $expected = array(
  2764. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2765. array('div' => array('class' => 'input text')),
  2766. '*/div',
  2767. array('div' => array('class' => 'input email')),
  2768. '*/div',
  2769. array('div' => array('class' => 'input tel')),
  2770. '*/div',
  2771. array('div' => array('class' => 'input password')),
  2772. '*/div',
  2773. array('div' => array('class' => 'input date')),
  2774. '*/div',
  2775. array('div' => array('class' => 'input date')),
  2776. '*/div',
  2777. array('div' => array('class' => 'input datetime')),
  2778. '*/div',
  2779. array('div' => array('class' => 'input number')),
  2780. '*/div',
  2781. array('div' => array('class' => 'input select')),
  2782. '*/div',
  2783. );
  2784. $this->assertTags($result, $expected);
  2785. $this->Form->create('Contact');
  2786. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2787. $this->assertTags($result, $expected);
  2788. $this->Form->create('Contact');
  2789. $result = $this->Form->inputs('Hello');
  2790. $expected = array(
  2791. 'fieldset' => array(),
  2792. 'legend' => array(),
  2793. 'Hello',
  2794. '/legend',
  2795. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2796. array('div' => array('class' => 'input text')),
  2797. '*/div',
  2798. array('div' => array('class' => 'input email')),
  2799. '*/div',
  2800. array('div' => array('class' => 'input tel')),
  2801. '*/div',
  2802. array('div' => array('class' => 'input password')),
  2803. '*/div',
  2804. array('div' => array('class' => 'input date')),
  2805. '*/div',
  2806. array('div' => array('class' => 'input date')),
  2807. '*/div',
  2808. array('div' => array('class' => 'input datetime')),
  2809. '*/div',
  2810. array('div' => array('class' => 'input number')),
  2811. '*/div',
  2812. array('div' => array('class' => 'input select')),
  2813. '*/div',
  2814. '/fieldset'
  2815. );
  2816. $this->assertTags($result, $expected);
  2817. $this->Form->create('Contact');
  2818. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2819. $expected = array(
  2820. 'fieldset' => array(),
  2821. 'legend' => array(),
  2822. 'Hello',
  2823. '/legend',
  2824. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2825. array('div' => array('class' => 'input text')),
  2826. '*/div',
  2827. array('div' => array('class' => 'input email')),
  2828. '*/div',
  2829. array('div' => array('class' => 'input tel')),
  2830. '*/div',
  2831. array('div' => array('class' => 'input password')),
  2832. '*/div',
  2833. array('div' => array('class' => 'input date')),
  2834. '*/div',
  2835. array('div' => array('class' => 'input date')),
  2836. '*/div',
  2837. array('div' => array('class' => 'input datetime')),
  2838. '*/div',
  2839. array('div' => array('class' => 'input number')),
  2840. '*/div',
  2841. array('div' => array('class' => 'input select')),
  2842. '*/div',
  2843. '/fieldset'
  2844. );
  2845. $this->assertTags($result, $expected);
  2846. $this->Form->create('Contact');
  2847. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2848. $this->assertTags($result, $expected);
  2849. $this->Form->end();
  2850. $this->Form->create(false);
  2851. $expected = array(
  2852. 'fieldset' => array(),
  2853. array('div' => array('class' => 'input text')),
  2854. 'label' => array('for' => 'foo'),
  2855. 'Foo',
  2856. '/label',
  2857. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2858. '*/div',
  2859. '/fieldset'
  2860. );
  2861. $result = $this->Form->inputs(
  2862. array('foo' => array('type' => 'text')),
  2863. array(),
  2864. array('legend' => false)
  2865. );
  2866. $this->assertTags($result, $expected);
  2867. }
  2868. /**
  2869. * Tests inputs() works with plugin models
  2870. *
  2871. * @return void
  2872. */
  2873. public function testInputsPluginModel() {
  2874. $this->loadFixtures('Post');
  2875. App::build(array(
  2876. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2877. ));
  2878. CakePlugin::load('TestPlugin');
  2879. $this->Form->request['models'] = array(
  2880. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2881. );
  2882. $this->Form->create('TestPlugin.TestPluginPost');
  2883. $result = $this->Form->inputs();
  2884. $this->assertContains('data[TestPluginPost][id]', $result);
  2885. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2886. $this->assertContains('data[TestPluginPost][title]', $result);
  2887. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2888. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2889. $this->assertEquals('TestPluginPost', $this->Form->model());
  2890. }
  2891. /**
  2892. * testSelectAsCheckbox method
  2893. *
  2894. * test multi-select widget with checkbox formatting.
  2895. *
  2896. * @return void
  2897. */
  2898. public function testSelectAsCheckbox() {
  2899. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2900. $expected = array(
  2901. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2902. array('div' => array('class' => 'checkbox')),
  2903. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2904. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2905. 'first',
  2906. '/label',
  2907. '/div',
  2908. array('div' => array('class' => 'checkbox')),
  2909. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2910. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2911. 'second',
  2912. '/label',
  2913. '/div',
  2914. array('div' => array('class' => 'checkbox')),
  2915. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2916. array('label' => array('for' => 'ModelMultiField2')),
  2917. 'third',
  2918. '/label',
  2919. '/div',
  2920. );
  2921. $this->assertTags($result, $expected);
  2922. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2923. $expected = array(
  2924. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2925. array('div' => array('class' => 'checkbox')),
  2926. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2927. array('label' => array('for' => 'ModelMultiField12')),
  2928. 'half',
  2929. '/label',
  2930. '/div',
  2931. );
  2932. $this->assertTags($result, $expected);
  2933. }
  2934. /**
  2935. * testLabel method
  2936. *
  2937. * test label generation.
  2938. *
  2939. * @return void
  2940. */
  2941. public function testLabel() {
  2942. $this->Form->text('Person.name');
  2943. $result = $this->Form->label();
  2944. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2945. $this->Form->text('Person.name');
  2946. $result = $this->Form->label();
  2947. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2948. $result = $this->Form->label('Person.first_name');
  2949. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2950. $result = $this->Form->label('Person.first_name', 'Your first name');
  2951. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2952. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2953. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2954. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2955. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2956. $result = $this->Form->label('Person.first_name', '');
  2957. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2958. $result = $this->Form->label('Person.2.name', '');
  2959. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2960. }
  2961. /**
  2962. * testTextbox method
  2963. *
  2964. * test textbox element generation
  2965. *
  2966. * @return void
  2967. */
  2968. public function testTextbox() {
  2969. $result = $this->Form->text('Model.field');
  2970. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2971. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2972. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2973. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2974. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2975. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2976. $result = $this->Form->text('Model.text');
  2977. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2978. $Contact = ClassRegistry::getObject('Contact');
  2979. $Contact->validationErrors['text'] = array(true);
  2980. $this->Form->request->data['Contact']['text'] = 'test';
  2981. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2982. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2983. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2984. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2985. $expected = array(
  2986. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2987. );
  2988. $this->assertTags($result, $expected);
  2989. }
  2990. /**
  2991. * testDefaultValue method
  2992. *
  2993. * Test default value setting
  2994. *
  2995. * @return void
  2996. */
  2997. public function testDefaultValue() {
  2998. $this->Form->request->data['Model']['field'] = 'test';
  2999. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3000. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3001. unset($this->Form->request->data['Model']['field']);
  3002. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3003. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3004. }
  3005. /**
  3006. * testCheckboxDefaultValue method
  3007. *
  3008. * Test default value setting on checkbox() method
  3009. *
  3010. * @return void
  3011. */
  3012. public function testCheckboxDefaultValue() {
  3013. $this->Form->request->data['Model']['field'] = false;
  3014. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3015. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3016. unset($this->Form->request->data['Model']['field']);
  3017. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3018. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3019. $this->Form->request->data['Model']['field'] = true;
  3020. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3021. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3022. unset($this->Form->request->data['Model']['field']);
  3023. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3024. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3025. }
  3026. /**
  3027. * testError method
  3028. *
  3029. * Test field error generation
  3030. *
  3031. * @return void
  3032. */
  3033. public function testError() {
  3034. $Contact = ClassRegistry::getObject('Contact');
  3035. $Contact->validationErrors['field'] = array(1);
  3036. $result = $this->Form->error('Contact.field');
  3037. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3038. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3039. $this->assertEquals('Error in field Field', $result);
  3040. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3041. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3042. $this->assertEquals('This field contains invalid input', $result);
  3043. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3044. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3045. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3046. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3047. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3048. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3049. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3050. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3051. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3052. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3053. $this->assertEquals('<strong>Badness!</strong>', $result);
  3054. $Contact->validationErrors['field'] = array("email");
  3055. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3056. $expected = array(
  3057. 'div' => array('class' => 'field-error'),
  3058. 'No good!',
  3059. '/div'
  3060. );
  3061. $this->assertTags($result, $expected);
  3062. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3063. $result = $this->Form->error('Contact.field', array(
  3064. 'notEmpty' => 'Cannot be empty',
  3065. 'email' => 'No good!'
  3066. ));
  3067. $expected = array(
  3068. 'div' => array('class' => 'error-message'),
  3069. 'ul' => array(),
  3070. '<li', 'Cannot be empty', '/li',
  3071. '<li', 'No good!', '/li',
  3072. '<li', 'Something else', '/li',
  3073. '/ul',
  3074. '/div'
  3075. );
  3076. $this->assertTags($result, $expected);
  3077. // Testing error messages list options
  3078. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3079. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3080. $expected = array(
  3081. 'div' => array('class' => 'error-message'),
  3082. 'ol' => array(),
  3083. '<li', 'notEmpty', '/li',
  3084. '<li', 'email', '/li',
  3085. '/ol',
  3086. '/div'
  3087. );
  3088. $this->assertTags($result, $expected);
  3089. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3090. $expected = array(
  3091. 'div' => array('class' => 'error-message'),
  3092. 'ol' => array(),
  3093. '<li', 'notEmpty', '/li',
  3094. '<li', 'email', '/li',
  3095. '/ol',
  3096. '/div'
  3097. );
  3098. $this->assertTags($result, $expected);
  3099. $result = $this->Form->error('Contact.field', null, array(
  3100. 'listOptions' => array(
  3101. 'class' => 'ul-class',
  3102. 'itemOptions' => array(
  3103. 'class' => 'li-class'
  3104. )
  3105. )
  3106. ));
  3107. $expected = array(
  3108. 'div' => array('class' => 'error-message'),
  3109. 'ul' => array('class' => 'ul-class'),
  3110. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3111. array('li' => array('class' => 'li-class')), 'email', '/li',
  3112. '/ul',
  3113. '/div'
  3114. );
  3115. $this->assertTags($result, $expected);
  3116. }
  3117. /**
  3118. * test error options when using form->input();
  3119. *
  3120. * @return void
  3121. */
  3122. public function testInputErrorEscape() {
  3123. $this->Form->create('ValidateProfile');
  3124. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3125. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3126. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3127. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3128. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3129. $this->assertRegExp('/required<br>/', $result);
  3130. }
  3131. /**
  3132. * testPassword method
  3133. *
  3134. * Test password element generation
  3135. *
  3136. * @return void
  3137. */
  3138. public function testPassword() {
  3139. $Contact = ClassRegistry::getObject('Contact');
  3140. $result = $this->Form->password('Contact.field');
  3141. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3142. $Contact->validationErrors['passwd'] = 1;
  3143. $this->Form->request->data['Contact']['passwd'] = 'test';
  3144. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3145. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3146. }
  3147. /**
  3148. * testRadio method
  3149. *
  3150. * Test radio element set generation
  3151. *
  3152. * @return void
  3153. */
  3154. public function testRadio() {
  3155. $result = $this->Form->radio('Model.field', array('option A'));
  3156. $expected = array(
  3157. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3158. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3159. 'label' => array('for' => 'ModelField0'),
  3160. 'option A',
  3161. '/label'
  3162. );
  3163. $this->assertTags($result, $expected);
  3164. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3165. $expected = array(
  3166. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3167. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3168. 'label' => array('for' => 'ModelField12'),
  3169. 'half',
  3170. '/label'
  3171. );
  3172. $this->assertTags($result, $expected);
  3173. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3174. $expected = array(
  3175. 'fieldset' => array(),
  3176. 'legend' => array(),
  3177. 'Field',
  3178. '/legend',
  3179. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3180. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3181. array('label' => array('for' => 'ModelField0')),
  3182. 'option A',
  3183. '/label',
  3184. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3185. array('label' => array('for' => 'ModelField1')),
  3186. 'option B',
  3187. '/label',
  3188. '/fieldset'
  3189. );
  3190. $this->assertTags($result, $expected);
  3191. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3192. $expected = array(
  3193. 'fieldset' => array(),
  3194. 'legend' => array(),
  3195. 'Field',
  3196. '/legend',
  3197. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3198. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3199. array('label' => array('for' => 'ModelField0')),
  3200. 'option A',
  3201. '/label',
  3202. 'br' => array(),
  3203. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3204. array('label' => array('for' => 'ModelField1')),
  3205. 'option B',
  3206. '/label',
  3207. '/fieldset'
  3208. );
  3209. $this->assertTags($result, $expected);
  3210. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3211. $expected = array(
  3212. 'fieldset' => array(),
  3213. 'legend' => array(),
  3214. 'Gender',
  3215. '/legend',
  3216. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3217. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3218. array('label' => array('for' => 'EmployeeGenderMale')),
  3219. 'Male',
  3220. '/label',
  3221. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3222. array('label' => array('for' => 'EmployeeGenderFemale')),
  3223. 'Female',
  3224. '/label',
  3225. '/fieldset',
  3226. );
  3227. $this->assertTags($result, $expected);
  3228. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3229. $expected = array(
  3230. 'fieldset' => array(),
  3231. 'legend' => array(),
  3232. 'Gender',
  3233. '/legend',
  3234. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3235. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3236. array('label' => array('for' => 'OfficerGenderMale')),
  3237. 'Male',
  3238. '/label',
  3239. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3240. array('label' => array('for' => 'OfficerGenderFemale')),
  3241. 'Female',
  3242. '/label',
  3243. '/fieldset',
  3244. );
  3245. $this->assertTags($result, $expected);
  3246. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3247. $expected = array(
  3248. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3249. array('input' => array(
  3250. 'type' => 'radio',
  3251. 'name' => 'data[Contact][1][imrequired]',
  3252. 'value' => '0',
  3253. 'id' => 'Contact1Imrequired0',
  3254. 'required' => 'required'
  3255. )),
  3256. 'label' => array('for' => 'Contact1Imrequired0'),
  3257. 'option A',
  3258. '/label'
  3259. );
  3260. $this->assertTags($result, $expected);
  3261. $result = $this->Form->radio('Model.1.field', array('option A'));
  3262. $expected = array(
  3263. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3264. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3265. 'label' => array('for' => 'Model1Field0'),
  3266. 'option A',
  3267. '/label'
  3268. );
  3269. $this->assertTags($result, $expected);
  3270. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3271. $expected = array(
  3272. 'fieldset' => array(),
  3273. 'legend' => array(),
  3274. 'Field',
  3275. '/legend',
  3276. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3277. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3278. array('label' => array('for' => 'ModelField0')),
  3279. 'option A',
  3280. '/label',
  3281. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3282. array('label' => array('for' => 'ModelField1')),
  3283. 'option B',
  3284. '/label',
  3285. '/fieldset'
  3286. );
  3287. $this->assertTags($result, $expected);
  3288. }
  3289. /**
  3290. * Test radio inputs with between as string or array. Also ensure
  3291. * that an array with less between elements works.
  3292. *
  3293. * @return void
  3294. */
  3295. public function testRadioBetween() {
  3296. $result = $this->Form->radio(
  3297. 'Model.field',
  3298. array('option A', 'option B'),
  3299. array('between' => 'I am between')
  3300. );
  3301. $expected = array(
  3302. 'fieldset' => array(),
  3303. 'legend' => array(),
  3304. 'Field',
  3305. '/legend',
  3306. 'I am between',
  3307. 'input' => array(
  3308. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3309. 'value' => '', 'id' => 'ModelField_'
  3310. ),
  3311. array('input' => array(
  3312. 'type' => 'radio', 'name' => 'data[Model][field]',
  3313. 'value' => '0', 'id' => 'ModelField0'
  3314. )),
  3315. array('label' => array('for' => 'ModelField0')),
  3316. 'option A',
  3317. '/label',
  3318. array('input' => array(
  3319. 'type' => 'radio', 'name' => 'data[Model][field]',
  3320. 'value' => '1', 'id' => 'ModelField1'
  3321. )),
  3322. array('label' => array('for' => 'ModelField1')),
  3323. 'option B',
  3324. '/label',
  3325. '/fieldset'
  3326. );
  3327. $this->assertTags($result, $expected);
  3328. $result = $this->Form->radio(
  3329. 'Model.field',
  3330. array('option A', 'option B', 'option C'),
  3331. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3332. );
  3333. $expected = array(
  3334. 'fieldset' => array(),
  3335. 'legend' => array(),
  3336. 'Field',
  3337. '/legend',
  3338. 'input' => array(
  3339. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3340. 'value' => '', 'id' => 'ModelField_'
  3341. ),
  3342. array('input' => array(
  3343. 'type' => 'radio', 'name' => 'data[Model][field]',
  3344. 'value' => '0', 'id' => 'ModelField0'
  3345. )),
  3346. array('label' => array('for' => 'ModelField0')),
  3347. 'option A',
  3348. '/label',
  3349. 'between A',
  3350. '--separator--',
  3351. array('input' => array(
  3352. 'type' => 'radio', 'name' => 'data[Model][field]',
  3353. 'value' => '1', 'id' => 'ModelField1'
  3354. )),
  3355. array('label' => array('for' => 'ModelField1')),
  3356. 'option B',
  3357. '/label',
  3358. 'between B',
  3359. '--separator--',
  3360. array('input' => array(
  3361. 'type' => 'radio', 'name' => 'data[Model][field]',
  3362. 'value' => '2', 'id' => 'ModelField2'
  3363. )),
  3364. array('label' => array('for' => 'ModelField2')),
  3365. 'option C',
  3366. '/label',
  3367. 'between C',
  3368. '/fieldset'
  3369. );
  3370. $this->assertTags($result, $expected);
  3371. $result = $this->Form->input('Model.field', array(
  3372. 'options' => array('1' => 'first', '2' => 'second'),
  3373. 'type' => 'radio',
  3374. 'before' => '--before--',
  3375. 'after' => '--after--',
  3376. 'separator' => '--separator--',
  3377. 'between' => array('--between first--', '--between second--')
  3378. ));
  3379. $expected = array(
  3380. 'div' => array('class' => 'input radio'),
  3381. '--before--',
  3382. 'fieldset' => array(),
  3383. 'legend' => array(),
  3384. 'Field',
  3385. '/legend',
  3386. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3387. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3388. array('label' => array('for' => 'ModelField1')),
  3389. 'first',
  3390. '/label',
  3391. '--between first--',
  3392. '--separator--',
  3393. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3394. array('label' => array('for' => 'ModelField2')),
  3395. 'second',
  3396. '/label',
  3397. '--between second--',
  3398. '/fieldset',
  3399. '--after--',
  3400. '/div'
  3401. );
  3402. $this->assertTags($result, $expected);
  3403. $result = $this->Form->input('Model.field', array(
  3404. 'options' => array('1' => 'first', '2' => 'second'),
  3405. 'type' => 'radio',
  3406. 'before' => '--before--',
  3407. 'after' => '--after--',
  3408. 'separator' => '--separator--',
  3409. 'between' => array('--between first--')
  3410. ));
  3411. $expected = array(
  3412. 'div' => array('class' => 'input radio'),
  3413. '--before--',
  3414. 'fieldset' => array(),
  3415. 'legend' => array(),
  3416. 'Field',
  3417. '/legend',
  3418. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3419. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3420. array('label' => array('for' => 'ModelField1')),
  3421. 'first',
  3422. '/label',
  3423. '--between first--',
  3424. '--separator--',
  3425. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3426. array('label' => array('for' => 'ModelField2')),
  3427. 'second',
  3428. '/label',
  3429. '/fieldset',
  3430. '--after--',
  3431. '/div'
  3432. );
  3433. $this->assertTags($result, $expected);
  3434. }
  3435. /**
  3436. * Test that radios with a 0 value are selected under the correct conditions.
  3437. * Also ensure that values that are booleanish are handled correctly.
  3438. *
  3439. * @return void
  3440. */
  3441. public function testRadioOptionWithBooleanishValues() {
  3442. $expected = array(
  3443. 'fieldset' => array(),
  3444. 'legend' => array(),
  3445. 'Field',
  3446. '/legend',
  3447. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3448. array('label' => array('for' => 'ModelField1')),
  3449. 'Yes',
  3450. '/label',
  3451. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3452. array('label' => array('for' => 'ModelField0')),
  3453. 'No',
  3454. '/label',
  3455. '/fieldset'
  3456. );
  3457. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3458. $this->assertTags($result, $expected);
  3459. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3460. $this->assertTags($result, $expected);
  3461. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3462. $this->assertTags($result, $expected);
  3463. $expected = array(
  3464. 'fieldset' => array(),
  3465. 'legend' => array(),
  3466. 'Field',
  3467. '/legend',
  3468. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3469. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3470. array('label' => array('for' => 'ModelField1')),
  3471. 'Yes',
  3472. '/label',
  3473. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3474. array('label' => array('for' => 'ModelField0')),
  3475. 'No',
  3476. '/label',
  3477. '/fieldset'
  3478. );
  3479. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3480. $this->assertTags($result, $expected);
  3481. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3482. $this->assertTags($result, $expected);
  3483. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3484. $this->assertTags($result, $expected);
  3485. $expected = array(
  3486. 'fieldset' => array(),
  3487. 'legend' => array(),
  3488. 'Field',
  3489. '/legend',
  3490. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3491. array('label' => array('for' => 'ModelField1')),
  3492. 'Yes',
  3493. '/label',
  3494. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3495. array('label' => array('for' => 'ModelField0')),
  3496. 'No',
  3497. '/label',
  3498. '/fieldset'
  3499. );
  3500. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3501. $this->assertTags($result, $expected);
  3502. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3503. $this->assertTags($result, $expected);
  3504. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3505. $this->assertTags($result, $expected);
  3506. }
  3507. /**
  3508. * test disabled radio options
  3509. *
  3510. * @return void
  3511. */
  3512. public function testRadioDisabled() {
  3513. $result = $this->Form->radio(
  3514. 'Model.field',
  3515. array('option A', 'option B'),
  3516. array('disabled' => array(0), 'value' => '0')
  3517. );
  3518. $expected = array(
  3519. 'fieldset' => array(),
  3520. 'legend' => array(),
  3521. 'Field',
  3522. '/legend',
  3523. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3524. array('label' => array('for' => 'ModelField0')),
  3525. 'option A',
  3526. '/label',
  3527. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3528. array('label' => array('for' => 'ModelField1')),
  3529. 'option B',
  3530. '/label',
  3531. '/fieldset'
  3532. );
  3533. $this->assertTags($result, $expected);
  3534. $result = $this->Form->radio(
  3535. 'Model.field',
  3536. array('option A', 'option B'),
  3537. array('disabled' => true, 'value' => '0')
  3538. );
  3539. $expected = array(
  3540. 'fieldset' => array(),
  3541. 'legend' => array(),
  3542. 'Field',
  3543. '/legend',
  3544. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3545. array('label' => array('for' => 'ModelField0')),
  3546. 'option A',
  3547. '/label',
  3548. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3549. array('label' => array('for' => 'ModelField1')),
  3550. 'option B',
  3551. '/label',
  3552. '/fieldset'
  3553. );
  3554. $this->assertTags($result, $expected);
  3555. $result = $this->Form->radio(
  3556. 'Model.field',
  3557. array('option A', 'option B'),
  3558. array('disabled' => 'disabled', 'value' => '0')
  3559. );
  3560. $expected = array(
  3561. 'fieldset' => array(),
  3562. 'legend' => array(),
  3563. 'Field',
  3564. '/legend',
  3565. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3566. array('label' => array('for' => 'ModelField0')),
  3567. 'option A',
  3568. '/label',
  3569. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3570. array('label' => array('for' => 'ModelField1')),
  3571. 'option B',
  3572. '/label',
  3573. '/fieldset'
  3574. );
  3575. $this->assertTags($result, $expected);
  3576. $result = $this->Form->input('Model.field', array(
  3577. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3578. 'type' => 'radio',
  3579. 'disabled' => array(2, '3x'),
  3580. ));
  3581. $expected = array(
  3582. 'div' => array('class' => 'input radio'),
  3583. 'fieldset' => array(),
  3584. 'legend' => array(),
  3585. 'Field',
  3586. '/legend',
  3587. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3588. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3589. array('label' => array('for' => 'ModelField1')),
  3590. 'first',
  3591. '/label',
  3592. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3593. array('label' => array('for' => 'ModelField2')),
  3594. 'second',
  3595. '/label',
  3596. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3597. array('label' => array('for' => 'ModelField2x')),
  3598. '2x',
  3599. '/label',
  3600. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3601. array('label' => array('for' => 'ModelField3')),
  3602. 'third',
  3603. '/label',
  3604. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3605. array('label' => array('for' => 'ModelField3x')),
  3606. '3x',
  3607. '/label',
  3608. '/fieldset',
  3609. '/div'
  3610. );
  3611. $this->assertTags($result, $expected);
  3612. }
  3613. /**
  3614. * test disabling the hidden input for radio buttons
  3615. *
  3616. * @return void
  3617. */
  3618. public function testRadioHiddenInputDisabling() {
  3619. $result = $this->Form->input('Model.1.field', array(
  3620. 'type' => 'radio',
  3621. 'options' => array('option A'),
  3622. 'hiddenField' => false
  3623. )
  3624. );
  3625. $expected = array(
  3626. 'div' => array('class' => 'input radio'),
  3627. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3628. 'label' => array('for' => 'Model1Field0'),
  3629. 'option A',
  3630. '/label',
  3631. '/div'
  3632. );
  3633. $this->assertTags($result, $expected);
  3634. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3635. $expected = array(
  3636. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3637. 'label' => array('for' => 'Model1Field0'),
  3638. 'option A',
  3639. '/label'
  3640. );
  3641. $this->assertTags($result, $expected);
  3642. }
  3643. /**
  3644. * test adding an empty option for radio buttons
  3645. *
  3646. * @return void
  3647. */
  3648. public function testRadioAddEmptyOption() {
  3649. $result = $this->Form->input('Model.1.field', array(
  3650. 'type' => 'radio',
  3651. 'options' => array('option A'),
  3652. 'empty' => true,
  3653. 'hiddenField' => false
  3654. ));
  3655. $expected = array(
  3656. 'div' => array('class' => 'input radio'),
  3657. 'fieldset' => array(),
  3658. 'legend' => array(),
  3659. 'Field',
  3660. '/legend',
  3661. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3662. array('label' => array('for' => 'Model1Field')),
  3663. __('empty'),
  3664. '/label',
  3665. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3666. array('label' => array('for' => 'Model1Field0')),
  3667. 'option A',
  3668. '/label',
  3669. '/fieldset',
  3670. '/div'
  3671. );
  3672. $this->assertTags($result, $expected);
  3673. $result = $this->Form->input('Model.1.field', array(
  3674. 'type' => 'radio',
  3675. 'options' => array('option A'),
  3676. 'empty' => 'CustomEmptyLabel',
  3677. 'hiddenField' => false
  3678. ));
  3679. $expected = array(
  3680. 'div' => array('class' => 'input radio'),
  3681. 'fieldset' => array(),
  3682. 'legend' => array(),
  3683. 'Field',
  3684. '/legend',
  3685. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3686. array('label' => array('for' => 'Model1Field')),
  3687. 'CustomEmptyLabel',
  3688. '/label',
  3689. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3690. array('label' => array('for' => 'Model1Field0')),
  3691. 'option A',
  3692. '/label',
  3693. '/fieldset',
  3694. '/div'
  3695. );
  3696. $this->assertTags($result, $expected);
  3697. $result = $this->Form->input('Model.1.field', array(
  3698. 'type' => 'radio',
  3699. 'options' => array('option A'),
  3700. 'empty' => false,
  3701. 'hiddenField' => false
  3702. ));
  3703. $this->assertTextNotContains('"Model1Field"', $result);
  3704. }
  3705. /**
  3706. * testSelect method
  3707. *
  3708. * Test select element generation.
  3709. *
  3710. * @return void
  3711. */
  3712. public function testSelect() {
  3713. $result = $this->Form->select('Model.field', array());
  3714. $expected = array(
  3715. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3716. array('option' => array('value' => '')),
  3717. '/option',
  3718. '/select'
  3719. );
  3720. $this->assertTags($result, $expected);
  3721. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3722. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3723. $expected = array(
  3724. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3725. array('option' => array('value' => '')),
  3726. '/option',
  3727. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3728. 'good',
  3729. '/option',
  3730. array('option' => array('value' => 'other')),
  3731. 'bad',
  3732. '/option',
  3733. '/select'
  3734. );
  3735. $this->assertTags($result, $expected);
  3736. $this->Form->request->data = array();
  3737. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3738. $expected = array(
  3739. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3740. array('option' => array('value' => '')),
  3741. '/option',
  3742. array('option' => array('value' => 'value')),
  3743. 'good',
  3744. '/option',
  3745. array('option' => array('value' => 'other')),
  3746. 'bad',
  3747. '/option',
  3748. '/select'
  3749. );
  3750. $this->assertTags($result, $expected);
  3751. $result = $this->Form->select(
  3752. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3753. array('empty' => false)
  3754. );
  3755. $expected = array(
  3756. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3757. array('option' => array('value' => 'first')),
  3758. 'first &quot;html&quot; &lt;chars&gt;',
  3759. '/option',
  3760. array('option' => array('value' => 'second')),
  3761. 'value',
  3762. '/option',
  3763. '/select'
  3764. );
  3765. $this->assertTags($result, $expected);
  3766. $result = $this->Form->select(
  3767. 'Model.field',
  3768. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3769. array('escape' => false, 'empty' => false)
  3770. );
  3771. $expected = array(
  3772. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3773. array('option' => array('value' => 'first')),
  3774. 'first "html" <chars>',
  3775. '/option',
  3776. array('option' => array('value' => 'second')),
  3777. 'value',
  3778. '/option',
  3779. '/select'
  3780. );
  3781. $this->assertTags($result, $expected);
  3782. $options = array(
  3783. array('value' => 'first', 'name' => 'First'),
  3784. array('value' => 'first', 'name' => 'Another First'),
  3785. );
  3786. $result = $this->Form->select(
  3787. 'Model.field',
  3788. $options,
  3789. array('escape' => false, 'empty' => false)
  3790. );
  3791. $expected = array(
  3792. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3793. array('option' => array('value' => 'first')),
  3794. 'First',
  3795. '/option',
  3796. array('option' => array('value' => 'first')),
  3797. 'Another First',
  3798. '/option',
  3799. '/select'
  3800. );
  3801. $this->assertTags($result, $expected);
  3802. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3803. $result = $this->Form->select(
  3804. 'Model.contact_id',
  3805. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3806. array('escape' => false, 'empty' => 'pick something')
  3807. );
  3808. $expected = array(
  3809. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3810. array('option' => array('value' => '')), 'pick something', '/option',
  3811. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3812. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3813. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3814. '/select'
  3815. );
  3816. $this->assertTags($result, $expected);
  3817. $this->Form->request->data['Model']['field'] = 0;
  3818. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3819. $expected = array(
  3820. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3821. array('option' => array('value' => '')), '/option',
  3822. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3823. array('option' => array('value' => '1')), 'Yes', '/option',
  3824. '/select'
  3825. );
  3826. $this->assertTags($result, $expected);
  3827. $this->Form->request->data['Model']['field'] = 50;
  3828. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3829. $expected = array(
  3830. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3831. array('option' => array('value' => '')), '/option',
  3832. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3833. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3834. '/select'
  3835. );
  3836. $this->assertTags($result, $expected);
  3837. $result = $this->Form->select('Contact.required_one', array('option A'));
  3838. $expected = array(
  3839. 'select' => array(
  3840. 'name' => 'data[Contact][required_one]',
  3841. 'id' => 'ContactRequiredOne',
  3842. 'required' => 'required'
  3843. ),
  3844. array('option' => array('value' => '')), '/option',
  3845. array('option' => array('value' => '0')), 'option A', '/option',
  3846. '/select'
  3847. );
  3848. $this->assertTags($result, $expected);
  3849. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  3850. $expected = array(
  3851. 'select' => array(
  3852. 'name' => 'data[Contact][required_one]',
  3853. 'id' => 'ContactRequiredOne',
  3854. 'disabled' => 'disabled'
  3855. ),
  3856. array('option' => array('value' => '')), '/option',
  3857. array('option' => array('value' => '0')), 'option A', '/option',
  3858. '/select'
  3859. );
  3860. $this->assertTags($result, $expected);
  3861. }
  3862. /**
  3863. * test that select() with optiongroups listens to the escape param.
  3864. *
  3865. * @return void
  3866. */
  3867. public function testSelectOptionGroupEscaping() {
  3868. $options = array(
  3869. '>< Key' => array(
  3870. 1 => 'One',
  3871. 2 => 'Two'
  3872. )
  3873. );
  3874. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3875. $expected = array(
  3876. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3877. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3878. array('option' => array('value' => '1')), 'One', '/option',
  3879. array('option' => array('value' => '2')), 'Two', '/option',
  3880. '/optgroup',
  3881. '/select'
  3882. );
  3883. $this->assertTags($result, $expected);
  3884. $options = array(
  3885. '>< Key' => array(
  3886. 1 => 'One',
  3887. 2 => 'Two'
  3888. )
  3889. );
  3890. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3891. $expected = array(
  3892. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3893. 'optgroup' => array('label' => '>< Key'),
  3894. array('option' => array('value' => '1')), 'One', '/option',
  3895. array('option' => array('value' => '2')), 'Two', '/option',
  3896. '/optgroup',
  3897. '/select'
  3898. );
  3899. $this->assertTags($result, $expected);
  3900. }
  3901. /**
  3902. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3903. *
  3904. * @return void
  3905. */
  3906. public function testSelectWithNullAttributes() {
  3907. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3908. $expected = array(
  3909. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3910. array('option' => array('value' => '0')),
  3911. 'first',
  3912. '/option',
  3913. array('option' => array('value' => '1')),
  3914. 'second',
  3915. '/option',
  3916. '/select'
  3917. );
  3918. $this->assertTags($result, $expected);
  3919. }
  3920. /**
  3921. * testNestedSelect method
  3922. *
  3923. * test select element generation with optgroups
  3924. *
  3925. * @return void
  3926. */
  3927. public function testNestedSelect() {
  3928. $result = $this->Form->select(
  3929. 'Model.field',
  3930. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3931. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3932. )), array('empty' => false)
  3933. );
  3934. $expected = array(
  3935. 'select' => array('name' => 'data[Model][field]',
  3936. 'id' => 'ModelField'),
  3937. array('option' => array('value' => 1)),
  3938. 'One',
  3939. '/option',
  3940. array('option' => array('value' => 2)),
  3941. 'Two',
  3942. '/option',
  3943. array('optgroup' => array('label' => 'Three')),
  3944. array('option' => array('value' => 4)),
  3945. 'Four',
  3946. '/option',
  3947. array('option' => array('value' => 5)),
  3948. 'Five',
  3949. '/option',
  3950. '/optgroup',
  3951. '/select'
  3952. );
  3953. $this->assertTags($result, $expected);
  3954. $result = $this->Form->select(
  3955. 'Model.field',
  3956. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3957. array('showParents' => true, 'empty' => false)
  3958. );
  3959. $expected = array(
  3960. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3961. array('option' => array('value' => 1)),
  3962. 'One',
  3963. '/option',
  3964. array('option' => array('value' => 2)),
  3965. 'Two',
  3966. '/option',
  3967. array('optgroup' => array('label' => 'Three')),
  3968. array('option' => array('value' => 3)),
  3969. 'Three',
  3970. '/option',
  3971. array('option' => array('value' => 4)),
  3972. 'Four',
  3973. '/option',
  3974. '/optgroup',
  3975. '/select'
  3976. );
  3977. $this->assertTags($result, $expected);
  3978. }
  3979. /**
  3980. * testSelectMultiple method
  3981. *
  3982. * test generation of multiple select elements
  3983. *
  3984. * @return void
  3985. */
  3986. public function testSelectMultiple() {
  3987. $options = array('first', 'second', 'third');
  3988. $result = $this->Form->select(
  3989. 'Model.multi_field', $options, array('multiple' => true)
  3990. );
  3991. $expected = array(
  3992. 'input' => array(
  3993. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3994. ),
  3995. 'select' => array(
  3996. 'name' => 'data[Model][multi_field][]',
  3997. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3998. ),
  3999. array('option' => array('value' => '0')),
  4000. 'first',
  4001. '/option',
  4002. array('option' => array('value' => '1')),
  4003. 'second',
  4004. '/option',
  4005. array('option' => array('value' => '2')),
  4006. 'third',
  4007. '/option',
  4008. '/select'
  4009. );
  4010. $this->assertTags($result, $expected);
  4011. $result = $this->Form->select(
  4012. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4013. );
  4014. $expected = array(
  4015. 'input' => array(
  4016. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4017. ),
  4018. 'select' => array(
  4019. 'name' => 'data[Model][multi_field][]',
  4020. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4021. ),
  4022. array('option' => array('value' => '0')),
  4023. 'first',
  4024. '/option',
  4025. array('option' => array('value' => '1')),
  4026. 'second',
  4027. '/option',
  4028. array('option' => array('value' => '2')),
  4029. 'third',
  4030. '/option',
  4031. '/select'
  4032. );
  4033. $this->assertTags($result, $expected);
  4034. $result = $this->Form->select(
  4035. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4036. );
  4037. $expected = array(
  4038. 'input' => array(
  4039. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4040. ),
  4041. 'select' => array(
  4042. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4043. 'multiple' => 'multiple'
  4044. ),
  4045. array('option' => array('value' => '0', 'selected' => 'selected')),
  4046. 'first',
  4047. '/option',
  4048. array('option' => array('value' => '1', 'selected' => 'selected')),
  4049. 'second',
  4050. '/option',
  4051. array('option' => array('value' => '2')),
  4052. 'third',
  4053. '/option',
  4054. '/select'
  4055. );
  4056. $this->assertTags($result, $expected);
  4057. $result = $this->Form->select(
  4058. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4059. );
  4060. $expected = array(
  4061. 'select' => array(
  4062. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4063. ),
  4064. array('option' => array('value' => '0', 'selected' => 'selected')),
  4065. 'first',
  4066. '/option',
  4067. array('option' => array('value' => '1', 'selected' => 'selected')),
  4068. 'second',
  4069. '/option',
  4070. array('option' => array('value' => '2')),
  4071. 'third',
  4072. '/option',
  4073. '/select'
  4074. );
  4075. $this->assertTags($result, $expected);
  4076. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4077. $selected = array('2', '3x');
  4078. $result = $this->Form->select(
  4079. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4080. );
  4081. $expected = array(
  4082. 'input' => array(
  4083. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4084. ),
  4085. 'select' => array(
  4086. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4087. ),
  4088. array('option' => array('value' => '1')),
  4089. 'One',
  4090. '/option',
  4091. array('option' => array('value' => '2', 'selected' => 'selected')),
  4092. 'Two',
  4093. '/option',
  4094. array('option' => array('value' => '3')),
  4095. 'Three',
  4096. '/option',
  4097. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4098. 'Stringy',
  4099. '/option',
  4100. '/select'
  4101. );
  4102. $this->assertTags($result, $expected);
  4103. $result = $this->Form->select('Contact.required_one', array(
  4104. '1' => 'option A',
  4105. '2' => 'option B'
  4106. ), array('multiple' => true));
  4107. $expected = array(
  4108. 'input' => array(
  4109. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4110. ),
  4111. 'select' => array(
  4112. 'name' => 'data[Contact][required_one][]',
  4113. 'id' => 'ContactRequiredOne',
  4114. 'required' => 'required',
  4115. 'multiple' => 'multiple'
  4116. ),
  4117. array('option' => array('value' => '1')), 'option A', '/option',
  4118. array('option' => array('value' => '2')), 'option B', '/option',
  4119. '/select'
  4120. );
  4121. $this->assertTags($result, $expected);
  4122. }
  4123. /**
  4124. * Test generating multiple select with disabled elements.
  4125. *
  4126. * @return void
  4127. */
  4128. public function testSelectMultipleWithDisabledElements() {
  4129. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4130. $disabled = array(2, 3);
  4131. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4132. $expected = array(
  4133. 'input' => array(
  4134. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4135. ),
  4136. 'select' => array(
  4137. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4138. ),
  4139. array('option' => array('value' => '1')),
  4140. 'One',
  4141. '/option',
  4142. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4143. 'Two',
  4144. '/option',
  4145. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4146. 'Three',
  4147. '/option',
  4148. array('option' => array('value' => '3x')),
  4149. 'Stringy',
  4150. '/option',
  4151. '/select'
  4152. );
  4153. $this->assertTags($result, $expected);
  4154. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4155. $disabled = array('2', '3x');
  4156. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4157. $expected = array(
  4158. array('div' => array('class' => 'input select')),
  4159. array('label' => array('for' => 'ContactMultiple')),
  4160. 'Multiple',
  4161. '/label',
  4162. 'input' => array(
  4163. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4164. ),
  4165. 'select' => array(
  4166. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4167. ),
  4168. array('option' => array('value' => '1')),
  4169. 'One',
  4170. '/option',
  4171. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4172. 'Two',
  4173. '/option',
  4174. array('option' => array('value' => '3')),
  4175. 'Three',
  4176. '/option',
  4177. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4178. 'Stringy',
  4179. '/option',
  4180. '/select',
  4181. '/div'
  4182. );
  4183. $this->assertTags($result, $expected);
  4184. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4185. $disabled = true;
  4186. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4187. $expected = array(
  4188. array('div' => array('class' => 'input select')),
  4189. array('label' => array('for' => 'ContactMultiple')),
  4190. 'Multiple',
  4191. '/label',
  4192. 'input' => array(
  4193. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4194. ),
  4195. 'select' => array(
  4196. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4197. ),
  4198. array('option' => array('value' => '1')),
  4199. 'One',
  4200. '/option',
  4201. array('option' => array('value' => '2')),
  4202. 'Two',
  4203. '/option',
  4204. array('option' => array('value' => '3')),
  4205. 'Three',
  4206. '/option',
  4207. array('option' => array('value' => '3x')),
  4208. 'Stringy',
  4209. '/option',
  4210. '/select',
  4211. '/div'
  4212. );
  4213. $this->assertTags($result, $expected);
  4214. }
  4215. /**
  4216. * Test generating select with disabled elements.
  4217. *
  4218. * @return void
  4219. */
  4220. public function testSelectWithDisabledElements() {
  4221. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4222. $disabled = array(2, 3);
  4223. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4224. $expected = array(
  4225. 'select' => array(
  4226. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4227. ),
  4228. array('option' => array('value' => '')),
  4229. '/option',
  4230. array('option' => array('value' => '1')),
  4231. 'One',
  4232. '/option',
  4233. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4234. 'Two',
  4235. '/option',
  4236. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4237. 'Three',
  4238. '/option',
  4239. array('option' => array('value' => '3x')),
  4240. 'Stringy',
  4241. '/option',
  4242. '/select'
  4243. );
  4244. $this->assertTags($result, $expected);
  4245. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4246. $disabled = array('2', '3x');
  4247. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4248. $expected = array(
  4249. array('div' => array('class' => 'input select')),
  4250. array('label' => array('for' => 'ModelField')),
  4251. 'Field',
  4252. '/label',
  4253. 'select' => array(
  4254. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4255. ),
  4256. array('option' => array('value' => '1')),
  4257. 'One',
  4258. '/option',
  4259. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4260. 'Two',
  4261. '/option',
  4262. array('option' => array('value' => '3')),
  4263. 'Three',
  4264. '/option',
  4265. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4266. 'Stringy',
  4267. '/option',
  4268. '/select',
  4269. '/div'
  4270. );
  4271. $this->assertTags($result, $expected);
  4272. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4273. $disabled = true;
  4274. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4275. $expected = array(
  4276. array('div' => array('class' => 'input select')),
  4277. array('label' => array('for' => 'ModelField')),
  4278. 'Field',
  4279. '/label',
  4280. 'select' => array(
  4281. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4282. ),
  4283. array('option' => array('value' => '1')),
  4284. 'One',
  4285. '/option',
  4286. array('option' => array('value' => '2')),
  4287. 'Two',
  4288. '/option',
  4289. array('option' => array('value' => '3')),
  4290. 'Three',
  4291. '/option',
  4292. array('option' => array('value' => '3x')),
  4293. 'Stringy',
  4294. '/option',
  4295. '/select',
  4296. '/div'
  4297. );
  4298. $this->assertTags($result, $expected);
  4299. }
  4300. /**
  4301. * test generation of habtm select boxes.
  4302. *
  4303. * @return void
  4304. */
  4305. public function testHabtmSelectBox() {
  4306. $this->View->viewVars['contactTags'] = array(
  4307. 1 => 'blue',
  4308. 2 => 'red',
  4309. 3 => 'green'
  4310. );
  4311. $this->Form->request->data = array(
  4312. 'Contact' => array(),
  4313. 'ContactTag' => array(
  4314. array(
  4315. 'id' => '1',
  4316. 'name' => 'blue'
  4317. ),
  4318. array(
  4319. 'id' => 3,
  4320. 'name' => 'green'
  4321. )
  4322. )
  4323. );
  4324. $this->Form->create('Contact');
  4325. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4326. $expected = array(
  4327. 'input' => array(
  4328. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4329. ),
  4330. 'select' => array(
  4331. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4332. 'multiple' => 'multiple'
  4333. ),
  4334. array('option' => array('value' => '1', 'selected' => 'selected')),
  4335. 'blue',
  4336. '/option',
  4337. array('option' => array('value' => '2')),
  4338. 'red',
  4339. '/option',
  4340. array('option' => array('value' => '3', 'selected' => 'selected')),
  4341. 'green',
  4342. '/option',
  4343. '/select'
  4344. );
  4345. $this->assertTags($result, $expected);
  4346. // make sure only 50 is selected, and not 50f5c0cf
  4347. $this->View->viewVars['contactTags'] = array(
  4348. '1' => 'blue',
  4349. '50f5c0cf' => 'red',
  4350. '50' => 'green'
  4351. );
  4352. $this->Form->request->data = array(
  4353. 'Contact' => array(),
  4354. 'ContactTag' => array(
  4355. array(
  4356. 'id' => 1,
  4357. 'name' => 'blue'
  4358. ),
  4359. array(
  4360. 'id' => 50,
  4361. 'name' => 'green'
  4362. )
  4363. )
  4364. );
  4365. $this->Form->create('Contact');
  4366. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4367. $expected = array(
  4368. 'input' => array(
  4369. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4370. ),
  4371. 'select' => array(
  4372. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4373. 'multiple' => 'multiple'
  4374. ),
  4375. array('option' => array('value' => '1', 'selected' => 'selected')),
  4376. 'blue',
  4377. '/option',
  4378. array('option' => array('value' => '50f5c0cf')),
  4379. 'red',
  4380. '/option',
  4381. array('option' => array('value' => '50', 'selected' => 'selected')),
  4382. 'green',
  4383. '/option',
  4384. '/select'
  4385. );
  4386. $this->assertTags($result, $expected);
  4387. }
  4388. /**
  4389. * test generation of multi select elements in checkbox format
  4390. *
  4391. * @return void
  4392. */
  4393. public function testSelectMultipleCheckboxes() {
  4394. $result = $this->Form->select(
  4395. 'Model.multi_field',
  4396. array('first', 'second', 'third'),
  4397. array('multiple' => 'checkbox')
  4398. );
  4399. $expected = array(
  4400. 'input' => array(
  4401. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4402. ),
  4403. array('div' => array('class' => 'checkbox')),
  4404. array('input' => array(
  4405. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4406. 'value' => '0', 'id' => 'ModelMultiField0'
  4407. )),
  4408. array('label' => array('for' => 'ModelMultiField0')),
  4409. 'first',
  4410. '/label',
  4411. '/div',
  4412. array('div' => array('class' => 'checkbox')),
  4413. array('input' => array(
  4414. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4415. 'value' => '1', 'id' => 'ModelMultiField1'
  4416. )),
  4417. array('label' => array('for' => 'ModelMultiField1')),
  4418. 'second',
  4419. '/label',
  4420. '/div',
  4421. array('div' => array('class' => 'checkbox')),
  4422. array('input' => array(
  4423. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4424. 'value' => '2', 'id' => 'ModelMultiField2'
  4425. )),
  4426. array('label' => array('for' => 'ModelMultiField2')),
  4427. 'third',
  4428. '/label',
  4429. '/div'
  4430. );
  4431. $this->assertTags($result, $expected);
  4432. $result = $this->Form->select(
  4433. 'Model.multi_field',
  4434. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4435. array('multiple' => 'checkbox')
  4436. );
  4437. $expected = array(
  4438. 'input' => array(
  4439. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4440. ),
  4441. array('div' => array('class' => 'checkbox')),
  4442. array('input' => array(
  4443. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4444. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4445. )),
  4446. array('label' => array('for' => 'ModelMultiFieldA')),
  4447. 'first',
  4448. '/label',
  4449. '/div',
  4450. array('div' => array('class' => 'checkbox')),
  4451. array('input' => array(
  4452. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4453. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4454. )),
  4455. array('label' => array('for' => 'ModelMultiFieldB')),
  4456. 'second',
  4457. '/label',
  4458. '/div',
  4459. array('div' => array('class' => 'checkbox')),
  4460. array('input' => array(
  4461. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4462. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4463. )),
  4464. array('label' => array('for' => 'ModelMultiFieldC')),
  4465. 'third',
  4466. '/label',
  4467. '/div'
  4468. );
  4469. $this->assertTags($result, $expected);
  4470. $result = $this->Form->select(
  4471. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4472. );
  4473. $expected = array(
  4474. 'input' => array(
  4475. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4476. ),
  4477. array('div' => array('class' => 'checkbox')),
  4478. array('input' => array(
  4479. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4480. 'value' => '1', 'id' => 'ModelMultiField1'
  4481. )),
  4482. array('label' => array('for' => 'ModelMultiField1')),
  4483. 'first',
  4484. '/label',
  4485. '/div'
  4486. );
  4487. $this->assertTags($result, $expected);
  4488. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4489. $result = $this->Form->select(
  4490. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4491. );
  4492. $expected = array(
  4493. 'input' => array(
  4494. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4495. ),
  4496. array('div' => array('class' => 'checkbox')),
  4497. array('input' => array(
  4498. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4499. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4500. )),
  4501. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4502. 'first',
  4503. '/label',
  4504. '/div',
  4505. array('div' => array('class' => 'checkbox')),
  4506. array('input' => array(
  4507. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4508. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4509. )),
  4510. array('label' => array('for' => 'ModelTagsArray')),
  4511. 'Array',
  4512. '/label',
  4513. '/div'
  4514. );
  4515. $this->assertTags($result, $expected);
  4516. }
  4517. /**
  4518. * test multiple checkboxes with div styles.
  4519. *
  4520. * @return void
  4521. */
  4522. public function testSelectMultipleCheckboxDiv() {
  4523. $result = $this->Form->select(
  4524. 'Model.tags',
  4525. array('first', 'second'),
  4526. array('multiple' => 'checkbox', 'class' => 'my-class')
  4527. );
  4528. $expected = array(
  4529. 'input' => array(
  4530. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4531. ),
  4532. array('div' => array('class' => 'my-class')),
  4533. array('input' => array(
  4534. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4535. 'value' => '0', 'id' => 'ModelTags0'
  4536. )),
  4537. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4538. '/div',
  4539. array('div' => array('class' => 'my-class')),
  4540. array('input' => array(
  4541. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4542. 'value' => '1', 'id' => 'ModelTags1'
  4543. )),
  4544. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4545. '/div'
  4546. );
  4547. $this->assertTags($result, $expected);
  4548. $result = $this->Form->input('Model.tags', array(
  4549. 'options' => array('first', 'second'),
  4550. 'multiple' => 'checkbox',
  4551. 'class' => 'my-class',
  4552. 'div' => false,
  4553. 'label' => false
  4554. ));
  4555. $this->assertTags($result, $expected);
  4556. $Contact = ClassRegistry::getObject('Contact');
  4557. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4558. $result = $this->Form->input('Contact.tags', array(
  4559. 'options' => array('one'),
  4560. 'multiple' => 'checkbox',
  4561. 'label' => false,
  4562. 'div' => false
  4563. ));
  4564. $expected = array(
  4565. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4566. array('div' => array('class' => 'checkbox form-error')),
  4567. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4568. array('label' => array('for' => 'ContactTags0')),
  4569. 'one',
  4570. '/label',
  4571. '/div'
  4572. );
  4573. $this->assertTags($result, $expected);
  4574. $result = $this->Form->input('Contact.tags', array(
  4575. 'options' => array('one'),
  4576. 'multiple' => 'checkbox',
  4577. 'class' => 'mycheckbox',
  4578. 'label' => false,
  4579. 'div' => false
  4580. ));
  4581. $expected = array(
  4582. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4583. array('div' => array('class' => 'mycheckbox form-error')),
  4584. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4585. array('label' => array('for' => 'ContactTags0')),
  4586. 'one',
  4587. '/label',
  4588. '/div'
  4589. );
  4590. $this->assertTags($result, $expected);
  4591. }
  4592. /**
  4593. * Checks the security hash array generated for multiple-input checkbox elements
  4594. *
  4595. * @return void
  4596. */
  4597. public function testSelectMultipleCheckboxSecurity() {
  4598. $this->Form->request['_Token'] = array('key' => 'testKey');
  4599. $this->assertEquals(array(), $this->Form->fields);
  4600. $result = $this->Form->select(
  4601. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4602. array('multiple' => 'checkbox')
  4603. );
  4604. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4605. $result = $this->Form->secure($this->Form->fields);
  4606. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4607. $this->assertRegExp('/"' . $key . '"/', $result);
  4608. }
  4609. /**
  4610. * Multiple select elements should always be secured as they always participate
  4611. * in the POST data.
  4612. *
  4613. * @return void
  4614. */
  4615. public function testSelectMultipleSecureWithNoOptions() {
  4616. $this->Form->request['_Token'] = array('key' => 'testkey');
  4617. $this->assertEquals(array(), $this->Form->fields);
  4618. $this->Form->select(
  4619. 'Model.select',
  4620. array(),
  4621. array('multiple' => true)
  4622. );
  4623. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4624. }
  4625. /**
  4626. * When a select box has no options it should not be added to the fields list
  4627. * as it always fail post validation.
  4628. *
  4629. * @return void
  4630. */
  4631. public function testSelectNoSecureWithNoOptions() {
  4632. $this->Form->request['_Token'] = array('key' => 'testkey');
  4633. $this->assertEquals(array(), $this->Form->fields);
  4634. $this->Form->select(
  4635. 'Model.select',
  4636. array()
  4637. );
  4638. $this->assertEquals(array(), $this->Form->fields);
  4639. $this->Form->select(
  4640. 'Model.select',
  4641. array(),
  4642. array('empty' => true)
  4643. );
  4644. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4645. }
  4646. /**
  4647. * testInputMultipleCheckboxes method
  4648. *
  4649. * test input() resulting in multi select elements being generated.
  4650. *
  4651. * @return void
  4652. */
  4653. public function testInputMultipleCheckboxes() {
  4654. $result = $this->Form->input('Model.multi_field', array(
  4655. 'options' => array('first', 'second', 'third'),
  4656. 'multiple' => 'checkbox'
  4657. ));
  4658. $expected = array(
  4659. array('div' => array('class' => 'input select')),
  4660. array('label' => array('for' => 'ModelMultiField')),
  4661. 'Multi Field',
  4662. '/label',
  4663. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4664. array('div' => array('class' => 'checkbox')),
  4665. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4666. array('label' => array('for' => 'ModelMultiField0')),
  4667. 'first',
  4668. '/label',
  4669. '/div',
  4670. array('div' => array('class' => 'checkbox')),
  4671. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4672. array('label' => array('for' => 'ModelMultiField1')),
  4673. 'second',
  4674. '/label',
  4675. '/div',
  4676. array('div' => array('class' => 'checkbox')),
  4677. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4678. array('label' => array('for' => 'ModelMultiField2')),
  4679. 'third',
  4680. '/label',
  4681. '/div',
  4682. '/div'
  4683. );
  4684. $this->assertTags($result, $expected);
  4685. $result = $this->Form->input('Model.multi_field', array(
  4686. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4687. 'multiple' => 'checkbox'
  4688. ));
  4689. $expected = array(
  4690. array('div' => array('class' => 'input select')),
  4691. array('label' => array('for' => 'ModelMultiField')),
  4692. 'Multi Field',
  4693. '/label',
  4694. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4695. array('div' => array('class' => 'checkbox')),
  4696. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4697. array('label' => array('for' => 'ModelMultiFieldA')),
  4698. 'first',
  4699. '/label',
  4700. '/div',
  4701. array('div' => array('class' => 'checkbox')),
  4702. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4703. array('label' => array('for' => 'ModelMultiFieldB')),
  4704. 'second',
  4705. '/label',
  4706. '/div',
  4707. array('div' => array('class' => 'checkbox')),
  4708. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4709. array('label' => array('for' => 'ModelMultiFieldC')),
  4710. 'third',
  4711. '/label',
  4712. '/div',
  4713. '/div'
  4714. );
  4715. $this->assertTags($result, $expected);
  4716. $result = $this->Form->input('Model.multi_field', array(
  4717. 'options' => array('1' => 'first'),
  4718. 'multiple' => 'checkbox',
  4719. 'label' => false,
  4720. 'div' => false
  4721. ));
  4722. $expected = array(
  4723. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4724. array('div' => array('class' => 'checkbox')),
  4725. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4726. array('label' => array('for' => 'ModelMultiField1')),
  4727. 'first',
  4728. '/label',
  4729. '/div'
  4730. );
  4731. $this->assertTags($result, $expected);
  4732. $result = $this->Form->input('Model.multi_field', array(
  4733. 'options' => array('2' => 'second'),
  4734. 'multiple' => 'checkbox',
  4735. 'label' => false,
  4736. 'div' => false
  4737. ));
  4738. $expected = array(
  4739. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4740. array('div' => array('class' => 'checkbox')),
  4741. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4742. array('label' => array('for' => 'ModelMultiField2')),
  4743. 'second',
  4744. '/label',
  4745. '/div'
  4746. );
  4747. $this->assertTags($result, $expected);
  4748. }
  4749. /**
  4750. * testSelectHiddenFieldOmission method
  4751. *
  4752. * test that select() with 'hiddenField' => false omits the hidden field
  4753. *
  4754. * @return void
  4755. */
  4756. public function testSelectHiddenFieldOmission() {
  4757. $result = $this->Form->select('Model.multi_field',
  4758. array('first', 'second'),
  4759. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4760. );
  4761. $expected = array(
  4762. array('div' => array('class' => 'checkbox')),
  4763. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4764. array('label' => array('for' => 'ModelMultiField0')),
  4765. 'first',
  4766. '/label',
  4767. '/div',
  4768. array('div' => array('class' => 'checkbox')),
  4769. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4770. array('label' => array('for' => 'ModelMultiField1')),
  4771. 'second',
  4772. '/label',
  4773. '/div'
  4774. );
  4775. $this->assertTags($result, $expected);
  4776. $result = $this->Form->input('Model.multi_field', array(
  4777. 'options' => array('first', 'second'),
  4778. 'multiple' => 'checkbox',
  4779. 'hiddenField' => false
  4780. ));
  4781. $expected = array(
  4782. array('div' => array('class' => 'input select')),
  4783. array('label' => array('for' => 'ModelMultiField')),
  4784. 'Multi Field',
  4785. '/label',
  4786. array('div' => array('class' => 'checkbox')),
  4787. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4788. array('label' => array('for' => 'ModelMultiField0')),
  4789. 'first',
  4790. '/label',
  4791. '/div',
  4792. array('div' => array('class' => 'checkbox')),
  4793. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4794. array('label' => array('for' => 'ModelMultiField1')),
  4795. 'second',
  4796. '/label',
  4797. '/div',
  4798. '/div'
  4799. );
  4800. $this->assertTags($result, $expected);
  4801. }
  4802. /**
  4803. * test that select() with multiple = checkbox works with overriding name attribute.
  4804. *
  4805. * @return void
  4806. */
  4807. public function testSelectCheckboxMultipleOverrideName() {
  4808. $result = $this->Form->input('category', array(
  4809. 'type' => 'select',
  4810. 'multiple' => 'checkbox',
  4811. 'name' => 'data[fish]',
  4812. 'options' => array('1', '2'),
  4813. 'div' => false,
  4814. 'label' => false,
  4815. ));
  4816. $expected = array(
  4817. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4818. array('div' => array('class' => 'checkbox')),
  4819. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4820. array('label' => array('for' => 'Category0')), '1', '/label',
  4821. '/div',
  4822. array('div' => array('class' => 'checkbox')),
  4823. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4824. array('label' => array('for' => 'Category1')), '2', '/label',
  4825. '/div'
  4826. );
  4827. $this->assertTags($result, $expected);
  4828. }
  4829. /**
  4830. * Test that 'id' overrides all the checkbox id's as well.
  4831. *
  4832. * @return void
  4833. */
  4834. public function testSelectCheckboxMultipleId() {
  4835. $result = $this->Form->select(
  4836. 'Model.multi_field',
  4837. array('first', 'second', 'third'),
  4838. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4839. );
  4840. $expected = array(
  4841. 'input' => array(
  4842. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4843. ),
  4844. array('div' => array('class' => 'checkbox')),
  4845. array('input' => array(
  4846. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4847. 'value' => '0', 'id' => 'CustomId0'
  4848. )),
  4849. array('label' => array('for' => 'CustomId0')),
  4850. 'first',
  4851. '/label',
  4852. '/div',
  4853. array('div' => array('class' => 'checkbox')),
  4854. array('input' => array(
  4855. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4856. 'value' => '1', 'id' => 'CustomId1'
  4857. )),
  4858. array('label' => array('for' => 'CustomId1')),
  4859. 'second',
  4860. '/label',
  4861. '/div',
  4862. array('div' => array('class' => 'checkbox')),
  4863. array('input' => array(
  4864. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4865. 'value' => '2', 'id' => 'CustomId2'
  4866. )),
  4867. array('label' => array('for' => 'CustomId2')),
  4868. 'third',
  4869. '/label',
  4870. '/div'
  4871. );
  4872. $this->assertTags($result, $expected);
  4873. }
  4874. /**
  4875. * testCheckbox method
  4876. *
  4877. * Test generation of checkboxes
  4878. *
  4879. * @return void
  4880. */
  4881. public function testCheckbox() {
  4882. $result = $this->Form->checkbox('Model.field');
  4883. $expected = array(
  4884. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4885. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4886. );
  4887. $this->assertTags($result, $expected);
  4888. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4889. $expected = array(
  4890. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4891. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4892. );
  4893. $this->assertTags($result, $expected);
  4894. $Contact = ClassRegistry::getObject('Contact');
  4895. $Contact->validationErrors['field'] = 1;
  4896. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4897. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4898. $expected = array(
  4899. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4900. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4901. );
  4902. $this->assertTags($result, $expected);
  4903. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4904. $expected = array(
  4905. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4906. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4907. );
  4908. $this->assertTags($result, $expected);
  4909. $this->Form->request->data['Contact']['field'] = '';
  4910. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4911. $expected = array(
  4912. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4913. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4914. );
  4915. $this->assertTags($result, $expected);
  4916. $Contact->validationErrors = array();
  4917. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4918. $expected = array(
  4919. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4920. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4921. );
  4922. $this->assertTags($result, $expected);
  4923. $this->Form->request->data['Contact']['published'] = 1;
  4924. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4925. $expected = array(
  4926. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4927. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4928. );
  4929. $this->assertTags($result, $expected);
  4930. $this->Form->request->data['Contact']['published'] = 0;
  4931. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4932. $expected = array(
  4933. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4934. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4935. );
  4936. $this->assertTags($result, $expected);
  4937. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4938. $expected = array(
  4939. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4940. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4941. );
  4942. $this->assertTags($result, $expected);
  4943. $result = $this->Form->checkbox('CustomField.1.value');
  4944. $expected = array(
  4945. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4946. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4947. );
  4948. $this->assertTags($result, $expected);
  4949. }
  4950. /**
  4951. * test checkbox() with a custom name attribute
  4952. *
  4953. * @return void
  4954. */
  4955. public function testCheckboxCustomNameAttribute() {
  4956. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4957. $expected = array(
  4958. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4959. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4960. );
  4961. $this->assertTags($result, $expected);
  4962. }
  4963. /**
  4964. * test the checked option for checkboxes.
  4965. *
  4966. * @return void
  4967. */
  4968. public function testCheckboxCheckedOption() {
  4969. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4970. $expected = array(
  4971. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4972. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4973. );
  4974. $this->assertTags($result, $expected);
  4975. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4976. $expected = array(
  4977. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4978. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4979. );
  4980. $this->assertTags($result, $expected);
  4981. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4982. $expected = array(
  4983. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4984. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4985. );
  4986. $this->assertTags($result, $expected);
  4987. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4988. $expected = array(
  4989. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4990. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4991. );
  4992. $this->assertTags($result, $expected);
  4993. $this->Form->request->data['Model']['field'] = 1;
  4994. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4995. $expected = array(
  4996. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4997. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4998. );
  4999. $this->assertTags($result, $expected);
  5000. }
  5001. /**
  5002. * Test that disabled attribute works on both the checkbox and hidden input.
  5003. *
  5004. * @return void
  5005. */
  5006. public function testCheckboxDisabling() {
  5007. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5008. $expected = array(
  5009. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5010. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5011. );
  5012. $this->assertTags($result, $expected);
  5013. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5014. $expected = array(
  5015. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5016. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5017. );
  5018. $this->assertTags($result, $expected);
  5019. }
  5020. /**
  5021. * Test that the hidden input for checkboxes can be omitted or set to a
  5022. * specific value.
  5023. *
  5024. * @return void
  5025. */
  5026. public function testCheckboxHiddenField() {
  5027. $result = $this->Form->input('UserForm.something', array(
  5028. 'type' => 'checkbox',
  5029. 'hiddenField' => false
  5030. ));
  5031. $expected = array(
  5032. 'div' => array('class' => 'input checkbox'),
  5033. array('input' => array(
  5034. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5035. 'value' => '1', 'id' => 'UserFormSomething'
  5036. )),
  5037. 'label' => array('for' => 'UserFormSomething'),
  5038. 'Something',
  5039. '/label',
  5040. '/div'
  5041. );
  5042. $this->assertTags($result, $expected);
  5043. $result = $this->Form->input('UserForm.something', array(
  5044. 'type' => 'checkbox',
  5045. 'value' => 'Y',
  5046. 'hiddenField' => 'N',
  5047. ));
  5048. $expected = array(
  5049. 'div' => array('class' => 'input checkbox'),
  5050. array('input' => array(
  5051. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  5052. 'value' => 'N', 'id' => 'UserFormSomething_'
  5053. )),
  5054. array('input' => array(
  5055. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5056. 'value' => 'Y', 'id' => 'UserFormSomething'
  5057. )),
  5058. 'label' => array('for' => 'UserFormSomething'),
  5059. 'Something',
  5060. '/label',
  5061. '/div'
  5062. );
  5063. $this->assertTags($result, $expected);
  5064. }
  5065. /**
  5066. * testDateTime method
  5067. *
  5068. * Test generation of date/time select elements
  5069. *
  5070. * @return void
  5071. */
  5072. public function testDateTime() {
  5073. extract($this->dateRegex);
  5074. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5075. $now = strtotime('now');
  5076. $expected = array(
  5077. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5078. $daysRegex,
  5079. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5080. date('j', $now),
  5081. '/option',
  5082. '*/select',
  5083. '-',
  5084. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5085. $monthsRegex,
  5086. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5087. date('F', $now),
  5088. '/option',
  5089. '*/select',
  5090. '-',
  5091. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5092. $yearsRegex,
  5093. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5094. date('Y', $now),
  5095. '/option',
  5096. '*/select',
  5097. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5098. $hoursRegex,
  5099. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5100. date('g', $now),
  5101. '/option',
  5102. '*/select',
  5103. ':',
  5104. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5105. $minutesRegex,
  5106. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5107. date('i', $now),
  5108. '/option',
  5109. '*/select',
  5110. ' ',
  5111. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5112. $meridianRegex,
  5113. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5114. date('a', $now),
  5115. '/option',
  5116. '*/select'
  5117. );
  5118. $this->assertTags($result, $expected);
  5119. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5120. $expected = array(
  5121. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5122. $daysRegex,
  5123. array('option' => array('value' => '')),
  5124. '/option',
  5125. '*/select',
  5126. '-',
  5127. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5128. $monthsRegex,
  5129. array('option' => array('value' => '')),
  5130. '/option',
  5131. '*/select',
  5132. '-',
  5133. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5134. $yearsRegex,
  5135. array('option' => array('value' => '')),
  5136. '/option',
  5137. '*/select',
  5138. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5139. $hoursRegex,
  5140. array('option' => array('value' => '')),
  5141. '/option',
  5142. '*/select',
  5143. ':',
  5144. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5145. $minutesRegex,
  5146. array('option' => array('value' => '')),
  5147. '/option',
  5148. '*/select',
  5149. ' ',
  5150. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5151. $meridianRegex,
  5152. array('option' => array('value' => '')),
  5153. '/option',
  5154. '*/select'
  5155. );
  5156. $this->assertTags($result, $expected);
  5157. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5158. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5159. $this->assertTags($result, $expected);
  5160. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5161. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5162. $this->assertTags($result, $expected);
  5163. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5164. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5165. $expected = array(
  5166. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5167. $daysRegex,
  5168. array('option' => array('value' => '')),
  5169. '/option',
  5170. '*/select',
  5171. '-',
  5172. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5173. $monthsRegex,
  5174. array('option' => array('value' => '')),
  5175. '/option',
  5176. '*/select',
  5177. '-',
  5178. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5179. $yearsRegex,
  5180. array('option' => array('value' => '')),
  5181. '/option',
  5182. '*/select',
  5183. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5184. $hoursRegex,
  5185. array('option' => array('value' => '')),
  5186. '/option',
  5187. '*/select',
  5188. ':',
  5189. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5190. $minutesRegex,
  5191. array('option' => array('value' => '')),
  5192. '/option',
  5193. array('option' => array('value' => '00')),
  5194. '00',
  5195. '/option',
  5196. array('option' => array('value' => '05')),
  5197. '05',
  5198. '/option',
  5199. array('option' => array('value' => '10')),
  5200. '10',
  5201. '/option',
  5202. '*/select',
  5203. ' ',
  5204. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5205. $meridianRegex,
  5206. array('option' => array('value' => '')),
  5207. '/option',
  5208. '*/select'
  5209. );
  5210. $this->assertTags($result, $expected);
  5211. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5212. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5213. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5214. $this->Form->request->data['Contact']['data'] = null;
  5215. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5216. $expected = array(
  5217. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5218. $daysRegex,
  5219. array('option' => array('value' => '')),
  5220. '/option',
  5221. '*/select',
  5222. '-',
  5223. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5224. $monthsRegex,
  5225. array('option' => array('value' => '')),
  5226. '/option',
  5227. '*/select',
  5228. '-',
  5229. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5230. $yearsRegex,
  5231. array('option' => array('value' => '')),
  5232. '/option',
  5233. '*/select',
  5234. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5235. $hoursRegex,
  5236. array('option' => array('value' => '')),
  5237. '/option',
  5238. '*/select',
  5239. ':',
  5240. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5241. $minutesRegex,
  5242. array('option' => array('value' => '')),
  5243. '/option',
  5244. '*/select',
  5245. ' ',
  5246. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5247. $meridianRegex,
  5248. array('option' => array('value' => '')),
  5249. '/option',
  5250. '*/select'
  5251. );
  5252. $this->assertTags($result, $expected);
  5253. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5254. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5255. $now = strtotime($this->Form->data['Model']['field']);
  5256. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5257. $expected = array(
  5258. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5259. $daysRegex,
  5260. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5261. date('j', $now),
  5262. '/option',
  5263. '*/select',
  5264. '-',
  5265. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5266. $monthsRegex,
  5267. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5268. date('F', $now),
  5269. '/option',
  5270. '*/select',
  5271. '-',
  5272. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5273. $yearsRegex,
  5274. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5275. date('Y', $now),
  5276. '/option',
  5277. '*/select',
  5278. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5279. $hoursRegex,
  5280. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5281. date('g', $now),
  5282. '/option',
  5283. '*/select',
  5284. ':',
  5285. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5286. $minutesRegex,
  5287. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5288. date('i', $now),
  5289. '/option',
  5290. '*/select',
  5291. ' ',
  5292. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5293. $meridianRegex,
  5294. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5295. date('a', $now),
  5296. '/option',
  5297. '*/select'
  5298. );
  5299. $this->assertTags($result, $expected);
  5300. $selected = strtotime('2008-10-26 12:33:00');
  5301. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5302. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5303. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5304. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5305. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5306. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5307. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5308. $this->Form->create('Contact');
  5309. $result = $this->Form->input('published');
  5310. $now = strtotime('now');
  5311. $expected = array(
  5312. 'div' => array('class' => 'input date'),
  5313. 'label' => array('for' => 'ContactPublishedMonth'),
  5314. 'Published',
  5315. '/label',
  5316. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5317. $monthsRegex,
  5318. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5319. date('F', $now),
  5320. '/option',
  5321. '*/select',
  5322. '-',
  5323. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5324. $daysRegex,
  5325. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5326. date('j', $now),
  5327. '/option',
  5328. '*/select',
  5329. '-',
  5330. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5331. $yearsRegex,
  5332. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5333. date('Y', $now),
  5334. '/option',
  5335. '*/select',
  5336. '/div'
  5337. );
  5338. $this->assertTags($result, $expected);
  5339. $result = $this->Form->input('published2', array('type' => 'date'));
  5340. $now = strtotime('now');
  5341. $expected = array(
  5342. 'div' => array('class' => 'input date'),
  5343. 'label' => array('for' => 'ContactPublished2Month'),
  5344. 'Published2',
  5345. '/label',
  5346. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5347. $monthsRegex,
  5348. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5349. date('F', $now),
  5350. '/option',
  5351. '*/select',
  5352. '-',
  5353. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5354. $daysRegex,
  5355. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5356. date('j', $now),
  5357. '/option',
  5358. '*/select',
  5359. '-',
  5360. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5361. $yearsRegex,
  5362. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5363. date('Y', $now),
  5364. '/option',
  5365. '*/select',
  5366. '/div'
  5367. );
  5368. $this->assertTags($result, $expected);
  5369. $this->Form->create('Contact');
  5370. $result = $this->Form->input('published', array('monthNames' => false));
  5371. $now = strtotime('now');
  5372. $expected = array(
  5373. 'div' => array('class' => 'input date'),
  5374. 'label' => array('for' => 'ContactPublishedMonth'),
  5375. 'Published',
  5376. '/label',
  5377. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5378. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5379. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5380. date('m', $now),
  5381. '/option',
  5382. '*/select',
  5383. '-',
  5384. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5385. $daysRegex,
  5386. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5387. date('j', $now),
  5388. '/option',
  5389. '*/select',
  5390. '-',
  5391. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5392. $yearsRegex,
  5393. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5394. date('Y', $now),
  5395. '/option',
  5396. '*/select',
  5397. '/div'
  5398. );
  5399. $this->assertTags($result, $expected);
  5400. $result = $this->Form->input('published', array(
  5401. 'timeFormat' => 24,
  5402. 'interval' => 5,
  5403. 'selected' => strtotime('2009-09-03 13:37:00'),
  5404. 'type' => 'datetime'
  5405. ));
  5406. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5407. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5408. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5409. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5410. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5411. }
  5412. /**
  5413. * Test dateTime with rounding
  5414. *
  5415. * @return void
  5416. */
  5417. public function testDateTimeRounding() {
  5418. $this->Form->request->data['Contact'] = array(
  5419. 'date' => array(
  5420. 'day' => '13',
  5421. 'month' => '12',
  5422. 'year' => '2010',
  5423. 'hour' => '04',
  5424. 'min' => '19',
  5425. 'meridian' => 'AM'
  5426. )
  5427. );
  5428. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5429. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5430. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5431. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5432. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5433. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5434. }
  5435. /**
  5436. * Test that empty values don't trigger errors.
  5437. *
  5438. * @return void
  5439. */
  5440. public function testDateTimeNoErrorsOnEmptyData() {
  5441. $this->Form->request->data['Contact'] = array(
  5442. 'date' => array(
  5443. 'day' => '',
  5444. 'month' => '',
  5445. 'year' => '',
  5446. 'hour' => '',
  5447. 'min' => '',
  5448. 'meridian' => ''
  5449. )
  5450. );
  5451. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5452. $this->assertNotEmpty($result);
  5453. }
  5454. /**
  5455. * test that datetime() and default values work.
  5456. *
  5457. * @return void
  5458. */
  5459. public function testDatetimeWithDefault() {
  5460. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5461. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5462. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5463. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5464. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5465. 'default' => '2009-06-01 11:15:30'
  5466. ));
  5467. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5468. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5469. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5470. }
  5471. /**
  5472. * test that bogus non-date time data doesn't cause errors.
  5473. *
  5474. * @return void
  5475. */
  5476. public function testDateTimeWithBogusData() {
  5477. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5478. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5479. }
  5480. /**
  5481. * testDateTimeEmptyAsArray
  5482. *
  5483. * @return void
  5484. */
  5485. public function testDateTimeEmptyAsArray() {
  5486. $result = $this->Form->dateTime('Contact.date',
  5487. 'DMY',
  5488. '12',
  5489. array(
  5490. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5491. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5492. )
  5493. )
  5494. );
  5495. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5496. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5497. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5498. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5499. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5500. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5501. $result = $this->Form->dateTime('Contact.date',
  5502. 'DMY',
  5503. '12',
  5504. array(
  5505. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5506. )
  5507. );
  5508. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5509. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5510. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5511. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5512. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5513. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5514. }
  5515. /**
  5516. * testFormDateTimeMulti method
  5517. *
  5518. * test multiple datetime element generation
  5519. *
  5520. * @return void
  5521. */
  5522. public function testFormDateTimeMulti() {
  5523. extract($this->dateRegex);
  5524. $result = $this->Form->dateTime('Contact.1.updated');
  5525. $expected = array(
  5526. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5527. $daysRegex,
  5528. array('option' => array('value' => '')),
  5529. '/option',
  5530. '*/select',
  5531. '-',
  5532. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5533. $monthsRegex,
  5534. array('option' => array('value' => '')),
  5535. '/option',
  5536. '*/select',
  5537. '-',
  5538. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5539. $yearsRegex,
  5540. array('option' => array('value' => '')),
  5541. '/option',
  5542. '*/select',
  5543. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5544. $hoursRegex,
  5545. array('option' => array('value' => '')),
  5546. '/option',
  5547. '*/select',
  5548. ':',
  5549. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5550. $minutesRegex,
  5551. array('option' => array('value' => '')),
  5552. '/option',
  5553. '*/select',
  5554. ' ',
  5555. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5556. $meridianRegex,
  5557. array('option' => array('value' => '')),
  5558. '/option',
  5559. '*/select'
  5560. );
  5561. $this->assertTags($result, $expected);
  5562. $result = $this->Form->dateTime('Contact.2.updated');
  5563. $expected = array(
  5564. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5565. $daysRegex,
  5566. array('option' => array('value' => '')),
  5567. '/option',
  5568. '*/select',
  5569. '-',
  5570. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5571. $monthsRegex,
  5572. array('option' => array('value' => '')),
  5573. '/option',
  5574. '*/select',
  5575. '-',
  5576. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5577. $yearsRegex,
  5578. array('option' => array('value' => '')),
  5579. '/option',
  5580. '*/select',
  5581. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5582. $hoursRegex,
  5583. array('option' => array('value' => '')),
  5584. '/option',
  5585. '*/select',
  5586. ':',
  5587. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5588. $minutesRegex,
  5589. array('option' => array('value' => '')),
  5590. '/option',
  5591. '*/select',
  5592. ' ',
  5593. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5594. $meridianRegex,
  5595. array('option' => array('value' => '')),
  5596. '/option',
  5597. '*/select'
  5598. );
  5599. $this->assertTags($result, $expected);
  5600. }
  5601. /**
  5602. * When changing the date format, the label should always focus the first select box when
  5603. * clicked.
  5604. *
  5605. * @return void
  5606. */
  5607. public function testDateTimeLabelIdMatchesFirstInput() {
  5608. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5609. $this->assertContains('label for="ModelDateMonth"', $result);
  5610. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5611. $this->assertContains('label for="ModelDateDay"', $result);
  5612. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5613. $this->assertContains('label for="ModelDateYear"', $result);
  5614. }
  5615. /**
  5616. * testMonth method
  5617. *
  5618. * @return void
  5619. */
  5620. public function testMonth() {
  5621. $result = $this->Form->month('Model.field');
  5622. $expected = array(
  5623. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5624. array('option' => array('value' => '')),
  5625. '/option',
  5626. array('option' => array('value' => '01')),
  5627. date('F', strtotime('2008-01-01 00:00:00')),
  5628. '/option',
  5629. array('option' => array('value' => '02')),
  5630. date('F', strtotime('2008-02-01 00:00:00')),
  5631. '/option',
  5632. '*/select',
  5633. );
  5634. $this->assertTags($result, $expected);
  5635. $result = $this->Form->month('Model.field', array('empty' => true));
  5636. $expected = array(
  5637. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5638. array('option' => array('value' => '')),
  5639. '/option',
  5640. array('option' => array('value' => '01')),
  5641. date('F', strtotime('2008-01-01 00:00:00')),
  5642. '/option',
  5643. array('option' => array('value' => '02')),
  5644. date('F', strtotime('2008-02-01 00:00:00')),
  5645. '/option',
  5646. '*/select',
  5647. );
  5648. $this->assertTags($result, $expected);
  5649. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5650. $expected = array(
  5651. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5652. array('option' => array('value' => '')),
  5653. '/option',
  5654. array('option' => array('value' => '01')),
  5655. '01',
  5656. '/option',
  5657. array('option' => array('value' => '02')),
  5658. '02',
  5659. '/option',
  5660. '*/select',
  5661. );
  5662. $this->assertTags($result, $expected);
  5663. $monthNames = array(
  5664. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5665. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5666. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5667. $expected = array(
  5668. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5669. array('option' => array('value' => '')),
  5670. '/option',
  5671. array('option' => array('value' => '01')),
  5672. 'Jan',
  5673. '/option',
  5674. array('option' => array('value' => '02')),
  5675. 'Feb',
  5676. '/option',
  5677. '*/select',
  5678. );
  5679. $this->assertTags($result, $expected);
  5680. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5681. $result = $this->Form->month('Project.release');
  5682. $expected = array(
  5683. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  5684. array('option' => array('value' => '')),
  5685. '/option',
  5686. array('option' => array('value' => '01')),
  5687. 'January',
  5688. '/option',
  5689. array('option' => array('value' => '02', 'selected' => 'selected')),
  5690. 'February',
  5691. '/option',
  5692. '*/select',
  5693. );
  5694. $this->assertTags($result, $expected);
  5695. }
  5696. /**
  5697. * testDay method
  5698. *
  5699. * @return void
  5700. */
  5701. public function testDay() {
  5702. extract($this->dateRegex);
  5703. $result = $this->Form->day('Model.field', array('value' => false));
  5704. $expected = array(
  5705. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5706. array('option' => array('value' => '')),
  5707. '/option',
  5708. array('option' => array('value' => '01')),
  5709. '1',
  5710. '/option',
  5711. array('option' => array('value' => '02')),
  5712. '2',
  5713. '/option',
  5714. $daysRegex,
  5715. '/select',
  5716. );
  5717. $this->assertTags($result, $expected);
  5718. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5719. $result = $this->Form->day('Model.field');
  5720. $expected = array(
  5721. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5722. array('option' => array('value' => '')),
  5723. '/option',
  5724. array('option' => array('value' => '01')),
  5725. '1',
  5726. '/option',
  5727. array('option' => array('value' => '02')),
  5728. '2',
  5729. '/option',
  5730. $daysRegex,
  5731. array('option' => array('value' => '10', 'selected' => 'selected')),
  5732. '10',
  5733. '/option',
  5734. $daysRegex,
  5735. '/select',
  5736. );
  5737. $this->assertTags($result, $expected);
  5738. $this->Form->request->data['Model']['field'] = '';
  5739. $result = $this->Form->day('Model.field', array('value' => '10'));
  5740. $expected = array(
  5741. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5742. array('option' => array('value' => '')),
  5743. '/option',
  5744. array('option' => array('value' => '01')),
  5745. '1',
  5746. '/option',
  5747. array('option' => array('value' => '02')),
  5748. '2',
  5749. '/option',
  5750. $daysRegex,
  5751. array('option' => array('value' => '10', 'selected' => 'selected')),
  5752. '10',
  5753. '/option',
  5754. $daysRegex,
  5755. '/select',
  5756. );
  5757. $this->assertTags($result, $expected);
  5758. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5759. $result = $this->Form->day('Model.field', array('value' => true));
  5760. $expected = array(
  5761. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5762. array('option' => array('value' => '')),
  5763. '/option',
  5764. array('option' => array('value' => '01')),
  5765. '1',
  5766. '/option',
  5767. array('option' => array('value' => '02')),
  5768. '2',
  5769. '/option',
  5770. $daysRegex,
  5771. array('option' => array('value' => '10', 'selected' => 'selected')),
  5772. '10',
  5773. '/option',
  5774. $daysRegex,
  5775. '/select',
  5776. );
  5777. $this->assertTags($result, $expected);
  5778. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5779. $result = $this->Form->day('Project.release');
  5780. $expected = array(
  5781. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  5782. array('option' => array('value' => '')),
  5783. '/option',
  5784. array('option' => array('value' => '01')),
  5785. '1',
  5786. '/option',
  5787. array('option' => array('value' => '02')),
  5788. '2',
  5789. '/option',
  5790. $daysRegex,
  5791. array('option' => array('value' => '10', 'selected' => 'selected')),
  5792. '10',
  5793. '/option',
  5794. $daysRegex,
  5795. '/select',
  5796. );
  5797. $this->assertTags($result, $expected);
  5798. }
  5799. /**
  5800. * testMinute method
  5801. *
  5802. * @return void
  5803. */
  5804. public function testMinute() {
  5805. extract($this->dateRegex);
  5806. $result = $this->Form->minute('Model.field');
  5807. $expected = array(
  5808. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5809. array('option' => array('value' => '')),
  5810. '/option',
  5811. array('option' => array('value' => '00')),
  5812. '00',
  5813. '/option',
  5814. array('option' => array('value' => '01')),
  5815. '01',
  5816. '/option',
  5817. array('option' => array('value' => '02')),
  5818. '02',
  5819. '/option',
  5820. $minutesRegex,
  5821. '/select',
  5822. );
  5823. $this->assertTags($result, $expected);
  5824. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5825. $result = $this->Form->minute('Model.field');
  5826. $expected = array(
  5827. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5828. array('option' => array('value' => '')),
  5829. '/option',
  5830. array('option' => array('value' => '00')),
  5831. '00',
  5832. '/option',
  5833. array('option' => array('value' => '01')),
  5834. '01',
  5835. '/option',
  5836. array('option' => array('value' => '02')),
  5837. '02',
  5838. '/option',
  5839. $minutesRegex,
  5840. array('option' => array('value' => '12', 'selected' => 'selected')),
  5841. '12',
  5842. '/option',
  5843. $minutesRegex,
  5844. '/select',
  5845. );
  5846. $this->assertTags($result, $expected);
  5847. $this->Form->request->data['Model']['field'] = '';
  5848. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5849. $expected = array(
  5850. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5851. array('option' => array('value' => '')),
  5852. '/option',
  5853. array('option' => array('value' => '00')),
  5854. '00',
  5855. '/option',
  5856. array('option' => array('value' => '05')),
  5857. '05',
  5858. '/option',
  5859. array('option' => array('value' => '10')),
  5860. '10',
  5861. '/option',
  5862. $minutesRegex,
  5863. '/select',
  5864. );
  5865. $this->assertTags($result, $expected);
  5866. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5867. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5868. $expected = array(
  5869. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5870. array('option' => array('value' => '')),
  5871. '/option',
  5872. array('option' => array('value' => '00')),
  5873. '00',
  5874. '/option',
  5875. array('option' => array('value' => '05')),
  5876. '05',
  5877. '/option',
  5878. array('option' => array('value' => '10', 'selected' => 'selected')),
  5879. '10',
  5880. '/option',
  5881. $minutesRegex,
  5882. '/select',
  5883. );
  5884. $this->assertTags($result, $expected);
  5885. }
  5886. /**
  5887. * testHour method
  5888. *
  5889. * @return void
  5890. */
  5891. public function testHour() {
  5892. extract($this->dateRegex);
  5893. $result = $this->Form->hour('Model.field', false);
  5894. $expected = array(
  5895. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5896. array('option' => array('value' => '')),
  5897. '/option',
  5898. array('option' => array('value' => '01')),
  5899. '1',
  5900. '/option',
  5901. array('option' => array('value' => '02')),
  5902. '2',
  5903. '/option',
  5904. $hoursRegex,
  5905. '/select',
  5906. );
  5907. $this->assertTags($result, $expected);
  5908. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5909. $result = $this->Form->hour('Model.field', false);
  5910. $expected = array(
  5911. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5912. array('option' => array('value' => '')),
  5913. '/option',
  5914. array('option' => array('value' => '01')),
  5915. '1',
  5916. '/option',
  5917. array('option' => array('value' => '02')),
  5918. '2',
  5919. '/option',
  5920. $hoursRegex,
  5921. array('option' => array('value' => '12', 'selected' => 'selected')),
  5922. '12',
  5923. '/option',
  5924. '/select',
  5925. );
  5926. $this->assertTags($result, $expected);
  5927. $this->Form->request->data['Model']['field'] = '';
  5928. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5929. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5930. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5931. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5932. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5933. $result = $this->Form->hour('Model.field', true);
  5934. $expected = array(
  5935. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5936. array('option' => array('value' => '')),
  5937. '/option',
  5938. array('option' => array('value' => '00', 'selected' => 'selected')),
  5939. '0',
  5940. '/option',
  5941. array('option' => array('value' => '01')),
  5942. '1',
  5943. '/option',
  5944. array('option' => array('value' => '02')),
  5945. '2',
  5946. '/option',
  5947. $hoursRegex,
  5948. '/select',
  5949. );
  5950. $this->assertTags($result, $expected);
  5951. unset($this->Form->request->data['Model']['field']);
  5952. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5953. $thisHour = date('H');
  5954. $optValue = date('G');
  5955. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5956. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  5957. $result = $this->Form->hour('Model.field', true);
  5958. $expected = array(
  5959. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5960. array('option' => array('value' => '')),
  5961. '/option',
  5962. array('option' => array('value' => '00')),
  5963. '0',
  5964. '/option',
  5965. array('option' => array('value' => '01', 'selected' => 'selected')),
  5966. '1',
  5967. '/option',
  5968. array('option' => array('value' => '02')),
  5969. '2',
  5970. '/option',
  5971. $hoursRegex,
  5972. '/select',
  5973. );
  5974. $this->assertTags($result, $expected);
  5975. }
  5976. /**
  5977. * testYear method
  5978. *
  5979. * @return void
  5980. */
  5981. public function testYear() {
  5982. $result = $this->Form->year('Model.field', 2006, 2007);
  5983. $expected = array(
  5984. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5985. array('option' => array('value' => '')),
  5986. '/option',
  5987. array('option' => array('value' => '2007')),
  5988. '2007',
  5989. '/option',
  5990. array('option' => array('value' => '2006')),
  5991. '2006',
  5992. '/option',
  5993. '/select',
  5994. );
  5995. $this->assertTags($result, $expected);
  5996. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5997. $expected = array(
  5998. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5999. array('option' => array('value' => '')),
  6000. '/option',
  6001. array('option' => array('value' => '2006')),
  6002. '2006',
  6003. '/option',
  6004. array('option' => array('value' => '2007')),
  6005. '2007',
  6006. '/option',
  6007. '/select',
  6008. );
  6009. $this->assertTags($result, $expected);
  6010. $this->request->data['Contact']['published'] = '';
  6011. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6012. $expected = array(
  6013. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6014. array('option' => array('value' => '')),
  6015. '/option',
  6016. array('option' => array('value' => '2007')),
  6017. '2007',
  6018. '/option',
  6019. array('option' => array('value' => '2006')),
  6020. '2006',
  6021. '/option',
  6022. '/select',
  6023. );
  6024. $this->assertTags($result, $expected);
  6025. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6026. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6027. $expected = array(
  6028. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6029. array('option' => array('value' => '2007')),
  6030. '2007',
  6031. '/option',
  6032. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6033. '2006',
  6034. '/option',
  6035. '/select',
  6036. );
  6037. $this->assertTags($result, $expected);
  6038. $this->Form->request->data['Contact']['published'] = '';
  6039. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6040. $expected = array(
  6041. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6042. array('option' => array('value' => '')),
  6043. '/option',
  6044. array('option' => array('value' => '2007')),
  6045. '2007',
  6046. '/option',
  6047. array('option' => array('value' => '2006')),
  6048. '2006',
  6049. '/option',
  6050. '/select',
  6051. );
  6052. $this->assertTags($result, $expected);
  6053. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6054. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6055. $expected = array(
  6056. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6057. array('option' => array('value' => '2007')),
  6058. '2007',
  6059. '/option',
  6060. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6061. '2006',
  6062. '/option',
  6063. '/select',
  6064. );
  6065. $this->assertTags($result, $expected);
  6066. $this->Form->request->data['Contact']['published'] = '';
  6067. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6068. $expected = array(
  6069. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6070. array('option' => array('value' => '')),
  6071. '/option',
  6072. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6073. '2007',
  6074. '/option',
  6075. array('option' => array('value' => '2006')),
  6076. '2006',
  6077. '/option',
  6078. '/select',
  6079. );
  6080. $this->assertTags($result, $expected);
  6081. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6082. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6083. $expected = array(
  6084. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6085. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6086. '2007',
  6087. '/option',
  6088. array('option' => array('value' => '2006')),
  6089. '2006',
  6090. '/option',
  6091. '/select',
  6092. );
  6093. $this->assertTags($result, $expected);
  6094. $this->Form->request->data['Contact']['published'] = '';
  6095. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6096. $expected = array(
  6097. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6098. array('option' => array('value' => '2008')),
  6099. '2008',
  6100. '/option',
  6101. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6102. '2007',
  6103. '/option',
  6104. array('option' => array('value' => '2006')),
  6105. '2006',
  6106. '/option',
  6107. '/select',
  6108. );
  6109. $this->assertTags($result, $expected);
  6110. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6111. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6112. $expected = array(
  6113. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6114. array('option' => array('value' => '2008')),
  6115. '2008',
  6116. '/option',
  6117. array('option' => array('value' => '2007')),
  6118. '2007',
  6119. '/option',
  6120. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6121. '2006',
  6122. '/option',
  6123. '/select',
  6124. );
  6125. $this->assertTags($result, $expected);
  6126. $this->Form->request->data = array();
  6127. $this->Form->create('Contact');
  6128. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6129. $expected = array(
  6130. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6131. array('option' => array('value' => '2008')),
  6132. '2008',
  6133. '/option',
  6134. array('option' => array('value' => '2007')),
  6135. '2007',
  6136. '/option',
  6137. array('option' => array('value' => '2006')),
  6138. '2006',
  6139. '/option',
  6140. '/select',
  6141. );
  6142. $this->assertTags($result, $expected);
  6143. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6144. $this->assertContains('data[Contact][published][year]', $result);
  6145. }
  6146. /**
  6147. * testYearAutoExpandRange method
  6148. *
  6149. * @return void
  6150. */
  6151. public function testYearAutoExpandRange() {
  6152. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6153. $result = $this->Form->year('User.birthday');
  6154. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6155. $result = $matches[1];
  6156. $expected = range(date('Y') + 20, 1930);
  6157. $this->assertEquals($result, $expected);
  6158. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6159. $result = $this->Form->year('Project.release');
  6160. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6161. $result = $matches[1];
  6162. $expected = range(2050, date('Y') - 20);
  6163. $this->assertEquals($result, $expected);
  6164. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6165. $result = $this->Form->year('Project.release', 1890, 1900);
  6166. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6167. $result = $matches[1];
  6168. $expected = range(1900, 1881);
  6169. $this->assertEquals($result, $expected);
  6170. }
  6171. /**
  6172. * testInputDate method
  6173. *
  6174. * Test various inputs with type date and different dateFormat values.
  6175. * Failing to provide a dateFormat key should not error.
  6176. * It should simply not pre-select any value then.
  6177. *
  6178. * @return void
  6179. */
  6180. public function testInputDate() {
  6181. $this->Form->request->data = array(
  6182. 'User' => array(
  6183. 'month_year' => array('month' => date('m')),
  6184. 'just_year' => array('month' => date('m')),
  6185. 'just_month' => array('year' => date('Y')),
  6186. 'just_day' => array('month' => date('m')),
  6187. )
  6188. );
  6189. $this->Form->create('User');
  6190. $result = $this->Form->input('month_year',
  6191. array(
  6192. 'label' => false,
  6193. 'div' => false,
  6194. 'type' => 'date',
  6195. 'dateFormat' => 'MY',
  6196. 'minYear' => 2006,
  6197. 'maxYear' => 2008
  6198. )
  6199. );
  6200. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6201. $this->assertNotContains('value="2008" selected="selected"', $result);
  6202. $result = $this->Form->input('just_year',
  6203. array(
  6204. 'type' => 'date',
  6205. 'label' => false,
  6206. 'dateFormat' => 'Y',
  6207. 'minYear' => date('Y'),
  6208. 'maxYear' => date('Y', strtotime('+20 years'))
  6209. )
  6210. );
  6211. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6212. $result = $this->Form->input('just_month',
  6213. array(
  6214. 'type' => 'date',
  6215. 'label' => false,
  6216. 'dateFormat' => 'M',
  6217. 'empty' => false,
  6218. )
  6219. );
  6220. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6221. $result = $this->Form->input('just_day',
  6222. array(
  6223. 'type' => 'date',
  6224. 'label' => false,
  6225. 'dateFormat' => 'D',
  6226. 'empty' => false,
  6227. )
  6228. );
  6229. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6230. }
  6231. /**
  6232. * testInputDateMaxYear method
  6233. *
  6234. * Let's say we want to only allow users born from 2006 to 2008 to register
  6235. * This being the first singup page, we still don't have any data
  6236. *
  6237. * @return void
  6238. */
  6239. public function testInputDateMaxYear() {
  6240. $this->Form->request->data = array();
  6241. $this->Form->create('User');
  6242. $result = $this->Form->input('birthday',
  6243. array(
  6244. 'label' => false,
  6245. 'div' => false,
  6246. 'type' => 'date',
  6247. 'dateFormat' => 'DMY',
  6248. 'minYear' => 2006,
  6249. 'maxYear' => 2008
  6250. )
  6251. );
  6252. $this->assertContains('value="2008" selected="selected"', $result);
  6253. }
  6254. /**
  6255. * testTextArea method
  6256. *
  6257. * @return void
  6258. */
  6259. public function testTextArea() {
  6260. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6261. $result = $this->Form->textarea('Model.field');
  6262. $expected = array(
  6263. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6264. 'some test data',
  6265. '/textarea',
  6266. );
  6267. $this->assertTags($result, $expected);
  6268. $result = $this->Form->textarea('Model.tmp');
  6269. $expected = array(
  6270. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6271. '/textarea',
  6272. );
  6273. $this->assertTags($result, $expected);
  6274. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6275. $result = $this->Form->textarea('Model.field');
  6276. $expected = array(
  6277. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6278. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6279. '/textarea',
  6280. );
  6281. $this->assertTags($result, $expected);
  6282. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6283. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6284. $expected = array(
  6285. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6286. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6287. '/textarea',
  6288. );
  6289. $this->assertTags($result, $expected);
  6290. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6291. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6292. $expected = array(
  6293. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6294. '/textarea'
  6295. );
  6296. $this->assertTags($result, $expected);
  6297. }
  6298. /**
  6299. * testTextAreaWithStupidCharacters method
  6300. *
  6301. * test text area with non-ascii characters
  6302. *
  6303. * @return void
  6304. */
  6305. public function testTextAreaWithStupidCharacters() {
  6306. $this->loadFixtures('Post');
  6307. $result = $this->Form->input('Post.content', array(
  6308. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6309. ));
  6310. $expected = array(
  6311. 'div' => array('class' => 'input textarea'),
  6312. 'label' => array('for' => 'PostContent'),
  6313. 'Current Text',
  6314. '/label',
  6315. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6316. 'GREAT®',
  6317. '/textarea',
  6318. '/div'
  6319. );
  6320. $this->assertTags($result, $expected);
  6321. }
  6322. /**
  6323. * testHiddenField method
  6324. *
  6325. * @return void
  6326. */
  6327. public function testHiddenField() {
  6328. $Contact = ClassRegistry::getObject('Contact');
  6329. $Contact->validationErrors['field'] = 1;
  6330. $this->Form->request->data['Contact']['field'] = 'test';
  6331. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6332. $this->assertTags($result, array(
  6333. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6334. );
  6335. }
  6336. /**
  6337. * testFileUploadField method
  6338. *
  6339. * @return void
  6340. */
  6341. public function testFileUploadField() {
  6342. $result = $this->Form->file('Model.upload');
  6343. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6344. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6345. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6346. $expected = array(
  6347. 'div' => array('class' => 'input file'),
  6348. 'label' => array('for' => 'ModelUpload'),
  6349. 'Upload',
  6350. '/label',
  6351. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6352. '/div'
  6353. );
  6354. $this->assertTags($result, $expected);
  6355. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6356. $result = $this->Form->file('Model.upload');
  6357. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6358. }
  6359. /**
  6360. * test File upload input on a model not used in create();
  6361. *
  6362. * @return void
  6363. */
  6364. public function testFileUploadOnOtherModel() {
  6365. $this->Form->create('ValidateUser', array('type' => 'file'));
  6366. $result = $this->Form->file('ValidateProfile.city');
  6367. $expected = array(
  6368. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6369. );
  6370. $this->assertTags($result, $expected);
  6371. }
  6372. /**
  6373. * testButton method
  6374. *
  6375. * @return void
  6376. */
  6377. public function testButton() {
  6378. $result = $this->Form->button('Hi');
  6379. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6380. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6381. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6382. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6383. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6384. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6385. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6386. $result = $this->Form->button('No type', array('type' => false));
  6387. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6388. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6389. $this->assertNotRegExp('/\&039/', $result);
  6390. }
  6391. /**
  6392. * Test that button() makes unlocked fields by default.
  6393. *
  6394. * @return void
  6395. */
  6396. public function testButtonUnlockedByDefault() {
  6397. $this->Form->request->params['_Token']['key'] = 'secured';
  6398. $this->Form->button('Save', array('name' => 'save'));
  6399. $this->Form->button('Clear');
  6400. $result = $this->Form->unlockField();
  6401. $this->assertEquals(array('save'), $result);
  6402. }
  6403. /**
  6404. * testPostButton method
  6405. *
  6406. * @return void
  6407. */
  6408. public function testPostButton() {
  6409. $result = $this->Form->postButton('Hi', '/controller/action');
  6410. $this->assertTags($result, array(
  6411. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6412. 'div' => array('style' => 'display:none;'),
  6413. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6414. '/div',
  6415. 'button' => array('type' => 'submit'),
  6416. 'Hi',
  6417. '/button',
  6418. '/form'
  6419. ));
  6420. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6421. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6422. }
  6423. /**
  6424. * Test that postButton adds _Token fields.
  6425. *
  6426. * @return void
  6427. */
  6428. public function testSecurePostButton() {
  6429. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6430. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6431. $expected = array(
  6432. 'form' => array(
  6433. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6434. ),
  6435. array('div' => array('style' => 'display:none;')),
  6436. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6437. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6438. '/div',
  6439. 'button' => array('type' => 'submit'),
  6440. 'Delete',
  6441. '/button',
  6442. array('div' => array('style' => 'display:none;')),
  6443. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6444. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6445. '/div',
  6446. '/form',
  6447. );
  6448. $this->assertTags($result, $expected);
  6449. }
  6450. /**
  6451. * testPostLink method
  6452. *
  6453. * @return void
  6454. */
  6455. public function testPostLink() {
  6456. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6457. $this->assertTags($result, array(
  6458. 'form' => array(
  6459. 'method' => 'post', 'action' => '/posts/delete/1',
  6460. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6461. ),
  6462. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6463. '/form',
  6464. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6465. 'Delete',
  6466. '/a'
  6467. ));
  6468. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6469. $this->assertTags($result, array(
  6470. 'form' => array(
  6471. 'method' => 'post', 'action' => '/posts/delete/1',
  6472. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6473. ),
  6474. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6475. '/form',
  6476. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6477. 'Delete',
  6478. '/a'
  6479. ));
  6480. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6481. $this->assertTags($result, array(
  6482. 'form' => array(
  6483. 'method' => 'post', 'action' => '/posts/delete/1',
  6484. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6485. ),
  6486. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6487. '/form',
  6488. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6489. 'Delete',
  6490. '/a'
  6491. ));
  6492. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  6493. $this->assertTags($result, array(
  6494. 'form' => array(
  6495. 'method' => 'post', 'action' => '/posts/delete/1',
  6496. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6497. ),
  6498. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6499. '/form',
  6500. '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;/'),
  6501. 'Delete',
  6502. '/a'
  6503. ));
  6504. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6505. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6506. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6507. $this->assertTags($result, array(
  6508. 'form' => array(
  6509. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6510. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6511. ),
  6512. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6513. '/form',
  6514. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6515. 'Delete',
  6516. '/a'
  6517. ));
  6518. }
  6519. /**
  6520. * Test that postLink adds _Token fields.
  6521. *
  6522. * @return void
  6523. */
  6524. public function testSecurePostLink() {
  6525. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6526. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6527. $expected = array(
  6528. 'form' => array(
  6529. 'method' => 'post', 'action' => '/posts/delete/1',
  6530. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6531. ),
  6532. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6533. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6534. 'div' => array('style' => 'display:none;'),
  6535. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6536. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6537. '/div',
  6538. '/form',
  6539. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6540. 'Delete',
  6541. '/a'
  6542. );
  6543. $this->assertTags($result, $expected);
  6544. }
  6545. /**
  6546. * testSubmitButton method
  6547. *
  6548. * @return void
  6549. */
  6550. public function testSubmitButton() {
  6551. $result = $this->Form->submit('');
  6552. $expected = array(
  6553. 'div' => array('class' => 'submit'),
  6554. 'input' => array('type' => 'submit', 'value' => ''),
  6555. '/div'
  6556. );
  6557. $this->assertTags($result, $expected);
  6558. $result = $this->Form->submit('Test Submit');
  6559. $expected = array(
  6560. 'div' => array('class' => 'submit'),
  6561. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6562. '/div'
  6563. );
  6564. $this->assertTags($result, $expected);
  6565. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6566. $expected = array(
  6567. 'span' => array('class' => 'submit'),
  6568. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6569. '/span'
  6570. );
  6571. $this->assertTags($result, $expected);
  6572. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6573. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6574. $this->assertTags($result, $expected);
  6575. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6576. $expected = array(
  6577. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6578. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6579. '/div'
  6580. );
  6581. $this->assertTags($result, $expected);
  6582. $result = $this->Form->submit('Next >');
  6583. $expected = array(
  6584. 'div' => array('class' => 'submit'),
  6585. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6586. '/div'
  6587. );
  6588. $this->assertTags($result, $expected);
  6589. $result = $this->Form->submit('Next >', array('escape' => false));
  6590. $expected = array(
  6591. 'div' => array('class' => 'submit'),
  6592. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6593. '/div'
  6594. );
  6595. $this->assertTags($result, $expected);
  6596. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6597. $expected = array(
  6598. 'div' => array('class' => 'submit'),
  6599. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6600. '/div'
  6601. );
  6602. $this->assertTags($result, $expected);
  6603. $before = '--before--';
  6604. $after = '--after--';
  6605. $result = $this->Form->submit('Test', array('before' => $before));
  6606. $expected = array(
  6607. 'div' => array('class' => 'submit'),
  6608. '--before--',
  6609. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6610. '/div'
  6611. );
  6612. $this->assertTags($result, $expected);
  6613. $result = $this->Form->submit('Test', array('after' => $after));
  6614. $expected = array(
  6615. 'div' => array('class' => 'submit'),
  6616. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6617. '--after--',
  6618. '/div'
  6619. );
  6620. $this->assertTags($result, $expected);
  6621. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6622. $expected = array(
  6623. 'div' => array('class' => 'submit'),
  6624. '--before--',
  6625. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6626. '--after--',
  6627. '/div'
  6628. );
  6629. $this->assertTags($result, $expected);
  6630. }
  6631. /**
  6632. * test image submit types.
  6633. *
  6634. * @return void
  6635. */
  6636. public function testSubmitImage() {
  6637. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6638. $expected = array(
  6639. 'div' => array('class' => 'submit'),
  6640. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6641. '/div'
  6642. );
  6643. $this->assertTags($result, $expected);
  6644. $result = $this->Form->submit('/relative/cake.power.gif');
  6645. $expected = array(
  6646. 'div' => array('class' => 'submit'),
  6647. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  6648. '/div'
  6649. );
  6650. $this->assertTags($result, $expected);
  6651. $result = $this->Form->submit('cake.power.gif');
  6652. $expected = array(
  6653. 'div' => array('class' => 'submit'),
  6654. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6655. '/div'
  6656. );
  6657. $this->assertTags($result, $expected);
  6658. $result = $this->Form->submit('Not.an.image');
  6659. $expected = array(
  6660. 'div' => array('class' => 'submit'),
  6661. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6662. '/div'
  6663. );
  6664. $this->assertTags($result, $expected);
  6665. $after = '--after--';
  6666. $before = '--before--';
  6667. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6668. $expected = array(
  6669. 'div' => array('class' => 'submit'),
  6670. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6671. '--after--',
  6672. '/div'
  6673. );
  6674. $this->assertTags($result, $expected);
  6675. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6676. $expected = array(
  6677. 'div' => array('class' => 'submit'),
  6678. '--before--',
  6679. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6680. '/div'
  6681. );
  6682. $this->assertTags($result, $expected);
  6683. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6684. $expected = array(
  6685. 'div' => array('class' => 'submit'),
  6686. '--before--',
  6687. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6688. '--after--',
  6689. '/div'
  6690. );
  6691. $this->assertTags($result, $expected);
  6692. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6693. $expected = array(
  6694. 'div' => array('class' => 'submit'),
  6695. '--before--',
  6696. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6697. '--after--',
  6698. '/div'
  6699. );
  6700. $this->assertTags($result, $expected);
  6701. }
  6702. /**
  6703. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6704. * be submitted at a time.
  6705. *
  6706. * @return void
  6707. */
  6708. public function testSubmitUnlockedByDefault() {
  6709. $this->Form->request->params['_Token']['key'] = 'secured';
  6710. $this->Form->submit('Go go');
  6711. $this->Form->submit('Save', array('name' => 'save'));
  6712. $result = $this->Form->unlockField();
  6713. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6714. }
  6715. /**
  6716. * Test submit image with timestamps.
  6717. *
  6718. * @return void
  6719. */
  6720. public function testSubmitImageTimestamp() {
  6721. Configure::write('Asset.timestamp', 'force');
  6722. $result = $this->Form->submit('cake.power.gif');
  6723. $expected = array(
  6724. 'div' => array('class' => 'submit'),
  6725. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6726. '/div'
  6727. );
  6728. $this->assertTags($result, $expected);
  6729. }
  6730. /**
  6731. * test the create() method
  6732. *
  6733. * @return void
  6734. */
  6735. public function testCreate() {
  6736. $result = $this->Form->create('Contact');
  6737. $encoding = strtolower(Configure::read('App.encoding'));
  6738. $expected = array(
  6739. 'form' => array(
  6740. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6741. 'accept-charset' => $encoding
  6742. ),
  6743. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6744. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6745. '/div'
  6746. );
  6747. $this->assertTags($result, $expected);
  6748. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6749. $expected = array('form' => array(
  6750. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6751. 'accept-charset' => $encoding
  6752. ));
  6753. $this->assertTags($result, $expected);
  6754. $result = $this->Form->create('Contact', array('type' => 'get'));
  6755. $expected = array('form' => array(
  6756. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6757. 'accept-charset' => $encoding
  6758. ));
  6759. $this->assertTags($result, $expected);
  6760. $result = $this->Form->create('Contact', array('type' => 'put'));
  6761. $expected = array(
  6762. 'form' => array(
  6763. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6764. 'accept-charset' => $encoding
  6765. ),
  6766. 'div' => array('style' => 'display:none;'),
  6767. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6768. '/div'
  6769. );
  6770. $this->assertTags($result, $expected);
  6771. $result = $this->Form->create('Contact', array('type' => 'file'));
  6772. $expected = array(
  6773. 'form' => array(
  6774. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6775. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6776. ),
  6777. 'div' => array('style' => 'display:none;'),
  6778. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6779. '/div'
  6780. );
  6781. $this->assertTags($result, $expected);
  6782. $this->Form->request->data['Contact']['id'] = 1;
  6783. $this->Form->request->here = '/contacts/edit/1';
  6784. $this->Form->request['action'] = 'edit';
  6785. $result = $this->Form->create('Contact');
  6786. $expected = array(
  6787. 'form' => array(
  6788. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6789. 'accept-charset' => $encoding
  6790. ),
  6791. 'div' => array('style' => 'display:none;'),
  6792. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6793. '/div'
  6794. );
  6795. $this->assertTags($result, $expected);
  6796. $this->Form->request->data['Contact']['id'] = 1;
  6797. $this->Form->request->here = '/contacts/edit/1';
  6798. $this->Form->request['action'] = 'edit';
  6799. $result = $this->Form->create('Contact', array('type' => 'file'));
  6800. $expected = array(
  6801. 'form' => array(
  6802. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6803. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6804. ),
  6805. 'div' => array('style' => 'display:none;'),
  6806. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6807. '/div'
  6808. );
  6809. $this->assertTags($result, $expected);
  6810. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6811. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6812. $expected = array(
  6813. 'form' => array(
  6814. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6815. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  6816. ),
  6817. 'div' => array('style' => 'display:none;'),
  6818. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6819. '/div'
  6820. );
  6821. $this->assertTags($result, $expected);
  6822. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6823. $expected = array(
  6824. 'form' => array(
  6825. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6826. 'accept-charset' => $encoding
  6827. ),
  6828. 'div' => array('style' => 'display:none;'),
  6829. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6830. '/div'
  6831. );
  6832. $this->assertTags($result, $expected);
  6833. $this->Form->request['action'] = 'add';
  6834. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6835. $expected = array(
  6836. 'form' => array(
  6837. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6838. 'accept-charset' => $encoding
  6839. ),
  6840. 'div' => array('style' => 'display:none;'),
  6841. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6842. '/div'
  6843. );
  6844. $this->assertTags($result, $expected);
  6845. $result = $this->Form->create('User', array('action' => 'login'));
  6846. $expected = array(
  6847. 'form' => array(
  6848. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6849. 'accept-charset' => $encoding
  6850. ),
  6851. 'div' => array('style' => 'display:none;'),
  6852. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6853. '/div'
  6854. );
  6855. $this->assertTags($result, $expected);
  6856. $result = $this->Form->create('User', array('url' => '/users/login'));
  6857. $expected = array(
  6858. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6859. 'div' => array('style' => 'display:none;'),
  6860. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6861. '/div'
  6862. );
  6863. $this->assertTags($result, $expected);
  6864. $this->Form->request['controller'] = 'pages';
  6865. $result = $this->Form->create('User', array('action' => 'signup'));
  6866. $expected = array(
  6867. 'form' => array(
  6868. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6869. 'accept-charset' => $encoding
  6870. ),
  6871. 'div' => array('style' => 'display:none;'),
  6872. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6873. '/div'
  6874. );
  6875. $this->assertTags($result, $expected);
  6876. $this->Form->request->data = array();
  6877. $this->Form->request['controller'] = 'contacts';
  6878. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6879. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6880. $expected = array(
  6881. 'form' => array(
  6882. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6883. 'accept-charset' => 'utf-8'
  6884. ),
  6885. 'div' => array('style' => 'display:none;'),
  6886. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6887. '/div'
  6888. );
  6889. $this->assertTags($result, $expected);
  6890. }
  6891. /**
  6892. * Test the onsubmit option for create()
  6893. *
  6894. * @return void
  6895. */
  6896. public function testCreateOnSubmit() {
  6897. $this->Form->request->data = array();
  6898. $this->Form->request['controller'] = 'contacts';
  6899. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6900. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6901. $expected = array(
  6902. 'form' => array(
  6903. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6904. 'accept-charset' => 'utf-8'
  6905. ),
  6906. 'div' => array('style' => 'display:none;'),
  6907. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6908. '/div'
  6909. );
  6910. $this->assertTags($result, $expected);
  6911. $this->Form->request->data = array();
  6912. $this->Form->request['controller'] = 'contacts';
  6913. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6914. $result = $this->Form->create(array(
  6915. 'url' => array('action' => 'index', 'param'),
  6916. 'default' => false,
  6917. 'onsubmit' => 'someFunction();'
  6918. ));
  6919. $expected = array(
  6920. 'form' => array(
  6921. 'id' => 'ContactAddForm', 'method' => 'post',
  6922. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6923. 'action' => '/contacts/index/param',
  6924. 'accept-charset' => 'utf-8'
  6925. ),
  6926. 'div' => array('style' => 'display:none;'),
  6927. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6928. '/div'
  6929. );
  6930. $this->assertTags($result, $expected);
  6931. }
  6932. /**
  6933. * test create() with automatic url generation
  6934. *
  6935. * @return void
  6936. */
  6937. public function testCreateAutoUrl() {
  6938. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6939. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6940. $this->Form->request->base = '/base_url';
  6941. $result = $this->Form->create('Contact');
  6942. $expected = array(
  6943. 'form' => array(
  6944. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6945. 'accept-charset' => 'utf-8'
  6946. ),
  6947. 'div' => array('style' => 'display:none;'),
  6948. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6949. '/div'
  6950. );
  6951. $this->assertTags($result, $expected);
  6952. $this->Form->request['action'] = 'delete';
  6953. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6954. $this->Form->request->base = '/base_url';
  6955. $result = $this->Form->create('Contact');
  6956. $expected = array(
  6957. 'form' => array(
  6958. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6959. 'accept-charset' => 'utf-8'
  6960. ),
  6961. 'div' => array('style' => 'display:none;'),
  6962. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6963. '/div'
  6964. );
  6965. $this->assertTags($result, $expected);
  6966. }
  6967. /**
  6968. * test create() with a custom route
  6969. *
  6970. * @return void
  6971. */
  6972. public function testCreateCustomRoute() {
  6973. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6974. $encoding = strtolower(Configure::read('App.encoding'));
  6975. $result = $this->Form->create('User', array('action' => 'login'));
  6976. $expected = array(
  6977. 'form' => array(
  6978. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6979. 'accept-charset' => $encoding
  6980. ),
  6981. 'div' => array('style' => 'display:none;'),
  6982. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6983. '/div'
  6984. );
  6985. $this->assertTags($result, $expected);
  6986. }
  6987. /**
  6988. * test that inputDefaults are stored and used.
  6989. *
  6990. * @return void
  6991. */
  6992. public function testCreateWithInputDefaults() {
  6993. $this->Form->create('User', array(
  6994. 'inputDefaults' => array(
  6995. 'div' => false,
  6996. 'label' => false,
  6997. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6998. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6999. )
  7000. ));
  7001. $result = $this->Form->input('username');
  7002. $expected = array(
  7003. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  7004. );
  7005. $this->assertTags($result, $expected);
  7006. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  7007. $expected = array(
  7008. 'div' => array('class' => 'input text'),
  7009. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  7010. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7011. '/div'
  7012. );
  7013. $this->assertTags($result, $expected);
  7014. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7015. $expected = array(
  7016. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7017. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7018. );
  7019. $this->assertTags($result, $expected);
  7020. $this->Form->create('User', array(
  7021. 'inputDefaults' => array(
  7022. 'div' => false,
  7023. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7024. )
  7025. ));
  7026. $result = $this->Form->input('username', array('div' => true));
  7027. $expected = array(
  7028. 'div' => array('class' => 'input text'),
  7029. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7030. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7031. '/div'
  7032. );
  7033. $this->assertTags($result, $expected);
  7034. }
  7035. /**
  7036. * test automatic accept-charset overriding
  7037. *
  7038. * @return void
  7039. */
  7040. public function testCreateWithAcceptCharset() {
  7041. $result = $this->Form->create('UserForm', array(
  7042. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7043. )
  7044. );
  7045. $expected = array(
  7046. 'form' => array(
  7047. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7048. 'accept-charset' => 'iso-8859-1'
  7049. ),
  7050. 'div' => array('style' => 'display:none;'),
  7051. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7052. '/div'
  7053. );
  7054. $this->assertTags($result, $expected);
  7055. }
  7056. /**
  7057. * Test base form url when url param is passed with multiple parameters (&)
  7058. *
  7059. */
  7060. public function testCreateQuerystringrequest() {
  7061. $encoding = strtolower(Configure::read('App.encoding'));
  7062. $result = $this->Form->create('Contact', array(
  7063. 'type' => 'post',
  7064. 'escape' => false,
  7065. 'url' => array(
  7066. 'controller' => 'controller',
  7067. 'action' => 'action',
  7068. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7069. )
  7070. ));
  7071. $expected = array(
  7072. 'form' => array(
  7073. 'id' => 'ContactAddForm',
  7074. 'method' => 'post',
  7075. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7076. 'accept-charset' => $encoding
  7077. ),
  7078. 'div' => array('style' => 'display:none;'),
  7079. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7080. '/div'
  7081. );
  7082. $this->assertTags($result, $expected);
  7083. $result = $this->Form->create('Contact', array(
  7084. 'type' => 'post',
  7085. 'url' => array(
  7086. 'controller' => 'controller',
  7087. 'action' => 'action',
  7088. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7089. )
  7090. ));
  7091. $expected = array(
  7092. 'form' => array(
  7093. 'id' => 'ContactAddForm',
  7094. 'method' => 'post',
  7095. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7096. 'accept-charset' => $encoding
  7097. ),
  7098. 'div' => array('style' => 'display:none;'),
  7099. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7100. '/div'
  7101. );
  7102. $this->assertTags($result, $expected);
  7103. }
  7104. /**
  7105. * test that create() doesn't cause errors by multiple id's being in the primary key
  7106. * as could happen with multiple select or checkboxes.
  7107. *
  7108. * @return void
  7109. */
  7110. public function testCreateWithMultipleIdInData() {
  7111. $encoding = strtolower(Configure::read('App.encoding'));
  7112. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7113. $result = $this->Form->create('Contact');
  7114. $expected = array(
  7115. 'form' => array(
  7116. 'id' => 'ContactAddForm',
  7117. 'method' => 'post',
  7118. 'action' => '/contacts/add',
  7119. 'accept-charset' => $encoding
  7120. ),
  7121. 'div' => array('style' => 'display:none;'),
  7122. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7123. '/div'
  7124. );
  7125. $this->assertTags($result, $expected);
  7126. }
  7127. /**
  7128. * test that create() doesn't add in extra passed params.
  7129. *
  7130. * @return void
  7131. */
  7132. public function testCreatePassedArgs() {
  7133. $encoding = strtolower(Configure::read('App.encoding'));
  7134. $this->Form->request->data['Contact']['id'] = 1;
  7135. $result = $this->Form->create('Contact', array(
  7136. 'type' => 'post',
  7137. 'escape' => false,
  7138. 'url' => array(
  7139. 'action' => 'edit',
  7140. 'myparam'
  7141. )
  7142. ));
  7143. $expected = array(
  7144. 'form' => array(
  7145. 'id' => 'ContactAddForm',
  7146. 'method' => 'post',
  7147. 'action' => '/contacts/edit/myparam',
  7148. 'accept-charset' => $encoding
  7149. ),
  7150. 'div' => array('style' => 'display:none;'),
  7151. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7152. '/div'
  7153. );
  7154. $this->assertTags($result, $expected);
  7155. }
  7156. /**
  7157. * test creating a get form, and get form inputs.
  7158. *
  7159. * @return void
  7160. */
  7161. public function testGetFormCreate() {
  7162. $encoding = strtolower(Configure::read('App.encoding'));
  7163. $result = $this->Form->create('Contact', array('type' => 'get'));
  7164. $this->assertTags($result, array('form' => array(
  7165. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7166. 'accept-charset' => $encoding
  7167. )));
  7168. $result = $this->Form->text('Contact.name');
  7169. $this->assertTags($result, array('input' => array(
  7170. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7171. )));
  7172. $result = $this->Form->password('password');
  7173. $this->assertTags($result, array('input' => array(
  7174. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7175. )));
  7176. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7177. $result = $this->Form->text('user_form');
  7178. $this->assertTags($result, array('input' => array(
  7179. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7180. )));
  7181. }
  7182. /**
  7183. * test get form, and inputs when the model param is false
  7184. *
  7185. * @return void
  7186. */
  7187. public function testGetFormWithFalseModel() {
  7188. $encoding = strtolower(Configure::read('App.encoding'));
  7189. $this->Form->request['controller'] = 'contact_test';
  7190. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7191. $expected = array('form' => array(
  7192. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7193. 'accept-charset' => $encoding
  7194. ));
  7195. $this->assertTags($result, $expected);
  7196. $result = $this->Form->text('reason');
  7197. $expected = array(
  7198. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7199. );
  7200. $this->assertTags($result, $expected);
  7201. }
  7202. /**
  7203. * test that datetime() works with GET style forms.
  7204. *
  7205. * @return void
  7206. */
  7207. public function testDateTimeWithGetForms() {
  7208. extract($this->dateRegex);
  7209. $this->Form->create('Contact', array('type' => 'get'));
  7210. $result = $this->Form->datetime('created');
  7211. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7212. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7213. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7214. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7215. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7216. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7217. }
  7218. /**
  7219. * testEditFormWithData method
  7220. *
  7221. * test auto populating form elements from submitted data.
  7222. *
  7223. * @return void
  7224. */
  7225. public function testEditFormWithData() {
  7226. $this->Form->request->data = array('Person' => array(
  7227. 'id' => 1,
  7228. 'first_name' => 'Nate',
  7229. 'last_name' => 'Abele',
  7230. 'email' => 'nate@example.com'
  7231. ));
  7232. $this->Form->request->addParams(array(
  7233. 'models' => array('Person'),
  7234. 'controller' => 'people',
  7235. 'action' => 'add'
  7236. ));
  7237. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7238. $this->Form->create();
  7239. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7240. $expected = array(
  7241. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7242. 'select' => array(
  7243. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7244. ),
  7245. array('option' => array('value' => 1)), 'Nate', '/option',
  7246. array('option' => array('value' => 2)), 'Garrett', '/option',
  7247. array('option' => array('value' => 3)), 'Larry', '/option',
  7248. '/select'
  7249. );
  7250. $this->assertTags($result, $expected);
  7251. }
  7252. /**
  7253. * Test that required fields are created for various types of validation.
  7254. *
  7255. * @return void
  7256. */
  7257. public function testFormInputRequiredDetection() {
  7258. $this->Form->create('Contact');
  7259. $result = $this->Form->input('Contact.non_existing');
  7260. $expected = array(
  7261. 'div' => array('class' => 'input text'),
  7262. 'label' => array('for' => 'ContactNonExisting'),
  7263. 'Non Existing',
  7264. '/label',
  7265. 'input' => array(
  7266. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7267. 'id' => 'ContactNonExisting'
  7268. ),
  7269. '/div'
  7270. );
  7271. $this->assertTags($result, $expected);
  7272. $result = $this->Form->input('Contact.imrequired');
  7273. $expected = array(
  7274. 'div' => array('class' => 'input text required'),
  7275. 'label' => array('for' => 'ContactImrequired'),
  7276. 'Imrequired',
  7277. '/label',
  7278. 'input' => array(
  7279. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7280. 'id' => 'ContactImrequired',
  7281. 'required' => 'required'
  7282. ),
  7283. '/div'
  7284. );
  7285. $this->assertTags($result, $expected);
  7286. $result = $this->Form->input('Contact.imalsorequired');
  7287. $expected = array(
  7288. 'div' => array('class' => 'input text required'),
  7289. 'label' => array('for' => 'ContactImalsorequired'),
  7290. 'Imalsorequired',
  7291. '/label',
  7292. 'input' => array(
  7293. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7294. 'id' => 'ContactImalsorequired',
  7295. 'required' => 'required'
  7296. ),
  7297. '/div'
  7298. );
  7299. $this->assertTags($result, $expected);
  7300. $result = $this->Form->input('Contact.imrequiredtoo');
  7301. $expected = array(
  7302. 'div' => array('class' => 'input text required'),
  7303. 'label' => array('for' => 'ContactImrequiredtoo'),
  7304. 'Imrequiredtoo',
  7305. '/label',
  7306. 'input' => array(
  7307. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7308. 'id' => 'ContactImrequiredtoo',
  7309. 'required' => 'required'
  7310. ),
  7311. '/div'
  7312. );
  7313. $this->assertTags($result, $expected);
  7314. $result = $this->Form->input('Contact.required_one');
  7315. $expected = array(
  7316. 'div' => array('class' => 'input text required'),
  7317. 'label' => array('for' => 'ContactRequiredOne'),
  7318. 'Required One',
  7319. '/label',
  7320. 'input' => array(
  7321. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7322. 'id' => 'ContactRequiredOne',
  7323. 'required' => 'required'
  7324. ),
  7325. '/div'
  7326. );
  7327. $this->assertTags($result, $expected);
  7328. $result = $this->Form->input('Contact.string_required');
  7329. $expected = array(
  7330. 'div' => array('class' => 'input text required'),
  7331. 'label' => array('for' => 'ContactStringRequired'),
  7332. 'String Required',
  7333. '/label',
  7334. 'input' => array(
  7335. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7336. 'id' => 'ContactStringRequired',
  7337. 'required' => 'required'
  7338. ),
  7339. '/div'
  7340. );
  7341. $this->assertTags($result, $expected);
  7342. $result = $this->Form->input('Contact.imnotrequired');
  7343. $expected = array(
  7344. 'div' => array('class' => 'input text'),
  7345. 'label' => array('for' => 'ContactImnotrequired'),
  7346. 'Imnotrequired',
  7347. '/label',
  7348. 'input' => array(
  7349. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7350. 'id' => 'ContactImnotrequired'
  7351. ),
  7352. '/div'
  7353. );
  7354. $this->assertTags($result, $expected);
  7355. $result = $this->Form->input('Contact.imalsonotrequired');
  7356. $expected = array(
  7357. 'div' => array('class' => 'input text'),
  7358. 'label' => array('for' => 'ContactImalsonotrequired'),
  7359. 'Imalsonotrequired',
  7360. '/label',
  7361. 'input' => array(
  7362. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7363. 'id' => 'ContactImalsonotrequired'
  7364. ),
  7365. '/div'
  7366. );
  7367. $this->assertTags($result, $expected);
  7368. $result = $this->Form->input('Contact.imalsonotrequired2');
  7369. $expected = array(
  7370. 'div' => array('class' => 'input text'),
  7371. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7372. 'Imalsonotrequired2',
  7373. '/label',
  7374. 'input' => array(
  7375. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7376. 'id' => 'ContactImalsonotrequired2'
  7377. ),
  7378. '/div'
  7379. );
  7380. $this->assertTags($result, $expected);
  7381. $result = $this->Form->input('Contact.imnotrequiredeither');
  7382. $expected = array(
  7383. 'div' => array('class' => 'input text'),
  7384. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7385. 'Imnotrequiredeither',
  7386. '/label',
  7387. 'input' => array(
  7388. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7389. 'id' => 'ContactImnotrequiredeither'
  7390. ),
  7391. '/div'
  7392. );
  7393. $this->assertTags($result, $expected);
  7394. $result = $this->Form->input('Contact.iamrequiredalways');
  7395. $expected = array(
  7396. 'div' => array('class' => 'input text required'),
  7397. 'label' => array('for' => 'ContactIamrequiredalways'),
  7398. 'Iamrequiredalways',
  7399. '/label',
  7400. 'input' => array(
  7401. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7402. 'id' => 'ContactIamrequiredalways',
  7403. 'required' => 'required'
  7404. ),
  7405. '/div'
  7406. );
  7407. $this->assertTags($result, $expected);
  7408. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7409. $expected = array(
  7410. 'div' => array('class' => 'input checkbox required'),
  7411. array('input' => array(
  7412. 'type' => 'hidden',
  7413. 'name' => 'data[Contact][boolean_field]',
  7414. 'id' => 'ContactBooleanField_',
  7415. 'value' => '0'
  7416. )),
  7417. array('input' => array(
  7418. 'type' => 'checkbox',
  7419. 'name' => 'data[Contact][boolean_field]',
  7420. 'value' => '1',
  7421. 'id' => 'ContactBooleanField'
  7422. )),
  7423. 'label' => array('for' => 'ContactBooleanField'),
  7424. 'Boolean Field',
  7425. '/label',
  7426. '/div'
  7427. );
  7428. $this->assertTags($result, $expected);
  7429. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7430. $expected = array(
  7431. 'div' => array('class' => 'input checkbox required'),
  7432. array('input' => array(
  7433. 'type' => 'hidden',
  7434. 'name' => 'data[Contact][boolean_field]',
  7435. 'id' => 'ContactBooleanField_',
  7436. 'value' => '0'
  7437. )),
  7438. array('input' => array(
  7439. 'type' => 'checkbox',
  7440. 'name' => 'data[Contact][boolean_field]',
  7441. 'value' => '1',
  7442. 'id' => 'ContactBooleanField',
  7443. 'required' => 'required'
  7444. )),
  7445. 'label' => array('for' => 'ContactBooleanField'),
  7446. 'Boolean Field',
  7447. '/label',
  7448. '/div'
  7449. );
  7450. $this->assertTags($result, $expected);
  7451. }
  7452. /**
  7453. * Test that required fields are created when only using ModelValidator::add().
  7454. *
  7455. * @return void
  7456. */
  7457. public function testFormInputRequiredDetectionModelValidator() {
  7458. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  7459. $this->Form->create('ContactTag');
  7460. $result = $this->Form->input('ContactTag.iwillberequired');
  7461. $expected = array(
  7462. 'div' => array('class' => 'input text required'),
  7463. 'label' => array('for' => 'ContactTagIwillberequired'),
  7464. 'Iwillberequired',
  7465. '/label',
  7466. 'input' => array(
  7467. 'name' => 'data[ContactTag][iwillberequired]',
  7468. 'type' => 'text',
  7469. 'id' => 'ContactTagIwillberequired',
  7470. 'required' => 'required'
  7471. ),
  7472. '/div'
  7473. );
  7474. $this->assertTags($result, $expected);
  7475. }
  7476. /**
  7477. * testFormMagicInput method
  7478. *
  7479. * @return void
  7480. */
  7481. public function testFormMagicInput() {
  7482. $encoding = strtolower(Configure::read('App.encoding'));
  7483. $result = $this->Form->create('Contact');
  7484. $expected = array(
  7485. 'form' => array(
  7486. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7487. 'accept-charset' => $encoding
  7488. ),
  7489. 'div' => array('style' => 'display:none;'),
  7490. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7491. '/div'
  7492. );
  7493. $this->assertTags($result, $expected);
  7494. $result = $this->Form->input('name');
  7495. $expected = array(
  7496. 'div' => array('class' => 'input text'),
  7497. 'label' => array('for' => 'ContactName'),
  7498. 'Name',
  7499. '/label',
  7500. 'input' => array(
  7501. 'type' => 'text', 'name' => 'data[Contact][name]',
  7502. 'id' => 'ContactName', 'maxlength' => '255'
  7503. ),
  7504. '/div'
  7505. );
  7506. $this->assertTags($result, $expected);
  7507. $result = $this->Form->input('non_existing_field_in_contact_model');
  7508. $expected = array(
  7509. 'div' => array('class' => 'input text'),
  7510. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7511. 'Non Existing Field In Contact Model',
  7512. '/label',
  7513. 'input' => array(
  7514. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  7515. 'id' => 'ContactNonExistingFieldInContactModel'
  7516. ),
  7517. '/div'
  7518. );
  7519. $this->assertTags($result, $expected);
  7520. $result = $this->Form->input('Address.street');
  7521. $expected = array(
  7522. 'div' => array('class' => 'input text'),
  7523. 'label' => array('for' => 'AddressStreet'),
  7524. 'Street',
  7525. '/label',
  7526. 'input' => array(
  7527. 'type' => 'text', 'name' => 'data[Address][street]',
  7528. 'id' => 'AddressStreet'
  7529. ),
  7530. '/div'
  7531. );
  7532. $this->assertTags($result, $expected);
  7533. $result = $this->Form->input('Address.non_existing_field_in_model');
  7534. $expected = array(
  7535. 'div' => array('class' => 'input text'),
  7536. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7537. 'Non Existing Field In Model',
  7538. '/label',
  7539. 'input' => array(
  7540. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  7541. 'id' => 'AddressNonExistingFieldInModel'
  7542. ),
  7543. '/div'
  7544. );
  7545. $this->assertTags($result, $expected);
  7546. $result = $this->Form->input('name', array('div' => false));
  7547. $expected = array(
  7548. 'label' => array('for' => 'ContactName'),
  7549. 'Name',
  7550. '/label',
  7551. 'input' => array(
  7552. 'type' => 'text', 'name' => 'data[Contact][name]',
  7553. 'id' => 'ContactName', 'maxlength' => '255'
  7554. )
  7555. );
  7556. $this->assertTags($result, $expected);
  7557. extract($this->dateRegex);
  7558. $now = strtotime('now');
  7559. $result = $this->Form->input('Contact.published', array('div' => false));
  7560. $expected = array(
  7561. 'label' => array('for' => 'ContactPublishedMonth'),
  7562. 'Published',
  7563. '/label',
  7564. array('select' => array(
  7565. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  7566. )),
  7567. $monthsRegex,
  7568. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7569. date('F', $now),
  7570. '/option',
  7571. '*/select',
  7572. '-',
  7573. array('select' => array(
  7574. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  7575. )),
  7576. $daysRegex,
  7577. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7578. date('j', $now),
  7579. '/option',
  7580. '*/select',
  7581. '-',
  7582. array('select' => array(
  7583. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  7584. )),
  7585. $yearsRegex,
  7586. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7587. date('Y', $now),
  7588. '*/select'
  7589. );
  7590. $this->assertTags($result, $expected);
  7591. $result = $this->Form->input('Contact.updated', array('div' => false));
  7592. $expected = array(
  7593. 'label' => array('for' => 'ContactUpdatedMonth'),
  7594. 'Updated',
  7595. '/label',
  7596. array('select' => array(
  7597. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  7598. )),
  7599. $monthsRegex,
  7600. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7601. date('F', $now),
  7602. '/option',
  7603. '*/select',
  7604. '-',
  7605. array('select' => array(
  7606. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  7607. )),
  7608. $daysRegex,
  7609. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7610. date('j', $now),
  7611. '/option',
  7612. '*/select',
  7613. '-',
  7614. array('select' => array(
  7615. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  7616. )),
  7617. $yearsRegex,
  7618. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7619. date('Y', $now),
  7620. '*/select'
  7621. );
  7622. $this->assertTags($result, $expected);
  7623. $result = $this->Form->input('UserForm.stuff');
  7624. $expected = array(
  7625. 'div' => array('class' => 'input text'),
  7626. 'label' => array('for' => 'UserFormStuff'),
  7627. 'Stuff',
  7628. '/label',
  7629. 'input' => array(
  7630. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  7631. 'id' => 'UserFormStuff', 'maxlength' => 10
  7632. ),
  7633. '/div'
  7634. );
  7635. $this->assertTags($result, $expected);
  7636. }
  7637. /**
  7638. * testForMagicInputNonExistingNorValidated method
  7639. *
  7640. * @return void
  7641. */
  7642. public function testForMagicInputNonExistingNorValidated() {
  7643. $encoding = strtolower(Configure::read('App.encoding'));
  7644. $result = $this->Form->create('Contact');
  7645. $expected = array(
  7646. 'form' => array(
  7647. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7648. 'accept-charset' => $encoding
  7649. ),
  7650. 'div' => array('style' => 'display:none;'),
  7651. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7652. '/div'
  7653. );
  7654. $this->assertTags($result, $expected);
  7655. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7656. $expected = array(
  7657. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7658. 'Non Existing Nor Validated',
  7659. '/label',
  7660. 'input' => array(
  7661. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7662. 'id' => 'ContactNonExistingNorValidated'
  7663. )
  7664. );
  7665. $this->assertTags($result, $expected);
  7666. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  7667. 'div' => false, 'value' => 'my value'
  7668. ));
  7669. $expected = array(
  7670. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7671. 'Non Existing Nor Validated',
  7672. '/label',
  7673. 'input' => array(
  7674. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7675. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  7676. )
  7677. );
  7678. $this->assertTags($result, $expected);
  7679. $this->Form->request->data = array(
  7680. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  7681. ));
  7682. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7683. $expected = array(
  7684. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7685. 'Non Existing Nor Validated',
  7686. '/label',
  7687. 'input' => array(
  7688. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7689. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  7690. )
  7691. );
  7692. $this->assertTags($result, $expected);
  7693. }
  7694. /**
  7695. * testFormMagicInputLabel method
  7696. *
  7697. * @return void
  7698. */
  7699. public function testFormMagicInputLabel() {
  7700. $encoding = strtolower(Configure::read('App.encoding'));
  7701. $result = $this->Form->create('Contact');
  7702. $expected = array(
  7703. 'form' => array(
  7704. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7705. 'accept-charset' => $encoding
  7706. ),
  7707. 'div' => array('style' => 'display:none;'),
  7708. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7709. '/div'
  7710. );
  7711. $this->assertTags($result, $expected);
  7712. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  7713. $this->assertTags($result, array('input' => array(
  7714. 'name' => 'data[Contact][name]', 'type' => 'text',
  7715. 'id' => 'ContactName', 'maxlength' => '255')
  7716. ));
  7717. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  7718. $expected = array(
  7719. 'label' => array('for' => 'ContactName'),
  7720. 'My label',
  7721. '/label',
  7722. 'input' => array(
  7723. 'type' => 'text', 'name' => 'data[Contact][name]',
  7724. 'id' => 'ContactName', 'maxlength' => '255'
  7725. )
  7726. );
  7727. $this->assertTags($result, $expected);
  7728. $result = $this->Form->input('Contact.name', array(
  7729. 'div' => false, 'label' => array('class' => 'mandatory')
  7730. ));
  7731. $expected = array(
  7732. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7733. 'Name',
  7734. '/label',
  7735. 'input' => array(
  7736. 'type' => 'text', 'name' => 'data[Contact][name]',
  7737. 'id' => 'ContactName', 'maxlength' => '255'
  7738. )
  7739. );
  7740. $this->assertTags($result, $expected);
  7741. $result = $this->Form->input('Contact.name', array(
  7742. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7743. ));
  7744. $expected = array(
  7745. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7746. 'My label',
  7747. '/label',
  7748. 'input' => array(
  7749. 'type' => 'text', 'name' => 'data[Contact][name]',
  7750. 'id' => 'ContactName', 'maxlength' => '255'
  7751. )
  7752. );
  7753. $this->assertTags($result, $expected);
  7754. $result = $this->Form->input('Contact.name', array(
  7755. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7756. ));
  7757. $expected = array(
  7758. 'label' => array('for' => 'my_id'),
  7759. 'Name',
  7760. '/label',
  7761. 'input' => array(
  7762. 'type' => 'text', 'name' => 'data[Contact][name]',
  7763. 'id' => 'my_id', 'maxlength' => '255'
  7764. )
  7765. );
  7766. $this->assertTags($result, $expected);
  7767. $result = $this->Form->input('1.id');
  7768. $this->assertTags($result, array('input' => array(
  7769. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7770. 'id' => 'Contact1Id'
  7771. )));
  7772. $result = $this->Form->input("1.name");
  7773. $expected = array(
  7774. 'div' => array('class' => 'input text'),
  7775. 'label' => array('for' => 'Contact1Name'),
  7776. 'Name',
  7777. '/label',
  7778. 'input' => array(
  7779. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  7780. 'id' => 'Contact1Name', 'maxlength' => '255'
  7781. ),
  7782. '/div'
  7783. );
  7784. $this->assertTags($result, $expected);
  7785. $result = $this->Form->input('Contact.1.id');
  7786. $this->assertTags($result, array(
  7787. 'input' => array(
  7788. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7789. 'id' => 'Contact1Id'
  7790. )
  7791. ));
  7792. $result = $this->Form->input("Model.1.name");
  7793. $expected = array(
  7794. 'div' => array('class' => 'input text'),
  7795. 'label' => array('for' => 'Model1Name'),
  7796. 'Name',
  7797. '/label',
  7798. 'input' => array(
  7799. 'type' => 'text', 'name' => 'data[Model][1][name]',
  7800. 'id' => 'Model1Name'
  7801. ),
  7802. '/div'
  7803. );
  7804. $this->assertTags($result, $expected);
  7805. }
  7806. /**
  7807. * testFormEnd method
  7808. *
  7809. * @return void
  7810. */
  7811. public function testFormEnd() {
  7812. $this->assertEquals('</form>', $this->Form->end());
  7813. $result = $this->Form->end('');
  7814. $expected = array(
  7815. 'div' => array('class' => 'submit'),
  7816. 'input' => array('type' => 'submit', 'value' => ''),
  7817. '/div',
  7818. '/form'
  7819. );
  7820. $this->assertTags($result, $expected);
  7821. $result = $this->Form->end(array('label' => ''));
  7822. $expected = array(
  7823. 'div' => array('class' => 'submit'),
  7824. 'input' => array('type' => 'submit', 'value' => ''),
  7825. '/div',
  7826. '/form'
  7827. );
  7828. $this->assertTags($result, $expected);
  7829. $result = $this->Form->end('save');
  7830. $expected = array(
  7831. 'div' => array('class' => 'submit'),
  7832. 'input' => array('type' => 'submit', 'value' => 'save'),
  7833. '/div',
  7834. '/form'
  7835. );
  7836. $this->assertTags($result, $expected);
  7837. $result = $this->Form->end(array('label' => 'save'));
  7838. $expected = array(
  7839. 'div' => array('class' => 'submit'),
  7840. 'input' => array('type' => 'submit', 'value' => 'save'),
  7841. '/div',
  7842. '/form'
  7843. );
  7844. $this->assertTags($result, $expected);
  7845. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7846. $expected = array(
  7847. 'div' => array('class' => 'submit'),
  7848. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7849. '/div',
  7850. '/form'
  7851. );
  7852. $this->assertTags($result, $expected);
  7853. $result = $this->Form->end(array('name' => 'Whatever'));
  7854. $expected = array(
  7855. 'div' => array('class' => 'submit'),
  7856. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7857. '/div',
  7858. '/form'
  7859. );
  7860. $this->assertTags($result, $expected);
  7861. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7862. $expected = array(
  7863. 'div' => array('class' => 'good'),
  7864. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7865. '/div',
  7866. '/form'
  7867. );
  7868. $this->assertTags($result, $expected);
  7869. $result = $this->Form->end(array(
  7870. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7871. ));
  7872. $expected = array(
  7873. 'div' => array('class' => 'good'),
  7874. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7875. '/div',
  7876. '/form'
  7877. );
  7878. $this->assertTags($result, $expected);
  7879. }
  7880. /**
  7881. * testMultipleFormWithIdFields method
  7882. *
  7883. * @return void
  7884. */
  7885. public function testMultipleFormWithIdFields() {
  7886. $this->Form->create('UserForm');
  7887. $result = $this->Form->input('id');
  7888. $this->assertTags($result, array('input' => array(
  7889. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7890. )));
  7891. $result = $this->Form->input('ValidateItem.id');
  7892. $this->assertTags($result, array('input' => array(
  7893. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7894. 'id' => 'ValidateItemId'
  7895. )));
  7896. $result = $this->Form->input('ValidateUser.id');
  7897. $this->assertTags($result, array('input' => array(
  7898. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7899. 'id' => 'ValidateUserId'
  7900. )));
  7901. }
  7902. /**
  7903. * testDbLessModel method
  7904. *
  7905. * @return void
  7906. */
  7907. public function testDbLessModel() {
  7908. $this->Form->create('TestMail');
  7909. $result = $this->Form->input('name');
  7910. $expected = array(
  7911. 'div' => array('class' => 'input text'),
  7912. 'label' => array('for' => 'TestMailName'),
  7913. 'Name',
  7914. '/label',
  7915. 'input' => array(
  7916. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7917. 'id' => 'TestMailName'
  7918. ),
  7919. '/div'
  7920. );
  7921. $this->assertTags($result, $expected);
  7922. ClassRegistry::init('TestMail');
  7923. $this->Form->create('TestMail');
  7924. $result = $this->Form->input('name');
  7925. $expected = array(
  7926. 'div' => array('class' => 'input text'),
  7927. 'label' => array('for' => 'TestMailName'),
  7928. 'Name',
  7929. '/label',
  7930. 'input' => array(
  7931. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7932. 'id' => 'TestMailName'
  7933. ),
  7934. '/div'
  7935. );
  7936. $this->assertTags($result, $expected);
  7937. }
  7938. /**
  7939. * testBrokenness method
  7940. *
  7941. * @return void
  7942. */
  7943. public function testBrokenness() {
  7944. /*
  7945. * #4 This test has two parents and four children. By default (as of r7117) both
  7946. * parents are show but the first parent is missing a child. This is the inconsistency
  7947. * in the default behaviour - one parent has all children, the other does not - dependent
  7948. * on the data values.
  7949. */
  7950. $result = $this->Form->select('Model.field', array(
  7951. 'Fred' => array(
  7952. 'freds_son_1' => 'Fred',
  7953. 'freds_son_2' => 'Freddie'
  7954. ),
  7955. 'Bert' => array(
  7956. 'berts_son_1' => 'Albert',
  7957. 'berts_son_2' => 'Bertie')
  7958. ),
  7959. array('showParents' => true, 'empty' => false)
  7960. );
  7961. $expected = array(
  7962. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7963. array('optgroup' => array('label' => 'Fred')),
  7964. array('option' => array('value' => 'freds_son_1')),
  7965. 'Fred',
  7966. '/option',
  7967. array('option' => array('value' => 'freds_son_2')),
  7968. 'Freddie',
  7969. '/option',
  7970. '/optgroup',
  7971. array('optgroup' => array('label' => 'Bert')),
  7972. array('option' => array('value' => 'berts_son_1')),
  7973. 'Albert',
  7974. '/option',
  7975. array('option' => array('value' => 'berts_son_2')),
  7976. 'Bertie',
  7977. '/option',
  7978. '/optgroup',
  7979. '/select'
  7980. );
  7981. $this->assertTags($result, $expected);
  7982. /*
  7983. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7984. * changed, so we should expect the same select list data, just with a different name
  7985. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7986. * This is where data corruption can occur, because when a select value is missing from
  7987. * a list a form will substitute the first value in the list - without the user knowing.
  7988. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7989. * affect the availability of 3 => 'Three' as a valid option.
  7990. */
  7991. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7992. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7993. ));
  7994. $result = $this->Form->select(
  7995. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7996. );
  7997. $expected = array(
  7998. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7999. array('option' => array('value' => 1)),
  8000. 'One',
  8001. '/option',
  8002. array('option' => array('value' => 2)),
  8003. 'Two',
  8004. '/option',
  8005. array('optgroup' => array('label' => 'Three')),
  8006. array('option' => array('value' => 3)),
  8007. 'Three',
  8008. '/option',
  8009. array('option' => array('value' => 4)),
  8010. 'Four',
  8011. '/option',
  8012. array('option' => array('value' => 5)),
  8013. 'Five',
  8014. '/option',
  8015. '/optgroup',
  8016. '/select'
  8017. );
  8018. $this->assertTags($result, $expected);
  8019. }
  8020. /**
  8021. * Test the generation of fields for a multi record form.
  8022. *
  8023. * @return void
  8024. */
  8025. public function testMultiRecordForm() {
  8026. $this->Form->create('ValidateProfile');
  8027. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8028. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8029. $expected = array(
  8030. 'div' => array('class' => 'input textarea'),
  8031. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8032. 'Name',
  8033. '/label',
  8034. 'textarea' => array(
  8035. 'id' => 'ValidateProfile1ValidateItem2Name',
  8036. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  8037. 'cols' => 30,
  8038. 'rows' => 6
  8039. ),
  8040. 'Value',
  8041. '/textarea',
  8042. '/div'
  8043. );
  8044. $this->assertTags($result, $expected);
  8045. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8046. $expected = array(
  8047. 'div' => array('class' => 'input date'),
  8048. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8049. 'Created',
  8050. '/label',
  8051. array('select' => array(
  8052. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  8053. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8054. )
  8055. ),
  8056. array('option' => array('value' => '')), '/option',
  8057. $this->dateRegex['monthsRegex'],
  8058. '/select', '-',
  8059. array('select' => array(
  8060. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  8061. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8062. )
  8063. ),
  8064. array('option' => array('value' => '')), '/option',
  8065. $this->dateRegex['daysRegex'],
  8066. '/select', '-',
  8067. array('select' => array(
  8068. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  8069. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8070. )
  8071. ),
  8072. array('option' => array('value' => '')), '/option',
  8073. $this->dateRegex['yearsRegex'],
  8074. '/select',
  8075. '/div'
  8076. );
  8077. $this->assertTags($result, $expected);
  8078. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8079. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8080. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8081. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8082. $expected = array(
  8083. 'div' => array('class' => 'input select error'),
  8084. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8085. 'Profile',
  8086. '/label',
  8087. 'select' => array(
  8088. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  8089. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8090. 'class' => 'form-error'
  8091. ),
  8092. '/select',
  8093. array('div' => array('class' => 'error-message')),
  8094. 'Error',
  8095. '/div',
  8096. '/div'
  8097. );
  8098. $this->assertTags($result, $expected);
  8099. }
  8100. /**
  8101. * test the correct display of multi-record form validation errors.
  8102. *
  8103. * @return void
  8104. */
  8105. public function testMultiRecordFormValidationErrors() {
  8106. $this->Form->create('ValidateProfile');
  8107. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8108. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8109. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8110. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8111. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8112. $result = $this->Form->error('ValidateProfile.2.city');
  8113. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8114. $result = $this->Form->error('2.city');
  8115. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8116. }
  8117. /**
  8118. * test the correct display of multi-record form validation errors.
  8119. *
  8120. * @return void
  8121. */
  8122. public function testSaveManyRecordFormValidationErrors() {
  8123. $this->Form->create('ValidateUser');
  8124. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8125. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8126. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8127. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8128. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8129. $result = $this->Form->error('ValidateUser.0.city');
  8130. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8131. }
  8132. /**
  8133. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8134. *
  8135. * @return void
  8136. */
  8137. public function testInputTemplate() {
  8138. $result = $this->Form->input('Contact.email', array(
  8139. 'type' => 'text', 'format' => array('input')
  8140. ));
  8141. $expected = array(
  8142. 'div' => array('class' => 'input text'),
  8143. 'input' => array(
  8144. 'type' => 'text', 'name' => 'data[Contact][email]',
  8145. 'id' => 'ContactEmail'
  8146. ),
  8147. '/div'
  8148. );
  8149. $this->assertTags($result, $expected);
  8150. $result = $this->Form->input('Contact.email', array(
  8151. 'type' => 'text', 'format' => array('input', 'label'),
  8152. 'label' => '<em>Email (required)</em>'
  8153. ));
  8154. $expected = array(
  8155. 'div' => array('class' => 'input text'),
  8156. array('input' => array(
  8157. 'type' => 'text', 'name' => 'data[Contact][email]',
  8158. 'id' => 'ContactEmail'
  8159. )),
  8160. 'label' => array('for' => 'ContactEmail'),
  8161. 'em' => array(),
  8162. 'Email (required)',
  8163. '/em',
  8164. '/label',
  8165. '/div'
  8166. );
  8167. $this->assertTags($result, $expected);
  8168. $result = $this->Form->input('Contact.email', array(
  8169. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8170. 'between' => '<div>Something in the middle</div>',
  8171. 'after' => '<span>Some text at the end</span>'
  8172. ));
  8173. $expected = array(
  8174. 'div' => array('class' => 'input text'),
  8175. array('input' => array(
  8176. 'type' => 'text', 'name' => 'data[Contact][email]',
  8177. 'id' => 'ContactEmail'
  8178. )),
  8179. array('div' => array()),
  8180. 'Something in the middle',
  8181. '/div',
  8182. 'label' => array('for' => 'ContactEmail'),
  8183. 'Email',
  8184. '/label',
  8185. 'span' => array(),
  8186. 'Some text at the end',
  8187. '/span',
  8188. '/div'
  8189. );
  8190. $this->assertTags($result, $expected);
  8191. $result = $this->Form->input('Contact.method', array(
  8192. 'type' => 'radio',
  8193. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8194. 'between' => 'I am between',
  8195. ));
  8196. $expected = array(
  8197. 'div' => array('class' => 'input radio'),
  8198. 'fieldset' => array(),
  8199. 'legend' => array(),
  8200. 'Method',
  8201. '/legend',
  8202. 'I am between',
  8203. 'input' => array(
  8204. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  8205. 'value' => '', 'id' => 'ContactMethod_'
  8206. ),
  8207. array('input' => array(
  8208. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8209. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8210. )),
  8211. array('label' => array('for' => 'ContactMethodEmail')),
  8212. 'Email',
  8213. '/label',
  8214. array('input' => array(
  8215. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8216. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8217. )),
  8218. array('label' => array('for' => 'ContactMethodPigeon')),
  8219. 'Pigeon',
  8220. '/label',
  8221. '/fieldset',
  8222. '/div',
  8223. );
  8224. $this->assertTags($result, $expected);
  8225. }
  8226. /**
  8227. * test that some html5 inputs + FormHelper::__call() work
  8228. *
  8229. * @return void
  8230. */
  8231. public function testHtml5Inputs() {
  8232. $result = $this->Form->email('User.email');
  8233. $expected = array(
  8234. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  8235. );
  8236. $this->assertTags($result, $expected);
  8237. $result = $this->Form->search('User.query');
  8238. $expected = array(
  8239. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  8240. );
  8241. $this->assertTags($result, $expected);
  8242. $result = $this->Form->search('User.query', array('value' => 'test'));
  8243. $expected = array(
  8244. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8245. );
  8246. $this->assertTags($result, $expected);
  8247. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8248. $expected = array(
  8249. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8250. );
  8251. $this->assertTags($result, $expected);
  8252. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8253. $expected = array(
  8254. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8255. );
  8256. $this->assertTags($result, $expected);
  8257. }
  8258. /**
  8259. *
  8260. * @expectedException CakeException
  8261. * @return void
  8262. */
  8263. public function testHtml5InputException() {
  8264. $this->Form->email();
  8265. }
  8266. /**
  8267. * Tests that a model can be loaded from the model names passed in the request object
  8268. *
  8269. * @return void
  8270. */
  8271. public function testIntrospectModelFromRequest() {
  8272. $this->loadFixtures('Post');
  8273. App::build(array(
  8274. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  8275. ));
  8276. CakePlugin::load('TestPlugin');
  8277. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  8278. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8279. $this->Form->create('TestPluginPost');
  8280. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8281. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8282. CakePlugin::unload();
  8283. App::build();
  8284. }
  8285. /**
  8286. * Tests that it is possible to set the validation errors directly in the helper for a field
  8287. *
  8288. * @return void
  8289. */
  8290. public function testCustomValidationErrors() {
  8291. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8292. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8293. $this->assertEquals('Badness!', $result);
  8294. }
  8295. /**
  8296. * Tests that the 'on' key validates as expected on create
  8297. *
  8298. * @return void
  8299. */
  8300. public function testRequiredOnCreate() {
  8301. $this->Form->create('Contact');
  8302. $result = $this->Form->input('Contact.imrequiredonupdate');
  8303. $expected = array(
  8304. 'div' => array('class' => 'input text'),
  8305. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8306. 'Imrequiredonupdate',
  8307. '/label',
  8308. 'input' => array(
  8309. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8310. 'id' => 'ContactImrequiredonupdate'
  8311. ),
  8312. '/div'
  8313. );
  8314. $this->assertTags($result, $expected);
  8315. $result = $this->Form->input('Contact.imrequiredoncreate');
  8316. $expected = array(
  8317. 'div' => array('class' => 'input text required'),
  8318. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8319. 'Imrequiredoncreate',
  8320. '/label',
  8321. 'input' => array(
  8322. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8323. 'id' => 'ContactImrequiredoncreate',
  8324. 'required' => 'required'
  8325. ),
  8326. '/div'
  8327. );
  8328. $this->assertTags($result, $expected);
  8329. $result = $this->Form->input('Contact.imrequiredonboth');
  8330. $expected = array(
  8331. 'div' => array('class' => 'input text required'),
  8332. 'label' => array('for' => 'ContactImrequiredonboth'),
  8333. 'Imrequiredonboth',
  8334. '/label',
  8335. 'input' => array(
  8336. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8337. 'id' => 'ContactImrequiredonboth',
  8338. 'required' => 'required'
  8339. ),
  8340. '/div'
  8341. );
  8342. $this->assertTags($result, $expected);
  8343. $this->Form->inputDefaults(array('required' => false));
  8344. $result = $this->Form->input('Contact.imrequired');
  8345. $expected = array(
  8346. 'div' => array('class' => 'input text'),
  8347. 'label' => array('for' => 'ContactImrequired'),
  8348. 'Imrequired',
  8349. '/label',
  8350. 'input' => array(
  8351. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8352. 'id' => 'ContactImrequired'
  8353. ),
  8354. '/div'
  8355. );
  8356. $this->assertTags($result, $expected);
  8357. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8358. $this->assertTags($result, $expected);
  8359. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8360. $expected = array(
  8361. 'div' => array('class' => 'input text required'),
  8362. 'label' => array('for' => 'ContactImrequired'),
  8363. 'Imrequired',
  8364. '/label',
  8365. 'input' => array(
  8366. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8367. 'id' => 'ContactImrequired'
  8368. ),
  8369. '/div'
  8370. );
  8371. $this->assertTags($result, $expected);
  8372. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8373. $this->assertTags($result, $expected);
  8374. }
  8375. /**
  8376. * Tests that the 'on' key validates as expected on update
  8377. *
  8378. * @return void
  8379. */
  8380. public function testRequiredOnUpdate() {
  8381. $this->Form->request->data['Contact']['id'] = 1;
  8382. $this->Form->create('Contact');
  8383. $result = $this->Form->input('Contact.imrequiredonupdate');
  8384. $expected = array(
  8385. 'div' => array('class' => 'input text required'),
  8386. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8387. 'Imrequiredonupdate',
  8388. '/label',
  8389. 'input' => array(
  8390. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8391. 'id' => 'ContactImrequiredonupdate',
  8392. 'required' => 'required'
  8393. ),
  8394. '/div'
  8395. );
  8396. $this->assertTags($result, $expected);
  8397. $result = $this->Form->input('Contact.imrequiredoncreate');
  8398. $expected = array(
  8399. 'div' => array('class' => 'input text'),
  8400. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8401. 'Imrequiredoncreate',
  8402. '/label',
  8403. 'input' => array(
  8404. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8405. 'id' => 'ContactImrequiredoncreate'
  8406. ),
  8407. '/div'
  8408. );
  8409. $this->assertTags($result, $expected);
  8410. $result = $this->Form->input('Contact.imrequiredonboth');
  8411. $expected = array(
  8412. 'div' => array('class' => 'input text required'),
  8413. 'label' => array('for' => 'ContactImrequiredonboth'),
  8414. 'Imrequiredonboth',
  8415. '/label',
  8416. 'input' => array(
  8417. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8418. 'id' => 'ContactImrequiredonboth',
  8419. 'required' => 'required'
  8420. ),
  8421. '/div'
  8422. );
  8423. $this->assertTags($result, $expected);
  8424. $result = $this->Form->input('Contact.imrequired');
  8425. $expected = array(
  8426. 'div' => array('class' => 'input text required'),
  8427. 'label' => array('for' => 'ContactImrequired'),
  8428. 'Imrequired',
  8429. '/label',
  8430. 'input' => array(
  8431. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8432. 'id' => 'ContactImrequired',
  8433. 'required' => 'required'
  8434. ),
  8435. '/div'
  8436. );
  8437. $this->assertTags($result, $expected);
  8438. }
  8439. /**
  8440. * Test inputDefaults setter and getter
  8441. *
  8442. * @return void
  8443. */
  8444. public function testInputDefaults() {
  8445. $this->Form->create('Contact');
  8446. $this->Form->inputDefaults(array(
  8447. 'label' => false,
  8448. 'div' => array(
  8449. 'style' => 'color: #000;'
  8450. )
  8451. ));
  8452. $result = $this->Form->input('Contact.field1');
  8453. $expected = array(
  8454. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8455. 'input' => array(
  8456. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8457. 'id' => 'ContactField1'
  8458. ),
  8459. '/div'
  8460. );
  8461. $this->assertTags($result, $expected);
  8462. $this->Form->inputDefaults(array(
  8463. 'div' => false,
  8464. 'label' => 'Label',
  8465. ));
  8466. $result = $this->Form->input('Contact.field1');
  8467. $expected = array(
  8468. 'label' => array('for' => 'ContactField1'),
  8469. 'Label',
  8470. '/label',
  8471. 'input' => array(
  8472. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8473. 'id' => 'ContactField1'
  8474. ),
  8475. );
  8476. $this->assertTags($result, $expected);
  8477. $this->Form->inputDefaults(array(
  8478. 'label' => false,
  8479. ), true);
  8480. $result = $this->Form->input('Contact.field1');
  8481. $expected = array(
  8482. 'input' => array(
  8483. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8484. 'id' => 'ContactField1'
  8485. ),
  8486. );
  8487. $this->assertTags($result, $expected);
  8488. $result = $this->Form->inputDefaults();
  8489. $expected = array(
  8490. 'div' => false,
  8491. 'label' => false,
  8492. );
  8493. $this->assertEqual($result, $expected);
  8494. }
  8495. }