FormHelperTest.php 243 KB

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