FormHelperTest.php 243 KB

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