FormHelperTest.php 283 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584
  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. class ContactsTable extends Table
  38. {
  39. /**
  40. * Default schema
  41. *
  42. * @var array
  43. */
  44. protected $_schema = [
  45. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  46. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  47. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  48. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  51. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  52. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  53. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  54. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  55. ];
  56. /**
  57. * Initializes the schema
  58. *
  59. * @return void
  60. */
  61. public function initialize(array $config)
  62. {
  63. $this->schema($this->_schema);
  64. }
  65. }
  66. /**
  67. * ValidateUser class
  68. */
  69. class ValidateUsersTable extends Table
  70. {
  71. /**
  72. * schema method
  73. *
  74. * @var array
  75. */
  76. protected $_schema = [
  77. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  78. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  79. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  80. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  81. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  82. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  83. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  84. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  85. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  86. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  87. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  88. ];
  89. /**
  90. * Initializes the schema
  91. *
  92. * @return void
  93. */
  94. public function initialize(array $config)
  95. {
  96. $this->schema($this->_schema);
  97. }
  98. }
  99. /**
  100. * FormHelperTest class
  101. *
  102. * @property FormHelper $Form
  103. */
  104. class FormHelperTest extends TestCase
  105. {
  106. /**
  107. * Fixtures to be used
  108. *
  109. * @var array
  110. */
  111. public $fixtures = ['core.articles', 'core.comments'];
  112. /**
  113. * Do not load the fixtures by default
  114. *
  115. * @var bool
  116. */
  117. public $autoFixtures = false;
  118. /**
  119. * setUp method
  120. *
  121. * @return void
  122. */
  123. public function setUp()
  124. {
  125. parent::setUp();
  126. Configure::write('Config.language', 'eng');
  127. Configure::write('App.base', '');
  128. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  129. Configure::delete('Asset');
  130. $this->View = new View();
  131. $this->Form = new FormHelper($this->View);
  132. $request = new Request('articles/add');
  133. $request->here = '/articles/add';
  134. $request['controller'] = 'articles';
  135. $request['action'] = 'add';
  136. $request->webroot = '';
  137. $request->base = '';
  138. $this->Form->Url->request = $this->Form->request = $request;
  139. $this->dateRegex = [
  140. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  141. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  142. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  143. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  144. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  145. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  146. ];
  147. $this->article = [
  148. 'schema' => [
  149. 'id' => ['type' => 'integer'],
  150. 'author_id' => ['type' => 'integer', 'null' => true],
  151. 'title' => ['type' => 'string', 'null' => true],
  152. 'body' => 'text',
  153. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  154. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  155. ],
  156. 'required' => [
  157. 'author_id' => true,
  158. 'title' => true,
  159. ]
  160. ];
  161. Security::salt('foo!');
  162. Router::connect('/:controller', ['action' => 'index']);
  163. Router::connect('/:controller/:action/*');
  164. }
  165. /**
  166. * tearDown method
  167. *
  168. * @return void
  169. */
  170. public function tearDown()
  171. {
  172. parent::tearDown();
  173. unset($this->Form, $this->Controller, $this->View);
  174. TableRegistry::clear();
  175. }
  176. /**
  177. * Test construct() with the templates option.
  178. *
  179. * @return void
  180. */
  181. public function testConstructTemplatesFile()
  182. {
  183. $helper = new FormHelper($this->View, [
  184. 'templates' => 'htmlhelper_tags'
  185. ]);
  186. $result = $helper->input('name');
  187. $this->assertContains('<input', $result);
  188. }
  189. /**
  190. * Test that when specifying custom widgets the config array for that widget
  191. * is overwritten instead of merged.
  192. *
  193. * @return void
  194. */
  195. public function testConstructWithWidgets()
  196. {
  197. $config = [
  198. 'widgets' => [
  199. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  200. ]
  201. ];
  202. $helper = new FormHelper($this->View, $config);
  203. $registry = $helper->widgetRegistry();
  204. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  205. }
  206. /**
  207. * Test that when specifying custom widgets config file and it should be
  208. * added to widgets array. WidgetRegistry will load widgets in constructor.
  209. *
  210. * @return void
  211. */
  212. public function testConstructWithWidgetsConfig()
  213. {
  214. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  215. $registry = $helper->widgetRegistry();
  216. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  217. }
  218. /**
  219. * Test registering a new widget class and rendering it.
  220. *
  221. * @return void
  222. */
  223. public function testAddWidgetAndRenderWidget()
  224. {
  225. $data = [
  226. 'val' => 1
  227. ];
  228. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  229. $this->assertNull($this->Form->addWidget('test', $mock));
  230. $mock->expects($this->once())
  231. ->method('render')
  232. ->with($data)
  233. ->will($this->returnValue('HTML'));
  234. $result = $this->Form->widget('test', $data);
  235. $this->assertEquals('HTML', $result);
  236. }
  237. /**
  238. * Test that secureFields() of widget is called after calling render(),
  239. * not before.
  240. *
  241. * @return void
  242. */
  243. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  244. {
  245. $data = [
  246. 'val' => 1,
  247. 'name' => 'test'
  248. ];
  249. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  250. $this->assertNull($this->Form->addWidget('test', $mock));
  251. $mock->expects($this->at(0))
  252. ->method('render')
  253. ->with($data)
  254. ->will($this->returnValue('HTML'));
  255. $mock->expects($this->at(1))
  256. ->method('secureFields')
  257. ->with($data)
  258. ->will($this->returnValue(['test']));
  259. $result = $this->Form->widget('test', $data + ['secure' => true]);
  260. $this->assertEquals('HTML', $result);
  261. }
  262. /**
  263. * Test that empty string is not added to secure fields list when
  264. * rendering input widget without name.
  265. *
  266. * @return void
  267. */
  268. public function testRenderingWidgetWithEmptyName()
  269. {
  270. $this->assertEquals([], $this->Form->fields);
  271. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  272. $this->assertEquals('<select name=""></select>', $result);
  273. $this->assertEquals([], $this->Form->fields);
  274. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  275. $this->assertEquals('<select name="0"></select>', $result);
  276. $this->assertEquals(['0'], $this->Form->fields);
  277. }
  278. /**
  279. * Test registering an invalid widget class.
  280. *
  281. * @expectedException \RuntimeException
  282. * @return void
  283. */
  284. public function testAddWidgetInvalid()
  285. {
  286. $mock = new \StdClass();
  287. $this->Form->addWidget('test', $mock);
  288. $this->Form->widget('test');
  289. }
  290. /**
  291. * Test adding a new context class.
  292. *
  293. * @return void
  294. */
  295. public function testAddContextProvider()
  296. {
  297. $context = 'My data';
  298. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  299. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  300. $this->assertInstanceOf('Cake\Network\Request', $request);
  301. $this->assertEquals($context, $data['entity']);
  302. return $stub;
  303. });
  304. $this->Form->create($context);
  305. $result = $this->Form->context();
  306. $this->assertSame($stub, $result);
  307. }
  308. /**
  309. * Test replacing a context class.
  310. *
  311. * @return void
  312. */
  313. public function testAddContextProviderReplace()
  314. {
  315. $entity = new Article();
  316. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  317. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  318. return $stub;
  319. });
  320. $this->Form->create($entity);
  321. $result = $this->Form->context();
  322. $this->assertSame($stub, $result);
  323. }
  324. /**
  325. * Test overriding a context class.
  326. *
  327. * @return void
  328. */
  329. public function testAddContextProviderAdd()
  330. {
  331. $entity = new Article();
  332. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  333. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  334. if ($data['entity'] instanceof Entity) {
  335. return $stub;
  336. }
  337. });
  338. $this->Form->create($entity);
  339. $result = $this->Form->context();
  340. $this->assertSame($stub, $result);
  341. }
  342. /**
  343. * Test adding an invalid context class.
  344. *
  345. * @expectedException \RuntimeException
  346. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  347. * @return void
  348. */
  349. public function testAddContextProviderInvalid()
  350. {
  351. $context = 'My data';
  352. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  353. return new \StdClass();
  354. });
  355. $this->Form->create($context);
  356. }
  357. /**
  358. * Provides context options for create().
  359. *
  360. * @return array
  361. */
  362. public function contextSelectionProvider()
  363. {
  364. $entity = new Article();
  365. $collection = $this->getMockBuilder('Cake\Collection\Collection')
  366. ->setMethods(['extract'])
  367. ->setConstructorArgs([[$entity]])
  368. ->getMock();
  369. $emptyCollection = new Collection([]);
  370. $emptyArray = [];
  371. $arrayObject = new \ArrayObject([]);
  372. $data = [
  373. 'schema' => [
  374. 'title' => ['type' => 'string']
  375. ]
  376. ];
  377. $form = new Form();
  378. return [
  379. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  380. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  381. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  382. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  383. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  384. 'form' => [$form, 'Cake\View\Form\FormContext'],
  385. 'none' => [null, 'Cake\View\Form\NullContext'],
  386. 'false' => [false, 'Cake\View\Form\NullContext'],
  387. ];
  388. }
  389. /**
  390. * Test default context selection in create()
  391. *
  392. * @dataProvider contextSelectionProvider
  393. * @return void
  394. */
  395. public function testCreateContextSelectionBuiltIn($data, $class)
  396. {
  397. $this->loadFixtures('Articles');
  398. $this->Form->create($data);
  399. $this->assertInstanceOf($class, $this->Form->context());
  400. }
  401. /**
  402. * Data provider for type option.
  403. *
  404. * @return array
  405. */
  406. public static function requestTypeProvider()
  407. {
  408. return [
  409. // type, method, override
  410. ['post', 'post', 'POST'],
  411. ['put', 'post', 'PUT'],
  412. ['patch', 'post', 'PATCH'],
  413. ['delete', 'post', 'DELETE'],
  414. ];
  415. }
  416. /**
  417. * Test creating file forms.
  418. *
  419. * @return void
  420. */
  421. public function testCreateFile()
  422. {
  423. $encoding = strtolower(Configure::read('App.encoding'));
  424. $result = $this->Form->create(false, ['type' => 'file']);
  425. $expected = [
  426. 'form' => [
  427. 'method' => 'post', 'action' => '/articles/add',
  428. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  429. ],
  430. 'div' => ['style' => 'display:none;'],
  431. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  432. '/div'
  433. ];
  434. $this->assertHtml($expected, $result);
  435. }
  436. /**
  437. * Test creating GET forms.
  438. *
  439. * @return void
  440. */
  441. public function testCreateGet()
  442. {
  443. $encoding = strtolower(Configure::read('App.encoding'));
  444. $result = $this->Form->create(false, ['type' => 'get']);
  445. $expected = ['form' => [
  446. 'method' => 'get', 'action' => '/articles/add',
  447. 'accept-charset' => $encoding
  448. ]];
  449. $this->assertHtml($expected, $result);
  450. }
  451. /**
  452. * Test create() with the templates option.
  453. *
  454. * @return void
  455. */
  456. public function testCreateTemplatesArray()
  457. {
  458. $result = $this->Form->create($this->article, [
  459. 'templates' => [
  460. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  461. ]
  462. ]);
  463. $expected = [
  464. 'form' => [
  465. 'class' => 'form-horizontal',
  466. 'method' => 'post',
  467. 'action' => '/articles/add',
  468. 'accept-charset' => 'utf-8'
  469. ]
  470. ];
  471. $this->assertHtml($expected, $result);
  472. }
  473. /**
  474. * Test create() with the templates option.
  475. *
  476. * @return void
  477. */
  478. public function testCreateTemplatesFile()
  479. {
  480. $result = $this->Form->create($this->article, [
  481. 'templates' => 'htmlhelper_tags',
  482. ]);
  483. $expected = [
  484. 'start form',
  485. 'div' => ['class' => 'hidden'],
  486. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  487. '/div'
  488. ];
  489. $this->assertHtml($expected, $result);
  490. }
  491. /**
  492. * Test that create() and end() restore templates.
  493. *
  494. * @return void
  495. */
  496. public function testCreateEndRestoreTemplates()
  497. {
  498. $this->Form->create($this->article, [
  499. 'templates' => ['input' => 'custom input element']
  500. ]);
  501. $this->Form->end();
  502. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  503. }
  504. /**
  505. * Test using template vars in various templates used by input() method.
  506. *
  507. * @return void
  508. */
  509. public function testInputTemplateVars()
  510. {
  511. $result = $this->Form->input('text', [
  512. 'templates' => [
  513. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  514. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  515. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  516. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  517. ],
  518. 'templateVars' => [
  519. 'forinput' => 'in-input',
  520. 'forlabel' => 'in-label',
  521. 'forgroup' => 'in-group',
  522. 'forcontainer' => 'in-container'
  523. ]
  524. ]);
  525. $expected = [
  526. 'div' => ['class'],
  527. 'label' => ['for'],
  528. 'Text in-label',
  529. '/label',
  530. 'in-group',
  531. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  532. 'in-container',
  533. '/div',
  534. ];
  535. $this->assertHtml($expected, $result);
  536. }
  537. /**
  538. * Test ensuring template variables work in template files loaded
  539. * during input().
  540. *
  541. * @return void
  542. */
  543. public function testInputTemplatesFromFile()
  544. {
  545. $result = $this->Form->input('title', [
  546. 'templates' => 'test_templates',
  547. 'templateVars' => [
  548. 'forcontainer' => 'container-data'
  549. ]
  550. ]);
  551. $expected = [
  552. 'div' => ['class'],
  553. 'label' => ['for'],
  554. 'Title',
  555. '/label',
  556. 'input' => ['name', 'type' => 'text', 'id'],
  557. 'container-data',
  558. '/div',
  559. ];
  560. $this->assertHtml($expected, $result);
  561. }
  562. /**
  563. * Test using template vars in inputSubmit and submitContainer template.
  564. *
  565. * @return void
  566. */
  567. public function testSubmitTemplateVars()
  568. {
  569. $this->Form->templates([
  570. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  571. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  572. ]);
  573. $result = $this->Form->submit('Submit', [
  574. 'templateVars' => [
  575. 'forinput' => 'in-input',
  576. 'forcontainer' => 'in-container'
  577. ]
  578. ]);
  579. $expected = [
  580. 'div' => ['class'],
  581. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  582. 'in-container',
  583. '/div',
  584. ];
  585. $this->assertHtml($expected, $result);
  586. }
  587. /**
  588. * test the create() method
  589. *
  590. * @dataProvider requestTypeProvider
  591. * @return void
  592. */
  593. public function testCreateTypeOptions($type, $method, $override)
  594. {
  595. $encoding = strtolower(Configure::read('App.encoding'));
  596. $result = $this->Form->create(false, ['type' => $type]);
  597. $expected = [
  598. 'form' => [
  599. 'method' => $method, 'action' => '/articles/add',
  600. 'accept-charset' => $encoding
  601. ],
  602. 'div' => ['style' => 'display:none;'],
  603. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  604. '/div'
  605. ];
  606. $this->assertHtml($expected, $result);
  607. }
  608. /**
  609. * Test opening a form for an update operation.
  610. *
  611. * @return void
  612. */
  613. public function testCreateUpdateForm()
  614. {
  615. $encoding = strtolower(Configure::read('App.encoding'));
  616. $this->Form->request->here = '/articles/edit/1';
  617. $this->Form->request['action'] = 'edit';
  618. $this->article['defaults']['id'] = 1;
  619. $result = $this->Form->create($this->article);
  620. $expected = [
  621. 'form' => [
  622. 'method' => 'post', 'action' => '/articles/edit/1',
  623. 'accept-charset' => $encoding
  624. ],
  625. 'div' => ['style' => 'display:none;'],
  626. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  627. '/div'
  628. ];
  629. $this->assertHtml($expected, $result);
  630. }
  631. /**
  632. * test create() with automatic url generation
  633. *
  634. * @return void
  635. */
  636. public function testCreateAutoUrl()
  637. {
  638. $encoding = strtolower(Configure::read('App.encoding'));
  639. $this->Form->request['action'] = 'delete';
  640. $this->Form->request->here = '/articles/delete/10';
  641. $this->Form->request->base = '';
  642. $result = $this->Form->create($this->article);
  643. $expected = [
  644. 'form' => [
  645. 'method' => 'post', 'action' => '/articles/delete/10',
  646. 'accept-charset' => $encoding
  647. ],
  648. 'div' => ['style' => 'display:none;'],
  649. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  650. '/div'
  651. ];
  652. $this->assertHtml($expected, $result);
  653. $this->article['defaults'] = ['id' => 1];
  654. $this->Form->request->here = '/articles/edit/1';
  655. $this->Form->request['action'] = 'delete';
  656. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  657. $expected = [
  658. 'form' => [
  659. 'method' => 'post',
  660. 'action' => '/articles/edit/1',
  661. 'accept-charset' => $encoding
  662. ],
  663. 'div' => ['style' => 'display:none;'],
  664. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  665. '/div'
  666. ];
  667. $this->assertHtml($expected, $result);
  668. $this->Form->request['action'] = 'add';
  669. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  670. $expected = [
  671. 'form' => [
  672. 'method' => 'post',
  673. 'action' => '/articles/publish/1',
  674. 'accept-charset' => $encoding
  675. ],
  676. 'div' => ['style' => 'display:none;'],
  677. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  678. '/div'
  679. ];
  680. $this->assertHtml($expected, $result);
  681. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  682. $expected = [
  683. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  684. 'div' => ['style' => 'display:none;'],
  685. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  686. '/div'
  687. ];
  688. $this->assertHtml($expected, $result);
  689. $this->Form->request['controller'] = 'Pages';
  690. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  691. $expected = [
  692. 'form' => [
  693. 'method' => 'post', 'action' => '/Pages/signup/1',
  694. 'accept-charset' => $encoding
  695. ],
  696. 'div' => ['style' => 'display:none;'],
  697. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  698. '/div'
  699. ];
  700. $this->assertHtml($expected, $result);
  701. }
  702. /**
  703. * Test create() with no URL (no "action" attribute for <form> tag)
  704. *
  705. * @return void
  706. */
  707. public function testCreateNoUrl()
  708. {
  709. $result = $this->Form->create(false, ['url' => false]);
  710. $expected = [
  711. 'form' => [
  712. 'method' => 'post',
  713. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  714. ],
  715. 'div' => ['style' => 'display:none;'],
  716. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  717. '/div'
  718. ];
  719. $this->assertHtml($expected, $result);
  720. }
  721. /**
  722. * test create() with a custom route
  723. *
  724. * @return void
  725. */
  726. public function testCreateCustomRoute()
  727. {
  728. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  729. $encoding = strtolower(Configure::read('App.encoding'));
  730. $this->Form->request['controller'] = 'users';
  731. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  732. $expected = [
  733. 'form' => [
  734. 'method' => 'post', 'action' => '/login',
  735. 'accept-charset' => $encoding
  736. ],
  737. 'div' => ['style' => 'display:none;'],
  738. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  739. '/div'
  740. ];
  741. $this->assertHtml($expected, $result);
  742. Router::connect(
  743. '/new-article',
  744. ['controller' => 'articles', 'action' => 'myaction'],
  745. ['_name' => 'my-route']
  746. );
  747. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  748. $expected = [
  749. 'form' => [
  750. 'method' => 'post', 'action' => '/new-article',
  751. 'accept-charset' => $encoding,
  752. ],
  753. 'div' => ['style' => 'display:none;'],
  754. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  755. '/div'
  756. ];
  757. $this->assertHtml($expected, $result);
  758. }
  759. /**
  760. * test automatic accept-charset overriding
  761. *
  762. * @return void
  763. */
  764. public function testCreateWithAcceptCharset()
  765. {
  766. $result = $this->Form->create(
  767. $this->article,
  768. [
  769. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  770. ]
  771. );
  772. $expected = [
  773. 'form' => [
  774. 'method' => 'post', 'action' => '/articles',
  775. 'accept-charset' => 'iso-8859-1'
  776. ],
  777. 'div' => ['style' => 'display:none;'],
  778. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  779. '/div'
  780. ];
  781. $this->assertHtml($expected, $result);
  782. }
  783. /**
  784. * Test base form URL when url param is passed with multiple parameters (&)
  785. *
  786. */
  787. public function testCreateQuerystringrequest()
  788. {
  789. $encoding = strtolower(Configure::read('App.encoding'));
  790. $result = $this->Form->create($this->article, [
  791. 'type' => 'post',
  792. 'escape' => false,
  793. 'url' => [
  794. 'controller' => 'controller',
  795. 'action' => 'action',
  796. '?' => ['param1' => 'value1', 'param2' => 'value2']
  797. ]
  798. ]);
  799. $expected = [
  800. 'form' => [
  801. 'method' => 'post',
  802. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  803. 'accept-charset' => $encoding
  804. ],
  805. 'div' => ['style' => 'display:none;'],
  806. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  807. '/div'
  808. ];
  809. $this->assertHtml($expected, $result);
  810. $result = $this->Form->create($this->article, [
  811. 'type' => 'post',
  812. 'url' => [
  813. 'controller' => 'controller',
  814. 'action' => 'action',
  815. '?' => ['param1' => 'value1', 'param2' => 'value2']
  816. ]
  817. ]);
  818. $this->assertHtml($expected, $result);
  819. }
  820. /**
  821. * test that create() doesn't cause errors by multiple id's being in the primary key
  822. * as could happen with multiple select or checkboxes.
  823. *
  824. * @return void
  825. */
  826. public function testCreateWithMultipleIdInData()
  827. {
  828. $encoding = strtolower(Configure::read('App.encoding'));
  829. $this->Form->request->data['Article']['id'] = [1, 2];
  830. $result = $this->Form->create($this->article);
  831. $expected = [
  832. 'form' => [
  833. 'method' => 'post',
  834. 'action' => '/articles/add',
  835. 'accept-charset' => $encoding
  836. ],
  837. 'div' => ['style' => 'display:none;'],
  838. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  839. '/div'
  840. ];
  841. $this->assertHtml($expected, $result);
  842. }
  843. /**
  844. * test that create() doesn't add in extra passed params.
  845. *
  846. * @return void
  847. */
  848. public function testCreatePassedArgs()
  849. {
  850. $encoding = strtolower(Configure::read('App.encoding'));
  851. $this->Form->request->data['Article']['id'] = 1;
  852. $result = $this->Form->create($this->article, [
  853. 'type' => 'post',
  854. 'escape' => false,
  855. 'url' => [
  856. 'action' => 'edit',
  857. 'myparam'
  858. ]
  859. ]);
  860. $expected = [
  861. 'form' => [
  862. 'method' => 'post',
  863. 'action' => '/articles/edit/myparam',
  864. 'accept-charset' => $encoding
  865. ],
  866. 'div' => ['style' => 'display:none;'],
  867. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  868. '/div'
  869. ];
  870. $this->assertHtml($expected, $result);
  871. }
  872. /**
  873. * test creating a get form, and get form inputs.
  874. *
  875. * @return void
  876. */
  877. public function testGetFormCreate()
  878. {
  879. $encoding = strtolower(Configure::read('App.encoding'));
  880. $result = $this->Form->create($this->article, ['type' => 'get']);
  881. $expected = ['form' => [
  882. 'method' => 'get', 'action' => '/articles/add',
  883. 'accept-charset' => $encoding
  884. ]];
  885. $this->assertHtml($expected, $result);
  886. $result = $this->Form->text('title');
  887. $expected = ['input' => [
  888. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  889. ]];
  890. $this->assertHtml($expected, $result);
  891. $result = $this->Form->password('password');
  892. $expected = ['input' => [
  893. 'name' => 'password', 'type' => 'password'
  894. ]];
  895. $this->assertHtml($expected, $result);
  896. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  897. $result = $this->Form->text('user_form');
  898. $expected = ['input' => [
  899. 'name' => 'user_form', 'type' => 'text'
  900. ]];
  901. $this->assertHtml($expected, $result);
  902. }
  903. /**
  904. * test get form, and inputs when the model param is false
  905. *
  906. * @return void
  907. */
  908. public function testGetFormWithFalseModel()
  909. {
  910. $encoding = strtolower(Configure::read('App.encoding'));
  911. $this->Form->request['controller'] = 'contact_test';
  912. $result = $this->Form->create(false, [
  913. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  914. ]);
  915. $expected = ['form' => [
  916. 'method' => 'get', 'action' => '/contact_test/add',
  917. 'accept-charset' => $encoding
  918. ]];
  919. $this->assertHtml($expected, $result);
  920. $result = $this->Form->text('reason');
  921. $expected = [
  922. 'input' => ['type' => 'text', 'name' => 'reason']
  923. ];
  924. $this->assertHtml($expected, $result);
  925. }
  926. /**
  927. * testFormCreateWithSecurity method
  928. *
  929. * Test form->create() with security key.
  930. *
  931. * @return void
  932. */
  933. public function testCreateWithSecurity()
  934. {
  935. $this->Form->request->params['_csrfToken'] = 'testKey';
  936. $encoding = strtolower(Configure::read('App.encoding'));
  937. $result = $this->Form->create($this->article, [
  938. 'url' => '/articles/publish',
  939. ]);
  940. $expected = [
  941. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  942. 'div' => ['style' => 'display:none;'],
  943. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  944. ['input' => [
  945. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  946. ]],
  947. '/div'
  948. ];
  949. $this->assertHtml($expected, $result);
  950. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  951. $expected['form']['id'] = 'MyForm';
  952. $this->assertHtml($expected, $result);
  953. }
  954. /**
  955. * testFormCreateGetNoSecurity method
  956. *
  957. * Test form->create() with no security key as its a get form
  958. *
  959. * @return void
  960. */
  961. public function testCreateEndGetNoSecurity()
  962. {
  963. $this->Form->request->params['_csrfToken'] = 'testKey';
  964. $encoding = strtolower(Configure::read('App.encoding'));
  965. $article = new Article();
  966. $result = $this->Form->create($article, [
  967. 'type' => 'get',
  968. 'url' => '/contacts/add'
  969. ]);
  970. $this->assertNotContains('testKey', $result);
  971. $result = $this->Form->end();
  972. $this->assertNotContains('testKey', $result);
  973. }
  974. /**
  975. * test that create() clears the fields property so it starts fresh
  976. *
  977. * @return void
  978. */
  979. public function testCreateClearingFields()
  980. {
  981. $this->Form->fields = ['model_id'];
  982. $this->Form->create($this->article);
  983. $this->assertEquals([], $this->Form->fields);
  984. }
  985. /**
  986. * Tests form hash generation with model-less data
  987. *
  988. * @return void
  989. */
  990. public function testValidateHashNoModel()
  991. {
  992. $this->Form->request->params['_Token'] = 'foo';
  993. $result = $this->Form->secure(['anything']);
  994. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  995. }
  996. /**
  997. * Tests that hidden fields generated for checkboxes don't get locked
  998. *
  999. * @return void
  1000. */
  1001. public function testNoCheckboxLocking()
  1002. {
  1003. $this->Form->request->params['_Token'] = 'foo';
  1004. $this->assertSame([], $this->Form->fields);
  1005. $this->Form->checkbox('check', ['value' => '1']);
  1006. $this->assertSame($this->Form->fields, ['check']);
  1007. }
  1008. /**
  1009. * testFormSecurityFields method
  1010. *
  1011. * Test generation of secure form hash generation.
  1012. *
  1013. * @return void
  1014. */
  1015. public function testFormSecurityFields()
  1016. {
  1017. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1018. $this->Form->request->params['_Token'] = 'testKey';
  1019. $result = $this->Form->secure($fields);
  1020. $hash = Security::hash(serialize($fields) . Security::salt());
  1021. $hash .= ':' . 'Model.valid';
  1022. $hash = urlencode($hash);
  1023. $tokenDebug = urlencode(json_encode([
  1024. '',
  1025. $fields,
  1026. []
  1027. ]));
  1028. $expected = [
  1029. 'div' => ['style' => 'display:none;'],
  1030. ['input' => [
  1031. 'type' => 'hidden',
  1032. 'name' => '_Token[fields]',
  1033. 'value' => $hash
  1034. ]],
  1035. ['input' => [
  1036. 'type' => 'hidden',
  1037. 'name' => '_Token[unlocked]',
  1038. 'value' => '',
  1039. ]],
  1040. ['input' => [
  1041. 'type' => 'hidden',
  1042. 'name' => '_Token[debug]',
  1043. 'value' => $tokenDebug
  1044. ]],
  1045. '/div'
  1046. ];
  1047. $this->assertHtml($expected, $result);
  1048. }
  1049. /**
  1050. * testFormSecurityFields method
  1051. *
  1052. * Test debug token is not generated if debug is false
  1053. *
  1054. * @return void
  1055. */
  1056. public function testFormSecurityFieldsNoDebugMode()
  1057. {
  1058. Configure::write('debug', false);
  1059. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1060. $this->Form->request->params['_Token'] = 'testKey';
  1061. $result = $this->Form->secure($fields);
  1062. $hash = Security::hash(serialize($fields) . Security::salt());
  1063. $hash .= ':' . 'Model.valid';
  1064. $hash = urlencode($hash);
  1065. $expected = [
  1066. 'div' => ['style' => 'display:none;'],
  1067. ['input' => [
  1068. 'type' => 'hidden',
  1069. 'name' => '_Token[fields]',
  1070. 'value' => $hash
  1071. ]],
  1072. ['input' => [
  1073. 'type' => 'hidden',
  1074. 'name' => '_Token[unlocked]',
  1075. 'value' => '',
  1076. ]],
  1077. '/div'
  1078. ];
  1079. $this->assertHtml($expected, $result);
  1080. }
  1081. /**
  1082. * Tests correct generation of number fields for double and float fields
  1083. *
  1084. * @return void
  1085. */
  1086. public function testTextFieldGenerationForFloats()
  1087. {
  1088. $this->article['schema'] = [
  1089. 'foo' => [
  1090. 'type' => 'float',
  1091. 'null' => false,
  1092. 'default' => null,
  1093. 'length' => 10
  1094. ]
  1095. ];
  1096. $this->Form->create($this->article);
  1097. $result = $this->Form->input('foo');
  1098. $expected = [
  1099. 'div' => ['class' => 'input number'],
  1100. 'label' => ['for' => 'foo'],
  1101. 'Foo',
  1102. '/label',
  1103. ['input' => [
  1104. 'type' => 'number',
  1105. 'name' => 'foo',
  1106. 'id' => 'foo',
  1107. 'step' => 'any'
  1108. ]],
  1109. '/div'
  1110. ];
  1111. $this->assertHtml($expected, $result);
  1112. $result = $this->Form->input('foo', ['step' => 0.5]);
  1113. $expected = [
  1114. 'div' => ['class' => 'input number'],
  1115. 'label' => ['for' => 'foo'],
  1116. 'Foo',
  1117. '/label',
  1118. ['input' => [
  1119. 'type' => 'number',
  1120. 'name' => 'foo',
  1121. 'id' => 'foo',
  1122. 'step' => '0.5'
  1123. ]],
  1124. '/div'
  1125. ];
  1126. $this->assertHtml($expected, $result);
  1127. }
  1128. /**
  1129. * Tests correct generation of number fields for integer fields
  1130. *
  1131. * @return void
  1132. */
  1133. public function testTextFieldTypeNumberGenerationForIntegers()
  1134. {
  1135. TableRegistry::get('Contacts', [
  1136. 'className' => __NAMESPACE__ . '\ContactsTable'
  1137. ]);
  1138. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1139. $result = $this->Form->input('age');
  1140. $expected = [
  1141. 'div' => ['class' => 'input number'],
  1142. 'label' => ['for' => 'age'],
  1143. 'Age',
  1144. '/label',
  1145. ['input' => [
  1146. 'type' => 'number', 'name' => 'age',
  1147. 'id' => 'age'
  1148. ]],
  1149. '/div'
  1150. ];
  1151. $this->assertHtml($expected, $result);
  1152. }
  1153. /**
  1154. * Tests correct generation of file upload fields for binary fields
  1155. *
  1156. * @return void
  1157. */
  1158. public function testFileUploadFieldTypeGenerationForBinaries()
  1159. {
  1160. $table = TableRegistry::get('Contacts', [
  1161. 'className' => __NAMESPACE__ . '\ContactsTable'
  1162. ]);
  1163. $table->schema(['foo' => [
  1164. 'type' => 'binary',
  1165. 'null' => false,
  1166. 'default' => null,
  1167. 'length' => 1024
  1168. ]]);
  1169. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1170. $result = $this->Form->input('foo');
  1171. $expected = [
  1172. 'div' => ['class' => 'input file'],
  1173. 'label' => ['for' => 'foo'],
  1174. 'Foo',
  1175. '/label',
  1176. ['input' => [
  1177. 'type' => 'file', 'name' => 'foo',
  1178. 'id' => 'foo'
  1179. ]],
  1180. '/div'
  1181. ];
  1182. $this->assertHtml($expected, $result);
  1183. }
  1184. /**
  1185. * testFormSecurityMultipleFields method
  1186. *
  1187. * Test secure() with multiple row form. Ensure hash is correct.
  1188. *
  1189. * @return void
  1190. */
  1191. public function testFormSecurityMultipleFields()
  1192. {
  1193. $this->Form->request->params['_Token'] = 'foo';
  1194. $fields = [
  1195. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1196. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1197. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1198. ];
  1199. $result = $this->Form->secure($fields);
  1200. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1201. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1202. $tokenDebug = urlencode(json_encode([
  1203. '',
  1204. $fields,
  1205. []
  1206. ]));
  1207. $expected = [
  1208. 'div' => ['style' => 'display:none;'],
  1209. ['input' => [
  1210. 'type' => 'hidden', 'name' => '_Token[fields]',
  1211. 'value' => $hash
  1212. ]],
  1213. ['input' => [
  1214. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1215. 'value' => ''
  1216. ]],
  1217. ['input' => [
  1218. 'type' => 'hidden', 'name' => '_Token[debug]',
  1219. 'value' => $tokenDebug,
  1220. ]],
  1221. '/div'
  1222. ];
  1223. $this->assertHtml($expected, $result);
  1224. }
  1225. /**
  1226. * testFormSecurityMultipleSubmitButtons
  1227. *
  1228. * test form submit generation and ensure that _Token is only created on end()
  1229. *
  1230. * @return void
  1231. */
  1232. public function testFormSecurityMultipleSubmitButtons()
  1233. {
  1234. $this->Form->request->params['_Token'] = 'testKey';
  1235. $this->Form->create($this->article);
  1236. $this->Form->text('Address.title');
  1237. $this->Form->text('Address.first_name');
  1238. $result = $this->Form->submit('Save', ['name' => 'save']);
  1239. $expected = [
  1240. 'div' => ['class' => 'submit'],
  1241. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1242. '/div',
  1243. ];
  1244. $this->assertHtml($expected, $result);
  1245. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1246. $expected = [
  1247. 'div' => ['class' => 'submit'],
  1248. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1249. '/div',
  1250. ];
  1251. $this->assertHtml($expected, $result);
  1252. $result = $this->Form->end();
  1253. $tokenDebug = urlencode(json_encode([
  1254. '/articles/add',
  1255. [
  1256. 'Address.title',
  1257. 'Address.first_name',
  1258. ],
  1259. ['save', 'cancel']
  1260. ]));
  1261. $expected = [
  1262. 'div' => ['style' => 'display:none;'],
  1263. ['input' => [
  1264. 'type' => 'hidden',
  1265. 'name' => '_Token[fields]',
  1266. 'value'
  1267. ]],
  1268. ['input' => [
  1269. 'type' => 'hidden',
  1270. 'name' => '_Token[unlocked]',
  1271. 'value' => 'cancel%7Csave'
  1272. ]],
  1273. ['input' => [
  1274. 'type' => 'hidden', 'name' => '_Token[debug]',
  1275. 'value' => $tokenDebug,
  1276. ]],
  1277. '/div'
  1278. ];
  1279. $this->assertHtml($expected, $result);
  1280. }
  1281. /**
  1282. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1283. *
  1284. * @return void
  1285. */
  1286. public function testSecurityButtonNestedNamed()
  1287. {
  1288. $key = 'testKey';
  1289. $this->Form->request->params['_csrfToken'] = $key;
  1290. $this->Form->create('Addresses');
  1291. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1292. $result = $this->Form->unlockField();
  1293. $this->assertEquals(['Address.button'], $result);
  1294. }
  1295. /**
  1296. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1297. *
  1298. * @return void
  1299. */
  1300. public function testSecuritySubmitNestedNamed()
  1301. {
  1302. $this->Form->request->params['_Token'] = 'testKey';
  1303. $this->Form->create($this->article);
  1304. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1305. $result = $this->Form->unlockField();
  1306. $this->assertEquals(['Address.button'], $result);
  1307. }
  1308. /**
  1309. * Test that the correct fields are unlocked for image submits with no names.
  1310. *
  1311. * @return void
  1312. */
  1313. public function testSecuritySubmitImageNoName()
  1314. {
  1315. $key = 'testKey';
  1316. $this->Form->request->params['_Token'] = 'testKey';
  1317. $this->Form->create(false);
  1318. $result = $this->Form->submit('save.png');
  1319. $expected = [
  1320. 'div' => ['class' => 'submit'],
  1321. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1322. '/div'
  1323. ];
  1324. $this->assertHtml($expected, $result);
  1325. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1326. }
  1327. /**
  1328. * Test that the correct fields are unlocked for image submits with names.
  1329. *
  1330. * @return void
  1331. */
  1332. public function testSecuritySubmitImageName()
  1333. {
  1334. $this->Form->request->params['_Token'] = 'testKey';
  1335. $this->Form->create(null);
  1336. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1337. $expected = [
  1338. 'div' => ['class' => 'submit'],
  1339. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1340. '/div'
  1341. ];
  1342. $this->assertHtml($expected, $result);
  1343. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1344. }
  1345. /**
  1346. * testFormSecurityMultipleInputFields method
  1347. *
  1348. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1349. *
  1350. * @return void
  1351. */
  1352. public function testFormSecurityMultipleInputFields()
  1353. {
  1354. $this->Form->request->params['_Token'] = 'testKey';
  1355. $this->Form->create();
  1356. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1357. $this->Form->input('Addresses.0.title');
  1358. $this->Form->input('Addresses.0.first_name');
  1359. $this->Form->input('Addresses.0.last_name');
  1360. $this->Form->input('Addresses.0.address');
  1361. $this->Form->input('Addresses.0.city');
  1362. $this->Form->input('Addresses.0.phone');
  1363. $this->Form->input('Addresses.0.primary', ['type' => 'checkbox']);
  1364. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1365. $this->Form->input('Addresses.1.title');
  1366. $this->Form->input('Addresses.1.first_name');
  1367. $this->Form->input('Addresses.1.last_name');
  1368. $this->Form->input('Addresses.1.address');
  1369. $this->Form->input('Addresses.1.city');
  1370. $this->Form->input('Addresses.1.phone');
  1371. $this->Form->input('Addresses.1.primary', ['type' => 'checkbox']);
  1372. $result = $this->Form->secure($this->Form->fields);
  1373. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1374. $tokenDebug = urlencode(json_encode([
  1375. '/articles/add',
  1376. [
  1377. 'Addresses.0.id' => '123456',
  1378. 'Addresses.0.title',
  1379. 'Addresses.0.first_name',
  1380. 'Addresses.0.last_name',
  1381. 'Addresses.0.address',
  1382. 'Addresses.0.city',
  1383. 'Addresses.0.phone',
  1384. 'Addresses.0.primary',
  1385. 'Addresses.1.id' => '654321',
  1386. 'Addresses.1.title',
  1387. 'Addresses.1.first_name',
  1388. 'Addresses.1.last_name',
  1389. 'Addresses.1.address',
  1390. 'Addresses.1.city',
  1391. 'Addresses.1.phone',
  1392. 'Addresses.1.primary',
  1393. ],
  1394. []
  1395. ]));
  1396. $expected = [
  1397. 'div' => ['style' => 'display:none;'],
  1398. ['input' => [
  1399. 'type' => 'hidden', 'name' => '_Token[fields]',
  1400. 'value' => $hash
  1401. ]],
  1402. ['input' => [
  1403. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1404. 'value' => ''
  1405. ]],
  1406. ['input' => [
  1407. 'type' => 'hidden', 'name' => '_Token[debug]',
  1408. 'value' => $tokenDebug,
  1409. ]],
  1410. '/div'
  1411. ];
  1412. $this->assertHtml($expected, $result);
  1413. }
  1414. /**
  1415. * Test form security with Model.field.0 style inputs
  1416. *
  1417. * @return void
  1418. */
  1419. public function testFormSecurityArrayFields()
  1420. {
  1421. $this->Form->request->params['_Token'] = 'testKey';
  1422. $this->Form->create();
  1423. $this->Form->text('Address.primary.1');
  1424. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1425. $this->Form->text('Address.secondary.1.0');
  1426. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1427. }
  1428. /**
  1429. * testFormSecurityMultipleInputDisabledFields method
  1430. *
  1431. * test secure form generation with multiple records and disabled fields.
  1432. *
  1433. * @return void
  1434. */
  1435. public function testFormSecurityMultipleInputDisabledFields()
  1436. {
  1437. $this->Form->request->params['_Token'] = [
  1438. 'unlockedFields' => ['first_name', 'address']
  1439. ];
  1440. $this->Form->create();
  1441. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1442. $this->Form->text('Addresses.0.title');
  1443. $this->Form->text('Addresses.0.first_name');
  1444. $this->Form->text('Addresses.0.last_name');
  1445. $this->Form->text('Addresses.0.address');
  1446. $this->Form->text('Addresses.0.city');
  1447. $this->Form->text('Addresses.0.phone');
  1448. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1449. $this->Form->text('Addresses.1.title');
  1450. $this->Form->text('Addresses.1.first_name');
  1451. $this->Form->text('Addresses.1.last_name');
  1452. $this->Form->text('Addresses.1.address');
  1453. $this->Form->text('Addresses.1.city');
  1454. $this->Form->text('Addresses.1.phone');
  1455. $result = $this->Form->secure($this->Form->fields);
  1456. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1457. $tokenDebug = urlencode(json_encode([
  1458. '/articles/add',
  1459. [
  1460. 'Addresses.0.id' => '123456',
  1461. 'Addresses.0.title',
  1462. 'Addresses.0.last_name',
  1463. 'Addresses.0.city',
  1464. 'Addresses.0.phone',
  1465. 'Addresses.1.id' => '654321',
  1466. 'Addresses.1.title',
  1467. 'Addresses.1.last_name',
  1468. 'Addresses.1.city',
  1469. 'Addresses.1.phone'
  1470. ],
  1471. [
  1472. 'first_name',
  1473. 'address'
  1474. ]
  1475. ]));
  1476. $expected = [
  1477. 'div' => ['style' => 'display:none;'],
  1478. ['input' => [
  1479. 'type' => 'hidden',
  1480. 'name' => '_Token[fields]',
  1481. 'value' => $hash
  1482. ]],
  1483. ['input' => [
  1484. 'type' => 'hidden',
  1485. 'name' => '_Token[unlocked]',
  1486. 'value' => 'address%7Cfirst_name',
  1487. ]],
  1488. ['input' => [
  1489. 'type' => 'hidden', 'name' => '_Token[debug]',
  1490. 'value' => $tokenDebug,
  1491. ]],
  1492. '/div'
  1493. ];
  1494. $this->assertHtml($expected, $result);
  1495. }
  1496. /**
  1497. * testFormSecurityInputDisabledFields method
  1498. *
  1499. * Test single record form with disabled fields.
  1500. *
  1501. * @return void
  1502. */
  1503. public function testFormSecurityInputUnlockedFields()
  1504. {
  1505. $this->Form->request['_Token'] = [
  1506. 'unlockedFields' => ['first_name', 'address']
  1507. ];
  1508. $this->Form->create();
  1509. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1510. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1511. $this->Form->text('Addresses.title');
  1512. $this->Form->text('Addresses.first_name');
  1513. $this->Form->text('Addresses.last_name');
  1514. $this->Form->text('Addresses.address');
  1515. $this->Form->text('Addresses.city');
  1516. $this->Form->text('Addresses.phone');
  1517. $result = $this->Form->fields;
  1518. $expected = [
  1519. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1520. 'Addresses.city', 'Addresses.phone'
  1521. ];
  1522. $this->assertEquals($expected, $result);
  1523. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1524. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1525. $tokenDebug = urlencode(json_encode([
  1526. '/articles/add',
  1527. [
  1528. 'Addresses.id' => '123456',
  1529. 'Addresses.title',
  1530. 'Addresses.last_name',
  1531. 'Addresses.city',
  1532. 'Addresses.phone'
  1533. ],
  1534. [
  1535. 'first_name',
  1536. 'address'
  1537. ]
  1538. ]));
  1539. $expected = [
  1540. 'div' => ['style' => 'display:none;'],
  1541. ['input' => [
  1542. 'type' => 'hidden',
  1543. 'name' => '_Token[fields]',
  1544. 'value' => $hash,
  1545. 'data-foo' => 'bar',
  1546. ]],
  1547. ['input' => [
  1548. 'type' => 'hidden',
  1549. 'name' => '_Token[unlocked]',
  1550. 'value' => 'address%7Cfirst_name',
  1551. 'data-foo' => 'bar',
  1552. ]],
  1553. ['input' => [
  1554. 'type' => 'hidden', 'name' => '_Token[debug]',
  1555. 'value' => $tokenDebug,
  1556. 'data-foo' => 'bar'
  1557. ]],
  1558. '/div'
  1559. ];
  1560. $this->assertHtml($expected, $result);
  1561. }
  1562. /**
  1563. * testFormSecurityInputUnlockedFieldsDebugSecurityTrue method
  1564. *
  1565. * Test single record form with debugSecurity param.
  1566. *
  1567. * @return void
  1568. */
  1569. public function testFormSecurityInputUnlockedFieldsDebugSecurityTrue()
  1570. {
  1571. $this->Form->request['_Token'] = [
  1572. 'unlockedFields' => ['first_name', 'address']
  1573. ];
  1574. $this->Form->create();
  1575. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1576. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1577. $this->Form->text('Addresses.title');
  1578. $this->Form->text('Addresses.first_name');
  1579. $this->Form->text('Addresses.last_name');
  1580. $this->Form->text('Addresses.address');
  1581. $this->Form->text('Addresses.city');
  1582. $this->Form->text('Addresses.phone');
  1583. $result = $this->Form->fields;
  1584. $expected = [
  1585. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1586. 'Addresses.city', 'Addresses.phone'
  1587. ];
  1588. $this->assertEquals($expected, $result);
  1589. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1590. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1591. $tokenDebug = urlencode(json_encode([
  1592. '/articles/add',
  1593. [
  1594. 'Addresses.id' => '123456',
  1595. 'Addresses.title',
  1596. 'Addresses.last_name',
  1597. 'Addresses.city',
  1598. 'Addresses.phone'
  1599. ],
  1600. [
  1601. 'first_name',
  1602. 'address'
  1603. ]
  1604. ]));
  1605. $expected = [
  1606. 'div' => ['style' => 'display:none;'],
  1607. ['input' => [
  1608. 'type' => 'hidden',
  1609. 'name' => '_Token[fields]',
  1610. 'value' => $hash,
  1611. 'data-foo' => 'bar',
  1612. ]],
  1613. ['input' => [
  1614. 'type' => 'hidden',
  1615. 'name' => '_Token[unlocked]',
  1616. 'value' => 'address%7Cfirst_name',
  1617. 'data-foo' => 'bar',
  1618. ]],
  1619. ['input' => [
  1620. 'type' => 'hidden', 'name' => '_Token[debug]',
  1621. 'value' => $tokenDebug,
  1622. 'data-foo' => 'bar'
  1623. ]],
  1624. '/div'
  1625. ];
  1626. $this->assertHtml($expected, $result);
  1627. }
  1628. /**
  1629. * testFormSecurityInputUnlockedFieldsDebugSecurityFalse method
  1630. *
  1631. * Debug is false, debugSecurity is true -> no debug
  1632. *
  1633. * @return void
  1634. */
  1635. public function testFormSecurityInputUnlockedFieldsDebugSecurityDebugFalse()
  1636. {
  1637. $this->Form->request['_Token'] = [
  1638. 'unlockedFields' => ['first_name', 'address']
  1639. ];
  1640. $this->Form->create();
  1641. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1642. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1643. $this->Form->text('Addresses.title');
  1644. $this->Form->text('Addresses.first_name');
  1645. $this->Form->text('Addresses.last_name');
  1646. $this->Form->text('Addresses.address');
  1647. $this->Form->text('Addresses.city');
  1648. $this->Form->text('Addresses.phone');
  1649. $result = $this->Form->fields;
  1650. $expected = [
  1651. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1652. 'Addresses.city', 'Addresses.phone'
  1653. ];
  1654. $this->assertEquals($expected, $result);
  1655. Configure::write('debug', false);
  1656. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1657. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1658. $expected = [
  1659. 'div' => ['style' => 'display:none;'],
  1660. ['input' => [
  1661. 'type' => 'hidden',
  1662. 'name' => '_Token[fields]',
  1663. 'value' => $hash,
  1664. 'data-foo' => 'bar',
  1665. ]],
  1666. ['input' => [
  1667. 'type' => 'hidden',
  1668. 'name' => '_Token[unlocked]',
  1669. 'value' => 'address%7Cfirst_name',
  1670. 'data-foo' => 'bar',
  1671. ]],
  1672. '/div'
  1673. ];
  1674. $this->assertHtml($expected, $result);
  1675. }
  1676. /**
  1677. * testFormSecurityInputUnlockedFieldsDebugSecurityFalse method
  1678. *
  1679. * Test single record form with debugSecurity param.
  1680. *
  1681. * @return void
  1682. */
  1683. public function testFormSecurityInputUnlockedFieldsDebugSecurityFalse()
  1684. {
  1685. $this->Form->request['_Token'] = [
  1686. 'unlockedFields' => ['first_name', 'address']
  1687. ];
  1688. $this->Form->create();
  1689. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1690. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1691. $this->Form->text('Addresses.title');
  1692. $this->Form->text('Addresses.first_name');
  1693. $this->Form->text('Addresses.last_name');
  1694. $this->Form->text('Addresses.address');
  1695. $this->Form->text('Addresses.city');
  1696. $this->Form->text('Addresses.phone');
  1697. $result = $this->Form->fields;
  1698. $expected = [
  1699. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1700. 'Addresses.city', 'Addresses.phone'
  1701. ];
  1702. $this->assertEquals($expected, $result);
  1703. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1704. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1705. $expected = [
  1706. 'div' => ['style' => 'display:none;'],
  1707. ['input' => [
  1708. 'type' => 'hidden',
  1709. 'name' => '_Token[fields]',
  1710. 'value' => $hash,
  1711. 'data-foo' => 'bar',
  1712. ]],
  1713. ['input' => [
  1714. 'type' => 'hidden',
  1715. 'name' => '_Token[unlocked]',
  1716. 'value' => 'address%7Cfirst_name',
  1717. 'data-foo' => 'bar',
  1718. ]],
  1719. '/div'
  1720. ];
  1721. $this->assertHtml($expected, $result);
  1722. }
  1723. /**
  1724. * test securing inputs with custom name attributes.
  1725. *
  1726. * @return void
  1727. */
  1728. public function testFormSecureWithCustomNameAttribute()
  1729. {
  1730. $this->Form->request->params['_Token'] = 'testKey';
  1731. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1732. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1733. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1734. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1735. }
  1736. /**
  1737. * testFormSecuredInput method
  1738. *
  1739. * Test generation of entire secure form, assertions made on input() output.
  1740. *
  1741. * @return void
  1742. */
  1743. public function testFormSecuredInput()
  1744. {
  1745. $this->Form->request->params['_csrfToken'] = 'testKey';
  1746. $this->Form->request->params['_Token'] = 'stuff';
  1747. $this->article['schema'] = [
  1748. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1749. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1750. ];
  1751. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1752. $encoding = strtolower(Configure::read('App.encoding'));
  1753. $expected = [
  1754. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1755. 'div' => ['style' => 'display:none;'],
  1756. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1757. ['input' => [
  1758. 'type' => 'hidden',
  1759. 'name' => '_csrfToken',
  1760. 'value' => 'testKey'
  1761. ]],
  1762. '/div'
  1763. ];
  1764. $this->assertHtml($expected, $result);
  1765. $result = $this->Form->input('ratio');
  1766. $expected = [
  1767. 'div' => ['class'],
  1768. 'label' => ['for'],
  1769. 'Ratio',
  1770. '/label',
  1771. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1772. '/div',
  1773. ];
  1774. $this->assertHtml($expected, $result);
  1775. $result = $this->Form->input('population');
  1776. $expected = [
  1777. 'div' => ['class'],
  1778. 'label' => ['for'],
  1779. 'Population',
  1780. '/label',
  1781. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1782. '/div',
  1783. ];
  1784. $this->assertHtml($expected, $result);
  1785. $result = $this->Form->input('published', ['type' => 'text']);
  1786. $expected = [
  1787. 'div' => ['class' => 'input text'],
  1788. 'label' => ['for' => 'published'],
  1789. 'Published',
  1790. '/label',
  1791. ['input' => [
  1792. 'type' => 'text',
  1793. 'name' => 'published',
  1794. 'id' => 'published'
  1795. ]],
  1796. '/div'
  1797. ];
  1798. $this->assertHtml($expected, $result);
  1799. $result = $this->Form->input('other', ['type' => 'text']);
  1800. $expected = [
  1801. 'div' => ['class' => 'input text'],
  1802. 'label' => ['for' => 'other'],
  1803. 'Other',
  1804. '/label',
  1805. ['input' => [
  1806. 'type' => 'text',
  1807. 'name' => 'other',
  1808. 'id',
  1809. ]],
  1810. '/div'
  1811. ];
  1812. $this->assertHtml($expected, $result);
  1813. $result = $this->Form->hidden('stuff');
  1814. $expected = [
  1815. 'input' => [
  1816. 'type' => 'hidden',
  1817. 'name' => 'stuff'
  1818. ]
  1819. ];
  1820. $this->assertHtml($expected, $result);
  1821. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1822. $expected = ['input' => [
  1823. 'type' => 'hidden',
  1824. 'name' => 'hidden',
  1825. 'value' => '0'
  1826. ]];
  1827. $this->assertHtml($expected, $result);
  1828. $result = $this->Form->input('something', ['type' => 'checkbox']);
  1829. $expected = [
  1830. 'div' => ['class' => 'input checkbox'],
  1831. ['input' => [
  1832. 'type' => 'hidden',
  1833. 'name' => 'something',
  1834. 'value' => '0'
  1835. ]],
  1836. 'label' => ['for' => 'something'],
  1837. ['input' => [
  1838. 'type' => 'checkbox',
  1839. 'name' => 'something',
  1840. 'value' => '1',
  1841. 'id' => 'something'
  1842. ]],
  1843. 'Something',
  1844. '/label',
  1845. '/div'
  1846. ];
  1847. $this->assertHtml($expected, $result);
  1848. $result = $this->Form->fields;
  1849. $expectedFields = [
  1850. 'ratio',
  1851. 'population',
  1852. 'published',
  1853. 'other',
  1854. 'stuff' => '',
  1855. 'hidden' => '0',
  1856. 'something'
  1857. ];
  1858. $this->assertEquals($expectedFields, $result);
  1859. $result = $this->Form->secure($this->Form->fields);
  1860. $tokenDebug = urlencode(json_encode([
  1861. '/articles/add',
  1862. $expectedFields,
  1863. []
  1864. ]));
  1865. $expected = [
  1866. 'div' => ['style' => 'display:none;'],
  1867. ['input' => [
  1868. 'type' => 'hidden',
  1869. 'name' => '_Token[fields]',
  1870. 'value'
  1871. ]],
  1872. ['input' => [
  1873. 'type' => 'hidden',
  1874. 'name' => '_Token[unlocked]',
  1875. 'value' => ''
  1876. ]],
  1877. ['input' => [
  1878. 'type' => 'hidden', 'name' => '_Token[debug]',
  1879. 'value' => $tokenDebug,
  1880. ]],
  1881. '/div'
  1882. ];
  1883. $this->assertHtml($expected, $result);
  1884. }
  1885. /**
  1886. * Test secured inputs with custom names.
  1887. *
  1888. * @return void
  1889. */
  1890. public function testSecuredInputCustomName()
  1891. {
  1892. $this->Form->request->params['_Token'] = 'testKey';
  1893. $this->assertEquals([], $this->Form->fields);
  1894. $this->Form->text('text_input', [
  1895. 'name' => 'Option[General.default_role]',
  1896. ]);
  1897. $expected = ['Option.General.default_role'];
  1898. $this->assertEquals($expected, $this->Form->fields);
  1899. $this->Form->select('select_box', [1, 2], [
  1900. 'name' => 'Option[General.select_role]',
  1901. ]);
  1902. $expected[] = 'Option.General.select_role';
  1903. $this->assertEquals($expected, $this->Form->fields);
  1904. $this->Form->text('other.things[]');
  1905. $expected[] = 'other.things';
  1906. $this->assertEquals($expected, $this->Form->fields);
  1907. }
  1908. /**
  1909. * Test that a hidden field followed by a visible field
  1910. * undoes the hidden field locking.
  1911. *
  1912. * @return void
  1913. */
  1914. public function testSecuredInputDuplicate()
  1915. {
  1916. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  1917. $this->assertEquals([], $this->Form->fields);
  1918. $this->Form->input('text_val', [
  1919. 'type' => 'hidden',
  1920. 'value' => 'some text',
  1921. ]);
  1922. $expected = ['text_val' => 'some text'];
  1923. $this->assertEquals($expected, $this->Form->fields);
  1924. $this->Form->input('text_val', [
  1925. 'type' => 'text',
  1926. ]);
  1927. $expected = ['text_val'];
  1928. $this->assertEquals($expected, $this->Form->fields);
  1929. }
  1930. /**
  1931. * Tests that the correct keys are added to the field hash index
  1932. *
  1933. * @return void
  1934. */
  1935. public function testFormSecuredFileInput()
  1936. {
  1937. $this->assertEquals([], $this->Form->fields);
  1938. $this->Form->file('Attachment.file');
  1939. $expected = [
  1940. 'Attachment.file.name', 'Attachment.file.type',
  1941. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1942. 'Attachment.file.size'
  1943. ];
  1944. $this->assertEquals($expected, $this->Form->fields);
  1945. }
  1946. /**
  1947. * test that multiple selects keys are added to field hash
  1948. *
  1949. * @return void
  1950. */
  1951. public function testFormSecuredMultipleSelect()
  1952. {
  1953. $this->Form->request->params['_csrfToken'] = 'testKey';
  1954. $this->assertEquals([], $this->Form->fields);
  1955. $options = ['1' => 'one', '2' => 'two'];
  1956. $this->Form->select('Model.select', $options);
  1957. $expected = ['Model.select'];
  1958. $this->assertEquals($expected, $this->Form->fields);
  1959. $this->Form->fields = [];
  1960. $this->Form->select('Model.select', $options, ['multiple' => true]);
  1961. $this->assertEquals($expected, $this->Form->fields);
  1962. }
  1963. /**
  1964. * testFormSecuredRadio method
  1965. *
  1966. * @return void
  1967. */
  1968. public function testFormSecuredRadio()
  1969. {
  1970. $this->Form->request->params['_Token'] = 'testKey';
  1971. $this->assertEquals([], $this->Form->fields);
  1972. $options = ['1' => 'option1', '2' => 'option2'];
  1973. $this->Form->radio('Test.test', $options);
  1974. $expected = ['Test.test'];
  1975. $this->assertEquals($expected, $this->Form->fields);
  1976. $this->Form->radio('Test.all', $options, [
  1977. 'disabled' => ['option1', 'option2']
  1978. ]);
  1979. $expected = ['Test.test', 'Test.all' => ''];
  1980. $this->assertEquals($expected, $this->Form->fields);
  1981. $this->Form->radio('Test.some', $options, [
  1982. 'disabled' => ['option1']
  1983. ]);
  1984. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  1985. $this->assertEquals($expected, $this->Form->fields);
  1986. }
  1987. /**
  1988. * Test that when disabled is in a list based attribute array it works.
  1989. *
  1990. * @return void
  1991. */
  1992. public function testFormSecuredAndDisabledNotAssoc()
  1993. {
  1994. $this->Form->request->params['_Token'] = 'testKey';
  1995. $this->Form->select('Model.select', [1, 2], ['disabled']);
  1996. $this->Form->checkbox('Model.checkbox', ['disabled']);
  1997. $this->Form->text('Model.text', ['disabled']);
  1998. $this->Form->textarea('Model.textarea', ['disabled']);
  1999. $this->Form->password('Model.password', ['disabled']);
  2000. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2001. $expected = [
  2002. 'Model.radio' => ''
  2003. ];
  2004. $this->assertEquals($expected, $this->Form->fields);
  2005. }
  2006. /**
  2007. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  2008. * hashing.
  2009. *
  2010. * @return void
  2011. */
  2012. public function testFormSecuredAndDisabled()
  2013. {
  2014. $this->Form->request->params['_Token'] = 'testKey';
  2015. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2016. $this->Form->text('Model.text', ['disabled' => true]);
  2017. $this->Form->password('Model.text', ['disabled' => true]);
  2018. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2019. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2020. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2021. $this->Form->year('Model.year', ['disabled' => true]);
  2022. $this->Form->month('Model.month', ['disabled' => true]);
  2023. $this->Form->day('Model.day', ['disabled' => true]);
  2024. $this->Form->hour('Model.hour', ['disabled' => true]);
  2025. $this->Form->minute('Model.minute', ['disabled' => true]);
  2026. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2027. $expected = [
  2028. 'Model.radio' => ''
  2029. ];
  2030. $this->assertEquals($expected, $this->Form->fields);
  2031. }
  2032. /**
  2033. * testDisableSecurityUsingForm method
  2034. *
  2035. * @return void
  2036. */
  2037. public function testDisableSecurityUsingForm()
  2038. {
  2039. $this->Form->request['_Token'] = [
  2040. 'disabledFields' => []
  2041. ];
  2042. $this->Form->create();
  2043. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2044. $this->Form->text('Addresses.title');
  2045. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2046. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2047. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2048. $result = $this->Form->fields;
  2049. $expected = [
  2050. 'Addresses.id' => '123456', 'Addresses.title',
  2051. ];
  2052. $this->assertEquals($expected, $result);
  2053. }
  2054. /**
  2055. * test disableField
  2056. *
  2057. * @return void
  2058. */
  2059. public function testUnlockFieldAddsToList()
  2060. {
  2061. $this->Form->request['_Token'] = [
  2062. 'unlockedFields' => []
  2063. ];
  2064. $this->Form->unlockField('Contact.name');
  2065. $this->Form->text('Contact.name');
  2066. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2067. $this->assertEquals([], $this->Form->fields);
  2068. }
  2069. /**
  2070. * test unlockField removing from fields array.
  2071. *
  2072. * @return void
  2073. */
  2074. public function testUnlockFieldRemovingFromFields()
  2075. {
  2076. $this->Form->request['_Token'] = [
  2077. 'unlockedFields' => []
  2078. ];
  2079. $this->Form->create($this->article);
  2080. $this->Form->hidden('Article.id', ['value' => 1]);
  2081. $this->Form->text('Article.title');
  2082. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2083. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  2084. $this->Form->unlockField('Article.title');
  2085. $this->Form->unlockField('Article.id');
  2086. $this->assertEquals([], $this->Form->fields);
  2087. }
  2088. /**
  2089. * test reset unlockFields, when create new form.
  2090. *
  2091. * @return void
  2092. */
  2093. public function testResetUnlockFields()
  2094. {
  2095. $this->Form->request['_Token'] = [
  2096. 'key' => 'testKey',
  2097. 'unlockedFields' => []
  2098. ];
  2099. $this->Form->unlockField('Contact.id');
  2100. $this->Form->create('Contact');
  2101. $this->Form->hidden('Contact.id', ['value' => 1]);
  2102. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2103. $this->Form->end();
  2104. $this->Form->create('Contact');
  2105. $this->Form->hidden('Contact.id', ['value' => 1]);
  2106. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2107. }
  2108. /**
  2109. * Test that only the path + query elements of a form's URL show up in their hash.
  2110. *
  2111. * @return void
  2112. */
  2113. public function testSecuredFormUrlIgnoresHost()
  2114. {
  2115. $this->Form->request['_Token'] = ['key' => 'testKey'];
  2116. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  2117. $this->Form->create($this->article, [
  2118. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2119. ]);
  2120. $result = $this->Form->secure();
  2121. $this->assertContains($expected, $result);
  2122. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2123. $result = $this->Form->secure();
  2124. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2125. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2126. $result = $this->Form->secure();
  2127. $this->assertContains($expected, $result, 'URL path + query should work.');
  2128. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2129. $result = $this->Form->secure();
  2130. $this->assertNotContains($expected, $result, 'URL is different');
  2131. }
  2132. /**
  2133. * Test that URL, HTML and identifer show up in their hashs.
  2134. *
  2135. * @return void
  2136. */
  2137. public function testSecuredFormUrlHasHtmlAndIdentifer()
  2138. {
  2139. $this->Form->request['_Token'] = ['key' => 'testKey'];
  2140. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  2141. $res = $this->Form->create($this->article, [
  2142. 'url' => [
  2143. 'controller' => 'articles',
  2144. 'action' => 'view',
  2145. '?' => [
  2146. 'page' => 1,
  2147. 'limit' => 10,
  2148. 'html' => '<>"',
  2149. ],
  2150. '#' => 'result',
  2151. ],
  2152. ]);
  2153. $result = $this->Form->secure();
  2154. $this->assertContains($expected, $result);
  2155. $this->Form->create($this->article, [
  2156. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2157. ]);
  2158. $result = $this->Form->secure();
  2159. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2160. $this->Form->create($this->article, [
  2161. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2162. ]);
  2163. $result = $this->Form->secure();
  2164. $this->assertContains($expected, $result, 'URL path + query should work.');
  2165. }
  2166. /**
  2167. * test error message display
  2168. *
  2169. * @return void
  2170. */
  2171. public function testErrorMessageDisplay()
  2172. {
  2173. $this->article['errors'] = [
  2174. 'Article' => [
  2175. 'title' => 'error message',
  2176. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2177. ]
  2178. ];
  2179. $this->Form->create($this->article);
  2180. $result = $this->Form->input('Article.title');
  2181. $expected = [
  2182. 'div' => ['class' => 'input text error'],
  2183. 'label' => ['for' => 'article-title'],
  2184. 'Title',
  2185. '/label',
  2186. 'input' => [
  2187. 'type' => 'text', 'name' => 'Article[title]',
  2188. 'id' => 'article-title', 'class' => 'form-error'
  2189. ],
  2190. ['div' => ['class' => 'error-message']],
  2191. 'error message',
  2192. '/div',
  2193. '/div'
  2194. ];
  2195. $this->assertHtml($expected, $result);
  2196. $result = $this->Form->input('Article.title', [
  2197. 'templates' => [
  2198. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2199. ]
  2200. ]);
  2201. $expected = [
  2202. 'div' => ['class' => 'input text error'],
  2203. 'label' => ['for' => 'article-title'],
  2204. 'Title',
  2205. '/label',
  2206. 'input' => [
  2207. 'type' => 'text', 'name' => 'Article[title]',
  2208. 'id' => 'article-title', 'class' => 'form-error'
  2209. ],
  2210. '/div'
  2211. ];
  2212. $this->assertHtml($expected, $result);
  2213. $result = $this->Form->input('Article.content');
  2214. $expected = [
  2215. 'div' => ['class' => 'input text error'],
  2216. 'label' => ['for' => 'article-content'],
  2217. 'Content',
  2218. '/label',
  2219. 'input' => [
  2220. 'type' => 'text', 'name' => 'Article[content]',
  2221. 'id' => 'article-content', 'class' => 'form-error'
  2222. ],
  2223. ['div' => ['class' => 'error-message']],
  2224. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2225. '/div',
  2226. '/div'
  2227. ];
  2228. $this->assertHtml($expected, $result);
  2229. $result = $this->Form->input('Article.content', ['error' => ['escape' => true]]);
  2230. $expected = [
  2231. 'div' => ['class' => 'input text error'],
  2232. 'label' => ['for' => 'article-content'],
  2233. 'Content',
  2234. '/label',
  2235. 'input' => [
  2236. 'type' => 'text', 'name' => 'Article[content]',
  2237. 'id' => 'article-content', 'class' => 'form-error'
  2238. ],
  2239. ['div' => ['class' => 'error-message']],
  2240. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2241. '/div',
  2242. '/div'
  2243. ];
  2244. $this->assertHtml($expected, $result);
  2245. $result = $this->Form->input('Article.content', ['error' => ['escape' => false]]);
  2246. $expected = [
  2247. 'div' => ['class' => 'input text error'],
  2248. 'label' => ['for' => 'article-content'],
  2249. 'Content',
  2250. '/label',
  2251. 'input' => [
  2252. 'type' => 'text', 'name' => 'Article[content]',
  2253. 'id' => 'article-content', 'class' => 'form-error'
  2254. ],
  2255. ['div' => ['class' => 'error-message']],
  2256. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2257. '/div',
  2258. '/div'
  2259. ];
  2260. $this->assertHtml($expected, $result);
  2261. }
  2262. /**
  2263. * Test validation errors, when validation message is an empty string.
  2264. *
  2265. * @return void
  2266. */
  2267. public function testEmptyErrorValidation()
  2268. {
  2269. $this->article['errors'] = [
  2270. 'Article' => ['title' => '']
  2271. ];
  2272. $this->Form->create($this->article);
  2273. $result = $this->Form->input('Article.title');
  2274. $expected = [
  2275. 'div' => ['class' => 'input text error'],
  2276. 'label' => ['for' => 'article-title'],
  2277. 'Title',
  2278. '/label',
  2279. 'input' => [
  2280. 'type' => 'text', 'name' => 'Article[title]',
  2281. 'id' => 'article-title', 'class' => 'form-error'
  2282. ],
  2283. ['div' => ['class' => 'error-message']],
  2284. [],
  2285. '/div',
  2286. '/div'
  2287. ];
  2288. $this->assertHtml($expected, $result);
  2289. }
  2290. /**
  2291. * Test validation errors, when calling input() overriding validation message by an empty string.
  2292. *
  2293. * @return void
  2294. */
  2295. public function testEmptyInputErrorValidation()
  2296. {
  2297. $this->article['errors'] = [
  2298. 'Article' => ['title' => 'error message']
  2299. ];
  2300. $this->Form->create($this->article);
  2301. $result = $this->Form->input('Article.title', ['error' => '']);
  2302. $expected = [
  2303. 'div' => ['class' => 'input text error'],
  2304. 'label' => ['for' => 'article-title'],
  2305. 'Title',
  2306. '/label',
  2307. 'input' => [
  2308. 'type' => 'text', 'name' => 'Article[title]',
  2309. 'id' => 'article-title', 'class' => 'form-error'
  2310. ],
  2311. ['div' => ['class' => 'error-message']],
  2312. [],
  2313. '/div',
  2314. '/div'
  2315. ];
  2316. $this->assertHtml($expected, $result);
  2317. }
  2318. /**
  2319. * Test validation errors, when calling input() overriding validation messages
  2320. *
  2321. * @return void
  2322. */
  2323. public function testInputErrorMessage()
  2324. {
  2325. $this->article['errors'] = [
  2326. 'title' => ['error message']
  2327. ];
  2328. $this->Form->create($this->article);
  2329. $result = $this->Form->input('title', [
  2330. 'error' => 'Custom error!'
  2331. ]);
  2332. $expected = [
  2333. 'div' => ['class' => 'input text required error'],
  2334. 'label' => ['for' => 'title'],
  2335. 'Title',
  2336. '/label',
  2337. 'input' => [
  2338. 'type' => 'text', 'name' => 'title',
  2339. 'id' => 'title', 'class' => 'form-error',
  2340. 'required' => 'required',
  2341. ],
  2342. ['div' => ['class' => 'error-message']],
  2343. 'Custom error!',
  2344. '/div',
  2345. '/div'
  2346. ];
  2347. $this->assertHtml($expected, $result);
  2348. $result = $this->Form->input('title', [
  2349. 'error' => ['error message' => 'Custom error!']
  2350. ]);
  2351. $expected = [
  2352. 'div' => ['class' => 'input text required error'],
  2353. 'label' => ['for' => 'title'],
  2354. 'Title',
  2355. '/label',
  2356. 'input' => [
  2357. 'type' => 'text',
  2358. 'name' => 'title',
  2359. 'id' => 'title',
  2360. 'class' => 'form-error',
  2361. 'required' => 'required'
  2362. ],
  2363. ['div' => ['class' => 'error-message']],
  2364. 'Custom error!',
  2365. '/div',
  2366. '/div'
  2367. ];
  2368. $this->assertHtml($expected, $result);
  2369. }
  2370. /**
  2371. * Tests displaying errors for nested entities
  2372. *
  2373. * @return void
  2374. */
  2375. public function testFormValidationAssociated()
  2376. {
  2377. $nested = new Entity(['foo' => 'bar']);
  2378. $nested->errors('foo', ['not a valid bar']);
  2379. $entity = new Entity(['nested' => $nested]);
  2380. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2381. $result = $this->Form->error('nested.foo');
  2382. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2383. }
  2384. /**
  2385. * testFormValidationAssociatedSecondLevel method
  2386. *
  2387. * test form error display with associated model.
  2388. *
  2389. * @return void
  2390. */
  2391. public function testFormValidationAssociatedSecondLevel()
  2392. {
  2393. $inner = new Entity(['bar' => 'baz']);
  2394. $nested = new Entity(['foo' => $inner]);
  2395. $entity = new Entity(['nested' => $nested]);
  2396. $inner->errors('bar', ['not a valid one']);
  2397. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2398. $result = $this->Form->error('nested.foo.bar');
  2399. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2400. }
  2401. /**
  2402. * testFormValidationMultiRecord method
  2403. *
  2404. * test form error display with multiple records.
  2405. *
  2406. * @return void
  2407. */
  2408. public function testFormValidationMultiRecord()
  2409. {
  2410. $one = new Entity;
  2411. $two = new Entity;
  2412. TableRegistry::get('Contacts', [
  2413. 'className' => __NAMESPACE__ . '\ContactsTable'
  2414. ]);
  2415. $one->set('email', '');
  2416. $one->errors('email', ['invalid email']);
  2417. $two->set('name', '');
  2418. $two->errors('name', ['This is wrong']);
  2419. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2420. $result = $this->Form->input('0.email');
  2421. $expected = [
  2422. 'div' => ['class' => 'input email error'],
  2423. 'label' => ['for' => '0-email'],
  2424. 'Email',
  2425. '/label',
  2426. 'input' => [
  2427. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2428. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2429. ],
  2430. ['div' => ['class' => 'error-message']],
  2431. 'invalid email',
  2432. '/div',
  2433. '/div'
  2434. ];
  2435. $this->assertHtml($expected, $result);
  2436. $result = $this->Form->input('1.name');
  2437. $expected = [
  2438. 'div' => ['class' => 'input text error'],
  2439. 'label' => ['for' => '1-name'],
  2440. 'Name',
  2441. '/label',
  2442. 'input' => [
  2443. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2444. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2445. ],
  2446. ['div' => ['class' => 'error-message']],
  2447. 'This is wrong',
  2448. '/div',
  2449. '/div'
  2450. ];
  2451. $this->assertHtml($expected, $result);
  2452. }
  2453. /**
  2454. * testInput method
  2455. *
  2456. * Test various incarnations of input().
  2457. *
  2458. * @return void
  2459. */
  2460. public function testInput()
  2461. {
  2462. TableRegistry::get('ValidateUsers', [
  2463. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2464. ]);
  2465. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2466. $result = $this->Form->input('ValidateUsers.balance');
  2467. $expected = [
  2468. 'div' => ['class'],
  2469. 'label' => ['for'],
  2470. 'Balance',
  2471. '/label',
  2472. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2473. '/div',
  2474. ];
  2475. $this->assertHtml($expected, $result);
  2476. $result = $this->Form->input('ValidateUser.cost_decimal');
  2477. $expected = [
  2478. 'div' => ['class'],
  2479. 'label' => ['for'],
  2480. 'Cost Decimal',
  2481. '/label',
  2482. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2483. '/div',
  2484. ];
  2485. $this->assertHtml($expected, $result);
  2486. $result = $this->Form->input('ValidateUser.null_decimal');
  2487. $expected = [
  2488. 'div' => ['class'],
  2489. 'label' => ['for'],
  2490. 'Null Decimal',
  2491. '/label',
  2492. 'input' => ['name', 'type' => 'number', 'id'],
  2493. '/div',
  2494. ];
  2495. $this->assertHtml($expected, $result);
  2496. }
  2497. /**
  2498. * Tests the input method and passing custom options
  2499. *
  2500. * @return void
  2501. */
  2502. public function testInputCustomization()
  2503. {
  2504. TableRegistry::get('Contacts', [
  2505. 'className' => __NAMESPACE__ . '\ContactsTable'
  2506. ]);
  2507. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2508. $result = $this->Form->input('Contact.email', ['id' => 'custom']);
  2509. $expected = [
  2510. 'div' => ['class' => 'input email'],
  2511. 'label' => ['for' => 'custom'],
  2512. 'Email',
  2513. '/label',
  2514. ['input' => [
  2515. 'type' => 'email', 'name' => 'Contact[email]',
  2516. 'id' => 'custom', 'maxlength' => 255
  2517. ]],
  2518. '/div'
  2519. ];
  2520. $this->assertHtml($expected, $result);
  2521. $result = $this->Form->input('Contact.email', [
  2522. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2523. ]);
  2524. $expected = [
  2525. '<div',
  2526. 'label' => ['for' => 'contact-email'],
  2527. 'Email',
  2528. '/label',
  2529. ['input' => [
  2530. 'type' => 'email', 'name' => 'Contact[email]',
  2531. 'id' => 'contact-email', 'maxlength' => 255
  2532. ]],
  2533. '/div'
  2534. ];
  2535. $this->assertHtml($expected, $result);
  2536. $result = $this->Form->input('Contact.email', ['type' => 'text']);
  2537. $expected = [
  2538. 'div' => ['class' => 'input text'],
  2539. 'label' => ['for' => 'contact-email'],
  2540. 'Email',
  2541. '/label',
  2542. ['input' => [
  2543. 'type' => 'text', 'name' => 'Contact[email]',
  2544. 'id' => 'contact-email', 'maxlength' => '255'
  2545. ]],
  2546. '/div'
  2547. ];
  2548. $this->assertHtml($expected, $result);
  2549. $result = $this->Form->input('Contact.5.email', ['type' => 'text']);
  2550. $expected = [
  2551. 'div' => ['class' => 'input text'],
  2552. 'label' => ['for' => 'contact-5-email'],
  2553. 'Email',
  2554. '/label',
  2555. ['input' => [
  2556. 'type' => 'text', 'name' => 'Contact[5][email]',
  2557. 'id' => 'contact-5-email', 'maxlength' => '255'
  2558. ]],
  2559. '/div'
  2560. ];
  2561. $this->assertHtml($expected, $result);
  2562. $result = $this->Form->input('Contact.password');
  2563. $expected = [
  2564. 'div' => ['class' => 'input password'],
  2565. 'label' => ['for' => 'contact-password'],
  2566. 'Password',
  2567. '/label',
  2568. ['input' => [
  2569. 'type' => 'password', 'name' => 'Contact[password]',
  2570. 'id' => 'contact-password'
  2571. ]],
  2572. '/div'
  2573. ];
  2574. $this->assertHtml($expected, $result);
  2575. $result = $this->Form->input('Contact.email', [
  2576. 'type' => 'file', 'class' => 'textbox'
  2577. ]);
  2578. $expected = [
  2579. 'div' => ['class' => 'input file'],
  2580. 'label' => ['for' => 'contact-email'],
  2581. 'Email',
  2582. '/label',
  2583. ['input' => [
  2584. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2585. 'id' => 'contact-email'
  2586. ]],
  2587. '/div'
  2588. ];
  2589. $this->assertHtml($expected, $result);
  2590. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2591. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2592. $result = $this->Form->input('phone');
  2593. $expected = [
  2594. 'div' => ['class' => 'input tel'],
  2595. 'label' => ['for' => 'phone'],
  2596. 'Phone',
  2597. '/label',
  2598. ['input' => [
  2599. 'type' => 'tel', 'name' => 'phone',
  2600. 'value' => 'Hello &amp; World &gt; weird chars',
  2601. 'id' => 'phone', 'maxlength' => 255
  2602. ]],
  2603. '/div'
  2604. ];
  2605. $this->assertHtml($expected, $result);
  2606. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2607. $this->Form->create();
  2608. $result = $this->Form->input('Model.0.OtherModel.field', ['id' => 'myId']);
  2609. $expected = [
  2610. 'div' => ['class' => 'input text'],
  2611. 'label' => ['for' => 'myId'],
  2612. 'Field',
  2613. '/label',
  2614. 'input' => [
  2615. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2616. 'value' => 'My value', 'id' => 'myId'
  2617. ],
  2618. '/div'
  2619. ];
  2620. $this->assertHtml($expected, $result);
  2621. $this->Form->request->data = [];
  2622. $entity->errors('field', 'Badness!');
  2623. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2624. $result = $this->Form->input('field');
  2625. $expected = [
  2626. 'div' => ['class' => 'input text error'],
  2627. 'label' => ['for' => 'field'],
  2628. 'Field',
  2629. '/label',
  2630. 'input' => [
  2631. 'type' => 'text', 'name' => 'field',
  2632. 'id' => 'field', 'class' => 'form-error'
  2633. ],
  2634. ['div' => ['class' => 'error-message']],
  2635. 'Badness!',
  2636. '/div',
  2637. '/div'
  2638. ];
  2639. $this->assertHtml($expected, $result);
  2640. $result = $this->Form->input('field', [
  2641. 'templates' => [
  2642. 'inputContainerError' => '{{content}}{{error}}',
  2643. 'error' => '<span class="error-message">{{content}}</span>'
  2644. ]
  2645. ]);
  2646. $expected = [
  2647. 'label' => ['for' => 'field'],
  2648. 'Field',
  2649. '/label',
  2650. 'input' => [
  2651. 'type' => 'text', 'name' => 'field',
  2652. 'id' => 'field', 'class' => 'form-error'
  2653. ],
  2654. ['span' => ['class' => 'error-message']],
  2655. 'Badness!',
  2656. '/span'
  2657. ];
  2658. $this->assertHtml($expected, $result);
  2659. $entity->errors('field', ['minLength'], true);
  2660. $result = $this->Form->input('field', [
  2661. 'error' => [
  2662. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2663. 'maxLength' => 'login too large'
  2664. ]
  2665. ]);
  2666. $expected = [
  2667. 'div' => ['class' => 'input text error'],
  2668. 'label' => ['for' => 'field'],
  2669. 'Field',
  2670. '/label',
  2671. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2672. ['div' => ['class' => 'error-message']],
  2673. 'Le login doit contenir au moins 2 caractères',
  2674. '/div',
  2675. '/div'
  2676. ];
  2677. $this->assertHtml($expected, $result);
  2678. $entity->errors('field', ['maxLength'], true);
  2679. $result = $this->Form->input('field', [
  2680. 'error' => [
  2681. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2682. 'maxLength' => 'login too large',
  2683. ]
  2684. ]);
  2685. $expected = [
  2686. 'div' => ['class' => 'input text error'],
  2687. 'label' => ['for' => 'field'],
  2688. 'Field',
  2689. '/label',
  2690. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2691. ['div' => ['class' => 'error-message']],
  2692. 'login too large',
  2693. '/div',
  2694. '/div'
  2695. ];
  2696. $this->assertHtml($expected, $result);
  2697. }
  2698. /**
  2699. * Test that input() accepts a template file.
  2700. *
  2701. * @return void
  2702. */
  2703. public function testInputWithTemplateFile()
  2704. {
  2705. $result = $this->Form->input('field', [
  2706. 'templates' => 'htmlhelper_tags'
  2707. ]);
  2708. $expected = [
  2709. 'label' => ['for' => 'field'],
  2710. 'Field',
  2711. '/label',
  2712. 'input' => [
  2713. 'type' => 'text', 'name' => 'field',
  2714. 'id' => 'field'
  2715. ],
  2716. ];
  2717. $this->assertHtml($expected, $result);
  2718. }
  2719. /**
  2720. * Test that nested inputs end with brackets
  2721. *
  2722. * @return void
  2723. */
  2724. public function testNestedInputsEndWithBrackets()
  2725. {
  2726. $result = $this->Form->text('nested.text[]');
  2727. $expected = [
  2728. 'input' => [
  2729. 'type' => 'text', 'name' => 'nested[text][]'
  2730. ],
  2731. ];
  2732. $result = $this->Form->file('nested.file[]');
  2733. $expected = [
  2734. 'input' => [
  2735. 'type' => 'file', 'name' => 'nested[file][]'
  2736. ],
  2737. ];
  2738. $this->assertHtml($expected, $result);
  2739. }
  2740. /**
  2741. * Test id prefix
  2742. *
  2743. * @return void
  2744. */
  2745. public function testCreateIdPrefix()
  2746. {
  2747. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2748. $result = $this->Form->input('field');
  2749. $expected = [
  2750. 'div' => ['class' => 'input text'],
  2751. 'label' => ['for' => 'prefix-field'],
  2752. 'Field',
  2753. '/label',
  2754. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2755. '/div'
  2756. ];
  2757. $this->assertHtml($expected, $result);
  2758. $result = $this->Form->input('field', ['id' => 'custom-id']);
  2759. $expected = [
  2760. 'div' => ['class' => 'input text'],
  2761. 'label' => ['for' => 'custom-id'],
  2762. 'Field',
  2763. '/label',
  2764. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2765. '/div'
  2766. ];
  2767. $this->assertHtml($expected, $result);
  2768. $result = $this->Form->radio('Model.field', ['option A']);
  2769. $expected = [
  2770. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2771. 'label' => ['for' => 'prefix-model-field-0'],
  2772. ['input' => [
  2773. 'type' => 'radio',
  2774. 'name' => 'Model[field]',
  2775. 'value' => '0',
  2776. 'id' => 'prefix-model-field-0'
  2777. ]],
  2778. 'option A',
  2779. '/label'
  2780. ];
  2781. $this->assertHtml($expected, $result);
  2782. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2783. $expected = [
  2784. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2785. 'label' => ['for' => 'prefix-model-field-0'],
  2786. ['input' => [
  2787. 'type' => 'radio',
  2788. 'name' => 'Model[field]',
  2789. 'value' => '0',
  2790. 'id' => 'prefix-model-field-0'
  2791. ]],
  2792. 'option A',
  2793. '/label'
  2794. ];
  2795. $this->assertHtml($expected, $result);
  2796. $result = $this->Form->select(
  2797. 'Model.multi_field',
  2798. ['first'],
  2799. ['multiple' => 'checkbox']
  2800. );
  2801. $expected = [
  2802. 'input' => [
  2803. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2804. ],
  2805. ['div' => ['class' => 'checkbox']],
  2806. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2807. ['input' => [
  2808. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2809. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2810. ]],
  2811. 'first',
  2812. '/label',
  2813. '/div',
  2814. ];
  2815. $this->assertHtml($expected, $result);
  2816. $this->Form->end();
  2817. $result = $this->Form->input('field');
  2818. $expected = [
  2819. 'div' => ['class' => 'input text'],
  2820. 'label' => ['for' => 'field'],
  2821. 'Field',
  2822. '/label',
  2823. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2824. '/div'
  2825. ];
  2826. $this->assertHtml($expected, $result);
  2827. }
  2828. /**
  2829. * Test that inputs with 0 can be created.
  2830. *
  2831. * @return void
  2832. */
  2833. public function testInputZero()
  2834. {
  2835. TableRegistry::get('Contacts', [
  2836. 'className' => __NAMESPACE__ . '\ContactsTable'
  2837. ]);
  2838. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2839. $result = $this->Form->input('0');
  2840. $expected = [
  2841. 'div' => ['class' => 'input text'],
  2842. 'label' => ['for' => '0'], '/label',
  2843. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2844. '/div'
  2845. ];
  2846. $this->assertHtml($expected, $result);
  2847. }
  2848. /**
  2849. * test input() with checkbox creation
  2850. *
  2851. * @return void
  2852. */
  2853. public function testInputCheckbox()
  2854. {
  2855. $result = $this->Form->input('User.active', ['label' => false, 'checked' => true]);
  2856. $expected = [
  2857. 'div' => ['class' => 'input checkbox'],
  2858. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2859. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2860. '/div'
  2861. ];
  2862. $this->assertHtml($expected, $result);
  2863. $result = $this->Form->input('User.active', ['label' => false, 'checked' => 1]);
  2864. $expected = [
  2865. 'div' => ['class' => 'input checkbox'],
  2866. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2867. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2868. '/div'
  2869. ];
  2870. $this->assertHtml($expected, $result);
  2871. $result = $this->Form->input('User.active', ['label' => false, 'checked' => '1']);
  2872. $expected = [
  2873. 'div' => ['class' => 'input checkbox'],
  2874. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2875. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2876. '/div'
  2877. ];
  2878. $this->assertHtml($expected, $result);
  2879. $result = $this->Form->input('User.disabled', [
  2880. 'label' => 'Disabled',
  2881. 'type' => 'checkbox',
  2882. 'data-foo' => 'disabled'
  2883. ]);
  2884. $expected = [
  2885. 'div' => ['class' => 'input checkbox'],
  2886. 'input' => ['type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'],
  2887. 'label' => ['for' => 'user-disabled'],
  2888. ['input' => [
  2889. 'type' => 'checkbox',
  2890. 'name' => 'User[disabled]',
  2891. 'value' => '1',
  2892. 'id' => 'user-disabled',
  2893. 'data-foo' => 'disabled'
  2894. ]],
  2895. 'Disabled',
  2896. '/label',
  2897. '/div'
  2898. ];
  2899. $this->assertHtml($expected, $result);
  2900. }
  2901. /**
  2902. * Test that input() does not create wrapping div and label tag for hidden fields
  2903. *
  2904. * @return void
  2905. */
  2906. public function testInputHidden()
  2907. {
  2908. TableRegistry::get('ValidateUsers', [
  2909. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2910. ]);
  2911. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2912. $result = $this->Form->input('ValidateUser.id');
  2913. $expected = [
  2914. 'input' => ['name', 'type' => 'hidden', 'id']
  2915. ];
  2916. $this->assertHtml($expected, $result);
  2917. $result = $this->Form->input('ValidateUser.custom', ['type' => 'hidden']);
  2918. $expected = [
  2919. 'input' => ['name', 'type' => 'hidden', 'id']
  2920. ];
  2921. $this->assertHtml($expected, $result);
  2922. }
  2923. /**
  2924. * test form->input() with datetime
  2925. *
  2926. * @return void
  2927. */
  2928. public function testInputDatetime()
  2929. {
  2930. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  2931. ->setMethods(['datetime'])
  2932. ->setConstructorArgs([new View()])
  2933. ->getMock();
  2934. $this->Form->expects($this->once())->method('datetime')
  2935. ->with('prueba', [
  2936. 'type' => 'datetime',
  2937. 'timeFormat' => 24,
  2938. 'minYear' => 2008,
  2939. 'maxYear' => 2011,
  2940. 'interval' => 15,
  2941. 'options' => null,
  2942. 'empty' => false,
  2943. 'id' => 'prueba',
  2944. 'required' => false,
  2945. 'templateVars' => []
  2946. ])
  2947. ->will($this->returnValue('This is it!'));
  2948. $result = $this->Form->input('prueba', [
  2949. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2950. 'maxYear' => 2011, 'interval' => 15
  2951. ]);
  2952. $expected = [
  2953. 'div' => ['class' => 'input datetime'],
  2954. '<label',
  2955. 'Prueba',
  2956. '/label',
  2957. 'This is it!',
  2958. '/div'
  2959. ];
  2960. $this->assertHtml($expected, $result);
  2961. }
  2962. /**
  2963. * test form->input() with datetime with id prefix
  2964. *
  2965. * @return void
  2966. */
  2967. public function testInputDatetimeIdPrefix()
  2968. {
  2969. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  2970. ->setMethods(['datetime'])
  2971. ->setConstructorArgs([new View()])
  2972. ->getMock();
  2973. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2974. $this->Form->expects($this->once())->method('datetime')
  2975. ->with('prueba', [
  2976. 'type' => 'datetime',
  2977. 'timeFormat' => 24,
  2978. 'minYear' => 2008,
  2979. 'maxYear' => 2011,
  2980. 'interval' => 15,
  2981. 'options' => null,
  2982. 'empty' => false,
  2983. 'id' => 'prefix-prueba',
  2984. 'required' => false,
  2985. 'templateVars' => []
  2986. ])
  2987. ->will($this->returnValue('This is it!'));
  2988. $result = $this->Form->input('prueba', [
  2989. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2990. 'maxYear' => 2011, 'interval' => 15
  2991. ]);
  2992. $expected = [
  2993. 'div' => ['class' => 'input datetime'],
  2994. '<label',
  2995. 'Prueba',
  2996. '/label',
  2997. 'This is it!',
  2998. '/div'
  2999. ];
  3000. $this->assertHtml($expected, $result);
  3001. }
  3002. /**
  3003. * Test generating checkboxes with disabled elements.
  3004. *
  3005. * @return void
  3006. */
  3007. public function testInputCheckboxWithDisabledElements()
  3008. {
  3009. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3010. $result = $this->Form->input('Contact.multiple', [
  3011. 'multiple' => 'checkbox',
  3012. 'disabled' => 'disabled',
  3013. 'options' => $options
  3014. ]);
  3015. $expected = [
  3016. ['div' => ['class' => 'input select']],
  3017. ['label' => ['for' => "contact-multiple"]],
  3018. 'Multiple',
  3019. '/label',
  3020. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'disabled' => 'disabled', 'value' => '']],
  3021. ['div' => ['class' => 'checkbox']],
  3022. ['label' => ['for' => "contact-multiple-1"]],
  3023. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1"]],
  3024. 'One',
  3025. '/label',
  3026. '/div',
  3027. ['div' => ['class' => 'checkbox']],
  3028. ['label' => ['for' => "contact-multiple-2"]],
  3029. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2"]],
  3030. 'Two',
  3031. '/label',
  3032. '/div',
  3033. ['div' => ['class' => 'checkbox']],
  3034. ['label' => ['for' => "contact-multiple-3"]],
  3035. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3"]],
  3036. 'Three',
  3037. '/label',
  3038. '/div',
  3039. '/div'
  3040. ];
  3041. $this->assertHtml($expected, $result);
  3042. // make sure 50 does only disable 50, and not 50f5c0cf
  3043. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3044. $disabled = [50];
  3045. $expected = [
  3046. ['div' => ['class' => 'input select']],
  3047. ['label' => ['for' => "contact-multiple"]],
  3048. 'Multiple',
  3049. '/label',
  3050. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '']],
  3051. ['div' => ['class' => 'checkbox']],
  3052. ['label' => ['for' => "contact-multiple-50"]],
  3053. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50"]],
  3054. 'Fifty',
  3055. '/label',
  3056. '/div',
  3057. ['div' => ['class' => 'checkbox']],
  3058. ['label' => ['for' => "contact-multiple-50f5c0cf"]],
  3059. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf"]],
  3060. 'Stringy',
  3061. '/label',
  3062. '/div',
  3063. '/div'
  3064. ];
  3065. $result = $this->Form->input('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3066. $this->assertHtml($expected, $result);
  3067. }
  3068. /**
  3069. * test input name with leading integer, ensure attributes are generated correctly.
  3070. *
  3071. * @return void
  3072. */
  3073. public function testInputWithLeadingInteger()
  3074. {
  3075. $result = $this->Form->text('0.Node.title');
  3076. $expected = [
  3077. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3078. ];
  3079. $this->assertHtml($expected, $result);
  3080. }
  3081. /**
  3082. * test form->input() with select type inputs.
  3083. *
  3084. * @return void
  3085. */
  3086. public function testInputSelectType()
  3087. {
  3088. $result = $this->Form->input(
  3089. 'email',
  3090. [
  3091. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3092. );
  3093. $expected = [
  3094. 'div' => ['class' => 'input select'],
  3095. 'label' => ['for' => 'email'],
  3096. 'Email',
  3097. '/label',
  3098. ['select' => ['name' => 'email', 'id' => 'email']],
  3099. ['option' => ['value' => '']],
  3100. '/option',
  3101. ['option' => ['value' => 'è']],
  3102. 'Firést',
  3103. '/option',
  3104. ['option' => ['value' => 'é']],
  3105. 'Secoènd',
  3106. '/option',
  3107. '/select',
  3108. '/div'
  3109. ];
  3110. $this->assertHtml($expected, $result);
  3111. $result = $this->Form->input(
  3112. 'email',
  3113. [
  3114. 'options' => ['First', 'Second'], 'empty' => true]
  3115. );
  3116. $expected = [
  3117. 'div' => ['class' => 'input select'],
  3118. 'label' => ['for' => 'email'],
  3119. 'Email',
  3120. '/label',
  3121. ['select' => ['name' => 'email', 'id' => 'email']],
  3122. ['option' => ['value' => '']],
  3123. '/option',
  3124. ['option' => ['value' => '0']],
  3125. 'First',
  3126. '/option',
  3127. ['option' => ['value' => '1']],
  3128. 'Second',
  3129. '/option',
  3130. '/select',
  3131. '/div'
  3132. ];
  3133. $this->assertHtml($expected, $result);
  3134. $result = $this->Form->input('email', [
  3135. 'type' => 'select',
  3136. 'options' => new \ArrayObject(['First', 'Second']),
  3137. 'empty' => true
  3138. ]);
  3139. $this->assertHtml($expected, $result);
  3140. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3141. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3142. $result = $this->Form->input('Model.user_id', ['empty' => true]);
  3143. $expected = [
  3144. 'div' => ['class' => 'input select'],
  3145. 'label' => ['for' => 'model-user-id'],
  3146. 'User',
  3147. '/label',
  3148. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3149. ['option' => ['value' => '']],
  3150. '/option',
  3151. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3152. 'good',
  3153. '/option',
  3154. ['option' => ['value' => 'other']],
  3155. 'bad',
  3156. '/option',
  3157. '/select',
  3158. '/div'
  3159. ];
  3160. $this->assertHtml($expected, $result);
  3161. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3162. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3163. $result = $this->Form->input('Thing.user_id', ['empty' => 'Some Empty']);
  3164. $expected = [
  3165. 'div' => ['class' => 'input select'],
  3166. 'label' => ['for' => 'thing-user-id'],
  3167. 'User',
  3168. '/label',
  3169. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3170. ['option' => ['value' => '']],
  3171. 'Some Empty',
  3172. '/option',
  3173. ['option' => ['value' => 'value']],
  3174. 'good',
  3175. '/option',
  3176. ['option' => ['value' => 'other']],
  3177. 'bad',
  3178. '/option',
  3179. '/select',
  3180. '/div'
  3181. ];
  3182. $this->assertHtml($expected, $result);
  3183. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3184. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3185. $result = $this->Form->input('Thing.user_id', ['empty' => 'Some Empty']);
  3186. $expected = [
  3187. 'div' => ['class' => 'input select'],
  3188. 'label' => ['for' => 'thing-user-id'],
  3189. 'User',
  3190. '/label',
  3191. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3192. ['option' => ['value' => '']],
  3193. 'Some Empty',
  3194. '/option',
  3195. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3196. 'good',
  3197. '/option',
  3198. ['option' => ['value' => 'other']],
  3199. 'bad',
  3200. '/option',
  3201. '/select',
  3202. '/div'
  3203. ];
  3204. $this->assertHtml($expected, $result);
  3205. $this->Form->data = [];
  3206. $result = $this->Form->input('Publisher.id', [
  3207. 'label' => 'Publisher',
  3208. 'type' => 'select',
  3209. 'multiple' => 'checkbox',
  3210. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3211. ]);
  3212. $expected = [
  3213. ['div' => ['class' => 'input select']],
  3214. ['label' => ['for' => 'publisher-id']],
  3215. 'Publisher',
  3216. '/label',
  3217. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3218. ['div' => ['class' => 'checkbox']],
  3219. ['label' => ['for' => 'publisher-id-value-1']],
  3220. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3221. 'Label 1',
  3222. '/label',
  3223. '/div',
  3224. ['div' => ['class' => 'checkbox']],
  3225. ['label' => ['for' => 'publisher-id-value-2']],
  3226. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3227. 'Label 2',
  3228. '/label',
  3229. '/div',
  3230. '/div'
  3231. ];
  3232. $this->assertHtml($expected, $result);
  3233. }
  3234. /**
  3235. * test that input() and a non standard primary key makes a hidden input by default.
  3236. *
  3237. * @return void
  3238. */
  3239. public function testInputWithNonStandardPrimaryKeyMakesHidden()
  3240. {
  3241. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3242. $this->Form->create($this->article);
  3243. $result = $this->Form->input('title');
  3244. $expected = [
  3245. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3246. ];
  3247. $this->assertHtml($expected, $result);
  3248. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3249. $this->Form->create($this->article);
  3250. $result = $this->Form->input('title');
  3251. $expected = [
  3252. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3253. ];
  3254. $this->assertHtml($expected, $result);
  3255. $result = $this->Form->input('body');
  3256. $expected = [
  3257. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3258. ];
  3259. $this->assertHtml($expected, $result);
  3260. }
  3261. /**
  3262. * test that overriding the magic select type widget is possible
  3263. *
  3264. * @return void
  3265. */
  3266. public function testInputOverridingMagicSelectType()
  3267. {
  3268. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3269. $result = $this->Form->input('Model.user_id', ['type' => 'text']);
  3270. $expected = [
  3271. 'div' => ['class' => 'input text'],
  3272. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3273. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3274. '/div'
  3275. ];
  3276. $this->assertHtml($expected, $result);
  3277. //Check that magic types still work for plural/singular vars
  3278. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3279. $result = $this->Form->input('Model.type');
  3280. $expected = [
  3281. 'div' => ['class' => 'input select'],
  3282. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3283. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3284. ['option' => ['value' => 'value']], 'good', '/option',
  3285. ['option' => ['value' => 'other']], 'bad', '/option',
  3286. '/select',
  3287. '/div'
  3288. ];
  3289. $this->assertHtml($expected, $result);
  3290. }
  3291. /**
  3292. * Test that inferred types do not override developer input
  3293. *
  3294. * @return void
  3295. */
  3296. public function testInputMagicTypeDoesNotOverride()
  3297. {
  3298. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3299. $result = $this->Form->input('Model.user', ['type' => 'checkbox']);
  3300. $expected = [
  3301. 'div' => ['class' => 'input checkbox'],
  3302. ['input' => [
  3303. 'type' => 'hidden',
  3304. 'name' => 'Model[user]',
  3305. 'value' => 0,
  3306. ]],
  3307. 'label' => ['for' => 'model-user'],
  3308. ['input' => [
  3309. 'name' => 'Model[user]',
  3310. 'type' => 'checkbox',
  3311. 'id' => 'model-user',
  3312. 'value' => 1
  3313. ]],
  3314. 'User',
  3315. '/label',
  3316. '/div'
  3317. ];
  3318. $this->assertHtml($expected, $result);
  3319. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3320. $options = [
  3321. 1 => 'blue',
  3322. 2 => 'red'
  3323. ];
  3324. $result = $this->Form->input('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3325. $expected = [
  3326. 'div' => ['class' => 'input select'],
  3327. 'label' => ['for' => 'tags-ids'],
  3328. 'Tags',
  3329. '/label',
  3330. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3331. ['div' => ['class' => 'checkbox']],
  3332. ['label' => ['for' => 'tags-ids-1']],
  3333. ['input' => [
  3334. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3335. 'value' => '1', 'name' => 'tags[_ids][]'
  3336. ]],
  3337. 'blue',
  3338. '/label',
  3339. '/div',
  3340. ['div' => ['class' => 'checkbox']],
  3341. ['label' => ['for' => 'tags-ids-2']],
  3342. ['input' => [
  3343. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3344. 'value' => '2', 'name' => 'tags[_ids][]'
  3345. ]],
  3346. 'red',
  3347. '/label',
  3348. '/div',
  3349. '/div'
  3350. ];
  3351. $this->assertHtml($expected, $result);
  3352. }
  3353. /**
  3354. * Test that magic input() selects are created for type=number
  3355. *
  3356. * @return void
  3357. */
  3358. public function testInputMagicSelectForTypeNumber()
  3359. {
  3360. TableRegistry::get('ValidateUsers', [
  3361. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3362. ]);
  3363. $entity = new Entity(['balance' => 1]);
  3364. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3365. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3366. $result = $this->Form->input('balance');
  3367. $expected = [
  3368. 'div' => ['class' => 'input select'],
  3369. 'label' => ['for' => 'balance'],
  3370. 'Balance',
  3371. '/label',
  3372. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3373. ['option' => ['value' => '0']],
  3374. 'nothing',
  3375. '/option',
  3376. ['option' => ['value' => '1', 'selected' => 'selected']],
  3377. 'some',
  3378. '/option',
  3379. ['option' => ['value' => '100']],
  3380. 'a lot',
  3381. '/option',
  3382. '/select',
  3383. '/div'
  3384. ];
  3385. $this->assertHtml($expected, $result);
  3386. }
  3387. /**
  3388. * Test invalid 'input' type option to input() function.
  3389. *
  3390. * @expectedException \RuntimeException
  3391. * @expectedExceptionMessage Invalid type 'input' used for field 'text'
  3392. * @return void
  3393. */
  3394. public function testInvalidInputTypeOption()
  3395. {
  3396. $this->Form->input('text', ['type' => 'input']);
  3397. }
  3398. /**
  3399. * Test that magic input() selects can easily be converted into radio types without error.
  3400. *
  3401. * @return void
  3402. */
  3403. public function testInputMagicSelectChangeToRadio()
  3404. {
  3405. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3406. $result = $this->Form->input('Model.user_id', ['type' => 'radio']);
  3407. $this->assertContains('input type="radio"', $result);
  3408. }
  3409. /**
  3410. * testFormInputSubmit method
  3411. *
  3412. * test correct results for form::input() and type submit.
  3413. *
  3414. * @return void
  3415. */
  3416. public function testFormInputSubmit()
  3417. {
  3418. $result = $this->Form->input('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3419. $expected = [
  3420. 'div' => ['class' => 'submit'],
  3421. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3422. '/div'
  3423. ];
  3424. $this->assertHtml($expected, $result);
  3425. }
  3426. /**
  3427. * testFormInputs method
  3428. *
  3429. * test correct results from form::inputs().
  3430. *
  3431. * @return void
  3432. */
  3433. public function testFormInputsLegendFieldset()
  3434. {
  3435. $this->Form->create($this->article);
  3436. $result = $this->Form->allInputs([], ['legend' => 'The Legend']);
  3437. $expected = [
  3438. '<fieldset',
  3439. '<legend',
  3440. 'The Legend',
  3441. '/legend',
  3442. '*/fieldset',
  3443. ];
  3444. $this->assertHtml($expected, $result);
  3445. $result = $this->Form->allInputs([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3446. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3447. $this->assertContains('<fieldset>', $result);
  3448. $result = $this->Form->allInputs([], ['fieldset' => false, 'legend' => false]);
  3449. $this->assertNotContains('<legend>', $result);
  3450. $this->assertNotContains('<fieldset>', $result);
  3451. $result = $this->Form->allInputs([], ['fieldset' => false, 'legend' => 'Hello']);
  3452. $this->assertNotContains('<legend>', $result);
  3453. $this->assertNotContains('<fieldset>', $result);
  3454. $this->Form->create($this->article);
  3455. $this->Form->request->params['prefix'] = 'admin';
  3456. $this->Form->request->params['action'] = 'admin_edit';
  3457. $this->Form->request->params['controller'] = 'articles';
  3458. $result = $this->Form->allInputs();
  3459. $expected = [
  3460. '<fieldset',
  3461. '<legend',
  3462. 'New Article',
  3463. '/legend',
  3464. '*/fieldset',
  3465. ];
  3466. $this->assertHtml($expected, $result);
  3467. $this->Form->create($this->article);
  3468. $result = $this->Form->allInputs([], ['fieldset' => [], 'legend' => 'The Legend']);
  3469. $expected = [
  3470. '<fieldset',
  3471. '<legend',
  3472. 'The Legend',
  3473. '/legend',
  3474. '*/fieldset',
  3475. ];
  3476. $this->assertHtml($expected, $result);
  3477. $this->Form->create($this->article);
  3478. $result = $this->Form->allInputs([], [
  3479. 'fieldset' => [
  3480. 'class' => 'some-class some-other-class',
  3481. 'disabled' => true,
  3482. 'data-param' => 'a-param'
  3483. ],
  3484. 'legend' => 'The Legend'
  3485. ]);
  3486. $expected = [
  3487. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3488. '<legend',
  3489. 'The Legend',
  3490. '/legend',
  3491. '*/fieldset',
  3492. ];
  3493. $this->assertHtml($expected, $result);
  3494. }
  3495. /**
  3496. * Test the inputs() method.
  3497. *
  3498. * @return void
  3499. */
  3500. public function testFormInputs()
  3501. {
  3502. $this->Form->create($this->article);
  3503. $result = $this->Form->allInputs();
  3504. $expected = [
  3505. '<fieldset',
  3506. '<legend', 'New Article', '/legend',
  3507. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3508. ['div' => ['class' => 'input select required']],
  3509. '*/div',
  3510. ['div' => ['class' => 'input text required']],
  3511. '*/div',
  3512. ['div' => ['class' => 'input text']],
  3513. '*/div',
  3514. ['div' => ['class' => 'input text']],
  3515. '*/div',
  3516. '/fieldset',
  3517. ];
  3518. $this->assertHtml($expected, $result);
  3519. $result = $this->Form->allInputs([
  3520. 'published' => ['type' => 'boolean']
  3521. ]);
  3522. $expected = [
  3523. '<fieldset',
  3524. '<legend', 'New Article', '/legend',
  3525. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3526. ['div' => ['class' => 'input select required']],
  3527. '*/div',
  3528. ['div' => ['class' => 'input text required']],
  3529. '*/div',
  3530. ['div' => ['class' => 'input text']],
  3531. '*/div',
  3532. ['div' => ['class' => 'input boolean']],
  3533. '*/div',
  3534. '/fieldset',
  3535. ];
  3536. $this->assertHtml($expected, $result);
  3537. $this->Form->create($this->article);
  3538. $result = $this->Form->allInputs([], ['legend' => 'Hello']);
  3539. $expected = [
  3540. 'fieldset' => [],
  3541. 'legend' => [],
  3542. 'Hello',
  3543. '/legend',
  3544. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3545. ['div' => ['class' => 'input select required']],
  3546. '*/div',
  3547. ['div' => ['class' => 'input text required']],
  3548. '*/div',
  3549. ['div' => ['class' => 'input text']],
  3550. '*/div',
  3551. ['div' => ['class' => 'input text']],
  3552. '*/div',
  3553. '/fieldset'
  3554. ];
  3555. $this->assertHtml($expected, $result);
  3556. $this->Form->create(false);
  3557. $expected = [
  3558. 'fieldset' => [],
  3559. ['div' => ['class' => 'input text']],
  3560. 'label' => ['for' => 'foo'],
  3561. 'Foo',
  3562. '/label',
  3563. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3564. '*/div',
  3565. '/fieldset'
  3566. ];
  3567. $result = $this->Form->allInputs(
  3568. ['foo' => ['type' => 'text']],
  3569. ['legend' => false]
  3570. );
  3571. $this->assertHtml($expected, $result);
  3572. }
  3573. /**
  3574. * testFormInputsBlacklist
  3575. *
  3576. * @return void
  3577. */
  3578. public function testFormInputsBlacklist()
  3579. {
  3580. $this->Form->create($this->article);
  3581. $result = $this->Form->allInputs([
  3582. 'id' => false
  3583. ]);
  3584. $expected = [
  3585. '<fieldset',
  3586. '<legend', 'New Article', '/legend',
  3587. ['div' => ['class' => 'input select required']],
  3588. '*/div',
  3589. ['div' => ['class' => 'input text required']],
  3590. '*/div',
  3591. ['div' => ['class' => 'input text']],
  3592. '*/div',
  3593. ['div' => ['class' => 'input text']],
  3594. '*/div',
  3595. '/fieldset',
  3596. ];
  3597. $this->assertHtml($expected, $result);
  3598. $this->Form->create($this->article);
  3599. $result = $this->Form->allInputs([
  3600. 'id' => []
  3601. ]);
  3602. $expected = [
  3603. '<fieldset',
  3604. '<legend', 'New Article', '/legend',
  3605. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3606. ['div' => ['class' => 'input select required']],
  3607. '*/div',
  3608. ['div' => ['class' => 'input text required']],
  3609. '*/div',
  3610. ['div' => ['class' => 'input text']],
  3611. '*/div',
  3612. ['div' => ['class' => 'input text']],
  3613. '*/div',
  3614. '/fieldset',
  3615. ];
  3616. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3617. }
  3618. /**
  3619. * testSelectAsCheckbox method
  3620. *
  3621. * test multi-select widget with checkbox formatting.
  3622. *
  3623. * @return void
  3624. */
  3625. public function testSelectAsCheckbox()
  3626. {
  3627. $result = $this->Form->select(
  3628. 'Model.multi_field',
  3629. ['first', 'second', 'third'],
  3630. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3631. );
  3632. $expected = [
  3633. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3634. ['div' => ['class' => 'checkbox']],
  3635. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3636. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3637. 'first',
  3638. '/label',
  3639. '/div',
  3640. ['div' => ['class' => 'checkbox']],
  3641. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3642. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3643. 'second',
  3644. '/label',
  3645. '/div',
  3646. ['div' => ['class' => 'checkbox']],
  3647. ['label' => ['for' => 'model-multi-field-2']],
  3648. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3649. 'third',
  3650. '/label',
  3651. '/div',
  3652. ];
  3653. $this->assertHtml($expected, $result);
  3654. $result = $this->Form->select(
  3655. 'Model.multi_field',
  3656. ['1/2' => 'half'],
  3657. ['multiple' => 'checkbox']
  3658. );
  3659. $expected = [
  3660. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3661. ['div' => ['class' => 'checkbox']],
  3662. ['label' => ['for' => 'model-multi-field-1-2']],
  3663. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3664. 'half',
  3665. '/label',
  3666. '/div',
  3667. ];
  3668. $this->assertHtml($expected, $result);
  3669. }
  3670. /**
  3671. * testLabel method
  3672. *
  3673. * test label generation.
  3674. *
  3675. * @return void
  3676. */
  3677. public function testLabel()
  3678. {
  3679. $result = $this->Form->label('Person.name');
  3680. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3681. $this->assertHtml($expected, $result);
  3682. $result = $this->Form->label('Person.name');
  3683. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3684. $this->assertHtml($expected, $result);
  3685. $result = $this->Form->label('Person.first_name');
  3686. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3687. $this->assertHtml($expected, $result);
  3688. $result = $this->Form->label('Person.first_name', 'Your first name');
  3689. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3690. $this->assertHtml($expected, $result);
  3691. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3692. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3693. $this->assertHtml($expected, $result);
  3694. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3695. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3696. $this->assertHtml($expected, $result);
  3697. $result = $this->Form->label('Person.first_name', '');
  3698. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3699. $this->assertHtml($expected, $result);
  3700. $result = $this->Form->label('Person.2.name', '');
  3701. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3702. $this->assertHtml($expected, $result);
  3703. }
  3704. /**
  3705. * Test that label() can accept an input with the correct template vars.
  3706. *
  3707. * @return void
  3708. */
  3709. public function testLabelContainInput()
  3710. {
  3711. $this->Form->templates([
  3712. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3713. ]);
  3714. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3715. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3716. ]);
  3717. $expected = [
  3718. 'label' => ['for' => 'person-accept-terms'],
  3719. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3720. 'Accept',
  3721. '/label',
  3722. ];
  3723. $this->assertHtml($expected, $result);
  3724. }
  3725. /**
  3726. * testTextbox method
  3727. *
  3728. * test textbox element generation
  3729. *
  3730. * @return void
  3731. */
  3732. public function testTextbox()
  3733. {
  3734. $result = $this->Form->text('Model.field');
  3735. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3736. $this->assertHtml($expected, $result);
  3737. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3738. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3739. $this->assertHtml($expected, $result);
  3740. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3741. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3742. $this->assertHtml($expected, $result);
  3743. }
  3744. /**
  3745. * Test that text() hooks up with request data and error fields.
  3746. *
  3747. * @return void
  3748. */
  3749. public function testTextBoxDataAndError()
  3750. {
  3751. $this->article['errors'] = [
  3752. 'Contact' => ['text' => 'wrong']
  3753. ];
  3754. $this->Form->create($this->article);
  3755. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3756. $result = $this->Form->text('Model.text');
  3757. $expected = [
  3758. 'input' => [
  3759. 'type' => 'text',
  3760. 'name' => 'Model[text]',
  3761. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3762. ]
  3763. ];
  3764. $this->assertHtml($expected, $result);
  3765. $this->Form->request->data['Contact']['text'] = 'test';
  3766. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3767. $expected = [
  3768. 'input' => [
  3769. 'type' => 'text',
  3770. 'name' => 'Contact[text]',
  3771. 'value' => 'test',
  3772. 'id' => 'theID',
  3773. 'class' => 'form-error'
  3774. ]
  3775. ];
  3776. $this->assertHtml($expected, $result);
  3777. }
  3778. /**
  3779. * testDefaultValue method
  3780. *
  3781. * Test default value setting
  3782. *
  3783. * @return void
  3784. */
  3785. public function testTextDefaultValue()
  3786. {
  3787. $this->Form->request->data['Model']['field'] = 'test';
  3788. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3789. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3790. $this->assertHtml($expected, $result);
  3791. unset($this->Form->request->data['Model']['field']);
  3792. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3793. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3794. $this->assertHtml($expected, $result);
  3795. $Articles = TableRegistry::get('Articles');
  3796. $title = $Articles->schema()->column('title');
  3797. $Articles->schema()->addColumn(
  3798. 'title',
  3799. ['default' => 'default title'] + $title
  3800. );
  3801. $entity = $Articles->newEntity();
  3802. $this->Form->create($entity);
  3803. // Get default value from schema
  3804. $result = $this->Form->text('title');
  3805. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  3806. $this->assertHtml($expected, $result);
  3807. // Don't get value from schema
  3808. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3809. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  3810. $this->assertHtml($expected, $result);
  3811. // Custom default value overrides default value from schema
  3812. $result = $this->Form->text('title', ['default' => 'override default']);
  3813. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  3814. $this->assertHtml($expected, $result);
  3815. // Default value from schema is used only for new entities.
  3816. $entity->isNew(false);
  3817. $result = $this->Form->text('title');
  3818. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  3819. $this->assertHtml($expected, $result);
  3820. }
  3821. /**
  3822. * testError method
  3823. *
  3824. * Test field error generation
  3825. *
  3826. * @return void
  3827. */
  3828. public function testError()
  3829. {
  3830. $this->article['errors'] = [
  3831. 'Article' => ['field' => 'email']
  3832. ];
  3833. $this->Form->create($this->article);
  3834. $result = $this->Form->error('Article.field');
  3835. $expected = [
  3836. ['div' => ['class' => 'error-message']],
  3837. 'email',
  3838. '/div',
  3839. ];
  3840. $this->assertHtml($expected, $result);
  3841. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  3842. $expected = [
  3843. ['div' => ['class' => 'error-message']],
  3844. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3845. '/div',
  3846. ];
  3847. $this->assertHtml($expected, $result);
  3848. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3849. $expected = [
  3850. ['div' => ['class' => 'error-message']],
  3851. '<strong', 'Badness!', '/strong',
  3852. '/div',
  3853. ];
  3854. $this->assertHtml($expected, $result);
  3855. }
  3856. /**
  3857. * test error translation can use rule names for translating.
  3858. *
  3859. * @return void
  3860. */
  3861. public function testErrorRuleName()
  3862. {
  3863. $this->article['errors'] = [
  3864. 'Article' => [
  3865. 'field' => ['email' => 'Your email was not good']
  3866. ]
  3867. ];
  3868. $this->Form->create($this->article);
  3869. $result = $this->Form->error('Article.field');
  3870. $expected = [
  3871. ['div' => ['class' => 'error-message']],
  3872. 'Your email was not good',
  3873. '/div',
  3874. ];
  3875. $this->assertHtml($expected, $result);
  3876. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  3877. $expected = [
  3878. ['div' => ['class' => 'error-message']],
  3879. 'Email in use',
  3880. '/div',
  3881. ];
  3882. $this->assertHtml($expected, $result);
  3883. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  3884. $expected = [
  3885. ['div' => ['class' => 'error-message']],
  3886. 'Email in use',
  3887. '/div',
  3888. ];
  3889. $this->assertHtml($expected, $result);
  3890. $result = $this->Form->error('Article.field', [
  3891. 'email' => 'Key is preferred',
  3892. 'Your email was not good' => 'Email in use'
  3893. ]);
  3894. $expected = [
  3895. ['div' => ['class' => 'error-message']],
  3896. 'Key is preferred',
  3897. '/div',
  3898. ];
  3899. $this->assertHtml($expected, $result);
  3900. }
  3901. /**
  3902. * Test error with nested lists.
  3903. *
  3904. * @return void
  3905. */
  3906. public function testErrorMessages()
  3907. {
  3908. $this->article['errors'] = [
  3909. 'Article' => ['field' => 'email']
  3910. ];
  3911. $this->Form->create($this->article);
  3912. $result = $this->Form->error('Article.field', [
  3913. 'email' => 'No good!'
  3914. ]);
  3915. $expected = [
  3916. 'div' => ['class' => 'error-message'],
  3917. 'No good!',
  3918. '/div'
  3919. ];
  3920. $this->assertHtml($expected, $result);
  3921. }
  3922. /**
  3923. * test error() with multiple messages.
  3924. *
  3925. * @return void
  3926. */
  3927. public function testErrorMultipleMessages()
  3928. {
  3929. $this->article['errors'] = [
  3930. 'field' => ['notBlank', 'email', 'Something else']
  3931. ];
  3932. $this->Form->create($this->article);
  3933. $result = $this->Form->error('field', [
  3934. 'notBlank' => 'Cannot be empty',
  3935. 'email' => 'No good!'
  3936. ]);
  3937. $expected = [
  3938. 'div' => ['class' => 'error-message'],
  3939. 'ul' => [],
  3940. '<li', 'Cannot be empty', '/li',
  3941. '<li', 'No good!', '/li',
  3942. '<li', 'Something else', '/li',
  3943. '/ul',
  3944. '/div'
  3945. ];
  3946. $this->assertHtml($expected, $result);
  3947. }
  3948. /**
  3949. * testPassword method
  3950. *
  3951. * Test password element generation
  3952. *
  3953. * @return void
  3954. */
  3955. public function testPassword()
  3956. {
  3957. $this->article['errors'] = [
  3958. 'Contact' => [
  3959. 'passwd' => 1
  3960. ]
  3961. ];
  3962. $this->Form->create($this->article);
  3963. $result = $this->Form->password('Contact.field');
  3964. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  3965. $this->assertHtml($expected, $result);
  3966. $this->Form->request->data['Contact']['passwd'] = 'test';
  3967. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  3968. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  3969. $this->assertHtml($expected, $result);
  3970. }
  3971. /**
  3972. * testRadio method
  3973. *
  3974. * Test radio element set generation
  3975. *
  3976. * @return void
  3977. */
  3978. public function testRadio()
  3979. {
  3980. $result = $this->Form->radio('Model.field', ['option A']);
  3981. $expected = [
  3982. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3983. 'label' => ['for' => 'model-field-0'],
  3984. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  3985. 'option A',
  3986. '/label'
  3987. ];
  3988. $this->assertHtml($expected, $result);
  3989. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  3990. $this->assertHtml($expected, $result);
  3991. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  3992. $expected = [
  3993. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3994. ['label' => ['for' => 'model-field-0']],
  3995. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  3996. 'option A',
  3997. '/label',
  3998. ['label' => ['for' => 'model-field-1']],
  3999. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4000. 'option B',
  4001. '/label',
  4002. ];
  4003. $this->assertHtml($expected, $result);
  4004. $result = $this->Form->radio(
  4005. 'Employee.gender',
  4006. ['male' => 'Male', 'female' => 'Female'],
  4007. ['form' => 'my-form']
  4008. );
  4009. $expected = [
  4010. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4011. ['label' => ['for' => 'employee-gender-male']],
  4012. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4013. 'Male',
  4014. '/label',
  4015. ['label' => ['for' => 'employee-gender-female']],
  4016. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4017. 'Female',
  4018. '/label',
  4019. ];
  4020. $this->assertHtml($expected, $result);
  4021. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4022. $expected = [
  4023. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4024. ['label' => ['for' => 'model-custom-0']],
  4025. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4026. 'option A',
  4027. '/label',
  4028. ['label' => ['for' => 'model-custom-1']],
  4029. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4030. 'option B',
  4031. '/label',
  4032. ];
  4033. $this->assertHtml($expected, $result);
  4034. $result = $this->Form->radio(
  4035. 'Employee.gender',
  4036. [
  4037. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4038. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4039. ]
  4040. );
  4041. $expected = [
  4042. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4043. ['label' => ['for' => 'employee-gender-male']],
  4044. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4045. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4046. 'Male',
  4047. '/label',
  4048. ['label' => ['for' => 'employee-gender-female']],
  4049. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4050. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4051. 'Female',
  4052. '/label',
  4053. ];
  4054. $this->assertHtml($expected, $result);
  4055. }
  4056. /**
  4057. * Test default value setting on radio() method
  4058. *
  4059. * @return void
  4060. */
  4061. public function testRadioDefaultValue()
  4062. {
  4063. $Articles = TableRegistry::get('Articles');
  4064. $title = $Articles->schema()->column('title');
  4065. $Articles->schema()->addColumn(
  4066. 'title',
  4067. ['default' => '1'] + $title
  4068. );
  4069. $this->Form->create($Articles->newEntity());
  4070. $result = $this->Form->radio('title', ['option A', 'option B']);
  4071. $expected = [
  4072. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4073. ['label' => ['for' => 'title-0']],
  4074. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4075. 'option A',
  4076. '/label',
  4077. ['label' => ['for' => 'title-1']],
  4078. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4079. 'option B',
  4080. '/label',
  4081. ];
  4082. $this->assertHtml($expected, $result);
  4083. }
  4084. /**
  4085. * Test that input works with radio types
  4086. *
  4087. * @return void
  4088. */
  4089. public function testInputRadio()
  4090. {
  4091. $result = $this->Form->input('test', [
  4092. 'type' => 'radio',
  4093. 'options' => ['A', 'B'],
  4094. ]);
  4095. $expected = [
  4096. ['div' => ['class' => 'input radio']],
  4097. '<label',
  4098. 'Test',
  4099. '/label',
  4100. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4101. ['label' => ['for' => 'test-0']],
  4102. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4103. 'A',
  4104. '/label',
  4105. ['label' => ['for' => 'test-1']],
  4106. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4107. 'B',
  4108. '/label',
  4109. '/div',
  4110. ];
  4111. $this->assertHtml($expected, $result);
  4112. $result = $this->Form->input('test', [
  4113. 'type' => 'radio',
  4114. 'options' => ['A', 'B'],
  4115. 'value' => '0'
  4116. ]);
  4117. $expected = [
  4118. ['div' => ['class' => 'input radio']],
  4119. '<label',
  4120. 'Test',
  4121. '/label',
  4122. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4123. ['label' => ['for' => 'test-0']],
  4124. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4125. 'A',
  4126. '/label',
  4127. ['label' => ['for' => 'test-1']],
  4128. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4129. 'B',
  4130. '/label',
  4131. '/div',
  4132. ];
  4133. $this->assertHtml($expected, $result);
  4134. $result = $this->Form->input('test', [
  4135. 'type' => 'radio',
  4136. 'options' => ['A', 'B'],
  4137. 'label' => false
  4138. ]);
  4139. $expected = [
  4140. ['div' => ['class' => 'input radio']],
  4141. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4142. ['label' => ['for' => 'test-0']],
  4143. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4144. 'A',
  4145. '/label',
  4146. ['label' => ['for' => 'test-1']],
  4147. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4148. 'B',
  4149. '/label',
  4150. '/div',
  4151. ];
  4152. $this->assertHtml($expected, $result);
  4153. }
  4154. /**
  4155. * Test that radio() works with label = false
  4156. *
  4157. * @return void
  4158. */
  4159. public function testRadioNoLabel()
  4160. {
  4161. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4162. $expected = [
  4163. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4164. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4165. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4166. ];
  4167. $this->assertHtml($expected, $result);
  4168. }
  4169. /**
  4170. * test generating radio input inside label ala twitter bootstrap
  4171. *
  4172. * @return void
  4173. */
  4174. public function testRadioInputInsideLabel()
  4175. {
  4176. $this->Form->templates([
  4177. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4178. 'radioWrapper' => '{{label}}'
  4179. ]);
  4180. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4181. //@codingStandardsIgnoreStart
  4182. $expected = [
  4183. ['input' => [
  4184. 'type' => 'hidden',
  4185. 'name' => 'Model[field]',
  4186. 'value' => ''
  4187. ]],
  4188. ['label' => ['for' => 'model-field-0']],
  4189. ['input' => [
  4190. 'type' => 'radio',
  4191. 'name' => 'Model[field]',
  4192. 'value' => '0',
  4193. 'id' => 'model-field-0'
  4194. ]],
  4195. 'option A',
  4196. '/label',
  4197. ['label' => ['for' => 'model-field-1']],
  4198. ['input' => [
  4199. 'type' => 'radio',
  4200. 'name' => 'Model[field]',
  4201. 'value' => '1',
  4202. 'id' => 'model-field-1'
  4203. ]],
  4204. 'option B',
  4205. '/label',
  4206. ];
  4207. //@codingStandardsIgnoreEnd
  4208. $this->assertHtml($expected, $result);
  4209. }
  4210. /**
  4211. * test disabling the hidden input for radio buttons
  4212. *
  4213. * @return void
  4214. */
  4215. public function testRadioHiddenInputDisabling()
  4216. {
  4217. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4218. $expected = [
  4219. 'label' => ['for' => 'model-1-field-0'],
  4220. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4221. 'option A',
  4222. '/label'
  4223. ];
  4224. $this->assertHtml($expected, $result);
  4225. }
  4226. /**
  4227. * testRadio method
  4228. *
  4229. * Test radio element set generation
  4230. *
  4231. * @return void
  4232. */
  4233. public function testRadioOutOfRange()
  4234. {
  4235. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4236. $expected = [
  4237. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4238. 'label' => ['for' => 'model-field-v'],
  4239. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4240. 'value',
  4241. '/label'
  4242. ];
  4243. $this->assertHtml($expected, $result);
  4244. }
  4245. /**
  4246. * testSelect method
  4247. *
  4248. * Test select element generation.
  4249. *
  4250. * @return void
  4251. */
  4252. public function testSelect()
  4253. {
  4254. $result = $this->Form->select('Model.field', []);
  4255. $expected = [
  4256. 'select' => ['name' => 'Model[field]'],
  4257. '/select'
  4258. ];
  4259. $this->assertHtml($expected, $result);
  4260. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4261. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4262. $expected = [
  4263. 'select' => ['name' => 'Model[field]'],
  4264. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4265. 'good',
  4266. '/option',
  4267. ['option' => ['value' => 'other']],
  4268. 'bad',
  4269. '/option',
  4270. '/select'
  4271. ];
  4272. $this->assertHtml($expected, $result);
  4273. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4274. $this->assertHtml($expected, $result);
  4275. $this->Form->request->data = [];
  4276. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4277. $expected = [
  4278. 'select' => ['name' => 'Model[field]'],
  4279. ['option' => ['value' => 'value']],
  4280. 'good',
  4281. '/option',
  4282. ['option' => ['value' => 'other']],
  4283. 'bad',
  4284. '/option',
  4285. '/select'
  4286. ];
  4287. $this->assertHtml($expected, $result);
  4288. $options = [
  4289. ['value' => 'first', 'text' => 'First'],
  4290. ['value' => 'first', 'text' => 'Another First'],
  4291. ];
  4292. $result = $this->Form->select(
  4293. 'Model.field',
  4294. $options,
  4295. ['escape' => false, 'empty' => false]
  4296. );
  4297. $expected = [
  4298. 'select' => ['name' => 'Model[field]'],
  4299. ['option' => ['value' => 'first']],
  4300. 'First',
  4301. '/option',
  4302. ['option' => ['value' => 'first']],
  4303. 'Another First',
  4304. '/option',
  4305. '/select'
  4306. ];
  4307. $this->assertHtml($expected, $result);
  4308. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4309. $result = $this->Form->select(
  4310. 'Model.contact_id',
  4311. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4312. ['escape' => false, 'empty' => 'pick something']
  4313. );
  4314. $expected = [
  4315. 'select' => ['name' => 'Model[contact_id]'],
  4316. ['option' => ['value' => '']], 'pick something', '/option',
  4317. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4318. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4319. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4320. '/select'
  4321. ];
  4322. $this->assertHtml($expected, $result);
  4323. $this->Form->request->data['Model']['field'] = 0;
  4324. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4325. $expected = [
  4326. 'select' => ['name' => 'Model[field]'],
  4327. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4328. ['option' => ['value' => '1']], 'Yes', '/option',
  4329. '/select'
  4330. ];
  4331. $this->assertHtml($expected, $result);
  4332. }
  4333. /**
  4334. * Test that select() escapes HTML.
  4335. *
  4336. * @return void
  4337. */
  4338. public function testSelectEscapeHtml()
  4339. {
  4340. $result = $this->Form->select(
  4341. 'Model.field',
  4342. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4343. ['empty' => false]
  4344. );
  4345. $expected = [
  4346. 'select' => ['name' => 'Model[field]'],
  4347. ['option' => ['value' => 'first']],
  4348. 'first &quot;html&quot; &lt;chars&gt;',
  4349. '/option',
  4350. ['option' => ['value' => 'second']],
  4351. 'value',
  4352. '/option',
  4353. '/select'
  4354. ];
  4355. $this->assertHtml($expected, $result);
  4356. $result = $this->Form->select(
  4357. 'Model.field',
  4358. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4359. ['escape' => false, 'empty' => false]
  4360. );
  4361. $expected = [
  4362. 'select' => ['name' => 'Model[field]'],
  4363. ['option' => ['value' => 'first']],
  4364. 'first "html" <chars>',
  4365. '/option',
  4366. ['option' => ['value' => 'second']],
  4367. 'value',
  4368. '/option',
  4369. '/select'
  4370. ];
  4371. $this->assertHtml($expected, $result);
  4372. }
  4373. /**
  4374. * test select() with required and disabled attributes.
  4375. *
  4376. * @return void
  4377. */
  4378. public function testSelectRequired()
  4379. {
  4380. $this->article['required'] = [
  4381. 'user_id' => true
  4382. ];
  4383. $this->Form->create($this->article);
  4384. $result = $this->Form->select('user_id', ['option A']);
  4385. $expected = [
  4386. 'select' => [
  4387. 'name' => 'user_id',
  4388. 'required' => 'required'
  4389. ],
  4390. ['option' => ['value' => '0']], 'option A', '/option',
  4391. '/select'
  4392. ];
  4393. $this->assertHtml($expected, $result);
  4394. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4395. $expected = [
  4396. 'select' => [
  4397. 'name' => 'user_id',
  4398. 'disabled' => 'disabled'
  4399. ],
  4400. ['option' => ['value' => '0']], 'option A', '/option',
  4401. '/select'
  4402. ];
  4403. $this->assertHtml($expected, $result);
  4404. }
  4405. /**
  4406. * testNestedSelect method
  4407. *
  4408. * test select element generation with optgroups
  4409. *
  4410. * @return void
  4411. */
  4412. public function testNestedSelect()
  4413. {
  4414. $result = $this->Form->select(
  4415. 'Model.field',
  4416. [1 => 'One', 2 => 'Two', 'Three' => [
  4417. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4418. ]],
  4419. ['empty' => false]
  4420. );
  4421. $expected = [
  4422. 'select' => ['name' => 'Model[field]'],
  4423. ['option' => ['value' => 1]],
  4424. 'One',
  4425. '/option',
  4426. ['option' => ['value' => 2]],
  4427. 'Two',
  4428. '/option',
  4429. ['optgroup' => ['label' => 'Three']],
  4430. ['option' => ['value' => 3]],
  4431. 'Three',
  4432. '/option',
  4433. ['option' => ['value' => 4]],
  4434. 'Four',
  4435. '/option',
  4436. ['option' => ['value' => 5]],
  4437. 'Five',
  4438. '/option',
  4439. '/optgroup',
  4440. '/select'
  4441. ];
  4442. $this->assertHtml($expected, $result);
  4443. }
  4444. /**
  4445. * testSelectMultiple method
  4446. *
  4447. * test generation of multiple select elements
  4448. *
  4449. * @return void
  4450. */
  4451. public function testSelectMultiple()
  4452. {
  4453. $options = ['first', 'second', 'third'];
  4454. $result = $this->Form->select(
  4455. 'Model.multi_field',
  4456. $options,
  4457. ['form' => 'my-form', 'multiple' => true]
  4458. );
  4459. $expected = [
  4460. 'input' => [
  4461. 'type' => 'hidden',
  4462. 'name' => 'Model[multi_field]',
  4463. 'value' => '',
  4464. 'form' => 'my-form',
  4465. ],
  4466. 'select' => [
  4467. 'name' => 'Model[multi_field][]',
  4468. 'multiple' => 'multiple',
  4469. 'form' => 'my-form',
  4470. ],
  4471. ['option' => ['value' => '0']],
  4472. 'first',
  4473. '/option',
  4474. ['option' => ['value' => '1']],
  4475. 'second',
  4476. '/option',
  4477. ['option' => ['value' => '2']],
  4478. 'third',
  4479. '/option',
  4480. '/select'
  4481. ];
  4482. $this->assertHtml($expected, $result);
  4483. $result = $this->Form->select(
  4484. 'Model.multi_field',
  4485. $options,
  4486. ['multiple' => 'multiple', 'form' => 'my-form']
  4487. );
  4488. $this->assertHtml($expected, $result);
  4489. }
  4490. /**
  4491. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4492. *
  4493. * @return void
  4494. */
  4495. public function testCheckboxZeroValue()
  4496. {
  4497. $result = $this->Form->input('User.get_spam', [
  4498. 'type' => 'checkbox',
  4499. 'value' => '0',
  4500. 'hiddenField' => '1',
  4501. ]);
  4502. $expected = [
  4503. 'div' => ['class' => 'input checkbox'],
  4504. 'label' => ['for' => 'user-get-spam'],
  4505. ['input' => [
  4506. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4507. 'value' => '1'
  4508. ]],
  4509. ['input' => [
  4510. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4511. 'value' => '0', 'id' => 'user-get-spam'
  4512. ]],
  4513. 'Get Spam',
  4514. '/label',
  4515. '/div'
  4516. ];
  4517. $this->assertHtml($expected, $result);
  4518. }
  4519. /**
  4520. * test generation of habtm select boxes.
  4521. *
  4522. * @return void
  4523. */
  4524. public function testHabtmSelectBox()
  4525. {
  4526. $this->loadFixtures('Articles');
  4527. $options = [
  4528. 1 => 'blue',
  4529. 2 => 'red',
  4530. 3 => 'green'
  4531. ];
  4532. $tags = [
  4533. new Entity(['id' => 1, 'name' => 'blue']),
  4534. new Entity(['id' => 3, 'name' => 'green'])
  4535. ];
  4536. $article = new Article(['tags' => $tags]);
  4537. $this->Form->create($article);
  4538. $result = $this->Form->input('tags._ids', ['options' => $options]);
  4539. $expected = [
  4540. 'div' => ['class' => 'input select'],
  4541. 'label' => ['for' => 'tags-ids'],
  4542. 'Tags',
  4543. '/label',
  4544. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4545. 'select' => [
  4546. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4547. 'multiple' => 'multiple'
  4548. ],
  4549. ['option' => ['value' => '1', 'selected' => 'selected']],
  4550. 'blue',
  4551. '/option',
  4552. ['option' => ['value' => '2']],
  4553. 'red',
  4554. '/option',
  4555. ['option' => ['value' => '3', 'selected' => 'selected']],
  4556. 'green',
  4557. '/option',
  4558. '/select',
  4559. '/div'
  4560. ];
  4561. $this->assertHtml($expected, $result);
  4562. // make sure only 50 is selected, and not 50f5c0cf
  4563. $options = [
  4564. '1' => 'blue',
  4565. '50f5c0cf' => 'red',
  4566. '50' => 'green'
  4567. ];
  4568. $tags = [
  4569. new Entity(['id' => 1, 'name' => 'blue']),
  4570. new Entity(['id' => 50, 'name' => 'green'])
  4571. ];
  4572. $article = new Article(['tags' => $tags]);
  4573. $this->Form->create($article);
  4574. $result = $this->Form->input('tags._ids', ['options' => $options]);
  4575. $expected = [
  4576. 'div' => ['class' => 'input select'],
  4577. 'label' => ['for' => 'tags-ids'],
  4578. 'Tags',
  4579. '/label',
  4580. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4581. 'select' => [
  4582. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4583. 'multiple' => 'multiple'
  4584. ],
  4585. ['option' => ['value' => '1', 'selected' => 'selected']],
  4586. 'blue',
  4587. '/option',
  4588. ['option' => ['value' => '50f5c0cf']],
  4589. 'red',
  4590. '/option',
  4591. ['option' => ['value' => '50', 'selected' => 'selected']],
  4592. 'green',
  4593. '/option',
  4594. '/select',
  4595. '/div'
  4596. ];
  4597. $this->assertHtml($expected, $result);
  4598. $spacecraft = [
  4599. 1 => 'Orion',
  4600. 2 => 'Helios'
  4601. ];
  4602. $this->View->viewVars['spacecraft'] = $spacecraft;
  4603. $this->Form->create();
  4604. $result = $this->Form->input('spacecraft._ids');
  4605. $expected = [
  4606. 'div' => ['class' => 'input select'],
  4607. 'label' => ['for' => 'spacecraft-ids'],
  4608. 'Spacecraft',
  4609. '/label',
  4610. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4611. 'select' => [
  4612. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4613. 'multiple' => 'multiple'
  4614. ],
  4615. ['option' => ['value' => '1']],
  4616. 'Orion',
  4617. '/option',
  4618. ['option' => ['value' => '2']],
  4619. 'Helios',
  4620. '/option',
  4621. '/select',
  4622. '/div'
  4623. ];
  4624. $this->assertHtml($expected, $result);
  4625. }
  4626. /**
  4627. * Tests that errors for belongsToMany select fields are being
  4628. * picked up properly.
  4629. *
  4630. * @return void
  4631. */
  4632. public function testErrorsForBelongsToManySelect()
  4633. {
  4634. $spacecraft = [
  4635. 1 => 'Orion',
  4636. 2 => 'Helios'
  4637. ];
  4638. $this->View->viewVars['spacecraft'] = $spacecraft;
  4639. $article = new Article();
  4640. $article->errors('spacecraft', ['Invalid']);
  4641. $this->Form->create($article);
  4642. $result = $this->Form->input('spacecraft._ids');
  4643. $expected = [
  4644. ['div' => ['class' => 'input select error']],
  4645. 'label' => ['for' => 'spacecraft-ids'],
  4646. 'Spacecraft',
  4647. '/label',
  4648. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4649. 'select' => [
  4650. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4651. 'multiple' => 'multiple'
  4652. ],
  4653. ['option' => ['value' => '1']],
  4654. 'Orion',
  4655. '/option',
  4656. ['option' => ['value' => '2']],
  4657. 'Helios',
  4658. '/option',
  4659. '/select',
  4660. ['div' => ['class' => 'error-message']],
  4661. 'Invalid',
  4662. '/div',
  4663. '/div'
  4664. ];
  4665. $this->assertHtml($expected, $result);
  4666. }
  4667. /**
  4668. * test generation of multi select elements in checkbox format
  4669. *
  4670. * @return void
  4671. */
  4672. public function testSelectMultipleCheckboxes()
  4673. {
  4674. $result = $this->Form->select(
  4675. 'Model.multi_field',
  4676. ['first', 'second', 'third'],
  4677. ['multiple' => 'checkbox']
  4678. );
  4679. $expected = [
  4680. 'input' => [
  4681. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4682. ],
  4683. ['div' => ['class' => 'checkbox']],
  4684. ['label' => ['for' => 'model-multi-field-0']],
  4685. ['input' => [
  4686. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4687. 'value' => '0', 'id' => 'model-multi-field-0'
  4688. ]],
  4689. 'first',
  4690. '/label',
  4691. '/div',
  4692. ['div' => ['class' => 'checkbox']],
  4693. ['label' => ['for' => 'model-multi-field-1']],
  4694. ['input' => [
  4695. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4696. 'value' => '1', 'id' => 'model-multi-field-1'
  4697. ]],
  4698. 'second',
  4699. '/label',
  4700. '/div',
  4701. ['div' => ['class' => 'checkbox']],
  4702. ['label' => ['for' => 'model-multi-field-2']],
  4703. ['input' => [
  4704. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4705. 'value' => '2', 'id' => 'model-multi-field-2'
  4706. ]],
  4707. 'third',
  4708. '/label',
  4709. '/div'
  4710. ];
  4711. $this->assertHtml($expected, $result);
  4712. $result = $this->Form->select(
  4713. 'Model.multi_field',
  4714. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4715. ['multiple' => 'checkbox']
  4716. );
  4717. $expected = [
  4718. 'input' => [
  4719. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4720. ],
  4721. ['div' => ['class' => 'checkbox']],
  4722. ['label' => ['for' => 'model-multi-field-a+']],
  4723. ['input' => [
  4724. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4725. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  4726. ]],
  4727. 'first',
  4728. '/label',
  4729. '/div',
  4730. ['div' => ['class' => 'checkbox']],
  4731. ['label' => ['for' => 'model-multi-field-a++']],
  4732. ['input' => [
  4733. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4734. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  4735. ]],
  4736. 'second',
  4737. '/label',
  4738. '/div',
  4739. ['div' => ['class' => 'checkbox']],
  4740. ['label' => ['for' => 'model-multi-field-a+++']],
  4741. ['input' => [
  4742. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4743. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  4744. ]],
  4745. 'third',
  4746. '/label',
  4747. '/div'
  4748. ];
  4749. $this->assertHtml($expected, $result);
  4750. $result = $this->Form->select(
  4751. 'Model.multi_field',
  4752. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4753. ['multiple' => 'checkbox']
  4754. );
  4755. $expected = [
  4756. 'input' => [
  4757. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4758. ],
  4759. ['div' => ['class' => 'checkbox']],
  4760. ['label' => ['for' => 'model-multi-field-a-b']],
  4761. ['input' => [
  4762. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4763. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  4764. ]],
  4765. 'first',
  4766. '/label',
  4767. '/div',
  4768. ['div' => ['class' => 'checkbox']],
  4769. ['label' => ['for' => 'model-multi-field-a-b1']],
  4770. ['input' => [
  4771. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4772. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  4773. ]],
  4774. 'second',
  4775. '/label',
  4776. '/div',
  4777. ['div' => ['class' => 'checkbox']],
  4778. ['label' => ['for' => 'model-multi-field-a-b2']],
  4779. ['input' => [
  4780. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4781. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  4782. ]],
  4783. 'third',
  4784. '/label',
  4785. '/div'
  4786. ];
  4787. $this->assertHtml($expected, $result);
  4788. }
  4789. /**
  4790. * Ensure that multiCheckbox reads from the request data.
  4791. *
  4792. * @return void
  4793. */
  4794. public function testSelectMultipleCheckboxRequestData()
  4795. {
  4796. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  4797. $result = $this->Form->select(
  4798. 'Model.tags',
  4799. ['1' => 'first', 'Array' => 'Array'],
  4800. ['multiple' => 'checkbox']
  4801. );
  4802. $expected = [
  4803. 'input' => [
  4804. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  4805. ],
  4806. ['div' => ['class' => 'checkbox']],
  4807. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  4808. ['input' => [
  4809. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4810. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  4811. ]],
  4812. 'first',
  4813. '/label',
  4814. '/div',
  4815. ['div' => ['class' => 'checkbox']],
  4816. ['label' => ['for' => 'model-tags-array']],
  4817. ['input' => [
  4818. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4819. 'value' => 'Array', 'id' => 'model-tags-array'
  4820. ]],
  4821. 'Array',
  4822. '/label',
  4823. '/div'
  4824. ];
  4825. $this->assertHtml($expected, $result);
  4826. }
  4827. /**
  4828. * Checks the security hash array generated for multiple-input checkbox elements
  4829. *
  4830. * @return void
  4831. */
  4832. public function testSelectMultipleCheckboxSecurity()
  4833. {
  4834. $this->Form->request->params['_Token'] = 'testKey';
  4835. $this->assertEquals([], $this->Form->fields);
  4836. $result = $this->Form->select(
  4837. 'Model.multi_field',
  4838. ['1' => 'first', '2' => 'second', '3' => 'third'],
  4839. ['multiple' => 'checkbox']
  4840. );
  4841. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  4842. $result = $this->Form->secure($this->Form->fields);
  4843. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4844. $this->assertRegExp('/"' . $key . '"/', $result);
  4845. }
  4846. /**
  4847. * Multiple select elements should always be secured as they always participate
  4848. * in the POST data.
  4849. *
  4850. * @return void
  4851. */
  4852. public function testSelectMultipleSecureWithNoOptions()
  4853. {
  4854. $this->assertEquals([], $this->Form->fields);
  4855. $this->Form->select(
  4856. 'Model.select',
  4857. [],
  4858. ['multiple' => true]
  4859. );
  4860. $this->assertEquals(['Model.select'], $this->Form->fields);
  4861. }
  4862. /**
  4863. * When a select box has no options it should not be added to the fields list
  4864. * as it always fail post validation.
  4865. *
  4866. * @return void
  4867. */
  4868. public function testSelectNoSecureWithNoOptions()
  4869. {
  4870. $this->Form->request->params['_Token'] = 'testkey';
  4871. $this->assertEquals([], $this->Form->fields);
  4872. $this->Form->select(
  4873. 'Model.select',
  4874. []
  4875. );
  4876. $this->assertEquals([], $this->Form->fields);
  4877. $this->Form->select(
  4878. 'Model.user_id',
  4879. [],
  4880. ['empty' => true]
  4881. );
  4882. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  4883. }
  4884. /**
  4885. * testInputMultipleCheckboxes method
  4886. *
  4887. * test input() resulting in multi select elements being generated.
  4888. *
  4889. * @return void
  4890. */
  4891. public function testInputMultipleCheckboxes()
  4892. {
  4893. $result = $this->Form->input('Model.multi_field', [
  4894. 'options' => ['first', 'second', 'third'],
  4895. 'multiple' => 'checkbox'
  4896. ]);
  4897. $expected = [
  4898. ['div' => ['class' => 'input select']],
  4899. ['label' => ['for' => 'model-multi-field']],
  4900. 'Multi Field',
  4901. '/label',
  4902. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  4903. ['div' => ['class' => 'checkbox']],
  4904. ['label' => ['for' => 'model-multi-field-0']],
  4905. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  4906. 'first',
  4907. '/label',
  4908. '/div',
  4909. ['div' => ['class' => 'checkbox']],
  4910. ['label' => ['for' => 'model-multi-field-1']],
  4911. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  4912. 'second',
  4913. '/label',
  4914. '/div',
  4915. ['div' => ['class' => 'checkbox']],
  4916. ['label' => ['for' => 'model-multi-field-2']],
  4917. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  4918. 'third',
  4919. '/label',
  4920. '/div',
  4921. '/div'
  4922. ];
  4923. $this->assertHtml($expected, $result);
  4924. $result = $this->Form->input('Model.multi_field', [
  4925. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  4926. 'multiple' => 'checkbox'
  4927. ]);
  4928. $expected = [
  4929. ['div' => ['class' => 'input select']],
  4930. ['label' => ['for' => 'model-multi-field']],
  4931. 'Multi Field',
  4932. '/label',
  4933. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  4934. ['div' => ['class' => 'checkbox']],
  4935. ['label' => ['for' => 'model-multi-field-a']],
  4936. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  4937. 'first',
  4938. '/label',
  4939. '/div',
  4940. ['div' => ['class' => 'checkbox']],
  4941. ['label' => ['for' => 'model-multi-field-b']],
  4942. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  4943. 'second',
  4944. '/label',
  4945. '/div',
  4946. ['div' => ['class' => 'checkbox']],
  4947. ['label' => ['for' => 'model-multi-field-c']],
  4948. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  4949. 'third',
  4950. '/label',
  4951. '/div',
  4952. '/div'
  4953. ];
  4954. $this->assertHtml($expected, $result);
  4955. }
  4956. /**
  4957. * testSelectHiddenFieldOmission method
  4958. *
  4959. * test that select() with 'hiddenField' => false omits the hidden field
  4960. *
  4961. * @return void
  4962. */
  4963. public function testSelectHiddenFieldOmission()
  4964. {
  4965. $result = $this->Form->select(
  4966. 'Model.multi_field',
  4967. ['first', 'second'],
  4968. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  4969. );
  4970. $this->assertNotContains('type="hidden"', $result);
  4971. }
  4972. /**
  4973. * test that select() with multiple = checkbox works with overriding name attribute.
  4974. *
  4975. * @return void
  4976. */
  4977. public function testSelectCheckboxMultipleOverrideName()
  4978. {
  4979. $result = $this->Form->select('category', ['1', '2'], [
  4980. 'multiple' => 'checkbox',
  4981. 'name' => 'fish',
  4982. ]);
  4983. $expected = [
  4984. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  4985. ['div' => ['class' => 'checkbox']],
  4986. ['label' => ['for' => 'fish-0']],
  4987. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  4988. '1',
  4989. '/label',
  4990. '/div',
  4991. ['div' => ['class' => 'checkbox']],
  4992. ['label' => ['for' => 'fish-1']],
  4993. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  4994. '2',
  4995. '/label',
  4996. '/div'
  4997. ];
  4998. $this->assertHtml($expected, $result);
  4999. $result = $this->Form->multiCheckbox(
  5000. 'category',
  5001. new Collection(['1', '2']),
  5002. ['name' => 'fish']
  5003. );
  5004. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5005. 'name' => 'fish',
  5006. ]);
  5007. $this->assertHtml($expected, $result);
  5008. }
  5009. /**
  5010. * Test that input() works with multicheckbox.
  5011. *
  5012. * @return void
  5013. */
  5014. public function testInputMultiCheckbox()
  5015. {
  5016. $result = $this->Form->input('category', [
  5017. 'type' => 'multicheckbox',
  5018. 'options' => ['1', '2'],
  5019. ]);
  5020. $expected = [
  5021. ['div' => ['class' => 'input multicheckbox']],
  5022. '<label',
  5023. 'Category',
  5024. '/label',
  5025. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5026. ['div' => ['class' => 'checkbox']],
  5027. ['label' => ['for' => 'category-0']],
  5028. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5029. '1',
  5030. '/label',
  5031. '/div',
  5032. ['div' => ['class' => 'checkbox']],
  5033. ['label' => ['for' => 'category-1']],
  5034. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5035. '2',
  5036. '/label',
  5037. '/div',
  5038. '/div',
  5039. ];
  5040. $this->assertHtml($expected, $result);
  5041. }
  5042. /**
  5043. * testCheckbox method
  5044. *
  5045. * Test generation of checkboxes
  5046. *
  5047. * @return void
  5048. */
  5049. public function testCheckbox()
  5050. {
  5051. $result = $this->Form->checkbox('Model.field');
  5052. $expected = [
  5053. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5054. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5055. ];
  5056. $this->assertHtml($expected, $result);
  5057. $result = $this->Form->checkbox('Model.field', [
  5058. 'id' => 'theID',
  5059. 'value' => 'myvalue',
  5060. 'form' => 'my-form',
  5061. ]);
  5062. $expected = [
  5063. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5064. ['input' => [
  5065. 'type' => 'checkbox', 'name' => 'Model[field]',
  5066. 'value' => 'myvalue', 'id' => 'theID',
  5067. 'form' => 'my-form',
  5068. ]]
  5069. ];
  5070. $this->assertHtml($expected, $result);
  5071. }
  5072. /**
  5073. * testCheckboxDefaultValue method
  5074. *
  5075. * Test default value setting on checkbox() method
  5076. *
  5077. * @return void
  5078. */
  5079. public function testCheckboxDefaultValue()
  5080. {
  5081. $this->Form->request->data['Model']['field'] = false;
  5082. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5083. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5084. $this->assertHtml($expected, $result);
  5085. unset($this->Form->request->data['Model']['field']);
  5086. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5087. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5088. $this->assertHtml($expected, $result);
  5089. $this->Form->request->data['Model']['field'] = true;
  5090. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5091. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5092. $this->assertHtml($expected, $result);
  5093. unset($this->Form->request->data['Model']['field']);
  5094. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5095. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5096. $this->assertHtml($expected, $result);
  5097. $Articles = TableRegistry::get('Articles');
  5098. $Articles->schema()->addColumn(
  5099. 'published',
  5100. ['type' => 'boolean', 'null' => false, 'default' => true]
  5101. );
  5102. $this->Form->create($Articles->newEntity());
  5103. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5104. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5105. $this->assertHtml($expected, $result);
  5106. }
  5107. /**
  5108. * Test checkbox being checked or having errors.
  5109. *
  5110. * @return void
  5111. */
  5112. public function testCheckboxCheckedAndError()
  5113. {
  5114. $this->article['errors'] = [
  5115. 'published' => true
  5116. ];
  5117. $this->Form->request->data['published'] = 'myvalue';
  5118. $this->Form->create($this->article);
  5119. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5120. $expected = [
  5121. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5122. ['input' => [
  5123. 'type' => 'checkbox',
  5124. 'name' => 'published',
  5125. 'value' => 'myvalue',
  5126. 'id' => 'theID',
  5127. 'checked' => 'checked',
  5128. 'class' => 'form-error'
  5129. ]]
  5130. ];
  5131. $this->assertHtml($expected, $result);
  5132. $this->Form->request->data['published'] = '';
  5133. $result = $this->Form->checkbox('published');
  5134. $expected = [
  5135. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5136. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5137. ];
  5138. $this->assertHtml($expected, $result);
  5139. }
  5140. /**
  5141. * test checkbox() with a custom name attribute
  5142. *
  5143. * @return void
  5144. */
  5145. public function testCheckboxCustomNameAttribute()
  5146. {
  5147. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5148. $expected = [
  5149. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5150. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5151. ];
  5152. $this->assertHtml($expected, $result);
  5153. }
  5154. /**
  5155. * Test that the hidden input for checkboxes can be omitted or set to a
  5156. * specific value.
  5157. *
  5158. * @return void
  5159. */
  5160. public function testCheckboxHiddenField()
  5161. {
  5162. $result = $this->Form->checkbox('UserForm.something', [
  5163. 'hiddenField' => false
  5164. ]);
  5165. $expected = [
  5166. 'input' => [
  5167. 'type' => 'checkbox',
  5168. 'name' => 'UserForm[something]',
  5169. 'value' => '1'
  5170. ],
  5171. ];
  5172. $this->assertHtml($expected, $result);
  5173. $result = $this->Form->checkbox('UserForm.something', [
  5174. 'value' => 'Y',
  5175. 'hiddenField' => 'N',
  5176. ]);
  5177. $expected = [
  5178. ['input' => [
  5179. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5180. 'value' => 'N'
  5181. ]],
  5182. ['input' => [
  5183. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5184. 'value' => 'Y'
  5185. ]],
  5186. ];
  5187. $this->assertHtml($expected, $result);
  5188. }
  5189. /**
  5190. * Test the time type.
  5191. *
  5192. * @return void
  5193. */
  5194. public function testTime()
  5195. {
  5196. $result = $this->Form->time('start_time', [
  5197. 'timeFormat' => 12,
  5198. 'interval' => 5,
  5199. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5200. ]);
  5201. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5202. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5203. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5204. $this->assertNotContains('year', $result);
  5205. $this->assertNotContains('month', $result);
  5206. $this->assertNotContains('day', $result);
  5207. $result = $this->Form->time('start_time', [
  5208. 'timeFormat' => 12,
  5209. 'interval' => 5,
  5210. 'value' => '2014-03-08 16:30:00'
  5211. ]);
  5212. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5213. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5214. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5215. $this->assertNotContains('year', $result);
  5216. $this->assertNotContains('month', $result);
  5217. $this->assertNotContains('day', $result);
  5218. }
  5219. /**
  5220. * Ensure that timeFormat=24 has no merdian.
  5221. *
  5222. * @return void.
  5223. */
  5224. public function testTimeFormat24NoMeridian()
  5225. {
  5226. $result = $this->Form->time('start_time', [
  5227. 'timeFormat' => 24,
  5228. 'interval' => 5,
  5229. 'value' => '2014-03-08 16:30:00'
  5230. ]);
  5231. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5232. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5233. $this->assertNotContains('meridian', $result);
  5234. $this->assertNotContains('pm', $result);
  5235. $this->assertNotContains('year', $result);
  5236. $this->assertNotContains('month', $result);
  5237. $this->assertNotContains('day', $result);
  5238. }
  5239. /**
  5240. * Test the date type.
  5241. *
  5242. * @return void
  5243. */
  5244. public function testDate()
  5245. {
  5246. $result = $this->Form->date('start_day', [
  5247. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5248. ]);
  5249. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5250. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5251. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5252. $this->assertNotContains('hour', $result);
  5253. $this->assertNotContains('minute', $result);
  5254. $this->assertNotContains('second', $result);
  5255. $this->assertNotContains('meridian', $result);
  5256. }
  5257. /**
  5258. * testDateTime method
  5259. *
  5260. * Test generation of date/time select elements
  5261. *
  5262. * @return void
  5263. */
  5264. public function testDateTime()
  5265. {
  5266. extract($this->dateRegex);
  5267. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5268. $now = strtotime('now');
  5269. $expected = [
  5270. ['select' => ['name' => 'Contact[date][year]']],
  5271. ['option' => ['value' => '']],
  5272. '/option',
  5273. $yearsRegex,
  5274. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5275. date('Y', $now),
  5276. '/option',
  5277. '*/select',
  5278. ['select' => ['name' => 'Contact[date][month]']],
  5279. ['option' => ['value' => '']],
  5280. '/option',
  5281. $monthsRegex,
  5282. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5283. date('F', $now),
  5284. '/option',
  5285. '*/select',
  5286. ['select' => ['name' => 'Contact[date][day]']],
  5287. ['option' => ['value' => '']],
  5288. '/option',
  5289. $daysRegex,
  5290. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5291. date('j', $now),
  5292. '/option',
  5293. '*/select',
  5294. ['select' => ['name' => 'Contact[date][hour]']],
  5295. ['option' => ['value' => '']],
  5296. '/option',
  5297. $hoursRegex,
  5298. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5299. date('G', $now),
  5300. '/option',
  5301. '*/select',
  5302. ['select' => ['name' => 'Contact[date][minute]']],
  5303. ['option' => ['value' => '']],
  5304. '/option',
  5305. $minutesRegex,
  5306. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5307. date('i', $now),
  5308. '/option',
  5309. '*/select',
  5310. ];
  5311. $this->assertHtml($expected, $result);
  5312. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5313. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5314. $now = strtotime('now');
  5315. $expected = [
  5316. ['select' => ['name' => 'Contact[date][year]']],
  5317. $yearsRegex,
  5318. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5319. date('Y', $now),
  5320. '/option',
  5321. '*/select',
  5322. ['select' => ['name' => 'Contact[date][month]']],
  5323. $monthsRegex,
  5324. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5325. date('F', $now),
  5326. '/option',
  5327. '*/select',
  5328. ['select' => ['name' => 'Contact[date][day]']],
  5329. $daysRegex,
  5330. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5331. date('j', $now),
  5332. '/option',
  5333. '*/select',
  5334. ['select' => ['name' => 'Contact[date][hour]']],
  5335. $hoursRegex,
  5336. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5337. date('G', $now),
  5338. '/option',
  5339. '*/select',
  5340. ['select' => ['name' => 'Contact[date][minute]']],
  5341. $minutesRegex,
  5342. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5343. date('i', $now),
  5344. '/option',
  5345. '*/select',
  5346. ];
  5347. $this->assertHtml($expected, $result);
  5348. }
  5349. /**
  5350. * Test that datetime fields are added to protected fields list.
  5351. *
  5352. * @return void
  5353. */
  5354. public function testDateTimeSecured()
  5355. {
  5356. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5357. $this->Form->dateTime('Contact.date');
  5358. $expected = [
  5359. 'Contact.date.year',
  5360. 'Contact.date.month',
  5361. 'Contact.date.day',
  5362. 'Contact.date.hour',
  5363. 'Contact.date.minute',
  5364. ];
  5365. $this->assertEquals($expected, $this->Form->fields);
  5366. $this->Form->fields = [];
  5367. $this->Form->date('Contact.published');
  5368. $expected = [
  5369. 'Contact.published.year',
  5370. 'Contact.published.month',
  5371. 'Contact.published.day',
  5372. ];
  5373. $this->assertEquals($expected, $this->Form->fields);
  5374. }
  5375. /**
  5376. * Test that datetime fields are added to protected fields list.
  5377. *
  5378. * @return void
  5379. */
  5380. public function testDateTimeSecuredDisabled()
  5381. {
  5382. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5383. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5384. $expected = [];
  5385. $this->assertEquals($expected, $this->Form->fields);
  5386. $this->Form->fields = [];
  5387. $this->Form->date('Contact.published', ['secure' => false]);
  5388. $expected = [];
  5389. $this->assertEquals($expected, $this->Form->fields);
  5390. }
  5391. /**
  5392. * Test empty defaulting to true for datetime.
  5393. *
  5394. * @return void
  5395. */
  5396. public function testDatetimeEmpty()
  5397. {
  5398. extract($this->dateRegex);
  5399. $now = strtotime('now');
  5400. $result = $this->Form->dateTime('Contact.date', [
  5401. 'timeFormat' => 12,
  5402. 'empty' => true,
  5403. 'default' => true
  5404. ]);
  5405. $expected = [
  5406. ['select' => ['name' => 'Contact[date][year]']],
  5407. $yearsRegex,
  5408. ['option' => ['value' => '']],
  5409. '/option',
  5410. '*/select',
  5411. ['select' => ['name' => 'Contact[date][month]']],
  5412. $monthsRegex,
  5413. ['option' => ['value' => '']],
  5414. '/option',
  5415. '*/select',
  5416. ['select' => ['name' => 'Contact[date][day]']],
  5417. $daysRegex,
  5418. ['option' => ['value' => '']],
  5419. '/option',
  5420. '*/select',
  5421. ['select' => ['name' => 'Contact[date][hour]']],
  5422. $hoursRegex,
  5423. ['option' => ['value' => '']],
  5424. '/option',
  5425. '*/select',
  5426. ['select' => ['name' => 'Contact[date][minute]']],
  5427. $minutesRegex,
  5428. ['option' => ['value' => '']],
  5429. '/option',
  5430. '*/select',
  5431. ['select' => ['name' => 'Contact[date][meridian]']],
  5432. $meridianRegex,
  5433. ['option' => ['value' => '']],
  5434. '/option',
  5435. '*/select'
  5436. ];
  5437. $this->assertHtml($expected, $result);
  5438. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5439. }
  5440. /**
  5441. * Test datetime with interval option.
  5442. *
  5443. * @return void
  5444. */
  5445. public function testDatetimeMinuteInterval()
  5446. {
  5447. extract($this->dateRegex);
  5448. $now = strtotime('now');
  5449. $result = $this->Form->dateTime('Contact.date', [
  5450. 'interval' => 5,
  5451. 'value' => ''
  5452. ]);
  5453. $expected = [
  5454. ['select' => ['name' => 'Contact[date][year]']],
  5455. $yearsRegex,
  5456. ['option' => ['selected' => 'selected', 'value' => '']],
  5457. '/option',
  5458. '*/select',
  5459. ['select' => ['name' => 'Contact[date][month]']],
  5460. $monthsRegex,
  5461. ['option' => ['selected' => 'selected', 'value' => '']],
  5462. '/option',
  5463. '*/select',
  5464. ['select' => ['name' => 'Contact[date][day]']],
  5465. $daysRegex,
  5466. ['option' => ['selected' => 'selected', 'value' => '']],
  5467. '/option',
  5468. '*/select',
  5469. ['select' => ['name' => 'Contact[date][hour]']],
  5470. $hoursRegex,
  5471. ['option' => ['selected' => 'selected', 'value' => '']],
  5472. '/option',
  5473. '*/select',
  5474. ['select' => ['name' => 'Contact[date][minute]']],
  5475. $minutesRegex,
  5476. ['option' => ['selected' => 'selected', 'value' => '']],
  5477. '/option',
  5478. ['option' => ['value' => '00']],
  5479. '00',
  5480. '/option',
  5481. ['option' => ['value' => '05']],
  5482. '05',
  5483. '/option',
  5484. ['option' => ['value' => '10']],
  5485. '10',
  5486. '/option',
  5487. '*/select',
  5488. ];
  5489. $this->assertHtml($expected, $result);
  5490. }
  5491. /**
  5492. * Test dateTime with rounding
  5493. *
  5494. * @return void
  5495. */
  5496. public function testDateTimeRounding()
  5497. {
  5498. $this->Form->request->data['Contact'] = [
  5499. 'date' => [
  5500. 'day' => '13',
  5501. 'month' => '12',
  5502. 'year' => '2010',
  5503. 'hour' => '04',
  5504. 'minute' => '19',
  5505. 'meridian' => 'AM'
  5506. ]
  5507. ];
  5508. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5509. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5510. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5511. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5512. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5513. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5514. }
  5515. /**
  5516. * test that datetime() and default values work.
  5517. *
  5518. * @return void
  5519. */
  5520. public function testDatetimeWithDefault()
  5521. {
  5522. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5523. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5524. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5525. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5526. $result = $this->Form->dateTime('Contact.updated', [
  5527. 'default' => '2009-06-01 11:15:30'
  5528. ]);
  5529. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5530. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5531. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5532. }
  5533. /**
  5534. * testDateTime all zeros
  5535. *
  5536. * @return void
  5537. */
  5538. public function testDateTimeAllZeros()
  5539. {
  5540. $result = $this->Form->dateTime('Contact.date', [
  5541. 'timeFormat' => false,
  5542. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5543. 'value' => '0000-00-00'
  5544. ]);
  5545. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5546. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5547. }
  5548. /**
  5549. * testDateTimeEmptyAsArray
  5550. *
  5551. * @return void
  5552. */
  5553. public function testDateTimeEmptyAsArray()
  5554. {
  5555. $result = $this->Form->dateTime('Contact.date', [
  5556. 'empty' => [
  5557. 'day' => 'DAY',
  5558. 'month' => 'MONTH',
  5559. 'year' => 'YEAR',
  5560. 'hour' => 'HOUR',
  5561. 'minute' => 'MINUTE',
  5562. 'meridian' => false
  5563. ],
  5564. 'default' => true
  5565. ]);
  5566. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5567. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5568. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5569. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5570. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5571. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5572. $result = $this->Form->dateTime('Contact.date', [
  5573. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5574. 'default' => true
  5575. ]);
  5576. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5577. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5578. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5579. }
  5580. /**
  5581. * testFormDateTimeMulti method
  5582. *
  5583. * test multiple datetime element generation
  5584. *
  5585. * @return void
  5586. */
  5587. public function testFormDateTimeMulti()
  5588. {
  5589. extract($this->dateRegex);
  5590. $result = $this->Form->dateTime('Contact.1.updated');
  5591. $this->assertContains('Contact[1][updated][month]', $result);
  5592. $this->assertContains('Contact[1][updated][day]', $result);
  5593. $this->assertContains('Contact[1][updated][year]', $result);
  5594. $this->assertContains('Contact[1][updated][hour]', $result);
  5595. $this->assertContains('Contact[1][updated][minute]', $result);
  5596. }
  5597. /**
  5598. * When changing the date format, the label should always focus the first select box when
  5599. * clicked.
  5600. *
  5601. * @return void
  5602. */
  5603. public function testDateTimeLabelIdMatchesFirstInput()
  5604. {
  5605. $result = $this->Form->input('Model.date', ['type' => 'date']);
  5606. $this->assertContains('<label>Date</label>', $result);
  5607. $result = $this->Form->input('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5608. $this->assertContains('<label>Date</label>', $result);
  5609. $result = $this->Form->input('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5610. $this->assertContains('<label>Date</label>', $result);
  5611. }
  5612. /**
  5613. * test datetime second=true
  5614. *
  5615. * @return void
  5616. */
  5617. public function testDateTimeSecondOptions()
  5618. {
  5619. $result = $this->Form->dateTime('updated', ['second' => true]);
  5620. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5621. $result = $this->Form->dateTime('updated', ['second' => []]);
  5622. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5623. $result = $this->Form->dateTime('updated', ['second' => null]);
  5624. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5625. $result = $this->Form->dateTime('updated', ['second' => false]);
  5626. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5627. }
  5628. /**
  5629. * testMonth method
  5630. *
  5631. * @return void
  5632. */
  5633. public function testMonth()
  5634. {
  5635. $result = $this->Form->month('Model.field', ['value' => '']);
  5636. $expected = [
  5637. ['select' => ['name' => 'Model[field][month]']],
  5638. ['option' => ['value' => '', 'selected' => 'selected']],
  5639. '/option',
  5640. ['option' => ['value' => '01']],
  5641. date('F', strtotime('2008-01-01 00:00:00')),
  5642. '/option',
  5643. ['option' => ['value' => '02']],
  5644. date('F', strtotime('2008-02-01 00:00:00')),
  5645. '/option',
  5646. '*/select',
  5647. ];
  5648. $this->assertHtml($expected, $result);
  5649. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  5650. $expected = [
  5651. ['select' => ['name' => 'Model[field][month]']],
  5652. ['option' => ['selected' => 'selected', 'value' => '']],
  5653. '/option',
  5654. ['option' => ['value' => '01']],
  5655. date('F', strtotime('2008-01-01 00:00:00')),
  5656. '/option',
  5657. ['option' => ['value' => '02']],
  5658. date('F', strtotime('2008-02-01 00:00:00')),
  5659. '/option',
  5660. '*/select',
  5661. ];
  5662. $this->assertHtml($expected, $result);
  5663. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  5664. $expected = [
  5665. ['select' => ['name' => 'Model[field][month]']],
  5666. ['option' => ['selected' => 'selected', 'value' => '']],
  5667. '/option',
  5668. ['option' => ['value' => '01']],
  5669. '1',
  5670. '/option',
  5671. ['option' => ['value' => '02']],
  5672. '2',
  5673. '/option',
  5674. '*/select',
  5675. ];
  5676. $this->assertHtml($expected, $result);
  5677. $monthNames = [
  5678. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5679. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  5680. ];
  5681. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  5682. $expected = [
  5683. ['select' => ['name' => 'Model[field][month]']],
  5684. ['option' => ['value' => '']],
  5685. '/option',
  5686. ['option' => ['value' => '01', 'selected' => 'selected']],
  5687. 'Jan',
  5688. '/option',
  5689. ['option' => ['value' => '02']],
  5690. 'Feb',
  5691. '/option',
  5692. '*/select',
  5693. ];
  5694. $this->assertHtml($expected, $result);
  5695. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5696. $result = $this->Form->month('Project.release');
  5697. $expected = [
  5698. ['select' => ['name' => 'Project[release][month]']],
  5699. ['option' => ['value' => '']],
  5700. '/option',
  5701. ['option' => ['value' => '01']],
  5702. 'January',
  5703. '/option',
  5704. ['option' => ['value' => '02', 'selected' => 'selected']],
  5705. 'February',
  5706. '/option',
  5707. '*/select',
  5708. ];
  5709. $this->assertHtml($expected, $result);
  5710. $result = $this->Form->month('Contact.published', [
  5711. 'empty' => 'Published on',
  5712. ]);
  5713. $this->assertContains('Published on', $result);
  5714. }
  5715. /**
  5716. * testDay method
  5717. *
  5718. * @return void
  5719. */
  5720. public function testDay()
  5721. {
  5722. extract($this->dateRegex);
  5723. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  5724. $expected = [
  5725. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  5726. ['option' => ['selected' => 'selected', 'value' => '']],
  5727. '/option',
  5728. ['option' => ['value' => '01']],
  5729. '1',
  5730. '/option',
  5731. ['option' => ['value' => '02']],
  5732. '2',
  5733. '/option',
  5734. $daysRegex,
  5735. '/select',
  5736. ];
  5737. $this->assertHtml($expected, $result);
  5738. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5739. $result = $this->Form->day('Model.field');
  5740. $expected = [
  5741. ['select' => ['name' => 'Model[field][day]']],
  5742. ['option' => ['value' => '']],
  5743. '/option',
  5744. ['option' => ['value' => '01']],
  5745. '1',
  5746. '/option',
  5747. ['option' => ['value' => '02']],
  5748. '2',
  5749. '/option',
  5750. $daysRegex,
  5751. ['option' => ['value' => '10', 'selected' => 'selected']],
  5752. '10',
  5753. '/option',
  5754. $daysRegex,
  5755. '/select',
  5756. ];
  5757. $this->assertHtml($expected, $result);
  5758. $this->Form->request->data['Model']['field'] = '';
  5759. $result = $this->Form->day('Model.field', ['value' => '10']);
  5760. $expected = [
  5761. ['select' => ['name' => 'Model[field][day]']],
  5762. ['option' => ['value' => '']],
  5763. '/option',
  5764. ['option' => ['value' => '01']],
  5765. '1',
  5766. '/option',
  5767. ['option' => ['value' => '02']],
  5768. '2',
  5769. '/option',
  5770. $daysRegex,
  5771. ['option' => ['value' => '10', 'selected' => 'selected']],
  5772. '10',
  5773. '/option',
  5774. $daysRegex,
  5775. '/select',
  5776. ];
  5777. $this->assertHtml($expected, $result);
  5778. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5779. $result = $this->Form->day('Project.release');
  5780. $expected = [
  5781. ['select' => ['name' => 'Project[release][day]']],
  5782. ['option' => ['value' => '']],
  5783. '/option',
  5784. ['option' => ['value' => '01']],
  5785. '1',
  5786. '/option',
  5787. ['option' => ['value' => '02']],
  5788. '2',
  5789. '/option',
  5790. $daysRegex,
  5791. ['option' => ['value' => '10', 'selected' => 'selected']],
  5792. '10',
  5793. '/option',
  5794. $daysRegex,
  5795. '/select',
  5796. ];
  5797. $this->assertHtml($expected, $result);
  5798. $result = $this->Form->day('Contact.published', [
  5799. 'empty' => 'Published on',
  5800. ]);
  5801. $this->assertContains('Published on', $result);
  5802. }
  5803. /**
  5804. * testMinute method
  5805. *
  5806. * @return void
  5807. */
  5808. public function testMinute()
  5809. {
  5810. extract($this->dateRegex);
  5811. $result = $this->Form->minute('Model.field', ['value' => '']);
  5812. $expected = [
  5813. ['select' => ['name' => 'Model[field][minute]']],
  5814. ['option' => ['selected' => 'selected', 'value' => '']],
  5815. '/option',
  5816. ['option' => ['value' => '00']],
  5817. '00',
  5818. '/option',
  5819. ['option' => ['value' => '01']],
  5820. '01',
  5821. '/option',
  5822. ['option' => ['value' => '02']],
  5823. '02',
  5824. '/option',
  5825. $minutesRegex,
  5826. '/select',
  5827. ];
  5828. $this->assertHtml($expected, $result);
  5829. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5830. $result = $this->Form->minute('Model.field');
  5831. $expected = [
  5832. ['select' => ['name' => 'Model[field][minute]']],
  5833. ['option' => ['value' => '']],
  5834. '/option',
  5835. ['option' => ['value' => '00']],
  5836. '00',
  5837. '/option',
  5838. ['option' => ['value' => '01']],
  5839. '01',
  5840. '/option',
  5841. ['option' => ['value' => '02']],
  5842. '02',
  5843. '/option',
  5844. $minutesRegex,
  5845. ['option' => ['value' => '12', 'selected' => 'selected']],
  5846. '12',
  5847. '/option',
  5848. $minutesRegex,
  5849. '/select',
  5850. ];
  5851. $this->assertHtml($expected, $result);
  5852. $this->Form->request->data['Model']['field'] = '';
  5853. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  5854. $expected = [
  5855. ['select' => ['name' => 'Model[field][minute]']],
  5856. ['option' => ['selected' => 'selected', 'value' => '']],
  5857. '/option',
  5858. ['option' => ['value' => '00']],
  5859. '00',
  5860. '/option',
  5861. ['option' => ['value' => '05']],
  5862. '05',
  5863. '/option',
  5864. ['option' => ['value' => '10']],
  5865. '10',
  5866. '/option',
  5867. $minutesRegex,
  5868. '/select',
  5869. ];
  5870. $this->assertHtml($expected, $result);
  5871. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5872. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  5873. $expected = [
  5874. ['select' => ['name' => 'Model[field][minute]']],
  5875. ['option' => ['value' => '']],
  5876. '/option',
  5877. ['option' => ['value' => '00']],
  5878. '00',
  5879. '/option',
  5880. ['option' => ['value' => '05']],
  5881. '05',
  5882. '/option',
  5883. ['option' => ['value' => '10', 'selected' => 'selected']],
  5884. '10',
  5885. '/option',
  5886. $minutesRegex,
  5887. '/select',
  5888. ];
  5889. $this->assertHtml($expected, $result);
  5890. }
  5891. /**
  5892. * Test generating an input for the meridian.
  5893. *
  5894. * @return void
  5895. */
  5896. public function testMeridian()
  5897. {
  5898. extract($this->dateRegex);
  5899. $now = new \DateTime();
  5900. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  5901. $expected = [
  5902. ['select' => ['name' => 'Model[field][meridian]']],
  5903. ['option' => ['value' => '']],
  5904. '/option',
  5905. $meridianRegex,
  5906. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  5907. $now->format('a'),
  5908. '/option',
  5909. '*/select'
  5910. ];
  5911. $this->assertHtml($expected, $result);
  5912. }
  5913. /**
  5914. * testHour method
  5915. *
  5916. * @return void
  5917. */
  5918. public function testHour()
  5919. {
  5920. extract($this->dateRegex);
  5921. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  5922. $expected = [
  5923. ['select' => ['name' => 'Model[field][hour]']],
  5924. ['option' => ['selected' => 'selected', 'value' => '']],
  5925. '/option',
  5926. ['option' => ['value' => '01']],
  5927. '1',
  5928. '/option',
  5929. ['option' => ['value' => '02']],
  5930. '2',
  5931. '/option',
  5932. $hoursRegex,
  5933. '/select',
  5934. ];
  5935. $this->assertHtml($expected, $result);
  5936. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5937. $result = $this->Form->hour('Model.field', ['format' => 12]);
  5938. $expected = [
  5939. ['select' => ['name' => 'Model[field][hour]']],
  5940. ['option' => ['value' => '']],
  5941. '/option',
  5942. ['option' => ['value' => '01']],
  5943. '1',
  5944. '/option',
  5945. ['option' => ['value' => '02']],
  5946. '2',
  5947. '/option',
  5948. $hoursRegex,
  5949. ['option' => ['value' => '12', 'selected' => 'selected']],
  5950. '12',
  5951. '/option',
  5952. '/select',
  5953. ];
  5954. $this->assertHtml($expected, $result);
  5955. $this->Form->request->data['Model']['field'] = '';
  5956. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  5957. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5958. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  5959. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5960. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5961. $result = $this->Form->hour('Model.field', ['format' => 24]);
  5962. $expected = [
  5963. ['select' => ['name' => 'Model[field][hour]']],
  5964. ['option' => ['value' => '']],
  5965. '/option',
  5966. ['option' => ['value' => '00', 'selected' => 'selected']],
  5967. '0',
  5968. '/option',
  5969. ['option' => ['value' => '01']],
  5970. '1',
  5971. '/option',
  5972. ['option' => ['value' => '02']],
  5973. '2',
  5974. '/option',
  5975. $hoursRegex,
  5976. '/select',
  5977. ];
  5978. $this->assertHtml($expected, $result);
  5979. unset($this->Form->request->data['Model']['field']);
  5980. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  5981. $thisHour = date('H');
  5982. $optValue = date('G');
  5983. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5984. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  5985. $result = $this->Form->hour('Model.field', ['format' => 24]);
  5986. $expected = [
  5987. ['select' => ['name' => 'Model[field][hour]']],
  5988. ['option' => ['value' => '']],
  5989. '/option',
  5990. ['option' => ['value' => '00']],
  5991. '0',
  5992. '/option',
  5993. ['option' => ['value' => '01', 'selected' => 'selected']],
  5994. '1',
  5995. '/option',
  5996. ['option' => ['value' => '02']],
  5997. '2',
  5998. '/option',
  5999. $hoursRegex,
  6000. '/select',
  6001. ];
  6002. $this->assertHtml($expected, $result);
  6003. }
  6004. /**
  6005. * testYear method
  6006. *
  6007. * @return void
  6008. */
  6009. public function testYear()
  6010. {
  6011. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6012. $expected = [
  6013. ['select' => ['name' => 'Model[field][year]']],
  6014. ['option' => ['selected' => 'selected', 'value' => '']],
  6015. '/option',
  6016. ['option' => ['value' => '2007']],
  6017. '2007',
  6018. '/option',
  6019. ['option' => ['value' => '2006']],
  6020. '2006',
  6021. '/option',
  6022. '/select',
  6023. ];
  6024. $this->assertHtml($expected, $result);
  6025. $result = $this->Form->year('Model.field', [
  6026. 'value' => '',
  6027. 'minYear' => 2006,
  6028. 'maxYear' => 2007,
  6029. 'orderYear' => 'asc'
  6030. ]);
  6031. $expected = [
  6032. ['select' => ['name' => 'Model[field][year]']],
  6033. ['option' => ['selected' => 'selected', 'value' => '']],
  6034. '/option',
  6035. ['option' => ['value' => '2006']],
  6036. '2006',
  6037. '/option',
  6038. ['option' => ['value' => '2007']],
  6039. '2007',
  6040. '/option',
  6041. '/select',
  6042. ];
  6043. $this->assertHtml($expected, $result);
  6044. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6045. $result = $this->Form->year('Contact.published', [
  6046. 'empty' => false,
  6047. 'minYear' => 2006,
  6048. 'maxYear' => 2007,
  6049. ]);
  6050. $expected = [
  6051. ['select' => ['name' => 'Contact[published][year]']],
  6052. ['option' => ['value' => '2007']],
  6053. '2007',
  6054. '/option',
  6055. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6056. '2006',
  6057. '/option',
  6058. '/select',
  6059. ];
  6060. $this->assertHtml($expected, $result);
  6061. $result = $this->Form->year('Contact.published', [
  6062. 'empty' => 'Published on',
  6063. ]);
  6064. $this->assertContains('Published on', $result);
  6065. }
  6066. /**
  6067. * Test minYear being prior to the unix epoch
  6068. *
  6069. * @return void
  6070. */
  6071. public function testInputDatetimePreEpoch()
  6072. {
  6073. $start = date('Y') - 80;
  6074. $end = date('Y') - 18;
  6075. $result = $this->Form->input('birth_year', [
  6076. 'type' => 'date',
  6077. 'label' => 'Birth Year',
  6078. 'minYear' => $start,
  6079. 'maxYear' => $end,
  6080. 'month' => false,
  6081. 'day' => false,
  6082. ]);
  6083. $this->assertContains('value="' . $start . '">' . $start, $result);
  6084. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6085. $this->assertNotContains('value="00">00', $result);
  6086. }
  6087. /**
  6088. * testYearAutoExpandRange method
  6089. *
  6090. * @return void
  6091. */
  6092. public function testYearAutoExpandRange()
  6093. {
  6094. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6095. $result = $this->Form->year('User.birthday');
  6096. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6097. $result = $matches[1];
  6098. $expected = range(date('Y') + 5, 1930);
  6099. $this->assertEquals($expected, $result);
  6100. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6101. $result = $this->Form->year('Project.release');
  6102. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6103. $result = $matches[1];
  6104. $expected = range(2050, date('Y') - 5);
  6105. $this->assertEquals($expected, $result);
  6106. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6107. $result = $this->Form->year('Project.release', [
  6108. 'minYear' => 1890,
  6109. 'maxYear' => 1900
  6110. ]);
  6111. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6112. $result = $matches[1];
  6113. $expected = range(1900, 1881);
  6114. $this->assertEquals($expected, $result);
  6115. }
  6116. /**
  6117. * Test that input() accepts the type of date and passes options in.
  6118. *
  6119. * @return void
  6120. */
  6121. public function testInputDate()
  6122. {
  6123. $this->Form->request->data = [
  6124. 'month_year' => ['month' => date('m')],
  6125. ];
  6126. $this->Form->create($this->article);
  6127. $result = $this->Form->input('month_year', [
  6128. 'label' => false,
  6129. 'type' => 'date',
  6130. 'minYear' => 2006,
  6131. 'maxYear' => 2008
  6132. ]);
  6133. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6134. $this->assertNotContains('value="2008" selected="selected"', $result);
  6135. }
  6136. /**
  6137. * Test the label option being set to false.
  6138. *
  6139. * @return void
  6140. */
  6141. public function testInputLabelFalse()
  6142. {
  6143. $this->Form->create($this->article);
  6144. $result = $this->Form->input('title', ['label' => false]);
  6145. $expected = [
  6146. 'div' => ['class' => 'input text required'],
  6147. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6148. '/div'
  6149. ];
  6150. $this->assertHtml($expected, $result);
  6151. }
  6152. /**
  6153. * testInputDateMaxYear method
  6154. *
  6155. * Let's say we want to only allow users born from 2006 to 2008 to register
  6156. * This being the first signup page, we still don't have any data
  6157. *
  6158. * @return void
  6159. */
  6160. public function testInputDateMaxYear()
  6161. {
  6162. $this->Form->request->data = [];
  6163. $this->Form->create($this->article);
  6164. $result = $this->Form->input('birthday', [
  6165. 'label' => false,
  6166. 'type' => 'date',
  6167. 'minYear' => 2006,
  6168. 'maxYear' => 2008,
  6169. 'default' => true
  6170. ]);
  6171. $this->assertContains('value="2008" selected="selected"', $result);
  6172. $this->assertContains('value="2006"', $result);
  6173. $this->assertNotContains('value="2005"', $result);
  6174. $this->assertNotContains('value="2009"', $result);
  6175. }
  6176. /**
  6177. * testTextArea method
  6178. *
  6179. * @return void
  6180. */
  6181. public function testTextArea()
  6182. {
  6183. $this->Form->request->data = ['field' => 'some test data'];
  6184. $result = $this->Form->textarea('field');
  6185. $expected = [
  6186. 'textarea' => ['name' => 'field', 'rows' => 5],
  6187. 'some test data',
  6188. '/textarea',
  6189. ];
  6190. $this->assertHtml($expected, $result);
  6191. $result = $this->Form->textarea('user.bio');
  6192. $expected = [
  6193. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6194. '/textarea',
  6195. ];
  6196. $this->assertHtml($expected, $result);
  6197. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6198. $result = $this->Form->textarea('field');
  6199. $expected = [
  6200. 'textarea' => ['name' => 'field', 'rows' => 5],
  6201. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6202. '/textarea',
  6203. ];
  6204. $this->assertHtml($expected, $result);
  6205. $this->Form->request->data = [
  6206. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6207. ];
  6208. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6209. $expected = [
  6210. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6211. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6212. '/textarea',
  6213. ];
  6214. $this->assertHtml($expected, $result);
  6215. $result = $this->Form->textarea('0.OtherModel.field');
  6216. $expected = [
  6217. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6218. '/textarea'
  6219. ];
  6220. $this->assertHtml($expected, $result);
  6221. }
  6222. /**
  6223. * testTextAreaWithStupidCharacters method
  6224. *
  6225. * test text area with non-ascii characters
  6226. *
  6227. * @return void
  6228. */
  6229. public function testTextAreaWithStupidCharacters()
  6230. {
  6231. $result = $this->Form->textarea('Post.content', [
  6232. 'value' => "GREAT®",
  6233. 'rows' => '15',
  6234. 'cols' => '75'
  6235. ]);
  6236. $expected = [
  6237. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6238. 'GREAT®',
  6239. '/textarea',
  6240. ];
  6241. $this->assertHtml($expected, $result);
  6242. }
  6243. /**
  6244. * Test textareas waxlength read from schema.
  6245. *
  6246. * @return void
  6247. */
  6248. public function testTextAreaMaxLength()
  6249. {
  6250. $this->Form->create([
  6251. 'schema' => [
  6252. 'stuff' => ['type' => 'string', 'length' => 10],
  6253. ]
  6254. ]);
  6255. $result = $this->Form->input('other', ['type' => 'textarea']);
  6256. $expected = [
  6257. 'div' => ['class' => 'input textarea'],
  6258. 'label' => ['for' => 'other'],
  6259. 'Other',
  6260. '/label',
  6261. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6262. '/textarea',
  6263. '/div'
  6264. ];
  6265. $this->assertHtml($expected, $result);
  6266. $result = $this->Form->input('stuff', ['type' => 'textarea']);
  6267. $expected = [
  6268. 'div' => ['class' => 'input textarea'],
  6269. 'label' => ['for' => 'stuff'],
  6270. 'Stuff',
  6271. '/label',
  6272. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6273. '/textarea',
  6274. '/div'
  6275. ];
  6276. $this->assertHtml($expected, $result);
  6277. }
  6278. /**
  6279. * testHiddenField method
  6280. *
  6281. * @return void
  6282. */
  6283. public function testHiddenField()
  6284. {
  6285. $this->article['errors'] = [
  6286. 'field' => true
  6287. ];
  6288. $this->Form->request->data['field'] = 'test';
  6289. $this->Form->create($this->article);
  6290. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6291. $expected = [
  6292. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6293. $this->assertHtml($expected, $result);
  6294. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6295. $expected = [
  6296. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6297. ];
  6298. $this->assertHtml($expected, $result);
  6299. }
  6300. /**
  6301. * testFileUploadField method
  6302. *
  6303. * @return void
  6304. */
  6305. public function testFileUploadField()
  6306. {
  6307. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6308. $result = $this->Form->file('Model.upload');
  6309. $this->assertHtml($expected, $result);
  6310. $this->Form->request->data['Model']['upload'] = [
  6311. 'name' => '', 'type' => '', 'tmp_name' => '',
  6312. 'error' => 4, 'size' => 0
  6313. ];
  6314. $result = $this->Form->file('Model.upload');
  6315. $this->assertHtml($expected, $result);
  6316. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6317. $result = $this->Form->file('Model.upload');
  6318. $this->assertHtml($expected, $result);
  6319. }
  6320. /**
  6321. * test File upload input on a model not used in create();
  6322. *
  6323. * @return void
  6324. */
  6325. public function testFileUploadOnOtherModel()
  6326. {
  6327. $this->Form->create($this->article, ['type' => 'file']);
  6328. $result = $this->Form->file('ValidateProfile.city');
  6329. $expected = [
  6330. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6331. ];
  6332. $this->assertHtml($expected, $result);
  6333. }
  6334. /**
  6335. * testButton method
  6336. *
  6337. * @return void
  6338. */
  6339. public function testButton()
  6340. {
  6341. $result = $this->Form->button('Hi');
  6342. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6343. $this->assertHtml($expected, $result);
  6344. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6345. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6346. $this->assertHtml($expected, $result);
  6347. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6348. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6349. $this->assertHtml($expected, $result);
  6350. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6351. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6352. $this->assertHtml($expected, $result);
  6353. $result = $this->Form->button('No type', ['type' => false]);
  6354. $expected = ['button' => [], 'No type', '/button'];
  6355. $this->assertHtml($expected, $result);
  6356. $result = $this->Form->button('Upload Text', [
  6357. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6358. 'escape' => false
  6359. ]);
  6360. $this->assertNotRegExp('/\&039/', $result);
  6361. }
  6362. /**
  6363. * Test that button() makes unlocked fields by default.
  6364. *
  6365. * @return void
  6366. */
  6367. public function testButtonUnlockedByDefault()
  6368. {
  6369. $this->Form->request->params['_csrfToken'] = 'secured';
  6370. $this->Form->button('Save', ['name' => 'save']);
  6371. $this->Form->button('Clear');
  6372. $result = $this->Form->unlockField();
  6373. $this->assertEquals(['save'], $result);
  6374. }
  6375. /**
  6376. * testPostButton method
  6377. *
  6378. * @return void
  6379. */
  6380. public function testPostButton()
  6381. {
  6382. $result = $this->Form->postButton('Hi', '/controller/action');
  6383. $expected = [
  6384. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6385. 'div' => ['style' => 'display:none;'],
  6386. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6387. '/div',
  6388. 'button' => ['type' => 'submit'],
  6389. 'Hi',
  6390. '/button',
  6391. '/form'
  6392. ];
  6393. $this->assertHtml($expected, $result);
  6394. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6395. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6396. }
  6397. public function testPostButtonMethodType()
  6398. {
  6399. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6400. $expected = [
  6401. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6402. 'div' => ['style' => 'display:none;'],
  6403. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6404. '/div',
  6405. 'button' => ['type' => 'submit'],
  6406. 'Hi',
  6407. '/button',
  6408. '/form'
  6409. ];
  6410. $this->assertHtml($expected, $result);
  6411. }
  6412. public function testPostButtonFormOptions()
  6413. {
  6414. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6415. $expected = [
  6416. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6417. 'div' => ['style' => 'display:none;'],
  6418. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6419. '/div',
  6420. 'button' => ['type' => 'submit'],
  6421. 'Hi',
  6422. '/button',
  6423. '/form'
  6424. ];
  6425. $this->assertHtml($expected, $result);
  6426. }
  6427. /**
  6428. * Test using postButton with N dimensional data.
  6429. *
  6430. * @return void
  6431. */
  6432. public function testPostButtonNestedData()
  6433. {
  6434. $data = [
  6435. 'one' => [
  6436. 'two' => [
  6437. 3, 4, 5
  6438. ]
  6439. ]
  6440. ];
  6441. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6442. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6443. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6444. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6445. }
  6446. /**
  6447. * Test that postButton adds _Token fields.
  6448. *
  6449. * @return void
  6450. */
  6451. public function testSecurePostButton()
  6452. {
  6453. $this->Form->request->params['_csrfToken'] = 'testkey';
  6454. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6455. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6456. $tokenDebug = urlencode(json_encode([
  6457. '/posts/delete/1',
  6458. [],
  6459. []
  6460. ]));
  6461. $expected = [
  6462. 'form' => [
  6463. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6464. ],
  6465. ['div' => ['style' => 'display:none;']],
  6466. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6467. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6468. '/div',
  6469. 'button' => ['type' => 'submit'],
  6470. 'Delete',
  6471. '/button',
  6472. ['div' => ['style' => 'display:none;']],
  6473. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6474. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6475. ['input' => [
  6476. 'type' => 'hidden', 'name' => '_Token[debug]',
  6477. 'value' => $tokenDebug,
  6478. ]],
  6479. '/div',
  6480. '/form',
  6481. ];
  6482. $this->assertHtml($expected, $result);
  6483. }
  6484. /**
  6485. * testPostLink method
  6486. *
  6487. * @return void
  6488. */
  6489. public function testPostLink()
  6490. {
  6491. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6492. $expected = [
  6493. 'form' => [
  6494. 'method' => 'post', 'action' => '/posts/delete/1',
  6495. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6496. ],
  6497. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6498. '/form',
  6499. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6500. 'Delete',
  6501. '/a'
  6502. ];
  6503. $this->assertHtml($expected, $result);
  6504. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6505. $expected = [
  6506. 'form' => [
  6507. 'method' => 'post', 'action' => '/posts/delete/1',
  6508. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6509. ],
  6510. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6511. '/form',
  6512. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6513. 'Delete',
  6514. '/a'
  6515. ];
  6516. $this->assertHtml($expected, $result);
  6517. $result = $this->Form->postLink(
  6518. 'Delete',
  6519. '/posts/delete/1',
  6520. ['target' => '_blank', 'class' => 'btn btn-danger']
  6521. );
  6522. $expected = [
  6523. 'form' => [
  6524. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6525. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6526. ],
  6527. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6528. '/form',
  6529. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6530. 'Delete',
  6531. '/a'
  6532. ];
  6533. $this->assertHtml($expected, $result);
  6534. }
  6535. /**
  6536. * Test the confirm option for postLink()
  6537. *
  6538. * @return void
  6539. */
  6540. public function testPostLinkWithConfirm()
  6541. {
  6542. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6543. $expected = [
  6544. 'form' => [
  6545. 'method' => 'post', 'action' => '/posts/delete/1',
  6546. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6547. ],
  6548. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6549. '/form',
  6550. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6551. 'Delete',
  6552. '/a'
  6553. ];
  6554. $this->assertHtml($expected, $result);
  6555. $result = $this->Form->postLink(
  6556. 'Delete',
  6557. '/posts/delete/1',
  6558. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6559. );
  6560. $expected = [
  6561. 'form' => [
  6562. 'method' => 'post', 'action' => '/posts/delete/1',
  6563. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6564. ],
  6565. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6566. '/form',
  6567. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\nthis \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6568. 'Delete',
  6569. '/a'
  6570. ];
  6571. $this->assertHtml($expected, $result);
  6572. }
  6573. /**
  6574. * test postLink() with query string args.
  6575. *
  6576. * @return void
  6577. */
  6578. public function testPostLinkWithQuery()
  6579. {
  6580. $result = $this->Form->postLink(
  6581. 'Delete',
  6582. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6583. );
  6584. $expected = [
  6585. 'form' => [
  6586. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6587. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6588. ],
  6589. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6590. '/form',
  6591. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6592. 'Delete',
  6593. '/a'
  6594. ];
  6595. $this->assertHtml($expected, $result);
  6596. }
  6597. /**
  6598. * Test postLink with additional data.
  6599. *
  6600. * @return void
  6601. */
  6602. public function testPostLinkWithData()
  6603. {
  6604. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6605. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  6606. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6607. $this->Form->create($entity);
  6608. $this->Form->end();
  6609. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6610. $this->assertContains(
  6611. '<input type="hidden" name="name" value="show"',
  6612. $result,
  6613. 'should not contain entity data.'
  6614. );
  6615. }
  6616. /**
  6617. * Test that security hashes for postLink include the url.
  6618. *
  6619. * @return void
  6620. */
  6621. public function testPostLinkSecurityHash()
  6622. {
  6623. $hash = Security::hash(
  6624. '/posts/delete/1' .
  6625. serialize(['id' => '1']) .
  6626. '' .
  6627. Security::salt()
  6628. );
  6629. $hash .= '%3Aid';
  6630. $this->Form->request->params['_Token']['key'] = 'test';
  6631. $result = $this->Form->postLink(
  6632. 'Delete',
  6633. '/posts/delete/1',
  6634. ['data' => ['id' => 1]]
  6635. );
  6636. $tokenDebug = urlencode(json_encode([
  6637. '/posts/delete/1',
  6638. [
  6639. 'id' => 1
  6640. ],
  6641. []
  6642. ]));
  6643. $expected = [
  6644. 'form' => [
  6645. 'method' => 'post', 'action' => '/posts/delete/1',
  6646. 'name', 'style' => 'display:none;'
  6647. ],
  6648. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6649. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6650. 'div' => ['style' => 'display:none;'],
  6651. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6652. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6653. ['input' => [
  6654. 'type' => 'hidden', 'name' => '_Token[debug]',
  6655. 'value' => $tokenDebug,
  6656. ]],
  6657. '/div',
  6658. '/form',
  6659. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6660. 'Delete',
  6661. '/a'
  6662. ];
  6663. $this->assertHtml($expected, $result);
  6664. }
  6665. /**
  6666. * Test that postLink doesn't modify the fields in the containing form.
  6667. *
  6668. * postLink() calls inside open forms should not modify the field list
  6669. * for the form.
  6670. *
  6671. * @return void
  6672. */
  6673. public function testPostLinkSecurityHashBlockMode()
  6674. {
  6675. $hash = Security::hash(
  6676. '/posts/delete/1' .
  6677. serialize([]) .
  6678. '' .
  6679. Security::salt()
  6680. );
  6681. $hash .= '%3A';
  6682. $this->Form->request->params['_Token']['key'] = 'test';
  6683. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  6684. $this->Form->input('title');
  6685. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6686. $result = $this->View->fetch('postLink');
  6687. $this->assertEquals(['title'], $this->Form->fields);
  6688. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  6689. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  6690. }
  6691. /**
  6692. * Test that security does not include debug token if debug is false.
  6693. *
  6694. * @return void
  6695. */
  6696. public function testPostLinkSecurityHashNoDebugMode()
  6697. {
  6698. Configure::write('debug', false);
  6699. $hash = Security::hash(
  6700. '/posts/delete/1' .
  6701. serialize(['id' => '1']) .
  6702. '' .
  6703. Security::salt()
  6704. );
  6705. $hash .= '%3Aid';
  6706. $this->Form->request->params['_Token']['key'] = 'test';
  6707. $result = $this->Form->postLink(
  6708. 'Delete',
  6709. '/posts/delete/1',
  6710. ['data' => ['id' => 1]]
  6711. );
  6712. $expected = [
  6713. 'form' => [
  6714. 'method' => 'post', 'action' => '/posts/delete/1',
  6715. 'name', 'style' => 'display:none;'
  6716. ],
  6717. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6718. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6719. 'div' => ['style' => 'display:none;'],
  6720. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6721. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6722. '/div',
  6723. '/form',
  6724. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6725. 'Delete',
  6726. '/a'
  6727. ];
  6728. $this->assertHtml($expected, $result);
  6729. }
  6730. /**
  6731. * Test using postLink with N dimensional data.
  6732. *
  6733. * @return void
  6734. */
  6735. public function testPostLinkNestedData()
  6736. {
  6737. $data = [
  6738. 'one' => [
  6739. 'two' => [
  6740. 3, 4, 5
  6741. ]
  6742. ]
  6743. ];
  6744. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6745. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6746. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6747. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6748. }
  6749. /**
  6750. * test creating postLinks after a GET form.
  6751. *
  6752. * @return void
  6753. */
  6754. public function testPostLinkAfterGetForm()
  6755. {
  6756. $this->Form->request->params['_csrfToken'] = 'testkey';
  6757. $this->Form->request->params['_Token'] = 'val';
  6758. $this->Form->create($this->article, ['type' => 'get']);
  6759. $this->Form->end();
  6760. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6761. $tokenDebug = urlencode(json_encode([
  6762. '/posts/delete/1',
  6763. [],
  6764. []
  6765. ]));
  6766. $expected = [
  6767. 'form' => [
  6768. 'method' => 'post', 'action' => '/posts/delete/1',
  6769. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6770. ],
  6771. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6772. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6773. 'div' => ['style' => 'display:none;'],
  6774. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6775. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6776. ['input' => [
  6777. 'type' => 'hidden', 'name' => '_Token[debug]',
  6778. 'value' => $tokenDebug,
  6779. ]],
  6780. '/div',
  6781. '/form',
  6782. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6783. 'Delete',
  6784. '/a'
  6785. ];
  6786. $this->assertHtml($expected, $result);
  6787. }
  6788. /**
  6789. * Test that postLink adds form tags to view block
  6790. *
  6791. * @return void
  6792. */
  6793. public function testPostLinkFormBuffer()
  6794. {
  6795. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6796. $expected = [
  6797. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6798. 'Delete',
  6799. '/a'
  6800. ];
  6801. $this->assertHtml($expected, $result);
  6802. $result = $this->View->fetch('postLink');
  6803. $expected = [
  6804. 'form' => [
  6805. 'method' => 'post', 'action' => '/posts/delete/1',
  6806. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6807. ],
  6808. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6809. '/form'
  6810. ];
  6811. $this->assertHtml($expected, $result);
  6812. $result = $this->Form->postLink(
  6813. 'Delete',
  6814. '/posts/delete/2',
  6815. ['block' => true, 'method' => 'DELETE']
  6816. );
  6817. $expected = [
  6818. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6819. 'Delete',
  6820. '/a'
  6821. ];
  6822. $this->assertHtml($expected, $result);
  6823. $result = $this->View->fetch('postLink');
  6824. $expected = [
  6825. 'form' => [
  6826. 'method' => 'post', 'action' => '/posts/delete/1',
  6827. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6828. ],
  6829. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6830. '/form',
  6831. [
  6832. 'form' => [
  6833. 'method' => 'post', 'action' => '/posts/delete/2',
  6834. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6835. ],
  6836. ],
  6837. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6838. '/form'
  6839. ];
  6840. $this->assertHtml($expected, $result);
  6841. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6842. $expected = [
  6843. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6844. 'Delete',
  6845. '/a'
  6846. ];
  6847. $this->assertHtml($expected, $result);
  6848. $result = $this->View->fetch('foobar');
  6849. $expected = [
  6850. 'form' => [
  6851. 'method' => 'post', 'action' => '/posts/delete/1',
  6852. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6853. ],
  6854. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6855. '/form'
  6856. ];
  6857. $this->assertHtml($expected, $result);
  6858. }
  6859. /**
  6860. * testSubmitButton method
  6861. *
  6862. * @return void
  6863. */
  6864. public function testSubmitButton()
  6865. {
  6866. $result = $this->Form->submit('');
  6867. $expected = [
  6868. 'div' => ['class' => 'submit'],
  6869. 'input' => ['type' => 'submit', 'value' => ''],
  6870. '/div'
  6871. ];
  6872. $this->assertHtml($expected, $result);
  6873. $result = $this->Form->submit('Test Submit');
  6874. $expected = [
  6875. 'div' => ['class' => 'submit'],
  6876. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6877. '/div'
  6878. ];
  6879. $this->assertHtml($expected, $result);
  6880. $result = $this->Form->submit('Next >');
  6881. $expected = [
  6882. 'div' => ['class' => 'submit'],
  6883. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6884. '/div'
  6885. ];
  6886. $this->assertHtml($expected, $result);
  6887. $result = $this->Form->submit('Next >', ['escape' => false]);
  6888. $expected = [
  6889. 'div' => ['class' => 'submit'],
  6890. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6891. '/div'
  6892. ];
  6893. $this->assertHtml($expected, $result);
  6894. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6895. $expected = [
  6896. 'div' => ['class' => 'submit'],
  6897. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6898. '/div'
  6899. ];
  6900. $this->assertHtml($expected, $result);
  6901. }
  6902. /**
  6903. * test image submit types.
  6904. *
  6905. * @return void
  6906. */
  6907. public function testSubmitImage()
  6908. {
  6909. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6910. $expected = [
  6911. 'div' => ['class' => 'submit'],
  6912. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6913. '/div'
  6914. ];
  6915. $this->assertHtml($expected, $result);
  6916. $result = $this->Form->submit('/relative/cake.power.gif');
  6917. $expected = [
  6918. 'div' => ['class' => 'submit'],
  6919. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6920. '/div'
  6921. ];
  6922. $this->assertHtml($expected, $result);
  6923. $result = $this->Form->submit('cake.power.gif');
  6924. $expected = [
  6925. 'div' => ['class' => 'submit'],
  6926. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6927. '/div'
  6928. ];
  6929. $this->assertHtml($expected, $result);
  6930. $result = $this->Form->submit('Not.an.image');
  6931. $expected = [
  6932. 'div' => ['class' => 'submit'],
  6933. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6934. '/div'
  6935. ];
  6936. $this->assertHtml($expected, $result);
  6937. }
  6938. /**
  6939. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6940. * be submitted at a time.
  6941. *
  6942. * @return void
  6943. */
  6944. public function testSubmitUnlockedByDefault()
  6945. {
  6946. $this->Form->request->params['_Token'] = 'secured';
  6947. $this->Form->submit('Go go');
  6948. $this->Form->submit('Save', ['name' => 'save']);
  6949. $result = $this->Form->unlockField();
  6950. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6951. }
  6952. /**
  6953. * Test submit image with timestamps.
  6954. *
  6955. * @return void
  6956. */
  6957. public function testSubmitImageTimestamp()
  6958. {
  6959. Configure::write('Asset.timestamp', 'force');
  6960. $result = $this->Form->submit('cake.power.gif');
  6961. $expected = [
  6962. 'div' => ['class' => 'submit'],
  6963. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6964. '/div'
  6965. ];
  6966. $this->assertHtml($expected, $result);
  6967. }
  6968. /**
  6969. * test that datetime() works with GET style forms.
  6970. *
  6971. * @return void
  6972. */
  6973. public function testDateTimeWithGetForms()
  6974. {
  6975. extract($this->dateRegex);
  6976. $this->Form->create($this->article, ['type' => 'get']);
  6977. $result = $this->Form->datetime('created');
  6978. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  6979. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  6980. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  6981. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  6982. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  6983. }
  6984. /**
  6985. * testForMagicInputNonExistingNorValidated method
  6986. *
  6987. * @return void
  6988. */
  6989. public function testForMagicInputNonExistingNorValidated()
  6990. {
  6991. $result = $this->Form->create($this->article);
  6992. $this->Form->templates(['inputContainer' => '{{content}}']);
  6993. $result = $this->Form->input('non_existing_nor_validated');
  6994. $expected = [
  6995. 'label' => ['for' => 'non-existing-nor-validated'],
  6996. 'Non Existing Nor Validated',
  6997. '/label',
  6998. 'input' => [
  6999. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7000. 'id' => 'non-existing-nor-validated'
  7001. ]
  7002. ];
  7003. $this->assertHtml($expected, $result);
  7004. $result = $this->Form->input('non_existing_nor_validated', [
  7005. 'val' => 'my value'
  7006. ]);
  7007. $expected = [
  7008. 'label' => ['for' => 'non-existing-nor-validated'],
  7009. 'Non Existing Nor Validated',
  7010. '/label',
  7011. 'input' => [
  7012. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7013. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7014. ]
  7015. ];
  7016. $this->assertHtml($expected, $result);
  7017. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7018. $result = $this->Form->input('non_existing_nor_validated');
  7019. $expected = [
  7020. 'label' => ['for' => 'non-existing-nor-validated'],
  7021. 'Non Existing Nor Validated',
  7022. '/label',
  7023. 'input' => [
  7024. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7025. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7026. ]
  7027. ];
  7028. $this->assertHtml($expected, $result);
  7029. }
  7030. /**
  7031. * testFormMagicInputLabel method
  7032. *
  7033. * @return void
  7034. */
  7035. public function testFormMagicInputLabel()
  7036. {
  7037. TableRegistry::get('Contacts', [
  7038. 'className' => __NAMESPACE__ . '\ContactsTable'
  7039. ]);
  7040. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7041. $this->Form->templates(['inputContainer' => '{{content}}']);
  7042. $result = $this->Form->input('Contacts.name', ['label' => 'My label']);
  7043. $expected = [
  7044. 'label' => ['for' => 'contacts-name'],
  7045. 'My label',
  7046. '/label',
  7047. 'input' => [
  7048. 'type' => 'text', 'name' => 'Contacts[name]',
  7049. 'id' => 'contacts-name', 'maxlength' => '255'
  7050. ]
  7051. ];
  7052. $this->assertHtml($expected, $result);
  7053. $result = $this->Form->input('name', [
  7054. 'label' => ['class' => 'mandatory']
  7055. ]);
  7056. $expected = [
  7057. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7058. 'Name',
  7059. '/label',
  7060. 'input' => [
  7061. 'type' => 'text', 'name' => 'name',
  7062. 'id' => 'name', 'maxlength' => '255'
  7063. ]
  7064. ];
  7065. $this->assertHtml($expected, $result);
  7066. $result = $this->Form->input('name', [
  7067. 'div' => false,
  7068. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7069. ]);
  7070. $expected = [
  7071. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7072. 'My label',
  7073. '/label',
  7074. 'input' => [
  7075. 'type' => 'text', 'name' => 'name',
  7076. 'id' => 'name', 'maxlength' => '255'
  7077. ]
  7078. ];
  7079. $this->assertHtml($expected, $result);
  7080. $result = $this->Form->input('Contact.name', [
  7081. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7082. ]);
  7083. $expected = [
  7084. 'label' => ['for' => 'my_id'],
  7085. 'Name',
  7086. '/label',
  7087. 'input' => [
  7088. 'type' => 'text', 'name' => 'Contact[name]',
  7089. 'id' => 'my_id', 'maxlength' => '255'
  7090. ]
  7091. ];
  7092. $this->assertHtml($expected, $result);
  7093. $result = $this->Form->input('1.id');
  7094. $expected = ['input' => [
  7095. 'type' => 'hidden', 'name' => '1[id]',
  7096. 'id' => '1-id'
  7097. ]];
  7098. $this->assertHtml($expected, $result);
  7099. $result = $this->Form->input("1.name");
  7100. $expected = [
  7101. 'label' => ['for' => '1-name'],
  7102. 'Name',
  7103. '/label',
  7104. 'input' => [
  7105. 'type' => 'text', 'name' => '1[name]',
  7106. 'id' => '1-name', 'maxlength' => '255'
  7107. ]
  7108. ];
  7109. $this->assertHtml($expected, $result);
  7110. }
  7111. /**
  7112. * testFormEnd method
  7113. *
  7114. * @return void
  7115. */
  7116. public function testFormEnd()
  7117. {
  7118. $this->assertEquals('</form>', $this->Form->end());
  7119. }
  7120. /**
  7121. * Test the generation of fields for a multi record form.
  7122. *
  7123. * @return void
  7124. */
  7125. public function testMultiRecordForm()
  7126. {
  7127. $this->loadFixtures('Articles', 'Comments');
  7128. $articles = TableRegistry::get('Articles');
  7129. $articles->hasMany('Comments');
  7130. $comment = new Entity(['comment' => 'Value']);
  7131. $article = new Article(['comments' => [$comment]]);
  7132. $this->Form->create([$article]);
  7133. $result = $this->Form->input('0.comments.1.comment');
  7134. //@codingStandardsIgnoreStart
  7135. $expected = [
  7136. 'div' => ['class' => 'input textarea'],
  7137. 'label' => ['for' => '0-comments-1-comment'],
  7138. 'Comment',
  7139. '/label',
  7140. 'textarea' => [
  7141. 'name',
  7142. 'id' => '0-comments-1-comment',
  7143. 'rows' => 5
  7144. ],
  7145. '/textarea',
  7146. '/div'
  7147. ];
  7148. //@codingStandardsIgnoreEnd
  7149. $this->assertHtml($expected, $result);
  7150. $result = $this->Form->input('0.comments.0.comment');
  7151. //@codingStandardsIgnoreStart
  7152. $expected = [
  7153. 'div' => ['class' => 'input textarea'],
  7154. 'label' => ['for' => '0-comments-0-comment'],
  7155. 'Comment',
  7156. '/label',
  7157. 'textarea' => [
  7158. 'name',
  7159. 'id' => '0-comments-0-comment',
  7160. 'rows' => 5
  7161. ],
  7162. 'Value',
  7163. '/textarea',
  7164. '/div'
  7165. ];
  7166. //@codingStandardsIgnoreEnd
  7167. $this->assertHtml($expected, $result);
  7168. $comment->errors('comment', ['Not valid']);
  7169. $result = $this->Form->input('0.comments.0.comment');
  7170. //@codingStandardsIgnoreStart
  7171. $expected = [
  7172. 'div' => ['class' => 'input textarea error'],
  7173. 'label' => ['for' => '0-comments-0-comment'],
  7174. 'Comment',
  7175. '/label',
  7176. 'textarea' => [
  7177. 'name',
  7178. 'class' => 'form-error',
  7179. 'id' => '0-comments-0-comment',
  7180. 'rows' => 5
  7181. ],
  7182. 'Value',
  7183. '/textarea',
  7184. ['div' => ['class' => 'error-message']],
  7185. 'Not valid',
  7186. '/div',
  7187. '/div'
  7188. ];
  7189. //@codingStandardsIgnoreEnd
  7190. $this->assertHtml($expected, $result);
  7191. TableRegistry::get('Comments')
  7192. ->validator('default')
  7193. ->allowEmpty('comment', false);
  7194. $result = $this->Form->input('0.comments.1.comment');
  7195. //@codingStandardsIgnoreStart
  7196. $expected = [
  7197. 'div' => ['class' => 'input textarea required'],
  7198. 'label' => ['for' => '0-comments-1-comment'],
  7199. 'Comment',
  7200. '/label',
  7201. 'textarea' => [
  7202. 'name',
  7203. 'required' => 'required',
  7204. 'id' => '0-comments-1-comment',
  7205. 'rows' => 5
  7206. ],
  7207. '/textarea',
  7208. '/div'
  7209. ];
  7210. //@codingStandardsIgnoreEnd
  7211. $this->assertHtml($expected, $result);
  7212. }
  7213. /**
  7214. * test that some html5 inputs + FormHelper::__call() work
  7215. *
  7216. * @return void
  7217. */
  7218. public function testHtml5Inputs()
  7219. {
  7220. $result = $this->Form->email('User.email');
  7221. $expected = [
  7222. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7223. ];
  7224. $this->assertHtml($expected, $result);
  7225. $result = $this->Form->search('User.query');
  7226. $expected = [
  7227. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7228. ];
  7229. $this->assertHtml($expected, $result);
  7230. $result = $this->Form->search('User.query', ['value' => 'test']);
  7231. $expected = [
  7232. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7233. ];
  7234. $this->assertHtml($expected, $result);
  7235. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7236. $expected = [
  7237. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7238. ];
  7239. $this->assertHtml($expected, $result);
  7240. }
  7241. /**
  7242. * Test accessing html5 inputs through input().
  7243. *
  7244. * @return void
  7245. */
  7246. public function testHtml5InputWithInput()
  7247. {
  7248. $this->Form->create();
  7249. $this->Form->templates(['inputContainer' => '{{content}}']);
  7250. $result = $this->Form->input('website', [
  7251. 'type' => 'url',
  7252. 'val' => 'http://domain.tld',
  7253. 'label' => false
  7254. ]);
  7255. $expected = [
  7256. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7257. ];
  7258. $this->assertHtml($expected, $result);
  7259. }
  7260. /**
  7261. * Test errors when field name is missing.
  7262. *
  7263. * @expectedException \Cake\Core\Exception\Exception
  7264. * @return void
  7265. */
  7266. public function testHtml5InputException()
  7267. {
  7268. $this->Form->email();
  7269. }
  7270. /**
  7271. * Tests that formhelper sets required attributes.
  7272. *
  7273. * @return void
  7274. */
  7275. public function testRequiredAttribute()
  7276. {
  7277. $this->article['required'] = [
  7278. 'title' => true,
  7279. 'body' => false,
  7280. ];
  7281. $this->Form->create($this->article);
  7282. $result = $this->Form->input('title');
  7283. $expected = [
  7284. 'div' => ['class' => 'input text required'],
  7285. 'label' => ['for' => 'title'],
  7286. 'Title',
  7287. '/label',
  7288. 'input' => [
  7289. 'type' => 'text',
  7290. 'name' => 'title',
  7291. 'id' => 'title',
  7292. 'required' => 'required',
  7293. ],
  7294. '/div'
  7295. ];
  7296. $this->assertHtml($expected, $result);
  7297. $result = $this->Form->input('title', ['required' => false]);
  7298. $this->assertNotContains('required', $result);
  7299. $result = $this->Form->input('body');
  7300. $expected = [
  7301. 'div' => ['class' => 'input text'],
  7302. 'label' => ['for' => 'body'],
  7303. 'Body',
  7304. '/label',
  7305. 'input' => [
  7306. 'type' => 'text',
  7307. 'name' => 'body',
  7308. 'id' => 'body',
  7309. ],
  7310. '/div'
  7311. ];
  7312. $this->assertHtml($expected, $result);
  7313. $result = $this->Form->input('body', ['required' => true]);
  7314. $this->assertContains('required', $result);
  7315. }
  7316. /**
  7317. * Tests that it is possible to put inputs outside of the label
  7318. *
  7319. * @return void
  7320. */
  7321. public function testInputsNotNested()
  7322. {
  7323. $this->Form->templates([
  7324. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7325. 'formGroup' => '{{input}}{{label}}',
  7326. ]);
  7327. $result = $this->Form->input('foo', ['type' => 'checkbox']);
  7328. $expected = [
  7329. 'div' => ['class' => 'input checkbox'],
  7330. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7331. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7332. 'label' => ['for' => 'foo'],
  7333. 'Foo',
  7334. '/label',
  7335. '/div'
  7336. ];
  7337. $this->assertHtml($expected, $result);
  7338. $result = $this->Form->input('foo', ['type' => 'checkbox', 'label' => false]);
  7339. $expected = [
  7340. 'div' => ['class' => 'input checkbox'],
  7341. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7342. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7343. '/div'
  7344. ];
  7345. $this->assertHtml($expected, $result);
  7346. $result = $this->Form->input('confirm', [
  7347. 'type' => 'radio',
  7348. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7349. ]);
  7350. $expected = [
  7351. 'div' => ['class' => 'input radio'],
  7352. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7353. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7354. ['label' => ['for' => 'confirm-y']],
  7355. 'Yes',
  7356. '/label',
  7357. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7358. ['label' => ['for' => 'confirm-n']],
  7359. 'No',
  7360. '/label',
  7361. '<label',
  7362. 'Confirm',
  7363. '/label',
  7364. '/div',
  7365. ];
  7366. $this->assertHtml($expected, $result);
  7367. $result = $this->Form->select('category', ['1', '2'], [
  7368. 'multiple' => 'checkbox',
  7369. 'name' => 'fish',
  7370. ]);
  7371. $expected = [
  7372. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7373. ['div' => ['class' => 'checkbox']],
  7374. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7375. ['label' => ['for' => 'fish-0']],
  7376. '1',
  7377. '/label',
  7378. '/div',
  7379. ['div' => ['class' => 'checkbox']],
  7380. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7381. ['label' => ['for' => 'fish-1']],
  7382. '2',
  7383. '/label',
  7384. '/div'
  7385. ];
  7386. $this->assertHtml($expected, $result);
  7387. }
  7388. /**
  7389. * Test that *Container templates are used by input.
  7390. *
  7391. * @return void
  7392. */
  7393. public function testInputContainerTemplates()
  7394. {
  7395. $this->Form->templates([
  7396. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7397. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7398. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7399. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7400. ]);
  7401. $this->article['errors'] = [
  7402. 'Article' => ['published' => 'error message']
  7403. ];
  7404. $this->Form->create($this->article);
  7405. $result = $this->Form->input('accept', [
  7406. 'type' => 'checkbox'
  7407. ]);
  7408. $expected = [
  7409. 'div' => ['class' => 'check'],
  7410. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7411. 'label' => ['for' => 'accept'],
  7412. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7413. 'Accept',
  7414. '/label',
  7415. '/div'
  7416. ];
  7417. $this->assertHtml($expected, $result);
  7418. $result = $this->Form->input('accept', [
  7419. 'type' => 'radio',
  7420. 'options' => ['Y', 'N']
  7421. ]);
  7422. $this->assertContains('<div class="rad">', $result);
  7423. $result = $this->Form->input('Article.published', [
  7424. 'type' => 'radio',
  7425. 'options' => ['Y', 'N']
  7426. ]);
  7427. $this->assertContains('<div class="rad err">', $result);
  7428. $result = $this->Form->input('Article.created', [
  7429. 'type' => 'datetime',
  7430. ]);
  7431. $this->assertContains('<div class="dt">', $result);
  7432. }
  7433. /**
  7434. * Test that *Container templates are used by input.
  7435. *
  7436. * @return void
  7437. */
  7438. public function testFormGroupTemplates()
  7439. {
  7440. $this->Form->templates([
  7441. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7442. ]);
  7443. $this->Form->create($this->article);
  7444. $result = $this->Form->input('accept', [
  7445. 'type' => 'radio',
  7446. 'options' => ['Y', 'N']
  7447. ]);
  7448. $this->assertContains('<div class="radio">', $result);
  7449. }
  7450. /**
  7451. * Test resetting templates.
  7452. *
  7453. * @return void
  7454. */
  7455. public function testResetTemplates()
  7456. {
  7457. $this->Form->templates(['input' => '<input/>']);
  7458. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7459. $this->assertNull($this->Form->resetTemplates());
  7460. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7461. }
  7462. /**
  7463. * Test the context method.
  7464. *
  7465. * @return void
  7466. */
  7467. public function testContext()
  7468. {
  7469. $result = $this->Form->context();
  7470. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7471. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7472. $this->assertSame($mock, $this->Form->context($mock));
  7473. $this->assertSame($mock, $this->Form->context());
  7474. }
  7475. /**
  7476. * testAutoDomId
  7477. *
  7478. * @return void
  7479. */
  7480. public function testAutoDomId()
  7481. {
  7482. $result = $this->Form->text('field', ['id' => true]);
  7483. $expected = [
  7484. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7485. ];
  7486. $this->assertHtml($expected, $result);
  7487. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7488. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7489. $expected = [
  7490. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7491. ['label' => ['for' => 'field-0']],
  7492. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7493. 'option A',
  7494. '/label',
  7495. ['label' => ['for' => 'field-1']],
  7496. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7497. 'option B',
  7498. '/label',
  7499. ];
  7500. $this->assertHtml($expected, $result);
  7501. $result = $this->Form->select(
  7502. 'multi_field',
  7503. ['first', 'second'],
  7504. ['multiple' => 'checkbox', 'id' => true]
  7505. );
  7506. $expected = [
  7507. 'input' => [
  7508. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7509. ],
  7510. ['div' => ['class' => 'checkbox']],
  7511. ['label' => ['for' => 'multi-field-0']],
  7512. ['input' => [
  7513. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7514. 'value' => '0', 'id' => 'multi-field-0'
  7515. ]],
  7516. 'first',
  7517. '/label',
  7518. '/div',
  7519. ['div' => ['class' => 'checkbox']],
  7520. ['label' => ['for' => 'multi-field-1']],
  7521. ['input' => [
  7522. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7523. 'value' => '1', 'id' => 'multi-field-1'
  7524. ]],
  7525. 'second',
  7526. '/label',
  7527. '/div',
  7528. ];
  7529. $this->assertHtml($expected, $result);
  7530. }
  7531. /**
  7532. * Test the basic setters and getters for value sources
  7533. *
  7534. * @return void
  7535. */
  7536. public function testFormValuesSourcesSettersGetters()
  7537. {
  7538. $expected = ['context'];
  7539. $result = $this->Form->getValuesSources();
  7540. $this->assertEquals($expected, $result);
  7541. $expected = null;
  7542. $result = $this->Form->getSourceValue('id');
  7543. $this->assertEquals($expected, $result);
  7544. $expected = ['query', 'data', 'context'];
  7545. $this->Form->setValuesSources(['query', 'data', 'invalid', 'context', 'foo']);
  7546. $result = $this->Form->getValuesSources();
  7547. $this->assertEquals($expected, $result);
  7548. $this->Form->request->data['id'] = '1';
  7549. $this->Form->request->query['id'] = '2';
  7550. $this->Form->setValuesSources(['context']);
  7551. $expected = '1';
  7552. $result = $this->Form->getSourceValue('id');
  7553. $this->assertEquals($expected, $result);
  7554. $this->Form->setValuesSources('query');
  7555. $expected = ['query'];
  7556. $result = $this->Form->getValuesSources();
  7557. $this->assertEquals($expected, $result);
  7558. $expected = '2';
  7559. $result = $this->Form->getSourceValue('id');
  7560. $this->assertEquals($expected, $result);
  7561. $this->Form->setValuesSources(['data']);
  7562. $expected = '1';
  7563. $result = $this->Form->getSourceValue('id');
  7564. $this->assertEquals($expected, $result);
  7565. $this->Form->setValuesSources(['query', 'data']);
  7566. $expected = '2';
  7567. $result = $this->Form->getSourceValue('id');
  7568. $this->assertEquals($expected, $result);
  7569. }
  7570. /**
  7571. * Tests the different input rendering values based on sources values switching
  7572. *
  7573. * @return void
  7574. */
  7575. public function testFormValuesSourcesSingleSwitchRendering()
  7576. {
  7577. $this->loadFixtures('Articles');
  7578. $articles = TableRegistry::get('Articles');
  7579. $article = new Article();
  7580. $articles->patchEntity($article, ['id' => '3']);
  7581. $this->Form->create($article);
  7582. $this->Form->setValuesSources(['context']);
  7583. $result = $this->Form->input('id');
  7584. $expected = [
  7585. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7586. ];
  7587. $this->assertHtml($expected, $result);
  7588. $this->Form->request->query['id'] = '5';
  7589. $this->Form->setValuesSources(['query']);
  7590. $this->Form->create($article);
  7591. $result = $this->Form->input('id');
  7592. $expected = [
  7593. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7594. ];
  7595. $this->assertHtml($expected, $result);
  7596. $this->Form->create($article);
  7597. $this->Form->setValuesSources(['query']);
  7598. $result = $this->Form->input('id');
  7599. $expected = [
  7600. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7601. ];
  7602. $this->assertHtml($expected, $result);
  7603. $this->Form->request->query['id'] = '5a';
  7604. $this->Form->request->data['id'] = '5b';
  7605. $this->Form->setValuesSources(['context']);
  7606. $this->Form->create($article);
  7607. $result = $this->Form->input('id');
  7608. $expected = [
  7609. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7610. ];
  7611. $this->assertHtml($expected, $result);
  7612. $this->Form->setValuesSources(['data']);
  7613. $this->Form->create($article);
  7614. $result = $this->Form->input('id');
  7615. $expected = [
  7616. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7617. ];
  7618. $this->assertHtml($expected, $result);
  7619. $this->Form->request->data['id'] = '6';
  7620. $this->Form->request->query['id'] = '7';
  7621. $this->Form->create($article);
  7622. $this->Form->setValuesSources(['data']);
  7623. $result = $this->Form->input('id');
  7624. $expected = [
  7625. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '6']],
  7626. ];
  7627. $this->assertHtml($expected, $result);
  7628. $this->Form->request->data['id'] = '8';
  7629. $this->Form->request->query['id'] = '9';
  7630. $this->Form->create($article);
  7631. $this->Form->setValuesSources(['query']);
  7632. $result = $this->Form->input('id');
  7633. $expected = [
  7634. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7635. ];
  7636. $this->assertHtml($expected, $result);
  7637. }
  7638. /**
  7639. * Tests the different input rendering values based on sources values switching while supplying
  7640. * an entity (base context) and multiple sources (such as data, query)
  7641. *
  7642. * @return void
  7643. */
  7644. public function testFormValuesSourcesListSwitchRendering()
  7645. {
  7646. $this->loadFixtures('Articles');
  7647. $articles = TableRegistry::get('Articles');
  7648. $article = new Article();
  7649. $articles->patchEntity($article, ['id' => '3']);
  7650. $this->Form->request->query['id'] = '9';
  7651. $this->Form->create($article);
  7652. $this->Form->setValuesSources(['context']);
  7653. $result = $this->Form->input('id');
  7654. $expected = [
  7655. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7656. ];
  7657. $this->assertHtml($expected, $result);
  7658. $this->Form->setValuesSources(['query']);
  7659. $result = $this->Form->input('id');
  7660. $expected = [
  7661. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7662. ];
  7663. $this->assertHtml($expected, $result);
  7664. $this->Form->setValuesSources(['context', 'query']);
  7665. $result = $this->Form->input('id');
  7666. $expected = [
  7667. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7668. ];
  7669. $this->assertHtml($expected, $result);
  7670. $this->Form->setValuesSources(['query', 'context']);
  7671. $result = $this->Form->input('id');
  7672. $expected = [
  7673. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7674. ];
  7675. $this->assertHtml($expected, $result);
  7676. $this->Form->setValuesSources(['data', 'query', 'context']);
  7677. $result = $this->Form->input('id');
  7678. $expected = [
  7679. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7680. ];
  7681. $this->assertHtml($expected, $result);
  7682. $this->Form->request->data['id'] = '8';
  7683. $this->Form->request->query['id'] = '9';
  7684. $this->Form->setValuesSources(['data', 'query', 'context']);
  7685. $result = $this->Form->input('id');
  7686. $expected = [
  7687. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7688. ];
  7689. $this->assertHtml($expected, $result);
  7690. }
  7691. /**
  7692. * Test the different form input renderings based on values sources switchings through form options
  7693. *
  7694. * @return void
  7695. */
  7696. public function testFormValuesSourcesSwitchViaOptionsRendering()
  7697. {
  7698. $this->loadFixtures('Articles');
  7699. $articles = TableRegistry::get('Articles');
  7700. $article = new Article();
  7701. $articles->patchEntity($article, ['id' => '3']);
  7702. $this->Form->request->data['id'] = '4';
  7703. $this->Form->request->query['id'] = '5';
  7704. $this->Form->create($article, ['valuesSources' => 'query']);
  7705. $result = $this->Form->input('id');
  7706. $expected = [
  7707. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7708. ];
  7709. $this->assertHtml($expected, $result);
  7710. $result = $this->Form->getSourceValue('id');
  7711. $this->assertEquals('5', $result);
  7712. $this->Form->request->data['id'] = '6';
  7713. $this->Form->request->query['id'] = '7';
  7714. $this->Form->setValuesSources(['context']);
  7715. $this->Form->create($article, ['valuesSources' => 'query']);
  7716. $result = $this->Form->input('id');
  7717. $expected = [
  7718. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '7']],
  7719. ];
  7720. $this->assertHtml($expected, $result);
  7721. $result = $this->Form->getSourceValue('id');
  7722. $this->assertEquals('7', $result);
  7723. $this->Form->setValuesSources(['query']);
  7724. $this->Form->create($article, ['valuesSources' => 'data']);
  7725. $result = $this->Form->input('id');
  7726. $expected = [
  7727. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '6']],
  7728. ];
  7729. $this->assertHtml($expected, $result);
  7730. $result = $this->Form->getSourceValue('id');
  7731. $this->assertEquals('6', $result);
  7732. $this->Form->request->data['id'] = '8';
  7733. $this->Form->request->query['id'] = '9';
  7734. $this->Form->setValuesSources(['query']);
  7735. $this->Form->create($article, ['valuesSources' => ['context', 'data']]);
  7736. $result = $this->Form->input('id');
  7737. $expected = [
  7738. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7739. ];
  7740. $this->assertHtml($expected, $result);
  7741. $result = $this->Form->getSourceValue('id');
  7742. $this->assertEquals('8', $result);
  7743. }
  7744. /**
  7745. * Test the different form input renderings based on values sources switchings through form options
  7746. *
  7747. * @return void
  7748. */
  7749. public function testFormValuesSourcesSwitchViaOptionsAndSetterRendering()
  7750. {
  7751. $this->loadFixtures('Articles');
  7752. $articles = TableRegistry::get('Articles');
  7753. $article = new Article();
  7754. $articles->patchEntity($article, ['id' => '3']);
  7755. $this->Form->request->data['id'] = '10';
  7756. $this->Form->request->query['id'] = '11';
  7757. $this->Form->setValuesSources(['context'])->create($article, ['valuesSources' => ['query', 'data']]);
  7758. $result = $this->Form->input('id');
  7759. $expected = [
  7760. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7761. ];
  7762. $this->assertHtml($expected, $result);
  7763. $result = $this->Form->getSourceValue('id');
  7764. $this->assertEquals('11', $result);
  7765. unset($this->Form->request->query['id']);
  7766. $this->Form->setValuesSources(['context'])->create($article, ['valuesSources' => ['query', 'data']]);
  7767. $result = $this->Form->input('id');
  7768. $expected = [
  7769. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7770. ];
  7771. $this->assertHtml($expected, $result);
  7772. $result = $this->Form->getSourceValue('id');
  7773. $this->assertEquals('10', $result);
  7774. }
  7775. /**
  7776. * Test the different form values sources resetting through From::end();
  7777. *
  7778. * @return void
  7779. */
  7780. public function testFormValuesSourcesResetViaEnd()
  7781. {
  7782. $expected = ['context'];
  7783. $result = $this->Form->getValuesSources();
  7784. $this->assertEquals($expected, $result);
  7785. $expected = ['query', 'context', 'data'];
  7786. $this->Form->setValuesSources(['query', 'context', 'data']);
  7787. $result = $this->Form->getValuesSources();
  7788. $this->assertEquals($expected, $result);
  7789. $this->Form->create();
  7790. $result = $this->Form->getValuesSources();
  7791. $this->assertEquals($expected, $result);
  7792. $this->Form->end();
  7793. $expected = ['context'];
  7794. $result = $this->Form->getValuesSources();
  7795. $this->assertEquals($expected, $result);
  7796. }
  7797. /**
  7798. * Test sources values schema defaults handling
  7799. *
  7800. * @return void
  7801. */
  7802. public function testFormValuesSourcesSchemaDefaults()
  7803. {
  7804. $Articles = TableRegistry::get('Articles');
  7805. $entity = $Articles->newEntity();
  7806. $this->Form->create($entity);
  7807. $result = $this->Form->getSourceValue('title');
  7808. $expected = '';
  7809. $this->assertEquals($expected, $result);
  7810. $Articles = TableRegistry::get('Articles');
  7811. $title = $Articles->schema()->column('title');
  7812. $Articles->schema()->addColumn(
  7813. 'title',
  7814. ['default' => 'default title'] + $title
  7815. );
  7816. $entity = $Articles->newEntity();
  7817. $this->Form->create($entity);
  7818. $result = $this->Form->getSourceValue('title');
  7819. $expected = 'default title';
  7820. $this->assertEquals($expected, $result);
  7821. $Articles = TableRegistry::get('Articles');
  7822. $title = $Articles->schema()->column('title');
  7823. $Articles->schema()->addColumn(
  7824. 'title',
  7825. ['default' => 'default title'] + $title
  7826. );
  7827. $entity = $Articles->newEntity();
  7828. $this->Form->create($entity);
  7829. $this->Form->setValuesSources(['query']);
  7830. $result = $this->Form->getSourceValue('title');
  7831. $expected = '';
  7832. $this->assertEquals($expected, $result);
  7833. $this->Form->setValuesSources(['context']);
  7834. $result = $this->Form->getSourceValue('title');
  7835. $expected = 'default title';
  7836. $this->assertEquals($expected, $result);
  7837. }
  7838. /**
  7839. * Test sources values defaults handling
  7840. *
  7841. * @return void
  7842. */
  7843. public function testFormValuesSourcesDefaults()
  7844. {
  7845. $this->Form->request->query['password'] = 'open Sesame';
  7846. $this->Form->create();
  7847. $result = $this->Form->password('password');
  7848. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7849. $this->assertHtml($expected, $result);
  7850. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7851. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7852. $this->assertHtml($expected, $result);
  7853. $this->Form->setValuesSources('query');
  7854. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7855. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7856. $this->assertHtml($expected, $result);
  7857. $this->Form->setValuesSources('data');
  7858. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7859. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7860. $this->assertHtml($expected, $result);
  7861. }
  7862. /**
  7863. * Test sources values schema defaults handling
  7864. *
  7865. * @return void
  7866. */
  7867. public function testSourcesValueDoesntExistPassThrough()
  7868. {
  7869. $this->Form->request->query['category'] = 'sesame-cookies';
  7870. $Articles = TableRegistry::get('Articles');
  7871. $entity = $Articles->newEntity();
  7872. $this->Form->create($entity);
  7873. $this->Form->setValuesSources(['query', 'context']);
  7874. $result = $this->Form->getSourceValue('category');
  7875. $expected = 'sesame-cookies';
  7876. $this->assertEquals($expected, $result);
  7877. $this->Form->setValuesSources(['context', 'query']);
  7878. $result = $this->Form->getSourceValue('category');
  7879. $expected = 'sesame-cookies';
  7880. $this->assertEquals($expected, $result);
  7881. }
  7882. }