FormHelperTest.php 258 KB

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