FormHelperTest.php 188 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Collection\Collection;
  17. use Cake\Controller\Controller;
  18. use Cake\Core\App;
  19. use Cake\Core\Configure;
  20. use Cake\Core\Plugin;
  21. use Cake\Network\Request;
  22. use Cake\ORM\Entity;
  23. use Cake\ORM\Table;
  24. use Cake\ORM\TableRegistry;
  25. use Cake\Routing\Router;
  26. use Cake\TestSuite\TestCase;
  27. use Cake\Utility\Security;
  28. use Cake\View\Helper\FormHelper;
  29. use Cake\View\Helper\HtmlHelper;
  30. use Cake\View\View;
  31. /**
  32. * Test stub.
  33. */
  34. class Article extends Entity {
  35. }
  36. /**
  37. * Contact class
  38. *
  39. */
  40. class ContactsTable extends Table {
  41. /**
  42. * Default schema
  43. *
  44. * @var array
  45. */
  46. protected $_schema = array(
  47. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  48. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  49. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  50. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  51. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  52. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  53. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  54. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  55. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null),
  56. '_constraints' => array('primary' => ['type' => 'primary', 'columns' => ['id']])
  57. );
  58. /**
  59. * Initializes the schema
  60. *
  61. * @return void
  62. */
  63. public function initialize(array $config) {
  64. $this->schema($this->_schema);
  65. }
  66. }
  67. /**
  68. * ValidateUser class
  69. *
  70. */
  71. class ValidateUsersTable extends Table {
  72. /**
  73. * schema method
  74. *
  75. * @var array
  76. */
  77. protected $_schema = array(
  78. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  79. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  80. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'balance' => array('type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2),
  82. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3),
  83. 'ratio' => array('type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6),
  84. 'population' => array('type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0),
  85. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  86. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  87. '_constraints' => array('primary' => ['type' => 'primary', 'columns' => ['id']])
  88. );
  89. /**
  90. * Initializes the schema
  91. *
  92. * @return void
  93. */
  94. public function initialize(array $config) {
  95. $this->schema($this->_schema);
  96. }
  97. }
  98. /**
  99. * FormHelperTest class
  100. *
  101. * @property FormHelper $Form
  102. */
  103. class FormHelperTest extends TestCase {
  104. /**
  105. * Fixtures to be used
  106. *
  107. * @var array
  108. */
  109. public $fixtures = array('core.articles', 'core.comments');
  110. /**
  111. * Do not load the fixtures by default
  112. *
  113. * @var bool
  114. */
  115. public $autoFixtures = false;
  116. /**
  117. * setUp method
  118. *
  119. * @return void
  120. */
  121. public function setUp() {
  122. parent::setUp();
  123. Configure::write('Config.language', 'eng');
  124. Configure::write('App.base', '');
  125. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  126. Configure::delete('Asset');
  127. $this->View = new View();
  128. $this->Form = new FormHelper($this->View);
  129. $request = new Request('articles/add');
  130. $request->here = '/articles/add';
  131. $request['controller'] = 'articles';
  132. $request['action'] = 'add';
  133. $request->webroot = '';
  134. $request->base = '';
  135. $this->Form->Url->request = $this->Form->request = $request;
  136. $this->dateRegex = array(
  137. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  138. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  139. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  140. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  141. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  142. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  143. );
  144. $this->article = [
  145. 'schema' => [
  146. 'id' => ['type' => 'integer'],
  147. 'author_id' => ['type' => 'integer', 'null' => true],
  148. 'title' => ['type' => 'string', 'null' => true],
  149. 'body' => 'text',
  150. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  151. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  152. ],
  153. 'required' => [
  154. 'author_id' => true,
  155. 'title' => true,
  156. ]
  157. ];
  158. Security::salt('foo!');
  159. Router::connect('/:controller', array('action' => 'index'));
  160. Router::connect('/:controller/:action/*');
  161. }
  162. /**
  163. * tearDown method
  164. *
  165. * @return void
  166. */
  167. public function tearDown() {
  168. parent::tearDown();
  169. unset($this->Form, $this->Controller, $this->View);
  170. TableRegistry::clear();
  171. }
  172. /**
  173. * Test construct() with the templates option.
  174. *
  175. * @return void
  176. */
  177. public function testConstructTemplatesFile() {
  178. $helper = new FormHelper($this->View, [
  179. 'templates' => 'htmlhelper_tags.php'
  180. ]);
  181. $result = $helper->input('name');
  182. $this->assertContains('<input', $result);
  183. }
  184. /**
  185. * Test that when specifying custom widgets the config array for that widget
  186. * is overwritten instead of merged.
  187. *
  188. * @return void
  189. */
  190. public function testConstructWithWidgets() {
  191. $expected = [
  192. 'button' => ['Cake\View\Widget\ButtonWidget'],
  193. 'checkbox' => ['Cake\View\Widget\CheckboxWidget'],
  194. 'file' => ['Cake\View\Widget\FileWidget'],
  195. 'label' => ['Cake\View\Widget\LabelWidget'],
  196. 'nestingLabel' => ['Cake\View\Widget\NestingLabelWidget'],
  197. 'multicheckbox' => ['Cake\View\Widget\MultiCheckboxWidget', 'nestingLabel'],
  198. 'radio' => ['Cake\View\Widget\RadioWidget', 'nestingLabel'],
  199. 'select' => ['Cake\View\Widget\SelectBoxWidget'],
  200. 'textarea' => ['Cake\View\Widget\TextareaWidget'],
  201. 'datetime' => ['MyPlugin\View\Widget\DateTimeWidget', 'select'],
  202. '_default' => ['Cake\View\Widget\BasicWidget']
  203. ];
  204. $helper = $this->getMock(
  205. 'Cake\View\Helper\FormHelper',
  206. ['widgetRegistry'],
  207. [],
  208. '',
  209. false
  210. );
  211. $helper->expects($this->once())
  212. ->method('widgetRegistry')
  213. ->with(null, $expected);
  214. $config = [
  215. 'widgets' => [
  216. 'datetime' => ['MyPlugin\View\Widget\DateTimeWidget', 'select']
  217. ]
  218. ];
  219. $helper->__construct($this->View, $config);
  220. }
  221. /**
  222. * Test registering a new widget class and rendering it.
  223. *
  224. * @return void
  225. */
  226. public function testAddWidgetAndRenderWidget() {
  227. $data = [
  228. 'val' => 1
  229. ];
  230. $mock = $this->getMock('Cake\View\Widget\WidgetInterface');
  231. $this->assertNull($this->Form->addWidget('test', $mock));
  232. $mock->expects($this->once())
  233. ->method('render')
  234. ->with($data)
  235. ->will($this->returnValue('HTML'));
  236. $result = $this->Form->widget('test', $data);
  237. $this->assertEquals('HTML', $result);
  238. }
  239. /**
  240. * Test registering an invalid widget class.
  241. *
  242. * @expectedException \RuntimeException
  243. * @return void
  244. */
  245. public function testAddWidgetInvalid() {
  246. $mock = new \StdClass();
  247. $this->Form->addWidget('test', $mock);
  248. $this->Form->widget('test');
  249. }
  250. /**
  251. * Test adding a new context class.
  252. *
  253. * @return void
  254. */
  255. public function testAddContextProvider() {
  256. $context = 'My data';
  257. $stub = $this->getMock('Cake\View\Form\ContextInterface');
  258. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  259. $this->assertInstanceOf('Cake\Network\Request', $request);
  260. $this->assertEquals($context, $data['entity']);
  261. return $stub;
  262. });
  263. $this->Form->create($context);
  264. $result = $this->Form->context();
  265. $this->assertSame($stub, $result);
  266. }
  267. /**
  268. * Test replacing a context class.
  269. *
  270. * @return void
  271. */
  272. public function testAddContextProviderReplace() {
  273. $entity = new Article();
  274. $stub = $this->getMock('Cake\View\Form\ContextInterface');
  275. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  276. return $stub;
  277. });
  278. $this->Form->create($entity);
  279. $result = $this->Form->context();
  280. $this->assertSame($stub, $result);
  281. }
  282. /**
  283. * Test overriding a context class.
  284. *
  285. * @return void
  286. */
  287. public function testAddContextProviderAdd() {
  288. $entity = new Article();
  289. $stub = $this->getMock('Cake\View\Form\ContextInterface');
  290. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  291. if ($data['entity'] instanceof Entity) {
  292. return $stub;
  293. }
  294. });
  295. $this->Form->create($entity);
  296. $result = $this->Form->context();
  297. $this->assertSame($stub, $result);
  298. }
  299. /**
  300. * Test adding an invalid context class.
  301. *
  302. * @expectedException RuntimeException
  303. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  304. * @return void
  305. */
  306. public function testAddContextProviderInvalid() {
  307. $context = 'My data';
  308. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  309. return new \StdClass();
  310. });
  311. $this->Form->create($context);
  312. }
  313. /**
  314. * Provides context options for create().
  315. *
  316. * @return array
  317. */
  318. public function contextSelectionProvider() {
  319. $entity = new Article();
  320. $collection = $this->getMock('Cake\Collection\Collection', ['extract'], [[$entity]]);
  321. $emptyCollection = new Collection([]);
  322. $emptyArray = [];
  323. $arrayObject = new \ArrayObject([]);
  324. $data = [
  325. 'schema' => [
  326. 'title' => ['type' => 'string']
  327. ]
  328. ];
  329. return [
  330. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  331. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  332. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  333. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  334. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  335. 'none' => [null, 'Cake\View\Form\NullContext'],
  336. 'false' => [false, 'Cake\View\Form\NullContext'],
  337. ];
  338. }
  339. /**
  340. * Test default context selection in create()
  341. *
  342. * @dataProvider contextSelectionProvider
  343. * @return void
  344. */
  345. public function testCreateContextSelectionBuiltIn($data, $class) {
  346. $this->loadFixtures('Articles');
  347. $this->Form->create($data);
  348. $this->assertInstanceOf($class, $this->Form->context());
  349. }
  350. /**
  351. * Data provider for type option.
  352. *
  353. * @return array
  354. */
  355. public static function requestTypeProvider() {
  356. return [
  357. // type, method, override
  358. ['post', 'post', 'POST'],
  359. ['put', 'post', 'PUT'],
  360. ['patch', 'post', 'PATCH'],
  361. ['delete', 'post', 'DELETE'],
  362. ];
  363. }
  364. /**
  365. * Test creating file forms.
  366. *
  367. * @return void
  368. */
  369. public function testCreateFile() {
  370. $encoding = strtolower(Configure::read('App.encoding'));
  371. $result = $this->Form->create(false, array('type' => 'file'));
  372. $expected = array(
  373. 'form' => array(
  374. 'method' => 'post', 'action' => '/articles/add',
  375. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  376. ),
  377. 'div' => array('style' => 'display:none;'),
  378. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  379. '/div'
  380. );
  381. $this->assertHtml($expected, $result);
  382. }
  383. /**
  384. * Test creating GET forms.
  385. *
  386. * @return void
  387. */
  388. public function testCreateGet() {
  389. $encoding = strtolower(Configure::read('App.encoding'));
  390. $result = $this->Form->create(false, array('type' => 'get'));
  391. $expected = array('form' => array(
  392. 'method' => 'get', 'action' => '/articles/add',
  393. 'accept-charset' => $encoding
  394. ));
  395. $this->assertHtml($expected, $result);
  396. }
  397. /**
  398. * Test create() with the templates option.
  399. *
  400. * @return void
  401. */
  402. public function testCreateTemplatesArray() {
  403. $result = $this->Form->create($this->article, [
  404. 'templates' => [
  405. 'formstart' => '<form class="form-horizontal"{{attrs}}>',
  406. ]
  407. ]);
  408. $expected = [
  409. 'form' => [
  410. 'class' => 'form-horizontal',
  411. 'method' => 'post',
  412. 'action' => '/articles/add',
  413. 'accept-charset' => 'utf-8'
  414. ]
  415. ];
  416. $this->assertHtml($expected, $result);
  417. }
  418. /**
  419. * Test create() with the templates option.
  420. *
  421. * @return void
  422. */
  423. public function testCreateTemplatesFile() {
  424. $result = $this->Form->create($this->article, [
  425. 'templates' => 'htmlhelper_tags.php',
  426. ]);
  427. $expected = [
  428. 'start form',
  429. 'div' => ['class' => 'hidden'],
  430. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  431. '/div'
  432. ];
  433. $this->assertHtml($expected, $result);
  434. }
  435. /**
  436. * Test that create() and end() restore templates.
  437. *
  438. * @return void
  439. */
  440. public function testCreateEndRestoreTemplates() {
  441. $this->Form->create($this->article, [
  442. 'templates' => ['input' => 'custom input element']
  443. ]);
  444. $this->Form->end();
  445. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  446. }
  447. /**
  448. * test the create() method
  449. *
  450. * @dataProvider requestTypeProvider
  451. * @return void
  452. */
  453. public function testCreateTypeOptions($type, $method, $override) {
  454. $encoding = strtolower(Configure::read('App.encoding'));
  455. $result = $this->Form->create(false, array('type' => $type));
  456. $expected = array(
  457. 'form' => array(
  458. 'method' => $method, 'action' => '/articles/add',
  459. 'accept-charset' => $encoding
  460. ),
  461. 'div' => array('style' => 'display:none;'),
  462. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => $override),
  463. '/div'
  464. );
  465. $this->assertHtml($expected, $result);
  466. }
  467. /**
  468. * Test opening a form for an update operation.
  469. *
  470. * @return void
  471. */
  472. public function testCreateUpdateForm() {
  473. $encoding = strtolower(Configure::read('App.encoding'));
  474. $this->Form->request->here = '/articles/edit/1';
  475. $this->Form->request['action'] = 'edit';
  476. $this->article['defaults']['id'] = 1;
  477. $result = $this->Form->create($this->article);
  478. $expected = array(
  479. 'form' => array(
  480. 'method' => 'post', 'action' => '/articles/edit/1',
  481. 'accept-charset' => $encoding
  482. ),
  483. 'div' => array('style' => 'display:none;'),
  484. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  485. '/div'
  486. );
  487. $this->assertHtml($expected, $result);
  488. }
  489. /**
  490. * test create() with automatic url generation
  491. *
  492. * @return void
  493. */
  494. public function testCreateAutoUrl() {
  495. $encoding = strtolower(Configure::read('App.encoding'));
  496. $this->Form->request['action'] = 'delete';
  497. $this->Form->request->here = '/articles/delete/10';
  498. $this->Form->request->base = '';
  499. $result = $this->Form->create($this->article);
  500. $expected = array(
  501. 'form' => array(
  502. 'method' => 'post', 'action' => '/articles/delete/10',
  503. 'accept-charset' => $encoding
  504. ),
  505. 'div' => array('style' => 'display:none;'),
  506. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  507. '/div'
  508. );
  509. $this->assertHtml($expected, $result);
  510. $this->article['defaults'] = ['id' => 1];
  511. $this->Form->request->here = '/articles/edit/1';
  512. $this->Form->request['action'] = 'delete';
  513. $result = $this->Form->create($this->article, ['action' => 'edit']);
  514. $expected = array(
  515. 'form' => array(
  516. 'method' => 'post',
  517. 'action' => '/articles/edit/1',
  518. 'accept-charset' => $encoding
  519. ),
  520. 'div' => array('style' => 'display:none;'),
  521. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  522. '/div'
  523. );
  524. $this->assertHtml($expected, $result);
  525. $this->Form->request['action'] = 'add';
  526. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  527. $expected = array(
  528. 'form' => array(
  529. 'method' => 'post',
  530. 'action' => '/articles/publish/1',
  531. 'accept-charset' => $encoding
  532. ),
  533. 'div' => array('style' => 'display:none;'),
  534. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  535. '/div'
  536. );
  537. $this->assertHtml($expected, $result);
  538. $result = $this->Form->create($this->article, array('url' => '/articles/publish'));
  539. $expected = array(
  540. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  541. 'div' => array('style' => 'display:none;'),
  542. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  543. '/div'
  544. );
  545. $this->assertHtml($expected, $result);
  546. $this->Form->request['controller'] = 'Pages';
  547. $result = $this->Form->create($this->article, array('action' => 'signup'));
  548. $expected = array(
  549. 'form' => array(
  550. 'method' => 'post', 'action' => '/Pages/signup/1',
  551. 'accept-charset' => $encoding
  552. ),
  553. 'div' => array('style' => 'display:none;'),
  554. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  555. '/div'
  556. );
  557. $this->assertHtml($expected, $result);
  558. }
  559. /**
  560. * test create() with a custom route
  561. *
  562. * @return void
  563. */
  564. public function testCreateCustomRoute() {
  565. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  566. $encoding = strtolower(Configure::read('App.encoding'));
  567. $this->Form->request['controller'] = 'users';
  568. $result = $this->Form->create(false, array('action' => 'login'));
  569. $expected = array(
  570. 'form' => array(
  571. 'method' => 'post', 'action' => '/login',
  572. 'accept-charset' => $encoding
  573. ),
  574. 'div' => array('style' => 'display:none;'),
  575. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  576. '/div'
  577. );
  578. $this->assertHtml($expected, $result);
  579. Router::connect(
  580. '/new-article',
  581. ['controller' => 'articles', 'action' => 'myaction'],
  582. ['_name' => 'my-route']
  583. );
  584. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  585. $expected = array(
  586. 'form' => array(
  587. 'method' => 'post', 'action' => '/new-article',
  588. 'accept-charset' => $encoding,
  589. ),
  590. 'div' => array('style' => 'display:none;'),
  591. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  592. '/div'
  593. );
  594. $this->assertHtml($expected, $result);
  595. }
  596. /**
  597. * test automatic accept-charset overriding
  598. *
  599. * @return void
  600. */
  601. public function testCreateWithAcceptCharset() {
  602. $result = $this->Form->create($this->article, array(
  603. 'type' => 'post', 'action' => 'index', 'encoding' => 'iso-8859-1'
  604. )
  605. );
  606. $expected = array(
  607. 'form' => array(
  608. 'method' => 'post', 'action' => '/articles',
  609. 'accept-charset' => 'iso-8859-1'
  610. ),
  611. 'div' => array('style' => 'display:none;'),
  612. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  613. '/div'
  614. );
  615. $this->assertHtml($expected, $result);
  616. }
  617. /**
  618. * Test base form URL when url param is passed with multiple parameters (&)
  619. *
  620. */
  621. public function testCreateQuerystringrequest() {
  622. $encoding = strtolower(Configure::read('App.encoding'));
  623. $result = $this->Form->create($this->article, array(
  624. 'type' => 'post',
  625. 'escape' => false,
  626. 'url' => array(
  627. 'controller' => 'controller',
  628. 'action' => 'action',
  629. '?' => array('param1' => 'value1', 'param2' => 'value2')
  630. )
  631. ));
  632. $expected = array(
  633. 'form' => array(
  634. 'method' => 'post',
  635. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  636. 'accept-charset' => $encoding
  637. ),
  638. 'div' => array('style' => 'display:none;'),
  639. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  640. '/div'
  641. );
  642. $this->assertHtml($expected, $result);
  643. $result = $this->Form->create($this->article, array(
  644. 'type' => 'post',
  645. 'url' => array(
  646. 'controller' => 'controller',
  647. 'action' => 'action',
  648. '?' => array('param1' => 'value1', 'param2' => 'value2')
  649. )
  650. ));
  651. $this->assertHtml($expected, $result);
  652. }
  653. /**
  654. * test that create() doesn't cause errors by multiple id's being in the primary key
  655. * as could happen with multiple select or checkboxes.
  656. *
  657. * @return void
  658. */
  659. public function testCreateWithMultipleIdInData() {
  660. $encoding = strtolower(Configure::read('App.encoding'));
  661. $this->Form->request->data['Article']['id'] = array(1, 2);
  662. $result = $this->Form->create($this->article);
  663. $expected = array(
  664. 'form' => array(
  665. 'method' => 'post',
  666. 'action' => '/articles/add',
  667. 'accept-charset' => $encoding
  668. ),
  669. 'div' => array('style' => 'display:none;'),
  670. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  671. '/div'
  672. );
  673. $this->assertHtml($expected, $result);
  674. }
  675. /**
  676. * test that create() doesn't add in extra passed params.
  677. *
  678. * @return void
  679. */
  680. public function testCreatePassedArgs() {
  681. $encoding = strtolower(Configure::read('App.encoding'));
  682. $this->Form->request->data['Article']['id'] = 1;
  683. $result = $this->Form->create($this->article, array(
  684. 'type' => 'post',
  685. 'escape' => false,
  686. 'url' => array(
  687. 'action' => 'edit',
  688. 'myparam'
  689. )
  690. ));
  691. $expected = array(
  692. 'form' => array(
  693. 'method' => 'post',
  694. 'action' => '/articles/edit/myparam',
  695. 'accept-charset' => $encoding
  696. ),
  697. 'div' => array('style' => 'display:none;'),
  698. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  699. '/div'
  700. );
  701. $this->assertHtml($expected, $result);
  702. }
  703. /**
  704. * test creating a get form, and get form inputs.
  705. *
  706. * @return void
  707. */
  708. public function testGetFormCreate() {
  709. $encoding = strtolower(Configure::read('App.encoding'));
  710. $result = $this->Form->create($this->article, array('type' => 'get'));
  711. $expected = array('form' => array(
  712. 'method' => 'get', 'action' => '/articles/add',
  713. 'accept-charset' => $encoding
  714. ));
  715. $this->assertHtml($expected, $result);
  716. $result = $this->Form->text('title');
  717. $expected = array('input' => array(
  718. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  719. ));
  720. $this->assertHtml($expected, $result);
  721. $result = $this->Form->password('password');
  722. $expected = array('input' => array(
  723. 'name' => 'password', 'type' => 'password'
  724. ));
  725. $this->assertHtml($expected, $result);
  726. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  727. $result = $this->Form->text('user_form');
  728. $expected = array('input' => array(
  729. 'name' => 'user_form', 'type' => 'text'
  730. ));
  731. $this->assertHtml($expected, $result);
  732. }
  733. /**
  734. * test get form, and inputs when the model param is false
  735. *
  736. * @return void
  737. */
  738. public function testGetFormWithFalseModel() {
  739. $encoding = strtolower(Configure::read('App.encoding'));
  740. $this->Form->request['controller'] = 'contact_test';
  741. $result = $this->Form->create(false, array(
  742. 'type' => 'get', 'url' => array('controller' => 'contact_test')
  743. ));
  744. $expected = array('form' => array(
  745. 'method' => 'get', 'action' => '/contact_test/add',
  746. 'accept-charset' => $encoding
  747. ));
  748. $this->assertHtml($expected, $result);
  749. $result = $this->Form->text('reason');
  750. $expected = array(
  751. 'input' => array('type' => 'text', 'name' => 'reason')
  752. );
  753. $this->assertHtml($expected, $result);
  754. }
  755. /**
  756. * testFormCreateWithSecurity method
  757. *
  758. * Test form->create() with security key.
  759. *
  760. * @return void
  761. */
  762. public function testCreateWithSecurity() {
  763. $this->Form->request->params['_csrfToken'] = 'testKey';
  764. $encoding = strtolower(Configure::read('App.encoding'));
  765. $result = $this->Form->create($this->article, [
  766. 'url' => '/articles/publish',
  767. ]);
  768. $expected = array(
  769. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  770. 'div' => array('style' => 'display:none;'),
  771. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  772. array('input' => array(
  773. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  774. )),
  775. '/div'
  776. );
  777. $this->assertHtml($expected, $result);
  778. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  779. $expected['form']['id'] = 'MyForm';
  780. $this->assertHtml($expected, $result);
  781. }
  782. /**
  783. * testFormCreateGetNoSecurity method
  784. *
  785. * Test form->create() with no security key as its a get form
  786. *
  787. * @return void
  788. */
  789. public function testCreateEndGetNoSecurity() {
  790. $this->Form->request->params['_csrfToken'] = 'testKey';
  791. $encoding = strtolower(Configure::read('App.encoding'));
  792. $article = new Article();
  793. $result = $this->Form->create($article, [
  794. 'type' => 'get',
  795. 'url' => '/contacts/add'
  796. ]);
  797. $this->assertNotContains('testKey', $result);
  798. $result = $this->Form->end();
  799. $this->assertNotContains('testKey', $result);
  800. }
  801. /**
  802. * test that create() clears the fields property so it starts fresh
  803. *
  804. * @return void
  805. */
  806. public function testCreateClearingFields() {
  807. $this->Form->fields = array('model_id');
  808. $this->Form->create($this->article);
  809. $this->assertEquals(array(), $this->Form->fields);
  810. }
  811. /**
  812. * Tests form hash generation with model-less data
  813. *
  814. * @return void
  815. */
  816. public function testValidateHashNoModel() {
  817. $this->Form->request->params['_Token'] = 'foo';
  818. $result = $this->Form->secure(array('anything'));
  819. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  820. }
  821. /**
  822. * Tests that hidden fields generated for checkboxes don't get locked
  823. *
  824. * @return void
  825. */
  826. public function testNoCheckboxLocking() {
  827. $this->Form->request->params['_Token'] = 'foo';
  828. $this->assertSame([], $this->Form->fields);
  829. $this->Form->checkbox('check', array('value' => '1'));
  830. $this->assertSame($this->Form->fields, array('check'));
  831. }
  832. /**
  833. * testFormSecurityFields method
  834. *
  835. * Test generation of secure form hash generation.
  836. *
  837. * @return void
  838. */
  839. public function testFormSecurityFields() {
  840. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  841. $this->Form->request->params['_Token'] = 'testKey';
  842. $result = $this->Form->secure($fields);
  843. $hash = Security::hash(serialize($fields) . Security::salt());
  844. $hash .= ':' . 'Model.valid';
  845. $hash = urlencode($hash);
  846. $expected = array(
  847. 'div' => array('style' => 'display:none;'),
  848. array('input' => array(
  849. 'type' => 'hidden',
  850. 'name' => '_Token[fields]',
  851. 'value' => $hash
  852. )),
  853. array('input' => array(
  854. 'type' => 'hidden',
  855. 'name' => '_Token[unlocked]',
  856. 'value' => '',
  857. )),
  858. '/div'
  859. );
  860. $this->assertHtml($expected, $result);
  861. }
  862. /**
  863. * Tests correct generation of number fields for double and float fields
  864. *
  865. * @return void
  866. */
  867. public function testTextFieldGenerationForFloats() {
  868. $this->article['schema'] = [
  869. 'foo' => [
  870. 'type' => 'float',
  871. 'null' => false,
  872. 'default' => null,
  873. 'length' => 10
  874. ]
  875. ];
  876. $this->Form->create($this->article);
  877. $result = $this->Form->input('foo');
  878. $expected = array(
  879. 'div' => array('class' => 'input number'),
  880. 'label' => array('for' => 'foo'),
  881. 'Foo',
  882. '/label',
  883. array('input' => array(
  884. 'type' => 'number',
  885. 'name' => 'foo',
  886. 'id' => 'foo',
  887. 'step' => 'any'
  888. )),
  889. '/div'
  890. );
  891. $this->assertHtml($expected, $result);
  892. $result = $this->Form->input('foo', array('step' => 0.5));
  893. $expected = array(
  894. 'div' => array('class' => 'input number'),
  895. 'label' => array('for' => 'foo'),
  896. 'Foo',
  897. '/label',
  898. array('input' => array(
  899. 'type' => 'number',
  900. 'name' => 'foo',
  901. 'id' => 'foo',
  902. 'step' => '0.5'
  903. )),
  904. '/div'
  905. );
  906. $this->assertHtml($expected, $result);
  907. }
  908. /**
  909. * Tests correct generation of number fields for integer fields
  910. *
  911. * @return void
  912. */
  913. public function testTextFieldTypeNumberGenerationForIntegers() {
  914. TableRegistry::get('Contacts', [
  915. 'className' => __NAMESPACE__ . '\ContactsTable'
  916. ]);
  917. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  918. $result = $this->Form->input('age');
  919. $expected = array(
  920. 'div' => array('class' => 'input number'),
  921. 'label' => array('for' => 'age'),
  922. 'Age',
  923. '/label',
  924. array('input' => array(
  925. 'type' => 'number', 'name' => 'age',
  926. 'id' => 'age'
  927. )),
  928. '/div'
  929. );
  930. $this->assertHtml($expected, $result);
  931. }
  932. /**
  933. * Tests correct generation of file upload fields for binary fields
  934. *
  935. * @return void
  936. */
  937. public function testFileUploadFieldTypeGenerationForBinaries() {
  938. $table = TableRegistry::get('Contacts', [
  939. 'className' => __NAMESPACE__ . '\ContactsTable'
  940. ]);
  941. $table->schema(array('foo' => array(
  942. 'type' => 'binary',
  943. 'null' => false,
  944. 'default' => null,
  945. 'length' => 1024
  946. )));
  947. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  948. $result = $this->Form->input('foo');
  949. $expected = array(
  950. 'div' => array('class' => 'input file'),
  951. 'label' => array('for' => 'foo'),
  952. 'Foo',
  953. '/label',
  954. array('input' => array(
  955. 'type' => 'file', 'name' => 'foo',
  956. 'id' => 'foo'
  957. )),
  958. '/div'
  959. );
  960. $this->assertHtml($expected, $result);
  961. }
  962. /**
  963. * testFormSecurityMultipleFields method
  964. *
  965. * Test secure() with multiple row form. Ensure hash is correct.
  966. *
  967. * @return void
  968. */
  969. public function testFormSecurityMultipleFields() {
  970. $this->Form->request->params['_Token'] = 'foo';
  971. $fields = array(
  972. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  973. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  974. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  975. );
  976. $result = $this->Form->secure($fields);
  977. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  978. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  979. $expected = array(
  980. 'div' => array('style' => 'display:none;'),
  981. array('input' => array(
  982. 'type' => 'hidden', 'name' => '_Token[fields]',
  983. 'value' => $hash
  984. )),
  985. array('input' => array(
  986. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  987. 'value' => ''
  988. )),
  989. '/div'
  990. );
  991. $this->assertHtml($expected, $result);
  992. }
  993. /**
  994. * testFormSecurityMultipleSubmitButtons
  995. *
  996. * test form submit generation and ensure that _Token is only created on end()
  997. *
  998. * @return void
  999. */
  1000. public function testFormSecurityMultipleSubmitButtons() {
  1001. $this->Form->request->params['_Token'] = 'testKey';
  1002. $this->Form->create($this->article);
  1003. $this->Form->text('Address.title');
  1004. $this->Form->text('Address.first_name');
  1005. $result = $this->Form->submit('Save', array('name' => 'save'));
  1006. $expected = array(
  1007. 'div' => array('class' => 'submit'),
  1008. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  1009. '/div',
  1010. );
  1011. $this->assertHtml($expected, $result);
  1012. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  1013. $expected = array(
  1014. 'div' => array('class' => 'submit'),
  1015. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  1016. '/div',
  1017. );
  1018. $this->assertHtml($expected, $result);
  1019. $result = $this->Form->end();
  1020. $expected = array(
  1021. 'div' => array('style' => 'display:none;'),
  1022. array('input' => array(
  1023. 'type' => 'hidden',
  1024. 'name' => '_Token[fields]',
  1025. 'value'
  1026. )),
  1027. array('input' => array(
  1028. 'type' => 'hidden',
  1029. 'name' => '_Token[unlocked]',
  1030. 'value' => 'cancel%7Csave'
  1031. )),
  1032. '/div'
  1033. );
  1034. $this->assertHtml($expected, $result);
  1035. }
  1036. /**
  1037. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1038. *
  1039. * @return void
  1040. */
  1041. public function testSecurityButtonNestedNamed() {
  1042. $key = 'testKey';
  1043. $this->Form->request->params['_csrfToken'] = $key;
  1044. $this->Form->create('Addresses');
  1045. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1046. $result = $this->Form->unlockField();
  1047. $this->assertEquals(array('Address.button'), $result);
  1048. }
  1049. /**
  1050. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1051. *
  1052. * @return void
  1053. */
  1054. public function testSecuritySubmitNestedNamed() {
  1055. $this->Form->request->params['_Token'] = 'testKey';
  1056. $this->Form->create($this->article);
  1057. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1058. $result = $this->Form->unlockField();
  1059. $this->assertEquals(array('Address.button'), $result);
  1060. }
  1061. /**
  1062. * Test that the correct fields are unlocked for image submits with no names.
  1063. *
  1064. * @return void
  1065. */
  1066. public function testSecuritySubmitImageNoName() {
  1067. $key = 'testKey';
  1068. $this->Form->request->params['_Token'] = 'testKey';
  1069. $this->Form->create(false);
  1070. $result = $this->Form->submit('save.png');
  1071. $expected = array(
  1072. 'div' => array('class' => 'submit'),
  1073. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  1074. '/div'
  1075. );
  1076. $this->assertHtml($expected, $result);
  1077. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  1078. }
  1079. /**
  1080. * Test that the correct fields are unlocked for image submits with names.
  1081. *
  1082. * @return void
  1083. */
  1084. public function testSecuritySubmitImageName() {
  1085. $this->Form->request->params['_Token'] = 'testKey';
  1086. $this->Form->create(null);
  1087. $result = $this->Form->submit('save.png', array('name' => 'test'));
  1088. $expected = array(
  1089. 'div' => array('class' => 'submit'),
  1090. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  1091. '/div'
  1092. );
  1093. $this->assertHtml($expected, $result);
  1094. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  1095. }
  1096. /**
  1097. * testFormSecurityMultipleInputFields method
  1098. *
  1099. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1100. *
  1101. * @return void
  1102. */
  1103. public function testFormSecurityMultipleInputFields() {
  1104. $this->Form->request->params['_Token'] = 'testKey';
  1105. $this->Form->create();
  1106. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1107. $this->Form->input('Addresses.0.title');
  1108. $this->Form->input('Addresses.0.first_name');
  1109. $this->Form->input('Addresses.0.last_name');
  1110. $this->Form->input('Addresses.0.address');
  1111. $this->Form->input('Addresses.0.city');
  1112. $this->Form->input('Addresses.0.phone');
  1113. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  1114. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1115. $this->Form->input('Addresses.1.title');
  1116. $this->Form->input('Addresses.1.first_name');
  1117. $this->Form->input('Addresses.1.last_name');
  1118. $this->Form->input('Addresses.1.address');
  1119. $this->Form->input('Addresses.1.city');
  1120. $this->Form->input('Addresses.1.phone');
  1121. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  1122. $result = $this->Form->secure($this->Form->fields);
  1123. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1124. $expected = array(
  1125. 'div' => array('style' => 'display:none;'),
  1126. array('input' => array(
  1127. 'type' => 'hidden', 'name' => '_Token[fields]',
  1128. 'value' => $hash
  1129. )),
  1130. array('input' => array(
  1131. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1132. 'value' => ''
  1133. )),
  1134. '/div'
  1135. );
  1136. $this->assertHtml($expected, $result);
  1137. }
  1138. /**
  1139. * Test form security with Model.field.0 style inputs
  1140. *
  1141. * @return void
  1142. */
  1143. public function testFormSecurityArrayFields() {
  1144. $this->Form->request->params['_Token'] = 'testKey';
  1145. $this->Form->create();
  1146. $this->Form->text('Address.primary.1');
  1147. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1148. $this->Form->text('Address.secondary.1.0');
  1149. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1150. }
  1151. /**
  1152. * testFormSecurityMultipleInputDisabledFields method
  1153. *
  1154. * test secure form generation with multiple records and disabled fields.
  1155. *
  1156. * @return void
  1157. */
  1158. public function testFormSecurityMultipleInputDisabledFields() {
  1159. $this->Form->request->params['_Token'] = array(
  1160. 'unlockedFields' => array('first_name', 'address')
  1161. );
  1162. $this->Form->create();
  1163. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1164. $this->Form->text('Addresses.0.title');
  1165. $this->Form->text('Addresses.0.first_name');
  1166. $this->Form->text('Addresses.0.last_name');
  1167. $this->Form->text('Addresses.0.address');
  1168. $this->Form->text('Addresses.0.city');
  1169. $this->Form->text('Addresses.0.phone');
  1170. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1171. $this->Form->text('Addresses.1.title');
  1172. $this->Form->text('Addresses.1.first_name');
  1173. $this->Form->text('Addresses.1.last_name');
  1174. $this->Form->text('Addresses.1.address');
  1175. $this->Form->text('Addresses.1.city');
  1176. $this->Form->text('Addresses.1.phone');
  1177. $result = $this->Form->secure($this->Form->fields);
  1178. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1179. $expected = array(
  1180. 'div' => array('style' => 'display:none;'),
  1181. array('input' => array(
  1182. 'type' => 'hidden',
  1183. 'name' => '_Token[fields]',
  1184. 'value' => $hash
  1185. )),
  1186. array('input' => array(
  1187. 'type' => 'hidden',
  1188. 'name' => '_Token[unlocked]',
  1189. 'value' => 'address%7Cfirst_name',
  1190. )),
  1191. '/div'
  1192. );
  1193. $this->assertHtml($expected, $result);
  1194. }
  1195. /**
  1196. * testFormSecurityInputDisabledFields method
  1197. *
  1198. * Test single record form with disabled fields.
  1199. *
  1200. * @return void
  1201. */
  1202. public function testFormSecurityInputUnlockedFields() {
  1203. $this->Form->request['_Token'] = array(
  1204. 'unlockedFields' => array('first_name', 'address')
  1205. );
  1206. $this->Form->create();
  1207. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1208. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1209. $this->Form->text('Addresses.title');
  1210. $this->Form->text('Addresses.first_name');
  1211. $this->Form->text('Addresses.last_name');
  1212. $this->Form->text('Addresses.address');
  1213. $this->Form->text('Addresses.city');
  1214. $this->Form->text('Addresses.phone');
  1215. $result = $this->Form->fields;
  1216. $expected = array(
  1217. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1218. 'Addresses.city', 'Addresses.phone'
  1219. );
  1220. $this->assertEquals($expected, $result);
  1221. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1222. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1223. $expected = array(
  1224. 'div' => array('style' => 'display:none;'),
  1225. array('input' => array(
  1226. 'type' => 'hidden',
  1227. 'name' => '_Token[fields]',
  1228. 'value' => $hash,
  1229. 'data-foo' => 'bar',
  1230. )),
  1231. array('input' => array(
  1232. 'type' => 'hidden',
  1233. 'name' => '_Token[unlocked]',
  1234. 'value' => 'address%7Cfirst_name',
  1235. 'data-foo' => 'bar',
  1236. )),
  1237. '/div'
  1238. );
  1239. $this->assertHtml($expected, $result);
  1240. }
  1241. /**
  1242. * test securing inputs with custom name attributes.
  1243. *
  1244. * @return void
  1245. */
  1246. public function testFormSecureWithCustomNameAttribute() {
  1247. $this->Form->request->params['_Token'] = 'testKey';
  1248. $this->Form->text('UserForm.published', array('name' => 'User[custom]'));
  1249. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1250. $this->Form->text('UserForm.published', array('name' => 'User[custom][another][value]'));
  1251. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1252. }
  1253. /**
  1254. * testFormSecuredInput method
  1255. *
  1256. * Test generation of entire secure form, assertions made on input() output.
  1257. *
  1258. * @return void
  1259. */
  1260. public function testFormSecuredInput() {
  1261. $this->Form->request->params['_csrfToken'] = 'testKey';
  1262. $this->Form->request->params['_Token'] = 'stuff';
  1263. $this->article['schema'] = [
  1264. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1265. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1266. ];
  1267. $result = $this->Form->create($this->article, array('url' => '/articles/add'));
  1268. $encoding = strtolower(Configure::read('App.encoding'));
  1269. $expected = array(
  1270. 'form' => array('method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding),
  1271. 'div' => array('style' => 'display:none;'),
  1272. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1273. array('input' => array(
  1274. 'type' => 'hidden',
  1275. 'name' => '_csrfToken',
  1276. 'value' => 'testKey'
  1277. )),
  1278. '/div'
  1279. );
  1280. $this->assertHtml($expected, $result);
  1281. $result = $this->Form->input('ratio');
  1282. $expected = array(
  1283. 'div' => array('class'),
  1284. 'label' => array('for'),
  1285. 'Ratio',
  1286. '/label',
  1287. 'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
  1288. '/div',
  1289. );
  1290. $this->assertHtml($expected, $result);
  1291. $result = $this->Form->input('population');
  1292. $expected = array(
  1293. 'div' => array('class'),
  1294. 'label' => array('for'),
  1295. 'Population',
  1296. '/label',
  1297. 'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
  1298. '/div',
  1299. );
  1300. $this->assertHtml($expected, $result);
  1301. $result = $this->Form->input('published', array('type' => 'text'));
  1302. $expected = array(
  1303. 'div' => array('class' => 'input text'),
  1304. 'label' => array('for' => 'published'),
  1305. 'Published',
  1306. '/label',
  1307. array('input' => array(
  1308. 'type' => 'text',
  1309. 'name' => 'published',
  1310. 'id' => 'published'
  1311. )),
  1312. '/div'
  1313. );
  1314. $this->assertHtml($expected, $result);
  1315. $result = $this->Form->input('other', array('type' => 'text'));
  1316. $expected = array(
  1317. 'div' => array('class' => 'input text'),
  1318. 'label' => array('for' => 'other'),
  1319. 'Other',
  1320. '/label',
  1321. array('input' => array(
  1322. 'type' => 'text',
  1323. 'name' => 'other',
  1324. 'id',
  1325. )),
  1326. '/div'
  1327. );
  1328. $this->assertHtml($expected, $result);
  1329. $result = $this->Form->hidden('stuff');
  1330. $expected = array(
  1331. 'input' => array(
  1332. 'type' => 'hidden',
  1333. 'name' => 'stuff',
  1334. ));
  1335. $this->assertHtml($expected, $result);
  1336. $result = $this->Form->hidden('hidden', array('value' => '0'));
  1337. $expected = array('input' => array(
  1338. 'type' => 'hidden',
  1339. 'name' => 'hidden',
  1340. 'value' => '0'
  1341. ));
  1342. $this->assertHtml($expected, $result);
  1343. $result = $this->Form->input('something', array('type' => 'checkbox'));
  1344. $expected = array(
  1345. 'div' => array('class' => 'input checkbox'),
  1346. 'label' => array('for' => 'something'),
  1347. array('input' => array(
  1348. 'type' => 'hidden',
  1349. 'name' => 'something',
  1350. 'value' => '0'
  1351. )),
  1352. array('input' => array(
  1353. 'type' => 'checkbox',
  1354. 'name' => 'something',
  1355. 'value' => '1',
  1356. 'id' => 'something'
  1357. )),
  1358. 'Something',
  1359. '/label',
  1360. '/div'
  1361. );
  1362. $this->assertHtml($expected, $result);
  1363. $result = $this->Form->fields;
  1364. $expected = array(
  1365. 'ratio', 'population', 'published', 'other',
  1366. 'stuff' => '',
  1367. 'hidden' => '0',
  1368. 'something'
  1369. );
  1370. $this->assertEquals($expected, $result);
  1371. $result = $this->Form->secure($this->Form->fields);
  1372. $expected = array(
  1373. 'div' => array('style' => 'display:none;'),
  1374. array('input' => array(
  1375. 'type' => 'hidden',
  1376. 'name' => '_Token[fields]',
  1377. 'value'
  1378. )),
  1379. array('input' => array(
  1380. 'type' => 'hidden',
  1381. 'name' => '_Token[unlocked]',
  1382. 'value' => ''
  1383. )),
  1384. '/div'
  1385. );
  1386. $this->assertHtml($expected, $result);
  1387. }
  1388. /**
  1389. * Test secured inputs with custom names.
  1390. *
  1391. * @return void
  1392. */
  1393. public function testSecuredInputCustomName() {
  1394. $this->Form->request->params['_Token'] = 'testKey';
  1395. $this->assertEquals(array(), $this->Form->fields);
  1396. $this->Form->text('text_input', array(
  1397. 'name' => 'Option[General.default_role]',
  1398. ));
  1399. $expected = array('Option.General.default_role');
  1400. $this->assertEquals($expected, $this->Form->fields);
  1401. $this->Form->select('select_box', [1, 2], [
  1402. 'name' => 'Option[General.select_role]',
  1403. ]);
  1404. $expected = ['Option.General.default_role', 'Option.General.select_role'];
  1405. $this->assertEquals($expected, $this->Form->fields);
  1406. }
  1407. /**
  1408. * Tests that the correct keys are added to the field hash index
  1409. *
  1410. * @return void
  1411. */
  1412. public function testFormSecuredFileInput() {
  1413. $this->assertEquals(array(), $this->Form->fields);
  1414. $this->Form->file('Attachment.file');
  1415. $expected = array(
  1416. 'Attachment.file.name', 'Attachment.file.type',
  1417. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1418. 'Attachment.file.size'
  1419. );
  1420. $this->assertEquals($expected, $this->Form->fields);
  1421. }
  1422. /**
  1423. * test that multiple selects keys are added to field hash
  1424. *
  1425. * @return void
  1426. */
  1427. public function testFormSecuredMultipleSelect() {
  1428. $this->Form->request->params['_csrfToken'] = 'testKey';
  1429. $this->assertEquals(array(), $this->Form->fields);
  1430. $options = array('1' => 'one', '2' => 'two');
  1431. $this->Form->select('Model.select', $options);
  1432. $expected = array('Model.select');
  1433. $this->assertEquals($expected, $this->Form->fields);
  1434. $this->Form->fields = array();
  1435. $this->Form->select('Model.select', $options, array('multiple' => true));
  1436. $this->assertEquals($expected, $this->Form->fields);
  1437. }
  1438. /**
  1439. * testFormSecuredRadio method
  1440. *
  1441. * @return void
  1442. */
  1443. public function testFormSecuredRadio() {
  1444. $this->Form->request->params['_Token'] = 'testKey';
  1445. $this->assertEquals(array(), $this->Form->fields);
  1446. $options = array('1' => 'option1', '2' => 'option2');
  1447. $this->Form->radio('Test.test', $options);
  1448. $expected = array('Test.test');
  1449. $this->assertEquals($expected, $this->Form->fields);
  1450. }
  1451. /**
  1452. * Test that when disabled is in a list based attribute array it works.
  1453. *
  1454. * @return void
  1455. */
  1456. public function testFormSecuredAndDisabledNotAssoc() {
  1457. $this->Form->request->params['_Token'] = 'testKey';
  1458. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1459. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1460. $this->Form->text('Model.text', array('disabled'));
  1461. $this->Form->textarea('Model.textarea', array('disabled'));
  1462. $this->Form->password('Model.password', array('disabled'));
  1463. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1464. $expected = array(
  1465. 'Model.radio' => ''
  1466. );
  1467. $this->assertEquals($expected, $this->Form->fields);
  1468. }
  1469. /**
  1470. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1471. * hashing.
  1472. *
  1473. * @return void
  1474. */
  1475. public function testFormSecuredAndDisabled() {
  1476. $this->Form->request->params['_Token'] = 'testKey';
  1477. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1478. $this->Form->text('Model.text', array('disabled' => true));
  1479. $this->Form->password('Model.text', array('disabled' => true));
  1480. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1481. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1482. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1483. $this->Form->year('Model.year', array('disabled' => true));
  1484. $this->Form->month('Model.month', array('disabled' => true));
  1485. $this->Form->day('Model.day', array('disabled' => true));
  1486. $this->Form->hour('Model.hour', array('disabled' => true));
  1487. $this->Form->minute('Model.minute', array('disabled' => true));
  1488. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1489. $expected = array(
  1490. 'Model.radio' => ''
  1491. );
  1492. $this->assertEquals($expected, $this->Form->fields);
  1493. }
  1494. /**
  1495. * testDisableSecurityUsingForm method
  1496. *
  1497. * @return void
  1498. */
  1499. public function testDisableSecurityUsingForm() {
  1500. $this->Form->request['_Token'] = [
  1501. 'disabledFields' => []
  1502. ];
  1503. $this->Form->create();
  1504. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1505. $this->Form->text('Addresses.title');
  1506. $this->Form->text('Addresses.first_name', ['secure' => false]);
  1507. $this->Form->textarea('Addresses.city', ['secure' => false]);
  1508. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  1509. $result = $this->Form->fields;
  1510. $expected = [
  1511. 'Addresses.id' => '123456', 'Addresses.title',
  1512. ];
  1513. $this->assertEquals($expected, $result);
  1514. }
  1515. /**
  1516. * test disableField
  1517. *
  1518. * @return void
  1519. */
  1520. public function testUnlockFieldAddsToList() {
  1521. $this->Form->request['_Token'] = array(
  1522. 'unlockedFields' => array()
  1523. );
  1524. $this->Form->unlockField('Contact.name');
  1525. $this->Form->text('Contact.name');
  1526. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1527. $this->assertEquals(array(), $this->Form->fields);
  1528. }
  1529. /**
  1530. * test unlockField removing from fields array.
  1531. *
  1532. * @return void
  1533. */
  1534. public function testUnlockFieldRemovingFromFields() {
  1535. $this->Form->request['_Token'] = array(
  1536. 'unlockedFields' => array()
  1537. );
  1538. $this->Form->create($this->article);
  1539. $this->Form->hidden('Article.id', array('value' => 1));
  1540. $this->Form->text('Article.title');
  1541. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  1542. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  1543. $this->Form->unlockField('Article.title');
  1544. $this->Form->unlockField('Article.id');
  1545. $this->assertEquals(array(), $this->Form->fields);
  1546. }
  1547. /**
  1548. * Test that only the path + query elements of a form's URL show up in their hash.
  1549. *
  1550. * @return void
  1551. */
  1552. public function testSecuredFormUrlIgnoresHost() {
  1553. $this->Form->request['_Token'] = array('key' => 'testKey');
  1554. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  1555. $this->Form->create($this->article, array(
  1556. 'url' => array('controller' => 'articles', 'action' => 'view', 1, '?' => array('page' => 1))
  1557. ));
  1558. $result = $this->Form->secure();
  1559. $this->assertContains($expected, $result);
  1560. $this->Form->create($this->article, array('url' => 'http://localhost/articles/view/1?page=1'));
  1561. $result = $this->Form->secure();
  1562. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  1563. $this->Form->create($this->article, array('url' => '/articles/view/1?page=1'));
  1564. $result = $this->Form->secure();
  1565. $this->assertContains($expected, $result, 'URL path + query should work.');
  1566. $this->Form->create($this->article, array('url' => '/articles/view/1'));
  1567. $result = $this->Form->secure();
  1568. $this->assertNotContains($expected, $result, 'URL is different');
  1569. }
  1570. /**
  1571. * Test that URL, HTML and identifer show up in their hashs.
  1572. *
  1573. * @return void
  1574. */
  1575. public function testSecuredFormUrlHasHtmlAndIdentifer() {
  1576. $this->Form->request['_Token'] = array('key' => 'testKey');
  1577. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  1578. $res = $this->Form->create($this->article, array(
  1579. 'url' => array(
  1580. 'controller' => 'articles',
  1581. 'action' => 'view',
  1582. '?' => array(
  1583. 'page' => 1,
  1584. 'limit' => 10,
  1585. 'html' => '<>"',
  1586. ),
  1587. '#' => 'result',
  1588. ),
  1589. ));
  1590. $result = $this->Form->secure();
  1591. $this->assertContains($expected, $result);
  1592. $this->Form->create($this->article, array(
  1593. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  1594. ));
  1595. $result = $this->Form->secure();
  1596. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  1597. $this->Form->create($this->article, array(
  1598. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  1599. ));
  1600. $result = $this->Form->secure();
  1601. $this->assertContains($expected, $result, 'URL path + query should work.');
  1602. }
  1603. /**
  1604. * test error message display
  1605. *
  1606. * @return void
  1607. */
  1608. public function testErrorMessageDisplay() {
  1609. $this->article['errors'] = [
  1610. 'Article' => ['title' => 'error message']
  1611. ];
  1612. $this->Form->create($this->article);
  1613. $result = $this->Form->input('Article.title');
  1614. $expected = [
  1615. 'div' => ['class' => 'input text error'],
  1616. 'label' => ['for' => 'article-title'],
  1617. 'Title',
  1618. '/label',
  1619. 'input' => [
  1620. 'type' => 'text', 'name' => 'Article[title]',
  1621. 'id' => 'article-title', 'class' => 'form-error'
  1622. ],
  1623. ['div' => ['class' => 'error-message']],
  1624. 'error message',
  1625. '/div',
  1626. '/div'
  1627. ];
  1628. $this->assertHtml($expected, $result);
  1629. $result = $this->Form->input('Article.title', [
  1630. 'templates' => [
  1631. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  1632. ]
  1633. ]);
  1634. $expected = [
  1635. 'div' => ['class' => 'input text error'],
  1636. 'label' => ['for' => 'article-title'],
  1637. 'Title',
  1638. '/label',
  1639. 'input' => [
  1640. 'type' => 'text', 'name' => 'Article[title]',
  1641. 'id' => 'article-title', 'class' => 'form-error'
  1642. ],
  1643. '/div'
  1644. ];
  1645. $this->assertHtml($expected, $result);
  1646. }
  1647. /**
  1648. * Test validation errors, when validation message is an empty string.
  1649. *
  1650. * @return void
  1651. */
  1652. public function testEmptyErrorValidation() {
  1653. $this->article['errors'] = [
  1654. 'Article' => ['title' => '']
  1655. ];
  1656. $this->Form->create($this->article);
  1657. $result = $this->Form->input('Article.title');
  1658. $expected = [
  1659. 'div' => ['class' => 'input text error'],
  1660. 'label' => ['for' => 'article-title'],
  1661. 'Title',
  1662. '/label',
  1663. 'input' => [
  1664. 'type' => 'text', 'name' => 'Article[title]',
  1665. 'id' => 'article-title', 'class' => 'form-error'
  1666. ],
  1667. ['div' => ['class' => 'error-message']],
  1668. [],
  1669. '/div',
  1670. '/div'
  1671. ];
  1672. $this->assertHtml($expected, $result);
  1673. }
  1674. /**
  1675. * Test validation errors, when calling input() overriding validation message by an empty string.
  1676. *
  1677. * @return void
  1678. */
  1679. public function testEmptyInputErrorValidation() {
  1680. $this->article['errors'] = [
  1681. 'Article' => ['title' => 'error message']
  1682. ];
  1683. $this->Form->create($this->article);
  1684. $result = $this->Form->input('Article.title', array('error' => ''));
  1685. $expected = [
  1686. 'div' => ['class' => 'input text error'],
  1687. 'label' => ['for' => 'article-title'],
  1688. 'Title',
  1689. '/label',
  1690. 'input' => [
  1691. 'type' => 'text', 'name' => 'Article[title]',
  1692. 'id' => 'article-title', 'class' => 'form-error'
  1693. ],
  1694. ['div' => ['class' => 'error-message']],
  1695. [],
  1696. '/div',
  1697. '/div'
  1698. ];
  1699. $this->assertHtml($expected, $result);
  1700. }
  1701. /**
  1702. * Tests displaying errors for nested entities
  1703. *
  1704. * @return void
  1705. */
  1706. public function testFormValidationAssociated() {
  1707. $nested = new Entity(['foo' => 'bar']);
  1708. $nested->errors('foo', ['not a valid bar']);
  1709. $entity = new Entity(['nested' => $nested]);
  1710. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  1711. $result = $this->Form->error('nested.foo');
  1712. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  1713. }
  1714. /**
  1715. * testFormValidationAssociatedSecondLevel method
  1716. *
  1717. * test form error display with associated model.
  1718. *
  1719. * @return void
  1720. */
  1721. public function testFormValidationAssociatedSecondLevel() {
  1722. $inner = new Entity(['bar' => 'baz']);
  1723. $nested = new Entity(['foo' => $inner]);
  1724. $entity = new Entity(['nested' => $nested]);
  1725. $inner->errors('bar', ['not a valid one']);
  1726. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  1727. $result = $this->Form->error('nested.foo.bar');
  1728. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  1729. }
  1730. /**
  1731. * testFormValidationMultiRecord method
  1732. *
  1733. * test form error display with multiple records.
  1734. *
  1735. * @return void
  1736. */
  1737. public function testFormValidationMultiRecord() {
  1738. $one = new Entity;
  1739. $two = new Entity;
  1740. TableRegistry::get('Contacts', [
  1741. 'className' => __NAMESPACE__ . '\ContactsTable'
  1742. ]);
  1743. $one->set('email', '');
  1744. $one->errors('email', ['invalid email']);
  1745. $two->set('name', '');
  1746. $two->errors('name', ['This is wrong']);
  1747. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  1748. $result = $this->Form->input('0.email');
  1749. $expected = array(
  1750. 'div' => array('class' => 'input email error'),
  1751. 'label' => array('for' => '0-email'),
  1752. 'Email',
  1753. '/label',
  1754. 'input' => array(
  1755. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  1756. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  1757. ),
  1758. array('div' => array('class' => 'error-message')),
  1759. 'invalid email',
  1760. '/div',
  1761. '/div'
  1762. );
  1763. $this->assertHtml($expected, $result);
  1764. $result = $this->Form->input('1.name');
  1765. $expected = array(
  1766. 'div' => array('class' => 'input text error'),
  1767. 'label' => array('for' => '1-name'),
  1768. 'Name',
  1769. '/label',
  1770. 'input' => array(
  1771. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  1772. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  1773. ),
  1774. array('div' => array('class' => 'error-message')),
  1775. 'This is wrong',
  1776. '/div',
  1777. '/div'
  1778. );
  1779. $this->assertHtml($expected, $result);
  1780. }
  1781. /**
  1782. * testInput method
  1783. *
  1784. * Test various incarnations of input().
  1785. *
  1786. * @return void
  1787. */
  1788. public function testInput() {
  1789. TableRegistry::get('ValidateUsers', [
  1790. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  1791. ]);
  1792. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  1793. $result = $this->Form->input('ValidateUsers.balance');
  1794. $expected = array(
  1795. 'div' => array('class'),
  1796. 'label' => array('for'),
  1797. 'Balance',
  1798. '/label',
  1799. 'input' => array('name', 'type' => 'number', 'id', 'step'),
  1800. '/div',
  1801. );
  1802. $this->assertHtml($expected, $result);
  1803. $result = $this->Form->input('ValidateUser.cost_decimal');
  1804. $expected = array(
  1805. 'div' => array('class'),
  1806. 'label' => array('for'),
  1807. 'Cost Decimal',
  1808. '/label',
  1809. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  1810. '/div',
  1811. );
  1812. $this->assertHtml($expected, $result);
  1813. }
  1814. /**
  1815. * Tests the input method and passing custom options
  1816. *
  1817. * @return void
  1818. */
  1819. public function testInputCustomization() {
  1820. TableRegistry::get('Contacts', [
  1821. 'className' => __NAMESPACE__ . '\ContactsTable'
  1822. ]);
  1823. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1824. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1825. $expected = array(
  1826. 'div' => array('class' => 'input email'),
  1827. 'label' => array('for' => 'custom'),
  1828. 'Email',
  1829. '/label',
  1830. array('input' => array(
  1831. 'type' => 'email', 'name' => 'Contact[email]',
  1832. 'id' => 'custom', 'maxlength' => 255
  1833. )),
  1834. '/div'
  1835. );
  1836. $this->assertHtml($expected, $result);
  1837. $result = $this->Form->input('Contact.email', array(
  1838. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  1839. ));
  1840. $expected = array(
  1841. '<div',
  1842. 'label' => array('for' => 'contact-email'),
  1843. 'Email',
  1844. '/label',
  1845. array('input' => array(
  1846. 'type' => 'email', 'name' => 'Contact[email]',
  1847. 'id' => 'contact-email', 'maxlength' => 255
  1848. )),
  1849. '/div'
  1850. );
  1851. $this->assertHtml($expected, $result);
  1852. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1853. $expected = array(
  1854. 'div' => array('class' => 'input text'),
  1855. 'label' => array('for' => 'contact-email'),
  1856. 'Email',
  1857. '/label',
  1858. array('input' => array(
  1859. 'type' => 'text', 'name' => 'Contact[email]',
  1860. 'id' => 'contact-email', 'maxlength' => '255'
  1861. )),
  1862. '/div'
  1863. );
  1864. $this->assertHtml($expected, $result);
  1865. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1866. $expected = array(
  1867. 'div' => array('class' => 'input text'),
  1868. 'label' => array('for' => 'contact-5-email'),
  1869. 'Email',
  1870. '/label',
  1871. array('input' => array(
  1872. 'type' => 'text', 'name' => 'Contact[5][email]',
  1873. 'id' => 'contact-5-email', 'maxlength' => '255'
  1874. )),
  1875. '/div'
  1876. );
  1877. $this->assertHtml($expected, $result);
  1878. $result = $this->Form->input('Contact.password');
  1879. $expected = array(
  1880. 'div' => array('class' => 'input password'),
  1881. 'label' => array('for' => 'contact-password'),
  1882. 'Password',
  1883. '/label',
  1884. array('input' => array(
  1885. 'type' => 'password', 'name' => 'Contact[password]',
  1886. 'id' => 'contact-password'
  1887. )),
  1888. '/div'
  1889. );
  1890. $this->assertHtml($expected, $result);
  1891. $result = $this->Form->input('Contact.email', array(
  1892. 'type' => 'file', 'class' => 'textbox'
  1893. ));
  1894. $expected = array(
  1895. 'div' => array('class' => 'input file'),
  1896. 'label' => array('for' => 'contact-email'),
  1897. 'Email',
  1898. '/label',
  1899. array('input' => array(
  1900. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  1901. 'id' => 'contact-email'
  1902. )),
  1903. '/div'
  1904. );
  1905. $this->assertHtml($expected, $result);
  1906. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  1907. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  1908. $result = $this->Form->input('phone');
  1909. $expected = array(
  1910. 'div' => array('class' => 'input tel'),
  1911. 'label' => array('for' => 'phone'),
  1912. 'Phone',
  1913. '/label',
  1914. array('input' => array(
  1915. 'type' => 'tel', 'name' => 'phone',
  1916. 'value' => 'Hello &amp; World &gt; weird chars',
  1917. 'id' => 'phone', 'maxlength' => 255
  1918. )),
  1919. '/div'
  1920. );
  1921. $this->assertHtml($expected, $result);
  1922. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1923. $this->Form->create();
  1924. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1925. $expected = array(
  1926. 'div' => array('class' => 'input text'),
  1927. 'label' => array('for' => 'myId'),
  1928. 'Field',
  1929. '/label',
  1930. 'input' => array(
  1931. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  1932. 'value' => 'My value', 'id' => 'myId'
  1933. ),
  1934. '/div'
  1935. );
  1936. $this->assertHtml($expected, $result);
  1937. $this->Form->request->data = [];
  1938. $entity->errors('field', 'Badness!');
  1939. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  1940. $result = $this->Form->input('field');
  1941. $expected = array(
  1942. 'div' => array('class' => 'input text error'),
  1943. 'label' => array('for' => 'field'),
  1944. 'Field',
  1945. '/label',
  1946. 'input' => array(
  1947. 'type' => 'text', 'name' => 'field',
  1948. 'id' => 'field', 'class' => 'form-error'
  1949. ),
  1950. array('div' => array('class' => 'error-message')),
  1951. 'Badness!',
  1952. '/div',
  1953. '/div'
  1954. );
  1955. $this->assertHtml($expected, $result);
  1956. $result = $this->Form->input('field', array(
  1957. 'templates' => [
  1958. 'inputContainerError' => '{{content}}{{error}}',
  1959. 'error' => '<span class="error-message">{{content}}</span>'
  1960. ]
  1961. ));
  1962. $expected = array(
  1963. 'label' => array('for' => 'field'),
  1964. 'Field',
  1965. '/label',
  1966. 'input' => array(
  1967. 'type' => 'text', 'name' => 'field',
  1968. 'id' => 'field', 'class' => 'form-error'
  1969. ),
  1970. array('span' => array('class' => 'error-message')),
  1971. 'Badness!',
  1972. '/span'
  1973. );
  1974. $this->assertHtml($expected, $result);
  1975. $entity->errors('field', ['minLength']);
  1976. $result = $this->Form->input('field', array(
  1977. 'error' => array(
  1978. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1979. 'maxLength' => 'login too large'
  1980. )
  1981. ));
  1982. $expected = array(
  1983. 'div' => array('class' => 'input text error'),
  1984. 'label' => array('for' => 'field'),
  1985. 'Field',
  1986. '/label',
  1987. 'input' => array('type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'),
  1988. array('div' => array('class' => 'error-message')),
  1989. 'Le login doit contenir au moins 2 caractères',
  1990. '/div',
  1991. '/div'
  1992. );
  1993. $this->assertHtml($expected, $result);
  1994. $entity->errors('field', ['maxLength']);
  1995. $result = $this->Form->input('field', array(
  1996. 'error' => array(
  1997. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1998. 'maxLength' => 'login too large',
  1999. )
  2000. ));
  2001. $expected = array(
  2002. 'div' => array('class' => 'input text error'),
  2003. 'label' => array('for' => 'field'),
  2004. 'Field',
  2005. '/label',
  2006. 'input' => array('type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'),
  2007. array('div' => array('class' => 'error-message')),
  2008. 'login too large',
  2009. '/div',
  2010. '/div'
  2011. );
  2012. $this->assertHtml($expected, $result);
  2013. }
  2014. /**
  2015. * Test that input() accepts a template file.
  2016. *
  2017. * @return void
  2018. */
  2019. public function testInputWithTemplateFile() {
  2020. $result = $this->Form->input('field', array(
  2021. 'templates' => 'htmlhelper_tags'
  2022. ));
  2023. $expected = array(
  2024. 'label' => array('for' => 'field'),
  2025. 'Field',
  2026. '/label',
  2027. 'input' => array(
  2028. 'type' => 'text', 'name' => 'field',
  2029. 'id' => 'field'
  2030. ),
  2031. );
  2032. $this->assertHtml($expected, $result);
  2033. }
  2034. /**
  2035. * Test id prefix
  2036. *
  2037. * @return void
  2038. */
  2039. public function testCreateIdPrefix() {
  2040. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2041. $result = $this->Form->input('field');
  2042. $expected = [
  2043. 'div' => ['class' => 'input text'],
  2044. 'label' => ['for' => 'prefix-field'],
  2045. 'Field',
  2046. '/label',
  2047. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2048. '/div'
  2049. ];
  2050. $this->assertHtml($expected, $result);
  2051. $result = $this->Form->input('field', ['id' => 'custom-id']);
  2052. $expected = [
  2053. 'div' => ['class' => 'input text'],
  2054. 'label' => ['for' => 'custom-id'],
  2055. 'Field',
  2056. '/label',
  2057. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2058. '/div'
  2059. ];
  2060. $this->assertHtml($expected, $result);
  2061. $result = $this->Form->radio('Model.field', ['option A']);
  2062. $expected = [
  2063. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2064. 'label' => ['for' => 'prefix-model-field-0'],
  2065. ['input' => [
  2066. 'type' => 'radio',
  2067. 'name' => 'Model[field]',
  2068. 'value' => '0',
  2069. 'id' => 'prefix-model-field-0'
  2070. ]],
  2071. 'option A',
  2072. '/label'
  2073. ];
  2074. $this->assertHtml($expected, $result);
  2075. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2076. $expected = [
  2077. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2078. 'label' => ['for' => 'prefix-model-field-0'],
  2079. ['input' => [
  2080. 'type' => 'radio',
  2081. 'name' => 'Model[field]',
  2082. 'value' => '0',
  2083. 'id' => 'prefix-model-field-0'
  2084. ]],
  2085. 'option A',
  2086. '/label'
  2087. ];
  2088. $this->assertHtml($expected, $result);
  2089. $result = $this->Form->select(
  2090. 'Model.multi_field',
  2091. ['first'],
  2092. ['multiple' => 'checkbox']
  2093. );
  2094. $expected = [
  2095. 'input' => [
  2096. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2097. ],
  2098. ['div' => ['class' => 'checkbox']],
  2099. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2100. ['input' => [
  2101. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2102. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2103. ]],
  2104. 'first',
  2105. '/label',
  2106. '/div',
  2107. ];
  2108. $this->assertHtml($expected, $result);
  2109. $this->Form->end();
  2110. $result = $this->Form->input('field');
  2111. $expected = [
  2112. 'div' => ['class' => 'input text'],
  2113. 'label' => ['for' => 'field'],
  2114. 'Field',
  2115. '/label',
  2116. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2117. '/div'
  2118. ];
  2119. $this->assertHtml($expected, $result);
  2120. }
  2121. /**
  2122. * Test that inputs with 0 can be created.
  2123. *
  2124. * @return void
  2125. */
  2126. public function testInputZero() {
  2127. TableRegistry::get('Contacts', [
  2128. 'className' => __NAMESPACE__ . '\ContactsTable'
  2129. ]);
  2130. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2131. $result = $this->Form->input('0');
  2132. $expected = array(
  2133. 'div' => array('class' => 'input text'),
  2134. 'label' => array('for' => '0'), '/label',
  2135. 'input' => array('type' => 'text', 'name' => '0', 'id' => '0'),
  2136. '/div'
  2137. );
  2138. $this->assertHtml($expected, $result);
  2139. }
  2140. /**
  2141. * test input() with checkbox creation
  2142. *
  2143. * @return void
  2144. */
  2145. public function testInputCheckbox() {
  2146. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  2147. $expected = array(
  2148. 'div' => array('class' => 'input checkbox'),
  2149. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2150. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2151. '/div'
  2152. );
  2153. $this->assertHtml($expected, $result);
  2154. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2155. $expected = array(
  2156. 'div' => array('class' => 'input checkbox'),
  2157. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2158. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2159. '/div'
  2160. );
  2161. $this->assertHtml($expected, $result);
  2162. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2163. $expected = array(
  2164. 'div' => array('class' => 'input checkbox'),
  2165. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2166. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2167. '/div'
  2168. );
  2169. $this->assertHtml($expected, $result);
  2170. $result = $this->Form->input('User.disabled', array(
  2171. 'label' => 'Disabled',
  2172. 'type' => 'checkbox',
  2173. 'data-foo' => 'disabled'
  2174. ));
  2175. $expected = array(
  2176. 'div' => array('class' => 'input checkbox'),
  2177. 'label' => array('for' => 'user-disabled'),
  2178. 'input' => array('type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'),
  2179. array('input' => array(
  2180. 'type' => 'checkbox',
  2181. 'name' => 'User[disabled]',
  2182. 'value' => '1',
  2183. 'id' => 'user-disabled',
  2184. 'data-foo' => 'disabled'
  2185. )),
  2186. 'Disabled',
  2187. '/label',
  2188. '/div'
  2189. );
  2190. $this->assertHtml($expected, $result);
  2191. }
  2192. /**
  2193. * Test that input() does not create wrapping div and label tag for hidden fields
  2194. *
  2195. * @return void
  2196. */
  2197. public function testInputHidden() {
  2198. TableRegistry::get('ValidateUsers', [
  2199. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2200. ]);
  2201. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2202. $result = $this->Form->input('ValidateUser.id');
  2203. $expected = array(
  2204. 'input' => array('name', 'type' => 'hidden', 'id')
  2205. );
  2206. $this->assertHtml($expected, $result);
  2207. $result = $this->Form->input('ValidateUser.custom', ['type' => 'hidden']);
  2208. $expected = array(
  2209. 'input' => array('name', 'type' => 'hidden', 'id')
  2210. );
  2211. $this->assertHtml($expected, $result);
  2212. }
  2213. /**
  2214. * test form->input() with datetime
  2215. *
  2216. * @return void
  2217. */
  2218. public function testInputDatetime() {
  2219. $this->Form = $this->getMock(
  2220. 'Cake\View\Helper\FormHelper',
  2221. ['datetime'],
  2222. [new View()]
  2223. );
  2224. $this->Form->expects($this->once())->method('datetime')
  2225. ->with('prueba', [
  2226. 'type' => 'datetime',
  2227. 'timeFormat' => 24,
  2228. 'minYear' => 2008,
  2229. 'maxYear' => 2011,
  2230. 'interval' => 15,
  2231. 'options' => null,
  2232. 'empty' => false,
  2233. 'id' => 'prueba',
  2234. 'required' => false,
  2235. ])
  2236. ->will($this->returnValue('This is it!'));
  2237. $result = $this->Form->input('prueba', array(
  2238. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2239. 'maxYear' => 2011, 'interval' => 15
  2240. ));
  2241. $expected = array(
  2242. 'div' => array('class' => 'input datetime'),
  2243. '<label',
  2244. 'Prueba',
  2245. '/label',
  2246. 'This is it!',
  2247. '/div'
  2248. );
  2249. $this->assertHtml($expected, $result);
  2250. }
  2251. /**
  2252. * test form->input() with datetime with id prefix
  2253. *
  2254. * @return void
  2255. */
  2256. public function testInputDatetimeIdPrefix() {
  2257. $this->Form = $this->getMock(
  2258. 'Cake\View\Helper\FormHelper',
  2259. ['datetime'],
  2260. [new View()]
  2261. );
  2262. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2263. $this->Form->expects($this->once())->method('datetime')
  2264. ->with('prueba', [
  2265. 'type' => 'datetime',
  2266. 'timeFormat' => 24,
  2267. 'minYear' => 2008,
  2268. 'maxYear' => 2011,
  2269. 'interval' => 15,
  2270. 'options' => null,
  2271. 'empty' => false,
  2272. 'id' => 'prefix-prueba',
  2273. 'required' => false,
  2274. ])
  2275. ->will($this->returnValue('This is it!'));
  2276. $result = $this->Form->input('prueba', array(
  2277. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2278. 'maxYear' => 2011, 'interval' => 15
  2279. ));
  2280. $expected = array(
  2281. 'div' => array('class' => 'input datetime'),
  2282. '<label',
  2283. 'Prueba',
  2284. '/label',
  2285. 'This is it!',
  2286. '/div'
  2287. );
  2288. $this->assertHtml($expected, $result);
  2289. }
  2290. /**
  2291. * Test generating checkboxes with disabled elements.
  2292. *
  2293. * @return void
  2294. */
  2295. public function testInputCheckboxWithDisabledElements() {
  2296. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2297. $result = $this->Form->input('Contact.multiple', array(
  2298. 'multiple' => 'checkbox',
  2299. 'disabled' => 'disabled',
  2300. 'options' => $options
  2301. ));
  2302. $expected = array(
  2303. array('div' => array('class' => 'input select')),
  2304. array('label' => array('for' => "contact-multiple")),
  2305. 'Multiple',
  2306. '/label',
  2307. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'disabled' => 'disabled', 'value' => '')),
  2308. array('div' => array('class' => 'checkbox')),
  2309. array('label' => array('for' => "contact-multiple-1")),
  2310. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1")),
  2311. 'One',
  2312. '/label',
  2313. '/div',
  2314. array('div' => array('class' => 'checkbox')),
  2315. array('label' => array('for' => "contact-multiple-2")),
  2316. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2")),
  2317. 'Two',
  2318. '/label',
  2319. '/div',
  2320. array('div' => array('class' => 'checkbox')),
  2321. array('label' => array('for' => "contact-multiple-3")),
  2322. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3")),
  2323. 'Three',
  2324. '/label',
  2325. '/div',
  2326. '/div'
  2327. );
  2328. $this->assertHtml($expected, $result);
  2329. // make sure 50 does only disable 50, and not 50f5c0cf
  2330. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2331. $disabled = array(50);
  2332. $expected = array(
  2333. array('div' => array('class' => 'input select')),
  2334. array('label' => array('for' => "contact-multiple")),
  2335. 'Multiple',
  2336. '/label',
  2337. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '')),
  2338. array('div' => array('class' => 'checkbox')),
  2339. array('label' => array('for' => "contact-multiple-50")),
  2340. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50")),
  2341. 'Fifty',
  2342. '/label',
  2343. '/div',
  2344. array('div' => array('class' => 'checkbox')),
  2345. array('label' => array('for' => "contact-multiple-50f5c0cf")),
  2346. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf")),
  2347. 'Stringy',
  2348. '/label',
  2349. '/div',
  2350. '/div'
  2351. );
  2352. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2353. $this->assertHtml($expected, $result);
  2354. }
  2355. /**
  2356. * test input name with leading integer, ensure attributes are generated correctly.
  2357. *
  2358. * @return void
  2359. */
  2360. public function testInputWithLeadingInteger() {
  2361. $result = $this->Form->text('0.Node.title');
  2362. $expected = array(
  2363. 'input' => array('name' => '0[Node][title]', 'type' => 'text')
  2364. );
  2365. $this->assertHtml($expected, $result);
  2366. }
  2367. /**
  2368. * test form->input() with select type inputs.
  2369. *
  2370. * @return void
  2371. */
  2372. public function testInputSelectType() {
  2373. $result = $this->Form->input('email', array(
  2374. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2375. );
  2376. $expected = array(
  2377. 'div' => array('class' => 'input select'),
  2378. 'label' => array('for' => 'email'),
  2379. 'Email',
  2380. '/label',
  2381. array('select' => array('name' => 'email', 'id' => 'email')),
  2382. array('option' => array('value' => '')),
  2383. '/option',
  2384. array('option' => array('value' => 'è')),
  2385. 'Firést',
  2386. '/option',
  2387. array('option' => array('value' => 'é')),
  2388. 'Secoènd',
  2389. '/option',
  2390. '/select',
  2391. '/div'
  2392. );
  2393. $this->assertHtml($expected, $result);
  2394. $result = $this->Form->input('email', array(
  2395. 'options' => array('First', 'Second'), 'empty' => true)
  2396. );
  2397. $expected = array(
  2398. 'div' => array('class' => 'input select'),
  2399. 'label' => array('for' => 'email'),
  2400. 'Email',
  2401. '/label',
  2402. array('select' => array('name' => 'email', 'id' => 'email')),
  2403. array('option' => array('value' => '')),
  2404. '/option',
  2405. array('option' => array('value' => '0')),
  2406. 'First',
  2407. '/option',
  2408. array('option' => array('value' => '1')),
  2409. 'Second',
  2410. '/option',
  2411. '/select',
  2412. '/div'
  2413. );
  2414. $this->assertHtml($expected, $result);
  2415. $result = $this->Form->input('email', [
  2416. 'type' => 'select',
  2417. 'options' => new \ArrayObject(['First', 'Second']),
  2418. 'empty' => true
  2419. ]);
  2420. $this->assertHtml($expected, $result);
  2421. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2422. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2423. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2424. $expected = array(
  2425. 'div' => array('class' => 'input select'),
  2426. 'label' => array('for' => 'model-user-id'),
  2427. 'User',
  2428. '/label',
  2429. 'select' => array('name' => 'Model[user_id]', 'id' => 'model-user-id'),
  2430. array('option' => array('value' => '')),
  2431. '/option',
  2432. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2433. 'good',
  2434. '/option',
  2435. array('option' => array('value' => 'other')),
  2436. 'bad',
  2437. '/option',
  2438. '/select',
  2439. '/div'
  2440. );
  2441. $this->assertHtml($expected, $result);
  2442. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2443. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2444. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2445. $expected = array(
  2446. 'div' => array('class' => 'input select'),
  2447. 'label' => array('for' => 'thing-user-id'),
  2448. 'User',
  2449. '/label',
  2450. 'select' => array('name' => 'Thing[user_id]', 'id' => 'thing-user-id'),
  2451. array('option' => array('value' => '')),
  2452. 'Some Empty',
  2453. '/option',
  2454. array('option' => array('value' => 'value')),
  2455. 'good',
  2456. '/option',
  2457. array('option' => array('value' => 'other')),
  2458. 'bad',
  2459. '/option',
  2460. '/select',
  2461. '/div'
  2462. );
  2463. $this->assertHtml($expected, $result);
  2464. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2465. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2466. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2467. $expected = array(
  2468. 'div' => array('class' => 'input select'),
  2469. 'label' => array('for' => 'thing-user-id'),
  2470. 'User',
  2471. '/label',
  2472. 'select' => array('name' => 'Thing[user_id]', 'id' => 'thing-user-id'),
  2473. array('option' => array('value' => '')),
  2474. 'Some Empty',
  2475. '/option',
  2476. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2477. 'good',
  2478. '/option',
  2479. array('option' => array('value' => 'other')),
  2480. 'bad',
  2481. '/option',
  2482. '/select',
  2483. '/div'
  2484. );
  2485. $this->assertHtml($expected, $result);
  2486. $this->Form->data = array();
  2487. $result = $this->Form->input('Publisher.id', array(
  2488. 'label' => 'Publisher',
  2489. 'type' => 'select',
  2490. 'multiple' => 'checkbox',
  2491. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2492. ));
  2493. $expected = array(
  2494. array('div' => array('class' => 'input select')),
  2495. array('label' => array('for' => 'publisher-id')),
  2496. 'Publisher',
  2497. '/label',
  2498. 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''),
  2499. array('div' => array('class' => 'checkbox')),
  2500. array('label' => array('for' => 'publisher-id-value-1')),
  2501. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1')),
  2502. 'Label 1',
  2503. '/label',
  2504. '/div',
  2505. array('div' => array('class' => 'checkbox')),
  2506. array('label' => array('for' => 'publisher-id-value-2')),
  2507. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2')),
  2508. 'Label 2',
  2509. '/label',
  2510. '/div',
  2511. '/div'
  2512. );
  2513. $this->assertHtml($expected, $result);
  2514. }
  2515. /**
  2516. * test that input() and a non standard primary key makes a hidden input by default.
  2517. *
  2518. * @return void
  2519. */
  2520. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2521. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  2522. $this->Form->create($this->article);
  2523. $result = $this->Form->input('title');
  2524. $expected = array(
  2525. 'input' => array('type' => 'hidden', 'name' => 'title', 'id' => 'title'),
  2526. );
  2527. $this->assertHtml($expected, $result);
  2528. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  2529. $this->Form->create($this->article);
  2530. $result = $this->Form->input('title');
  2531. $expected = array(
  2532. 'input' => array('type' => 'hidden', 'name' => 'title', 'id' => 'title'),
  2533. );
  2534. $this->assertHtml($expected, $result);
  2535. $result = $this->Form->input('body');
  2536. $expected = array(
  2537. 'input' => array('type' => 'hidden', 'name' => 'body', 'id' => 'body'),
  2538. );
  2539. $this->assertHtml($expected, $result);
  2540. }
  2541. /**
  2542. * test that overriding the magic select type widget is possible
  2543. *
  2544. * @return void
  2545. */
  2546. public function testInputOverridingMagicSelectType() {
  2547. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2548. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2549. $expected = array(
  2550. 'div' => array('class' => 'input text'),
  2551. 'label' => array('for' => 'model-user-id'), 'User', '/label',
  2552. 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'),
  2553. '/div'
  2554. );
  2555. $this->assertHtml($expected, $result);
  2556. //Check that magic types still work for plural/singular vars
  2557. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2558. $result = $this->Form->input('Model.type');
  2559. $expected = array(
  2560. 'div' => array('class' => 'input select'),
  2561. 'label' => array('for' => 'model-type'), 'Type', '/label',
  2562. 'select' => array('name' => 'Model[type]', 'id' => 'model-type'),
  2563. array('option' => array('value' => 'value')), 'good', '/option',
  2564. array('option' => array('value' => 'other')), 'bad', '/option',
  2565. '/select',
  2566. '/div'
  2567. );
  2568. $this->assertHtml($expected, $result);
  2569. }
  2570. /**
  2571. * Test that inferred types do not override developer input
  2572. *
  2573. * @return void
  2574. */
  2575. public function testInputMagicTypeDoesNotOverride() {
  2576. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2577. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2578. $expected = array(
  2579. 'div' => array('class' => 'input checkbox'),
  2580. 'label' => array('for' => 'model-user'),
  2581. array('input' => array(
  2582. 'type' => 'hidden',
  2583. 'name' => 'Model[user]',
  2584. 'value' => 0,
  2585. )),
  2586. array('input' => array(
  2587. 'name' => 'Model[user]',
  2588. 'type' => 'checkbox',
  2589. 'id' => 'model-user',
  2590. 'value' => 1
  2591. )),
  2592. 'User',
  2593. '/label',
  2594. '/div'
  2595. );
  2596. $this->assertHtml($expected, $result);
  2597. }
  2598. /**
  2599. * Test that magic input() selects are created for type=number
  2600. *
  2601. * @return void
  2602. */
  2603. public function testInputMagicSelectForTypeNumber() {
  2604. TableRegistry::get('ValidateUsers', [
  2605. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2606. ]);
  2607. $entity = new Entity(['balance' => 1]);
  2608. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  2609. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2610. $result = $this->Form->input('balance');
  2611. $expected = array(
  2612. 'div' => array('class' => 'input select'),
  2613. 'label' => array('for' => 'balance'),
  2614. 'Balance',
  2615. '/label',
  2616. 'select' => array('name' => 'balance', 'id' => 'balance'),
  2617. array('option' => array('value' => '0')),
  2618. 'nothing',
  2619. '/option',
  2620. array('option' => array('value' => '1', 'selected' => 'selected')),
  2621. 'some',
  2622. '/option',
  2623. array('option' => array('value' => '100')),
  2624. 'a lot',
  2625. '/option',
  2626. '/select',
  2627. '/div'
  2628. );
  2629. $this->assertHtml($expected, $result);
  2630. }
  2631. /**
  2632. * Test that magic input() selects can easily be converted into radio types without error.
  2633. *
  2634. * @return void
  2635. */
  2636. public function testInputMagicSelectChangeToRadio() {
  2637. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2638. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2639. $this->assertContains('input type="radio"', $result);
  2640. }
  2641. /**
  2642. * testFormInputs method
  2643. *
  2644. * test correct results from form::inputs().
  2645. *
  2646. * @return void
  2647. */
  2648. public function testFormInputsLegendFieldset() {
  2649. $this->Form->create($this->article);
  2650. $result = $this->Form->allInputs([], array('legend' => 'The Legend'));
  2651. $expected = array(
  2652. '<fieldset',
  2653. '<legend',
  2654. 'The Legend',
  2655. '/legend',
  2656. '*/fieldset',
  2657. );
  2658. $this->assertHtml($expected, $result);
  2659. $result = $this->Form->allInputs([], array('fieldset' => true, 'legend' => 'Field of Dreams'));
  2660. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  2661. $this->assertContains('<fieldset>', $result);
  2662. $result = $this->Form->allInputs([], array('fieldset' => false, 'legend' => false));
  2663. $this->assertNotContains('<legend>', $result);
  2664. $this->assertNotContains('<fieldset>', $result);
  2665. $result = $this->Form->allInputs([], array('fieldset' => false, 'legend' => 'Hello'));
  2666. $this->assertNotContains('<legend>', $result);
  2667. $this->assertNotContains('<fieldset>', $result);
  2668. $this->Form->create($this->article);
  2669. $this->Form->request->params['prefix'] = 'admin';
  2670. $this->Form->request->params['action'] = 'admin_edit';
  2671. $this->Form->request->params['controller'] = 'articles';
  2672. $result = $this->Form->allInputs();
  2673. $expected = [
  2674. '<fieldset',
  2675. '<legend',
  2676. 'New Article',
  2677. '/legend',
  2678. '*/fieldset',
  2679. ];
  2680. $this->assertHtml($expected, $result);
  2681. }
  2682. /**
  2683. * Test the inputs() method.
  2684. *
  2685. * @return void
  2686. */
  2687. public function testFormInputs() {
  2688. $this->Form->create($this->article);
  2689. $result = $this->Form->allInputs();
  2690. $expected = array(
  2691. '<fieldset',
  2692. '<legend', 'New Article', '/legend',
  2693. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2694. array('div' => array('class' => 'input select required')),
  2695. '*/div',
  2696. array('div' => array('class' => 'input text required')),
  2697. '*/div',
  2698. array('div' => array('class' => 'input text')),
  2699. '*/div',
  2700. array('div' => array('class' => 'input text')),
  2701. '*/div',
  2702. '/fieldset',
  2703. );
  2704. $this->assertHtml($expected, $result);
  2705. $result = $this->Form->allInputs([
  2706. 'published' => ['type' => 'boolean']
  2707. ]);
  2708. $expected = array(
  2709. '<fieldset',
  2710. '<legend', 'New Article', '/legend',
  2711. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2712. array('div' => array('class' => 'input select required')),
  2713. '*/div',
  2714. array('div' => array('class' => 'input text required')),
  2715. '*/div',
  2716. array('div' => array('class' => 'input text')),
  2717. '*/div',
  2718. array('div' => array('class' => 'input boolean')),
  2719. '*/div',
  2720. '/fieldset',
  2721. );
  2722. $this->assertHtml($expected, $result);
  2723. $this->Form->create($this->article);
  2724. $result = $this->Form->allInputs([], ['legend' => 'Hello']);
  2725. $expected = array(
  2726. 'fieldset' => array(),
  2727. 'legend' => array(),
  2728. 'Hello',
  2729. '/legend',
  2730. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2731. array('div' => array('class' => 'input select required')),
  2732. '*/div',
  2733. array('div' => array('class' => 'input text required')),
  2734. '*/div',
  2735. array('div' => array('class' => 'input text')),
  2736. '*/div',
  2737. array('div' => array('class' => 'input text')),
  2738. '*/div',
  2739. '/fieldset'
  2740. );
  2741. $this->assertHtml($expected, $result);
  2742. $this->Form->create(false);
  2743. $expected = array(
  2744. 'fieldset' => array(),
  2745. array('div' => array('class' => 'input text')),
  2746. 'label' => array('for' => 'foo'),
  2747. 'Foo',
  2748. '/label',
  2749. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  2750. '*/div',
  2751. '/fieldset'
  2752. );
  2753. $result = $this->Form->allInputs(
  2754. array('foo' => array('type' => 'text')),
  2755. array('legend' => false)
  2756. );
  2757. $this->assertHtml($expected, $result);
  2758. }
  2759. /**
  2760. * testFormInputsBlacklist
  2761. *
  2762. * @return void
  2763. */
  2764. public function testFormInputsBlacklist() {
  2765. $this->Form->create($this->article);
  2766. $result = $this->Form->allInputs([
  2767. 'id' => false
  2768. ]);
  2769. $expected = array(
  2770. '<fieldset',
  2771. '<legend', 'New Article', '/legend',
  2772. array('div' => array('class' => 'input select required')),
  2773. '*/div',
  2774. array('div' => array('class' => 'input text required')),
  2775. '*/div',
  2776. array('div' => array('class' => 'input text')),
  2777. '*/div',
  2778. array('div' => array('class' => 'input text')),
  2779. '*/div',
  2780. '/fieldset',
  2781. );
  2782. $this->assertHtml($expected, $result);
  2783. $this->Form->create($this->article);
  2784. $result = $this->Form->allInputs([
  2785. 'id' => []
  2786. ]);
  2787. $expected = array(
  2788. '<fieldset',
  2789. '<legend', 'New Article', '/legend',
  2790. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2791. array('div' => array('class' => 'input select required')),
  2792. '*/div',
  2793. array('div' => array('class' => 'input text required')),
  2794. '*/div',
  2795. array('div' => array('class' => 'input text')),
  2796. '*/div',
  2797. array('div' => array('class' => 'input text')),
  2798. '*/div',
  2799. '/fieldset',
  2800. );
  2801. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  2802. }
  2803. /**
  2804. * testSelectAsCheckbox method
  2805. *
  2806. * test multi-select widget with checkbox formatting.
  2807. *
  2808. * @return void
  2809. */
  2810. public function testSelectAsCheckbox() {
  2811. $result = $this->Form->select(
  2812. 'Model.multi_field',
  2813. array('first', 'second', 'third'),
  2814. array('multiple' => 'checkbox', 'value' => array(0, 1))
  2815. );
  2816. $expected = array(
  2817. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  2818. array('div' => array('class' => 'checkbox')),
  2819. array('label' => array('for' => 'model-multi-field-0', 'class' => 'selected')),
  2820. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0')),
  2821. 'first',
  2822. '/label',
  2823. '/div',
  2824. array('div' => array('class' => 'checkbox')),
  2825. array('label' => array('for' => 'model-multi-field-1', 'class' => 'selected')),
  2826. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1')),
  2827. 'second',
  2828. '/label',
  2829. '/div',
  2830. array('div' => array('class' => 'checkbox')),
  2831. array('label' => array('for' => 'model-multi-field-2')),
  2832. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  2833. 'third',
  2834. '/label',
  2835. '/div',
  2836. );
  2837. $this->assertHtml($expected, $result);
  2838. $result = $this->Form->select(
  2839. 'Model.multi_field',
  2840. array('1/2' => 'half'),
  2841. array('multiple' => 'checkbox')
  2842. );
  2843. $expected = array(
  2844. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  2845. array('div' => array('class' => 'checkbox')),
  2846. array('label' => array('for' => 'model-multi-field-1-2')),
  2847. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2')),
  2848. 'half',
  2849. '/label',
  2850. '/div',
  2851. );
  2852. $this->assertHtml($expected, $result);
  2853. }
  2854. /**
  2855. * testLabel method
  2856. *
  2857. * test label generation.
  2858. *
  2859. * @return void
  2860. */
  2861. public function testLabel() {
  2862. $result = $this->Form->label('Person.name');
  2863. $expected = array('label' => array('for' => 'person-name'), 'Name', '/label');
  2864. $this->assertHtml($expected, $result);
  2865. $result = $this->Form->label('Person.name');
  2866. $expected = array('label' => array('for' => 'person-name'), 'Name', '/label');
  2867. $this->assertHtml($expected, $result);
  2868. $result = $this->Form->label('Person.first_name');
  2869. $expected = array('label' => array('for' => 'person-first-name'), 'First Name', '/label');
  2870. $this->assertHtml($expected, $result);
  2871. $result = $this->Form->label('Person.first_name', 'Your first name');
  2872. $expected = array('label' => array('for' => 'person-first-name'), 'Your first name', '/label');
  2873. $this->assertHtml($expected, $result);
  2874. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2875. $expected = array('label' => array('for' => 'person-first-name', 'class' => 'my-class'), 'Your first name', '/label');
  2876. $this->assertHtml($expected, $result);
  2877. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2878. $expected = array('label' => array('for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label');
  2879. $this->assertHtml($expected, $result);
  2880. $result = $this->Form->label('Person.first_name', '');
  2881. $expected = array('label' => array('for' => 'person-first-name'), '/label');
  2882. $this->assertHtml($expected, $result);
  2883. $result = $this->Form->label('Person.2.name', '');
  2884. $expected = array('label' => array('for' => 'person-2-name'), '/label');
  2885. $this->assertHtml($expected, $result);
  2886. }
  2887. /**
  2888. * Test that label() can accept an input with the correct template vars.
  2889. *
  2890. * @return void
  2891. */
  2892. public function testLabelContainInput() {
  2893. $this->Form->templates([
  2894. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  2895. ]);
  2896. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  2897. 'input' => '<input type="checkbox" name="accept_tos" >'
  2898. ]);
  2899. $expected = [
  2900. 'label' => ['for' => 'person-accept-terms'],
  2901. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  2902. 'Accept',
  2903. '/label',
  2904. ];
  2905. $this->assertHtml($expected, $result);
  2906. }
  2907. /**
  2908. * testTextbox method
  2909. *
  2910. * test textbox element generation
  2911. *
  2912. * @return void
  2913. */
  2914. public function testTextbox() {
  2915. $result = $this->Form->text('Model.field');
  2916. $expected = array('input' => array('type' => 'text', 'name' => 'Model[field]'));
  2917. $this->assertHtml($expected, $result);
  2918. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2919. $expected = array('input' => array('type' => 'password', 'name' => 'Model[field]'));
  2920. $this->assertHtml($expected, $result);
  2921. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2922. $expected = array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID'));
  2923. $this->assertHtml($expected, $result);
  2924. }
  2925. /**
  2926. * Test that text() hooks up with request data and error fields.
  2927. *
  2928. * @return void
  2929. */
  2930. public function testTextBoxDataAndError() {
  2931. $this->article['errors'] = [
  2932. 'Contact' => ['text' => 'wrong']
  2933. ];
  2934. $this->Form->create($this->article);
  2935. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2936. $result = $this->Form->text('Model.text');
  2937. $expected = [
  2938. 'input' => [
  2939. 'type' => 'text',
  2940. 'name' => 'Model[text]',
  2941. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  2942. ]
  2943. ];
  2944. $this->assertHtml($expected, $result);
  2945. $this->Form->request->data['Contact']['text'] = 'test';
  2946. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  2947. $expected = [
  2948. 'input' => [
  2949. 'type' => 'text',
  2950. 'name' => 'Contact[text]',
  2951. 'value' => 'test',
  2952. 'id' => 'theID',
  2953. 'class' => 'form-error'
  2954. ]
  2955. ];
  2956. $this->assertHtml($expected, $result);
  2957. }
  2958. /**
  2959. * testDefaultValue method
  2960. *
  2961. * Test default value setting
  2962. *
  2963. * @return void
  2964. */
  2965. public function testTextDefaultValue() {
  2966. $this->Form->request->data['Model']['field'] = 'test';
  2967. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2968. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  2969. $this->assertHtml($expected, $result);
  2970. unset($this->Form->request->data['Model']['field']);
  2971. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2972. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  2973. $this->assertHtml($expected, $result);
  2974. }
  2975. /**
  2976. * testError method
  2977. *
  2978. * Test field error generation
  2979. *
  2980. * @return void
  2981. */
  2982. public function testError() {
  2983. $this->article['errors'] = [
  2984. 'Article' => ['field' => 'email']
  2985. ];
  2986. $this->Form->create($this->article);
  2987. $result = $this->Form->error('Article.field');
  2988. $expected = [
  2989. ['div' => ['class' => 'error-message']],
  2990. 'email',
  2991. '/div',
  2992. ];
  2993. $this->assertHtml($expected, $result);
  2994. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  2995. $expected = [
  2996. ['div' => ['class' => 'error-message']],
  2997. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  2998. '/div',
  2999. ];
  3000. $this->assertHtml($expected, $result);
  3001. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3002. $expected = [
  3003. ['div' => ['class' => 'error-message']],
  3004. '<strong', 'Badness!', '/strong',
  3005. '/div',
  3006. ];
  3007. $this->assertHtml($expected, $result);
  3008. }
  3009. /**
  3010. * Test error with nested lists.
  3011. *
  3012. * @return void
  3013. */
  3014. public function testErrorMessages() {
  3015. $this->article['errors'] = [
  3016. 'Article' => ['field' => 'email']
  3017. ];
  3018. $this->Form->create($this->article);
  3019. $result = $this->Form->error('Article.field', array(
  3020. 'email' => 'No good!'
  3021. ));
  3022. $expected = array(
  3023. 'div' => array('class' => 'error-message'),
  3024. 'No good!',
  3025. '/div'
  3026. );
  3027. $this->assertHtml($expected, $result);
  3028. }
  3029. /**
  3030. * test error() with multiple messages.
  3031. *
  3032. * @return void
  3033. */
  3034. public function testErrorMultipleMessages() {
  3035. $this->article['errors'] = [
  3036. 'field' => ['notEmpty', 'email', 'Something else']
  3037. ];
  3038. $this->Form->create($this->article);
  3039. $result = $this->Form->error('field', array(
  3040. 'notEmpty' => 'Cannot be empty',
  3041. 'email' => 'No good!'
  3042. ));
  3043. $expected = array(
  3044. 'div' => array('class' => 'error-message'),
  3045. 'ul' => array(),
  3046. '<li', 'Cannot be empty', '/li',
  3047. '<li', 'No good!', '/li',
  3048. '<li', 'Something else', '/li',
  3049. '/ul',
  3050. '/div'
  3051. );
  3052. $this->assertHtml($expected, $result);
  3053. }
  3054. /**
  3055. * testPassword method
  3056. *
  3057. * Test password element generation
  3058. *
  3059. * @return void
  3060. */
  3061. public function testPassword() {
  3062. $this->article['errors'] = [
  3063. 'Contact' => [
  3064. 'passwd' => 1
  3065. ]
  3066. ];
  3067. $this->Form->create($this->article);
  3068. $result = $this->Form->password('Contact.field');
  3069. $expected = array('input' => array('type' => 'password', 'name' => 'Contact[field]'));
  3070. $this->assertHtml($expected, $result);
  3071. $this->Form->request->data['Contact']['passwd'] = 'test';
  3072. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3073. $expected = array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error'));
  3074. $this->assertHtml($expected, $result);
  3075. }
  3076. /**
  3077. * testRadio method
  3078. *
  3079. * Test radio element set generation
  3080. *
  3081. * @return void
  3082. */
  3083. public function testRadio() {
  3084. $result = $this->Form->radio('Model.field', array('option A'));
  3085. $expected = array(
  3086. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3087. 'label' => array('for' => 'model-field-0'),
  3088. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3089. 'option A',
  3090. '/label'
  3091. );
  3092. $this->assertHtml($expected, $result);
  3093. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  3094. $this->assertHtml($expected, $result);
  3095. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3096. $expected = array(
  3097. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3098. array('label' => array('for' => 'model-field-0')),
  3099. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3100. 'option A',
  3101. '/label',
  3102. array('label' => array('for' => 'model-field-1')),
  3103. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1')),
  3104. 'option B',
  3105. '/label',
  3106. );
  3107. $this->assertHtml($expected, $result);
  3108. $result = $this->Form->radio(
  3109. 'Employee.gender',
  3110. array('male' => 'Male', 'female' => 'Female'),
  3111. ['form' => 'my-form']
  3112. );
  3113. $expected = array(
  3114. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'),
  3115. array('label' => array('for' => 'employee-gender-male')),
  3116. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form')),
  3117. 'Male',
  3118. '/label',
  3119. array('label' => array('for' => 'employee-gender-female')),
  3120. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form')),
  3121. 'Female',
  3122. '/label',
  3123. );
  3124. $this->assertHtml($expected, $result);
  3125. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  3126. $expected = array(
  3127. array('input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '')),
  3128. array('label' => array('for' => 'model-custom-0')),
  3129. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0')),
  3130. 'option A',
  3131. '/label',
  3132. array('label' => array('for' => 'model-custom-1')),
  3133. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1')),
  3134. 'option B',
  3135. '/label',
  3136. );
  3137. $this->assertHtml($expected, $result);
  3138. }
  3139. /**
  3140. * Test that input works with radio types
  3141. *
  3142. * @return void
  3143. */
  3144. public function testInputRadio() {
  3145. $result = $this->Form->input('test', [
  3146. 'type' => 'radio',
  3147. 'options' => ['A', 'B'],
  3148. ]);
  3149. $expected = [
  3150. ['div' => ['class' => 'input radio']],
  3151. '<label',
  3152. 'Test',
  3153. '/label',
  3154. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  3155. ['label' => ['for' => 'test-0']],
  3156. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  3157. 'A',
  3158. '/label',
  3159. ['label' => ['for' => 'test-1']],
  3160. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  3161. 'B',
  3162. '/label',
  3163. '/div',
  3164. ];
  3165. $this->assertHtml($expected, $result);
  3166. $result = $this->Form->input('test', [
  3167. 'type' => 'radio',
  3168. 'options' => ['A', 'B'],
  3169. 'label' => false
  3170. ]);
  3171. $expected = [
  3172. ['div' => ['class' => 'input radio']],
  3173. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  3174. ['label' => ['for' => 'test-0']],
  3175. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  3176. 'A',
  3177. '/label',
  3178. ['label' => ['for' => 'test-1']],
  3179. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  3180. 'B',
  3181. '/label',
  3182. '/div',
  3183. ];
  3184. $this->assertHtml($expected, $result);
  3185. }
  3186. /**
  3187. * Test that radio() works with label = false
  3188. *
  3189. * @return void
  3190. */
  3191. public function testRadioNoLabel() {
  3192. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  3193. $expected = [
  3194. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3195. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  3196. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  3197. ];
  3198. $this->assertHtml($expected, $result);
  3199. }
  3200. /**
  3201. * test generating radio input inside label ala twitter bootstrap
  3202. *
  3203. * @return void
  3204. */
  3205. public function testRadioInputInsideLabel() {
  3206. $this->Form->templates([
  3207. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3208. 'radioWrapper' => '{{label}}'
  3209. ]);
  3210. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  3211. $expected = [
  3212. ['input' => [
  3213. 'type' => 'hidden',
  3214. 'name' => 'Model[field]',
  3215. 'value' => ''
  3216. ]],
  3217. ['label' => ['for' => 'model-field-0']],
  3218. ['input' => [
  3219. 'type' => 'radio',
  3220. 'name' => 'Model[field]',
  3221. 'value' => '0',
  3222. 'id' => 'model-field-0'
  3223. ]],
  3224. 'option A',
  3225. '/label',
  3226. ['label' => ['for' => 'model-field-1']],
  3227. ['input' => [
  3228. 'type' => 'radio',
  3229. 'name' => 'Model[field]',
  3230. 'value' => '1',
  3231. 'id' => 'model-field-1'
  3232. ]],
  3233. 'option B',
  3234. '/label',
  3235. ];
  3236. $this->assertHtml($expected, $result);
  3237. }
  3238. /**
  3239. * test disabling the hidden input for radio buttons
  3240. *
  3241. * @return void
  3242. */
  3243. public function testRadioHiddenInputDisabling() {
  3244. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3245. $expected = array(
  3246. 'label' => array('for' => 'model-1-field-0'),
  3247. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'),
  3248. 'option A',
  3249. '/label'
  3250. );
  3251. $this->assertHtml($expected, $result);
  3252. }
  3253. /**
  3254. * testSelect method
  3255. *
  3256. * Test select element generation.
  3257. *
  3258. * @return void
  3259. */
  3260. public function testSelect() {
  3261. $result = $this->Form->select('Model.field', array());
  3262. $expected = array(
  3263. 'select' => array('name' => 'Model[field]'),
  3264. '/select'
  3265. );
  3266. $this->assertHtml($expected, $result);
  3267. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3268. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3269. $expected = array(
  3270. 'select' => array('name' => 'Model[field]'),
  3271. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3272. 'good',
  3273. '/option',
  3274. array('option' => array('value' => 'other')),
  3275. 'bad',
  3276. '/option',
  3277. '/select'
  3278. );
  3279. $this->assertHtml($expected, $result);
  3280. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  3281. $this->assertHtml($expected, $result);
  3282. $this->Form->request->data = array();
  3283. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3284. $expected = array(
  3285. 'select' => array('name' => 'Model[field]'),
  3286. array('option' => array('value' => 'value')),
  3287. 'good',
  3288. '/option',
  3289. array('option' => array('value' => 'other')),
  3290. 'bad',
  3291. '/option',
  3292. '/select'
  3293. );
  3294. $this->assertHtml($expected, $result);
  3295. $options = array(
  3296. array('value' => 'first', 'text' => 'First'),
  3297. array('value' => 'first', 'text' => 'Another First'),
  3298. );
  3299. $result = $this->Form->select(
  3300. 'Model.field',
  3301. $options,
  3302. array('escape' => false, 'empty' => false)
  3303. );
  3304. $expected = array(
  3305. 'select' => array('name' => 'Model[field]'),
  3306. array('option' => array('value' => 'first')),
  3307. 'First',
  3308. '/option',
  3309. array('option' => array('value' => 'first')),
  3310. 'Another First',
  3311. '/option',
  3312. '/select'
  3313. );
  3314. $this->assertHtml($expected, $result);
  3315. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3316. $result = $this->Form->select(
  3317. 'Model.contact_id',
  3318. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3319. array('escape' => false, 'empty' => 'pick something')
  3320. );
  3321. $expected = array(
  3322. 'select' => array('name' => 'Model[contact_id]'),
  3323. array('option' => array('value' => '')), 'pick something', '/option',
  3324. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3325. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3326. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3327. '/select'
  3328. );
  3329. $this->assertHtml($expected, $result);
  3330. $this->Form->request->data['Model']['field'] = 0;
  3331. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3332. $expected = array(
  3333. 'select' => array('name' => 'Model[field]'),
  3334. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3335. array('option' => array('value' => '1')), 'Yes', '/option',
  3336. '/select'
  3337. );
  3338. $this->assertHtml($expected, $result);
  3339. }
  3340. /**
  3341. * Test that select() escapes HTML.
  3342. *
  3343. * @return void
  3344. */
  3345. public function testSelectEscapeHtml() {
  3346. $result = $this->Form->select(
  3347. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3348. array('empty' => false)
  3349. );
  3350. $expected = array(
  3351. 'select' => array('name' => 'Model[field]'),
  3352. array('option' => array('value' => 'first')),
  3353. 'first &quot;html&quot; &lt;chars&gt;',
  3354. '/option',
  3355. array('option' => array('value' => 'second')),
  3356. 'value',
  3357. '/option',
  3358. '/select'
  3359. );
  3360. $this->assertHtml($expected, $result);
  3361. $result = $this->Form->select(
  3362. 'Model.field',
  3363. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3364. array('escape' => false, 'empty' => false)
  3365. );
  3366. $expected = array(
  3367. 'select' => array('name' => 'Model[field]'),
  3368. array('option' => array('value' => 'first')),
  3369. 'first "html" <chars>',
  3370. '/option',
  3371. array('option' => array('value' => 'second')),
  3372. 'value',
  3373. '/option',
  3374. '/select'
  3375. );
  3376. $this->assertHtml($expected, $result);
  3377. }
  3378. /**
  3379. * test select() with required and disabled attributes.
  3380. *
  3381. * @return void
  3382. */
  3383. public function testSelectRequired() {
  3384. $this->article['required'] = [
  3385. 'user_id' => true
  3386. ];
  3387. $this->Form->create($this->article);
  3388. $result = $this->Form->select('user_id', array('option A'));
  3389. $expected = array(
  3390. 'select' => array(
  3391. 'name' => 'user_id',
  3392. 'required' => 'required'
  3393. ),
  3394. array('option' => array('value' => '0')), 'option A', '/option',
  3395. '/select'
  3396. );
  3397. $this->assertHtml($expected, $result);
  3398. $result = $this->Form->select('user_id', array('option A'), array('disabled' => true));
  3399. $expected = array(
  3400. 'select' => array(
  3401. 'name' => 'user_id',
  3402. 'disabled' => 'disabled'
  3403. ),
  3404. array('option' => array('value' => '0')), 'option A', '/option',
  3405. '/select'
  3406. );
  3407. $this->assertHtml($expected, $result);
  3408. }
  3409. /**
  3410. * testNestedSelect method
  3411. *
  3412. * test select element generation with optgroups
  3413. *
  3414. * @return void
  3415. */
  3416. public function testNestedSelect() {
  3417. $result = $this->Form->select(
  3418. 'Model.field',
  3419. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3420. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3421. )), array('empty' => false)
  3422. );
  3423. $expected = array(
  3424. 'select' => array('name' => 'Model[field]'),
  3425. array('option' => array('value' => 1)),
  3426. 'One',
  3427. '/option',
  3428. array('option' => array('value' => 2)),
  3429. 'Two',
  3430. '/option',
  3431. array('optgroup' => array('label' => 'Three')),
  3432. array('option' => array('value' => 3)),
  3433. 'Three',
  3434. '/option',
  3435. array('option' => array('value' => 4)),
  3436. 'Four',
  3437. '/option',
  3438. array('option' => array('value' => 5)),
  3439. 'Five',
  3440. '/option',
  3441. '/optgroup',
  3442. '/select'
  3443. );
  3444. $this->assertHtml($expected, $result);
  3445. }
  3446. /**
  3447. * testSelectMultiple method
  3448. *
  3449. * test generation of multiple select elements
  3450. *
  3451. * @return void
  3452. */
  3453. public function testSelectMultiple() {
  3454. $options = array('first', 'second', 'third');
  3455. $result = $this->Form->select(
  3456. 'Model.multi_field',
  3457. $options,
  3458. ['form' => 'my-form', 'multiple' => true]
  3459. );
  3460. $expected = array(
  3461. 'input' => array(
  3462. 'type' => 'hidden',
  3463. 'name' => 'Model[multi_field]',
  3464. 'value' => '',
  3465. 'form' => 'my-form',
  3466. ),
  3467. 'select' => array(
  3468. 'name' => 'Model[multi_field][]',
  3469. 'multiple' => 'multiple',
  3470. 'form' => 'my-form',
  3471. ),
  3472. array('option' => array('value' => '0')),
  3473. 'first',
  3474. '/option',
  3475. array('option' => array('value' => '1')),
  3476. 'second',
  3477. '/option',
  3478. array('option' => array('value' => '2')),
  3479. 'third',
  3480. '/option',
  3481. '/select'
  3482. );
  3483. $this->assertHtml($expected, $result);
  3484. $result = $this->Form->select(
  3485. 'Model.multi_field',
  3486. $options,
  3487. ['multiple' => 'multiple', 'form' => 'my-form']
  3488. );
  3489. $this->assertHtml($expected, $result);
  3490. }
  3491. /**
  3492. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  3493. *
  3494. * @return void
  3495. */
  3496. public function testCheckboxZeroValue() {
  3497. $result = $this->Form->input('User.get_spam', array(
  3498. 'type' => 'checkbox',
  3499. 'value' => '0',
  3500. 'hiddenField' => '1',
  3501. ));
  3502. $expected = array(
  3503. 'div' => array('class' => 'input checkbox'),
  3504. 'label' => array('for' => 'user-get-spam'),
  3505. array('input' => array(
  3506. 'type' => 'hidden', 'name' => 'User[get_spam]',
  3507. 'value' => '1'
  3508. )),
  3509. array('input' => array(
  3510. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  3511. 'value' => '0', 'id' => 'user-get-spam'
  3512. )),
  3513. 'Get Spam',
  3514. '/label',
  3515. '/div'
  3516. );
  3517. $this->assertHtml($expected, $result);
  3518. }
  3519. /**
  3520. * test generation of habtm select boxes.
  3521. *
  3522. * @return void
  3523. */
  3524. public function testHabtmSelectBox() {
  3525. $this->loadFixtures('Articles');
  3526. $options = array(
  3527. 1 => 'blue',
  3528. 2 => 'red',
  3529. 3 => 'green'
  3530. );
  3531. $tags = [
  3532. new Entity(['id' => 1, 'name' => 'blue']),
  3533. new Entity(['id' => 3, 'name' => 'green'])
  3534. ];
  3535. $article = new Article(['tags' => $tags]);
  3536. $this->Form->create($article);
  3537. $result = $this->Form->input('tags._ids', ['options' => $options]);
  3538. $expected = array(
  3539. 'div' => array('class' => 'input select'),
  3540. 'label' => array('for' => 'tags-ids'),
  3541. 'Tags',
  3542. '/label',
  3543. 'input' => array('type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''),
  3544. 'select' => array(
  3545. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  3546. 'multiple' => 'multiple'
  3547. ),
  3548. array('option' => array('value' => '1', 'selected' => 'selected')),
  3549. 'blue',
  3550. '/option',
  3551. array('option' => array('value' => '2')),
  3552. 'red',
  3553. '/option',
  3554. array('option' => array('value' => '3', 'selected' => 'selected')),
  3555. 'green',
  3556. '/option',
  3557. '/select',
  3558. '/div'
  3559. );
  3560. $this->assertHtml($expected, $result);
  3561. // make sure only 50 is selected, and not 50f5c0cf
  3562. $options = array(
  3563. '1' => 'blue',
  3564. '50f5c0cf' => 'red',
  3565. '50' => 'green'
  3566. );
  3567. $tags = [
  3568. new Entity(['id' => 1, 'name' => 'blue']),
  3569. new Entity(['id' => 50, 'name' => 'green'])
  3570. ];
  3571. $article = new Article(['tags' => $tags]);
  3572. $this->Form->create($article);
  3573. $result = $this->Form->input('tags._ids', ['options' => $options]);
  3574. $expected = array(
  3575. 'div' => array('class' => 'input select'),
  3576. 'label' => array('for' => 'tags-ids'),
  3577. 'Tags',
  3578. '/label',
  3579. 'input' => array('type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''),
  3580. 'select' => array(
  3581. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  3582. 'multiple' => 'multiple'
  3583. ),
  3584. array('option' => array('value' => '1', 'selected' => 'selected')),
  3585. 'blue',
  3586. '/option',
  3587. array('option' => array('value' => '50f5c0cf')),
  3588. 'red',
  3589. '/option',
  3590. array('option' => array('value' => '50', 'selected' => 'selected')),
  3591. 'green',
  3592. '/option',
  3593. '/select',
  3594. '/div'
  3595. );
  3596. $this->assertHtml($expected, $result);
  3597. }
  3598. /**
  3599. * test generation of multi select elements in checkbox format
  3600. *
  3601. * @return void
  3602. */
  3603. public function testSelectMultipleCheckboxes() {
  3604. $result = $this->Form->select(
  3605. 'Model.multi_field',
  3606. array('first', 'second', 'third'),
  3607. array('multiple' => 'checkbox')
  3608. );
  3609. $expected = array(
  3610. 'input' => array(
  3611. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3612. ),
  3613. array('div' => array('class' => 'checkbox')),
  3614. array('label' => array('for' => 'model-multi-field-0')),
  3615. array('input' => array(
  3616. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3617. 'value' => '0', 'id' => 'model-multi-field-0'
  3618. )),
  3619. 'first',
  3620. '/label',
  3621. '/div',
  3622. array('div' => array('class' => 'checkbox')),
  3623. array('label' => array('for' => 'model-multi-field-1')),
  3624. array('input' => array(
  3625. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3626. 'value' => '1', 'id' => 'model-multi-field-1'
  3627. )),
  3628. 'second',
  3629. '/label',
  3630. '/div',
  3631. array('div' => array('class' => 'checkbox')),
  3632. array('label' => array('for' => 'model-multi-field-2')),
  3633. array('input' => array(
  3634. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3635. 'value' => '2', 'id' => 'model-multi-field-2'
  3636. )),
  3637. 'third',
  3638. '/label',
  3639. '/div'
  3640. );
  3641. $this->assertHtml($expected, $result);
  3642. $result = $this->Form->select(
  3643. 'Model.multi_field',
  3644. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  3645. array('multiple' => 'checkbox')
  3646. );
  3647. $expected = array(
  3648. 'input' => array(
  3649. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3650. ),
  3651. array('div' => array('class' => 'checkbox')),
  3652. array('label' => array('for' => 'model-multi-field-a+')),
  3653. array('input' => array(
  3654. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3655. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  3656. )),
  3657. 'first',
  3658. '/label',
  3659. '/div',
  3660. array('div' => array('class' => 'checkbox')),
  3661. array('label' => array('for' => 'model-multi-field-a++')),
  3662. array('input' => array(
  3663. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3664. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  3665. )),
  3666. 'second',
  3667. '/label',
  3668. '/div',
  3669. array('div' => array('class' => 'checkbox')),
  3670. array('label' => array('for' => 'model-multi-field-a+++')),
  3671. array('input' => array(
  3672. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3673. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  3674. )),
  3675. 'third',
  3676. '/label',
  3677. '/div'
  3678. );
  3679. $this->assertHtml($expected, $result);
  3680. $result = $this->Form->select(
  3681. 'Model.multi_field',
  3682. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  3683. array('multiple' => 'checkbox')
  3684. );
  3685. $expected = array(
  3686. 'input' => array(
  3687. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3688. ),
  3689. array('div' => array('class' => 'checkbox')),
  3690. array('label' => array('for' => 'model-multi-field-a-b')),
  3691. array('input' => array(
  3692. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3693. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  3694. )),
  3695. 'first',
  3696. '/label',
  3697. '/div',
  3698. array('div' => array('class' => 'checkbox')),
  3699. array('label' => array('for' => 'model-multi-field-a-b1')),
  3700. array('input' => array(
  3701. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3702. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  3703. )),
  3704. 'second',
  3705. '/label',
  3706. '/div',
  3707. array('div' => array('class' => 'checkbox')),
  3708. array('label' => array('for' => 'model-multi-field-a-b2')),
  3709. array('input' => array(
  3710. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3711. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  3712. )),
  3713. 'third',
  3714. '/label',
  3715. '/div'
  3716. );
  3717. $this->assertHtml($expected, $result);
  3718. }
  3719. /**
  3720. * Ensure that multiCheckbox reads from the request data.
  3721. *
  3722. * @return void
  3723. */
  3724. public function testSelectMultipleCheckboxRequestData() {
  3725. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3726. $result = $this->Form->select(
  3727. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3728. );
  3729. $expected = array(
  3730. 'input' => array(
  3731. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  3732. ),
  3733. array('div' => array('class' => 'checkbox')),
  3734. array('label' => array('for' => 'model-tags-1', 'class' => 'selected')),
  3735. array('input' => array(
  3736. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3737. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  3738. )),
  3739. 'first',
  3740. '/label',
  3741. '/div',
  3742. array('div' => array('class' => 'checkbox')),
  3743. array('label' => array('for' => 'model-tags-array')),
  3744. array('input' => array(
  3745. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3746. 'value' => 'Array', 'id' => 'model-tags-array'
  3747. )),
  3748. 'Array',
  3749. '/label',
  3750. '/div'
  3751. );
  3752. $this->assertHtml($expected, $result);
  3753. }
  3754. /**
  3755. * Checks the security hash array generated for multiple-input checkbox elements
  3756. *
  3757. * @return void
  3758. */
  3759. public function testSelectMultipleCheckboxSecurity() {
  3760. $this->Form->request->params['_Token'] = 'testKey';
  3761. $this->assertEquals(array(), $this->Form->fields);
  3762. $result = $this->Form->select(
  3763. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3764. array('multiple' => 'checkbox')
  3765. );
  3766. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3767. $result = $this->Form->secure($this->Form->fields);
  3768. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3769. $this->assertRegExp('/"' . $key . '"/', $result);
  3770. }
  3771. /**
  3772. * Multiple select elements should always be secured as they always participate
  3773. * in the POST data.
  3774. *
  3775. * @return void
  3776. */
  3777. public function testSelectMultipleSecureWithNoOptions() {
  3778. $this->assertEquals(array(), $this->Form->fields);
  3779. $this->Form->select(
  3780. 'Model.select',
  3781. array(),
  3782. array('multiple' => true)
  3783. );
  3784. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3785. }
  3786. /**
  3787. * When a select box has no options it should not be added to the fields list
  3788. * as it always fail post validation.
  3789. *
  3790. * @return void
  3791. */
  3792. public function testSelectNoSecureWithNoOptions() {
  3793. $this->Form->request->params['_Token'] = 'testkey';
  3794. $this->assertEquals([], $this->Form->fields);
  3795. $this->Form->select(
  3796. 'Model.select',
  3797. []
  3798. );
  3799. $this->assertEquals([], $this->Form->fields);
  3800. $this->Form->select(
  3801. 'Model.user_id',
  3802. [],
  3803. ['empty' => true]
  3804. );
  3805. $this->assertEquals(array('Model.user_id'), $this->Form->fields);
  3806. }
  3807. /**
  3808. * testInputMultipleCheckboxes method
  3809. *
  3810. * test input() resulting in multi select elements being generated.
  3811. *
  3812. * @return void
  3813. */
  3814. public function testInputMultipleCheckboxes() {
  3815. $result = $this->Form->input('Model.multi_field', array(
  3816. 'options' => array('first', 'second', 'third'),
  3817. 'multiple' => 'checkbox'
  3818. ));
  3819. $expected = array(
  3820. array('div' => array('class' => 'input select')),
  3821. array('label' => array('for' => 'model-multi-field')),
  3822. 'Multi Field',
  3823. '/label',
  3824. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3825. array('div' => array('class' => 'checkbox')),
  3826. array('label' => array('for' => 'model-multi-field-0')),
  3827. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0')),
  3828. 'first',
  3829. '/label',
  3830. '/div',
  3831. array('div' => array('class' => 'checkbox')),
  3832. array('label' => array('for' => 'model-multi-field-1')),
  3833. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1')),
  3834. 'second',
  3835. '/label',
  3836. '/div',
  3837. array('div' => array('class' => 'checkbox')),
  3838. array('label' => array('for' => 'model-multi-field-2')),
  3839. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  3840. 'third',
  3841. '/label',
  3842. '/div',
  3843. '/div'
  3844. );
  3845. $this->assertHtml($expected, $result);
  3846. $result = $this->Form->input('Model.multi_field', array(
  3847. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3848. 'multiple' => 'checkbox'
  3849. ));
  3850. $expected = array(
  3851. array('div' => array('class' => 'input select')),
  3852. array('label' => array('for' => 'model-multi-field')),
  3853. 'Multi Field',
  3854. '/label',
  3855. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3856. array('div' => array('class' => 'checkbox')),
  3857. array('label' => array('for' => 'model-multi-field-a')),
  3858. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a')),
  3859. 'first',
  3860. '/label',
  3861. '/div',
  3862. array('div' => array('class' => 'checkbox')),
  3863. array('label' => array('for' => 'model-multi-field-b')),
  3864. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b')),
  3865. 'second',
  3866. '/label',
  3867. '/div',
  3868. array('div' => array('class' => 'checkbox')),
  3869. array('label' => array('for' => 'model-multi-field-c')),
  3870. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c')),
  3871. 'third',
  3872. '/label',
  3873. '/div',
  3874. '/div'
  3875. );
  3876. $this->assertHtml($expected, $result);
  3877. }
  3878. /**
  3879. * testSelectHiddenFieldOmission method
  3880. *
  3881. * test that select() with 'hiddenField' => false omits the hidden field
  3882. *
  3883. * @return void
  3884. */
  3885. public function testSelectHiddenFieldOmission() {
  3886. $result = $this->Form->select('Model.multi_field',
  3887. array('first', 'second'),
  3888. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  3889. );
  3890. $this->assertNotContains('type="hidden"', $result);
  3891. }
  3892. /**
  3893. * test that select() with multiple = checkbox works with overriding name attribute.
  3894. *
  3895. * @return void
  3896. */
  3897. public function testSelectCheckboxMultipleOverrideName() {
  3898. $result = $this->Form->select('category', ['1', '2'], [
  3899. 'multiple' => 'checkbox',
  3900. 'name' => 'fish',
  3901. ]);
  3902. $expected = array(
  3903. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => ''),
  3904. array('div' => array('class' => 'checkbox')),
  3905. array('label' => array('for' => 'fish-0')),
  3906. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0')),
  3907. '1',
  3908. '/label',
  3909. '/div',
  3910. array('div' => array('class' => 'checkbox')),
  3911. array('label' => array('for' => 'fish-1')),
  3912. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1')),
  3913. '2',
  3914. '/label',
  3915. '/div'
  3916. );
  3917. $this->assertHtml($expected, $result);
  3918. $result = $this->Form->multiCheckbox(
  3919. 'category',
  3920. new Collection(['1', '2']),
  3921. ['name' => 'fish']
  3922. );
  3923. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  3924. 'name' => 'fish',
  3925. ]);
  3926. $this->assertHtml($expected, $result);
  3927. }
  3928. /**
  3929. * Test that input() works with multicheckbox.
  3930. *
  3931. * @return void
  3932. */
  3933. public function testInputMultiCheckbox() {
  3934. $result = $this->Form->input('category', [
  3935. 'type' => 'multicheckbox',
  3936. 'options' => ['1', '2'],
  3937. ]);
  3938. $expected = [
  3939. ['div' => ['class' => 'input multicheckbox']],
  3940. '<label',
  3941. 'Category',
  3942. '/label',
  3943. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  3944. ['div' => ['class' => 'checkbox']],
  3945. ['label' => ['for' => 'category-0']],
  3946. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  3947. '1',
  3948. '/label',
  3949. '/div',
  3950. ['div' => ['class' => 'checkbox']],
  3951. ['label' => ['for' => 'category-1']],
  3952. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  3953. '2',
  3954. '/label',
  3955. '/div',
  3956. '/div',
  3957. ];
  3958. $this->assertHtml($expected, $result);
  3959. }
  3960. /**
  3961. * testCheckbox method
  3962. *
  3963. * Test generation of checkboxes
  3964. *
  3965. * @return void
  3966. */
  3967. public function testCheckbox() {
  3968. $result = $this->Form->checkbox('Model.field');
  3969. $expected = array(
  3970. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  3971. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'))
  3972. );
  3973. $this->assertHtml($expected, $result);
  3974. $result = $this->Form->checkbox('Model.field', array(
  3975. 'id' => 'theID',
  3976. 'value' => 'myvalue',
  3977. 'form' => 'my-form',
  3978. ));
  3979. $expected = array(
  3980. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'),
  3981. array('input' => array(
  3982. 'type' => 'checkbox', 'name' => 'Model[field]',
  3983. 'value' => 'myvalue', 'id' => 'theID',
  3984. 'form' => 'my-form',
  3985. ))
  3986. );
  3987. $this->assertHtml($expected, $result);
  3988. }
  3989. /**
  3990. * testCheckboxDefaultValue method
  3991. *
  3992. * Test default value setting on checkbox() method
  3993. *
  3994. * @return void
  3995. */
  3996. public function testCheckboxDefaultValue() {
  3997. $this->Form->request->data['Model']['field'] = false;
  3998. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3999. $expected = array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'));
  4000. $this->assertHtml($expected, $result);
  4001. unset($this->Form->request->data['Model']['field']);
  4002. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  4003. $expected = array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked'));
  4004. $this->assertHtml($expected, $result);
  4005. $this->Form->request->data['Model']['field'] = true;
  4006. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  4007. $expected = array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked'));
  4008. $this->assertHtml($expected, $result);
  4009. unset($this->Form->request->data['Model']['field']);
  4010. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  4011. $expected = array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'));
  4012. $this->assertHtml($expected, $result);
  4013. }
  4014. /**
  4015. * Test checkbox being checked or having errors.
  4016. *
  4017. * @return void
  4018. */
  4019. public function testCheckboxCheckedAndError() {
  4020. $this->article['errors'] = [
  4021. 'published' => true
  4022. ];
  4023. $this->Form->request->data['published'] = 'myvalue';
  4024. $this->Form->create($this->article);
  4025. $result = $this->Form->checkbox('published', array('id' => 'theID', 'value' => 'myvalue'));
  4026. $expected = array(
  4027. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4028. array('input' => array(
  4029. 'type' => 'checkbox',
  4030. 'name' => 'published',
  4031. 'value' => 'myvalue',
  4032. 'id' => 'theID',
  4033. 'checked' => 'checked',
  4034. 'class' => 'form-error'
  4035. ))
  4036. );
  4037. $this->assertHtml($expected, $result);
  4038. $this->Form->request->data['published'] = '';
  4039. $result = $this->Form->checkbox('published');
  4040. $expected = array(
  4041. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4042. array('input' => array('type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error'))
  4043. );
  4044. $this->assertHtml($expected, $result);
  4045. }
  4046. /**
  4047. * test checkbox() with a custom name attribute
  4048. *
  4049. * @return void
  4050. */
  4051. public function testCheckboxCustomNameAttribute() {
  4052. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4053. $expected = array(
  4054. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0'),
  4055. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1'))
  4056. );
  4057. $this->assertHtml($expected, $result);
  4058. }
  4059. /**
  4060. * Test that the hidden input for checkboxes can be omitted or set to a
  4061. * specific value.
  4062. *
  4063. * @return void
  4064. */
  4065. public function testCheckboxHiddenField() {
  4066. $result = $this->Form->checkbox('UserForm.something', array(
  4067. 'hiddenField' => false
  4068. ));
  4069. $expected = array(
  4070. 'input' => array(
  4071. 'type' => 'checkbox',
  4072. 'name' => 'UserForm[something]',
  4073. 'value' => '1'
  4074. ),
  4075. );
  4076. $this->assertHtml($expected, $result);
  4077. $result = $this->Form->checkbox('UserForm.something', array(
  4078. 'value' => 'Y',
  4079. 'hiddenField' => 'N',
  4080. ));
  4081. $expected = array(
  4082. array('input' => array(
  4083. 'type' => 'hidden', 'name' => 'UserForm[something]',
  4084. 'value' => 'N'
  4085. )),
  4086. array('input' => array(
  4087. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  4088. 'value' => 'Y'
  4089. )),
  4090. );
  4091. $this->assertHtml($expected, $result);
  4092. }
  4093. /**
  4094. * Test the time type.
  4095. *
  4096. * @return void
  4097. */
  4098. public function testTime() {
  4099. $result = $this->Form->time('start_time', array(
  4100. 'timeFormat' => 12,
  4101. 'interval' => 5,
  4102. 'value' => array('hour' => '4', 'minute' => '30', 'meridian' => 'pm')
  4103. ));
  4104. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  4105. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  4106. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  4107. $this->assertNotContains('year', $result);
  4108. $this->assertNotContains('month', $result);
  4109. $this->assertNotContains('day', $result);
  4110. $result = $this->Form->time('start_time', array(
  4111. 'timeFormat' => 12,
  4112. 'interval' => 5,
  4113. 'value' => '2014-03-08 16:30:00'
  4114. ));
  4115. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  4116. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  4117. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  4118. $this->assertNotContains('year', $result);
  4119. $this->assertNotContains('month', $result);
  4120. $this->assertNotContains('day', $result);
  4121. }
  4122. /**
  4123. * Ensure that timeFormat=24 has no merdian.
  4124. *
  4125. * @return void.
  4126. */
  4127. public function testTimeFormat24NoMeridian() {
  4128. $result = $this->Form->time('start_time', array(
  4129. 'timeFormat' => 24,
  4130. 'interval' => 5,
  4131. 'value' => '2014-03-08 16:30:00'
  4132. ));
  4133. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  4134. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  4135. $this->assertNotContains('meridian', $result);
  4136. $this->assertNotContains('pm', $result);
  4137. $this->assertNotContains('year', $result);
  4138. $this->assertNotContains('month', $result);
  4139. $this->assertNotContains('day', $result);
  4140. }
  4141. /**
  4142. * Test the date type.
  4143. *
  4144. * @return void
  4145. */
  4146. public function testDate() {
  4147. $result = $this->Form->date('start_day', array(
  4148. 'value' => array('year' => '2014', 'month' => '03', 'day' => '08')
  4149. ));
  4150. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  4151. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  4152. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  4153. $this->assertNotContains('hour', $result);
  4154. $this->assertNotContains('minute', $result);
  4155. $this->assertNotContains('second', $result);
  4156. $this->assertNotContains('meridian', $result);
  4157. }
  4158. /**
  4159. * testDateTime method
  4160. *
  4161. * Test generation of date/time select elements
  4162. *
  4163. * @return void
  4164. */
  4165. public function testDateTime() {
  4166. extract($this->dateRegex);
  4167. $result = $this->Form->dateTime('Contact.date', array('empty' => false));
  4168. $now = strtotime('now');
  4169. $expected = array(
  4170. array('select' => array('name' => 'Contact[date][year]')),
  4171. $yearsRegex,
  4172. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4173. date('Y', $now),
  4174. '/option',
  4175. '*/select',
  4176. array('select' => array('name' => 'Contact[date][month]')),
  4177. $monthsRegex,
  4178. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4179. date('F', $now),
  4180. '/option',
  4181. '*/select',
  4182. array('select' => array('name' => 'Contact[date][day]')),
  4183. $daysRegex,
  4184. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4185. date('j', $now),
  4186. '/option',
  4187. '*/select',
  4188. array('select' => array('name' => 'Contact[date][hour]')),
  4189. $hoursRegex,
  4190. array('option' => array('value' => date('H', $now), 'selected' => 'selected')),
  4191. date('G', $now),
  4192. '/option',
  4193. '*/select',
  4194. array('select' => array('name' => 'Contact[date][minute]')),
  4195. $minutesRegex,
  4196. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4197. date('i', $now),
  4198. '/option',
  4199. '*/select',
  4200. );
  4201. $this->assertHtml($expected, $result);
  4202. }
  4203. /**
  4204. * Test that datetime fields are added to protected fields list.
  4205. *
  4206. * @return void
  4207. */
  4208. public function testDateTimeSecured() {
  4209. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  4210. $this->Form->dateTime('Contact.date');
  4211. $expected = [
  4212. 'Contact.date.year',
  4213. 'Contact.date.month',
  4214. 'Contact.date.day',
  4215. 'Contact.date.hour',
  4216. 'Contact.date.minute',
  4217. 'Contact.date.meridian',
  4218. ];
  4219. $this->assertEquals($expected, $this->Form->fields);
  4220. $this->Form->fields = [];
  4221. $this->Form->date('Contact.published');
  4222. $expected = [
  4223. 'Contact.published.year',
  4224. 'Contact.published.month',
  4225. 'Contact.published.day',
  4226. ];
  4227. $this->assertEquals($expected, $this->Form->fields);
  4228. }
  4229. /**
  4230. * Test that datetime fields are added to protected fields list.
  4231. *
  4232. * @return void
  4233. */
  4234. public function testDateTimeSecuredDisabled() {
  4235. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  4236. $this->Form->dateTime('Contact.date', ['secure' => false]);
  4237. $expected = [];
  4238. $this->assertEquals($expected, $this->Form->fields);
  4239. $this->Form->fields = [];
  4240. $this->Form->date('Contact.published', ['secure' => false]);
  4241. $expected = [];
  4242. $this->assertEquals($expected, $this->Form->fields);
  4243. }
  4244. /**
  4245. * Test empty defaulting to true for datetime.
  4246. *
  4247. * @return void
  4248. */
  4249. public function testDatetimeEmpty() {
  4250. extract($this->dateRegex);
  4251. $now = strtotime('now');
  4252. $result = $this->Form->dateTime('Contact.date', array(
  4253. 'timeFormat' => 12,
  4254. 'empty' => true,
  4255. ));
  4256. $expected = array(
  4257. array('select' => array('name' => 'Contact[date][year]')),
  4258. $yearsRegex,
  4259. array('option' => array('value' => '')),
  4260. '/option',
  4261. '*/select',
  4262. array('select' => array('name' => 'Contact[date][month]')),
  4263. $monthsRegex,
  4264. array('option' => array('value' => '')),
  4265. '/option',
  4266. '*/select',
  4267. array('select' => array('name' => 'Contact[date][day]')),
  4268. $daysRegex,
  4269. array('option' => array('value' => '')),
  4270. '/option',
  4271. '*/select',
  4272. array('select' => array('name' => 'Contact[date][hour]')),
  4273. $hoursRegex,
  4274. array('option' => array('value' => '')),
  4275. '/option',
  4276. '*/select',
  4277. array('select' => array('name' => 'Contact[date][minute]')),
  4278. $minutesRegex,
  4279. array('option' => array('value' => '')),
  4280. '/option',
  4281. '*/select',
  4282. array('select' => array('name' => 'Contact[date][meridian]')),
  4283. $meridianRegex,
  4284. array('option' => array('value' => '')),
  4285. '/option',
  4286. '*/select'
  4287. );
  4288. $this->assertHtml($expected, $result);
  4289. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4290. }
  4291. /**
  4292. * Test datetime with interval option.
  4293. *
  4294. * @return void
  4295. */
  4296. public function testDatetimeMinuteInterval() {
  4297. extract($this->dateRegex);
  4298. $now = strtotime('now');
  4299. $result = $this->Form->dateTime('Contact.date', array(
  4300. 'interval' => 5,
  4301. 'value' => ''
  4302. ));
  4303. $expected = array(
  4304. array('select' => array('name' => 'Contact[date][year]')),
  4305. $yearsRegex,
  4306. array('option' => array('selected' => 'selected', 'value' => '')),
  4307. '/option',
  4308. '*/select',
  4309. array('select' => array('name' => 'Contact[date][month]')),
  4310. $monthsRegex,
  4311. array('option' => array('selected' => 'selected', 'value' => '')),
  4312. '/option',
  4313. '*/select',
  4314. array('select' => array('name' => 'Contact[date][day]')),
  4315. $daysRegex,
  4316. array('option' => array('selected' => 'selected', 'value' => '')),
  4317. '/option',
  4318. '*/select',
  4319. array('select' => array('name' => 'Contact[date][hour]')),
  4320. $hoursRegex,
  4321. array('option' => array('selected' => 'selected', 'value' => '')),
  4322. '/option',
  4323. '*/select',
  4324. array('select' => array('name' => 'Contact[date][minute]')),
  4325. $minutesRegex,
  4326. array('option' => array('selected' => 'selected', 'value' => '')),
  4327. '/option',
  4328. array('option' => array('value' => '00')),
  4329. '00',
  4330. '/option',
  4331. array('option' => array('value' => '05')),
  4332. '05',
  4333. '/option',
  4334. array('option' => array('value' => '10')),
  4335. '10',
  4336. '/option',
  4337. '*/select',
  4338. );
  4339. $this->assertHtml($expected, $result);
  4340. }
  4341. /**
  4342. * Test dateTime with rounding
  4343. *
  4344. * @return void
  4345. */
  4346. public function testDateTimeRounding() {
  4347. $this->Form->request->data['Contact'] = array(
  4348. 'date' => array(
  4349. 'day' => '13',
  4350. 'month' => '12',
  4351. 'year' => '2010',
  4352. 'hour' => '04',
  4353. 'minute' => '19',
  4354. 'meridian' => 'AM'
  4355. )
  4356. );
  4357. $result = $this->Form->dateTime('Contact.date', array('interval' => 15));
  4358. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4359. $result = $this->Form->dateTime('Contact.date', array('interval' => 15, 'round' => 'up'));
  4360. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  4361. $result = $this->Form->dateTime('Contact.date', array('interval' => 5, 'round' => 'down'));
  4362. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4363. }
  4364. /**
  4365. * test that datetime() and default values work.
  4366. *
  4367. * @return void
  4368. */
  4369. public function testDatetimeWithDefault() {
  4370. $result = $this->Form->dateTime('Contact.updated', array('value' => '2009-06-01 11:15:30'));
  4371. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4372. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4373. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4374. $result = $this->Form->dateTime('Contact.updated', array(
  4375. 'default' => '2009-06-01 11:15:30'
  4376. ));
  4377. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4378. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4379. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4380. }
  4381. /**
  4382. * testDateTime all zeros
  4383. *
  4384. * @return void
  4385. */
  4386. public function testDateTimeAllZeros() {
  4387. $result = $this->Form->dateTime('Contact.date', array(
  4388. 'timeFormat' => false,
  4389. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  4390. 'value' => '0000-00-00'
  4391. ));
  4392. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  4393. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  4394. }
  4395. /**
  4396. * testDateTimeEmptyAsArray
  4397. *
  4398. * @return void
  4399. */
  4400. public function testDateTimeEmptyAsArray() {
  4401. $result = $this->Form->dateTime('Contact.date', array(
  4402. 'empty' => array(
  4403. 'day' => 'DAY',
  4404. 'month' => 'MONTH',
  4405. 'year' => 'YEAR',
  4406. 'hour' => 'HOUR',
  4407. 'minute' => 'MINUTE',
  4408. 'meridian' => false
  4409. )
  4410. ));
  4411. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4412. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4413. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4414. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4415. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4416. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4417. $result = $this->Form->dateTime('Contact.date', array(
  4418. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4419. ));
  4420. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4421. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4422. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4423. }
  4424. /**
  4425. * testFormDateTimeMulti method
  4426. *
  4427. * test multiple datetime element generation
  4428. *
  4429. * @return void
  4430. */
  4431. public function testFormDateTimeMulti() {
  4432. extract($this->dateRegex);
  4433. $result = $this->Form->dateTime('Contact.1.updated');
  4434. $this->assertContains('Contact[1][updated][month]', $result);
  4435. $this->assertContains('Contact[1][updated][day]', $result);
  4436. $this->assertContains('Contact[1][updated][year]', $result);
  4437. $this->assertContains('Contact[1][updated][hour]', $result);
  4438. $this->assertContains('Contact[1][updated][minute]', $result);
  4439. }
  4440. /**
  4441. * When changing the date format, the label should always focus the first select box when
  4442. * clicked.
  4443. *
  4444. * @return void
  4445. */
  4446. public function testDateTimeLabelIdMatchesFirstInput() {
  4447. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4448. $this->assertContains('<label>Date</label>', $result);
  4449. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4450. $this->assertContains('<label>Date</label>', $result);
  4451. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4452. $this->assertContains('<label>Date</label>', $result);
  4453. }
  4454. /**
  4455. * testMonth method
  4456. *
  4457. * @return void
  4458. */
  4459. public function testMonth() {
  4460. $result = $this->Form->month('Model.field', ['value' => '']);
  4461. $expected = array(
  4462. array('select' => array('name' => 'Model[field][month]')),
  4463. array('option' => array('value' => '', 'selected' => 'selected')),
  4464. '/option',
  4465. array('option' => array('value' => '01')),
  4466. date('F', strtotime('2008-01-01 00:00:00')),
  4467. '/option',
  4468. array('option' => array('value' => '02')),
  4469. date('F', strtotime('2008-02-01 00:00:00')),
  4470. '/option',
  4471. '*/select',
  4472. );
  4473. $this->assertHtml($expected, $result);
  4474. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  4475. $expected = array(
  4476. array('select' => array('name' => 'Model[field][month]')),
  4477. array('option' => array('selected' => 'selected', 'value' => '')),
  4478. '/option',
  4479. array('option' => array('value' => '01')),
  4480. date('F', strtotime('2008-01-01 00:00:00')),
  4481. '/option',
  4482. array('option' => array('value' => '02')),
  4483. date('F', strtotime('2008-02-01 00:00:00')),
  4484. '/option',
  4485. '*/select',
  4486. );
  4487. $this->assertHtml($expected, $result);
  4488. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  4489. $expected = array(
  4490. array('select' => array('name' => 'Model[field][month]')),
  4491. array('option' => array('selected' => 'selected', 'value' => '')),
  4492. '/option',
  4493. array('option' => array('value' => '01')),
  4494. '1',
  4495. '/option',
  4496. array('option' => array('value' => '02')),
  4497. '2',
  4498. '/option',
  4499. '*/select',
  4500. );
  4501. $this->assertHtml($expected, $result);
  4502. $monthNames = [
  4503. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4504. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  4505. ];
  4506. $result = $this->Form->month('Model.field', array('value' => '1', 'monthNames' => $monthNames));
  4507. $expected = array(
  4508. array('select' => array('name' => 'Model[field][month]')),
  4509. array('option' => array('value' => '')),
  4510. '/option',
  4511. array('option' => array('value' => '01', 'selected' => 'selected')),
  4512. 'Jan',
  4513. '/option',
  4514. array('option' => array('value' => '02')),
  4515. 'Feb',
  4516. '/option',
  4517. '*/select',
  4518. );
  4519. $this->assertHtml($expected, $result);
  4520. $this->Form->request->data['Project']['release'] = '2050-02-10';
  4521. $result = $this->Form->month('Project.release');
  4522. $expected = array(
  4523. array('select' => array('name' => 'Project[release][month]')),
  4524. array('option' => array('value' => '')),
  4525. '/option',
  4526. array('option' => array('value' => '01')),
  4527. 'January',
  4528. '/option',
  4529. array('option' => array('value' => '02', 'selected' => 'selected')),
  4530. 'February',
  4531. '/option',
  4532. '*/select',
  4533. );
  4534. $this->assertHtml($expected, $result);
  4535. }
  4536. /**
  4537. * testDay method
  4538. *
  4539. * @return void
  4540. */
  4541. public function testDay() {
  4542. extract($this->dateRegex);
  4543. $result = $this->Form->day('Model.field', array('value' => false));
  4544. $expected = array(
  4545. array('select' => array('name' => 'Model[field][day]')),
  4546. array('option' => array('selected' => 'selected', 'value' => '')),
  4547. '/option',
  4548. array('option' => array('value' => '01')),
  4549. '1',
  4550. '/option',
  4551. array('option' => array('value' => '02')),
  4552. '2',
  4553. '/option',
  4554. $daysRegex,
  4555. '/select',
  4556. );
  4557. $this->assertHtml($expected, $result);
  4558. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4559. $result = $this->Form->day('Model.field');
  4560. $expected = array(
  4561. array('select' => array('name' => 'Model[field][day]')),
  4562. array('option' => array('value' => '')),
  4563. '/option',
  4564. array('option' => array('value' => '01')),
  4565. '1',
  4566. '/option',
  4567. array('option' => array('value' => '02')),
  4568. '2',
  4569. '/option',
  4570. $daysRegex,
  4571. array('option' => array('value' => '10', 'selected' => 'selected')),
  4572. '10',
  4573. '/option',
  4574. $daysRegex,
  4575. '/select',
  4576. );
  4577. $this->assertHtml($expected, $result);
  4578. $this->Form->request->data['Model']['field'] = '';
  4579. $result = $this->Form->day('Model.field', array('value' => '10'));
  4580. $expected = array(
  4581. array('select' => array('name' => 'Model[field][day]')),
  4582. array('option' => array('value' => '')),
  4583. '/option',
  4584. array('option' => array('value' => '01')),
  4585. '1',
  4586. '/option',
  4587. array('option' => array('value' => '02')),
  4588. '2',
  4589. '/option',
  4590. $daysRegex,
  4591. array('option' => array('value' => '10', 'selected' => 'selected')),
  4592. '10',
  4593. '/option',
  4594. $daysRegex,
  4595. '/select',
  4596. );
  4597. $this->assertHtml($expected, $result);
  4598. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4599. $result = $this->Form->day('Project.release');
  4600. $expected = array(
  4601. array('select' => array('name' => 'Project[release][day]')),
  4602. array('option' => array('value' => '')),
  4603. '/option',
  4604. array('option' => array('value' => '01')),
  4605. '1',
  4606. '/option',
  4607. array('option' => array('value' => '02')),
  4608. '2',
  4609. '/option',
  4610. $daysRegex,
  4611. array('option' => array('value' => '10', 'selected' => 'selected')),
  4612. '10',
  4613. '/option',
  4614. $daysRegex,
  4615. '/select',
  4616. );
  4617. $this->assertHtml($expected, $result);
  4618. }
  4619. /**
  4620. * testMinute method
  4621. *
  4622. * @return void
  4623. */
  4624. public function testMinute() {
  4625. extract($this->dateRegex);
  4626. $result = $this->Form->minute('Model.field', ['value' => '']);
  4627. $expected = array(
  4628. array('select' => array('name' => 'Model[field][minute]')),
  4629. array('option' => array('selected' => 'selected', 'value' => '')),
  4630. '/option',
  4631. array('option' => array('value' => '00')),
  4632. '00',
  4633. '/option',
  4634. array('option' => array('value' => '01')),
  4635. '01',
  4636. '/option',
  4637. array('option' => array('value' => '02')),
  4638. '02',
  4639. '/option',
  4640. $minutesRegex,
  4641. '/select',
  4642. );
  4643. $this->assertHtml($expected, $result);
  4644. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4645. $result = $this->Form->minute('Model.field');
  4646. $expected = array(
  4647. array('select' => array('name' => 'Model[field][minute]')),
  4648. array('option' => array('value' => '')),
  4649. '/option',
  4650. array('option' => array('value' => '00')),
  4651. '00',
  4652. '/option',
  4653. array('option' => array('value' => '01')),
  4654. '01',
  4655. '/option',
  4656. array('option' => array('value' => '02')),
  4657. '02',
  4658. '/option',
  4659. $minutesRegex,
  4660. array('option' => array('value' => '12', 'selected' => 'selected')),
  4661. '12',
  4662. '/option',
  4663. $minutesRegex,
  4664. '/select',
  4665. );
  4666. $this->assertHtml($expected, $result);
  4667. $this->Form->request->data['Model']['field'] = '';
  4668. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4669. $expected = array(
  4670. array('select' => array('name' => 'Model[field][minute]')),
  4671. array('option' => array('selected' => 'selected', 'value' => '')),
  4672. '/option',
  4673. array('option' => array('value' => '00')),
  4674. '00',
  4675. '/option',
  4676. array('option' => array('value' => '05')),
  4677. '05',
  4678. '/option',
  4679. array('option' => array('value' => '10')),
  4680. '10',
  4681. '/option',
  4682. $minutesRegex,
  4683. '/select',
  4684. );
  4685. $this->assertHtml($expected, $result);
  4686. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  4687. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4688. $expected = array(
  4689. array('select' => array('name' => 'Model[field][minute]')),
  4690. array('option' => array('value' => '')),
  4691. '/option',
  4692. array('option' => array('value' => '00')),
  4693. '00',
  4694. '/option',
  4695. array('option' => array('value' => '05')),
  4696. '05',
  4697. '/option',
  4698. array('option' => array('value' => '10', 'selected' => 'selected')),
  4699. '10',
  4700. '/option',
  4701. $minutesRegex,
  4702. '/select',
  4703. );
  4704. $this->assertHtml($expected, $result);
  4705. }
  4706. /**
  4707. * Test generating an input for the meridian.
  4708. *
  4709. * @return void
  4710. */
  4711. public function testMeridian() {
  4712. extract($this->dateRegex);
  4713. $now = time();
  4714. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  4715. $expected = [
  4716. array('select' => array('name' => 'Model[field][meridian]')),
  4717. array('option' => array('value' => '')),
  4718. '/option',
  4719. $meridianRegex,
  4720. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4721. date('a', $now),
  4722. '/option',
  4723. '*/select'
  4724. ];
  4725. $this->assertHtml($expected, $result);
  4726. }
  4727. /**
  4728. * testHour method
  4729. *
  4730. * @return void
  4731. */
  4732. public function testHour() {
  4733. extract($this->dateRegex);
  4734. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  4735. $expected = array(
  4736. array('select' => array('name' => 'Model[field][hour]')),
  4737. array('option' => array('selected' => 'selected', 'value' => '')),
  4738. '/option',
  4739. array('option' => array('value' => '01')),
  4740. '1',
  4741. '/option',
  4742. array('option' => array('value' => '02')),
  4743. '2',
  4744. '/option',
  4745. $hoursRegex,
  4746. '/select',
  4747. );
  4748. $this->assertHtml($expected, $result);
  4749. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4750. $result = $this->Form->hour('Model.field', ['format' => 12]);
  4751. $expected = array(
  4752. array('select' => array('name' => 'Model[field][hour]')),
  4753. array('option' => array('value' => '')),
  4754. '/option',
  4755. array('option' => array('value' => '01')),
  4756. '1',
  4757. '/option',
  4758. array('option' => array('value' => '02')),
  4759. '2',
  4760. '/option',
  4761. $hoursRegex,
  4762. array('option' => array('value' => '12', 'selected' => 'selected')),
  4763. '12',
  4764. '/option',
  4765. '/select',
  4766. );
  4767. $this->assertHtml($expected, $result);
  4768. $this->Form->request->data['Model']['field'] = '';
  4769. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => '23'));
  4770. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  4771. $result = $this->Form->hour('Model.field', array('format' => 12, 'value' => '23'));
  4772. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  4773. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4774. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4775. $expected = array(
  4776. array('select' => array('name' => 'Model[field][hour]')),
  4777. array('option' => array('value' => '')),
  4778. '/option',
  4779. array('option' => array('value' => '00', 'selected' => 'selected')),
  4780. '0',
  4781. '/option',
  4782. array('option' => array('value' => '01')),
  4783. '1',
  4784. '/option',
  4785. array('option' => array('value' => '02')),
  4786. '2',
  4787. '/option',
  4788. $hoursRegex,
  4789. '/select',
  4790. );
  4791. $this->assertHtml($expected, $result);
  4792. unset($this->Form->request->data['Model']['field']);
  4793. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => 'now'));
  4794. $thisHour = date('H');
  4795. $optValue = date('G');
  4796. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  4797. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  4798. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4799. $expected = array(
  4800. array('select' => array('name' => 'Model[field][hour]')),
  4801. array('option' => array('value' => '')),
  4802. '/option',
  4803. array('option' => array('value' => '00')),
  4804. '0',
  4805. '/option',
  4806. array('option' => array('value' => '01', 'selected' => 'selected')),
  4807. '1',
  4808. '/option',
  4809. array('option' => array('value' => '02')),
  4810. '2',
  4811. '/option',
  4812. $hoursRegex,
  4813. '/select',
  4814. );
  4815. $this->assertHtml($expected, $result);
  4816. }
  4817. /**
  4818. * testYear method
  4819. *
  4820. * @return void
  4821. */
  4822. public function testYear() {
  4823. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  4824. $expected = array(
  4825. array('select' => array('name' => 'Model[field][year]')),
  4826. array('option' => array('selected' => 'selected', 'value' => '')),
  4827. '/option',
  4828. array('option' => array('value' => '2007')),
  4829. '2007',
  4830. '/option',
  4831. array('option' => array('value' => '2006')),
  4832. '2006',
  4833. '/option',
  4834. '/select',
  4835. );
  4836. $this->assertHtml($expected, $result);
  4837. $result = $this->Form->year('Model.field', [
  4838. 'value' => '',
  4839. 'minYear' => 2006,
  4840. 'maxYear' => 2007,
  4841. 'orderYear' => 'asc'
  4842. ]);
  4843. $expected = array(
  4844. array('select' => array('name' => 'Model[field][year]')),
  4845. array('option' => array('selected' => 'selected', 'value' => '')),
  4846. '/option',
  4847. array('option' => array('value' => '2006')),
  4848. '2006',
  4849. '/option',
  4850. array('option' => array('value' => '2007')),
  4851. '2007',
  4852. '/option',
  4853. '/select',
  4854. );
  4855. $this->assertHtml($expected, $result);
  4856. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  4857. $result = $this->Form->year('Contact.published', [
  4858. 'empty' => false,
  4859. 'minYear' => 2006,
  4860. 'maxYear' => 2007,
  4861. ]);
  4862. $expected = array(
  4863. array('select' => array('name' => 'Contact[published][year]')),
  4864. array('option' => array('value' => '2007')),
  4865. '2007',
  4866. '/option',
  4867. array('option' => array('value' => '2006', 'selected' => 'selected')),
  4868. '2006',
  4869. '/option',
  4870. '/select',
  4871. );
  4872. $this->assertHtml($expected, $result);
  4873. }
  4874. /**
  4875. * testYearAutoExpandRange method
  4876. *
  4877. * @return void
  4878. */
  4879. public function testYearAutoExpandRange() {
  4880. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  4881. $result = $this->Form->year('User.birthday');
  4882. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4883. $result = $matches[1];
  4884. $expected = range(date('Y') + 5, 1930);
  4885. $this->assertEquals($expected, $result);
  4886. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4887. $result = $this->Form->year('Project.release');
  4888. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4889. $result = $matches[1];
  4890. $expected = range(2050, date('Y') - 5);
  4891. $this->assertEquals($expected, $result);
  4892. $this->Form->request->data['Project']['release'] = '1881-10-10';
  4893. $result = $this->Form->year('Project.release', [
  4894. 'minYear' => 1890,
  4895. 'maxYear' => 1900
  4896. ]);
  4897. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4898. $result = $matches[1];
  4899. $expected = range(1900, 1881);
  4900. $this->assertEquals($expected, $result);
  4901. }
  4902. /**
  4903. * Test that input() accepts the type of date and passes options in.
  4904. *
  4905. * @return void
  4906. */
  4907. public function testInputDate() {
  4908. $this->Form->request->data = array(
  4909. 'month_year' => array('month' => date('m')),
  4910. );
  4911. $this->Form->create($this->article);
  4912. $result = $this->Form->input('month_year', array(
  4913. 'label' => false,
  4914. 'type' => 'date',
  4915. 'minYear' => 2006,
  4916. 'maxYear' => 2008
  4917. ));
  4918. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  4919. $this->assertNotContains('value="2008" selected="selected"', $result);
  4920. }
  4921. /**
  4922. * Test the label option being set to false.
  4923. *
  4924. * @return void
  4925. */
  4926. public function testInputLabelFalse() {
  4927. $this->Form->create($this->article);
  4928. $result = $this->Form->input('title', ['label' => false]);
  4929. $expected = [
  4930. 'div' => ['class' => 'input text required'],
  4931. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  4932. '/div'
  4933. ];
  4934. $this->assertHtml($expected, $result);
  4935. }
  4936. /**
  4937. * testInputDateMaxYear method
  4938. *
  4939. * Let's say we want to only allow users born from 2006 to 2008 to register
  4940. * This being the first singup page, we still don't have any data
  4941. *
  4942. * @return void
  4943. */
  4944. public function testInputDateMaxYear() {
  4945. $this->Form->request->data = [];
  4946. $this->Form->create($this->article);
  4947. $result = $this->Form->input('birthday', array(
  4948. 'label' => false,
  4949. 'type' => 'date',
  4950. 'minYear' => 2006,
  4951. 'maxYear' => 2008
  4952. ));
  4953. $this->assertContains('value="2008" selected="selected"', $result);
  4954. }
  4955. /**
  4956. * testTextArea method
  4957. *
  4958. * @return void
  4959. */
  4960. public function testTextArea() {
  4961. $this->Form->request->data = array('field' => 'some test data');
  4962. $result = $this->Form->textarea('field');
  4963. $expected = array(
  4964. 'textarea' => array('name' => 'field'),
  4965. 'some test data',
  4966. '/textarea',
  4967. );
  4968. $this->assertHtml($expected, $result);
  4969. $result = $this->Form->textarea('user.bio');
  4970. $expected = array(
  4971. 'textarea' => array('name' => 'user[bio]'),
  4972. '/textarea',
  4973. );
  4974. $this->assertHtml($expected, $result);
  4975. $this->Form->request->data = array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  4976. $result = $this->Form->textarea('field');
  4977. $expected = array(
  4978. 'textarea' => array('name' => 'field'),
  4979. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  4980. '/textarea',
  4981. );
  4982. $this->assertHtml($expected, $result);
  4983. $this->Form->request->data = [
  4984. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  4985. ];
  4986. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  4987. $expected = array(
  4988. 'textarea' => array('name' => 'Model[field]'),
  4989. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  4990. '/textarea',
  4991. );
  4992. $this->assertHtml($expected, $result);
  4993. $result = $this->Form->textarea('0.OtherModel.field');
  4994. $expected = array(
  4995. 'textarea' => array('name' => '0[OtherModel][field]'),
  4996. '/textarea'
  4997. );
  4998. $this->assertHtml($expected, $result);
  4999. }
  5000. /**
  5001. * testTextAreaWithStupidCharacters method
  5002. *
  5003. * test text area with non-ascii characters
  5004. *
  5005. * @return void
  5006. */
  5007. public function testTextAreaWithStupidCharacters() {
  5008. $result = $this->Form->textarea('Post.content', [
  5009. 'value' => "GREAT®",
  5010. 'rows' => '15',
  5011. 'cols' => '75'
  5012. ]);
  5013. $expected = [
  5014. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5015. 'GREAT®',
  5016. '/textarea',
  5017. ];
  5018. $this->assertHtml($expected, $result);
  5019. }
  5020. /**
  5021. * Test textareas waxlength read from schema.
  5022. *
  5023. * @return void
  5024. */
  5025. public function testTextAreaMaxLength() {
  5026. $this->Form->create([
  5027. 'schema' => [
  5028. 'stuff' => ['type' => 'string', 'length' => 10],
  5029. ]
  5030. ]);
  5031. $result = $this->Form->input('other', array('type' => 'textarea'));
  5032. $expected = array(
  5033. 'div' => array('class' => 'input textarea'),
  5034. 'label' => array('for' => 'other'),
  5035. 'Other',
  5036. '/label',
  5037. 'textarea' => array('name' => 'other', 'id' => 'other'),
  5038. '/textarea',
  5039. '/div'
  5040. );
  5041. $this->assertTags($result, $expected);
  5042. $result = $this->Form->input('stuff', array('type' => 'textarea'));
  5043. $expected = array(
  5044. 'div' => array('class' => 'input textarea'),
  5045. 'label' => array('for' => 'stuff'),
  5046. 'Stuff',
  5047. '/label',
  5048. 'textarea' => array('name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff'),
  5049. '/textarea',
  5050. '/div'
  5051. );
  5052. $this->assertTags($result, $expected);
  5053. }
  5054. /**
  5055. * testHiddenField method
  5056. *
  5057. * @return void
  5058. */
  5059. public function testHiddenField() {
  5060. $this->article['errors'] = [
  5061. 'field' => true
  5062. ];
  5063. $this->Form->request->data['field'] = 'test';
  5064. $this->Form->create($this->article);
  5065. $result = $this->Form->hidden('field', array('id' => 'theID'));
  5066. $expected = array(
  5067. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test'));
  5068. $this->assertHtml($expected, $result);
  5069. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5070. $expected = [
  5071. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  5072. ];
  5073. $this->assertHtml($expected, $result);
  5074. }
  5075. /**
  5076. * testFileUploadField method
  5077. *
  5078. * @return void
  5079. */
  5080. public function testFileUploadField() {
  5081. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5082. $result = $this->Form->file('Model.upload');
  5083. $this->assertHtml($expected, $result);
  5084. $this->Form->request->data['Model']['upload'] = [
  5085. 'name' => '', 'type' => '', 'tmp_name' => '',
  5086. 'error' => 4, 'size' => 0
  5087. ];
  5088. $result = $this->Form->file('Model.upload');
  5089. $this->assertHtml($expected, $result);
  5090. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  5091. $result = $this->Form->file('Model.upload');
  5092. $this->assertHtml($expected, $result);
  5093. }
  5094. /**
  5095. * test File upload input on a model not used in create();
  5096. *
  5097. * @return void
  5098. */
  5099. public function testFileUploadOnOtherModel() {
  5100. $this->Form->create($this->article, array('type' => 'file'));
  5101. $result = $this->Form->file('ValidateProfile.city');
  5102. $expected = array(
  5103. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]')
  5104. );
  5105. $this->assertHtml($expected, $result);
  5106. }
  5107. /**
  5108. * testButton method
  5109. *
  5110. * @return void
  5111. */
  5112. public function testButton() {
  5113. $result = $this->Form->button('Hi');
  5114. $expected = array('button' => array('type' => 'submit'), 'Hi', '/button');
  5115. $this->assertHtml($expected, $result);
  5116. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5117. $expected = array('button' => array('type' => 'reset'), 'Clear Form >', '/button');
  5118. $this->assertHtml($expected, $result);
  5119. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5120. $expected = array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button');
  5121. $this->assertHtml($expected, $result);
  5122. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5123. $expected = array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button');
  5124. $this->assertHtml($expected, $result);
  5125. $result = $this->Form->button('No type', array('type' => false));
  5126. $expected = array('button' => array(), 'No type', '/button');
  5127. $this->assertHtml($expected, $result);
  5128. $result = $this->Form->button('Upload Text', array(
  5129. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  5130. 'escape' => false
  5131. ));
  5132. $this->assertNotRegExp('/\&039/', $result);
  5133. }
  5134. /**
  5135. * Test that button() makes unlocked fields by default.
  5136. *
  5137. * @return void
  5138. */
  5139. public function testButtonUnlockedByDefault() {
  5140. $this->Form->request->params['_csrfToken'] = 'secured';
  5141. $this->Form->button('Save', array('name' => 'save'));
  5142. $this->Form->button('Clear');
  5143. $result = $this->Form->unlockField();
  5144. $this->assertEquals(array('save'), $result);
  5145. }
  5146. /**
  5147. * testPostButton method
  5148. *
  5149. * @return void
  5150. */
  5151. public function testPostButton() {
  5152. $result = $this->Form->postButton('Hi', '/controller/action');
  5153. $expected = array(
  5154. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5155. 'div' => array('style' => 'display:none;'),
  5156. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5157. '/div',
  5158. 'button' => array('type' => 'submit'),
  5159. 'Hi',
  5160. '/button',
  5161. '/form'
  5162. );
  5163. $this->assertHtml($expected, $result);
  5164. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5165. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  5166. }
  5167. /**
  5168. * Test using postButton with N dimensional data.
  5169. *
  5170. * @return void
  5171. */
  5172. public function testPostButtonNestedData() {
  5173. $data = array(
  5174. 'one' => array(
  5175. 'two' => array(
  5176. 3, 4, 5
  5177. )
  5178. )
  5179. );
  5180. $result = $this->Form->postButton('Send', '/', array('data' => $data));
  5181. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5182. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5183. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5184. }
  5185. /**
  5186. * Test that postButton adds _Token fields.
  5187. *
  5188. * @return void
  5189. */
  5190. public function testSecurePostButton() {
  5191. $this->Form->request->params['_csrfToken'] = 'testkey';
  5192. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5193. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5194. $expected = array(
  5195. 'form' => array(
  5196. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5197. ),
  5198. array('div' => array('style' => 'display:none;')),
  5199. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5200. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5201. '/div',
  5202. 'button' => array('type' => 'submit'),
  5203. 'Delete',
  5204. '/button',
  5205. array('div' => array('style' => 'display:none;')),
  5206. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5207. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5208. '/div',
  5209. '/form',
  5210. );
  5211. $this->assertHtml($expected, $result);
  5212. }
  5213. /**
  5214. * testPostLink method
  5215. *
  5216. * @return void
  5217. */
  5218. public function testPostLink() {
  5219. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5220. $expected = array(
  5221. 'form' => array(
  5222. 'method' => 'post', 'action' => '/posts/delete/1',
  5223. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5224. ),
  5225. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5226. '/form',
  5227. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5228. 'Delete',
  5229. '/a'
  5230. );
  5231. $this->assertHtml($expected, $result);
  5232. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5233. $expected = array(
  5234. 'form' => array(
  5235. 'method' => 'post', 'action' => '/posts/delete/1',
  5236. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5237. ),
  5238. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5239. '/form',
  5240. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5241. 'Delete',
  5242. '/a'
  5243. );
  5244. $this->assertHtml($expected, $result);
  5245. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('confirm' => 'Confirm?'));
  5246. $expected = array(
  5247. 'form' => array(
  5248. 'method' => 'post', 'action' => '/posts/delete/1',
  5249. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5250. ),
  5251. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5252. '/form',
  5253. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5254. 'Delete',
  5255. '/a'
  5256. );
  5257. $this->assertHtml($expected, $result);
  5258. $result = $this->Form->postLink(
  5259. 'Delete',
  5260. '/posts/delete/1',
  5261. array('escape' => false, 'confirm' => '\'Confirm\' this "deletion"?')
  5262. );
  5263. $expected = array(
  5264. 'form' => array(
  5265. 'method' => 'post', 'action' => '/posts/delete/1',
  5266. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5267. ),
  5268. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5269. '/form',
  5270. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;&#039;Confirm&#039; this \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5271. 'Delete',
  5272. '/a'
  5273. );
  5274. $this->assertHtml($expected, $result);
  5275. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5276. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  5277. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  5278. $expected = array(
  5279. 'form' => array(
  5280. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  5281. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5282. ),
  5283. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5284. '/form',
  5285. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5286. 'Delete',
  5287. '/a'
  5288. );
  5289. $this->assertHtml($expected, $result);
  5290. $result = $this->Form->postLink(
  5291. '',
  5292. array('controller' => 'items', 'action' => 'delete', 10),
  5293. array('class' => 'btn btn-danger', 'escape' => false, 'confirm' => 'Confirm thing')
  5294. );
  5295. $expected = array(
  5296. 'form' => array(
  5297. 'method' => 'post', 'action' => '/items/delete/10',
  5298. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5299. ),
  5300. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5301. '/form',
  5302. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5303. '/a'
  5304. );
  5305. $this->assertHtml($expected, $result);
  5306. }
  5307. /**
  5308. * Test that security hashes for postLink include the url.
  5309. *
  5310. * @return void
  5311. */
  5312. public function testPostLinkSecurityHash() {
  5313. $hash = Security::hash(
  5314. '/posts/delete/1' .
  5315. serialize(array()) .
  5316. '' .
  5317. Security::salt()
  5318. );
  5319. $hash .= '%3A';
  5320. $this->Form->request->params['_Token']['key'] = 'test';
  5321. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5322. $expected = array(
  5323. 'form' => array(
  5324. 'method' => 'post', 'action' => '/posts/delete/1',
  5325. 'name', 'style' => 'display:none;'
  5326. ),
  5327. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5328. 'div' => array('style' => 'display:none;'),
  5329. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash)),
  5330. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5331. '/div',
  5332. '/form',
  5333. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5334. 'Delete',
  5335. '/a'
  5336. );
  5337. $this->assertHtml($expected, $result);
  5338. }
  5339. /**
  5340. * Test using postLink with N dimensional data.
  5341. *
  5342. * @return void
  5343. */
  5344. public function testPostLinkNestedData() {
  5345. $data = array(
  5346. 'one' => array(
  5347. 'two' => array(
  5348. 3, 4, 5
  5349. )
  5350. )
  5351. );
  5352. $result = $this->Form->postLink('Send', '/', array('data' => $data));
  5353. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5354. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5355. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5356. }
  5357. /**
  5358. * test creating postLinks after a GET form.
  5359. *
  5360. * @return void
  5361. */
  5362. public function testPostLinkAfterGetForm() {
  5363. $this->Form->request->params['_csrfToken'] = 'testkey';
  5364. $this->Form->request->params['_Token'] = 'val';
  5365. $this->Form->create($this->article, array('type' => 'get'));
  5366. $this->Form->end();
  5367. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5368. $expected = array(
  5369. 'form' => array(
  5370. 'method' => 'post', 'action' => '/posts/delete/1',
  5371. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5372. ),
  5373. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5374. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5375. 'div' => array('style' => 'display:none;'),
  5376. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5377. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5378. '/div',
  5379. '/form',
  5380. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5381. 'Delete',
  5382. '/a'
  5383. );
  5384. $this->assertHtml($expected, $result);
  5385. }
  5386. /**
  5387. * Test that postLink adds form tags to view block
  5388. *
  5389. * @return void
  5390. */
  5391. public function testPostLinkFormBuffer() {
  5392. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => true));
  5393. $expected = array(
  5394. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5395. 'Delete',
  5396. '/a'
  5397. );
  5398. $this->assertHtml($expected, $result);
  5399. $result = $this->View->fetch('postLink');
  5400. $expected = array(
  5401. 'form' => array(
  5402. 'method' => 'post', 'action' => '/posts/delete/1',
  5403. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5404. ),
  5405. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5406. '/form'
  5407. );
  5408. $this->assertHtml($expected, $result);
  5409. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  5410. array('block' => true, 'method' => 'DELETE')
  5411. );
  5412. $expected = array(
  5413. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5414. 'Delete',
  5415. '/a'
  5416. );
  5417. $this->assertHtml($expected, $result);
  5418. $result = $this->View->fetch('postLink');
  5419. $expected = array(
  5420. 'form' => array(
  5421. 'method' => 'post', 'action' => '/posts/delete/1',
  5422. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5423. ),
  5424. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5425. '/form',
  5426. array(
  5427. 'form' => array(
  5428. 'method' => 'post', 'action' => '/posts/delete/2',
  5429. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5430. ),
  5431. ),
  5432. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  5433. '/form'
  5434. );
  5435. $this->assertHtml($expected, $result);
  5436. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  5437. $expected = array(
  5438. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5439. 'Delete',
  5440. '/a'
  5441. );
  5442. $this->assertHtml($expected, $result);
  5443. $result = $this->View->fetch('foobar');
  5444. $expected = array(
  5445. 'form' => array(
  5446. 'method' => 'post', 'action' => '/posts/delete/1',
  5447. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5448. ),
  5449. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5450. '/form'
  5451. );
  5452. $this->assertHtml($expected, $result);
  5453. }
  5454. /**
  5455. * testSubmitButton method
  5456. *
  5457. * @return void
  5458. */
  5459. public function testSubmitButton() {
  5460. $result = $this->Form->submit('');
  5461. $expected = array(
  5462. 'div' => array('class' => 'submit'),
  5463. 'input' => array('type' => 'submit', 'value' => ''),
  5464. '/div'
  5465. );
  5466. $this->assertHtml($expected, $result);
  5467. $result = $this->Form->submit('Test Submit');
  5468. $expected = array(
  5469. 'div' => array('class' => 'submit'),
  5470. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5471. '/div'
  5472. );
  5473. $this->assertHtml($expected, $result);
  5474. $result = $this->Form->submit('Next >');
  5475. $expected = array(
  5476. 'div' => array('class' => 'submit'),
  5477. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5478. '/div'
  5479. );
  5480. $this->assertHtml($expected, $result);
  5481. $result = $this->Form->submit('Next >', array('escape' => false));
  5482. $expected = array(
  5483. 'div' => array('class' => 'submit'),
  5484. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5485. '/div'
  5486. );
  5487. $this->assertHtml($expected, $result);
  5488. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5489. $expected = array(
  5490. 'div' => array('class' => 'submit'),
  5491. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5492. '/div'
  5493. );
  5494. $this->assertHtml($expected, $result);
  5495. }
  5496. /**
  5497. * test image submit types.
  5498. *
  5499. * @return void
  5500. */
  5501. public function testSubmitImage() {
  5502. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5503. $expected = array(
  5504. 'div' => array('class' => 'submit'),
  5505. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5506. '/div'
  5507. );
  5508. $this->assertHtml($expected, $result);
  5509. $result = $this->Form->submit('/relative/cake.power.gif');
  5510. $expected = array(
  5511. 'div' => array('class' => 'submit'),
  5512. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5513. '/div'
  5514. );
  5515. $this->assertHtml($expected, $result);
  5516. $result = $this->Form->submit('cake.power.gif');
  5517. $expected = array(
  5518. 'div' => array('class' => 'submit'),
  5519. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5520. '/div'
  5521. );
  5522. $this->assertHtml($expected, $result);
  5523. $result = $this->Form->submit('Not.an.image');
  5524. $expected = array(
  5525. 'div' => array('class' => 'submit'),
  5526. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5527. '/div'
  5528. );
  5529. $this->assertHtml($expected, $result);
  5530. }
  5531. /**
  5532. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5533. * be submitted at a time.
  5534. *
  5535. * @return void
  5536. */
  5537. public function testSubmitUnlockedByDefault() {
  5538. $this->Form->request->params['_Token'] = 'secured';
  5539. $this->Form->submit('Go go');
  5540. $this->Form->submit('Save', array('name' => 'save'));
  5541. $result = $this->Form->unlockField();
  5542. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5543. }
  5544. /**
  5545. * Test submit image with timestamps.
  5546. *
  5547. * @return void
  5548. */
  5549. public function testSubmitImageTimestamp() {
  5550. Configure::write('Asset.timestamp', 'force');
  5551. $result = $this->Form->submit('cake.power.gif');
  5552. $expected = array(
  5553. 'div' => array('class' => 'submit'),
  5554. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5555. '/div'
  5556. );
  5557. $this->assertHtml($expected, $result);
  5558. }
  5559. /**
  5560. * test that datetime() works with GET style forms.
  5561. *
  5562. * @return void
  5563. */
  5564. public function testDateTimeWithGetForms() {
  5565. extract($this->dateRegex);
  5566. $this->Form->create($this->article, array('type' => 'get'));
  5567. $result = $this->Form->datetime('created');
  5568. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  5569. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  5570. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  5571. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  5572. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  5573. }
  5574. /**
  5575. * testForMagicInputNonExistingNorValidated method
  5576. *
  5577. * @return void
  5578. */
  5579. public function testForMagicInputNonExistingNorValidated() {
  5580. $result = $this->Form->create($this->article);
  5581. $this->Form->templates(['inputContainer' => '{{content}}']);
  5582. $result = $this->Form->input('non_existing_nor_validated');
  5583. $expected = array(
  5584. 'label' => array('for' => 'non-existing-nor-validated'),
  5585. 'Non Existing Nor Validated',
  5586. '/label',
  5587. 'input' => array(
  5588. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  5589. 'id' => 'non-existing-nor-validated'
  5590. )
  5591. );
  5592. $this->assertHtml($expected, $result);
  5593. $result = $this->Form->input('non_existing_nor_validated', array(
  5594. 'val' => 'my value'
  5595. ));
  5596. $expected = array(
  5597. 'label' => array('for' => 'non-existing-nor-validated'),
  5598. 'Non Existing Nor Validated',
  5599. '/label',
  5600. 'input' => array(
  5601. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  5602. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  5603. )
  5604. );
  5605. $this->assertHtml($expected, $result);
  5606. $this->Form->request->data = array('non_existing_nor_validated' => 'CakePHP magic');
  5607. $result = $this->Form->input('non_existing_nor_validated');
  5608. $expected = array(
  5609. 'label' => array('for' => 'non-existing-nor-validated'),
  5610. 'Non Existing Nor Validated',
  5611. '/label',
  5612. 'input' => array(
  5613. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  5614. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  5615. )
  5616. );
  5617. $this->assertHtml($expected, $result);
  5618. }
  5619. /**
  5620. * testFormMagicInputLabel method
  5621. *
  5622. * @return void
  5623. */
  5624. public function testFormMagicInputLabel() {
  5625. TableRegistry::get('Contacts', [
  5626. 'className' => __NAMESPACE__ . '\ContactsTable'
  5627. ]);
  5628. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  5629. $this->Form->templates(['inputContainer' => '{{content}}']);
  5630. $result = $this->Form->input('Contacts.name', array('label' => 'My label'));
  5631. $expected = array(
  5632. 'label' => array('for' => 'contacts-name'),
  5633. 'My label',
  5634. '/label',
  5635. 'input' => array(
  5636. 'type' => 'text', 'name' => 'Contacts[name]',
  5637. 'id' => 'contacts-name', 'maxlength' => '255'
  5638. )
  5639. );
  5640. $this->assertHtml($expected, $result);
  5641. $result = $this->Form->input('name', array(
  5642. 'label' => array('class' => 'mandatory')
  5643. ));
  5644. $expected = array(
  5645. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  5646. 'Name',
  5647. '/label',
  5648. 'input' => array(
  5649. 'type' => 'text', 'name' => 'name',
  5650. 'id' => 'name', 'maxlength' => '255'
  5651. )
  5652. );
  5653. $this->assertHtml($expected, $result);
  5654. $result = $this->Form->input('name', array(
  5655. 'div' => false,
  5656. 'label' => array('class' => 'mandatory', 'text' => 'My label')
  5657. ));
  5658. $expected = array(
  5659. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  5660. 'My label',
  5661. '/label',
  5662. 'input' => array(
  5663. 'type' => 'text', 'name' => 'name',
  5664. 'id' => 'name', 'maxlength' => '255'
  5665. )
  5666. );
  5667. $this->assertHtml($expected, $result);
  5668. $result = $this->Form->input('Contact.name', array(
  5669. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  5670. ));
  5671. $expected = array(
  5672. 'label' => array('for' => 'my_id'),
  5673. 'Name',
  5674. '/label',
  5675. 'input' => array(
  5676. 'type' => 'text', 'name' => 'Contact[name]',
  5677. 'id' => 'my_id', 'maxlength' => '255'
  5678. )
  5679. );
  5680. $this->assertHtml($expected, $result);
  5681. $result = $this->Form->input('1.id');
  5682. $expected = array('input' => array(
  5683. 'type' => 'hidden', 'name' => '1[id]',
  5684. 'id' => '1-id'
  5685. ));
  5686. $this->assertHtml($expected, $result);
  5687. $result = $this->Form->input("1.name");
  5688. $expected = array(
  5689. 'label' => array('for' => '1-name'),
  5690. 'Name',
  5691. '/label',
  5692. 'input' => array(
  5693. 'type' => 'text', 'name' => '1[name]',
  5694. 'id' => '1-name', 'maxlength' => '255'
  5695. )
  5696. );
  5697. $this->assertHtml($expected, $result);
  5698. }
  5699. /**
  5700. * testFormEnd method
  5701. *
  5702. * @return void
  5703. */
  5704. public function testFormEnd() {
  5705. $this->assertEquals('</form>', $this->Form->end());
  5706. }
  5707. /**
  5708. * Test the generation of fields for a multi record form.
  5709. *
  5710. * @return void
  5711. */
  5712. public function testMultiRecordForm() {
  5713. $this->loadFixtures('Articles', 'Comments');
  5714. $articles = TableRegistry::get('Articles');
  5715. $articles->hasMany('Comments');
  5716. $comment = new Entity(['comment' => 'Value']);
  5717. $article = new Article(['comments' => [$comment]]);
  5718. $this->Form->create([$article]);
  5719. $result = $this->Form->input('0.comments.1.comment');
  5720. $expected = array(
  5721. 'div' => array('class' => 'input textarea'),
  5722. 'label' => array('for' => '0-comments-1-comment'),
  5723. 'Comment',
  5724. '/label',
  5725. 'textarea' => array(
  5726. 'name',
  5727. 'id' => '0-comments-1-comment',
  5728. ),
  5729. '/textarea',
  5730. '/div'
  5731. );
  5732. $this->assertHtml($expected, $result);
  5733. $result = $this->Form->input('0.comments.0.comment');
  5734. $expected = array(
  5735. 'div' => array('class' => 'input textarea'),
  5736. 'label' => array('for' => '0-comments-0-comment'),
  5737. 'Comment',
  5738. '/label',
  5739. 'textarea' => array(
  5740. 'name',
  5741. 'id' => '0-comments-0-comment'
  5742. ),
  5743. 'Value',
  5744. '/textarea',
  5745. '/div'
  5746. );
  5747. $this->assertHtml($expected, $result);
  5748. $comment->errors('comment', ['Not valid']);
  5749. $result = $this->Form->input('0.comments.0.comment');
  5750. $expected = array(
  5751. 'div' => array('class' => 'input textarea error'),
  5752. 'label' => array('for' => '0-comments-0-comment'),
  5753. 'Comment',
  5754. '/label',
  5755. 'textarea' => array(
  5756. 'name',
  5757. 'class' => 'form-error',
  5758. 'id' => '0-comments-0-comment'
  5759. ),
  5760. 'Value',
  5761. '/textarea',
  5762. array('div' => array('class' => 'error-message')),
  5763. 'Not valid',
  5764. '/div',
  5765. '/div'
  5766. );
  5767. $this->assertHtml($expected, $result);
  5768. TableRegistry::get('Comments')
  5769. ->validator('default')
  5770. ->allowEmpty('comment', false);
  5771. $result = $this->Form->input('0.comments.1.comment');
  5772. $expected = array(
  5773. 'div' => array('class' => 'input textarea required'),
  5774. 'label' => array('for' => '0-comments-1-comment'),
  5775. 'Comment',
  5776. '/label',
  5777. 'textarea' => array(
  5778. 'name',
  5779. 'required' => 'required',
  5780. 'id' => '0-comments-1-comment'
  5781. ),
  5782. '/textarea',
  5783. '/div'
  5784. );
  5785. $this->assertHtml($expected, $result);
  5786. }
  5787. /**
  5788. * test that some html5 inputs + FormHelper::__call() work
  5789. *
  5790. * @return void
  5791. */
  5792. public function testHtml5Inputs() {
  5793. $result = $this->Form->email('User.email');
  5794. $expected = array(
  5795. 'input' => array('type' => 'email', 'name' => 'User[email]')
  5796. );
  5797. $this->assertHtml($expected, $result);
  5798. $result = $this->Form->search('User.query');
  5799. $expected = array(
  5800. 'input' => array('type' => 'search', 'name' => 'User[query]')
  5801. );
  5802. $this->assertHtml($expected, $result);
  5803. $result = $this->Form->search('User.query', array('value' => 'test'));
  5804. $expected = array(
  5805. 'input' => array('type' => 'search', 'name' => 'User[query]', 'value' => 'test')
  5806. );
  5807. $this->assertHtml($expected, $result);
  5808. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  5809. $expected = array(
  5810. 'input' => array('type' => 'text', 'name' => 'User[query]', 'value' => 'test')
  5811. );
  5812. $this->assertHtml($expected, $result);
  5813. }
  5814. /**
  5815. * Test accessing html5 inputs through input().
  5816. *
  5817. * @return void
  5818. */
  5819. public function testHtml5InputWithInput() {
  5820. $this->Form->create();
  5821. $this->Form->templates(['inputContainer' => '{{content}}']);
  5822. $result = $this->Form->input('website', array(
  5823. 'type' => 'url',
  5824. 'val' => 'http://domain.tld',
  5825. 'label' => false
  5826. ));
  5827. $expected = array(
  5828. 'input' => array('type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld')
  5829. );
  5830. $this->assertHtml($expected, $result);
  5831. }
  5832. /**
  5833. * Test errors when field name is missing.
  5834. *
  5835. * @expectedException \Cake\Core\Exception\Exception
  5836. * @return void
  5837. */
  5838. public function testHtml5InputException() {
  5839. $this->Form->email();
  5840. }
  5841. /**
  5842. * Tests that formhelper sets required attributes.
  5843. *
  5844. * @return void
  5845. */
  5846. public function testRequiredAttribute() {
  5847. $this->article['required'] = [
  5848. 'title' => true,
  5849. 'body' => false,
  5850. ];
  5851. $this->Form->create($this->article);
  5852. $result = $this->Form->input('title');
  5853. $expected = array(
  5854. 'div' => array('class' => 'input text required'),
  5855. 'label' => array('for' => 'title'),
  5856. 'Title',
  5857. '/label',
  5858. 'input' => array(
  5859. 'type' => 'text',
  5860. 'name' => 'title',
  5861. 'id' => 'title',
  5862. 'required' => 'required',
  5863. ),
  5864. '/div'
  5865. );
  5866. $this->assertHtml($expected, $result);
  5867. $result = $this->Form->input('title', ['required' => false]);
  5868. $this->assertNotContains('required', $result);
  5869. $result = $this->Form->input('body');
  5870. $expected = array(
  5871. 'div' => array('class' => 'input text'),
  5872. 'label' => array('for' => 'body'),
  5873. 'Body',
  5874. '/label',
  5875. 'input' => array(
  5876. 'type' => 'text',
  5877. 'name' => 'body',
  5878. 'id' => 'body',
  5879. ),
  5880. '/div'
  5881. );
  5882. $this->assertHtml($expected, $result);
  5883. $result = $this->Form->input('body', ['required' => true]);
  5884. $this->assertContains('required', $result);
  5885. }
  5886. /**
  5887. * Tests that it is possible to put inputs outside of the label
  5888. *
  5889. * @return void
  5890. */
  5891. public function testInputsNotNested() {
  5892. $this->Form->templates([
  5893. 'nestingLabel' => '{{input}}<label{{attrs}}>{{text}}</label>',
  5894. 'formGroup' => '{{input}}{{label}}',
  5895. ]);
  5896. $result = $this->Form->input('foo', ['type' => 'checkbox']);
  5897. $expected = [
  5898. 'div' => ['class' => 'input checkbox'],
  5899. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  5900. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  5901. 'label' => ['for' => 'foo'],
  5902. 'Foo',
  5903. '/label',
  5904. '/div'
  5905. ];
  5906. $this->assertHtml($expected, $result);
  5907. $result = $this->Form->input('foo', ['type' => 'checkbox', 'label' => false]);
  5908. $expected = [
  5909. 'div' => ['class' => 'input checkbox'],
  5910. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  5911. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  5912. '/div'
  5913. ];
  5914. $this->assertHtml($expected, $result);
  5915. $result = $this->Form->input('confirm', [
  5916. 'type' => 'radio',
  5917. 'options' => ['Y' => 'Yes', 'N' => 'No']
  5918. ]);
  5919. $expected = [
  5920. 'div' => ['class' => 'input radio'],
  5921. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  5922. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  5923. ['label' => ['for' => 'confirm-y']],
  5924. 'Yes',
  5925. '/label',
  5926. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  5927. ['label' => ['for' => 'confirm-n']],
  5928. 'No',
  5929. '/label',
  5930. '<label',
  5931. 'Confirm',
  5932. '/label',
  5933. '/div',
  5934. ];
  5935. $this->assertHtml($expected, $result);
  5936. $result = $this->Form->select('category', ['1', '2'], [
  5937. 'multiple' => 'checkbox',
  5938. 'name' => 'fish',
  5939. ]);
  5940. $expected = [
  5941. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5942. ['div' => ['class' => 'checkbox']],
  5943. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5944. ['label' => ['for' => 'fish-0']],
  5945. '1',
  5946. '/label',
  5947. '/div',
  5948. ['div' => ['class' => 'checkbox']],
  5949. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5950. ['label' => ['for' => 'fish-1']],
  5951. '2',
  5952. '/label',
  5953. '/div'
  5954. ];
  5955. $this->assertHtml($expected, $result);
  5956. }
  5957. /**
  5958. * Test that *Container templates are used by input.
  5959. *
  5960. * @return void
  5961. */
  5962. public function testInputContainerTemplates() {
  5963. $this->Form->templates([
  5964. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  5965. 'radioContainer' => '<div class="rad">{{content}}</div>',
  5966. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  5967. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  5968. ]);
  5969. $this->article['errors'] = [
  5970. 'Article' => ['published' => 'error message']
  5971. ];
  5972. $this->Form->create($this->article);
  5973. $result = $this->Form->input('accept', [
  5974. 'type' => 'checkbox'
  5975. ]);
  5976. $expected = [
  5977. 'div' => ['class' => 'check'],
  5978. 'label' => ['for' => 'accept'],
  5979. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  5980. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  5981. 'Accept',
  5982. '/label',
  5983. '/div'
  5984. ];
  5985. $this->assertHtml($expected, $result);
  5986. $result = $this->Form->input('accept', [
  5987. 'type' => 'radio',
  5988. 'options' => ['Y', 'N']
  5989. ]);
  5990. $this->assertContains('<div class="rad">', $result);
  5991. $result = $this->Form->input('Article.published', [
  5992. 'type' => 'radio',
  5993. 'options' => ['Y', 'N']
  5994. ]);
  5995. $this->assertContains('<div class="rad err">', $result);
  5996. $result = $this->Form->input('Article.created', [
  5997. 'type' => 'datetime',
  5998. ]);
  5999. $this->assertContains('<div class="dt">', $result);
  6000. }
  6001. /**
  6002. * Test resetting templates.
  6003. *
  6004. * @return void
  6005. */
  6006. public function testResetTemplates() {
  6007. $this->Form->templates(['input' => '<input>']);
  6008. $this->assertEquals('<input>', $this->Form->templater()->get('input'));
  6009. $this->assertNull($this->Form->resetTemplates());
  6010. $this->assertNotEquals('<input>', $this->Form->templater()->get('input'));
  6011. }
  6012. /**
  6013. * Test the context method.
  6014. *
  6015. * @return void
  6016. */
  6017. public function testContext() {
  6018. $result = $this->Form->context();
  6019. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  6020. $mock = $this->getMock('Cake\View\Form\ContextInterface');
  6021. $this->assertSame($mock, $this->Form->context($mock));
  6022. $this->assertSame($mock, $this->Form->context());
  6023. }
  6024. }