FormHelperTest.php 254 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083
  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 2005-2012, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ClassRegistry', 'Utility');
  20. App::uses('Controller', 'Controller');
  21. App::uses('View', 'View');
  22. App::uses('Model', 'Model');
  23. App::uses('Security', 'Utility');
  24. App::uses('CakeRequest', 'Network');
  25. App::uses('HtmlHelper', 'View/Helper');
  26. App::uses('FormHelper', 'View/Helper');
  27. App::uses('Router', 'Routing');
  28. /**
  29. * ContactTestController class
  30. *
  31. * @package cake
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * name property
  37. *
  38. * @var string 'ContactTest'
  39. */
  40. public $name = 'ContactTest';
  41. /**
  42. * uses property
  43. *
  44. * @var mixed null
  45. */
  46. public $uses = null;
  47. }
  48. /**
  49. * Contact class
  50. *
  51. * @package cake
  52. * @package Cake.Test.Case.View.Helper
  53. */
  54. class Contact extends CakeTestModel {
  55. /**
  56. * primaryKey property
  57. *
  58. * @var string 'id'
  59. */
  60. public $primaryKey = 'id';
  61. /**
  62. * useTable property
  63. *
  64. * @var bool false
  65. */
  66. public $useTable = false;
  67. /**
  68. * name property
  69. *
  70. * @var string 'Contact'
  71. */
  72. public $name = 'Contact';
  73. /**
  74. * Default schema
  75. *
  76. * @var array
  77. */
  78. protected $_schema = array(
  79. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  80. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  82. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  83. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  84. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  85. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  86. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  87. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  88. );
  89. /**
  90. * validate property
  91. *
  92. * @var array
  93. */
  94. public $validate = array(
  95. 'non_existing' => array(),
  96. 'idontexist' => array(),
  97. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  98. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  99. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  100. 'imrequiredonboth' => array(
  101. 'required' => array('rule' => 'alphaNumeric'),
  102. ),
  103. 'string_required' => 'notEmpty',
  104. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  105. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  106. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  107. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  108. 'imalsonotrequired' => array(
  109. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  110. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  111. ),
  112. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  113. );
  114. /**
  115. * schema method
  116. *
  117. * @return void
  118. */
  119. public function setSchema($schema) {
  120. $this->_schema = $schema;
  121. }
  122. /**
  123. * hasAndBelongsToMany property
  124. *
  125. * @var array
  126. */
  127. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  128. /**
  129. * hasAndBelongsToMany property
  130. *
  131. * @var array
  132. */
  133. public $belongsTo = array('User' => array('className' => 'UserForm'));
  134. }
  135. /**
  136. * ContactTagsContact class
  137. *
  138. * @package cake
  139. * @package Cake.Test.Case.View.Helper
  140. */
  141. class ContactTagsContact extends CakeTestModel {
  142. /**
  143. * useTable property
  144. *
  145. * @var bool false
  146. */
  147. public $useTable = false;
  148. /**
  149. * name property
  150. *
  151. * @var string 'Contact'
  152. */
  153. public $name = 'ContactTagsContact';
  154. /**
  155. * Default schema
  156. *
  157. * @var array
  158. */
  159. protected $_schema = array(
  160. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  161. 'contact_tag_id' => array(
  162. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  163. )
  164. );
  165. /**
  166. * schema method
  167. *
  168. * @return void
  169. */
  170. public function setSchema($schema) {
  171. $this->_schema = $schema;
  172. }
  173. }
  174. /**
  175. * ContactNonStandardPk class
  176. *
  177. * @package cake
  178. * @package Cake.Test.Case.View.Helper
  179. */
  180. class ContactNonStandardPk extends Contact {
  181. /**
  182. * primaryKey property
  183. *
  184. * @var string 'pk'
  185. */
  186. public $primaryKey = 'pk';
  187. /**
  188. * name property
  189. *
  190. * @var string 'ContactNonStandardPk'
  191. */
  192. public $name = 'ContactNonStandardPk';
  193. /**
  194. * schema method
  195. *
  196. * @return void
  197. */
  198. public function schema($field = false) {
  199. $this->_schema = parent::schema();
  200. $this->_schema['pk'] = $this->_schema['id'];
  201. unset($this->_schema['id']);
  202. return $this->_schema;
  203. }
  204. }
  205. /**
  206. * ContactTag class
  207. *
  208. * @package cake
  209. * @package Cake.Test.Case.View.Helper
  210. */
  211. class ContactTag extends Model {
  212. /**
  213. * useTable property
  214. *
  215. * @var bool false
  216. */
  217. public $useTable = false;
  218. /**
  219. * schema definition
  220. *
  221. * @var array
  222. */
  223. protected $_schema = array(
  224. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  225. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  226. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  227. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  228. );
  229. }
  230. /**
  231. * UserForm class
  232. *
  233. * @package cake
  234. * @package Cake.Test.Case.View.Helper
  235. */
  236. class UserForm extends CakeTestModel {
  237. /**
  238. * useTable property
  239. *
  240. * @var bool false
  241. */
  242. public $useTable = false;
  243. /**
  244. * primaryKey property
  245. *
  246. * @var string 'id'
  247. */
  248. public $primaryKey = 'id';
  249. /**
  250. * name property
  251. *
  252. * @var string 'UserForm'
  253. */
  254. public $name = 'UserForm';
  255. /**
  256. * hasMany property
  257. *
  258. * @var array
  259. */
  260. public $hasMany = array(
  261. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  262. ));
  263. /**
  264. * schema definition
  265. *
  266. * @var array
  267. */
  268. protected $_schema = array(
  269. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  270. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  271. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  272. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  273. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  274. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  275. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  276. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  277. );
  278. }
  279. /**
  280. * OpenidUrl class
  281. *
  282. * @package cake
  283. * @package Cake.Test.Case.View.Helper
  284. */
  285. class OpenidUrl extends CakeTestModel {
  286. /**
  287. * useTable property
  288. *
  289. * @var bool false
  290. */
  291. public $useTable = false;
  292. /**
  293. * primaryKey property
  294. *
  295. * @var string 'id'
  296. */
  297. public $primaryKey = 'id';
  298. /**
  299. * name property
  300. *
  301. * @var string 'OpenidUrl'
  302. */
  303. public $name = 'OpenidUrl';
  304. /**
  305. * belongsTo property
  306. *
  307. * @var array
  308. */
  309. public $belongsTo = array('UserForm' => array(
  310. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  311. ));
  312. /**
  313. * validate property
  314. *
  315. * @var array
  316. */
  317. public $validate = array('openid_not_registered' => array());
  318. /**
  319. * schema method
  320. *
  321. * @var array
  322. */
  323. protected $_schema = array(
  324. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  325. 'user_form_id' => array(
  326. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  327. ),
  328. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  329. );
  330. /**
  331. * beforeValidate method
  332. *
  333. * @return void
  334. */
  335. public function beforeValidate($options = array()) {
  336. $this->invalidate('openid_not_registered');
  337. return true;
  338. }
  339. }
  340. /**
  341. * ValidateUser class
  342. *
  343. * @package cake
  344. * @package Cake.Test.Case.View.Helper
  345. */
  346. class ValidateUser extends CakeTestModel {
  347. /**
  348. * primaryKey property
  349. *
  350. * @var string 'id'
  351. */
  352. public $primaryKey = 'id';
  353. /**
  354. * useTable property
  355. *
  356. * @var bool false
  357. */
  358. public $useTable = false;
  359. /**
  360. * name property
  361. *
  362. * @var string 'ValidateUser'
  363. */
  364. public $name = 'ValidateUser';
  365. /**
  366. * hasOne property
  367. *
  368. * @var array
  369. */
  370. public $hasOne = array('ValidateProfile' => array(
  371. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  372. ));
  373. /**
  374. * schema method
  375. *
  376. * @var array
  377. */
  378. protected $_schema = array(
  379. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  380. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  381. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  382. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  383. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  384. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  385. );
  386. /**
  387. * beforeValidate method
  388. *
  389. * @return void
  390. */
  391. public function beforeValidate($options = array()) {
  392. $this->invalidate('email');
  393. return false;
  394. }
  395. }
  396. /**
  397. * ValidateProfile class
  398. *
  399. * @package cake
  400. * @package Cake.Test.Case.View.Helper
  401. */
  402. class ValidateProfile extends CakeTestModel {
  403. /**
  404. * primaryKey property
  405. *
  406. * @var string 'id'
  407. */
  408. public $primaryKey = 'id';
  409. /**
  410. * useTable property
  411. *
  412. * @var bool false
  413. */
  414. public $useTable = false;
  415. /**
  416. * schema property
  417. *
  418. * @var array
  419. */
  420. protected $_schema = array(
  421. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  422. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  423. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  424. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  425. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  426. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  427. );
  428. /**
  429. * name property
  430. *
  431. * @var string 'ValidateProfile'
  432. */
  433. public $name = 'ValidateProfile';
  434. /**
  435. * hasOne property
  436. *
  437. * @var array
  438. */
  439. public $hasOne = array('ValidateItem' => array(
  440. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  441. ));
  442. /**
  443. * belongsTo property
  444. *
  445. * @var array
  446. */
  447. public $belongsTo = array('ValidateUser' => array(
  448. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  449. ));
  450. /**
  451. * beforeValidate method
  452. *
  453. * @return void
  454. */
  455. public function beforeValidate($options = array()) {
  456. $this->invalidate('full_name');
  457. $this->invalidate('city');
  458. return false;
  459. }
  460. }
  461. /**
  462. * ValidateItem class
  463. *
  464. * @package cake
  465. * @package Cake.Test.Case.View.Helper
  466. */
  467. class ValidateItem extends CakeTestModel {
  468. /**
  469. * primaryKey property
  470. *
  471. * @var string 'id'
  472. */
  473. public $primaryKey = 'id';
  474. /**
  475. * useTable property
  476. *
  477. * @var bool false
  478. */
  479. public $useTable = false;
  480. /**
  481. * name property
  482. *
  483. * @var string 'ValidateItem'
  484. */
  485. public $name = 'ValidateItem';
  486. /**
  487. * schema property
  488. *
  489. * @var array
  490. */
  491. protected $_schema = array(
  492. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  493. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  494. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  495. 'description' => array(
  496. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  497. ),
  498. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  499. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  500. );
  501. /**
  502. * belongsTo property
  503. *
  504. * @var array
  505. */
  506. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  507. /**
  508. * beforeValidate method
  509. *
  510. * @return void
  511. */
  512. public function beforeValidate($options = array()) {
  513. $this->invalidate('description');
  514. return false;
  515. }
  516. }
  517. /**
  518. * TestMail class
  519. *
  520. * @package cake
  521. * @package Cake.Test.Case.View.Helper
  522. */
  523. class TestMail extends CakeTestModel {
  524. /**
  525. * primaryKey property
  526. *
  527. * @var string 'id'
  528. */
  529. public $primaryKey = 'id';
  530. /**
  531. * useTable property
  532. *
  533. * @var bool false
  534. */
  535. public $useTable = false;
  536. /**
  537. * name property
  538. *
  539. * @var string 'TestMail'
  540. */
  541. public $name = 'TestMail';
  542. }
  543. /**
  544. * FormHelperTest class
  545. *
  546. * @package cake
  547. * @subpackage Cake.Test.Case.View.Helper
  548. * @property FormHelper $Form
  549. */
  550. class FormHelperTest extends CakeTestCase {
  551. /**
  552. * Fixtures to be used
  553. *
  554. * @var array
  555. */
  556. public $fixtures = array('core.post');
  557. /**
  558. * Do not load the fixtures by default
  559. *
  560. * @var boolean
  561. */
  562. public $autoFixtures = false;
  563. /**
  564. * setUp method
  565. *
  566. * @return void
  567. */
  568. public function setUp() {
  569. parent::setUp();
  570. Configure::write('App.base', '');
  571. $this->Controller = new ContactTestController();
  572. $this->View = new View($this->Controller);
  573. $this->Form = new FormHelper($this->View);
  574. $this->Form->Html = new HtmlHelper($this->View);
  575. $this->Form->request = new CakeRequest('contacts/add', false);
  576. $this->Form->request->here = '/contacts/add';
  577. $this->Form->request['action'] = 'add';
  578. $this->Form->request->webroot = '';
  579. $this->Form->request->base = '';
  580. ClassRegistry::addObject('Contact', new Contact());
  581. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  582. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  583. ClassRegistry::addObject('User', new UserForm());
  584. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  585. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  586. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  587. $this->oldSalt = Configure::read('Security.salt');
  588. $this->dateRegex = array(
  589. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  590. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  591. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  592. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  593. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  594. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  595. );
  596. Configure::write('Security.salt', 'foo!');
  597. }
  598. /**
  599. * tearDown method
  600. *
  601. * @return void
  602. */
  603. public function tearDown() {
  604. parent::tearDown();
  605. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  606. Configure::write('Security.salt', $this->oldSalt);
  607. }
  608. /**
  609. * testFormCreateWithSecurity method
  610. *
  611. * Test form->create() with security key.
  612. *
  613. * @return void
  614. */
  615. public function testCreateWithSecurity() {
  616. $this->Form->request['_Token'] = array('key' => 'testKey');
  617. $encoding = strtolower(Configure::read('App.encoding'));
  618. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  619. $expected = array(
  620. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  621. 'div' => array('style' => 'display:none;'),
  622. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  623. array('input' => array(
  624. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  625. )),
  626. '/div'
  627. );
  628. $this->assertTags($result, $expected);
  629. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  630. $expected['form']['id'] = 'MyForm';
  631. $this->assertTags($result, $expected);
  632. }
  633. /**
  634. * test that create() clears the fields property so it starts fresh
  635. *
  636. * @return void
  637. */
  638. public function testCreateClearingFields() {
  639. $this->Form->fields = array('model_id');
  640. $this->Form->create('Contact');
  641. $this->assertEquals(array(), $this->Form->fields);
  642. }
  643. /**
  644. * Tests form hash generation with model-less data
  645. *
  646. * @return void
  647. */
  648. public function testValidateHashNoModel() {
  649. $this->Form->request['_Token'] = array('key' => 'foo');
  650. $result = $this->Form->secure(array('anything'));
  651. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  652. }
  653. /**
  654. * Tests that models with identical field names get resolved properly
  655. *
  656. * @return void
  657. */
  658. public function testDuplicateFieldNameResolution() {
  659. $result = $this->Form->create('ValidateUser');
  660. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  661. $result = $this->Form->input('ValidateItem.name');
  662. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  663. $result = $this->Form->input('ValidateUser.name');
  664. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  665. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  666. $this->assertRegExp('/type="text"/', $result);
  667. $result = $this->Form->input('ValidateItem.name');
  668. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  669. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  670. $this->assertRegExp('/<textarea/', $result);
  671. $result = $this->Form->input('name');
  672. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  673. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  674. $this->assertRegExp('/type="text"/', $result);
  675. }
  676. /**
  677. * Tests that hidden fields generated for checkboxes don't get locked
  678. *
  679. * @return void
  680. */
  681. public function testNoCheckboxLocking() {
  682. $this->Form->request['_Token'] = array('key' => 'foo');
  683. $this->assertSame(array(), $this->Form->fields);
  684. $this->Form->checkbox('check', array('value' => '1'));
  685. $this->assertSame($this->Form->fields, array('check'));
  686. }
  687. /**
  688. * testFormSecurityFields method
  689. *
  690. * Test generation of secure form hash generation.
  691. *
  692. * @return void
  693. */
  694. public function testFormSecurityFields() {
  695. $key = 'testKey';
  696. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  697. $this->Form->request['_Token'] = array('key' => $key);
  698. $result = $this->Form->secure($fields);
  699. $expected = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  700. $expected .= ':' . 'Model.valid';
  701. $expected = array(
  702. 'div' => array('style' => 'display:none;'),
  703. array('input' => array(
  704. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  705. 'value' => urlencode($expected), 'id' => 'preg:/TokenFields\d+/'
  706. )),
  707. array('input' => array(
  708. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  709. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  710. )),
  711. '/div'
  712. );
  713. $this->assertTags($result, $expected);
  714. }
  715. /**
  716. * Tests correct generation of number fields for double and float fields
  717. *
  718. * @return void
  719. */
  720. public function testTextFieldGenerationForFloats() {
  721. $model = ClassRegistry::getObject('Contact');
  722. $model->setSchema(array('foo' => array(
  723. 'type' => 'float',
  724. 'null' => false,
  725. 'default' => null,
  726. 'length' => null
  727. )));
  728. $this->Form->create('Contact');
  729. $result = $this->Form->input('foo');
  730. $expected = array(
  731. 'div' => array('class' => 'input number'),
  732. 'label' => array('for' => 'ContactFoo'),
  733. 'Foo',
  734. '/label',
  735. array('input' => array(
  736. 'type' => 'number',
  737. 'name' => 'data[Contact][foo]',
  738. 'id' => 'ContactFoo',
  739. 'step' => 'any'
  740. )),
  741. '/div'
  742. );
  743. $this->assertTags($result, $expected);
  744. $result = $this->Form->input('foo', array('step' => 0.5));
  745. $expected = array(
  746. 'div' => array('class' => 'input number'),
  747. 'label' => array('for' => 'ContactFoo'),
  748. 'Foo',
  749. '/label',
  750. array('input' => array(
  751. 'type' => 'number',
  752. 'name' => 'data[Contact][foo]',
  753. 'id' => 'ContactFoo',
  754. 'step' => '0.5'
  755. )),
  756. '/div'
  757. );
  758. $this->assertTags($result, $expected);
  759. }
  760. /**
  761. * Tests correct generation of number fields for integer fields
  762. *
  763. * @access public
  764. * @return void
  765. */
  766. public function testTextFieldTypeNumberGenerationForIntegers() {
  767. $model = ClassRegistry::getObject('Contact');
  768. $model->setSchema(array('foo' => array(
  769. 'type' => 'integer',
  770. 'null' => false,
  771. 'default' => null,
  772. 'length' => null
  773. )));
  774. $this->Form->create('Contact');
  775. $result = $this->Form->input('foo');
  776. $expected = array(
  777. 'div' => array('class' => 'input number'),
  778. 'label' => array('for' => 'ContactFoo'),
  779. 'Foo',
  780. '/label',
  781. array('input' => array(
  782. 'type' => 'number', 'name' => 'data[Contact][foo]',
  783. 'id' => 'ContactFoo'
  784. )),
  785. '/div'
  786. );
  787. $this->assertTags($result, $expected);
  788. }
  789. /**
  790. * testFormSecurityMultipleFields method
  791. *
  792. * Test secure() with multiple row form. Ensure hash is correct.
  793. *
  794. * @return void
  795. */
  796. public function testFormSecurityMultipleFields() {
  797. $key = 'testKey';
  798. $fields = array(
  799. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  800. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  801. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  802. );
  803. $this->Form->request['_Token'] = array('key' => $key);
  804. $result = $this->Form->secure($fields);
  805. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  806. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  807. $expected = array(
  808. 'div' => array('style' => 'display:none;'),
  809. array('input' => array(
  810. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  811. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  812. )),
  813. array('input' => array(
  814. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  815. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  816. )),
  817. '/div'
  818. );
  819. $this->assertTags($result, $expected);
  820. }
  821. /**
  822. * testFormSecurityMultipleSubmitButtons
  823. *
  824. * test form submit generation and ensure that _Token is only created on end()
  825. *
  826. * @return void
  827. */
  828. public function testFormSecurityMultipleSubmitButtons() {
  829. $key = 'testKey';
  830. $this->Form->request['_Token'] = array('key' => $key);
  831. $this->Form->create('Addresses');
  832. $this->Form->input('Address.title');
  833. $this->Form->input('Address.first_name');
  834. $result = $this->Form->submit('Save', array('name' => 'save'));
  835. $expected = array(
  836. 'div' => array('class' => 'submit'),
  837. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  838. '/div',
  839. );
  840. $this->assertTags($result, $expected);
  841. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  842. $expected = array(
  843. 'div' => array('class' => 'submit'),
  844. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  845. '/div',
  846. );
  847. $this->assertTags($result, $expected);
  848. $result = $this->Form->end(null);
  849. $expected = array(
  850. 'div' => array('style' => 'display:none;'),
  851. array('input' => array(
  852. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  853. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  854. )),
  855. array('input' => array(
  856. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  857. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  858. )),
  859. '/div'
  860. );
  861. $this->assertTags($result, $expected);
  862. }
  863. /**
  864. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  865. *
  866. * @return void
  867. */
  868. public function testSecurityButtonNestedNamed() {
  869. $key = 'testKey';
  870. $this->Form->request['_Token'] = array('key' => $key);
  871. $this->Form->create('Addresses');
  872. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  873. $result = $this->Form->unlockField();
  874. $this->assertEquals(array('Address.button'), $result);
  875. }
  876. /**
  877. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  878. *
  879. * @return void
  880. */
  881. public function testSecuritySubmitNestedNamed() {
  882. $key = 'testKey';
  883. $this->Form->request['_Token'] = array('key' => $key);
  884. $this->Form->create('Addresses');
  885. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  886. $result = $this->Form->unlockField();
  887. $this->assertEquals(array('Address.button'), $result);
  888. }
  889. /**
  890. * Test that the correct fields are unlocked for image submits with no names.
  891. *
  892. * @return void
  893. */
  894. public function testSecuritySubmitImageNoName() {
  895. $key = 'testKey';
  896. $this->Form->request['_Token'] = array('key' => $key);
  897. $this->Form->create('User');
  898. $result = $this->Form->submit('save.png');
  899. $expected = array(
  900. 'div' => array('class' => 'submit'),
  901. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  902. '/div'
  903. );
  904. $this->assertTags($result, $expected);
  905. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  906. }
  907. /**
  908. * Test that the correct fields are unlocked for image submits with names.
  909. *
  910. * @return void
  911. */
  912. public function testSecuritySubmitImageName() {
  913. $key = 'testKey';
  914. $this->Form->request['_Token'] = array('key' => $key);
  915. $this->Form->create('User');
  916. $result = $this->Form->submit('save.png', array('name' => 'test'));
  917. $expected = array(
  918. 'div' => array('class' => 'submit'),
  919. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  920. '/div'
  921. );
  922. $this->assertTags($result, $expected);
  923. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  924. }
  925. /**
  926. * testFormSecurityMultipleInputFields method
  927. *
  928. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  929. *
  930. * @return void
  931. */
  932. public function testFormSecurityMultipleInputFields() {
  933. $key = 'testKey';
  934. $this->Form->request['_Token'] = array('key' => $key);
  935. $this->Form->create('Addresses');
  936. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  937. $this->Form->input('Addresses.0.title');
  938. $this->Form->input('Addresses.0.first_name');
  939. $this->Form->input('Addresses.0.last_name');
  940. $this->Form->input('Addresses.0.address');
  941. $this->Form->input('Addresses.0.city');
  942. $this->Form->input('Addresses.0.phone');
  943. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  944. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  945. $this->Form->input('Addresses.1.title');
  946. $this->Form->input('Addresses.1.first_name');
  947. $this->Form->input('Addresses.1.last_name');
  948. $this->Form->input('Addresses.1.address');
  949. $this->Form->input('Addresses.1.city');
  950. $this->Form->input('Addresses.1.phone');
  951. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  952. $result = $this->Form->secure($this->Form->fields);
  953. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  954. $expected = array(
  955. 'div' => array('style' => 'display:none;'),
  956. array('input' => array(
  957. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  958. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  959. )),
  960. array('input' => array(
  961. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  962. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  963. )),
  964. '/div'
  965. );
  966. $this->assertTags($result, $expected);
  967. }
  968. /**
  969. * Test form security with Model.field.0 style inputs
  970. *
  971. * @return void
  972. */
  973. public function testFormSecurityArrayFields() {
  974. $key = 'testKey';
  975. $this->Form->request->params['_Token']['key'] = $key;
  976. $this->Form->create('Address');
  977. $this->Form->input('Address.primary.1');
  978. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  979. $this->Form->input('Address.secondary.1.0');
  980. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  981. }
  982. /**
  983. * testFormSecurityMultipleInputDisabledFields method
  984. *
  985. * test secure form generation with multiple records and disabled fields.
  986. *
  987. * @return void
  988. */
  989. public function testFormSecurityMultipleInputDisabledFields() {
  990. $key = 'testKey';
  991. $this->Form->request->params['_Token'] = array(
  992. 'key' => $key,
  993. 'unlockedFields' => array('first_name', 'address')
  994. );
  995. $this->Form->create();
  996. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  997. $this->Form->input('Addresses.0.title');
  998. $this->Form->input('Addresses.0.first_name');
  999. $this->Form->input('Addresses.0.last_name');
  1000. $this->Form->input('Addresses.0.address');
  1001. $this->Form->input('Addresses.0.city');
  1002. $this->Form->input('Addresses.0.phone');
  1003. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1004. $this->Form->input('Addresses.1.title');
  1005. $this->Form->input('Addresses.1.first_name');
  1006. $this->Form->input('Addresses.1.last_name');
  1007. $this->Form->input('Addresses.1.address');
  1008. $this->Form->input('Addresses.1.city');
  1009. $this->Form->input('Addresses.1.phone');
  1010. $result = $this->Form->secure($this->Form->fields);
  1011. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1012. $expected = array(
  1013. 'div' => array('style' => 'display:none;'),
  1014. array('input' => array(
  1015. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1016. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1017. )),
  1018. array('input' => array(
  1019. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1020. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1021. )),
  1022. '/div'
  1023. );
  1024. $this->assertTags($result, $expected);
  1025. }
  1026. /**
  1027. * testFormSecurityInputDisabledFields method
  1028. *
  1029. * Test single record form with disabled fields.
  1030. *
  1031. * @return void
  1032. */
  1033. public function testFormSecurityInputUnlockedFields() {
  1034. $key = 'testKey';
  1035. $this->Form->request['_Token'] = array(
  1036. 'key' => $key,
  1037. 'unlockedFields' => array('first_name', 'address')
  1038. );
  1039. $this->Form->create();
  1040. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1041. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1042. $this->Form->input('Addresses.title');
  1043. $this->Form->input('Addresses.first_name');
  1044. $this->Form->input('Addresses.last_name');
  1045. $this->Form->input('Addresses.address');
  1046. $this->Form->input('Addresses.city');
  1047. $this->Form->input('Addresses.phone');
  1048. $result = $this->Form->fields;
  1049. $expected = array(
  1050. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1051. 'Addresses.city', 'Addresses.phone'
  1052. );
  1053. $this->assertEquals($expected, $result);
  1054. $result = $this->Form->secure($expected);
  1055. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1056. $expected = array(
  1057. 'div' => array('style' => 'display:none;'),
  1058. array('input' => array(
  1059. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1060. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1061. )),
  1062. array('input' => array(
  1063. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1064. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1065. )),
  1066. '/div'
  1067. );
  1068. $this->assertTags($result, $expected);
  1069. }
  1070. /**
  1071. * test securing inputs with custom name attributes.
  1072. *
  1073. * @return void
  1074. */
  1075. public function testFormSecureWithCustomNameAttribute() {
  1076. $this->Form->request->params['_Token']['key'] = 'testKey';
  1077. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1078. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1079. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1080. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1081. }
  1082. /**
  1083. * testFormSecuredInput method
  1084. *
  1085. * Test generation of entire secure form, assertions made on input() output.
  1086. *
  1087. * @return void
  1088. */
  1089. public function testFormSecuredInput() {
  1090. $this->Form->request['_Token'] = array('key' => 'testKey');
  1091. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1092. $encoding = strtolower(Configure::read('App.encoding'));
  1093. $expected = array(
  1094. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1095. 'div' => array('style' => 'display:none;'),
  1096. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1097. array('input' => array(
  1098. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1099. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1100. )),
  1101. '/div'
  1102. );
  1103. $this->assertTags($result, $expected);
  1104. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1105. $expected = array(
  1106. 'div' => array('class' => 'input text'),
  1107. 'label' => array('for' => 'UserFormPublished'),
  1108. 'Published',
  1109. '/label',
  1110. array('input' => array(
  1111. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1112. 'id' => 'UserFormPublished'
  1113. )),
  1114. '/div'
  1115. );
  1116. $this->assertTags($result, $expected);
  1117. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1118. $expected = array(
  1119. 'div' => array('class' => 'input text'),
  1120. 'label' => array('for' => 'UserFormOther'),
  1121. 'Other',
  1122. '/label',
  1123. array('input' => array(
  1124. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1125. 'id' => 'UserFormOther'
  1126. )),
  1127. '/div'
  1128. );
  1129. $this->assertTags($result, $expected);
  1130. $result = $this->Form->hidden('UserForm.stuff');
  1131. $expected = array('input' => array(
  1132. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1133. 'id' => 'UserFormStuff'
  1134. ));
  1135. $this->assertTags($result, $expected);
  1136. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1137. $expected = array('input' => array(
  1138. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1139. 'value' => '0', 'id' => 'UserFormHidden'
  1140. ));
  1141. $this->assertTags($result, $expected);
  1142. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1143. $expected = array(
  1144. 'div' => array('class' => 'input checkbox'),
  1145. array('input' => array(
  1146. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1147. 'value' => '0', 'id' => 'UserFormSomething_'
  1148. )),
  1149. array('input' => array(
  1150. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1151. 'value' => '1', 'id' => 'UserFormSomething'
  1152. )),
  1153. 'label' => array('for' => 'UserFormSomething'),
  1154. 'Something',
  1155. '/label',
  1156. '/div'
  1157. );
  1158. $this->assertTags($result, $expected);
  1159. $result = $this->Form->fields;
  1160. $expected = array(
  1161. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1162. 'UserForm.hidden' => '0', 'UserForm.something'
  1163. );
  1164. $this->assertEquals($expected, $result);
  1165. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1166. $result = $this->Form->secure($this->Form->fields);
  1167. $expected = array(
  1168. 'div' => array('style' => 'display:none;'),
  1169. array('input' => array(
  1170. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1171. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1172. )),
  1173. array('input' => array(
  1174. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1175. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1176. )),
  1177. '/div'
  1178. );
  1179. $this->assertTags($result, $expected);
  1180. }
  1181. /**
  1182. * Tests that the correct keys are added to the field hash index
  1183. *
  1184. * @return void
  1185. */
  1186. public function testFormSecuredFileInput() {
  1187. $this->Form->request['_Token'] = array('key' => 'testKey');
  1188. $this->assertEquals(array(), $this->Form->fields);
  1189. $result = $this->Form->file('Attachment.file');
  1190. $expected = array(
  1191. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1192. 'Attachment.file.error', 'Attachment.file.size'
  1193. );
  1194. $this->assertEquals($expected, $this->Form->fields);
  1195. }
  1196. /**
  1197. * test that multiple selects keys are added to field hash
  1198. *
  1199. * @return void
  1200. */
  1201. public function testFormSecuredMultipleSelect() {
  1202. $this->Form->request['_Token'] = array('key' => 'testKey');
  1203. $this->assertEquals(array(), $this->Form->fields);
  1204. $options = array('1' => 'one', '2' => 'two');
  1205. $this->Form->select('Model.select', $options);
  1206. $expected = array('Model.select');
  1207. $this->assertEquals($expected, $this->Form->fields);
  1208. $this->Form->fields = array();
  1209. $this->Form->select('Model.select', $options, array('multiple' => true));
  1210. $this->assertEquals($expected, $this->Form->fields);
  1211. }
  1212. /**
  1213. * testFormSecuredRadio method
  1214. *
  1215. * @return void
  1216. */
  1217. public function testFormSecuredRadio() {
  1218. $this->Form->request['_Token'] = array('key' => 'testKey');
  1219. $this->assertEquals(array(), $this->Form->fields);
  1220. $options = array('1' => 'option1', '2' => 'option2');
  1221. $this->Form->radio('Test.test', $options);
  1222. $expected = array('Test.test');
  1223. $this->assertEquals($expected, $this->Form->fields);
  1224. }
  1225. /**
  1226. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1227. * hashing.
  1228. *
  1229. * @return void
  1230. */
  1231. public function testFormSecuredAndDisabled() {
  1232. $this->Form->request['_Token'] = array('key' => 'testKey');
  1233. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1234. $this->Form->text('Model.text', array('disabled' => true));
  1235. $this->Form->password('Model.text', array('disabled' => true));
  1236. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1237. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1238. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1239. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1240. $this->Form->month('Model.month', array('disabled' => true));
  1241. $this->Form->day('Model.day', array('disabled' => true));
  1242. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1243. $this->Form->minute('Model.minute', array('disabled' => true));
  1244. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1245. $expected = array(
  1246. 'Model.radio' => ''
  1247. );
  1248. $this->assertEquals($expected, $this->Form->fields);
  1249. }
  1250. /**
  1251. * testDisableSecurityUsingForm method
  1252. *
  1253. * @return void
  1254. */
  1255. public function testDisableSecurityUsingForm() {
  1256. $this->Form->request['_Token'] = array(
  1257. 'key' => 'testKey',
  1258. 'disabledFields' => array()
  1259. );
  1260. $this->Form->create();
  1261. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1262. $this->Form->input('Addresses.title');
  1263. $this->Form->input('Addresses.first_name', array('secure' => false));
  1264. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1265. $this->Form->input('Addresses.zip', array(
  1266. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1267. ));
  1268. $result = $this->Form->fields;
  1269. $expected = array(
  1270. 'Addresses.id' => '123456', 'Addresses.title',
  1271. );
  1272. $this->assertEquals($expected, $result);
  1273. }
  1274. /**
  1275. * test disableField
  1276. *
  1277. * @return void
  1278. */
  1279. public function testUnlockFieldAddsToList() {
  1280. $this->Form->request['_Token'] = array(
  1281. 'key' => 'testKey',
  1282. 'unlockedFields' => array()
  1283. );
  1284. $this->Form->create('Contact');
  1285. $this->Form->unlockField('Contact.name');
  1286. $this->Form->text('Contact.name');
  1287. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1288. $this->assertEquals(array(), $this->Form->fields);
  1289. }
  1290. /**
  1291. * test unlockField removing from fields array.
  1292. *
  1293. * @return void
  1294. */
  1295. public function testUnlockFieldRemovingFromFields() {
  1296. $this->Form->request['_Token'] = array(
  1297. 'key' => 'testKey',
  1298. 'unlockedFields' => array()
  1299. );
  1300. $this->Form->create('Contact');
  1301. $this->Form->hidden('Contact.id', array('value' => 1));
  1302. $this->Form->text('Contact.name');
  1303. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1304. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1305. $this->Form->unlockField('Contact.name');
  1306. $this->Form->unlockField('Contact.id');
  1307. $this->assertEquals(array(), $this->Form->fields);
  1308. }
  1309. /**
  1310. * testTagIsInvalid method
  1311. *
  1312. * @return void
  1313. */
  1314. public function testTagIsInvalid() {
  1315. $Contact = ClassRegistry::getObject('Contact');
  1316. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1317. $this->Form->setEntity('Contact.0.email');
  1318. $result = $this->Form->tagIsInvalid();
  1319. $expected = array('Please provide an email');
  1320. $this->assertEquals($expected, $result);
  1321. $this->Form->setEntity('Contact.1.email');
  1322. $result = $this->Form->tagIsInvalid();
  1323. $expected = false;
  1324. $this->assertSame($expected, $result);
  1325. $this->Form->setEntity('Contact.0.name');
  1326. $result = $this->Form->tagIsInvalid();
  1327. $expected = false;
  1328. $this->assertSame($expected, $result);
  1329. }
  1330. /**
  1331. * testPasswordValidation method
  1332. *
  1333. * test validation errors on password input.
  1334. *
  1335. * @return void
  1336. */
  1337. public function testPasswordValidation() {
  1338. $Contact = ClassRegistry::getObject('Contact');
  1339. $Contact->validationErrors['password'] = array('Please provide a password');
  1340. $result = $this->Form->input('Contact.password');
  1341. $expected = array(
  1342. 'div' => array('class' => 'input password error'),
  1343. 'label' => array('for' => 'ContactPassword'),
  1344. 'Password',
  1345. '/label',
  1346. 'input' => array(
  1347. 'type' => 'password', 'name' => 'data[Contact][password]',
  1348. 'id' => 'ContactPassword', 'class' => 'form-error'
  1349. ),
  1350. array('div' => array('class' => 'error-message')),
  1351. 'Please provide a password',
  1352. '/div',
  1353. '/div'
  1354. );
  1355. $this->assertTags($result, $expected);
  1356. }
  1357. /**
  1358. * testEmptyErrorValidation method
  1359. *
  1360. * test validation error div when validation message is an empty string
  1361. *
  1362. * @access public
  1363. * @return void
  1364. */
  1365. public function testEmptyErrorValidation() {
  1366. $this->Form->validationErrors['Contact']['password'] = '';
  1367. $result = $this->Form->input('Contact.password');
  1368. $expected = array(
  1369. 'div' => array('class' => 'input password error'),
  1370. 'label' => array('for' => 'ContactPassword'),
  1371. 'Password',
  1372. '/label',
  1373. 'input' => array(
  1374. 'type' => 'password', 'name' => 'data[Contact][password]',
  1375. 'id' => 'ContactPassword', 'class' => 'form-error'
  1376. ),
  1377. array('div' => array('class' => 'error-message')),
  1378. array(),
  1379. '/div',
  1380. '/div'
  1381. );
  1382. $this->assertTags($result, $expected);
  1383. }
  1384. /**
  1385. * testEmptyInputErrorValidation method
  1386. *
  1387. * test validation error div when validation message is overridden by an empty string when calling input()
  1388. *
  1389. * @access public
  1390. * @return void
  1391. */
  1392. public function testEmptyInputErrorValidation() {
  1393. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1394. $result = $this->Form->input('Contact.password', array('error' => ''));
  1395. $expected = array(
  1396. 'div' => array('class' => 'input password error'),
  1397. 'label' => array('for' => 'ContactPassword'),
  1398. 'Password',
  1399. '/label',
  1400. 'input' => array(
  1401. 'type' => 'password', 'name' => 'data[Contact][password]',
  1402. 'id' => 'ContactPassword', 'class' => 'form-error'
  1403. ),
  1404. array('div' => array('class' => 'error-message')),
  1405. array(),
  1406. '/div',
  1407. '/div'
  1408. );
  1409. $this->assertTags($result, $expected);
  1410. }
  1411. /**
  1412. * testFormValidationAssociated method
  1413. *
  1414. * test display of form errors in conjunction with model::validates.
  1415. *
  1416. * @return void
  1417. */
  1418. public function testFormValidationAssociated() {
  1419. $this->UserForm = ClassRegistry::getObject('UserForm');
  1420. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1421. $data = array(
  1422. 'UserForm' => array('name' => 'user'),
  1423. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1424. );
  1425. $result = $this->UserForm->OpenidUrl->create($data);
  1426. $this->assertFalse(empty($result));
  1427. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1428. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1429. $encoding = strtolower(Configure::read('App.encoding'));
  1430. $expected = array(
  1431. 'form' => array(
  1432. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1433. 'accept-charset' => $encoding
  1434. ),
  1435. 'div' => array('style' => 'display:none;'),
  1436. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1437. '/div'
  1438. );
  1439. $this->assertTags($result, $expected);
  1440. $result = $this->Form->error(
  1441. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1442. );
  1443. $this->assertEquals('Error, not registered', $result);
  1444. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1445. }
  1446. /**
  1447. * testFormValidationAssociatedFirstLevel method
  1448. *
  1449. * test form error display with associated model.
  1450. *
  1451. * @return void
  1452. */
  1453. public function testFormValidationAssociatedFirstLevel() {
  1454. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1455. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1456. $data = array(
  1457. 'ValidateUser' => array('name' => 'mariano'),
  1458. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1459. );
  1460. $result = $this->ValidateUser->create($data);
  1461. $this->assertFalse(empty($result));
  1462. $this->assertFalse($this->ValidateUser->validates());
  1463. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1464. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1465. $encoding = strtolower(Configure::read('App.encoding'));
  1466. $expected = array(
  1467. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1468. 'div' => array('style' => 'display:none;'),
  1469. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1470. '/div'
  1471. );
  1472. $this->assertTags($result, $expected);
  1473. $result = $this->Form->error(
  1474. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1475. );
  1476. $this->assertEquals('Invalid email', $result);
  1477. $result = $this->Form->error(
  1478. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1479. );
  1480. $this->assertEquals('Invalid name', $result);
  1481. $result = $this->Form->error(
  1482. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1483. );
  1484. $this->assertEquals('Invalid city', $result);
  1485. unset($this->ValidateUser->ValidateProfile);
  1486. unset($this->ValidateUser);
  1487. }
  1488. /**
  1489. * testFormValidationAssociatedSecondLevel method
  1490. *
  1491. * test form error display with associated model.
  1492. *
  1493. * @return void
  1494. */
  1495. public function testFormValidationAssociatedSecondLevel() {
  1496. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1497. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1498. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1499. $data = array(
  1500. 'ValidateUser' => array('name' => 'mariano'),
  1501. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1502. 'ValidateItem' => array('name' => 'Item')
  1503. );
  1504. $result = $this->ValidateUser->create($data);
  1505. $this->assertFalse(empty($result));
  1506. $this->assertFalse($this->ValidateUser->validates());
  1507. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1508. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1509. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1510. $encoding = strtolower(Configure::read('App.encoding'));
  1511. $expected = array(
  1512. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1513. 'div' => array('style' => 'display:none;'),
  1514. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1515. '/div'
  1516. );
  1517. $this->assertTags($result, $expected);
  1518. $result = $this->Form->error(
  1519. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1520. );
  1521. $this->assertEquals('Invalid email', $result);
  1522. $result = $this->Form->error(
  1523. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1524. );
  1525. $this->assertEquals('Invalid name', $result);
  1526. $result = $this->Form->error(
  1527. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1528. );
  1529. $result = $this->Form->error(
  1530. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1531. );
  1532. $this->assertEquals('Invalid description', $result);
  1533. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1534. unset($this->ValidateUser->ValidateProfile);
  1535. unset($this->ValidateUser);
  1536. }
  1537. /**
  1538. * testFormValidationMultiRecord method
  1539. *
  1540. * test form error display with multiple records.
  1541. *
  1542. * @return void
  1543. */
  1544. public function testFormValidationMultiRecord() {
  1545. $Contact = ClassRegistry::getObject('Contact');
  1546. $Contact->validationErrors[2] = array(
  1547. 'name' => array('This field cannot be left blank')
  1548. );
  1549. $result = $this->Form->input('Contact.2.name');
  1550. $expected = array(
  1551. 'div' => array('class' => 'input text error'),
  1552. 'label' => array('for' => 'Contact2Name'),
  1553. 'Name',
  1554. '/label',
  1555. 'input' => array(
  1556. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1557. 'class' => 'form-error', 'maxlength' => 255
  1558. ),
  1559. array('div' => array('class' => 'error-message')),
  1560. 'This field cannot be left blank',
  1561. '/div',
  1562. '/div'
  1563. );
  1564. $this->assertTags($result, $expected);
  1565. }
  1566. /**
  1567. * testMultipleInputValidation method
  1568. *
  1569. * test multiple record form validation error display.
  1570. *
  1571. * @return void
  1572. */
  1573. public function testMultipleInputValidation() {
  1574. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1575. $Address->validationErrors[0] = array(
  1576. 'title' => array('This field cannot be empty'),
  1577. 'first_name' => array('This field cannot be empty')
  1578. );
  1579. $Address->validationErrors[1] = array(
  1580. 'last_name' => array('You must have a last name')
  1581. );
  1582. $this->Form->create();
  1583. $result = $this->Form->input('Address.0.title');
  1584. $expected = array(
  1585. 'div' => array('class'),
  1586. 'label' => array('for'),
  1587. 'preg:/[^<]+/',
  1588. '/label',
  1589. 'input' => array(
  1590. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1591. ),
  1592. array('div' => array('class' => 'error-message')),
  1593. 'This field cannot be empty',
  1594. '/div',
  1595. '/div'
  1596. );
  1597. $this->assertTags($result, $expected);
  1598. $result = $this->Form->input('Address.0.first_name');
  1599. $expected = array(
  1600. 'div' => array('class'),
  1601. 'label' => array('for'),
  1602. 'preg:/[^<]+/',
  1603. '/label',
  1604. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1605. array('div' => array('class' => 'error-message')),
  1606. 'This field cannot be empty',
  1607. '/div',
  1608. '/div'
  1609. );
  1610. $this->assertTags($result, $expected);
  1611. $result = $this->Form->input('Address.0.last_name');
  1612. $expected = array(
  1613. 'div' => array('class'),
  1614. 'label' => array('for'),
  1615. 'preg:/[^<]+/',
  1616. '/label',
  1617. 'input' => array('type' => 'text', 'name', 'id'),
  1618. '/div'
  1619. );
  1620. $this->assertTags($result, $expected);
  1621. $result = $this->Form->input('Address.1.last_name');
  1622. $expected = array(
  1623. 'div' => array('class'),
  1624. 'label' => array('for'),
  1625. 'preg:/[^<]+/',
  1626. '/label',
  1627. 'input' => array(
  1628. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1629. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1630. ),
  1631. array('div' => array('class' => 'error-message')),
  1632. 'You must have a last name',
  1633. '/div',
  1634. '/div'
  1635. );
  1636. $this->assertTags($result, $expected);
  1637. }
  1638. /**
  1639. * testInput method
  1640. *
  1641. * Test various incarnations of input().
  1642. *
  1643. * @return void
  1644. */
  1645. public function testInput() {
  1646. $result = $this->Form->input('ValidateUser.balance');
  1647. $expected = array(
  1648. 'div' => array('class'),
  1649. 'label' => array('for'),
  1650. 'Balance',
  1651. '/label',
  1652. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1653. '/div',
  1654. );
  1655. $this->assertTags($result, $expected);
  1656. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1657. $expected = array(
  1658. 'div' => array('class' => 'input text'),
  1659. 'label' => array('for' => 'custom'),
  1660. 'Email',
  1661. '/label',
  1662. array('input' => array(
  1663. 'type' => 'text', 'name' => 'data[Contact][email]',
  1664. 'id' => 'custom', 'maxlength' => 255
  1665. )),
  1666. '/div'
  1667. );
  1668. $this->assertTags($result, $expected);
  1669. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1670. $expected = array(
  1671. '<div',
  1672. 'label' => array('for' => 'ContactEmail'),
  1673. 'Email',
  1674. '/label',
  1675. array('input' => array(
  1676. 'type' => 'text', 'name' => 'data[Contact][email]',
  1677. 'id' => 'ContactEmail', 'maxlength' => 255
  1678. )),
  1679. '/div'
  1680. );
  1681. $this->assertTags($result, $expected);
  1682. $result = $this->Form->hidden('Contact.idontexist');
  1683. $expected = array('input' => array(
  1684. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1685. 'id' => 'ContactIdontexist'
  1686. ));
  1687. $this->assertTags($result, $expected);
  1688. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1689. $expected = array(
  1690. 'div' => array('class' => 'input text'),
  1691. 'label' => array('for' => 'ContactEmail'),
  1692. 'Email',
  1693. '/label',
  1694. array('input' => array(
  1695. 'type' => 'text', 'name' => 'data[Contact][email]',
  1696. 'id' => 'ContactEmail'
  1697. )),
  1698. '/div'
  1699. );
  1700. $this->assertTags($result, $expected);
  1701. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1702. $expected = array(
  1703. 'div' => array('class' => 'input text'),
  1704. 'label' => array('for' => 'Contact5Email'),
  1705. 'Email',
  1706. '/label',
  1707. array('input' => array(
  1708. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1709. 'id' => 'Contact5Email'
  1710. )),
  1711. '/div'
  1712. );
  1713. $this->assertTags($result, $expected);
  1714. $result = $this->Form->input('Contact.password');
  1715. $expected = array(
  1716. 'div' => array('class' => 'input password'),
  1717. 'label' => array('for' => 'ContactPassword'),
  1718. 'Password',
  1719. '/label',
  1720. array('input' => array(
  1721. 'type' => 'password', 'name' => 'data[Contact][password]',
  1722. 'id' => 'ContactPassword'
  1723. )),
  1724. '/div'
  1725. );
  1726. $this->assertTags($result, $expected);
  1727. $result = $this->Form->input('Contact.email', array(
  1728. 'type' => 'file', 'class' => 'textbox'
  1729. ));
  1730. $expected = array(
  1731. 'div' => array('class' => 'input file'),
  1732. 'label' => array('for' => 'ContactEmail'),
  1733. 'Email',
  1734. '/label',
  1735. array('input' => array(
  1736. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1737. 'id' => 'ContactEmail'
  1738. )),
  1739. '/div'
  1740. );
  1741. $this->assertTags($result, $expected);
  1742. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1743. $result = $this->Form->input('Contact.phone');
  1744. $expected = array(
  1745. 'div' => array('class' => 'input text'),
  1746. 'label' => array('for' => 'ContactPhone'),
  1747. 'Phone',
  1748. '/label',
  1749. array('input' => array(
  1750. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1751. 'value' => 'Hello &amp; World &gt; weird chars',
  1752. 'id' => 'ContactPhone', 'maxlength' => 255
  1753. )),
  1754. '/div'
  1755. );
  1756. $this->assertTags($result, $expected);
  1757. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1758. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1759. $expected = array(
  1760. 'div' => array('class' => 'input text'),
  1761. 'label' => array('for' => 'myId'),
  1762. 'Field',
  1763. '/label',
  1764. 'input' => array(
  1765. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1766. 'value' => 'My value', 'id' => 'myId'
  1767. ),
  1768. '/div'
  1769. );
  1770. $this->assertTags($result, $expected);
  1771. unset($this->Form->request->data);
  1772. $Contact = ClassRegistry::getObject('Contact');
  1773. $Contact->validationErrors['field'] = array('Badness!');
  1774. $result = $this->Form->input('Contact.field');
  1775. $expected = array(
  1776. 'div' => array('class' => 'input text error'),
  1777. 'label' => array('for' => 'ContactField'),
  1778. 'Field',
  1779. '/label',
  1780. 'input' => array(
  1781. 'type' => 'text', 'name' => 'data[Contact][field]',
  1782. 'id' => 'ContactField', 'class' => 'form-error'
  1783. ),
  1784. array('div' => array('class' => 'error-message')),
  1785. 'Badness!',
  1786. '/div',
  1787. '/div'
  1788. );
  1789. $this->assertTags($result, $expected);
  1790. $result = $this->Form->input('Contact.field', array(
  1791. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1792. ));
  1793. $expected = array(
  1794. 'label' => array('for' => 'ContactField'),
  1795. 'Field',
  1796. '/label',
  1797. 'input' => array(
  1798. 'type' => 'text', 'name' => 'data[Contact][field]',
  1799. 'id' => 'ContactField', 'class' => 'form-error'
  1800. ),
  1801. array('span' => array('class' => 'error-message')),
  1802. 'Badness!',
  1803. '/span'
  1804. );
  1805. $this->assertTags($result, $expected);
  1806. $result = $this->Form->input('Contact.field', array(
  1807. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1808. ));
  1809. $expected = array(
  1810. 'div' => array('class' => 'input text error'),
  1811. 'label' => array('for' => 'ContactField'),
  1812. 'Field',
  1813. '/label',
  1814. 'input' => array(
  1815. 'type' => 'text', 'name' => 'data[Contact][field]',
  1816. 'id' => 'ContactField', 'class' => 'form-error'
  1817. ),
  1818. array('div' => array('class' => 'error')),
  1819. 'Badness!',
  1820. '/div'
  1821. );
  1822. $this->assertTags($result, $expected);
  1823. $result = $this->Form->input('Contact.field', array(
  1824. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1825. ));
  1826. $expected = array(
  1827. 'span' => array('class' => 'input text error'),
  1828. 'label' => array('for' => 'ContactField'),
  1829. 'Field',
  1830. '/label',
  1831. 'input' => array(
  1832. 'type' => 'text', 'name' => 'data[Contact][field]',
  1833. 'id' => 'ContactField', 'class' => 'form-error'
  1834. ),
  1835. 'Badness!',
  1836. '/span'
  1837. );
  1838. $this->assertTags($result, $expected);
  1839. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1840. $expected = array(
  1841. 'div' => 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. 'A message to you, Rudy',
  1850. array('div' => array('class' => 'error-message')),
  1851. 'Badness!',
  1852. '/div',
  1853. '/div'
  1854. );
  1855. $this->assertTags($result, $expected);
  1856. $this->Form->setEntity(null);
  1857. $this->Form->setEntity('Contact.field');
  1858. $result = $this->Form->input('Contact.field', array(
  1859. 'after' => 'A message to you, Rudy', 'error' => false
  1860. ));
  1861. $expected = array(
  1862. 'div' => array('class' => 'input text'),
  1863. 'label' => array('for' => 'ContactField'),
  1864. 'Field',
  1865. '/label',
  1866. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1867. 'A message to you, Rudy',
  1868. '/div'
  1869. );
  1870. $this->assertTags($result, $expected);
  1871. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1872. $expected = array(
  1873. 'div' => array('class' => 'input text'),
  1874. 'label' => array('for' => 'ObjectField'),
  1875. 'Field',
  1876. '/label',
  1877. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1878. 'A message to you, Rudy',
  1879. '/div'
  1880. );
  1881. $this->assertTags($result, $expected);
  1882. $Contact->validationErrors['field'] = array('minLength');
  1883. $result = $this->Form->input('Contact.field', array(
  1884. 'error' => array(
  1885. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1886. 'maxLength' => 'login too large'
  1887. )
  1888. ));
  1889. $expected = array(
  1890. 'div' => array('class' => 'input text error'),
  1891. 'label' => array('for' => 'ContactField'),
  1892. 'Field',
  1893. '/label',
  1894. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1895. array('div' => array('class' => 'error-message')),
  1896. 'Le login doit contenir au moins 2 caractères',
  1897. '/div',
  1898. '/div'
  1899. );
  1900. $this->assertTags($result, $expected);
  1901. $Contact->validationErrors['field'] = array('maxLength');
  1902. $result = $this->Form->input('Contact.field', array(
  1903. 'error' => array(
  1904. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1905. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1906. 'maxLength' => 'login too large',
  1907. )
  1908. ));
  1909. $expected = array(
  1910. 'div' => array('class' => 'input text error'),
  1911. 'label' => array('for' => 'ContactField'),
  1912. 'Field',
  1913. '/label',
  1914. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1915. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1916. 'login too large',
  1917. '/span',
  1918. '/div'
  1919. );
  1920. $this->assertTags($result, $expected);
  1921. }
  1922. /**
  1923. * test input() with checkbox creation
  1924. *
  1925. * @return void
  1926. */
  1927. public function testInputCheckbox() {
  1928. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1929. $expected = array(
  1930. 'div' => array('class' => 'input checkbox'),
  1931. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1932. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1933. '/div'
  1934. );
  1935. $this->assertTags($result, $expected);
  1936. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1937. $expected = array(
  1938. 'div' => array('class' => 'input checkbox'),
  1939. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1940. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1941. '/div'
  1942. );
  1943. $this->assertTags($result, $expected);
  1944. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1945. $expected = array(
  1946. 'div' => array('class' => 'input checkbox'),
  1947. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1948. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1949. '/div'
  1950. );
  1951. $this->assertTags($result, $expected);
  1952. }
  1953. /**
  1954. * test form->input() with time types.
  1955. *
  1956. */
  1957. public function testInputTime() {
  1958. extract($this->dateRegex);
  1959. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1960. $result = explode(':', $result);
  1961. $this->assertRegExp('/option value="23"/', $result[0]);
  1962. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1963. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1964. $result = explode(':', $result);
  1965. $this->assertRegExp('/option value="23"/', $result[0]);
  1966. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1967. $result = $this->Form->input('Model.field', array(
  1968. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1969. ));
  1970. $result = explode(':', $result);
  1971. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1972. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1973. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1974. $result = $this->Form->input('Model.field', array(
  1975. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  1976. ));
  1977. $result = explode(':', $result);
  1978. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1979. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1980. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1981. $result = $this->Form->input('prueba', array(
  1982. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  1983. 'maxYear' => date('Y') + 1 ,'interval' => 15
  1984. ));
  1985. $result = explode(':', $result);
  1986. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1987. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1988. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1989. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  1990. $result = $this->Form->input('Random.start_time', array(
  1991. 'type' => 'time',
  1992. 'selected' => '18:15'
  1993. ));
  1994. $this->assertRegExp('#<option value="06"[^>]*>6</option>#', $result);
  1995. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result);
  1996. $this->assertRegExp('#<option value="pm"[^>]*>pm</option>#', $result);
  1997. }
  1998. /**
  1999. * test form->input() with datetime, date and time types
  2000. *
  2001. * @return void
  2002. */
  2003. public function testInputDatetime() {
  2004. extract($this->dateRegex);
  2005. $result = $this->Form->input('prueba', array(
  2006. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2007. 'maxYear' => date('Y') + 1, 'interval' => 15
  2008. ));
  2009. $result = explode(':', $result);
  2010. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2011. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2012. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2013. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2014. //related to ticket #5013
  2015. $result = $this->Form->input('Contact.date', array(
  2016. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2017. ));
  2018. $this->assertRegExp('/class="customClass"/', $result);
  2019. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2020. $result = $this->Form->input('Contact.date', array(
  2021. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2022. ));
  2023. $this->assertRegExp('/id="customIdDay"/', $result);
  2024. $this->assertRegExp('/id="customIdMonth"/', $result);
  2025. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2026. $result = $this->Form->input('Model.field', array(
  2027. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2028. ));
  2029. $this->assertRegExp('/id="customIDDay"/', $result);
  2030. $this->assertRegExp('/id="customIDHour"/', $result);
  2031. $result = explode('</select><select', $result);
  2032. $result = explode(':', $result[1]);
  2033. $this->assertRegExp('/option value="23"/', $result[0]);
  2034. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2035. $result = $this->Form->input('Model.field', array(
  2036. 'type' => 'datetime', 'timeFormat' => 12
  2037. ));
  2038. $result = explode('</select><select', $result);
  2039. $result = explode(':', $result[1]);
  2040. $this->assertRegExp('/option value="12"/', $result[0]);
  2041. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2042. $this->Form->request->data = array('Contact' => array('created' => null));
  2043. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2044. $expected = array(
  2045. 'div' => array('class' => 'input date'),
  2046. 'label' => array('for' => 'ContactCreatedMonth'),
  2047. 'Created',
  2048. '/label',
  2049. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2050. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2051. $monthsRegex,
  2052. '/select', '-',
  2053. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2054. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2055. $daysRegex,
  2056. '/select', '-',
  2057. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2058. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2059. $yearsRegex,
  2060. '/select',
  2061. '/div'
  2062. );
  2063. $this->assertTags($result, $expected);
  2064. $this->Form->request->data = array('Contact' => array('created' => null));
  2065. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2066. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2067. $this->Form->request->data = array('Contact' => array('created' => null));
  2068. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2069. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2070. $result = $this->Form->input('Contact.created', array(
  2071. 'type' => 'date',
  2072. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2073. 'timeFormat' => 'NONE'
  2074. ));
  2075. $this->assertRegExp('/for\="created-month"/', $result);
  2076. }
  2077. /**
  2078. * Test generating checkboxes in a loop.
  2079. *
  2080. * @return void
  2081. */
  2082. public function testInputCheckboxesInLoop() {
  2083. for ($i = 1; $i < 5; $i++) {
  2084. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2085. $expected = array(
  2086. 'div' => array('class' => 'input checkbox'),
  2087. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2088. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2089. 'label' => array('for' => "Contact{$i}Email"),
  2090. 'Email',
  2091. '/label',
  2092. '/div'
  2093. );
  2094. $this->assertTags($result, $expected);
  2095. }
  2096. }
  2097. /**
  2098. * test input name with leading integer, ensure attributes are generated correctly.
  2099. *
  2100. * @return void
  2101. */
  2102. public function testInputWithLeadingInteger() {
  2103. $result = $this->Form->text('0.Node.title');
  2104. $expected = array(
  2105. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2106. );
  2107. $this->assertTags($result, $expected);
  2108. }
  2109. /**
  2110. * test form->input() with select type inputs.
  2111. *
  2112. * @return void
  2113. */
  2114. public function testInputSelectType() {
  2115. $result = $this->Form->input('email', array(
  2116. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2117. );
  2118. $expected = array(
  2119. 'div' => array('class' => 'input select'),
  2120. 'label' => array('for' => 'email'),
  2121. 'Email',
  2122. '/label',
  2123. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2124. array('option' => array('value' => '')),
  2125. '/option',
  2126. array('option' => array('value' => 'è')),
  2127. 'Firést',
  2128. '/option',
  2129. array('option' => array('value' => 'é')),
  2130. 'Secoènd',
  2131. '/option',
  2132. '/select',
  2133. '/div'
  2134. );
  2135. $this->assertTags($result, $expected);
  2136. $result = $this->Form->input('email', array(
  2137. 'options' => array('First', 'Second'), 'empty' => true)
  2138. );
  2139. $expected = array(
  2140. 'div' => array('class' => 'input select'),
  2141. 'label' => array('for' => 'email'),
  2142. 'Email',
  2143. '/label',
  2144. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2145. array('option' => array('value' => '')),
  2146. '/option',
  2147. array('option' => array('value' => '0')),
  2148. 'First',
  2149. '/option',
  2150. array('option' => array('value' => '1')),
  2151. 'Second',
  2152. '/option',
  2153. '/select',
  2154. '/div'
  2155. );
  2156. $this->assertTags($result, $expected);
  2157. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2158. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2159. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2160. $expected = array(
  2161. 'div' => array('class' => 'input select'),
  2162. 'label' => array('for' => 'ModelUserId'),
  2163. 'User',
  2164. '/label',
  2165. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2166. array('option' => array('value' => '')),
  2167. '/option',
  2168. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2169. 'good',
  2170. '/option',
  2171. array('option' => array('value' => 'other')),
  2172. 'bad',
  2173. '/option',
  2174. '/select',
  2175. '/div'
  2176. );
  2177. $this->assertTags($result, $expected);
  2178. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2179. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2180. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2181. $expected = array(
  2182. 'div' => array('class' => 'input select'),
  2183. 'label' => array('for' => 'ThingUserId'),
  2184. 'User',
  2185. '/label',
  2186. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2187. array('option' => array('value' => '')),
  2188. 'Some Empty',
  2189. '/option',
  2190. array('option' => array('value' => 'value')),
  2191. 'good',
  2192. '/option',
  2193. array('option' => array('value' => 'other')),
  2194. 'bad',
  2195. '/option',
  2196. '/select',
  2197. '/div'
  2198. );
  2199. $this->assertTags($result, $expected);
  2200. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2201. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2202. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2203. $expected = array(
  2204. 'div' => array('class' => 'input select'),
  2205. 'label' => array('for' => 'ThingUserId'),
  2206. 'User',
  2207. '/label',
  2208. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2209. array('option' => array('value' => '')),
  2210. 'Some Empty',
  2211. '/option',
  2212. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2213. 'good',
  2214. '/option',
  2215. array('option' => array('value' => 'other')),
  2216. 'bad',
  2217. '/option',
  2218. '/select',
  2219. '/div'
  2220. );
  2221. $this->assertTags($result, $expected);
  2222. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2223. $this->Form->request->data = array('User' => array('User' => array('value')));
  2224. $result = $this->Form->input('User.User', array('empty' => true));
  2225. $expected = array(
  2226. 'div' => array('class' => 'input select'),
  2227. 'label' => array('for' => 'UserUser'),
  2228. 'User',
  2229. '/label',
  2230. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2231. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2232. array('option' => array('value' => '')),
  2233. '/option',
  2234. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2235. 'good',
  2236. '/option',
  2237. array('option' => array('value' => 'other')),
  2238. 'bad',
  2239. '/option',
  2240. '/select',
  2241. '/div'
  2242. );
  2243. $this->assertTags($result, $expected);
  2244. $this->Form->data = array();
  2245. $result = $this->Form->input('Publisher.id', array(
  2246. 'label' => 'Publisher',
  2247. 'type' => 'select',
  2248. 'multiple' => 'checkbox',
  2249. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2250. ));
  2251. $expected = array(
  2252. array('div' => array('class' => 'input select')),
  2253. array('label' => array('for' => 'PublisherId')),
  2254. 'Publisher',
  2255. '/label',
  2256. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2257. array('div' => array('class' => 'checkbox')),
  2258. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2259. array('label' => array('for' => 'PublisherIdValue1')),
  2260. 'Label 1',
  2261. '/label',
  2262. '/div',
  2263. array('div' => array('class' => 'checkbox')),
  2264. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2265. array('label' => array('for' => 'PublisherIdValue2')),
  2266. 'Label 2',
  2267. '/label',
  2268. '/div',
  2269. '/div'
  2270. );
  2271. $this->assertTags($result, $expected);
  2272. }
  2273. /**
  2274. * test that input() and a non standard primary key makes a hidden input by default.
  2275. *
  2276. * @return void
  2277. */
  2278. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2279. $this->Form->create('User');
  2280. $this->Form->fieldset = array(
  2281. 'User' => array(
  2282. 'fields' => array(
  2283. 'model_id' => array('type' => 'integer')
  2284. ),
  2285. 'validates' => array(),
  2286. 'key' => 'model_id'
  2287. )
  2288. );
  2289. $result = $this->Form->input('model_id');
  2290. $expected = array(
  2291. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2292. );
  2293. $this->assertTags($result, $expected);
  2294. }
  2295. /**
  2296. * test that overriding the magic select type widget is possible
  2297. *
  2298. * @return void
  2299. */
  2300. public function testInputOverridingMagicSelectType() {
  2301. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2302. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2303. $expected = array(
  2304. 'div' => array('class' => 'input text'),
  2305. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2306. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2307. '/div'
  2308. );
  2309. $this->assertTags($result, $expected);
  2310. //Check that magic types still work for plural/singular vars
  2311. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2312. $result = $this->Form->input('Model.type');
  2313. $expected = array(
  2314. 'div' => array('class' => 'input select'),
  2315. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2316. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2317. array('option' => array('value' => 'value')), 'good', '/option',
  2318. array('option' => array('value' => 'other')), 'bad', '/option',
  2319. '/select',
  2320. '/div'
  2321. );
  2322. $this->assertTags($result, $expected);
  2323. }
  2324. /**
  2325. * Test that magic input() selects can easily be converted into radio types without error.
  2326. *
  2327. * @return void
  2328. */
  2329. public function testInputMagicSelectChangeToRadio() {
  2330. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2331. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2332. $this->assertRegExp('/input type="radio"/', $result);
  2333. }
  2334. /**
  2335. * fields with the same name as the model should work.
  2336. *
  2337. * @return void
  2338. */
  2339. public function testInputWithMatchingFieldAndModelName() {
  2340. $this->Form->create('User');
  2341. $this->Form->fieldset = array(
  2342. 'User' => array(
  2343. 'fields' => array(
  2344. 'User' => array('type' => 'text')
  2345. ),
  2346. 'validates' => array(),
  2347. 'key' => 'id'
  2348. )
  2349. );
  2350. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2351. $result = $this->Form->input('User', array('type' => 'text'));
  2352. $expected = array(
  2353. 'div' => array('class' => 'input text'),
  2354. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2355. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2356. '/div'
  2357. );
  2358. $this->assertTags($result, $expected);
  2359. }
  2360. /**
  2361. * testFormInputs method
  2362. *
  2363. * test correct results from form::inputs().
  2364. *
  2365. * @return void
  2366. */
  2367. public function testFormInputs() {
  2368. $this->Form->create('Contact');
  2369. $result = $this->Form->inputs('The Legend');
  2370. $expected = array(
  2371. '<fieldset',
  2372. '<legend',
  2373. 'The Legend',
  2374. '/legend',
  2375. '*/fieldset',
  2376. );
  2377. $this->assertTags($result, $expected);
  2378. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2379. $expected = array(
  2380. 'fieldset' => array('class' => 'classy-stuff'),
  2381. '<legend',
  2382. 'Field of Dreams',
  2383. '/legend',
  2384. '*/fieldset'
  2385. );
  2386. $this->assertTags($result, $expected);
  2387. $this->Form->create('Contact');
  2388. $this->Form->request['prefix'] = 'admin';
  2389. $this->Form->request['action'] = 'admin_edit';
  2390. $result = $this->Form->inputs();
  2391. $expected = array(
  2392. '<fieldset',
  2393. '<legend',
  2394. 'Edit Contact',
  2395. '/legend',
  2396. '*/fieldset',
  2397. );
  2398. $this->assertTags($result, $expected);
  2399. $this->Form->create('Contact');
  2400. $result = $this->Form->inputs(false);
  2401. $expected = array(
  2402. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2403. array('div' => array('class' => 'input text')),
  2404. '*/div',
  2405. array('div' => array('class' => 'input text')),
  2406. '*/div',
  2407. array('div' => array('class' => 'input text')),
  2408. '*/div',
  2409. array('div' => array('class' => 'input password')),
  2410. '*/div',
  2411. array('div' => array('class' => 'input date')),
  2412. '*/div',
  2413. array('div' => array('class' => 'input date')),
  2414. '*/div',
  2415. array('div' => array('class' => 'input datetime')),
  2416. '*/div',
  2417. array('div' => array('class' => 'input number')),
  2418. '*/div',
  2419. array('div' => array('class' => 'input select')),
  2420. '*/div',
  2421. );
  2422. $this->assertTags($result, $expected);
  2423. $this->Form->create('Contact');
  2424. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2425. $expected = array(
  2426. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2427. array('div' => array('class' => 'input text')),
  2428. '*/div',
  2429. array('div' => array('class' => 'input text')),
  2430. '*/div',
  2431. array('div' => array('class' => 'input text')),
  2432. '*/div',
  2433. array('div' => array('class' => 'input password')),
  2434. '*/div',
  2435. array('div' => array('class' => 'input date')),
  2436. '*/div',
  2437. array('div' => array('class' => 'input date')),
  2438. '*/div',
  2439. array('div' => array('class' => 'input datetime')),
  2440. '*/div',
  2441. array('div' => array('class' => 'input number')),
  2442. '*/div',
  2443. array('div' => array('class' => 'input select')),
  2444. '*/div',
  2445. );
  2446. $this->assertTags($result, $expected);
  2447. $this->Form->create('Contact');
  2448. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2449. $expected = array(
  2450. 'fieldset' => array(),
  2451. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2452. array('div' => array('class' => 'input text')),
  2453. '*/div',
  2454. array('div' => array('class' => 'input text')),
  2455. '*/div',
  2456. array('div' => array('class' => 'input text')),
  2457. '*/div',
  2458. array('div' => array('class' => 'input password')),
  2459. '*/div',
  2460. array('div' => array('class' => 'input date')),
  2461. '*/div',
  2462. array('div' => array('class' => 'input date')),
  2463. '*/div',
  2464. array('div' => array('class' => 'input datetime')),
  2465. '*/div',
  2466. array('div' => array('class' => 'input number')),
  2467. '*/div',
  2468. array('div' => array('class' => 'input select')),
  2469. '*/div',
  2470. '/fieldset'
  2471. );
  2472. $this->assertTags($result, $expected);
  2473. $this->Form->create('Contact');
  2474. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2475. $expected = array(
  2476. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2477. array('div' => array('class' => 'input text')),
  2478. '*/div',
  2479. array('div' => array('class' => 'input text')),
  2480. '*/div',
  2481. array('div' => array('class' => 'input text')),
  2482. '*/div',
  2483. array('div' => array('class' => 'input password')),
  2484. '*/div',
  2485. array('div' => array('class' => 'input date')),
  2486. '*/div',
  2487. array('div' => array('class' => 'input date')),
  2488. '*/div',
  2489. array('div' => array('class' => 'input datetime')),
  2490. '*/div',
  2491. array('div' => array('class' => 'input number')),
  2492. '*/div',
  2493. array('div' => array('class' => 'input select')),
  2494. '*/div',
  2495. );
  2496. $this->assertTags($result, $expected);
  2497. $this->Form->create('Contact');
  2498. $result = $this->Form->inputs('Hello');
  2499. $expected = array(
  2500. 'fieldset' => array(),
  2501. 'legend' => array(),
  2502. 'Hello',
  2503. '/legend',
  2504. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2505. array('div' => array('class' => 'input text')),
  2506. '*/div',
  2507. array('div' => array('class' => 'input text')),
  2508. '*/div',
  2509. array('div' => array('class' => 'input text')),
  2510. '*/div',
  2511. array('div' => array('class' => 'input password')),
  2512. '*/div',
  2513. array('div' => array('class' => 'input date')),
  2514. '*/div',
  2515. array('div' => array('class' => 'input date')),
  2516. '*/div',
  2517. array('div' => array('class' => 'input datetime')),
  2518. '*/div',
  2519. array('div' => array('class' => 'input number')),
  2520. '*/div',
  2521. array('div' => array('class' => 'input select')),
  2522. '*/div',
  2523. '/fieldset'
  2524. );
  2525. $this->assertTags($result, $expected);
  2526. $this->Form->create('Contact');
  2527. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2528. $expected = array(
  2529. 'fieldset' => array(),
  2530. 'legend' => array(),
  2531. 'Hello',
  2532. '/legend',
  2533. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2534. array('div' => array('class' => 'input text')),
  2535. '*/div',
  2536. array('div' => array('class' => 'input text')),
  2537. '*/div',
  2538. array('div' => array('class' => 'input text')),
  2539. '*/div',
  2540. array('div' => array('class' => 'input password')),
  2541. '*/div',
  2542. array('div' => array('class' => 'input date')),
  2543. '*/div',
  2544. array('div' => array('class' => 'input date')),
  2545. '*/div',
  2546. array('div' => array('class' => 'input datetime')),
  2547. '*/div',
  2548. array('div' => array('class' => 'input number')),
  2549. '*/div',
  2550. array('div' => array('class' => 'input select')),
  2551. '*/div',
  2552. '/fieldset'
  2553. );
  2554. $this->assertTags($result, $expected);
  2555. }
  2556. /**
  2557. * testSelectAsCheckbox method
  2558. *
  2559. * test multi-select widget with checkbox formatting.
  2560. *
  2561. * @return void
  2562. */
  2563. public function testSelectAsCheckbox() {
  2564. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2565. $expected = array(
  2566. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2567. array('div' => array('class' => 'checkbox')),
  2568. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2569. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2570. 'first',
  2571. '/label',
  2572. '/div',
  2573. array('div' => array('class' => 'checkbox')),
  2574. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2575. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2576. 'second',
  2577. '/label',
  2578. '/div',
  2579. array('div' => array('class' => 'checkbox')),
  2580. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2581. array('label' => array('for' => 'ModelMultiField2')),
  2582. 'third',
  2583. '/label',
  2584. '/div',
  2585. );
  2586. $this->assertTags($result, $expected);
  2587. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2588. $expected = array(
  2589. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2590. array('div' => array('class' => 'checkbox')),
  2591. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2592. array('label' => array('for' => 'ModelMultiField12')),
  2593. 'half',
  2594. '/label',
  2595. '/div',
  2596. );
  2597. $this->assertTags($result, $expected);
  2598. }
  2599. /**
  2600. * testLabel method
  2601. *
  2602. * test label generation.
  2603. *
  2604. * @return void
  2605. */
  2606. public function testLabel() {
  2607. $this->Form->text('Person.name');
  2608. $result = $this->Form->label();
  2609. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2610. $this->Form->text('Person.name');
  2611. $result = $this->Form->label();
  2612. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2613. $result = $this->Form->label('Person.first_name');
  2614. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2615. $result = $this->Form->label('Person.first_name', 'Your first name');
  2616. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2617. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2618. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2619. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2620. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2621. $result = $this->Form->label('Person.first_name', '');
  2622. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2623. $result = $this->Form->label('Person.2.name', '');
  2624. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2625. }
  2626. /**
  2627. * testTextbox method
  2628. *
  2629. * test textbox element generation
  2630. *
  2631. * @return void
  2632. */
  2633. public function testTextbox() {
  2634. $result = $this->Form->text('Model.field');
  2635. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2636. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2637. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2638. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2639. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2640. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2641. $result = $this->Form->text('Model.text');
  2642. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2643. $Contact = ClassRegistry::getObject('Contact');
  2644. $Contact->validationErrors['text'] = array(true);
  2645. $this->Form->request->data['Contact']['text'] = 'test';
  2646. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2647. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2648. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2649. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2650. $expected = array(
  2651. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2652. );
  2653. $this->assertTags($result, $expected);
  2654. }
  2655. /**
  2656. * testDefaultValue method
  2657. *
  2658. * Test default value setting
  2659. *
  2660. * @return void
  2661. */
  2662. public function testDefaultValue() {
  2663. $this->Form->request->data['Model']['field'] = 'test';
  2664. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2665. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2666. unset($this->Form->request->data['Model']['field']);
  2667. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2668. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2669. }
  2670. /**
  2671. * testCheckboxDefaultValue method
  2672. *
  2673. * Test default value setting on checkbox() method
  2674. *
  2675. * @return void
  2676. */
  2677. public function testCheckboxDefaultValue() {
  2678. $this->Form->request->data['Model']['field'] = false;
  2679. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2680. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2681. unset($this->Form->request->data['Model']['field']);
  2682. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2683. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2684. $this->Form->request->data['Model']['field'] = true;
  2685. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2686. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2687. unset($this->Form->request->data['Model']['field']);
  2688. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2689. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2690. }
  2691. /**
  2692. * testError method
  2693. *
  2694. * Test field error generation
  2695. *
  2696. * @return void
  2697. */
  2698. public function testError() {
  2699. $Contact = ClassRegistry::getObject('Contact');
  2700. $Contact->validationErrors['field'] = array(1);
  2701. $result = $this->Form->error('Contact.field');
  2702. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2703. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2704. $this->assertEquals('Error in field Field', $result);
  2705. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2706. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2707. $this->assertEquals('This field contains invalid input', $result);
  2708. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2709. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2710. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2711. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2712. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2713. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2714. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2715. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2716. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2717. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2718. $this->assertEquals('<strong>Badness!</strong>', $result);
  2719. $Contact->validationErrors['field'] = array("email");
  2720. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2721. $expected = array(
  2722. 'div' => array('class' => 'field-error'),
  2723. 'No good!',
  2724. '/div'
  2725. );
  2726. $this->assertTags($result, $expected);
  2727. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2728. $result = $this->Form->error('Contact.field', array(
  2729. 'notEmpty' => 'Cannot be empty',
  2730. 'email' => 'No good!'
  2731. ));
  2732. $expected = array(
  2733. 'div' => array('class' => 'error-message'),
  2734. 'ul' => array(),
  2735. '<li', 'Cannot be empty', '/li',
  2736. '<li', 'No good!', '/li',
  2737. '<li', 'Something else', '/li',
  2738. '/ul',
  2739. '/div'
  2740. );
  2741. $this->assertTags($result, $expected);
  2742. /** Testing error messages list options **/
  2743. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2744. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2745. $expected = array(
  2746. 'div' => array('class' => 'error-message'),
  2747. 'ol' => array(),
  2748. '<li', 'notEmpty', '/li',
  2749. '<li', 'email', '/li',
  2750. '/ol',
  2751. '/div'
  2752. );
  2753. $this->assertTags($result, $expected);
  2754. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2755. $expected = array(
  2756. 'div' => array('class' => 'error-message'),
  2757. 'ol' => array(),
  2758. '<li', 'notEmpty', '/li',
  2759. '<li', 'email', '/li',
  2760. '/ol',
  2761. '/div'
  2762. );
  2763. $this->assertTags($result, $expected);
  2764. $result = $this->Form->error('Contact.field', null, array(
  2765. 'listOptions' => array(
  2766. 'class' => 'ul-class',
  2767. 'itemOptions' => array(
  2768. 'class' => 'li-class'
  2769. )
  2770. )
  2771. ));
  2772. $expected = array(
  2773. 'div' => array('class' => 'error-message'),
  2774. 'ul' => array('class' => 'ul-class'),
  2775. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2776. array('li' => array('class' => 'li-class')), 'email', '/li',
  2777. '/ul',
  2778. '/div'
  2779. );
  2780. $this->assertTags($result, $expected);
  2781. }
  2782. /**
  2783. * test error options when using form->input();
  2784. *
  2785. * @return void
  2786. */
  2787. public function testInputErrorEscape() {
  2788. $this->Form->create('ValidateProfile');
  2789. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2790. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2791. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2792. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2793. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2794. $this->assertRegExp('/required<br>/', $result);
  2795. }
  2796. /**
  2797. * testPassword method
  2798. *
  2799. * Test password element generation
  2800. *
  2801. * @return void
  2802. */
  2803. public function testPassword() {
  2804. $Contact = ClassRegistry::getObject('Contact');
  2805. $result = $this->Form->password('Contact.field');
  2806. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2807. $Contact->validationErrors['passwd'] = 1;
  2808. $this->Form->request->data['Contact']['passwd'] = 'test';
  2809. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2810. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2811. }
  2812. /**
  2813. * testRadio method
  2814. *
  2815. * Test radio element set generation
  2816. *
  2817. * @return void
  2818. */
  2819. public function testRadio() {
  2820. $result = $this->Form->radio('Model.field', array('option A'));
  2821. $expected = array(
  2822. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2823. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2824. 'label' => array('for' => 'ModelField0'),
  2825. 'option A',
  2826. '/label'
  2827. );
  2828. $this->assertTags($result, $expected);
  2829. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2830. $expected = array(
  2831. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2832. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2833. 'label' => array('for' => 'ModelField12'),
  2834. 'half',
  2835. '/label'
  2836. );
  2837. $this->assertTags($result, $expected);
  2838. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2839. $expected = array(
  2840. 'fieldset' => array(),
  2841. 'legend' => array(),
  2842. 'Field',
  2843. '/legend',
  2844. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2845. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2846. array('label' => array('for' => 'ModelField0')),
  2847. 'option A',
  2848. '/label',
  2849. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2850. array('label' => array('for' => 'ModelField1')),
  2851. 'option B',
  2852. '/label',
  2853. '/fieldset'
  2854. );
  2855. $this->assertTags($result, $expected);
  2856. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  2857. $expected = array(
  2858. 'fieldset' => array(),
  2859. 'legend' => array(),
  2860. 'Field',
  2861. '/legend',
  2862. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2863. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2864. array('label' => array('for' => 'ModelField0')),
  2865. 'option A',
  2866. '/label',
  2867. 'br' => array(),
  2868. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2869. array('label' => array('for' => 'ModelField1')),
  2870. 'option B',
  2871. '/label',
  2872. '/fieldset'
  2873. );
  2874. $this->assertTags($result, $expected);
  2875. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  2876. $expected = array(
  2877. 'fieldset' => array(),
  2878. 'legend' => array(),
  2879. 'Field',
  2880. '/legend',
  2881. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'checked' => 'checked')),
  2882. array('label' => array('for' => 'ModelField1')),
  2883. 'Yes',
  2884. '/label',
  2885. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2886. array('label' => array('for' => 'ModelField0')),
  2887. 'No',
  2888. '/label',
  2889. '/fieldset'
  2890. );
  2891. $this->assertTags($result, $expected);
  2892. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  2893. $expected = array(
  2894. 'fieldset' => array(),
  2895. 'legend' => array(),
  2896. 'Field',
  2897. '/legend',
  2898. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2899. array('label' => array('for' => 'ModelField1')),
  2900. 'Yes',
  2901. '/label',
  2902. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  2903. array('label' => array('for' => 'ModelField0')),
  2904. 'No',
  2905. '/label',
  2906. '/fieldset'
  2907. );
  2908. $this->assertTags($result, $expected);
  2909. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  2910. $expected = array(
  2911. 'fieldset' => array(),
  2912. 'legend' => array(),
  2913. 'Field',
  2914. '/legend',
  2915. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2916. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2917. array('label' => array('for' => 'ModelField1')),
  2918. 'Yes',
  2919. '/label',
  2920. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2921. array('label' => array('for' => 'ModelField0')),
  2922. 'No',
  2923. '/label',
  2924. '/fieldset'
  2925. );
  2926. $this->assertTags($result, $expected);
  2927. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  2928. $expected = array(
  2929. 'fieldset' => array(),
  2930. 'legend' => array(),
  2931. 'Field',
  2932. '/legend',
  2933. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2934. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2935. array('label' => array('for' => 'ModelField1')),
  2936. 'Yes',
  2937. '/label',
  2938. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2939. array('label' => array('for' => 'ModelField0')),
  2940. 'No',
  2941. '/label',
  2942. '/fieldset'
  2943. );
  2944. $this->assertTags($result, $expected);
  2945. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2946. $expected = array(
  2947. 'div' => array('class' => 'input radio'),
  2948. 'fieldset' => array(),
  2949. 'legend' => array(),
  2950. 'Legend title',
  2951. '/legend',
  2952. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2953. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2954. array('label' => array('for' => 'NewsletterSubscribe0')),
  2955. 'Unsubscribe',
  2956. '/label',
  2957. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2958. array('label' => array('for' => 'NewsletterSubscribe1')),
  2959. 'Subscribe',
  2960. '/label',
  2961. '/fieldset',
  2962. '/div'
  2963. );
  2964. $this->assertTags($result, $expected);
  2965. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2966. $expected = array(
  2967. 'div' => array('class' => 'input radio'),
  2968. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2969. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2970. array('label' => array('for' => 'NewsletterSubscribe0')),
  2971. 'Unsubscribe',
  2972. '/label',
  2973. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2974. array('label' => array('for' => 'NewsletterSubscribe1')),
  2975. 'Subscribe',
  2976. '/label',
  2977. '/div'
  2978. );
  2979. $this->assertTags($result, $expected);
  2980. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2981. $expected = array(
  2982. 'div' => array('class' => 'input radio'),
  2983. 'fieldset' => array(),
  2984. 'legend' => array(),
  2985. 'Legend title',
  2986. '/legend',
  2987. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2988. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2989. 'Unsubscribe',
  2990. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2991. 'Subscribe',
  2992. '/fieldset',
  2993. '/div'
  2994. );
  2995. $this->assertTags($result, $expected);
  2996. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2997. $expected = array(
  2998. 'div' => array('class' => 'input radio'),
  2999. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3000. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3001. 'Unsubscribe',
  3002. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3003. 'Subscribe',
  3004. '/div'
  3005. );
  3006. $this->assertTags($result, $expected);
  3007. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3008. $expected = array(
  3009. 'div' => array('class' => 'input radio'),
  3010. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3011. 'Unsubscribe',
  3012. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  3013. 'Subscribe',
  3014. '/div'
  3015. );
  3016. $this->assertTags($result, $expected);
  3017. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3018. $expected = array(
  3019. 'fieldset' => array(),
  3020. 'legend' => array(),
  3021. 'Gender',
  3022. '/legend',
  3023. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3024. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3025. array('label' => array('for' => 'EmployeeGenderMale')),
  3026. 'Male',
  3027. '/label',
  3028. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3029. array('label' => array('for' => 'EmployeeGenderFemale')),
  3030. 'Female',
  3031. '/label',
  3032. '/fieldset',
  3033. );
  3034. $this->assertTags($result, $expected);
  3035. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3036. $expected = array(
  3037. 'fieldset' => array(),
  3038. 'legend' => array(),
  3039. 'Gender',
  3040. '/legend',
  3041. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3042. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3043. array('label' => array('for' => 'OfficerGenderMale')),
  3044. 'Male',
  3045. '/label',
  3046. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3047. array('label' => array('for' => 'OfficerGenderFemale')),
  3048. 'Female',
  3049. '/label',
  3050. '/fieldset',
  3051. );
  3052. $this->assertTags($result, $expected);
  3053. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3054. $expected = array(
  3055. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3056. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3057. 'label' => array('for' => 'Contact1Imrequired0'),
  3058. 'option A',
  3059. '/label'
  3060. );
  3061. $this->assertTags($result, $expected);
  3062. $result = $this->Form->radio('Model.1.field', array('option A'));
  3063. $expected = array(
  3064. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3065. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3066. 'label' => array('for' => 'Model1Field0'),
  3067. 'option A',
  3068. '/label'
  3069. );
  3070. $this->assertTags($result, $expected);
  3071. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3072. $expected = array(
  3073. 'fieldset' => array(),
  3074. 'legend' => array(),
  3075. 'Field',
  3076. '/legend',
  3077. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3078. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3079. array('label' => array('for' => 'ModelField0')),
  3080. 'option A',
  3081. '/label',
  3082. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3083. array('label' => array('for' => 'ModelField1')),
  3084. 'option B',
  3085. '/label',
  3086. '/fieldset'
  3087. );
  3088. $this->assertTags($result, $expected);
  3089. $result = $this->Form->radio(
  3090. 'Model.field',
  3091. array('option A', 'option B'),
  3092. array('between' => 'I am between')
  3093. );
  3094. $expected = array(
  3095. 'fieldset' => array(),
  3096. 'legend' => array(),
  3097. 'Field',
  3098. '/legend',
  3099. 'I am between',
  3100. 'input' => array(
  3101. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3102. 'value' => '', 'id' => 'ModelField_'
  3103. ),
  3104. array('input' => array(
  3105. 'type' => 'radio', 'name' => 'data[Model][field]',
  3106. 'value' => '0', 'id' => 'ModelField0'
  3107. )),
  3108. array('label' => array('for' => 'ModelField0')),
  3109. 'option A',
  3110. '/label',
  3111. array('input' => array(
  3112. 'type' => 'radio', 'name' => 'data[Model][field]',
  3113. 'value' => '1', 'id' => 'ModelField1'
  3114. )),
  3115. array('label' => array('for' => 'ModelField1')),
  3116. 'option B',
  3117. '/label',
  3118. '/fieldset'
  3119. );
  3120. $this->assertTags($result, $expected);
  3121. }
  3122. /**
  3123. * test disabled radio options
  3124. *
  3125. * @return void
  3126. */
  3127. public function testRadioDisabled() {
  3128. $result = $this->Form->radio(
  3129. 'Model.field',
  3130. array('option A', 'option B'),
  3131. array('disabled' => array('option A'), 'value' => 'option A')
  3132. );
  3133. $expected = array(
  3134. 'fieldset' => array(),
  3135. 'legend' => array(),
  3136. 'Field',
  3137. '/legend',
  3138. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3139. array('label' => array('for' => 'ModelField0')),
  3140. 'option A',
  3141. '/label',
  3142. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3143. array('label' => array('for' => 'ModelField1')),
  3144. 'option B',
  3145. '/label',
  3146. '/fieldset'
  3147. );
  3148. $this->assertTags($result, $expected);
  3149. $result = $this->Form->radio(
  3150. 'Model.field',
  3151. array('option A', 'option B'),
  3152. array('disabled' => true, 'value' => 'option A')
  3153. );
  3154. $expected = array(
  3155. 'fieldset' => array(),
  3156. 'legend' => array(),
  3157. 'Field',
  3158. '/legend',
  3159. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3160. array('label' => array('for' => 'ModelField0')),
  3161. 'option A',
  3162. '/label',
  3163. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3164. array('label' => array('for' => 'ModelField1')),
  3165. 'option B',
  3166. '/label',
  3167. '/fieldset'
  3168. );
  3169. $this->assertTags($result, $expected);
  3170. $result = $this->Form->radio(
  3171. 'Model.field',
  3172. array('option A', 'option B'),
  3173. array('disabled' => 'disabled', 'value' => 'option A')
  3174. );
  3175. $expected = array(
  3176. 'fieldset' => array(),
  3177. 'legend' => array(),
  3178. 'Field',
  3179. '/legend',
  3180. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  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', 'disabled' => 'disabled')),
  3185. array('label' => array('for' => 'ModelField1')),
  3186. 'option B',
  3187. '/label',
  3188. '/fieldset'
  3189. );
  3190. $this->assertTags($result, $expected);
  3191. }
  3192. /**
  3193. * test disabling the hidden input for radio buttons
  3194. *
  3195. * @return void
  3196. */
  3197. public function testRadioHiddenInputDisabling() {
  3198. $result = $this->Form->input('Model.1.field', array(
  3199. 'type' => 'radio',
  3200. 'options' => array('option A'),
  3201. 'hiddenField' => false
  3202. )
  3203. );
  3204. $expected = array(
  3205. 'div' => array('class' => 'input radio'),
  3206. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3207. 'label' => array('for' => 'Model1Field0'),
  3208. 'option A',
  3209. '/label',
  3210. '/div'
  3211. );
  3212. $this->assertTags($result, $expected);
  3213. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3214. $expected = array(
  3215. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3216. 'label' => array('for' => 'Model1Field0'),
  3217. 'option A',
  3218. '/label'
  3219. );
  3220. $this->assertTags($result, $expected);
  3221. }
  3222. /**
  3223. * test adding an empty option for radio buttons
  3224. *
  3225. * @return void
  3226. */
  3227. public function testRadioAddEmptyOption() {
  3228. $result = $this->Form->input('Model.1.field', array(
  3229. 'type' => 'radio',
  3230. 'options' => array('option A'),
  3231. 'empty' => true,
  3232. 'hiddenField' => false
  3233. ));
  3234. $expected = array(
  3235. 'div' => array('class' => 'input radio'),
  3236. 'fieldset' => array(),
  3237. 'legend' => array(),
  3238. 'Field',
  3239. '/legend',
  3240. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3241. array('label' => array('for' => 'Model1Field')),
  3242. __('empty'),
  3243. '/label',
  3244. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3245. array('label' => array('for' => 'Model1Field0')),
  3246. 'option A',
  3247. '/label',
  3248. '/fieldset',
  3249. '/div'
  3250. );
  3251. $this->assertTags($result, $expected);
  3252. $result = $this->Form->input('Model.1.field', array(
  3253. 'type' => 'radio',
  3254. 'options' => array('option A'),
  3255. 'empty' => 'CustomEmptyLabel',
  3256. 'hiddenField' => false
  3257. ));
  3258. $expected = array(
  3259. 'div' => array('class' => 'input radio'),
  3260. 'fieldset' => array(),
  3261. 'legend' => array(),
  3262. 'Field',
  3263. '/legend',
  3264. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3265. array('label' => array('for' => 'Model1Field')),
  3266. 'CustomEmptyLabel',
  3267. '/label',
  3268. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3269. array('label' => array('for' => 'Model1Field0')),
  3270. 'option A',
  3271. '/label',
  3272. '/fieldset',
  3273. '/div'
  3274. );
  3275. $this->assertTags($result, $expected);
  3276. $result = $this->Form->input('Model.1.field', array(
  3277. 'type' => 'radio',
  3278. 'options' => array('option A'),
  3279. 'empty' => false,
  3280. 'hiddenField' => false
  3281. ));
  3282. $this->assertTextNotContains('"Model1Field"', $result);
  3283. }
  3284. /**
  3285. * testSelect method
  3286. *
  3287. * Test select element generation.
  3288. *
  3289. * @return void
  3290. */
  3291. public function testSelect() {
  3292. $result = $this->Form->select('Model.field', array());
  3293. $expected = array(
  3294. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3295. array('option' => array('value' => '')),
  3296. '/option',
  3297. '/select'
  3298. );
  3299. $this->assertTags($result, $expected);
  3300. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3301. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3302. $expected = array(
  3303. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3304. array('option' => array('value' => '')),
  3305. '/option',
  3306. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3307. 'good',
  3308. '/option',
  3309. array('option' => array('value' => 'other')),
  3310. 'bad',
  3311. '/option',
  3312. '/select'
  3313. );
  3314. $this->assertTags($result, $expected);
  3315. $this->Form->request->data = array();
  3316. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3317. $expected = array(
  3318. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3319. array('option' => array('value' => '')),
  3320. '/option',
  3321. array('option' => array('value' => 'value')),
  3322. 'good',
  3323. '/option',
  3324. array('option' => array('value' => 'other')),
  3325. 'bad',
  3326. '/option',
  3327. '/select'
  3328. );
  3329. $this->assertTags($result, $expected);
  3330. $result = $this->Form->select(
  3331. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3332. array('empty' => false)
  3333. );
  3334. $expected = array(
  3335. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3336. array('option' => array('value' => 'first')),
  3337. 'first &quot;html&quot; &lt;chars&gt;',
  3338. '/option',
  3339. array('option' => array('value' => 'second')),
  3340. 'value',
  3341. '/option',
  3342. '/select'
  3343. );
  3344. $this->assertTags($result, $expected);
  3345. $result = $this->Form->select(
  3346. 'Model.field',
  3347. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3348. array('escape' => false, 'empty' => false)
  3349. );
  3350. $expected = array(
  3351. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3352. array('option' => array('value' => 'first')),
  3353. 'first "html" <chars>',
  3354. '/option',
  3355. array('option' => array('value' => 'second')),
  3356. 'value',
  3357. '/option',
  3358. '/select'
  3359. );
  3360. $this->assertTags($result, $expected);
  3361. $options = array(
  3362. array('value' => 'first', 'name' => 'First'),
  3363. array('value' => 'first', 'name' => 'Another First'),
  3364. );
  3365. $result = $this->Form->select(
  3366. 'Model.field',
  3367. $options,
  3368. array('escape' => false, 'empty' => false)
  3369. );
  3370. $expected = array(
  3371. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3372. array('option' => array('value' => 'first')),
  3373. 'First',
  3374. '/option',
  3375. array('option' => array('value' => 'first')),
  3376. 'Another First',
  3377. '/option',
  3378. '/select'
  3379. );
  3380. $this->assertTags($result, $expected);
  3381. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3382. $result = $this->Form->select(
  3383. 'Model.contact_id',
  3384. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3385. array('escape' => false, 'empty' => 'pick something')
  3386. );
  3387. $expected = array(
  3388. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3389. array('option' => array('value' => '')), 'pick something', '/option',
  3390. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3391. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3392. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3393. '/select'
  3394. );
  3395. $this->assertTags($result, $expected);
  3396. $this->Form->request->data['Model']['field'] = 0;
  3397. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3398. $expected = array(
  3399. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3400. array('option' => array('value' => '')), '/option',
  3401. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3402. array('option' => array('value' => '1')), 'Yes', '/option',
  3403. '/select'
  3404. );
  3405. $this->assertTags($result, $expected);
  3406. }
  3407. /**
  3408. * test that select() with optiongroups listens to the escape param.
  3409. *
  3410. * @return void
  3411. */
  3412. public function testSelectOptionGroupEscaping() {
  3413. $options = array(
  3414. '>< Key' => array(
  3415. 1 => 'One',
  3416. 2 => 'Two'
  3417. )
  3418. );
  3419. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3420. $expected = array(
  3421. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3422. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3423. array('option' => array('value' => '1')), 'One', '/option',
  3424. array('option' => array('value' => '2')), 'Two', '/option',
  3425. '/optgroup',
  3426. '/select'
  3427. );
  3428. $this->assertTags($result, $expected);
  3429. $options = array(
  3430. '>< Key' => array(
  3431. 1 => 'One',
  3432. 2 => 'Two'
  3433. )
  3434. );
  3435. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3436. $expected = array(
  3437. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3438. 'optgroup' => array('label' => '>< Key'),
  3439. array('option' => array('value' => '1')), 'One', '/option',
  3440. array('option' => array('value' => '2')), 'Two', '/option',
  3441. '/optgroup',
  3442. '/select'
  3443. );
  3444. $this->assertTags($result, $expected);
  3445. }
  3446. /**
  3447. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3448. *
  3449. * @return void
  3450. */
  3451. public function testSelectWithNullAttributes() {
  3452. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3453. $expected = array(
  3454. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3455. array('option' => array('value' => '0')),
  3456. 'first',
  3457. '/option',
  3458. array('option' => array('value' => '1')),
  3459. 'second',
  3460. '/option',
  3461. '/select'
  3462. );
  3463. $this->assertTags($result, $expected);
  3464. }
  3465. /**
  3466. * testNestedSelect method
  3467. *
  3468. * test select element generation with optgroups
  3469. *
  3470. * @return void
  3471. */
  3472. public function testNestedSelect() {
  3473. $result = $this->Form->select(
  3474. 'Model.field',
  3475. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3476. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3477. )), array('empty' => false)
  3478. );
  3479. $expected = array(
  3480. 'select' => array('name' => 'data[Model][field]',
  3481. 'id' => 'ModelField'),
  3482. array('option' => array('value' => 1)),
  3483. 'One',
  3484. '/option',
  3485. array('option' => array('value' => 2)),
  3486. 'Two',
  3487. '/option',
  3488. array('optgroup' => array('label' => 'Three')),
  3489. array('option' => array('value' => 4)),
  3490. 'Four',
  3491. '/option',
  3492. array('option' => array('value' => 5)),
  3493. 'Five',
  3494. '/option',
  3495. '/optgroup',
  3496. '/select'
  3497. );
  3498. $this->assertTags($result, $expected);
  3499. $result = $this->Form->select(
  3500. 'Model.field',
  3501. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3502. array('showParents' => true, 'empty' => false)
  3503. );
  3504. $expected = array(
  3505. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3506. array('option' => array('value' => 1)),
  3507. 'One',
  3508. '/option',
  3509. array('option' => array('value' => 2)),
  3510. 'Two',
  3511. '/option',
  3512. array('optgroup' => array('label' => 'Three')),
  3513. array('option' => array('value' => 3)),
  3514. 'Three',
  3515. '/option',
  3516. array('option' => array('value' => 4)),
  3517. 'Four',
  3518. '/option',
  3519. '/optgroup',
  3520. '/select'
  3521. );
  3522. $this->assertTags($result, $expected);
  3523. }
  3524. /**
  3525. * testSelectMultiple method
  3526. *
  3527. * test generation of multiple select elements
  3528. *
  3529. * @return void
  3530. */
  3531. public function testSelectMultiple() {
  3532. $options = array('first', 'second', 'third');
  3533. $result = $this->Form->select(
  3534. 'Model.multi_field', $options, array('multiple' => true)
  3535. );
  3536. $expected = array(
  3537. 'input' => array(
  3538. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3539. ),
  3540. 'select' => array(
  3541. 'name' => 'data[Model][multi_field][]',
  3542. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3543. ),
  3544. array('option' => array('value' => '0')),
  3545. 'first',
  3546. '/option',
  3547. array('option' => array('value' => '1')),
  3548. 'second',
  3549. '/option',
  3550. array('option' => array('value' => '2')),
  3551. 'third',
  3552. '/option',
  3553. '/select'
  3554. );
  3555. $this->assertTags($result, $expected);
  3556. $result = $this->Form->select(
  3557. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3558. );
  3559. $expected = array(
  3560. 'input' => array(
  3561. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3562. ),
  3563. 'select' => array(
  3564. 'name' => 'data[Model][multi_field][]',
  3565. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3566. ),
  3567. array('option' => array('value' => '0')),
  3568. 'first',
  3569. '/option',
  3570. array('option' => array('value' => '1')),
  3571. 'second',
  3572. '/option',
  3573. array('option' => array('value' => '2')),
  3574. 'third',
  3575. '/option',
  3576. '/select'
  3577. );
  3578. $this->assertTags($result, $expected);
  3579. $result = $this->Form->select(
  3580. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3581. );
  3582. $expected = array(
  3583. 'input' => array(
  3584. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3585. ),
  3586. 'select' => array(
  3587. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3588. 'multiple' => 'multiple'
  3589. ),
  3590. array('option' => array('value' => '0', 'selected' => 'selected')),
  3591. 'first',
  3592. '/option',
  3593. array('option' => array('value' => '1', 'selected' => 'selected')),
  3594. 'second',
  3595. '/option',
  3596. array('option' => array('value' => '2')),
  3597. 'third',
  3598. '/option',
  3599. '/select'
  3600. );
  3601. $this->assertTags($result, $expected);
  3602. $result = $this->Form->select(
  3603. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3604. );
  3605. $expected = array(
  3606. 'select' => array(
  3607. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3608. ),
  3609. array('option' => array('value' => '0', 'selected' => 'selected')),
  3610. 'first',
  3611. '/option',
  3612. array('option' => array('value' => '1', 'selected' => 'selected')),
  3613. 'second',
  3614. '/option',
  3615. array('option' => array('value' => '2')),
  3616. 'third',
  3617. '/option',
  3618. '/select'
  3619. );
  3620. $this->assertTags($result, $expected);
  3621. }
  3622. /**
  3623. * test generation of habtm select boxes.
  3624. *
  3625. * @return void
  3626. */
  3627. public function testHabtmSelectBox() {
  3628. $this->View->viewVars['contactTags'] = array(
  3629. 1 => 'blue',
  3630. 2 => 'red',
  3631. 3 => 'green'
  3632. );
  3633. $this->Form->request->data = array(
  3634. 'Contact' => array(),
  3635. 'ContactTag' => array(
  3636. array(
  3637. 'id' => 1,
  3638. 'name' => 'blue'
  3639. ),
  3640. array(
  3641. 'id' => 3,
  3642. 'name' => 'green'
  3643. )
  3644. )
  3645. );
  3646. $this->Form->create('Contact');
  3647. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3648. $expected = array(
  3649. 'input' => array(
  3650. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3651. ),
  3652. 'select' => array(
  3653. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3654. 'multiple' => 'multiple'
  3655. ),
  3656. array('option' => array('value' => '1', 'selected' => 'selected')),
  3657. 'blue',
  3658. '/option',
  3659. array('option' => array('value' => '2')),
  3660. 'red',
  3661. '/option',
  3662. array('option' => array('value' => '3', 'selected' => 'selected')),
  3663. 'green',
  3664. '/option',
  3665. '/select'
  3666. );
  3667. $this->assertTags($result, $expected);
  3668. }
  3669. /**
  3670. * test generation of multi select elements in checkbox format
  3671. *
  3672. * @return void
  3673. */
  3674. public function testSelectMultipleCheckboxes() {
  3675. $result = $this->Form->select(
  3676. 'Model.multi_field',
  3677. array('first', 'second', 'third'),
  3678. array('multiple' => 'checkbox')
  3679. );
  3680. $expected = array(
  3681. 'input' => array(
  3682. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3683. ),
  3684. array('div' => array('class' => 'checkbox')),
  3685. array('input' => array(
  3686. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3687. 'value' => '0', 'id' => 'ModelMultiField0'
  3688. )),
  3689. array('label' => array('for' => 'ModelMultiField0')),
  3690. 'first',
  3691. '/label',
  3692. '/div',
  3693. array('div' => array('class' => 'checkbox')),
  3694. array('input' => array(
  3695. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3696. 'value' => '1', 'id' => 'ModelMultiField1'
  3697. )),
  3698. array('label' => array('for' => 'ModelMultiField1')),
  3699. 'second',
  3700. '/label',
  3701. '/div',
  3702. array('div' => array('class' => 'checkbox')),
  3703. array('input' => array(
  3704. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3705. 'value' => '2', 'id' => 'ModelMultiField2'
  3706. )),
  3707. array('label' => array('for' => 'ModelMultiField2')),
  3708. 'third',
  3709. '/label',
  3710. '/div'
  3711. );
  3712. $this->assertTags($result, $expected);
  3713. $result = $this->Form->select(
  3714. 'Model.multi_field',
  3715. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3716. array('multiple' => 'checkbox')
  3717. );
  3718. $expected = array(
  3719. 'input' => array(
  3720. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3721. ),
  3722. array('div' => array('class' => 'checkbox')),
  3723. array('input' => array(
  3724. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3725. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3726. )),
  3727. array('label' => array('for' => 'ModelMultiFieldA')),
  3728. 'first',
  3729. '/label',
  3730. '/div',
  3731. array('div' => array('class' => 'checkbox')),
  3732. array('input' => array(
  3733. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3734. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3735. )),
  3736. array('label' => array('for' => 'ModelMultiFieldB')),
  3737. 'second',
  3738. '/label',
  3739. '/div',
  3740. array('div' => array('class' => 'checkbox')),
  3741. array('input' => array(
  3742. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3743. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3744. )),
  3745. array('label' => array('for' => 'ModelMultiFieldC')),
  3746. 'third',
  3747. '/label',
  3748. '/div'
  3749. );
  3750. $this->assertTags($result, $expected);
  3751. $result = $this->Form->select(
  3752. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3753. );
  3754. $expected = array(
  3755. 'input' => array(
  3756. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3757. ),
  3758. array('div' => array('class' => 'checkbox')),
  3759. array('input' => array(
  3760. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3761. 'value' => '1', 'id' => 'ModelMultiField1'
  3762. )),
  3763. array('label' => array('for' => 'ModelMultiField1')),
  3764. 'first',
  3765. '/label',
  3766. '/div'
  3767. );
  3768. $this->assertTags($result, $expected);
  3769. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3770. $result = $this->Form->select(
  3771. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3772. );
  3773. $expected = array(
  3774. 'input' => array(
  3775. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3776. ),
  3777. array('div' => array('class' => 'checkbox')),
  3778. array('input' => array(
  3779. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3780. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3781. )),
  3782. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3783. 'first',
  3784. '/label',
  3785. '/div',
  3786. array('div' => array('class' => 'checkbox')),
  3787. array('input' => array(
  3788. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3789. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3790. )),
  3791. array('label' => array('for' => 'ModelTagsArray')),
  3792. 'Array',
  3793. '/label',
  3794. '/div'
  3795. );
  3796. $this->assertTags($result, $expected);
  3797. }
  3798. /**
  3799. * test multiple checkboxes with div styles.
  3800. *
  3801. * @return void
  3802. */
  3803. public function testSelectMultipleCheckboxDiv() {
  3804. $result = $this->Form->select(
  3805. 'Model.tags',
  3806. array('first', 'second'),
  3807. array('multiple' => 'checkbox', 'class' => 'my-class')
  3808. );
  3809. $expected = array(
  3810. 'input' => array(
  3811. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3812. ),
  3813. array('div' => array('class' => 'my-class')),
  3814. array('input' => array(
  3815. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3816. 'value' => '0', 'id' => 'ModelTags0'
  3817. )),
  3818. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3819. '/div',
  3820. array('div' => array('class' => 'my-class')),
  3821. array('input' => array(
  3822. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3823. 'value' => '1', 'id' => 'ModelTags1'
  3824. )),
  3825. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  3826. '/div'
  3827. );
  3828. $this->assertTags($result, $expected);
  3829. $result = $this->Form->input('Model.tags', array(
  3830. 'options' => array('first', 'second'),
  3831. 'multiple' => 'checkbox',
  3832. 'class' => 'my-class',
  3833. 'div' => false,
  3834. 'label' => false
  3835. ));
  3836. $this->assertTags($result, $expected);
  3837. $Contact = ClassRegistry::getObject('Contact');
  3838. $Contact->validationErrors['tags'] = 'Select atleast one option';
  3839. $result = $this->Form->input('Contact.tags', array(
  3840. 'options' => array('one'),
  3841. 'multiple' => 'checkbox',
  3842. 'label' => false,
  3843. 'div' => false
  3844. ));
  3845. $expected = array(
  3846. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3847. array('div' => array('class' => 'checkbox form-error')),
  3848. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3849. array('label' => array('for' => 'ContactTags0')),
  3850. 'one',
  3851. '/label',
  3852. '/div'
  3853. );
  3854. $this->assertTags($result, $expected);
  3855. $result = $this->Form->input('Contact.tags', array(
  3856. 'options' => array('one'),
  3857. 'multiple' => 'checkbox',
  3858. 'class' => 'mycheckbox',
  3859. 'label' => false,
  3860. 'div' => false
  3861. ));
  3862. $expected = array(
  3863. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3864. array('div' => array('class' => 'mycheckbox form-error')),
  3865. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3866. array('label' => array('for' => 'ContactTags0')),
  3867. 'one',
  3868. '/label',
  3869. '/div'
  3870. );
  3871. $this->assertTags($result, $expected);
  3872. }
  3873. /**
  3874. * Checks the security hash array generated for multiple-input checkbox elements
  3875. *
  3876. * @return void
  3877. */
  3878. public function testSelectMultipleCheckboxSecurity() {
  3879. $this->Form->request['_Token'] = array('key' => 'testKey');
  3880. $this->assertEquals(array(), $this->Form->fields);
  3881. $result = $this->Form->select(
  3882. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3883. array('multiple' => 'checkbox')
  3884. );
  3885. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3886. $result = $this->Form->secure($this->Form->fields);
  3887. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3888. $this->assertRegExp('/"' . $key . '"/', $result);
  3889. }
  3890. /**
  3891. * Multiple select elements should always be secured as they always participate
  3892. * in the POST data.
  3893. *
  3894. * @return void
  3895. */
  3896. public function testSelectMultipleSecureWithNoOptions() {
  3897. $this->Form->request['_Token'] = array('key' => 'testkey');
  3898. $this->assertEquals(array(), $this->Form->fields);
  3899. $result = $this->Form->select(
  3900. 'Model.select',
  3901. array(),
  3902. array('multiple' => true)
  3903. );
  3904. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3905. }
  3906. /**
  3907. * When a select box has no options it should not be added to the fields list
  3908. * as it always fail post validation.
  3909. *
  3910. * @return void
  3911. */
  3912. public function testSelectNoSecureWithNoOptions() {
  3913. $this->Form->request['_Token'] = array('key' => 'testkey');
  3914. $this->assertEquals(array(), $this->Form->fields);
  3915. $this->Form->select(
  3916. 'Model.select',
  3917. array()
  3918. );
  3919. $this->assertEquals(array(), $this->Form->fields);
  3920. $this->Form->select(
  3921. 'Model.select',
  3922. array(),
  3923. array('empty' => true)
  3924. );
  3925. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3926. }
  3927. /**
  3928. * testInputMultipleCheckboxes method
  3929. *
  3930. * test input() resulting in multi select elements being generated.
  3931. *
  3932. * @return void
  3933. */
  3934. public function testInputMultipleCheckboxes() {
  3935. $result = $this->Form->input('Model.multi_field', array(
  3936. 'options' => array('first', 'second', 'third'),
  3937. 'multiple' => 'checkbox'
  3938. ));
  3939. $expected = array(
  3940. array('div' => array('class' => 'input select')),
  3941. array('label' => array('for' => 'ModelMultiField')),
  3942. 'Multi Field',
  3943. '/label',
  3944. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3945. array('div' => array('class' => 'checkbox')),
  3946. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3947. array('label' => array('for' => 'ModelMultiField0')),
  3948. 'first',
  3949. '/label',
  3950. '/div',
  3951. array('div' => array('class' => 'checkbox')),
  3952. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3953. array('label' => array('for' => 'ModelMultiField1')),
  3954. 'second',
  3955. '/label',
  3956. '/div',
  3957. array('div' => array('class' => 'checkbox')),
  3958. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3959. array('label' => array('for' => 'ModelMultiField2')),
  3960. 'third',
  3961. '/label',
  3962. '/div',
  3963. '/div'
  3964. );
  3965. $this->assertTags($result, $expected);
  3966. $result = $this->Form->input('Model.multi_field', array(
  3967. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3968. 'multiple' => 'checkbox'
  3969. ));
  3970. $expected = array(
  3971. array('div' => array('class' => 'input select')),
  3972. array('label' => array('for' => 'ModelMultiField')),
  3973. 'Multi Field',
  3974. '/label',
  3975. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3976. array('div' => array('class' => 'checkbox')),
  3977. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  3978. array('label' => array('for' => 'ModelMultiFieldA')),
  3979. 'first',
  3980. '/label',
  3981. '/div',
  3982. array('div' => array('class' => 'checkbox')),
  3983. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  3984. array('label' => array('for' => 'ModelMultiFieldB')),
  3985. 'second',
  3986. '/label',
  3987. '/div',
  3988. array('div' => array('class' => 'checkbox')),
  3989. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  3990. array('label' => array('for' => 'ModelMultiFieldC')),
  3991. 'third',
  3992. '/label',
  3993. '/div',
  3994. '/div'
  3995. );
  3996. $this->assertTags($result, $expected);
  3997. $result = $this->Form->input('Model.multi_field', array(
  3998. 'options' => array('1' => 'first'),
  3999. 'multiple' => 'checkbox',
  4000. 'label' => false,
  4001. 'div' => false
  4002. ));
  4003. $expected = array(
  4004. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4005. array('div' => array('class' => 'checkbox')),
  4006. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4007. array('label' => array('for' => 'ModelMultiField1')),
  4008. 'first',
  4009. '/label',
  4010. '/div'
  4011. );
  4012. $this->assertTags($result, $expected);
  4013. $result = $this->Form->input('Model.multi_field', array(
  4014. 'options' => array('2' => 'second'),
  4015. 'multiple' => 'checkbox',
  4016. 'label' => false,
  4017. 'div' => false
  4018. ));
  4019. $expected = array(
  4020. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4021. array('div' => array('class' => 'checkbox')),
  4022. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4023. array('label' => array('for' => 'ModelMultiField2')),
  4024. 'second',
  4025. '/label',
  4026. '/div'
  4027. );
  4028. $this->assertTags($result, $expected);
  4029. }
  4030. /**
  4031. * testSelectHiddenFieldOmission method
  4032. *
  4033. * test that select() with 'hiddenField' => false omits the hidden field
  4034. *
  4035. * @return void
  4036. */
  4037. public function testSelectHiddenFieldOmission() {
  4038. $result = $this->Form->select('Model.multi_field',
  4039. array('first', 'second'),
  4040. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4041. );
  4042. $expected = array(
  4043. array('div' => array('class' => 'checkbox')),
  4044. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4045. array('label' => array('for' => 'ModelMultiField0')),
  4046. 'first',
  4047. '/label',
  4048. '/div',
  4049. array('div' => array('class' => 'checkbox')),
  4050. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4051. array('label' => array('for' => 'ModelMultiField1')),
  4052. 'second',
  4053. '/label',
  4054. '/div'
  4055. );
  4056. $this->assertTags($result, $expected);
  4057. $result = $this->Form->input('Model.multi_field', array(
  4058. 'options' => array('first', 'second'),
  4059. 'multiple' => 'checkbox',
  4060. 'hiddenField' => false
  4061. ));
  4062. $expected = array(
  4063. array('div' => array('class' => 'input select')),
  4064. array('label' => array('for' => 'ModelMultiField')),
  4065. 'Multi Field',
  4066. '/label',
  4067. array('div' => array('class' => 'checkbox')),
  4068. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4069. array('label' => array('for' => 'ModelMultiField0')),
  4070. 'first',
  4071. '/label',
  4072. '/div',
  4073. array('div' => array('class' => 'checkbox')),
  4074. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4075. array('label' => array('for' => 'ModelMultiField1')),
  4076. 'second',
  4077. '/label',
  4078. '/div',
  4079. '/div'
  4080. );
  4081. $this->assertTags($result, $expected);
  4082. }
  4083. /**
  4084. * test that select() with multiple = checkbox works with overriding name attribute.
  4085. *
  4086. * @return void
  4087. */
  4088. public function testSelectCheckboxMultipleOverrideName() {
  4089. $result = $this->Form->input('category', array(
  4090. 'type' => 'select',
  4091. 'multiple' => 'checkbox',
  4092. 'name' => 'data[fish]',
  4093. 'options' => array('1', '2'),
  4094. 'div' => false,
  4095. 'label' => false,
  4096. ));
  4097. $expected = array(
  4098. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4099. array('div' => array('class' => 'checkbox')),
  4100. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4101. array('label' => array('for' => 'Category0')), '1', '/label',
  4102. '/div',
  4103. array('div' => array('class' => 'checkbox')),
  4104. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4105. array('label' => array('for' => 'Category1')), '2', '/label',
  4106. '/div'
  4107. );
  4108. $this->assertTags($result, $expected);
  4109. }
  4110. /**
  4111. * Test that 'id' overrides all the checkbox id's as well.
  4112. *
  4113. * @return void
  4114. */
  4115. public function testSelectCheckboxMultipleId() {
  4116. $result = $this->Form->select(
  4117. 'Model.multi_field',
  4118. array('first', 'second', 'third'),
  4119. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4120. );
  4121. $expected = array(
  4122. 'input' => array(
  4123. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4124. ),
  4125. array('div' => array('class' => 'checkbox')),
  4126. array('input' => array(
  4127. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4128. 'value' => '0', 'id' => 'CustomId0'
  4129. )),
  4130. array('label' => array('for' => 'CustomId0')),
  4131. 'first',
  4132. '/label',
  4133. '/div',
  4134. array('div' => array('class' => 'checkbox')),
  4135. array('input' => array(
  4136. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4137. 'value' => '1', 'id' => 'CustomId1'
  4138. )),
  4139. array('label' => array('for' => 'CustomId1')),
  4140. 'second',
  4141. '/label',
  4142. '/div',
  4143. array('div' => array('class' => 'checkbox')),
  4144. array('input' => array(
  4145. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4146. 'value' => '2', 'id' => 'CustomId2'
  4147. )),
  4148. array('label' => array('for' => 'CustomId2')),
  4149. 'third',
  4150. '/label',
  4151. '/div'
  4152. );
  4153. $this->assertTags($result, $expected);
  4154. }
  4155. /**
  4156. * testCheckbox method
  4157. *
  4158. * Test generation of checkboxes
  4159. *
  4160. * @return void
  4161. */
  4162. public function testCheckbox() {
  4163. $result = $this->Form->checkbox('Model.field');
  4164. $expected = array(
  4165. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4166. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4167. );
  4168. $this->assertTags($result, $expected);
  4169. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4170. $expected = array(
  4171. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4172. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4173. );
  4174. $this->assertTags($result, $expected);
  4175. $Contact = ClassRegistry::getObject('Contact');
  4176. $Contact->validationErrors['field'] = 1;
  4177. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4178. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4179. $expected = array(
  4180. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4181. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4182. );
  4183. $this->assertTags($result, $expected);
  4184. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4185. $expected = array(
  4186. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4187. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4188. );
  4189. $this->assertTags($result, $expected);
  4190. $this->Form->request->data['Contact']['field'] = '';
  4191. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4192. $expected = array(
  4193. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4194. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4195. );
  4196. $this->assertTags($result, $expected);
  4197. $Contact->validationErrors = array();
  4198. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4199. $expected = array(
  4200. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4201. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4202. );
  4203. $this->assertTags($result, $expected);
  4204. $this->Form->request->data['Contact']['published'] = 1;
  4205. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4206. $expected = array(
  4207. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4208. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4209. );
  4210. $this->assertTags($result, $expected);
  4211. $this->Form->request->data['Contact']['published'] = 0;
  4212. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4213. $expected = array(
  4214. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4215. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4216. );
  4217. $this->assertTags($result, $expected);
  4218. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4219. $expected = array(
  4220. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4221. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4222. );
  4223. $this->assertTags($result, $expected);
  4224. $result = $this->Form->checkbox('CustomField.1.value');
  4225. $expected = array(
  4226. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4227. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4228. );
  4229. $this->assertTags($result, $expected);
  4230. }
  4231. /**
  4232. * test checkbox() with a custom name attribute
  4233. *
  4234. * @return void
  4235. */
  4236. public function testCheckboxCustomNameAttribute() {
  4237. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4238. $expected = array(
  4239. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4240. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4241. );
  4242. $this->assertTags($result, $expected);
  4243. }
  4244. /**
  4245. * test the checked option for checkboxes.
  4246. *
  4247. * @return void
  4248. */
  4249. public function testCheckboxCheckedOption() {
  4250. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4251. $expected = array(
  4252. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4253. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4254. );
  4255. $this->assertTags($result, $expected);
  4256. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4257. $expected = array(
  4258. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4259. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4260. );
  4261. $this->assertTags($result, $expected);
  4262. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4263. $expected = array(
  4264. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4265. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4266. );
  4267. $this->assertTags($result, $expected);
  4268. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4269. $expected = array(
  4270. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4271. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4272. );
  4273. $this->assertTags($result, $expected);
  4274. $this->Form->request->data['Model']['field'] = 1;
  4275. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4276. $expected = array(
  4277. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4278. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4279. );
  4280. $this->assertTags($result, $expected);
  4281. }
  4282. /**
  4283. * Test that disabled attribute works on both the checkbox and hidden input.
  4284. *
  4285. * @return void
  4286. */
  4287. public function testCheckboxDisabling() {
  4288. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4289. $expected = array(
  4290. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4291. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4292. );
  4293. $this->assertTags($result, $expected);
  4294. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4295. $expected = array(
  4296. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4297. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4298. );
  4299. $this->assertTags($result, $expected);
  4300. }
  4301. /**
  4302. * Test that the hidden input for checkboxes can be omitted or set to a
  4303. * specific value.
  4304. *
  4305. * @return void
  4306. */
  4307. public function testCheckboxHiddenField() {
  4308. $result = $this->Form->input('UserForm.something', array(
  4309. 'type' => 'checkbox',
  4310. 'hiddenField' => false
  4311. ));
  4312. $expected = array(
  4313. 'div' => array('class' => 'input checkbox'),
  4314. array('input' => array(
  4315. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4316. 'value' => '1', 'id' => 'UserFormSomething'
  4317. )),
  4318. 'label' => array('for' => 'UserFormSomething'),
  4319. 'Something',
  4320. '/label',
  4321. '/div'
  4322. );
  4323. $this->assertTags($result, $expected);
  4324. $result = $this->Form->input('UserForm.something', array(
  4325. 'type' => 'checkbox',
  4326. 'value' => 'Y',
  4327. 'hiddenField' => 'N',
  4328. ));
  4329. $expected = array(
  4330. 'div' => array('class' => 'input checkbox'),
  4331. array('input' => array(
  4332. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4333. 'value' => 'N', 'id' => 'UserFormSomething_'
  4334. )),
  4335. array('input' => array(
  4336. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4337. 'value' => 'Y', 'id' => 'UserFormSomething'
  4338. )),
  4339. 'label' => array('for' => 'UserFormSomething'),
  4340. 'Something',
  4341. '/label',
  4342. '/div'
  4343. );
  4344. $this->assertTags($result, $expected);
  4345. }
  4346. /**
  4347. * testDateTime method
  4348. *
  4349. * Test generation of date/time select elements
  4350. *
  4351. * @return void
  4352. */
  4353. public function testDateTime() {
  4354. extract($this->dateRegex);
  4355. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4356. $now = strtotime('now');
  4357. $expected = array(
  4358. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4359. $daysRegex,
  4360. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4361. date('j', $now),
  4362. '/option',
  4363. '*/select',
  4364. '-',
  4365. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4366. $monthsRegex,
  4367. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4368. date('F', $now),
  4369. '/option',
  4370. '*/select',
  4371. '-',
  4372. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4373. $yearsRegex,
  4374. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4375. date('Y', $now),
  4376. '/option',
  4377. '*/select',
  4378. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4379. $hoursRegex,
  4380. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4381. date('g', $now),
  4382. '/option',
  4383. '*/select',
  4384. ':',
  4385. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4386. $minutesRegex,
  4387. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4388. date('i', $now),
  4389. '/option',
  4390. '*/select',
  4391. ' ',
  4392. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4393. $meridianRegex,
  4394. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4395. date('a', $now),
  4396. '/option',
  4397. '*/select'
  4398. );
  4399. $this->assertTags($result, $expected);
  4400. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4401. $expected = array(
  4402. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4403. $daysRegex,
  4404. array('option' => array('value' => '')),
  4405. '/option',
  4406. '*/select',
  4407. '-',
  4408. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4409. $monthsRegex,
  4410. array('option' => array('value' => '')),
  4411. '/option',
  4412. '*/select',
  4413. '-',
  4414. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4415. $yearsRegex,
  4416. array('option' => array('value' => '')),
  4417. '/option',
  4418. '*/select',
  4419. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4420. $hoursRegex,
  4421. array('option' => array('value' => '')),
  4422. '/option',
  4423. '*/select',
  4424. ':',
  4425. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4426. $minutesRegex,
  4427. array('option' => array('value' => '')),
  4428. '/option',
  4429. '*/select',
  4430. ' ',
  4431. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4432. $meridianRegex,
  4433. array('option' => array('value' => '')),
  4434. '/option',
  4435. '*/select'
  4436. );
  4437. $this->assertTags($result, $expected);
  4438. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4439. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4440. $this->assertTags($result, $expected);
  4441. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4442. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4443. $this->assertTags($result, $expected);
  4444. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4445. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4446. $expected = array(
  4447. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4448. $daysRegex,
  4449. array('option' => array('value' => '')),
  4450. '/option',
  4451. '*/select',
  4452. '-',
  4453. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4454. $monthsRegex,
  4455. array('option' => array('value' => '')),
  4456. '/option',
  4457. '*/select',
  4458. '-',
  4459. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4460. $yearsRegex,
  4461. array('option' => array('value' => '')),
  4462. '/option',
  4463. '*/select',
  4464. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4465. $hoursRegex,
  4466. array('option' => array('value' => '')),
  4467. '/option',
  4468. '*/select',
  4469. ':',
  4470. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4471. $minutesRegex,
  4472. array('option' => array('value' => '')),
  4473. '/option',
  4474. array('option' => array('value' => '00')),
  4475. '00',
  4476. '/option',
  4477. array('option' => array('value' => '05')),
  4478. '05',
  4479. '/option',
  4480. array('option' => array('value' => '10')),
  4481. '10',
  4482. '/option',
  4483. '*/select',
  4484. ' ',
  4485. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4486. $meridianRegex,
  4487. array('option' => array('value' => '')),
  4488. '/option',
  4489. '*/select'
  4490. );
  4491. $this->assertTags($result, $expected);
  4492. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4493. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4494. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4495. $this->Form->request->data['Contact']['data'] = null;
  4496. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4497. $expected = array(
  4498. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4499. $daysRegex,
  4500. array('option' => array('value' => '')),
  4501. '/option',
  4502. '*/select',
  4503. '-',
  4504. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4505. $monthsRegex,
  4506. array('option' => array('value' => '')),
  4507. '/option',
  4508. '*/select',
  4509. '-',
  4510. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4511. $yearsRegex,
  4512. array('option' => array('value' => '')),
  4513. '/option',
  4514. '*/select',
  4515. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4516. $hoursRegex,
  4517. array('option' => array('value' => '')),
  4518. '/option',
  4519. '*/select',
  4520. ':',
  4521. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4522. $minutesRegex,
  4523. array('option' => array('value' => '')),
  4524. '/option',
  4525. '*/select',
  4526. ' ',
  4527. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4528. $meridianRegex,
  4529. array('option' => array('value' => '')),
  4530. '/option',
  4531. '*/select'
  4532. );
  4533. $this->assertTags($result, $expected);
  4534. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4535. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4536. $now = strtotime($this->Form->data['Model']['field']);
  4537. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4538. $expected = array(
  4539. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4540. $daysRegex,
  4541. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4542. date('j', $now),
  4543. '/option',
  4544. '*/select',
  4545. '-',
  4546. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4547. $monthsRegex,
  4548. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4549. date('F', $now),
  4550. '/option',
  4551. '*/select',
  4552. '-',
  4553. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4554. $yearsRegex,
  4555. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4556. date('Y', $now),
  4557. '/option',
  4558. '*/select',
  4559. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4560. $hoursRegex,
  4561. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4562. date('g', $now),
  4563. '/option',
  4564. '*/select',
  4565. ':',
  4566. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4567. $minutesRegex,
  4568. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4569. date('i', $now),
  4570. '/option',
  4571. '*/select',
  4572. ' ',
  4573. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4574. $meridianRegex,
  4575. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4576. date('a', $now),
  4577. '/option',
  4578. '*/select'
  4579. );
  4580. $this->assertTags($result, $expected);
  4581. $selected = strtotime('2008-10-26 12:33:00');
  4582. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4583. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4584. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4585. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4586. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4587. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4588. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4589. $this->Form->create('Contact');
  4590. $result = $this->Form->input('published');
  4591. $now = strtotime('now');
  4592. $expected = array(
  4593. 'div' => array('class' => 'input date'),
  4594. 'label' => array('for' => 'ContactPublishedMonth'),
  4595. 'Published',
  4596. '/label',
  4597. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4598. $monthsRegex,
  4599. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4600. date('F', $now),
  4601. '/option',
  4602. '*/select',
  4603. '-',
  4604. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4605. $daysRegex,
  4606. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4607. date('j', $now),
  4608. '/option',
  4609. '*/select',
  4610. '-',
  4611. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4612. $yearsRegex,
  4613. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4614. date('Y', $now),
  4615. '/option',
  4616. '*/select',
  4617. '/div'
  4618. );
  4619. $this->assertTags($result, $expected);
  4620. $result = $this->Form->input('published2', array('type' => 'date'));
  4621. $now = strtotime('now');
  4622. $expected = array(
  4623. 'div' => array('class' => 'input date'),
  4624. 'label' => array('for' => 'ContactPublished2Month'),
  4625. 'Published2',
  4626. '/label',
  4627. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4628. $monthsRegex,
  4629. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4630. date('F', $now),
  4631. '/option',
  4632. '*/select',
  4633. '-',
  4634. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4635. $daysRegex,
  4636. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4637. date('j', $now),
  4638. '/option',
  4639. '*/select',
  4640. '-',
  4641. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4642. $yearsRegex,
  4643. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4644. date('Y', $now),
  4645. '/option',
  4646. '*/select',
  4647. '/div'
  4648. );
  4649. $this->assertTags($result, $expected);
  4650. $this->Form->create('Contact');
  4651. $result = $this->Form->input('published', array('monthNames' => false));
  4652. $now = strtotime('now');
  4653. $expected = array(
  4654. 'div' => array('class' => 'input date'),
  4655. 'label' => array('for' => 'ContactPublishedMonth'),
  4656. 'Published',
  4657. '/label',
  4658. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4659. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4660. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4661. date('m', $now),
  4662. '/option',
  4663. '*/select',
  4664. '-',
  4665. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4666. $daysRegex,
  4667. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4668. date('j', $now),
  4669. '/option',
  4670. '*/select',
  4671. '-',
  4672. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4673. $yearsRegex,
  4674. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4675. date('Y', $now),
  4676. '/option',
  4677. '*/select',
  4678. '/div'
  4679. );
  4680. $this->assertTags($result, $expected);
  4681. $result = $this->Form->input('published', array('type' => 'time'));
  4682. $now = strtotime('now');
  4683. $expected = array(
  4684. 'div' => array('class' => 'input time'),
  4685. 'label' => array('for' => 'ContactPublishedHour'),
  4686. 'Published',
  4687. '/label',
  4688. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4689. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4690. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4691. date('g', $now),
  4692. '/option',
  4693. '*/select',
  4694. ':',
  4695. );
  4696. $this->assertTags($result, $expected);
  4697. $result = $this->Form->input('published', array(
  4698. 'timeFormat' => 24,
  4699. 'interval' => 5,
  4700. 'selected' => strtotime('2009-09-03 13:37:00'),
  4701. 'type' => 'datetime'
  4702. ));
  4703. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4704. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4705. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4706. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4707. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4708. $this->assertNoErrors();
  4709. $this->Form->request->data['Contact'] = array(
  4710. 'date' => array(
  4711. 'day' => '',
  4712. 'month' => '',
  4713. 'year' => '',
  4714. 'hour' => '',
  4715. 'min' => '',
  4716. 'meridian' => ''
  4717. )
  4718. );
  4719. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4720. }
  4721. /**
  4722. * test that datetime() and default values work.
  4723. *
  4724. * @return void
  4725. */
  4726. public function testDatetimeWithDefault() {
  4727. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4728. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4729. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4730. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4731. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4732. 'default' => '2009-06-01 11:15:30'
  4733. ));
  4734. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4735. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4736. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4737. }
  4738. /**
  4739. * test that bogus non-date time data doesn't cause errors.
  4740. *
  4741. * @return void
  4742. */
  4743. public function testDateTimeWithBogusData() {
  4744. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4745. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4746. }
  4747. /**
  4748. * testDateTimeEmptyAsArray
  4749. *
  4750. * @return void
  4751. */
  4752. public function testDateTimeEmptyAsArray() {
  4753. $result = $this->Form->dateTime('Contact.date',
  4754. 'DMY',
  4755. '12',
  4756. array(
  4757. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4758. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4759. )
  4760. )
  4761. );
  4762. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4763. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4764. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4765. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4766. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4767. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4768. $result = $this->Form->dateTime('Contact.date',
  4769. 'DMY',
  4770. '12',
  4771. array(
  4772. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4773. )
  4774. );
  4775. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4776. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4777. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4778. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4779. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4780. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4781. }
  4782. /**
  4783. * testFormDateTimeMulti method
  4784. *
  4785. * test multiple datetime element generation
  4786. *
  4787. * @return void
  4788. */
  4789. public function testFormDateTimeMulti() {
  4790. extract($this->dateRegex);
  4791. $result = $this->Form->dateTime('Contact.1.updated');
  4792. $expected = array(
  4793. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4794. $daysRegex,
  4795. array('option' => array('value' => '')),
  4796. '/option',
  4797. '*/select',
  4798. '-',
  4799. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4800. $monthsRegex,
  4801. array('option' => array('value' => '')),
  4802. '/option',
  4803. '*/select',
  4804. '-',
  4805. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4806. $yearsRegex,
  4807. array('option' => array('value' => '')),
  4808. '/option',
  4809. '*/select',
  4810. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4811. $hoursRegex,
  4812. array('option' => array('value' => '')),
  4813. '/option',
  4814. '*/select',
  4815. ':',
  4816. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4817. $minutesRegex,
  4818. array('option' => array('value' => '')),
  4819. '/option',
  4820. '*/select',
  4821. ' ',
  4822. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4823. $meridianRegex,
  4824. array('option' => array('value' => '')),
  4825. '/option',
  4826. '*/select'
  4827. );
  4828. $this->assertTags($result, $expected);
  4829. $result = $this->Form->dateTime('Contact.2.updated');
  4830. $expected = array(
  4831. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4832. $daysRegex,
  4833. array('option' => array('value' => '')),
  4834. '/option',
  4835. '*/select',
  4836. '-',
  4837. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  4838. $monthsRegex,
  4839. array('option' => array('value' => '')),
  4840. '/option',
  4841. '*/select',
  4842. '-',
  4843. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  4844. $yearsRegex,
  4845. array('option' => array('value' => '')),
  4846. '/option',
  4847. '*/select',
  4848. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  4849. $hoursRegex,
  4850. array('option' => array('value' => '')),
  4851. '/option',
  4852. '*/select',
  4853. ':',
  4854. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  4855. $minutesRegex,
  4856. array('option' => array('value' => '')),
  4857. '/option',
  4858. '*/select',
  4859. ' ',
  4860. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  4861. $meridianRegex,
  4862. array('option' => array('value' => '')),
  4863. '/option',
  4864. '*/select'
  4865. );
  4866. $this->assertTags($result, $expected);
  4867. }
  4868. /**
  4869. * When changing the date format, the label should always focus the first select box when
  4870. * clicked.
  4871. *
  4872. * @return void
  4873. */
  4874. public function testDateTimeLabelIdMatchesFirstInput() {
  4875. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4876. $this->assertContains('label for="ModelDateMonth"', $result);
  4877. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4878. $this->assertContains('label for="ModelDateDay"', $result);
  4879. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4880. $this->assertContains('label for="ModelDateYear"', $result);
  4881. }
  4882. /**
  4883. * testMonth method
  4884. *
  4885. * @return void
  4886. */
  4887. public function testMonth() {
  4888. $result = $this->Form->month('Model.field');
  4889. $expected = array(
  4890. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4891. array('option' => array('value' => '')),
  4892. '/option',
  4893. array('option' => array('value' => '01')),
  4894. date('F', strtotime('2008-01-01 00:00:00')),
  4895. '/option',
  4896. array('option' => array('value' => '02')),
  4897. date('F', strtotime('2008-02-01 00:00:00')),
  4898. '/option',
  4899. '*/select',
  4900. );
  4901. $this->assertTags($result, $expected);
  4902. $result = $this->Form->month('Model.field', array('empty' => true));
  4903. $expected = array(
  4904. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4905. array('option' => array('value' => '')),
  4906. '/option',
  4907. array('option' => array('value' => '01')),
  4908. date('F', strtotime('2008-01-01 00:00:00')),
  4909. '/option',
  4910. array('option' => array('value' => '02')),
  4911. date('F', strtotime('2008-02-01 00:00:00')),
  4912. '/option',
  4913. '*/select',
  4914. );
  4915. $this->assertTags($result, $expected);
  4916. $result = $this->Form->month('Model.field', array('monthNames' => false));
  4917. $expected = array(
  4918. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4919. array('option' => array('value' => '')),
  4920. '/option',
  4921. array('option' => array('value' => '01')),
  4922. '01',
  4923. '/option',
  4924. array('option' => array('value' => '02')),
  4925. '02',
  4926. '/option',
  4927. '*/select',
  4928. );
  4929. $this->assertTags($result, $expected);
  4930. $monthNames = array(
  4931. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4932. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  4933. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  4934. $expected = array(
  4935. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4936. array('option' => array('value' => '')),
  4937. '/option',
  4938. array('option' => array('value' => '01')),
  4939. 'Jan',
  4940. '/option',
  4941. array('option' => array('value' => '02')),
  4942. 'Feb',
  4943. '/option',
  4944. '*/select',
  4945. );
  4946. $this->assertTags($result, $expected);
  4947. }
  4948. /**
  4949. * testDay method
  4950. *
  4951. * @return void
  4952. */
  4953. public function testDay() {
  4954. extract($this->dateRegex);
  4955. $result = $this->Form->day('Model.field', array('value' => false));
  4956. $expected = array(
  4957. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4958. array('option' => array('value' => '')),
  4959. '/option',
  4960. array('option' => array('value' => '01')),
  4961. '1',
  4962. '/option',
  4963. array('option' => array('value' => '02')),
  4964. '2',
  4965. '/option',
  4966. $daysRegex,
  4967. '/select',
  4968. );
  4969. $this->assertTags($result, $expected);
  4970. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4971. $result = $this->Form->day('Model.field');
  4972. $expected = array(
  4973. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4974. array('option' => array('value' => '')),
  4975. '/option',
  4976. array('option' => array('value' => '01')),
  4977. '1',
  4978. '/option',
  4979. array('option' => array('value' => '02')),
  4980. '2',
  4981. '/option',
  4982. $daysRegex,
  4983. array('option' => array('value' => '10', 'selected' => 'selected')),
  4984. '10',
  4985. '/option',
  4986. $daysRegex,
  4987. '/select',
  4988. );
  4989. $this->assertTags($result, $expected);
  4990. $this->Form->request->data['Model']['field'] = '';
  4991. $result = $this->Form->day('Model.field', array('value' => '10'));
  4992. $expected = array(
  4993. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4994. array('option' => array('value' => '')),
  4995. '/option',
  4996. array('option' => array('value' => '01')),
  4997. '1',
  4998. '/option',
  4999. array('option' => array('value' => '02')),
  5000. '2',
  5001. '/option',
  5002. $daysRegex,
  5003. array('option' => array('value' => '10', 'selected' => 'selected')),
  5004. '10',
  5005. '/option',
  5006. $daysRegex,
  5007. '/select',
  5008. );
  5009. $this->assertTags($result, $expected);
  5010. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5011. $result = $this->Form->day('Model.field', array('value' => true));
  5012. $expected = array(
  5013. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5014. array('option' => array('value' => '')),
  5015. '/option',
  5016. array('option' => array('value' => '01')),
  5017. '1',
  5018. '/option',
  5019. array('option' => array('value' => '02')),
  5020. '2',
  5021. '/option',
  5022. $daysRegex,
  5023. array('option' => array('value' => '10', 'selected' => 'selected')),
  5024. '10',
  5025. '/option',
  5026. $daysRegex,
  5027. '/select',
  5028. );
  5029. $this->assertTags($result, $expected);
  5030. }
  5031. /**
  5032. * testMinute method
  5033. *
  5034. * @return void
  5035. */
  5036. public function testMinute() {
  5037. extract($this->dateRegex);
  5038. $result = $this->Form->minute('Model.field');
  5039. $expected = array(
  5040. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5041. array('option' => array('value' => '')),
  5042. '/option',
  5043. array('option' => array('value' => '00')),
  5044. '00',
  5045. '/option',
  5046. array('option' => array('value' => '01')),
  5047. '01',
  5048. '/option',
  5049. array('option' => array('value' => '02')),
  5050. '02',
  5051. '/option',
  5052. $minutesRegex,
  5053. '/select',
  5054. );
  5055. $this->assertTags($result, $expected);
  5056. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5057. $result = $this->Form->minute('Model.field');
  5058. $expected = array(
  5059. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5060. array('option' => array('value' => '')),
  5061. '/option',
  5062. array('option' => array('value' => '00')),
  5063. '00',
  5064. '/option',
  5065. array('option' => array('value' => '01')),
  5066. '01',
  5067. '/option',
  5068. array('option' => array('value' => '02')),
  5069. '02',
  5070. '/option',
  5071. $minutesRegex,
  5072. array('option' => array('value' => '12', 'selected' => 'selected')),
  5073. '12',
  5074. '/option',
  5075. $minutesRegex,
  5076. '/select',
  5077. );
  5078. $this->assertTags($result, $expected);
  5079. $this->Form->request->data['Model']['field'] = '';
  5080. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5081. $expected = array(
  5082. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5083. array('option' => array('value' => '')),
  5084. '/option',
  5085. array('option' => array('value' => '00')),
  5086. '00',
  5087. '/option',
  5088. array('option' => array('value' => '05')),
  5089. '05',
  5090. '/option',
  5091. array('option' => array('value' => '10')),
  5092. '10',
  5093. '/option',
  5094. $minutesRegex,
  5095. '/select',
  5096. );
  5097. $this->assertTags($result, $expected);
  5098. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5099. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5100. $expected = array(
  5101. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5102. array('option' => array('value' => '')),
  5103. '/option',
  5104. array('option' => array('value' => '00')),
  5105. '00',
  5106. '/option',
  5107. array('option' => array('value' => '05')),
  5108. '05',
  5109. '/option',
  5110. array('option' => array('value' => '10', 'selected' => 'selected')),
  5111. '10',
  5112. '/option',
  5113. $minutesRegex,
  5114. '/select',
  5115. );
  5116. $this->assertTags($result, $expected);
  5117. }
  5118. /**
  5119. * testHour method
  5120. *
  5121. * @return void
  5122. */
  5123. public function testHour() {
  5124. extract($this->dateRegex);
  5125. $result = $this->Form->hour('Model.field', false);
  5126. $expected = array(
  5127. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5128. array('option' => array('value' => '')),
  5129. '/option',
  5130. array('option' => array('value' => '01')),
  5131. '1',
  5132. '/option',
  5133. array('option' => array('value' => '02')),
  5134. '2',
  5135. '/option',
  5136. $hoursRegex,
  5137. '/select',
  5138. );
  5139. $this->assertTags($result, $expected);
  5140. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5141. $result = $this->Form->hour('Model.field', false);
  5142. $expected = array(
  5143. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5144. array('option' => array('value' => '')),
  5145. '/option',
  5146. array('option' => array('value' => '01')),
  5147. '1',
  5148. '/option',
  5149. array('option' => array('value' => '02')),
  5150. '2',
  5151. '/option',
  5152. $hoursRegex,
  5153. array('option' => array('value' => '12', 'selected' => 'selected')),
  5154. '12',
  5155. '/option',
  5156. '/select',
  5157. );
  5158. $this->assertTags($result, $expected);
  5159. $this->Form->request->data['Model']['field'] = '';
  5160. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5161. $expected = array(
  5162. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5163. array('option' => array('value' => '')),
  5164. '/option',
  5165. array('option' => array('value' => '00')),
  5166. '0',
  5167. '/option',
  5168. array('option' => array('value' => '01')),
  5169. '1',
  5170. '/option',
  5171. array('option' => array('value' => '02')),
  5172. '2',
  5173. '/option',
  5174. $hoursRegex,
  5175. array('option' => array('value' => '23', 'selected' => 'selected')),
  5176. '23',
  5177. '/option',
  5178. '/select',
  5179. );
  5180. $this->assertTags($result, $expected);
  5181. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5182. $result = $this->Form->hour('Model.field', true);
  5183. $expected = array(
  5184. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5185. array('option' => array('value' => '')),
  5186. '/option',
  5187. array('option' => array('value' => '00', 'selected' => 'selected')),
  5188. '0',
  5189. '/option',
  5190. array('option' => array('value' => '01')),
  5191. '1',
  5192. '/option',
  5193. array('option' => array('value' => '02')),
  5194. '2',
  5195. '/option',
  5196. $hoursRegex,
  5197. '/select',
  5198. );
  5199. $this->assertTags($result, $expected);
  5200. unset($this->Form->request->data['Model']['field']);
  5201. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5202. $thisHour = date('H');
  5203. $optValue = date('G');
  5204. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5205. }
  5206. /**
  5207. * testYear method
  5208. *
  5209. * @return void
  5210. */
  5211. public function testYear() {
  5212. $result = $this->Form->year('Model.field', 2006, 2007);
  5213. $expected = array(
  5214. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5215. array('option' => array('value' => '')),
  5216. '/option',
  5217. array('option' => array('value' => '2007')),
  5218. '2007',
  5219. '/option',
  5220. array('option' => array('value' => '2006')),
  5221. '2006',
  5222. '/option',
  5223. '/select',
  5224. );
  5225. $this->assertTags($result, $expected);
  5226. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5227. $expected = array(
  5228. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5229. array('option' => array('value' => '')),
  5230. '/option',
  5231. array('option' => array('value' => '2006')),
  5232. '2006',
  5233. '/option',
  5234. array('option' => array('value' => '2007')),
  5235. '2007',
  5236. '/option',
  5237. '/select',
  5238. );
  5239. $this->assertTags($result, $expected);
  5240. $this->request->data['Contact']['published'] = '';
  5241. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5242. $expected = array(
  5243. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5244. array('option' => array('value' => '')),
  5245. '/option',
  5246. array('option' => array('value' => '2007')),
  5247. '2007',
  5248. '/option',
  5249. array('option' => array('value' => '2006')),
  5250. '2006',
  5251. '/option',
  5252. '/select',
  5253. );
  5254. $this->assertTags($result, $expected);
  5255. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5256. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5257. $expected = array(
  5258. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5259. array('option' => array('value' => '2007')),
  5260. '2007',
  5261. '/option',
  5262. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5263. '2006',
  5264. '/option',
  5265. '/select',
  5266. );
  5267. $this->assertTags($result, $expected);
  5268. $this->Form->request->data['Contact']['published'] = '';
  5269. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5270. $expected = array(
  5271. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5272. array('option' => array('value' => '')),
  5273. '/option',
  5274. array('option' => array('value' => '2007')),
  5275. '2007',
  5276. '/option',
  5277. array('option' => array('value' => '2006')),
  5278. '2006',
  5279. '/option',
  5280. '/select',
  5281. );
  5282. $this->assertTags($result, $expected);
  5283. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5284. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5285. $expected = array(
  5286. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5287. array('option' => array('value' => '2007')),
  5288. '2007',
  5289. '/option',
  5290. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5291. '2006',
  5292. '/option',
  5293. '/select',
  5294. );
  5295. $this->assertTags($result, $expected);
  5296. $this->Form->request->data['Contact']['published'] = '';
  5297. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5298. $expected = array(
  5299. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5300. array('option' => array('value' => '')),
  5301. '/option',
  5302. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5303. '2007',
  5304. '/option',
  5305. array('option' => array('value' => '2006')),
  5306. '2006',
  5307. '/option',
  5308. '/select',
  5309. );
  5310. $this->assertTags($result, $expected);
  5311. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5312. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5313. $expected = array(
  5314. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5315. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5316. '2007',
  5317. '/option',
  5318. array('option' => array('value' => '2006')),
  5319. '2006',
  5320. '/option',
  5321. '/select',
  5322. );
  5323. $this->assertTags($result, $expected);
  5324. $this->Form->request->data['Contact']['published'] = '';
  5325. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5326. $expected = array(
  5327. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5328. array('option' => array('value' => '2008')),
  5329. '2008',
  5330. '/option',
  5331. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5332. '2007',
  5333. '/option',
  5334. array('option' => array('value' => '2006')),
  5335. '2006',
  5336. '/option',
  5337. '/select',
  5338. );
  5339. $this->assertTags($result, $expected);
  5340. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5341. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5342. $expected = array(
  5343. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5344. array('option' => array('value' => '2008')),
  5345. '2008',
  5346. '/option',
  5347. array('option' => array('value' => '2007')),
  5348. '2007',
  5349. '/option',
  5350. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5351. '2006',
  5352. '/option',
  5353. '/select',
  5354. );
  5355. $this->assertTags($result, $expected);
  5356. $this->Form->request->data = array();
  5357. $this->Form->create('Contact');
  5358. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5359. $expected = array(
  5360. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5361. array('option' => array('value' => '2008')),
  5362. '2008',
  5363. '/option',
  5364. array('option' => array('value' => '2007')),
  5365. '2007',
  5366. '/option',
  5367. array('option' => array('value' => '2006')),
  5368. '2006',
  5369. '/option',
  5370. '/select',
  5371. );
  5372. $this->assertTags($result, $expected);
  5373. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5374. $this->assertContains('data[Contact][published][year]', $result);
  5375. }
  5376. /**
  5377. * testTextArea method
  5378. *
  5379. * @return void
  5380. */
  5381. public function testTextArea() {
  5382. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5383. $result = $this->Form->textarea('Model.field');
  5384. $expected = array(
  5385. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5386. 'some test data',
  5387. '/textarea',
  5388. );
  5389. $this->assertTags($result, $expected);
  5390. $result = $this->Form->textarea('Model.tmp');
  5391. $expected = array(
  5392. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5393. '/textarea',
  5394. );
  5395. $this->assertTags($result, $expected);
  5396. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5397. $result = $this->Form->textarea('Model.field');
  5398. $expected = array(
  5399. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5400. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5401. '/textarea',
  5402. );
  5403. $this->assertTags($result, $expected);
  5404. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5405. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5406. $expected = array(
  5407. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5408. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5409. '/textarea',
  5410. );
  5411. $this->assertTags($result, $expected);
  5412. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5413. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5414. $expected = array(
  5415. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5416. '/textarea'
  5417. );
  5418. $this->assertTags($result, $expected);
  5419. }
  5420. /**
  5421. * testTextAreaWithStupidCharacters method
  5422. *
  5423. * test text area with non-ascii characters
  5424. *
  5425. * @return void
  5426. */
  5427. public function testTextAreaWithStupidCharacters() {
  5428. $this->loadFixtures('Post');
  5429. $result = $this->Form->input('Post.content', array(
  5430. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5431. ));
  5432. $expected = array(
  5433. 'div' => array('class' => 'input text'),
  5434. 'label' => array('for' => 'PostContent'),
  5435. 'Current Text',
  5436. '/label',
  5437. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5438. 'GREAT®',
  5439. '/textarea',
  5440. '/div'
  5441. );
  5442. $this->assertTags($result, $expected);
  5443. }
  5444. /**
  5445. * testHiddenField method
  5446. *
  5447. * @return void
  5448. */
  5449. public function testHiddenField() {
  5450. $Contact = ClassRegistry::getObject('Contact');
  5451. $Contact->validationErrors['field'] = 1;
  5452. $this->Form->request->data['Contact']['field'] = 'test';
  5453. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5454. $this->assertTags($result, array(
  5455. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5456. );
  5457. }
  5458. /**
  5459. * testFileUploadField method
  5460. *
  5461. * @return void
  5462. */
  5463. public function testFileUploadField() {
  5464. $result = $this->Form->file('Model.upload');
  5465. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5466. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5467. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5468. $expected = array(
  5469. 'div' => array('class' => 'input file'),
  5470. 'label' => array('for' => 'ModelUpload'),
  5471. 'Upload',
  5472. '/label',
  5473. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5474. '/div'
  5475. );
  5476. $this->assertTags($result, $expected);
  5477. }
  5478. /**
  5479. * test File upload input on a model not used in create();
  5480. *
  5481. * @return void
  5482. */
  5483. public function testFileUploadOnOtherModel() {
  5484. $this->Form->create('ValidateUser', array('type' => 'file'));
  5485. $result = $this->Form->file('ValidateProfile.city');
  5486. $expected = array(
  5487. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5488. );
  5489. $this->assertTags($result, $expected);
  5490. }
  5491. /**
  5492. * testButton method
  5493. *
  5494. * @return void
  5495. */
  5496. public function testButton() {
  5497. $result = $this->Form->button('Hi');
  5498. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5499. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5500. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5501. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5502. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5503. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5504. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5505. $result = $this->Form->button('No type', array('type' => false));
  5506. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5507. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5508. $this->assertNotRegExp('/\&039/', $result);
  5509. }
  5510. /**
  5511. * Test that button() makes unlocked fields by default.
  5512. *
  5513. * @return void
  5514. */
  5515. public function testButtonUnlockedByDefault() {
  5516. $this->Form->request->params['_Token']['key'] = 'secured';
  5517. $this->Form->button('Save', array('name' => 'save'));
  5518. $this->Form->button('Clear');
  5519. $result = $this->Form->unlockField();
  5520. $this->assertEquals(array('save'), $result);
  5521. }
  5522. /**
  5523. * testPostButton method
  5524. *
  5525. * @return void
  5526. */
  5527. public function testPostButton() {
  5528. $result = $this->Form->postButton('Hi', '/controller/action');
  5529. $this->assertTags($result, array(
  5530. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5531. 'div' => array('style' => 'display:none;'),
  5532. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5533. '/div',
  5534. 'button' => array('type' => 'submit'),
  5535. 'Hi',
  5536. '/button',
  5537. '/form'
  5538. ));
  5539. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5540. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5541. }
  5542. /**
  5543. * Test that postButton adds _Token fields.
  5544. *
  5545. * @return void
  5546. */
  5547. public function testSecurePostButton() {
  5548. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5549. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5550. $expected = array(
  5551. 'form' => array(
  5552. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5553. ),
  5554. array('div' => array('style' => 'display:none;')),
  5555. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5556. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5557. '/div',
  5558. 'button' => array('type' => 'submit'),
  5559. 'Delete',
  5560. '/button',
  5561. array('div' => array('style' => 'display:none;')),
  5562. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5563. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5564. '/div',
  5565. '/form',
  5566. );
  5567. $this->assertTags($result, $expected);
  5568. }
  5569. /**
  5570. * testPostLink method
  5571. *
  5572. * @return void
  5573. */
  5574. public function testPostLink() {
  5575. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5576. $this->assertTags($result, array(
  5577. 'form' => array(
  5578. 'method' => 'post', 'action' => '/posts/delete/1',
  5579. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5580. ),
  5581. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5582. '/form',
  5583. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5584. 'Delete',
  5585. '/a'
  5586. ));
  5587. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5588. $this->assertTags($result, array(
  5589. 'form' => array(
  5590. 'method' => 'post', 'action' => '/posts/delete/1',
  5591. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5592. ),
  5593. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5594. '/form',
  5595. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5596. 'Delete',
  5597. '/a'
  5598. ));
  5599. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5600. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5601. }
  5602. /**
  5603. * Test that postLink adds _Token fields.
  5604. *
  5605. * @return void
  5606. */
  5607. public function testSecurePostLink() {
  5608. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5609. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5610. $expected = array(
  5611. 'form' => array(
  5612. 'method' => 'post', 'action' => '/posts/delete/1',
  5613. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5614. ),
  5615. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5616. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5617. 'div' => array('style' => 'display:none;'),
  5618. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5619. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5620. '/div',
  5621. '/form',
  5622. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5623. 'Delete',
  5624. '/a'
  5625. );
  5626. $this->assertTags($result, $expected);
  5627. }
  5628. /**
  5629. * testSubmitButton method
  5630. *
  5631. * @return void
  5632. */
  5633. public function testSubmitButton() {
  5634. $result = $this->Form->submit('');
  5635. $expected = array(
  5636. 'div' => array('class' => 'submit'),
  5637. 'input' => array('type' => 'submit', 'value' => ''),
  5638. '/div'
  5639. );
  5640. $this->assertTags($result, $expected);
  5641. $result = $this->Form->submit('Test Submit');
  5642. $expected = array(
  5643. 'div' => array('class' => 'submit'),
  5644. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5645. '/div'
  5646. );
  5647. $this->assertTags($result, $expected);
  5648. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5649. $expected = array(
  5650. 'span' => array('class' => 'submit'),
  5651. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5652. '/span'
  5653. );
  5654. $this->assertTags($result, $expected);
  5655. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5656. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5657. $this->assertTags($result, $expected);
  5658. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5659. $expected = array(
  5660. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5661. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5662. '/div'
  5663. );
  5664. $this->assertTags($result, $expected);
  5665. $result = $this->Form->submit('Next >');
  5666. $expected = array(
  5667. 'div' => array('class' => 'submit'),
  5668. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5669. '/div'
  5670. );
  5671. $this->assertTags($result, $expected);
  5672. $result = $this->Form->submit('Next >', array('escape' => false));
  5673. $expected = array(
  5674. 'div' => array('class' => 'submit'),
  5675. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5676. '/div'
  5677. );
  5678. $this->assertTags($result, $expected);
  5679. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5680. $expected = array(
  5681. 'div' => array('class' => 'submit'),
  5682. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5683. '/div'
  5684. );
  5685. $this->assertTags($result, $expected);
  5686. $before = '--before--';
  5687. $after = '--after--';
  5688. $result = $this->Form->submit('Test', array('before' => $before));
  5689. $expected = array(
  5690. 'div' => array('class' => 'submit'),
  5691. '--before--',
  5692. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5693. '/div'
  5694. );
  5695. $this->assertTags($result, $expected);
  5696. $result = $this->Form->submit('Test', array('after' => $after));
  5697. $expected = array(
  5698. 'div' => array('class' => 'submit'),
  5699. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5700. '--after--',
  5701. '/div'
  5702. );
  5703. $this->assertTags($result, $expected);
  5704. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5705. $expected = array(
  5706. 'div' => array('class' => 'submit'),
  5707. '--before--',
  5708. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5709. '--after--',
  5710. '/div'
  5711. );
  5712. $this->assertTags($result, $expected);
  5713. }
  5714. /**
  5715. * test image submit types.
  5716. *
  5717. * @return void
  5718. */
  5719. public function testSubmitImage() {
  5720. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5721. $expected = array(
  5722. 'div' => array('class' => 'submit'),
  5723. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5724. '/div'
  5725. );
  5726. $this->assertTags($result, $expected);
  5727. $result = $this->Form->submit('/relative/cake.power.gif');
  5728. $expected = array(
  5729. 'div' => array('class' => 'submit'),
  5730. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5731. '/div'
  5732. );
  5733. $this->assertTags($result, $expected);
  5734. $result = $this->Form->submit('cake.power.gif');
  5735. $expected = array(
  5736. 'div' => array('class' => 'submit'),
  5737. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5738. '/div'
  5739. );
  5740. $this->assertTags($result, $expected);
  5741. $result = $this->Form->submit('Not.an.image');
  5742. $expected = array(
  5743. 'div' => array('class' => 'submit'),
  5744. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5745. '/div'
  5746. );
  5747. $this->assertTags($result, $expected);
  5748. $after = '--after--';
  5749. $before = '--before--';
  5750. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5751. $expected = array(
  5752. 'div' => array('class' => 'submit'),
  5753. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5754. '--after--',
  5755. '/div'
  5756. );
  5757. $this->assertTags($result, $expected);
  5758. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5759. $expected = array(
  5760. 'div' => array('class' => 'submit'),
  5761. '--before--',
  5762. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5763. '/div'
  5764. );
  5765. $this->assertTags($result, $expected);
  5766. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5767. $expected = array(
  5768. 'div' => array('class' => 'submit'),
  5769. '--before--',
  5770. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5771. '--after--',
  5772. '/div'
  5773. );
  5774. $this->assertTags($result, $expected);
  5775. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5776. $expected = array(
  5777. 'div' => array('class' => 'submit'),
  5778. '--before--',
  5779. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5780. '--after--',
  5781. '/div'
  5782. );
  5783. $this->assertTags($result, $expected);
  5784. }
  5785. /**
  5786. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5787. * be submitted at a time.
  5788. *
  5789. * @return void
  5790. */
  5791. public function testSubmitUnlockedByDefault() {
  5792. $this->Form->request->params['_Token']['key'] = 'secured';
  5793. $this->Form->submit('Go go');
  5794. $this->Form->submit('Save', array('name' => 'save'));
  5795. $result = $this->Form->unlockField();
  5796. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5797. }
  5798. /**
  5799. * Test submit image with timestamps.
  5800. *
  5801. * @return void
  5802. */
  5803. public function testSubmitImageTimestamp() {
  5804. Configure::write('Asset.timestamp', 'force');
  5805. $result = $this->Form->submit('cake.power.gif');
  5806. $expected = array(
  5807. 'div' => array('class' => 'submit'),
  5808. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5809. '/div'
  5810. );
  5811. $this->assertTags($result, $expected);
  5812. }
  5813. /**
  5814. * test the create() method
  5815. *
  5816. * @return void
  5817. */
  5818. public function testCreate() {
  5819. $result = $this->Form->create('Contact');
  5820. $encoding = strtolower(Configure::read('App.encoding'));
  5821. $expected = array(
  5822. 'form' => array(
  5823. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5824. 'accept-charset' => $encoding
  5825. ),
  5826. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5827. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5828. '/div'
  5829. );
  5830. $this->assertTags($result, $expected);
  5831. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5832. $expected = array('form' => array(
  5833. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5834. 'accept-charset' => $encoding
  5835. ));
  5836. $this->assertTags($result, $expected);
  5837. $result = $this->Form->create('Contact', array('type' => 'get'));
  5838. $expected = array('form' => array(
  5839. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5840. 'accept-charset' => $encoding
  5841. ));
  5842. $this->assertTags($result, $expected);
  5843. $result = $this->Form->create('Contact', array('type' => 'put'));
  5844. $expected = array(
  5845. 'form' => array(
  5846. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5847. 'accept-charset' => $encoding
  5848. ),
  5849. 'div' => array('style' => 'display:none;'),
  5850. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5851. '/div'
  5852. );
  5853. $this->assertTags($result, $expected);
  5854. $result = $this->Form->create('Contact', array('type' => 'file'));
  5855. $expected = array(
  5856. 'form' => array(
  5857. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5858. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5859. ),
  5860. 'div' => array('style' => 'display:none;'),
  5861. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5862. '/div'
  5863. );
  5864. $this->assertTags($result, $expected);
  5865. $this->Form->request->data['Contact']['id'] = 1;
  5866. $this->Form->request->here = '/contacts/edit/1';
  5867. $this->Form->request['action'] = 'edit';
  5868. $result = $this->Form->create('Contact');
  5869. $expected = array(
  5870. 'form' => array(
  5871. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5872. 'accept-charset' => $encoding
  5873. ),
  5874. 'div' => array('style' => 'display:none;'),
  5875. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5876. '/div'
  5877. );
  5878. $this->assertTags($result, $expected);
  5879. $this->Form->request->data['Contact']['id'] = 1;
  5880. $this->Form->request->here = '/contacts/edit/1';
  5881. $this->Form->request['action'] = 'edit';
  5882. $result = $this->Form->create('Contact', array('type' => 'file'));
  5883. $expected = array(
  5884. 'form' => array(
  5885. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5886. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5887. ),
  5888. 'div' => array('style' => 'display:none;'),
  5889. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5890. '/div'
  5891. );
  5892. $this->assertTags($result, $expected);
  5893. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  5894. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  5895. $expected = array(
  5896. 'form' => array(
  5897. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  5898. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  5899. ),
  5900. 'div' => array('style' => 'display:none;'),
  5901. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5902. '/div'
  5903. );
  5904. $this->assertTags($result, $expected);
  5905. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  5906. $expected = array(
  5907. 'form' => array(
  5908. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  5909. 'accept-charset' => $encoding
  5910. ),
  5911. 'div' => array('style' => 'display:none;'),
  5912. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5913. '/div'
  5914. );
  5915. $this->assertTags($result, $expected);
  5916. $this->Form->request['action'] = 'add';
  5917. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  5918. $expected = array(
  5919. 'form' => array(
  5920. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  5921. 'accept-charset' => $encoding
  5922. ),
  5923. 'div' => array('style' => 'display:none;'),
  5924. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5925. '/div'
  5926. );
  5927. $this->assertTags($result, $expected);
  5928. $result = $this->Form->create('User', array('action' => 'login'));
  5929. $expected = array(
  5930. 'form' => array(
  5931. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  5932. 'accept-charset' => $encoding
  5933. ),
  5934. 'div' => array('style' => 'display:none;'),
  5935. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5936. '/div'
  5937. );
  5938. $this->assertTags($result, $expected);
  5939. $result = $this->Form->create('User', array('url' => '/users/login'));
  5940. $expected = array(
  5941. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  5942. 'div' => array('style' => 'display:none;'),
  5943. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5944. '/div'
  5945. );
  5946. $this->assertTags($result, $expected);
  5947. $this->Form->request['controller'] = 'pages';
  5948. $result = $this->Form->create('User', array('action' => 'signup'));
  5949. $expected = array(
  5950. 'form' => array(
  5951. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  5952. 'accept-charset' => $encoding
  5953. ),
  5954. 'div' => array('style' => 'display:none;'),
  5955. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5956. '/div'
  5957. );
  5958. $this->assertTags($result, $expected);
  5959. $this->Form->request->data = array();
  5960. $this->Form->request['controller'] = 'contacts';
  5961. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5962. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  5963. $expected = array(
  5964. 'form' => array(
  5965. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  5966. 'accept-charset' => 'utf-8'
  5967. ),
  5968. 'div' => array('style' => 'display:none;'),
  5969. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5970. '/div'
  5971. );
  5972. $this->assertTags($result, $expected);
  5973. }
  5974. /**
  5975. * Test the onsubmit option for create()
  5976. *
  5977. * @return void
  5978. */
  5979. public function testCreateOnSubmit() {
  5980. $this->Form->request->data = array();
  5981. $this->Form->request['controller'] = 'contacts';
  5982. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5983. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  5984. $expected = array(
  5985. 'form' => array(
  5986. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  5987. 'accept-charset' => 'utf-8'
  5988. ),
  5989. 'div' => array('style' => 'display:none;'),
  5990. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5991. '/div'
  5992. );
  5993. $this->assertTags($result, $expected);
  5994. $this->Form->request->data = array();
  5995. $this->Form->request['controller'] = 'contacts';
  5996. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5997. $result = $this->Form->create(array(
  5998. 'url' => array('action' => 'index', 'param'),
  5999. 'default' => false,
  6000. 'onsubmit' => 'someFunction();'
  6001. ));
  6002. $expected = array(
  6003. 'form' => array(
  6004. 'id' => 'ContactAddForm', 'method' => 'post',
  6005. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6006. 'action' => '/contacts/index/param',
  6007. 'accept-charset' => 'utf-8'
  6008. ),
  6009. 'div' => array('style' => 'display:none;'),
  6010. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6011. '/div'
  6012. );
  6013. $this->assertTags($result, $expected);
  6014. }
  6015. /**
  6016. * test create() with automatic url generation
  6017. *
  6018. * @return void
  6019. */
  6020. public function testCreateAutoUrl() {
  6021. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6022. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6023. $this->Form->request->base = '/base_url';
  6024. $result = $this->Form->create('Contact');
  6025. $expected = array(
  6026. 'form' => array(
  6027. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6028. 'accept-charset' => 'utf-8'
  6029. ),
  6030. 'div' => array('style' => 'display:none;'),
  6031. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6032. '/div'
  6033. );
  6034. $this->assertTags($result, $expected);
  6035. $this->Form->request['action'] = 'delete';
  6036. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6037. $this->Form->request->base = '/base_url';
  6038. $result = $this->Form->create('Contact');
  6039. $expected = array(
  6040. 'form' => array(
  6041. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6042. 'accept-charset' => 'utf-8'
  6043. ),
  6044. 'div' => array('style' => 'display:none;'),
  6045. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6046. '/div'
  6047. );
  6048. $this->assertTags($result, $expected);
  6049. }
  6050. /**
  6051. * test create() with a custom route
  6052. *
  6053. * @return void
  6054. */
  6055. public function testCreateCustomRoute() {
  6056. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6057. $encoding = strtolower(Configure::read('App.encoding'));
  6058. $result = $this->Form->create('User', array('action' => 'login'));
  6059. $expected = array(
  6060. 'form' => array(
  6061. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6062. 'accept-charset' => $encoding
  6063. ),
  6064. 'div' => array('style' => 'display:none;'),
  6065. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6066. '/div'
  6067. );
  6068. $this->assertTags($result, $expected);
  6069. }
  6070. /**
  6071. * test that inputDefaults are stored and used.
  6072. *
  6073. * @return void
  6074. */
  6075. public function testCreateWithInputDefaults() {
  6076. $this->Form->create('User', array(
  6077. 'inputDefaults' => array(
  6078. 'div' => false,
  6079. 'label' => false,
  6080. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6081. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6082. )
  6083. ));
  6084. $result = $this->Form->input('username');
  6085. $expected = array(
  6086. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6087. );
  6088. $this->assertTags($result, $expected);
  6089. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6090. $expected = array(
  6091. 'div' => array('class' => 'input text'),
  6092. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6093. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6094. '/div'
  6095. );
  6096. $this->assertTags($result, $expected);
  6097. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6098. $expected = array(
  6099. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6100. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6101. );
  6102. $this->assertTags($result, $expected);
  6103. $this->Form->create('User', array(
  6104. 'inputDefaults' => array(
  6105. 'div' => false,
  6106. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6107. )
  6108. ));
  6109. $result = $this->Form->input('username', array('div' => true));
  6110. $expected = array(
  6111. 'div' => array('class' => 'input text'),
  6112. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6113. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6114. '/div'
  6115. );
  6116. $this->assertTags($result, $expected);
  6117. }
  6118. /**
  6119. * test automatic accept-charset overriding
  6120. *
  6121. * @return void
  6122. */
  6123. public function testCreateWithAcceptCharset() {
  6124. $result = $this->Form->create('UserForm', array(
  6125. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6126. )
  6127. );
  6128. $expected = array(
  6129. 'form' => array(
  6130. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6131. 'accept-charset' => 'iso-8859-1'
  6132. ),
  6133. 'div' => array('style' => 'display:none;'),
  6134. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6135. '/div'
  6136. );
  6137. $this->assertTags($result, $expected);
  6138. }
  6139. /**
  6140. * Test base form url when url param is passed with multiple parameters (&)
  6141. *
  6142. */
  6143. public function testCreateQuerystringrequest() {
  6144. $encoding = strtolower(Configure::read('App.encoding'));
  6145. $result = $this->Form->create('Contact', array(
  6146. 'type' => 'post',
  6147. 'escape' => false,
  6148. 'url' => array(
  6149. 'controller' => 'controller',
  6150. 'action' => 'action',
  6151. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6152. )
  6153. ));
  6154. $expected = array(
  6155. 'form' => array(
  6156. 'id' => 'ContactAddForm',
  6157. 'method' => 'post',
  6158. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6159. 'accept-charset' => $encoding
  6160. ),
  6161. 'div' => array('style' => 'display:none;'),
  6162. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6163. '/div'
  6164. );
  6165. $this->assertTags($result, $expected);
  6166. $result = $this->Form->create('Contact', array(
  6167. 'type' => 'post',
  6168. 'url' => array(
  6169. 'controller' => 'controller',
  6170. 'action' => 'action',
  6171. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6172. )
  6173. ));
  6174. $expected = array(
  6175. 'form' => array(
  6176. 'id' => 'ContactAddForm',
  6177. 'method' => 'post',
  6178. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6179. 'accept-charset' => $encoding
  6180. ),
  6181. 'div' => array('style' => 'display:none;'),
  6182. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6183. '/div'
  6184. );
  6185. $this->assertTags($result, $expected);
  6186. }
  6187. /**
  6188. * test that create() doesn't cause errors by multiple id's being in the primary key
  6189. * as could happen with multiple select or checkboxes.
  6190. *
  6191. * @return void
  6192. */
  6193. public function testCreateWithMultipleIdInData() {
  6194. $encoding = strtolower(Configure::read('App.encoding'));
  6195. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6196. $result = $this->Form->create('Contact');
  6197. $expected = array(
  6198. 'form' => array(
  6199. 'id' => 'ContactAddForm',
  6200. 'method' => 'post',
  6201. 'action' => '/contacts/add',
  6202. 'accept-charset' => $encoding
  6203. ),
  6204. 'div' => array('style' => 'display:none;'),
  6205. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6206. '/div'
  6207. );
  6208. $this->assertTags($result, $expected);
  6209. }
  6210. /**
  6211. * test that create() doesn't add in extra passed params.
  6212. *
  6213. * @return void
  6214. */
  6215. public function testCreatePassedArgs() {
  6216. $encoding = strtolower(Configure::read('App.encoding'));
  6217. $this->Form->request->data['Contact']['id'] = 1;
  6218. $result = $this->Form->create('Contact', array(
  6219. 'type' => 'post',
  6220. 'escape' => false,
  6221. 'url' => array(
  6222. 'action' => 'edit',
  6223. 'myparam'
  6224. )
  6225. ));
  6226. $expected = array(
  6227. 'form' => array(
  6228. 'id' => 'ContactAddForm',
  6229. 'method' => 'post',
  6230. 'action' => '/contacts/edit/myparam',
  6231. 'accept-charset' => $encoding
  6232. ),
  6233. 'div' => array('style' => 'display:none;'),
  6234. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6235. '/div'
  6236. );
  6237. $this->assertTags($result, $expected);
  6238. }
  6239. /**
  6240. * test creating a get form, and get form inputs.
  6241. *
  6242. * @return void
  6243. */
  6244. public function testGetFormCreate() {
  6245. $encoding = strtolower(Configure::read('App.encoding'));
  6246. $result = $this->Form->create('Contact', array('type' => 'get'));
  6247. $this->assertTags($result, array('form' => array(
  6248. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6249. 'accept-charset' => $encoding
  6250. )));
  6251. $result = $this->Form->text('Contact.name');
  6252. $this->assertTags($result, array('input' => array(
  6253. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6254. )));
  6255. $result = $this->Form->password('password');
  6256. $this->assertTags($result, array('input' => array(
  6257. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6258. )));
  6259. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6260. $result = $this->Form->text('user_form');
  6261. $this->assertTags($result, array('input' => array(
  6262. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6263. )));
  6264. }
  6265. /**
  6266. * test get form, and inputs when the model param is false
  6267. *
  6268. * @return void
  6269. */
  6270. public function testGetFormWithFalseModel() {
  6271. $encoding = strtolower(Configure::read('App.encoding'));
  6272. $this->Form->request['controller'] = 'contact_test';
  6273. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6274. $expected = array('form' => array(
  6275. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6276. 'accept-charset' => $encoding
  6277. ));
  6278. $this->assertTags($result, $expected);
  6279. $result = $this->Form->text('reason');
  6280. $expected = array(
  6281. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6282. );
  6283. $this->assertTags($result, $expected);
  6284. }
  6285. /**
  6286. * test that datetime() works with GET style forms.
  6287. *
  6288. * @return void
  6289. */
  6290. public function testDateTimeWithGetForms() {
  6291. extract($this->dateRegex);
  6292. $this->Form->create('Contact', array('type' => 'get'));
  6293. $result = $this->Form->datetime('created');
  6294. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6295. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6296. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6297. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6298. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6299. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6300. }
  6301. /**
  6302. * testEditFormWithData method
  6303. *
  6304. * test auto populating form elements from submitted data.
  6305. *
  6306. * @return void
  6307. */
  6308. public function testEditFormWithData() {
  6309. $this->Form->request->data = array('Person' => array(
  6310. 'id' => 1,
  6311. 'first_name' => 'Nate',
  6312. 'last_name' => 'Abele',
  6313. 'email' => 'nate@example.com'
  6314. ));
  6315. $this->Form->request->addParams(array(
  6316. 'models' => array('Person'),
  6317. 'controller' => 'people',
  6318. 'action' => 'add'
  6319. ));
  6320. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6321. $this->Form->create();
  6322. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6323. $expected = array(
  6324. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6325. 'select' => array(
  6326. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6327. ),
  6328. array('option' => array('value' => 1)), 'Nate', '/option',
  6329. array('option' => array('value' => 2)), 'Garrett', '/option',
  6330. array('option' => array('value' => 3)), 'Larry', '/option',
  6331. '/select'
  6332. );
  6333. $this->assertTags($result, $expected);
  6334. }
  6335. /**
  6336. * Test that required fields are created for various types of validation.
  6337. *
  6338. * @return void
  6339. */
  6340. public function testFormInputRequiredDetection() {
  6341. $this->Form->create('Contact');
  6342. $result = $this->Form->input('Contact.non_existing');
  6343. $expected = array(
  6344. 'div' => array('class' => 'input text'),
  6345. 'label' => array('for' => 'ContactNonExisting'),
  6346. 'Non Existing',
  6347. '/label',
  6348. 'input' => array(
  6349. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6350. 'id' => 'ContactNonExisting'
  6351. ),
  6352. '/div'
  6353. );
  6354. $this->assertTags($result, $expected);
  6355. $result = $this->Form->input('Contact.imrequired');
  6356. $expected = array(
  6357. 'div' => array('class' => 'input text required'),
  6358. 'label' => array('for' => 'ContactImrequired'),
  6359. 'Imrequired',
  6360. '/label',
  6361. 'input' => array(
  6362. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6363. 'id' => 'ContactImrequired'
  6364. ),
  6365. '/div'
  6366. );
  6367. $this->assertTags($result, $expected);
  6368. $result = $this->Form->input('Contact.imalsorequired');
  6369. $expected = array(
  6370. 'div' => array('class' => 'input text required'),
  6371. 'label' => array('for' => 'ContactImalsorequired'),
  6372. 'Imalsorequired',
  6373. '/label',
  6374. 'input' => array(
  6375. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6376. 'id' => 'ContactImalsorequired'
  6377. ),
  6378. '/div'
  6379. );
  6380. $this->assertTags($result, $expected);
  6381. $result = $this->Form->input('Contact.imrequiredtoo');
  6382. $expected = array(
  6383. 'div' => array('class' => 'input text required'),
  6384. 'label' => array('for' => 'ContactImrequiredtoo'),
  6385. 'Imrequiredtoo',
  6386. '/label',
  6387. 'input' => array(
  6388. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6389. 'id' => 'ContactImrequiredtoo'
  6390. ),
  6391. '/div'
  6392. );
  6393. $this->assertTags($result, $expected);
  6394. $result = $this->Form->input('Contact.required_one');
  6395. $expected = array(
  6396. 'div' => array('class' => 'input text required'),
  6397. 'label' => array('for' => 'ContactRequiredOne'),
  6398. 'Required One',
  6399. '/label',
  6400. 'input' => array(
  6401. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6402. 'id' => 'ContactRequiredOne'
  6403. ),
  6404. '/div'
  6405. );
  6406. $this->assertTags($result, $expected);
  6407. $result = $this->Form->input('Contact.string_required');
  6408. $expected = array(
  6409. 'div' => array('class' => 'input text required'),
  6410. 'label' => array('for' => 'ContactStringRequired'),
  6411. 'String Required',
  6412. '/label',
  6413. 'input' => array(
  6414. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6415. 'id' => 'ContactStringRequired'
  6416. ),
  6417. '/div'
  6418. );
  6419. $this->assertTags($result, $expected);
  6420. $result = $this->Form->input('Contact.imnotrequired');
  6421. $expected = array(
  6422. 'div' => array('class' => 'input text'),
  6423. 'label' => array('for' => 'ContactImnotrequired'),
  6424. 'Imnotrequired',
  6425. '/label',
  6426. 'input' => array(
  6427. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6428. 'id' => 'ContactImnotrequired'
  6429. ),
  6430. '/div'
  6431. );
  6432. $this->assertTags($result, $expected);
  6433. $result = $this->Form->input('Contact.imalsonotrequired');
  6434. $expected = array(
  6435. 'div' => array('class' => 'input text'),
  6436. 'label' => array('for' => 'ContactImalsonotrequired'),
  6437. 'Imalsonotrequired',
  6438. '/label',
  6439. 'input' => array(
  6440. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6441. 'id' => 'ContactImalsonotrequired'
  6442. ),
  6443. '/div'
  6444. );
  6445. $this->assertTags($result, $expected);
  6446. $result = $this->Form->input('Contact.imnotrequiredeither');
  6447. $expected = array(
  6448. 'div' => array('class' => 'input text'),
  6449. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6450. 'Imnotrequiredeither',
  6451. '/label',
  6452. 'input' => array(
  6453. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6454. 'id' => 'ContactImnotrequiredeither'
  6455. ),
  6456. '/div'
  6457. );
  6458. $this->assertTags($result, $expected);
  6459. }
  6460. /**
  6461. * testFormMagicInput method
  6462. *
  6463. * @return void
  6464. */
  6465. public function testFormMagicInput() {
  6466. $encoding = strtolower(Configure::read('App.encoding'));
  6467. $result = $this->Form->create('Contact');
  6468. $expected = array(
  6469. 'form' => array(
  6470. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6471. 'accept-charset' => $encoding
  6472. ),
  6473. 'div' => array('style' => 'display:none;'),
  6474. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6475. '/div'
  6476. );
  6477. $this->assertTags($result, $expected);
  6478. $result = $this->Form->input('name');
  6479. $expected = array(
  6480. 'div' => array('class' => 'input text'),
  6481. 'label' => array('for' => 'ContactName'),
  6482. 'Name',
  6483. '/label',
  6484. 'input' => array(
  6485. 'type' => 'text', 'name' => 'data[Contact][name]',
  6486. 'id' => 'ContactName', 'maxlength' => '255'
  6487. ),
  6488. '/div'
  6489. );
  6490. $this->assertTags($result, $expected);
  6491. $result = $this->Form->input('non_existing_field_in_contact_model');
  6492. $expected = array(
  6493. 'div' => array('class' => 'input text'),
  6494. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6495. 'Non Existing Field In Contact Model',
  6496. '/label',
  6497. 'input' => array(
  6498. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6499. 'id' => 'ContactNonExistingFieldInContactModel'
  6500. ),
  6501. '/div'
  6502. );
  6503. $this->assertTags($result, $expected);
  6504. $result = $this->Form->input('Address.street');
  6505. $expected = array(
  6506. 'div' => array('class' => 'input text'),
  6507. 'label' => array('for' => 'AddressStreet'),
  6508. 'Street',
  6509. '/label',
  6510. 'input' => array(
  6511. 'type' => 'text', 'name' => 'data[Address][street]',
  6512. 'id' => 'AddressStreet'
  6513. ),
  6514. '/div'
  6515. );
  6516. $this->assertTags($result, $expected);
  6517. $result = $this->Form->input('Address.non_existing_field_in_model');
  6518. $expected = array(
  6519. 'div' => array('class' => 'input text'),
  6520. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6521. 'Non Existing Field In Model',
  6522. '/label',
  6523. 'input' => array(
  6524. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6525. 'id' => 'AddressNonExistingFieldInModel'
  6526. ),
  6527. '/div'
  6528. );
  6529. $this->assertTags($result, $expected);
  6530. $result = $this->Form->input('name', array('div' => false));
  6531. $expected = array(
  6532. 'label' => array('for' => 'ContactName'),
  6533. 'Name',
  6534. '/label',
  6535. 'input' => array(
  6536. 'type' => 'text', 'name' => 'data[Contact][name]',
  6537. 'id' => 'ContactName', 'maxlength' => '255'
  6538. )
  6539. );
  6540. $this->assertTags($result, $expected);
  6541. extract($this->dateRegex);
  6542. $now = strtotime('now');
  6543. $result = $this->Form->input('Contact.published', array('div' => false));
  6544. $expected = array(
  6545. 'label' => array('for' => 'ContactPublishedMonth'),
  6546. 'Published',
  6547. '/label',
  6548. array('select' => array(
  6549. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6550. )),
  6551. $monthsRegex,
  6552. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6553. date('F', $now),
  6554. '/option',
  6555. '*/select',
  6556. '-',
  6557. array('select' => array(
  6558. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6559. )),
  6560. $daysRegex,
  6561. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6562. date('j', $now),
  6563. '/option',
  6564. '*/select',
  6565. '-',
  6566. array('select' => array(
  6567. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6568. )),
  6569. $yearsRegex,
  6570. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6571. date('Y', $now),
  6572. '*/select'
  6573. );
  6574. $this->assertTags($result, $expected);
  6575. $result = $this->Form->input('Contact.updated', array('div' => false));
  6576. $expected = array(
  6577. 'label' => array('for' => 'ContactUpdatedMonth'),
  6578. 'Updated',
  6579. '/label',
  6580. array('select' => array(
  6581. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6582. )),
  6583. $monthsRegex,
  6584. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6585. date('F', $now),
  6586. '/option',
  6587. '*/select',
  6588. '-',
  6589. array('select' => array(
  6590. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6591. )),
  6592. $daysRegex,
  6593. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6594. date('j', $now),
  6595. '/option',
  6596. '*/select',
  6597. '-',
  6598. array('select' => array(
  6599. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6600. )),
  6601. $yearsRegex,
  6602. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6603. date('Y', $now),
  6604. '*/select'
  6605. );
  6606. $this->assertTags($result, $expected);
  6607. $result = $this->Form->input('UserForm.stuff');
  6608. $expected = array(
  6609. 'div' => array('class' => 'input text'),
  6610. 'label' => array('for' => 'UserFormStuff'),
  6611. 'Stuff',
  6612. '/label',
  6613. 'input' => array(
  6614. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6615. 'id' => 'UserFormStuff', 'maxlength' => 10
  6616. ),
  6617. '/div'
  6618. );
  6619. $this->assertTags($result, $expected);
  6620. }
  6621. /**
  6622. * testForMagicInputNonExistingNorValidated method
  6623. *
  6624. * @return void
  6625. */
  6626. public function testForMagicInputNonExistingNorValidated() {
  6627. $encoding = strtolower(Configure::read('App.encoding'));
  6628. $result = $this->Form->create('Contact');
  6629. $expected = array(
  6630. 'form' => array(
  6631. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6632. 'accept-charset' => $encoding
  6633. ),
  6634. 'div' => array('style' => 'display:none;'),
  6635. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6636. '/div'
  6637. );
  6638. $this->assertTags($result, $expected);
  6639. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6640. $expected = array(
  6641. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6642. 'Non Existing Nor Validated',
  6643. '/label',
  6644. 'input' => array(
  6645. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6646. 'id' => 'ContactNonExistingNorValidated'
  6647. )
  6648. );
  6649. $this->assertTags($result, $expected);
  6650. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6651. 'div' => false, 'value' => 'my value'
  6652. ));
  6653. $expected = array(
  6654. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6655. 'Non Existing Nor Validated',
  6656. '/label',
  6657. 'input' => array(
  6658. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6659. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6660. )
  6661. );
  6662. $this->assertTags($result, $expected);
  6663. $this->Form->request->data = array(
  6664. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6665. ));
  6666. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6667. $expected = array(
  6668. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6669. 'Non Existing Nor Validated',
  6670. '/label',
  6671. 'input' => array(
  6672. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6673. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6674. )
  6675. );
  6676. $this->assertTags($result, $expected);
  6677. }
  6678. /**
  6679. * testFormMagicInputLabel method
  6680. *
  6681. * @return void
  6682. */
  6683. public function testFormMagicInputLabel() {
  6684. $encoding = strtolower(Configure::read('App.encoding'));
  6685. $result = $this->Form->create('Contact');
  6686. $expected = array(
  6687. 'form' => array(
  6688. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6689. 'accept-charset' => $encoding
  6690. ),
  6691. 'div' => array('style' => 'display:none;'),
  6692. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6693. '/div'
  6694. );
  6695. $this->assertTags($result, $expected);
  6696. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6697. $this->assertTags($result, array('input' => array(
  6698. 'name' => 'data[Contact][name]', 'type' => 'text',
  6699. 'id' => 'ContactName', 'maxlength' => '255')
  6700. ));
  6701. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6702. $expected = array(
  6703. 'label' => array('for' => 'ContactName'),
  6704. 'My label',
  6705. '/label',
  6706. 'input' => array(
  6707. 'type' => 'text', 'name' => 'data[Contact][name]',
  6708. 'id' => 'ContactName', 'maxlength' => '255'
  6709. )
  6710. );
  6711. $this->assertTags($result, $expected);
  6712. $result = $this->Form->input('Contact.name', array(
  6713. 'div' => false, 'label' => array('class' => 'mandatory')
  6714. ));
  6715. $expected = array(
  6716. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6717. 'Name',
  6718. '/label',
  6719. 'input' => array(
  6720. 'type' => 'text', 'name' => 'data[Contact][name]',
  6721. 'id' => 'ContactName', 'maxlength' => '255'
  6722. )
  6723. );
  6724. $this->assertTags($result, $expected);
  6725. $result = $this->Form->input('Contact.name', array(
  6726. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6727. ));
  6728. $expected = array(
  6729. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6730. 'My label',
  6731. '/label',
  6732. 'input' => array(
  6733. 'type' => 'text', 'name' => 'data[Contact][name]',
  6734. 'id' => 'ContactName', 'maxlength' => '255'
  6735. )
  6736. );
  6737. $this->assertTags($result, $expected);
  6738. $result = $this->Form->input('Contact.name', array(
  6739. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6740. ));
  6741. $expected = array(
  6742. 'label' => array('for' => 'my_id'),
  6743. 'Name',
  6744. '/label',
  6745. 'input' => array(
  6746. 'type' => 'text', 'name' => 'data[Contact][name]',
  6747. 'id' => 'my_id', 'maxlength' => '255'
  6748. )
  6749. );
  6750. $this->assertTags($result, $expected);
  6751. $result = $this->Form->input('1.id');
  6752. $this->assertTags($result, array('input' => array(
  6753. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6754. 'id' => 'Contact1Id'
  6755. )));
  6756. $result = $this->Form->input("1.name");
  6757. $expected = array(
  6758. 'div' => array('class' => 'input text'),
  6759. 'label' => array('for' => 'Contact1Name'),
  6760. 'Name',
  6761. '/label',
  6762. 'input' => array(
  6763. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6764. 'id' => 'Contact1Name', 'maxlength' => '255'
  6765. ),
  6766. '/div'
  6767. );
  6768. $this->assertTags($result, $expected);
  6769. $result = $this->Form->input('Contact.1.id');
  6770. $this->assertTags($result, array(
  6771. 'input' => array(
  6772. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6773. 'id' => 'Contact1Id'
  6774. )
  6775. ));
  6776. $result = $this->Form->input("Model.1.name");
  6777. $expected = array(
  6778. 'div' => array('class' => 'input text'),
  6779. 'label' => array('for' => 'Model1Name'),
  6780. 'Name',
  6781. '/label',
  6782. 'input' => array(
  6783. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6784. 'id' => 'Model1Name'
  6785. ),
  6786. '/div'
  6787. );
  6788. $this->assertTags($result, $expected);
  6789. }
  6790. /**
  6791. * testFormEnd method
  6792. *
  6793. * @return void
  6794. */
  6795. public function testFormEnd() {
  6796. $this->assertEquals('</form>', $this->Form->end());
  6797. $result = $this->Form->end('');
  6798. $expected = array(
  6799. 'div' => array('class' => 'submit'),
  6800. 'input' => array('type' => 'submit', 'value' => ''),
  6801. '/div',
  6802. '/form'
  6803. );
  6804. $this->assertTags($result, $expected);
  6805. $result = $this->Form->end(array('label' => ''));
  6806. $expected = array(
  6807. 'div' => array('class' => 'submit'),
  6808. 'input' => array('type' => 'submit', 'value' => ''),
  6809. '/div',
  6810. '/form'
  6811. );
  6812. $this->assertTags($result, $expected);
  6813. $result = $this->Form->end('save');
  6814. $expected = array(
  6815. 'div' => array('class' => 'submit'),
  6816. 'input' => array('type' => 'submit', 'value' => 'save'),
  6817. '/div',
  6818. '/form'
  6819. );
  6820. $this->assertTags($result, $expected);
  6821. $result = $this->Form->end(array('label' => 'save'));
  6822. $expected = array(
  6823. 'div' => array('class' => 'submit'),
  6824. 'input' => array('type' => 'submit', 'value' => 'save'),
  6825. '/div',
  6826. '/form'
  6827. );
  6828. $this->assertTags($result, $expected);
  6829. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  6830. $expected = array(
  6831. 'div' => array('class' => 'submit'),
  6832. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6833. '/div',
  6834. '/form'
  6835. );
  6836. $this->assertTags($result, $expected);
  6837. $result = $this->Form->end(array('name' => 'Whatever'));
  6838. $expected = array(
  6839. 'div' => array('class' => 'submit'),
  6840. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  6841. '/div',
  6842. '/form'
  6843. );
  6844. $this->assertTags($result, $expected);
  6845. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  6846. $expected = array(
  6847. 'div' => array('class' => 'good'),
  6848. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6849. '/div',
  6850. '/form'
  6851. );
  6852. $this->assertTags($result, $expected);
  6853. $result = $this->Form->end(array(
  6854. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  6855. ));
  6856. $expected = array(
  6857. 'div' => array('class' => 'good'),
  6858. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6859. '/div',
  6860. '/form'
  6861. );
  6862. $this->assertTags($result, $expected);
  6863. }
  6864. /**
  6865. * testMultipleFormWithIdFields method
  6866. *
  6867. * @return void
  6868. */
  6869. public function testMultipleFormWithIdFields() {
  6870. $this->Form->create('UserForm');
  6871. $result = $this->Form->input('id');
  6872. $this->assertTags($result, array('input' => array(
  6873. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  6874. )));
  6875. $result = $this->Form->input('ValidateItem.id');
  6876. $this->assertTags($result, array('input' => array(
  6877. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  6878. 'id' => 'ValidateItemId'
  6879. )));
  6880. $result = $this->Form->input('ValidateUser.id');
  6881. $this->assertTags($result, array('input' => array(
  6882. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  6883. 'id' => 'ValidateUserId'
  6884. )));
  6885. }
  6886. /**
  6887. * testDbLessModel method
  6888. *
  6889. * @return void
  6890. */
  6891. public function testDbLessModel() {
  6892. $this->Form->create('TestMail');
  6893. $result = $this->Form->input('name');
  6894. $expected = array(
  6895. 'div' => array('class' => 'input text'),
  6896. 'label' => array('for' => 'TestMailName'),
  6897. 'Name',
  6898. '/label',
  6899. 'input' => array(
  6900. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6901. 'id' => 'TestMailName'
  6902. ),
  6903. '/div'
  6904. );
  6905. $this->assertTags($result, $expected);
  6906. ClassRegistry::init('TestMail');
  6907. $this->Form->create('TestMail');
  6908. $result = $this->Form->input('name');
  6909. $expected = array(
  6910. 'div' => array('class' => 'input text'),
  6911. 'label' => array('for' => 'TestMailName'),
  6912. 'Name',
  6913. '/label',
  6914. 'input' => array(
  6915. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6916. 'id' => 'TestMailName'
  6917. ),
  6918. '/div'
  6919. );
  6920. $this->assertTags($result, $expected);
  6921. }
  6922. /**
  6923. * testBrokenness method
  6924. *
  6925. * @return void
  6926. */
  6927. public function testBrokenness() {
  6928. /*
  6929. * #4 This test has two parents and four children. By default (as of r7117) both
  6930. * parents are show but the first parent is missing a child. This is the inconsistency
  6931. * in the default behaviour - one parent has all children, the other does not - dependent
  6932. * on the data values.
  6933. */
  6934. $result = $this->Form->select('Model.field', array(
  6935. 'Fred' => array(
  6936. 'freds_son_1' => 'Fred',
  6937. 'freds_son_2' => 'Freddie'
  6938. ),
  6939. 'Bert' => array(
  6940. 'berts_son_1' => 'Albert',
  6941. 'berts_son_2' => 'Bertie')
  6942. ),
  6943. array('showParents' => true, 'empty' => false)
  6944. );
  6945. $expected = array(
  6946. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6947. array('optgroup' => array('label' => 'Fred')),
  6948. array('option' => array('value' => 'freds_son_1')),
  6949. 'Fred',
  6950. '/option',
  6951. array('option' => array('value' => 'freds_son_2')),
  6952. 'Freddie',
  6953. '/option',
  6954. '/optgroup',
  6955. array('optgroup' => array('label' => 'Bert')),
  6956. array('option' => array('value' => 'berts_son_1')),
  6957. 'Albert',
  6958. '/option',
  6959. array('option' => array('value' => 'berts_son_2')),
  6960. 'Bertie',
  6961. '/option',
  6962. '/optgroup',
  6963. '/select'
  6964. );
  6965. $this->assertTags($result, $expected);
  6966. /*
  6967. * #2 This is structurally identical to the test above (#1) - only the parent name has
  6968. * changed, so we should expect the same select list data, just with a different name
  6969. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  6970. * This is where data corruption can occur, because when a select value is missing from
  6971. * a list a form will substitute the first value in the list - without the user knowing.
  6972. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  6973. * affect the availability of 3 => 'Three' as a valid option.
  6974. */
  6975. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  6976. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  6977. ));
  6978. $result = $this->Form->select(
  6979. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  6980. );
  6981. $expected = array(
  6982. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6983. array('option' => array('value' => 1)),
  6984. 'One',
  6985. '/option',
  6986. array('option' => array('value' => 2)),
  6987. 'Two',
  6988. '/option',
  6989. array('optgroup' => array('label' => 'Three')),
  6990. array('option' => array('value' => 3)),
  6991. 'Three',
  6992. '/option',
  6993. array('option' => array('value' => 4)),
  6994. 'Four',
  6995. '/option',
  6996. array('option' => array('value' => 5)),
  6997. 'Five',
  6998. '/option',
  6999. '/optgroup',
  7000. '/select'
  7001. );
  7002. $this->assertTags($result, $expected);
  7003. }
  7004. /**
  7005. * Test the generation of fields for a multi record form.
  7006. *
  7007. * @return void
  7008. */
  7009. public function testMultiRecordForm() {
  7010. $this->Form->create('ValidateProfile');
  7011. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7012. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7013. $expected = array(
  7014. 'div' => array('class' => 'input textarea'),
  7015. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7016. 'Name',
  7017. '/label',
  7018. 'textarea' => array(
  7019. 'id' => 'ValidateProfile1ValidateItem2Name',
  7020. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7021. 'cols' => 30,
  7022. 'rows' => 6
  7023. ),
  7024. 'Value',
  7025. '/textarea',
  7026. '/div'
  7027. );
  7028. $this->assertTags($result, $expected);
  7029. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7030. $expected = array(
  7031. 'div' => array('class' => 'input date'),
  7032. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7033. 'Created',
  7034. '/label',
  7035. array('select' => array(
  7036. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7037. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7038. )
  7039. ),
  7040. array('option' => array('value' => '')), '/option',
  7041. $this->dateRegex['monthsRegex'],
  7042. '/select', '-',
  7043. array('select' => array(
  7044. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7045. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7046. )
  7047. ),
  7048. array('option' => array('value' => '')), '/option',
  7049. $this->dateRegex['daysRegex'],
  7050. '/select', '-',
  7051. array('select' => array(
  7052. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7053. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7054. )
  7055. ),
  7056. array('option' => array('value' => '')), '/option',
  7057. $this->dateRegex['yearsRegex'],
  7058. '/select',
  7059. '/div'
  7060. );
  7061. $this->assertTags($result, $expected);
  7062. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7063. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7064. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7065. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7066. $expected = array(
  7067. 'div' => array('class' => 'input select error'),
  7068. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7069. 'Profile',
  7070. '/label',
  7071. 'select' => array(
  7072. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7073. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7074. 'class' => 'form-error'
  7075. ),
  7076. '/select',
  7077. array('div' => array('class' => 'error-message')),
  7078. 'Error',
  7079. '/div',
  7080. '/div'
  7081. );
  7082. $this->assertTags($result, $expected);
  7083. }
  7084. /**
  7085. * test the correct display of multi-record form validation errors.
  7086. *
  7087. * @return void
  7088. */
  7089. public function testMultiRecordFormValidationErrors() {
  7090. $this->Form->create('ValidateProfile');
  7091. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7092. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7093. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7094. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7095. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7096. $result = $this->Form->error('ValidateProfile.2.city');
  7097. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7098. $result = $this->Form->error('2.city');
  7099. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7100. }
  7101. /**
  7102. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7103. *
  7104. * @return void
  7105. */
  7106. public function testInputTemplate() {
  7107. $result = $this->Form->input('Contact.email', array(
  7108. 'type' => 'text', 'format' => array('input')
  7109. ));
  7110. $expected = array(
  7111. 'div' => array('class' => 'input text'),
  7112. 'input' => array(
  7113. 'type' => 'text', 'name' => 'data[Contact][email]',
  7114. 'id' => 'ContactEmail'
  7115. ),
  7116. '/div'
  7117. );
  7118. $this->assertTags($result, $expected);
  7119. $result = $this->Form->input('Contact.email', array(
  7120. 'type' => 'text', 'format' => array('input', 'label'),
  7121. 'label' => '<em>Email (required)</em>'
  7122. ));
  7123. $expected = array(
  7124. 'div' => array('class' => 'input text'),
  7125. array('input' => array(
  7126. 'type' => 'text', 'name' => 'data[Contact][email]',
  7127. 'id' => 'ContactEmail'
  7128. )),
  7129. 'label' => array('for' => 'ContactEmail'),
  7130. 'em' => array(),
  7131. 'Email (required)',
  7132. '/em',
  7133. '/label',
  7134. '/div'
  7135. );
  7136. $this->assertTags($result, $expected);
  7137. $result = $this->Form->input('Contact.email', array(
  7138. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7139. 'between' => '<div>Something in the middle</div>',
  7140. 'after' => '<span>Some text at the end</span>'
  7141. ));
  7142. $expected = array(
  7143. 'div' => array('class' => 'input text'),
  7144. array('input' => array(
  7145. 'type' => 'text', 'name' => 'data[Contact][email]',
  7146. 'id' => 'ContactEmail'
  7147. )),
  7148. array('div' => array()),
  7149. 'Something in the middle',
  7150. '/div',
  7151. 'label' => array('for' => 'ContactEmail'),
  7152. 'Email',
  7153. '/label',
  7154. 'span' => array(),
  7155. 'Some text at the end',
  7156. '/span',
  7157. '/div'
  7158. );
  7159. $this->assertTags($result, $expected);
  7160. $result = $this->Form->input('Contact.method', array(
  7161. 'type' => 'radio',
  7162. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7163. 'between' => 'I am between',
  7164. ));
  7165. $expected = array(
  7166. 'div' => array('class' => 'input radio'),
  7167. 'fieldset' => array(),
  7168. 'legend' => array(),
  7169. 'Method',
  7170. '/legend',
  7171. 'I am between',
  7172. 'input' => array(
  7173. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7174. 'value' => '', 'id' => 'ContactMethod_'
  7175. ),
  7176. array('input' => array(
  7177. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7178. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7179. )),
  7180. array('label' => array('for' => 'ContactMethodEmail')),
  7181. 'Email',
  7182. '/label',
  7183. array('input' => array(
  7184. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7185. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7186. )),
  7187. array('label' => array('for' => 'ContactMethodPigeon')),
  7188. 'Pigeon',
  7189. '/label',
  7190. '/fieldset',
  7191. '/div',
  7192. );
  7193. $this->assertTags($result, $expected);
  7194. }
  7195. /**
  7196. * test that some html5 inputs + FormHelper::__call() work
  7197. *
  7198. * @return void
  7199. */
  7200. public function testHtml5Inputs() {
  7201. $result = $this->Form->email('User.email');
  7202. $expected = array(
  7203. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7204. );
  7205. $this->assertTags($result, $expected);
  7206. $result = $this->Form->search('User.query');
  7207. $expected = array(
  7208. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7209. );
  7210. $this->assertTags($result, $expected);
  7211. $result = $this->Form->search('User.query', array('value' => 'test'));
  7212. $expected = array(
  7213. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7214. );
  7215. $this->assertTags($result, $expected);
  7216. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7217. $expected = array(
  7218. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7219. );
  7220. $this->assertTags($result, $expected);
  7221. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7222. $expected = array(
  7223. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7224. );
  7225. $this->assertTags($result, $expected);
  7226. }
  7227. /**
  7228. *
  7229. * @expectedException CakeException
  7230. * @return void
  7231. */
  7232. public function testHtml5InputException() {
  7233. $this->Form->email();
  7234. }
  7235. /**
  7236. * Tests that a model can be loaded from the model names passed in the request object
  7237. *
  7238. * @return void
  7239. */
  7240. public function testIntrospectModelFromRequest() {
  7241. $this->loadFixtures('Post');
  7242. App::build(array(
  7243. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7244. ));
  7245. CakePlugin::load('TestPlugin');
  7246. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7247. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7248. $this->Form->create('TestPluginPost');
  7249. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7250. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7251. CakePlugin::unload();
  7252. App::build();
  7253. }
  7254. /**
  7255. * Tests that it is possible to set the validation errors directly in the helper for a field
  7256. *
  7257. * @return void
  7258. */
  7259. public function testCustomValidationErrors() {
  7260. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7261. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7262. $this->assertEquals('Badness!', $result);
  7263. }
  7264. /**
  7265. * Tests that the 'on' key validates as expected on create
  7266. *
  7267. * @return void
  7268. */
  7269. public function testRequiredOnCreate() {
  7270. $this->Form->create('Contact');
  7271. $result = $this->Form->input('Contact.imrequiredonupdate');
  7272. $expected = array(
  7273. 'div' => array('class' => 'input text'),
  7274. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7275. 'Imrequiredonupdate',
  7276. '/label',
  7277. 'input' => array(
  7278. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7279. 'id' => 'ContactImrequiredonupdate'
  7280. ),
  7281. '/div'
  7282. );
  7283. $this->assertTags($result, $expected);
  7284. $result = $this->Form->input('Contact.imrequiredoncreate');
  7285. $expected = array(
  7286. 'div' => array('class' => 'input text required'),
  7287. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7288. 'Imrequiredoncreate',
  7289. '/label',
  7290. 'input' => array(
  7291. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7292. 'id' => 'ContactImrequiredoncreate'
  7293. ),
  7294. '/div'
  7295. );
  7296. $this->assertTags($result, $expected);
  7297. $result = $this->Form->input('Contact.imrequiredonboth');
  7298. $expected = array(
  7299. 'div' => array('class' => 'input text required'),
  7300. 'label' => array('for' => 'ContactImrequiredonboth'),
  7301. 'Imrequiredonboth',
  7302. '/label',
  7303. 'input' => array(
  7304. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7305. 'id' => 'ContactImrequiredonboth'
  7306. ),
  7307. '/div'
  7308. );
  7309. $this->assertTags($result, $expected);
  7310. $result = $this->Form->input('Contact.imrequired');
  7311. $expected = array(
  7312. 'div' => array('class' => 'input text required'),
  7313. 'label' => array('for' => 'ContactImrequired'),
  7314. 'Imrequired',
  7315. '/label',
  7316. 'input' => array(
  7317. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7318. 'id' => 'ContactImrequired'
  7319. ),
  7320. '/div'
  7321. );
  7322. $this->assertTags($result, $expected);
  7323. }
  7324. /**
  7325. * Tests that the 'on' key validates as expected on update
  7326. *
  7327. * @return void
  7328. */
  7329. public function testRequiredOnUpdate() {
  7330. $this->Form->request->data['Contact']['id'] = 1;
  7331. $this->Form->create('Contact');
  7332. $result = $this->Form->input('Contact.imrequiredonupdate');
  7333. $expected = array(
  7334. 'div' => array('class' => 'input text required'),
  7335. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7336. 'Imrequiredonupdate',
  7337. '/label',
  7338. 'input' => array(
  7339. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7340. 'id' => 'ContactImrequiredonupdate'
  7341. ),
  7342. '/div'
  7343. );
  7344. $this->assertTags($result, $expected);
  7345. $result = $this->Form->input('Contact.imrequiredoncreate');
  7346. $expected = array(
  7347. 'div' => array('class' => 'input text'),
  7348. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7349. 'Imrequiredoncreate',
  7350. '/label',
  7351. 'input' => array(
  7352. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7353. 'id' => 'ContactImrequiredoncreate'
  7354. ),
  7355. '/div'
  7356. );
  7357. $this->assertTags($result, $expected);
  7358. $result = $this->Form->input('Contact.imrequiredonboth');
  7359. $expected = array(
  7360. 'div' => array('class' => 'input text required'),
  7361. 'label' => array('for' => 'ContactImrequiredonboth'),
  7362. 'Imrequiredonboth',
  7363. '/label',
  7364. 'input' => array(
  7365. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7366. 'id' => 'ContactImrequiredonboth'
  7367. ),
  7368. '/div'
  7369. );
  7370. $this->assertTags($result, $expected);
  7371. $result = $this->Form->input('Contact.imrequired');
  7372. $expected = array(
  7373. 'div' => array('class' => 'input text required'),
  7374. 'label' => array('for' => 'ContactImrequired'),
  7375. 'Imrequired',
  7376. '/label',
  7377. 'input' => array(
  7378. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7379. 'id' => 'ContactImrequired'
  7380. ),
  7381. '/div'
  7382. );
  7383. $this->assertTags($result, $expected);
  7384. }
  7385. /**
  7386. * Test inputDefaults setter and getter
  7387. *
  7388. * @return void
  7389. */
  7390. public function testInputDefaults() {
  7391. $this->Form->create('Contact');
  7392. $this->Form->inputDefaults(array(
  7393. 'label' => false,
  7394. 'div' => array(
  7395. 'style' => 'color: #000;'
  7396. )
  7397. ));
  7398. $result = $this->Form->input('Contact.field1');
  7399. $expected = array(
  7400. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7401. 'input' => array(
  7402. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7403. 'id' => 'ContactField1'
  7404. ),
  7405. '/div'
  7406. );
  7407. $this->assertTags($result, $expected);
  7408. $this->Form->inputDefaults(array(
  7409. 'div' => false,
  7410. 'label' => 'Label',
  7411. ));
  7412. $result = $this->Form->input('Contact.field1');
  7413. $expected = array(
  7414. 'label' => array('for' => 'ContactField1'),
  7415. 'Label',
  7416. '/label',
  7417. 'input' => array(
  7418. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7419. 'id' => 'ContactField1'
  7420. ),
  7421. );
  7422. $this->assertTags($result, $expected);
  7423. $this->Form->inputDefaults(array(
  7424. 'label' => false,
  7425. ), true);
  7426. $result = $this->Form->input('Contact.field1');
  7427. $expected = array(
  7428. 'input' => array(
  7429. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7430. 'id' => 'ContactField1'
  7431. ),
  7432. );
  7433. $this->assertTags($result, $expected);
  7434. $result = $this->Form->inputDefaults();
  7435. $expected = array(
  7436. 'div' => false,
  7437. 'label' => false,
  7438. );
  7439. $this->assertEqual($result, $expected);
  7440. }
  7441. }