FormHelperTest.php 229 KB

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