FormHelperTest.php 261 KB

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