FormHelperTest.php 239 KB

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