FormHelperTest.php 325 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823
  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. }
  185. /**
  186. * Test construct() with the templates option.
  187. *
  188. * @return void
  189. */
  190. public function testConstructTemplatesFile()
  191. {
  192. $helper = new FormHelper($this->View, [
  193. 'templates' => 'htmlhelper_tags',
  194. ]);
  195. $result = $helper->control('name');
  196. $this->assertContains('<input', $result);
  197. }
  198. /**
  199. * Test that when specifying custom widgets the config array for that widget
  200. * is overwritten instead of merged.
  201. *
  202. * @return void
  203. */
  204. public function testConstructWithWidgets()
  205. {
  206. $config = [
  207. 'widgets' => [
  208. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  209. ],
  210. ];
  211. $helper = new FormHelper($this->View, $config);
  212. $locator = $helper->getWidgetLocator();
  213. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  214. }
  215. /**
  216. * Test that when specifying custom widgets config file and it should be
  217. * added to widgets array. WidgetLocator will load widgets in constructor.
  218. *
  219. * @return void
  220. */
  221. public function testConstructWithWidgetsConfig()
  222. {
  223. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  224. $locator = $helper->getWidgetLocator();
  225. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  226. }
  227. /**
  228. * Test setting the widget locator
  229. *
  230. * @return void
  231. */
  232. public function testSetAndGetWidgetLocator()
  233. {
  234. $helper = new FormHelper($this->View);
  235. $locator = new WidgetLocator($helper->templater(), $this->View);
  236. $helper->setWidgetLocator($locator);
  237. $this->assertSame($locator, $helper->getWidgetLocator());
  238. }
  239. /**
  240. * Test overridding grouped input types which controls generation of "for"
  241. * attribute of labels.
  242. *
  243. * @return void
  244. */
  245. public function testConstructWithGroupedInputTypes()
  246. {
  247. $helper = new FormHelper($this->View, [
  248. 'groupedInputTypes' => ['radio'],
  249. ]);
  250. $result = $helper->control('when', ['type' => 'datetime']);
  251. $this->assertContains('<label for="when">When</label>', $result);
  252. }
  253. /**
  254. * Test registering a new widget class and rendering it.
  255. *
  256. * @return void
  257. */
  258. public function testAddWidgetAndRenderWidget()
  259. {
  260. $data = [
  261. 'val' => 1,
  262. ];
  263. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  264. $this->Form->addWidget('test', $mock);
  265. $mock->expects($this->once())
  266. ->method('render')
  267. ->with($data)
  268. ->will($this->returnValue('HTML'));
  269. $result = $this->Form->widget('test', $data);
  270. $this->assertEquals('HTML', $result);
  271. }
  272. /**
  273. * Test that secureFields() of widget is called after calling render(),
  274. * not before.
  275. *
  276. * @return void
  277. */
  278. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  279. {
  280. $data = [
  281. 'val' => 1,
  282. 'name' => 'test',
  283. ];
  284. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  285. $this->Form->addWidget('test', $mock);
  286. $mock->expects($this->at(0))
  287. ->method('render')
  288. ->with($data)
  289. ->will($this->returnValue('HTML'));
  290. $mock->expects($this->at(1))
  291. ->method('secureFields')
  292. ->with($data)
  293. ->will($this->returnValue(['test']));
  294. $result = $this->Form->widget('test', $data + ['secure' => true]);
  295. $this->assertEquals('HTML', $result);
  296. }
  297. /**
  298. * Test that empty string is not added to secure fields list when
  299. * rendering input widget without name.
  300. *
  301. * @return void
  302. */
  303. public function testRenderingWidgetWithEmptyName()
  304. {
  305. $this->assertEquals([], $this->Form->fields);
  306. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  307. $this->assertEquals('<select name=""></select>', $result);
  308. $this->assertEquals([], $this->Form->fields);
  309. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  310. $this->assertEquals('<select name="0"></select>', $result);
  311. $this->assertEquals(['0'], $this->Form->fields);
  312. }
  313. /**
  314. * Test registering an invalid widget class.
  315. *
  316. * @return void
  317. */
  318. public function testAddWidgetInvalid()
  319. {
  320. $this->expectException(\RuntimeException::class);
  321. $mock = new \StdClass();
  322. $this->Form->addWidget('test', $mock);
  323. $this->Form->widget('test');
  324. }
  325. /**
  326. * Test adding a new context class.
  327. *
  328. * @return void
  329. */
  330. public function testAddContextProvider()
  331. {
  332. $context = 'My data';
  333. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  334. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  335. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  336. $this->assertEquals($context, $data['entity']);
  337. return $stub;
  338. });
  339. $this->Form->create($context);
  340. $result = $this->Form->context();
  341. $this->assertSame($stub, $result);
  342. }
  343. /**
  344. * Test replacing a context class.
  345. *
  346. * @return void
  347. */
  348. public function testAddContextProviderReplace()
  349. {
  350. $entity = new Article();
  351. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  352. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  353. return $stub;
  354. });
  355. $this->Form->create($entity);
  356. $result = $this->Form->context();
  357. $this->assertSame($stub, $result);
  358. }
  359. /**
  360. * Test overriding a context class.
  361. *
  362. * @return void
  363. */
  364. public function testAddContextProviderAdd()
  365. {
  366. $entity = new Article();
  367. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  368. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  369. if ($data['entity'] instanceof Entity) {
  370. return $stub;
  371. }
  372. });
  373. $this->Form->create($entity);
  374. $result = $this->Form->context();
  375. $this->assertSame($stub, $result);
  376. }
  377. /**
  378. * Test adding an invalid context class.
  379. *
  380. * @return void
  381. */
  382. public function testAddContextProviderInvalid()
  383. {
  384. $this->expectException(\RuntimeException::class);
  385. $this->expectExceptionMessage('Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.');
  386. $context = 'My data';
  387. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  388. return new \StdClass();
  389. });
  390. $this->Form->create($context);
  391. }
  392. /**
  393. * Provides context options for create().
  394. *
  395. * @return array
  396. */
  397. public function contextSelectionProvider()
  398. {
  399. $entity = new Article();
  400. $collection = new Collection([$entity]);
  401. $emptyCollection = new Collection([]);
  402. $arrayObject = new \ArrayObject([]);
  403. $data = [
  404. 'schema' => [
  405. 'title' => ['type' => 'string'],
  406. ],
  407. ];
  408. $form = new Form();
  409. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  410. return [
  411. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  412. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  413. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  414. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  415. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  416. 'form' => [$form, 'Cake\View\Form\FormContext'],
  417. 'none' => [null, 'Cake\View\Form\NullContext'],
  418. 'false' => [false, 'Cake\View\Form\NullContext'],
  419. 'custom' => [$custom, get_class($custom)],
  420. ];
  421. }
  422. /**
  423. * Test default context selection in create()
  424. *
  425. * @dataProvider contextSelectionProvider
  426. * @return void
  427. */
  428. public function testCreateContextSelectionBuiltIn($data, $class)
  429. {
  430. $this->loadFixtures('Articles');
  431. $this->Form->create($data);
  432. $this->assertInstanceOf($class, $this->Form->context());
  433. }
  434. /**
  435. * Data provider for type option.
  436. *
  437. * @return array
  438. */
  439. public static function requestTypeProvider()
  440. {
  441. return [
  442. // type, method, override
  443. ['post', 'post', 'POST'],
  444. ['put', 'post', 'PUT'],
  445. ['patch', 'post', 'PATCH'],
  446. ['delete', 'post', 'DELETE'],
  447. ];
  448. }
  449. /**
  450. * Test creating file forms.
  451. *
  452. * @return void
  453. */
  454. public function testCreateFile()
  455. {
  456. $encoding = strtolower(Configure::read('App.encoding'));
  457. $result = $this->Form->create(false, ['type' => 'file']);
  458. $expected = [
  459. 'form' => [
  460. 'method' => 'post', 'action' => '/articles/add',
  461. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  462. ],
  463. 'div' => ['style' => 'display:none;'],
  464. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  465. '/div',
  466. ];
  467. $this->assertHtml($expected, $result);
  468. }
  469. /**
  470. * Test creating GET forms.
  471. *
  472. * @return void
  473. */
  474. public function testCreateGet()
  475. {
  476. $encoding = strtolower(Configure::read('App.encoding'));
  477. $result = $this->Form->create(false, ['type' => 'get']);
  478. $expected = ['form' => [
  479. 'method' => 'get', 'action' => '/articles/add',
  480. 'accept-charset' => $encoding,
  481. ]];
  482. $this->assertHtml($expected, $result);
  483. }
  484. /**
  485. * Test explicit method/enctype options.
  486. *
  487. * Explicit method overwrites inferred method from 'type'
  488. *
  489. * @return void
  490. */
  491. public function testCreateExplicitMethodEnctype()
  492. {
  493. $encoding = strtolower(Configure::read('App.encoding'));
  494. $result = $this->Form->create(false, [
  495. 'type' => 'get',
  496. 'method' => 'put',
  497. 'enctype' => 'multipart/form-data',
  498. ]);
  499. $expected = ['form' => [
  500. 'method' => 'put',
  501. 'action' => '/articles/add',
  502. 'enctype' => 'multipart/form-data',
  503. 'accept-charset' => $encoding,
  504. ]];
  505. $this->assertHtml($expected, $result);
  506. }
  507. /**
  508. * Test create() with the templates option.
  509. *
  510. * @return void
  511. */
  512. public function testCreateTemplatesArray()
  513. {
  514. $result = $this->Form->create($this->article, [
  515. 'templates' => [
  516. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  517. ],
  518. ]);
  519. $expected = [
  520. 'form' => [
  521. 'class' => 'form-horizontal',
  522. 'method' => 'post',
  523. 'action' => '/articles/add',
  524. 'accept-charset' => 'utf-8',
  525. ],
  526. ];
  527. $this->assertHtml($expected, $result);
  528. }
  529. /**
  530. * Test create() with the templates option.
  531. *
  532. * @return void
  533. */
  534. public function testCreateTemplatesFile()
  535. {
  536. $result = $this->Form->create($this->article, [
  537. 'templates' => 'htmlhelper_tags',
  538. ]);
  539. $expected = [
  540. 'start form',
  541. 'div' => ['class' => 'hidden'],
  542. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  543. '/div',
  544. ];
  545. $this->assertHtml($expected, $result);
  546. }
  547. /**
  548. * Test that create() and end() restore templates.
  549. *
  550. * @return void
  551. */
  552. public function testCreateEndRestoreTemplates()
  553. {
  554. $this->Form->create($this->article, [
  555. 'templates' => ['input' => 'custom input element'],
  556. ]);
  557. $this->Form->end();
  558. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  559. }
  560. /**
  561. * Test using template vars in various templates used by control() method.
  562. *
  563. * @return void
  564. */
  565. public function testControlTemplateVars()
  566. {
  567. $result = $this->Form->control('text', [
  568. 'templates' => [
  569. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  570. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  571. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  572. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  573. ],
  574. 'templateVars' => [
  575. 'forinput' => 'in-input',
  576. 'forlabel' => 'in-label',
  577. 'forgroup' => 'in-group',
  578. 'forcontainer' => 'in-container',
  579. ],
  580. ]);
  581. $expected = [
  582. 'div' => ['class'],
  583. 'label' => ['for'],
  584. 'Text in-label',
  585. '/label',
  586. 'in-group',
  587. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  588. 'in-container',
  589. '/div',
  590. ];
  591. $this->assertHtml($expected, $result);
  592. }
  593. /**
  594. * Test ensuring template variables work in template files loaded
  595. * during control().
  596. *
  597. * @return void
  598. */
  599. public function testControlTemplatesFromFile()
  600. {
  601. $result = $this->Form->control('title', [
  602. 'templates' => 'test_templates',
  603. 'templateVars' => [
  604. 'forcontainer' => 'container-data',
  605. ],
  606. ]);
  607. $expected = [
  608. 'div' => ['class'],
  609. 'label' => ['for'],
  610. 'Title',
  611. '/label',
  612. 'input' => ['name', 'type' => 'text', 'id'],
  613. 'container-data',
  614. '/div',
  615. ];
  616. $this->assertHtml($expected, $result);
  617. }
  618. /**
  619. * Test using template vars in inputSubmit and submitContainer template.
  620. *
  621. * @return void
  622. */
  623. public function testSubmitTemplateVars()
  624. {
  625. $this->Form->setTemplates([
  626. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  627. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  628. ]);
  629. $result = $this->Form->submit('Submit', [
  630. 'templateVars' => [
  631. 'forinput' => 'in-input',
  632. 'forcontainer' => 'in-container',
  633. ],
  634. ]);
  635. $expected = [
  636. 'div' => ['class'],
  637. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  638. 'in-container',
  639. '/div',
  640. ];
  641. $this->assertHtml($expected, $result);
  642. }
  643. /**
  644. * test the create() method
  645. *
  646. * @dataProvider requestTypeProvider
  647. * @return void
  648. */
  649. public function testCreateTypeOptions($type, $method, $override)
  650. {
  651. $encoding = strtolower(Configure::read('App.encoding'));
  652. $result = $this->Form->create(false, ['type' => $type]);
  653. $expected = [
  654. 'form' => [
  655. 'method' => $method, 'action' => '/articles/add',
  656. 'accept-charset' => $encoding,
  657. ],
  658. 'div' => ['style' => 'display:none;'],
  659. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  660. '/div',
  661. ];
  662. $this->assertHtml($expected, $result);
  663. }
  664. /**
  665. * Test using template vars in Create (formStart template)
  666. *
  667. * @return void
  668. */
  669. public function testCreateTemplateVars()
  670. {
  671. $result = $this->Form->create($this->article, [
  672. 'templates' => [
  673. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  674. ],
  675. 'templateVars' => ['header' => 'headertext'],
  676. ]);
  677. $expected = [
  678. 'h4' => ['class'],
  679. 'headertext',
  680. '/h4',
  681. 'form' => [
  682. 'method' => 'post',
  683. 'action' => '/articles/add',
  684. 'accept-charset' => 'utf-8',
  685. ],
  686. ];
  687. $this->assertHtml($expected, $result);
  688. }
  689. /**
  690. * Test opening a form for an update operation.
  691. *
  692. * @return void
  693. */
  694. public function testCreateUpdateForm()
  695. {
  696. $encoding = strtolower(Configure::read('App.encoding'));
  697. $this->View->setRequest($this->View->getRequest()
  698. ->withRequestTarget('/articles/edit/1')
  699. ->withParam('action', 'edit'));
  700. $this->article['defaults']['id'] = 1;
  701. $result = $this->Form->create($this->article);
  702. $expected = [
  703. 'form' => [
  704. 'method' => 'post',
  705. 'action' => '/articles/edit/1',
  706. 'accept-charset' => $encoding,
  707. ],
  708. 'div' => ['style' => 'display:none;'],
  709. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  710. '/div',
  711. ];
  712. $this->assertHtml($expected, $result);
  713. }
  714. /**
  715. * test create() with automatic url generation
  716. *
  717. * @return void
  718. */
  719. public function testCreateAutoUrl()
  720. {
  721. $encoding = strtolower(Configure::read('App.encoding'));
  722. $this->View->setRequest($this->View->getRequest()
  723. ->withRequestTarget('/articles/delete/10')
  724. ->withParam('action', 'delete'));
  725. $result = $this->Form->create($this->article);
  726. $expected = [
  727. 'form' => [
  728. 'method' => 'post', 'action' => '/articles/delete/10',
  729. 'accept-charset' => $encoding,
  730. ],
  731. 'div' => ['style' => 'display:none;'],
  732. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  733. '/div',
  734. ];
  735. $this->assertHtml($expected, $result);
  736. $this->article['defaults'] = ['id' => 1];
  737. $this->View->setRequest($this->View->getRequest()
  738. ->withRequestTarget('/articles/edit/1')
  739. ->withParam('action', 'delete'));
  740. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  741. $expected = [
  742. 'form' => [
  743. 'method' => 'post',
  744. 'action' => '/articles/edit/1',
  745. 'accept-charset' => $encoding,
  746. ],
  747. 'div' => ['style' => 'display:none;'],
  748. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  749. '/div',
  750. ];
  751. $this->assertHtml($expected, $result);
  752. $this->View->setRequest($this->View->getRequest()
  753. ->withParam('action', 'add'));
  754. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  755. $expected = [
  756. 'form' => [
  757. 'method' => 'post',
  758. 'action' => '/articles/publish/1',
  759. 'accept-charset' => $encoding,
  760. ],
  761. 'div' => ['style' => 'display:none;'],
  762. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  763. '/div',
  764. ];
  765. $this->assertHtml($expected, $result);
  766. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  767. $expected = [
  768. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  769. 'div' => ['style' => 'display:none;'],
  770. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  771. '/div',
  772. ];
  773. $this->assertHtml($expected, $result);
  774. $this->View->setRequest($this->View->getRequest()
  775. ->withParam('controller', 'Pages'));
  776. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  777. $expected = [
  778. 'form' => [
  779. 'method' => 'post', 'action' => '/Pages/signup/1',
  780. 'accept-charset' => $encoding,
  781. ],
  782. 'div' => ['style' => 'display:none;'],
  783. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  784. '/div',
  785. ];
  786. $this->assertHtml($expected, $result);
  787. }
  788. /**
  789. * Test create() with no URL (no "action" attribute for <form> tag)
  790. *
  791. * @return void
  792. */
  793. public function testCreateNoUrl()
  794. {
  795. $result = $this->Form->create(false, ['url' => false]);
  796. $expected = [
  797. 'form' => [
  798. 'method' => 'post',
  799. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  800. ],
  801. 'div' => ['style' => 'display:none;'],
  802. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  803. '/div',
  804. ];
  805. $this->assertHtml($expected, $result);
  806. }
  807. /**
  808. * test create() with a custom route
  809. *
  810. * @return void
  811. */
  812. public function testCreateCustomRoute()
  813. {
  814. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  815. $encoding = strtolower(Configure::read('App.encoding'));
  816. $this->View->setRequest($this->View->getRequest()
  817. ->withParam('controller', 'users'));
  818. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  819. $expected = [
  820. 'form' => [
  821. 'method' => 'post', 'action' => '/login',
  822. 'accept-charset' => $encoding,
  823. ],
  824. 'div' => ['style' => 'display:none;'],
  825. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  826. '/div',
  827. ];
  828. $this->assertHtml($expected, $result);
  829. Router::connect(
  830. '/new-article',
  831. ['controller' => 'articles', 'action' => 'myaction'],
  832. ['_name' => 'my-route']
  833. );
  834. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  835. $expected = [
  836. 'form' => [
  837. 'method' => 'post', 'action' => '/new-article',
  838. 'accept-charset' => $encoding,
  839. ],
  840. 'div' => ['style' => 'display:none;'],
  841. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  842. '/div',
  843. ];
  844. $this->assertHtml($expected, $result);
  845. }
  846. /**
  847. * test automatic accept-charset overriding
  848. *
  849. * @return void
  850. */
  851. public function testCreateWithAcceptCharset()
  852. {
  853. $result = $this->Form->create(
  854. $this->article,
  855. [
  856. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  857. ]
  858. );
  859. $expected = [
  860. 'form' => [
  861. 'method' => 'post', 'action' => '/articles',
  862. 'accept-charset' => 'iso-8859-1',
  863. ],
  864. 'div' => ['style' => 'display:none;'],
  865. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  866. '/div',
  867. ];
  868. $this->assertHtml($expected, $result);
  869. }
  870. /**
  871. * Test base form URL when url param is passed with multiple parameters (&)
  872. *
  873. */
  874. public function testCreateQueryStringRequest()
  875. {
  876. $encoding = strtolower(Configure::read('App.encoding'));
  877. $result = $this->Form->create($this->article, [
  878. 'type' => 'post',
  879. 'escape' => false,
  880. 'url' => [
  881. 'controller' => 'controller',
  882. 'action' => 'action',
  883. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  884. ],
  885. ]);
  886. $expected = [
  887. 'form' => [
  888. 'method' => 'post',
  889. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  890. 'accept-charset' => $encoding,
  891. ],
  892. 'div' => ['style' => 'display:none;'],
  893. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  894. '/div',
  895. ];
  896. $this->assertHtml($expected, $result);
  897. $result = $this->Form->create($this->article, [
  898. 'type' => 'post',
  899. 'url' => [
  900. 'controller' => 'controller',
  901. 'action' => 'action',
  902. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  903. ],
  904. ]);
  905. $this->assertHtml($expected, $result);
  906. }
  907. /**
  908. * test that create() doesn't cause errors by multiple id's being in the primary key
  909. * as could happen with multiple select or checkboxes.
  910. *
  911. * @return void
  912. */
  913. public function testCreateWithMultipleIdInData()
  914. {
  915. $encoding = strtolower(Configure::read('App.encoding'));
  916. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  917. $result = $this->Form->create($this->article);
  918. $expected = [
  919. 'form' => [
  920. 'method' => 'post',
  921. 'action' => '/articles/add',
  922. 'accept-charset' => $encoding,
  923. ],
  924. 'div' => ['style' => 'display:none;'],
  925. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  926. '/div',
  927. ];
  928. $this->assertHtml($expected, $result);
  929. }
  930. /**
  931. * test that create() doesn't add in extra passed params.
  932. *
  933. * @return void
  934. */
  935. public function testCreatePassedArgs()
  936. {
  937. $encoding = strtolower(Configure::read('App.encoding'));
  938. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  939. $result = $this->Form->create($this->article, [
  940. 'type' => 'post',
  941. 'escape' => false,
  942. 'url' => [
  943. 'action' => 'edit',
  944. 'myparam',
  945. ],
  946. ]);
  947. $expected = [
  948. 'form' => [
  949. 'method' => 'post',
  950. 'action' => '/articles/edit/myparam',
  951. 'accept-charset' => $encoding,
  952. ],
  953. 'div' => ['style' => 'display:none;'],
  954. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  955. '/div',
  956. ];
  957. $this->assertHtml($expected, $result);
  958. }
  959. /**
  960. * test creating a get form, and get form inputs.
  961. *
  962. * @return void
  963. */
  964. public function testGetFormCreate()
  965. {
  966. $encoding = strtolower(Configure::read('App.encoding'));
  967. $result = $this->Form->create($this->article, ['type' => 'get']);
  968. $expected = ['form' => [
  969. 'method' => 'get', 'action' => '/articles/add',
  970. 'accept-charset' => $encoding,
  971. ]];
  972. $this->assertHtml($expected, $result);
  973. $result = $this->Form->text('title');
  974. $expected = ['input' => [
  975. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  976. ]];
  977. $this->assertHtml($expected, $result);
  978. $result = $this->Form->password('password');
  979. $expected = ['input' => [
  980. 'name' => 'password', 'type' => 'password',
  981. ]];
  982. $this->assertHtml($expected, $result);
  983. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  984. $result = $this->Form->text('user_form');
  985. $expected = ['input' => [
  986. 'name' => 'user_form', 'type' => 'text',
  987. ]];
  988. $this->assertHtml($expected, $result);
  989. }
  990. /**
  991. * test get form, and inputs when the model param is false
  992. *
  993. * @return void
  994. */
  995. public function testGetFormWithFalseModel()
  996. {
  997. $encoding = strtolower(Configure::read('App.encoding'));
  998. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'contact_test'));
  999. $result = $this->Form->create(false, [
  1000. 'type' => 'get', 'url' => ['controller' => 'contact_test'],
  1001. ]);
  1002. $expected = ['form' => [
  1003. 'method' => 'get', 'action' => '/contact_test/add',
  1004. 'accept-charset' => $encoding,
  1005. ]];
  1006. $this->assertHtml($expected, $result);
  1007. $result = $this->Form->text('reason');
  1008. $expected = [
  1009. 'input' => ['type' => 'text', 'name' => 'reason'],
  1010. ];
  1011. $this->assertHtml($expected, $result);
  1012. }
  1013. /**
  1014. * testFormCreateWithSecurity method
  1015. *
  1016. * Test form->create() with security key.
  1017. *
  1018. * @return void
  1019. */
  1020. public function testCreateWithSecurity()
  1021. {
  1022. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1023. $encoding = strtolower(Configure::read('App.encoding'));
  1024. $result = $this->Form->create($this->article, [
  1025. 'url' => '/articles/publish',
  1026. ]);
  1027. $expected = [
  1028. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  1029. 'div' => ['style' => 'display:none;'],
  1030. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1031. ['input' => [
  1032. 'type' => 'hidden',
  1033. 'name' => '_csrfToken',
  1034. 'value' => 'testKey',
  1035. 'autocomplete' => 'off',
  1036. ]],
  1037. '/div',
  1038. ];
  1039. $this->assertHtml($expected, $result);
  1040. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1041. $expected['form']['id'] = 'MyForm';
  1042. $this->assertHtml($expected, $result);
  1043. }
  1044. /**
  1045. * testFormCreateGetNoSecurity method
  1046. *
  1047. * Test form->create() with no security key as its a get form
  1048. *
  1049. * @return void
  1050. */
  1051. public function testCreateEndGetNoSecurity()
  1052. {
  1053. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1054. $article = new Article();
  1055. $result = $this->Form->create($article, [
  1056. 'type' => 'get',
  1057. 'url' => '/contacts/add',
  1058. ]);
  1059. $this->assertNotContains('testKey', $result);
  1060. $result = $this->Form->end();
  1061. $this->assertNotContains('testKey', $result);
  1062. }
  1063. /**
  1064. * test that create() clears the fields property so it starts fresh
  1065. *
  1066. * @return void
  1067. */
  1068. public function testCreateClearingFields()
  1069. {
  1070. $this->Form->fields = ['model_id'];
  1071. $this->Form->create($this->article);
  1072. $this->assertEquals([], $this->Form->fields);
  1073. }
  1074. /**
  1075. * Tests form hash generation with model-less data
  1076. *
  1077. * @return void
  1078. */
  1079. public function testValidateHashNoModel()
  1080. {
  1081. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1082. $fields = ['anything'];
  1083. $result = $this->Form->secure($fields);
  1084. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1085. $this->assertContains($hash, $result);
  1086. }
  1087. /**
  1088. * Tests that hidden fields generated for checkboxes don't get locked
  1089. *
  1090. * @return void
  1091. */
  1092. public function testNoCheckboxLocking()
  1093. {
  1094. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1095. $this->assertSame([], $this->Form->fields);
  1096. $this->Form->checkbox('check', ['value' => '1']);
  1097. $this->assertSame($this->Form->fields, ['check']);
  1098. }
  1099. /**
  1100. * testFormSecurityFields method
  1101. *
  1102. * Test generation of secure form hash generation.
  1103. *
  1104. * @return void
  1105. */
  1106. public function testFormSecurityFields()
  1107. {
  1108. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1109. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1110. $result = $this->Form->secure($fields);
  1111. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1112. $hash .= ':' . 'Model.valid';
  1113. $hash = urlencode($hash);
  1114. $tokenDebug = urlencode(json_encode([
  1115. '',
  1116. $fields,
  1117. [],
  1118. ]));
  1119. $expected = [
  1120. 'div' => ['style' => 'display:none;'],
  1121. ['input' => [
  1122. 'type' => 'hidden',
  1123. 'name' => '_Token[fields]',
  1124. 'value' => $hash,
  1125. 'autocomplete' => 'off',
  1126. ]],
  1127. ['input' => [
  1128. 'type' => 'hidden',
  1129. 'name' => '_Token[unlocked]',
  1130. 'value' => '',
  1131. 'autocomplete' => 'off',
  1132. ]],
  1133. ['input' => [
  1134. 'type' => 'hidden',
  1135. 'name' => '_Token[debug]',
  1136. 'value' => $tokenDebug,
  1137. 'autocomplete' => 'off',
  1138. ]],
  1139. '/div',
  1140. ];
  1141. $this->assertHtml($expected, $result);
  1142. }
  1143. /**
  1144. * testFormSecurityFields method
  1145. *
  1146. * Test debug token is not generated if debug is false
  1147. *
  1148. * @return void
  1149. */
  1150. public function testFormSecurityFieldsNoDebugMode()
  1151. {
  1152. Configure::write('debug', false);
  1153. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1154. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1155. $result = $this->Form->secure($fields);
  1156. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1157. $hash .= ':' . 'Model.valid';
  1158. $hash = urlencode($hash);
  1159. $expected = [
  1160. 'div' => ['style' => 'display:none;'],
  1161. ['input' => [
  1162. 'type' => 'hidden',
  1163. 'name' => '_Token[fields]',
  1164. 'autocomplete' => 'off',
  1165. 'value' => $hash,
  1166. ]],
  1167. ['input' => [
  1168. 'type' => 'hidden',
  1169. 'name' => '_Token[unlocked]',
  1170. 'autocomplete' => 'off',
  1171. 'value' => '',
  1172. ]],
  1173. '/div',
  1174. ];
  1175. $this->assertHtml($expected, $result);
  1176. }
  1177. /**
  1178. * Tests correct generation of number fields for smallint
  1179. *
  1180. * @return void
  1181. */
  1182. public function testTextFieldGenerationForSmallint()
  1183. {
  1184. $this->article['schema'] = [
  1185. 'foo' => [
  1186. 'type' => 'smallinteger',
  1187. 'null' => false,
  1188. 'default' => null,
  1189. 'length' => 10,
  1190. ],
  1191. ];
  1192. $this->Form->create($this->article);
  1193. $result = $this->Form->control('foo');
  1194. $this->assertContains('class="input number"', $result);
  1195. $this->assertContains('type="number"', $result);
  1196. }
  1197. /**
  1198. * Tests correct generation of number fields for tinyint
  1199. *
  1200. * @return void
  1201. */
  1202. public function testTextFieldGenerationForTinyint()
  1203. {
  1204. $this->article['schema'] = [
  1205. 'foo' => [
  1206. 'type' => 'tinyinteger',
  1207. 'null' => false,
  1208. 'default' => null,
  1209. 'length' => 10,
  1210. ],
  1211. ];
  1212. $this->Form->create($this->article);
  1213. $result = $this->Form->control('foo');
  1214. $this->assertContains('class="input number"', $result);
  1215. $this->assertContains('type="number"', $result);
  1216. }
  1217. /**
  1218. * Tests correct generation of number fields for double and float fields
  1219. *
  1220. * @return void
  1221. */
  1222. public function testTextFieldGenerationForFloats()
  1223. {
  1224. $this->article['schema'] = [
  1225. 'foo' => [
  1226. 'type' => 'float',
  1227. 'null' => false,
  1228. 'default' => null,
  1229. 'length' => 10,
  1230. ],
  1231. ];
  1232. $this->Form->create($this->article);
  1233. $result = $this->Form->control('foo');
  1234. $expected = [
  1235. 'div' => ['class' => 'input number'],
  1236. 'label' => ['for' => 'foo'],
  1237. 'Foo',
  1238. '/label',
  1239. ['input' => [
  1240. 'type' => 'number',
  1241. 'name' => 'foo',
  1242. 'id' => 'foo',
  1243. 'step' => 'any',
  1244. ]],
  1245. '/div',
  1246. ];
  1247. $this->assertHtml($expected, $result);
  1248. $result = $this->Form->control('foo', ['step' => 0.5]);
  1249. $expected = [
  1250. 'div' => ['class' => 'input number'],
  1251. 'label' => ['for' => 'foo'],
  1252. 'Foo',
  1253. '/label',
  1254. ['input' => [
  1255. 'type' => 'number',
  1256. 'name' => 'foo',
  1257. 'id' => 'foo',
  1258. 'step' => '0.5',
  1259. ]],
  1260. '/div',
  1261. ];
  1262. $this->assertHtml($expected, $result);
  1263. }
  1264. /**
  1265. * Tests correct generation of number fields for integer fields
  1266. *
  1267. * @return void
  1268. */
  1269. public function testTextFieldTypeNumberGenerationForIntegers()
  1270. {
  1271. $this->getTableLocator()->get('Contacts', [
  1272. 'className' => __NAMESPACE__ . '\ContactsTable',
  1273. ]);
  1274. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1275. $result = $this->Form->control('age');
  1276. $expected = [
  1277. 'div' => ['class' => 'input number'],
  1278. 'label' => ['for' => 'age'],
  1279. 'Age',
  1280. '/label',
  1281. ['input' => [
  1282. 'type' => 'number', 'name' => 'age',
  1283. 'id' => 'age',
  1284. ]],
  1285. '/div',
  1286. ];
  1287. $this->assertHtml($expected, $result);
  1288. }
  1289. /**
  1290. * Tests correct generation of file upload fields for binary fields
  1291. *
  1292. * @return void
  1293. */
  1294. public function testFileUploadFieldTypeGenerationForBinaries()
  1295. {
  1296. $table = $this->getTableLocator()->get('Contacts', [
  1297. 'className' => __NAMESPACE__ . '\ContactsTable',
  1298. ]);
  1299. $table->setSchema(['foo' => [
  1300. 'type' => 'binary',
  1301. 'null' => false,
  1302. 'default' => null,
  1303. 'length' => 1024,
  1304. ]]);
  1305. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1306. $result = $this->Form->control('foo');
  1307. $expected = [
  1308. 'div' => ['class' => 'input file'],
  1309. 'label' => ['for' => 'foo'],
  1310. 'Foo',
  1311. '/label',
  1312. ['input' => [
  1313. 'type' => 'file', 'name' => 'foo',
  1314. 'id' => 'foo',
  1315. ]],
  1316. '/div',
  1317. ];
  1318. $this->assertHtml($expected, $result);
  1319. }
  1320. /**
  1321. * testFormSecurityMultipleFields method
  1322. *
  1323. * Test secure() with multiple row form. Ensure hash is correct.
  1324. *
  1325. * @return void
  1326. */
  1327. public function testFormSecurityMultipleFields()
  1328. {
  1329. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1330. $fields = [
  1331. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1332. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1333. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1334. ];
  1335. $result = $this->Form->secure($fields);
  1336. $hash = '8670192c3f040bf58680479060b4755b7a5c3596' .
  1337. '%3AModel.0.hidden%7CModel.0.valid%7CModel.1.hidden%7CModel.1.valid';
  1338. $tokenDebug = urlencode(json_encode([
  1339. '',
  1340. $fields,
  1341. [],
  1342. ]));
  1343. $expected = [
  1344. 'div' => ['style' => 'display:none;'],
  1345. ['input' => [
  1346. 'type' => 'hidden',
  1347. 'name' => '_Token[fields]',
  1348. 'value' => $hash,
  1349. 'autocomplete' => 'off',
  1350. ]],
  1351. ['input' => [
  1352. 'type' => 'hidden',
  1353. 'name' => '_Token[unlocked]',
  1354. 'autocomplete' => 'off',
  1355. 'value' => '',
  1356. ]],
  1357. ['input' => [
  1358. 'type' => 'hidden',
  1359. 'name' => '_Token[debug]',
  1360. 'value' => $tokenDebug,
  1361. 'autocomplete' => 'off',
  1362. ]],
  1363. '/div',
  1364. ];
  1365. $this->assertHtml($expected, $result);
  1366. }
  1367. /**
  1368. * testFormSecurityMultipleSubmitButtons
  1369. *
  1370. * test form submit generation and ensure that _Token is only created on end()
  1371. *
  1372. * @return void
  1373. */
  1374. public function testFormSecurityMultipleSubmitButtons()
  1375. {
  1376. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1377. $this->Form->create($this->article);
  1378. $this->Form->text('Address.title');
  1379. $this->Form->text('Address.first_name');
  1380. $result = $this->Form->submit('Save', ['name' => 'save']);
  1381. $expected = [
  1382. 'div' => ['class' => 'submit'],
  1383. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1384. '/div',
  1385. ];
  1386. $this->assertHtml($expected, $result);
  1387. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1388. $expected = [
  1389. 'div' => ['class' => 'submit'],
  1390. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1391. '/div',
  1392. ];
  1393. $this->assertHtml($expected, $result);
  1394. $result = $this->Form->end();
  1395. $tokenDebug = urlencode(json_encode([
  1396. '/articles/add',
  1397. [
  1398. 'Address.title',
  1399. 'Address.first_name',
  1400. ],
  1401. ['save', 'cancel'],
  1402. ]));
  1403. $expected = [
  1404. 'div' => ['style' => 'display:none;'],
  1405. ['input' => [
  1406. 'type' => 'hidden',
  1407. 'name' => '_Token[fields]',
  1408. 'autocomplete',
  1409. 'value',
  1410. ]],
  1411. ['input' => [
  1412. 'type' => 'hidden',
  1413. 'name' => '_Token[unlocked]',
  1414. 'value' => 'cancel%7Csave',
  1415. 'autocomplete' => 'off',
  1416. ]],
  1417. ['input' => [
  1418. 'type' => 'hidden',
  1419. 'name' => '_Token[debug]',
  1420. 'value' => $tokenDebug,
  1421. 'autocomplete' => 'off',
  1422. ]],
  1423. '/div',
  1424. ];
  1425. $this->assertHtml($expected, $result);
  1426. }
  1427. /**
  1428. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1429. *
  1430. * @return void
  1431. */
  1432. public function testSecurityButtonNestedNamed()
  1433. {
  1434. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1435. $this->Form->create('Addresses');
  1436. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1437. $result = $this->Form->unlockField();
  1438. $this->assertEquals(['Address.button'], $result);
  1439. }
  1440. /**
  1441. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1442. *
  1443. * @return void
  1444. */
  1445. public function testSecuritySubmitNestedNamed()
  1446. {
  1447. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1448. $this->Form->create($this->article);
  1449. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1450. $result = $this->Form->unlockField();
  1451. $this->assertEquals(['Address.button'], $result);
  1452. }
  1453. /**
  1454. * Test that the correct fields are unlocked for image submits with no names.
  1455. *
  1456. * @return void
  1457. */
  1458. public function testSecuritySubmitImageNoName()
  1459. {
  1460. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1461. $this->Form->create(false);
  1462. $result = $this->Form->submit('save.png');
  1463. $expected = [
  1464. 'div' => ['class' => 'submit'],
  1465. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1466. '/div',
  1467. ];
  1468. $this->assertHtml($expected, $result);
  1469. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1470. }
  1471. /**
  1472. * Test that the correct fields are unlocked for image submits with names.
  1473. *
  1474. * @return void
  1475. */
  1476. public function testSecuritySubmitImageName()
  1477. {
  1478. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1479. $this->Form->create(null);
  1480. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1481. $expected = [
  1482. 'div' => ['class' => 'submit'],
  1483. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1484. '/div',
  1485. ];
  1486. $this->assertHtml($expected, $result);
  1487. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1488. }
  1489. /**
  1490. * testFormSecurityMultipleControlFields method
  1491. *
  1492. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1493. *
  1494. * @return void
  1495. */
  1496. public function testFormSecurityMultipleControlFields()
  1497. {
  1498. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1499. $this->Form->create();
  1500. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1501. $this->Form->control('Addresses.0.title');
  1502. $this->Form->control('Addresses.0.first_name');
  1503. $this->Form->control('Addresses.0.last_name');
  1504. $this->Form->control('Addresses.0.address');
  1505. $this->Form->control('Addresses.0.city');
  1506. $this->Form->control('Addresses.0.phone');
  1507. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1508. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1509. $this->Form->control('Addresses.1.title');
  1510. $this->Form->control('Addresses.1.first_name');
  1511. $this->Form->control('Addresses.1.last_name');
  1512. $this->Form->control('Addresses.1.address');
  1513. $this->Form->control('Addresses.1.city');
  1514. $this->Form->control('Addresses.1.phone');
  1515. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1516. $result = $this->Form->secure($this->Form->fields);
  1517. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1518. $tokenDebug = urlencode(json_encode([
  1519. '/articles/add',
  1520. [
  1521. 'Addresses.0.id' => '123456',
  1522. 'Addresses.0.title',
  1523. 'Addresses.0.first_name',
  1524. 'Addresses.0.last_name',
  1525. 'Addresses.0.address',
  1526. 'Addresses.0.city',
  1527. 'Addresses.0.phone',
  1528. 'Addresses.0.primary',
  1529. 'Addresses.1.id' => '654321',
  1530. 'Addresses.1.title',
  1531. 'Addresses.1.first_name',
  1532. 'Addresses.1.last_name',
  1533. 'Addresses.1.address',
  1534. 'Addresses.1.city',
  1535. 'Addresses.1.phone',
  1536. 'Addresses.1.primary',
  1537. ],
  1538. [],
  1539. ]));
  1540. $expected = [
  1541. 'div' => ['style' => 'display:none;'],
  1542. ['input' => [
  1543. 'type' => 'hidden',
  1544. 'name' => '_Token[fields]',
  1545. 'value' => $hash,
  1546. 'autocomplete' => 'off',
  1547. ]],
  1548. ['input' => [
  1549. 'type' => 'hidden',
  1550. 'name' => '_Token[unlocked]',
  1551. 'autocomplete' => 'off',
  1552. 'value' => '',
  1553. ]],
  1554. ['input' => [
  1555. 'type' => 'hidden',
  1556. 'name' => '_Token[debug]',
  1557. 'value' => $tokenDebug,
  1558. 'autocomplete' => 'off',
  1559. ]],
  1560. '/div',
  1561. ];
  1562. $this->assertHtml($expected, $result);
  1563. }
  1564. /**
  1565. * testFormSecurityArrayFields method
  1566. *
  1567. * Test form security with Model.field.0 style inputs.
  1568. *
  1569. * @return void
  1570. */
  1571. public function testFormSecurityArrayFields()
  1572. {
  1573. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1574. $this->Form->create();
  1575. $this->Form->text('Address.primary.1');
  1576. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1577. $this->Form->text('Address.secondary.1.0');
  1578. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1579. }
  1580. /**
  1581. * testFormSecurityMultipleControlDisabledFields method
  1582. *
  1583. * Test secure form generation with multiple records and disabled fields.
  1584. *
  1585. * @return void
  1586. */
  1587. public function testFormSecurityMultipleControlDisabledFields()
  1588. {
  1589. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1590. 'unlockedFields' => ['first_name', 'address'],
  1591. ]));
  1592. $this->Form->create();
  1593. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1594. $this->Form->text('Addresses.0.title');
  1595. $this->Form->text('Addresses.0.first_name');
  1596. $this->Form->text('Addresses.0.last_name');
  1597. $this->Form->text('Addresses.0.address');
  1598. $this->Form->text('Addresses.0.city');
  1599. $this->Form->text('Addresses.0.phone');
  1600. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1601. $this->Form->text('Addresses.1.title');
  1602. $this->Form->text('Addresses.1.first_name');
  1603. $this->Form->text('Addresses.1.last_name');
  1604. $this->Form->text('Addresses.1.address');
  1605. $this->Form->text('Addresses.1.city');
  1606. $this->Form->text('Addresses.1.phone');
  1607. $result = $this->Form->secure($this->Form->fields);
  1608. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1609. $tokenDebug = urlencode(json_encode([
  1610. '/articles/add',
  1611. [
  1612. 'Addresses.0.id' => '123456',
  1613. 'Addresses.0.title',
  1614. 'Addresses.0.last_name',
  1615. 'Addresses.0.city',
  1616. 'Addresses.0.phone',
  1617. 'Addresses.1.id' => '654321',
  1618. 'Addresses.1.title',
  1619. 'Addresses.1.last_name',
  1620. 'Addresses.1.city',
  1621. 'Addresses.1.phone',
  1622. ],
  1623. [
  1624. 'first_name',
  1625. 'address',
  1626. ],
  1627. ]));
  1628. $expected = [
  1629. 'div' => ['style' => 'display:none;'],
  1630. ['input' => [
  1631. 'type' => 'hidden',
  1632. 'name' => '_Token[fields]',
  1633. 'autocomplete' => 'off',
  1634. 'value' => $hash,
  1635. ]],
  1636. ['input' => [
  1637. 'type' => 'hidden',
  1638. 'name' => '_Token[unlocked]',
  1639. 'autocomplete' => 'off',
  1640. 'value' => 'address%7Cfirst_name',
  1641. ]],
  1642. ['input' => [
  1643. 'type' => 'hidden',
  1644. 'name' => '_Token[debug]',
  1645. 'autocomplete' => 'off',
  1646. 'value' => $tokenDebug,
  1647. ]],
  1648. '/div',
  1649. ];
  1650. $this->assertHtml($expected, $result);
  1651. }
  1652. /**
  1653. * testFormSecurityControlDisabledFields method
  1654. *
  1655. * Test single record form with disabled fields.
  1656. *
  1657. * @return void
  1658. */
  1659. public function testFormSecurityControlUnlockedFields()
  1660. {
  1661. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1662. 'unlockedFields' => ['first_name', 'address'],
  1663. ]));
  1664. $this->Form->create();
  1665. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1666. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1667. $this->Form->text('Addresses.title');
  1668. $this->Form->text('Addresses.first_name');
  1669. $this->Form->text('Addresses.last_name');
  1670. $this->Form->text('Addresses.address');
  1671. $this->Form->text('Addresses.city');
  1672. $this->Form->text('Addresses.phone');
  1673. $result = $this->Form->fields;
  1674. $expected = [
  1675. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1676. 'Addresses.city', 'Addresses.phone',
  1677. ];
  1678. $this->assertEquals($expected, $result);
  1679. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1680. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1681. $tokenDebug = urlencode(json_encode([
  1682. '/articles/add',
  1683. [
  1684. 'Addresses.id' => '123456',
  1685. 'Addresses.title',
  1686. 'Addresses.last_name',
  1687. 'Addresses.city',
  1688. 'Addresses.phone',
  1689. ],
  1690. [
  1691. 'first_name',
  1692. 'address',
  1693. ],
  1694. ]));
  1695. $expected = [
  1696. 'div' => ['style' => 'display:none;'],
  1697. ['input' => [
  1698. 'type' => 'hidden',
  1699. 'name' => '_Token[fields]',
  1700. 'value' => $hash,
  1701. 'autocomplete' => 'off',
  1702. 'data-foo' => 'bar',
  1703. ]],
  1704. ['input' => [
  1705. 'type' => 'hidden',
  1706. 'name' => '_Token[unlocked]',
  1707. 'value' => 'address%7Cfirst_name',
  1708. 'autocomplete' => 'off',
  1709. 'data-foo' => 'bar',
  1710. ]],
  1711. ['input' => [
  1712. 'type' => 'hidden', 'name' => '_Token[debug]',
  1713. 'value' => $tokenDebug,
  1714. 'autocomplete' => 'off',
  1715. 'data-foo' => 'bar',
  1716. ]],
  1717. '/div',
  1718. ];
  1719. $this->assertHtml($expected, $result);
  1720. }
  1721. /**
  1722. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1723. *
  1724. * Test single record form with debugSecurity param.
  1725. *
  1726. * @return void
  1727. */
  1728. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1729. {
  1730. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1731. 'unlockedFields' => ['first_name', 'address'],
  1732. ]));
  1733. $this->Form->create();
  1734. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1735. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1736. $this->Form->text('Addresses.title');
  1737. $this->Form->text('Addresses.first_name');
  1738. $this->Form->text('Addresses.last_name');
  1739. $this->Form->text('Addresses.address');
  1740. $this->Form->text('Addresses.city');
  1741. $this->Form->text('Addresses.phone');
  1742. $result = $this->Form->fields;
  1743. $expected = [
  1744. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1745. 'Addresses.city', 'Addresses.phone',
  1746. ];
  1747. $this->assertEquals($expected, $result);
  1748. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1749. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1750. $tokenDebug = urlencode(json_encode([
  1751. '/articles/add',
  1752. [
  1753. 'Addresses.id' => '123456',
  1754. 'Addresses.title',
  1755. 'Addresses.last_name',
  1756. 'Addresses.city',
  1757. 'Addresses.phone',
  1758. ],
  1759. [
  1760. 'first_name',
  1761. 'address',
  1762. ],
  1763. ]));
  1764. $expected = [
  1765. 'div' => ['style' => 'display:none;'],
  1766. ['input' => [
  1767. 'type' => 'hidden',
  1768. 'name' => '_Token[fields]',
  1769. 'value' => $hash,
  1770. 'autocomplete' => 'off',
  1771. 'data-foo' => 'bar',
  1772. ]],
  1773. ['input' => [
  1774. 'type' => 'hidden',
  1775. 'name' => '_Token[unlocked]',
  1776. 'value' => 'address%7Cfirst_name',
  1777. 'autocomplete' => 'off',
  1778. 'data-foo' => 'bar',
  1779. ]],
  1780. ['input' => [
  1781. 'type' => 'hidden', 'name' => '_Token[debug]',
  1782. 'value' => $tokenDebug,
  1783. 'autocomplete' => 'off',
  1784. 'data-foo' => 'bar',
  1785. ]],
  1786. '/div',
  1787. ];
  1788. $this->assertHtml($expected, $result);
  1789. }
  1790. /**
  1791. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1792. *
  1793. * Debug is false, debugSecurity is true -> no debug
  1794. *
  1795. * @return void
  1796. */
  1797. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1798. {
  1799. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1800. 'unlockedFields' => ['first_name', 'address'],
  1801. ]));
  1802. $this->Form->create();
  1803. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1804. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1805. $this->Form->text('Addresses.title');
  1806. $this->Form->text('Addresses.first_name');
  1807. $this->Form->text('Addresses.last_name');
  1808. $this->Form->text('Addresses.address');
  1809. $this->Form->text('Addresses.city');
  1810. $this->Form->text('Addresses.phone');
  1811. $result = $this->Form->fields;
  1812. $expected = [
  1813. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1814. 'Addresses.city', 'Addresses.phone',
  1815. ];
  1816. $this->assertEquals($expected, $result);
  1817. Configure::write('debug', false);
  1818. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1819. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1820. $expected = [
  1821. 'div' => ['style' => 'display:none;'],
  1822. ['input' => [
  1823. 'type' => 'hidden',
  1824. 'name' => '_Token[fields]',
  1825. 'value' => $hash,
  1826. 'autocomplete' => 'off',
  1827. 'data-foo' => 'bar',
  1828. ]],
  1829. ['input' => [
  1830. 'type' => 'hidden',
  1831. 'name' => '_Token[unlocked]',
  1832. 'value' => 'address%7Cfirst_name',
  1833. 'autocomplete' => 'off',
  1834. 'data-foo' => 'bar',
  1835. ]],
  1836. '/div',
  1837. ];
  1838. $this->assertHtml($expected, $result);
  1839. }
  1840. /**
  1841. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1842. *
  1843. * Test single record form with debugSecurity param.
  1844. *
  1845. * @return void
  1846. */
  1847. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1848. {
  1849. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1850. 'unlockedFields' => ['first_name', 'address'],
  1851. ]));
  1852. $this->Form->create();
  1853. $this->assertEquals(
  1854. $this->View->getRequest()->getParam('_Token.unlockedFields'),
  1855. $this->Form->unlockField()
  1856. );
  1857. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1858. $this->Form->text('Addresses.title');
  1859. $this->Form->text('Addresses.first_name');
  1860. $this->Form->text('Addresses.last_name');
  1861. $this->Form->text('Addresses.address');
  1862. $this->Form->text('Addresses.city');
  1863. $this->Form->text('Addresses.phone');
  1864. $result = $this->Form->fields;
  1865. $expected = [
  1866. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1867. 'Addresses.city', 'Addresses.phone',
  1868. ];
  1869. $this->assertEquals($expected, $result);
  1870. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1871. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1872. $expected = [
  1873. 'div' => ['style' => 'display:none;'],
  1874. ['input' => [
  1875. 'type' => 'hidden',
  1876. 'name' => '_Token[fields]',
  1877. 'value' => $hash,
  1878. 'autocomplete' => 'off',
  1879. 'data-foo' => 'bar',
  1880. ]],
  1881. ['input' => [
  1882. 'type' => 'hidden',
  1883. 'name' => '_Token[unlocked]',
  1884. 'value' => 'address%7Cfirst_name',
  1885. 'autocomplete' => 'off',
  1886. 'data-foo' => 'bar',
  1887. ]],
  1888. '/div',
  1889. ];
  1890. $this->assertHtml($expected, $result);
  1891. }
  1892. /**
  1893. * testFormSecureWithCustomNameAttribute method
  1894. *
  1895. * Test securing inputs with custom name attributes.
  1896. *
  1897. * @return void
  1898. */
  1899. public function testFormSecureWithCustomNameAttribute()
  1900. {
  1901. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1902. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1903. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1904. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1905. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1906. }
  1907. /**
  1908. * testFormSecuredControl method
  1909. *
  1910. * Test generation of entire secure form, assertions made on control() output.
  1911. *
  1912. * @return void
  1913. */
  1914. public function testFormSecuredControl()
  1915. {
  1916. $this->View->setRequest($this->View->getRequest()
  1917. ->withParam('_Token', 'stuff')
  1918. ->withParam('_csrfToken', 'testKey'));
  1919. $this->article['schema'] = [
  1920. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1921. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1922. ];
  1923. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1924. $encoding = strtolower(Configure::read('App.encoding'));
  1925. $expected = [
  1926. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1927. 'div' => ['style' => 'display:none;'],
  1928. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1929. ['input' => [
  1930. 'type' => 'hidden',
  1931. 'name' => '_csrfToken',
  1932. 'value' => 'testKey',
  1933. 'autocomplete' => 'off',
  1934. ]],
  1935. '/div',
  1936. ];
  1937. $this->assertHtml($expected, $result);
  1938. $result = $this->Form->control('ratio');
  1939. $expected = [
  1940. 'div' => ['class'],
  1941. 'label' => ['for'],
  1942. 'Ratio',
  1943. '/label',
  1944. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1945. '/div',
  1946. ];
  1947. $this->assertHtml($expected, $result);
  1948. $result = $this->Form->control('population');
  1949. $expected = [
  1950. 'div' => ['class'],
  1951. 'label' => ['for'],
  1952. 'Population',
  1953. '/label',
  1954. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1955. '/div',
  1956. ];
  1957. $this->assertHtml($expected, $result);
  1958. $result = $this->Form->control('published', ['type' => 'text']);
  1959. $expected = [
  1960. 'div' => ['class' => 'input text'],
  1961. 'label' => ['for' => 'published'],
  1962. 'Published',
  1963. '/label',
  1964. ['input' => [
  1965. 'type' => 'text',
  1966. 'name' => 'published',
  1967. 'id' => 'published',
  1968. ]],
  1969. '/div',
  1970. ];
  1971. $this->assertHtml($expected, $result);
  1972. $result = $this->Form->control('other', ['type' => 'text']);
  1973. $expected = [
  1974. 'div' => ['class' => 'input text'],
  1975. 'label' => ['for' => 'other'],
  1976. 'Other',
  1977. '/label',
  1978. ['input' => [
  1979. 'type' => 'text',
  1980. 'name' => 'other',
  1981. 'id',
  1982. ]],
  1983. '/div',
  1984. ];
  1985. $this->assertHtml($expected, $result);
  1986. $result = $this->Form->hidden('stuff');
  1987. $expected = [
  1988. 'input' => [
  1989. 'type' => 'hidden',
  1990. 'name' => 'stuff',
  1991. ],
  1992. ];
  1993. $this->assertHtml($expected, $result);
  1994. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1995. $expected = ['input' => [
  1996. 'type' => 'hidden',
  1997. 'name' => 'hidden',
  1998. 'value' => '0',
  1999. ]];
  2000. $this->assertHtml($expected, $result);
  2001. $result = $this->Form->control('something', ['type' => 'checkbox']);
  2002. $expected = [
  2003. 'div' => ['class' => 'input checkbox'],
  2004. ['input' => [
  2005. 'type' => 'hidden',
  2006. 'name' => 'something',
  2007. 'value' => '0',
  2008. ]],
  2009. 'label' => ['for' => 'something'],
  2010. ['input' => [
  2011. 'type' => 'checkbox',
  2012. 'name' => 'something',
  2013. 'value' => '1',
  2014. 'id' => 'something',
  2015. ]],
  2016. 'Something',
  2017. '/label',
  2018. '/div',
  2019. ];
  2020. $this->assertHtml($expected, $result);
  2021. $result = $this->Form->fields;
  2022. $expectedFields = [
  2023. 'ratio',
  2024. 'population',
  2025. 'published',
  2026. 'other',
  2027. 'stuff' => '',
  2028. 'hidden' => '0',
  2029. 'something',
  2030. ];
  2031. $this->assertEquals($expectedFields, $result);
  2032. $result = $this->Form->secure($this->Form->fields);
  2033. $tokenDebug = urlencode(json_encode([
  2034. '/articles/add',
  2035. $expectedFields,
  2036. [],
  2037. ]));
  2038. $expected = [
  2039. 'div' => ['style' => 'display:none;'],
  2040. ['input' => [
  2041. 'type' => 'hidden',
  2042. 'name' => '_Token[fields]',
  2043. 'value',
  2044. 'autocomplete',
  2045. ]],
  2046. ['input' => [
  2047. 'type' => 'hidden',
  2048. 'name' => '_Token[unlocked]',
  2049. 'value' => '',
  2050. 'autocomplete' => 'off',
  2051. ]],
  2052. ['input' => [
  2053. 'type' => 'hidden', 'name' => '_Token[debug]',
  2054. 'value' => $tokenDebug,
  2055. 'autocomplete' => 'off',
  2056. ]],
  2057. '/div',
  2058. ];
  2059. $this->assertHtml($expected, $result);
  2060. }
  2061. /**
  2062. * testSecuredControlCustomName method
  2063. *
  2064. * Test secured inputs with custom names.
  2065. *
  2066. * @return void
  2067. */
  2068. public function testSecuredControlCustomName()
  2069. {
  2070. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2071. $this->assertEquals([], $this->Form->fields);
  2072. $this->Form->text('text_input', [
  2073. 'name' => 'Option[General.default_role]',
  2074. ]);
  2075. $expected = ['Option.General.default_role'];
  2076. $this->assertEquals($expected, $this->Form->fields);
  2077. $this->Form->select('select_box', [1, 2], [
  2078. 'name' => 'Option[General.select_role]',
  2079. ]);
  2080. $expected[] = 'Option.General.select_role';
  2081. $this->assertEquals($expected, $this->Form->fields);
  2082. $this->Form->text('other.things[]');
  2083. $expected[] = 'other.things';
  2084. $this->assertEquals($expected, $this->Form->fields);
  2085. }
  2086. /**
  2087. * testSecuredControlDuplicate method
  2088. *
  2089. * Test that a hidden field followed by a visible field
  2090. * undoes the hidden field locking.
  2091. *
  2092. * @return void
  2093. */
  2094. public function testSecuredControlDuplicate()
  2095. {
  2096. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2097. $this->assertEquals([], $this->Form->fields);
  2098. $this->Form->control('text_val', [
  2099. 'type' => 'hidden',
  2100. 'value' => 'some text',
  2101. ]);
  2102. $expected = ['text_val' => 'some text'];
  2103. $this->assertEquals($expected, $this->Form->fields);
  2104. $this->Form->control('text_val', [
  2105. 'type' => 'text',
  2106. ]);
  2107. $expected = ['text_val'];
  2108. $this->assertEquals($expected, $this->Form->fields);
  2109. }
  2110. /**
  2111. * testFormSecuredFileControl method
  2112. *
  2113. * Tests that the correct keys are added to the field hash index.
  2114. *
  2115. * @return void
  2116. */
  2117. public function testFormSecuredFileControl()
  2118. {
  2119. $this->assertEquals([], $this->Form->fields);
  2120. $this->Form->file('Attachment.file');
  2121. $expected = [
  2122. 'Attachment.file.name', 'Attachment.file.type',
  2123. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2124. 'Attachment.file.size',
  2125. ];
  2126. $this->assertEquals($expected, $this->Form->fields);
  2127. }
  2128. /**
  2129. * testFormSecuredMultipleSelect method
  2130. *
  2131. * Test that multiple selects keys are added to field hash.
  2132. *
  2133. * @return void
  2134. */
  2135. public function testFormSecuredMultipleSelect()
  2136. {
  2137. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  2138. $this->assertEquals([], $this->Form->fields);
  2139. $options = ['1' => 'one', '2' => 'two'];
  2140. $this->Form->select('Model.select', $options);
  2141. $expected = ['Model.select'];
  2142. $this->assertEquals($expected, $this->Form->fields);
  2143. $this->Form->fields = [];
  2144. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2145. $this->assertEquals($expected, $this->Form->fields);
  2146. }
  2147. /**
  2148. * testFormSecuredRadio method
  2149. *
  2150. * @return void
  2151. */
  2152. public function testFormSecuredRadio()
  2153. {
  2154. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2155. $this->assertEquals([], $this->Form->fields);
  2156. $options = ['1' => 'option1', '2' => 'option2'];
  2157. $this->Form->radio('Test.test', $options);
  2158. $expected = ['Test.test'];
  2159. $this->assertEquals($expected, $this->Form->fields);
  2160. $this->Form->radio('Test.all', $options, [
  2161. 'disabled' => ['option1', 'option2'],
  2162. ]);
  2163. $expected = ['Test.test', 'Test.all' => ''];
  2164. $this->assertEquals($expected, $this->Form->fields);
  2165. $this->Form->radio('Test.some', $options, [
  2166. 'disabled' => ['option1'],
  2167. ]);
  2168. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2169. $this->assertEquals($expected, $this->Form->fields);
  2170. }
  2171. /**
  2172. * testFormSecuredAndDisabledNotAssoc method
  2173. *
  2174. * Test that when disabled is in a list based attribute array it works.
  2175. *
  2176. * @return void
  2177. */
  2178. public function testFormSecuredAndDisabledNotAssoc()
  2179. {
  2180. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2181. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2182. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2183. $this->Form->text('Model.text', ['disabled']);
  2184. $this->Form->textarea('Model.textarea', ['disabled']);
  2185. $this->Form->password('Model.password', ['disabled']);
  2186. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2187. $expected = [
  2188. 'Model.radio' => '',
  2189. ];
  2190. $this->assertEquals($expected, $this->Form->fields);
  2191. }
  2192. /**
  2193. * testFormSecuredAndDisabled method
  2194. *
  2195. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2196. * hashing.
  2197. *
  2198. * @return void
  2199. */
  2200. public function testFormSecuredAndDisabled()
  2201. {
  2202. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2203. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2204. $this->Form->text('Model.text', ['disabled' => true]);
  2205. $this->Form->password('Model.text', ['disabled' => true]);
  2206. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2207. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2208. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2209. $this->Form->year('Model.year', ['disabled' => true]);
  2210. $this->Form->month('Model.month', ['disabled' => true]);
  2211. $this->Form->day('Model.day', ['disabled' => true]);
  2212. $this->Form->hour('Model.hour', ['disabled' => true]);
  2213. $this->Form->minute('Model.minute', ['disabled' => true]);
  2214. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2215. $expected = [
  2216. 'Model.radio' => '',
  2217. ];
  2218. $this->assertEquals($expected, $this->Form->fields);
  2219. }
  2220. /**
  2221. * testDisableSecurityUsingForm method
  2222. *
  2223. * @return void
  2224. */
  2225. public function testDisableSecurityUsingForm()
  2226. {
  2227. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2228. 'disabledFields' => [],
  2229. ]));
  2230. $this->Form->create();
  2231. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2232. $this->Form->text('Addresses.title');
  2233. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2234. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2235. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2236. $result = $this->Form->fields;
  2237. $expected = [
  2238. 'Addresses.id' => '123456', 'Addresses.title',
  2239. ];
  2240. $this->assertEquals($expected, $result);
  2241. }
  2242. /**
  2243. * testUnlockFieldAddsToList method
  2244. *
  2245. * Test disableField.
  2246. *
  2247. * @return void
  2248. */
  2249. public function testUnlockFieldAddsToList()
  2250. {
  2251. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2252. 'unlockedFields' => [],
  2253. ]));
  2254. $this->Form->unlockField('Contact.name');
  2255. $this->Form->text('Contact.name');
  2256. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2257. $this->assertEquals([], $this->Form->fields);
  2258. }
  2259. /**
  2260. * testUnlockFieldRemovingFromFields method
  2261. *
  2262. * Test unlockField removing from fields array.
  2263. *
  2264. * @return void
  2265. */
  2266. public function testUnlockFieldRemovingFromFields()
  2267. {
  2268. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2269. 'unlockedFields' => [],
  2270. ]));
  2271. $this->Form->create($this->article);
  2272. $this->Form->hidden('Article.id', ['value' => 1]);
  2273. $this->Form->text('Article.title');
  2274. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2275. $this->assertContains('Article.title', $this->Form->fields, 'Field should be secured.');
  2276. $this->Form->unlockField('Article.title');
  2277. $this->Form->unlockField('Article.id');
  2278. $this->assertEquals([], $this->Form->fields);
  2279. }
  2280. /**
  2281. * testResetUnlockFields method
  2282. *
  2283. * Test reset unlockFields, when create new form.
  2284. *
  2285. * @return void
  2286. */
  2287. public function testResetUnlockFields()
  2288. {
  2289. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2290. 'key' => 'testKey',
  2291. 'unlockedFields' => [],
  2292. ]));
  2293. $this->Form->unlockField('Contact.id');
  2294. $this->Form->create('Contact');
  2295. $this->Form->hidden('Contact.id', ['value' => 1]);
  2296. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2297. $this->Form->end();
  2298. $this->Form->create('Contact');
  2299. $this->Form->hidden('Contact.id', ['value' => 1]);
  2300. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2301. }
  2302. /**
  2303. * testSecuredFormUrlIgnoresHost method
  2304. *
  2305. * Test that only the path + query elements of a form's URL show up in their hash.
  2306. *
  2307. * @return void
  2308. */
  2309. public function testSecuredFormUrlIgnoresHost()
  2310. {
  2311. $this->View->setRequest($this->View->getRequest()->withParam('_Token', ['key' => 'testKey']));
  2312. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2313. $this->Form->create($this->article, [
  2314. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2315. ]);
  2316. $result = $this->Form->secure();
  2317. $this->assertContains($expected, $result);
  2318. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2319. $result = $this->Form->secure();
  2320. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2321. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2322. $result = $this->Form->secure();
  2323. $this->assertContains($expected, $result, 'URL path + query should work.');
  2324. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2325. $result = $this->Form->secure();
  2326. $this->assertNotContains($expected, $result, 'URL is different');
  2327. }
  2328. /**
  2329. * testSecuredFormUrlHasHtmlAndIdentifier method
  2330. *
  2331. * Test that URL, HTML and identifier show up in their hashes.
  2332. *
  2333. * @return void
  2334. */
  2335. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2336. {
  2337. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2338. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2339. $this->Form->create($this->article, [
  2340. 'url' => [
  2341. 'controller' => 'articles',
  2342. 'action' => 'view',
  2343. '?' => [
  2344. 'page' => 1,
  2345. 'limit' => 10,
  2346. 'html' => '<>"',
  2347. ],
  2348. '#' => 'result',
  2349. ],
  2350. ]);
  2351. $result = $this->Form->secure();
  2352. $this->assertContains($expected, $result);
  2353. $this->Form->create($this->article, [
  2354. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2355. ]);
  2356. $result = $this->Form->secure();
  2357. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2358. $this->Form->create($this->article, [
  2359. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2360. ]);
  2361. $result = $this->Form->secure();
  2362. $this->assertContains($expected, $result, 'URL path + query should work.');
  2363. }
  2364. /**
  2365. * testErrorMessageDisplay method
  2366. *
  2367. * Test error message display.
  2368. *
  2369. * @return void
  2370. */
  2371. public function testErrorMessageDisplay()
  2372. {
  2373. $this->article['errors'] = [
  2374. 'Article' => [
  2375. 'title' => 'error message',
  2376. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2377. ],
  2378. ];
  2379. $this->Form->create($this->article);
  2380. $result = $this->Form->control('Article.title');
  2381. $expected = [
  2382. 'div' => ['class' => 'input text error'],
  2383. 'label' => ['for' => 'article-title'],
  2384. 'Title',
  2385. '/label',
  2386. 'input' => [
  2387. 'type' => 'text', 'name' => 'Article[title]',
  2388. 'id' => 'article-title', 'class' => 'form-error',
  2389. ],
  2390. ['div' => ['class' => 'error-message']],
  2391. 'error message',
  2392. '/div',
  2393. '/div',
  2394. ];
  2395. $this->assertHtml($expected, $result);
  2396. $result = $this->Form->control('Article.title', [
  2397. 'templates' => [
  2398. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2399. ],
  2400. ]);
  2401. $expected = [
  2402. 'div' => ['class' => 'input text error'],
  2403. 'label' => ['for' => 'article-title'],
  2404. 'Title',
  2405. '/label',
  2406. 'input' => [
  2407. 'type' => 'text', 'name' => 'Article[title]',
  2408. 'id' => 'article-title', 'class' => 'form-error',
  2409. ],
  2410. '/div',
  2411. ];
  2412. $this->assertHtml($expected, $result);
  2413. $result = $this->Form->control('Article.content');
  2414. $expected = [
  2415. 'div' => ['class' => 'input text error'],
  2416. 'label' => ['for' => 'article-content'],
  2417. 'Content',
  2418. '/label',
  2419. 'input' => [
  2420. 'type' => 'text', 'name' => 'Article[content]',
  2421. 'id' => 'article-content', 'class' => 'form-error',
  2422. ],
  2423. ['div' => ['class' => 'error-message']],
  2424. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2425. '/div',
  2426. '/div',
  2427. ];
  2428. $this->assertHtml($expected, $result);
  2429. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2430. $expected = [
  2431. 'div' => ['class' => 'input text error'],
  2432. 'label' => ['for' => 'article-content'],
  2433. 'Content',
  2434. '/label',
  2435. 'input' => [
  2436. 'type' => 'text', 'name' => 'Article[content]',
  2437. 'id' => 'article-content', 'class' => 'form-error',
  2438. ],
  2439. ['div' => ['class' => 'error-message']],
  2440. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2441. '/div',
  2442. '/div',
  2443. ];
  2444. $this->assertHtml($expected, $result);
  2445. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2446. $expected = [
  2447. 'div' => ['class' => 'input text error'],
  2448. 'label' => ['for' => 'article-content'],
  2449. 'Content',
  2450. '/label',
  2451. 'input' => [
  2452. 'type' => 'text', 'name' => 'Article[content]',
  2453. 'id' => 'article-content', 'class' => 'form-error',
  2454. ],
  2455. ['div' => ['class' => 'error-message']],
  2456. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2457. '/div',
  2458. '/div',
  2459. ];
  2460. $this->assertHtml($expected, $result);
  2461. }
  2462. /**
  2463. * testEmptyErrorValidation method
  2464. *
  2465. * Test validation errors, when validation message is an empty string.
  2466. *
  2467. * @return void
  2468. */
  2469. public function testEmptyErrorValidation()
  2470. {
  2471. $this->article['errors'] = [
  2472. 'Article' => ['title' => ''],
  2473. ];
  2474. $this->Form->create($this->article);
  2475. $result = $this->Form->control('Article.title');
  2476. $expected = [
  2477. 'div' => ['class' => 'input text error'],
  2478. 'label' => ['for' => 'article-title'],
  2479. 'Title',
  2480. '/label',
  2481. 'input' => [
  2482. 'type' => 'text', 'name' => 'Article[title]',
  2483. 'id' => 'article-title', 'class' => 'form-error',
  2484. ],
  2485. ['div' => ['class' => 'error-message']],
  2486. [],
  2487. '/div',
  2488. '/div',
  2489. ];
  2490. $this->assertHtml($expected, $result);
  2491. }
  2492. /**
  2493. * testEmptyControlErrorValidation method
  2494. *
  2495. * Test validation errors, when calling control() overriding validation message by an empty string.
  2496. *
  2497. * @return void
  2498. */
  2499. public function testEmptyControlErrorValidation()
  2500. {
  2501. $this->article['errors'] = [
  2502. 'Article' => ['title' => 'error message'],
  2503. ];
  2504. $this->Form->create($this->article);
  2505. $result = $this->Form->control('Article.title', ['error' => '']);
  2506. $expected = [
  2507. 'div' => ['class' => 'input text error'],
  2508. 'label' => ['for' => 'article-title'],
  2509. 'Title',
  2510. '/label',
  2511. 'input' => [
  2512. 'type' => 'text', 'name' => 'Article[title]',
  2513. 'id' => 'article-title', 'class' => 'form-error',
  2514. ],
  2515. ['div' => ['class' => 'error-message']],
  2516. [],
  2517. '/div',
  2518. '/div',
  2519. ];
  2520. $this->assertHtml($expected, $result);
  2521. }
  2522. /**
  2523. * testControlErrorMessage method
  2524. *
  2525. * Test validation errors, when calling control() overriding validation messages.
  2526. *
  2527. * @return void
  2528. */
  2529. public function testControlErrorMessage()
  2530. {
  2531. $this->article['errors'] = [
  2532. 'title' => ['error message'],
  2533. ];
  2534. $this->Form->create($this->article);
  2535. $result = $this->Form->control('title', [
  2536. 'error' => 'Custom error!',
  2537. ]);
  2538. $expected = [
  2539. 'div' => ['class' => 'input text required error'],
  2540. 'label' => ['for' => 'title'],
  2541. 'Title',
  2542. '/label',
  2543. 'input' => [
  2544. 'type' => 'text', 'name' => 'title',
  2545. 'id' => 'title', 'class' => 'form-error',
  2546. 'required' => 'required',
  2547. ],
  2548. ['div' => ['class' => 'error-message']],
  2549. 'Custom error!',
  2550. '/div',
  2551. '/div',
  2552. ];
  2553. $this->assertHtml($expected, $result);
  2554. $result = $this->Form->control('title', [
  2555. 'error' => ['error message' => 'Custom error!'],
  2556. ]);
  2557. $expected = [
  2558. 'div' => ['class' => 'input text required error'],
  2559. 'label' => ['for' => 'title'],
  2560. 'Title',
  2561. '/label',
  2562. 'input' => [
  2563. 'type' => 'text',
  2564. 'name' => 'title',
  2565. 'id' => 'title',
  2566. 'class' => 'form-error',
  2567. 'required' => 'required',
  2568. ],
  2569. ['div' => ['class' => 'error-message']],
  2570. 'Custom error!',
  2571. '/div',
  2572. '/div',
  2573. ];
  2574. $this->assertHtml($expected, $result);
  2575. }
  2576. /**
  2577. * testFormValidationAssociated method
  2578. *
  2579. * Tests displaying errors for nested entities.
  2580. *
  2581. * @return void
  2582. */
  2583. public function testFormValidationAssociated()
  2584. {
  2585. $nested = new Entity(['foo' => 'bar']);
  2586. $nested->setError('foo', ['not a valid bar']);
  2587. $entity = new Entity(['nested' => $nested]);
  2588. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2589. $result = $this->Form->error('nested.foo');
  2590. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2591. }
  2592. /**
  2593. * testFormValidationAssociatedSecondLevel method
  2594. *
  2595. * Test form error display with associated model.
  2596. *
  2597. * @return void
  2598. */
  2599. public function testFormValidationAssociatedSecondLevel()
  2600. {
  2601. $inner = new Entity(['bar' => 'baz']);
  2602. $nested = new Entity(['foo' => $inner]);
  2603. $entity = new Entity(['nested' => $nested]);
  2604. $inner->setError('bar', ['not a valid one']);
  2605. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2606. $result = $this->Form->error('nested.foo.bar');
  2607. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2608. }
  2609. /**
  2610. * testFormValidationMultiRecord method
  2611. *
  2612. * Test form error display with multiple records.
  2613. *
  2614. * @return void
  2615. */
  2616. public function testFormValidationMultiRecord()
  2617. {
  2618. $one = new Entity();
  2619. $two = new Entity();
  2620. $this->getTableLocator()->get('Contacts', [
  2621. 'className' => __NAMESPACE__ . '\ContactsTable',
  2622. ]);
  2623. $one->set('email', '');
  2624. $one->setError('email', ['invalid email']);
  2625. $two->set('name', '');
  2626. $two->setError('name', ['This is wrong']);
  2627. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2628. $result = $this->Form->control('0.email');
  2629. $expected = [
  2630. 'div' => ['class' => 'input email error'],
  2631. 'label' => ['for' => '0-email'],
  2632. 'Email',
  2633. '/label',
  2634. 'input' => [
  2635. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2636. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2637. ],
  2638. ['div' => ['class' => 'error-message']],
  2639. 'invalid email',
  2640. '/div',
  2641. '/div',
  2642. ];
  2643. $this->assertHtml($expected, $result);
  2644. $result = $this->Form->control('1.name');
  2645. $expected = [
  2646. 'div' => ['class' => 'input text error'],
  2647. 'label' => ['for' => '1-name'],
  2648. 'Name',
  2649. '/label',
  2650. 'input' => [
  2651. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2652. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2653. ],
  2654. ['div' => ['class' => 'error-message']],
  2655. 'This is wrong',
  2656. '/div',
  2657. '/div',
  2658. ];
  2659. $this->assertHtml($expected, $result);
  2660. }
  2661. /**
  2662. * testControl method
  2663. *
  2664. * Test various incarnations of control().
  2665. *
  2666. * @return void
  2667. */
  2668. public function testControl()
  2669. {
  2670. $this->getTableLocator()->get('ValidateUsers', [
  2671. 'className' => __NAMESPACE__ . '\ValidateUsersTable',
  2672. ]);
  2673. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2674. $result = $this->Form->control('ValidateUsers.balance');
  2675. $expected = [
  2676. 'div' => ['class'],
  2677. 'label' => ['for'],
  2678. 'Balance',
  2679. '/label',
  2680. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2681. '/div',
  2682. ];
  2683. $this->assertHtml($expected, $result);
  2684. $result = $this->Form->control('ValidateUser.cost_decimal');
  2685. $expected = [
  2686. 'div' => ['class'],
  2687. 'label' => ['for'],
  2688. 'Cost Decimal',
  2689. '/label',
  2690. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2691. '/div',
  2692. ];
  2693. $this->assertHtml($expected, $result);
  2694. $result = $this->Form->control('ValidateUser.null_decimal');
  2695. $expected = [
  2696. 'div' => ['class'],
  2697. 'label' => ['for'],
  2698. 'Null Decimal',
  2699. '/label',
  2700. 'input' => ['name', 'type' => 'number', 'id'],
  2701. '/div',
  2702. ];
  2703. $this->assertHtml($expected, $result);
  2704. }
  2705. /**
  2706. * testControlCustomization method
  2707. *
  2708. * Tests the input method and passing custom options.
  2709. *
  2710. * @return void
  2711. */
  2712. public function testControlCustomization()
  2713. {
  2714. $this->getTableLocator()->get('Contacts', [
  2715. 'className' => __NAMESPACE__ . '\ContactsTable',
  2716. ]);
  2717. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2718. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2719. $expected = [
  2720. 'div' => ['class' => 'input email'],
  2721. 'label' => ['for' => 'custom'],
  2722. 'Email',
  2723. '/label',
  2724. ['input' => [
  2725. 'type' => 'email', 'name' => 'Contact[email]',
  2726. 'id' => 'custom', 'maxlength' => 255,
  2727. ]],
  2728. '/div',
  2729. ];
  2730. $this->assertHtml($expected, $result);
  2731. $result = $this->Form->control('Contact.email', [
  2732. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2733. ]);
  2734. $expected = [
  2735. '<div',
  2736. 'label' => ['for' => 'contact-email'],
  2737. 'Email',
  2738. '/label',
  2739. ['input' => [
  2740. 'type' => 'email', 'name' => 'Contact[email]',
  2741. 'id' => 'contact-email', 'maxlength' => 255,
  2742. ]],
  2743. '/div',
  2744. ];
  2745. $this->assertHtml($expected, $result);
  2746. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2747. $expected = [
  2748. 'div' => ['class' => 'input text'],
  2749. 'label' => ['for' => 'contact-email'],
  2750. 'Email',
  2751. '/label',
  2752. ['input' => [
  2753. 'type' => 'text', 'name' => 'Contact[email]',
  2754. 'id' => 'contact-email', 'maxlength' => '255',
  2755. ]],
  2756. '/div',
  2757. ];
  2758. $this->assertHtml($expected, $result);
  2759. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2760. $expected = [
  2761. 'div' => ['class' => 'input text'],
  2762. 'label' => ['for' => 'contact-5-email'],
  2763. 'Email',
  2764. '/label',
  2765. ['input' => [
  2766. 'type' => 'text', 'name' => 'Contact[5][email]',
  2767. 'id' => 'contact-5-email', 'maxlength' => '255',
  2768. ]],
  2769. '/div',
  2770. ];
  2771. $this->assertHtml($expected, $result);
  2772. $result = $this->Form->control('Contact.password');
  2773. $expected = [
  2774. 'div' => ['class' => 'input password'],
  2775. 'label' => ['for' => 'contact-password'],
  2776. 'Password',
  2777. '/label',
  2778. ['input' => [
  2779. 'type' => 'password', 'name' => 'Contact[password]',
  2780. 'id' => 'contact-password',
  2781. ]],
  2782. '/div',
  2783. ];
  2784. $this->assertHtml($expected, $result);
  2785. $result = $this->Form->control('Contact.email', [
  2786. 'type' => 'file', 'class' => 'textbox',
  2787. ]);
  2788. $expected = [
  2789. 'div' => ['class' => 'input file'],
  2790. 'label' => ['for' => 'contact-email'],
  2791. 'Email',
  2792. '/label',
  2793. ['input' => [
  2794. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2795. 'id' => 'contact-email',
  2796. ]],
  2797. '/div',
  2798. ];
  2799. $this->assertHtml($expected, $result);
  2800. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2801. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2802. $result = $this->Form->control('phone');
  2803. $expected = [
  2804. 'div' => ['class' => 'input tel'],
  2805. 'label' => ['for' => 'phone'],
  2806. 'Phone',
  2807. '/label',
  2808. ['input' => [
  2809. 'type' => 'tel', 'name' => 'phone',
  2810. 'value' => 'Hello &amp; World &gt; weird chars',
  2811. 'id' => 'phone', 'maxlength' => 255,
  2812. ]],
  2813. '/div',
  2814. ];
  2815. $this->assertHtml($expected, $result);
  2816. $this->View->setRequest(
  2817. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2818. );
  2819. $this->Form->create();
  2820. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2821. $expected = [
  2822. 'div' => ['class' => 'input text'],
  2823. 'label' => ['for' => 'myId'],
  2824. 'Field',
  2825. '/label',
  2826. 'input' => [
  2827. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2828. 'value' => 'My value', 'id' => 'myId',
  2829. ],
  2830. '/div',
  2831. ];
  2832. $this->assertHtml($expected, $result);
  2833. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2834. $this->Form->create();
  2835. $entity->setError('field', 'Badness!');
  2836. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2837. $result = $this->Form->control('field');
  2838. $expected = [
  2839. 'div' => ['class' => 'input text error'],
  2840. 'label' => ['for' => 'field'],
  2841. 'Field',
  2842. '/label',
  2843. 'input' => [
  2844. 'type' => 'text', 'name' => 'field',
  2845. 'id' => 'field', 'class' => 'form-error',
  2846. ],
  2847. ['div' => ['class' => 'error-message']],
  2848. 'Badness!',
  2849. '/div',
  2850. '/div',
  2851. ];
  2852. $this->assertHtml($expected, $result);
  2853. $result = $this->Form->control('field', [
  2854. 'templates' => [
  2855. 'inputContainerError' => '{{content}}{{error}}',
  2856. 'error' => '<span class="error-message">{{content}}</span>',
  2857. ],
  2858. ]);
  2859. $expected = [
  2860. 'label' => ['for' => 'field'],
  2861. 'Field',
  2862. '/label',
  2863. 'input' => [
  2864. 'type' => 'text', 'name' => 'field',
  2865. 'id' => 'field', 'class' => 'form-error',
  2866. ],
  2867. ['span' => ['class' => 'error-message']],
  2868. 'Badness!',
  2869. '/span',
  2870. ];
  2871. $this->assertHtml($expected, $result);
  2872. $entity->setError('field', ['minLength'], true);
  2873. $result = $this->Form->control('field', [
  2874. 'error' => [
  2875. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2876. 'maxLength' => 'login too large',
  2877. ],
  2878. ]);
  2879. $expected = [
  2880. 'div' => ['class' => 'input text error'],
  2881. 'label' => ['for' => 'field'],
  2882. 'Field',
  2883. '/label',
  2884. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2885. ['div' => ['class' => 'error-message']],
  2886. 'Le login doit contenir au moins 2 caractères',
  2887. '/div',
  2888. '/div',
  2889. ];
  2890. $this->assertHtml($expected, $result);
  2891. $entity->setError('field', ['maxLength'], true);
  2892. $result = $this->Form->control('field', [
  2893. 'error' => [
  2894. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2895. 'maxLength' => 'login too large',
  2896. ],
  2897. ]);
  2898. $expected = [
  2899. 'div' => ['class' => 'input text error'],
  2900. 'label' => ['for' => 'field'],
  2901. 'Field',
  2902. '/label',
  2903. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2904. ['div' => ['class' => 'error-message']],
  2905. 'login too large',
  2906. '/div',
  2907. '/div',
  2908. ];
  2909. $this->assertHtml($expected, $result);
  2910. }
  2911. /**
  2912. * testControlWithTemplateFile method
  2913. *
  2914. * Test that control() accepts a template file.
  2915. *
  2916. * @return void
  2917. */
  2918. public function testControlWithTemplateFile()
  2919. {
  2920. $result = $this->Form->control('field', [
  2921. 'templates' => 'htmlhelper_tags',
  2922. ]);
  2923. $expected = [
  2924. 'label' => ['for' => 'field'],
  2925. 'Field',
  2926. '/label',
  2927. 'input' => [
  2928. 'type' => 'text', 'name' => 'field',
  2929. 'id' => 'field',
  2930. ],
  2931. ];
  2932. $this->assertHtml($expected, $result);
  2933. }
  2934. /**
  2935. * testNestedControlsEndWithBrackets method
  2936. *
  2937. * Test that nested inputs end with brackets.
  2938. *
  2939. * @return void
  2940. */
  2941. public function testNestedControlsEndWithBrackets()
  2942. {
  2943. $result = $this->Form->text('nested.text[]');
  2944. $expected = [
  2945. 'input' => [
  2946. 'type' => 'text', 'name' => 'nested[text][]',
  2947. ],
  2948. ];
  2949. $this->assertHtml($expected, $result);
  2950. $result = $this->Form->file('nested.file[]');
  2951. $expected = [
  2952. 'input' => [
  2953. 'type' => 'file', 'name' => 'nested[file][]',
  2954. ],
  2955. ];
  2956. $this->assertHtml($expected, $result);
  2957. }
  2958. /**
  2959. * testCreateIdPrefix method
  2960. *
  2961. * Test id prefix.
  2962. *
  2963. * @return void
  2964. */
  2965. public function testCreateIdPrefix()
  2966. {
  2967. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2968. $result = $this->Form->control('field');
  2969. $expected = [
  2970. 'div' => ['class' => 'input text'],
  2971. 'label' => ['for' => 'prefix-field'],
  2972. 'Field',
  2973. '/label',
  2974. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2975. '/div',
  2976. ];
  2977. $this->assertHtml($expected, $result);
  2978. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2979. $expected = [
  2980. 'div' => ['class' => 'input text'],
  2981. 'label' => ['for' => 'custom-id'],
  2982. 'Field',
  2983. '/label',
  2984. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2985. '/div',
  2986. ];
  2987. $this->assertHtml($expected, $result);
  2988. $result = $this->Form->radio('Model.field', ['option A']);
  2989. $expected = [
  2990. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2991. 'label' => ['for' => 'prefix-model-field-0'],
  2992. ['input' => [
  2993. 'type' => 'radio',
  2994. 'name' => 'Model[field]',
  2995. 'value' => '0',
  2996. 'id' => 'prefix-model-field-0',
  2997. ]],
  2998. 'option A',
  2999. '/label',
  3000. ];
  3001. $this->assertHtml($expected, $result);
  3002. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  3003. $expected = [
  3004. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3005. 'label' => ['for' => 'prefix-model-field-0'],
  3006. ['input' => [
  3007. 'type' => 'radio',
  3008. 'name' => 'Model[field]',
  3009. 'value' => '0',
  3010. 'id' => 'prefix-model-field-0',
  3011. ]],
  3012. 'option A',
  3013. '/label',
  3014. ];
  3015. $this->assertHtml($expected, $result);
  3016. $result = $this->Form->select(
  3017. 'Model.multi_field',
  3018. ['first'],
  3019. ['multiple' => 'checkbox']
  3020. );
  3021. $expected = [
  3022. 'input' => [
  3023. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  3024. ],
  3025. ['div' => ['class' => 'checkbox']],
  3026. ['label' => ['for' => 'prefix-model-multi-field-0']],
  3027. ['input' => [
  3028. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3029. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  3030. ]],
  3031. 'first',
  3032. '/label',
  3033. '/div',
  3034. ];
  3035. $this->assertHtml($expected, $result);
  3036. $this->Form->end();
  3037. $result = $this->Form->control('field');
  3038. $expected = [
  3039. 'div' => ['class' => 'input text'],
  3040. 'label' => ['for' => 'field'],
  3041. 'Field',
  3042. '/label',
  3043. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3044. '/div',
  3045. ];
  3046. $this->assertHtml($expected, $result);
  3047. }
  3048. /**
  3049. * testControlZero method
  3050. *
  3051. * Test that inputs with 0 can be created.
  3052. *
  3053. * @return void
  3054. */
  3055. public function testControlZero()
  3056. {
  3057. $this->getTableLocator()->get('Contacts', [
  3058. 'className' => __NAMESPACE__ . '\ContactsTable',
  3059. ]);
  3060. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3061. $result = $this->Form->control('0');
  3062. $expected = [
  3063. 'div' => ['class' => 'input text'],
  3064. 'label' => ['for' => '0'], '/label',
  3065. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3066. '/div',
  3067. ];
  3068. $this->assertHtml($expected, $result);
  3069. }
  3070. /**
  3071. * testControlCheckbox method
  3072. *
  3073. * Test control() with checkbox creation.
  3074. *
  3075. * @return void
  3076. */
  3077. public function testControlCheckbox()
  3078. {
  3079. $articles = $this->getTableLocator()->get('Articles');
  3080. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3081. $article = $articles->newEntity();
  3082. $this->Form->create($article);
  3083. $result = $this->Form->control('Articles.active');
  3084. $expected = [
  3085. 'div' => ['class' => 'input checkbox'],
  3086. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3087. 'label' => ['for' => 'articles-active'],
  3088. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3089. 'Active',
  3090. '/label',
  3091. '/div',
  3092. ];
  3093. $this->assertHtml($expected, $result);
  3094. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3095. $expected = [
  3096. 'div' => ['class' => 'input checkbox'],
  3097. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3098. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3099. '/div',
  3100. ];
  3101. $this->assertHtml($expected, $result);
  3102. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3103. $expected = [
  3104. 'div' => ['class' => 'input checkbox'],
  3105. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3106. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3107. '/div',
  3108. ];
  3109. $this->assertHtml($expected, $result);
  3110. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3111. $expected = [
  3112. 'div' => ['class' => 'input checkbox'],
  3113. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3114. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3115. '/div',
  3116. ];
  3117. $this->assertHtml($expected, $result);
  3118. $result = $this->Form->control('Articles.disabled', [
  3119. 'label' => 'Disabled',
  3120. 'type' => 'checkbox',
  3121. 'data-foo' => 'disabled',
  3122. ]);
  3123. $expected = [
  3124. 'div' => ['class' => 'input checkbox'],
  3125. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3126. 'label' => ['for' => 'articles-disabled'],
  3127. ['input' => [
  3128. 'type' => 'checkbox',
  3129. 'name' => 'Articles[disabled]',
  3130. 'value' => '1',
  3131. 'id' => 'articles-disabled',
  3132. 'data-foo' => 'disabled',
  3133. ]],
  3134. 'Disabled',
  3135. '/label',
  3136. '/div',
  3137. ];
  3138. $this->assertHtml($expected, $result);
  3139. $result = $this->Form->control('Articles.confirm', [
  3140. 'label' => 'Confirm <b>me</b>!',
  3141. 'type' => 'checkbox',
  3142. 'escape' => false,
  3143. ]);
  3144. $expected = [
  3145. 'div' => ['class' => 'input checkbox'],
  3146. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3147. 'label' => ['for' => 'articles-confirm'],
  3148. ['input' => [
  3149. 'type' => 'checkbox',
  3150. 'name' => 'Articles[confirm]',
  3151. 'value' => '1',
  3152. 'id' => 'articles-confirm',
  3153. ]],
  3154. 'Confirm <b>me</b>!',
  3155. '/label',
  3156. '/div',
  3157. ];
  3158. $this->assertHtml($expected, $result);
  3159. }
  3160. /**
  3161. * testControlHidden method
  3162. *
  3163. * Test that control() does not create wrapping div and label tag for hidden fields.
  3164. *
  3165. * @return void
  3166. */
  3167. public function testControlHidden()
  3168. {
  3169. $this->getTableLocator()->get('ValidateUsers', [
  3170. 'className' => __NAMESPACE__ . '\ValidateUsersTable',
  3171. ]);
  3172. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3173. $result = $this->Form->control('ValidateUser.id');
  3174. $expected = [
  3175. 'input' => ['name', 'type' => 'hidden', 'id'],
  3176. ];
  3177. $this->assertHtml($expected, $result);
  3178. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3179. $expected = [
  3180. 'input' => ['name', 'type' => 'hidden', 'id'],
  3181. ];
  3182. $this->assertHtml($expected, $result);
  3183. }
  3184. /**
  3185. * testControlDatetime method
  3186. *
  3187. * Test form->control() with datetime.
  3188. *
  3189. * @return void
  3190. */
  3191. public function testControlDatetime()
  3192. {
  3193. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3194. ->setMethods(['datetime'])
  3195. ->setConstructorArgs([new View()])
  3196. ->getMock();
  3197. $this->Form->expects($this->once())->method('datetime')
  3198. ->with('prueba', [
  3199. 'type' => 'datetime',
  3200. 'timeFormat' => 24,
  3201. 'minYear' => 2008,
  3202. 'maxYear' => 2011,
  3203. 'interval' => 15,
  3204. 'options' => null,
  3205. 'empty' => false,
  3206. 'id' => 'prueba',
  3207. 'required' => false,
  3208. 'templateVars' => [],
  3209. ])
  3210. ->will($this->returnValue('This is it!'));
  3211. $result = $this->Form->control('prueba', [
  3212. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3213. 'maxYear' => 2011, 'interval' => 15,
  3214. ]);
  3215. $expected = [
  3216. 'div' => ['class' => 'input datetime'],
  3217. '<label',
  3218. 'Prueba',
  3219. '/label',
  3220. 'This is it!',
  3221. '/div',
  3222. ];
  3223. $this->assertHtml($expected, $result);
  3224. }
  3225. /**
  3226. * testControlDatetimeIdPrefix method
  3227. *
  3228. * Test form->control() with datetime with id prefix.
  3229. *
  3230. * @return void
  3231. */
  3232. public function testControlDatetimeIdPrefix()
  3233. {
  3234. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3235. ->setMethods(['datetime'])
  3236. ->setConstructorArgs([new View()])
  3237. ->getMock();
  3238. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3239. $this->Form->expects($this->once())->method('datetime')
  3240. ->with('prueba', [
  3241. 'type' => 'datetime',
  3242. 'timeFormat' => 24,
  3243. 'minYear' => 2008,
  3244. 'maxYear' => 2011,
  3245. 'interval' => 15,
  3246. 'options' => null,
  3247. 'empty' => false,
  3248. 'id' => 'prefix-prueba',
  3249. 'required' => false,
  3250. 'templateVars' => [],
  3251. ])
  3252. ->will($this->returnValue('This is it!'));
  3253. $result = $this->Form->control('prueba', [
  3254. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3255. 'maxYear' => 2011, 'interval' => 15,
  3256. ]);
  3257. $expected = [
  3258. 'div' => ['class' => 'input datetime'],
  3259. '<label',
  3260. 'Prueba',
  3261. '/label',
  3262. 'This is it!',
  3263. '/div',
  3264. ];
  3265. $this->assertHtml($expected, $result);
  3266. }
  3267. /**
  3268. * testControlCheckboxWithDisabledElements method
  3269. *
  3270. * Test generating checkboxes with disabled elements.
  3271. *
  3272. * @return void
  3273. */
  3274. public function testControlCheckboxWithDisabledElements()
  3275. {
  3276. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3277. $result = $this->Form->control('Contact.multiple', [
  3278. 'multiple' => 'checkbox',
  3279. 'disabled' => 'disabled',
  3280. 'options' => $options,
  3281. ]);
  3282. $expected = [
  3283. ['div' => ['class' => 'input select']],
  3284. ['label' => ['for' => 'contact-multiple']],
  3285. 'Multiple',
  3286. '/label',
  3287. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3288. ['div' => ['class' => 'checkbox']],
  3289. ['label' => ['for' => 'contact-multiple-1']],
  3290. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3291. 'One',
  3292. '/label',
  3293. '/div',
  3294. ['div' => ['class' => 'checkbox']],
  3295. ['label' => ['for' => 'contact-multiple-2']],
  3296. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3297. 'Two',
  3298. '/label',
  3299. '/div',
  3300. ['div' => ['class' => 'checkbox']],
  3301. ['label' => ['for' => 'contact-multiple-3']],
  3302. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3303. 'Three',
  3304. '/label',
  3305. '/div',
  3306. '/div',
  3307. ];
  3308. $this->assertHtml($expected, $result);
  3309. // make sure 50 does only disable 50, and not 50f5c0cf
  3310. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3311. $disabled = [50];
  3312. $expected = [
  3313. ['div' => ['class' => 'input select']],
  3314. ['label' => ['for' => 'contact-multiple']],
  3315. 'Multiple',
  3316. '/label',
  3317. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3318. ['div' => ['class' => 'checkbox']],
  3319. ['label' => ['for' => 'contact-multiple-50']],
  3320. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3321. 'Fifty',
  3322. '/label',
  3323. '/div',
  3324. ['div' => ['class' => 'checkbox']],
  3325. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3326. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3327. 'Stringy',
  3328. '/label',
  3329. '/div',
  3330. '/div',
  3331. ];
  3332. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3333. $this->assertHtml($expected, $result);
  3334. }
  3335. /**
  3336. * testControlWithLeadingInteger method
  3337. *
  3338. * Test input name with leading integer, ensure attributes are generated correctly.
  3339. *
  3340. * @return void
  3341. */
  3342. public function testControlWithLeadingInteger()
  3343. {
  3344. $result = $this->Form->text('0.Node.title');
  3345. $expected = [
  3346. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3347. ];
  3348. $this->assertHtml($expected, $result);
  3349. }
  3350. /**
  3351. * testControlSelectType method
  3352. *
  3353. * Test form->control() with select type inputs.
  3354. *
  3355. * @return void
  3356. */
  3357. public function testControlSelectType()
  3358. {
  3359. $result = $this->Form->control(
  3360. 'email',
  3361. [
  3362. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3363. );
  3364. $expected = [
  3365. 'div' => ['class' => 'input select'],
  3366. 'label' => ['for' => 'email'],
  3367. 'Email',
  3368. '/label',
  3369. ['select' => ['name' => 'email', 'id' => 'email']],
  3370. ['option' => ['value' => '']],
  3371. '/option',
  3372. ['option' => ['value' => 'è']],
  3373. 'Firést',
  3374. '/option',
  3375. ['option' => ['value' => 'é']],
  3376. 'Secoènd',
  3377. '/option',
  3378. '/select',
  3379. '/div',
  3380. ];
  3381. $this->assertHtml($expected, $result);
  3382. $result = $this->Form->control(
  3383. 'email',
  3384. [
  3385. 'options' => ['First', 'Second'], 'empty' => true]
  3386. );
  3387. $expected = [
  3388. 'div' => ['class' => 'input select'],
  3389. 'label' => ['for' => 'email'],
  3390. 'Email',
  3391. '/label',
  3392. ['select' => ['name' => 'email', 'id' => 'email']],
  3393. ['option' => ['value' => '']],
  3394. '/option',
  3395. ['option' => ['value' => '0']],
  3396. 'First',
  3397. '/option',
  3398. ['option' => ['value' => '1']],
  3399. 'Second',
  3400. '/option',
  3401. '/select',
  3402. '/div',
  3403. ];
  3404. $this->assertHtml($expected, $result);
  3405. $result = $this->Form->control('email', [
  3406. 'type' => 'select',
  3407. 'options' => new \ArrayObject(['First', 'Second']),
  3408. 'empty' => true,
  3409. ]);
  3410. $this->assertHtml($expected, $result);
  3411. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3412. $this->View->setRequest(
  3413. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3414. );
  3415. $this->Form->create();
  3416. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3417. $expected = [
  3418. 'div' => ['class' => 'input select'],
  3419. 'label' => ['for' => 'model-user-id'],
  3420. 'User',
  3421. '/label',
  3422. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3423. ['option' => ['value' => '']],
  3424. '/option',
  3425. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3426. 'good',
  3427. '/option',
  3428. ['option' => ['value' => 'other']],
  3429. 'bad',
  3430. '/option',
  3431. '/select',
  3432. '/div',
  3433. ];
  3434. $this->assertHtml($expected, $result);
  3435. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3436. $this->View->setRequest(
  3437. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3438. );
  3439. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3440. $expected = [
  3441. 'div' => ['class' => 'input select'],
  3442. 'label' => ['for' => 'thing-user-id'],
  3443. 'User',
  3444. '/label',
  3445. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3446. ['option' => ['value' => '']],
  3447. 'Some Empty',
  3448. '/option',
  3449. ['option' => ['value' => 'value']],
  3450. 'good',
  3451. '/option',
  3452. ['option' => ['value' => 'other']],
  3453. 'bad',
  3454. '/option',
  3455. '/select',
  3456. '/div',
  3457. ];
  3458. $this->assertHtml($expected, $result);
  3459. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3460. $this->View->setRequest(
  3461. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3462. );
  3463. $this->Form->create();
  3464. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3465. $expected = [
  3466. 'div' => ['class' => 'input select'],
  3467. 'label' => ['for' => 'thing-user-id'],
  3468. 'User',
  3469. '/label',
  3470. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3471. ['option' => ['value' => '']],
  3472. 'Some Empty',
  3473. '/option',
  3474. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3475. 'good',
  3476. '/option',
  3477. ['option' => ['value' => 'other']],
  3478. 'bad',
  3479. '/option',
  3480. '/select',
  3481. '/div',
  3482. ];
  3483. $this->assertHtml($expected, $result);
  3484. $result = $this->Form->control('Publisher.id', [
  3485. 'label' => 'Publisher',
  3486. 'type' => 'select',
  3487. 'multiple' => 'checkbox',
  3488. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3489. ]);
  3490. $expected = [
  3491. ['div' => ['class' => 'input select']],
  3492. ['label' => ['for' => 'publisher-id']],
  3493. 'Publisher',
  3494. '/label',
  3495. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3496. ['div' => ['class' => 'checkbox']],
  3497. ['label' => ['for' => 'publisher-id-value-1']],
  3498. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3499. 'Label 1',
  3500. '/label',
  3501. '/div',
  3502. ['div' => ['class' => 'checkbox']],
  3503. ['label' => ['for' => 'publisher-id-value-2']],
  3504. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3505. 'Label 2',
  3506. '/label',
  3507. '/div',
  3508. '/div',
  3509. ];
  3510. $this->assertHtml($expected, $result);
  3511. }
  3512. /**
  3513. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3514. *
  3515. * Test that control() and a non standard primary key makes a hidden input by default.
  3516. *
  3517. * @return void
  3518. */
  3519. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3520. {
  3521. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3522. $this->Form->create($this->article);
  3523. $result = $this->Form->control('title');
  3524. $expected = [
  3525. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3526. ];
  3527. $this->assertHtml($expected, $result);
  3528. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3529. $this->Form->create($this->article);
  3530. $result = $this->Form->control('title');
  3531. $expected = [
  3532. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3533. ];
  3534. $this->assertHtml($expected, $result);
  3535. $result = $this->Form->control('body');
  3536. $expected = [
  3537. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3538. ];
  3539. $this->assertHtml($expected, $result);
  3540. }
  3541. /**
  3542. * testControlOverridingMagicSelectType method
  3543. *
  3544. * Test that overriding the magic select type widget is possible.
  3545. *
  3546. * @return void
  3547. */
  3548. public function testControlOverridingMagicSelectType()
  3549. {
  3550. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3551. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3552. $expected = [
  3553. 'div' => ['class' => 'input text'],
  3554. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3555. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3556. '/div',
  3557. ];
  3558. $this->assertHtml($expected, $result);
  3559. //Check that magic types still work for plural/singular vars
  3560. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3561. $result = $this->Form->control('Model.type');
  3562. $expected = [
  3563. 'div' => ['class' => 'input select'],
  3564. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3565. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3566. ['option' => ['value' => 'value']], 'good', '/option',
  3567. ['option' => ['value' => 'other']], 'bad', '/option',
  3568. '/select',
  3569. '/div',
  3570. ];
  3571. $this->assertHtml($expected, $result);
  3572. }
  3573. /**
  3574. * testControlMagicTypeDoesNotOverride method
  3575. *
  3576. * Test that inferred types do not override developer input.
  3577. *
  3578. * @return void
  3579. */
  3580. public function testControlMagicTypeDoesNotOverride()
  3581. {
  3582. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3583. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3584. $expected = [
  3585. 'div' => ['class' => 'input checkbox'],
  3586. ['input' => [
  3587. 'type' => 'hidden',
  3588. 'name' => 'Model[user]',
  3589. 'value' => 0,
  3590. ]],
  3591. 'label' => ['for' => 'model-user'],
  3592. ['input' => [
  3593. 'name' => 'Model[user]',
  3594. 'type' => 'checkbox',
  3595. 'id' => 'model-user',
  3596. 'value' => 1,
  3597. ]],
  3598. 'User',
  3599. '/label',
  3600. '/div',
  3601. ];
  3602. $this->assertHtml($expected, $result);
  3603. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3604. $options = [
  3605. 1 => 'blue',
  3606. 2 => 'red',
  3607. ];
  3608. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3609. $expected = [
  3610. 'div' => ['class' => 'input select'],
  3611. 'label' => ['for' => 'tags-ids'],
  3612. 'Tags',
  3613. '/label',
  3614. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3615. ['div' => ['class' => 'checkbox']],
  3616. ['label' => ['for' => 'tags-ids-1']],
  3617. ['input' => [
  3618. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3619. 'value' => '1', 'name' => 'tags[_ids][]',
  3620. ]],
  3621. 'blue',
  3622. '/label',
  3623. '/div',
  3624. ['div' => ['class' => 'checkbox']],
  3625. ['label' => ['for' => 'tags-ids-2']],
  3626. ['input' => [
  3627. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3628. 'value' => '2', 'name' => 'tags[_ids][]',
  3629. ]],
  3630. 'red',
  3631. '/label',
  3632. '/div',
  3633. '/div',
  3634. ];
  3635. $this->assertHtml($expected, $result);
  3636. }
  3637. /**
  3638. * testControlMagicSelectForTypeNumber method
  3639. *
  3640. * Test that magic control() selects are created for type=number.
  3641. *
  3642. * @return void
  3643. */
  3644. public function testControlMagicSelectForTypeNumber()
  3645. {
  3646. $this->getTableLocator()->get('ValidateUsers', [
  3647. 'className' => __NAMESPACE__ . '\ValidateUsersTable',
  3648. ]);
  3649. $entity = new Entity(['balance' => 1]);
  3650. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3651. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3652. $result = $this->Form->control('balance');
  3653. $expected = [
  3654. 'div' => ['class' => 'input select'],
  3655. 'label' => ['for' => 'balance'],
  3656. 'Balance',
  3657. '/label',
  3658. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3659. ['option' => ['value' => '0']],
  3660. 'nothing',
  3661. '/option',
  3662. ['option' => ['value' => '1', 'selected' => 'selected']],
  3663. 'some',
  3664. '/option',
  3665. ['option' => ['value' => '100']],
  3666. 'a lot',
  3667. '/option',
  3668. '/select',
  3669. '/div',
  3670. ];
  3671. $this->assertHtml($expected, $result);
  3672. }
  3673. /**
  3674. * testInvalidControlTypeOption method
  3675. *
  3676. * Test invalid 'input' type option to control() function.
  3677. *
  3678. * @return void
  3679. */
  3680. public function testInvalidControlTypeOption()
  3681. {
  3682. $this->expectException(\RuntimeException::class);
  3683. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3684. $this->Form->control('text', ['type' => 'input']);
  3685. }
  3686. /**
  3687. * testControlMagicSelectChangeToRadio method
  3688. *
  3689. * Test that magic control() selects can easily be converted into radio types without error.
  3690. *
  3691. * @return void
  3692. */
  3693. public function testControlMagicSelectChangeToRadio()
  3694. {
  3695. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3696. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3697. $this->assertContains('input type="radio"', $result);
  3698. }
  3699. /**
  3700. * testFormControlSubmit method
  3701. *
  3702. * Test correct results for form::control() and type submit.
  3703. *
  3704. * @return void
  3705. */
  3706. public function testFormControlSubmit()
  3707. {
  3708. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3709. $expected = [
  3710. 'div' => ['class' => 'submit'],
  3711. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3712. '/div',
  3713. ];
  3714. $this->assertHtml($expected, $result);
  3715. }
  3716. /**
  3717. * testFormControls method
  3718. *
  3719. * Test correct results from Form::controls().
  3720. *
  3721. * @return void
  3722. */
  3723. public function testFormControlsLegendFieldset()
  3724. {
  3725. $this->Form->create($this->article);
  3726. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3727. $expected = [
  3728. '<fieldset',
  3729. '<legend',
  3730. 'The Legend',
  3731. '/legend',
  3732. '*/fieldset',
  3733. ];
  3734. $this->assertHtml($expected, $result);
  3735. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3736. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3737. $this->assertContains('<fieldset>', $result);
  3738. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3739. $this->assertNotContains('<legend>', $result);
  3740. $this->assertNotContains('<fieldset>', $result);
  3741. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3742. $this->assertNotContains('<legend>', $result);
  3743. $this->assertNotContains('<fieldset>', $result);
  3744. $this->Form->create($this->article);
  3745. $this->View->setRequest($this->View->getRequest()
  3746. ->withParam('prefix', 'admin')
  3747. ->withParam('action', 'admin_edit')
  3748. ->withParam('controller', 'articles'));
  3749. $result = $this->Form->allControls();
  3750. $expected = [
  3751. '<fieldset',
  3752. '<legend',
  3753. 'New Article',
  3754. '/legend',
  3755. '*/fieldset',
  3756. ];
  3757. $this->assertHtml($expected, $result);
  3758. $this->Form->create($this->article);
  3759. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3760. $expected = [
  3761. '<fieldset',
  3762. '<legend',
  3763. 'The Legend',
  3764. '/legend',
  3765. '*/fieldset',
  3766. ];
  3767. $this->assertHtml($expected, $result);
  3768. $this->Form->create($this->article);
  3769. $result = $this->Form->allControls([], [
  3770. 'fieldset' => [
  3771. 'class' => 'some-class some-other-class',
  3772. 'disabled' => true,
  3773. 'data-param' => 'a-param',
  3774. ],
  3775. 'legend' => 'The Legend',
  3776. ]);
  3777. $expected = [
  3778. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3779. '<legend',
  3780. 'The Legend',
  3781. '/legend',
  3782. '*/fieldset',
  3783. ];
  3784. $this->assertHtml($expected, $result);
  3785. }
  3786. /**
  3787. * testFormControls method
  3788. *
  3789. * Test the controls() method.
  3790. *
  3791. * @return void
  3792. */
  3793. public function testFormControls()
  3794. {
  3795. $this->Form->create($this->article);
  3796. $result = $this->Form->allControls();
  3797. $expected = [
  3798. '<fieldset',
  3799. '<legend', 'New Article', '/legend',
  3800. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3801. ['div' => ['class' => 'input select required']],
  3802. '*/div',
  3803. ['div' => ['class' => 'input text required']],
  3804. '*/div',
  3805. ['div' => ['class' => 'input text']],
  3806. '*/div',
  3807. ['div' => ['class' => 'input text']],
  3808. '*/div',
  3809. '/fieldset',
  3810. ];
  3811. $this->assertHtml($expected, $result);
  3812. $result = $this->Form->allControls([
  3813. 'published' => ['type' => 'boolean'],
  3814. ]);
  3815. $expected = [
  3816. '<fieldset',
  3817. '<legend', 'New Article', '/legend',
  3818. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3819. ['div' => ['class' => 'input select required']],
  3820. '*/div',
  3821. ['div' => ['class' => 'input text required']],
  3822. '*/div',
  3823. ['div' => ['class' => 'input text']],
  3824. '*/div',
  3825. ['div' => ['class' => 'input boolean']],
  3826. '*/div',
  3827. '/fieldset',
  3828. ];
  3829. $this->assertHtml($expected, $result);
  3830. $this->Form->create($this->article);
  3831. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3832. $expected = [
  3833. 'fieldset' => [],
  3834. 'legend' => [],
  3835. 'Hello',
  3836. '/legend',
  3837. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3838. ['div' => ['class' => 'input select required']],
  3839. '*/div',
  3840. ['div' => ['class' => 'input text required']],
  3841. '*/div',
  3842. ['div' => ['class' => 'input text']],
  3843. '*/div',
  3844. ['div' => ['class' => 'input text']],
  3845. '*/div',
  3846. '/fieldset',
  3847. ];
  3848. $this->assertHtml($expected, $result);
  3849. $this->Form->create(false);
  3850. $expected = [
  3851. 'fieldset' => [],
  3852. ['div' => ['class' => 'input text']],
  3853. 'label' => ['for' => 'foo'],
  3854. 'Foo',
  3855. '/label',
  3856. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3857. '*/div',
  3858. '/fieldset',
  3859. ];
  3860. $result = $this->Form->allControls(
  3861. ['foo' => ['type' => 'text']],
  3862. ['legend' => false]
  3863. );
  3864. $this->assertHtml($expected, $result);
  3865. }
  3866. /**
  3867. * testFormControlsBlacklist method
  3868. *
  3869. * @return void
  3870. */
  3871. public function testFormControlsBlacklist()
  3872. {
  3873. $this->Form->create($this->article);
  3874. $result = $this->Form->allControls([
  3875. 'id' => false,
  3876. ]);
  3877. $expected = [
  3878. '<fieldset',
  3879. '<legend', 'New Article', '/legend',
  3880. ['div' => ['class' => 'input select required']],
  3881. '*/div',
  3882. ['div' => ['class' => 'input text required']],
  3883. '*/div',
  3884. ['div' => ['class' => 'input text']],
  3885. '*/div',
  3886. ['div' => ['class' => 'input text']],
  3887. '*/div',
  3888. '/fieldset',
  3889. ];
  3890. $this->assertHtml($expected, $result);
  3891. $this->Form->create($this->article);
  3892. $result = $this->Form->allControls([
  3893. 'id' => [],
  3894. ]);
  3895. $expected = [
  3896. '<fieldset',
  3897. '<legend', 'New Article', '/legend',
  3898. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3899. ['div' => ['class' => 'input select required']],
  3900. '*/div',
  3901. ['div' => ['class' => 'input text required']],
  3902. '*/div',
  3903. ['div' => ['class' => 'input text']],
  3904. '*/div',
  3905. ['div' => ['class' => 'input text']],
  3906. '*/div',
  3907. '/fieldset',
  3908. ];
  3909. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3910. }
  3911. /**
  3912. * testSelectAsCheckbox method
  3913. *
  3914. * Test multi-select widget with checkbox formatting.
  3915. *
  3916. * @return void
  3917. */
  3918. public function testSelectAsCheckbox()
  3919. {
  3920. $result = $this->Form->select(
  3921. 'Model.multi_field',
  3922. ['first', 'second', 'third'],
  3923. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3924. );
  3925. $expected = [
  3926. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3927. ['div' => ['class' => 'checkbox']],
  3928. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3929. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3930. 'first',
  3931. '/label',
  3932. '/div',
  3933. ['div' => ['class' => 'checkbox']],
  3934. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3935. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3936. 'second',
  3937. '/label',
  3938. '/div',
  3939. ['div' => ['class' => 'checkbox']],
  3940. ['label' => ['for' => 'model-multi-field-2']],
  3941. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3942. 'third',
  3943. '/label',
  3944. '/div',
  3945. ];
  3946. $this->assertHtml($expected, $result);
  3947. $result = $this->Form->select(
  3948. 'Model.multi_field',
  3949. ['1/2' => 'half'],
  3950. ['multiple' => 'checkbox']
  3951. );
  3952. $expected = [
  3953. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3954. ['div' => ['class' => 'checkbox']],
  3955. ['label' => ['for' => 'model-multi-field-1-2']],
  3956. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3957. 'half',
  3958. '/label',
  3959. '/div',
  3960. ];
  3961. $this->assertHtml($expected, $result);
  3962. }
  3963. /**
  3964. * testLabel method
  3965. *
  3966. * Test label generation.
  3967. *
  3968. * @return void
  3969. */
  3970. public function testLabel()
  3971. {
  3972. $result = $this->Form->label('Person.name');
  3973. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3974. $this->assertHtml($expected, $result);
  3975. $result = $this->Form->label('Person.name');
  3976. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3977. $this->assertHtml($expected, $result);
  3978. $result = $this->Form->label('Person.first_name');
  3979. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3980. $this->assertHtml($expected, $result);
  3981. $result = $this->Form->label('Person.first_name', 'Your first name');
  3982. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3983. $this->assertHtml($expected, $result);
  3984. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3985. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3986. $this->assertHtml($expected, $result);
  3987. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3988. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3989. $this->assertHtml($expected, $result);
  3990. $result = $this->Form->label('Person.first_name', '');
  3991. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3992. $this->assertHtml($expected, $result);
  3993. $result = $this->Form->label('Person.2.name', '');
  3994. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3995. $this->assertHtml($expected, $result);
  3996. }
  3997. /**
  3998. * testLabelContainControl method
  3999. *
  4000. * Test that label() can accept an input with the correct template vars.
  4001. *
  4002. * @return void
  4003. */
  4004. public function testLabelContainControl()
  4005. {
  4006. $this->Form->setTemplates([
  4007. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4008. ]);
  4009. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  4010. 'input' => '<input type="checkbox" name="accept_tos"/>',
  4011. ]);
  4012. $expected = [
  4013. 'label' => ['for' => 'person-accept-terms'],
  4014. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  4015. 'Accept',
  4016. '/label',
  4017. ];
  4018. $this->assertHtml($expected, $result);
  4019. }
  4020. /**
  4021. * testTextbox method
  4022. *
  4023. * Test textbox element generation.
  4024. *
  4025. * @return void
  4026. */
  4027. public function testTextbox()
  4028. {
  4029. $result = $this->Form->text('Model.field');
  4030. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  4031. $this->assertHtml($expected, $result);
  4032. $result = $this->Form->text('Model.field', ['type' => 'password']);
  4033. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  4034. $this->assertHtml($expected, $result);
  4035. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  4036. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  4037. $this->assertHtml($expected, $result);
  4038. }
  4039. /**
  4040. * testTextBoxDataAndError method
  4041. *
  4042. * Test that text() hooks up with request data and error fields.
  4043. *
  4044. * @return void
  4045. */
  4046. public function testTextBoxDataAndError()
  4047. {
  4048. $this->article['errors'] = [
  4049. 'Contact' => ['text' => 'wrong'],
  4050. ];
  4051. $this->View->setRequest($this->View->getRequest()
  4052. ->withData('Model.text', 'test <strong>HTML</strong> values')
  4053. ->withData('Contact.text', 'test'));
  4054. $this->Form->create($this->article);
  4055. $result = $this->Form->text('Model.text');
  4056. $expected = [
  4057. 'input' => [
  4058. 'type' => 'text',
  4059. 'name' => 'Model[text]',
  4060. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4061. ],
  4062. ];
  4063. $this->assertHtml($expected, $result);
  4064. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4065. $expected = [
  4066. 'input' => [
  4067. 'type' => 'text',
  4068. 'name' => 'Contact[text]',
  4069. 'value' => 'test',
  4070. 'id' => 'theID',
  4071. 'class' => 'form-error',
  4072. ],
  4073. ];
  4074. $this->assertHtml($expected, $result);
  4075. }
  4076. /**
  4077. * testDefaultValue method
  4078. *
  4079. * Test default value setting.
  4080. *
  4081. * @return void
  4082. */
  4083. public function testTextDefaultValue()
  4084. {
  4085. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  4086. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4087. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4088. $this->assertHtml($expected, $result);
  4089. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4090. $this->Form->create();
  4091. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4092. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4093. $this->assertHtml($expected, $result);
  4094. $Articles = $this->getTableLocator()->get('Articles');
  4095. $title = $Articles->getSchema()->getColumn('title');
  4096. $Articles->getSchema()->addColumn(
  4097. 'title',
  4098. ['default' => 'default title'] + $title
  4099. );
  4100. $entity = $Articles->newEntity();
  4101. $this->Form->create($entity);
  4102. // Get default value from schema
  4103. $result = $this->Form->text('title');
  4104. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4105. $this->assertHtml($expected, $result);
  4106. // Don't get value from schema
  4107. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4108. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4109. $this->assertHtml($expected, $result);
  4110. // Custom default value overrides default value from schema
  4111. $result = $this->Form->text('title', ['default' => 'override default']);
  4112. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4113. $this->assertHtml($expected, $result);
  4114. // Default value from schema is used only for new entities.
  4115. $entity->isNew(false);
  4116. $result = $this->Form->text('title');
  4117. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4118. $this->assertHtml($expected, $result);
  4119. }
  4120. /**
  4121. * testError method
  4122. *
  4123. * Test field error generation.
  4124. *
  4125. * @return void
  4126. */
  4127. public function testError()
  4128. {
  4129. $this->article['errors'] = [
  4130. 'Article' => ['field' => 'email'],
  4131. ];
  4132. $this->Form->create($this->article);
  4133. $result = $this->Form->error('Article.field');
  4134. $expected = [
  4135. ['div' => ['class' => 'error-message']],
  4136. 'email',
  4137. '/div',
  4138. ];
  4139. $this->assertHtml($expected, $result);
  4140. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4141. $expected = [
  4142. ['div' => ['class' => 'error-message']],
  4143. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4144. '/div',
  4145. ];
  4146. $this->assertHtml($expected, $result);
  4147. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4148. $expected = [
  4149. ['div' => ['class' => 'error-message']],
  4150. '<strong', 'Badness!', '/strong',
  4151. '/div',
  4152. ];
  4153. $this->assertHtml($expected, $result);
  4154. }
  4155. /**
  4156. * testErrorRuleName method
  4157. *
  4158. * Test error translation can use rule names for translating.
  4159. *
  4160. * @return void
  4161. */
  4162. public function testErrorRuleName()
  4163. {
  4164. $this->article['errors'] = [
  4165. 'Article' => [
  4166. 'field' => ['email' => 'Your email was not good'],
  4167. ],
  4168. ];
  4169. $this->Form->create($this->article);
  4170. $result = $this->Form->error('Article.field');
  4171. $expected = [
  4172. ['div' => ['class' => 'error-message']],
  4173. 'Your email was not good',
  4174. '/div',
  4175. ];
  4176. $this->assertHtml($expected, $result);
  4177. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4178. $expected = [
  4179. ['div' => ['class' => 'error-message']],
  4180. 'Email in use',
  4181. '/div',
  4182. ];
  4183. $this->assertHtml($expected, $result);
  4184. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4185. $expected = [
  4186. ['div' => ['class' => 'error-message']],
  4187. 'Email in use',
  4188. '/div',
  4189. ];
  4190. $this->assertHtml($expected, $result);
  4191. $result = $this->Form->error('Article.field', [
  4192. 'email' => 'Key is preferred',
  4193. 'Your email was not good' => 'Email in use',
  4194. ]);
  4195. $expected = [
  4196. ['div' => ['class' => 'error-message']],
  4197. 'Key is preferred',
  4198. '/div',
  4199. ];
  4200. $this->assertHtml($expected, $result);
  4201. }
  4202. /**
  4203. * testErrorMessages method
  4204. *
  4205. * Test error with nested lists.
  4206. *
  4207. * @return void
  4208. */
  4209. public function testErrorMessages()
  4210. {
  4211. $this->article['errors'] = [
  4212. 'Article' => ['field' => 'email'],
  4213. ];
  4214. $this->Form->create($this->article);
  4215. $result = $this->Form->error('Article.field', [
  4216. 'email' => 'No good!',
  4217. ]);
  4218. $expected = [
  4219. 'div' => ['class' => 'error-message'],
  4220. 'No good!',
  4221. '/div',
  4222. ];
  4223. $this->assertHtml($expected, $result);
  4224. }
  4225. /**
  4226. * testErrorMultipleMessages method
  4227. *
  4228. * Test error() with multiple messages.
  4229. *
  4230. * @return void
  4231. */
  4232. public function testErrorMultipleMessages()
  4233. {
  4234. $this->article['errors'] = [
  4235. 'field' => ['notBlank', 'email', 'Something else'],
  4236. ];
  4237. $this->Form->create($this->article);
  4238. $result = $this->Form->error('field', [
  4239. 'notBlank' => 'Cannot be empty',
  4240. 'email' => 'No good!',
  4241. ]);
  4242. $expected = [
  4243. 'div' => ['class' => 'error-message'],
  4244. 'ul' => [],
  4245. '<li', 'Cannot be empty', '/li',
  4246. '<li', 'No good!', '/li',
  4247. '<li', 'Something else', '/li',
  4248. '/ul',
  4249. '/div',
  4250. ];
  4251. $this->assertHtml($expected, $result);
  4252. }
  4253. /**
  4254. * testPassword method
  4255. *
  4256. * Test password element generation.
  4257. *
  4258. * @return void
  4259. */
  4260. public function testPassword()
  4261. {
  4262. $this->article['errors'] = [
  4263. 'Contact' => [
  4264. 'passwd' => 1,
  4265. ],
  4266. ];
  4267. $this->Form->create($this->article);
  4268. $result = $this->Form->password('Contact.field');
  4269. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4270. $this->assertHtml($expected, $result);
  4271. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4272. $this->Form->create($this->article);
  4273. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4274. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4275. $this->assertHtml($expected, $result);
  4276. }
  4277. /**
  4278. * testRadio method
  4279. *
  4280. * Test radio element set generation.
  4281. *
  4282. * @return void
  4283. */
  4284. public function testRadio()
  4285. {
  4286. $result = $this->Form->radio('Model.field', ['option A']);
  4287. $expected = [
  4288. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4289. 'label' => ['for' => 'model-field-0'],
  4290. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4291. 'option A',
  4292. '/label',
  4293. ];
  4294. $this->assertHtml($expected, $result);
  4295. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4296. $this->assertHtml($expected, $result);
  4297. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4298. $expected = [
  4299. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4300. ['label' => ['for' => 'model-field-0']],
  4301. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4302. 'option A',
  4303. '/label',
  4304. ['label' => ['for' => 'model-field-1']],
  4305. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4306. 'option B',
  4307. '/label',
  4308. ];
  4309. $this->assertHtml($expected, $result);
  4310. $result = $this->Form->radio(
  4311. 'Employee.gender',
  4312. ['male' => 'Male', 'female' => 'Female'],
  4313. ['form' => 'my-form']
  4314. );
  4315. $expected = [
  4316. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4317. ['label' => ['for' => 'employee-gender-male']],
  4318. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4319. 'Male',
  4320. '/label',
  4321. ['label' => ['for' => 'employee-gender-female']],
  4322. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4323. 'Female',
  4324. '/label',
  4325. ];
  4326. $this->assertHtml($expected, $result);
  4327. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4328. $expected = [
  4329. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4330. ['label' => ['for' => 'model-custom-0']],
  4331. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4332. 'option A',
  4333. '/label',
  4334. ['label' => ['for' => 'model-custom-1']],
  4335. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4336. 'option B',
  4337. '/label',
  4338. ];
  4339. $this->assertHtml($expected, $result);
  4340. $result = $this->Form->radio(
  4341. 'Employee.gender',
  4342. [
  4343. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4344. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4345. ]
  4346. );
  4347. $expected = [
  4348. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4349. ['label' => ['for' => 'employee-gender-male']],
  4350. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4351. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4352. 'Male',
  4353. '/label',
  4354. ['label' => ['for' => 'employee-gender-female']],
  4355. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4356. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4357. 'Female',
  4358. '/label',
  4359. ];
  4360. $this->assertHtml($expected, $result);
  4361. }
  4362. /**
  4363. * Test radio with complex options and empty disabled data.
  4364. *
  4365. * @return void
  4366. */
  4367. public function testRadioComplexDisabled()
  4368. {
  4369. $options = [
  4370. ['value' => 'r', 'text' => 'red'],
  4371. ['value' => 'b', 'text' => 'blue'],
  4372. ];
  4373. $attrs = ['disabled' => []];
  4374. $result = $this->Form->radio('Model.field', $options, $attrs);
  4375. $expected = [
  4376. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4377. ['label' => ['for' => 'model-field-r']],
  4378. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4379. 'red',
  4380. '/label',
  4381. ['label' => ['for' => 'model-field-b']],
  4382. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4383. 'blue',
  4384. '/label',
  4385. ];
  4386. $this->assertHtml($expected, $result);
  4387. $attrs = ['disabled' => ['r']];
  4388. $result = $this->Form->radio('Model.field', $options, $attrs);
  4389. $this->assertContains('disabled="disabled"', $result);
  4390. }
  4391. /**
  4392. * testRadioDefaultValue method
  4393. *
  4394. * Test default value setting on radio() method.
  4395. *
  4396. * @return void
  4397. */
  4398. public function testRadioDefaultValue()
  4399. {
  4400. $Articles = $this->getTableLocator()->get('Articles');
  4401. $title = $Articles->getSchema()->getColumn('title');
  4402. $Articles->getSchema()->addColumn(
  4403. 'title',
  4404. ['default' => '1'] + $title
  4405. );
  4406. $this->Form->create($Articles->newEntity());
  4407. $result = $this->Form->radio('title', ['option A', 'option B']);
  4408. $expected = [
  4409. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4410. ['label' => ['for' => 'title-0']],
  4411. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4412. 'option A',
  4413. '/label',
  4414. ['label' => ['for' => 'title-1']],
  4415. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4416. 'option B',
  4417. '/label',
  4418. ];
  4419. $this->assertHtml($expected, $result);
  4420. }
  4421. /**
  4422. * Test setting a hiddenField value on radio buttons.
  4423. *
  4424. * @return void
  4425. */
  4426. public function testRadioHiddenFieldValue()
  4427. {
  4428. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4429. $expected = [
  4430. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4431. 'label' => ['for' => 'title-0'],
  4432. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4433. 'option A',
  4434. '/label',
  4435. ];
  4436. $this->assertHtml($expected, $result);
  4437. }
  4438. /**
  4439. * testControlRadio method
  4440. *
  4441. * Test that input works with radio types.
  4442. *
  4443. * @return void
  4444. */
  4445. public function testControlRadio()
  4446. {
  4447. $result = $this->Form->control('test', [
  4448. 'type' => 'radio',
  4449. 'options' => ['A', 'B'],
  4450. ]);
  4451. $expected = [
  4452. ['div' => ['class' => 'input radio']],
  4453. '<label',
  4454. 'Test',
  4455. '/label',
  4456. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4457. ['label' => ['for' => 'test-0']],
  4458. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4459. 'A',
  4460. '/label',
  4461. ['label' => ['for' => 'test-1']],
  4462. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4463. 'B',
  4464. '/label',
  4465. '/div',
  4466. ];
  4467. $this->assertHtml($expected, $result);
  4468. $result = $this->Form->control('test', [
  4469. 'type' => 'radio',
  4470. 'options' => ['A', 'B'],
  4471. 'value' => '0',
  4472. ]);
  4473. $expected = [
  4474. ['div' => ['class' => 'input radio']],
  4475. '<label',
  4476. 'Test',
  4477. '/label',
  4478. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4479. ['label' => ['for' => 'test-0']],
  4480. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4481. 'A',
  4482. '/label',
  4483. ['label' => ['for' => 'test-1']],
  4484. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4485. 'B',
  4486. '/label',
  4487. '/div',
  4488. ];
  4489. $this->assertHtml($expected, $result);
  4490. $result = $this->Form->control('test', [
  4491. 'type' => 'radio',
  4492. 'options' => ['A', 'B'],
  4493. 'label' => false,
  4494. ]);
  4495. $expected = [
  4496. ['div' => ['class' => 'input radio']],
  4497. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4498. ['label' => ['for' => 'test-0']],
  4499. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4500. 'A',
  4501. '/label',
  4502. ['label' => ['for' => 'test-1']],
  4503. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4504. 'B',
  4505. '/label',
  4506. '/div',
  4507. ];
  4508. $this->assertHtml($expected, $result);
  4509. }
  4510. /**
  4511. * testRadioNoLabel method
  4512. *
  4513. * Test that radio() works with label = false.
  4514. *
  4515. * @return void
  4516. */
  4517. public function testRadioNoLabel()
  4518. {
  4519. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4520. $expected = [
  4521. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4522. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4523. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4524. ];
  4525. $this->assertHtml($expected, $result);
  4526. }
  4527. /**
  4528. * testRadioControlInsideLabel method
  4529. *
  4530. * Test generating radio input inside label ala twitter bootstrap.
  4531. *
  4532. * @return void
  4533. */
  4534. public function testRadioControlInsideLabel()
  4535. {
  4536. $this->Form->setTemplates([
  4537. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4538. 'radioWrapper' => '{{label}}',
  4539. ]);
  4540. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4541. //@codingStandardsIgnoreStart
  4542. $expected = [
  4543. ['input' => [
  4544. 'type' => 'hidden',
  4545. 'name' => 'Model[field]',
  4546. 'value' => ''
  4547. ]],
  4548. ['label' => ['for' => 'model-field-0']],
  4549. ['input' => [
  4550. 'type' => 'radio',
  4551. 'name' => 'Model[field]',
  4552. 'value' => '0',
  4553. 'id' => 'model-field-0'
  4554. ]],
  4555. 'option A',
  4556. '/label',
  4557. ['label' => ['for' => 'model-field-1']],
  4558. ['input' => [
  4559. 'type' => 'radio',
  4560. 'name' => 'Model[field]',
  4561. 'value' => '1',
  4562. 'id' => 'model-field-1'
  4563. ]],
  4564. 'option B',
  4565. '/label',
  4566. ];
  4567. //@codingStandardsIgnoreEnd
  4568. $this->assertHtml($expected, $result);
  4569. }
  4570. /**
  4571. * testRadioHiddenControlDisabling method
  4572. *
  4573. * Test disabling the hidden input for radio buttons.
  4574. *
  4575. * @return void
  4576. */
  4577. public function testRadioHiddenControlDisabling()
  4578. {
  4579. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4580. $expected = [
  4581. 'label' => ['for' => 'model-1-field-0'],
  4582. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4583. 'option A',
  4584. '/label',
  4585. ];
  4586. $this->assertHtml($expected, $result);
  4587. }
  4588. /**
  4589. * testRadioOutOfRange method
  4590. *
  4591. * Test radio element set generation.
  4592. *
  4593. * @return void
  4594. */
  4595. public function testRadioOutOfRange()
  4596. {
  4597. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4598. $expected = [
  4599. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4600. 'label' => ['for' => 'model-field-v'],
  4601. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4602. 'value',
  4603. '/label',
  4604. ];
  4605. $this->assertHtml($expected, $result);
  4606. }
  4607. /**
  4608. * testSelect method
  4609. *
  4610. * Test select element generation.
  4611. *
  4612. * @return void
  4613. */
  4614. public function testSelect()
  4615. {
  4616. $result = $this->Form->select('Model.field', []);
  4617. $expected = [
  4618. 'select' => ['name' => 'Model[field]'],
  4619. '/select',
  4620. ];
  4621. $this->assertHtml($expected, $result);
  4622. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4623. $this->Form->create();
  4624. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4625. $expected = [
  4626. 'select' => ['name' => 'Model[field]'],
  4627. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4628. 'good',
  4629. '/option',
  4630. ['option' => ['value' => 'other']],
  4631. 'bad',
  4632. '/option',
  4633. '/select',
  4634. ];
  4635. $this->assertHtml($expected, $result);
  4636. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4637. $this->assertHtml($expected, $result);
  4638. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4639. $this->Form->create();
  4640. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4641. $expected = [
  4642. 'select' => ['name' => 'Model[field]'],
  4643. ['option' => ['value' => 'value']],
  4644. 'good',
  4645. '/option',
  4646. ['option' => ['value' => 'other']],
  4647. 'bad',
  4648. '/option',
  4649. '/select',
  4650. ];
  4651. $this->assertHtml($expected, $result);
  4652. $options = [
  4653. ['value' => 'first', 'text' => 'First'],
  4654. ['value' => 'first', 'text' => 'Another First'],
  4655. ];
  4656. $result = $this->Form->select(
  4657. 'Model.field',
  4658. $options,
  4659. ['escape' => false, 'empty' => false]
  4660. );
  4661. $expected = [
  4662. 'select' => ['name' => 'Model[field]'],
  4663. ['option' => ['value' => 'first']],
  4664. 'First',
  4665. '/option',
  4666. ['option' => ['value' => 'first']],
  4667. 'Another First',
  4668. '/option',
  4669. '/select',
  4670. ];
  4671. $this->assertHtml($expected, $result);
  4672. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4673. $this->Form->create();
  4674. $result = $this->Form->select(
  4675. 'Model.contact_id',
  4676. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4677. ['escape' => false, 'empty' => 'pick something']
  4678. );
  4679. $expected = [
  4680. 'select' => ['name' => 'Model[contact_id]'],
  4681. ['option' => ['value' => '']], 'pick something', '/option',
  4682. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4683. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4684. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4685. '/select',
  4686. ];
  4687. $this->assertHtml($expected, $result);
  4688. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4689. $this->Form->create();
  4690. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4691. $expected = [
  4692. 'select' => ['name' => 'Model[field]'],
  4693. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4694. ['option' => ['value' => '1']], 'Yes', '/option',
  4695. '/select',
  4696. ];
  4697. $this->assertHtml($expected, $result);
  4698. }
  4699. /**
  4700. * testSelectEscapeHtml method
  4701. *
  4702. * Test that select() escapes HTML.
  4703. *
  4704. * @return void
  4705. */
  4706. public function testSelectEscapeHtml()
  4707. {
  4708. $result = $this->Form->select(
  4709. 'Model.field',
  4710. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4711. ['empty' => false]
  4712. );
  4713. $expected = [
  4714. 'select' => ['name' => 'Model[field]'],
  4715. ['option' => ['value' => 'first']],
  4716. 'first &quot;html&quot; &lt;chars&gt;',
  4717. '/option',
  4718. ['option' => ['value' => 'second']],
  4719. 'value',
  4720. '/option',
  4721. '/select',
  4722. ];
  4723. $this->assertHtml($expected, $result);
  4724. $result = $this->Form->select(
  4725. 'Model.field',
  4726. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4727. ['escape' => false, 'empty' => false]
  4728. );
  4729. $expected = [
  4730. 'select' => ['name' => 'Model[field]'],
  4731. ['option' => ['value' => 'first']],
  4732. 'first "html" <chars>',
  4733. '/option',
  4734. ['option' => ['value' => 'second']],
  4735. 'value',
  4736. '/option',
  4737. '/select',
  4738. ];
  4739. $this->assertHtml($expected, $result);
  4740. }
  4741. /**
  4742. * testSelectRequired method
  4743. *
  4744. * Test select() with required and disabled attributes.
  4745. *
  4746. * @return void
  4747. */
  4748. public function testSelectRequired()
  4749. {
  4750. $this->article['required'] = [
  4751. 'user_id' => true,
  4752. ];
  4753. $this->Form->create($this->article);
  4754. $result = $this->Form->select('user_id', ['option A']);
  4755. $expected = [
  4756. 'select' => [
  4757. 'name' => 'user_id',
  4758. 'required' => 'required',
  4759. ],
  4760. ['option' => ['value' => '0']], 'option A', '/option',
  4761. '/select',
  4762. ];
  4763. $this->assertHtml($expected, $result);
  4764. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4765. $expected = [
  4766. 'select' => [
  4767. 'name' => 'user_id',
  4768. 'disabled' => 'disabled',
  4769. ],
  4770. ['option' => ['value' => '0']], 'option A', '/option',
  4771. '/select',
  4772. ];
  4773. $this->assertHtml($expected, $result);
  4774. }
  4775. /**
  4776. * testNestedSelect method
  4777. *
  4778. * Test select element generation with optgroups.
  4779. *
  4780. * @return void
  4781. */
  4782. public function testNestedSelect()
  4783. {
  4784. $result = $this->Form->select(
  4785. 'Model.field',
  4786. [1 => 'One', 2 => 'Two', 'Three' => [
  4787. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4788. ]],
  4789. ['empty' => false]
  4790. );
  4791. $expected = [
  4792. 'select' => ['name' => 'Model[field]'],
  4793. ['option' => ['value' => 1]],
  4794. 'One',
  4795. '/option',
  4796. ['option' => ['value' => 2]],
  4797. 'Two',
  4798. '/option',
  4799. ['optgroup' => ['label' => 'Three']],
  4800. ['option' => ['value' => 3]],
  4801. 'Three',
  4802. '/option',
  4803. ['option' => ['value' => 4]],
  4804. 'Four',
  4805. '/option',
  4806. ['option' => ['value' => 5]],
  4807. 'Five',
  4808. '/option',
  4809. '/optgroup',
  4810. '/select',
  4811. ];
  4812. $this->assertHtml($expected, $result);
  4813. }
  4814. /**
  4815. * testSelectMultiple method
  4816. *
  4817. * Test generation of multiple select elements.
  4818. *
  4819. * @return void
  4820. */
  4821. public function testSelectMultiple()
  4822. {
  4823. $options = ['first', 'second', 'third'];
  4824. $result = $this->Form->select(
  4825. 'Model.multi_field',
  4826. $options,
  4827. ['form' => 'my-form', 'multiple' => true]
  4828. );
  4829. $expected = [
  4830. 'input' => [
  4831. 'type' => 'hidden',
  4832. 'name' => 'Model[multi_field]',
  4833. 'value' => '',
  4834. 'form' => 'my-form',
  4835. ],
  4836. 'select' => [
  4837. 'name' => 'Model[multi_field][]',
  4838. 'multiple' => 'multiple',
  4839. 'form' => 'my-form',
  4840. ],
  4841. ['option' => ['value' => '0']],
  4842. 'first',
  4843. '/option',
  4844. ['option' => ['value' => '1']],
  4845. 'second',
  4846. '/option',
  4847. ['option' => ['value' => '2']],
  4848. 'third',
  4849. '/option',
  4850. '/select',
  4851. ];
  4852. $this->assertHtml($expected, $result);
  4853. $result = $this->Form->select(
  4854. 'Model.multi_field',
  4855. $options,
  4856. ['multiple' => 'multiple', 'form' => 'my-form']
  4857. );
  4858. $this->assertHtml($expected, $result);
  4859. $result = $this->Form->select(
  4860. 'Model.multi_field',
  4861. $options,
  4862. ['form' => 'my-form', 'multiple' => false]
  4863. );
  4864. $this->assertNotContains('multiple', $result);
  4865. }
  4866. /**
  4867. * testCheckboxZeroValue method
  4868. *
  4869. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4870. *
  4871. * @return void
  4872. */
  4873. public function testCheckboxZeroValue()
  4874. {
  4875. $result = $this->Form->control('User.get_spam', [
  4876. 'type' => 'checkbox',
  4877. 'value' => '0',
  4878. 'hiddenField' => '1',
  4879. ]);
  4880. $expected = [
  4881. 'div' => ['class' => 'input checkbox'],
  4882. 'label' => ['for' => 'user-get-spam'],
  4883. ['input' => [
  4884. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4885. 'value' => '1',
  4886. ]],
  4887. ['input' => [
  4888. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4889. 'value' => '0', 'id' => 'user-get-spam',
  4890. ]],
  4891. 'Get Spam',
  4892. '/label',
  4893. '/div',
  4894. ];
  4895. $this->assertHtml($expected, $result);
  4896. }
  4897. /**
  4898. * testHabtmSelectBox method
  4899. *
  4900. * Test generation of habtm select boxes.
  4901. *
  4902. * @return void
  4903. */
  4904. public function testHabtmSelectBox()
  4905. {
  4906. $this->loadFixtures('Articles');
  4907. $options = [
  4908. 1 => 'blue',
  4909. 2 => 'red',
  4910. 3 => 'green',
  4911. ];
  4912. $tags = [
  4913. new Entity(['id' => 1, 'name' => 'blue']),
  4914. new Entity(['id' => 3, 'name' => 'green']),
  4915. ];
  4916. $article = new Article(['tags' => $tags]);
  4917. $this->Form->create($article);
  4918. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4919. $expected = [
  4920. 'div' => ['class' => 'input select'],
  4921. 'label' => ['for' => 'tags-ids'],
  4922. 'Tags',
  4923. '/label',
  4924. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4925. 'select' => [
  4926. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4927. 'multiple' => 'multiple',
  4928. ],
  4929. ['option' => ['value' => '1', 'selected' => 'selected']],
  4930. 'blue',
  4931. '/option',
  4932. ['option' => ['value' => '2']],
  4933. 'red',
  4934. '/option',
  4935. ['option' => ['value' => '3', 'selected' => 'selected']],
  4936. 'green',
  4937. '/option',
  4938. '/select',
  4939. '/div',
  4940. ];
  4941. $this->assertHtml($expected, $result);
  4942. // make sure only 50 is selected, and not 50f5c0cf
  4943. $options = [
  4944. '1' => 'blue',
  4945. '50f5c0cf' => 'red',
  4946. '50' => 'green',
  4947. ];
  4948. $tags = [
  4949. new Entity(['id' => 1, 'name' => 'blue']),
  4950. new Entity(['id' => 50, 'name' => 'green']),
  4951. ];
  4952. $article = new Article(['tags' => $tags]);
  4953. $this->Form->create($article);
  4954. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4955. $expected = [
  4956. 'div' => ['class' => 'input select'],
  4957. 'label' => ['for' => 'tags-ids'],
  4958. 'Tags',
  4959. '/label',
  4960. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4961. 'select' => [
  4962. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4963. 'multiple' => 'multiple',
  4964. ],
  4965. ['option' => ['value' => '1', 'selected' => 'selected']],
  4966. 'blue',
  4967. '/option',
  4968. ['option' => ['value' => '50f5c0cf']],
  4969. 'red',
  4970. '/option',
  4971. ['option' => ['value' => '50', 'selected' => 'selected']],
  4972. 'green',
  4973. '/option',
  4974. '/select',
  4975. '/div',
  4976. ];
  4977. $this->assertHtml($expected, $result);
  4978. $spacecraft = [
  4979. 1 => 'Orion',
  4980. 2 => 'Helios',
  4981. ];
  4982. $this->View->viewVars['spacecraft'] = $spacecraft;
  4983. $this->Form->create();
  4984. $result = $this->Form->control('spacecraft._ids');
  4985. $expected = [
  4986. 'div' => ['class' => 'input select'],
  4987. 'label' => ['for' => 'spacecraft-ids'],
  4988. 'Spacecraft',
  4989. '/label',
  4990. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4991. 'select' => [
  4992. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4993. 'multiple' => 'multiple',
  4994. ],
  4995. ['option' => ['value' => '1']],
  4996. 'Orion',
  4997. '/option',
  4998. ['option' => ['value' => '2']],
  4999. 'Helios',
  5000. '/option',
  5001. '/select',
  5002. '/div',
  5003. ];
  5004. $this->assertHtml($expected, $result);
  5005. }
  5006. /**
  5007. * testErrorsForBelongsToManySelect method
  5008. *
  5009. * Tests that errors for belongsToMany select fields are being
  5010. * picked up properly.
  5011. *
  5012. * @return void
  5013. */
  5014. public function testErrorsForBelongsToManySelect()
  5015. {
  5016. $spacecraft = [
  5017. 1 => 'Orion',
  5018. 2 => 'Helios',
  5019. ];
  5020. $this->View->viewVars['spacecraft'] = $spacecraft;
  5021. $article = new Article();
  5022. $article->setError('spacecraft', ['Invalid']);
  5023. $this->Form->create($article);
  5024. $result = $this->Form->control('spacecraft._ids');
  5025. $expected = [
  5026. ['div' => ['class' => 'input select error']],
  5027. 'label' => ['for' => 'spacecraft-ids'],
  5028. 'Spacecraft',
  5029. '/label',
  5030. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  5031. 'select' => [
  5032. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  5033. 'multiple' => 'multiple',
  5034. ],
  5035. ['option' => ['value' => '1']],
  5036. 'Orion',
  5037. '/option',
  5038. ['option' => ['value' => '2']],
  5039. 'Helios',
  5040. '/option',
  5041. '/select',
  5042. ['div' => ['class' => 'error-message']],
  5043. 'Invalid',
  5044. '/div',
  5045. '/div',
  5046. ];
  5047. $this->assertHtml($expected, $result);
  5048. }
  5049. /**
  5050. * testSelectMultipleCheckboxes method
  5051. *
  5052. * Test generation of multi select elements in checkbox format.
  5053. *
  5054. * @return void
  5055. */
  5056. public function testSelectMultipleCheckboxes()
  5057. {
  5058. $result = $this->Form->select(
  5059. 'Model.multi_field',
  5060. ['first', 'second', 'third'],
  5061. ['multiple' => 'checkbox']
  5062. );
  5063. $expected = [
  5064. 'input' => [
  5065. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5066. ],
  5067. ['div' => ['class' => 'checkbox']],
  5068. ['label' => ['for' => 'model-multi-field-0']],
  5069. ['input' => [
  5070. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5071. 'value' => '0', 'id' => 'model-multi-field-0',
  5072. ]],
  5073. 'first',
  5074. '/label',
  5075. '/div',
  5076. ['div' => ['class' => 'checkbox']],
  5077. ['label' => ['for' => 'model-multi-field-1']],
  5078. ['input' => [
  5079. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5080. 'value' => '1', 'id' => 'model-multi-field-1',
  5081. ]],
  5082. 'second',
  5083. '/label',
  5084. '/div',
  5085. ['div' => ['class' => 'checkbox']],
  5086. ['label' => ['for' => 'model-multi-field-2']],
  5087. ['input' => [
  5088. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5089. 'value' => '2', 'id' => 'model-multi-field-2',
  5090. ]],
  5091. 'third',
  5092. '/label',
  5093. '/div',
  5094. ];
  5095. $this->assertHtml($expected, $result);
  5096. $result = $this->Form->select(
  5097. 'Model.multi_field',
  5098. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5099. ['multiple' => 'checkbox']
  5100. );
  5101. $expected = [
  5102. 'input' => [
  5103. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5104. ],
  5105. ['div' => ['class' => 'checkbox']],
  5106. ['label' => ['for' => 'model-multi-field-a+']],
  5107. ['input' => [
  5108. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5109. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  5110. ]],
  5111. 'first',
  5112. '/label',
  5113. '/div',
  5114. ['div' => ['class' => 'checkbox']],
  5115. ['label' => ['for' => 'model-multi-field-a++']],
  5116. ['input' => [
  5117. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5118. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  5119. ]],
  5120. 'second',
  5121. '/label',
  5122. '/div',
  5123. ['div' => ['class' => 'checkbox']],
  5124. ['label' => ['for' => 'model-multi-field-a+++']],
  5125. ['input' => [
  5126. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5127. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  5128. ]],
  5129. 'third',
  5130. '/label',
  5131. '/div',
  5132. ];
  5133. $this->assertHtml($expected, $result);
  5134. $result = $this->Form->select(
  5135. 'Model.multi_field',
  5136. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5137. ['multiple' => 'checkbox']
  5138. );
  5139. $expected = [
  5140. 'input' => [
  5141. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5142. ],
  5143. ['div' => ['class' => 'checkbox']],
  5144. ['label' => ['for' => 'model-multi-field-a-b']],
  5145. ['input' => [
  5146. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5147. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  5148. ]],
  5149. 'first',
  5150. '/label',
  5151. '/div',
  5152. ['div' => ['class' => 'checkbox']],
  5153. ['label' => ['for' => 'model-multi-field-a-b1']],
  5154. ['input' => [
  5155. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5156. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  5157. ]],
  5158. 'second',
  5159. '/label',
  5160. '/div',
  5161. ['div' => ['class' => 'checkbox']],
  5162. ['label' => ['for' => 'model-multi-field-a-b2']],
  5163. ['input' => [
  5164. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5165. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  5166. ]],
  5167. 'third',
  5168. '/label',
  5169. '/div',
  5170. ];
  5171. $this->assertHtml($expected, $result);
  5172. }
  5173. /**
  5174. * testSelectMultipleCheckboxRequestData method
  5175. *
  5176. * Ensure that multiCheckbox reads from the request data.
  5177. *
  5178. * @return void
  5179. */
  5180. public function testSelectMultipleCheckboxRequestData()
  5181. {
  5182. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5183. $result = $this->Form->select(
  5184. 'Model.tags',
  5185. ['1' => 'first', 'Array' => 'Array'],
  5186. ['multiple' => 'checkbox']
  5187. );
  5188. $expected = [
  5189. 'input' => [
  5190. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '',
  5191. ],
  5192. ['div' => ['class' => 'checkbox']],
  5193. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5194. ['input' => [
  5195. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5196. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5197. ]],
  5198. 'first',
  5199. '/label',
  5200. '/div',
  5201. ['div' => ['class' => 'checkbox']],
  5202. ['label' => ['for' => 'model-tags-array']],
  5203. ['input' => [
  5204. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5205. 'value' => 'Array', 'id' => 'model-tags-array',
  5206. ]],
  5207. 'Array',
  5208. '/label',
  5209. '/div',
  5210. ];
  5211. $this->assertHtml($expected, $result);
  5212. }
  5213. /**
  5214. * testSelectMultipleCheckboxSecurity method
  5215. *
  5216. * Checks the security hash array generated for multiple-input checkbox elements.
  5217. *
  5218. * @return void
  5219. */
  5220. public function testSelectMultipleCheckboxSecurity()
  5221. {
  5222. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  5223. $this->assertEquals([], $this->Form->fields);
  5224. $this->Form->select(
  5225. 'Model.multi_field',
  5226. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5227. ['multiple' => 'checkbox']
  5228. );
  5229. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5230. $result = $this->Form->secure($this->Form->fields);
  5231. $key = '8af36fb34e6f2ef8ba0eb473bb4365ec232f3fe5%3A';
  5232. $this->assertContains('"' . $key . '"', $result);
  5233. }
  5234. /**
  5235. * testSelectMultipleSecureWithNoOptions method
  5236. *
  5237. * Multiple select elements should always be secured as they always participate
  5238. * in the POST data.
  5239. *
  5240. * @return void
  5241. */
  5242. public function testSelectMultipleSecureWithNoOptions()
  5243. {
  5244. $this->assertEquals([], $this->Form->fields);
  5245. $this->Form->select(
  5246. 'Model.select',
  5247. [],
  5248. ['multiple' => true]
  5249. );
  5250. $this->assertEquals(['Model.select'], $this->Form->fields);
  5251. }
  5252. /**
  5253. * testSelectNoSecureWithNoOptions method
  5254. *
  5255. * When a select box has no options it should not be added to the fields list
  5256. * as it always fail post validation.
  5257. *
  5258. * @return void
  5259. */
  5260. public function testSelectNoSecureWithNoOptions()
  5261. {
  5262. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testkey'));
  5263. $this->assertEquals([], $this->Form->fields);
  5264. $this->Form->select(
  5265. 'Model.select',
  5266. []
  5267. );
  5268. $this->assertEquals([], $this->Form->fields);
  5269. $this->Form->select(
  5270. 'Model.user_id',
  5271. [],
  5272. ['empty' => true]
  5273. );
  5274. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5275. }
  5276. /**
  5277. * testControlMultipleCheckboxes method
  5278. *
  5279. * Test control() resulting in multi select elements being generated.
  5280. *
  5281. * @return void
  5282. */
  5283. public function testControlMultipleCheckboxes()
  5284. {
  5285. $result = $this->Form->control('Model.multi_field', [
  5286. 'options' => ['first', 'second', 'third'],
  5287. 'multiple' => 'checkbox',
  5288. ]);
  5289. $expected = [
  5290. ['div' => ['class' => 'input select']],
  5291. ['label' => ['for' => 'model-multi-field']],
  5292. 'Multi Field',
  5293. '/label',
  5294. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5295. ['div' => ['class' => 'checkbox']],
  5296. ['label' => ['for' => 'model-multi-field-0']],
  5297. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5298. 'first',
  5299. '/label',
  5300. '/div',
  5301. ['div' => ['class' => 'checkbox']],
  5302. ['label' => ['for' => 'model-multi-field-1']],
  5303. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5304. 'second',
  5305. '/label',
  5306. '/div',
  5307. ['div' => ['class' => 'checkbox']],
  5308. ['label' => ['for' => 'model-multi-field-2']],
  5309. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5310. 'third',
  5311. '/label',
  5312. '/div',
  5313. '/div',
  5314. ];
  5315. $this->assertHtml($expected, $result);
  5316. $result = $this->Form->control('Model.multi_field', [
  5317. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5318. 'multiple' => 'checkbox',
  5319. ]);
  5320. $expected = [
  5321. ['div' => ['class' => 'input select']],
  5322. ['label' => ['for' => 'model-multi-field']],
  5323. 'Multi Field',
  5324. '/label',
  5325. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5326. ['div' => ['class' => 'checkbox']],
  5327. ['label' => ['for' => 'model-multi-field-a']],
  5328. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5329. 'first',
  5330. '/label',
  5331. '/div',
  5332. ['div' => ['class' => 'checkbox']],
  5333. ['label' => ['for' => 'model-multi-field-b']],
  5334. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5335. 'second',
  5336. '/label',
  5337. '/div',
  5338. ['div' => ['class' => 'checkbox']],
  5339. ['label' => ['for' => 'model-multi-field-c']],
  5340. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5341. 'third',
  5342. '/label',
  5343. '/div',
  5344. '/div',
  5345. ];
  5346. $this->assertHtml($expected, $result);
  5347. }
  5348. /**
  5349. * testSelectHiddenFieldOmission method
  5350. *
  5351. * Test that select() with 'hiddenField' => false omits the hidden field.
  5352. *
  5353. * @return void
  5354. */
  5355. public function testSelectHiddenFieldOmission()
  5356. {
  5357. $result = $this->Form->select(
  5358. 'Model.multi_field',
  5359. ['first', 'second'],
  5360. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5361. );
  5362. $this->assertNotContains('type="hidden"', $result);
  5363. }
  5364. /**
  5365. * testSelectCheckboxMultipleOverrideName method
  5366. *
  5367. * Test that select() with multiple = checkbox works with overriding name attribute.
  5368. *
  5369. * @return void
  5370. */
  5371. public function testSelectCheckboxMultipleOverrideName()
  5372. {
  5373. $result = $this->Form->select('category', ['1', '2'], [
  5374. 'multiple' => 'checkbox',
  5375. 'name' => 'fish',
  5376. ]);
  5377. $expected = [
  5378. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5379. ['div' => ['class' => 'checkbox']],
  5380. ['label' => ['for' => 'fish-0']],
  5381. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5382. '1',
  5383. '/label',
  5384. '/div',
  5385. ['div' => ['class' => 'checkbox']],
  5386. ['label' => ['for' => 'fish-1']],
  5387. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5388. '2',
  5389. '/label',
  5390. '/div',
  5391. ];
  5392. $this->assertHtml($expected, $result);
  5393. $result = $this->Form->multiCheckbox(
  5394. 'category',
  5395. new Collection(['1', '2']),
  5396. ['name' => 'fish']
  5397. );
  5398. $this->assertHtml($expected, $result);
  5399. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5400. 'name' => 'fish',
  5401. ]);
  5402. $this->assertHtml($expected, $result);
  5403. }
  5404. /**
  5405. * testControlMultiCheckbox method
  5406. *
  5407. * Test that control() works with multicheckbox.
  5408. *
  5409. * @return void
  5410. */
  5411. public function testControlMultiCheckbox()
  5412. {
  5413. $result = $this->Form->control('category', [
  5414. 'type' => 'multicheckbox',
  5415. 'options' => ['1', '2'],
  5416. ]);
  5417. $expected = [
  5418. ['div' => ['class' => 'input multicheckbox']],
  5419. '<label',
  5420. 'Category',
  5421. '/label',
  5422. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5423. ['div' => ['class' => 'checkbox']],
  5424. ['label' => ['for' => 'category-0']],
  5425. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5426. '1',
  5427. '/label',
  5428. '/div',
  5429. ['div' => ['class' => 'checkbox']],
  5430. ['label' => ['for' => 'category-1']],
  5431. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5432. '2',
  5433. '/label',
  5434. '/div',
  5435. '/div',
  5436. ];
  5437. $this->assertHtml($expected, $result);
  5438. }
  5439. /**
  5440. * testCheckbox method
  5441. *
  5442. * Test generation of checkboxes.
  5443. *
  5444. * @return void
  5445. */
  5446. public function testCheckbox()
  5447. {
  5448. $result = $this->Form->checkbox('Model.field');
  5449. $expected = [
  5450. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5451. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5452. ];
  5453. $this->assertHtml($expected, $result);
  5454. $result = $this->Form->checkbox('Model.field', [
  5455. 'id' => 'theID',
  5456. 'value' => 'myvalue',
  5457. 'form' => 'my-form',
  5458. ]);
  5459. $expected = [
  5460. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5461. ['input' => [
  5462. 'type' => 'checkbox', 'name' => 'Model[field]',
  5463. 'value' => 'myvalue', 'id' => 'theID',
  5464. 'form' => 'my-form',
  5465. ]],
  5466. ];
  5467. $this->assertHtml($expected, $result);
  5468. }
  5469. /**
  5470. * testCheckboxDefaultValue method
  5471. *
  5472. * Test default value setting on checkbox() method.
  5473. *
  5474. * @return void
  5475. */
  5476. public function testCheckboxDefaultValue()
  5477. {
  5478. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5479. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5480. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5481. $this->assertHtml($expected, $result);
  5482. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5483. $this->Form->create();
  5484. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5485. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5486. $this->assertHtml($expected, $result);
  5487. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5488. $this->Form->create();
  5489. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5490. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5491. $this->assertHtml($expected, $result);
  5492. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5493. $this->Form->create();
  5494. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5495. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5496. $this->assertHtml($expected, $result);
  5497. $Articles = $this->getTableLocator()->get('Articles');
  5498. $Articles->getSchema()->addColumn(
  5499. 'published',
  5500. ['type' => 'boolean', 'null' => false, 'default' => true]
  5501. );
  5502. $this->Form->create($Articles->newEntity());
  5503. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5504. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5505. $this->assertHtml($expected, $result);
  5506. }
  5507. /**
  5508. * testCheckboxCheckedAndError method
  5509. *
  5510. * Test checkbox being checked or having errors.
  5511. *
  5512. * @return void
  5513. */
  5514. public function testCheckboxCheckedAndError()
  5515. {
  5516. $this->article['errors'] = [
  5517. 'published' => true,
  5518. ];
  5519. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5520. $this->Form->create($this->article);
  5521. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5522. $expected = [
  5523. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5524. ['input' => [
  5525. 'type' => 'checkbox',
  5526. 'name' => 'published',
  5527. 'value' => 'myvalue',
  5528. 'id' => 'theID',
  5529. 'checked' => 'checked',
  5530. 'class' => 'form-error',
  5531. ]],
  5532. ];
  5533. $this->assertHtml($expected, $result);
  5534. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5535. $this->Form->create($this->article);
  5536. $result = $this->Form->checkbox('published');
  5537. $expected = [
  5538. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5539. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5540. ];
  5541. $this->assertHtml($expected, $result);
  5542. }
  5543. /**
  5544. * testCheckboxCustomNameAttribute method
  5545. *
  5546. * Test checkbox() with a custom name attribute.
  5547. *
  5548. * @return void
  5549. */
  5550. public function testCheckboxCustomNameAttribute()
  5551. {
  5552. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5553. $expected = [
  5554. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5555. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5556. ];
  5557. $this->assertHtml($expected, $result);
  5558. }
  5559. /**
  5560. * testCheckboxHiddenField method
  5561. *
  5562. * Test that the hidden input for checkboxes can be omitted or set to a
  5563. * specific value.
  5564. *
  5565. * @return void
  5566. */
  5567. public function testCheckboxHiddenField()
  5568. {
  5569. $result = $this->Form->checkbox('UserForm.something', [
  5570. 'hiddenField' => false,
  5571. ]);
  5572. $expected = [
  5573. 'input' => [
  5574. 'type' => 'checkbox',
  5575. 'name' => 'UserForm[something]',
  5576. 'value' => '1',
  5577. ],
  5578. ];
  5579. $this->assertHtml($expected, $result);
  5580. $result = $this->Form->checkbox('UserForm.something', [
  5581. 'value' => 'Y',
  5582. 'hiddenField' => 'N',
  5583. ]);
  5584. $expected = [
  5585. ['input' => [
  5586. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5587. 'value' => 'N',
  5588. ]],
  5589. ['input' => [
  5590. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5591. 'value' => 'Y',
  5592. ]],
  5593. ];
  5594. $this->assertHtml($expected, $result);
  5595. }
  5596. /**
  5597. * testTime method
  5598. *
  5599. * Test the time type.
  5600. *
  5601. * @return void
  5602. */
  5603. public function testTime()
  5604. {
  5605. $result = $this->Form->time('start_time', [
  5606. 'timeFormat' => 12,
  5607. 'interval' => 5,
  5608. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm'],
  5609. ]);
  5610. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5611. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5612. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5613. $this->assertNotContains('year', $result);
  5614. $this->assertNotContains('month', $result);
  5615. $this->assertNotContains('day', $result);
  5616. $result = $this->Form->time('start_time', [
  5617. 'timeFormat' => 12,
  5618. 'interval' => 5,
  5619. 'value' => '2014-03-08 16:30:00',
  5620. ]);
  5621. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5622. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5623. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5624. $this->assertNotContains('year', $result);
  5625. $this->assertNotContains('month', $result);
  5626. $this->assertNotContains('day', $result);
  5627. }
  5628. /**
  5629. * testTimeFormat24NoMeridian method
  5630. *
  5631. * Ensure that timeFormat=24 has no merdian.
  5632. *
  5633. * @return void
  5634. */
  5635. public function testTimeFormat24NoMeridian()
  5636. {
  5637. $result = $this->Form->time('start_time', [
  5638. 'timeFormat' => 24,
  5639. 'interval' => 5,
  5640. 'value' => '2014-03-08 16:30:00',
  5641. ]);
  5642. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5643. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5644. $this->assertNotContains('meridian', $result);
  5645. $this->assertNotContains('pm', $result);
  5646. $this->assertNotContains('year', $result);
  5647. $this->assertNotContains('month', $result);
  5648. $this->assertNotContains('day', $result);
  5649. }
  5650. /**
  5651. * testDate method
  5652. *
  5653. * Test the date type.
  5654. *
  5655. * @return void
  5656. */
  5657. public function testDate()
  5658. {
  5659. $result = $this->Form->date('start_day', [
  5660. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08'],
  5661. ]);
  5662. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5663. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5664. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5665. $this->assertNotContains('hour', $result);
  5666. $this->assertNotContains('minute', $result);
  5667. $this->assertNotContains('second', $result);
  5668. $this->assertNotContains('meridian', $result);
  5669. }
  5670. /**
  5671. * testDateTime method
  5672. *
  5673. * Test generation of date/time select elements.
  5674. *
  5675. * @return void
  5676. */
  5677. public function testDateTime()
  5678. {
  5679. extract($this->dateRegex);
  5680. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5681. $now = strtotime('now');
  5682. $expected = [
  5683. ['select' => ['name' => 'Contact[date][year]']],
  5684. ['option' => ['value' => '']],
  5685. '/option',
  5686. $yearsRegex,
  5687. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5688. date('Y', $now),
  5689. '/option',
  5690. '*/select',
  5691. ['select' => ['name' => 'Contact[date][month]']],
  5692. ['option' => ['value' => '']],
  5693. '/option',
  5694. $monthsRegex,
  5695. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5696. date('F', $now),
  5697. '/option',
  5698. '*/select',
  5699. ['select' => ['name' => 'Contact[date][day]']],
  5700. ['option' => ['value' => '']],
  5701. '/option',
  5702. $daysRegex,
  5703. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5704. date('j', $now),
  5705. '/option',
  5706. '*/select',
  5707. ['select' => ['name' => 'Contact[date][hour]']],
  5708. ['option' => ['value' => '']],
  5709. '/option',
  5710. $hoursRegex,
  5711. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5712. date('G', $now),
  5713. '/option',
  5714. '*/select',
  5715. ['select' => ['name' => 'Contact[date][minute]']],
  5716. ['option' => ['value' => '']],
  5717. '/option',
  5718. $minutesRegex,
  5719. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5720. date('i', $now),
  5721. '/option',
  5722. '*/select',
  5723. ];
  5724. $this->assertHtml($expected, $result);
  5725. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5726. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5727. $now = strtotime('now');
  5728. $expected = [
  5729. ['select' => ['name' => 'Contact[date][year]']],
  5730. $yearsRegex,
  5731. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5732. date('Y', $now),
  5733. '/option',
  5734. '*/select',
  5735. ['select' => ['name' => 'Contact[date][month]']],
  5736. $monthsRegex,
  5737. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5738. date('F', $now),
  5739. '/option',
  5740. '*/select',
  5741. ['select' => ['name' => 'Contact[date][day]']],
  5742. $daysRegex,
  5743. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5744. date('j', $now),
  5745. '/option',
  5746. '*/select',
  5747. ['select' => ['name' => 'Contact[date][hour]']],
  5748. $hoursRegex,
  5749. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5750. date('G', $now),
  5751. '/option',
  5752. '*/select',
  5753. ['select' => ['name' => 'Contact[date][minute]']],
  5754. $minutesRegex,
  5755. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5756. date('i', $now),
  5757. '/option',
  5758. '*/select',
  5759. ];
  5760. $this->assertHtml($expected, $result);
  5761. }
  5762. /**
  5763. * testDateTimeSecured method
  5764. *
  5765. * Test that datetime fields are added to protected fields list.
  5766. *
  5767. * @return void
  5768. */
  5769. public function testDateTimeSecured()
  5770. {
  5771. $this->View->setRequest(
  5772. $this->View->getRequest()->withParam('_Token', ['unlockedFields' => []])
  5773. );
  5774. $this->Form->dateTime('Contact.date');
  5775. $expected = [
  5776. 'Contact.date.year',
  5777. 'Contact.date.month',
  5778. 'Contact.date.day',
  5779. 'Contact.date.hour',
  5780. 'Contact.date.minute',
  5781. ];
  5782. $this->assertEquals($expected, $this->Form->fields);
  5783. $this->Form->fields = [];
  5784. $this->Form->date('Contact.published');
  5785. $expected = [
  5786. 'Contact.published.year',
  5787. 'Contact.published.month',
  5788. 'Contact.published.day',
  5789. ];
  5790. $this->assertEquals($expected, $this->Form->fields);
  5791. }
  5792. /**
  5793. * testDateTimeSecuredDisabled method
  5794. *
  5795. * Test that datetime fields are added to protected fields list.
  5796. *
  5797. * @return void
  5798. */
  5799. public function testDateTimeSecuredDisabled()
  5800. {
  5801. $this->View->setRequest(
  5802. $this->View->getRequest()->withParam('_Token', ['unlockedFields' => []])
  5803. );
  5804. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5805. $expected = [];
  5806. $this->assertEquals($expected, $this->Form->fields);
  5807. $this->Form->fields = [];
  5808. $this->Form->date('Contact.published', ['secure' => false]);
  5809. $expected = [];
  5810. $this->assertEquals($expected, $this->Form->fields);
  5811. }
  5812. /**
  5813. * Test empty defaulting to true for datetime.
  5814. *
  5815. * @return void
  5816. */
  5817. public function testDatetimeEmpty()
  5818. {
  5819. extract($this->dateRegex);
  5820. $result = $this->Form->dateTime('Contact.date', [
  5821. 'timeFormat' => 12,
  5822. 'empty' => true,
  5823. 'default' => true,
  5824. ]);
  5825. $expected = [
  5826. ['select' => ['name' => 'Contact[date][year]']],
  5827. $yearsRegex,
  5828. ['option' => ['value' => '']],
  5829. '/option',
  5830. '*/select',
  5831. ['select' => ['name' => 'Contact[date][month]']],
  5832. $monthsRegex,
  5833. ['option' => ['value' => '']],
  5834. '/option',
  5835. '*/select',
  5836. ['select' => ['name' => 'Contact[date][day]']],
  5837. $daysRegex,
  5838. ['option' => ['value' => '']],
  5839. '/option',
  5840. '*/select',
  5841. ['select' => ['name' => 'Contact[date][hour]']],
  5842. $hoursRegex,
  5843. ['option' => ['value' => '']],
  5844. '/option',
  5845. '*/select',
  5846. ['select' => ['name' => 'Contact[date][minute]']],
  5847. $minutesRegex,
  5848. ['option' => ['value' => '']],
  5849. '/option',
  5850. '*/select',
  5851. ['select' => ['name' => 'Contact[date][meridian]']],
  5852. $meridianRegex,
  5853. ['option' => ['value' => '']],
  5854. '/option',
  5855. '*/select',
  5856. ];
  5857. $this->assertHtml($expected, $result);
  5858. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5859. }
  5860. /**
  5861. * Presence check for array form empty options
  5862. *
  5863. * @return void
  5864. */
  5865. public function testDateTimeEmptyAsArrayPresence()
  5866. {
  5867. $result = $this->Form->dateTime('Contact.date', [
  5868. 'empty' => [
  5869. 'day' => 'DAY',
  5870. 'month' => 'MONTH',
  5871. 'year' => 'YEAR',
  5872. 'hour' => 'HOUR',
  5873. 'minute' => 'MINUTE',
  5874. 'meridian' => false,
  5875. ],
  5876. 'default' => true,
  5877. ]);
  5878. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5879. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5880. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5881. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5882. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5883. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5884. }
  5885. /**
  5886. * Test datetime with array empty value, ensuring
  5887. * empty options aren't duplicated.
  5888. *
  5889. * @return void
  5890. */
  5891. public function testDatetimeEmptyArrayForm()
  5892. {
  5893. extract($this->dateRegex);
  5894. $result = $this->Form->dateTime('Contact.date', [
  5895. 'minYear' => '2017',
  5896. 'maxYear' => '2019',
  5897. 'empty' => [
  5898. 'year' => 'pick year',
  5899. 'month' => 'pick month',
  5900. ],
  5901. 'hour' => false,
  5902. 'minute' => false,
  5903. 'second' => false,
  5904. 'meridian' => false,
  5905. ]);
  5906. $expected = [
  5907. ['select' => ['name' => 'Contact[date][year]']],
  5908. ['option' => ['value' => '', 'selected' => 'selected']], 'pick year', '/option',
  5909. '*/select',
  5910. ['select' => ['name' => 'Contact[date][month]']],
  5911. ['option' => ['value' => '', 'selected' => 'selected']], 'pick month', '/option',
  5912. $monthsRegex,
  5913. '*/select',
  5914. ['select' => ['name' => 'Contact[date][day]']],
  5915. $daysRegex,
  5916. '*/select',
  5917. ];
  5918. $this->assertHtml($expected, $result);
  5919. }
  5920. /**
  5921. * testDatetimeMinuteInterval method
  5922. *
  5923. * Test datetime with interval option.
  5924. *
  5925. * @return void
  5926. */
  5927. public function testDatetimeMinuteInterval()
  5928. {
  5929. extract($this->dateRegex);
  5930. $result = $this->Form->dateTime('Contact.date', [
  5931. 'interval' => 5,
  5932. 'value' => '',
  5933. ]);
  5934. $expected = [
  5935. ['select' => ['name' => 'Contact[date][year]']],
  5936. $yearsRegex,
  5937. ['option' => ['selected' => 'selected', 'value' => '']],
  5938. '/option',
  5939. '*/select',
  5940. ['select' => ['name' => 'Contact[date][month]']],
  5941. $monthsRegex,
  5942. ['option' => ['selected' => 'selected', 'value' => '']],
  5943. '/option',
  5944. '*/select',
  5945. ['select' => ['name' => 'Contact[date][day]']],
  5946. $daysRegex,
  5947. ['option' => ['selected' => 'selected', 'value' => '']],
  5948. '/option',
  5949. '*/select',
  5950. ['select' => ['name' => 'Contact[date][hour]']],
  5951. $hoursRegex,
  5952. ['option' => ['selected' => 'selected', 'value' => '']],
  5953. '/option',
  5954. '*/select',
  5955. ['select' => ['name' => 'Contact[date][minute]']],
  5956. $minutesRegex,
  5957. ['option' => ['selected' => 'selected', 'value' => '']],
  5958. '/option',
  5959. ['option' => ['value' => '00']],
  5960. '00',
  5961. '/option',
  5962. ['option' => ['value' => '05']],
  5963. '05',
  5964. '/option',
  5965. ['option' => ['value' => '10']],
  5966. '10',
  5967. '/option',
  5968. '*/select',
  5969. ];
  5970. $this->assertHtml($expected, $result);
  5971. }
  5972. /**
  5973. * testDateTimeRounding method
  5974. *
  5975. * Test dateTime with rounding.
  5976. *
  5977. * @return void
  5978. */
  5979. public function testDateTimeRounding()
  5980. {
  5981. $this->View->setRequest($this->View->getRequest()->withData('Contact', [
  5982. 'date' => [
  5983. 'day' => '13',
  5984. 'month' => '12',
  5985. 'year' => '2010',
  5986. 'hour' => '04',
  5987. 'minute' => '19',
  5988. 'meridian' => 'AM',
  5989. ],
  5990. ]));
  5991. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5992. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5993. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5994. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5995. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5996. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5997. }
  5998. /**
  5999. * testDatetimeWithDefault method
  6000. *
  6001. * Test that datetime() and default values work.
  6002. *
  6003. * @return void
  6004. */
  6005. public function testDatetimeWithDefault()
  6006. {
  6007. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  6008. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  6009. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  6010. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  6011. $result = $this->Form->dateTime('Contact.updated', [
  6012. 'default' => '2009-06-01 11:15:30',
  6013. ]);
  6014. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  6015. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  6016. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  6017. }
  6018. /**
  6019. * testDateTimeAllZeros method
  6020. *
  6021. * Test datetime() with all zeros.
  6022. *
  6023. * @return void
  6024. */
  6025. public function testDateTimeAllZeros()
  6026. {
  6027. $result = $this->Form->dateTime('Contact.date', [
  6028. 'timeFormat' => false,
  6029. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  6030. 'value' => '0000-00-00',
  6031. ]);
  6032. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  6033. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  6034. }
  6035. /**
  6036. * testFormDateTimeMulti method
  6037. *
  6038. * Test multiple datetime element generation.
  6039. *
  6040. * @return void
  6041. */
  6042. public function testFormDateTimeMulti()
  6043. {
  6044. extract($this->dateRegex);
  6045. $result = $this->Form->dateTime('Contact.1.updated');
  6046. $this->assertContains('Contact[1][updated][month]', $result);
  6047. $this->assertContains('Contact[1][updated][day]', $result);
  6048. $this->assertContains('Contact[1][updated][year]', $result);
  6049. $this->assertContains('Contact[1][updated][hour]', $result);
  6050. $this->assertContains('Contact[1][updated][minute]', $result);
  6051. }
  6052. /**
  6053. * testDateTimeLabelIdMatchesFirstControl method
  6054. *
  6055. * When changing the date format, the label should always focus the first select box when
  6056. * clicked.
  6057. *
  6058. * @return void
  6059. */
  6060. public function testDateTimeLabelIdMatchesFirstControl()
  6061. {
  6062. $result = $this->Form->control('Model.date', ['type' => 'date']);
  6063. $this->assertContains('<label>Date</label>', $result);
  6064. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  6065. $this->assertContains('<label>Date</label>', $result);
  6066. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  6067. $this->assertContains('<label>Date</label>', $result);
  6068. }
  6069. /**
  6070. * testDateTimeSecondOptions method
  6071. *
  6072. * Test datetime second=true.
  6073. *
  6074. * @return void
  6075. */
  6076. public function testDateTimeSecondOptions()
  6077. {
  6078. $result = $this->Form->dateTime('updated', ['second' => true]);
  6079. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6080. $result = $this->Form->dateTime('updated', ['second' => []]);
  6081. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6082. $result = $this->Form->dateTime('updated', ['second' => null]);
  6083. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6084. $result = $this->Form->dateTime('updated', ['second' => false]);
  6085. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6086. }
  6087. /**
  6088. * testMonth method
  6089. *
  6090. * Test generation of a month input.
  6091. *
  6092. * @return void
  6093. */
  6094. public function testMonth()
  6095. {
  6096. $result = $this->Form->month('Model.field', ['value' => '']);
  6097. $expected = [
  6098. ['select' => ['name' => 'Model[field][month]']],
  6099. ['option' => ['value' => '', 'selected' => 'selected']],
  6100. '/option',
  6101. ['option' => ['value' => '01']],
  6102. date('F', strtotime('2008-01-01 00:00:00')),
  6103. '/option',
  6104. ['option' => ['value' => '02']],
  6105. date('F', strtotime('2008-02-01 00:00:00')),
  6106. '/option',
  6107. '*/select',
  6108. ];
  6109. $this->assertHtml($expected, $result);
  6110. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  6111. $expected = [
  6112. ['select' => ['name' => 'Model[field][month]']],
  6113. ['option' => ['selected' => 'selected', 'value' => '']],
  6114. '/option',
  6115. ['option' => ['value' => '01']],
  6116. date('F', strtotime('2008-01-01 00:00:00')),
  6117. '/option',
  6118. ['option' => ['value' => '02']],
  6119. date('F', strtotime('2008-02-01 00:00:00')),
  6120. '/option',
  6121. '*/select',
  6122. ];
  6123. $this->assertHtml($expected, $result);
  6124. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6125. $expected = [
  6126. ['select' => ['name' => 'Model[field][month]']],
  6127. ['option' => ['selected' => 'selected', 'value' => '']],
  6128. '/option',
  6129. ['option' => ['value' => '01']],
  6130. '1',
  6131. '/option',
  6132. ['option' => ['value' => '02']],
  6133. '2',
  6134. '/option',
  6135. '*/select',
  6136. ];
  6137. $this->assertHtml($expected, $result);
  6138. $monthNames = [
  6139. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6140. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec',
  6141. ];
  6142. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6143. $expected = [
  6144. ['select' => ['name' => 'Model[field][month]']],
  6145. ['option' => ['value' => '']],
  6146. '/option',
  6147. ['option' => ['value' => '01', 'selected' => 'selected']],
  6148. 'Jan',
  6149. '/option',
  6150. ['option' => ['value' => '02']],
  6151. 'Feb',
  6152. '/option',
  6153. '*/select',
  6154. ];
  6155. $this->assertHtml($expected, $result);
  6156. $this->View->setRequest(
  6157. $this->View->getRequest()->withData('Project.release', '2050-02-10')
  6158. );
  6159. $this->Form->create();
  6160. $result = $this->Form->month('Project.release');
  6161. $expected = [
  6162. ['select' => ['name' => 'Project[release][month]']],
  6163. ['option' => ['value' => '']],
  6164. '/option',
  6165. ['option' => ['value' => '01']],
  6166. 'January',
  6167. '/option',
  6168. ['option' => ['value' => '02', 'selected' => 'selected']],
  6169. 'February',
  6170. '/option',
  6171. '*/select',
  6172. ];
  6173. $this->assertHtml($expected, $result);
  6174. $result = $this->Form->month('Contact.published', [
  6175. 'empty' => 'Published on',
  6176. ]);
  6177. $this->assertContains('Published on', $result);
  6178. }
  6179. /**
  6180. * testDay method
  6181. *
  6182. * Test generation of a day input.
  6183. *
  6184. * @return void
  6185. */
  6186. public function testDay()
  6187. {
  6188. extract($this->dateRegex);
  6189. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6190. $expected = [
  6191. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6192. ['option' => ['selected' => 'selected', 'value' => '']],
  6193. '/option',
  6194. ['option' => ['value' => '01']],
  6195. '1',
  6196. '/option',
  6197. ['option' => ['value' => '02']],
  6198. '2',
  6199. '/option',
  6200. $daysRegex,
  6201. '/select',
  6202. ];
  6203. $this->assertHtml($expected, $result);
  6204. $this->View->setRequest(
  6205. $this->View->getRequest()->withData('Model.field', '2006-10-10 23:12:32')
  6206. );
  6207. $this->Form->create();
  6208. $result = $this->Form->day('Model.field');
  6209. $expected = [
  6210. ['select' => ['name' => 'Model[field][day]']],
  6211. ['option' => ['value' => '']],
  6212. '/option',
  6213. ['option' => ['value' => '01']],
  6214. '1',
  6215. '/option',
  6216. ['option' => ['value' => '02']],
  6217. '2',
  6218. '/option',
  6219. $daysRegex,
  6220. ['option' => ['value' => '10', 'selected' => 'selected']],
  6221. '10',
  6222. '/option',
  6223. $daysRegex,
  6224. '/select',
  6225. ];
  6226. $this->assertHtml($expected, $result);
  6227. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6228. $this->Form->create();
  6229. $result = $this->Form->day('Model.field', ['value' => '10']);
  6230. $expected = [
  6231. ['select' => ['name' => 'Model[field][day]']],
  6232. ['option' => ['value' => '']],
  6233. '/option',
  6234. ['option' => ['value' => '01']],
  6235. '1',
  6236. '/option',
  6237. ['option' => ['value' => '02']],
  6238. '2',
  6239. '/option',
  6240. $daysRegex,
  6241. ['option' => ['value' => '10', 'selected' => 'selected']],
  6242. '10',
  6243. '/option',
  6244. $daysRegex,
  6245. '/select',
  6246. ];
  6247. $this->assertHtml($expected, $result);
  6248. $this->View->setRequest(
  6249. $this->View->getRequest()->withData('Project.release', '2050-10-10')
  6250. );
  6251. $this->Form->create();
  6252. $result = $this->Form->day('Project.release');
  6253. $expected = [
  6254. ['select' => ['name' => 'Project[release][day]']],
  6255. ['option' => ['value' => '']],
  6256. '/option',
  6257. ['option' => ['value' => '01']],
  6258. '1',
  6259. '/option',
  6260. ['option' => ['value' => '02']],
  6261. '2',
  6262. '/option',
  6263. $daysRegex,
  6264. ['option' => ['value' => '10', 'selected' => 'selected']],
  6265. '10',
  6266. '/option',
  6267. $daysRegex,
  6268. '/select',
  6269. ];
  6270. $this->assertHtml($expected, $result);
  6271. $result = $this->Form->day('Contact.published', [
  6272. 'empty' => 'Published on',
  6273. ]);
  6274. $this->assertContains('Published on', $result);
  6275. }
  6276. /**
  6277. * testMinute method
  6278. *
  6279. * Test generation of a minute input.
  6280. *
  6281. * @return void
  6282. */
  6283. public function testMinute()
  6284. {
  6285. extract($this->dateRegex);
  6286. $result = $this->Form->minute('Model.field', ['value' => '']);
  6287. $expected = [
  6288. ['select' => ['name' => 'Model[field][minute]']],
  6289. ['option' => ['selected' => 'selected', 'value' => '']],
  6290. '/option',
  6291. ['option' => ['value' => '00']],
  6292. '00',
  6293. '/option',
  6294. ['option' => ['value' => '01']],
  6295. '01',
  6296. '/option',
  6297. ['option' => ['value' => '02']],
  6298. '02',
  6299. '/option',
  6300. $minutesRegex,
  6301. '/select',
  6302. ];
  6303. $this->assertHtml($expected, $result);
  6304. $this->View->setRequest(
  6305. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6306. );
  6307. $this->Form->create();
  6308. $result = $this->Form->minute('Model.field');
  6309. $expected = [
  6310. ['select' => ['name' => 'Model[field][minute]']],
  6311. ['option' => ['value' => '']],
  6312. '/option',
  6313. ['option' => ['value' => '00']],
  6314. '00',
  6315. '/option',
  6316. ['option' => ['value' => '01']],
  6317. '01',
  6318. '/option',
  6319. ['option' => ['value' => '02']],
  6320. '02',
  6321. '/option',
  6322. $minutesRegex,
  6323. ['option' => ['value' => '12', 'selected' => 'selected']],
  6324. '12',
  6325. '/option',
  6326. $minutesRegex,
  6327. '/select',
  6328. ];
  6329. $this->assertHtml($expected, $result);
  6330. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6331. $this->Form->create();
  6332. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6333. $expected = [
  6334. ['select' => ['name' => 'Model[field][minute]']],
  6335. ['option' => ['selected' => 'selected', 'value' => '']],
  6336. '/option',
  6337. ['option' => ['value' => '00']],
  6338. '00',
  6339. '/option',
  6340. ['option' => ['value' => '05']],
  6341. '05',
  6342. '/option',
  6343. ['option' => ['value' => '10']],
  6344. '10',
  6345. '/option',
  6346. $minutesRegex,
  6347. '/select',
  6348. ];
  6349. $this->assertHtml($expected, $result);
  6350. $this->View->setRequest(
  6351. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:10:32')
  6352. );
  6353. $this->Form->create();
  6354. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6355. $expected = [
  6356. ['select' => ['name' => 'Model[field][minute]']],
  6357. ['option' => ['value' => '']],
  6358. '/option',
  6359. ['option' => ['value' => '00']],
  6360. '00',
  6361. '/option',
  6362. ['option' => ['value' => '05']],
  6363. '05',
  6364. '/option',
  6365. ['option' => ['value' => '10', 'selected' => 'selected']],
  6366. '10',
  6367. '/option',
  6368. $minutesRegex,
  6369. '/select',
  6370. ];
  6371. $this->assertHtml($expected, $result);
  6372. }
  6373. /**
  6374. * testMeridian method
  6375. *
  6376. * Test generating an input for the meridian.
  6377. *
  6378. * @return void
  6379. */
  6380. public function testMeridian()
  6381. {
  6382. extract($this->dateRegex);
  6383. $now = new \DateTime();
  6384. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6385. $expected = [
  6386. ['select' => ['name' => 'Model[field][meridian]']],
  6387. ['option' => ['value' => '']],
  6388. '/option',
  6389. $meridianRegex,
  6390. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6391. $now->format('a'),
  6392. '/option',
  6393. '*/select',
  6394. ];
  6395. $this->assertHtml($expected, $result);
  6396. }
  6397. /**
  6398. * testHour method
  6399. *
  6400. * Test generation of an hour input.
  6401. *
  6402. * @return void
  6403. */
  6404. public function testHour()
  6405. {
  6406. extract($this->dateRegex);
  6407. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6408. $expected = [
  6409. ['select' => ['name' => 'Model[field][hour]']],
  6410. ['option' => ['selected' => 'selected', 'value' => '']],
  6411. '/option',
  6412. ['option' => ['value' => '01']],
  6413. '1',
  6414. '/option',
  6415. ['option' => ['value' => '02']],
  6416. '2',
  6417. '/option',
  6418. $hoursRegex,
  6419. '/select',
  6420. ];
  6421. $this->assertHtml($expected, $result);
  6422. $this->View->setRequest(
  6423. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6424. );
  6425. $this->Form->create();
  6426. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6427. $expected = [
  6428. ['select' => ['name' => 'Model[field][hour]']],
  6429. ['option' => ['value' => '']],
  6430. '/option',
  6431. ['option' => ['value' => '01']],
  6432. '1',
  6433. '/option',
  6434. ['option' => ['value' => '02']],
  6435. '2',
  6436. '/option',
  6437. $hoursRegex,
  6438. ['option' => ['value' => '12', 'selected' => 'selected']],
  6439. '12',
  6440. '/option',
  6441. '/select',
  6442. ];
  6443. $this->assertHtml($expected, $result);
  6444. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6445. $this->Form->create();
  6446. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6447. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6448. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6449. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6450. $this->View->setRequest(
  6451. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6452. );
  6453. $this->Form->create();
  6454. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6455. $expected = [
  6456. ['select' => ['name' => 'Model[field][hour]']],
  6457. ['option' => ['value' => '']],
  6458. '/option',
  6459. ['option' => ['value' => '00', 'selected' => 'selected']],
  6460. '0',
  6461. '/option',
  6462. ['option' => ['value' => '01']],
  6463. '1',
  6464. '/option',
  6465. ['option' => ['value' => '02']],
  6466. '2',
  6467. '/option',
  6468. $hoursRegex,
  6469. '/select',
  6470. ];
  6471. $this->assertHtml($expected, $result);
  6472. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  6473. $this->Form->create();
  6474. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6475. $thisHour = date('H');
  6476. $optValue = date('G');
  6477. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6478. $this->View->setRequest(
  6479. $this->View->getRequest()->withData('Model.field', '2050-10-10 01:12:32')
  6480. );
  6481. $this->Form->create();
  6482. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6483. $expected = [
  6484. ['select' => ['name' => 'Model[field][hour]']],
  6485. ['option' => ['value' => '']],
  6486. '/option',
  6487. ['option' => ['value' => '00']],
  6488. '0',
  6489. '/option',
  6490. ['option' => ['value' => '01', 'selected' => 'selected']],
  6491. '1',
  6492. '/option',
  6493. ['option' => ['value' => '02']],
  6494. '2',
  6495. '/option',
  6496. $hoursRegex,
  6497. '/select',
  6498. ];
  6499. $this->assertHtml($expected, $result);
  6500. }
  6501. /**
  6502. * testYear method
  6503. *
  6504. * Test generation of a year input.
  6505. *
  6506. * @return void
  6507. */
  6508. public function testYear()
  6509. {
  6510. $this->View->setRequest(
  6511. $this->View->getRequest()->withData('Contact.published', '2006-10-10')
  6512. );
  6513. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6514. $expected = [
  6515. ['select' => ['name' => 'Model[field][year]']],
  6516. ['option' => ['selected' => 'selected', 'value' => '']],
  6517. '/option',
  6518. ['option' => ['value' => '2007']],
  6519. '2007',
  6520. '/option',
  6521. ['option' => ['value' => '2006']],
  6522. '2006',
  6523. '/option',
  6524. '/select',
  6525. ];
  6526. $this->assertHtml($expected, $result);
  6527. $result = $this->Form->year('Model.field', [
  6528. 'value' => '',
  6529. 'minYear' => 2006,
  6530. 'maxYear' => 2007,
  6531. 'orderYear' => 'asc',
  6532. ]);
  6533. $expected = [
  6534. ['select' => ['name' => 'Model[field][year]']],
  6535. ['option' => ['selected' => 'selected', 'value' => '']],
  6536. '/option',
  6537. ['option' => ['value' => '2006']],
  6538. '2006',
  6539. '/option',
  6540. ['option' => ['value' => '2007']],
  6541. '2007',
  6542. '/option',
  6543. '/select',
  6544. ];
  6545. $this->assertHtml($expected, $result);
  6546. $result = $this->Form->year('Contact.published', [
  6547. 'empty' => false,
  6548. 'minYear' => 2006,
  6549. 'maxYear' => 2007,
  6550. ]);
  6551. $expected = [
  6552. ['select' => ['name' => 'Contact[published][year]']],
  6553. ['option' => ['value' => '2007']],
  6554. '2007',
  6555. '/option',
  6556. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6557. '2006',
  6558. '/option',
  6559. '/select',
  6560. ];
  6561. $this->assertHtml($expected, $result);
  6562. $result = $this->Form->year('Contact.published', [
  6563. 'empty' => 'Published on',
  6564. ]);
  6565. $this->assertContains('Published on', $result);
  6566. }
  6567. /**
  6568. * testControlDatetimePreEpoch method
  6569. *
  6570. * Test minYear being prior to the unix epoch.
  6571. *
  6572. * @return void
  6573. */
  6574. public function testControlDatetimePreEpoch()
  6575. {
  6576. $start = date('Y') - 80;
  6577. $end = date('Y') - 18;
  6578. $result = $this->Form->control('birth_year', [
  6579. 'type' => 'date',
  6580. 'label' => 'Birth Year',
  6581. 'minYear' => $start,
  6582. 'maxYear' => $end,
  6583. 'month' => false,
  6584. 'day' => false,
  6585. ]);
  6586. $this->assertContains('value="' . $start . '">' . $start, $result);
  6587. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6588. $this->assertNotContains('value="00">00', $result);
  6589. }
  6590. /**
  6591. * test control() datetime & required attributes
  6592. *
  6593. * @return void
  6594. */
  6595. public function testControlDatetimeRequired()
  6596. {
  6597. $result = $this->Form->control('birthday', [
  6598. 'type' => 'date',
  6599. 'required' => true,
  6600. ]);
  6601. $this->assertContains(
  6602. '<select name="birthday[year]" required="required"',
  6603. $result
  6604. );
  6605. }
  6606. /**
  6607. * testYearAutoExpandRange method
  6608. *
  6609. * @return void
  6610. */
  6611. public function testYearAutoExpandRange()
  6612. {
  6613. $this->View->setRequest($this->View->getRequest()->withData('User.birthday', '1930-10-10'));
  6614. $result = $this->Form->year('User.birthday');
  6615. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6616. $result = $matches[1];
  6617. $expected = range(date('Y') + 5, 1930);
  6618. $this->assertEquals($expected, $result);
  6619. $this->View->setRequest($this->View->getRequest()->withData('Project.release', '2050-10-10'));
  6620. $this->Form->create();
  6621. $result = $this->Form->year('Project.release');
  6622. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6623. $result = $matches[1];
  6624. $expected = range(2050, date('Y') - 5);
  6625. $this->assertEquals($expected, $result);
  6626. $this->View->setRequest($this->View->getRequest()->withData('Project.release', '1881-10-10'));
  6627. $this->Form->create();
  6628. $result = $this->Form->year('Project.release', [
  6629. 'minYear' => 1890,
  6630. 'maxYear' => 1900,
  6631. ]);
  6632. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6633. $result = $matches[1];
  6634. $expected = range(1900, 1881);
  6635. $this->assertEquals($expected, $result);
  6636. }
  6637. /**
  6638. * testControlDate method
  6639. *
  6640. * Test that control() accepts the type of date and passes options in.
  6641. *
  6642. * @return void
  6643. */
  6644. public function testControlDate()
  6645. {
  6646. $this->View->setRequest($this->View->getRequest()->withParsedBody([
  6647. 'month_year' => ['month' => date('m')],
  6648. ]));
  6649. $this->Form->create($this->article);
  6650. $result = $this->Form->control('month_year', [
  6651. 'label' => false,
  6652. 'type' => 'date',
  6653. 'minYear' => 2006,
  6654. 'maxYear' => 2008,
  6655. ]);
  6656. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6657. $this->assertNotContains('value="2008" selected="selected"', $result);
  6658. }
  6659. /**
  6660. * testControlLabelFalse method
  6661. *
  6662. * Test the label option being set to false.
  6663. *
  6664. * @return void
  6665. */
  6666. public function testControlLabelFalse()
  6667. {
  6668. $this->Form->create($this->article);
  6669. $result = $this->Form->control('title', ['label' => false]);
  6670. $expected = [
  6671. 'div' => ['class' => 'input text required'],
  6672. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6673. '/div',
  6674. ];
  6675. $this->assertHtml($expected, $result);
  6676. }
  6677. /**
  6678. * testControlDateMaxYear method
  6679. *
  6680. * Let's say we want to only allow users born from 2006 to 2008 to register
  6681. * This being the first signup page, we still don't have any data.
  6682. *
  6683. * @return void
  6684. */
  6685. public function testControlDateMaxYear()
  6686. {
  6687. $this->Form->create($this->article);
  6688. $result = $this->Form->control('birthday', [
  6689. 'label' => false,
  6690. 'type' => 'date',
  6691. 'minYear' => 2006,
  6692. 'maxYear' => 2008,
  6693. 'default' => true,
  6694. ]);
  6695. $this->assertContains('value="2008" selected="selected"', $result);
  6696. $this->assertContains('value="2006"', $result);
  6697. $this->assertNotContains('value="2005"', $result);
  6698. $this->assertNotContains('value="2009"', $result);
  6699. }
  6700. /**
  6701. * testTextArea method
  6702. *
  6703. * Test generation of a textarea input.
  6704. *
  6705. * @return void
  6706. */
  6707. public function testTextArea()
  6708. {
  6709. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  6710. $result = $this->Form->textarea('field');
  6711. $expected = [
  6712. 'textarea' => ['name' => 'field', 'rows' => 5],
  6713. 'some test data',
  6714. '/textarea',
  6715. ];
  6716. $this->assertHtml($expected, $result);
  6717. $result = $this->Form->textarea('user.bio');
  6718. $expected = [
  6719. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6720. '/textarea',
  6721. ];
  6722. $this->assertHtml($expected, $result);
  6723. $this->View->setRequest($this->View->getRequest()
  6724. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6725. $this->Form->create();
  6726. $result = $this->Form->textarea('field');
  6727. $expected = [
  6728. 'textarea' => ['name' => 'field', 'rows' => 5],
  6729. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6730. '/textarea',
  6731. ];
  6732. $this->assertHtml($expected, $result);
  6733. $this->View->setRequest($this->View->getRequest()->withData(
  6734. 'Model.field',
  6735. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  6736. ));
  6737. $this->Form->create();
  6738. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6739. $expected = [
  6740. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6741. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6742. '/textarea',
  6743. ];
  6744. $this->assertHtml($expected, $result);
  6745. $result = $this->Form->textarea('0.OtherModel.field');
  6746. $expected = [
  6747. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6748. '/textarea',
  6749. ];
  6750. $this->assertHtml($expected, $result);
  6751. }
  6752. /**
  6753. * testTextAreaWithStupidCharacters method
  6754. *
  6755. * Test text area with non-ascii characters.
  6756. *
  6757. * @return void
  6758. */
  6759. public function testTextAreaWithStupidCharacters()
  6760. {
  6761. $result = $this->Form->textarea('Post.content', [
  6762. 'value' => 'GREAT®',
  6763. 'rows' => '15',
  6764. 'cols' => '75',
  6765. ]);
  6766. $expected = [
  6767. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6768. 'GREAT®',
  6769. '/textarea',
  6770. ];
  6771. $this->assertHtml($expected, $result);
  6772. }
  6773. /**
  6774. * testTextAreaMaxLength method
  6775. *
  6776. * Test textareas maxlength read from schema.
  6777. *
  6778. * @return void
  6779. */
  6780. public function testTextAreaMaxLength()
  6781. {
  6782. $this->Form->create([
  6783. 'schema' => [
  6784. 'stuff' => ['type' => 'string', 'length' => 10],
  6785. ],
  6786. ]);
  6787. $result = $this->Form->control('other', ['type' => 'textarea']);
  6788. $expected = [
  6789. 'div' => ['class' => 'input textarea'],
  6790. 'label' => ['for' => 'other'],
  6791. 'Other',
  6792. '/label',
  6793. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6794. '/textarea',
  6795. '/div',
  6796. ];
  6797. $this->assertHtml($expected, $result);
  6798. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6799. $expected = [
  6800. 'div' => ['class' => 'input textarea'],
  6801. 'label' => ['for' => 'stuff'],
  6802. 'Stuff',
  6803. '/label',
  6804. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6805. '/textarea',
  6806. '/div',
  6807. ];
  6808. $this->assertHtml($expected, $result);
  6809. }
  6810. /**
  6811. * testHiddenField method
  6812. *
  6813. * Test generation of a hidden input.
  6814. *
  6815. * @return void
  6816. */
  6817. public function testHiddenField()
  6818. {
  6819. $this->article['errors'] = [
  6820. 'field' => true,
  6821. ];
  6822. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  6823. $this->Form->create($this->article);
  6824. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6825. $expected = [
  6826. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6827. $this->assertHtml($expected, $result);
  6828. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6829. $expected = [
  6830. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  6831. ];
  6832. $this->assertHtml($expected, $result);
  6833. }
  6834. /**
  6835. * testFileUploadField method
  6836. *
  6837. * Test generation of a file upload input.
  6838. *
  6839. * @return void
  6840. */
  6841. public function testFileUploadField()
  6842. {
  6843. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6844. $result = $this->Form->file('Model.upload');
  6845. $this->assertHtml($expected, $result);
  6846. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  6847. 'name' => '', 'type' => '', 'tmp_name' => '',
  6848. 'error' => 4, 'size' => 0,
  6849. ]));
  6850. $result = $this->Form->file('Model.upload');
  6851. $this->assertHtml($expected, $result);
  6852. $this->View->setRequest(
  6853. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6854. );
  6855. $result = $this->Form->file('Model.upload');
  6856. $this->assertHtml($expected, $result);
  6857. }
  6858. /**
  6859. * testFileUploadOnOtherModel method
  6860. *
  6861. * Test File upload input on a model not used in create().
  6862. *
  6863. * @return void
  6864. */
  6865. public function testFileUploadOnOtherModel()
  6866. {
  6867. $this->Form->create($this->article, ['type' => 'file']);
  6868. $result = $this->Form->file('ValidateProfile.city');
  6869. $expected = [
  6870. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  6871. ];
  6872. $this->assertHtml($expected, $result);
  6873. }
  6874. /**
  6875. * testButton method
  6876. *
  6877. * Test generation of a form button.
  6878. *
  6879. * @return void
  6880. */
  6881. public function testButton()
  6882. {
  6883. $result = $this->Form->button('Hi');
  6884. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6885. $this->assertHtml($expected, $result);
  6886. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6887. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6888. $this->assertHtml($expected, $result);
  6889. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6890. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6891. $this->assertHtml($expected, $result);
  6892. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6893. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6894. $this->assertHtml($expected, $result);
  6895. $result = $this->Form->button('No type', ['type' => false]);
  6896. $expected = ['button' => [], 'No type', '/button'];
  6897. $this->assertHtml($expected, $result);
  6898. $result = $this->Form->button('Upload Text', [
  6899. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6900. 'escape' => false,
  6901. ]);
  6902. $this->assertNotRegExp('/\&039/', $result);
  6903. }
  6904. /**
  6905. * testButtonUnlockedByDefault method
  6906. *
  6907. * Test that button() makes unlocked fields by default.
  6908. *
  6909. * @return void
  6910. */
  6911. public function testButtonUnlockedByDefault()
  6912. {
  6913. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'secured'));
  6914. $this->Form->button('Save', ['name' => 'save']);
  6915. $this->Form->button('Clear');
  6916. $result = $this->Form->unlockField();
  6917. $this->assertEquals(['save'], $result);
  6918. }
  6919. /**
  6920. * Test generation of a form button with confirm message.
  6921. *
  6922. * @return void
  6923. */
  6924. public function testButtonWithConfirm()
  6925. {
  6926. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6927. $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm(&quot;Confirm me!&quot;)) { return true; } return false;'], 'Hi', '/button'];
  6928. $this->assertHtml($expected, $result);
  6929. }
  6930. /**
  6931. * testPostButton method
  6932. *
  6933. * @return void
  6934. */
  6935. public function testPostButton()
  6936. {
  6937. $result = $this->Form->postButton('Hi', '/controller/action');
  6938. $expected = [
  6939. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6940. 'div' => ['style' => 'display:none;'],
  6941. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6942. '/div',
  6943. 'button' => ['type' => 'submit'],
  6944. 'Hi',
  6945. '/button',
  6946. '/form',
  6947. ];
  6948. $this->assertHtml($expected, $result);
  6949. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6950. $this->assertContains('<input type="hidden" name="extra" value="value"', $result);
  6951. }
  6952. /**
  6953. * testPostButtonMethodType method
  6954. *
  6955. * @return void
  6956. */
  6957. public function testPostButtonMethodType()
  6958. {
  6959. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6960. $expected = [
  6961. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6962. 'div' => ['style' => 'display:none;'],
  6963. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6964. '/div',
  6965. 'button' => ['type' => 'submit'],
  6966. 'Hi',
  6967. '/button',
  6968. '/form',
  6969. ];
  6970. $this->assertHtml($expected, $result);
  6971. }
  6972. /**
  6973. * testPostButtonFormOptions method
  6974. *
  6975. * @return void
  6976. */
  6977. public function testPostButtonFormOptions()
  6978. {
  6979. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6980. $expected = [
  6981. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6982. 'div' => ['style' => 'display:none;'],
  6983. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6984. '/div',
  6985. 'button' => ['type' => 'submit'],
  6986. 'Hi',
  6987. '/button',
  6988. '/form',
  6989. ];
  6990. $this->assertHtml($expected, $result);
  6991. }
  6992. /**
  6993. * testPostButtonNestedData method
  6994. *
  6995. * Test using postButton with N dimensional data.
  6996. *
  6997. * @return void
  6998. */
  6999. public function testPostButtonNestedData()
  7000. {
  7001. $data = [
  7002. 'one' => [
  7003. 'two' => [
  7004. 3, 4, 5,
  7005. ],
  7006. ],
  7007. ];
  7008. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  7009. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7010. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7011. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7012. }
  7013. /**
  7014. * testSecurePostButton method
  7015. *
  7016. * Test that postButton adds _Token fields.
  7017. *
  7018. * @return void
  7019. */
  7020. public function testSecurePostButton()
  7021. {
  7022. $this->View->setRequest($this->View->getRequest()
  7023. ->withParam('_csrfToken', 'testkey')
  7024. ->withParam('_Token.unlockedFields', []));
  7025. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  7026. $tokenDebug = urlencode(json_encode([
  7027. '/posts/delete/1',
  7028. [],
  7029. [],
  7030. ]));
  7031. $expected = [
  7032. 'form' => [
  7033. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  7034. ],
  7035. ['div' => ['style' => 'display:none;']],
  7036. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7037. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7038. '/div',
  7039. 'button' => ['type' => 'submit'],
  7040. 'Delete',
  7041. '/button',
  7042. ['div' => ['style' => 'display:none;']],
  7043. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7044. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7045. ['input' => [
  7046. 'type' => 'hidden',
  7047. 'name' => '_Token[debug]',
  7048. 'value' => $tokenDebug,
  7049. 'autocomplete' => 'off',
  7050. ]],
  7051. '/div',
  7052. '/form',
  7053. ];
  7054. $this->assertHtml($expected, $result);
  7055. }
  7056. /**
  7057. * testPostLink method
  7058. *
  7059. * @return void
  7060. */
  7061. public function testPostLink()
  7062. {
  7063. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7064. $expected = [
  7065. 'form' => [
  7066. 'method' => 'post', 'action' => '/posts/delete/1',
  7067. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7068. ],
  7069. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7070. '/form',
  7071. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7072. 'Delete',
  7073. '/a',
  7074. ];
  7075. $this->assertHtml($expected, $result);
  7076. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  7077. $expected = [
  7078. 'form' => [
  7079. 'method' => 'post', 'action' => '/posts/delete/1',
  7080. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7081. ],
  7082. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  7083. '/form',
  7084. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7085. 'Delete',
  7086. '/a',
  7087. ];
  7088. $this->assertHtml($expected, $result);
  7089. $result = $this->Form->postLink(
  7090. 'Delete',
  7091. '/posts/delete/1',
  7092. ['target' => '_blank', 'class' => 'btn btn-danger']
  7093. );
  7094. $expected = [
  7095. 'form' => [
  7096. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  7097. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7098. ],
  7099. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7100. '/form',
  7101. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7102. 'Delete',
  7103. '/a',
  7104. ];
  7105. $this->assertHtml($expected, $result);
  7106. }
  7107. /**
  7108. * testPostLinkWithConfirm method
  7109. *
  7110. * Test the confirm option for postLink().
  7111. *
  7112. * @return void
  7113. */
  7114. public function testPostLinkWithConfirm()
  7115. {
  7116. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  7117. $expected = [
  7118. 'form' => [
  7119. 'method' => 'post', 'action' => '/posts/delete/1',
  7120. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7121. ],
  7122. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7123. '/form',
  7124. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  7125. 'Delete',
  7126. '/a',
  7127. ];
  7128. $this->assertHtml($expected, $result);
  7129. $result = $this->Form->postLink(
  7130. 'Delete',
  7131. '/posts/delete/1',
  7132. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  7133. );
  7134. $expected = [
  7135. 'form' => [
  7136. 'method' => 'post', 'action' => '/posts/delete/1',
  7137. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7138. ],
  7139. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7140. '/form',
  7141. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  7142. 'Delete',
  7143. '/a',
  7144. ];
  7145. $this->assertHtml($expected, $result);
  7146. $this->Form->setTemplates(['confirmJs' => 'if (confirm({{confirmMessage}})) { $(\'form[name="{{formName}}"]\').submit();};']);
  7147. $result = $this->Form->postLink(
  7148. 'Delete',
  7149. '/posts/delete/1',
  7150. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  7151. );
  7152. $expected = [
  7153. 'form' => [
  7154. 'method' => 'post', 'action' => '/posts/delete/1',
  7155. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7156. ],
  7157. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7158. '/form',
  7159. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\("Confirm this deletion\?"\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/'],
  7160. 'Delete',
  7161. '/a',
  7162. ];
  7163. $this->assertHtml($expected, $result);
  7164. }
  7165. /**
  7166. * testPostLinkWithQuery method
  7167. *
  7168. * Test postLink() with query string args.
  7169. *
  7170. * @return void
  7171. */
  7172. public function testPostLinkWithQuery()
  7173. {
  7174. $result = $this->Form->postLink(
  7175. 'Delete',
  7176. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  7177. );
  7178. $expected = [
  7179. 'form' => [
  7180. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  7181. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7182. ],
  7183. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7184. '/form',
  7185. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7186. 'Delete',
  7187. '/a',
  7188. ];
  7189. $this->assertHtml($expected, $result);
  7190. }
  7191. /**
  7192. * testPostLinkWithData method
  7193. *
  7194. * Test postLink with additional data.
  7195. *
  7196. * @return void
  7197. */
  7198. public function testPostLinkWithData()
  7199. {
  7200. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7201. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7202. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7203. $this->Form->create($entity);
  7204. $this->Form->end();
  7205. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7206. $this->assertContains(
  7207. '<input type="hidden" name="name" value="show"',
  7208. $result,
  7209. 'should not contain entity data.'
  7210. );
  7211. }
  7212. /**
  7213. * testPostLinkSecurityHash method
  7214. *
  7215. * Test that security hashes for postLink include the url.
  7216. *
  7217. * @return void
  7218. */
  7219. public function testPostLinkSecurityHash()
  7220. {
  7221. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7222. $hash .= '%3Aid';
  7223. $this->View->setRequest($this->View->getRequest()->withParam('_Token.key', 'test'));
  7224. $result = $this->Form->postLink(
  7225. 'Delete',
  7226. '/posts/delete/1',
  7227. ['data' => ['id' => 1]]
  7228. );
  7229. $tokenDebug = urlencode(json_encode([
  7230. '/posts/delete/1',
  7231. [
  7232. 'id' => 1,
  7233. ],
  7234. [],
  7235. ]));
  7236. $expected = [
  7237. 'form' => [
  7238. 'method' => 'post', 'action' => '/posts/delete/1',
  7239. 'name', 'style' => 'display:none;',
  7240. ],
  7241. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7242. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7243. 'div' => ['style' => 'display:none;'],
  7244. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7245. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7246. ['input' => [
  7247. 'type' => 'hidden',
  7248. 'name' => '_Token[debug]',
  7249. 'value' => $tokenDebug,
  7250. 'autocomplete' => 'off',
  7251. ]],
  7252. '/div',
  7253. '/form',
  7254. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7255. 'Delete',
  7256. '/a',
  7257. ];
  7258. $this->assertHtml($expected, $result);
  7259. }
  7260. /**
  7261. * testPostLinkSecurityHashBlockMode method
  7262. *
  7263. * Test that postLink doesn't modify the fields in the containing form.
  7264. *
  7265. * postLink() calls inside open forms should not modify the field list
  7266. * for the form.
  7267. *
  7268. * @return void
  7269. */
  7270. public function testPostLinkSecurityHashBlockMode()
  7271. {
  7272. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  7273. $hash .= '%3A';
  7274. $this->View->setRequest($this->View->getRequest()->withParam('_Token.key', 'test'));
  7275. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7276. $this->Form->control('title');
  7277. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7278. $result = $this->View->fetch('postLink');
  7279. $this->assertEquals(['title'], $this->Form->fields);
  7280. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7281. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7282. }
  7283. /**
  7284. * testPostLinkSecurityHashNoDebugMode method
  7285. *
  7286. * Test that security does not include debug token if debug is false.
  7287. *
  7288. * @return void
  7289. */
  7290. public function testPostLinkSecurityHashNoDebugMode()
  7291. {
  7292. Configure::write('debug', false);
  7293. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7294. $hash .= '%3Aid';
  7295. $this->View->setRequest($this->View->getRequest()
  7296. ->withParam('_Token.key', 'test'));
  7297. $result = $this->Form->postLink(
  7298. 'Delete',
  7299. '/posts/delete/1',
  7300. ['data' => ['id' => 1]]
  7301. );
  7302. $expected = [
  7303. 'form' => [
  7304. 'method' => 'post', 'action' => '/posts/delete/1',
  7305. 'name', 'style' => 'display:none;',
  7306. ],
  7307. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7308. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7309. 'div' => ['style' => 'display:none;'],
  7310. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7311. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7312. '/div',
  7313. '/form',
  7314. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7315. 'Delete',
  7316. '/a',
  7317. ];
  7318. $this->assertHtml($expected, $result);
  7319. }
  7320. /**
  7321. * testPostLinkNestedData method
  7322. *
  7323. * Test using postLink with N dimensional data.
  7324. *
  7325. * @return void
  7326. */
  7327. public function testPostLinkNestedData()
  7328. {
  7329. $data = [
  7330. 'one' => [
  7331. 'two' => [
  7332. 3, 4, 5,
  7333. ],
  7334. ],
  7335. ];
  7336. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7337. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7338. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7339. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7340. }
  7341. /**
  7342. * testPostLinkAfterGetForm method
  7343. *
  7344. * Test creating postLinks after a GET form.
  7345. *
  7346. * @return void
  7347. */
  7348. public function testPostLinkAfterGetForm()
  7349. {
  7350. $this->View->setRequest($this->View->getRequest()
  7351. ->withParam('_csrfToken', 'testkey')
  7352. ->withParam('_Token', 'val'));
  7353. $this->Form->create($this->article, ['type' => 'get']);
  7354. $this->Form->end();
  7355. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7356. $tokenDebug = urlencode(json_encode([
  7357. '/posts/delete/1',
  7358. [],
  7359. [],
  7360. ]));
  7361. $expected = [
  7362. 'form' => [
  7363. 'method' => 'post', 'action' => '/posts/delete/1',
  7364. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7365. ],
  7366. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7367. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7368. 'div' => ['style' => 'display:none;'],
  7369. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7370. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7371. ['input' => [
  7372. 'type' => 'hidden', 'name' => '_Token[debug]',
  7373. 'value' => $tokenDebug,
  7374. 'autocomplete' => 'off',
  7375. ]],
  7376. '/div',
  7377. '/form',
  7378. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7379. 'Delete',
  7380. '/a',
  7381. ];
  7382. $this->assertHtml($expected, $result);
  7383. }
  7384. /**
  7385. * testPostLinkFormBuffer method
  7386. *
  7387. * Test that postLink adds form tags to view block.
  7388. *
  7389. * @return void
  7390. */
  7391. public function testPostLinkFormBuffer()
  7392. {
  7393. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7394. $expected = [
  7395. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7396. 'Delete',
  7397. '/a',
  7398. ];
  7399. $this->assertHtml($expected, $result);
  7400. $result = $this->View->fetch('postLink');
  7401. $expected = [
  7402. 'form' => [
  7403. 'method' => 'post', 'action' => '/posts/delete/1',
  7404. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7405. ],
  7406. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7407. '/form',
  7408. ];
  7409. $this->assertHtml($expected, $result);
  7410. $result = $this->Form->postLink(
  7411. 'Delete',
  7412. '/posts/delete/2',
  7413. ['block' => true, 'method' => 'DELETE']
  7414. );
  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('postLink');
  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. 'form' => [
  7431. 'method' => 'post', 'action' => '/posts/delete/2',
  7432. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7433. ],
  7434. ],
  7435. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7436. '/form',
  7437. ];
  7438. $this->assertHtml($expected, $result);
  7439. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7440. $expected = [
  7441. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7442. 'Delete',
  7443. '/a',
  7444. ];
  7445. $this->assertHtml($expected, $result);
  7446. $result = $this->View->fetch('foobar');
  7447. $expected = [
  7448. 'form' => [
  7449. 'method' => 'post', 'action' => '/posts/delete/1',
  7450. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  7451. ],
  7452. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7453. '/form',
  7454. ];
  7455. $this->assertHtml($expected, $result);
  7456. }
  7457. /**
  7458. * testSubmitButton method
  7459. *
  7460. * @return void
  7461. */
  7462. public function testSubmitButton()
  7463. {
  7464. $result = $this->Form->submit('');
  7465. $expected = [
  7466. 'div' => ['class' => 'submit'],
  7467. 'input' => ['type' => 'submit', 'value' => ''],
  7468. '/div',
  7469. ];
  7470. $this->assertHtml($expected, $result);
  7471. $result = $this->Form->submit('Test Submit');
  7472. $expected = [
  7473. 'div' => ['class' => 'submit'],
  7474. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7475. '/div',
  7476. ];
  7477. $this->assertHtml($expected, $result);
  7478. $result = $this->Form->submit('Next >');
  7479. $expected = [
  7480. 'div' => ['class' => 'submit'],
  7481. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7482. '/div',
  7483. ];
  7484. $this->assertHtml($expected, $result);
  7485. $result = $this->Form->submit('Next >', ['escape' => false]);
  7486. $expected = [
  7487. 'div' => ['class' => 'submit'],
  7488. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7489. '/div',
  7490. ];
  7491. $this->assertHtml($expected, $result);
  7492. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7493. $expected = [
  7494. 'div' => ['class' => 'submit'],
  7495. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7496. '/div',
  7497. ];
  7498. $this->assertHtml($expected, $result);
  7499. }
  7500. /**
  7501. * testSubmitImage method
  7502. *
  7503. * Test image submit types.
  7504. *
  7505. * @return void
  7506. */
  7507. public function testSubmitImage()
  7508. {
  7509. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7510. $expected = [
  7511. 'div' => ['class' => 'submit'],
  7512. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7513. '/div',
  7514. ];
  7515. $this->assertHtml($expected, $result);
  7516. $result = $this->Form->submit('/relative/cake.power.gif');
  7517. $expected = [
  7518. 'div' => ['class' => 'submit'],
  7519. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7520. '/div',
  7521. ];
  7522. $this->assertHtml($expected, $result);
  7523. $result = $this->Form->submit('cake.power.gif');
  7524. $expected = [
  7525. 'div' => ['class' => 'submit'],
  7526. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7527. '/div',
  7528. ];
  7529. $this->assertHtml($expected, $result);
  7530. $result = $this->Form->submit('Not.an.image');
  7531. $expected = [
  7532. 'div' => ['class' => 'submit'],
  7533. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7534. '/div',
  7535. ];
  7536. $this->assertHtml($expected, $result);
  7537. }
  7538. /**
  7539. * testSubmitUnlockedByDefault method
  7540. *
  7541. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7542. * be submitted at a time.
  7543. *
  7544. * @return void
  7545. */
  7546. public function testSubmitUnlockedByDefault()
  7547. {
  7548. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'secured'));
  7549. $this->Form->submit('Go go');
  7550. $this->Form->submit('Save', ['name' => 'save']);
  7551. $result = $this->Form->unlockField();
  7552. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7553. }
  7554. /**
  7555. * testSubmitImageTimestamp method
  7556. *
  7557. * Test submit image with timestamps.
  7558. *
  7559. * @return void
  7560. */
  7561. public function testSubmitImageTimestamp()
  7562. {
  7563. Configure::write('Asset.timestamp', 'force');
  7564. $result = $this->Form->submit('cake.power.gif');
  7565. $expected = [
  7566. 'div' => ['class' => 'submit'],
  7567. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7568. '/div',
  7569. ];
  7570. $this->assertHtml($expected, $result);
  7571. }
  7572. /**
  7573. * testDateTimeWithGetForms method
  7574. *
  7575. * Test that datetime() works with GET style forms.
  7576. *
  7577. * @return void
  7578. */
  7579. public function testDateTimeWithGetForms()
  7580. {
  7581. extract($this->dateRegex);
  7582. $this->Form->create($this->article, ['type' => 'get']);
  7583. $result = $this->Form->datetime('created');
  7584. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7585. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7586. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7587. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7588. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7589. }
  7590. /**
  7591. * testForMagicControlNonExistingNorValidated method
  7592. *
  7593. * @return void
  7594. */
  7595. public function testForMagicControlNonExistingNorValidated()
  7596. {
  7597. $this->Form->create($this->article);
  7598. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7599. $result = $this->Form->control('non_existing_nor_validated');
  7600. $expected = [
  7601. 'label' => ['for' => 'non-existing-nor-validated'],
  7602. 'Non Existing Nor Validated',
  7603. '/label',
  7604. 'input' => [
  7605. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7606. 'id' => 'non-existing-nor-validated',
  7607. ],
  7608. ];
  7609. $this->assertHtml($expected, $result);
  7610. $result = $this->Form->control('non_existing_nor_validated', [
  7611. 'val' => 'my value',
  7612. ]);
  7613. $expected = [
  7614. 'label' => ['for' => 'non-existing-nor-validated'],
  7615. 'Non Existing Nor Validated',
  7616. '/label',
  7617. 'input' => [
  7618. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7619. 'value' => 'my value', 'id' => 'non-existing-nor-validated',
  7620. ],
  7621. ];
  7622. $this->assertHtml($expected, $result);
  7623. $this->View->setRequest(
  7624. $this->View->getRequest()->withData('non_existing_nor_validated', 'CakePHP magic')
  7625. );
  7626. $this->Form->create($this->article);
  7627. $result = $this->Form->control('non_existing_nor_validated');
  7628. $expected = [
  7629. 'label' => ['for' => 'non-existing-nor-validated'],
  7630. 'Non Existing Nor Validated',
  7631. '/label',
  7632. 'input' => [
  7633. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7634. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated',
  7635. ],
  7636. ];
  7637. $this->assertHtml($expected, $result);
  7638. }
  7639. /**
  7640. * testFormMagicControlLabel method
  7641. *
  7642. * @return void
  7643. */
  7644. public function testFormMagicControlLabel()
  7645. {
  7646. $this->getTableLocator()->get('Contacts', [
  7647. 'className' => __NAMESPACE__ . '\ContactsTable',
  7648. ]);
  7649. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7650. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7651. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7652. $expected = [
  7653. 'label' => ['for' => 'contacts-name'],
  7654. 'My label',
  7655. '/label',
  7656. 'input' => [
  7657. 'type' => 'text', 'name' => 'Contacts[name]',
  7658. 'id' => 'contacts-name', 'maxlength' => '255',
  7659. ],
  7660. ];
  7661. $this->assertHtml($expected, $result);
  7662. $result = $this->Form->control('name', [
  7663. 'label' => ['class' => 'mandatory'],
  7664. ]);
  7665. $expected = [
  7666. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7667. 'Name',
  7668. '/label',
  7669. 'input' => [
  7670. 'type' => 'text', 'name' => 'name',
  7671. 'id' => 'name', 'maxlength' => '255',
  7672. ],
  7673. ];
  7674. $this->assertHtml($expected, $result);
  7675. $result = $this->Form->control('name', [
  7676. 'div' => false,
  7677. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  7678. ]);
  7679. $expected = [
  7680. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7681. 'My label',
  7682. '/label',
  7683. 'input' => [
  7684. 'type' => 'text', 'name' => 'name',
  7685. 'id' => 'name', 'maxlength' => '255',
  7686. ],
  7687. ];
  7688. $this->assertHtml($expected, $result);
  7689. $result = $this->Form->control('Contact.name', [
  7690. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  7691. ]);
  7692. $expected = [
  7693. 'label' => ['for' => 'my_id'],
  7694. 'Name',
  7695. '/label',
  7696. 'input' => [
  7697. 'type' => 'text', 'name' => 'Contact[name]',
  7698. 'id' => 'my_id', 'maxlength' => '255',
  7699. ],
  7700. ];
  7701. $this->assertHtml($expected, $result);
  7702. $result = $this->Form->control('1.id');
  7703. $expected = ['input' => [
  7704. 'type' => 'hidden', 'name' => '1[id]',
  7705. 'id' => '1-id',
  7706. ]];
  7707. $this->assertHtml($expected, $result);
  7708. $result = $this->Form->control('1.name');
  7709. $expected = [
  7710. 'label' => ['for' => '1-name'],
  7711. 'Name',
  7712. '/label',
  7713. 'input' => [
  7714. 'type' => 'text', 'name' => '1[name]',
  7715. 'id' => '1-name', 'maxlength' => '255',
  7716. ],
  7717. ];
  7718. $this->assertHtml($expected, $result);
  7719. }
  7720. /**
  7721. * testFormEnd method
  7722. *
  7723. * @return void
  7724. */
  7725. public function testFormEnd()
  7726. {
  7727. $this->assertEquals('</form>', $this->Form->end());
  7728. }
  7729. /**
  7730. * testMultiRecordForm method
  7731. *
  7732. * Test the generation of fields for a multi record form.
  7733. *
  7734. * @return void
  7735. */
  7736. public function testMultiRecordForm()
  7737. {
  7738. $this->loadFixtures('Articles', 'Comments');
  7739. $articles = $this->getTableLocator()->get('Articles');
  7740. $articles->hasMany('Comments');
  7741. $comment = new Entity(['comment' => 'Value']);
  7742. $article = new Article(['comments' => [$comment]]);
  7743. $this->Form->create([$article]);
  7744. $result = $this->Form->control('0.comments.1.comment');
  7745. //@codingStandardsIgnoreStart
  7746. $expected = [
  7747. 'div' => ['class' => 'input textarea'],
  7748. 'label' => ['for' => '0-comments-1-comment'],
  7749. 'Comment',
  7750. '/label',
  7751. 'textarea' => [
  7752. 'name',
  7753. 'id' => '0-comments-1-comment',
  7754. 'rows' => 5
  7755. ],
  7756. '/textarea',
  7757. '/div'
  7758. ];
  7759. //@codingStandardsIgnoreEnd
  7760. $this->assertHtml($expected, $result);
  7761. $result = $this->Form->control('0.comments.0.comment');
  7762. //@codingStandardsIgnoreStart
  7763. $expected = [
  7764. 'div' => ['class' => 'input textarea'],
  7765. 'label' => ['for' => '0-comments-0-comment'],
  7766. 'Comment',
  7767. '/label',
  7768. 'textarea' => [
  7769. 'name',
  7770. 'id' => '0-comments-0-comment',
  7771. 'rows' => 5
  7772. ],
  7773. 'Value',
  7774. '/textarea',
  7775. '/div'
  7776. ];
  7777. //@codingStandardsIgnoreEnd
  7778. $this->assertHtml($expected, $result);
  7779. $comment->setError('comment', ['Not valid']);
  7780. $result = $this->Form->control('0.comments.0.comment');
  7781. //@codingStandardsIgnoreStart
  7782. $expected = [
  7783. 'div' => ['class' => 'input textarea error'],
  7784. 'label' => ['for' => '0-comments-0-comment'],
  7785. 'Comment',
  7786. '/label',
  7787. 'textarea' => [
  7788. 'name',
  7789. 'class' => 'form-error',
  7790. 'id' => '0-comments-0-comment',
  7791. 'rows' => 5
  7792. ],
  7793. 'Value',
  7794. '/textarea',
  7795. ['div' => ['class' => 'error-message']],
  7796. 'Not valid',
  7797. '/div',
  7798. '/div'
  7799. ];
  7800. //@codingStandardsIgnoreEnd
  7801. $this->assertHtml($expected, $result);
  7802. $this->getTableLocator()->get('Comments')
  7803. ->getValidator('default')
  7804. ->allowEmpty('comment', false);
  7805. $result = $this->Form->control('0.comments.1.comment');
  7806. //@codingStandardsIgnoreStart
  7807. $expected = [
  7808. 'div' => ['class' => 'input textarea required'],
  7809. 'label' => ['for' => '0-comments-1-comment'],
  7810. 'Comment',
  7811. '/label',
  7812. 'textarea' => [
  7813. 'name',
  7814. 'required' => 'required',
  7815. 'id' => '0-comments-1-comment',
  7816. 'rows' => 5,
  7817. ],
  7818. '/textarea',
  7819. '/div'
  7820. ];
  7821. //@codingStandardsIgnoreEnd
  7822. $this->assertHtml($expected, $result);
  7823. }
  7824. /**
  7825. * testHtml5Controls method
  7826. *
  7827. * Test that some html5 inputs + FormHelper::__call() work.
  7828. *
  7829. * @return void
  7830. */
  7831. public function testHtml5Controls()
  7832. {
  7833. $result = $this->Form->email('User.email');
  7834. $expected = [
  7835. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  7836. ];
  7837. $this->assertHtml($expected, $result);
  7838. $result = $this->Form->search('User.query');
  7839. $expected = [
  7840. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  7841. ];
  7842. $this->assertHtml($expected, $result);
  7843. $result = $this->Form->search('User.query', ['value' => 'test']);
  7844. $expected = [
  7845. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  7846. ];
  7847. $this->assertHtml($expected, $result);
  7848. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7849. $expected = [
  7850. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  7851. ];
  7852. $this->assertHtml($expected, $result);
  7853. }
  7854. /**
  7855. * testHtml5ControlWithControl method
  7856. *
  7857. * Test accessing html5 inputs through control().
  7858. *
  7859. * @return void
  7860. */
  7861. public function testHtml5ControlWithControl()
  7862. {
  7863. $this->Form->create();
  7864. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7865. $result = $this->Form->control('website', [
  7866. 'type' => 'url',
  7867. 'val' => 'http://domain.tld',
  7868. 'label' => false,
  7869. ]);
  7870. $expected = [
  7871. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7872. ];
  7873. $this->assertHtml($expected, $result);
  7874. }
  7875. /**
  7876. * testHtml5ControlException method
  7877. *
  7878. * Test errors when field name is missing.
  7879. *
  7880. * @return void
  7881. */
  7882. public function testHtml5ControlException()
  7883. {
  7884. $this->expectException(\Cake\Core\Exception\Exception::class);
  7885. $this->Form->email();
  7886. }
  7887. /**
  7888. * tests fields that are required use custom validation messages
  7889. *
  7890. * @return void
  7891. */
  7892. public function testHtml5ErrorMessage()
  7893. {
  7894. $this->Form->setConfig('autoSetCustomValidity', true);
  7895. $validator = (new \Cake\Validation\Validator())
  7896. ->requirePresence('email', true, 'Custom error message')
  7897. ->requirePresence('password')
  7898. ->alphaNumeric('password')
  7899. ->notBlank('phone');
  7900. $table = $this->getTableLocator()->get('Contacts', [
  7901. 'className' => __NAMESPACE__ . '\ContactsTable',
  7902. ]);
  7903. $table->setValidator('default', $validator);
  7904. $contact = new Entity();
  7905. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7906. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7907. $result = $this->Form->control('password');
  7908. $expected = [
  7909. 'label' => ['for' => 'password'],
  7910. 'Password',
  7911. '/label',
  7912. 'input' => [
  7913. 'id' => 'password',
  7914. 'name' => 'password',
  7915. 'type' => 'password',
  7916. 'value' => '',
  7917. 'required' => 'required',
  7918. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7919. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.validity.valid) this.setCustomValidity(&#039;This field is required&#039;)',
  7920. ],
  7921. ];
  7922. $this->assertHtml($expected, $result);
  7923. $result = $this->Form->control('phone');
  7924. $expected = [
  7925. 'label' => ['for' => 'phone'],
  7926. 'Phone',
  7927. '/label',
  7928. 'input' => [
  7929. 'id' => 'phone',
  7930. 'name' => 'phone',
  7931. 'type' => 'tel',
  7932. 'value' => '',
  7933. 'maxlength' => 255,
  7934. 'required' => 'required',
  7935. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7936. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.validity.valid) this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  7937. ],
  7938. ];
  7939. $this->assertHtml($expected, $result);
  7940. $result = $this->Form->control('email');
  7941. $expected = [
  7942. 'label' => ['for' => 'email'],
  7943. 'Email',
  7944. '/label',
  7945. 'input' => [
  7946. 'id' => 'email',
  7947. 'name' => 'email',
  7948. 'type' => 'email',
  7949. 'value' => '',
  7950. 'maxlength' => 255,
  7951. 'required' => 'required',
  7952. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7953. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.validity.valid) this.setCustomValidity(&#039;Custom error message&#039;)',
  7954. ],
  7955. ];
  7956. $this->assertHtml($expected, $result);
  7957. }
  7958. /**
  7959. * tests that custom validation messages are in templateVars
  7960. *
  7961. * @return void
  7962. */
  7963. public function testHtml5ErrorMessageInTemplateVars()
  7964. {
  7965. $validator = (new \Cake\Validation\Validator())
  7966. ->requirePresence('email', true, 'Custom error "message" & entities')
  7967. ->requirePresence('password')
  7968. ->alphaNumeric('password')
  7969. ->notBlank('phone');
  7970. $table = $this->getTableLocator()->get('Contacts', [
  7971. 'className' => __NAMESPACE__ . '\ContactsTable',
  7972. ]);
  7973. $table->setValidator('default', $validator);
  7974. $contact = new Entity();
  7975. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7976. $this->Form->setTemplates([
  7977. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7978. 'inputContainer' => '{{content}}',
  7979. ]);
  7980. $result = $this->Form->control('password');
  7981. $expected = [
  7982. 'label' => ['for' => 'password'],
  7983. 'Password',
  7984. '/label',
  7985. 'input' => [
  7986. 'id' => 'password',
  7987. 'name' => 'password',
  7988. 'type' => 'password',
  7989. 'value' => '',
  7990. 'required' => 'required',
  7991. 'data-message' => 'This field is required',
  7992. ],
  7993. ];
  7994. $this->assertHtml($expected, $result);
  7995. $result = $this->Form->control('phone');
  7996. $expected = [
  7997. 'label' => ['for' => 'phone'],
  7998. 'Phone',
  7999. '/label',
  8000. 'input' => [
  8001. 'id' => 'phone',
  8002. 'name' => 'phone',
  8003. 'type' => 'tel',
  8004. 'value' => '',
  8005. 'maxlength' => 255,
  8006. 'required' => 'required',
  8007. 'data-message' => 'This field cannot be left empty',
  8008. ],
  8009. ];
  8010. $this->assertHtml($expected, $result);
  8011. $result = $this->Form->control('email', [
  8012. 'templateVars' => [
  8013. 'custom' => 'data-custom="1"',
  8014. ],
  8015. ]);
  8016. $expected = [
  8017. 'label' => ['for' => 'email'],
  8018. 'Email',
  8019. '/label',
  8020. 'input' => [
  8021. 'id' => 'email',
  8022. 'name' => 'email',
  8023. 'type' => 'email',
  8024. 'value' => '',
  8025. 'maxlength' => 255,
  8026. 'required' => 'required',
  8027. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  8028. 'data-custom' => '1',
  8029. ],
  8030. ];
  8031. $this->assertHtml($expected, $result);
  8032. }
  8033. /**
  8034. * testRequiredAttribute method
  8035. *
  8036. * Tests that formhelper sets required attributes.
  8037. *
  8038. * @return void
  8039. */
  8040. public function testRequiredAttribute()
  8041. {
  8042. $this->article['required'] = [
  8043. 'title' => true,
  8044. 'body' => false,
  8045. ];
  8046. $this->Form->create($this->article);
  8047. $result = $this->Form->control('title');
  8048. $expected = [
  8049. 'div' => ['class' => 'input text required'],
  8050. 'label' => ['for' => 'title'],
  8051. 'Title',
  8052. '/label',
  8053. 'input' => [
  8054. 'type' => 'text',
  8055. 'name' => 'title',
  8056. 'id' => 'title',
  8057. 'required' => 'required',
  8058. ],
  8059. '/div',
  8060. ];
  8061. $this->assertHtml($expected, $result);
  8062. $result = $this->Form->control('title', ['required' => false]);
  8063. $this->assertNotContains('required', $result);
  8064. $result = $this->Form->control('body');
  8065. $expected = [
  8066. 'div' => ['class' => 'input text'],
  8067. 'label' => ['for' => 'body'],
  8068. 'Body',
  8069. '/label',
  8070. 'input' => [
  8071. 'type' => 'text',
  8072. 'name' => 'body',
  8073. 'id' => 'body',
  8074. ],
  8075. '/div',
  8076. ];
  8077. $this->assertHtml($expected, $result);
  8078. $result = $this->Form->control('body', ['required' => true]);
  8079. $this->assertContains('required', $result);
  8080. }
  8081. /**
  8082. * testControlsNotNested method
  8083. *
  8084. * Tests that it is possible to put inputs outside of the label.
  8085. *
  8086. * @return void
  8087. */
  8088. public function testControlsNotNested()
  8089. {
  8090. $this->Form->setTemplates([
  8091. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  8092. 'formGroup' => '{{input}}{{label}}',
  8093. ]);
  8094. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  8095. $expected = [
  8096. 'div' => ['class' => 'input checkbox'],
  8097. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  8098. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  8099. 'label' => ['for' => 'foo'],
  8100. 'Foo',
  8101. '/label',
  8102. '/div',
  8103. ];
  8104. $this->assertHtml($expected, $result);
  8105. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  8106. $expected = [
  8107. 'div' => ['class' => 'input checkbox'],
  8108. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  8109. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  8110. '/div',
  8111. ];
  8112. $this->assertHtml($expected, $result);
  8113. $result = $this->Form->control('confirm', [
  8114. 'type' => 'radio',
  8115. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  8116. ]);
  8117. $expected = [
  8118. 'div' => ['class' => 'input radio'],
  8119. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  8120. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  8121. ['label' => ['for' => 'confirm-y']],
  8122. 'Yes',
  8123. '/label',
  8124. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  8125. ['label' => ['for' => 'confirm-n']],
  8126. 'No',
  8127. '/label',
  8128. '<label',
  8129. 'Confirm',
  8130. '/label',
  8131. '/div',
  8132. ];
  8133. $this->assertHtml($expected, $result);
  8134. $result = $this->Form->select('category', ['1', '2'], [
  8135. 'multiple' => 'checkbox',
  8136. 'name' => 'fish',
  8137. ]);
  8138. $expected = [
  8139. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  8140. ['div' => ['class' => 'checkbox']],
  8141. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  8142. ['label' => ['for' => 'fish-0']],
  8143. '1',
  8144. '/label',
  8145. '/div',
  8146. ['div' => ['class' => 'checkbox']],
  8147. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  8148. ['label' => ['for' => 'fish-1']],
  8149. '2',
  8150. '/label',
  8151. '/div',
  8152. ];
  8153. $this->assertHtml($expected, $result);
  8154. }
  8155. /**
  8156. * testControlContainerTemplates method
  8157. *
  8158. * Test that *Container templates are used by input.
  8159. *
  8160. * @return void
  8161. */
  8162. public function testControlContainerTemplates()
  8163. {
  8164. $this->Form->setTemplates([
  8165. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  8166. 'radioContainer' => '<div class="rad">{{content}}</div>',
  8167. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  8168. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  8169. ]);
  8170. $this->article['errors'] = [
  8171. 'Article' => ['published' => 'error message'],
  8172. ];
  8173. $this->Form->create($this->article);
  8174. $result = $this->Form->control('accept', [
  8175. 'type' => 'checkbox',
  8176. ]);
  8177. $expected = [
  8178. 'div' => ['class' => 'check'],
  8179. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  8180. 'label' => ['for' => 'accept'],
  8181. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  8182. 'Accept',
  8183. '/label',
  8184. '/div',
  8185. ];
  8186. $this->assertHtml($expected, $result);
  8187. $result = $this->Form->control('accept', [
  8188. 'type' => 'radio',
  8189. 'options' => ['Y', 'N'],
  8190. ]);
  8191. $this->assertContains('<div class="rad">', $result);
  8192. $result = $this->Form->control('Article.published', [
  8193. 'type' => 'radio',
  8194. 'options' => ['Y', 'N'],
  8195. ]);
  8196. $this->assertContains('<div class="rad err">', $result);
  8197. $result = $this->Form->control('Article.created', [
  8198. 'type' => 'datetime',
  8199. ]);
  8200. $this->assertContains('<div class="dt">', $result);
  8201. }
  8202. /**
  8203. * testFormGroupTemplates method
  8204. *
  8205. * Test that *Container templates are used by input.
  8206. *
  8207. * @return void
  8208. */
  8209. public function testFormGroupTemplates()
  8210. {
  8211. $this->Form->setTemplates([
  8212. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  8213. ]);
  8214. $this->Form->create($this->article);
  8215. $result = $this->Form->control('accept', [
  8216. 'type' => 'radio',
  8217. 'options' => ['Y', 'N'],
  8218. ]);
  8219. $this->assertContains('<div class="radio">', $result);
  8220. }
  8221. /**
  8222. * testResetTemplates method
  8223. *
  8224. * Test resetting templates.
  8225. *
  8226. * @return void
  8227. */
  8228. public function testResetTemplates()
  8229. {
  8230. $this->Form->setTemplates(['input' => '<input/>']);
  8231. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  8232. $this->Form->resetTemplates();
  8233. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  8234. }
  8235. /**
  8236. * testContext method
  8237. *
  8238. * Test the context method.
  8239. *
  8240. * @return void
  8241. */
  8242. public function testContext()
  8243. {
  8244. $result = $this->Form->context();
  8245. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  8246. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  8247. $this->assertSame($mock, $this->Form->context($mock));
  8248. $this->assertSame($mock, $this->Form->context());
  8249. }
  8250. /**
  8251. * testAutoDomId method
  8252. *
  8253. * @return void
  8254. */
  8255. public function testAutoDomId()
  8256. {
  8257. $result = $this->Form->text('field', ['id' => true]);
  8258. $expected = [
  8259. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  8260. ];
  8261. $this->assertHtml($expected, $result);
  8262. // Ensure id => doesn't cause problem when multiple inputs are generated.
  8263. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  8264. $expected = [
  8265. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  8266. ['label' => ['for' => 'field-0']],
  8267. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  8268. 'option A',
  8269. '/label',
  8270. ['label' => ['for' => 'field-1']],
  8271. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  8272. 'option B',
  8273. '/label',
  8274. ];
  8275. $this->assertHtml($expected, $result);
  8276. $result = $this->Form->select(
  8277. 'multi_field',
  8278. ['first', 'second'],
  8279. ['multiple' => 'checkbox', 'id' => true]
  8280. );
  8281. $expected = [
  8282. 'input' => [
  8283. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '',
  8284. ],
  8285. ['div' => ['class' => 'checkbox']],
  8286. ['label' => ['for' => 'multi-field-0']],
  8287. ['input' => [
  8288. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8289. 'value' => '0', 'id' => 'multi-field-0',
  8290. ]],
  8291. 'first',
  8292. '/label',
  8293. '/div',
  8294. ['div' => ['class' => 'checkbox']],
  8295. ['label' => ['for' => 'multi-field-1']],
  8296. ['input' => [
  8297. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8298. 'value' => '1', 'id' => 'multi-field-1',
  8299. ]],
  8300. 'second',
  8301. '/label',
  8302. '/div',
  8303. ];
  8304. $this->assertHtml($expected, $result);
  8305. }
  8306. /**
  8307. * Test the basic setters and getters for value sources
  8308. *
  8309. * @return void
  8310. */
  8311. public function testFormValueSourcesSettersGetters()
  8312. {
  8313. $this->View->setRequest($this->View->getRequest()
  8314. ->withData('id', '1')
  8315. ->withQueryParams(['id' => '2']));
  8316. $expected = ['context'];
  8317. $result = $this->Form->getValueSources();
  8318. $this->assertEquals($expected, $result);
  8319. $expected = ['query', 'data', 'context'];
  8320. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  8321. $result = $this->Form->getValueSources();
  8322. $this->assertEquals($expected, $result);
  8323. $this->Form->setValueSources(['context']);
  8324. $expected = '1';
  8325. $result = $this->Form->getSourceValue('id');
  8326. $this->assertEquals($expected, $result);
  8327. $this->Form->setValueSources('query');
  8328. $expected = ['query'];
  8329. $result = $this->Form->getValueSources();
  8330. $this->assertEquals($expected, $result);
  8331. $expected = '2';
  8332. $result = $this->Form->getSourceValue('id');
  8333. $this->assertEquals($expected, $result);
  8334. $this->Form->setValueSources(['data']);
  8335. $expected = '1';
  8336. $result = $this->Form->getSourceValue('id');
  8337. $this->assertEquals($expected, $result);
  8338. $this->Form->setValueSources(['query', 'data']);
  8339. $expected = '2';
  8340. $result = $this->Form->getSourceValue('id');
  8341. $this->assertEquals($expected, $result);
  8342. }
  8343. /**
  8344. * Tests the different input rendering values based on sources values switching
  8345. *
  8346. * @return void
  8347. */
  8348. public function testFormValueSourcesSingleSwitchRendering()
  8349. {
  8350. $this->loadFixtures('Articles');
  8351. $articles = $this->getTableLocator()->get('Articles');
  8352. $article = new Article();
  8353. $articles->patchEntity($article, ['id' => '3']);
  8354. $this->Form->create($article);
  8355. $this->Form->setValueSources(['context']);
  8356. $result = $this->Form->control('id');
  8357. $expected = [
  8358. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8359. ];
  8360. $this->assertHtml($expected, $result);
  8361. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  8362. $this->Form->setValueSources(['query']);
  8363. $result = $this->Form->control('id');
  8364. $expected = [
  8365. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8366. ];
  8367. $this->assertHtml($expected, $result);
  8368. $this->View->setRequest($this->View->getRequest()
  8369. ->withQueryParams(['id' => '5a'])
  8370. ->withData('id', '5b'));
  8371. $this->Form->setValueSources(['context']);
  8372. $this->Form->create($article);
  8373. $result = $this->Form->control('id');
  8374. $expected = [
  8375. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8376. ];
  8377. $this->assertHtml($expected, $result);
  8378. $this->Form->setValueSources(['data']);
  8379. $this->Form->create($article);
  8380. $result = $this->Form->control('id');
  8381. $expected = [
  8382. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8383. ];
  8384. $this->assertHtml($expected, $result);
  8385. $this->Form->setValueSources(['query']);
  8386. $result = $this->Form->control('id');
  8387. $expected = [
  8388. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8389. ];
  8390. $this->assertHtml($expected, $result);
  8391. }
  8392. /**
  8393. * Tests the different input rendering values based on sources values switching while supplying
  8394. * an entity (base context) and multiple sources (such as data, query)
  8395. *
  8396. * @return void
  8397. */
  8398. public function testFormValueSourcesListSwitchRendering()
  8399. {
  8400. $this->loadFixtures('Articles');
  8401. $articles = $this->getTableLocator()->get('Articles');
  8402. $article = new Article();
  8403. $articles->patchEntity($article, ['id' => '3']);
  8404. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  8405. $this->Form->create($article);
  8406. $this->Form->setValueSources(['context', 'query']);
  8407. $result = $this->Form->control('id');
  8408. $expected = [
  8409. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8410. ];
  8411. $this->assertHtml($expected, $result);
  8412. $this->Form->setValueSources(['query', 'context']);
  8413. $result = $this->Form->control('id');
  8414. $expected = [
  8415. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8416. ];
  8417. $this->assertHtml($expected, $result);
  8418. $this->Form->setValueSources(['data', 'query', 'context']);
  8419. $result = $this->Form->control('id');
  8420. $expected = [
  8421. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8422. ];
  8423. $this->assertHtml($expected, $result);
  8424. $this->View->setRequest($this->View->getRequest()
  8425. ->withData('id', '8')
  8426. ->withQueryParams(['id' => '9']));
  8427. $this->Form->setValueSources(['data', 'query', 'context']);
  8428. $result = $this->Form->control('id');
  8429. $expected = [
  8430. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8431. ];
  8432. $this->assertHtml($expected, $result);
  8433. }
  8434. /**
  8435. * Test the different form input renderings based on values sources switchings through form options
  8436. *
  8437. * @return void
  8438. */
  8439. public function testFormValueSourcesSwitchViaOptionsRendering()
  8440. {
  8441. $this->loadFixtures('Articles');
  8442. $articles = $this->getTableLocator()->get('Articles');
  8443. $article = new Article();
  8444. $articles->patchEntity($article, ['id' => '3']);
  8445. $this->View->setRequest(
  8446. $this->View->getRequest()->withData('id', 4)->withQueryParams(['id' => 5])
  8447. );
  8448. $this->Form->create($article, ['valueSources' => 'query']);
  8449. $result = $this->Form->control('id');
  8450. $expected = [
  8451. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8452. ];
  8453. $this->assertHtml($expected, $result);
  8454. $result = $this->Form->getSourceValue('id');
  8455. $this->assertEquals('5', $result);
  8456. $this->Form->setValueSources(['context']);
  8457. $this->Form->create($article, ['valueSources' => 'query']);
  8458. $result = $this->Form->control('id');
  8459. $expected = [
  8460. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8461. ];
  8462. $this->assertHtml($expected, $result);
  8463. $result = $this->Form->getSourceValue('id');
  8464. $this->assertEquals('5', $result);
  8465. $this->Form->setValueSources(['query']);
  8466. $this->Form->create($article, ['valueSources' => 'data']);
  8467. $result = $this->Form->control('id');
  8468. $expected = [
  8469. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8470. ];
  8471. $this->assertHtml($expected, $result);
  8472. $result = $this->Form->getSourceValue('id');
  8473. $this->assertEquals('4', $result);
  8474. $this->Form->setValueSources(['query']);
  8475. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8476. $result = $this->Form->control('id');
  8477. $expected = [
  8478. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8479. ];
  8480. $this->assertHtml($expected, $result);
  8481. $result = $this->Form->getSourceValue('id');
  8482. $this->assertEquals('4', $result);
  8483. }
  8484. /**
  8485. * Test the different form input renderings based on values sources switchings through form options
  8486. *
  8487. * @return void
  8488. */
  8489. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8490. {
  8491. $this->loadFixtures('Articles');
  8492. $articles = $this->getTableLocator()->get('Articles');
  8493. $article = new Article();
  8494. $articles->patchEntity($article, ['id' => '3']);
  8495. $this->View->setRequest(
  8496. $this->View->getRequest()->withData('id', 10)->withQueryParams(['id' => 11])
  8497. );
  8498. $this->Form->setValueSources(['context'])
  8499. ->create($article, ['valueSources' => ['query', 'data']]);
  8500. $result = $this->Form->control('id');
  8501. $expected = [
  8502. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8503. ];
  8504. $this->assertHtml($expected, $result);
  8505. $result = $this->Form->getSourceValue('id');
  8506. $this->assertEquals('11', $result);
  8507. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  8508. $this->Form->setValueSources(['context'])
  8509. ->create($article, ['valueSources' => ['query', 'data']]);
  8510. $result = $this->Form->control('id');
  8511. $expected = [
  8512. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8513. ];
  8514. $this->assertHtml($expected, $result);
  8515. $result = $this->Form->getSourceValue('id');
  8516. $this->assertEquals('10', $result);
  8517. }
  8518. /**
  8519. * Test the different form values sources resetting through From::end();
  8520. *
  8521. * @return void
  8522. */
  8523. public function testFormValueSourcesResetViaEnd()
  8524. {
  8525. $expected = ['context'];
  8526. $result = $this->Form->getValueSources();
  8527. $this->assertEquals($expected, $result);
  8528. $expected = ['query', 'context', 'data'];
  8529. $this->Form->setValueSources(['query', 'context', 'data']);
  8530. $result = $this->Form->getValueSources();
  8531. $this->assertEquals($expected, $result);
  8532. $this->Form->create();
  8533. $result = $this->Form->getValueSources();
  8534. $this->assertEquals($expected, $result);
  8535. $this->Form->end();
  8536. $result = $this->Form->getValueSources();
  8537. $this->assertEquals(['context'], $result);
  8538. }
  8539. /**
  8540. * Test sources values defaults handling
  8541. *
  8542. * @return void
  8543. */
  8544. public function testFormValueSourcesDefaults()
  8545. {
  8546. $this->View->setRequest(
  8547. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  8548. );
  8549. $this->Form->create();
  8550. $result = $this->Form->password('password');
  8551. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8552. $this->assertHtml($expected, $result);
  8553. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8554. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8555. $this->assertHtml($expected, $result);
  8556. $this->Form->setValueSources('query');
  8557. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8558. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8559. $this->assertHtml($expected, $result);
  8560. $this->Form->setValueSources('data');
  8561. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8562. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8563. $this->assertHtml($expected, $result);
  8564. }
  8565. /**
  8566. * Test sources values schema defaults handling
  8567. *
  8568. * @return void
  8569. */
  8570. public function testSourcesValueDoesntExistPassThrough()
  8571. {
  8572. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  8573. $articles = $this->getTableLocator()->get('Articles');
  8574. $entity = $articles->newEntity();
  8575. $this->Form->create($entity);
  8576. $this->Form->setValueSources(['query', 'context']);
  8577. $result = $this->Form->getSourceValue('category');
  8578. $this->assertEquals('sesame-cookies', $result);
  8579. $this->Form->setValueSources(['context', 'query']);
  8580. $result = $this->Form->getSourceValue('category');
  8581. $this->assertEquals('sesame-cookies', $result);
  8582. }
  8583. /**
  8584. * testNestedLabelInput method
  8585. *
  8586. * Test the `nestedInput` parameter
  8587. *
  8588. * @group deprecated
  8589. * @return void
  8590. */
  8591. public function testNestedLabelInput()
  8592. {
  8593. $this->deprecated(function () {
  8594. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8595. $expected = [
  8596. 'div' => ['class' => 'input text'],
  8597. 'label' => ['for' => 'foo'],
  8598. ['input' => [
  8599. 'type' => 'text',
  8600. 'name' => 'foo',
  8601. 'id' => 'foo',
  8602. ]],
  8603. 'Foo',
  8604. '/label',
  8605. '/div',
  8606. ];
  8607. $this->assertHtml($expected, $result);
  8608. });
  8609. }
  8610. /**
  8611. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8612. *
  8613. * This test makes sure `false` excludes the label from the render
  8614. *
  8615. * @return void
  8616. */
  8617. public function testControlLabelManipulationDisableLabels()
  8618. {
  8619. $result = $this->Form->control('test', [
  8620. 'type' => 'radio',
  8621. 'options' => ['A', 'B'],
  8622. 'labelOptions' => false,
  8623. ]);
  8624. $expected = [
  8625. ['div' => ['class' => 'input radio']],
  8626. '<label',
  8627. 'Test',
  8628. '/label',
  8629. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8630. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8631. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8632. '/div',
  8633. ];
  8634. $this->assertHtml($expected, $result);
  8635. $result = $this->Form->control('checkbox1', [
  8636. 'label' => 'My checkboxes',
  8637. 'multiple' => 'checkbox',
  8638. 'type' => 'select',
  8639. 'options' => [
  8640. ['text' => 'First Checkbox', 'value' => 1],
  8641. ['text' => 'Second Checkbox', 'value' => 2],
  8642. ],
  8643. 'labelOptions' => false,
  8644. ]);
  8645. $expected = [
  8646. ['div' => ['class' => 'input select']],
  8647. ['label' => ['for' => 'checkbox1']],
  8648. 'My checkboxes',
  8649. '/label',
  8650. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8651. ['div' => ['class' => 'checkbox']],
  8652. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8653. '/div',
  8654. ['div' => ['class' => 'checkbox']],
  8655. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8656. '/div',
  8657. '/div',
  8658. ];
  8659. $this->assertHtml($expected, $result);
  8660. }
  8661. /**
  8662. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8663. *
  8664. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8665. * added to the class if checked.
  8666. *
  8667. * Also checks to make sure any custom attributes are rendered correctly
  8668. *
  8669. * @return void
  8670. */
  8671. public function testControlLabelManipulationRadios()
  8672. {
  8673. $result = $this->Form->control('test', [
  8674. 'type' => 'radio',
  8675. 'options' => ['A', 'B'],
  8676. 'labelOptions' => ['class' => 'custom-class'],
  8677. ]);
  8678. $expected = [
  8679. ['div' => ['class' => 'input radio']],
  8680. '<label',
  8681. 'Test',
  8682. '/label',
  8683. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8684. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8685. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8686. 'A',
  8687. '/label',
  8688. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8689. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8690. 'B',
  8691. '/label',
  8692. '/div',
  8693. ];
  8694. $this->assertHtml($expected, $result);
  8695. $result = $this->Form->control('test', [
  8696. 'type' => 'radio',
  8697. 'options' => ['A', 'B'],
  8698. 'value' => 1,
  8699. 'labelOptions' => ['class' => 'custom-class'],
  8700. ]);
  8701. $expected = [
  8702. ['div' => ['class' => 'input radio']],
  8703. '<label',
  8704. 'Test',
  8705. '/label',
  8706. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8707. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8708. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8709. 'A',
  8710. '/label',
  8711. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8712. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8713. 'B',
  8714. '/label',
  8715. '/div',
  8716. ];
  8717. $this->assertHtml($expected, $result);
  8718. $result = $this->Form->control('test', [
  8719. 'type' => 'radio',
  8720. 'options' => ['A', 'B'],
  8721. 'value' => 1,
  8722. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  8723. ]);
  8724. $expected = [
  8725. ['div' => ['class' => 'input radio']],
  8726. '<label',
  8727. 'Test',
  8728. '/label',
  8729. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8730. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8731. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8732. 'A',
  8733. '/label',
  8734. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8735. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8736. 'B',
  8737. '/label',
  8738. '/div',
  8739. ];
  8740. $this->assertHtml($expected, $result);
  8741. $result = $this->Form->radio('test', ['A', 'B'], [
  8742. 'label' => [
  8743. 'class' => ['custom-class', 'another-class'],
  8744. 'data-name' => 'bob',
  8745. ],
  8746. 'value' => 1,
  8747. ]);
  8748. $expected = [
  8749. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8750. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8751. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8752. 'A',
  8753. '/label',
  8754. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8755. ['input' => [
  8756. 'type' => 'radio',
  8757. 'name' => 'test',
  8758. 'value' => '1',
  8759. 'id' => 'test-1',
  8760. 'checked' => 'checked',
  8761. ]],
  8762. 'B',
  8763. '/label',
  8764. ];
  8765. $this->assertHtml($expected, $result);
  8766. }
  8767. /**
  8768. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8769. *
  8770. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8771. * added to the class if checked.
  8772. *
  8773. * Also checks to make sure any custom attributes are rendered correctly
  8774. *
  8775. * @return void
  8776. */
  8777. public function testControlLabelManipulationCheckboxes()
  8778. {
  8779. $result = $this->Form->control('checkbox1', [
  8780. 'label' => 'My checkboxes',
  8781. 'multiple' => 'checkbox',
  8782. 'type' => 'select',
  8783. 'options' => [
  8784. ['text' => 'First Checkbox', 'value' => 1],
  8785. ['text' => 'Second Checkbox', 'value' => 2],
  8786. ],
  8787. 'labelOptions' => ['class' => 'custom-class'],
  8788. 'value' => ['1'],
  8789. ]);
  8790. $expected = [
  8791. ['div' => ['class' => 'input select']],
  8792. ['label' => ['for' => 'checkbox1']],
  8793. 'My checkboxes',
  8794. '/label',
  8795. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8796. ['div' => ['class' => 'checkbox']],
  8797. ['label' => [
  8798. 'class' => 'custom-class selected',
  8799. 'for' => 'checkbox1-1',
  8800. ]],
  8801. ['input' => [
  8802. 'type' => 'checkbox',
  8803. 'name' => 'checkbox1[]',
  8804. 'value' => '1',
  8805. 'id' => 'checkbox1-1',
  8806. 'checked' => 'checked',
  8807. ]],
  8808. 'First Checkbox',
  8809. '/label',
  8810. '/div',
  8811. ['div' => ['class' => 'checkbox']],
  8812. ['label' => [
  8813. 'class' => 'custom-class',
  8814. 'for' => 'checkbox1-2',
  8815. ]],
  8816. ['input' => [
  8817. 'type' => 'checkbox',
  8818. 'name' => 'checkbox1[]',
  8819. 'value' => '2',
  8820. 'id' => 'checkbox1-2',
  8821. ]],
  8822. 'Second Checkbox',
  8823. '/label',
  8824. '/div',
  8825. '/div',
  8826. ];
  8827. $this->assertHtml($expected, $result);
  8828. $result = $this->Form->control('checkbox1', [
  8829. 'label' => 'My checkboxes',
  8830. 'multiple' => 'checkbox',
  8831. 'type' => 'select',
  8832. 'options' => [
  8833. ['text' => 'First Checkbox', 'value' => 1],
  8834. ['text' => 'Second Checkbox', 'value' => 2],
  8835. ],
  8836. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8837. 'value' => ['1'],
  8838. ]);
  8839. $expected = [
  8840. ['div' => ['class' => 'input select']],
  8841. ['label' => ['for' => 'checkbox1']],
  8842. 'My checkboxes',
  8843. '/label',
  8844. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8845. ['div' => ['class' => 'checkbox']],
  8846. ['label' => [
  8847. 'class' => 'custom-class another-class selected',
  8848. 'data-name' => 'bob',
  8849. 'for' => 'checkbox1-1',
  8850. ]],
  8851. ['input' => [
  8852. 'type' => 'checkbox',
  8853. 'name' => 'checkbox1[]',
  8854. 'value' => '1',
  8855. 'id' => 'checkbox1-1',
  8856. 'checked' => 'checked',
  8857. ]],
  8858. 'First Checkbox',
  8859. '/label',
  8860. '/div',
  8861. ['div' => ['class' => 'checkbox']],
  8862. ['label' => [
  8863. 'class' => 'custom-class another-class',
  8864. 'data-name' => 'bob',
  8865. 'for' => 'checkbox1-2',
  8866. ]],
  8867. ['input' => [
  8868. 'type' => 'checkbox',
  8869. 'name' => 'checkbox1[]',
  8870. 'value' => '2',
  8871. 'id' => 'checkbox1-2',
  8872. ]],
  8873. 'Second Checkbox',
  8874. '/label',
  8875. '/div',
  8876. '/div',
  8877. ];
  8878. $this->assertHtml($expected, $result);
  8879. }
  8880. /**
  8881. * testControlMaxLengthArrayContext method
  8882. *
  8883. * Test control() with maxlength attribute in Array Context.
  8884. *
  8885. * @return void
  8886. */
  8887. public function testControlMaxLengthArrayContext()
  8888. {
  8889. $this->article['schema'] = [
  8890. 'title' => ['length' => 10],
  8891. ];
  8892. $this->Form->create($this->article);
  8893. $result = $this->Form->control('title');
  8894. $expected = [
  8895. 'div' => ['class'],
  8896. 'label' => ['for'],
  8897. 'Title',
  8898. '/label',
  8899. 'input' => [
  8900. 'id',
  8901. 'name' => 'title',
  8902. 'type' => 'text',
  8903. 'required' => 'required',
  8904. 'maxlength' => 10,
  8905. ],
  8906. '/div',
  8907. ];
  8908. $this->assertHtml($expected, $result);
  8909. }
  8910. /**
  8911. * testControlMaxLengthEntityContext method
  8912. *
  8913. * Test control() with maxlength attribute in Entity Context.
  8914. *
  8915. * @return void
  8916. */
  8917. public function testControlMaxLengthEntityContext()
  8918. {
  8919. $this->article['schema']['title']['length'] = 45;
  8920. $validator = new Validator();
  8921. $validator->maxLength('title', 10);
  8922. $article = new EntityContext(
  8923. new ServerRequest(),
  8924. [
  8925. 'entity' => new Entity($this->article),
  8926. 'table' => new Table([
  8927. 'schema' => $this->article['schema'],
  8928. 'validator' => $validator,
  8929. ]),
  8930. ]
  8931. );
  8932. $this->Form->create($article);
  8933. $result = $this->Form->control('title');
  8934. $expected = [
  8935. 'div' => ['class'],
  8936. 'label' => ['for'],
  8937. 'Title',
  8938. '/label',
  8939. 'input' => [
  8940. 'id',
  8941. 'name' => 'title',
  8942. 'type' => 'text',
  8943. 'required' => 'required',
  8944. 'maxlength' => 10,
  8945. ],
  8946. '/div',
  8947. ];
  8948. $this->assertHtml($expected, $result);
  8949. $this->article['schema']['title']['length'] = 45;
  8950. $validator = new Validator();
  8951. $validator->maxLength('title', 55);
  8952. $article = new EntityContext(
  8953. new ServerRequest(),
  8954. [
  8955. 'entity' => new Entity($this->article),
  8956. 'table' => new Table([
  8957. 'schema' => $this->article['schema'],
  8958. 'validator' => $validator,
  8959. ]),
  8960. ]
  8961. );
  8962. $this->Form->create($article);
  8963. $result = $this->Form->control('title');
  8964. $expected = [
  8965. 'div' => ['class'],
  8966. 'label' => ['for'],
  8967. 'Title',
  8968. '/label',
  8969. 'input' => [
  8970. 'id',
  8971. 'name' => 'title',
  8972. 'type' => 'text',
  8973. 'required' => 'required',
  8974. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8975. ],
  8976. '/div',
  8977. ];
  8978. $this->assertHtml($expected, $result);
  8979. $this->article['schema']['title']['length'] = 45;
  8980. $validator = new Validator();
  8981. $validator->maxLength('title', 55);
  8982. $article = new EntityContext(
  8983. new ServerRequest(),
  8984. [
  8985. 'entity' => new Entity($this->article),
  8986. 'table' => new Table([
  8987. 'schema' => $this->article['schema'],
  8988. 'validator' => $validator,
  8989. ]),
  8990. ]
  8991. );
  8992. $this->Form->create($article);
  8993. $result = $this->Form->control('title', ['maxlength' => 10]);
  8994. $expected = [
  8995. 'div' => ['class'],
  8996. 'label' => ['for'],
  8997. 'Title',
  8998. '/label',
  8999. 'input' => [
  9000. 'id',
  9001. 'name' => 'title',
  9002. 'type' => 'text',
  9003. 'required' => 'required',
  9004. 'maxlength' => 10, // Length set in options should take highest precedence.
  9005. ],
  9006. '/div',
  9007. ];
  9008. $this->assertHtml($expected, $result);
  9009. }
  9010. /**
  9011. * testControlMinMaxLengthEntityContext method
  9012. *
  9013. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  9014. *
  9015. * @return void
  9016. */
  9017. public function testControlMinMaxLengthEntityContext()
  9018. {
  9019. $validator = new Validator();
  9020. $validator->maxLength('title', 10);
  9021. $article = new EntityContext(
  9022. new ServerRequest(),
  9023. [
  9024. 'entity' => new Entity($this->article),
  9025. 'table' => new Table([
  9026. 'schema' => $this->article['schema'],
  9027. 'validator' => $validator,
  9028. ]),
  9029. ]
  9030. );
  9031. $this->Form->create($article);
  9032. $result = $this->Form->control('title');
  9033. $expected = [
  9034. 'div' => ['class'],
  9035. 'label' => ['for'],
  9036. 'Title',
  9037. '/label',
  9038. 'input' => [
  9039. 'id',
  9040. 'name' => 'title',
  9041. 'type' => 'text',
  9042. 'required' => 'required',
  9043. 'maxlength' => 10,
  9044. ],
  9045. '/div',
  9046. ];
  9047. $this->assertHtml($expected, $result);
  9048. }
  9049. /**
  9050. * testControlMaxLengthFormContext method
  9051. *
  9052. * Test control() with maxlength attribute in Form Context.
  9053. *
  9054. * @return void
  9055. */
  9056. public function testControlMaxLengthFormContext()
  9057. {
  9058. $validator = new Validator();
  9059. $validator->maxLength('title', 10);
  9060. $form = new Form();
  9061. $form->setValidator('default', $validator);
  9062. $this->Form->create($form);
  9063. $result = $this->Form->control('title');
  9064. $expected = [
  9065. 'div' => ['class'],
  9066. 'label' => ['for'],
  9067. 'Title',
  9068. '/label',
  9069. 'input' => [
  9070. 'id',
  9071. 'name' => 'title',
  9072. 'type' => 'text',
  9073. 'required' => 'required',
  9074. 'maxlength' => 10,
  9075. ],
  9076. '/div',
  9077. ];
  9078. $this->assertHtml($expected, $result);
  9079. }
  9080. }