FormHelperTest.php 323 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Collection\Collection;
  17. use Cake\Core\Configure;
  18. use Cake\Form\Form;
  19. use Cake\Http\ServerRequest;
  20. use Cake\ORM\Entity;
  21. use Cake\ORM\Table;
  22. use Cake\Routing\Router;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\Utility\Security;
  25. use Cake\Validation\Validator;
  26. use Cake\View\Form\EntityContext;
  27. use Cake\View\Helper\FormHelper;
  28. use Cake\View\View;
  29. use Cake\View\Widget\WidgetLocator;
  30. /**
  31. * Test stub.
  32. */
  33. class Article extends Entity
  34. {
  35. }
  36. /**
  37. * Contact class
  38. */
  39. class ContactsTable extends Table
  40. {
  41. /**
  42. * Default schema
  43. *
  44. * @var array
  45. */
  46. protected $_schema = [
  47. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  48. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  51. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  52. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  53. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  54. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  55. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  56. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  57. ];
  58. /**
  59. * Initializes the schema
  60. *
  61. * @param array $config
  62. * @return void
  63. */
  64. public function initialize(array $config)
  65. {
  66. $this->setSchema($this->_schema);
  67. }
  68. }
  69. /**
  70. * ValidateUser class
  71. */
  72. class ValidateUsersTable extends Table
  73. {
  74. /**
  75. * schema method
  76. *
  77. * @var array
  78. */
  79. protected $_schema = [
  80. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  81. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  82. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  83. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  84. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  85. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  86. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  87. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  88. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  89. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  90. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  91. ];
  92. /**
  93. * Initializes the schema
  94. *
  95. * @param array $config
  96. * @return void
  97. */
  98. public function initialize(array $config)
  99. {
  100. $this->setSchema($this->_schema);
  101. }
  102. }
  103. /**
  104. * FormHelperTest class
  105. *
  106. * @property \Cake\View\Helper\FormHelper $Form
  107. * @property \Cake\View\View $View
  108. */
  109. class FormHelperTest extends TestCase
  110. {
  111. /**
  112. * Fixtures to be used
  113. *
  114. * @var array
  115. */
  116. public $fixtures = ['core.Articles', 'core.Comments'];
  117. /**
  118. * Do not load the fixtures by default
  119. *
  120. * @var bool
  121. */
  122. public $autoFixtures = false;
  123. /**
  124. * @var array
  125. */
  126. protected $article = [];
  127. /**
  128. * setUp method
  129. *
  130. * @return void
  131. */
  132. public function setUp()
  133. {
  134. parent::setUp();
  135. Configure::write('Config.language', 'eng');
  136. Configure::write('App.base', '');
  137. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  138. $request = new ServerRequest([
  139. 'webroot' => '',
  140. 'base' => '',
  141. 'url' => '/articles/add',
  142. 'params' => [
  143. 'controller' => 'articles',
  144. 'action' => 'add',
  145. ]
  146. ]);
  147. $this->View = new View($request);
  148. $this->Form = new FormHelper($this->View);
  149. $this->dateRegex = [
  150. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  151. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  152. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  153. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  154. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  155. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  156. ];
  157. $this->article = [
  158. 'schema' => [
  159. 'id' => ['type' => 'integer'],
  160. 'author_id' => ['type' => 'integer', 'null' => true],
  161. 'title' => ['type' => 'string', 'null' => true],
  162. 'body' => 'text',
  163. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  164. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  165. ],
  166. 'required' => [
  167. 'author_id' => true,
  168. 'title' => true,
  169. ]
  170. ];
  171. Security::setSalt('foo!');
  172. Router::connect('/:controller', ['action' => 'index']);
  173. Router::connect('/:controller/:action/*');
  174. }
  175. /**
  176. * tearDown method
  177. *
  178. * @return void
  179. */
  180. public function tearDown()
  181. {
  182. parent::tearDown();
  183. unset($this->Form, $this->Controller, $this->View);
  184. $this->getTableLocator()->clear();
  185. }
  186. /**
  187. * Test construct() with the templates option.
  188. *
  189. * @return void
  190. */
  191. public function testConstructTemplatesFile()
  192. {
  193. $helper = new FormHelper($this->View, [
  194. 'templates' => 'htmlhelper_tags'
  195. ]);
  196. $result = $helper->control('name');
  197. $this->assertContains('<input', $result);
  198. }
  199. /**
  200. * Test that when specifying custom widgets the config array for that widget
  201. * is overwritten instead of merged.
  202. *
  203. * @return void
  204. */
  205. public function testConstructWithWidgets()
  206. {
  207. $config = [
  208. 'widgets' => [
  209. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  210. ]
  211. ];
  212. $helper = new FormHelper($this->View, $config);
  213. $locator = $helper->getWidgetLocator();
  214. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  215. }
  216. /**
  217. * Test that when specifying custom widgets config file and it should be
  218. * added to widgets array. WidgetLocator will load widgets in constructor.
  219. *
  220. * @return void
  221. */
  222. public function testConstructWithWidgetsConfig()
  223. {
  224. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  225. $locator = $helper->getWidgetLocator();
  226. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  227. }
  228. /**
  229. * Test setting the widget locator
  230. *
  231. * @return void
  232. */
  233. public function testSetAndGetWidgetLocator()
  234. {
  235. $helper = new FormHelper($this->View);
  236. $locator = new WidgetLocator($helper->templater(), $this->View);
  237. $helper->setWidgetLocator($locator);
  238. $this->assertSame($locator, $helper->getWidgetLocator());
  239. }
  240. /**
  241. * Test overridding grouped input types which controls generation of "for"
  242. * attribute of labels.
  243. *
  244. * @return void
  245. */
  246. public function testConstructWithGroupedInputTypes()
  247. {
  248. $helper = new FormHelper($this->View, [
  249. 'groupedInputTypes' => ['radio'],
  250. ]);
  251. $result = $helper->control('when', ['type' => 'datetime']);
  252. $this->assertContains('<label for="when">When</label>', $result);
  253. }
  254. /**
  255. * Test registering a new widget class and rendering it.
  256. *
  257. * @return void
  258. */
  259. public function testAddWidgetAndRenderWidget()
  260. {
  261. $data = [
  262. 'val' => 1
  263. ];
  264. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  265. $this->Form->addWidget('test', $mock);
  266. $mock->expects($this->once())
  267. ->method('render')
  268. ->with($data)
  269. ->will($this->returnValue('HTML'));
  270. $result = $this->Form->widget('test', $data);
  271. $this->assertEquals('HTML', $result);
  272. }
  273. /**
  274. * Test that secureFields() of widget is called after calling render(),
  275. * not before.
  276. *
  277. * @return void
  278. */
  279. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  280. {
  281. $data = [
  282. 'val' => 1,
  283. 'name' => 'test'
  284. ];
  285. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  286. $this->Form->addWidget('test', $mock);
  287. $mock->expects($this->at(0))
  288. ->method('render')
  289. ->with($data)
  290. ->will($this->returnValue('HTML'));
  291. $mock->expects($this->at(1))
  292. ->method('secureFields')
  293. ->with($data)
  294. ->will($this->returnValue(['test']));
  295. $result = $this->Form->widget('test', $data + ['secure' => true]);
  296. $this->assertEquals('HTML', $result);
  297. }
  298. /**
  299. * Test that empty string is not added to secure fields list when
  300. * rendering input widget without name.
  301. *
  302. * @return void
  303. */
  304. public function testRenderingWidgetWithEmptyName()
  305. {
  306. $this->assertEquals([], $this->Form->fields);
  307. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  308. $this->assertEquals('<select name=""></select>', $result);
  309. $this->assertEquals([], $this->Form->fields);
  310. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  311. $this->assertEquals('<select name="0"></select>', $result);
  312. $this->assertEquals(['0'], $this->Form->fields);
  313. }
  314. /**
  315. * Test registering an invalid widget class.
  316. *
  317. * @return void
  318. */
  319. public function testAddWidgetInvalid()
  320. {
  321. $this->expectException(\RuntimeException::class);
  322. $mock = new \StdClass();
  323. $this->Form->addWidget('test', $mock);
  324. $this->Form->widget('test');
  325. }
  326. /**
  327. * Test adding a new context class.
  328. *
  329. * @return void
  330. */
  331. public function testAddContextProvider()
  332. {
  333. $context = 'My data';
  334. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  335. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  336. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  337. $this->assertEquals($context, $data['entity']);
  338. return $stub;
  339. });
  340. $this->Form->create($context);
  341. $result = $this->Form->context();
  342. $this->assertSame($stub, $result);
  343. }
  344. /**
  345. * Test replacing a context class.
  346. *
  347. * @return void
  348. */
  349. public function testAddContextProviderReplace()
  350. {
  351. $entity = new Article();
  352. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  353. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  354. return $stub;
  355. });
  356. $this->Form->create($entity);
  357. $result = $this->Form->context();
  358. $this->assertSame($stub, $result);
  359. }
  360. /**
  361. * Test overriding a context class.
  362. *
  363. * @return void
  364. */
  365. public function testAddContextProviderAdd()
  366. {
  367. $entity = new Article();
  368. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  369. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  370. if ($data['entity'] instanceof Entity) {
  371. return $stub;
  372. }
  373. });
  374. $this->Form->create($entity);
  375. $result = $this->Form->context();
  376. $this->assertSame($stub, $result);
  377. }
  378. /**
  379. * Test adding an invalid context class.
  380. *
  381. * @return void
  382. */
  383. public function testAddContextProviderInvalid()
  384. {
  385. $this->expectException(\RuntimeException::class);
  386. $this->expectExceptionMessage('Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.');
  387. $context = 'My data';
  388. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  389. return new \StdClass();
  390. });
  391. $this->Form->create($context);
  392. }
  393. /**
  394. * Provides context options for create().
  395. *
  396. * @return array
  397. */
  398. public function contextSelectionProvider()
  399. {
  400. $entity = new Article();
  401. $collection = new Collection([$entity]);
  402. $emptyCollection = new Collection([]);
  403. $arrayObject = new \ArrayObject([]);
  404. $data = [
  405. 'schema' => [
  406. 'title' => ['type' => 'string']
  407. ]
  408. ];
  409. $form = new Form();
  410. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  411. return [
  412. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  413. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  414. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  415. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  416. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  417. 'form' => [$form, 'Cake\View\Form\FormContext'],
  418. 'none' => [null, 'Cake\View\Form\NullContext'],
  419. 'false' => [false, 'Cake\View\Form\NullContext'],
  420. 'custom' => [$custom, get_class($custom)]
  421. ];
  422. }
  423. /**
  424. * Test default context selection in create()
  425. *
  426. * @dataProvider contextSelectionProvider
  427. * @return void
  428. */
  429. public function testCreateContextSelectionBuiltIn($data, $class)
  430. {
  431. $this->loadFixtures('Articles');
  432. $this->Form->create($data);
  433. $this->assertInstanceOf($class, $this->Form->context());
  434. }
  435. /**
  436. * Data provider for type option.
  437. *
  438. * @return array
  439. */
  440. public static function requestTypeProvider()
  441. {
  442. return [
  443. // type, method, override
  444. ['post', 'post', 'POST'],
  445. ['put', 'post', 'PUT'],
  446. ['patch', 'post', 'PATCH'],
  447. ['delete', 'post', 'DELETE'],
  448. ];
  449. }
  450. /**
  451. * Test creating file forms.
  452. *
  453. * @return void
  454. */
  455. public function testCreateFile()
  456. {
  457. $encoding = strtolower(Configure::read('App.encoding'));
  458. $result = $this->Form->create(false, ['type' => 'file']);
  459. $expected = [
  460. 'form' => [
  461. 'method' => 'post', 'action' => '/articles/add',
  462. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  463. ],
  464. 'div' => ['style' => 'display:none;'],
  465. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  466. '/div'
  467. ];
  468. $this->assertHtml($expected, $result);
  469. }
  470. /**
  471. * Test creating GET forms.
  472. *
  473. * @return void
  474. */
  475. public function testCreateGet()
  476. {
  477. $encoding = strtolower(Configure::read('App.encoding'));
  478. $result = $this->Form->create(false, ['type' => 'get']);
  479. $expected = ['form' => [
  480. 'method' => 'get', 'action' => '/articles/add',
  481. 'accept-charset' => $encoding
  482. ]];
  483. $this->assertHtml($expected, $result);
  484. }
  485. /**
  486. * Test explicit method/enctype options.
  487. *
  488. * Explicit method overwrites inferred method from 'type'
  489. *
  490. * @return void
  491. */
  492. public function testCreateExplicitMethodEnctype()
  493. {
  494. $encoding = strtolower(Configure::read('App.encoding'));
  495. $result = $this->Form->create(false, [
  496. 'type' => 'get',
  497. 'method' => 'put',
  498. 'enctype' => 'multipart/form-data'
  499. ]);
  500. $expected = ['form' => [
  501. 'method' => 'put',
  502. 'action' => '/articles/add',
  503. 'enctype' => 'multipart/form-data',
  504. 'accept-charset' => $encoding
  505. ]];
  506. $this->assertHtml($expected, $result);
  507. }
  508. /**
  509. * Test create() with the templates option.
  510. *
  511. * @return void
  512. */
  513. public function testCreateTemplatesArray()
  514. {
  515. $result = $this->Form->create($this->article, [
  516. 'templates' => [
  517. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  518. ]
  519. ]);
  520. $expected = [
  521. 'form' => [
  522. 'class' => 'form-horizontal',
  523. 'method' => 'post',
  524. 'action' => '/articles/add',
  525. 'accept-charset' => 'utf-8'
  526. ]
  527. ];
  528. $this->assertHtml($expected, $result);
  529. }
  530. /**
  531. * Test create() with the templates option.
  532. *
  533. * @return void
  534. */
  535. public function testCreateTemplatesFile()
  536. {
  537. $result = $this->Form->create($this->article, [
  538. 'templates' => 'htmlhelper_tags',
  539. ]);
  540. $expected = [
  541. 'start form',
  542. 'div' => ['class' => 'hidden'],
  543. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  544. '/div'
  545. ];
  546. $this->assertHtml($expected, $result);
  547. }
  548. /**
  549. * Test that create() and end() restore templates.
  550. *
  551. * @return void
  552. */
  553. public function testCreateEndRestoreTemplates()
  554. {
  555. $this->Form->create($this->article, [
  556. 'templates' => ['input' => 'custom input element']
  557. ]);
  558. $this->Form->end();
  559. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  560. }
  561. /**
  562. * Test using template vars in various templates used by control() method.
  563. *
  564. * @return void
  565. */
  566. public function testControlTemplateVars()
  567. {
  568. $result = $this->Form->control('text', [
  569. 'templates' => [
  570. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  571. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  572. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  573. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  574. ],
  575. 'templateVars' => [
  576. 'forinput' => 'in-input',
  577. 'forlabel' => 'in-label',
  578. 'forgroup' => 'in-group',
  579. 'forcontainer' => 'in-container'
  580. ]
  581. ]);
  582. $expected = [
  583. 'div' => ['class'],
  584. 'label' => ['for'],
  585. 'Text in-label',
  586. '/label',
  587. 'in-group',
  588. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  589. 'in-container',
  590. '/div',
  591. ];
  592. $this->assertHtml($expected, $result);
  593. }
  594. /**
  595. * Test ensuring template variables work in template files loaded
  596. * during control().
  597. *
  598. * @return void
  599. */
  600. public function testControlTemplatesFromFile()
  601. {
  602. $result = $this->Form->control('title', [
  603. 'templates' => 'test_templates',
  604. 'templateVars' => [
  605. 'forcontainer' => 'container-data'
  606. ]
  607. ]);
  608. $expected = [
  609. 'div' => ['class'],
  610. 'label' => ['for'],
  611. 'Title',
  612. '/label',
  613. 'input' => ['name', 'type' => 'text', 'id'],
  614. 'container-data',
  615. '/div',
  616. ];
  617. $this->assertHtml($expected, $result);
  618. }
  619. /**
  620. * Test using template vars in inputSubmit and submitContainer template.
  621. *
  622. * @return void
  623. */
  624. public function testSubmitTemplateVars()
  625. {
  626. $this->Form->setTemplates([
  627. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  628. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  629. ]);
  630. $result = $this->Form->submit('Submit', [
  631. 'templateVars' => [
  632. 'forinput' => 'in-input',
  633. 'forcontainer' => 'in-container'
  634. ]
  635. ]);
  636. $expected = [
  637. 'div' => ['class'],
  638. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  639. 'in-container',
  640. '/div',
  641. ];
  642. $this->assertHtml($expected, $result);
  643. }
  644. /**
  645. * test the create() method
  646. *
  647. * @dataProvider requestTypeProvider
  648. * @return void
  649. */
  650. public function testCreateTypeOptions($type, $method, $override)
  651. {
  652. $encoding = strtolower(Configure::read('App.encoding'));
  653. $result = $this->Form->create(false, ['type' => $type]);
  654. $expected = [
  655. 'form' => [
  656. 'method' => $method, 'action' => '/articles/add',
  657. 'accept-charset' => $encoding
  658. ],
  659. 'div' => ['style' => 'display:none;'],
  660. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  661. '/div'
  662. ];
  663. $this->assertHtml($expected, $result);
  664. }
  665. /**
  666. * Test using template vars in Create (formStart template)
  667. *
  668. * @return void
  669. */
  670. public function testCreateTemplateVars()
  671. {
  672. $result = $this->Form->create($this->article, [
  673. 'templates' => [
  674. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  675. ],
  676. 'templateVars' => ['header' => 'headertext']
  677. ]);
  678. $expected = [
  679. 'h4' => ['class'],
  680. 'headertext',
  681. '/h4',
  682. 'form' => [
  683. 'method' => 'post',
  684. 'action' => '/articles/add',
  685. 'accept-charset' => 'utf-8'
  686. ]
  687. ];
  688. $this->assertHtml($expected, $result);
  689. }
  690. /**
  691. * Test opening a form for an update operation.
  692. *
  693. * @return void
  694. */
  695. public function testCreateUpdateForm()
  696. {
  697. $encoding = strtolower(Configure::read('App.encoding'));
  698. $this->View->setRequest($this->View->getRequest()
  699. ->withRequestTarget('/articles/edit/1')
  700. ->withParam('action', 'edit'));
  701. $this->article['defaults']['id'] = 1;
  702. $result = $this->Form->create($this->article);
  703. $expected = [
  704. 'form' => [
  705. 'method' => 'post',
  706. 'action' => '/articles/edit/1',
  707. 'accept-charset' => $encoding
  708. ],
  709. 'div' => ['style' => 'display:none;'],
  710. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  711. '/div'
  712. ];
  713. $this->assertHtml($expected, $result);
  714. }
  715. /**
  716. * test create() with automatic url generation
  717. *
  718. * @return void
  719. */
  720. public function testCreateAutoUrl()
  721. {
  722. $encoding = strtolower(Configure::read('App.encoding'));
  723. $this->View->setRequest($this->View->getRequest()
  724. ->withRequestTarget('/articles/delete/10')
  725. ->withParam('action', 'delete'));
  726. $result = $this->Form->create($this->article);
  727. $expected = [
  728. 'form' => [
  729. 'method' => 'post', 'action' => '/articles/delete/10',
  730. 'accept-charset' => $encoding
  731. ],
  732. 'div' => ['style' => 'display:none;'],
  733. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  734. '/div'
  735. ];
  736. $this->assertHtml($expected, $result);
  737. $this->article['defaults'] = ['id' => 1];
  738. $this->View->setRequest($this->View->getRequest()
  739. ->withRequestTarget('/articles/edit/1')
  740. ->withParam('action', 'delete'));
  741. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  742. $expected = [
  743. 'form' => [
  744. 'method' => 'post',
  745. 'action' => '/articles/edit/1',
  746. 'accept-charset' => $encoding
  747. ],
  748. 'div' => ['style' => 'display:none;'],
  749. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  750. '/div'
  751. ];
  752. $this->assertHtml($expected, $result);
  753. $this->View->setRequest($this->View->getRequest()
  754. ->withParam('action', 'add'));
  755. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  756. $expected = [
  757. 'form' => [
  758. 'method' => 'post',
  759. 'action' => '/articles/publish/1',
  760. 'accept-charset' => $encoding
  761. ],
  762. 'div' => ['style' => 'display:none;'],
  763. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  764. '/div'
  765. ];
  766. $this->assertHtml($expected, $result);
  767. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  768. $expected = [
  769. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  770. 'div' => ['style' => 'display:none;'],
  771. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  772. '/div'
  773. ];
  774. $this->assertHtml($expected, $result);
  775. $this->View->setRequest($this->View->getRequest()
  776. ->withParam('controller', 'Pages'));
  777. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  778. $expected = [
  779. 'form' => [
  780. 'method' => 'post', 'action' => '/Pages/signup/1',
  781. 'accept-charset' => $encoding
  782. ],
  783. 'div' => ['style' => 'display:none;'],
  784. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  785. '/div'
  786. ];
  787. $this->assertHtml($expected, $result);
  788. }
  789. /**
  790. * Test create() with no URL (no "action" attribute for <form> tag)
  791. *
  792. * @return void
  793. */
  794. public function testCreateNoUrl()
  795. {
  796. $result = $this->Form->create(false, ['url' => false]);
  797. $expected = [
  798. 'form' => [
  799. 'method' => 'post',
  800. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  801. ],
  802. 'div' => ['style' => 'display:none;'],
  803. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  804. '/div'
  805. ];
  806. $this->assertHtml($expected, $result);
  807. }
  808. /**
  809. * test create() with a custom route
  810. *
  811. * @return void
  812. */
  813. public function testCreateCustomRoute()
  814. {
  815. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  816. $encoding = strtolower(Configure::read('App.encoding'));
  817. $this->View->setRequest($this->View->getRequest()
  818. ->withParam('controller', 'users'));
  819. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  820. $expected = [
  821. 'form' => [
  822. 'method' => 'post', 'action' => '/login',
  823. 'accept-charset' => $encoding
  824. ],
  825. 'div' => ['style' => 'display:none;'],
  826. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  827. '/div'
  828. ];
  829. $this->assertHtml($expected, $result);
  830. Router::connect(
  831. '/new-article',
  832. ['controller' => 'articles', 'action' => 'myaction'],
  833. ['_name' => 'my-route']
  834. );
  835. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  836. $expected = [
  837. 'form' => [
  838. 'method' => 'post', 'action' => '/new-article',
  839. 'accept-charset' => $encoding,
  840. ],
  841. 'div' => ['style' => 'display:none;'],
  842. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  843. '/div'
  844. ];
  845. $this->assertHtml($expected, $result);
  846. }
  847. /**
  848. * test automatic accept-charset overriding
  849. *
  850. * @return void
  851. */
  852. public function testCreateWithAcceptCharset()
  853. {
  854. $result = $this->Form->create(
  855. $this->article,
  856. [
  857. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  858. ]
  859. );
  860. $expected = [
  861. 'form' => [
  862. 'method' => 'post', 'action' => '/articles',
  863. 'accept-charset' => 'iso-8859-1'
  864. ],
  865. 'div' => ['style' => 'display:none;'],
  866. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  867. '/div'
  868. ];
  869. $this->assertHtml($expected, $result);
  870. }
  871. /**
  872. * Test base form URL when url param is passed with multiple parameters (&)
  873. *
  874. */
  875. public function testCreateQueryStringRequest()
  876. {
  877. $encoding = strtolower(Configure::read('App.encoding'));
  878. $result = $this->Form->create($this->article, [
  879. 'type' => 'post',
  880. 'escape' => false,
  881. 'url' => [
  882. 'controller' => 'controller',
  883. 'action' => 'action',
  884. '?' => ['param1' => 'value1', 'param2' => 'value2']
  885. ]
  886. ]);
  887. $expected = [
  888. 'form' => [
  889. 'method' => 'post',
  890. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  891. 'accept-charset' => $encoding
  892. ],
  893. 'div' => ['style' => 'display:none;'],
  894. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  895. '/div'
  896. ];
  897. $this->assertHtml($expected, $result);
  898. $result = $this->Form->create($this->article, [
  899. 'type' => 'post',
  900. 'url' => [
  901. 'controller' => 'controller',
  902. 'action' => 'action',
  903. '?' => ['param1' => 'value1', 'param2' => 'value2']
  904. ]
  905. ]);
  906. $this->assertHtml($expected, $result);
  907. }
  908. /**
  909. * test that create() doesn't cause errors by multiple id's being in the primary key
  910. * as could happen with multiple select or checkboxes.
  911. *
  912. * @return void
  913. */
  914. public function testCreateWithMultipleIdInData()
  915. {
  916. $encoding = strtolower(Configure::read('App.encoding'));
  917. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  918. $result = $this->Form->create($this->article);
  919. $expected = [
  920. 'form' => [
  921. 'method' => 'post',
  922. 'action' => '/articles/add',
  923. 'accept-charset' => $encoding
  924. ],
  925. 'div' => ['style' => 'display:none;'],
  926. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  927. '/div'
  928. ];
  929. $this->assertHtml($expected, $result);
  930. }
  931. /**
  932. * test that create() doesn't add in extra passed params.
  933. *
  934. * @return void
  935. */
  936. public function testCreatePassedArgs()
  937. {
  938. $encoding = strtolower(Configure::read('App.encoding'));
  939. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  940. $result = $this->Form->create($this->article, [
  941. 'type' => 'post',
  942. 'escape' => false,
  943. 'url' => [
  944. 'action' => 'edit',
  945. 'myparam'
  946. ]
  947. ]);
  948. $expected = [
  949. 'form' => [
  950. 'method' => 'post',
  951. 'action' => '/articles/edit/myparam',
  952. 'accept-charset' => $encoding
  953. ],
  954. 'div' => ['style' => 'display:none;'],
  955. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  956. '/div'
  957. ];
  958. $this->assertHtml($expected, $result);
  959. }
  960. /**
  961. * test creating a get form, and get form inputs.
  962. *
  963. * @return void
  964. */
  965. public function testGetFormCreate()
  966. {
  967. $encoding = strtolower(Configure::read('App.encoding'));
  968. $result = $this->Form->create($this->article, ['type' => 'get']);
  969. $expected = ['form' => [
  970. 'method' => 'get', 'action' => '/articles/add',
  971. 'accept-charset' => $encoding
  972. ]];
  973. $this->assertHtml($expected, $result);
  974. $result = $this->Form->text('title');
  975. $expected = ['input' => [
  976. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  977. ]];
  978. $this->assertHtml($expected, $result);
  979. $result = $this->Form->password('password');
  980. $expected = ['input' => [
  981. 'name' => 'password', 'type' => 'password'
  982. ]];
  983. $this->assertHtml($expected, $result);
  984. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  985. $result = $this->Form->text('user_form');
  986. $expected = ['input' => [
  987. 'name' => 'user_form', 'type' => 'text'
  988. ]];
  989. $this->assertHtml($expected, $result);
  990. }
  991. /**
  992. * test get form, and inputs when the model param is false
  993. *
  994. * @return void
  995. */
  996. public function testGetFormWithFalseModel()
  997. {
  998. $encoding = strtolower(Configure::read('App.encoding'));
  999. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'contact_test'));
  1000. $result = $this->Form->create(false, [
  1001. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  1002. ]);
  1003. $expected = ['form' => [
  1004. 'method' => 'get', 'action' => '/contact_test/add',
  1005. 'accept-charset' => $encoding
  1006. ]];
  1007. $this->assertHtml($expected, $result);
  1008. $result = $this->Form->text('reason');
  1009. $expected = [
  1010. 'input' => ['type' => 'text', 'name' => 'reason']
  1011. ];
  1012. $this->assertHtml($expected, $result);
  1013. }
  1014. /**
  1015. * testFormCreateWithSecurity method
  1016. *
  1017. * Test form->create() with security key.
  1018. *
  1019. * @return void
  1020. */
  1021. public function testCreateWithSecurity()
  1022. {
  1023. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1024. $encoding = strtolower(Configure::read('App.encoding'));
  1025. $result = $this->Form->create($this->article, [
  1026. 'url' => '/articles/publish',
  1027. ]);
  1028. $expected = [
  1029. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  1030. 'div' => ['style' => 'display:none;'],
  1031. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1032. ['input' => [
  1033. 'type' => 'hidden',
  1034. 'name' => '_csrfToken',
  1035. 'value' => 'testKey',
  1036. 'autocomplete' => 'off',
  1037. ]],
  1038. '/div'
  1039. ];
  1040. $this->assertHtml($expected, $result);
  1041. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1042. $expected['form']['id'] = 'MyForm';
  1043. $this->assertHtml($expected, $result);
  1044. }
  1045. /**
  1046. * testFormCreateGetNoSecurity method
  1047. *
  1048. * Test form->create() with no security key as its a get form
  1049. *
  1050. * @return void
  1051. */
  1052. public function testCreateEndGetNoSecurity()
  1053. {
  1054. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1055. $article = new Article();
  1056. $result = $this->Form->create($article, [
  1057. 'type' => 'get',
  1058. 'url' => '/contacts/add'
  1059. ]);
  1060. $this->assertNotContains('testKey', $result);
  1061. $result = $this->Form->end();
  1062. $this->assertNotContains('testKey', $result);
  1063. }
  1064. /**
  1065. * test that create() clears the fields property so it starts fresh
  1066. *
  1067. * @return void
  1068. */
  1069. public function testCreateClearingFields()
  1070. {
  1071. $this->Form->fields = ['model_id'];
  1072. $this->Form->create($this->article);
  1073. $this->assertEquals([], $this->Form->fields);
  1074. }
  1075. /**
  1076. * Tests form hash generation with model-less data
  1077. *
  1078. * @return void
  1079. */
  1080. public function testValidateHashNoModel()
  1081. {
  1082. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1083. $fields = ['anything'];
  1084. $result = $this->Form->secure($fields);
  1085. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1086. $this->assertContains($hash, $result);
  1087. }
  1088. /**
  1089. * Tests that hidden fields generated for checkboxes don't get locked
  1090. *
  1091. * @return void
  1092. */
  1093. public function testNoCheckboxLocking()
  1094. {
  1095. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1096. $this->assertSame([], $this->Form->fields);
  1097. $this->Form->checkbox('check', ['value' => '1']);
  1098. $this->assertSame($this->Form->fields, ['check']);
  1099. }
  1100. /**
  1101. * testFormSecurityFields method
  1102. *
  1103. * Test generation of secure form hash generation.
  1104. *
  1105. * @return void
  1106. */
  1107. public function testFormSecurityFields()
  1108. {
  1109. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1110. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1111. $result = $this->Form->secure($fields);
  1112. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1113. $hash .= ':' . 'Model.valid';
  1114. $hash = urlencode($hash);
  1115. $tokenDebug = urlencode(json_encode([
  1116. '',
  1117. $fields,
  1118. []
  1119. ]));
  1120. $expected = [
  1121. 'div' => ['style' => 'display:none;'],
  1122. ['input' => [
  1123. 'type' => 'hidden',
  1124. 'name' => '_Token[fields]',
  1125. 'value' => $hash,
  1126. 'autocomplete' => 'off',
  1127. ]],
  1128. ['input' => [
  1129. 'type' => 'hidden',
  1130. 'name' => '_Token[unlocked]',
  1131. 'value' => '',
  1132. 'autocomplete' => 'off',
  1133. ]],
  1134. ['input' => [
  1135. 'type' => 'hidden',
  1136. 'name' => '_Token[debug]',
  1137. 'value' => $tokenDebug,
  1138. 'autocomplete' => 'off',
  1139. ]],
  1140. '/div'
  1141. ];
  1142. $this->assertHtml($expected, $result);
  1143. }
  1144. /**
  1145. * testFormSecurityFields method
  1146. *
  1147. * Test debug token is not generated if debug is false
  1148. *
  1149. * @return void
  1150. */
  1151. public function testFormSecurityFieldsNoDebugMode()
  1152. {
  1153. Configure::write('debug', false);
  1154. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1155. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1156. $result = $this->Form->secure($fields);
  1157. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1158. $hash .= ':' . 'Model.valid';
  1159. $hash = urlencode($hash);
  1160. $expected = [
  1161. 'div' => ['style' => 'display:none;'],
  1162. ['input' => [
  1163. 'type' => 'hidden',
  1164. 'name' => '_Token[fields]',
  1165. 'autocomplete' => 'off',
  1166. 'value' => $hash
  1167. ]],
  1168. ['input' => [
  1169. 'type' => 'hidden',
  1170. 'name' => '_Token[unlocked]',
  1171. 'autocomplete' => 'off',
  1172. 'value' => '',
  1173. ]],
  1174. '/div'
  1175. ];
  1176. $this->assertHtml($expected, $result);
  1177. }
  1178. /**
  1179. * Tests correct generation of number fields for smallint
  1180. *
  1181. * @return void
  1182. */
  1183. public function testTextFieldGenerationForSmallint()
  1184. {
  1185. $this->article['schema'] = [
  1186. 'foo' => [
  1187. 'type' => 'smallinteger',
  1188. 'null' => false,
  1189. 'default' => null,
  1190. 'length' => 10
  1191. ]
  1192. ];
  1193. $this->Form->create($this->article);
  1194. $result = $this->Form->control('foo');
  1195. $this->assertContains('class="input number"', $result);
  1196. $this->assertContains('type="number"', $result);
  1197. }
  1198. /**
  1199. * Tests correct generation of number fields for tinyint
  1200. *
  1201. * @return void
  1202. */
  1203. public function testTextFieldGenerationForTinyint()
  1204. {
  1205. $this->article['schema'] = [
  1206. 'foo' => [
  1207. 'type' => 'tinyinteger',
  1208. 'null' => false,
  1209. 'default' => null,
  1210. 'length' => 10
  1211. ]
  1212. ];
  1213. $this->Form->create($this->article);
  1214. $result = $this->Form->control('foo');
  1215. $this->assertContains('class="input number"', $result);
  1216. $this->assertContains('type="number"', $result);
  1217. }
  1218. /**
  1219. * Tests correct generation of number fields for double and float fields
  1220. *
  1221. * @return void
  1222. */
  1223. public function testTextFieldGenerationForFloats()
  1224. {
  1225. $this->article['schema'] = [
  1226. 'foo' => [
  1227. 'type' => 'float',
  1228. 'null' => false,
  1229. 'default' => null,
  1230. 'length' => 10
  1231. ]
  1232. ];
  1233. $this->Form->create($this->article);
  1234. $result = $this->Form->control('foo');
  1235. $expected = [
  1236. 'div' => ['class' => 'input number'],
  1237. 'label' => ['for' => 'foo'],
  1238. 'Foo',
  1239. '/label',
  1240. ['input' => [
  1241. 'type' => 'number',
  1242. 'name' => 'foo',
  1243. 'id' => 'foo',
  1244. 'step' => 'any'
  1245. ]],
  1246. '/div'
  1247. ];
  1248. $this->assertHtml($expected, $result);
  1249. $result = $this->Form->control('foo', ['step' => 0.5]);
  1250. $expected = [
  1251. 'div' => ['class' => 'input number'],
  1252. 'label' => ['for' => 'foo'],
  1253. 'Foo',
  1254. '/label',
  1255. ['input' => [
  1256. 'type' => 'number',
  1257. 'name' => 'foo',
  1258. 'id' => 'foo',
  1259. 'step' => '0.5'
  1260. ]],
  1261. '/div'
  1262. ];
  1263. $this->assertHtml($expected, $result);
  1264. }
  1265. /**
  1266. * Tests correct generation of number fields for integer fields
  1267. *
  1268. * @return void
  1269. */
  1270. public function testTextFieldTypeNumberGenerationForIntegers()
  1271. {
  1272. $this->getTableLocator()->get('Contacts', [
  1273. 'className' => __NAMESPACE__ . '\ContactsTable'
  1274. ]);
  1275. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1276. $result = $this->Form->control('age');
  1277. $expected = [
  1278. 'div' => ['class' => 'input number'],
  1279. 'label' => ['for' => 'age'],
  1280. 'Age',
  1281. '/label',
  1282. ['input' => [
  1283. 'type' => 'number', 'name' => 'age',
  1284. 'id' => 'age'
  1285. ]],
  1286. '/div'
  1287. ];
  1288. $this->assertHtml($expected, $result);
  1289. }
  1290. /**
  1291. * Tests correct generation of file upload fields for binary fields
  1292. *
  1293. * @return void
  1294. */
  1295. public function testFileUploadFieldTypeGenerationForBinaries()
  1296. {
  1297. $table = $this->getTableLocator()->get('Contacts', [
  1298. 'className' => __NAMESPACE__ . '\ContactsTable'
  1299. ]);
  1300. $table->setSchema(['foo' => [
  1301. 'type' => 'binary',
  1302. 'null' => false,
  1303. 'default' => null,
  1304. 'length' => 1024
  1305. ]]);
  1306. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1307. $result = $this->Form->control('foo');
  1308. $expected = [
  1309. 'div' => ['class' => 'input file'],
  1310. 'label' => ['for' => 'foo'],
  1311. 'Foo',
  1312. '/label',
  1313. ['input' => [
  1314. 'type' => 'file', 'name' => 'foo',
  1315. 'id' => 'foo'
  1316. ]],
  1317. '/div'
  1318. ];
  1319. $this->assertHtml($expected, $result);
  1320. }
  1321. /**
  1322. * testFormSecurityMultipleFields method
  1323. *
  1324. * Test secure() with multiple row form. Ensure hash is correct.
  1325. *
  1326. * @return void
  1327. */
  1328. public function testFormSecurityMultipleFields()
  1329. {
  1330. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1331. $fields = [
  1332. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1333. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1334. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1335. ];
  1336. $result = $this->Form->secure($fields);
  1337. $hash = '8670192c3f040bf58680479060b4755b7a5c3596' .
  1338. '%3AModel.0.hidden%7CModel.0.valid%7CModel.1.hidden%7CModel.1.valid';
  1339. $tokenDebug = urlencode(json_encode([
  1340. '',
  1341. $fields,
  1342. []
  1343. ]));
  1344. $expected = [
  1345. 'div' => ['style' => 'display:none;'],
  1346. ['input' => [
  1347. 'type' => 'hidden',
  1348. 'name' => '_Token[fields]',
  1349. 'value' => $hash,
  1350. 'autocomplete' => 'off',
  1351. ]],
  1352. ['input' => [
  1353. 'type' => 'hidden',
  1354. 'name' => '_Token[unlocked]',
  1355. 'autocomplete' => 'off',
  1356. 'value' => ''
  1357. ]],
  1358. ['input' => [
  1359. 'type' => 'hidden',
  1360. 'name' => '_Token[debug]',
  1361. 'value' => $tokenDebug,
  1362. 'autocomplete' => 'off',
  1363. ]],
  1364. '/div'
  1365. ];
  1366. $this->assertHtml($expected, $result);
  1367. }
  1368. /**
  1369. * testFormSecurityMultipleSubmitButtons
  1370. *
  1371. * test form submit generation and ensure that _Token is only created on end()
  1372. *
  1373. * @return void
  1374. */
  1375. public function testFormSecurityMultipleSubmitButtons()
  1376. {
  1377. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1378. $this->Form->create($this->article);
  1379. $this->Form->text('Address.title');
  1380. $this->Form->text('Address.first_name');
  1381. $result = $this->Form->submit('Save', ['name' => 'save']);
  1382. $expected = [
  1383. 'div' => ['class' => 'submit'],
  1384. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1385. '/div',
  1386. ];
  1387. $this->assertHtml($expected, $result);
  1388. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1389. $expected = [
  1390. 'div' => ['class' => 'submit'],
  1391. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1392. '/div',
  1393. ];
  1394. $this->assertHtml($expected, $result);
  1395. $result = $this->Form->end();
  1396. $tokenDebug = urlencode(json_encode([
  1397. '/articles/add',
  1398. [
  1399. 'Address.title',
  1400. 'Address.first_name',
  1401. ],
  1402. ['save', 'cancel']
  1403. ]));
  1404. $expected = [
  1405. 'div' => ['style' => 'display:none;'],
  1406. ['input' => [
  1407. 'type' => 'hidden',
  1408. 'name' => '_Token[fields]',
  1409. 'autocomplete',
  1410. 'value'
  1411. ]],
  1412. ['input' => [
  1413. 'type' => 'hidden',
  1414. 'name' => '_Token[unlocked]',
  1415. 'value' => 'cancel%7Csave',
  1416. 'autocomplete' => 'off',
  1417. ]],
  1418. ['input' => [
  1419. 'type' => 'hidden',
  1420. 'name' => '_Token[debug]',
  1421. 'value' => $tokenDebug,
  1422. 'autocomplete' => 'off',
  1423. ]],
  1424. '/div'
  1425. ];
  1426. $this->assertHtml($expected, $result);
  1427. }
  1428. /**
  1429. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1430. *
  1431. * @return void
  1432. */
  1433. public function testSecurityButtonNestedNamed()
  1434. {
  1435. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1436. $this->Form->create('Addresses');
  1437. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1438. $result = $this->Form->unlockField();
  1439. $this->assertEquals(['Address.button'], $result);
  1440. }
  1441. /**
  1442. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1443. *
  1444. * @return void
  1445. */
  1446. public function testSecuritySubmitNestedNamed()
  1447. {
  1448. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1449. $this->Form->create($this->article);
  1450. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1451. $result = $this->Form->unlockField();
  1452. $this->assertEquals(['Address.button'], $result);
  1453. }
  1454. /**
  1455. * Test that the correct fields are unlocked for image submits with no names.
  1456. *
  1457. * @return void
  1458. */
  1459. public function testSecuritySubmitImageNoName()
  1460. {
  1461. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1462. $this->Form->create(false);
  1463. $result = $this->Form->submit('save.png');
  1464. $expected = [
  1465. 'div' => ['class' => 'submit'],
  1466. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1467. '/div'
  1468. ];
  1469. $this->assertHtml($expected, $result);
  1470. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1471. }
  1472. /**
  1473. * Test that the correct fields are unlocked for image submits with names.
  1474. *
  1475. * @return void
  1476. */
  1477. public function testSecuritySubmitImageName()
  1478. {
  1479. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1480. $this->Form->create(null);
  1481. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1482. $expected = [
  1483. 'div' => ['class' => 'submit'],
  1484. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1485. '/div'
  1486. ];
  1487. $this->assertHtml($expected, $result);
  1488. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1489. }
  1490. /**
  1491. * testFormSecurityMultipleControlFields method
  1492. *
  1493. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1494. *
  1495. * @return void
  1496. */
  1497. public function testFormSecurityMultipleControlFields()
  1498. {
  1499. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1500. $this->Form->create();
  1501. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1502. $this->Form->control('Addresses.0.title');
  1503. $this->Form->control('Addresses.0.first_name');
  1504. $this->Form->control('Addresses.0.last_name');
  1505. $this->Form->control('Addresses.0.address');
  1506. $this->Form->control('Addresses.0.city');
  1507. $this->Form->control('Addresses.0.phone');
  1508. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1509. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1510. $this->Form->control('Addresses.1.title');
  1511. $this->Form->control('Addresses.1.first_name');
  1512. $this->Form->control('Addresses.1.last_name');
  1513. $this->Form->control('Addresses.1.address');
  1514. $this->Form->control('Addresses.1.city');
  1515. $this->Form->control('Addresses.1.phone');
  1516. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1517. $result = $this->Form->secure($this->Form->fields);
  1518. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1519. $tokenDebug = urlencode(json_encode([
  1520. '/articles/add',
  1521. [
  1522. 'Addresses.0.id' => '123456',
  1523. 'Addresses.0.title',
  1524. 'Addresses.0.first_name',
  1525. 'Addresses.0.last_name',
  1526. 'Addresses.0.address',
  1527. 'Addresses.0.city',
  1528. 'Addresses.0.phone',
  1529. 'Addresses.0.primary',
  1530. 'Addresses.1.id' => '654321',
  1531. 'Addresses.1.title',
  1532. 'Addresses.1.first_name',
  1533. 'Addresses.1.last_name',
  1534. 'Addresses.1.address',
  1535. 'Addresses.1.city',
  1536. 'Addresses.1.phone',
  1537. 'Addresses.1.primary',
  1538. ],
  1539. []
  1540. ]));
  1541. $expected = [
  1542. 'div' => ['style' => 'display:none;'],
  1543. ['input' => [
  1544. 'type' => 'hidden',
  1545. 'name' => '_Token[fields]',
  1546. 'value' => $hash,
  1547. 'autocomplete' => 'off',
  1548. ]],
  1549. ['input' => [
  1550. 'type' => 'hidden',
  1551. 'name' => '_Token[unlocked]',
  1552. 'autocomplete' => 'off',
  1553. 'value' => ''
  1554. ]],
  1555. ['input' => [
  1556. 'type' => 'hidden',
  1557. 'name' => '_Token[debug]',
  1558. 'value' => $tokenDebug,
  1559. 'autocomplete' => 'off',
  1560. ]],
  1561. '/div'
  1562. ];
  1563. $this->assertHtml($expected, $result);
  1564. }
  1565. /**
  1566. * testFormSecurityArrayFields method
  1567. *
  1568. * Test form security with Model.field.0 style inputs.
  1569. *
  1570. * @return void
  1571. */
  1572. public function testFormSecurityArrayFields()
  1573. {
  1574. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1575. $this->Form->create();
  1576. $this->Form->text('Address.primary.1');
  1577. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1578. $this->Form->text('Address.secondary.1.0');
  1579. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1580. }
  1581. /**
  1582. * testFormSecurityMultipleControlDisabledFields method
  1583. *
  1584. * Test secure form generation with multiple records and disabled fields.
  1585. *
  1586. * @return void
  1587. */
  1588. public function testFormSecurityMultipleControlDisabledFields()
  1589. {
  1590. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1591. 'unlockedFields' => ['first_name', 'address']
  1592. ]));
  1593. $this->Form->create();
  1594. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1595. $this->Form->text('Addresses.0.title');
  1596. $this->Form->text('Addresses.0.first_name');
  1597. $this->Form->text('Addresses.0.last_name');
  1598. $this->Form->text('Addresses.0.address');
  1599. $this->Form->text('Addresses.0.city');
  1600. $this->Form->text('Addresses.0.phone');
  1601. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1602. $this->Form->text('Addresses.1.title');
  1603. $this->Form->text('Addresses.1.first_name');
  1604. $this->Form->text('Addresses.1.last_name');
  1605. $this->Form->text('Addresses.1.address');
  1606. $this->Form->text('Addresses.1.city');
  1607. $this->Form->text('Addresses.1.phone');
  1608. $result = $this->Form->secure($this->Form->fields);
  1609. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1610. $tokenDebug = urlencode(json_encode([
  1611. '/articles/add',
  1612. [
  1613. 'Addresses.0.id' => '123456',
  1614. 'Addresses.0.title',
  1615. 'Addresses.0.last_name',
  1616. 'Addresses.0.city',
  1617. 'Addresses.0.phone',
  1618. 'Addresses.1.id' => '654321',
  1619. 'Addresses.1.title',
  1620. 'Addresses.1.last_name',
  1621. 'Addresses.1.city',
  1622. 'Addresses.1.phone'
  1623. ],
  1624. [
  1625. 'first_name',
  1626. 'address'
  1627. ]
  1628. ]));
  1629. $expected = [
  1630. 'div' => ['style' => 'display:none;'],
  1631. ['input' => [
  1632. 'type' => 'hidden',
  1633. 'name' => '_Token[fields]',
  1634. 'autocomplete' => 'off',
  1635. 'value' => $hash
  1636. ]],
  1637. ['input' => [
  1638. 'type' => 'hidden',
  1639. 'name' => '_Token[unlocked]',
  1640. 'autocomplete' => 'off',
  1641. 'value' => 'address%7Cfirst_name',
  1642. ]],
  1643. ['input' => [
  1644. 'type' => 'hidden',
  1645. 'name' => '_Token[debug]',
  1646. 'autocomplete' => 'off',
  1647. 'value' => $tokenDebug,
  1648. ]],
  1649. '/div'
  1650. ];
  1651. $this->assertHtml($expected, $result);
  1652. }
  1653. /**
  1654. * testFormSecurityControlDisabledFields method
  1655. *
  1656. * Test single record form with disabled fields.
  1657. *
  1658. * @return void
  1659. */
  1660. public function testFormSecurityControlUnlockedFields()
  1661. {
  1662. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1663. 'unlockedFields' => ['first_name', 'address']
  1664. ]));
  1665. $this->Form->create();
  1666. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1667. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1668. $this->Form->text('Addresses.title');
  1669. $this->Form->text('Addresses.first_name');
  1670. $this->Form->text('Addresses.last_name');
  1671. $this->Form->text('Addresses.address');
  1672. $this->Form->text('Addresses.city');
  1673. $this->Form->text('Addresses.phone');
  1674. $result = $this->Form->fields;
  1675. $expected = [
  1676. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1677. 'Addresses.city', 'Addresses.phone'
  1678. ];
  1679. $this->assertEquals($expected, $result);
  1680. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1681. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1682. $tokenDebug = urlencode(json_encode([
  1683. '/articles/add',
  1684. [
  1685. 'Addresses.id' => '123456',
  1686. 'Addresses.title',
  1687. 'Addresses.last_name',
  1688. 'Addresses.city',
  1689. 'Addresses.phone'
  1690. ],
  1691. [
  1692. 'first_name',
  1693. 'address'
  1694. ]
  1695. ]));
  1696. $expected = [
  1697. 'div' => ['style' => 'display:none;'],
  1698. ['input' => [
  1699. 'type' => 'hidden',
  1700. 'name' => '_Token[fields]',
  1701. 'value' => $hash,
  1702. 'autocomplete' => 'off',
  1703. 'data-foo' => 'bar',
  1704. ]],
  1705. ['input' => [
  1706. 'type' => 'hidden',
  1707. 'name' => '_Token[unlocked]',
  1708. 'value' => 'address%7Cfirst_name',
  1709. 'autocomplete' => 'off',
  1710. 'data-foo' => 'bar',
  1711. ]],
  1712. ['input' => [
  1713. 'type' => 'hidden', 'name' => '_Token[debug]',
  1714. 'value' => $tokenDebug,
  1715. 'autocomplete' => 'off',
  1716. 'data-foo' => 'bar'
  1717. ]],
  1718. '/div'
  1719. ];
  1720. $this->assertHtml($expected, $result);
  1721. }
  1722. /**
  1723. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1724. *
  1725. * Test single record form with debugSecurity param.
  1726. *
  1727. * @return void
  1728. */
  1729. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1730. {
  1731. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1732. 'unlockedFields' => ['first_name', 'address']
  1733. ]));
  1734. $this->Form->create();
  1735. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1736. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1737. $this->Form->text('Addresses.title');
  1738. $this->Form->text('Addresses.first_name');
  1739. $this->Form->text('Addresses.last_name');
  1740. $this->Form->text('Addresses.address');
  1741. $this->Form->text('Addresses.city');
  1742. $this->Form->text('Addresses.phone');
  1743. $result = $this->Form->fields;
  1744. $expected = [
  1745. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1746. 'Addresses.city', 'Addresses.phone'
  1747. ];
  1748. $this->assertEquals($expected, $result);
  1749. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1750. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1751. $tokenDebug = urlencode(json_encode([
  1752. '/articles/add',
  1753. [
  1754. 'Addresses.id' => '123456',
  1755. 'Addresses.title',
  1756. 'Addresses.last_name',
  1757. 'Addresses.city',
  1758. 'Addresses.phone'
  1759. ],
  1760. [
  1761. 'first_name',
  1762. 'address'
  1763. ]
  1764. ]));
  1765. $expected = [
  1766. 'div' => ['style' => 'display:none;'],
  1767. ['input' => [
  1768. 'type' => 'hidden',
  1769. 'name' => '_Token[fields]',
  1770. 'value' => $hash,
  1771. 'autocomplete' => 'off',
  1772. 'data-foo' => 'bar',
  1773. ]],
  1774. ['input' => [
  1775. 'type' => 'hidden',
  1776. 'name' => '_Token[unlocked]',
  1777. 'value' => 'address%7Cfirst_name',
  1778. 'autocomplete' => 'off',
  1779. 'data-foo' => 'bar',
  1780. ]],
  1781. ['input' => [
  1782. 'type' => 'hidden', 'name' => '_Token[debug]',
  1783. 'value' => $tokenDebug,
  1784. 'autocomplete' => 'off',
  1785. 'data-foo' => 'bar'
  1786. ]],
  1787. '/div'
  1788. ];
  1789. $this->assertHtml($expected, $result);
  1790. }
  1791. /**
  1792. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1793. *
  1794. * Debug is false, debugSecurity is true -> no debug
  1795. *
  1796. * @return void
  1797. */
  1798. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1799. {
  1800. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1801. 'unlockedFields' => ['first_name', 'address']
  1802. ]));
  1803. $this->Form->create();
  1804. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1805. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1806. $this->Form->text('Addresses.title');
  1807. $this->Form->text('Addresses.first_name');
  1808. $this->Form->text('Addresses.last_name');
  1809. $this->Form->text('Addresses.address');
  1810. $this->Form->text('Addresses.city');
  1811. $this->Form->text('Addresses.phone');
  1812. $result = $this->Form->fields;
  1813. $expected = [
  1814. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1815. 'Addresses.city', 'Addresses.phone'
  1816. ];
  1817. $this->assertEquals($expected, $result);
  1818. Configure::write('debug', false);
  1819. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1820. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1821. $expected = [
  1822. 'div' => ['style' => 'display:none;'],
  1823. ['input' => [
  1824. 'type' => 'hidden',
  1825. 'name' => '_Token[fields]',
  1826. 'value' => $hash,
  1827. 'autocomplete' => 'off',
  1828. 'data-foo' => 'bar',
  1829. ]],
  1830. ['input' => [
  1831. 'type' => 'hidden',
  1832. 'name' => '_Token[unlocked]',
  1833. 'value' => 'address%7Cfirst_name',
  1834. 'autocomplete' => 'off',
  1835. 'data-foo' => 'bar',
  1836. ]],
  1837. '/div'
  1838. ];
  1839. $this->assertHtml($expected, $result);
  1840. }
  1841. /**
  1842. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1843. *
  1844. * Test single record form with debugSecurity param.
  1845. *
  1846. * @return void
  1847. */
  1848. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1849. {
  1850. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1851. 'unlockedFields' => ['first_name', 'address']
  1852. ]));
  1853. $this->Form->create();
  1854. $this->assertEquals(
  1855. $this->View->getRequest()->getParam('_Token.unlockedFields'),
  1856. $this->Form->unlockField()
  1857. );
  1858. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1859. $this->Form->text('Addresses.title');
  1860. $this->Form->text('Addresses.first_name');
  1861. $this->Form->text('Addresses.last_name');
  1862. $this->Form->text('Addresses.address');
  1863. $this->Form->text('Addresses.city');
  1864. $this->Form->text('Addresses.phone');
  1865. $result = $this->Form->fields;
  1866. $expected = [
  1867. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1868. 'Addresses.city', 'Addresses.phone'
  1869. ];
  1870. $this->assertEquals($expected, $result);
  1871. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1872. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1873. $expected = [
  1874. 'div' => ['style' => 'display:none;'],
  1875. ['input' => [
  1876. 'type' => 'hidden',
  1877. 'name' => '_Token[fields]',
  1878. 'value' => $hash,
  1879. 'autocomplete' => 'off',
  1880. 'data-foo' => 'bar',
  1881. ]],
  1882. ['input' => [
  1883. 'type' => 'hidden',
  1884. 'name' => '_Token[unlocked]',
  1885. 'value' => 'address%7Cfirst_name',
  1886. 'autocomplete' => 'off',
  1887. 'data-foo' => 'bar',
  1888. ]],
  1889. '/div'
  1890. ];
  1891. $this->assertHtml($expected, $result);
  1892. }
  1893. /**
  1894. * testFormSecureWithCustomNameAttribute method
  1895. *
  1896. * Test securing inputs with custom name attributes.
  1897. *
  1898. * @return void
  1899. */
  1900. public function testFormSecureWithCustomNameAttribute()
  1901. {
  1902. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1903. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1904. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1905. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1906. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1907. }
  1908. /**
  1909. * testFormSecuredControl method
  1910. *
  1911. * Test generation of entire secure form, assertions made on control() output.
  1912. *
  1913. * @return void
  1914. */
  1915. public function testFormSecuredControl()
  1916. {
  1917. $this->View->setRequest($this->View->getRequest()
  1918. ->withParam('_Token', 'stuff')
  1919. ->withParam('_csrfToken', 'testKey'));
  1920. $this->article['schema'] = [
  1921. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1922. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1923. ];
  1924. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1925. $encoding = strtolower(Configure::read('App.encoding'));
  1926. $expected = [
  1927. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1928. 'div' => ['style' => 'display:none;'],
  1929. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1930. ['input' => [
  1931. 'type' => 'hidden',
  1932. 'name' => '_csrfToken',
  1933. 'value' => 'testKey',
  1934. 'autocomplete' => 'off'
  1935. ]],
  1936. '/div'
  1937. ];
  1938. $this->assertHtml($expected, $result);
  1939. $result = $this->Form->control('ratio');
  1940. $expected = [
  1941. 'div' => ['class'],
  1942. 'label' => ['for'],
  1943. 'Ratio',
  1944. '/label',
  1945. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1946. '/div',
  1947. ];
  1948. $this->assertHtml($expected, $result);
  1949. $result = $this->Form->control('population');
  1950. $expected = [
  1951. 'div' => ['class'],
  1952. 'label' => ['for'],
  1953. 'Population',
  1954. '/label',
  1955. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1956. '/div',
  1957. ];
  1958. $this->assertHtml($expected, $result);
  1959. $result = $this->Form->control('published', ['type' => 'text']);
  1960. $expected = [
  1961. 'div' => ['class' => 'input text'],
  1962. 'label' => ['for' => 'published'],
  1963. 'Published',
  1964. '/label',
  1965. ['input' => [
  1966. 'type' => 'text',
  1967. 'name' => 'published',
  1968. 'id' => 'published'
  1969. ]],
  1970. '/div'
  1971. ];
  1972. $this->assertHtml($expected, $result);
  1973. $result = $this->Form->control('other', ['type' => 'text']);
  1974. $expected = [
  1975. 'div' => ['class' => 'input text'],
  1976. 'label' => ['for' => 'other'],
  1977. 'Other',
  1978. '/label',
  1979. ['input' => [
  1980. 'type' => 'text',
  1981. 'name' => 'other',
  1982. 'id',
  1983. ]],
  1984. '/div'
  1985. ];
  1986. $this->assertHtml($expected, $result);
  1987. $result = $this->Form->hidden('stuff');
  1988. $expected = [
  1989. 'input' => [
  1990. 'type' => 'hidden',
  1991. 'name' => 'stuff'
  1992. ]
  1993. ];
  1994. $this->assertHtml($expected, $result);
  1995. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1996. $expected = ['input' => [
  1997. 'type' => 'hidden',
  1998. 'name' => 'hidden',
  1999. 'value' => '0'
  2000. ]];
  2001. $this->assertHtml($expected, $result);
  2002. $result = $this->Form->control('something', ['type' => 'checkbox']);
  2003. $expected = [
  2004. 'div' => ['class' => 'input checkbox'],
  2005. ['input' => [
  2006. 'type' => 'hidden',
  2007. 'name' => 'something',
  2008. 'value' => '0'
  2009. ]],
  2010. 'label' => ['for' => 'something'],
  2011. ['input' => [
  2012. 'type' => 'checkbox',
  2013. 'name' => 'something',
  2014. 'value' => '1',
  2015. 'id' => 'something'
  2016. ]],
  2017. 'Something',
  2018. '/label',
  2019. '/div'
  2020. ];
  2021. $this->assertHtml($expected, $result);
  2022. $result = $this->Form->fields;
  2023. $expectedFields = [
  2024. 'ratio',
  2025. 'population',
  2026. 'published',
  2027. 'other',
  2028. 'stuff' => '',
  2029. 'hidden' => '0',
  2030. 'something'
  2031. ];
  2032. $this->assertEquals($expectedFields, $result);
  2033. $result = $this->Form->secure($this->Form->fields);
  2034. $tokenDebug = urlencode(json_encode([
  2035. '/articles/add',
  2036. $expectedFields,
  2037. []
  2038. ]));
  2039. $expected = [
  2040. 'div' => ['style' => 'display:none;'],
  2041. ['input' => [
  2042. 'type' => 'hidden',
  2043. 'name' => '_Token[fields]',
  2044. 'value',
  2045. 'autocomplete'
  2046. ]],
  2047. ['input' => [
  2048. 'type' => 'hidden',
  2049. 'name' => '_Token[unlocked]',
  2050. 'value' => '',
  2051. 'autocomplete' => 'off'
  2052. ]],
  2053. ['input' => [
  2054. 'type' => 'hidden', 'name' => '_Token[debug]',
  2055. 'value' => $tokenDebug,
  2056. 'autocomplete' => 'off'
  2057. ]],
  2058. '/div'
  2059. ];
  2060. $this->assertHtml($expected, $result);
  2061. }
  2062. /**
  2063. * testSecuredControlCustomName method
  2064. *
  2065. * Test secured inputs with custom names.
  2066. *
  2067. * @return void
  2068. */
  2069. public function testSecuredControlCustomName()
  2070. {
  2071. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2072. $this->assertEquals([], $this->Form->fields);
  2073. $this->Form->text('text_input', [
  2074. 'name' => 'Option[General.default_role]',
  2075. ]);
  2076. $expected = ['Option.General.default_role'];
  2077. $this->assertEquals($expected, $this->Form->fields);
  2078. $this->Form->select('select_box', [1, 2], [
  2079. 'name' => 'Option[General.select_role]',
  2080. ]);
  2081. $expected[] = 'Option.General.select_role';
  2082. $this->assertEquals($expected, $this->Form->fields);
  2083. $this->Form->text('other.things[]');
  2084. $expected[] = 'other.things';
  2085. $this->assertEquals($expected, $this->Form->fields);
  2086. }
  2087. /**
  2088. * testSecuredControlDuplicate method
  2089. *
  2090. * Test that a hidden field followed by a visible field
  2091. * undoes the hidden field locking.
  2092. *
  2093. * @return void
  2094. */
  2095. public function testSecuredControlDuplicate()
  2096. {
  2097. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2098. $this->assertEquals([], $this->Form->fields);
  2099. $this->Form->control('text_val', [
  2100. 'type' => 'hidden',
  2101. 'value' => 'some text',
  2102. ]);
  2103. $expected = ['text_val' => 'some text'];
  2104. $this->assertEquals($expected, $this->Form->fields);
  2105. $this->Form->control('text_val', [
  2106. 'type' => 'text',
  2107. ]);
  2108. $expected = ['text_val'];
  2109. $this->assertEquals($expected, $this->Form->fields);
  2110. }
  2111. /**
  2112. * testFormSecuredFileControl method
  2113. *
  2114. * Tests that the correct keys are added to the field hash index.
  2115. *
  2116. * @return void
  2117. */
  2118. public function testFormSecuredFileControl()
  2119. {
  2120. $this->assertEquals([], $this->Form->fields);
  2121. $this->Form->file('Attachment.file');
  2122. $expected = [
  2123. 'Attachment.file.name', 'Attachment.file.type',
  2124. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2125. 'Attachment.file.size'
  2126. ];
  2127. $this->assertEquals($expected, $this->Form->fields);
  2128. }
  2129. /**
  2130. * testFormSecuredMultipleSelect method
  2131. *
  2132. * Test that multiple selects keys are added to field hash.
  2133. *
  2134. * @return void
  2135. */
  2136. public function testFormSecuredMultipleSelect()
  2137. {
  2138. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  2139. $this->assertEquals([], $this->Form->fields);
  2140. $options = ['1' => 'one', '2' => 'two'];
  2141. $this->Form->select('Model.select', $options);
  2142. $expected = ['Model.select'];
  2143. $this->assertEquals($expected, $this->Form->fields);
  2144. $this->Form->fields = [];
  2145. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2146. $this->assertEquals($expected, $this->Form->fields);
  2147. }
  2148. /**
  2149. * testFormSecuredRadio method
  2150. *
  2151. * @return void
  2152. */
  2153. public function testFormSecuredRadio()
  2154. {
  2155. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2156. $this->assertEquals([], $this->Form->fields);
  2157. $options = ['1' => 'option1', '2' => 'option2'];
  2158. $this->Form->radio('Test.test', $options);
  2159. $expected = ['Test.test'];
  2160. $this->assertEquals($expected, $this->Form->fields);
  2161. $this->Form->radio('Test.all', $options, [
  2162. 'disabled' => ['option1', 'option2']
  2163. ]);
  2164. $expected = ['Test.test', 'Test.all' => ''];
  2165. $this->assertEquals($expected, $this->Form->fields);
  2166. $this->Form->radio('Test.some', $options, [
  2167. 'disabled' => ['option1']
  2168. ]);
  2169. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2170. $this->assertEquals($expected, $this->Form->fields);
  2171. }
  2172. /**
  2173. * testFormSecuredAndDisabledNotAssoc method
  2174. *
  2175. * Test that when disabled is in a list based attribute array it works.
  2176. *
  2177. * @return void
  2178. */
  2179. public function testFormSecuredAndDisabledNotAssoc()
  2180. {
  2181. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2182. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2183. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2184. $this->Form->text('Model.text', ['disabled']);
  2185. $this->Form->textarea('Model.textarea', ['disabled']);
  2186. $this->Form->password('Model.password', ['disabled']);
  2187. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2188. $expected = [
  2189. 'Model.radio' => ''
  2190. ];
  2191. $this->assertEquals($expected, $this->Form->fields);
  2192. }
  2193. /**
  2194. * testFormSecuredAndDisabled method
  2195. *
  2196. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2197. * hashing.
  2198. *
  2199. * @return void
  2200. */
  2201. public function testFormSecuredAndDisabled()
  2202. {
  2203. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2204. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2205. $this->Form->text('Model.text', ['disabled' => true]);
  2206. $this->Form->password('Model.text', ['disabled' => true]);
  2207. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2208. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2209. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2210. $this->Form->year('Model.year', ['disabled' => true]);
  2211. $this->Form->month('Model.month', ['disabled' => true]);
  2212. $this->Form->day('Model.day', ['disabled' => true]);
  2213. $this->Form->hour('Model.hour', ['disabled' => true]);
  2214. $this->Form->minute('Model.minute', ['disabled' => true]);
  2215. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2216. $expected = [
  2217. 'Model.radio' => ''
  2218. ];
  2219. $this->assertEquals($expected, $this->Form->fields);
  2220. }
  2221. /**
  2222. * testDisableSecurityUsingForm method
  2223. *
  2224. * @return void
  2225. */
  2226. public function testDisableSecurityUsingForm()
  2227. {
  2228. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2229. 'disabledFields' => []
  2230. ]));
  2231. $this->Form->create();
  2232. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2233. $this->Form->text('Addresses.title');
  2234. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2235. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2236. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2237. $result = $this->Form->fields;
  2238. $expected = [
  2239. 'Addresses.id' => '123456', 'Addresses.title',
  2240. ];
  2241. $this->assertEquals($expected, $result);
  2242. }
  2243. /**
  2244. * testUnlockFieldAddsToList method
  2245. *
  2246. * Test disableField.
  2247. *
  2248. * @return void
  2249. */
  2250. public function testUnlockFieldAddsToList()
  2251. {
  2252. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2253. 'unlockedFields' => []
  2254. ]));
  2255. $this->Form->unlockField('Contact.name');
  2256. $this->Form->text('Contact.name');
  2257. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2258. $this->assertEquals([], $this->Form->fields);
  2259. }
  2260. /**
  2261. * testUnlockFieldRemovingFromFields method
  2262. *
  2263. * Test unlockField removing from fields array.
  2264. *
  2265. * @return void
  2266. */
  2267. public function testUnlockFieldRemovingFromFields()
  2268. {
  2269. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2270. 'unlockedFields' => []
  2271. ]));
  2272. $this->Form->create($this->article);
  2273. $this->Form->hidden('Article.id', ['value' => 1]);
  2274. $this->Form->text('Article.title');
  2275. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2276. $this->assertContains('Article.title', $this->Form->fields, 'Field should be secured.');
  2277. $this->Form->unlockField('Article.title');
  2278. $this->Form->unlockField('Article.id');
  2279. $this->assertEquals([], $this->Form->fields);
  2280. }
  2281. /**
  2282. * testResetUnlockFields method
  2283. *
  2284. * Test reset unlockFields, when create new form.
  2285. *
  2286. * @return void
  2287. */
  2288. public function testResetUnlockFields()
  2289. {
  2290. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2291. 'key' => 'testKey',
  2292. 'unlockedFields' => []
  2293. ]));
  2294. $this->Form->unlockField('Contact.id');
  2295. $this->Form->create('Contact');
  2296. $this->Form->hidden('Contact.id', ['value' => 1]);
  2297. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2298. $this->Form->end();
  2299. $this->Form->create('Contact');
  2300. $this->Form->hidden('Contact.id', ['value' => 1]);
  2301. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2302. }
  2303. /**
  2304. * testSecuredFormUrlIgnoresHost method
  2305. *
  2306. * Test that only the path + query elements of a form's URL show up in their hash.
  2307. *
  2308. * @return void
  2309. */
  2310. public function testSecuredFormUrlIgnoresHost()
  2311. {
  2312. $this->View->setRequest($this->View->getRequest()->withParam('_Token', ['key' => 'testKey']));
  2313. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2314. $this->Form->create($this->article, [
  2315. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2316. ]);
  2317. $result = $this->Form->secure();
  2318. $this->assertContains($expected, $result);
  2319. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2320. $result = $this->Form->secure();
  2321. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2322. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2323. $result = $this->Form->secure();
  2324. $this->assertContains($expected, $result, 'URL path + query should work.');
  2325. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2326. $result = $this->Form->secure();
  2327. $this->assertNotContains($expected, $result, 'URL is different');
  2328. }
  2329. /**
  2330. * testSecuredFormUrlHasHtmlAndIdentifier method
  2331. *
  2332. * Test that URL, HTML and identifier show up in their hashes.
  2333. *
  2334. * @return void
  2335. */
  2336. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2337. {
  2338. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2339. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2340. $this->Form->create($this->article, [
  2341. 'url' => [
  2342. 'controller' => 'articles',
  2343. 'action' => 'view',
  2344. '?' => [
  2345. 'page' => 1,
  2346. 'limit' => 10,
  2347. 'html' => '<>"',
  2348. ],
  2349. '#' => 'result',
  2350. ],
  2351. ]);
  2352. $result = $this->Form->secure();
  2353. $this->assertContains($expected, $result);
  2354. $this->Form->create($this->article, [
  2355. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2356. ]);
  2357. $result = $this->Form->secure();
  2358. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2359. $this->Form->create($this->article, [
  2360. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2361. ]);
  2362. $result = $this->Form->secure();
  2363. $this->assertContains($expected, $result, 'URL path + query should work.');
  2364. }
  2365. /**
  2366. * testErrorMessageDisplay method
  2367. *
  2368. * Test error message display.
  2369. *
  2370. * @return void
  2371. */
  2372. public function testErrorMessageDisplay()
  2373. {
  2374. $this->article['errors'] = [
  2375. 'Article' => [
  2376. 'title' => 'error message',
  2377. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2378. ]
  2379. ];
  2380. $this->Form->create($this->article);
  2381. $result = $this->Form->control('Article.title');
  2382. $expected = [
  2383. 'div' => ['class' => 'input text error'],
  2384. 'label' => ['for' => 'article-title'],
  2385. 'Title',
  2386. '/label',
  2387. 'input' => [
  2388. 'type' => 'text', 'name' => 'Article[title]',
  2389. 'id' => 'article-title', 'class' => 'form-error'
  2390. ],
  2391. ['div' => ['class' => 'error-message']],
  2392. 'error message',
  2393. '/div',
  2394. '/div'
  2395. ];
  2396. $this->assertHtml($expected, $result);
  2397. $result = $this->Form->control('Article.title', [
  2398. 'templates' => [
  2399. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2400. ]
  2401. ]);
  2402. $expected = [
  2403. 'div' => ['class' => 'input text error'],
  2404. 'label' => ['for' => 'article-title'],
  2405. 'Title',
  2406. '/label',
  2407. 'input' => [
  2408. 'type' => 'text', 'name' => 'Article[title]',
  2409. 'id' => 'article-title', 'class' => 'form-error'
  2410. ],
  2411. '/div'
  2412. ];
  2413. $this->assertHtml($expected, $result);
  2414. $result = $this->Form->control('Article.content');
  2415. $expected = [
  2416. 'div' => ['class' => 'input text error'],
  2417. 'label' => ['for' => 'article-content'],
  2418. 'Content',
  2419. '/label',
  2420. 'input' => [
  2421. 'type' => 'text', 'name' => 'Article[content]',
  2422. 'id' => 'article-content', 'class' => 'form-error'
  2423. ],
  2424. ['div' => ['class' => 'error-message']],
  2425. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2426. '/div',
  2427. '/div'
  2428. ];
  2429. $this->assertHtml($expected, $result);
  2430. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2431. $expected = [
  2432. 'div' => ['class' => 'input text error'],
  2433. 'label' => ['for' => 'article-content'],
  2434. 'Content',
  2435. '/label',
  2436. 'input' => [
  2437. 'type' => 'text', 'name' => 'Article[content]',
  2438. 'id' => 'article-content', 'class' => 'form-error'
  2439. ],
  2440. ['div' => ['class' => 'error-message']],
  2441. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2442. '/div',
  2443. '/div'
  2444. ];
  2445. $this->assertHtml($expected, $result);
  2446. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2447. $expected = [
  2448. 'div' => ['class' => 'input text error'],
  2449. 'label' => ['for' => 'article-content'],
  2450. 'Content',
  2451. '/label',
  2452. 'input' => [
  2453. 'type' => 'text', 'name' => 'Article[content]',
  2454. 'id' => 'article-content', 'class' => 'form-error'
  2455. ],
  2456. ['div' => ['class' => 'error-message']],
  2457. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2458. '/div',
  2459. '/div'
  2460. ];
  2461. $this->assertHtml($expected, $result);
  2462. }
  2463. /**
  2464. * testEmptyErrorValidation method
  2465. *
  2466. * Test validation errors, when validation message is an empty string.
  2467. *
  2468. * @return void
  2469. */
  2470. public function testEmptyErrorValidation()
  2471. {
  2472. $this->article['errors'] = [
  2473. 'Article' => ['title' => '']
  2474. ];
  2475. $this->Form->create($this->article);
  2476. $result = $this->Form->control('Article.title');
  2477. $expected = [
  2478. 'div' => ['class' => 'input text error'],
  2479. 'label' => ['for' => 'article-title'],
  2480. 'Title',
  2481. '/label',
  2482. 'input' => [
  2483. 'type' => 'text', 'name' => 'Article[title]',
  2484. 'id' => 'article-title', 'class' => 'form-error'
  2485. ],
  2486. ['div' => ['class' => 'error-message']],
  2487. [],
  2488. '/div',
  2489. '/div'
  2490. ];
  2491. $this->assertHtml($expected, $result);
  2492. }
  2493. /**
  2494. * testEmptyControlErrorValidation method
  2495. *
  2496. * Test validation errors, when calling control() overriding validation message by an empty string.
  2497. *
  2498. * @return void
  2499. */
  2500. public function testEmptyControlErrorValidation()
  2501. {
  2502. $this->article['errors'] = [
  2503. 'Article' => ['title' => 'error message']
  2504. ];
  2505. $this->Form->create($this->article);
  2506. $result = $this->Form->control('Article.title', ['error' => '']);
  2507. $expected = [
  2508. 'div' => ['class' => 'input text error'],
  2509. 'label' => ['for' => 'article-title'],
  2510. 'Title',
  2511. '/label',
  2512. 'input' => [
  2513. 'type' => 'text', 'name' => 'Article[title]',
  2514. 'id' => 'article-title', 'class' => 'form-error'
  2515. ],
  2516. ['div' => ['class' => 'error-message']],
  2517. [],
  2518. '/div',
  2519. '/div'
  2520. ];
  2521. $this->assertHtml($expected, $result);
  2522. }
  2523. /**
  2524. * testControlErrorMessage method
  2525. *
  2526. * Test validation errors, when calling control() overriding validation messages.
  2527. *
  2528. * @return void
  2529. */
  2530. public function testControlErrorMessage()
  2531. {
  2532. $this->article['errors'] = [
  2533. 'title' => ['error message']
  2534. ];
  2535. $this->Form->create($this->article);
  2536. $result = $this->Form->control('title', [
  2537. 'error' => 'Custom error!'
  2538. ]);
  2539. $expected = [
  2540. 'div' => ['class' => 'input text required error'],
  2541. 'label' => ['for' => 'title'],
  2542. 'Title',
  2543. '/label',
  2544. 'input' => [
  2545. 'type' => 'text', 'name' => 'title',
  2546. 'id' => 'title', 'class' => 'form-error',
  2547. 'required' => 'required',
  2548. ],
  2549. ['div' => ['class' => 'error-message']],
  2550. 'Custom error!',
  2551. '/div',
  2552. '/div'
  2553. ];
  2554. $this->assertHtml($expected, $result);
  2555. $result = $this->Form->control('title', [
  2556. 'error' => ['error message' => 'Custom error!']
  2557. ]);
  2558. $expected = [
  2559. 'div' => ['class' => 'input text required error'],
  2560. 'label' => ['for' => 'title'],
  2561. 'Title',
  2562. '/label',
  2563. 'input' => [
  2564. 'type' => 'text',
  2565. 'name' => 'title',
  2566. 'id' => 'title',
  2567. 'class' => 'form-error',
  2568. 'required' => 'required'
  2569. ],
  2570. ['div' => ['class' => 'error-message']],
  2571. 'Custom error!',
  2572. '/div',
  2573. '/div'
  2574. ];
  2575. $this->assertHtml($expected, $result);
  2576. }
  2577. /**
  2578. * testFormValidationAssociated method
  2579. *
  2580. * Tests displaying errors for nested entities.
  2581. *
  2582. * @return void
  2583. */
  2584. public function testFormValidationAssociated()
  2585. {
  2586. $nested = new Entity(['foo' => 'bar']);
  2587. $nested->setError('foo', ['not a valid bar']);
  2588. $entity = new Entity(['nested' => $nested]);
  2589. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2590. $result = $this->Form->error('nested.foo');
  2591. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2592. }
  2593. /**
  2594. * testFormValidationAssociatedSecondLevel method
  2595. *
  2596. * Test form error display with associated model.
  2597. *
  2598. * @return void
  2599. */
  2600. public function testFormValidationAssociatedSecondLevel()
  2601. {
  2602. $inner = new Entity(['bar' => 'baz']);
  2603. $nested = new Entity(['foo' => $inner]);
  2604. $entity = new Entity(['nested' => $nested]);
  2605. $inner->setError('bar', ['not a valid one']);
  2606. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2607. $result = $this->Form->error('nested.foo.bar');
  2608. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2609. }
  2610. /**
  2611. * testFormValidationMultiRecord method
  2612. *
  2613. * Test form error display with multiple records.
  2614. *
  2615. * @return void
  2616. */
  2617. public function testFormValidationMultiRecord()
  2618. {
  2619. $one = new Entity();
  2620. $two = new Entity();
  2621. $this->getTableLocator()->get('Contacts', [
  2622. 'className' => __NAMESPACE__ . '\ContactsTable'
  2623. ]);
  2624. $one->set('email', '');
  2625. $one->setError('email', ['invalid email']);
  2626. $two->set('name', '');
  2627. $two->setError('name', ['This is wrong']);
  2628. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2629. $result = $this->Form->control('0.email');
  2630. $expected = [
  2631. 'div' => ['class' => 'input email error'],
  2632. 'label' => ['for' => '0-email'],
  2633. 'Email',
  2634. '/label',
  2635. 'input' => [
  2636. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2637. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2638. ],
  2639. ['div' => ['class' => 'error-message']],
  2640. 'invalid email',
  2641. '/div',
  2642. '/div'
  2643. ];
  2644. $this->assertHtml($expected, $result);
  2645. $result = $this->Form->control('1.name');
  2646. $expected = [
  2647. 'div' => ['class' => 'input text error'],
  2648. 'label' => ['for' => '1-name'],
  2649. 'Name',
  2650. '/label',
  2651. 'input' => [
  2652. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2653. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2654. ],
  2655. ['div' => ['class' => 'error-message']],
  2656. 'This is wrong',
  2657. '/div',
  2658. '/div'
  2659. ];
  2660. $this->assertHtml($expected, $result);
  2661. }
  2662. /**
  2663. * testControl method
  2664. *
  2665. * Test various incarnations of control().
  2666. *
  2667. * @return void
  2668. */
  2669. public function testControl()
  2670. {
  2671. $this->getTableLocator()->get('ValidateUsers', [
  2672. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2673. ]);
  2674. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2675. $result = $this->Form->control('ValidateUsers.balance');
  2676. $expected = [
  2677. 'div' => ['class'],
  2678. 'label' => ['for'],
  2679. 'Balance',
  2680. '/label',
  2681. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2682. '/div',
  2683. ];
  2684. $this->assertHtml($expected, $result);
  2685. $result = $this->Form->control('ValidateUser.cost_decimal');
  2686. $expected = [
  2687. 'div' => ['class'],
  2688. 'label' => ['for'],
  2689. 'Cost Decimal',
  2690. '/label',
  2691. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2692. '/div',
  2693. ];
  2694. $this->assertHtml($expected, $result);
  2695. $result = $this->Form->control('ValidateUser.null_decimal');
  2696. $expected = [
  2697. 'div' => ['class'],
  2698. 'label' => ['for'],
  2699. 'Null Decimal',
  2700. '/label',
  2701. 'input' => ['name', 'type' => 'number', 'id'],
  2702. '/div',
  2703. ];
  2704. $this->assertHtml($expected, $result);
  2705. }
  2706. /**
  2707. * testControlCustomization method
  2708. *
  2709. * Tests the input method and passing custom options.
  2710. *
  2711. * @return void
  2712. */
  2713. public function testControlCustomization()
  2714. {
  2715. $this->getTableLocator()->get('Contacts', [
  2716. 'className' => __NAMESPACE__ . '\ContactsTable'
  2717. ]);
  2718. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2719. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2720. $expected = [
  2721. 'div' => ['class' => 'input email'],
  2722. 'label' => ['for' => 'custom'],
  2723. 'Email',
  2724. '/label',
  2725. ['input' => [
  2726. 'type' => 'email', 'name' => 'Contact[email]',
  2727. 'id' => 'custom', 'maxlength' => 255
  2728. ]],
  2729. '/div'
  2730. ];
  2731. $this->assertHtml($expected, $result);
  2732. $result = $this->Form->control('Contact.email', [
  2733. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2734. ]);
  2735. $expected = [
  2736. '<div',
  2737. 'label' => ['for' => 'contact-email'],
  2738. 'Email',
  2739. '/label',
  2740. ['input' => [
  2741. 'type' => 'email', 'name' => 'Contact[email]',
  2742. 'id' => 'contact-email', 'maxlength' => 255
  2743. ]],
  2744. '/div'
  2745. ];
  2746. $this->assertHtml($expected, $result);
  2747. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2748. $expected = [
  2749. 'div' => ['class' => 'input text'],
  2750. 'label' => ['for' => 'contact-email'],
  2751. 'Email',
  2752. '/label',
  2753. ['input' => [
  2754. 'type' => 'text', 'name' => 'Contact[email]',
  2755. 'id' => 'contact-email', 'maxlength' => '255'
  2756. ]],
  2757. '/div'
  2758. ];
  2759. $this->assertHtml($expected, $result);
  2760. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2761. $expected = [
  2762. 'div' => ['class' => 'input text'],
  2763. 'label' => ['for' => 'contact-5-email'],
  2764. 'Email',
  2765. '/label',
  2766. ['input' => [
  2767. 'type' => 'text', 'name' => 'Contact[5][email]',
  2768. 'id' => 'contact-5-email', 'maxlength' => '255'
  2769. ]],
  2770. '/div'
  2771. ];
  2772. $this->assertHtml($expected, $result);
  2773. $result = $this->Form->control('Contact.password');
  2774. $expected = [
  2775. 'div' => ['class' => 'input password'],
  2776. 'label' => ['for' => 'contact-password'],
  2777. 'Password',
  2778. '/label',
  2779. ['input' => [
  2780. 'type' => 'password', 'name' => 'Contact[password]',
  2781. 'id' => 'contact-password'
  2782. ]],
  2783. '/div'
  2784. ];
  2785. $this->assertHtml($expected, $result);
  2786. $result = $this->Form->control('Contact.email', [
  2787. 'type' => 'file', 'class' => 'textbox'
  2788. ]);
  2789. $expected = [
  2790. 'div' => ['class' => 'input file'],
  2791. 'label' => ['for' => 'contact-email'],
  2792. 'Email',
  2793. '/label',
  2794. ['input' => [
  2795. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2796. 'id' => 'contact-email'
  2797. ]],
  2798. '/div'
  2799. ];
  2800. $this->assertHtml($expected, $result);
  2801. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2802. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2803. $result = $this->Form->control('phone');
  2804. $expected = [
  2805. 'div' => ['class' => 'input tel'],
  2806. 'label' => ['for' => 'phone'],
  2807. 'Phone',
  2808. '/label',
  2809. ['input' => [
  2810. 'type' => 'tel', 'name' => 'phone',
  2811. 'value' => 'Hello &amp; World &gt; weird chars',
  2812. 'id' => 'phone', 'maxlength' => 255
  2813. ]],
  2814. '/div'
  2815. ];
  2816. $this->assertHtml($expected, $result);
  2817. $this->View->setRequest(
  2818. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2819. );
  2820. $this->Form->create();
  2821. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2822. $expected = [
  2823. 'div' => ['class' => 'input text'],
  2824. 'label' => ['for' => 'myId'],
  2825. 'Field',
  2826. '/label',
  2827. 'input' => [
  2828. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2829. 'value' => 'My value', 'id' => 'myId'
  2830. ],
  2831. '/div'
  2832. ];
  2833. $this->assertHtml($expected, $result);
  2834. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2835. $this->Form->create();
  2836. $entity->setError('field', 'Badness!');
  2837. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2838. $result = $this->Form->control('field');
  2839. $expected = [
  2840. 'div' => ['class' => 'input text error'],
  2841. 'label' => ['for' => 'field'],
  2842. 'Field',
  2843. '/label',
  2844. 'input' => [
  2845. 'type' => 'text', 'name' => 'field',
  2846. 'id' => 'field', 'class' => 'form-error'
  2847. ],
  2848. ['div' => ['class' => 'error-message']],
  2849. 'Badness!',
  2850. '/div',
  2851. '/div'
  2852. ];
  2853. $this->assertHtml($expected, $result);
  2854. $result = $this->Form->control('field', [
  2855. 'templates' => [
  2856. 'inputContainerError' => '{{content}}{{error}}',
  2857. 'error' => '<span class="error-message">{{content}}</span>'
  2858. ]
  2859. ]);
  2860. $expected = [
  2861. 'label' => ['for' => 'field'],
  2862. 'Field',
  2863. '/label',
  2864. 'input' => [
  2865. 'type' => 'text', 'name' => 'field',
  2866. 'id' => 'field', 'class' => 'form-error'
  2867. ],
  2868. ['span' => ['class' => 'error-message']],
  2869. 'Badness!',
  2870. '/span'
  2871. ];
  2872. $this->assertHtml($expected, $result);
  2873. $entity->setError('field', ['minLength'], true);
  2874. $result = $this->Form->control('field', [
  2875. 'error' => [
  2876. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2877. 'maxLength' => 'login too large'
  2878. ]
  2879. ]);
  2880. $expected = [
  2881. 'div' => ['class' => 'input text error'],
  2882. 'label' => ['for' => 'field'],
  2883. 'Field',
  2884. '/label',
  2885. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2886. ['div' => ['class' => 'error-message']],
  2887. 'Le login doit contenir au moins 2 caractères',
  2888. '/div',
  2889. '/div'
  2890. ];
  2891. $this->assertHtml($expected, $result);
  2892. $entity->setError('field', ['maxLength'], true);
  2893. $result = $this->Form->control('field', [
  2894. 'error' => [
  2895. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2896. 'maxLength' => 'login too large',
  2897. ]
  2898. ]);
  2899. $expected = [
  2900. 'div' => ['class' => 'input text error'],
  2901. 'label' => ['for' => 'field'],
  2902. 'Field',
  2903. '/label',
  2904. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2905. ['div' => ['class' => 'error-message']],
  2906. 'login too large',
  2907. '/div',
  2908. '/div'
  2909. ];
  2910. $this->assertHtml($expected, $result);
  2911. }
  2912. /**
  2913. * testControlWithTemplateFile method
  2914. *
  2915. * Test that control() accepts a template file.
  2916. *
  2917. * @return void
  2918. */
  2919. public function testControlWithTemplateFile()
  2920. {
  2921. $result = $this->Form->control('field', [
  2922. 'templates' => 'htmlhelper_tags'
  2923. ]);
  2924. $expected = [
  2925. 'label' => ['for' => 'field'],
  2926. 'Field',
  2927. '/label',
  2928. 'input' => [
  2929. 'type' => 'text', 'name' => 'field',
  2930. 'id' => 'field'
  2931. ],
  2932. ];
  2933. $this->assertHtml($expected, $result);
  2934. }
  2935. /**
  2936. * testNestedControlsEndWithBrackets method
  2937. *
  2938. * Test that nested inputs end with brackets.
  2939. *
  2940. * @return void
  2941. */
  2942. public function testNestedControlsEndWithBrackets()
  2943. {
  2944. $result = $this->Form->text('nested.text[]');
  2945. $expected = [
  2946. 'input' => [
  2947. 'type' => 'text', 'name' => 'nested[text][]'
  2948. ],
  2949. ];
  2950. $this->assertHtml($expected, $result);
  2951. $result = $this->Form->file('nested.file[]');
  2952. $expected = [
  2953. 'input' => [
  2954. 'type' => 'file', 'name' => 'nested[file][]'
  2955. ],
  2956. ];
  2957. $this->assertHtml($expected, $result);
  2958. }
  2959. /**
  2960. * testCreateIdPrefix method
  2961. *
  2962. * Test id prefix.
  2963. *
  2964. * @return void
  2965. */
  2966. public function testCreateIdPrefix()
  2967. {
  2968. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2969. $result = $this->Form->control('field');
  2970. $expected = [
  2971. 'div' => ['class' => 'input text'],
  2972. 'label' => ['for' => 'prefix-field'],
  2973. 'Field',
  2974. '/label',
  2975. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2976. '/div'
  2977. ];
  2978. $this->assertHtml($expected, $result);
  2979. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2980. $expected = [
  2981. 'div' => ['class' => 'input text'],
  2982. 'label' => ['for' => 'custom-id'],
  2983. 'Field',
  2984. '/label',
  2985. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2986. '/div'
  2987. ];
  2988. $this->assertHtml($expected, $result);
  2989. $result = $this->Form->radio('Model.field', ['option A']);
  2990. $expected = [
  2991. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2992. 'label' => ['for' => 'prefix-model-field-0'],
  2993. ['input' => [
  2994. 'type' => 'radio',
  2995. 'name' => 'Model[field]',
  2996. 'value' => '0',
  2997. 'id' => 'prefix-model-field-0'
  2998. ]],
  2999. 'option A',
  3000. '/label'
  3001. ];
  3002. $this->assertHtml($expected, $result);
  3003. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  3004. $expected = [
  3005. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3006. 'label' => ['for' => 'prefix-model-field-0'],
  3007. ['input' => [
  3008. 'type' => 'radio',
  3009. 'name' => 'Model[field]',
  3010. 'value' => '0',
  3011. 'id' => 'prefix-model-field-0'
  3012. ]],
  3013. 'option A',
  3014. '/label'
  3015. ];
  3016. $this->assertHtml($expected, $result);
  3017. $result = $this->Form->select(
  3018. 'Model.multi_field',
  3019. ['first'],
  3020. ['multiple' => 'checkbox']
  3021. );
  3022. $expected = [
  3023. 'input' => [
  3024. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3025. ],
  3026. ['div' => ['class' => 'checkbox']],
  3027. ['label' => ['for' => 'prefix-model-multi-field-0']],
  3028. ['input' => [
  3029. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3030. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  3031. ]],
  3032. 'first',
  3033. '/label',
  3034. '/div',
  3035. ];
  3036. $this->assertHtml($expected, $result);
  3037. $this->Form->end();
  3038. $result = $this->Form->control('field');
  3039. $expected = [
  3040. 'div' => ['class' => 'input text'],
  3041. 'label' => ['for' => 'field'],
  3042. 'Field',
  3043. '/label',
  3044. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3045. '/div'
  3046. ];
  3047. $this->assertHtml($expected, $result);
  3048. }
  3049. /**
  3050. * testControlZero method
  3051. *
  3052. * Test that inputs with 0 can be created.
  3053. *
  3054. * @return void
  3055. */
  3056. public function testControlZero()
  3057. {
  3058. $this->getTableLocator()->get('Contacts', [
  3059. 'className' => __NAMESPACE__ . '\ContactsTable'
  3060. ]);
  3061. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3062. $result = $this->Form->control('0');
  3063. $expected = [
  3064. 'div' => ['class' => 'input text'],
  3065. 'label' => ['for' => '0'], '/label',
  3066. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3067. '/div'
  3068. ];
  3069. $this->assertHtml($expected, $result);
  3070. }
  3071. /**
  3072. * testControlCheckbox method
  3073. *
  3074. * Test control() with checkbox creation.
  3075. *
  3076. * @return void
  3077. */
  3078. public function testControlCheckbox()
  3079. {
  3080. $articles = $this->getTableLocator()->get('Articles');
  3081. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3082. $article = $articles->newEntity();
  3083. $this->Form->create($article);
  3084. $result = $this->Form->control('Articles.active');
  3085. $expected = [
  3086. 'div' => ['class' => 'input checkbox'],
  3087. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3088. 'label' => ['for' => 'articles-active'],
  3089. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3090. 'Active',
  3091. '/label',
  3092. '/div'
  3093. ];
  3094. $this->assertHtml($expected, $result);
  3095. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3096. $expected = [
  3097. 'div' => ['class' => 'input checkbox'],
  3098. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3099. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3100. '/div'
  3101. ];
  3102. $this->assertHtml($expected, $result);
  3103. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3104. $expected = [
  3105. 'div' => ['class' => 'input checkbox'],
  3106. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3107. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3108. '/div'
  3109. ];
  3110. $this->assertHtml($expected, $result);
  3111. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3112. $expected = [
  3113. 'div' => ['class' => 'input checkbox'],
  3114. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3115. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3116. '/div'
  3117. ];
  3118. $this->assertHtml($expected, $result);
  3119. $result = $this->Form->control('Articles.disabled', [
  3120. 'label' => 'Disabled',
  3121. 'type' => 'checkbox',
  3122. 'data-foo' => 'disabled'
  3123. ]);
  3124. $expected = [
  3125. 'div' => ['class' => 'input checkbox'],
  3126. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3127. 'label' => ['for' => 'articles-disabled'],
  3128. ['input' => [
  3129. 'type' => 'checkbox',
  3130. 'name' => 'Articles[disabled]',
  3131. 'value' => '1',
  3132. 'id' => 'articles-disabled',
  3133. 'data-foo' => 'disabled'
  3134. ]],
  3135. 'Disabled',
  3136. '/label',
  3137. '/div'
  3138. ];
  3139. $this->assertHtml($expected, $result);
  3140. $result = $this->Form->control('Articles.confirm', [
  3141. 'label' => 'Confirm <b>me</b>!',
  3142. 'type' => 'checkbox',
  3143. 'escape' => false
  3144. ]);
  3145. $expected = [
  3146. 'div' => ['class' => 'input checkbox'],
  3147. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3148. 'label' => ['for' => 'articles-confirm'],
  3149. ['input' => [
  3150. 'type' => 'checkbox',
  3151. 'name' => 'Articles[confirm]',
  3152. 'value' => '1',
  3153. 'id' => 'articles-confirm',
  3154. ]],
  3155. 'Confirm <b>me</b>!',
  3156. '/label',
  3157. '/div'
  3158. ];
  3159. $this->assertHtml($expected, $result);
  3160. }
  3161. /**
  3162. * testControlHidden method
  3163. *
  3164. * Test that control() does not create wrapping div and label tag for hidden fields.
  3165. *
  3166. * @return void
  3167. */
  3168. public function testControlHidden()
  3169. {
  3170. $this->getTableLocator()->get('ValidateUsers', [
  3171. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3172. ]);
  3173. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3174. $result = $this->Form->control('ValidateUser.id');
  3175. $expected = [
  3176. 'input' => ['name', 'type' => 'hidden', 'id']
  3177. ];
  3178. $this->assertHtml($expected, $result);
  3179. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3180. $expected = [
  3181. 'input' => ['name', 'type' => 'hidden', 'id']
  3182. ];
  3183. $this->assertHtml($expected, $result);
  3184. }
  3185. /**
  3186. * testControlDatetime method
  3187. *
  3188. * Test form->control() with datetime.
  3189. *
  3190. * @return void
  3191. */
  3192. public function testControlDatetime()
  3193. {
  3194. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3195. ->setMethods(['datetime'])
  3196. ->setConstructorArgs([new View()])
  3197. ->getMock();
  3198. $this->Form->expects($this->once())->method('datetime')
  3199. ->with('prueba', [
  3200. 'type' => 'datetime',
  3201. 'timeFormat' => 24,
  3202. 'minYear' => 2008,
  3203. 'maxYear' => 2011,
  3204. 'interval' => 15,
  3205. 'options' => null,
  3206. 'empty' => false,
  3207. 'id' => 'prueba',
  3208. 'required' => false,
  3209. 'templateVars' => []
  3210. ])
  3211. ->will($this->returnValue('This is it!'));
  3212. $result = $this->Form->control('prueba', [
  3213. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3214. 'maxYear' => 2011, 'interval' => 15
  3215. ]);
  3216. $expected = [
  3217. 'div' => ['class' => 'input datetime'],
  3218. '<label',
  3219. 'Prueba',
  3220. '/label',
  3221. 'This is it!',
  3222. '/div'
  3223. ];
  3224. $this->assertHtml($expected, $result);
  3225. }
  3226. /**
  3227. * testControlDatetimeIdPrefix method
  3228. *
  3229. * Test form->control() with datetime with id prefix.
  3230. *
  3231. * @return void
  3232. */
  3233. public function testControlDatetimeIdPrefix()
  3234. {
  3235. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3236. ->setMethods(['datetime'])
  3237. ->setConstructorArgs([new View()])
  3238. ->getMock();
  3239. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3240. $this->Form->expects($this->once())->method('datetime')
  3241. ->with('prueba', [
  3242. 'type' => 'datetime',
  3243. 'timeFormat' => 24,
  3244. 'minYear' => 2008,
  3245. 'maxYear' => 2011,
  3246. 'interval' => 15,
  3247. 'options' => null,
  3248. 'empty' => false,
  3249. 'id' => 'prefix-prueba',
  3250. 'required' => false,
  3251. 'templateVars' => []
  3252. ])
  3253. ->will($this->returnValue('This is it!'));
  3254. $result = $this->Form->control('prueba', [
  3255. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3256. 'maxYear' => 2011, 'interval' => 15
  3257. ]);
  3258. $expected = [
  3259. 'div' => ['class' => 'input datetime'],
  3260. '<label',
  3261. 'Prueba',
  3262. '/label',
  3263. 'This is it!',
  3264. '/div'
  3265. ];
  3266. $this->assertHtml($expected, $result);
  3267. }
  3268. /**
  3269. * testControlCheckboxWithDisabledElements method
  3270. *
  3271. * Test generating checkboxes with disabled elements.
  3272. *
  3273. * @return void
  3274. */
  3275. public function testControlCheckboxWithDisabledElements()
  3276. {
  3277. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3278. $result = $this->Form->control('Contact.multiple', [
  3279. 'multiple' => 'checkbox',
  3280. 'disabled' => 'disabled',
  3281. 'options' => $options
  3282. ]);
  3283. $expected = [
  3284. ['div' => ['class' => 'input select']],
  3285. ['label' => ['for' => 'contact-multiple']],
  3286. 'Multiple',
  3287. '/label',
  3288. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3289. ['div' => ['class' => 'checkbox']],
  3290. ['label' => ['for' => 'contact-multiple-1']],
  3291. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3292. 'One',
  3293. '/label',
  3294. '/div',
  3295. ['div' => ['class' => 'checkbox']],
  3296. ['label' => ['for' => 'contact-multiple-2']],
  3297. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3298. 'Two',
  3299. '/label',
  3300. '/div',
  3301. ['div' => ['class' => 'checkbox']],
  3302. ['label' => ['for' => 'contact-multiple-3']],
  3303. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3304. 'Three',
  3305. '/label',
  3306. '/div',
  3307. '/div'
  3308. ];
  3309. $this->assertHtml($expected, $result);
  3310. // make sure 50 does only disable 50, and not 50f5c0cf
  3311. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3312. $disabled = [50];
  3313. $expected = [
  3314. ['div' => ['class' => 'input select']],
  3315. ['label' => ['for' => 'contact-multiple']],
  3316. 'Multiple',
  3317. '/label',
  3318. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3319. ['div' => ['class' => 'checkbox']],
  3320. ['label' => ['for' => 'contact-multiple-50']],
  3321. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3322. 'Fifty',
  3323. '/label',
  3324. '/div',
  3325. ['div' => ['class' => 'checkbox']],
  3326. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3327. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3328. 'Stringy',
  3329. '/label',
  3330. '/div',
  3331. '/div'
  3332. ];
  3333. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3334. $this->assertHtml($expected, $result);
  3335. }
  3336. /**
  3337. * testControlWithLeadingInteger method
  3338. *
  3339. * Test input name with leading integer, ensure attributes are generated correctly.
  3340. *
  3341. * @return void
  3342. */
  3343. public function testControlWithLeadingInteger()
  3344. {
  3345. $result = $this->Form->text('0.Node.title');
  3346. $expected = [
  3347. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3348. ];
  3349. $this->assertHtml($expected, $result);
  3350. }
  3351. /**
  3352. * testControlSelectType method
  3353. *
  3354. * Test form->control() with select type inputs.
  3355. *
  3356. * @return void
  3357. */
  3358. public function testControlSelectType()
  3359. {
  3360. $result = $this->Form->control(
  3361. 'email',
  3362. [
  3363. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3364. );
  3365. $expected = [
  3366. 'div' => ['class' => 'input select'],
  3367. 'label' => ['for' => 'email'],
  3368. 'Email',
  3369. '/label',
  3370. ['select' => ['name' => 'email', 'id' => 'email']],
  3371. ['option' => ['value' => '']],
  3372. '/option',
  3373. ['option' => ['value' => 'è']],
  3374. 'Firést',
  3375. '/option',
  3376. ['option' => ['value' => 'é']],
  3377. 'Secoènd',
  3378. '/option',
  3379. '/select',
  3380. '/div'
  3381. ];
  3382. $this->assertHtml($expected, $result);
  3383. $result = $this->Form->control(
  3384. 'email',
  3385. [
  3386. 'options' => ['First', 'Second'], 'empty' => true]
  3387. );
  3388. $expected = [
  3389. 'div' => ['class' => 'input select'],
  3390. 'label' => ['for' => 'email'],
  3391. 'Email',
  3392. '/label',
  3393. ['select' => ['name' => 'email', 'id' => 'email']],
  3394. ['option' => ['value' => '']],
  3395. '/option',
  3396. ['option' => ['value' => '0']],
  3397. 'First',
  3398. '/option',
  3399. ['option' => ['value' => '1']],
  3400. 'Second',
  3401. '/option',
  3402. '/select',
  3403. '/div'
  3404. ];
  3405. $this->assertHtml($expected, $result);
  3406. $result = $this->Form->control('email', [
  3407. 'type' => 'select',
  3408. 'options' => new \ArrayObject(['First', 'Second']),
  3409. 'empty' => true
  3410. ]);
  3411. $this->assertHtml($expected, $result);
  3412. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3413. $this->View->setRequest(
  3414. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3415. );
  3416. $this->Form->create();
  3417. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3418. $expected = [
  3419. 'div' => ['class' => 'input select'],
  3420. 'label' => ['for' => 'model-user-id'],
  3421. 'User',
  3422. '/label',
  3423. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3424. ['option' => ['value' => '']],
  3425. '/option',
  3426. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3427. 'good',
  3428. '/option',
  3429. ['option' => ['value' => 'other']],
  3430. 'bad',
  3431. '/option',
  3432. '/select',
  3433. '/div'
  3434. ];
  3435. $this->assertHtml($expected, $result);
  3436. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3437. $this->View->setRequest(
  3438. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3439. );
  3440. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3441. $expected = [
  3442. 'div' => ['class' => 'input select'],
  3443. 'label' => ['for' => 'thing-user-id'],
  3444. 'User',
  3445. '/label',
  3446. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3447. ['option' => ['value' => '']],
  3448. 'Some Empty',
  3449. '/option',
  3450. ['option' => ['value' => 'value']],
  3451. 'good',
  3452. '/option',
  3453. ['option' => ['value' => 'other']],
  3454. 'bad',
  3455. '/option',
  3456. '/select',
  3457. '/div'
  3458. ];
  3459. $this->assertHtml($expected, $result);
  3460. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3461. $this->View->setRequest(
  3462. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3463. );
  3464. $this->Form->create();
  3465. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3466. $expected = [
  3467. 'div' => ['class' => 'input select'],
  3468. 'label' => ['for' => 'thing-user-id'],
  3469. 'User',
  3470. '/label',
  3471. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3472. ['option' => ['value' => '']],
  3473. 'Some Empty',
  3474. '/option',
  3475. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3476. 'good',
  3477. '/option',
  3478. ['option' => ['value' => 'other']],
  3479. 'bad',
  3480. '/option',
  3481. '/select',
  3482. '/div'
  3483. ];
  3484. $this->assertHtml($expected, $result);
  3485. $result = $this->Form->control('Publisher.id', [
  3486. 'label' => 'Publisher',
  3487. 'type' => 'select',
  3488. 'multiple' => 'checkbox',
  3489. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3490. ]);
  3491. $expected = [
  3492. ['div' => ['class' => 'input select']],
  3493. ['label' => ['for' => 'publisher-id']],
  3494. 'Publisher',
  3495. '/label',
  3496. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3497. ['div' => ['class' => 'checkbox']],
  3498. ['label' => ['for' => 'publisher-id-value-1']],
  3499. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3500. 'Label 1',
  3501. '/label',
  3502. '/div',
  3503. ['div' => ['class' => 'checkbox']],
  3504. ['label' => ['for' => 'publisher-id-value-2']],
  3505. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3506. 'Label 2',
  3507. '/label',
  3508. '/div',
  3509. '/div'
  3510. ];
  3511. $this->assertHtml($expected, $result);
  3512. }
  3513. /**
  3514. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3515. *
  3516. * Test that control() and a non standard primary key makes a hidden input by default.
  3517. *
  3518. * @return void
  3519. */
  3520. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3521. {
  3522. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3523. $this->Form->create($this->article);
  3524. $result = $this->Form->control('title');
  3525. $expected = [
  3526. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3527. ];
  3528. $this->assertHtml($expected, $result);
  3529. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3530. $this->Form->create($this->article);
  3531. $result = $this->Form->control('title');
  3532. $expected = [
  3533. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3534. ];
  3535. $this->assertHtml($expected, $result);
  3536. $result = $this->Form->control('body');
  3537. $expected = [
  3538. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3539. ];
  3540. $this->assertHtml($expected, $result);
  3541. }
  3542. /**
  3543. * testControlOverridingMagicSelectType method
  3544. *
  3545. * Test that overriding the magic select type widget is possible.
  3546. *
  3547. * @return void
  3548. */
  3549. public function testControlOverridingMagicSelectType()
  3550. {
  3551. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3552. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3553. $expected = [
  3554. 'div' => ['class' => 'input text'],
  3555. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3556. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3557. '/div'
  3558. ];
  3559. $this->assertHtml($expected, $result);
  3560. //Check that magic types still work for plural/singular vars
  3561. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3562. $result = $this->Form->control('Model.type');
  3563. $expected = [
  3564. 'div' => ['class' => 'input select'],
  3565. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3566. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3567. ['option' => ['value' => 'value']], 'good', '/option',
  3568. ['option' => ['value' => 'other']], 'bad', '/option',
  3569. '/select',
  3570. '/div'
  3571. ];
  3572. $this->assertHtml($expected, $result);
  3573. }
  3574. /**
  3575. * testControlMagicTypeDoesNotOverride method
  3576. *
  3577. * Test that inferred types do not override developer input.
  3578. *
  3579. * @return void
  3580. */
  3581. public function testControlMagicTypeDoesNotOverride()
  3582. {
  3583. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3584. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3585. $expected = [
  3586. 'div' => ['class' => 'input checkbox'],
  3587. ['input' => [
  3588. 'type' => 'hidden',
  3589. 'name' => 'Model[user]',
  3590. 'value' => 0,
  3591. ]],
  3592. 'label' => ['for' => 'model-user'],
  3593. ['input' => [
  3594. 'name' => 'Model[user]',
  3595. 'type' => 'checkbox',
  3596. 'id' => 'model-user',
  3597. 'value' => 1
  3598. ]],
  3599. 'User',
  3600. '/label',
  3601. '/div'
  3602. ];
  3603. $this->assertHtml($expected, $result);
  3604. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3605. $options = [
  3606. 1 => 'blue',
  3607. 2 => 'red'
  3608. ];
  3609. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3610. $expected = [
  3611. 'div' => ['class' => 'input select'],
  3612. 'label' => ['for' => 'tags-ids'],
  3613. 'Tags',
  3614. '/label',
  3615. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3616. ['div' => ['class' => 'checkbox']],
  3617. ['label' => ['for' => 'tags-ids-1']],
  3618. ['input' => [
  3619. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3620. 'value' => '1', 'name' => 'tags[_ids][]'
  3621. ]],
  3622. 'blue',
  3623. '/label',
  3624. '/div',
  3625. ['div' => ['class' => 'checkbox']],
  3626. ['label' => ['for' => 'tags-ids-2']],
  3627. ['input' => [
  3628. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3629. 'value' => '2', 'name' => 'tags[_ids][]'
  3630. ]],
  3631. 'red',
  3632. '/label',
  3633. '/div',
  3634. '/div'
  3635. ];
  3636. $this->assertHtml($expected, $result);
  3637. }
  3638. /**
  3639. * testControlMagicSelectForTypeNumber method
  3640. *
  3641. * Test that magic control() selects are created for type=number.
  3642. *
  3643. * @return void
  3644. */
  3645. public function testControlMagicSelectForTypeNumber()
  3646. {
  3647. $this->getTableLocator()->get('ValidateUsers', [
  3648. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3649. ]);
  3650. $entity = new Entity(['balance' => 1]);
  3651. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3652. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3653. $result = $this->Form->control('balance');
  3654. $expected = [
  3655. 'div' => ['class' => 'input select'],
  3656. 'label' => ['for' => 'balance'],
  3657. 'Balance',
  3658. '/label',
  3659. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3660. ['option' => ['value' => '0']],
  3661. 'nothing',
  3662. '/option',
  3663. ['option' => ['value' => '1', 'selected' => 'selected']],
  3664. 'some',
  3665. '/option',
  3666. ['option' => ['value' => '100']],
  3667. 'a lot',
  3668. '/option',
  3669. '/select',
  3670. '/div'
  3671. ];
  3672. $this->assertHtml($expected, $result);
  3673. }
  3674. /**
  3675. * testInvalidControlTypeOption method
  3676. *
  3677. * Test invalid 'input' type option to control() function.
  3678. *
  3679. * @return void
  3680. */
  3681. public function testInvalidControlTypeOption()
  3682. {
  3683. $this->expectException(\RuntimeException::class);
  3684. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3685. $this->Form->control('text', ['type' => 'input']);
  3686. }
  3687. /**
  3688. * testControlMagicSelectChangeToRadio method
  3689. *
  3690. * Test that magic control() selects can easily be converted into radio types without error.
  3691. *
  3692. * @return void
  3693. */
  3694. public function testControlMagicSelectChangeToRadio()
  3695. {
  3696. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3697. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3698. $this->assertContains('input type="radio"', $result);
  3699. }
  3700. /**
  3701. * testFormControlSubmit method
  3702. *
  3703. * Test correct results for form::control() and type submit.
  3704. *
  3705. * @return void
  3706. */
  3707. public function testFormControlSubmit()
  3708. {
  3709. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3710. $expected = [
  3711. 'div' => ['class' => 'submit'],
  3712. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3713. '/div'
  3714. ];
  3715. $this->assertHtml($expected, $result);
  3716. }
  3717. /**
  3718. * testFormControls method
  3719. *
  3720. * Test correct results from Form::controls().
  3721. *
  3722. * @return void
  3723. */
  3724. public function testFormControlsLegendFieldset()
  3725. {
  3726. $this->Form->create($this->article);
  3727. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3728. $expected = [
  3729. '<fieldset',
  3730. '<legend',
  3731. 'The Legend',
  3732. '/legend',
  3733. '*/fieldset',
  3734. ];
  3735. $this->assertHtml($expected, $result);
  3736. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3737. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3738. $this->assertContains('<fieldset>', $result);
  3739. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3740. $this->assertNotContains('<legend>', $result);
  3741. $this->assertNotContains('<fieldset>', $result);
  3742. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3743. $this->assertNotContains('<legend>', $result);
  3744. $this->assertNotContains('<fieldset>', $result);
  3745. $this->Form->create($this->article);
  3746. $this->View->setRequest($this->View->getRequest()
  3747. ->withParam('prefix', 'admin')
  3748. ->withParam('action', 'admin_edit')
  3749. ->withParam('controller', 'articles'));
  3750. $result = $this->Form->allControls();
  3751. $expected = [
  3752. '<fieldset',
  3753. '<legend',
  3754. 'New Article',
  3755. '/legend',
  3756. '*/fieldset',
  3757. ];
  3758. $this->assertHtml($expected, $result);
  3759. $this->Form->create($this->article);
  3760. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3761. $expected = [
  3762. '<fieldset',
  3763. '<legend',
  3764. 'The Legend',
  3765. '/legend',
  3766. '*/fieldset',
  3767. ];
  3768. $this->assertHtml($expected, $result);
  3769. $this->Form->create($this->article);
  3770. $result = $this->Form->allControls([], [
  3771. 'fieldset' => [
  3772. 'class' => 'some-class some-other-class',
  3773. 'disabled' => true,
  3774. 'data-param' => 'a-param'
  3775. ],
  3776. 'legend' => 'The Legend'
  3777. ]);
  3778. $expected = [
  3779. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3780. '<legend',
  3781. 'The Legend',
  3782. '/legend',
  3783. '*/fieldset',
  3784. ];
  3785. $this->assertHtml($expected, $result);
  3786. }
  3787. /**
  3788. * testFormControls method
  3789. *
  3790. * Test the controls() method.
  3791. *
  3792. * @return void
  3793. */
  3794. public function testFormControls()
  3795. {
  3796. $this->Form->create($this->article);
  3797. $result = $this->Form->allControls();
  3798. $expected = [
  3799. '<fieldset',
  3800. '<legend', 'New Article', '/legend',
  3801. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3802. ['div' => ['class' => 'input select required']],
  3803. '*/div',
  3804. ['div' => ['class' => 'input text required']],
  3805. '*/div',
  3806. ['div' => ['class' => 'input text']],
  3807. '*/div',
  3808. ['div' => ['class' => 'input text']],
  3809. '*/div',
  3810. '/fieldset',
  3811. ];
  3812. $this->assertHtml($expected, $result);
  3813. $result = $this->Form->allControls([
  3814. 'published' => ['type' => 'boolean']
  3815. ]);
  3816. $expected = [
  3817. '<fieldset',
  3818. '<legend', 'New Article', '/legend',
  3819. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3820. ['div' => ['class' => 'input select required']],
  3821. '*/div',
  3822. ['div' => ['class' => 'input text required']],
  3823. '*/div',
  3824. ['div' => ['class' => 'input text']],
  3825. '*/div',
  3826. ['div' => ['class' => 'input boolean']],
  3827. '*/div',
  3828. '/fieldset',
  3829. ];
  3830. $this->assertHtml($expected, $result);
  3831. $this->Form->create($this->article);
  3832. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3833. $expected = [
  3834. 'fieldset' => [],
  3835. 'legend' => [],
  3836. 'Hello',
  3837. '/legend',
  3838. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3839. ['div' => ['class' => 'input select required']],
  3840. '*/div',
  3841. ['div' => ['class' => 'input text required']],
  3842. '*/div',
  3843. ['div' => ['class' => 'input text']],
  3844. '*/div',
  3845. ['div' => ['class' => 'input text']],
  3846. '*/div',
  3847. '/fieldset'
  3848. ];
  3849. $this->assertHtml($expected, $result);
  3850. $this->Form->create(false);
  3851. $expected = [
  3852. 'fieldset' => [],
  3853. ['div' => ['class' => 'input text']],
  3854. 'label' => ['for' => 'foo'],
  3855. 'Foo',
  3856. '/label',
  3857. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3858. '*/div',
  3859. '/fieldset'
  3860. ];
  3861. $result = $this->Form->allControls(
  3862. ['foo' => ['type' => 'text']],
  3863. ['legend' => false]
  3864. );
  3865. $this->assertHtml($expected, $result);
  3866. }
  3867. /**
  3868. * testFormControlsBlacklist method
  3869. *
  3870. * @return void
  3871. */
  3872. public function testFormControlsBlacklist()
  3873. {
  3874. $this->Form->create($this->article);
  3875. $result = $this->Form->allControls([
  3876. 'id' => false
  3877. ]);
  3878. $expected = [
  3879. '<fieldset',
  3880. '<legend', 'New Article', '/legend',
  3881. ['div' => ['class' => 'input select required']],
  3882. '*/div',
  3883. ['div' => ['class' => 'input text required']],
  3884. '*/div',
  3885. ['div' => ['class' => 'input text']],
  3886. '*/div',
  3887. ['div' => ['class' => 'input text']],
  3888. '*/div',
  3889. '/fieldset',
  3890. ];
  3891. $this->assertHtml($expected, $result);
  3892. $this->Form->create($this->article);
  3893. $result = $this->Form->allControls([
  3894. 'id' => []
  3895. ]);
  3896. $expected = [
  3897. '<fieldset',
  3898. '<legend', 'New Article', '/legend',
  3899. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3900. ['div' => ['class' => 'input select required']],
  3901. '*/div',
  3902. ['div' => ['class' => 'input text required']],
  3903. '*/div',
  3904. ['div' => ['class' => 'input text']],
  3905. '*/div',
  3906. ['div' => ['class' => 'input text']],
  3907. '*/div',
  3908. '/fieldset',
  3909. ];
  3910. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3911. }
  3912. /**
  3913. * testSelectAsCheckbox method
  3914. *
  3915. * Test multi-select widget with checkbox formatting.
  3916. *
  3917. * @return void
  3918. */
  3919. public function testSelectAsCheckbox()
  3920. {
  3921. $result = $this->Form->select(
  3922. 'Model.multi_field',
  3923. ['first', 'second', 'third'],
  3924. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3925. );
  3926. $expected = [
  3927. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3928. ['div' => ['class' => 'checkbox']],
  3929. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3930. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3931. 'first',
  3932. '/label',
  3933. '/div',
  3934. ['div' => ['class' => 'checkbox']],
  3935. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3936. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3937. 'second',
  3938. '/label',
  3939. '/div',
  3940. ['div' => ['class' => 'checkbox']],
  3941. ['label' => ['for' => 'model-multi-field-2']],
  3942. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3943. 'third',
  3944. '/label',
  3945. '/div',
  3946. ];
  3947. $this->assertHtml($expected, $result);
  3948. $result = $this->Form->select(
  3949. 'Model.multi_field',
  3950. ['1/2' => 'half'],
  3951. ['multiple' => 'checkbox']
  3952. );
  3953. $expected = [
  3954. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3955. ['div' => ['class' => 'checkbox']],
  3956. ['label' => ['for' => 'model-multi-field-1-2']],
  3957. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3958. 'half',
  3959. '/label',
  3960. '/div',
  3961. ];
  3962. $this->assertHtml($expected, $result);
  3963. }
  3964. /**
  3965. * testLabel method
  3966. *
  3967. * Test label generation.
  3968. *
  3969. * @return void
  3970. */
  3971. public function testLabel()
  3972. {
  3973. $result = $this->Form->label('Person.name');
  3974. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3975. $this->assertHtml($expected, $result);
  3976. $result = $this->Form->label('Person.name');
  3977. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3978. $this->assertHtml($expected, $result);
  3979. $result = $this->Form->label('Person.first_name');
  3980. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3981. $this->assertHtml($expected, $result);
  3982. $result = $this->Form->label('Person.first_name', 'Your first name');
  3983. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3984. $this->assertHtml($expected, $result);
  3985. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3986. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3987. $this->assertHtml($expected, $result);
  3988. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3989. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3990. $this->assertHtml($expected, $result);
  3991. $result = $this->Form->label('Person.first_name', '');
  3992. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3993. $this->assertHtml($expected, $result);
  3994. $result = $this->Form->label('Person.2.name', '');
  3995. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3996. $this->assertHtml($expected, $result);
  3997. }
  3998. /**
  3999. * testLabelContainControl method
  4000. *
  4001. * Test that label() can accept an input with the correct template vars.
  4002. *
  4003. * @return void
  4004. */
  4005. public function testLabelContainControl()
  4006. {
  4007. $this->Form->setTemplates([
  4008. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4009. ]);
  4010. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  4011. 'input' => '<input type="checkbox" name="accept_tos"/>'
  4012. ]);
  4013. $expected = [
  4014. 'label' => ['for' => 'person-accept-terms'],
  4015. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  4016. 'Accept',
  4017. '/label',
  4018. ];
  4019. $this->assertHtml($expected, $result);
  4020. }
  4021. /**
  4022. * testTextbox method
  4023. *
  4024. * Test textbox element generation.
  4025. *
  4026. * @return void
  4027. */
  4028. public function testTextbox()
  4029. {
  4030. $result = $this->Form->text('Model.field');
  4031. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  4032. $this->assertHtml($expected, $result);
  4033. $result = $this->Form->text('Model.field', ['type' => 'password']);
  4034. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  4035. $this->assertHtml($expected, $result);
  4036. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  4037. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  4038. $this->assertHtml($expected, $result);
  4039. }
  4040. /**
  4041. * testTextBoxDataAndError method
  4042. *
  4043. * Test that text() hooks up with request data and error fields.
  4044. *
  4045. * @return void
  4046. */
  4047. public function testTextBoxDataAndError()
  4048. {
  4049. $this->article['errors'] = [
  4050. 'Contact' => ['text' => 'wrong']
  4051. ];
  4052. $this->View->setRequest($this->View->getRequest()
  4053. ->withData('Model.text', 'test <strong>HTML</strong> values')
  4054. ->withData('Contact.text', 'test'));
  4055. $this->Form->create($this->article);
  4056. $result = $this->Form->text('Model.text');
  4057. $expected = [
  4058. 'input' => [
  4059. 'type' => 'text',
  4060. 'name' => 'Model[text]',
  4061. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4062. ]
  4063. ];
  4064. $this->assertHtml($expected, $result);
  4065. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4066. $expected = [
  4067. 'input' => [
  4068. 'type' => 'text',
  4069. 'name' => 'Contact[text]',
  4070. 'value' => 'test',
  4071. 'id' => 'theID',
  4072. 'class' => 'form-error'
  4073. ]
  4074. ];
  4075. $this->assertHtml($expected, $result);
  4076. }
  4077. /**
  4078. * testDefaultValue method
  4079. *
  4080. * Test default value setting.
  4081. *
  4082. * @return void
  4083. */
  4084. public function testTextDefaultValue()
  4085. {
  4086. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  4087. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4088. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4089. $this->assertHtml($expected, $result);
  4090. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4091. $this->Form->create();
  4092. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4093. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4094. $this->assertHtml($expected, $result);
  4095. $Articles = $this->getTableLocator()->get('Articles');
  4096. $title = $Articles->getSchema()->getColumn('title');
  4097. $Articles->getSchema()->addColumn(
  4098. 'title',
  4099. ['default' => 'default title'] + $title
  4100. );
  4101. $entity = $Articles->newEntity();
  4102. $this->Form->create($entity);
  4103. // Get default value from schema
  4104. $result = $this->Form->text('title');
  4105. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4106. $this->assertHtml($expected, $result);
  4107. // Don't get value from schema
  4108. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4109. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4110. $this->assertHtml($expected, $result);
  4111. // Custom default value overrides default value from schema
  4112. $result = $this->Form->text('title', ['default' => 'override default']);
  4113. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4114. $this->assertHtml($expected, $result);
  4115. // Default value from schema is used only for new entities.
  4116. $entity->isNew(false);
  4117. $result = $this->Form->text('title');
  4118. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4119. $this->assertHtml($expected, $result);
  4120. }
  4121. /**
  4122. * testError method
  4123. *
  4124. * Test field error generation.
  4125. *
  4126. * @return void
  4127. */
  4128. public function testError()
  4129. {
  4130. $this->article['errors'] = [
  4131. 'Article' => ['field' => 'email']
  4132. ];
  4133. $this->Form->create($this->article);
  4134. $result = $this->Form->error('Article.field');
  4135. $expected = [
  4136. ['div' => ['class' => 'error-message']],
  4137. 'email',
  4138. '/div',
  4139. ];
  4140. $this->assertHtml($expected, $result);
  4141. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4142. $expected = [
  4143. ['div' => ['class' => 'error-message']],
  4144. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4145. '/div',
  4146. ];
  4147. $this->assertHtml($expected, $result);
  4148. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4149. $expected = [
  4150. ['div' => ['class' => 'error-message']],
  4151. '<strong', 'Badness!', '/strong',
  4152. '/div',
  4153. ];
  4154. $this->assertHtml($expected, $result);
  4155. }
  4156. /**
  4157. * testErrorRuleName method
  4158. *
  4159. * Test error translation can use rule names for translating.
  4160. *
  4161. * @return void
  4162. */
  4163. public function testErrorRuleName()
  4164. {
  4165. $this->article['errors'] = [
  4166. 'Article' => [
  4167. 'field' => ['email' => 'Your email was not good']
  4168. ]
  4169. ];
  4170. $this->Form->create($this->article);
  4171. $result = $this->Form->error('Article.field');
  4172. $expected = [
  4173. ['div' => ['class' => 'error-message']],
  4174. 'Your email was not good',
  4175. '/div',
  4176. ];
  4177. $this->assertHtml($expected, $result);
  4178. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4179. $expected = [
  4180. ['div' => ['class' => 'error-message']],
  4181. 'Email in use',
  4182. '/div',
  4183. ];
  4184. $this->assertHtml($expected, $result);
  4185. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4186. $expected = [
  4187. ['div' => ['class' => 'error-message']],
  4188. 'Email in use',
  4189. '/div',
  4190. ];
  4191. $this->assertHtml($expected, $result);
  4192. $result = $this->Form->error('Article.field', [
  4193. 'email' => 'Key is preferred',
  4194. 'Your email was not good' => 'Email in use'
  4195. ]);
  4196. $expected = [
  4197. ['div' => ['class' => 'error-message']],
  4198. 'Key is preferred',
  4199. '/div',
  4200. ];
  4201. $this->assertHtml($expected, $result);
  4202. }
  4203. /**
  4204. * testErrorMessages method
  4205. *
  4206. * Test error with nested lists.
  4207. *
  4208. * @return void
  4209. */
  4210. public function testErrorMessages()
  4211. {
  4212. $this->article['errors'] = [
  4213. 'Article' => ['field' => 'email']
  4214. ];
  4215. $this->Form->create($this->article);
  4216. $result = $this->Form->error('Article.field', [
  4217. 'email' => 'No good!'
  4218. ]);
  4219. $expected = [
  4220. 'div' => ['class' => 'error-message'],
  4221. 'No good!',
  4222. '/div'
  4223. ];
  4224. $this->assertHtml($expected, $result);
  4225. }
  4226. /**
  4227. * testErrorMultipleMessages method
  4228. *
  4229. * Test error() with multiple messages.
  4230. *
  4231. * @return void
  4232. */
  4233. public function testErrorMultipleMessages()
  4234. {
  4235. $this->article['errors'] = [
  4236. 'field' => ['notBlank', 'email', 'Something else']
  4237. ];
  4238. $this->Form->create($this->article);
  4239. $result = $this->Form->error('field', [
  4240. 'notBlank' => 'Cannot be empty',
  4241. 'email' => 'No good!'
  4242. ]);
  4243. $expected = [
  4244. 'div' => ['class' => 'error-message'],
  4245. 'ul' => [],
  4246. '<li', 'Cannot be empty', '/li',
  4247. '<li', 'No good!', '/li',
  4248. '<li', 'Something else', '/li',
  4249. '/ul',
  4250. '/div'
  4251. ];
  4252. $this->assertHtml($expected, $result);
  4253. }
  4254. /**
  4255. * testPassword method
  4256. *
  4257. * Test password element generation.
  4258. *
  4259. * @return void
  4260. */
  4261. public function testPassword()
  4262. {
  4263. $this->article['errors'] = [
  4264. 'Contact' => [
  4265. 'passwd' => 1
  4266. ]
  4267. ];
  4268. $this->Form->create($this->article);
  4269. $result = $this->Form->password('Contact.field');
  4270. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4271. $this->assertHtml($expected, $result);
  4272. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4273. $this->Form->create($this->article);
  4274. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4275. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4276. $this->assertHtml($expected, $result);
  4277. }
  4278. /**
  4279. * testRadio method
  4280. *
  4281. * Test radio element set generation.
  4282. *
  4283. * @return void
  4284. */
  4285. public function testRadio()
  4286. {
  4287. $result = $this->Form->radio('Model.field', ['option A']);
  4288. $expected = [
  4289. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4290. 'label' => ['for' => 'model-field-0'],
  4291. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4292. 'option A',
  4293. '/label'
  4294. ];
  4295. $this->assertHtml($expected, $result);
  4296. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4297. $this->assertHtml($expected, $result);
  4298. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4299. $expected = [
  4300. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4301. ['label' => ['for' => 'model-field-0']],
  4302. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4303. 'option A',
  4304. '/label',
  4305. ['label' => ['for' => 'model-field-1']],
  4306. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4307. 'option B',
  4308. '/label',
  4309. ];
  4310. $this->assertHtml($expected, $result);
  4311. $result = $this->Form->radio(
  4312. 'Employee.gender',
  4313. ['male' => 'Male', 'female' => 'Female'],
  4314. ['form' => 'my-form']
  4315. );
  4316. $expected = [
  4317. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4318. ['label' => ['for' => 'employee-gender-male']],
  4319. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4320. 'Male',
  4321. '/label',
  4322. ['label' => ['for' => 'employee-gender-female']],
  4323. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4324. 'Female',
  4325. '/label',
  4326. ];
  4327. $this->assertHtml($expected, $result);
  4328. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4329. $expected = [
  4330. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4331. ['label' => ['for' => 'model-custom-0']],
  4332. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4333. 'option A',
  4334. '/label',
  4335. ['label' => ['for' => 'model-custom-1']],
  4336. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4337. 'option B',
  4338. '/label',
  4339. ];
  4340. $this->assertHtml($expected, $result);
  4341. $result = $this->Form->radio(
  4342. 'Employee.gender',
  4343. [
  4344. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4345. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4346. ]
  4347. );
  4348. $expected = [
  4349. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4350. ['label' => ['for' => 'employee-gender-male']],
  4351. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4352. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4353. 'Male',
  4354. '/label',
  4355. ['label' => ['for' => 'employee-gender-female']],
  4356. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4357. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4358. 'Female',
  4359. '/label',
  4360. ];
  4361. $this->assertHtml($expected, $result);
  4362. }
  4363. /**
  4364. * Test radio with complex options and empty disabled data.
  4365. *
  4366. * @return void
  4367. */
  4368. public function testRadioComplexDisabled()
  4369. {
  4370. $options = [
  4371. ['value' => 'r', 'text' => 'red'],
  4372. ['value' => 'b', 'text' => 'blue'],
  4373. ];
  4374. $attrs = ['disabled' => []];
  4375. $result = $this->Form->radio('Model.field', $options, $attrs);
  4376. $expected = [
  4377. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4378. ['label' => ['for' => 'model-field-r']],
  4379. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4380. 'red',
  4381. '/label',
  4382. ['label' => ['for' => 'model-field-b']],
  4383. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4384. 'blue',
  4385. '/label',
  4386. ];
  4387. $this->assertHtml($expected, $result);
  4388. $attrs = ['disabled' => ['r']];
  4389. $result = $this->Form->radio('Model.field', $options, $attrs);
  4390. $this->assertContains('disabled="disabled"', $result);
  4391. }
  4392. /**
  4393. * testRadioDefaultValue method
  4394. *
  4395. * Test default value setting on radio() method.
  4396. *
  4397. * @return void
  4398. */
  4399. public function testRadioDefaultValue()
  4400. {
  4401. $Articles = $this->getTableLocator()->get('Articles');
  4402. $title = $Articles->getSchema()->getColumn('title');
  4403. $Articles->getSchema()->addColumn(
  4404. 'title',
  4405. ['default' => '1'] + $title
  4406. );
  4407. $this->Form->create($Articles->newEntity());
  4408. $result = $this->Form->radio('title', ['option A', 'option B']);
  4409. $expected = [
  4410. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4411. ['label' => ['for' => 'title-0']],
  4412. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4413. 'option A',
  4414. '/label',
  4415. ['label' => ['for' => 'title-1']],
  4416. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4417. 'option B',
  4418. '/label',
  4419. ];
  4420. $this->assertHtml($expected, $result);
  4421. }
  4422. /**
  4423. * Test setting a hiddenField value on radio buttons.
  4424. *
  4425. * @return void
  4426. */
  4427. public function testRadioHiddenFieldValue()
  4428. {
  4429. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4430. $expected = [
  4431. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4432. 'label' => ['for' => 'title-0'],
  4433. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4434. 'option A',
  4435. '/label',
  4436. ];
  4437. $this->assertHtml($expected, $result);
  4438. }
  4439. /**
  4440. * testControlRadio method
  4441. *
  4442. * Test that input works with radio types.
  4443. *
  4444. * @return void
  4445. */
  4446. public function testControlRadio()
  4447. {
  4448. $result = $this->Form->control('test', [
  4449. 'type' => 'radio',
  4450. 'options' => ['A', 'B'],
  4451. ]);
  4452. $expected = [
  4453. ['div' => ['class' => 'input radio']],
  4454. '<label',
  4455. 'Test',
  4456. '/label',
  4457. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4458. ['label' => ['for' => 'test-0']],
  4459. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4460. 'A',
  4461. '/label',
  4462. ['label' => ['for' => 'test-1']],
  4463. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4464. 'B',
  4465. '/label',
  4466. '/div',
  4467. ];
  4468. $this->assertHtml($expected, $result);
  4469. $result = $this->Form->control('test', [
  4470. 'type' => 'radio',
  4471. 'options' => ['A', 'B'],
  4472. 'value' => '0'
  4473. ]);
  4474. $expected = [
  4475. ['div' => ['class' => 'input radio']],
  4476. '<label',
  4477. 'Test',
  4478. '/label',
  4479. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4480. ['label' => ['for' => 'test-0']],
  4481. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4482. 'A',
  4483. '/label',
  4484. ['label' => ['for' => 'test-1']],
  4485. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4486. 'B',
  4487. '/label',
  4488. '/div',
  4489. ];
  4490. $this->assertHtml($expected, $result);
  4491. $result = $this->Form->control('test', [
  4492. 'type' => 'radio',
  4493. 'options' => ['A', 'B'],
  4494. 'label' => false
  4495. ]);
  4496. $expected = [
  4497. ['div' => ['class' => 'input radio']],
  4498. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4499. ['label' => ['for' => 'test-0']],
  4500. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4501. 'A',
  4502. '/label',
  4503. ['label' => ['for' => 'test-1']],
  4504. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4505. 'B',
  4506. '/label',
  4507. '/div',
  4508. ];
  4509. $this->assertHtml($expected, $result);
  4510. }
  4511. /**
  4512. * testRadioNoLabel method
  4513. *
  4514. * Test that radio() works with label = false.
  4515. *
  4516. * @return void
  4517. */
  4518. public function testRadioNoLabel()
  4519. {
  4520. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4521. $expected = [
  4522. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4523. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4524. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4525. ];
  4526. $this->assertHtml($expected, $result);
  4527. }
  4528. /**
  4529. * testRadioControlInsideLabel method
  4530. *
  4531. * Test generating radio input inside label ala twitter bootstrap.
  4532. *
  4533. * @return void
  4534. */
  4535. public function testRadioControlInsideLabel()
  4536. {
  4537. $this->Form->setTemplates([
  4538. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4539. 'radioWrapper' => '{{label}}'
  4540. ]);
  4541. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4542. //@codingStandardsIgnoreStart
  4543. $expected = [
  4544. ['input' => [
  4545. 'type' => 'hidden',
  4546. 'name' => 'Model[field]',
  4547. 'value' => ''
  4548. ]],
  4549. ['label' => ['for' => 'model-field-0']],
  4550. ['input' => [
  4551. 'type' => 'radio',
  4552. 'name' => 'Model[field]',
  4553. 'value' => '0',
  4554. 'id' => 'model-field-0'
  4555. ]],
  4556. 'option A',
  4557. '/label',
  4558. ['label' => ['for' => 'model-field-1']],
  4559. ['input' => [
  4560. 'type' => 'radio',
  4561. 'name' => 'Model[field]',
  4562. 'value' => '1',
  4563. 'id' => 'model-field-1'
  4564. ]],
  4565. 'option B',
  4566. '/label',
  4567. ];
  4568. //@codingStandardsIgnoreEnd
  4569. $this->assertHtml($expected, $result);
  4570. }
  4571. /**
  4572. * testRadioHiddenControlDisabling method
  4573. *
  4574. * Test disabling the hidden input for radio buttons.
  4575. *
  4576. * @return void
  4577. */
  4578. public function testRadioHiddenControlDisabling()
  4579. {
  4580. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4581. $expected = [
  4582. 'label' => ['for' => 'model-1-field-0'],
  4583. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4584. 'option A',
  4585. '/label'
  4586. ];
  4587. $this->assertHtml($expected, $result);
  4588. }
  4589. /**
  4590. * testRadioOutOfRange method
  4591. *
  4592. * Test radio element set generation.
  4593. *
  4594. * @return void
  4595. */
  4596. public function testRadioOutOfRange()
  4597. {
  4598. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4599. $expected = [
  4600. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4601. 'label' => ['for' => 'model-field-v'],
  4602. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4603. 'value',
  4604. '/label'
  4605. ];
  4606. $this->assertHtml($expected, $result);
  4607. }
  4608. /**
  4609. * testSelect method
  4610. *
  4611. * Test select element generation.
  4612. *
  4613. * @return void
  4614. */
  4615. public function testSelect()
  4616. {
  4617. $result = $this->Form->select('Model.field', []);
  4618. $expected = [
  4619. 'select' => ['name' => 'Model[field]'],
  4620. '/select'
  4621. ];
  4622. $this->assertHtml($expected, $result);
  4623. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4624. $this->Form->create();
  4625. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4626. $expected = [
  4627. 'select' => ['name' => 'Model[field]'],
  4628. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4629. 'good',
  4630. '/option',
  4631. ['option' => ['value' => 'other']],
  4632. 'bad',
  4633. '/option',
  4634. '/select'
  4635. ];
  4636. $this->assertHtml($expected, $result);
  4637. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4638. $this->assertHtml($expected, $result);
  4639. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4640. $this->Form->create();
  4641. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4642. $expected = [
  4643. 'select' => ['name' => 'Model[field]'],
  4644. ['option' => ['value' => 'value']],
  4645. 'good',
  4646. '/option',
  4647. ['option' => ['value' => 'other']],
  4648. 'bad',
  4649. '/option',
  4650. '/select'
  4651. ];
  4652. $this->assertHtml($expected, $result);
  4653. $options = [
  4654. ['value' => 'first', 'text' => 'First'],
  4655. ['value' => 'first', 'text' => 'Another First'],
  4656. ];
  4657. $result = $this->Form->select(
  4658. 'Model.field',
  4659. $options,
  4660. ['escape' => false, 'empty' => false]
  4661. );
  4662. $expected = [
  4663. 'select' => ['name' => 'Model[field]'],
  4664. ['option' => ['value' => 'first']],
  4665. 'First',
  4666. '/option',
  4667. ['option' => ['value' => 'first']],
  4668. 'Another First',
  4669. '/option',
  4670. '/select'
  4671. ];
  4672. $this->assertHtml($expected, $result);
  4673. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4674. $this->Form->create();
  4675. $result = $this->Form->select(
  4676. 'Model.contact_id',
  4677. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4678. ['escape' => false, 'empty' => 'pick something']
  4679. );
  4680. $expected = [
  4681. 'select' => ['name' => 'Model[contact_id]'],
  4682. ['option' => ['value' => '']], 'pick something', '/option',
  4683. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4684. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4685. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4686. '/select'
  4687. ];
  4688. $this->assertHtml($expected, $result);
  4689. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4690. $this->Form->create();
  4691. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4692. $expected = [
  4693. 'select' => ['name' => 'Model[field]'],
  4694. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4695. ['option' => ['value' => '1']], 'Yes', '/option',
  4696. '/select'
  4697. ];
  4698. $this->assertHtml($expected, $result);
  4699. }
  4700. /**
  4701. * testSelectEscapeHtml method
  4702. *
  4703. * Test that select() escapes HTML.
  4704. *
  4705. * @return void
  4706. */
  4707. public function testSelectEscapeHtml()
  4708. {
  4709. $result = $this->Form->select(
  4710. 'Model.field',
  4711. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4712. ['empty' => false]
  4713. );
  4714. $expected = [
  4715. 'select' => ['name' => 'Model[field]'],
  4716. ['option' => ['value' => 'first']],
  4717. 'first &quot;html&quot; &lt;chars&gt;',
  4718. '/option',
  4719. ['option' => ['value' => 'second']],
  4720. 'value',
  4721. '/option',
  4722. '/select'
  4723. ];
  4724. $this->assertHtml($expected, $result);
  4725. $result = $this->Form->select(
  4726. 'Model.field',
  4727. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4728. ['escape' => false, 'empty' => false]
  4729. );
  4730. $expected = [
  4731. 'select' => ['name' => 'Model[field]'],
  4732. ['option' => ['value' => 'first']],
  4733. 'first "html" <chars>',
  4734. '/option',
  4735. ['option' => ['value' => 'second']],
  4736. 'value',
  4737. '/option',
  4738. '/select'
  4739. ];
  4740. $this->assertHtml($expected, $result);
  4741. }
  4742. /**
  4743. * testSelectRequired method
  4744. *
  4745. * Test select() with required and disabled attributes.
  4746. *
  4747. * @return void
  4748. */
  4749. public function testSelectRequired()
  4750. {
  4751. $this->article['required'] = [
  4752. 'user_id' => true
  4753. ];
  4754. $this->Form->create($this->article);
  4755. $result = $this->Form->select('user_id', ['option A']);
  4756. $expected = [
  4757. 'select' => [
  4758. 'name' => 'user_id',
  4759. 'required' => 'required'
  4760. ],
  4761. ['option' => ['value' => '0']], 'option A', '/option',
  4762. '/select'
  4763. ];
  4764. $this->assertHtml($expected, $result);
  4765. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4766. $expected = [
  4767. 'select' => [
  4768. 'name' => 'user_id',
  4769. 'disabled' => 'disabled'
  4770. ],
  4771. ['option' => ['value' => '0']], 'option A', '/option',
  4772. '/select'
  4773. ];
  4774. $this->assertHtml($expected, $result);
  4775. }
  4776. /**
  4777. * testNestedSelect method
  4778. *
  4779. * Test select element generation with optgroups.
  4780. *
  4781. * @return void
  4782. */
  4783. public function testNestedSelect()
  4784. {
  4785. $result = $this->Form->select(
  4786. 'Model.field',
  4787. [1 => 'One', 2 => 'Two', 'Three' => [
  4788. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4789. ]],
  4790. ['empty' => false]
  4791. );
  4792. $expected = [
  4793. 'select' => ['name' => 'Model[field]'],
  4794. ['option' => ['value' => 1]],
  4795. 'One',
  4796. '/option',
  4797. ['option' => ['value' => 2]],
  4798. 'Two',
  4799. '/option',
  4800. ['optgroup' => ['label' => 'Three']],
  4801. ['option' => ['value' => 3]],
  4802. 'Three',
  4803. '/option',
  4804. ['option' => ['value' => 4]],
  4805. 'Four',
  4806. '/option',
  4807. ['option' => ['value' => 5]],
  4808. 'Five',
  4809. '/option',
  4810. '/optgroup',
  4811. '/select'
  4812. ];
  4813. $this->assertHtml($expected, $result);
  4814. }
  4815. /**
  4816. * testSelectMultiple method
  4817. *
  4818. * Test generation of multiple select elements.
  4819. *
  4820. * @return void
  4821. */
  4822. public function testSelectMultiple()
  4823. {
  4824. $options = ['first', 'second', 'third'];
  4825. $result = $this->Form->select(
  4826. 'Model.multi_field',
  4827. $options,
  4828. ['form' => 'my-form', 'multiple' => true]
  4829. );
  4830. $expected = [
  4831. 'input' => [
  4832. 'type' => 'hidden',
  4833. 'name' => 'Model[multi_field]',
  4834. 'value' => '',
  4835. 'form' => 'my-form',
  4836. ],
  4837. 'select' => [
  4838. 'name' => 'Model[multi_field][]',
  4839. 'multiple' => 'multiple',
  4840. 'form' => 'my-form',
  4841. ],
  4842. ['option' => ['value' => '0']],
  4843. 'first',
  4844. '/option',
  4845. ['option' => ['value' => '1']],
  4846. 'second',
  4847. '/option',
  4848. ['option' => ['value' => '2']],
  4849. 'third',
  4850. '/option',
  4851. '/select'
  4852. ];
  4853. $this->assertHtml($expected, $result);
  4854. $result = $this->Form->select(
  4855. 'Model.multi_field',
  4856. $options,
  4857. ['multiple' => 'multiple', 'form' => 'my-form']
  4858. );
  4859. $this->assertHtml($expected, $result);
  4860. $result = $this->Form->select(
  4861. 'Model.multi_field',
  4862. $options,
  4863. ['form' => 'my-form', 'multiple' => false]
  4864. );
  4865. $this->assertNotContains('multiple', $result);
  4866. }
  4867. /**
  4868. * testCheckboxZeroValue method
  4869. *
  4870. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4871. *
  4872. * @return void
  4873. */
  4874. public function testCheckboxZeroValue()
  4875. {
  4876. $result = $this->Form->control('User.get_spam', [
  4877. 'type' => 'checkbox',
  4878. 'value' => '0',
  4879. 'hiddenField' => '1',
  4880. ]);
  4881. $expected = [
  4882. 'div' => ['class' => 'input checkbox'],
  4883. 'label' => ['for' => 'user-get-spam'],
  4884. ['input' => [
  4885. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4886. 'value' => '1'
  4887. ]],
  4888. ['input' => [
  4889. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4890. 'value' => '0', 'id' => 'user-get-spam'
  4891. ]],
  4892. 'Get Spam',
  4893. '/label',
  4894. '/div'
  4895. ];
  4896. $this->assertHtml($expected, $result);
  4897. }
  4898. /**
  4899. * testHabtmSelectBox method
  4900. *
  4901. * Test generation of habtm select boxes.
  4902. *
  4903. * @return void
  4904. */
  4905. public function testHabtmSelectBox()
  4906. {
  4907. $this->loadFixtures('Articles');
  4908. $options = [
  4909. 1 => 'blue',
  4910. 2 => 'red',
  4911. 3 => 'green'
  4912. ];
  4913. $tags = [
  4914. new Entity(['id' => 1, 'name' => 'blue']),
  4915. new Entity(['id' => 3, 'name' => 'green'])
  4916. ];
  4917. $article = new Article(['tags' => $tags]);
  4918. $this->Form->create($article);
  4919. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4920. $expected = [
  4921. 'div' => ['class' => 'input select'],
  4922. 'label' => ['for' => 'tags-ids'],
  4923. 'Tags',
  4924. '/label',
  4925. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4926. 'select' => [
  4927. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4928. 'multiple' => 'multiple'
  4929. ],
  4930. ['option' => ['value' => '1', 'selected' => 'selected']],
  4931. 'blue',
  4932. '/option',
  4933. ['option' => ['value' => '2']],
  4934. 'red',
  4935. '/option',
  4936. ['option' => ['value' => '3', 'selected' => 'selected']],
  4937. 'green',
  4938. '/option',
  4939. '/select',
  4940. '/div'
  4941. ];
  4942. $this->assertHtml($expected, $result);
  4943. // make sure only 50 is selected, and not 50f5c0cf
  4944. $options = [
  4945. '1' => 'blue',
  4946. '50f5c0cf' => 'red',
  4947. '50' => 'green'
  4948. ];
  4949. $tags = [
  4950. new Entity(['id' => 1, 'name' => 'blue']),
  4951. new Entity(['id' => 50, 'name' => 'green'])
  4952. ];
  4953. $article = new Article(['tags' => $tags]);
  4954. $this->Form->create($article);
  4955. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4956. $expected = [
  4957. 'div' => ['class' => 'input select'],
  4958. 'label' => ['for' => 'tags-ids'],
  4959. 'Tags',
  4960. '/label',
  4961. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4962. 'select' => [
  4963. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4964. 'multiple' => 'multiple'
  4965. ],
  4966. ['option' => ['value' => '1', 'selected' => 'selected']],
  4967. 'blue',
  4968. '/option',
  4969. ['option' => ['value' => '50f5c0cf']],
  4970. 'red',
  4971. '/option',
  4972. ['option' => ['value' => '50', 'selected' => 'selected']],
  4973. 'green',
  4974. '/option',
  4975. '/select',
  4976. '/div'
  4977. ];
  4978. $this->assertHtml($expected, $result);
  4979. $spacecraft = [
  4980. 1 => 'Orion',
  4981. 2 => 'Helios'
  4982. ];
  4983. $this->View->viewVars['spacecraft'] = $spacecraft;
  4984. $this->Form->create();
  4985. $result = $this->Form->control('spacecraft._ids');
  4986. $expected = [
  4987. 'div' => ['class' => 'input select'],
  4988. 'label' => ['for' => 'spacecraft-ids'],
  4989. 'Spacecraft',
  4990. '/label',
  4991. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4992. 'select' => [
  4993. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4994. 'multiple' => 'multiple'
  4995. ],
  4996. ['option' => ['value' => '1']],
  4997. 'Orion',
  4998. '/option',
  4999. ['option' => ['value' => '2']],
  5000. 'Helios',
  5001. '/option',
  5002. '/select',
  5003. '/div'
  5004. ];
  5005. $this->assertHtml($expected, $result);
  5006. }
  5007. /**
  5008. * testErrorsForBelongsToManySelect method
  5009. *
  5010. * Tests that errors for belongsToMany select fields are being
  5011. * picked up properly.
  5012. *
  5013. * @return void
  5014. */
  5015. public function testErrorsForBelongsToManySelect()
  5016. {
  5017. $spacecraft = [
  5018. 1 => 'Orion',
  5019. 2 => 'Helios'
  5020. ];
  5021. $this->View->viewVars['spacecraft'] = $spacecraft;
  5022. $article = new Article();
  5023. $article->setError('spacecraft', ['Invalid']);
  5024. $this->Form->create($article);
  5025. $result = $this->Form->control('spacecraft._ids');
  5026. $expected = [
  5027. ['div' => ['class' => 'input select error']],
  5028. 'label' => ['for' => 'spacecraft-ids'],
  5029. 'Spacecraft',
  5030. '/label',
  5031. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  5032. 'select' => [
  5033. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  5034. 'multiple' => 'multiple'
  5035. ],
  5036. ['option' => ['value' => '1']],
  5037. 'Orion',
  5038. '/option',
  5039. ['option' => ['value' => '2']],
  5040. 'Helios',
  5041. '/option',
  5042. '/select',
  5043. ['div' => ['class' => 'error-message']],
  5044. 'Invalid',
  5045. '/div',
  5046. '/div'
  5047. ];
  5048. $this->assertHtml($expected, $result);
  5049. }
  5050. /**
  5051. * testSelectMultipleCheckboxes method
  5052. *
  5053. * Test generation of multi select elements in checkbox format.
  5054. *
  5055. * @return void
  5056. */
  5057. public function testSelectMultipleCheckboxes()
  5058. {
  5059. $result = $this->Form->select(
  5060. 'Model.multi_field',
  5061. ['first', 'second', 'third'],
  5062. ['multiple' => 'checkbox']
  5063. );
  5064. $expected = [
  5065. 'input' => [
  5066. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5067. ],
  5068. ['div' => ['class' => 'checkbox']],
  5069. ['label' => ['for' => 'model-multi-field-0']],
  5070. ['input' => [
  5071. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5072. 'value' => '0', 'id' => 'model-multi-field-0'
  5073. ]],
  5074. 'first',
  5075. '/label',
  5076. '/div',
  5077. ['div' => ['class' => 'checkbox']],
  5078. ['label' => ['for' => 'model-multi-field-1']],
  5079. ['input' => [
  5080. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5081. 'value' => '1', 'id' => 'model-multi-field-1'
  5082. ]],
  5083. 'second',
  5084. '/label',
  5085. '/div',
  5086. ['div' => ['class' => 'checkbox']],
  5087. ['label' => ['for' => 'model-multi-field-2']],
  5088. ['input' => [
  5089. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5090. 'value' => '2', 'id' => 'model-multi-field-2'
  5091. ]],
  5092. 'third',
  5093. '/label',
  5094. '/div'
  5095. ];
  5096. $this->assertHtml($expected, $result);
  5097. $result = $this->Form->select(
  5098. 'Model.multi_field',
  5099. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5100. ['multiple' => 'checkbox']
  5101. );
  5102. $expected = [
  5103. 'input' => [
  5104. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5105. ],
  5106. ['div' => ['class' => 'checkbox']],
  5107. ['label' => ['for' => 'model-multi-field-a+']],
  5108. ['input' => [
  5109. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5110. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  5111. ]],
  5112. 'first',
  5113. '/label',
  5114. '/div',
  5115. ['div' => ['class' => 'checkbox']],
  5116. ['label' => ['for' => 'model-multi-field-a++']],
  5117. ['input' => [
  5118. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5119. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  5120. ]],
  5121. 'second',
  5122. '/label',
  5123. '/div',
  5124. ['div' => ['class' => 'checkbox']],
  5125. ['label' => ['for' => 'model-multi-field-a+++']],
  5126. ['input' => [
  5127. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5128. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  5129. ]],
  5130. 'third',
  5131. '/label',
  5132. '/div'
  5133. ];
  5134. $this->assertHtml($expected, $result);
  5135. $result = $this->Form->select(
  5136. 'Model.multi_field',
  5137. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5138. ['multiple' => 'checkbox']
  5139. );
  5140. $expected = [
  5141. 'input' => [
  5142. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5143. ],
  5144. ['div' => ['class' => 'checkbox']],
  5145. ['label' => ['for' => 'model-multi-field-a-b']],
  5146. ['input' => [
  5147. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5148. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5149. ]],
  5150. 'first',
  5151. '/label',
  5152. '/div',
  5153. ['div' => ['class' => 'checkbox']],
  5154. ['label' => ['for' => 'model-multi-field-a-b1']],
  5155. ['input' => [
  5156. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5157. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5158. ]],
  5159. 'second',
  5160. '/label',
  5161. '/div',
  5162. ['div' => ['class' => 'checkbox']],
  5163. ['label' => ['for' => 'model-multi-field-a-b2']],
  5164. ['input' => [
  5165. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5166. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5167. ]],
  5168. 'third',
  5169. '/label',
  5170. '/div'
  5171. ];
  5172. $this->assertHtml($expected, $result);
  5173. }
  5174. /**
  5175. * testSelectMultipleCheckboxRequestData method
  5176. *
  5177. * Ensure that multiCheckbox reads from the request data.
  5178. *
  5179. * @return void
  5180. */
  5181. public function testSelectMultipleCheckboxRequestData()
  5182. {
  5183. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5184. $result = $this->Form->select(
  5185. 'Model.tags',
  5186. ['1' => 'first', 'Array' => 'Array'],
  5187. ['multiple' => 'checkbox']
  5188. );
  5189. $expected = [
  5190. 'input' => [
  5191. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5192. ],
  5193. ['div' => ['class' => 'checkbox']],
  5194. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5195. ['input' => [
  5196. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5197. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5198. ]],
  5199. 'first',
  5200. '/label',
  5201. '/div',
  5202. ['div' => ['class' => 'checkbox']],
  5203. ['label' => ['for' => 'model-tags-array']],
  5204. ['input' => [
  5205. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5206. 'value' => 'Array', 'id' => 'model-tags-array'
  5207. ]],
  5208. 'Array',
  5209. '/label',
  5210. '/div'
  5211. ];
  5212. $this->assertHtml($expected, $result);
  5213. }
  5214. /**
  5215. * testSelectMultipleCheckboxSecurity method
  5216. *
  5217. * Checks the security hash array generated for multiple-input checkbox elements.
  5218. *
  5219. * @return void
  5220. */
  5221. public function testSelectMultipleCheckboxSecurity()
  5222. {
  5223. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  5224. $this->assertEquals([], $this->Form->fields);
  5225. $this->Form->select(
  5226. 'Model.multi_field',
  5227. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5228. ['multiple' => 'checkbox']
  5229. );
  5230. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5231. $result = $this->Form->secure($this->Form->fields);
  5232. $key = '8af36fb34e6f2ef8ba0eb473bb4365ec232f3fe5%3A';
  5233. $this->assertContains('"' . $key . '"', $result);
  5234. }
  5235. /**
  5236. * testSelectMultipleSecureWithNoOptions method
  5237. *
  5238. * Multiple select elements should always be secured as they always participate
  5239. * in the POST data.
  5240. *
  5241. * @return void
  5242. */
  5243. public function testSelectMultipleSecureWithNoOptions()
  5244. {
  5245. $this->assertEquals([], $this->Form->fields);
  5246. $this->Form->select(
  5247. 'Model.select',
  5248. [],
  5249. ['multiple' => true]
  5250. );
  5251. $this->assertEquals(['Model.select'], $this->Form->fields);
  5252. }
  5253. /**
  5254. * testSelectNoSecureWithNoOptions method
  5255. *
  5256. * When a select box has no options it should not be added to the fields list
  5257. * as it always fail post validation.
  5258. *
  5259. * @return void
  5260. */
  5261. public function testSelectNoSecureWithNoOptions()
  5262. {
  5263. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testkey'));
  5264. $this->assertEquals([], $this->Form->fields);
  5265. $this->Form->select(
  5266. 'Model.select',
  5267. []
  5268. );
  5269. $this->assertEquals([], $this->Form->fields);
  5270. $this->Form->select(
  5271. 'Model.user_id',
  5272. [],
  5273. ['empty' => true]
  5274. );
  5275. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5276. }
  5277. /**
  5278. * testControlMultipleCheckboxes method
  5279. *
  5280. * Test control() resulting in multi select elements being generated.
  5281. *
  5282. * @return void
  5283. */
  5284. public function testControlMultipleCheckboxes()
  5285. {
  5286. $result = $this->Form->control('Model.multi_field', [
  5287. 'options' => ['first', 'second', 'third'],
  5288. 'multiple' => 'checkbox'
  5289. ]);
  5290. $expected = [
  5291. ['div' => ['class' => 'input select']],
  5292. ['label' => ['for' => 'model-multi-field']],
  5293. 'Multi Field',
  5294. '/label',
  5295. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5296. ['div' => ['class' => 'checkbox']],
  5297. ['label' => ['for' => 'model-multi-field-0']],
  5298. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5299. 'first',
  5300. '/label',
  5301. '/div',
  5302. ['div' => ['class' => 'checkbox']],
  5303. ['label' => ['for' => 'model-multi-field-1']],
  5304. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5305. 'second',
  5306. '/label',
  5307. '/div',
  5308. ['div' => ['class' => 'checkbox']],
  5309. ['label' => ['for' => 'model-multi-field-2']],
  5310. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5311. 'third',
  5312. '/label',
  5313. '/div',
  5314. '/div'
  5315. ];
  5316. $this->assertHtml($expected, $result);
  5317. $result = $this->Form->control('Model.multi_field', [
  5318. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5319. 'multiple' => 'checkbox'
  5320. ]);
  5321. $expected = [
  5322. ['div' => ['class' => 'input select']],
  5323. ['label' => ['for' => 'model-multi-field']],
  5324. 'Multi Field',
  5325. '/label',
  5326. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5327. ['div' => ['class' => 'checkbox']],
  5328. ['label' => ['for' => 'model-multi-field-a']],
  5329. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5330. 'first',
  5331. '/label',
  5332. '/div',
  5333. ['div' => ['class' => 'checkbox']],
  5334. ['label' => ['for' => 'model-multi-field-b']],
  5335. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5336. 'second',
  5337. '/label',
  5338. '/div',
  5339. ['div' => ['class' => 'checkbox']],
  5340. ['label' => ['for' => 'model-multi-field-c']],
  5341. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5342. 'third',
  5343. '/label',
  5344. '/div',
  5345. '/div'
  5346. ];
  5347. $this->assertHtml($expected, $result);
  5348. }
  5349. /**
  5350. * testSelectHiddenFieldOmission method
  5351. *
  5352. * Test that select() with 'hiddenField' => false omits the hidden field.
  5353. *
  5354. * @return void
  5355. */
  5356. public function testSelectHiddenFieldOmission()
  5357. {
  5358. $result = $this->Form->select(
  5359. 'Model.multi_field',
  5360. ['first', 'second'],
  5361. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5362. );
  5363. $this->assertNotContains('type="hidden"', $result);
  5364. }
  5365. /**
  5366. * testSelectCheckboxMultipleOverrideName method
  5367. *
  5368. * Test that select() with multiple = checkbox works with overriding name attribute.
  5369. *
  5370. * @return void
  5371. */
  5372. public function testSelectCheckboxMultipleOverrideName()
  5373. {
  5374. $result = $this->Form->select('category', ['1', '2'], [
  5375. 'multiple' => 'checkbox',
  5376. 'name' => 'fish',
  5377. ]);
  5378. $expected = [
  5379. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5380. ['div' => ['class' => 'checkbox']],
  5381. ['label' => ['for' => 'fish-0']],
  5382. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5383. '1',
  5384. '/label',
  5385. '/div',
  5386. ['div' => ['class' => 'checkbox']],
  5387. ['label' => ['for' => 'fish-1']],
  5388. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5389. '2',
  5390. '/label',
  5391. '/div'
  5392. ];
  5393. $this->assertHtml($expected, $result);
  5394. $result = $this->Form->multiCheckbox(
  5395. 'category',
  5396. new Collection(['1', '2']),
  5397. ['name' => 'fish']
  5398. );
  5399. $this->assertHtml($expected, $result);
  5400. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5401. 'name' => 'fish',
  5402. ]);
  5403. $this->assertHtml($expected, $result);
  5404. }
  5405. /**
  5406. * testControlMultiCheckbox method
  5407. *
  5408. * Test that control() works with multicheckbox.
  5409. *
  5410. * @return void
  5411. */
  5412. public function testControlMultiCheckbox()
  5413. {
  5414. $result = $this->Form->control('category', [
  5415. 'type' => 'multicheckbox',
  5416. 'options' => ['1', '2'],
  5417. ]);
  5418. $expected = [
  5419. ['div' => ['class' => 'input multicheckbox']],
  5420. '<label',
  5421. 'Category',
  5422. '/label',
  5423. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5424. ['div' => ['class' => 'checkbox']],
  5425. ['label' => ['for' => 'category-0']],
  5426. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5427. '1',
  5428. '/label',
  5429. '/div',
  5430. ['div' => ['class' => 'checkbox']],
  5431. ['label' => ['for' => 'category-1']],
  5432. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5433. '2',
  5434. '/label',
  5435. '/div',
  5436. '/div',
  5437. ];
  5438. $this->assertHtml($expected, $result);
  5439. }
  5440. /**
  5441. * testCheckbox method
  5442. *
  5443. * Test generation of checkboxes.
  5444. *
  5445. * @return void
  5446. */
  5447. public function testCheckbox()
  5448. {
  5449. $result = $this->Form->checkbox('Model.field');
  5450. $expected = [
  5451. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5452. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5453. ];
  5454. $this->assertHtml($expected, $result);
  5455. $result = $this->Form->checkbox('Model.field', [
  5456. 'id' => 'theID',
  5457. 'value' => 'myvalue',
  5458. 'form' => 'my-form',
  5459. ]);
  5460. $expected = [
  5461. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5462. ['input' => [
  5463. 'type' => 'checkbox', 'name' => 'Model[field]',
  5464. 'value' => 'myvalue', 'id' => 'theID',
  5465. 'form' => 'my-form',
  5466. ]]
  5467. ];
  5468. $this->assertHtml($expected, $result);
  5469. }
  5470. /**
  5471. * testCheckboxDefaultValue method
  5472. *
  5473. * Test default value setting on checkbox() method.
  5474. *
  5475. * @return void
  5476. */
  5477. public function testCheckboxDefaultValue()
  5478. {
  5479. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5480. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5481. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5482. $this->assertHtml($expected, $result);
  5483. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5484. $this->Form->create();
  5485. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5486. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5487. $this->assertHtml($expected, $result);
  5488. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5489. $this->Form->create();
  5490. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5491. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5492. $this->assertHtml($expected, $result);
  5493. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5494. $this->Form->create();
  5495. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5496. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5497. $this->assertHtml($expected, $result);
  5498. $Articles = $this->getTableLocator()->get('Articles');
  5499. $Articles->getSchema()->addColumn(
  5500. 'published',
  5501. ['type' => 'boolean', 'null' => false, 'default' => true]
  5502. );
  5503. $this->Form->create($Articles->newEntity());
  5504. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5505. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5506. $this->assertHtml($expected, $result);
  5507. }
  5508. /**
  5509. * testCheckboxCheckedAndError method
  5510. *
  5511. * Test checkbox being checked or having errors.
  5512. *
  5513. * @return void
  5514. */
  5515. public function testCheckboxCheckedAndError()
  5516. {
  5517. $this->article['errors'] = [
  5518. 'published' => true
  5519. ];
  5520. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5521. $this->Form->create($this->article);
  5522. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5523. $expected = [
  5524. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5525. ['input' => [
  5526. 'type' => 'checkbox',
  5527. 'name' => 'published',
  5528. 'value' => 'myvalue',
  5529. 'id' => 'theID',
  5530. 'checked' => 'checked',
  5531. 'class' => 'form-error'
  5532. ]]
  5533. ];
  5534. $this->assertHtml($expected, $result);
  5535. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5536. $this->Form->create($this->article);
  5537. $result = $this->Form->checkbox('published');
  5538. $expected = [
  5539. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5540. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5541. ];
  5542. $this->assertHtml($expected, $result);
  5543. }
  5544. /**
  5545. * testCheckboxCustomNameAttribute method
  5546. *
  5547. * Test checkbox() with a custom name attribute.
  5548. *
  5549. * @return void
  5550. */
  5551. public function testCheckboxCustomNameAttribute()
  5552. {
  5553. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5554. $expected = [
  5555. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5556. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5557. ];
  5558. $this->assertHtml($expected, $result);
  5559. }
  5560. /**
  5561. * testCheckboxHiddenField method
  5562. *
  5563. * Test that the hidden input for checkboxes can be omitted or set to a
  5564. * specific value.
  5565. *
  5566. * @return void
  5567. */
  5568. public function testCheckboxHiddenField()
  5569. {
  5570. $result = $this->Form->checkbox('UserForm.something', [
  5571. 'hiddenField' => false
  5572. ]);
  5573. $expected = [
  5574. 'input' => [
  5575. 'type' => 'checkbox',
  5576. 'name' => 'UserForm[something]',
  5577. 'value' => '1'
  5578. ],
  5579. ];
  5580. $this->assertHtml($expected, $result);
  5581. $result = $this->Form->checkbox('UserForm.something', [
  5582. 'value' => 'Y',
  5583. 'hiddenField' => 'N',
  5584. ]);
  5585. $expected = [
  5586. ['input' => [
  5587. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5588. 'value' => 'N'
  5589. ]],
  5590. ['input' => [
  5591. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5592. 'value' => 'Y'
  5593. ]],
  5594. ];
  5595. $this->assertHtml($expected, $result);
  5596. }
  5597. /**
  5598. * testTime method
  5599. *
  5600. * Test the time type.
  5601. *
  5602. * @return void
  5603. */
  5604. public function testTime()
  5605. {
  5606. $result = $this->Form->time('start_time', [
  5607. 'timeFormat' => 12,
  5608. 'interval' => 5,
  5609. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5610. ]);
  5611. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5612. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5613. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5614. $this->assertNotContains('year', $result);
  5615. $this->assertNotContains('month', $result);
  5616. $this->assertNotContains('day', $result);
  5617. $result = $this->Form->time('start_time', [
  5618. 'timeFormat' => 12,
  5619. 'interval' => 5,
  5620. 'value' => '2014-03-08 16:30:00'
  5621. ]);
  5622. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5623. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5624. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5625. $this->assertNotContains('year', $result);
  5626. $this->assertNotContains('month', $result);
  5627. $this->assertNotContains('day', $result);
  5628. }
  5629. /**
  5630. * testTimeFormat24NoMeridian method
  5631. *
  5632. * Ensure that timeFormat=24 has no merdian.
  5633. *
  5634. * @return void
  5635. */
  5636. public function testTimeFormat24NoMeridian()
  5637. {
  5638. $result = $this->Form->time('start_time', [
  5639. 'timeFormat' => 24,
  5640. 'interval' => 5,
  5641. 'value' => '2014-03-08 16:30:00'
  5642. ]);
  5643. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5644. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5645. $this->assertNotContains('meridian', $result);
  5646. $this->assertNotContains('pm', $result);
  5647. $this->assertNotContains('year', $result);
  5648. $this->assertNotContains('month', $result);
  5649. $this->assertNotContains('day', $result);
  5650. }
  5651. /**
  5652. * testDate method
  5653. *
  5654. * Test the date type.
  5655. *
  5656. * @return void
  5657. */
  5658. public function testDate()
  5659. {
  5660. $result = $this->Form->date('start_day', [
  5661. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5662. ]);
  5663. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5664. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5665. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5666. $this->assertNotContains('hour', $result);
  5667. $this->assertNotContains('minute', $result);
  5668. $this->assertNotContains('second', $result);
  5669. $this->assertNotContains('meridian', $result);
  5670. }
  5671. /**
  5672. * testDateTime method
  5673. *
  5674. * Test generation of date/time select elements.
  5675. *
  5676. * @return void
  5677. */
  5678. public function testDateTime()
  5679. {
  5680. extract($this->dateRegex);
  5681. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5682. $now = strtotime('now');
  5683. $expected = [
  5684. ['select' => ['name' => 'Contact[date][year]']],
  5685. ['option' => ['value' => '']],
  5686. '/option',
  5687. $yearsRegex,
  5688. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5689. date('Y', $now),
  5690. '/option',
  5691. '*/select',
  5692. ['select' => ['name' => 'Contact[date][month]']],
  5693. ['option' => ['value' => '']],
  5694. '/option',
  5695. $monthsRegex,
  5696. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5697. date('F', $now),
  5698. '/option',
  5699. '*/select',
  5700. ['select' => ['name' => 'Contact[date][day]']],
  5701. ['option' => ['value' => '']],
  5702. '/option',
  5703. $daysRegex,
  5704. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5705. date('j', $now),
  5706. '/option',
  5707. '*/select',
  5708. ['select' => ['name' => 'Contact[date][hour]']],
  5709. ['option' => ['value' => '']],
  5710. '/option',
  5711. $hoursRegex,
  5712. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5713. date('G', $now),
  5714. '/option',
  5715. '*/select',
  5716. ['select' => ['name' => 'Contact[date][minute]']],
  5717. ['option' => ['value' => '']],
  5718. '/option',
  5719. $minutesRegex,
  5720. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5721. date('i', $now),
  5722. '/option',
  5723. '*/select',
  5724. ];
  5725. $this->assertHtml($expected, $result);
  5726. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5727. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5728. $now = strtotime('now');
  5729. $expected = [
  5730. ['select' => ['name' => 'Contact[date][year]']],
  5731. $yearsRegex,
  5732. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5733. date('Y', $now),
  5734. '/option',
  5735. '*/select',
  5736. ['select' => ['name' => 'Contact[date][month]']],
  5737. $monthsRegex,
  5738. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5739. date('F', $now),
  5740. '/option',
  5741. '*/select',
  5742. ['select' => ['name' => 'Contact[date][day]']],
  5743. $daysRegex,
  5744. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5745. date('j', $now),
  5746. '/option',
  5747. '*/select',
  5748. ['select' => ['name' => 'Contact[date][hour]']],
  5749. $hoursRegex,
  5750. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5751. date('G', $now),
  5752. '/option',
  5753. '*/select',
  5754. ['select' => ['name' => 'Contact[date][minute]']],
  5755. $minutesRegex,
  5756. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5757. date('i', $now),
  5758. '/option',
  5759. '*/select',
  5760. ];
  5761. $this->assertHtml($expected, $result);
  5762. }
  5763. /**
  5764. * testDateTimeSecured method
  5765. *
  5766. * Test that datetime fields are added to protected fields list.
  5767. *
  5768. * @return void
  5769. */
  5770. public function testDateTimeSecured()
  5771. {
  5772. $this->View->setRequest(
  5773. $this->View->getRequest()->withParam('_Token', ['unlockedFields' => []])
  5774. );
  5775. $this->Form->dateTime('Contact.date');
  5776. $expected = [
  5777. 'Contact.date.year',
  5778. 'Contact.date.month',
  5779. 'Contact.date.day',
  5780. 'Contact.date.hour',
  5781. 'Contact.date.minute',
  5782. ];
  5783. $this->assertEquals($expected, $this->Form->fields);
  5784. $this->Form->fields = [];
  5785. $this->Form->date('Contact.published');
  5786. $expected = [
  5787. 'Contact.published.year',
  5788. 'Contact.published.month',
  5789. 'Contact.published.day',
  5790. ];
  5791. $this->assertEquals($expected, $this->Form->fields);
  5792. }
  5793. /**
  5794. * testDateTimeSecuredDisabled method
  5795. *
  5796. * Test that datetime fields are added to protected fields list.
  5797. *
  5798. * @return void
  5799. */
  5800. public function testDateTimeSecuredDisabled()
  5801. {
  5802. $this->View->setRequest(
  5803. $this->View->getRequest()->withParam('_Token', ['unlockedFields' => []])
  5804. );
  5805. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5806. $expected = [];
  5807. $this->assertEquals($expected, $this->Form->fields);
  5808. $this->Form->fields = [];
  5809. $this->Form->date('Contact.published', ['secure' => false]);
  5810. $expected = [];
  5811. $this->assertEquals($expected, $this->Form->fields);
  5812. }
  5813. /**
  5814. * testDatetimeEmpty method
  5815. *
  5816. * Test empty defaulting to true for datetime.
  5817. *
  5818. * @return void
  5819. */
  5820. public function testDatetimeEmpty()
  5821. {
  5822. extract($this->dateRegex);
  5823. $result = $this->Form->dateTime('Contact.date', [
  5824. 'timeFormat' => 12,
  5825. 'empty' => true,
  5826. 'default' => true
  5827. ]);
  5828. $expected = [
  5829. ['select' => ['name' => 'Contact[date][year]']],
  5830. $yearsRegex,
  5831. ['option' => ['value' => '']],
  5832. '/option',
  5833. '*/select',
  5834. ['select' => ['name' => 'Contact[date][month]']],
  5835. $monthsRegex,
  5836. ['option' => ['value' => '']],
  5837. '/option',
  5838. '*/select',
  5839. ['select' => ['name' => 'Contact[date][day]']],
  5840. $daysRegex,
  5841. ['option' => ['value' => '']],
  5842. '/option',
  5843. '*/select',
  5844. ['select' => ['name' => 'Contact[date][hour]']],
  5845. $hoursRegex,
  5846. ['option' => ['value' => '']],
  5847. '/option',
  5848. '*/select',
  5849. ['select' => ['name' => 'Contact[date][minute]']],
  5850. $minutesRegex,
  5851. ['option' => ['value' => '']],
  5852. '/option',
  5853. '*/select',
  5854. ['select' => ['name' => 'Contact[date][meridian]']],
  5855. $meridianRegex,
  5856. ['option' => ['value' => '']],
  5857. '/option',
  5858. '*/select'
  5859. ];
  5860. $this->assertHtml($expected, $result);
  5861. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5862. }
  5863. /**
  5864. * testDatetimeMinuteInterval method
  5865. *
  5866. * Test datetime with interval option.
  5867. *
  5868. * @return void
  5869. */
  5870. public function testDatetimeMinuteInterval()
  5871. {
  5872. extract($this->dateRegex);
  5873. $result = $this->Form->dateTime('Contact.date', [
  5874. 'interval' => 5,
  5875. 'value' => ''
  5876. ]);
  5877. $expected = [
  5878. ['select' => ['name' => 'Contact[date][year]']],
  5879. $yearsRegex,
  5880. ['option' => ['selected' => 'selected', 'value' => '']],
  5881. '/option',
  5882. '*/select',
  5883. ['select' => ['name' => 'Contact[date][month]']],
  5884. $monthsRegex,
  5885. ['option' => ['selected' => 'selected', 'value' => '']],
  5886. '/option',
  5887. '*/select',
  5888. ['select' => ['name' => 'Contact[date][day]']],
  5889. $daysRegex,
  5890. ['option' => ['selected' => 'selected', 'value' => '']],
  5891. '/option',
  5892. '*/select',
  5893. ['select' => ['name' => 'Contact[date][hour]']],
  5894. $hoursRegex,
  5895. ['option' => ['selected' => 'selected', 'value' => '']],
  5896. '/option',
  5897. '*/select',
  5898. ['select' => ['name' => 'Contact[date][minute]']],
  5899. $minutesRegex,
  5900. ['option' => ['selected' => 'selected', 'value' => '']],
  5901. '/option',
  5902. ['option' => ['value' => '00']],
  5903. '00',
  5904. '/option',
  5905. ['option' => ['value' => '05']],
  5906. '05',
  5907. '/option',
  5908. ['option' => ['value' => '10']],
  5909. '10',
  5910. '/option',
  5911. '*/select',
  5912. ];
  5913. $this->assertHtml($expected, $result);
  5914. }
  5915. /**
  5916. * testDateTimeRounding method
  5917. *
  5918. * Test dateTime with rounding.
  5919. *
  5920. * @return void
  5921. */
  5922. public function testDateTimeRounding()
  5923. {
  5924. $this->View->setRequest($this->View->getRequest()->withData('Contact', [
  5925. 'date' => [
  5926. 'day' => '13',
  5927. 'month' => '12',
  5928. 'year' => '2010',
  5929. 'hour' => '04',
  5930. 'minute' => '19',
  5931. 'meridian' => 'AM'
  5932. ]
  5933. ]));
  5934. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5935. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5936. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5937. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5938. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5939. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5940. }
  5941. /**
  5942. * testDatetimeWithDefault method
  5943. *
  5944. * Test that datetime() and default values work.
  5945. *
  5946. * @return void
  5947. */
  5948. public function testDatetimeWithDefault()
  5949. {
  5950. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5951. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5952. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5953. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5954. $result = $this->Form->dateTime('Contact.updated', [
  5955. 'default' => '2009-06-01 11:15:30'
  5956. ]);
  5957. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5958. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5959. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5960. }
  5961. /**
  5962. * testDateTimeAllZeros method
  5963. *
  5964. * Test datetime() with all zeros.
  5965. *
  5966. * @return void
  5967. */
  5968. public function testDateTimeAllZeros()
  5969. {
  5970. $result = $this->Form->dateTime('Contact.date', [
  5971. 'timeFormat' => false,
  5972. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5973. 'value' => '0000-00-00'
  5974. ]);
  5975. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5976. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5977. }
  5978. /**
  5979. * testDateTimeEmptyAsArray method
  5980. *
  5981. * @return void
  5982. */
  5983. public function testDateTimeEmptyAsArray()
  5984. {
  5985. $result = $this->Form->dateTime('Contact.date', [
  5986. 'empty' => [
  5987. 'day' => 'DAY',
  5988. 'month' => 'MONTH',
  5989. 'year' => 'YEAR',
  5990. 'hour' => 'HOUR',
  5991. 'minute' => 'MINUTE',
  5992. 'meridian' => false
  5993. ],
  5994. 'default' => true
  5995. ]);
  5996. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5997. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5998. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5999. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  6000. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  6001. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  6002. $result = $this->Form->dateTime('Contact.date', [
  6003. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  6004. 'default' => true
  6005. ]);
  6006. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  6007. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  6008. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  6009. }
  6010. /**
  6011. * testFormDateTimeMulti method
  6012. *
  6013. * Test multiple datetime element generation.
  6014. *
  6015. * @return void
  6016. */
  6017. public function testFormDateTimeMulti()
  6018. {
  6019. extract($this->dateRegex);
  6020. $result = $this->Form->dateTime('Contact.1.updated');
  6021. $this->assertContains('Contact[1][updated][month]', $result);
  6022. $this->assertContains('Contact[1][updated][day]', $result);
  6023. $this->assertContains('Contact[1][updated][year]', $result);
  6024. $this->assertContains('Contact[1][updated][hour]', $result);
  6025. $this->assertContains('Contact[1][updated][minute]', $result);
  6026. }
  6027. /**
  6028. * testDateTimeLabelIdMatchesFirstControl method
  6029. *
  6030. * When changing the date format, the label should always focus the first select box when
  6031. * clicked.
  6032. *
  6033. * @return void
  6034. */
  6035. public function testDateTimeLabelIdMatchesFirstControl()
  6036. {
  6037. $result = $this->Form->control('Model.date', ['type' => 'date']);
  6038. $this->assertContains('<label>Date</label>', $result);
  6039. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  6040. $this->assertContains('<label>Date</label>', $result);
  6041. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  6042. $this->assertContains('<label>Date</label>', $result);
  6043. }
  6044. /**
  6045. * testDateTimeSecondOptions method
  6046. *
  6047. * Test datetime second=true.
  6048. *
  6049. * @return void
  6050. */
  6051. public function testDateTimeSecondOptions()
  6052. {
  6053. $result = $this->Form->dateTime('updated', ['second' => true]);
  6054. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6055. $result = $this->Form->dateTime('updated', ['second' => []]);
  6056. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6057. $result = $this->Form->dateTime('updated', ['second' => null]);
  6058. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6059. $result = $this->Form->dateTime('updated', ['second' => false]);
  6060. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6061. }
  6062. /**
  6063. * testMonth method
  6064. *
  6065. * Test generation of a month input.
  6066. *
  6067. * @return void
  6068. */
  6069. public function testMonth()
  6070. {
  6071. $result = $this->Form->month('Model.field', ['value' => '']);
  6072. $expected = [
  6073. ['select' => ['name' => 'Model[field][month]']],
  6074. ['option' => ['value' => '', 'selected' => 'selected']],
  6075. '/option',
  6076. ['option' => ['value' => '01']],
  6077. date('F', strtotime('2008-01-01 00:00:00')),
  6078. '/option',
  6079. ['option' => ['value' => '02']],
  6080. date('F', strtotime('2008-02-01 00:00:00')),
  6081. '/option',
  6082. '*/select',
  6083. ];
  6084. $this->assertHtml($expected, $result);
  6085. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  6086. $expected = [
  6087. ['select' => ['name' => 'Model[field][month]']],
  6088. ['option' => ['selected' => 'selected', 'value' => '']],
  6089. '/option',
  6090. ['option' => ['value' => '01']],
  6091. date('F', strtotime('2008-01-01 00:00:00')),
  6092. '/option',
  6093. ['option' => ['value' => '02']],
  6094. date('F', strtotime('2008-02-01 00:00:00')),
  6095. '/option',
  6096. '*/select',
  6097. ];
  6098. $this->assertHtml($expected, $result);
  6099. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6100. $expected = [
  6101. ['select' => ['name' => 'Model[field][month]']],
  6102. ['option' => ['selected' => 'selected', 'value' => '']],
  6103. '/option',
  6104. ['option' => ['value' => '01']],
  6105. '1',
  6106. '/option',
  6107. ['option' => ['value' => '02']],
  6108. '2',
  6109. '/option',
  6110. '*/select',
  6111. ];
  6112. $this->assertHtml($expected, $result);
  6113. $monthNames = [
  6114. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6115. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  6116. ];
  6117. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6118. $expected = [
  6119. ['select' => ['name' => 'Model[field][month]']],
  6120. ['option' => ['value' => '']],
  6121. '/option',
  6122. ['option' => ['value' => '01', 'selected' => 'selected']],
  6123. 'Jan',
  6124. '/option',
  6125. ['option' => ['value' => '02']],
  6126. 'Feb',
  6127. '/option',
  6128. '*/select',
  6129. ];
  6130. $this->assertHtml($expected, $result);
  6131. $this->View->setRequest(
  6132. $this->View->getRequest()->withData('Project.release', '2050-02-10')
  6133. );
  6134. $this->Form->create();
  6135. $result = $this->Form->month('Project.release');
  6136. $expected = [
  6137. ['select' => ['name' => 'Project[release][month]']],
  6138. ['option' => ['value' => '']],
  6139. '/option',
  6140. ['option' => ['value' => '01']],
  6141. 'January',
  6142. '/option',
  6143. ['option' => ['value' => '02', 'selected' => 'selected']],
  6144. 'February',
  6145. '/option',
  6146. '*/select',
  6147. ];
  6148. $this->assertHtml($expected, $result);
  6149. $result = $this->Form->month('Contact.published', [
  6150. 'empty' => 'Published on',
  6151. ]);
  6152. $this->assertContains('Published on', $result);
  6153. }
  6154. /**
  6155. * testDay method
  6156. *
  6157. * Test generation of a day input.
  6158. *
  6159. * @return void
  6160. */
  6161. public function testDay()
  6162. {
  6163. extract($this->dateRegex);
  6164. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6165. $expected = [
  6166. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6167. ['option' => ['selected' => 'selected', 'value' => '']],
  6168. '/option',
  6169. ['option' => ['value' => '01']],
  6170. '1',
  6171. '/option',
  6172. ['option' => ['value' => '02']],
  6173. '2',
  6174. '/option',
  6175. $daysRegex,
  6176. '/select',
  6177. ];
  6178. $this->assertHtml($expected, $result);
  6179. $this->View->setRequest(
  6180. $this->View->getRequest()->withData('Model.field', '2006-10-10 23:12:32')
  6181. );
  6182. $this->Form->create();
  6183. $result = $this->Form->day('Model.field');
  6184. $expected = [
  6185. ['select' => ['name' => 'Model[field][day]']],
  6186. ['option' => ['value' => '']],
  6187. '/option',
  6188. ['option' => ['value' => '01']],
  6189. '1',
  6190. '/option',
  6191. ['option' => ['value' => '02']],
  6192. '2',
  6193. '/option',
  6194. $daysRegex,
  6195. ['option' => ['value' => '10', 'selected' => 'selected']],
  6196. '10',
  6197. '/option',
  6198. $daysRegex,
  6199. '/select',
  6200. ];
  6201. $this->assertHtml($expected, $result);
  6202. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6203. $this->Form->create();
  6204. $result = $this->Form->day('Model.field', ['value' => '10']);
  6205. $expected = [
  6206. ['select' => ['name' => 'Model[field][day]']],
  6207. ['option' => ['value' => '']],
  6208. '/option',
  6209. ['option' => ['value' => '01']],
  6210. '1',
  6211. '/option',
  6212. ['option' => ['value' => '02']],
  6213. '2',
  6214. '/option',
  6215. $daysRegex,
  6216. ['option' => ['value' => '10', 'selected' => 'selected']],
  6217. '10',
  6218. '/option',
  6219. $daysRegex,
  6220. '/select',
  6221. ];
  6222. $this->assertHtml($expected, $result);
  6223. $this->View->setRequest(
  6224. $this->View->getRequest()->withData('Project.release', '2050-10-10')
  6225. );
  6226. $this->Form->create();
  6227. $result = $this->Form->day('Project.release');
  6228. $expected = [
  6229. ['select' => ['name' => 'Project[release][day]']],
  6230. ['option' => ['value' => '']],
  6231. '/option',
  6232. ['option' => ['value' => '01']],
  6233. '1',
  6234. '/option',
  6235. ['option' => ['value' => '02']],
  6236. '2',
  6237. '/option',
  6238. $daysRegex,
  6239. ['option' => ['value' => '10', 'selected' => 'selected']],
  6240. '10',
  6241. '/option',
  6242. $daysRegex,
  6243. '/select',
  6244. ];
  6245. $this->assertHtml($expected, $result);
  6246. $result = $this->Form->day('Contact.published', [
  6247. 'empty' => 'Published on',
  6248. ]);
  6249. $this->assertContains('Published on', $result);
  6250. }
  6251. /**
  6252. * testMinute method
  6253. *
  6254. * Test generation of a minute input.
  6255. *
  6256. * @return void
  6257. */
  6258. public function testMinute()
  6259. {
  6260. extract($this->dateRegex);
  6261. $result = $this->Form->minute('Model.field', ['value' => '']);
  6262. $expected = [
  6263. ['select' => ['name' => 'Model[field][minute]']],
  6264. ['option' => ['selected' => 'selected', 'value' => '']],
  6265. '/option',
  6266. ['option' => ['value' => '00']],
  6267. '00',
  6268. '/option',
  6269. ['option' => ['value' => '01']],
  6270. '01',
  6271. '/option',
  6272. ['option' => ['value' => '02']],
  6273. '02',
  6274. '/option',
  6275. $minutesRegex,
  6276. '/select',
  6277. ];
  6278. $this->assertHtml($expected, $result);
  6279. $this->View->setRequest(
  6280. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6281. );
  6282. $this->Form->create();
  6283. $result = $this->Form->minute('Model.field');
  6284. $expected = [
  6285. ['select' => ['name' => 'Model[field][minute]']],
  6286. ['option' => ['value' => '']],
  6287. '/option',
  6288. ['option' => ['value' => '00']],
  6289. '00',
  6290. '/option',
  6291. ['option' => ['value' => '01']],
  6292. '01',
  6293. '/option',
  6294. ['option' => ['value' => '02']],
  6295. '02',
  6296. '/option',
  6297. $minutesRegex,
  6298. ['option' => ['value' => '12', 'selected' => 'selected']],
  6299. '12',
  6300. '/option',
  6301. $minutesRegex,
  6302. '/select',
  6303. ];
  6304. $this->assertHtml($expected, $result);
  6305. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6306. $this->Form->create();
  6307. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6308. $expected = [
  6309. ['select' => ['name' => 'Model[field][minute]']],
  6310. ['option' => ['selected' => 'selected', 'value' => '']],
  6311. '/option',
  6312. ['option' => ['value' => '00']],
  6313. '00',
  6314. '/option',
  6315. ['option' => ['value' => '05']],
  6316. '05',
  6317. '/option',
  6318. ['option' => ['value' => '10']],
  6319. '10',
  6320. '/option',
  6321. $minutesRegex,
  6322. '/select',
  6323. ];
  6324. $this->assertHtml($expected, $result);
  6325. $this->View->setRequest(
  6326. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:10:32')
  6327. );
  6328. $this->Form->create();
  6329. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6330. $expected = [
  6331. ['select' => ['name' => 'Model[field][minute]']],
  6332. ['option' => ['value' => '']],
  6333. '/option',
  6334. ['option' => ['value' => '00']],
  6335. '00',
  6336. '/option',
  6337. ['option' => ['value' => '05']],
  6338. '05',
  6339. '/option',
  6340. ['option' => ['value' => '10', 'selected' => 'selected']],
  6341. '10',
  6342. '/option',
  6343. $minutesRegex,
  6344. '/select',
  6345. ];
  6346. $this->assertHtml($expected, $result);
  6347. }
  6348. /**
  6349. * testMeridian method
  6350. *
  6351. * Test generating an input for the meridian.
  6352. *
  6353. * @return void
  6354. */
  6355. public function testMeridian()
  6356. {
  6357. extract($this->dateRegex);
  6358. $now = new \DateTime();
  6359. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6360. $expected = [
  6361. ['select' => ['name' => 'Model[field][meridian]']],
  6362. ['option' => ['value' => '']],
  6363. '/option',
  6364. $meridianRegex,
  6365. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6366. $now->format('a'),
  6367. '/option',
  6368. '*/select'
  6369. ];
  6370. $this->assertHtml($expected, $result);
  6371. }
  6372. /**
  6373. * testHour method
  6374. *
  6375. * Test generation of an hour input.
  6376. *
  6377. * @return void
  6378. */
  6379. public function testHour()
  6380. {
  6381. extract($this->dateRegex);
  6382. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6383. $expected = [
  6384. ['select' => ['name' => 'Model[field][hour]']],
  6385. ['option' => ['selected' => 'selected', 'value' => '']],
  6386. '/option',
  6387. ['option' => ['value' => '01']],
  6388. '1',
  6389. '/option',
  6390. ['option' => ['value' => '02']],
  6391. '2',
  6392. '/option',
  6393. $hoursRegex,
  6394. '/select',
  6395. ];
  6396. $this->assertHtml($expected, $result);
  6397. $this->View->setRequest(
  6398. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6399. );
  6400. $this->Form->create();
  6401. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6402. $expected = [
  6403. ['select' => ['name' => 'Model[field][hour]']],
  6404. ['option' => ['value' => '']],
  6405. '/option',
  6406. ['option' => ['value' => '01']],
  6407. '1',
  6408. '/option',
  6409. ['option' => ['value' => '02']],
  6410. '2',
  6411. '/option',
  6412. $hoursRegex,
  6413. ['option' => ['value' => '12', 'selected' => 'selected']],
  6414. '12',
  6415. '/option',
  6416. '/select',
  6417. ];
  6418. $this->assertHtml($expected, $result);
  6419. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6420. $this->Form->create();
  6421. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6422. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6423. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6424. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6425. $this->View->setRequest(
  6426. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6427. );
  6428. $this->Form->create();
  6429. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6430. $expected = [
  6431. ['select' => ['name' => 'Model[field][hour]']],
  6432. ['option' => ['value' => '']],
  6433. '/option',
  6434. ['option' => ['value' => '00', 'selected' => 'selected']],
  6435. '0',
  6436. '/option',
  6437. ['option' => ['value' => '01']],
  6438. '1',
  6439. '/option',
  6440. ['option' => ['value' => '02']],
  6441. '2',
  6442. '/option',
  6443. $hoursRegex,
  6444. '/select',
  6445. ];
  6446. $this->assertHtml($expected, $result);
  6447. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  6448. $this->Form->create();
  6449. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6450. $thisHour = date('H');
  6451. $optValue = date('G');
  6452. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6453. $this->View->setRequest(
  6454. $this->View->getRequest()->withData('Model.field', '2050-10-10 01:12:32')
  6455. );
  6456. $this->Form->create();
  6457. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6458. $expected = [
  6459. ['select' => ['name' => 'Model[field][hour]']],
  6460. ['option' => ['value' => '']],
  6461. '/option',
  6462. ['option' => ['value' => '00']],
  6463. '0',
  6464. '/option',
  6465. ['option' => ['value' => '01', 'selected' => 'selected']],
  6466. '1',
  6467. '/option',
  6468. ['option' => ['value' => '02']],
  6469. '2',
  6470. '/option',
  6471. $hoursRegex,
  6472. '/select',
  6473. ];
  6474. $this->assertHtml($expected, $result);
  6475. }
  6476. /**
  6477. * testYear method
  6478. *
  6479. * Test generation of a year input.
  6480. *
  6481. * @return void
  6482. */
  6483. public function testYear()
  6484. {
  6485. $this->View->setRequest(
  6486. $this->View->getRequest()->withData('Contact.published', '2006-10-10')
  6487. );
  6488. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6489. $expected = [
  6490. ['select' => ['name' => 'Model[field][year]']],
  6491. ['option' => ['selected' => 'selected', 'value' => '']],
  6492. '/option',
  6493. ['option' => ['value' => '2007']],
  6494. '2007',
  6495. '/option',
  6496. ['option' => ['value' => '2006']],
  6497. '2006',
  6498. '/option',
  6499. '/select',
  6500. ];
  6501. $this->assertHtml($expected, $result);
  6502. $result = $this->Form->year('Model.field', [
  6503. 'value' => '',
  6504. 'minYear' => 2006,
  6505. 'maxYear' => 2007,
  6506. 'orderYear' => 'asc'
  6507. ]);
  6508. $expected = [
  6509. ['select' => ['name' => 'Model[field][year]']],
  6510. ['option' => ['selected' => 'selected', 'value' => '']],
  6511. '/option',
  6512. ['option' => ['value' => '2006']],
  6513. '2006',
  6514. '/option',
  6515. ['option' => ['value' => '2007']],
  6516. '2007',
  6517. '/option',
  6518. '/select',
  6519. ];
  6520. $this->assertHtml($expected, $result);
  6521. $result = $this->Form->year('Contact.published', [
  6522. 'empty' => false,
  6523. 'minYear' => 2006,
  6524. 'maxYear' => 2007,
  6525. ]);
  6526. $expected = [
  6527. ['select' => ['name' => 'Contact[published][year]']],
  6528. ['option' => ['value' => '2007']],
  6529. '2007',
  6530. '/option',
  6531. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6532. '2006',
  6533. '/option',
  6534. '/select',
  6535. ];
  6536. $this->assertHtml($expected, $result);
  6537. $result = $this->Form->year('Contact.published', [
  6538. 'empty' => 'Published on',
  6539. ]);
  6540. $this->assertContains('Published on', $result);
  6541. }
  6542. /**
  6543. * testControlDatetimePreEpoch method
  6544. *
  6545. * Test minYear being prior to the unix epoch.
  6546. *
  6547. * @return void
  6548. */
  6549. public function testControlDatetimePreEpoch()
  6550. {
  6551. $start = date('Y') - 80;
  6552. $end = date('Y') - 18;
  6553. $result = $this->Form->control('birth_year', [
  6554. 'type' => 'date',
  6555. 'label' => 'Birth Year',
  6556. 'minYear' => $start,
  6557. 'maxYear' => $end,
  6558. 'month' => false,
  6559. 'day' => false,
  6560. ]);
  6561. $this->assertContains('value="' . $start . '">' . $start, $result);
  6562. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6563. $this->assertNotContains('value="00">00', $result);
  6564. }
  6565. /**
  6566. * test control() datetime & required attributes
  6567. *
  6568. * @return void
  6569. */
  6570. public function testControlDatetimeRequired()
  6571. {
  6572. $result = $this->Form->control('birthday', [
  6573. 'type' => 'date',
  6574. 'required' => true
  6575. ]);
  6576. $this->assertContains(
  6577. '<select name="birthday[year]" required="required"',
  6578. $result
  6579. );
  6580. }
  6581. /**
  6582. * testYearAutoExpandRange method
  6583. *
  6584. * @return void
  6585. */
  6586. public function testYearAutoExpandRange()
  6587. {
  6588. $this->View->setRequest($this->View->getRequest()->withData('User.birthday', '1930-10-10'));
  6589. $result = $this->Form->year('User.birthday');
  6590. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6591. $result = $matches[1];
  6592. $expected = range(date('Y') + 5, 1930);
  6593. $this->assertEquals($expected, $result);
  6594. $this->View->setRequest($this->View->getRequest()->withData('Project.release', '2050-10-10'));
  6595. $this->Form->create();
  6596. $result = $this->Form->year('Project.release');
  6597. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6598. $result = $matches[1];
  6599. $expected = range(2050, date('Y') - 5);
  6600. $this->assertEquals($expected, $result);
  6601. $this->View->setRequest($this->View->getRequest()->withData('Project.release', '1881-10-10'));
  6602. $this->Form->create();
  6603. $result = $this->Form->year('Project.release', [
  6604. 'minYear' => 1890,
  6605. 'maxYear' => 1900
  6606. ]);
  6607. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6608. $result = $matches[1];
  6609. $expected = range(1900, 1881);
  6610. $this->assertEquals($expected, $result);
  6611. }
  6612. /**
  6613. * testControlDate method
  6614. *
  6615. * Test that control() accepts the type of date and passes options in.
  6616. *
  6617. * @return void
  6618. */
  6619. public function testControlDate()
  6620. {
  6621. $this->View->setRequest($this->View->getRequest()->withParsedBody([
  6622. 'month_year' => ['month' => date('m')],
  6623. ]));
  6624. $this->Form->create($this->article);
  6625. $result = $this->Form->control('month_year', [
  6626. 'label' => false,
  6627. 'type' => 'date',
  6628. 'minYear' => 2006,
  6629. 'maxYear' => 2008
  6630. ]);
  6631. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6632. $this->assertNotContains('value="2008" selected="selected"', $result);
  6633. }
  6634. /**
  6635. * testControlLabelFalse method
  6636. *
  6637. * Test the label option being set to false.
  6638. *
  6639. * @return void
  6640. */
  6641. public function testControlLabelFalse()
  6642. {
  6643. $this->Form->create($this->article);
  6644. $result = $this->Form->control('title', ['label' => false]);
  6645. $expected = [
  6646. 'div' => ['class' => 'input text required'],
  6647. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6648. '/div'
  6649. ];
  6650. $this->assertHtml($expected, $result);
  6651. }
  6652. /**
  6653. * testControlDateMaxYear method
  6654. *
  6655. * Let's say we want to only allow users born from 2006 to 2008 to register
  6656. * This being the first signup page, we still don't have any data.
  6657. *
  6658. * @return void
  6659. */
  6660. public function testControlDateMaxYear()
  6661. {
  6662. $this->Form->create($this->article);
  6663. $result = $this->Form->control('birthday', [
  6664. 'label' => false,
  6665. 'type' => 'date',
  6666. 'minYear' => 2006,
  6667. 'maxYear' => 2008,
  6668. 'default' => true
  6669. ]);
  6670. $this->assertContains('value="2008" selected="selected"', $result);
  6671. $this->assertContains('value="2006"', $result);
  6672. $this->assertNotContains('value="2005"', $result);
  6673. $this->assertNotContains('value="2009"', $result);
  6674. }
  6675. /**
  6676. * testTextArea method
  6677. *
  6678. * Test generation of a textarea input.
  6679. *
  6680. * @return void
  6681. */
  6682. public function testTextArea()
  6683. {
  6684. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  6685. $result = $this->Form->textarea('field');
  6686. $expected = [
  6687. 'textarea' => ['name' => 'field', 'rows' => 5],
  6688. 'some test data',
  6689. '/textarea',
  6690. ];
  6691. $this->assertHtml($expected, $result);
  6692. $result = $this->Form->textarea('user.bio');
  6693. $expected = [
  6694. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6695. '/textarea',
  6696. ];
  6697. $this->assertHtml($expected, $result);
  6698. $this->View->setRequest($this->View->getRequest()
  6699. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6700. $this->Form->create();
  6701. $result = $this->Form->textarea('field');
  6702. $expected = [
  6703. 'textarea' => ['name' => 'field', 'rows' => 5],
  6704. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6705. '/textarea',
  6706. ];
  6707. $this->assertHtml($expected, $result);
  6708. $this->View->setRequest($this->View->getRequest()->withData(
  6709. 'Model.field',
  6710. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  6711. ));
  6712. $this->Form->create();
  6713. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6714. $expected = [
  6715. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6716. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6717. '/textarea',
  6718. ];
  6719. $this->assertHtml($expected, $result);
  6720. $result = $this->Form->textarea('0.OtherModel.field');
  6721. $expected = [
  6722. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6723. '/textarea'
  6724. ];
  6725. $this->assertHtml($expected, $result);
  6726. }
  6727. /**
  6728. * testTextAreaWithStupidCharacters method
  6729. *
  6730. * Test text area with non-ascii characters.
  6731. *
  6732. * @return void
  6733. */
  6734. public function testTextAreaWithStupidCharacters()
  6735. {
  6736. $result = $this->Form->textarea('Post.content', [
  6737. 'value' => 'GREAT®',
  6738. 'rows' => '15',
  6739. 'cols' => '75'
  6740. ]);
  6741. $expected = [
  6742. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6743. 'GREAT®',
  6744. '/textarea',
  6745. ];
  6746. $this->assertHtml($expected, $result);
  6747. }
  6748. /**
  6749. * testTextAreaMaxLength method
  6750. *
  6751. * Test textareas maxlength read from schema.
  6752. *
  6753. * @return void
  6754. */
  6755. public function testTextAreaMaxLength()
  6756. {
  6757. $this->Form->create([
  6758. 'schema' => [
  6759. 'stuff' => ['type' => 'string', 'length' => 10],
  6760. ]
  6761. ]);
  6762. $result = $this->Form->control('other', ['type' => 'textarea']);
  6763. $expected = [
  6764. 'div' => ['class' => 'input textarea'],
  6765. 'label' => ['for' => 'other'],
  6766. 'Other',
  6767. '/label',
  6768. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6769. '/textarea',
  6770. '/div'
  6771. ];
  6772. $this->assertHtml($expected, $result);
  6773. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6774. $expected = [
  6775. 'div' => ['class' => 'input textarea'],
  6776. 'label' => ['for' => 'stuff'],
  6777. 'Stuff',
  6778. '/label',
  6779. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6780. '/textarea',
  6781. '/div'
  6782. ];
  6783. $this->assertHtml($expected, $result);
  6784. }
  6785. /**
  6786. * testHiddenField method
  6787. *
  6788. * Test generation of a hidden input.
  6789. *
  6790. * @return void
  6791. */
  6792. public function testHiddenField()
  6793. {
  6794. $this->article['errors'] = [
  6795. 'field' => true
  6796. ];
  6797. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  6798. $this->Form->create($this->article);
  6799. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6800. $expected = [
  6801. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6802. $this->assertHtml($expected, $result);
  6803. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6804. $expected = [
  6805. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6806. ];
  6807. $this->assertHtml($expected, $result);
  6808. }
  6809. /**
  6810. * testFileUploadField method
  6811. *
  6812. * Test generation of a file upload input.
  6813. *
  6814. * @return void
  6815. */
  6816. public function testFileUploadField()
  6817. {
  6818. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6819. $result = $this->Form->file('Model.upload');
  6820. $this->assertHtml($expected, $result);
  6821. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  6822. 'name' => '', 'type' => '', 'tmp_name' => '',
  6823. 'error' => 4, 'size' => 0
  6824. ]));
  6825. $result = $this->Form->file('Model.upload');
  6826. $this->assertHtml($expected, $result);
  6827. $this->View->setRequest(
  6828. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6829. );
  6830. $result = $this->Form->file('Model.upload');
  6831. $this->assertHtml($expected, $result);
  6832. }
  6833. /**
  6834. * testFileUploadOnOtherModel method
  6835. *
  6836. * Test File upload input on a model not used in create().
  6837. *
  6838. * @return void
  6839. */
  6840. public function testFileUploadOnOtherModel()
  6841. {
  6842. $this->Form->create($this->article, ['type' => 'file']);
  6843. $result = $this->Form->file('ValidateProfile.city');
  6844. $expected = [
  6845. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6846. ];
  6847. $this->assertHtml($expected, $result);
  6848. }
  6849. /**
  6850. * testButton method
  6851. *
  6852. * Test generation of a form button.
  6853. *
  6854. * @return void
  6855. */
  6856. public function testButton()
  6857. {
  6858. $result = $this->Form->button('Hi');
  6859. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6860. $this->assertHtml($expected, $result);
  6861. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6862. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6863. $this->assertHtml($expected, $result);
  6864. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6865. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6866. $this->assertHtml($expected, $result);
  6867. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6868. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6869. $this->assertHtml($expected, $result);
  6870. $result = $this->Form->button('No type', ['type' => false]);
  6871. $expected = ['button' => [], 'No type', '/button'];
  6872. $this->assertHtml($expected, $result);
  6873. $result = $this->Form->button('Upload Text', [
  6874. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6875. 'escape' => false
  6876. ]);
  6877. $this->assertNotRegExp('/\&039/', $result);
  6878. }
  6879. /**
  6880. * testButtonUnlockedByDefault method
  6881. *
  6882. * Test that button() makes unlocked fields by default.
  6883. *
  6884. * @return void
  6885. */
  6886. public function testButtonUnlockedByDefault()
  6887. {
  6888. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'secured'));
  6889. $this->Form->button('Save', ['name' => 'save']);
  6890. $this->Form->button('Clear');
  6891. $result = $this->Form->unlockField();
  6892. $this->assertEquals(['save'], $result);
  6893. }
  6894. /**
  6895. * Test generation of a form button with confirm message.
  6896. *
  6897. * @return void
  6898. */
  6899. public function testButtonWithConfirm()
  6900. {
  6901. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6902. $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm(&quot;Confirm me!&quot;)) { return true; } return false;'], 'Hi', '/button'];
  6903. $this->assertHtml($expected, $result);
  6904. }
  6905. /**
  6906. * testPostButton method
  6907. *
  6908. * @return void
  6909. */
  6910. public function testPostButton()
  6911. {
  6912. $result = $this->Form->postButton('Hi', '/controller/action');
  6913. $expected = [
  6914. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6915. 'div' => ['style' => 'display:none;'],
  6916. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6917. '/div',
  6918. 'button' => ['type' => 'submit'],
  6919. 'Hi',
  6920. '/button',
  6921. '/form'
  6922. ];
  6923. $this->assertHtml($expected, $result);
  6924. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6925. $this->assertContains('<input type="hidden" name="extra" value="value"', $result);
  6926. }
  6927. /**
  6928. * testPostButtonMethodType method
  6929. *
  6930. * @return void
  6931. */
  6932. public function testPostButtonMethodType()
  6933. {
  6934. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6935. $expected = [
  6936. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6937. 'div' => ['style' => 'display:none;'],
  6938. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6939. '/div',
  6940. 'button' => ['type' => 'submit'],
  6941. 'Hi',
  6942. '/button',
  6943. '/form'
  6944. ];
  6945. $this->assertHtml($expected, $result);
  6946. }
  6947. /**
  6948. * testPostButtonFormOptions method
  6949. *
  6950. * @return void
  6951. */
  6952. public function testPostButtonFormOptions()
  6953. {
  6954. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6955. $expected = [
  6956. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6957. 'div' => ['style' => 'display:none;'],
  6958. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6959. '/div',
  6960. 'button' => ['type' => 'submit'],
  6961. 'Hi',
  6962. '/button',
  6963. '/form'
  6964. ];
  6965. $this->assertHtml($expected, $result);
  6966. }
  6967. /**
  6968. * testPostButtonNestedData method
  6969. *
  6970. * Test using postButton with N dimensional data.
  6971. *
  6972. * @return void
  6973. */
  6974. public function testPostButtonNestedData()
  6975. {
  6976. $data = [
  6977. 'one' => [
  6978. 'two' => [
  6979. 3, 4, 5
  6980. ]
  6981. ]
  6982. ];
  6983. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6984. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6985. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6986. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6987. }
  6988. /**
  6989. * testSecurePostButton method
  6990. *
  6991. * Test that postButton adds _Token fields.
  6992. *
  6993. * @return void
  6994. */
  6995. public function testSecurePostButton()
  6996. {
  6997. $this->View->setRequest($this->View->getRequest()
  6998. ->withParam('_csrfToken', 'testkey')
  6999. ->withParam('_Token.unlockedFields', []));
  7000. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  7001. $tokenDebug = urlencode(json_encode([
  7002. '/posts/delete/1',
  7003. [],
  7004. []
  7005. ]));
  7006. $expected = [
  7007. 'form' => [
  7008. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  7009. ],
  7010. ['div' => ['style' => 'display:none;']],
  7011. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7012. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7013. '/div',
  7014. 'button' => ['type' => 'submit'],
  7015. 'Delete',
  7016. '/button',
  7017. ['div' => ['style' => 'display:none;']],
  7018. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7019. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7020. ['input' => [
  7021. 'type' => 'hidden',
  7022. 'name' => '_Token[debug]',
  7023. 'value' => $tokenDebug,
  7024. 'autocomplete' => 'off',
  7025. ]],
  7026. '/div',
  7027. '/form',
  7028. ];
  7029. $this->assertHtml($expected, $result);
  7030. }
  7031. /**
  7032. * testPostLink method
  7033. *
  7034. * @return void
  7035. */
  7036. public function testPostLink()
  7037. {
  7038. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7039. $expected = [
  7040. 'form' => [
  7041. 'method' => 'post', 'action' => '/posts/delete/1',
  7042. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7043. ],
  7044. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7045. '/form',
  7046. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7047. 'Delete',
  7048. '/a'
  7049. ];
  7050. $this->assertHtml($expected, $result);
  7051. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  7052. $expected = [
  7053. 'form' => [
  7054. 'method' => 'post', 'action' => '/posts/delete/1',
  7055. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7056. ],
  7057. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  7058. '/form',
  7059. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7060. 'Delete',
  7061. '/a'
  7062. ];
  7063. $this->assertHtml($expected, $result);
  7064. $result = $this->Form->postLink(
  7065. 'Delete',
  7066. '/posts/delete/1',
  7067. ['target' => '_blank', 'class' => 'btn btn-danger']
  7068. );
  7069. $expected = [
  7070. 'form' => [
  7071. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  7072. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7073. ],
  7074. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7075. '/form',
  7076. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7077. 'Delete',
  7078. '/a'
  7079. ];
  7080. $this->assertHtml($expected, $result);
  7081. }
  7082. /**
  7083. * testPostLinkWithConfirm method
  7084. *
  7085. * Test the confirm option for postLink().
  7086. *
  7087. * @return void
  7088. */
  7089. public function testPostLinkWithConfirm()
  7090. {
  7091. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  7092. $expected = [
  7093. 'form' => [
  7094. 'method' => 'post', 'action' => '/posts/delete/1',
  7095. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7096. ],
  7097. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7098. '/form',
  7099. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  7100. 'Delete',
  7101. '/a'
  7102. ];
  7103. $this->assertHtml($expected, $result);
  7104. $result = $this->Form->postLink(
  7105. 'Delete',
  7106. '/posts/delete/1',
  7107. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  7108. );
  7109. $expected = [
  7110. 'form' => [
  7111. 'method' => 'post', 'action' => '/posts/delete/1',
  7112. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7113. ],
  7114. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7115. '/form',
  7116. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  7117. 'Delete',
  7118. '/a'
  7119. ];
  7120. $this->assertHtml($expected, $result);
  7121. $this->Form->setTemplates(['confirmJs' => 'if (confirm({{confirmMessage}})) { $(\'form[name="{{formName}}"]\').submit();};']);
  7122. $result = $this->Form->postLink(
  7123. 'Delete',
  7124. '/posts/delete/1',
  7125. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  7126. );
  7127. $expected = [
  7128. 'form' => [
  7129. 'method' => 'post', 'action' => '/posts/delete/1',
  7130. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7131. ],
  7132. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7133. '/form',
  7134. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\("Confirm this deletion\?"\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/'],
  7135. 'Delete',
  7136. '/a'
  7137. ];
  7138. $this->assertHtml($expected, $result);
  7139. }
  7140. /**
  7141. * testPostLinkWithQuery method
  7142. *
  7143. * Test postLink() with query string args.
  7144. *
  7145. * @return void
  7146. */
  7147. public function testPostLinkWithQuery()
  7148. {
  7149. $result = $this->Form->postLink(
  7150. 'Delete',
  7151. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  7152. );
  7153. $expected = [
  7154. 'form' => [
  7155. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  7156. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7157. ],
  7158. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7159. '/form',
  7160. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7161. 'Delete',
  7162. '/a'
  7163. ];
  7164. $this->assertHtml($expected, $result);
  7165. }
  7166. /**
  7167. * testPostLinkWithData method
  7168. *
  7169. * Test postLink with additional data.
  7170. *
  7171. * @return void
  7172. */
  7173. public function testPostLinkWithData()
  7174. {
  7175. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7176. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7177. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7178. $this->Form->create($entity);
  7179. $this->Form->end();
  7180. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7181. $this->assertContains(
  7182. '<input type="hidden" name="name" value="show"',
  7183. $result,
  7184. 'should not contain entity data.'
  7185. );
  7186. }
  7187. /**
  7188. * testPostLinkSecurityHash method
  7189. *
  7190. * Test that security hashes for postLink include the url.
  7191. *
  7192. * @return void
  7193. */
  7194. public function testPostLinkSecurityHash()
  7195. {
  7196. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7197. $hash .= '%3Aid';
  7198. $this->View->setRequest($this->View->getRequest()->withParam('_Token.key', 'test'));
  7199. $result = $this->Form->postLink(
  7200. 'Delete',
  7201. '/posts/delete/1',
  7202. ['data' => ['id' => 1]]
  7203. );
  7204. $tokenDebug = urlencode(json_encode([
  7205. '/posts/delete/1',
  7206. [
  7207. 'id' => 1
  7208. ],
  7209. []
  7210. ]));
  7211. $expected = [
  7212. 'form' => [
  7213. 'method' => 'post', 'action' => '/posts/delete/1',
  7214. 'name', 'style' => 'display:none;'
  7215. ],
  7216. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7217. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7218. 'div' => ['style' => 'display:none;'],
  7219. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7220. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7221. ['input' => [
  7222. 'type' => 'hidden',
  7223. 'name' => '_Token[debug]',
  7224. 'value' => $tokenDebug,
  7225. 'autocomplete' => 'off',
  7226. ]],
  7227. '/div',
  7228. '/form',
  7229. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7230. 'Delete',
  7231. '/a'
  7232. ];
  7233. $this->assertHtml($expected, $result);
  7234. }
  7235. /**
  7236. * testPostLinkSecurityHashBlockMode method
  7237. *
  7238. * Test that postLink doesn't modify the fields in the containing form.
  7239. *
  7240. * postLink() calls inside open forms should not modify the field list
  7241. * for the form.
  7242. *
  7243. * @return void
  7244. */
  7245. public function testPostLinkSecurityHashBlockMode()
  7246. {
  7247. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  7248. $hash .= '%3A';
  7249. $this->View->setRequest($this->View->getRequest()->withParam('_Token.key', 'test'));
  7250. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7251. $this->Form->control('title');
  7252. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7253. $result = $this->View->fetch('postLink');
  7254. $this->assertEquals(['title'], $this->Form->fields);
  7255. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7256. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7257. }
  7258. /**
  7259. * testPostLinkSecurityHashNoDebugMode method
  7260. *
  7261. * Test that security does not include debug token if debug is false.
  7262. *
  7263. * @return void
  7264. */
  7265. public function testPostLinkSecurityHashNoDebugMode()
  7266. {
  7267. Configure::write('debug', false);
  7268. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7269. $hash .= '%3Aid';
  7270. $this->View->setRequest($this->View->getRequest()
  7271. ->withParam('_Token.key', 'test'));
  7272. $result = $this->Form->postLink(
  7273. 'Delete',
  7274. '/posts/delete/1',
  7275. ['data' => ['id' => 1]]
  7276. );
  7277. $expected = [
  7278. 'form' => [
  7279. 'method' => 'post', 'action' => '/posts/delete/1',
  7280. 'name', 'style' => 'display:none;'
  7281. ],
  7282. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7283. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7284. 'div' => ['style' => 'display:none;'],
  7285. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7286. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7287. '/div',
  7288. '/form',
  7289. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7290. 'Delete',
  7291. '/a'
  7292. ];
  7293. $this->assertHtml($expected, $result);
  7294. }
  7295. /**
  7296. * testPostLinkNestedData method
  7297. *
  7298. * Test using postLink with N dimensional data.
  7299. *
  7300. * @return void
  7301. */
  7302. public function testPostLinkNestedData()
  7303. {
  7304. $data = [
  7305. 'one' => [
  7306. 'two' => [
  7307. 3, 4, 5
  7308. ]
  7309. ]
  7310. ];
  7311. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7312. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7313. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7314. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7315. }
  7316. /**
  7317. * testPostLinkAfterGetForm method
  7318. *
  7319. * Test creating postLinks after a GET form.
  7320. *
  7321. * @return void
  7322. */
  7323. public function testPostLinkAfterGetForm()
  7324. {
  7325. $this->View->setRequest($this->View->getRequest()
  7326. ->withParam('_csrfToken', 'testkey')
  7327. ->withParam('_Token', 'val'));
  7328. $this->Form->create($this->article, ['type' => 'get']);
  7329. $this->Form->end();
  7330. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7331. $tokenDebug = urlencode(json_encode([
  7332. '/posts/delete/1',
  7333. [],
  7334. []
  7335. ]));
  7336. $expected = [
  7337. 'form' => [
  7338. 'method' => 'post', 'action' => '/posts/delete/1',
  7339. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7340. ],
  7341. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7342. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7343. 'div' => ['style' => 'display:none;'],
  7344. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7345. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7346. ['input' => [
  7347. 'type' => 'hidden', 'name' => '_Token[debug]',
  7348. 'value' => $tokenDebug,
  7349. 'autocomplete' => 'off'
  7350. ]],
  7351. '/div',
  7352. '/form',
  7353. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7354. 'Delete',
  7355. '/a'
  7356. ];
  7357. $this->assertHtml($expected, $result);
  7358. }
  7359. /**
  7360. * testPostLinkFormBuffer method
  7361. *
  7362. * Test that postLink adds form tags to view block.
  7363. *
  7364. * @return void
  7365. */
  7366. public function testPostLinkFormBuffer()
  7367. {
  7368. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7369. $expected = [
  7370. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7371. 'Delete',
  7372. '/a'
  7373. ];
  7374. $this->assertHtml($expected, $result);
  7375. $result = $this->View->fetch('postLink');
  7376. $expected = [
  7377. 'form' => [
  7378. 'method' => 'post', 'action' => '/posts/delete/1',
  7379. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7380. ],
  7381. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7382. '/form'
  7383. ];
  7384. $this->assertHtml($expected, $result);
  7385. $result = $this->Form->postLink(
  7386. 'Delete',
  7387. '/posts/delete/2',
  7388. ['block' => true, 'method' => 'DELETE']
  7389. );
  7390. $expected = [
  7391. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7392. 'Delete',
  7393. '/a'
  7394. ];
  7395. $this->assertHtml($expected, $result);
  7396. $result = $this->View->fetch('postLink');
  7397. $expected = [
  7398. 'form' => [
  7399. 'method' => 'post', 'action' => '/posts/delete/1',
  7400. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7401. ],
  7402. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7403. '/form',
  7404. [
  7405. 'form' => [
  7406. 'method' => 'post', 'action' => '/posts/delete/2',
  7407. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7408. ],
  7409. ],
  7410. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7411. '/form'
  7412. ];
  7413. $this->assertHtml($expected, $result);
  7414. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7415. $expected = [
  7416. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7417. 'Delete',
  7418. '/a'
  7419. ];
  7420. $this->assertHtml($expected, $result);
  7421. $result = $this->View->fetch('foobar');
  7422. $expected = [
  7423. 'form' => [
  7424. 'method' => 'post', 'action' => '/posts/delete/1',
  7425. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7426. ],
  7427. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7428. '/form'
  7429. ];
  7430. $this->assertHtml($expected, $result);
  7431. }
  7432. /**
  7433. * testSubmitButton method
  7434. *
  7435. * @return void
  7436. */
  7437. public function testSubmitButton()
  7438. {
  7439. $result = $this->Form->submit('');
  7440. $expected = [
  7441. 'div' => ['class' => 'submit'],
  7442. 'input' => ['type' => 'submit', 'value' => ''],
  7443. '/div'
  7444. ];
  7445. $this->assertHtml($expected, $result);
  7446. $result = $this->Form->submit('Test Submit');
  7447. $expected = [
  7448. 'div' => ['class' => 'submit'],
  7449. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7450. '/div'
  7451. ];
  7452. $this->assertHtml($expected, $result);
  7453. $result = $this->Form->submit('Next >');
  7454. $expected = [
  7455. 'div' => ['class' => 'submit'],
  7456. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7457. '/div'
  7458. ];
  7459. $this->assertHtml($expected, $result);
  7460. $result = $this->Form->submit('Next >', ['escape' => false]);
  7461. $expected = [
  7462. 'div' => ['class' => 'submit'],
  7463. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7464. '/div'
  7465. ];
  7466. $this->assertHtml($expected, $result);
  7467. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7468. $expected = [
  7469. 'div' => ['class' => 'submit'],
  7470. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7471. '/div'
  7472. ];
  7473. $this->assertHtml($expected, $result);
  7474. }
  7475. /**
  7476. * testSubmitImage method
  7477. *
  7478. * Test image submit types.
  7479. *
  7480. * @return void
  7481. */
  7482. public function testSubmitImage()
  7483. {
  7484. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7485. $expected = [
  7486. 'div' => ['class' => 'submit'],
  7487. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7488. '/div'
  7489. ];
  7490. $this->assertHtml($expected, $result);
  7491. $result = $this->Form->submit('/relative/cake.power.gif');
  7492. $expected = [
  7493. 'div' => ['class' => 'submit'],
  7494. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7495. '/div'
  7496. ];
  7497. $this->assertHtml($expected, $result);
  7498. $result = $this->Form->submit('cake.power.gif');
  7499. $expected = [
  7500. 'div' => ['class' => 'submit'],
  7501. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7502. '/div'
  7503. ];
  7504. $this->assertHtml($expected, $result);
  7505. $result = $this->Form->submit('Not.an.image');
  7506. $expected = [
  7507. 'div' => ['class' => 'submit'],
  7508. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7509. '/div'
  7510. ];
  7511. $this->assertHtml($expected, $result);
  7512. }
  7513. /**
  7514. * testSubmitUnlockedByDefault method
  7515. *
  7516. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7517. * be submitted at a time.
  7518. *
  7519. * @return void
  7520. */
  7521. public function testSubmitUnlockedByDefault()
  7522. {
  7523. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'secured'));
  7524. $this->Form->submit('Go go');
  7525. $this->Form->submit('Save', ['name' => 'save']);
  7526. $result = $this->Form->unlockField();
  7527. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7528. }
  7529. /**
  7530. * testSubmitImageTimestamp method
  7531. *
  7532. * Test submit image with timestamps.
  7533. *
  7534. * @return void
  7535. */
  7536. public function testSubmitImageTimestamp()
  7537. {
  7538. Configure::write('Asset.timestamp', 'force');
  7539. $result = $this->Form->submit('cake.power.gif');
  7540. $expected = [
  7541. 'div' => ['class' => 'submit'],
  7542. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7543. '/div'
  7544. ];
  7545. $this->assertHtml($expected, $result);
  7546. }
  7547. /**
  7548. * testDateTimeWithGetForms method
  7549. *
  7550. * Test that datetime() works with GET style forms.
  7551. *
  7552. * @return void
  7553. */
  7554. public function testDateTimeWithGetForms()
  7555. {
  7556. extract($this->dateRegex);
  7557. $this->Form->create($this->article, ['type' => 'get']);
  7558. $result = $this->Form->datetime('created');
  7559. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7560. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7561. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7562. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7563. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7564. }
  7565. /**
  7566. * testForMagicControlNonExistingNorValidated method
  7567. *
  7568. * @return void
  7569. */
  7570. public function testForMagicControlNonExistingNorValidated()
  7571. {
  7572. $this->Form->create($this->article);
  7573. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7574. $result = $this->Form->control('non_existing_nor_validated');
  7575. $expected = [
  7576. 'label' => ['for' => 'non-existing-nor-validated'],
  7577. 'Non Existing Nor Validated',
  7578. '/label',
  7579. 'input' => [
  7580. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7581. 'id' => 'non-existing-nor-validated'
  7582. ]
  7583. ];
  7584. $this->assertHtml($expected, $result);
  7585. $result = $this->Form->control('non_existing_nor_validated', [
  7586. 'val' => 'my value'
  7587. ]);
  7588. $expected = [
  7589. 'label' => ['for' => 'non-existing-nor-validated'],
  7590. 'Non Existing Nor Validated',
  7591. '/label',
  7592. 'input' => [
  7593. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7594. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7595. ]
  7596. ];
  7597. $this->assertHtml($expected, $result);
  7598. $this->View->setRequest(
  7599. $this->View->getRequest()->withData('non_existing_nor_validated', 'CakePHP magic')
  7600. );
  7601. $this->Form->create($this->article);
  7602. $result = $this->Form->control('non_existing_nor_validated');
  7603. $expected = [
  7604. 'label' => ['for' => 'non-existing-nor-validated'],
  7605. 'Non Existing Nor Validated',
  7606. '/label',
  7607. 'input' => [
  7608. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7609. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7610. ]
  7611. ];
  7612. $this->assertHtml($expected, $result);
  7613. }
  7614. /**
  7615. * testFormMagicControlLabel method
  7616. *
  7617. * @return void
  7618. */
  7619. public function testFormMagicControlLabel()
  7620. {
  7621. $this->getTableLocator()->get('Contacts', [
  7622. 'className' => __NAMESPACE__ . '\ContactsTable'
  7623. ]);
  7624. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7625. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7626. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7627. $expected = [
  7628. 'label' => ['for' => 'contacts-name'],
  7629. 'My label',
  7630. '/label',
  7631. 'input' => [
  7632. 'type' => 'text', 'name' => 'Contacts[name]',
  7633. 'id' => 'contacts-name', 'maxlength' => '255'
  7634. ]
  7635. ];
  7636. $this->assertHtml($expected, $result);
  7637. $result = $this->Form->control('name', [
  7638. 'label' => ['class' => 'mandatory']
  7639. ]);
  7640. $expected = [
  7641. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7642. 'Name',
  7643. '/label',
  7644. 'input' => [
  7645. 'type' => 'text', 'name' => 'name',
  7646. 'id' => 'name', 'maxlength' => '255'
  7647. ]
  7648. ];
  7649. $this->assertHtml($expected, $result);
  7650. $result = $this->Form->control('name', [
  7651. 'div' => false,
  7652. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7653. ]);
  7654. $expected = [
  7655. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7656. 'My label',
  7657. '/label',
  7658. 'input' => [
  7659. 'type' => 'text', 'name' => 'name',
  7660. 'id' => 'name', 'maxlength' => '255'
  7661. ]
  7662. ];
  7663. $this->assertHtml($expected, $result);
  7664. $result = $this->Form->control('Contact.name', [
  7665. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7666. ]);
  7667. $expected = [
  7668. 'label' => ['for' => 'my_id'],
  7669. 'Name',
  7670. '/label',
  7671. 'input' => [
  7672. 'type' => 'text', 'name' => 'Contact[name]',
  7673. 'id' => 'my_id', 'maxlength' => '255'
  7674. ]
  7675. ];
  7676. $this->assertHtml($expected, $result);
  7677. $result = $this->Form->control('1.id');
  7678. $expected = ['input' => [
  7679. 'type' => 'hidden', 'name' => '1[id]',
  7680. 'id' => '1-id'
  7681. ]];
  7682. $this->assertHtml($expected, $result);
  7683. $result = $this->Form->control('1.name');
  7684. $expected = [
  7685. 'label' => ['for' => '1-name'],
  7686. 'Name',
  7687. '/label',
  7688. 'input' => [
  7689. 'type' => 'text', 'name' => '1[name]',
  7690. 'id' => '1-name', 'maxlength' => '255'
  7691. ]
  7692. ];
  7693. $this->assertHtml($expected, $result);
  7694. }
  7695. /**
  7696. * testFormEnd method
  7697. *
  7698. * @return void
  7699. */
  7700. public function testFormEnd()
  7701. {
  7702. $this->assertEquals('</form>', $this->Form->end());
  7703. }
  7704. /**
  7705. * testMultiRecordForm method
  7706. *
  7707. * Test the generation of fields for a multi record form.
  7708. *
  7709. * @return void
  7710. */
  7711. public function testMultiRecordForm()
  7712. {
  7713. $this->loadFixtures('Articles', 'Comments');
  7714. $articles = $this->getTableLocator()->get('Articles');
  7715. $articles->hasMany('Comments');
  7716. $comment = new Entity(['comment' => 'Value']);
  7717. $article = new Article(['comments' => [$comment]]);
  7718. $this->Form->create([$article]);
  7719. $result = $this->Form->control('0.comments.1.comment');
  7720. //@codingStandardsIgnoreStart
  7721. $expected = [
  7722. 'div' => ['class' => 'input textarea'],
  7723. 'label' => ['for' => '0-comments-1-comment'],
  7724. 'Comment',
  7725. '/label',
  7726. 'textarea' => [
  7727. 'name',
  7728. 'id' => '0-comments-1-comment',
  7729. 'rows' => 5
  7730. ],
  7731. '/textarea',
  7732. '/div'
  7733. ];
  7734. //@codingStandardsIgnoreEnd
  7735. $this->assertHtml($expected, $result);
  7736. $result = $this->Form->control('0.comments.0.comment');
  7737. //@codingStandardsIgnoreStart
  7738. $expected = [
  7739. 'div' => ['class' => 'input textarea'],
  7740. 'label' => ['for' => '0-comments-0-comment'],
  7741. 'Comment',
  7742. '/label',
  7743. 'textarea' => [
  7744. 'name',
  7745. 'id' => '0-comments-0-comment',
  7746. 'rows' => 5
  7747. ],
  7748. 'Value',
  7749. '/textarea',
  7750. '/div'
  7751. ];
  7752. //@codingStandardsIgnoreEnd
  7753. $this->assertHtml($expected, $result);
  7754. $comment->setError('comment', ['Not valid']);
  7755. $result = $this->Form->control('0.comments.0.comment');
  7756. //@codingStandardsIgnoreStart
  7757. $expected = [
  7758. 'div' => ['class' => 'input textarea error'],
  7759. 'label' => ['for' => '0-comments-0-comment'],
  7760. 'Comment',
  7761. '/label',
  7762. 'textarea' => [
  7763. 'name',
  7764. 'class' => 'form-error',
  7765. 'id' => '0-comments-0-comment',
  7766. 'rows' => 5
  7767. ],
  7768. 'Value',
  7769. '/textarea',
  7770. ['div' => ['class' => 'error-message']],
  7771. 'Not valid',
  7772. '/div',
  7773. '/div'
  7774. ];
  7775. //@codingStandardsIgnoreEnd
  7776. $this->assertHtml($expected, $result);
  7777. $this->getTableLocator()->get('Comments')
  7778. ->getValidator('default')
  7779. ->allowEmpty('comment', false);
  7780. $result = $this->Form->control('0.comments.1.comment');
  7781. //@codingStandardsIgnoreStart
  7782. $expected = [
  7783. 'div' => ['class' => 'input textarea required'],
  7784. 'label' => ['for' => '0-comments-1-comment'],
  7785. 'Comment',
  7786. '/label',
  7787. 'textarea' => [
  7788. 'name',
  7789. 'required' => 'required',
  7790. 'id' => '0-comments-1-comment',
  7791. 'rows' => 5,
  7792. ],
  7793. '/textarea',
  7794. '/div'
  7795. ];
  7796. //@codingStandardsIgnoreEnd
  7797. $this->assertHtml($expected, $result);
  7798. }
  7799. /**
  7800. * testHtml5Controls method
  7801. *
  7802. * Test that some html5 inputs + FormHelper::__call() work.
  7803. *
  7804. * @return void
  7805. */
  7806. public function testHtml5Controls()
  7807. {
  7808. $result = $this->Form->email('User.email');
  7809. $expected = [
  7810. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7811. ];
  7812. $this->assertHtml($expected, $result);
  7813. $result = $this->Form->search('User.query');
  7814. $expected = [
  7815. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7816. ];
  7817. $this->assertHtml($expected, $result);
  7818. $result = $this->Form->search('User.query', ['value' => 'test']);
  7819. $expected = [
  7820. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7821. ];
  7822. $this->assertHtml($expected, $result);
  7823. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7824. $expected = [
  7825. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7826. ];
  7827. $this->assertHtml($expected, $result);
  7828. }
  7829. /**
  7830. * testHtml5ControlWithControl method
  7831. *
  7832. * Test accessing html5 inputs through control().
  7833. *
  7834. * @return void
  7835. */
  7836. public function testHtml5ControlWithControl()
  7837. {
  7838. $this->Form->create();
  7839. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7840. $result = $this->Form->control('website', [
  7841. 'type' => 'url',
  7842. 'val' => 'http://domain.tld',
  7843. 'label' => false
  7844. ]);
  7845. $expected = [
  7846. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7847. ];
  7848. $this->assertHtml($expected, $result);
  7849. }
  7850. /**
  7851. * testHtml5ControlException method
  7852. *
  7853. * Test errors when field name is missing.
  7854. *
  7855. * @return void
  7856. */
  7857. public function testHtml5ControlException()
  7858. {
  7859. $this->expectException(\Cake\Core\Exception\Exception::class);
  7860. $this->Form->email();
  7861. }
  7862. /**
  7863. * tests fields that are required use custom validation messages
  7864. *
  7865. * @return void
  7866. */
  7867. public function testHtml5ErrorMessage()
  7868. {
  7869. $this->Form->setConfig('autoSetCustomValidity', true);
  7870. $validator = (new \Cake\Validation\Validator())
  7871. ->requirePresence('email', true, 'Custom error message')
  7872. ->requirePresence('password')
  7873. ->alphaNumeric('password')
  7874. ->notBlank('phone');
  7875. $table = $this->getTableLocator()->get('Contacts', [
  7876. 'className' => __NAMESPACE__ . '\ContactsTable'
  7877. ]);
  7878. $table->setValidator('default', $validator);
  7879. $contact = new Entity();
  7880. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7881. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7882. $result = $this->Form->control('password');
  7883. $expected = [
  7884. 'label' => ['for' => 'password'],
  7885. 'Password',
  7886. '/label',
  7887. 'input' => [
  7888. 'id' => 'password',
  7889. 'name' => 'password',
  7890. 'type' => 'password',
  7891. 'value' => '',
  7892. 'required' => 'required',
  7893. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7894. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.validity.valid) this.setCustomValidity(&#039;This field is required&#039;)',
  7895. ]
  7896. ];
  7897. $this->assertHtml($expected, $result);
  7898. $result = $this->Form->control('phone');
  7899. $expected = [
  7900. 'label' => ['for' => 'phone'],
  7901. 'Phone',
  7902. '/label',
  7903. 'input' => [
  7904. 'id' => 'phone',
  7905. 'name' => 'phone',
  7906. 'type' => 'tel',
  7907. 'value' => '',
  7908. 'maxlength' => 255,
  7909. 'required' => 'required',
  7910. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7911. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.validity.valid) this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  7912. ]
  7913. ];
  7914. $this->assertHtml($expected, $result);
  7915. $result = $this->Form->control('email');
  7916. $expected = [
  7917. 'label' => ['for' => 'email'],
  7918. 'Email',
  7919. '/label',
  7920. 'input' => [
  7921. 'id' => 'email',
  7922. 'name' => 'email',
  7923. 'type' => 'email',
  7924. 'value' => '',
  7925. 'maxlength' => 255,
  7926. 'required' => 'required',
  7927. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7928. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.validity.valid) this.setCustomValidity(&#039;Custom error message&#039;)',
  7929. ]
  7930. ];
  7931. $this->assertHtml($expected, $result);
  7932. }
  7933. /**
  7934. * tests that custom validation messages are in templateVars
  7935. *
  7936. * @return void
  7937. */
  7938. public function testHtml5ErrorMessageInTemplateVars()
  7939. {
  7940. $validator = (new \Cake\Validation\Validator())
  7941. ->requirePresence('email', true, 'Custom error "message" & entities')
  7942. ->requirePresence('password')
  7943. ->alphaNumeric('password')
  7944. ->notBlank('phone');
  7945. $table = $this->getTableLocator()->get('Contacts', [
  7946. 'className' => __NAMESPACE__ . '\ContactsTable'
  7947. ]);
  7948. $table->setValidator('default', $validator);
  7949. $contact = new Entity();
  7950. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7951. $this->Form->setTemplates([
  7952. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7953. 'inputContainer' => '{{content}}'
  7954. ]);
  7955. $result = $this->Form->control('password');
  7956. $expected = [
  7957. 'label' => ['for' => 'password'],
  7958. 'Password',
  7959. '/label',
  7960. 'input' => [
  7961. 'id' => 'password',
  7962. 'name' => 'password',
  7963. 'type' => 'password',
  7964. 'value' => '',
  7965. 'required' => 'required',
  7966. 'data-message' => 'This field is required',
  7967. ]
  7968. ];
  7969. $this->assertHtml($expected, $result);
  7970. $result = $this->Form->control('phone');
  7971. $expected = [
  7972. 'label' => ['for' => 'phone'],
  7973. 'Phone',
  7974. '/label',
  7975. 'input' => [
  7976. 'id' => 'phone',
  7977. 'name' => 'phone',
  7978. 'type' => 'tel',
  7979. 'value' => '',
  7980. 'maxlength' => 255,
  7981. 'required' => 'required',
  7982. 'data-message' => 'This field cannot be left empty',
  7983. ],
  7984. ];
  7985. $this->assertHtml($expected, $result);
  7986. $result = $this->Form->control('email', [
  7987. 'templateVars' => [
  7988. 'custom' => 'data-custom="1"'
  7989. ]
  7990. ]);
  7991. $expected = [
  7992. 'label' => ['for' => 'email'],
  7993. 'Email',
  7994. '/label',
  7995. 'input' => [
  7996. 'id' => 'email',
  7997. 'name' => 'email',
  7998. 'type' => 'email',
  7999. 'value' => '',
  8000. 'maxlength' => 255,
  8001. 'required' => 'required',
  8002. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  8003. 'data-custom' => '1',
  8004. ],
  8005. ];
  8006. $this->assertHtml($expected, $result);
  8007. }
  8008. /**
  8009. * testRequiredAttribute method
  8010. *
  8011. * Tests that formhelper sets required attributes.
  8012. *
  8013. * @return void
  8014. */
  8015. public function testRequiredAttribute()
  8016. {
  8017. $this->article['required'] = [
  8018. 'title' => true,
  8019. 'body' => false,
  8020. ];
  8021. $this->Form->create($this->article);
  8022. $result = $this->Form->control('title');
  8023. $expected = [
  8024. 'div' => ['class' => 'input text required'],
  8025. 'label' => ['for' => 'title'],
  8026. 'Title',
  8027. '/label',
  8028. 'input' => [
  8029. 'type' => 'text',
  8030. 'name' => 'title',
  8031. 'id' => 'title',
  8032. 'required' => 'required',
  8033. ],
  8034. '/div'
  8035. ];
  8036. $this->assertHtml($expected, $result);
  8037. $result = $this->Form->control('title', ['required' => false]);
  8038. $this->assertNotContains('required', $result);
  8039. $result = $this->Form->control('body');
  8040. $expected = [
  8041. 'div' => ['class' => 'input text'],
  8042. 'label' => ['for' => 'body'],
  8043. 'Body',
  8044. '/label',
  8045. 'input' => [
  8046. 'type' => 'text',
  8047. 'name' => 'body',
  8048. 'id' => 'body',
  8049. ],
  8050. '/div'
  8051. ];
  8052. $this->assertHtml($expected, $result);
  8053. $result = $this->Form->control('body', ['required' => true]);
  8054. $this->assertContains('required', $result);
  8055. }
  8056. /**
  8057. * testControlsNotNested method
  8058. *
  8059. * Tests that it is possible to put inputs outside of the label.
  8060. *
  8061. * @return void
  8062. */
  8063. public function testControlsNotNested()
  8064. {
  8065. $this->Form->setTemplates([
  8066. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  8067. 'formGroup' => '{{input}}{{label}}',
  8068. ]);
  8069. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  8070. $expected = [
  8071. 'div' => ['class' => 'input checkbox'],
  8072. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  8073. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  8074. 'label' => ['for' => 'foo'],
  8075. 'Foo',
  8076. '/label',
  8077. '/div'
  8078. ];
  8079. $this->assertHtml($expected, $result);
  8080. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  8081. $expected = [
  8082. 'div' => ['class' => 'input checkbox'],
  8083. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  8084. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  8085. '/div'
  8086. ];
  8087. $this->assertHtml($expected, $result);
  8088. $result = $this->Form->control('confirm', [
  8089. 'type' => 'radio',
  8090. 'options' => ['Y' => 'Yes', 'N' => 'No']
  8091. ]);
  8092. $expected = [
  8093. 'div' => ['class' => 'input radio'],
  8094. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  8095. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  8096. ['label' => ['for' => 'confirm-y']],
  8097. 'Yes',
  8098. '/label',
  8099. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  8100. ['label' => ['for' => 'confirm-n']],
  8101. 'No',
  8102. '/label',
  8103. '<label',
  8104. 'Confirm',
  8105. '/label',
  8106. '/div',
  8107. ];
  8108. $this->assertHtml($expected, $result);
  8109. $result = $this->Form->select('category', ['1', '2'], [
  8110. 'multiple' => 'checkbox',
  8111. 'name' => 'fish',
  8112. ]);
  8113. $expected = [
  8114. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  8115. ['div' => ['class' => 'checkbox']],
  8116. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  8117. ['label' => ['for' => 'fish-0']],
  8118. '1',
  8119. '/label',
  8120. '/div',
  8121. ['div' => ['class' => 'checkbox']],
  8122. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  8123. ['label' => ['for' => 'fish-1']],
  8124. '2',
  8125. '/label',
  8126. '/div'
  8127. ];
  8128. $this->assertHtml($expected, $result);
  8129. }
  8130. /**
  8131. * testControlContainerTemplates method
  8132. *
  8133. * Test that *Container templates are used by input.
  8134. *
  8135. * @return void
  8136. */
  8137. public function testControlContainerTemplates()
  8138. {
  8139. $this->Form->setTemplates([
  8140. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  8141. 'radioContainer' => '<div class="rad">{{content}}</div>',
  8142. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  8143. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  8144. ]);
  8145. $this->article['errors'] = [
  8146. 'Article' => ['published' => 'error message']
  8147. ];
  8148. $this->Form->create($this->article);
  8149. $result = $this->Form->control('accept', [
  8150. 'type' => 'checkbox'
  8151. ]);
  8152. $expected = [
  8153. 'div' => ['class' => 'check'],
  8154. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  8155. 'label' => ['for' => 'accept'],
  8156. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  8157. 'Accept',
  8158. '/label',
  8159. '/div'
  8160. ];
  8161. $this->assertHtml($expected, $result);
  8162. $result = $this->Form->control('accept', [
  8163. 'type' => 'radio',
  8164. 'options' => ['Y', 'N']
  8165. ]);
  8166. $this->assertContains('<div class="rad">', $result);
  8167. $result = $this->Form->control('Article.published', [
  8168. 'type' => 'radio',
  8169. 'options' => ['Y', 'N']
  8170. ]);
  8171. $this->assertContains('<div class="rad err">', $result);
  8172. $result = $this->Form->control('Article.created', [
  8173. 'type' => 'datetime',
  8174. ]);
  8175. $this->assertContains('<div class="dt">', $result);
  8176. }
  8177. /**
  8178. * testFormGroupTemplates method
  8179. *
  8180. * Test that *Container templates are used by input.
  8181. *
  8182. * @return void
  8183. */
  8184. public function testFormGroupTemplates()
  8185. {
  8186. $this->Form->setTemplates([
  8187. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  8188. ]);
  8189. $this->Form->create($this->article);
  8190. $result = $this->Form->control('accept', [
  8191. 'type' => 'radio',
  8192. 'options' => ['Y', 'N']
  8193. ]);
  8194. $this->assertContains('<div class="radio">', $result);
  8195. }
  8196. /**
  8197. * testResetTemplates method
  8198. *
  8199. * Test resetting templates.
  8200. *
  8201. * @return void
  8202. */
  8203. public function testResetTemplates()
  8204. {
  8205. $this->Form->setTemplates(['input' => '<input/>']);
  8206. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  8207. $this->Form->resetTemplates();
  8208. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  8209. }
  8210. /**
  8211. * testContext method
  8212. *
  8213. * Test the context method.
  8214. *
  8215. * @return void
  8216. */
  8217. public function testContext()
  8218. {
  8219. $result = $this->Form->context();
  8220. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  8221. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  8222. $this->assertSame($mock, $this->Form->context($mock));
  8223. $this->assertSame($mock, $this->Form->context());
  8224. }
  8225. /**
  8226. * testAutoDomId method
  8227. *
  8228. * @return void
  8229. */
  8230. public function testAutoDomId()
  8231. {
  8232. $result = $this->Form->text('field', ['id' => true]);
  8233. $expected = [
  8234. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  8235. ];
  8236. $this->assertHtml($expected, $result);
  8237. // Ensure id => doesn't cause problem when multiple inputs are generated.
  8238. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  8239. $expected = [
  8240. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  8241. ['label' => ['for' => 'field-0']],
  8242. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  8243. 'option A',
  8244. '/label',
  8245. ['label' => ['for' => 'field-1']],
  8246. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  8247. 'option B',
  8248. '/label',
  8249. ];
  8250. $this->assertHtml($expected, $result);
  8251. $result = $this->Form->select(
  8252. 'multi_field',
  8253. ['first', 'second'],
  8254. ['multiple' => 'checkbox', 'id' => true]
  8255. );
  8256. $expected = [
  8257. 'input' => [
  8258. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  8259. ],
  8260. ['div' => ['class' => 'checkbox']],
  8261. ['label' => ['for' => 'multi-field-0']],
  8262. ['input' => [
  8263. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8264. 'value' => '0', 'id' => 'multi-field-0'
  8265. ]],
  8266. 'first',
  8267. '/label',
  8268. '/div',
  8269. ['div' => ['class' => 'checkbox']],
  8270. ['label' => ['for' => 'multi-field-1']],
  8271. ['input' => [
  8272. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8273. 'value' => '1', 'id' => 'multi-field-1'
  8274. ]],
  8275. 'second',
  8276. '/label',
  8277. '/div',
  8278. ];
  8279. $this->assertHtml($expected, $result);
  8280. }
  8281. /**
  8282. * Test the basic setters and getters for value sources
  8283. *
  8284. * @return void
  8285. */
  8286. public function testFormValueSourcesSettersGetters()
  8287. {
  8288. $this->View->setRequest($this->View->getRequest()
  8289. ->withData('id', '1')
  8290. ->withQueryParams(['id' => '2']));
  8291. $expected = ['context'];
  8292. $result = $this->Form->getValueSources();
  8293. $this->assertEquals($expected, $result);
  8294. $expected = ['query', 'data', 'context'];
  8295. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  8296. $result = $this->Form->getValueSources();
  8297. $this->assertEquals($expected, $result);
  8298. $this->Form->setValueSources(['context']);
  8299. $expected = '1';
  8300. $result = $this->Form->getSourceValue('id');
  8301. $this->assertEquals($expected, $result);
  8302. $this->Form->setValueSources('query');
  8303. $expected = ['query'];
  8304. $result = $this->Form->getValueSources();
  8305. $this->assertEquals($expected, $result);
  8306. $expected = '2';
  8307. $result = $this->Form->getSourceValue('id');
  8308. $this->assertEquals($expected, $result);
  8309. $this->Form->setValueSources(['data']);
  8310. $expected = '1';
  8311. $result = $this->Form->getSourceValue('id');
  8312. $this->assertEquals($expected, $result);
  8313. $this->Form->setValueSources(['query', 'data']);
  8314. $expected = '2';
  8315. $result = $this->Form->getSourceValue('id');
  8316. $this->assertEquals($expected, $result);
  8317. }
  8318. /**
  8319. * Tests the different input rendering values based on sources values switching
  8320. *
  8321. * @return void
  8322. */
  8323. public function testFormValueSourcesSingleSwitchRendering()
  8324. {
  8325. $this->loadFixtures('Articles');
  8326. $articles = $this->getTableLocator()->get('Articles');
  8327. $article = new Article();
  8328. $articles->patchEntity($article, ['id' => '3']);
  8329. $this->Form->create($article);
  8330. $this->Form->setValueSources(['context']);
  8331. $result = $this->Form->control('id');
  8332. $expected = [
  8333. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8334. ];
  8335. $this->assertHtml($expected, $result);
  8336. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  8337. $this->Form->setValueSources(['query']);
  8338. $result = $this->Form->control('id');
  8339. $expected = [
  8340. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8341. ];
  8342. $this->assertHtml($expected, $result);
  8343. $this->View->setRequest($this->View->getRequest()
  8344. ->withQueryParams(['id' => '5a'])
  8345. ->withData('id', '5b'));
  8346. $this->Form->setValueSources(['context']);
  8347. $this->Form->create($article);
  8348. $result = $this->Form->control('id');
  8349. $expected = [
  8350. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8351. ];
  8352. $this->assertHtml($expected, $result);
  8353. $this->Form->setValueSources(['data']);
  8354. $this->Form->create($article);
  8355. $result = $this->Form->control('id');
  8356. $expected = [
  8357. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8358. ];
  8359. $this->assertHtml($expected, $result);
  8360. $this->Form->setValueSources(['query']);
  8361. $result = $this->Form->control('id');
  8362. $expected = [
  8363. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8364. ];
  8365. $this->assertHtml($expected, $result);
  8366. }
  8367. /**
  8368. * Tests the different input rendering values based on sources values switching while supplying
  8369. * an entity (base context) and multiple sources (such as data, query)
  8370. *
  8371. * @return void
  8372. */
  8373. public function testFormValueSourcesListSwitchRendering()
  8374. {
  8375. $this->loadFixtures('Articles');
  8376. $articles = $this->getTableLocator()->get('Articles');
  8377. $article = new Article();
  8378. $articles->patchEntity($article, ['id' => '3']);
  8379. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  8380. $this->Form->create($article);
  8381. $this->Form->setValueSources(['context', 'query']);
  8382. $result = $this->Form->control('id');
  8383. $expected = [
  8384. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8385. ];
  8386. $this->assertHtml($expected, $result);
  8387. $this->Form->setValueSources(['query', 'context']);
  8388. $result = $this->Form->control('id');
  8389. $expected = [
  8390. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8391. ];
  8392. $this->assertHtml($expected, $result);
  8393. $this->Form->setValueSources(['data', 'query', 'context']);
  8394. $result = $this->Form->control('id');
  8395. $expected = [
  8396. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8397. ];
  8398. $this->assertHtml($expected, $result);
  8399. $this->View->setRequest($this->View->getRequest()
  8400. ->withData('id', '8')
  8401. ->withQueryParams(['id' => '9']));
  8402. $this->Form->setValueSources(['data', 'query', 'context']);
  8403. $result = $this->Form->control('id');
  8404. $expected = [
  8405. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8406. ];
  8407. $this->assertHtml($expected, $result);
  8408. }
  8409. /**
  8410. * Test the different form input renderings based on values sources switchings through form options
  8411. *
  8412. * @return void
  8413. */
  8414. public function testFormValueSourcesSwitchViaOptionsRendering()
  8415. {
  8416. $this->loadFixtures('Articles');
  8417. $articles = $this->getTableLocator()->get('Articles');
  8418. $article = new Article();
  8419. $articles->patchEntity($article, ['id' => '3']);
  8420. $this->View->setRequest(
  8421. $this->View->getRequest()->withData('id', 4)->withQueryParams(['id' => 5])
  8422. );
  8423. $this->Form->create($article, ['valueSources' => 'query']);
  8424. $result = $this->Form->control('id');
  8425. $expected = [
  8426. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8427. ];
  8428. $this->assertHtml($expected, $result);
  8429. $result = $this->Form->getSourceValue('id');
  8430. $this->assertEquals('5', $result);
  8431. $this->Form->setValueSources(['context']);
  8432. $this->Form->create($article, ['valueSources' => 'query']);
  8433. $result = $this->Form->control('id');
  8434. $expected = [
  8435. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8436. ];
  8437. $this->assertHtml($expected, $result);
  8438. $result = $this->Form->getSourceValue('id');
  8439. $this->assertEquals('5', $result);
  8440. $this->Form->setValueSources(['query']);
  8441. $this->Form->create($article, ['valueSources' => 'data']);
  8442. $result = $this->Form->control('id');
  8443. $expected = [
  8444. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8445. ];
  8446. $this->assertHtml($expected, $result);
  8447. $result = $this->Form->getSourceValue('id');
  8448. $this->assertEquals('4', $result);
  8449. $this->Form->setValueSources(['query']);
  8450. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8451. $result = $this->Form->control('id');
  8452. $expected = [
  8453. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8454. ];
  8455. $this->assertHtml($expected, $result);
  8456. $result = $this->Form->getSourceValue('id');
  8457. $this->assertEquals('4', $result);
  8458. }
  8459. /**
  8460. * Test the different form input renderings based on values sources switchings through form options
  8461. *
  8462. * @return void
  8463. */
  8464. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8465. {
  8466. $this->loadFixtures('Articles');
  8467. $articles = $this->getTableLocator()->get('Articles');
  8468. $article = new Article();
  8469. $articles->patchEntity($article, ['id' => '3']);
  8470. $this->View->setRequest(
  8471. $this->View->getRequest()->withData('id', 10)->withQueryParams(['id' => 11])
  8472. );
  8473. $this->Form->setValueSources(['context'])
  8474. ->create($article, ['valueSources' => ['query', 'data']]);
  8475. $result = $this->Form->control('id');
  8476. $expected = [
  8477. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8478. ];
  8479. $this->assertHtml($expected, $result);
  8480. $result = $this->Form->getSourceValue('id');
  8481. $this->assertEquals('11', $result);
  8482. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  8483. $this->Form->setValueSources(['context'])
  8484. ->create($article, ['valueSources' => ['query', 'data']]);
  8485. $result = $this->Form->control('id');
  8486. $expected = [
  8487. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8488. ];
  8489. $this->assertHtml($expected, $result);
  8490. $result = $this->Form->getSourceValue('id');
  8491. $this->assertEquals('10', $result);
  8492. }
  8493. /**
  8494. * Test the different form values sources resetting through From::end();
  8495. *
  8496. * @return void
  8497. */
  8498. public function testFormValueSourcesResetViaEnd()
  8499. {
  8500. $expected = ['context'];
  8501. $result = $this->Form->getValueSources();
  8502. $this->assertEquals($expected, $result);
  8503. $expected = ['query', 'context', 'data'];
  8504. $this->Form->setValueSources(['query', 'context', 'data']);
  8505. $result = $this->Form->getValueSources();
  8506. $this->assertEquals($expected, $result);
  8507. $this->Form->create();
  8508. $result = $this->Form->getValueSources();
  8509. $this->assertEquals($expected, $result);
  8510. $this->Form->end();
  8511. $result = $this->Form->getValueSources();
  8512. $this->assertEquals(['context'], $result);
  8513. }
  8514. /**
  8515. * Test sources values defaults handling
  8516. *
  8517. * @return void
  8518. */
  8519. public function testFormValueSourcesDefaults()
  8520. {
  8521. $this->View->setRequest(
  8522. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  8523. );
  8524. $this->Form->create();
  8525. $result = $this->Form->password('password');
  8526. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8527. $this->assertHtml($expected, $result);
  8528. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8529. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8530. $this->assertHtml($expected, $result);
  8531. $this->Form->setValueSources('query');
  8532. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8533. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8534. $this->assertHtml($expected, $result);
  8535. $this->Form->setValueSources('data');
  8536. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8537. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8538. $this->assertHtml($expected, $result);
  8539. }
  8540. /**
  8541. * Test sources values schema defaults handling
  8542. *
  8543. * @return void
  8544. */
  8545. public function testSourcesValueDoesntExistPassThrough()
  8546. {
  8547. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  8548. $articles = $this->getTableLocator()->get('Articles');
  8549. $entity = $articles->newEntity();
  8550. $this->Form->create($entity);
  8551. $this->Form->setValueSources(['query', 'context']);
  8552. $result = $this->Form->getSourceValue('category');
  8553. $this->assertEquals('sesame-cookies', $result);
  8554. $this->Form->setValueSources(['context', 'query']);
  8555. $result = $this->Form->getSourceValue('category');
  8556. $this->assertEquals('sesame-cookies', $result);
  8557. }
  8558. /**
  8559. * testNestedLabelInput method
  8560. *
  8561. * Test the `nestedInput` parameter
  8562. *
  8563. * @group deprecated
  8564. * @return void
  8565. */
  8566. public function testNestedLabelInput()
  8567. {
  8568. $this->deprecated(function () {
  8569. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8570. $expected = [
  8571. 'div' => ['class' => 'input text'],
  8572. 'label' => ['for' => 'foo'],
  8573. ['input' => [
  8574. 'type' => 'text',
  8575. 'name' => 'foo',
  8576. 'id' => 'foo'
  8577. ]],
  8578. 'Foo',
  8579. '/label',
  8580. '/div'
  8581. ];
  8582. $this->assertHtml($expected, $result);
  8583. });
  8584. }
  8585. /**
  8586. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8587. *
  8588. * This test makes sure `false` excludes the label from the render
  8589. *
  8590. * @return void
  8591. */
  8592. public function testControlLabelManipulationDisableLabels()
  8593. {
  8594. $result = $this->Form->control('test', [
  8595. 'type' => 'radio',
  8596. 'options' => ['A', 'B'],
  8597. 'labelOptions' => false
  8598. ]);
  8599. $expected = [
  8600. ['div' => ['class' => 'input radio']],
  8601. '<label',
  8602. 'Test',
  8603. '/label',
  8604. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8605. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8606. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8607. '/div',
  8608. ];
  8609. $this->assertHtml($expected, $result);
  8610. $result = $this->Form->control('checkbox1', [
  8611. 'label' => 'My checkboxes',
  8612. 'multiple' => 'checkbox',
  8613. 'type' => 'select',
  8614. 'options' => [
  8615. ['text' => 'First Checkbox', 'value' => 1],
  8616. ['text' => 'Second Checkbox', 'value' => 2]
  8617. ],
  8618. 'labelOptions' => false
  8619. ]);
  8620. $expected = [
  8621. ['div' => ['class' => 'input select']],
  8622. ['label' => ['for' => 'checkbox1']],
  8623. 'My checkboxes',
  8624. '/label',
  8625. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8626. ['div' => ['class' => 'checkbox']],
  8627. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8628. '/div',
  8629. ['div' => ['class' => 'checkbox']],
  8630. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8631. '/div',
  8632. '/div'
  8633. ];
  8634. $this->assertHtml($expected, $result);
  8635. }
  8636. /**
  8637. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8638. *
  8639. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8640. * added to the class if checked.
  8641. *
  8642. * Also checks to make sure any custom attributes are rendered correctly
  8643. *
  8644. * @return void
  8645. */
  8646. public function testControlLabelManipulationRadios()
  8647. {
  8648. $result = $this->Form->control('test', [
  8649. 'type' => 'radio',
  8650. 'options' => ['A', 'B'],
  8651. 'labelOptions' => ['class' => 'custom-class']
  8652. ]);
  8653. $expected = [
  8654. ['div' => ['class' => 'input radio']],
  8655. '<label',
  8656. 'Test',
  8657. '/label',
  8658. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8659. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8660. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8661. 'A',
  8662. '/label',
  8663. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8664. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8665. 'B',
  8666. '/label',
  8667. '/div',
  8668. ];
  8669. $this->assertHtml($expected, $result);
  8670. $result = $this->Form->control('test', [
  8671. 'type' => 'radio',
  8672. 'options' => ['A', 'B'],
  8673. 'value' => 1,
  8674. 'labelOptions' => ['class' => 'custom-class']
  8675. ]);
  8676. $expected = [
  8677. ['div' => ['class' => 'input radio']],
  8678. '<label',
  8679. 'Test',
  8680. '/label',
  8681. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8682. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8683. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8684. 'A',
  8685. '/label',
  8686. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8687. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8688. 'B',
  8689. '/label',
  8690. '/div',
  8691. ];
  8692. $this->assertHtml($expected, $result);
  8693. $result = $this->Form->control('test', [
  8694. 'type' => 'radio',
  8695. 'options' => ['A', 'B'],
  8696. 'value' => 1,
  8697. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8698. ]);
  8699. $expected = [
  8700. ['div' => ['class' => 'input radio']],
  8701. '<label',
  8702. 'Test',
  8703. '/label',
  8704. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8705. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8706. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8707. 'A',
  8708. '/label',
  8709. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8710. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8711. 'B',
  8712. '/label',
  8713. '/div',
  8714. ];
  8715. $this->assertHtml($expected, $result);
  8716. $result = $this->Form->radio('test', ['A', 'B'], [
  8717. 'label' => [
  8718. 'class' => ['custom-class', 'another-class'],
  8719. 'data-name' => 'bob'
  8720. ],
  8721. 'value' => 1
  8722. ]);
  8723. $expected = [
  8724. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8725. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8726. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8727. 'A',
  8728. '/label',
  8729. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8730. ['input' => [
  8731. 'type' => 'radio',
  8732. 'name' => 'test',
  8733. 'value' => '1',
  8734. 'id' => 'test-1',
  8735. 'checked' => 'checked'
  8736. ]],
  8737. 'B',
  8738. '/label',
  8739. ];
  8740. $this->assertHtml($expected, $result);
  8741. }
  8742. /**
  8743. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8744. *
  8745. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8746. * added to the class if checked.
  8747. *
  8748. * Also checks to make sure any custom attributes are rendered correctly
  8749. *
  8750. * @return void
  8751. */
  8752. public function testControlLabelManipulationCheckboxes()
  8753. {
  8754. $result = $this->Form->control('checkbox1', [
  8755. 'label' => 'My checkboxes',
  8756. 'multiple' => 'checkbox',
  8757. 'type' => 'select',
  8758. 'options' => [
  8759. ['text' => 'First Checkbox', 'value' => 1],
  8760. ['text' => 'Second Checkbox', 'value' => 2]
  8761. ],
  8762. 'labelOptions' => ['class' => 'custom-class'],
  8763. 'value' => ['1']
  8764. ]);
  8765. $expected = [
  8766. ['div' => ['class' => 'input select']],
  8767. ['label' => ['for' => 'checkbox1']],
  8768. 'My checkboxes',
  8769. '/label',
  8770. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8771. ['div' => ['class' => 'checkbox']],
  8772. ['label' => [
  8773. 'class' => 'custom-class selected',
  8774. 'for' => 'checkbox1-1'
  8775. ]],
  8776. ['input' => [
  8777. 'type' => 'checkbox',
  8778. 'name' => 'checkbox1[]',
  8779. 'value' => '1',
  8780. 'id' => 'checkbox1-1',
  8781. 'checked' => 'checked'
  8782. ]],
  8783. 'First Checkbox',
  8784. '/label',
  8785. '/div',
  8786. ['div' => ['class' => 'checkbox']],
  8787. ['label' => [
  8788. 'class' => 'custom-class',
  8789. 'for' => 'checkbox1-2'
  8790. ]],
  8791. ['input' => [
  8792. 'type' => 'checkbox',
  8793. 'name' => 'checkbox1[]',
  8794. 'value' => '2',
  8795. 'id' => 'checkbox1-2'
  8796. ]],
  8797. 'Second Checkbox',
  8798. '/label',
  8799. '/div',
  8800. '/div'
  8801. ];
  8802. $this->assertHtml($expected, $result);
  8803. $result = $this->Form->control('checkbox1', [
  8804. 'label' => 'My checkboxes',
  8805. 'multiple' => 'checkbox',
  8806. 'type' => 'select',
  8807. 'options' => [
  8808. ['text' => 'First Checkbox', 'value' => 1],
  8809. ['text' => 'Second Checkbox', 'value' => 2]
  8810. ],
  8811. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8812. 'value' => ['1']
  8813. ]);
  8814. $expected = [
  8815. ['div' => ['class' => 'input select']],
  8816. ['label' => ['for' => 'checkbox1']],
  8817. 'My checkboxes',
  8818. '/label',
  8819. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8820. ['div' => ['class' => 'checkbox']],
  8821. ['label' => [
  8822. 'class' => 'custom-class another-class selected',
  8823. 'data-name' => 'bob',
  8824. 'for' => 'checkbox1-1'
  8825. ]],
  8826. ['input' => [
  8827. 'type' => 'checkbox',
  8828. 'name' => 'checkbox1[]',
  8829. 'value' => '1',
  8830. 'id' => 'checkbox1-1',
  8831. 'checked' => 'checked'
  8832. ]],
  8833. 'First Checkbox',
  8834. '/label',
  8835. '/div',
  8836. ['div' => ['class' => 'checkbox']],
  8837. ['label' => [
  8838. 'class' => 'custom-class another-class',
  8839. 'data-name' => 'bob',
  8840. 'for' => 'checkbox1-2'
  8841. ]],
  8842. ['input' => [
  8843. 'type' => 'checkbox',
  8844. 'name' => 'checkbox1[]',
  8845. 'value' => '2',
  8846. 'id' => 'checkbox1-2'
  8847. ]],
  8848. 'Second Checkbox',
  8849. '/label',
  8850. '/div',
  8851. '/div'
  8852. ];
  8853. $this->assertHtml($expected, $result);
  8854. }
  8855. /**
  8856. * testControlMaxLengthArrayContext method
  8857. *
  8858. * Test control() with maxlength attribute in Array Context.
  8859. *
  8860. * @return void
  8861. */
  8862. public function testControlMaxLengthArrayContext()
  8863. {
  8864. $this->article['schema'] = [
  8865. 'title' => ['length' => 10]
  8866. ];
  8867. $this->Form->create($this->article);
  8868. $result = $this->Form->control('title');
  8869. $expected = [
  8870. 'div' => ['class'],
  8871. 'label' => ['for'],
  8872. 'Title',
  8873. '/label',
  8874. 'input' => [
  8875. 'id',
  8876. 'name' => 'title',
  8877. 'type' => 'text',
  8878. 'required' => 'required',
  8879. 'maxlength' => 10,
  8880. ],
  8881. '/div',
  8882. ];
  8883. $this->assertHtml($expected, $result);
  8884. }
  8885. /**
  8886. * testControlMaxLengthEntityContext method
  8887. *
  8888. * Test control() with maxlength attribute in Entity Context.
  8889. *
  8890. * @return void
  8891. */
  8892. public function testControlMaxLengthEntityContext()
  8893. {
  8894. $this->article['schema']['title']['length'] = 45;
  8895. $validator = new Validator();
  8896. $validator->maxLength('title', 10);
  8897. $article = new EntityContext(
  8898. new ServerRequest(),
  8899. [
  8900. 'entity' => new Entity($this->article),
  8901. 'table' => new Table([
  8902. 'schema' => $this->article['schema'],
  8903. 'validator' => $validator
  8904. ])
  8905. ]
  8906. );
  8907. $this->Form->create($article);
  8908. $result = $this->Form->control('title');
  8909. $expected = [
  8910. 'div' => ['class'],
  8911. 'label' => ['for'],
  8912. 'Title',
  8913. '/label',
  8914. 'input' => [
  8915. 'id',
  8916. 'name' => 'title',
  8917. 'type' => 'text',
  8918. 'required' => 'required',
  8919. 'maxlength' => 10,
  8920. ],
  8921. '/div',
  8922. ];
  8923. $this->assertHtml($expected, $result);
  8924. $this->article['schema']['title']['length'] = 45;
  8925. $validator = new Validator();
  8926. $validator->maxLength('title', 55);
  8927. $article = new EntityContext(
  8928. new ServerRequest(),
  8929. [
  8930. 'entity' => new Entity($this->article),
  8931. 'table' => new Table([
  8932. 'schema' => $this->article['schema'],
  8933. 'validator' => $validator
  8934. ])
  8935. ]
  8936. );
  8937. $this->Form->create($article);
  8938. $result = $this->Form->control('title');
  8939. $expected = [
  8940. 'div' => ['class'],
  8941. 'label' => ['for'],
  8942. 'Title',
  8943. '/label',
  8944. 'input' => [
  8945. 'id',
  8946. 'name' => 'title',
  8947. 'type' => 'text',
  8948. 'required' => 'required',
  8949. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8950. ],
  8951. '/div',
  8952. ];
  8953. $this->assertHtml($expected, $result);
  8954. $this->article['schema']['title']['length'] = 45;
  8955. $validator = new Validator();
  8956. $validator->maxLength('title', 55);
  8957. $article = new EntityContext(
  8958. new ServerRequest(),
  8959. [
  8960. 'entity' => new Entity($this->article),
  8961. 'table' => new Table([
  8962. 'schema' => $this->article['schema'],
  8963. 'validator' => $validator
  8964. ])
  8965. ]
  8966. );
  8967. $this->Form->create($article);
  8968. $result = $this->Form->control('title', ['maxlength' => 10]);
  8969. $expected = [
  8970. 'div' => ['class'],
  8971. 'label' => ['for'],
  8972. 'Title',
  8973. '/label',
  8974. 'input' => [
  8975. 'id',
  8976. 'name' => 'title',
  8977. 'type' => 'text',
  8978. 'required' => 'required',
  8979. 'maxlength' => 10, // Length set in options should take highest precedence.
  8980. ],
  8981. '/div',
  8982. ];
  8983. $this->assertHtml($expected, $result);
  8984. }
  8985. /**
  8986. * testControlMinMaxLengthEntityContext method
  8987. *
  8988. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8989. *
  8990. * @return void
  8991. */
  8992. public function testControlMinMaxLengthEntityContext()
  8993. {
  8994. $validator = new Validator();
  8995. $validator->maxLength('title', 10);
  8996. $article = new EntityContext(
  8997. new ServerRequest(),
  8998. [
  8999. 'entity' => new Entity($this->article),
  9000. 'table' => new Table([
  9001. 'schema' => $this->article['schema'],
  9002. 'validator' => $validator
  9003. ])
  9004. ]
  9005. );
  9006. $this->Form->create($article);
  9007. $result = $this->Form->control('title');
  9008. $expected = [
  9009. 'div' => ['class'],
  9010. 'label' => ['for'],
  9011. 'Title',
  9012. '/label',
  9013. 'input' => [
  9014. 'id',
  9015. 'name' => 'title',
  9016. 'type' => 'text',
  9017. 'required' => 'required',
  9018. 'maxlength' => 10,
  9019. ],
  9020. '/div',
  9021. ];
  9022. $this->assertHtml($expected, $result);
  9023. }
  9024. /**
  9025. * testControlMaxLengthFormContext method
  9026. *
  9027. * Test control() with maxlength attribute in Form Context.
  9028. *
  9029. * @return void
  9030. */
  9031. public function testControlMaxLengthFormContext()
  9032. {
  9033. $validator = new Validator();
  9034. $validator->maxLength('title', 10);
  9035. $form = new Form();
  9036. $form->setValidator('default', $validator);
  9037. $this->Form->create($form);
  9038. $result = $this->Form->control('title');
  9039. $expected = [
  9040. 'div' => ['class'],
  9041. 'label' => ['for'],
  9042. 'Title',
  9043. '/label',
  9044. 'input' => [
  9045. 'id',
  9046. 'name' => 'title',
  9047. 'type' => 'text',
  9048. 'required' => 'required',
  9049. 'maxlength' => 10,
  9050. ],
  9051. '/div',
  9052. ];
  9053. $this->assertHtml($expected, $result);
  9054. }
  9055. }