FormHelperTest.php 317 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577
  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\View\Helper\FormHelper;
  26. use Cake\View\View;
  27. use Cake\View\Widget\WidgetLocator;
  28. /**
  29. * Test stub.
  30. */
  31. class Article extends Entity
  32. {
  33. }
  34. /**
  35. * Contact class
  36. */
  37. class ContactsTable extends Table
  38. {
  39. /**
  40. * Default schema
  41. *
  42. * @var array
  43. */
  44. protected $_schema = [
  45. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  46. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  47. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  48. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  51. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  52. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  53. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  54. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  55. ];
  56. /**
  57. * Initializes the schema
  58. *
  59. * @return void
  60. */
  61. public function initialize(array $config)
  62. {
  63. $this->setSchema($this->_schema);
  64. }
  65. }
  66. /**
  67. * ValidateUser class
  68. */
  69. class ValidateUsersTable extends Table
  70. {
  71. /**
  72. * schema method
  73. *
  74. * @var array
  75. */
  76. protected $_schema = [
  77. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  78. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  79. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  80. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  81. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  82. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  83. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  84. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  85. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  86. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  87. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  88. ];
  89. /**
  90. * Initializes the schema
  91. *
  92. * @return void
  93. */
  94. public function initialize(array $config)
  95. {
  96. $this->setSchema($this->_schema);
  97. }
  98. }
  99. /**
  100. * FormHelperTest class
  101. *
  102. * @property \Cake\View\Helper\FormHelper $Form
  103. * @property \Cake\View\View $View
  104. */
  105. class FormHelperTest extends TestCase
  106. {
  107. /**
  108. * Fixtures to be used
  109. *
  110. * @var array
  111. */
  112. public $fixtures = ['core.articles', 'core.comments'];
  113. /**
  114. * Do not load the fixtures by default
  115. *
  116. * @var bool
  117. */
  118. public $autoFixtures = false;
  119. /**
  120. * @var array
  121. */
  122. protected $article = [];
  123. /**
  124. * setUp method
  125. *
  126. * @return void
  127. */
  128. public function setUp()
  129. {
  130. parent::setUp();
  131. Configure::write('Config.language', 'eng');
  132. Configure::write('App.base', '');
  133. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  134. $request = new ServerRequest([
  135. 'webroot' => '',
  136. 'base' => '',
  137. 'url' => '/articles/add',
  138. 'params' => [
  139. 'controller' => 'articles',
  140. 'action' => 'add',
  141. ]
  142. ]);
  143. $this->View = new View($request);
  144. $this->Form = new FormHelper($this->View);
  145. $this->dateRegex = [
  146. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  147. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  148. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  149. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  150. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  151. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  152. ];
  153. $this->article = [
  154. 'schema' => [
  155. 'id' => ['type' => 'integer'],
  156. 'author_id' => ['type' => 'integer', 'null' => true],
  157. 'title' => ['type' => 'string', 'null' => true],
  158. 'body' => 'text',
  159. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  160. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  161. ],
  162. 'required' => [
  163. 'author_id' => true,
  164. 'title' => true,
  165. ]
  166. ];
  167. Security::setSalt('foo!');
  168. Router::connect('/:controller', ['action' => 'index']);
  169. Router::connect('/:controller/:action/*');
  170. }
  171. /**
  172. * tearDown method
  173. *
  174. * @return void
  175. */
  176. public function tearDown()
  177. {
  178. parent::tearDown();
  179. unset($this->Form, $this->Controller, $this->View);
  180. $this->getTableLocator()->clear();
  181. }
  182. /**
  183. * Test construct() with the templates option.
  184. *
  185. * @return void
  186. */
  187. public function testConstructTemplatesFile()
  188. {
  189. $helper = new FormHelper($this->View, [
  190. 'templates' => 'htmlhelper_tags'
  191. ]);
  192. $result = $helper->control('name');
  193. $this->assertContains('<input', $result);
  194. }
  195. /**
  196. * Test that when specifying custom widgets the config array for that widget
  197. * is overwritten instead of merged.
  198. *
  199. * @return void
  200. */
  201. public function testConstructWithWidgets()
  202. {
  203. $config = [
  204. 'widgets' => [
  205. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  206. ]
  207. ];
  208. $helper = new FormHelper($this->View, $config);
  209. $locator = $helper->getWidgetLocator();
  210. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  211. }
  212. /**
  213. * Test that when specifying custom widgets config file and it should be
  214. * added to widgets array. WidgetLocator will load widgets in constructor.
  215. *
  216. * @return void
  217. */
  218. public function testConstructWithWidgetsConfig()
  219. {
  220. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  221. $locator = $helper->getWidgetLocator();
  222. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  223. }
  224. /**
  225. * Test setting the widget locator
  226. *
  227. * @return void
  228. */
  229. public function testSetAndGetWidgetLocator()
  230. {
  231. $helper = new FormHelper($this->View);
  232. $locator = new WidgetLocator($helper->templater(), $this->View);
  233. $helper->setWidgetLocator($locator);
  234. $this->assertSame($locator, $helper->getWidgetLocator());
  235. }
  236. /**
  237. * Test overridding grouped input types which controls generation of "for"
  238. * attribute of labels.
  239. *
  240. * @return void
  241. */
  242. public function testConstructWithGroupedInputTypes()
  243. {
  244. $helper = new FormHelper($this->View, [
  245. 'groupedInputTypes' => ['radio'],
  246. ]);
  247. $result = $helper->control('when', ['type' => 'datetime']);
  248. $this->assertContains('<label for="when">When</label>', $result);
  249. }
  250. /**
  251. * Test registering a new widget class and rendering it.
  252. *
  253. * @return void
  254. */
  255. public function testAddWidgetAndRenderWidget()
  256. {
  257. $data = [
  258. 'val' => 1
  259. ];
  260. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  261. $this->Form->addWidget('test', $mock);
  262. $mock->expects($this->once())
  263. ->method('render')
  264. ->with($data)
  265. ->will($this->returnValue('HTML'));
  266. $result = $this->Form->widget('test', $data);
  267. $this->assertEquals('HTML', $result);
  268. }
  269. /**
  270. * Test that secureFields() of widget is called after calling render(),
  271. * not before.
  272. *
  273. * @return void
  274. */
  275. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  276. {
  277. $data = [
  278. 'val' => 1,
  279. 'name' => 'test'
  280. ];
  281. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  282. $this->Form->addWidget('test', $mock);
  283. $mock->expects($this->at(0))
  284. ->method('render')
  285. ->with($data)
  286. ->will($this->returnValue('HTML'));
  287. $mock->expects($this->at(1))
  288. ->method('secureFields')
  289. ->with($data)
  290. ->will($this->returnValue(['test']));
  291. $result = $this->Form->widget('test', $data + ['secure' => true]);
  292. $this->assertEquals('HTML', $result);
  293. }
  294. /**
  295. * Test that empty string is not added to secure fields list when
  296. * rendering input widget without name.
  297. *
  298. * @return void
  299. */
  300. public function testRenderingWidgetWithEmptyName()
  301. {
  302. $this->assertEquals([], $this->Form->fields);
  303. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  304. $this->assertEquals('<select name=""></select>', $result);
  305. $this->assertEquals([], $this->Form->fields);
  306. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  307. $this->assertEquals('<select name="0"></select>', $result);
  308. $this->assertEquals(['0'], $this->Form->fields);
  309. }
  310. /**
  311. * Test registering an invalid widget class.
  312. *
  313. * @return void
  314. */
  315. public function testAddWidgetInvalid()
  316. {
  317. $this->expectException(\RuntimeException::class);
  318. $mock = new \StdClass();
  319. $this->Form->addWidget('test', $mock);
  320. $this->Form->widget('test');
  321. }
  322. /**
  323. * Test adding a new context class.
  324. *
  325. * @return void
  326. */
  327. public function testAddContextProvider()
  328. {
  329. $context = 'My data';
  330. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  331. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  332. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  333. $this->assertEquals($context, $data['entity']);
  334. return $stub;
  335. });
  336. $this->Form->create($context);
  337. $result = $this->Form->context();
  338. $this->assertSame($stub, $result);
  339. }
  340. /**
  341. * Test replacing a context class.
  342. *
  343. * @return void
  344. */
  345. public function testAddContextProviderReplace()
  346. {
  347. $entity = new Article();
  348. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  349. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  350. return $stub;
  351. });
  352. $this->Form->create($entity);
  353. $result = $this->Form->context();
  354. $this->assertSame($stub, $result);
  355. }
  356. /**
  357. * Test overriding a context class.
  358. *
  359. * @return void
  360. */
  361. public function testAddContextProviderAdd()
  362. {
  363. $entity = new Article();
  364. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  365. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  366. if ($data['entity'] instanceof Entity) {
  367. return $stub;
  368. }
  369. });
  370. $this->Form->create($entity);
  371. $result = $this->Form->context();
  372. $this->assertSame($stub, $result);
  373. }
  374. /**
  375. * Test adding an invalid context class.
  376. *
  377. * @return void
  378. */
  379. public function testAddContextProviderInvalid()
  380. {
  381. $this->expectException(\RuntimeException::class);
  382. $this->expectExceptionMessage('Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.');
  383. $context = 'My data';
  384. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  385. return new \StdClass();
  386. });
  387. $this->Form->create($context);
  388. }
  389. /**
  390. * Provides context options for create().
  391. *
  392. * @return array
  393. */
  394. public function contextSelectionProvider()
  395. {
  396. $entity = new Article();
  397. $collection = new Collection([$entity]);
  398. $emptyCollection = new Collection([]);
  399. $arrayObject = new \ArrayObject([]);
  400. $data = [
  401. 'schema' => [
  402. 'title' => ['type' => 'string']
  403. ]
  404. ];
  405. $form = new Form();
  406. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  407. return [
  408. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  409. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  410. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  411. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  412. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  413. 'form' => [$form, 'Cake\View\Form\FormContext'],
  414. 'none' => [null, 'Cake\View\Form\NullContext'],
  415. 'false' => [false, 'Cake\View\Form\NullContext'],
  416. 'custom' => [$custom, get_class($custom)]
  417. ];
  418. }
  419. /**
  420. * Test default context selection in create()
  421. *
  422. * @dataProvider contextSelectionProvider
  423. * @return void
  424. */
  425. public function testCreateContextSelectionBuiltIn($data, $class)
  426. {
  427. $this->loadFixtures('Articles');
  428. $this->Form->create($data);
  429. $this->assertInstanceOf($class, $this->Form->context());
  430. }
  431. /**
  432. * Data provider for type option.
  433. *
  434. * @return array
  435. */
  436. public static function requestTypeProvider()
  437. {
  438. return [
  439. // type, method, override
  440. ['post', 'post', 'POST'],
  441. ['put', 'post', 'PUT'],
  442. ['patch', 'post', 'PATCH'],
  443. ['delete', 'post', 'DELETE'],
  444. ];
  445. }
  446. /**
  447. * Test creating file forms.
  448. *
  449. * @return void
  450. */
  451. public function testCreateFile()
  452. {
  453. $encoding = strtolower(Configure::read('App.encoding'));
  454. $result = $this->Form->create(false, ['type' => 'file']);
  455. $expected = [
  456. 'form' => [
  457. 'method' => 'post', 'action' => '/articles/add',
  458. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  459. ],
  460. 'div' => ['style' => 'display:none;'],
  461. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  462. '/div'
  463. ];
  464. $this->assertHtml($expected, $result);
  465. }
  466. /**
  467. * Test creating GET forms.
  468. *
  469. * @return void
  470. */
  471. public function testCreateGet()
  472. {
  473. $encoding = strtolower(Configure::read('App.encoding'));
  474. $result = $this->Form->create(false, ['type' => 'get']);
  475. $expected = ['form' => [
  476. 'method' => 'get', 'action' => '/articles/add',
  477. 'accept-charset' => $encoding
  478. ]];
  479. $this->assertHtml($expected, $result);
  480. }
  481. /**
  482. * Test explicit method/enctype options.
  483. *
  484. * Explicit method overwrites inferred method from 'type'
  485. *
  486. * @return void
  487. */
  488. public function testCreateExplicitMethodEnctype()
  489. {
  490. $encoding = strtolower(Configure::read('App.encoding'));
  491. $result = $this->Form->create(false, [
  492. 'type' => 'get',
  493. 'method' => 'put',
  494. 'enctype' => 'multipart/form-data'
  495. ]);
  496. $expected = ['form' => [
  497. 'method' => 'put',
  498. 'action' => '/articles/add',
  499. 'enctype' => 'multipart/form-data',
  500. 'accept-charset' => $encoding
  501. ]];
  502. $this->assertHtml($expected, $result);
  503. }
  504. /**
  505. * Test create() with the templates option.
  506. *
  507. * @return void
  508. */
  509. public function testCreateTemplatesArray()
  510. {
  511. $result = $this->Form->create($this->article, [
  512. 'templates' => [
  513. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  514. ]
  515. ]);
  516. $expected = [
  517. 'form' => [
  518. 'class' => 'form-horizontal',
  519. 'method' => 'post',
  520. 'action' => '/articles/add',
  521. 'accept-charset' => 'utf-8'
  522. ]
  523. ];
  524. $this->assertHtml($expected, $result);
  525. }
  526. /**
  527. * Test create() with the templates option.
  528. *
  529. * @return void
  530. */
  531. public function testCreateTemplatesFile()
  532. {
  533. $result = $this->Form->create($this->article, [
  534. 'templates' => 'htmlhelper_tags',
  535. ]);
  536. $expected = [
  537. 'start form',
  538. 'div' => ['class' => 'hidden'],
  539. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  540. '/div'
  541. ];
  542. $this->assertHtml($expected, $result);
  543. }
  544. /**
  545. * Test that create() and end() restore templates.
  546. *
  547. * @return void
  548. */
  549. public function testCreateEndRestoreTemplates()
  550. {
  551. $this->Form->create($this->article, [
  552. 'templates' => ['input' => 'custom input element']
  553. ]);
  554. $this->Form->end();
  555. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  556. }
  557. /**
  558. * Test using template vars in various templates used by control() method.
  559. *
  560. * @return void
  561. */
  562. public function testControlTemplateVars()
  563. {
  564. $result = $this->Form->control('text', [
  565. 'templates' => [
  566. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  567. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  568. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  569. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  570. ],
  571. 'templateVars' => [
  572. 'forinput' => 'in-input',
  573. 'forlabel' => 'in-label',
  574. 'forgroup' => 'in-group',
  575. 'forcontainer' => 'in-container'
  576. ]
  577. ]);
  578. $expected = [
  579. 'div' => ['class'],
  580. 'label' => ['for'],
  581. 'Text in-label',
  582. '/label',
  583. 'in-group',
  584. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  585. 'in-container',
  586. '/div',
  587. ];
  588. $this->assertHtml($expected, $result);
  589. }
  590. /**
  591. * Test ensuring template variables work in template files loaded
  592. * during control().
  593. *
  594. * @return void
  595. */
  596. public function testControlTemplatesFromFile()
  597. {
  598. $result = $this->Form->control('title', [
  599. 'templates' => 'test_templates',
  600. 'templateVars' => [
  601. 'forcontainer' => 'container-data'
  602. ]
  603. ]);
  604. $expected = [
  605. 'div' => ['class'],
  606. 'label' => ['for'],
  607. 'Title',
  608. '/label',
  609. 'input' => ['name', 'type' => 'text', 'id'],
  610. 'container-data',
  611. '/div',
  612. ];
  613. $this->assertHtml($expected, $result);
  614. }
  615. /**
  616. * Test using template vars in inputSubmit and submitContainer template.
  617. *
  618. * @return void
  619. */
  620. public function testSubmitTemplateVars()
  621. {
  622. $this->Form->setTemplates([
  623. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  624. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  625. ]);
  626. $result = $this->Form->submit('Submit', [
  627. 'templateVars' => [
  628. 'forinput' => 'in-input',
  629. 'forcontainer' => 'in-container'
  630. ]
  631. ]);
  632. $expected = [
  633. 'div' => ['class'],
  634. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  635. 'in-container',
  636. '/div',
  637. ];
  638. $this->assertHtml($expected, $result);
  639. }
  640. /**
  641. * test the create() method
  642. *
  643. * @dataProvider requestTypeProvider
  644. * @return void
  645. */
  646. public function testCreateTypeOptions($type, $method, $override)
  647. {
  648. $encoding = strtolower(Configure::read('App.encoding'));
  649. $result = $this->Form->create(false, ['type' => $type]);
  650. $expected = [
  651. 'form' => [
  652. 'method' => $method, 'action' => '/articles/add',
  653. 'accept-charset' => $encoding
  654. ],
  655. 'div' => ['style' => 'display:none;'],
  656. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  657. '/div'
  658. ];
  659. $this->assertHtml($expected, $result);
  660. }
  661. /**
  662. * Test using template vars in Create (formStart template)
  663. *
  664. * @return void
  665. */
  666. public function testCreateTemplateVars()
  667. {
  668. $result = $this->Form->create($this->article, [
  669. 'templates' => [
  670. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  671. ],
  672. 'templateVars' => ['header' => 'headertext']
  673. ]);
  674. $expected = [
  675. 'h4' => ['class'],
  676. 'headertext',
  677. '/h4',
  678. 'form' => [
  679. 'method' => 'post',
  680. 'action' => '/articles/add',
  681. 'accept-charset' => 'utf-8'
  682. ]
  683. ];
  684. $this->assertHtml($expected, $result);
  685. }
  686. /**
  687. * Test opening a form for an update operation.
  688. *
  689. * @return void
  690. */
  691. public function testCreateUpdateForm()
  692. {
  693. $encoding = strtolower(Configure::read('App.encoding'));
  694. $this->View->setRequest($this->View->getRequest()
  695. ->withRequestTarget('/articles/edit/1')
  696. ->withParam('action', 'edit'));
  697. $this->article['defaults']['id'] = 1;
  698. $result = $this->Form->create($this->article);
  699. $expected = [
  700. 'form' => [
  701. 'method' => 'post',
  702. 'action' => '/articles/edit/1',
  703. 'accept-charset' => $encoding
  704. ],
  705. 'div' => ['style' => 'display:none;'],
  706. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  707. '/div'
  708. ];
  709. $this->assertHtml($expected, $result);
  710. }
  711. /**
  712. * test create() with automatic url generation
  713. *
  714. * @return void
  715. */
  716. public function testCreateAutoUrl()
  717. {
  718. $encoding = strtolower(Configure::read('App.encoding'));
  719. $this->View->setRequest($this->View->getRequest()
  720. ->withRequestTarget('/articles/delete/10')
  721. ->withParam('action', 'delete'));
  722. $result = $this->Form->create($this->article);
  723. $expected = [
  724. 'form' => [
  725. 'method' => 'post', 'action' => '/articles/delete/10',
  726. 'accept-charset' => $encoding
  727. ],
  728. 'div' => ['style' => 'display:none;'],
  729. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  730. '/div'
  731. ];
  732. $this->assertHtml($expected, $result);
  733. $this->article['defaults'] = ['id' => 1];
  734. $this->View->setRequest($this->View->getRequest()
  735. ->withRequestTarget('/articles/edit/1')
  736. ->withParam('action', 'delete'));
  737. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  738. $expected = [
  739. 'form' => [
  740. 'method' => 'post',
  741. 'action' => '/articles/edit/1',
  742. 'accept-charset' => $encoding
  743. ],
  744. 'div' => ['style' => 'display:none;'],
  745. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  746. '/div'
  747. ];
  748. $this->assertHtml($expected, $result);
  749. $this->View->setRequest($this->View->getRequest()
  750. ->withParam('action', 'add'));
  751. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  752. $expected = [
  753. 'form' => [
  754. 'method' => 'post',
  755. 'action' => '/articles/publish/1',
  756. 'accept-charset' => $encoding
  757. ],
  758. 'div' => ['style' => 'display:none;'],
  759. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  760. '/div'
  761. ];
  762. $this->assertHtml($expected, $result);
  763. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  764. $expected = [
  765. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  766. 'div' => ['style' => 'display:none;'],
  767. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  768. '/div'
  769. ];
  770. $this->assertHtml($expected, $result);
  771. $this->View->setRequest($this->View->getRequest()
  772. ->withParam('controller', 'Pages'));
  773. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  774. $expected = [
  775. 'form' => [
  776. 'method' => 'post', 'action' => '/Pages/signup/1',
  777. 'accept-charset' => $encoding
  778. ],
  779. 'div' => ['style' => 'display:none;'],
  780. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  781. '/div'
  782. ];
  783. $this->assertHtml($expected, $result);
  784. }
  785. /**
  786. * Test create() with no URL (no "action" attribute for <form> tag)
  787. *
  788. * @return void
  789. */
  790. public function testCreateNoUrl()
  791. {
  792. $result = $this->Form->create(false, ['url' => false]);
  793. $expected = [
  794. 'form' => [
  795. 'method' => 'post',
  796. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  797. ],
  798. 'div' => ['style' => 'display:none;'],
  799. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  800. '/div'
  801. ];
  802. $this->assertHtml($expected, $result);
  803. }
  804. /**
  805. * test create() with a custom route
  806. *
  807. * @return void
  808. */
  809. public function testCreateCustomRoute()
  810. {
  811. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  812. $encoding = strtolower(Configure::read('App.encoding'));
  813. $this->View->setRequest($this->View->getRequest()
  814. ->withParam('controller', 'users'));
  815. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  816. $expected = [
  817. 'form' => [
  818. 'method' => 'post', 'action' => '/login',
  819. 'accept-charset' => $encoding
  820. ],
  821. 'div' => ['style' => 'display:none;'],
  822. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  823. '/div'
  824. ];
  825. $this->assertHtml($expected, $result);
  826. Router::connect(
  827. '/new-article',
  828. ['controller' => 'articles', 'action' => 'myaction'],
  829. ['_name' => 'my-route']
  830. );
  831. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  832. $expected = [
  833. 'form' => [
  834. 'method' => 'post', 'action' => '/new-article',
  835. 'accept-charset' => $encoding,
  836. ],
  837. 'div' => ['style' => 'display:none;'],
  838. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  839. '/div'
  840. ];
  841. $this->assertHtml($expected, $result);
  842. }
  843. /**
  844. * test automatic accept-charset overriding
  845. *
  846. * @return void
  847. */
  848. public function testCreateWithAcceptCharset()
  849. {
  850. $result = $this->Form->create(
  851. $this->article,
  852. [
  853. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  854. ]
  855. );
  856. $expected = [
  857. 'form' => [
  858. 'method' => 'post', 'action' => '/articles',
  859. 'accept-charset' => 'iso-8859-1'
  860. ],
  861. 'div' => ['style' => 'display:none;'],
  862. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  863. '/div'
  864. ];
  865. $this->assertHtml($expected, $result);
  866. }
  867. /**
  868. * Test base form URL when url param is passed with multiple parameters (&)
  869. *
  870. */
  871. public function testCreateQueryStringRequest()
  872. {
  873. $encoding = strtolower(Configure::read('App.encoding'));
  874. $result = $this->Form->create($this->article, [
  875. 'type' => 'post',
  876. 'escape' => false,
  877. 'url' => [
  878. 'controller' => 'controller',
  879. 'action' => 'action',
  880. '?' => ['param1' => 'value1', 'param2' => 'value2']
  881. ]
  882. ]);
  883. $expected = [
  884. 'form' => [
  885. 'method' => 'post',
  886. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  887. 'accept-charset' => $encoding
  888. ],
  889. 'div' => ['style' => 'display:none;'],
  890. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  891. '/div'
  892. ];
  893. $this->assertHtml($expected, $result);
  894. $result = $this->Form->create($this->article, [
  895. 'type' => 'post',
  896. 'url' => [
  897. 'controller' => 'controller',
  898. 'action' => 'action',
  899. '?' => ['param1' => 'value1', 'param2' => 'value2']
  900. ]
  901. ]);
  902. $this->assertHtml($expected, $result);
  903. }
  904. /**
  905. * test that create() doesn't cause errors by multiple id's being in the primary key
  906. * as could happen with multiple select or checkboxes.
  907. *
  908. * @return void
  909. */
  910. public function testCreateWithMultipleIdInData()
  911. {
  912. $encoding = strtolower(Configure::read('App.encoding'));
  913. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  914. $result = $this->Form->create($this->article);
  915. $expected = [
  916. 'form' => [
  917. 'method' => 'post',
  918. 'action' => '/articles/add',
  919. 'accept-charset' => $encoding
  920. ],
  921. 'div' => ['style' => 'display:none;'],
  922. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  923. '/div'
  924. ];
  925. $this->assertHtml($expected, $result);
  926. }
  927. /**
  928. * test that create() doesn't add in extra passed params.
  929. *
  930. * @return void
  931. */
  932. public function testCreatePassedArgs()
  933. {
  934. $encoding = strtolower(Configure::read('App.encoding'));
  935. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  936. $result = $this->Form->create($this->article, [
  937. 'type' => 'post',
  938. 'escape' => false,
  939. 'url' => [
  940. 'action' => 'edit',
  941. 'myparam'
  942. ]
  943. ]);
  944. $expected = [
  945. 'form' => [
  946. 'method' => 'post',
  947. 'action' => '/articles/edit/myparam',
  948. 'accept-charset' => $encoding
  949. ],
  950. 'div' => ['style' => 'display:none;'],
  951. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  952. '/div'
  953. ];
  954. $this->assertHtml($expected, $result);
  955. }
  956. /**
  957. * test creating a get form, and get form inputs.
  958. *
  959. * @return void
  960. */
  961. public function testGetFormCreate()
  962. {
  963. $encoding = strtolower(Configure::read('App.encoding'));
  964. $result = $this->Form->create($this->article, ['type' => 'get']);
  965. $expected = ['form' => [
  966. 'method' => 'get', 'action' => '/articles/add',
  967. 'accept-charset' => $encoding
  968. ]];
  969. $this->assertHtml($expected, $result);
  970. $result = $this->Form->text('title');
  971. $expected = ['input' => [
  972. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  973. ]];
  974. $this->assertHtml($expected, $result);
  975. $result = $this->Form->password('password');
  976. $expected = ['input' => [
  977. 'name' => 'password', 'type' => 'password'
  978. ]];
  979. $this->assertHtml($expected, $result);
  980. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  981. $result = $this->Form->text('user_form');
  982. $expected = ['input' => [
  983. 'name' => 'user_form', 'type' => 'text'
  984. ]];
  985. $this->assertHtml($expected, $result);
  986. }
  987. /**
  988. * test get form, and inputs when the model param is false
  989. *
  990. * @return void
  991. */
  992. public function testGetFormWithFalseModel()
  993. {
  994. $encoding = strtolower(Configure::read('App.encoding'));
  995. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'contact_test'));
  996. $result = $this->Form->create(false, [
  997. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  998. ]);
  999. $expected = ['form' => [
  1000. 'method' => 'get', 'action' => '/contact_test/add',
  1001. 'accept-charset' => $encoding
  1002. ]];
  1003. $this->assertHtml($expected, $result);
  1004. $result = $this->Form->text('reason');
  1005. $expected = [
  1006. 'input' => ['type' => 'text', 'name' => 'reason']
  1007. ];
  1008. $this->assertHtml($expected, $result);
  1009. }
  1010. /**
  1011. * testFormCreateWithSecurity method
  1012. *
  1013. * Test form->create() with security key.
  1014. *
  1015. * @return void
  1016. */
  1017. public function testCreateWithSecurity()
  1018. {
  1019. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1020. $encoding = strtolower(Configure::read('App.encoding'));
  1021. $result = $this->Form->create($this->article, [
  1022. 'url' => '/articles/publish',
  1023. ]);
  1024. $expected = [
  1025. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  1026. 'div' => ['style' => 'display:none;'],
  1027. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1028. ['input' => [
  1029. 'type' => 'hidden',
  1030. 'name' => '_csrfToken',
  1031. 'value' => 'testKey',
  1032. 'autocomplete' => 'off',
  1033. ]],
  1034. '/div'
  1035. ];
  1036. $this->assertHtml($expected, $result);
  1037. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1038. $expected['form']['id'] = 'MyForm';
  1039. $this->assertHtml($expected, $result);
  1040. }
  1041. /**
  1042. * testFormCreateGetNoSecurity method
  1043. *
  1044. * Test form->create() with no security key as its a get form
  1045. *
  1046. * @return void
  1047. */
  1048. public function testCreateEndGetNoSecurity()
  1049. {
  1050. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1051. $article = new Article();
  1052. $result = $this->Form->create($article, [
  1053. 'type' => 'get',
  1054. 'url' => '/contacts/add'
  1055. ]);
  1056. $this->assertNotContains('testKey', $result);
  1057. $result = $this->Form->end();
  1058. $this->assertNotContains('testKey', $result);
  1059. }
  1060. /**
  1061. * test that create() clears the fields property so it starts fresh
  1062. *
  1063. * @return void
  1064. */
  1065. public function testCreateClearingFields()
  1066. {
  1067. $this->Form->fields = ['model_id'];
  1068. $this->Form->create($this->article);
  1069. $this->assertEquals([], $this->Form->fields);
  1070. }
  1071. /**
  1072. * Tests form hash generation with model-less data
  1073. *
  1074. * @return void
  1075. */
  1076. public function testValidateHashNoModel()
  1077. {
  1078. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1079. $fields = ['anything'];
  1080. $result = $this->Form->secure($fields);
  1081. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1082. $this->assertContains($hash, $result);
  1083. }
  1084. /**
  1085. * Tests that hidden fields generated for checkboxes don't get locked
  1086. *
  1087. * @return void
  1088. */
  1089. public function testNoCheckboxLocking()
  1090. {
  1091. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1092. $this->assertSame([], $this->Form->fields);
  1093. $this->Form->checkbox('check', ['value' => '1']);
  1094. $this->assertSame($this->Form->fields, ['check']);
  1095. }
  1096. /**
  1097. * testFormSecurityFields method
  1098. *
  1099. * Test generation of secure form hash generation.
  1100. *
  1101. * @return void
  1102. */
  1103. public function testFormSecurityFields()
  1104. {
  1105. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1106. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1107. $result = $this->Form->secure($fields);
  1108. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1109. $hash .= ':' . 'Model.valid';
  1110. $hash = urlencode($hash);
  1111. $tokenDebug = urlencode(json_encode([
  1112. '',
  1113. $fields,
  1114. []
  1115. ]));
  1116. $expected = [
  1117. 'div' => ['style' => 'display:none;'],
  1118. ['input' => [
  1119. 'type' => 'hidden',
  1120. 'name' => '_Token[fields]',
  1121. 'value' => $hash,
  1122. 'autocomplete' => 'off',
  1123. ]],
  1124. ['input' => [
  1125. 'type' => 'hidden',
  1126. 'name' => '_Token[unlocked]',
  1127. 'value' => '',
  1128. 'autocomplete' => 'off',
  1129. ]],
  1130. ['input' => [
  1131. 'type' => 'hidden',
  1132. 'name' => '_Token[debug]',
  1133. 'value' => $tokenDebug,
  1134. 'autocomplete' => 'off',
  1135. ]],
  1136. '/div'
  1137. ];
  1138. $this->assertHtml($expected, $result);
  1139. }
  1140. /**
  1141. * testFormSecurityFields method
  1142. *
  1143. * Test debug token is not generated if debug is false
  1144. *
  1145. * @return void
  1146. */
  1147. public function testFormSecurityFieldsNoDebugMode()
  1148. {
  1149. Configure::write('debug', false);
  1150. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1151. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1152. $result = $this->Form->secure($fields);
  1153. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1154. $hash .= ':' . 'Model.valid';
  1155. $hash = urlencode($hash);
  1156. $expected = [
  1157. 'div' => ['style' => 'display:none;'],
  1158. ['input' => [
  1159. 'type' => 'hidden',
  1160. 'name' => '_Token[fields]',
  1161. 'autocomplete' => 'off',
  1162. 'value' => $hash
  1163. ]],
  1164. ['input' => [
  1165. 'type' => 'hidden',
  1166. 'name' => '_Token[unlocked]',
  1167. 'autocomplete' => 'off',
  1168. 'value' => '',
  1169. ]],
  1170. '/div'
  1171. ];
  1172. $this->assertHtml($expected, $result);
  1173. }
  1174. /**
  1175. * Tests correct generation of number fields for smallint
  1176. *
  1177. * @return void
  1178. */
  1179. public function testTextFieldGenerationForSmallint()
  1180. {
  1181. $this->article['schema'] = [
  1182. 'foo' => [
  1183. 'type' => 'smallinteger',
  1184. 'null' => false,
  1185. 'default' => null,
  1186. 'length' => 10
  1187. ]
  1188. ];
  1189. $this->Form->create($this->article);
  1190. $result = $this->Form->control('foo');
  1191. $this->assertContains('class="input number"', $result);
  1192. $this->assertContains('type="number"', $result);
  1193. }
  1194. /**
  1195. * Tests correct generation of number fields for tinyint
  1196. *
  1197. * @return void
  1198. */
  1199. public function testTextFieldGenerationForTinyint()
  1200. {
  1201. $this->article['schema'] = [
  1202. 'foo' => [
  1203. 'type' => 'tinyinteger',
  1204. 'null' => false,
  1205. 'default' => null,
  1206. 'length' => 10
  1207. ]
  1208. ];
  1209. $this->Form->create($this->article);
  1210. $result = $this->Form->control('foo');
  1211. $this->assertContains('class="input number"', $result);
  1212. $this->assertContains('type="number"', $result);
  1213. }
  1214. /**
  1215. * Tests correct generation of number fields for double and float fields
  1216. *
  1217. * @return void
  1218. */
  1219. public function testTextFieldGenerationForFloats()
  1220. {
  1221. $this->article['schema'] = [
  1222. 'foo' => [
  1223. 'type' => 'float',
  1224. 'null' => false,
  1225. 'default' => null,
  1226. 'length' => 10
  1227. ]
  1228. ];
  1229. $this->Form->create($this->article);
  1230. $result = $this->Form->control('foo');
  1231. $expected = [
  1232. 'div' => ['class' => 'input number'],
  1233. 'label' => ['for' => 'foo'],
  1234. 'Foo',
  1235. '/label',
  1236. ['input' => [
  1237. 'type' => 'number',
  1238. 'name' => 'foo',
  1239. 'id' => 'foo',
  1240. 'step' => 'any'
  1241. ]],
  1242. '/div'
  1243. ];
  1244. $this->assertHtml($expected, $result);
  1245. $result = $this->Form->control('foo', ['step' => 0.5]);
  1246. $expected = [
  1247. 'div' => ['class' => 'input number'],
  1248. 'label' => ['for' => 'foo'],
  1249. 'Foo',
  1250. '/label',
  1251. ['input' => [
  1252. 'type' => 'number',
  1253. 'name' => 'foo',
  1254. 'id' => 'foo',
  1255. 'step' => '0.5'
  1256. ]],
  1257. '/div'
  1258. ];
  1259. $this->assertHtml($expected, $result);
  1260. }
  1261. /**
  1262. * Tests correct generation of number fields for integer fields
  1263. *
  1264. * @return void
  1265. */
  1266. public function testTextFieldTypeNumberGenerationForIntegers()
  1267. {
  1268. $this->getTableLocator()->get('Contacts', [
  1269. 'className' => __NAMESPACE__ . '\ContactsTable'
  1270. ]);
  1271. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1272. $result = $this->Form->control('age');
  1273. $expected = [
  1274. 'div' => ['class' => 'input number'],
  1275. 'label' => ['for' => 'age'],
  1276. 'Age',
  1277. '/label',
  1278. ['input' => [
  1279. 'type' => 'number', 'name' => 'age',
  1280. 'id' => 'age'
  1281. ]],
  1282. '/div'
  1283. ];
  1284. $this->assertHtml($expected, $result);
  1285. }
  1286. /**
  1287. * Tests correct generation of file upload fields for binary fields
  1288. *
  1289. * @return void
  1290. */
  1291. public function testFileUploadFieldTypeGenerationForBinaries()
  1292. {
  1293. $table = $this->getTableLocator()->get('Contacts', [
  1294. 'className' => __NAMESPACE__ . '\ContactsTable'
  1295. ]);
  1296. $table->setSchema(['foo' => [
  1297. 'type' => 'binary',
  1298. 'null' => false,
  1299. 'default' => null,
  1300. 'length' => 1024
  1301. ]]);
  1302. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1303. $result = $this->Form->control('foo');
  1304. $expected = [
  1305. 'div' => ['class' => 'input file'],
  1306. 'label' => ['for' => 'foo'],
  1307. 'Foo',
  1308. '/label',
  1309. ['input' => [
  1310. 'type' => 'file', 'name' => 'foo',
  1311. 'id' => 'foo'
  1312. ]],
  1313. '/div'
  1314. ];
  1315. $this->assertHtml($expected, $result);
  1316. }
  1317. /**
  1318. * testFormSecurityMultipleFields method
  1319. *
  1320. * Test secure() with multiple row form. Ensure hash is correct.
  1321. *
  1322. * @return void
  1323. */
  1324. public function testFormSecurityMultipleFields()
  1325. {
  1326. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1327. $fields = [
  1328. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1329. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1330. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1331. ];
  1332. $result = $this->Form->secure($fields);
  1333. $hash = '8670192c3f040bf58680479060b4755b7a5c3596' .
  1334. '%3AModel.0.hidden%7CModel.0.valid%7CModel.1.hidden%7CModel.1.valid';
  1335. $tokenDebug = urlencode(json_encode([
  1336. '',
  1337. $fields,
  1338. []
  1339. ]));
  1340. $expected = [
  1341. 'div' => ['style' => 'display:none;'],
  1342. ['input' => [
  1343. 'type' => 'hidden',
  1344. 'name' => '_Token[fields]',
  1345. 'value' => $hash,
  1346. 'autocomplete' => 'off',
  1347. ]],
  1348. ['input' => [
  1349. 'type' => 'hidden',
  1350. 'name' => '_Token[unlocked]',
  1351. 'autocomplete' => 'off',
  1352. 'value' => ''
  1353. ]],
  1354. ['input' => [
  1355. 'type' => 'hidden',
  1356. 'name' => '_Token[debug]',
  1357. 'value' => $tokenDebug,
  1358. 'autocomplete' => 'off',
  1359. ]],
  1360. '/div'
  1361. ];
  1362. $this->assertHtml($expected, $result);
  1363. }
  1364. /**
  1365. * testFormSecurityMultipleSubmitButtons
  1366. *
  1367. * test form submit generation and ensure that _Token is only created on end()
  1368. *
  1369. * @return void
  1370. */
  1371. public function testFormSecurityMultipleSubmitButtons()
  1372. {
  1373. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1374. $this->Form->create($this->article);
  1375. $this->Form->text('Address.title');
  1376. $this->Form->text('Address.first_name');
  1377. $result = $this->Form->submit('Save', ['name' => 'save']);
  1378. $expected = [
  1379. 'div' => ['class' => 'submit'],
  1380. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1381. '/div',
  1382. ];
  1383. $this->assertHtml($expected, $result);
  1384. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1385. $expected = [
  1386. 'div' => ['class' => 'submit'],
  1387. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1388. '/div',
  1389. ];
  1390. $this->assertHtml($expected, $result);
  1391. $result = $this->Form->end();
  1392. $tokenDebug = urlencode(json_encode([
  1393. '/articles/add',
  1394. [
  1395. 'Address.title',
  1396. 'Address.first_name',
  1397. ],
  1398. ['save', 'cancel']
  1399. ]));
  1400. $expected = [
  1401. 'div' => ['style' => 'display:none;'],
  1402. ['input' => [
  1403. 'type' => 'hidden',
  1404. 'name' => '_Token[fields]',
  1405. 'autocomplete',
  1406. 'value'
  1407. ]],
  1408. ['input' => [
  1409. 'type' => 'hidden',
  1410. 'name' => '_Token[unlocked]',
  1411. 'value' => 'cancel%7Csave',
  1412. 'autocomplete' => 'off',
  1413. ]],
  1414. ['input' => [
  1415. 'type' => 'hidden',
  1416. 'name' => '_Token[debug]',
  1417. 'value' => $tokenDebug,
  1418. 'autocomplete' => 'off',
  1419. ]],
  1420. '/div'
  1421. ];
  1422. $this->assertHtml($expected, $result);
  1423. }
  1424. /**
  1425. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1426. *
  1427. * @return void
  1428. */
  1429. public function testSecurityButtonNestedNamed()
  1430. {
  1431. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1432. $this->Form->create('Addresses');
  1433. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1434. $result = $this->Form->unlockField();
  1435. $this->assertEquals(['Address.button'], $result);
  1436. }
  1437. /**
  1438. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1439. *
  1440. * @return void
  1441. */
  1442. public function testSecuritySubmitNestedNamed()
  1443. {
  1444. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1445. $this->Form->create($this->article);
  1446. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1447. $result = $this->Form->unlockField();
  1448. $this->assertEquals(['Address.button'], $result);
  1449. }
  1450. /**
  1451. * Test that the correct fields are unlocked for image submits with no names.
  1452. *
  1453. * @return void
  1454. */
  1455. public function testSecuritySubmitImageNoName()
  1456. {
  1457. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1458. $this->Form->create(false);
  1459. $result = $this->Form->submit('save.png');
  1460. $expected = [
  1461. 'div' => ['class' => 'submit'],
  1462. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1463. '/div'
  1464. ];
  1465. $this->assertHtml($expected, $result);
  1466. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1467. }
  1468. /**
  1469. * Test that the correct fields are unlocked for image submits with names.
  1470. *
  1471. * @return void
  1472. */
  1473. public function testSecuritySubmitImageName()
  1474. {
  1475. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1476. $this->Form->create(null);
  1477. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1478. $expected = [
  1479. 'div' => ['class' => 'submit'],
  1480. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1481. '/div'
  1482. ];
  1483. $this->assertHtml($expected, $result);
  1484. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1485. }
  1486. /**
  1487. * testFormSecurityMultipleControlFields method
  1488. *
  1489. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1490. *
  1491. * @return void
  1492. */
  1493. public function testFormSecurityMultipleControlFields()
  1494. {
  1495. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1496. $this->Form->create();
  1497. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1498. $this->Form->control('Addresses.0.title');
  1499. $this->Form->control('Addresses.0.first_name');
  1500. $this->Form->control('Addresses.0.last_name');
  1501. $this->Form->control('Addresses.0.address');
  1502. $this->Form->control('Addresses.0.city');
  1503. $this->Form->control('Addresses.0.phone');
  1504. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1505. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1506. $this->Form->control('Addresses.1.title');
  1507. $this->Form->control('Addresses.1.first_name');
  1508. $this->Form->control('Addresses.1.last_name');
  1509. $this->Form->control('Addresses.1.address');
  1510. $this->Form->control('Addresses.1.city');
  1511. $this->Form->control('Addresses.1.phone');
  1512. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1513. $result = $this->Form->secure($this->Form->fields);
  1514. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1515. $tokenDebug = urlencode(json_encode([
  1516. '/articles/add',
  1517. [
  1518. 'Addresses.0.id' => '123456',
  1519. 'Addresses.0.title',
  1520. 'Addresses.0.first_name',
  1521. 'Addresses.0.last_name',
  1522. 'Addresses.0.address',
  1523. 'Addresses.0.city',
  1524. 'Addresses.0.phone',
  1525. 'Addresses.0.primary',
  1526. 'Addresses.1.id' => '654321',
  1527. 'Addresses.1.title',
  1528. 'Addresses.1.first_name',
  1529. 'Addresses.1.last_name',
  1530. 'Addresses.1.address',
  1531. 'Addresses.1.city',
  1532. 'Addresses.1.phone',
  1533. 'Addresses.1.primary',
  1534. ],
  1535. []
  1536. ]));
  1537. $expected = [
  1538. 'div' => ['style' => 'display:none;'],
  1539. ['input' => [
  1540. 'type' => 'hidden',
  1541. 'name' => '_Token[fields]',
  1542. 'value' => $hash,
  1543. 'autocomplete' => 'off',
  1544. ]],
  1545. ['input' => [
  1546. 'type' => 'hidden',
  1547. 'name' => '_Token[unlocked]',
  1548. 'autocomplete' => 'off',
  1549. 'value' => ''
  1550. ]],
  1551. ['input' => [
  1552. 'type' => 'hidden',
  1553. 'name' => '_Token[debug]',
  1554. 'value' => $tokenDebug,
  1555. 'autocomplete' => 'off',
  1556. ]],
  1557. '/div'
  1558. ];
  1559. $this->assertHtml($expected, $result);
  1560. }
  1561. /**
  1562. * testFormSecurityArrayFields method
  1563. *
  1564. * Test form security with Model.field.0 style inputs.
  1565. *
  1566. * @return void
  1567. */
  1568. public function testFormSecurityArrayFields()
  1569. {
  1570. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1571. $this->Form->create();
  1572. $this->Form->text('Address.primary.1');
  1573. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1574. $this->Form->text('Address.secondary.1.0');
  1575. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1576. }
  1577. /**
  1578. * testFormSecurityMultipleControlDisabledFields method
  1579. *
  1580. * Test secure form generation with multiple records and disabled fields.
  1581. *
  1582. * @return void
  1583. */
  1584. public function testFormSecurityMultipleControlDisabledFields()
  1585. {
  1586. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1587. 'unlockedFields' => ['first_name', 'address']
  1588. ]));
  1589. $this->Form->create();
  1590. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1591. $this->Form->text('Addresses.0.title');
  1592. $this->Form->text('Addresses.0.first_name');
  1593. $this->Form->text('Addresses.0.last_name');
  1594. $this->Form->text('Addresses.0.address');
  1595. $this->Form->text('Addresses.0.city');
  1596. $this->Form->text('Addresses.0.phone');
  1597. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1598. $this->Form->text('Addresses.1.title');
  1599. $this->Form->text('Addresses.1.first_name');
  1600. $this->Form->text('Addresses.1.last_name');
  1601. $this->Form->text('Addresses.1.address');
  1602. $this->Form->text('Addresses.1.city');
  1603. $this->Form->text('Addresses.1.phone');
  1604. $result = $this->Form->secure($this->Form->fields);
  1605. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1606. $tokenDebug = urlencode(json_encode([
  1607. '/articles/add',
  1608. [
  1609. 'Addresses.0.id' => '123456',
  1610. 'Addresses.0.title',
  1611. 'Addresses.0.last_name',
  1612. 'Addresses.0.city',
  1613. 'Addresses.0.phone',
  1614. 'Addresses.1.id' => '654321',
  1615. 'Addresses.1.title',
  1616. 'Addresses.1.last_name',
  1617. 'Addresses.1.city',
  1618. 'Addresses.1.phone'
  1619. ],
  1620. [
  1621. 'first_name',
  1622. 'address'
  1623. ]
  1624. ]));
  1625. $expected = [
  1626. 'div' => ['style' => 'display:none;'],
  1627. ['input' => [
  1628. 'type' => 'hidden',
  1629. 'name' => '_Token[fields]',
  1630. 'autocomplete' => 'off',
  1631. 'value' => $hash
  1632. ]],
  1633. ['input' => [
  1634. 'type' => 'hidden',
  1635. 'name' => '_Token[unlocked]',
  1636. 'autocomplete' => 'off',
  1637. 'value' => 'address%7Cfirst_name',
  1638. ]],
  1639. ['input' => [
  1640. 'type' => 'hidden',
  1641. 'name' => '_Token[debug]',
  1642. 'autocomplete' => 'off',
  1643. 'value' => $tokenDebug,
  1644. ]],
  1645. '/div'
  1646. ];
  1647. $this->assertHtml($expected, $result);
  1648. }
  1649. /**
  1650. * testFormSecurityControlDisabledFields method
  1651. *
  1652. * Test single record form with disabled fields.
  1653. *
  1654. * @return void
  1655. */
  1656. public function testFormSecurityControlUnlockedFields()
  1657. {
  1658. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1659. 'unlockedFields' => ['first_name', 'address']
  1660. ]));
  1661. $this->Form->create();
  1662. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1663. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1664. $this->Form->text('Addresses.title');
  1665. $this->Form->text('Addresses.first_name');
  1666. $this->Form->text('Addresses.last_name');
  1667. $this->Form->text('Addresses.address');
  1668. $this->Form->text('Addresses.city');
  1669. $this->Form->text('Addresses.phone');
  1670. $result = $this->Form->fields;
  1671. $expected = [
  1672. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1673. 'Addresses.city', 'Addresses.phone'
  1674. ];
  1675. $this->assertEquals($expected, $result);
  1676. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1677. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1678. $tokenDebug = urlencode(json_encode([
  1679. '/articles/add',
  1680. [
  1681. 'Addresses.id' => '123456',
  1682. 'Addresses.title',
  1683. 'Addresses.last_name',
  1684. 'Addresses.city',
  1685. 'Addresses.phone'
  1686. ],
  1687. [
  1688. 'first_name',
  1689. 'address'
  1690. ]
  1691. ]));
  1692. $expected = [
  1693. 'div' => ['style' => 'display:none;'],
  1694. ['input' => [
  1695. 'type' => 'hidden',
  1696. 'name' => '_Token[fields]',
  1697. 'value' => $hash,
  1698. 'autocomplete' => 'off',
  1699. 'data-foo' => 'bar',
  1700. ]],
  1701. ['input' => [
  1702. 'type' => 'hidden',
  1703. 'name' => '_Token[unlocked]',
  1704. 'value' => 'address%7Cfirst_name',
  1705. 'autocomplete' => 'off',
  1706. 'data-foo' => 'bar',
  1707. ]],
  1708. ['input' => [
  1709. 'type' => 'hidden', 'name' => '_Token[debug]',
  1710. 'value' => $tokenDebug,
  1711. 'autocomplete' => 'off',
  1712. 'data-foo' => 'bar'
  1713. ]],
  1714. '/div'
  1715. ];
  1716. $this->assertHtml($expected, $result);
  1717. }
  1718. /**
  1719. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1720. *
  1721. * Test single record form with debugSecurity param.
  1722. *
  1723. * @return void
  1724. */
  1725. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1726. {
  1727. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1728. 'unlockedFields' => ['first_name', 'address']
  1729. ]));
  1730. $this->Form->create();
  1731. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1732. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1733. $this->Form->text('Addresses.title');
  1734. $this->Form->text('Addresses.first_name');
  1735. $this->Form->text('Addresses.last_name');
  1736. $this->Form->text('Addresses.address');
  1737. $this->Form->text('Addresses.city');
  1738. $this->Form->text('Addresses.phone');
  1739. $result = $this->Form->fields;
  1740. $expected = [
  1741. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1742. 'Addresses.city', 'Addresses.phone'
  1743. ];
  1744. $this->assertEquals($expected, $result);
  1745. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1746. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1747. $tokenDebug = urlencode(json_encode([
  1748. '/articles/add',
  1749. [
  1750. 'Addresses.id' => '123456',
  1751. 'Addresses.title',
  1752. 'Addresses.last_name',
  1753. 'Addresses.city',
  1754. 'Addresses.phone'
  1755. ],
  1756. [
  1757. 'first_name',
  1758. 'address'
  1759. ]
  1760. ]));
  1761. $expected = [
  1762. 'div' => ['style' => 'display:none;'],
  1763. ['input' => [
  1764. 'type' => 'hidden',
  1765. 'name' => '_Token[fields]',
  1766. 'value' => $hash,
  1767. 'autocomplete' => 'off',
  1768. 'data-foo' => 'bar',
  1769. ]],
  1770. ['input' => [
  1771. 'type' => 'hidden',
  1772. 'name' => '_Token[unlocked]',
  1773. 'value' => 'address%7Cfirst_name',
  1774. 'autocomplete' => 'off',
  1775. 'data-foo' => 'bar',
  1776. ]],
  1777. ['input' => [
  1778. 'type' => 'hidden', 'name' => '_Token[debug]',
  1779. 'value' => $tokenDebug,
  1780. 'autocomplete' => 'off',
  1781. 'data-foo' => 'bar'
  1782. ]],
  1783. '/div'
  1784. ];
  1785. $this->assertHtml($expected, $result);
  1786. }
  1787. /**
  1788. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1789. *
  1790. * Debug is false, debugSecurity is true -> no debug
  1791. *
  1792. * @return void
  1793. */
  1794. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1795. {
  1796. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1797. 'unlockedFields' => ['first_name', 'address']
  1798. ]));
  1799. $this->Form->create();
  1800. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1801. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1802. $this->Form->text('Addresses.title');
  1803. $this->Form->text('Addresses.first_name');
  1804. $this->Form->text('Addresses.last_name');
  1805. $this->Form->text('Addresses.address');
  1806. $this->Form->text('Addresses.city');
  1807. $this->Form->text('Addresses.phone');
  1808. $result = $this->Form->fields;
  1809. $expected = [
  1810. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1811. 'Addresses.city', 'Addresses.phone'
  1812. ];
  1813. $this->assertEquals($expected, $result);
  1814. Configure::write('debug', false);
  1815. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1816. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1817. $expected = [
  1818. 'div' => ['style' => 'display:none;'],
  1819. ['input' => [
  1820. 'type' => 'hidden',
  1821. 'name' => '_Token[fields]',
  1822. 'value' => $hash,
  1823. 'autocomplete' => 'off',
  1824. 'data-foo' => 'bar',
  1825. ]],
  1826. ['input' => [
  1827. 'type' => 'hidden',
  1828. 'name' => '_Token[unlocked]',
  1829. 'value' => 'address%7Cfirst_name',
  1830. 'autocomplete' => 'off',
  1831. 'data-foo' => 'bar',
  1832. ]],
  1833. '/div'
  1834. ];
  1835. $this->assertHtml($expected, $result);
  1836. }
  1837. /**
  1838. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1839. *
  1840. * Test single record form with debugSecurity param.
  1841. *
  1842. * @return void
  1843. */
  1844. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1845. {
  1846. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1847. 'unlockedFields' => ['first_name', 'address']
  1848. ]));
  1849. $this->Form->create();
  1850. $this->assertEquals(
  1851. $this->View->getRequest()->getParam('_Token.unlockedFields'),
  1852. $this->Form->unlockField()
  1853. );
  1854. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1855. $this->Form->text('Addresses.title');
  1856. $this->Form->text('Addresses.first_name');
  1857. $this->Form->text('Addresses.last_name');
  1858. $this->Form->text('Addresses.address');
  1859. $this->Form->text('Addresses.city');
  1860. $this->Form->text('Addresses.phone');
  1861. $result = $this->Form->fields;
  1862. $expected = [
  1863. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1864. 'Addresses.city', 'Addresses.phone'
  1865. ];
  1866. $this->assertEquals($expected, $result);
  1867. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1868. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1869. $expected = [
  1870. 'div' => ['style' => 'display:none;'],
  1871. ['input' => [
  1872. 'type' => 'hidden',
  1873. 'name' => '_Token[fields]',
  1874. 'value' => $hash,
  1875. 'autocomplete' => 'off',
  1876. 'data-foo' => 'bar',
  1877. ]],
  1878. ['input' => [
  1879. 'type' => 'hidden',
  1880. 'name' => '_Token[unlocked]',
  1881. 'value' => 'address%7Cfirst_name',
  1882. 'autocomplete' => 'off',
  1883. 'data-foo' => 'bar',
  1884. ]],
  1885. '/div'
  1886. ];
  1887. $this->assertHtml($expected, $result);
  1888. }
  1889. /**
  1890. * testFormSecureWithCustomNameAttribute method
  1891. *
  1892. * Test securing inputs with custom name attributes.
  1893. *
  1894. * @return void
  1895. */
  1896. public function testFormSecureWithCustomNameAttribute()
  1897. {
  1898. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1899. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1900. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1901. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1902. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1903. }
  1904. /**
  1905. * testFormSecuredControl method
  1906. *
  1907. * Test generation of entire secure form, assertions made on control() output.
  1908. *
  1909. * @return void
  1910. */
  1911. public function testFormSecuredControl()
  1912. {
  1913. $this->View->setRequest($this->View->getRequest()
  1914. ->withParam('_Token', 'stuff')
  1915. ->withParam('_csrfToken', 'testKey'));
  1916. $this->article['schema'] = [
  1917. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1918. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1919. ];
  1920. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1921. $encoding = strtolower(Configure::read('App.encoding'));
  1922. $expected = [
  1923. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1924. 'div' => ['style' => 'display:none;'],
  1925. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1926. ['input' => [
  1927. 'type' => 'hidden',
  1928. 'name' => '_csrfToken',
  1929. 'value' => 'testKey',
  1930. 'autocomplete' => 'off'
  1931. ]],
  1932. '/div'
  1933. ];
  1934. $this->assertHtml($expected, $result);
  1935. $result = $this->Form->control('ratio');
  1936. $expected = [
  1937. 'div' => ['class'],
  1938. 'label' => ['for'],
  1939. 'Ratio',
  1940. '/label',
  1941. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1942. '/div',
  1943. ];
  1944. $this->assertHtml($expected, $result);
  1945. $result = $this->Form->control('population');
  1946. $expected = [
  1947. 'div' => ['class'],
  1948. 'label' => ['for'],
  1949. 'Population',
  1950. '/label',
  1951. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1952. '/div',
  1953. ];
  1954. $this->assertHtml($expected, $result);
  1955. $result = $this->Form->control('published', ['type' => 'text']);
  1956. $expected = [
  1957. 'div' => ['class' => 'input text'],
  1958. 'label' => ['for' => 'published'],
  1959. 'Published',
  1960. '/label',
  1961. ['input' => [
  1962. 'type' => 'text',
  1963. 'name' => 'published',
  1964. 'id' => 'published'
  1965. ]],
  1966. '/div'
  1967. ];
  1968. $this->assertHtml($expected, $result);
  1969. $result = $this->Form->control('other', ['type' => 'text']);
  1970. $expected = [
  1971. 'div' => ['class' => 'input text'],
  1972. 'label' => ['for' => 'other'],
  1973. 'Other',
  1974. '/label',
  1975. ['input' => [
  1976. 'type' => 'text',
  1977. 'name' => 'other',
  1978. 'id',
  1979. ]],
  1980. '/div'
  1981. ];
  1982. $this->assertHtml($expected, $result);
  1983. $result = $this->Form->hidden('stuff');
  1984. $expected = [
  1985. 'input' => [
  1986. 'type' => 'hidden',
  1987. 'name' => 'stuff'
  1988. ]
  1989. ];
  1990. $this->assertHtml($expected, $result);
  1991. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1992. $expected = ['input' => [
  1993. 'type' => 'hidden',
  1994. 'name' => 'hidden',
  1995. 'value' => '0'
  1996. ]];
  1997. $this->assertHtml($expected, $result);
  1998. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1999. $expected = [
  2000. 'div' => ['class' => 'input checkbox'],
  2001. ['input' => [
  2002. 'type' => 'hidden',
  2003. 'name' => 'something',
  2004. 'value' => '0'
  2005. ]],
  2006. 'label' => ['for' => 'something'],
  2007. ['input' => [
  2008. 'type' => 'checkbox',
  2009. 'name' => 'something',
  2010. 'value' => '1',
  2011. 'id' => 'something'
  2012. ]],
  2013. 'Something',
  2014. '/label',
  2015. '/div'
  2016. ];
  2017. $this->assertHtml($expected, $result);
  2018. $result = $this->Form->fields;
  2019. $expectedFields = [
  2020. 'ratio',
  2021. 'population',
  2022. 'published',
  2023. 'other',
  2024. 'stuff' => '',
  2025. 'hidden' => '0',
  2026. 'something'
  2027. ];
  2028. $this->assertEquals($expectedFields, $result);
  2029. $result = $this->Form->secure($this->Form->fields);
  2030. $tokenDebug = urlencode(json_encode([
  2031. '/articles/add',
  2032. $expectedFields,
  2033. []
  2034. ]));
  2035. $expected = [
  2036. 'div' => ['style' => 'display:none;'],
  2037. ['input' => [
  2038. 'type' => 'hidden',
  2039. 'name' => '_Token[fields]',
  2040. 'value',
  2041. 'autocomplete'
  2042. ]],
  2043. ['input' => [
  2044. 'type' => 'hidden',
  2045. 'name' => '_Token[unlocked]',
  2046. 'value' => '',
  2047. 'autocomplete' => 'off'
  2048. ]],
  2049. ['input' => [
  2050. 'type' => 'hidden', 'name' => '_Token[debug]',
  2051. 'value' => $tokenDebug,
  2052. 'autocomplete' => 'off'
  2053. ]],
  2054. '/div'
  2055. ];
  2056. $this->assertHtml($expected, $result);
  2057. }
  2058. /**
  2059. * testSecuredControlCustomName method
  2060. *
  2061. * Test secured inputs with custom names.
  2062. *
  2063. * @return void
  2064. */
  2065. public function testSecuredControlCustomName()
  2066. {
  2067. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2068. $this->assertEquals([], $this->Form->fields);
  2069. $this->Form->text('text_input', [
  2070. 'name' => 'Option[General.default_role]',
  2071. ]);
  2072. $expected = ['Option.General.default_role'];
  2073. $this->assertEquals($expected, $this->Form->fields);
  2074. $this->Form->select('select_box', [1, 2], [
  2075. 'name' => 'Option[General.select_role]',
  2076. ]);
  2077. $expected[] = 'Option.General.select_role';
  2078. $this->assertEquals($expected, $this->Form->fields);
  2079. $this->Form->text('other.things[]');
  2080. $expected[] = 'other.things';
  2081. $this->assertEquals($expected, $this->Form->fields);
  2082. }
  2083. /**
  2084. * testSecuredControlDuplicate method
  2085. *
  2086. * Test that a hidden field followed by a visible field
  2087. * undoes the hidden field locking.
  2088. *
  2089. * @return void
  2090. */
  2091. public function testSecuredControlDuplicate()
  2092. {
  2093. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2094. $this->assertEquals([], $this->Form->fields);
  2095. $this->Form->control('text_val', [
  2096. 'type' => 'hidden',
  2097. 'value' => 'some text',
  2098. ]);
  2099. $expected = ['text_val' => 'some text'];
  2100. $this->assertEquals($expected, $this->Form->fields);
  2101. $this->Form->control('text_val', [
  2102. 'type' => 'text',
  2103. ]);
  2104. $expected = ['text_val'];
  2105. $this->assertEquals($expected, $this->Form->fields);
  2106. }
  2107. /**
  2108. * testFormSecuredFileControl method
  2109. *
  2110. * Tests that the correct keys are added to the field hash index.
  2111. *
  2112. * @return void
  2113. */
  2114. public function testFormSecuredFileControl()
  2115. {
  2116. $this->assertEquals([], $this->Form->fields);
  2117. $this->Form->file('Attachment.file');
  2118. $expected = [
  2119. 'Attachment.file.name', 'Attachment.file.type',
  2120. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2121. 'Attachment.file.size'
  2122. ];
  2123. $this->assertEquals($expected, $this->Form->fields);
  2124. }
  2125. /**
  2126. * testFormSecuredMultipleSelect method
  2127. *
  2128. * Test that multiple selects keys are added to field hash.
  2129. *
  2130. * @return void
  2131. */
  2132. public function testFormSecuredMultipleSelect()
  2133. {
  2134. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  2135. $this->assertEquals([], $this->Form->fields);
  2136. $options = ['1' => 'one', '2' => 'two'];
  2137. $this->Form->select('Model.select', $options);
  2138. $expected = ['Model.select'];
  2139. $this->assertEquals($expected, $this->Form->fields);
  2140. $this->Form->fields = [];
  2141. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2142. $this->assertEquals($expected, $this->Form->fields);
  2143. }
  2144. /**
  2145. * testFormSecuredRadio method
  2146. *
  2147. * @return void
  2148. */
  2149. public function testFormSecuredRadio()
  2150. {
  2151. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2152. $this->assertEquals([], $this->Form->fields);
  2153. $options = ['1' => 'option1', '2' => 'option2'];
  2154. $this->Form->radio('Test.test', $options);
  2155. $expected = ['Test.test'];
  2156. $this->assertEquals($expected, $this->Form->fields);
  2157. $this->Form->radio('Test.all', $options, [
  2158. 'disabled' => ['option1', 'option2']
  2159. ]);
  2160. $expected = ['Test.test', 'Test.all' => ''];
  2161. $this->assertEquals($expected, $this->Form->fields);
  2162. $this->Form->radio('Test.some', $options, [
  2163. 'disabled' => ['option1']
  2164. ]);
  2165. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2166. $this->assertEquals($expected, $this->Form->fields);
  2167. }
  2168. /**
  2169. * testFormSecuredAndDisabledNotAssoc method
  2170. *
  2171. * Test that when disabled is in a list based attribute array it works.
  2172. *
  2173. * @return void
  2174. */
  2175. public function testFormSecuredAndDisabledNotAssoc()
  2176. {
  2177. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2178. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2179. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2180. $this->Form->text('Model.text', ['disabled']);
  2181. $this->Form->textarea('Model.textarea', ['disabled']);
  2182. $this->Form->password('Model.password', ['disabled']);
  2183. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2184. $expected = [
  2185. 'Model.radio' => ''
  2186. ];
  2187. $this->assertEquals($expected, $this->Form->fields);
  2188. }
  2189. /**
  2190. * testFormSecuredAndDisabled method
  2191. *
  2192. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2193. * hashing.
  2194. *
  2195. * @return void
  2196. */
  2197. public function testFormSecuredAndDisabled()
  2198. {
  2199. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2200. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2201. $this->Form->text('Model.text', ['disabled' => true]);
  2202. $this->Form->password('Model.text', ['disabled' => true]);
  2203. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2204. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2205. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2206. $this->Form->year('Model.year', ['disabled' => true]);
  2207. $this->Form->month('Model.month', ['disabled' => true]);
  2208. $this->Form->day('Model.day', ['disabled' => true]);
  2209. $this->Form->hour('Model.hour', ['disabled' => true]);
  2210. $this->Form->minute('Model.minute', ['disabled' => true]);
  2211. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2212. $expected = [
  2213. 'Model.radio' => ''
  2214. ];
  2215. $this->assertEquals($expected, $this->Form->fields);
  2216. }
  2217. /**
  2218. * testDisableSecurityUsingForm method
  2219. *
  2220. * @return void
  2221. */
  2222. public function testDisableSecurityUsingForm()
  2223. {
  2224. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2225. 'disabledFields' => []
  2226. ]));
  2227. $this->Form->create();
  2228. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2229. $this->Form->text('Addresses.title');
  2230. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2231. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2232. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2233. $result = $this->Form->fields;
  2234. $expected = [
  2235. 'Addresses.id' => '123456', 'Addresses.title',
  2236. ];
  2237. $this->assertEquals($expected, $result);
  2238. }
  2239. /**
  2240. * testUnlockFieldAddsToList method
  2241. *
  2242. * Test disableField.
  2243. *
  2244. * @return void
  2245. */
  2246. public function testUnlockFieldAddsToList()
  2247. {
  2248. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2249. 'unlockedFields' => []
  2250. ]));
  2251. $this->Form->unlockField('Contact.name');
  2252. $this->Form->text('Contact.name');
  2253. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2254. $this->assertEquals([], $this->Form->fields);
  2255. }
  2256. /**
  2257. * testUnlockFieldRemovingFromFields method
  2258. *
  2259. * Test unlockField removing from fields array.
  2260. *
  2261. * @return void
  2262. */
  2263. public function testUnlockFieldRemovingFromFields()
  2264. {
  2265. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2266. 'unlockedFields' => []
  2267. ]));
  2268. $this->Form->create($this->article);
  2269. $this->Form->hidden('Article.id', ['value' => 1]);
  2270. $this->Form->text('Article.title');
  2271. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2272. $this->assertContains('Article.title', $this->Form->fields, 'Field should be secured.');
  2273. $this->Form->unlockField('Article.title');
  2274. $this->Form->unlockField('Article.id');
  2275. $this->assertEquals([], $this->Form->fields);
  2276. }
  2277. /**
  2278. * testResetUnlockFields method
  2279. *
  2280. * Test reset unlockFields, when create new form.
  2281. *
  2282. * @return void
  2283. */
  2284. public function testResetUnlockFields()
  2285. {
  2286. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2287. 'key' => 'testKey',
  2288. 'unlockedFields' => []
  2289. ]));
  2290. $this->Form->unlockField('Contact.id');
  2291. $this->Form->create('Contact');
  2292. $this->Form->hidden('Contact.id', ['value' => 1]);
  2293. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2294. $this->Form->end();
  2295. $this->Form->create('Contact');
  2296. $this->Form->hidden('Contact.id', ['value' => 1]);
  2297. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2298. }
  2299. /**
  2300. * testSecuredFormUrlIgnoresHost method
  2301. *
  2302. * Test that only the path + query elements of a form's URL show up in their hash.
  2303. *
  2304. * @return void
  2305. */
  2306. public function testSecuredFormUrlIgnoresHost()
  2307. {
  2308. $this->View->setRequest($this->View->getRequest()->withParam('_Token', ['key' => 'testKey']));
  2309. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2310. $this->Form->create($this->article, [
  2311. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2312. ]);
  2313. $result = $this->Form->secure();
  2314. $this->assertContains($expected, $result);
  2315. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2316. $result = $this->Form->secure();
  2317. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2318. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2319. $result = $this->Form->secure();
  2320. $this->assertContains($expected, $result, 'URL path + query should work.');
  2321. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2322. $result = $this->Form->secure();
  2323. $this->assertNotContains($expected, $result, 'URL is different');
  2324. }
  2325. /**
  2326. * testSecuredFormUrlHasHtmlAndIdentifier method
  2327. *
  2328. * Test that URL, HTML and identifier show up in their hashes.
  2329. *
  2330. * @return void
  2331. */
  2332. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2333. {
  2334. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2335. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2336. $this->Form->create($this->article, [
  2337. 'url' => [
  2338. 'controller' => 'articles',
  2339. 'action' => 'view',
  2340. '?' => [
  2341. 'page' => 1,
  2342. 'limit' => 10,
  2343. 'html' => '<>"',
  2344. ],
  2345. '#' => 'result',
  2346. ],
  2347. ]);
  2348. $result = $this->Form->secure();
  2349. $this->assertContains($expected, $result);
  2350. $this->Form->create($this->article, [
  2351. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2352. ]);
  2353. $result = $this->Form->secure();
  2354. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2355. $this->Form->create($this->article, [
  2356. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2357. ]);
  2358. $result = $this->Form->secure();
  2359. $this->assertContains($expected, $result, 'URL path + query should work.');
  2360. }
  2361. /**
  2362. * testErrorMessageDisplay method
  2363. *
  2364. * Test error message display.
  2365. *
  2366. * @return void
  2367. */
  2368. public function testErrorMessageDisplay()
  2369. {
  2370. $this->article['errors'] = [
  2371. 'Article' => [
  2372. 'title' => 'error message',
  2373. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2374. ]
  2375. ];
  2376. $this->Form->create($this->article);
  2377. $result = $this->Form->control('Article.title');
  2378. $expected = [
  2379. 'div' => ['class' => 'input text error'],
  2380. 'label' => ['for' => 'article-title'],
  2381. 'Title',
  2382. '/label',
  2383. 'input' => [
  2384. 'type' => 'text', 'name' => 'Article[title]',
  2385. 'id' => 'article-title', 'class' => 'form-error'
  2386. ],
  2387. ['div' => ['class' => 'error-message']],
  2388. 'error message',
  2389. '/div',
  2390. '/div'
  2391. ];
  2392. $this->assertHtml($expected, $result);
  2393. $result = $this->Form->control('Article.title', [
  2394. 'templates' => [
  2395. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2396. ]
  2397. ]);
  2398. $expected = [
  2399. 'div' => ['class' => 'input text error'],
  2400. 'label' => ['for' => 'article-title'],
  2401. 'Title',
  2402. '/label',
  2403. 'input' => [
  2404. 'type' => 'text', 'name' => 'Article[title]',
  2405. 'id' => 'article-title', 'class' => 'form-error'
  2406. ],
  2407. '/div'
  2408. ];
  2409. $this->assertHtml($expected, $result);
  2410. $result = $this->Form->control('Article.content');
  2411. $expected = [
  2412. 'div' => ['class' => 'input text error'],
  2413. 'label' => ['for' => 'article-content'],
  2414. 'Content',
  2415. '/label',
  2416. 'input' => [
  2417. 'type' => 'text', 'name' => 'Article[content]',
  2418. 'id' => 'article-content', 'class' => 'form-error'
  2419. ],
  2420. ['div' => ['class' => 'error-message']],
  2421. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2422. '/div',
  2423. '/div'
  2424. ];
  2425. $this->assertHtml($expected, $result);
  2426. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2427. $expected = [
  2428. 'div' => ['class' => 'input text error'],
  2429. 'label' => ['for' => 'article-content'],
  2430. 'Content',
  2431. '/label',
  2432. 'input' => [
  2433. 'type' => 'text', 'name' => 'Article[content]',
  2434. 'id' => 'article-content', 'class' => 'form-error'
  2435. ],
  2436. ['div' => ['class' => 'error-message']],
  2437. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2438. '/div',
  2439. '/div'
  2440. ];
  2441. $this->assertHtml($expected, $result);
  2442. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2443. $expected = [
  2444. 'div' => ['class' => 'input text error'],
  2445. 'label' => ['for' => 'article-content'],
  2446. 'Content',
  2447. '/label',
  2448. 'input' => [
  2449. 'type' => 'text', 'name' => 'Article[content]',
  2450. 'id' => 'article-content', 'class' => 'form-error'
  2451. ],
  2452. ['div' => ['class' => 'error-message']],
  2453. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2454. '/div',
  2455. '/div'
  2456. ];
  2457. $this->assertHtml($expected, $result);
  2458. }
  2459. /**
  2460. * testEmptyErrorValidation method
  2461. *
  2462. * Test validation errors, when validation message is an empty string.
  2463. *
  2464. * @return void
  2465. */
  2466. public function testEmptyErrorValidation()
  2467. {
  2468. $this->article['errors'] = [
  2469. 'Article' => ['title' => '']
  2470. ];
  2471. $this->Form->create($this->article);
  2472. $result = $this->Form->control('Article.title');
  2473. $expected = [
  2474. 'div' => ['class' => 'input text error'],
  2475. 'label' => ['for' => 'article-title'],
  2476. 'Title',
  2477. '/label',
  2478. 'input' => [
  2479. 'type' => 'text', 'name' => 'Article[title]',
  2480. 'id' => 'article-title', 'class' => 'form-error'
  2481. ],
  2482. ['div' => ['class' => 'error-message']],
  2483. [],
  2484. '/div',
  2485. '/div'
  2486. ];
  2487. $this->assertHtml($expected, $result);
  2488. }
  2489. /**
  2490. * testEmptyControlErrorValidation method
  2491. *
  2492. * Test validation errors, when calling control() overriding validation message by an empty string.
  2493. *
  2494. * @return void
  2495. */
  2496. public function testEmptyControlErrorValidation()
  2497. {
  2498. $this->article['errors'] = [
  2499. 'Article' => ['title' => 'error message']
  2500. ];
  2501. $this->Form->create($this->article);
  2502. $result = $this->Form->control('Article.title', ['error' => '']);
  2503. $expected = [
  2504. 'div' => ['class' => 'input text error'],
  2505. 'label' => ['for' => 'article-title'],
  2506. 'Title',
  2507. '/label',
  2508. 'input' => [
  2509. 'type' => 'text', 'name' => 'Article[title]',
  2510. 'id' => 'article-title', 'class' => 'form-error'
  2511. ],
  2512. ['div' => ['class' => 'error-message']],
  2513. [],
  2514. '/div',
  2515. '/div'
  2516. ];
  2517. $this->assertHtml($expected, $result);
  2518. }
  2519. /**
  2520. * testControlErrorMessage method
  2521. *
  2522. * Test validation errors, when calling control() overriding validation messages.
  2523. *
  2524. * @return void
  2525. */
  2526. public function testControlErrorMessage()
  2527. {
  2528. $this->article['errors'] = [
  2529. 'title' => ['error message']
  2530. ];
  2531. $this->Form->create($this->article);
  2532. $result = $this->Form->control('title', [
  2533. 'error' => 'Custom error!'
  2534. ]);
  2535. $expected = [
  2536. 'div' => ['class' => 'input text required error'],
  2537. 'label' => ['for' => 'title'],
  2538. 'Title',
  2539. '/label',
  2540. 'input' => [
  2541. 'type' => 'text', 'name' => 'title',
  2542. 'id' => 'title', 'class' => 'form-error',
  2543. 'required' => 'required',
  2544. ],
  2545. ['div' => ['class' => 'error-message']],
  2546. 'Custom error!',
  2547. '/div',
  2548. '/div'
  2549. ];
  2550. $this->assertHtml($expected, $result);
  2551. $result = $this->Form->control('title', [
  2552. 'error' => ['error message' => 'Custom error!']
  2553. ]);
  2554. $expected = [
  2555. 'div' => ['class' => 'input text required error'],
  2556. 'label' => ['for' => 'title'],
  2557. 'Title',
  2558. '/label',
  2559. 'input' => [
  2560. 'type' => 'text',
  2561. 'name' => 'title',
  2562. 'id' => 'title',
  2563. 'class' => 'form-error',
  2564. 'required' => 'required'
  2565. ],
  2566. ['div' => ['class' => 'error-message']],
  2567. 'Custom error!',
  2568. '/div',
  2569. '/div'
  2570. ];
  2571. $this->assertHtml($expected, $result);
  2572. }
  2573. /**
  2574. * testFormValidationAssociated method
  2575. *
  2576. * Tests displaying errors for nested entities.
  2577. *
  2578. * @return void
  2579. */
  2580. public function testFormValidationAssociated()
  2581. {
  2582. $nested = new Entity(['foo' => 'bar']);
  2583. $nested->setError('foo', ['not a valid bar']);
  2584. $entity = new Entity(['nested' => $nested]);
  2585. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2586. $result = $this->Form->error('nested.foo');
  2587. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2588. }
  2589. /**
  2590. * testFormValidationAssociatedSecondLevel method
  2591. *
  2592. * Test form error display with associated model.
  2593. *
  2594. * @return void
  2595. */
  2596. public function testFormValidationAssociatedSecondLevel()
  2597. {
  2598. $inner = new Entity(['bar' => 'baz']);
  2599. $nested = new Entity(['foo' => $inner]);
  2600. $entity = new Entity(['nested' => $nested]);
  2601. $inner->setError('bar', ['not a valid one']);
  2602. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2603. $result = $this->Form->error('nested.foo.bar');
  2604. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2605. }
  2606. /**
  2607. * testFormValidationMultiRecord method
  2608. *
  2609. * Test form error display with multiple records.
  2610. *
  2611. * @return void
  2612. */
  2613. public function testFormValidationMultiRecord()
  2614. {
  2615. $one = new Entity();
  2616. $two = new Entity();
  2617. $this->getTableLocator()->get('Contacts', [
  2618. 'className' => __NAMESPACE__ . '\ContactsTable'
  2619. ]);
  2620. $one->set('email', '');
  2621. $one->setError('email', ['invalid email']);
  2622. $two->set('name', '');
  2623. $two->setError('name', ['This is wrong']);
  2624. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2625. $result = $this->Form->control('0.email');
  2626. $expected = [
  2627. 'div' => ['class' => 'input email error'],
  2628. 'label' => ['for' => '0-email'],
  2629. 'Email',
  2630. '/label',
  2631. 'input' => [
  2632. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2633. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2634. ],
  2635. ['div' => ['class' => 'error-message']],
  2636. 'invalid email',
  2637. '/div',
  2638. '/div'
  2639. ];
  2640. $this->assertHtml($expected, $result);
  2641. $result = $this->Form->control('1.name');
  2642. $expected = [
  2643. 'div' => ['class' => 'input text error'],
  2644. 'label' => ['for' => '1-name'],
  2645. 'Name',
  2646. '/label',
  2647. 'input' => [
  2648. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2649. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2650. ],
  2651. ['div' => ['class' => 'error-message']],
  2652. 'This is wrong',
  2653. '/div',
  2654. '/div'
  2655. ];
  2656. $this->assertHtml($expected, $result);
  2657. }
  2658. /**
  2659. * testControl method
  2660. *
  2661. * Test various incarnations of control().
  2662. *
  2663. * @return void
  2664. */
  2665. public function testControl()
  2666. {
  2667. $this->getTableLocator()->get('ValidateUsers', [
  2668. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2669. ]);
  2670. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2671. $result = $this->Form->control('ValidateUsers.balance');
  2672. $expected = [
  2673. 'div' => ['class'],
  2674. 'label' => ['for'],
  2675. 'Balance',
  2676. '/label',
  2677. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2678. '/div',
  2679. ];
  2680. $this->assertHtml($expected, $result);
  2681. $result = $this->Form->control('ValidateUser.cost_decimal');
  2682. $expected = [
  2683. 'div' => ['class'],
  2684. 'label' => ['for'],
  2685. 'Cost Decimal',
  2686. '/label',
  2687. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2688. '/div',
  2689. ];
  2690. $this->assertHtml($expected, $result);
  2691. $result = $this->Form->control('ValidateUser.null_decimal');
  2692. $expected = [
  2693. 'div' => ['class'],
  2694. 'label' => ['for'],
  2695. 'Null Decimal',
  2696. '/label',
  2697. 'input' => ['name', 'type' => 'number', 'id'],
  2698. '/div',
  2699. ];
  2700. $this->assertHtml($expected, $result);
  2701. }
  2702. /**
  2703. * testControlCustomization method
  2704. *
  2705. * Tests the input method and passing custom options.
  2706. *
  2707. * @return void
  2708. */
  2709. public function testControlCustomization()
  2710. {
  2711. $this->getTableLocator()->get('Contacts', [
  2712. 'className' => __NAMESPACE__ . '\ContactsTable'
  2713. ]);
  2714. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2715. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2716. $expected = [
  2717. 'div' => ['class' => 'input email'],
  2718. 'label' => ['for' => 'custom'],
  2719. 'Email',
  2720. '/label',
  2721. ['input' => [
  2722. 'type' => 'email', 'name' => 'Contact[email]',
  2723. 'id' => 'custom', 'maxlength' => 255
  2724. ]],
  2725. '/div'
  2726. ];
  2727. $this->assertHtml($expected, $result);
  2728. $result = $this->Form->control('Contact.email', [
  2729. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2730. ]);
  2731. $expected = [
  2732. '<div',
  2733. 'label' => ['for' => 'contact-email'],
  2734. 'Email',
  2735. '/label',
  2736. ['input' => [
  2737. 'type' => 'email', 'name' => 'Contact[email]',
  2738. 'id' => 'contact-email', 'maxlength' => 255
  2739. ]],
  2740. '/div'
  2741. ];
  2742. $this->assertHtml($expected, $result);
  2743. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2744. $expected = [
  2745. 'div' => ['class' => 'input text'],
  2746. 'label' => ['for' => 'contact-email'],
  2747. 'Email',
  2748. '/label',
  2749. ['input' => [
  2750. 'type' => 'text', 'name' => 'Contact[email]',
  2751. 'id' => 'contact-email', 'maxlength' => '255'
  2752. ]],
  2753. '/div'
  2754. ];
  2755. $this->assertHtml($expected, $result);
  2756. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2757. $expected = [
  2758. 'div' => ['class' => 'input text'],
  2759. 'label' => ['for' => 'contact-5-email'],
  2760. 'Email',
  2761. '/label',
  2762. ['input' => [
  2763. 'type' => 'text', 'name' => 'Contact[5][email]',
  2764. 'id' => 'contact-5-email', 'maxlength' => '255'
  2765. ]],
  2766. '/div'
  2767. ];
  2768. $this->assertHtml($expected, $result);
  2769. $result = $this->Form->control('Contact.password');
  2770. $expected = [
  2771. 'div' => ['class' => 'input password'],
  2772. 'label' => ['for' => 'contact-password'],
  2773. 'Password',
  2774. '/label',
  2775. ['input' => [
  2776. 'type' => 'password', 'name' => 'Contact[password]',
  2777. 'id' => 'contact-password'
  2778. ]],
  2779. '/div'
  2780. ];
  2781. $this->assertHtml($expected, $result);
  2782. $result = $this->Form->control('Contact.email', [
  2783. 'type' => 'file', 'class' => 'textbox'
  2784. ]);
  2785. $expected = [
  2786. 'div' => ['class' => 'input file'],
  2787. 'label' => ['for' => 'contact-email'],
  2788. 'Email',
  2789. '/label',
  2790. ['input' => [
  2791. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2792. 'id' => 'contact-email'
  2793. ]],
  2794. '/div'
  2795. ];
  2796. $this->assertHtml($expected, $result);
  2797. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2798. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2799. $result = $this->Form->control('phone');
  2800. $expected = [
  2801. 'div' => ['class' => 'input tel'],
  2802. 'label' => ['for' => 'phone'],
  2803. 'Phone',
  2804. '/label',
  2805. ['input' => [
  2806. 'type' => 'tel', 'name' => 'phone',
  2807. 'value' => 'Hello &amp; World &gt; weird chars',
  2808. 'id' => 'phone', 'maxlength' => 255
  2809. ]],
  2810. '/div'
  2811. ];
  2812. $this->assertHtml($expected, $result);
  2813. $this->View->setRequest(
  2814. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2815. );
  2816. $this->Form->create();
  2817. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2818. $expected = [
  2819. 'div' => ['class' => 'input text'],
  2820. 'label' => ['for' => 'myId'],
  2821. 'Field',
  2822. '/label',
  2823. 'input' => [
  2824. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2825. 'value' => 'My value', 'id' => 'myId'
  2826. ],
  2827. '/div'
  2828. ];
  2829. $this->assertHtml($expected, $result);
  2830. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2831. $this->Form->create();
  2832. $entity->setError('field', 'Badness!');
  2833. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2834. $result = $this->Form->control('field');
  2835. $expected = [
  2836. 'div' => ['class' => 'input text error'],
  2837. 'label' => ['for' => 'field'],
  2838. 'Field',
  2839. '/label',
  2840. 'input' => [
  2841. 'type' => 'text', 'name' => 'field',
  2842. 'id' => 'field', 'class' => 'form-error'
  2843. ],
  2844. ['div' => ['class' => 'error-message']],
  2845. 'Badness!',
  2846. '/div',
  2847. '/div'
  2848. ];
  2849. $this->assertHtml($expected, $result);
  2850. $result = $this->Form->control('field', [
  2851. 'templates' => [
  2852. 'inputContainerError' => '{{content}}{{error}}',
  2853. 'error' => '<span class="error-message">{{content}}</span>'
  2854. ]
  2855. ]);
  2856. $expected = [
  2857. 'label' => ['for' => 'field'],
  2858. 'Field',
  2859. '/label',
  2860. 'input' => [
  2861. 'type' => 'text', 'name' => 'field',
  2862. 'id' => 'field', 'class' => 'form-error'
  2863. ],
  2864. ['span' => ['class' => 'error-message']],
  2865. 'Badness!',
  2866. '/span'
  2867. ];
  2868. $this->assertHtml($expected, $result);
  2869. $entity->setError('field', ['minLength'], true);
  2870. $result = $this->Form->control('field', [
  2871. 'error' => [
  2872. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2873. 'maxLength' => 'login too large'
  2874. ]
  2875. ]);
  2876. $expected = [
  2877. 'div' => ['class' => 'input text error'],
  2878. 'label' => ['for' => 'field'],
  2879. 'Field',
  2880. '/label',
  2881. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2882. ['div' => ['class' => 'error-message']],
  2883. 'Le login doit contenir au moins 2 caractères',
  2884. '/div',
  2885. '/div'
  2886. ];
  2887. $this->assertHtml($expected, $result);
  2888. $entity->setError('field', ['maxLength'], true);
  2889. $result = $this->Form->control('field', [
  2890. 'error' => [
  2891. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2892. 'maxLength' => 'login too large',
  2893. ]
  2894. ]);
  2895. $expected = [
  2896. 'div' => ['class' => 'input text error'],
  2897. 'label' => ['for' => 'field'],
  2898. 'Field',
  2899. '/label',
  2900. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2901. ['div' => ['class' => 'error-message']],
  2902. 'login too large',
  2903. '/div',
  2904. '/div'
  2905. ];
  2906. $this->assertHtml($expected, $result);
  2907. }
  2908. /**
  2909. * testControlWithTemplateFile method
  2910. *
  2911. * Test that control() accepts a template file.
  2912. *
  2913. * @return void
  2914. */
  2915. public function testControlWithTemplateFile()
  2916. {
  2917. $result = $this->Form->control('field', [
  2918. 'templates' => 'htmlhelper_tags'
  2919. ]);
  2920. $expected = [
  2921. 'label' => ['for' => 'field'],
  2922. 'Field',
  2923. '/label',
  2924. 'input' => [
  2925. 'type' => 'text', 'name' => 'field',
  2926. 'id' => 'field'
  2927. ],
  2928. ];
  2929. $this->assertHtml($expected, $result);
  2930. }
  2931. /**
  2932. * testNestedControlsEndWithBrackets method
  2933. *
  2934. * Test that nested inputs end with brackets.
  2935. *
  2936. * @return void
  2937. */
  2938. public function testNestedControlsEndWithBrackets()
  2939. {
  2940. $result = $this->Form->text('nested.text[]');
  2941. $expected = [
  2942. 'input' => [
  2943. 'type' => 'text', 'name' => 'nested[text][]'
  2944. ],
  2945. ];
  2946. $this->assertHtml($expected, $result);
  2947. $result = $this->Form->file('nested.file[]');
  2948. $expected = [
  2949. 'input' => [
  2950. 'type' => 'file', 'name' => 'nested[file][]'
  2951. ],
  2952. ];
  2953. $this->assertHtml($expected, $result);
  2954. }
  2955. /**
  2956. * testCreateIdPrefix method
  2957. *
  2958. * Test id prefix.
  2959. *
  2960. * @return void
  2961. */
  2962. public function testCreateIdPrefix()
  2963. {
  2964. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2965. $result = $this->Form->control('field');
  2966. $expected = [
  2967. 'div' => ['class' => 'input text'],
  2968. 'label' => ['for' => 'prefix-field'],
  2969. 'Field',
  2970. '/label',
  2971. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2972. '/div'
  2973. ];
  2974. $this->assertHtml($expected, $result);
  2975. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2976. $expected = [
  2977. 'div' => ['class' => 'input text'],
  2978. 'label' => ['for' => 'custom-id'],
  2979. 'Field',
  2980. '/label',
  2981. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2982. '/div'
  2983. ];
  2984. $this->assertHtml($expected, $result);
  2985. $result = $this->Form->radio('Model.field', ['option A']);
  2986. $expected = [
  2987. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2988. 'label' => ['for' => 'prefix-model-field-0'],
  2989. ['input' => [
  2990. 'type' => 'radio',
  2991. 'name' => 'Model[field]',
  2992. 'value' => '0',
  2993. 'id' => 'prefix-model-field-0'
  2994. ]],
  2995. 'option A',
  2996. '/label'
  2997. ];
  2998. $this->assertHtml($expected, $result);
  2999. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  3000. $expected = [
  3001. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3002. 'label' => ['for' => 'prefix-model-field-0'],
  3003. ['input' => [
  3004. 'type' => 'radio',
  3005. 'name' => 'Model[field]',
  3006. 'value' => '0',
  3007. 'id' => 'prefix-model-field-0'
  3008. ]],
  3009. 'option A',
  3010. '/label'
  3011. ];
  3012. $this->assertHtml($expected, $result);
  3013. $result = $this->Form->select(
  3014. 'Model.multi_field',
  3015. ['first'],
  3016. ['multiple' => 'checkbox']
  3017. );
  3018. $expected = [
  3019. 'input' => [
  3020. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3021. ],
  3022. ['div' => ['class' => 'checkbox']],
  3023. ['label' => ['for' => 'prefix-model-multi-field-0']],
  3024. ['input' => [
  3025. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3026. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  3027. ]],
  3028. 'first',
  3029. '/label',
  3030. '/div',
  3031. ];
  3032. $this->assertHtml($expected, $result);
  3033. $this->Form->end();
  3034. $result = $this->Form->control('field');
  3035. $expected = [
  3036. 'div' => ['class' => 'input text'],
  3037. 'label' => ['for' => 'field'],
  3038. 'Field',
  3039. '/label',
  3040. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3041. '/div'
  3042. ];
  3043. $this->assertHtml($expected, $result);
  3044. }
  3045. /**
  3046. * testControlZero method
  3047. *
  3048. * Test that inputs with 0 can be created.
  3049. *
  3050. * @return void
  3051. */
  3052. public function testControlZero()
  3053. {
  3054. $this->getTableLocator()->get('Contacts', [
  3055. 'className' => __NAMESPACE__ . '\ContactsTable'
  3056. ]);
  3057. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3058. $result = $this->Form->control('0');
  3059. $expected = [
  3060. 'div' => ['class' => 'input text'],
  3061. 'label' => ['for' => '0'], '/label',
  3062. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3063. '/div'
  3064. ];
  3065. $this->assertHtml($expected, $result);
  3066. }
  3067. /**
  3068. * testControlCheckbox method
  3069. *
  3070. * Test control() with checkbox creation.
  3071. *
  3072. * @return void
  3073. */
  3074. public function testControlCheckbox()
  3075. {
  3076. $articles = $this->getTableLocator()->get('Articles');
  3077. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3078. $article = $articles->newEntity();
  3079. $this->Form->create($article);
  3080. $result = $this->Form->control('Articles.active');
  3081. $expected = [
  3082. 'div' => ['class' => 'input checkbox'],
  3083. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3084. 'label' => ['for' => 'articles-active'],
  3085. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3086. 'Active',
  3087. '/label',
  3088. '/div'
  3089. ];
  3090. $this->assertHtml($expected, $result);
  3091. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3092. $expected = [
  3093. 'div' => ['class' => 'input checkbox'],
  3094. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3095. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3096. '/div'
  3097. ];
  3098. $this->assertHtml($expected, $result);
  3099. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3100. $expected = [
  3101. 'div' => ['class' => 'input checkbox'],
  3102. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3103. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3104. '/div'
  3105. ];
  3106. $this->assertHtml($expected, $result);
  3107. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3108. $expected = [
  3109. 'div' => ['class' => 'input checkbox'],
  3110. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3111. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3112. '/div'
  3113. ];
  3114. $this->assertHtml($expected, $result);
  3115. $result = $this->Form->control('Articles.disabled', [
  3116. 'label' => 'Disabled',
  3117. 'type' => 'checkbox',
  3118. 'data-foo' => 'disabled'
  3119. ]);
  3120. $expected = [
  3121. 'div' => ['class' => 'input checkbox'],
  3122. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3123. 'label' => ['for' => 'articles-disabled'],
  3124. ['input' => [
  3125. 'type' => 'checkbox',
  3126. 'name' => 'Articles[disabled]',
  3127. 'value' => '1',
  3128. 'id' => 'articles-disabled',
  3129. 'data-foo' => 'disabled'
  3130. ]],
  3131. 'Disabled',
  3132. '/label',
  3133. '/div'
  3134. ];
  3135. $this->assertHtml($expected, $result);
  3136. $result = $this->Form->control('Articles.confirm', [
  3137. 'label' => 'Confirm <b>me</b>!',
  3138. 'type' => 'checkbox',
  3139. 'escape' => false
  3140. ]);
  3141. $expected = [
  3142. 'div' => ['class' => 'input checkbox'],
  3143. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3144. 'label' => ['for' => 'articles-confirm'],
  3145. ['input' => [
  3146. 'type' => 'checkbox',
  3147. 'name' => 'Articles[confirm]',
  3148. 'value' => '1',
  3149. 'id' => 'articles-confirm',
  3150. ]],
  3151. 'Confirm <b>me</b>!',
  3152. '/label',
  3153. '/div'
  3154. ];
  3155. $this->assertHtml($expected, $result);
  3156. }
  3157. /**
  3158. * testControlHidden method
  3159. *
  3160. * Test that control() does not create wrapping div and label tag for hidden fields.
  3161. *
  3162. * @return void
  3163. */
  3164. public function testControlHidden()
  3165. {
  3166. $this->getTableLocator()->get('ValidateUsers', [
  3167. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3168. ]);
  3169. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3170. $result = $this->Form->control('ValidateUser.id');
  3171. $expected = [
  3172. 'input' => ['name', 'type' => 'hidden', 'id']
  3173. ];
  3174. $this->assertHtml($expected, $result);
  3175. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3176. $expected = [
  3177. 'input' => ['name', 'type' => 'hidden', 'id']
  3178. ];
  3179. $this->assertHtml($expected, $result);
  3180. }
  3181. /**
  3182. * testControlDatetime method
  3183. *
  3184. * Test form->control() with datetime.
  3185. *
  3186. * @return void
  3187. */
  3188. public function testControlDatetime()
  3189. {
  3190. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3191. ->setMethods(['datetime'])
  3192. ->setConstructorArgs([new View()])
  3193. ->getMock();
  3194. $this->Form->expects($this->once())->method('datetime')
  3195. ->with('prueba', [
  3196. 'type' => 'datetime',
  3197. 'timeFormat' => 24,
  3198. 'minYear' => 2008,
  3199. 'maxYear' => 2011,
  3200. 'interval' => 15,
  3201. 'options' => null,
  3202. 'empty' => false,
  3203. 'id' => 'prueba',
  3204. 'required' => false,
  3205. 'templateVars' => []
  3206. ])
  3207. ->will($this->returnValue('This is it!'));
  3208. $result = $this->Form->control('prueba', [
  3209. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3210. 'maxYear' => 2011, 'interval' => 15
  3211. ]);
  3212. $expected = [
  3213. 'div' => ['class' => 'input datetime'],
  3214. '<label',
  3215. 'Prueba',
  3216. '/label',
  3217. 'This is it!',
  3218. '/div'
  3219. ];
  3220. $this->assertHtml($expected, $result);
  3221. }
  3222. /**
  3223. * testControlDatetimeIdPrefix method
  3224. *
  3225. * Test form->control() with datetime with id prefix.
  3226. *
  3227. * @return void
  3228. */
  3229. public function testControlDatetimeIdPrefix()
  3230. {
  3231. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3232. ->setMethods(['datetime'])
  3233. ->setConstructorArgs([new View()])
  3234. ->getMock();
  3235. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3236. $this->Form->expects($this->once())->method('datetime')
  3237. ->with('prueba', [
  3238. 'type' => 'datetime',
  3239. 'timeFormat' => 24,
  3240. 'minYear' => 2008,
  3241. 'maxYear' => 2011,
  3242. 'interval' => 15,
  3243. 'options' => null,
  3244. 'empty' => false,
  3245. 'id' => 'prefix-prueba',
  3246. 'required' => false,
  3247. 'templateVars' => []
  3248. ])
  3249. ->will($this->returnValue('This is it!'));
  3250. $result = $this->Form->control('prueba', [
  3251. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3252. 'maxYear' => 2011, 'interval' => 15
  3253. ]);
  3254. $expected = [
  3255. 'div' => ['class' => 'input datetime'],
  3256. '<label',
  3257. 'Prueba',
  3258. '/label',
  3259. 'This is it!',
  3260. '/div'
  3261. ];
  3262. $this->assertHtml($expected, $result);
  3263. }
  3264. /**
  3265. * testControlCheckboxWithDisabledElements method
  3266. *
  3267. * Test generating checkboxes with disabled elements.
  3268. *
  3269. * @return void
  3270. */
  3271. public function testControlCheckboxWithDisabledElements()
  3272. {
  3273. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3274. $result = $this->Form->control('Contact.multiple', [
  3275. 'multiple' => 'checkbox',
  3276. 'disabled' => 'disabled',
  3277. 'options' => $options
  3278. ]);
  3279. $expected = [
  3280. ['div' => ['class' => 'input select']],
  3281. ['label' => ['for' => 'contact-multiple']],
  3282. 'Multiple',
  3283. '/label',
  3284. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3285. ['div' => ['class' => 'checkbox']],
  3286. ['label' => ['for' => 'contact-multiple-1']],
  3287. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3288. 'One',
  3289. '/label',
  3290. '/div',
  3291. ['div' => ['class' => 'checkbox']],
  3292. ['label' => ['for' => 'contact-multiple-2']],
  3293. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3294. 'Two',
  3295. '/label',
  3296. '/div',
  3297. ['div' => ['class' => 'checkbox']],
  3298. ['label' => ['for' => 'contact-multiple-3']],
  3299. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3300. 'Three',
  3301. '/label',
  3302. '/div',
  3303. '/div'
  3304. ];
  3305. $this->assertHtml($expected, $result);
  3306. // make sure 50 does only disable 50, and not 50f5c0cf
  3307. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3308. $disabled = [50];
  3309. $expected = [
  3310. ['div' => ['class' => 'input select']],
  3311. ['label' => ['for' => 'contact-multiple']],
  3312. 'Multiple',
  3313. '/label',
  3314. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3315. ['div' => ['class' => 'checkbox']],
  3316. ['label' => ['for' => 'contact-multiple-50']],
  3317. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3318. 'Fifty',
  3319. '/label',
  3320. '/div',
  3321. ['div' => ['class' => 'checkbox']],
  3322. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3323. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3324. 'Stringy',
  3325. '/label',
  3326. '/div',
  3327. '/div'
  3328. ];
  3329. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3330. $this->assertHtml($expected, $result);
  3331. }
  3332. /**
  3333. * testControlWithLeadingInteger method
  3334. *
  3335. * Test input name with leading integer, ensure attributes are generated correctly.
  3336. *
  3337. * @return void
  3338. */
  3339. public function testControlWithLeadingInteger()
  3340. {
  3341. $result = $this->Form->text('0.Node.title');
  3342. $expected = [
  3343. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3344. ];
  3345. $this->assertHtml($expected, $result);
  3346. }
  3347. /**
  3348. * testControlSelectType method
  3349. *
  3350. * Test form->control() with select type inputs.
  3351. *
  3352. * @return void
  3353. */
  3354. public function testControlSelectType()
  3355. {
  3356. $result = $this->Form->control(
  3357. 'email',
  3358. [
  3359. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3360. );
  3361. $expected = [
  3362. 'div' => ['class' => 'input select'],
  3363. 'label' => ['for' => 'email'],
  3364. 'Email',
  3365. '/label',
  3366. ['select' => ['name' => 'email', 'id' => 'email']],
  3367. ['option' => ['value' => '']],
  3368. '/option',
  3369. ['option' => ['value' => 'è']],
  3370. 'Firést',
  3371. '/option',
  3372. ['option' => ['value' => 'é']],
  3373. 'Secoènd',
  3374. '/option',
  3375. '/select',
  3376. '/div'
  3377. ];
  3378. $this->assertHtml($expected, $result);
  3379. $result = $this->Form->control(
  3380. 'email',
  3381. [
  3382. 'options' => ['First', 'Second'], 'empty' => true]
  3383. );
  3384. $expected = [
  3385. 'div' => ['class' => 'input select'],
  3386. 'label' => ['for' => 'email'],
  3387. 'Email',
  3388. '/label',
  3389. ['select' => ['name' => 'email', 'id' => 'email']],
  3390. ['option' => ['value' => '']],
  3391. '/option',
  3392. ['option' => ['value' => '0']],
  3393. 'First',
  3394. '/option',
  3395. ['option' => ['value' => '1']],
  3396. 'Second',
  3397. '/option',
  3398. '/select',
  3399. '/div'
  3400. ];
  3401. $this->assertHtml($expected, $result);
  3402. $result = $this->Form->control('email', [
  3403. 'type' => 'select',
  3404. 'options' => new \ArrayObject(['First', 'Second']),
  3405. 'empty' => true
  3406. ]);
  3407. $this->assertHtml($expected, $result);
  3408. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3409. $this->View->setRequest(
  3410. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3411. );
  3412. $this->Form->create();
  3413. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3414. $expected = [
  3415. 'div' => ['class' => 'input select'],
  3416. 'label' => ['for' => 'model-user-id'],
  3417. 'User',
  3418. '/label',
  3419. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3420. ['option' => ['value' => '']],
  3421. '/option',
  3422. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3423. 'good',
  3424. '/option',
  3425. ['option' => ['value' => 'other']],
  3426. 'bad',
  3427. '/option',
  3428. '/select',
  3429. '/div'
  3430. ];
  3431. $this->assertHtml($expected, $result);
  3432. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3433. $this->View->setRequest(
  3434. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3435. );
  3436. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3437. $expected = [
  3438. 'div' => ['class' => 'input select'],
  3439. 'label' => ['for' => 'thing-user-id'],
  3440. 'User',
  3441. '/label',
  3442. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3443. ['option' => ['value' => '']],
  3444. 'Some Empty',
  3445. '/option',
  3446. ['option' => ['value' => 'value']],
  3447. 'good',
  3448. '/option',
  3449. ['option' => ['value' => 'other']],
  3450. 'bad',
  3451. '/option',
  3452. '/select',
  3453. '/div'
  3454. ];
  3455. $this->assertHtml($expected, $result);
  3456. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3457. $this->View->setRequest(
  3458. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3459. );
  3460. $this->Form->create();
  3461. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3462. $expected = [
  3463. 'div' => ['class' => 'input select'],
  3464. 'label' => ['for' => 'thing-user-id'],
  3465. 'User',
  3466. '/label',
  3467. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3468. ['option' => ['value' => '']],
  3469. 'Some Empty',
  3470. '/option',
  3471. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3472. 'good',
  3473. '/option',
  3474. ['option' => ['value' => 'other']],
  3475. 'bad',
  3476. '/option',
  3477. '/select',
  3478. '/div'
  3479. ];
  3480. $this->assertHtml($expected, $result);
  3481. $result = $this->Form->control('Publisher.id', [
  3482. 'label' => 'Publisher',
  3483. 'type' => 'select',
  3484. 'multiple' => 'checkbox',
  3485. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3486. ]);
  3487. $expected = [
  3488. ['div' => ['class' => 'input select']],
  3489. ['label' => ['for' => 'publisher-id']],
  3490. 'Publisher',
  3491. '/label',
  3492. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3493. ['div' => ['class' => 'checkbox']],
  3494. ['label' => ['for' => 'publisher-id-value-1']],
  3495. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3496. 'Label 1',
  3497. '/label',
  3498. '/div',
  3499. ['div' => ['class' => 'checkbox']],
  3500. ['label' => ['for' => 'publisher-id-value-2']],
  3501. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3502. 'Label 2',
  3503. '/label',
  3504. '/div',
  3505. '/div'
  3506. ];
  3507. $this->assertHtml($expected, $result);
  3508. }
  3509. /**
  3510. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3511. *
  3512. * Test that control() and a non standard primary key makes a hidden input by default.
  3513. *
  3514. * @return void
  3515. */
  3516. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3517. {
  3518. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3519. $this->Form->create($this->article);
  3520. $result = $this->Form->control('title');
  3521. $expected = [
  3522. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3523. ];
  3524. $this->assertHtml($expected, $result);
  3525. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3526. $this->Form->create($this->article);
  3527. $result = $this->Form->control('title');
  3528. $expected = [
  3529. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3530. ];
  3531. $this->assertHtml($expected, $result);
  3532. $result = $this->Form->control('body');
  3533. $expected = [
  3534. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3535. ];
  3536. $this->assertHtml($expected, $result);
  3537. }
  3538. /**
  3539. * testControlOverridingMagicSelectType method
  3540. *
  3541. * Test that overriding the magic select type widget is possible.
  3542. *
  3543. * @return void
  3544. */
  3545. public function testControlOverridingMagicSelectType()
  3546. {
  3547. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3548. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3549. $expected = [
  3550. 'div' => ['class' => 'input text'],
  3551. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3552. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3553. '/div'
  3554. ];
  3555. $this->assertHtml($expected, $result);
  3556. //Check that magic types still work for plural/singular vars
  3557. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3558. $result = $this->Form->control('Model.type');
  3559. $expected = [
  3560. 'div' => ['class' => 'input select'],
  3561. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3562. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3563. ['option' => ['value' => 'value']], 'good', '/option',
  3564. ['option' => ['value' => 'other']], 'bad', '/option',
  3565. '/select',
  3566. '/div'
  3567. ];
  3568. $this->assertHtml($expected, $result);
  3569. }
  3570. /**
  3571. * testControlMagicTypeDoesNotOverride method
  3572. *
  3573. * Test that inferred types do not override developer input.
  3574. *
  3575. * @return void
  3576. */
  3577. public function testControlMagicTypeDoesNotOverride()
  3578. {
  3579. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3580. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3581. $expected = [
  3582. 'div' => ['class' => 'input checkbox'],
  3583. ['input' => [
  3584. 'type' => 'hidden',
  3585. 'name' => 'Model[user]',
  3586. 'value' => 0,
  3587. ]],
  3588. 'label' => ['for' => 'model-user'],
  3589. ['input' => [
  3590. 'name' => 'Model[user]',
  3591. 'type' => 'checkbox',
  3592. 'id' => 'model-user',
  3593. 'value' => 1
  3594. ]],
  3595. 'User',
  3596. '/label',
  3597. '/div'
  3598. ];
  3599. $this->assertHtml($expected, $result);
  3600. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3601. $options = [
  3602. 1 => 'blue',
  3603. 2 => 'red'
  3604. ];
  3605. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3606. $expected = [
  3607. 'div' => ['class' => 'input select'],
  3608. 'label' => ['for' => 'tags-ids'],
  3609. 'Tags',
  3610. '/label',
  3611. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3612. ['div' => ['class' => 'checkbox']],
  3613. ['label' => ['for' => 'tags-ids-1']],
  3614. ['input' => [
  3615. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3616. 'value' => '1', 'name' => 'tags[_ids][]'
  3617. ]],
  3618. 'blue',
  3619. '/label',
  3620. '/div',
  3621. ['div' => ['class' => 'checkbox']],
  3622. ['label' => ['for' => 'tags-ids-2']],
  3623. ['input' => [
  3624. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3625. 'value' => '2', 'name' => 'tags[_ids][]'
  3626. ]],
  3627. 'red',
  3628. '/label',
  3629. '/div',
  3630. '/div'
  3631. ];
  3632. $this->assertHtml($expected, $result);
  3633. }
  3634. /**
  3635. * testControlMagicSelectForTypeNumber method
  3636. *
  3637. * Test that magic control() selects are created for type=number.
  3638. *
  3639. * @return void
  3640. */
  3641. public function testControlMagicSelectForTypeNumber()
  3642. {
  3643. $this->getTableLocator()->get('ValidateUsers', [
  3644. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3645. ]);
  3646. $entity = new Entity(['balance' => 1]);
  3647. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3648. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3649. $result = $this->Form->control('balance');
  3650. $expected = [
  3651. 'div' => ['class' => 'input select'],
  3652. 'label' => ['for' => 'balance'],
  3653. 'Balance',
  3654. '/label',
  3655. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3656. ['option' => ['value' => '0']],
  3657. 'nothing',
  3658. '/option',
  3659. ['option' => ['value' => '1', 'selected' => 'selected']],
  3660. 'some',
  3661. '/option',
  3662. ['option' => ['value' => '100']],
  3663. 'a lot',
  3664. '/option',
  3665. '/select',
  3666. '/div'
  3667. ];
  3668. $this->assertHtml($expected, $result);
  3669. }
  3670. /**
  3671. * testInvalidControlTypeOption method
  3672. *
  3673. * Test invalid 'input' type option to control() function.
  3674. *
  3675. * @return void
  3676. */
  3677. public function testInvalidControlTypeOption()
  3678. {
  3679. $this->expectException(\RuntimeException::class);
  3680. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3681. $this->Form->control('text', ['type' => 'input']);
  3682. }
  3683. /**
  3684. * testControlMagicSelectChangeToRadio method
  3685. *
  3686. * Test that magic control() selects can easily be converted into radio types without error.
  3687. *
  3688. * @return void
  3689. */
  3690. public function testControlMagicSelectChangeToRadio()
  3691. {
  3692. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3693. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3694. $this->assertContains('input type="radio"', $result);
  3695. }
  3696. /**
  3697. * testFormControlSubmit method
  3698. *
  3699. * Test correct results for form::control() and type submit.
  3700. *
  3701. * @return void
  3702. */
  3703. public function testFormControlSubmit()
  3704. {
  3705. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3706. $expected = [
  3707. 'div' => ['class' => 'submit'],
  3708. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3709. '/div'
  3710. ];
  3711. $this->assertHtml($expected, $result);
  3712. }
  3713. /**
  3714. * testFormControls method
  3715. *
  3716. * Test correct results from Form::controls().
  3717. *
  3718. * @return void
  3719. */
  3720. public function testFormControlsLegendFieldset()
  3721. {
  3722. $this->Form->create($this->article);
  3723. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3724. $expected = [
  3725. '<fieldset',
  3726. '<legend',
  3727. 'The Legend',
  3728. '/legend',
  3729. '*/fieldset',
  3730. ];
  3731. $this->assertHtml($expected, $result);
  3732. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3733. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3734. $this->assertContains('<fieldset>', $result);
  3735. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3736. $this->assertNotContains('<legend>', $result);
  3737. $this->assertNotContains('<fieldset>', $result);
  3738. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3739. $this->assertNotContains('<legend>', $result);
  3740. $this->assertNotContains('<fieldset>', $result);
  3741. $this->Form->create($this->article);
  3742. $this->View->setRequest($this->View->getRequest()
  3743. ->withParam('prefix', 'admin')
  3744. ->withParam('action', 'admin_edit')
  3745. ->withParam('controller', 'articles'));
  3746. $result = $this->Form->allControls();
  3747. $expected = [
  3748. '<fieldset',
  3749. '<legend',
  3750. 'New Article',
  3751. '/legend',
  3752. '*/fieldset',
  3753. ];
  3754. $this->assertHtml($expected, $result);
  3755. $this->Form->create($this->article);
  3756. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3757. $expected = [
  3758. '<fieldset',
  3759. '<legend',
  3760. 'The Legend',
  3761. '/legend',
  3762. '*/fieldset',
  3763. ];
  3764. $this->assertHtml($expected, $result);
  3765. $this->Form->create($this->article);
  3766. $result = $this->Form->allControls([], [
  3767. 'fieldset' => [
  3768. 'class' => 'some-class some-other-class',
  3769. 'disabled' => true,
  3770. 'data-param' => 'a-param'
  3771. ],
  3772. 'legend' => 'The Legend'
  3773. ]);
  3774. $expected = [
  3775. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3776. '<legend',
  3777. 'The Legend',
  3778. '/legend',
  3779. '*/fieldset',
  3780. ];
  3781. $this->assertHtml($expected, $result);
  3782. }
  3783. /**
  3784. * testFormControls method
  3785. *
  3786. * Test the controls() method.
  3787. *
  3788. * @return void
  3789. */
  3790. public function testFormControls()
  3791. {
  3792. $this->Form->create($this->article);
  3793. $result = $this->Form->allControls();
  3794. $expected = [
  3795. '<fieldset',
  3796. '<legend', 'New Article', '/legend',
  3797. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3798. ['div' => ['class' => 'input select required']],
  3799. '*/div',
  3800. ['div' => ['class' => 'input text required']],
  3801. '*/div',
  3802. ['div' => ['class' => 'input text']],
  3803. '*/div',
  3804. ['div' => ['class' => 'input text']],
  3805. '*/div',
  3806. '/fieldset',
  3807. ];
  3808. $this->assertHtml($expected, $result);
  3809. $result = $this->Form->allControls([
  3810. 'published' => ['type' => 'boolean']
  3811. ]);
  3812. $expected = [
  3813. '<fieldset',
  3814. '<legend', 'New Article', '/legend',
  3815. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3816. ['div' => ['class' => 'input select required']],
  3817. '*/div',
  3818. ['div' => ['class' => 'input text required']],
  3819. '*/div',
  3820. ['div' => ['class' => 'input text']],
  3821. '*/div',
  3822. ['div' => ['class' => 'input boolean']],
  3823. '*/div',
  3824. '/fieldset',
  3825. ];
  3826. $this->assertHtml($expected, $result);
  3827. $this->Form->create($this->article);
  3828. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3829. $expected = [
  3830. 'fieldset' => [],
  3831. 'legend' => [],
  3832. 'Hello',
  3833. '/legend',
  3834. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3835. ['div' => ['class' => 'input select required']],
  3836. '*/div',
  3837. ['div' => ['class' => 'input text required']],
  3838. '*/div',
  3839. ['div' => ['class' => 'input text']],
  3840. '*/div',
  3841. ['div' => ['class' => 'input text']],
  3842. '*/div',
  3843. '/fieldset'
  3844. ];
  3845. $this->assertHtml($expected, $result);
  3846. $this->Form->create(false);
  3847. $expected = [
  3848. 'fieldset' => [],
  3849. ['div' => ['class' => 'input text']],
  3850. 'label' => ['for' => 'foo'],
  3851. 'Foo',
  3852. '/label',
  3853. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3854. '*/div',
  3855. '/fieldset'
  3856. ];
  3857. $result = $this->Form->allControls(
  3858. ['foo' => ['type' => 'text']],
  3859. ['legend' => false]
  3860. );
  3861. $this->assertHtml($expected, $result);
  3862. }
  3863. /**
  3864. * testFormControlsBlacklist method
  3865. *
  3866. * @return void
  3867. */
  3868. public function testFormControlsBlacklist()
  3869. {
  3870. $this->Form->create($this->article);
  3871. $result = $this->Form->allControls([
  3872. 'id' => false
  3873. ]);
  3874. $expected = [
  3875. '<fieldset',
  3876. '<legend', 'New Article', '/legend',
  3877. ['div' => ['class' => 'input select required']],
  3878. '*/div',
  3879. ['div' => ['class' => 'input text required']],
  3880. '*/div',
  3881. ['div' => ['class' => 'input text']],
  3882. '*/div',
  3883. ['div' => ['class' => 'input text']],
  3884. '*/div',
  3885. '/fieldset',
  3886. ];
  3887. $this->assertHtml($expected, $result);
  3888. $this->Form->create($this->article);
  3889. $result = $this->Form->allControls([
  3890. 'id' => []
  3891. ]);
  3892. $expected = [
  3893. '<fieldset',
  3894. '<legend', 'New Article', '/legend',
  3895. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3896. ['div' => ['class' => 'input select required']],
  3897. '*/div',
  3898. ['div' => ['class' => 'input text required']],
  3899. '*/div',
  3900. ['div' => ['class' => 'input text']],
  3901. '*/div',
  3902. ['div' => ['class' => 'input text']],
  3903. '*/div',
  3904. '/fieldset',
  3905. ];
  3906. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3907. }
  3908. /**
  3909. * testSelectAsCheckbox method
  3910. *
  3911. * Test multi-select widget with checkbox formatting.
  3912. *
  3913. * @return void
  3914. */
  3915. public function testSelectAsCheckbox()
  3916. {
  3917. $result = $this->Form->select(
  3918. 'Model.multi_field',
  3919. ['first', 'second', 'third'],
  3920. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3921. );
  3922. $expected = [
  3923. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3924. ['div' => ['class' => 'checkbox']],
  3925. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3926. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3927. 'first',
  3928. '/label',
  3929. '/div',
  3930. ['div' => ['class' => 'checkbox']],
  3931. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3932. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3933. 'second',
  3934. '/label',
  3935. '/div',
  3936. ['div' => ['class' => 'checkbox']],
  3937. ['label' => ['for' => 'model-multi-field-2']],
  3938. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3939. 'third',
  3940. '/label',
  3941. '/div',
  3942. ];
  3943. $this->assertHtml($expected, $result);
  3944. $result = $this->Form->select(
  3945. 'Model.multi_field',
  3946. ['1/2' => 'half'],
  3947. ['multiple' => 'checkbox']
  3948. );
  3949. $expected = [
  3950. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3951. ['div' => ['class' => 'checkbox']],
  3952. ['label' => ['for' => 'model-multi-field-1-2']],
  3953. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3954. 'half',
  3955. '/label',
  3956. '/div',
  3957. ];
  3958. $this->assertHtml($expected, $result);
  3959. }
  3960. /**
  3961. * testLabel method
  3962. *
  3963. * Test label generation.
  3964. *
  3965. * @return void
  3966. */
  3967. public function testLabel()
  3968. {
  3969. $result = $this->Form->label('Person.name');
  3970. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3971. $this->assertHtml($expected, $result);
  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.first_name');
  3976. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3977. $this->assertHtml($expected, $result);
  3978. $result = $this->Form->label('Person.first_name', 'Your first name');
  3979. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3980. $this->assertHtml($expected, $result);
  3981. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3982. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3983. $this->assertHtml($expected, $result);
  3984. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3985. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3986. $this->assertHtml($expected, $result);
  3987. $result = $this->Form->label('Person.first_name', '');
  3988. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3989. $this->assertHtml($expected, $result);
  3990. $result = $this->Form->label('Person.2.name', '');
  3991. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3992. $this->assertHtml($expected, $result);
  3993. }
  3994. /**
  3995. * testLabelContainControl method
  3996. *
  3997. * Test that label() can accept an input with the correct template vars.
  3998. *
  3999. * @return void
  4000. */
  4001. public function testLabelContainControl()
  4002. {
  4003. $this->Form->setTemplates([
  4004. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4005. ]);
  4006. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  4007. 'input' => '<input type="checkbox" name="accept_tos"/>'
  4008. ]);
  4009. $expected = [
  4010. 'label' => ['for' => 'person-accept-terms'],
  4011. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  4012. 'Accept',
  4013. '/label',
  4014. ];
  4015. $this->assertHtml($expected, $result);
  4016. }
  4017. /**
  4018. * testTextbox method
  4019. *
  4020. * Test textbox element generation.
  4021. *
  4022. * @return void
  4023. */
  4024. public function testTextbox()
  4025. {
  4026. $result = $this->Form->text('Model.field');
  4027. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  4028. $this->assertHtml($expected, $result);
  4029. $result = $this->Form->text('Model.field', ['type' => 'password']);
  4030. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  4031. $this->assertHtml($expected, $result);
  4032. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  4033. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  4034. $this->assertHtml($expected, $result);
  4035. }
  4036. /**
  4037. * testTextBoxDataAndError method
  4038. *
  4039. * Test that text() hooks up with request data and error fields.
  4040. *
  4041. * @return void
  4042. */
  4043. public function testTextBoxDataAndError()
  4044. {
  4045. $this->article['errors'] = [
  4046. 'Contact' => ['text' => 'wrong']
  4047. ];
  4048. $this->View->setRequest($this->View->getRequest()
  4049. ->withData('Model.text', 'test <strong>HTML</strong> values')
  4050. ->withData('Contact.text', 'test'));
  4051. $this->Form->create($this->article);
  4052. $result = $this->Form->text('Model.text');
  4053. $expected = [
  4054. 'input' => [
  4055. 'type' => 'text',
  4056. 'name' => 'Model[text]',
  4057. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4058. ]
  4059. ];
  4060. $this->assertHtml($expected, $result);
  4061. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4062. $expected = [
  4063. 'input' => [
  4064. 'type' => 'text',
  4065. 'name' => 'Contact[text]',
  4066. 'value' => 'test',
  4067. 'id' => 'theID',
  4068. 'class' => 'form-error'
  4069. ]
  4070. ];
  4071. $this->assertHtml($expected, $result);
  4072. }
  4073. /**
  4074. * testDefaultValue method
  4075. *
  4076. * Test default value setting.
  4077. *
  4078. * @return void
  4079. */
  4080. public function testTextDefaultValue()
  4081. {
  4082. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  4083. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4084. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4085. $this->assertHtml($expected, $result);
  4086. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4087. $this->Form->create();
  4088. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4089. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4090. $this->assertHtml($expected, $result);
  4091. $Articles = $this->getTableLocator()->get('Articles');
  4092. $title = $Articles->getSchema()->getColumn('title');
  4093. $Articles->getSchema()->addColumn(
  4094. 'title',
  4095. ['default' => 'default title'] + $title
  4096. );
  4097. $entity = $Articles->newEntity();
  4098. $this->Form->create($entity);
  4099. // Get default value from schema
  4100. $result = $this->Form->text('title');
  4101. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4102. $this->assertHtml($expected, $result);
  4103. // Don't get value from schema
  4104. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4105. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4106. $this->assertHtml($expected, $result);
  4107. // Custom default value overrides default value from schema
  4108. $result = $this->Form->text('title', ['default' => 'override default']);
  4109. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4110. $this->assertHtml($expected, $result);
  4111. // Default value from schema is used only for new entities.
  4112. $entity->isNew(false);
  4113. $result = $this->Form->text('title');
  4114. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4115. $this->assertHtml($expected, $result);
  4116. }
  4117. /**
  4118. * testError method
  4119. *
  4120. * Test field error generation.
  4121. *
  4122. * @return void
  4123. */
  4124. public function testError()
  4125. {
  4126. $this->article['errors'] = [
  4127. 'Article' => ['field' => 'email']
  4128. ];
  4129. $this->Form->create($this->article);
  4130. $result = $this->Form->error('Article.field');
  4131. $expected = [
  4132. ['div' => ['class' => 'error-message']],
  4133. 'email',
  4134. '/div',
  4135. ];
  4136. $this->assertHtml($expected, $result);
  4137. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4138. $expected = [
  4139. ['div' => ['class' => 'error-message']],
  4140. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4141. '/div',
  4142. ];
  4143. $this->assertHtml($expected, $result);
  4144. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4145. $expected = [
  4146. ['div' => ['class' => 'error-message']],
  4147. '<strong', 'Badness!', '/strong',
  4148. '/div',
  4149. ];
  4150. $this->assertHtml($expected, $result);
  4151. }
  4152. /**
  4153. * testErrorRuleName method
  4154. *
  4155. * Test error translation can use rule names for translating.
  4156. *
  4157. * @return void
  4158. */
  4159. public function testErrorRuleName()
  4160. {
  4161. $this->article['errors'] = [
  4162. 'Article' => [
  4163. 'field' => ['email' => 'Your email was not good']
  4164. ]
  4165. ];
  4166. $this->Form->create($this->article);
  4167. $result = $this->Form->error('Article.field');
  4168. $expected = [
  4169. ['div' => ['class' => 'error-message']],
  4170. 'Your email was not good',
  4171. '/div',
  4172. ];
  4173. $this->assertHtml($expected, $result);
  4174. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4175. $expected = [
  4176. ['div' => ['class' => 'error-message']],
  4177. 'Email in use',
  4178. '/div',
  4179. ];
  4180. $this->assertHtml($expected, $result);
  4181. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4182. $expected = [
  4183. ['div' => ['class' => 'error-message']],
  4184. 'Email in use',
  4185. '/div',
  4186. ];
  4187. $this->assertHtml($expected, $result);
  4188. $result = $this->Form->error('Article.field', [
  4189. 'email' => 'Key is preferred',
  4190. 'Your email was not good' => 'Email in use'
  4191. ]);
  4192. $expected = [
  4193. ['div' => ['class' => 'error-message']],
  4194. 'Key is preferred',
  4195. '/div',
  4196. ];
  4197. $this->assertHtml($expected, $result);
  4198. }
  4199. /**
  4200. * testErrorMessages method
  4201. *
  4202. * Test error with nested lists.
  4203. *
  4204. * @return void
  4205. */
  4206. public function testErrorMessages()
  4207. {
  4208. $this->article['errors'] = [
  4209. 'Article' => ['field' => 'email']
  4210. ];
  4211. $this->Form->create($this->article);
  4212. $result = $this->Form->error('Article.field', [
  4213. 'email' => 'No good!'
  4214. ]);
  4215. $expected = [
  4216. 'div' => ['class' => 'error-message'],
  4217. 'No good!',
  4218. '/div'
  4219. ];
  4220. $this->assertHtml($expected, $result);
  4221. }
  4222. /**
  4223. * testErrorMultipleMessages method
  4224. *
  4225. * Test error() with multiple messages.
  4226. *
  4227. * @return void
  4228. */
  4229. public function testErrorMultipleMessages()
  4230. {
  4231. $this->article['errors'] = [
  4232. 'field' => ['notBlank', 'email', 'Something else']
  4233. ];
  4234. $this->Form->create($this->article);
  4235. $result = $this->Form->error('field', [
  4236. 'notBlank' => 'Cannot be empty',
  4237. 'email' => 'No good!'
  4238. ]);
  4239. $expected = [
  4240. 'div' => ['class' => 'error-message'],
  4241. 'ul' => [],
  4242. '<li', 'Cannot be empty', '/li',
  4243. '<li', 'No good!', '/li',
  4244. '<li', 'Something else', '/li',
  4245. '/ul',
  4246. '/div'
  4247. ];
  4248. $this->assertHtml($expected, $result);
  4249. }
  4250. /**
  4251. * testPassword method
  4252. *
  4253. * Test password element generation.
  4254. *
  4255. * @return void
  4256. */
  4257. public function testPassword()
  4258. {
  4259. $this->article['errors'] = [
  4260. 'Contact' => [
  4261. 'passwd' => 1
  4262. ]
  4263. ];
  4264. $this->Form->create($this->article);
  4265. $result = $this->Form->password('Contact.field');
  4266. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4267. $this->assertHtml($expected, $result);
  4268. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4269. $this->Form->create($this->article);
  4270. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4271. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4272. $this->assertHtml($expected, $result);
  4273. }
  4274. /**
  4275. * testRadio method
  4276. *
  4277. * Test radio element set generation.
  4278. *
  4279. * @return void
  4280. */
  4281. public function testRadio()
  4282. {
  4283. $result = $this->Form->radio('Model.field', ['option A']);
  4284. $expected = [
  4285. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4286. 'label' => ['for' => 'model-field-0'],
  4287. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4288. 'option A',
  4289. '/label'
  4290. ];
  4291. $this->assertHtml($expected, $result);
  4292. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4293. $this->assertHtml($expected, $result);
  4294. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4295. $expected = [
  4296. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4297. ['label' => ['for' => 'model-field-0']],
  4298. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4299. 'option A',
  4300. '/label',
  4301. ['label' => ['for' => 'model-field-1']],
  4302. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4303. 'option B',
  4304. '/label',
  4305. ];
  4306. $this->assertHtml($expected, $result);
  4307. $result = $this->Form->radio(
  4308. 'Employee.gender',
  4309. ['male' => 'Male', 'female' => 'Female'],
  4310. ['form' => 'my-form']
  4311. );
  4312. $expected = [
  4313. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4314. ['label' => ['for' => 'employee-gender-male']],
  4315. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4316. 'Male',
  4317. '/label',
  4318. ['label' => ['for' => 'employee-gender-female']],
  4319. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4320. 'Female',
  4321. '/label',
  4322. ];
  4323. $this->assertHtml($expected, $result);
  4324. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4325. $expected = [
  4326. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4327. ['label' => ['for' => 'model-custom-0']],
  4328. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4329. 'option A',
  4330. '/label',
  4331. ['label' => ['for' => 'model-custom-1']],
  4332. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4333. 'option B',
  4334. '/label',
  4335. ];
  4336. $this->assertHtml($expected, $result);
  4337. $result = $this->Form->radio(
  4338. 'Employee.gender',
  4339. [
  4340. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4341. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4342. ]
  4343. );
  4344. $expected = [
  4345. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4346. ['label' => ['for' => 'employee-gender-male']],
  4347. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4348. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4349. 'Male',
  4350. '/label',
  4351. ['label' => ['for' => 'employee-gender-female']],
  4352. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4353. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4354. 'Female',
  4355. '/label',
  4356. ];
  4357. $this->assertHtml($expected, $result);
  4358. }
  4359. /**
  4360. * Test radio with complex options and empty disabled data.
  4361. *
  4362. * @return void
  4363. */
  4364. public function testRadioComplexDisabled()
  4365. {
  4366. $options = [
  4367. ['value' => 'r', 'text' => 'red'],
  4368. ['value' => 'b', 'text' => 'blue'],
  4369. ];
  4370. $attrs = ['disabled' => []];
  4371. $result = $this->Form->radio('Model.field', $options, $attrs);
  4372. $expected = [
  4373. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4374. ['label' => ['for' => 'model-field-r']],
  4375. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4376. 'red',
  4377. '/label',
  4378. ['label' => ['for' => 'model-field-b']],
  4379. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4380. 'blue',
  4381. '/label',
  4382. ];
  4383. $this->assertHtml($expected, $result);
  4384. $attrs = ['disabled' => ['r']];
  4385. $result = $this->Form->radio('Model.field', $options, $attrs);
  4386. $this->assertContains('disabled="disabled"', $result);
  4387. }
  4388. /**
  4389. * testRadioDefaultValue method
  4390. *
  4391. * Test default value setting on radio() method.
  4392. *
  4393. * @return void
  4394. */
  4395. public function testRadioDefaultValue()
  4396. {
  4397. $Articles = $this->getTableLocator()->get('Articles');
  4398. $title = $Articles->getSchema()->getColumn('title');
  4399. $Articles->getSchema()->addColumn(
  4400. 'title',
  4401. ['default' => '1'] + $title
  4402. );
  4403. $this->Form->create($Articles->newEntity());
  4404. $result = $this->Form->radio('title', ['option A', 'option B']);
  4405. $expected = [
  4406. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4407. ['label' => ['for' => 'title-0']],
  4408. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4409. 'option A',
  4410. '/label',
  4411. ['label' => ['for' => 'title-1']],
  4412. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4413. 'option B',
  4414. '/label',
  4415. ];
  4416. $this->assertHtml($expected, $result);
  4417. }
  4418. /**
  4419. * Test setting a hiddenField value on radio buttons.
  4420. *
  4421. * @return void
  4422. */
  4423. public function testRadioHiddenFieldValue()
  4424. {
  4425. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4426. $expected = [
  4427. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4428. 'label' => ['for' => 'title-0'],
  4429. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4430. 'option A',
  4431. '/label',
  4432. ];
  4433. $this->assertHtml($expected, $result);
  4434. }
  4435. /**
  4436. * testControlRadio method
  4437. *
  4438. * Test that input works with radio types.
  4439. *
  4440. * @return void
  4441. */
  4442. public function testControlRadio()
  4443. {
  4444. $result = $this->Form->control('test', [
  4445. 'type' => 'radio',
  4446. 'options' => ['A', 'B'],
  4447. ]);
  4448. $expected = [
  4449. ['div' => ['class' => 'input radio']],
  4450. '<label',
  4451. 'Test',
  4452. '/label',
  4453. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4454. ['label' => ['for' => 'test-0']],
  4455. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4456. 'A',
  4457. '/label',
  4458. ['label' => ['for' => 'test-1']],
  4459. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4460. 'B',
  4461. '/label',
  4462. '/div',
  4463. ];
  4464. $this->assertHtml($expected, $result);
  4465. $result = $this->Form->control('test', [
  4466. 'type' => 'radio',
  4467. 'options' => ['A', 'B'],
  4468. 'value' => '0'
  4469. ]);
  4470. $expected = [
  4471. ['div' => ['class' => 'input radio']],
  4472. '<label',
  4473. 'Test',
  4474. '/label',
  4475. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4476. ['label' => ['for' => 'test-0']],
  4477. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4478. 'A',
  4479. '/label',
  4480. ['label' => ['for' => 'test-1']],
  4481. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4482. 'B',
  4483. '/label',
  4484. '/div',
  4485. ];
  4486. $this->assertHtml($expected, $result);
  4487. $result = $this->Form->control('test', [
  4488. 'type' => 'radio',
  4489. 'options' => ['A', 'B'],
  4490. 'label' => false
  4491. ]);
  4492. $expected = [
  4493. ['div' => ['class' => 'input radio']],
  4494. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4495. ['label' => ['for' => 'test-0']],
  4496. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4497. 'A',
  4498. '/label',
  4499. ['label' => ['for' => 'test-1']],
  4500. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4501. 'B',
  4502. '/label',
  4503. '/div',
  4504. ];
  4505. $this->assertHtml($expected, $result);
  4506. }
  4507. /**
  4508. * testRadioNoLabel method
  4509. *
  4510. * Test that radio() works with label = false.
  4511. *
  4512. * @return void
  4513. */
  4514. public function testRadioNoLabel()
  4515. {
  4516. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4517. $expected = [
  4518. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4519. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4520. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4521. ];
  4522. $this->assertHtml($expected, $result);
  4523. }
  4524. /**
  4525. * testRadioControlInsideLabel method
  4526. *
  4527. * Test generating radio input inside label ala twitter bootstrap.
  4528. *
  4529. * @return void
  4530. */
  4531. public function testRadioControlInsideLabel()
  4532. {
  4533. $this->Form->setTemplates([
  4534. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4535. 'radioWrapper' => '{{label}}'
  4536. ]);
  4537. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4538. //@codingStandardsIgnoreStart
  4539. $expected = [
  4540. ['input' => [
  4541. 'type' => 'hidden',
  4542. 'name' => 'Model[field]',
  4543. 'value' => ''
  4544. ]],
  4545. ['label' => ['for' => 'model-field-0']],
  4546. ['input' => [
  4547. 'type' => 'radio',
  4548. 'name' => 'Model[field]',
  4549. 'value' => '0',
  4550. 'id' => 'model-field-0'
  4551. ]],
  4552. 'option A',
  4553. '/label',
  4554. ['label' => ['for' => 'model-field-1']],
  4555. ['input' => [
  4556. 'type' => 'radio',
  4557. 'name' => 'Model[field]',
  4558. 'value' => '1',
  4559. 'id' => 'model-field-1'
  4560. ]],
  4561. 'option B',
  4562. '/label',
  4563. ];
  4564. //@codingStandardsIgnoreEnd
  4565. $this->assertHtml($expected, $result);
  4566. }
  4567. /**
  4568. * testRadioHiddenControlDisabling method
  4569. *
  4570. * Test disabling the hidden input for radio buttons.
  4571. *
  4572. * @return void
  4573. */
  4574. public function testRadioHiddenControlDisabling()
  4575. {
  4576. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4577. $expected = [
  4578. 'label' => ['for' => 'model-1-field-0'],
  4579. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4580. 'option A',
  4581. '/label'
  4582. ];
  4583. $this->assertHtml($expected, $result);
  4584. }
  4585. /**
  4586. * testRadioOutOfRange method
  4587. *
  4588. * Test radio element set generation.
  4589. *
  4590. * @return void
  4591. */
  4592. public function testRadioOutOfRange()
  4593. {
  4594. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4595. $expected = [
  4596. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4597. 'label' => ['for' => 'model-field-v'],
  4598. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4599. 'value',
  4600. '/label'
  4601. ];
  4602. $this->assertHtml($expected, $result);
  4603. }
  4604. /**
  4605. * testSelect method
  4606. *
  4607. * Test select element generation.
  4608. *
  4609. * @return void
  4610. */
  4611. public function testSelect()
  4612. {
  4613. $result = $this->Form->select('Model.field', []);
  4614. $expected = [
  4615. 'select' => ['name' => 'Model[field]'],
  4616. '/select'
  4617. ];
  4618. $this->assertHtml($expected, $result);
  4619. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4620. $this->Form->create();
  4621. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4622. $expected = [
  4623. 'select' => ['name' => 'Model[field]'],
  4624. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4625. 'good',
  4626. '/option',
  4627. ['option' => ['value' => 'other']],
  4628. 'bad',
  4629. '/option',
  4630. '/select'
  4631. ];
  4632. $this->assertHtml($expected, $result);
  4633. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4634. $this->assertHtml($expected, $result);
  4635. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4636. $this->Form->create();
  4637. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4638. $expected = [
  4639. 'select' => ['name' => 'Model[field]'],
  4640. ['option' => ['value' => 'value']],
  4641. 'good',
  4642. '/option',
  4643. ['option' => ['value' => 'other']],
  4644. 'bad',
  4645. '/option',
  4646. '/select'
  4647. ];
  4648. $this->assertHtml($expected, $result);
  4649. $options = [
  4650. ['value' => 'first', 'text' => 'First'],
  4651. ['value' => 'first', 'text' => 'Another First'],
  4652. ];
  4653. $result = $this->Form->select(
  4654. 'Model.field',
  4655. $options,
  4656. ['escape' => false, 'empty' => false]
  4657. );
  4658. $expected = [
  4659. 'select' => ['name' => 'Model[field]'],
  4660. ['option' => ['value' => 'first']],
  4661. 'First',
  4662. '/option',
  4663. ['option' => ['value' => 'first']],
  4664. 'Another First',
  4665. '/option',
  4666. '/select'
  4667. ];
  4668. $this->assertHtml($expected, $result);
  4669. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4670. $this->Form->create();
  4671. $result = $this->Form->select(
  4672. 'Model.contact_id',
  4673. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4674. ['escape' => false, 'empty' => 'pick something']
  4675. );
  4676. $expected = [
  4677. 'select' => ['name' => 'Model[contact_id]'],
  4678. ['option' => ['value' => '']], 'pick something', '/option',
  4679. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4680. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4681. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4682. '/select'
  4683. ];
  4684. $this->assertHtml($expected, $result);
  4685. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4686. $this->Form->create();
  4687. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4688. $expected = [
  4689. 'select' => ['name' => 'Model[field]'],
  4690. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4691. ['option' => ['value' => '1']], 'Yes', '/option',
  4692. '/select'
  4693. ];
  4694. $this->assertHtml($expected, $result);
  4695. }
  4696. /**
  4697. * testSelectEscapeHtml method
  4698. *
  4699. * Test that select() escapes HTML.
  4700. *
  4701. * @return void
  4702. */
  4703. public function testSelectEscapeHtml()
  4704. {
  4705. $result = $this->Form->select(
  4706. 'Model.field',
  4707. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4708. ['empty' => false]
  4709. );
  4710. $expected = [
  4711. 'select' => ['name' => 'Model[field]'],
  4712. ['option' => ['value' => 'first']],
  4713. 'first &quot;html&quot; &lt;chars&gt;',
  4714. '/option',
  4715. ['option' => ['value' => 'second']],
  4716. 'value',
  4717. '/option',
  4718. '/select'
  4719. ];
  4720. $this->assertHtml($expected, $result);
  4721. $result = $this->Form->select(
  4722. 'Model.field',
  4723. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4724. ['escape' => false, 'empty' => false]
  4725. );
  4726. $expected = [
  4727. 'select' => ['name' => 'Model[field]'],
  4728. ['option' => ['value' => 'first']],
  4729. 'first "html" <chars>',
  4730. '/option',
  4731. ['option' => ['value' => 'second']],
  4732. 'value',
  4733. '/option',
  4734. '/select'
  4735. ];
  4736. $this->assertHtml($expected, $result);
  4737. }
  4738. /**
  4739. * testSelectRequired method
  4740. *
  4741. * Test select() with required and disabled attributes.
  4742. *
  4743. * @return void
  4744. */
  4745. public function testSelectRequired()
  4746. {
  4747. $this->article['required'] = [
  4748. 'user_id' => true
  4749. ];
  4750. $this->Form->create($this->article);
  4751. $result = $this->Form->select('user_id', ['option A']);
  4752. $expected = [
  4753. 'select' => [
  4754. 'name' => 'user_id',
  4755. 'required' => 'required'
  4756. ],
  4757. ['option' => ['value' => '0']], 'option A', '/option',
  4758. '/select'
  4759. ];
  4760. $this->assertHtml($expected, $result);
  4761. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4762. $expected = [
  4763. 'select' => [
  4764. 'name' => 'user_id',
  4765. 'disabled' => 'disabled'
  4766. ],
  4767. ['option' => ['value' => '0']], 'option A', '/option',
  4768. '/select'
  4769. ];
  4770. $this->assertHtml($expected, $result);
  4771. }
  4772. /**
  4773. * testNestedSelect method
  4774. *
  4775. * Test select element generation with optgroups.
  4776. *
  4777. * @return void
  4778. */
  4779. public function testNestedSelect()
  4780. {
  4781. $result = $this->Form->select(
  4782. 'Model.field',
  4783. [1 => 'One', 2 => 'Two', 'Three' => [
  4784. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4785. ]],
  4786. ['empty' => false]
  4787. );
  4788. $expected = [
  4789. 'select' => ['name' => 'Model[field]'],
  4790. ['option' => ['value' => 1]],
  4791. 'One',
  4792. '/option',
  4793. ['option' => ['value' => 2]],
  4794. 'Two',
  4795. '/option',
  4796. ['optgroup' => ['label' => 'Three']],
  4797. ['option' => ['value' => 3]],
  4798. 'Three',
  4799. '/option',
  4800. ['option' => ['value' => 4]],
  4801. 'Four',
  4802. '/option',
  4803. ['option' => ['value' => 5]],
  4804. 'Five',
  4805. '/option',
  4806. '/optgroup',
  4807. '/select'
  4808. ];
  4809. $this->assertHtml($expected, $result);
  4810. }
  4811. /**
  4812. * testSelectMultiple method
  4813. *
  4814. * Test generation of multiple select elements.
  4815. *
  4816. * @return void
  4817. */
  4818. public function testSelectMultiple()
  4819. {
  4820. $options = ['first', 'second', 'third'];
  4821. $result = $this->Form->select(
  4822. 'Model.multi_field',
  4823. $options,
  4824. ['form' => 'my-form', 'multiple' => true]
  4825. );
  4826. $expected = [
  4827. 'input' => [
  4828. 'type' => 'hidden',
  4829. 'name' => 'Model[multi_field]',
  4830. 'value' => '',
  4831. 'form' => 'my-form',
  4832. ],
  4833. 'select' => [
  4834. 'name' => 'Model[multi_field][]',
  4835. 'multiple' => 'multiple',
  4836. 'form' => 'my-form',
  4837. ],
  4838. ['option' => ['value' => '0']],
  4839. 'first',
  4840. '/option',
  4841. ['option' => ['value' => '1']],
  4842. 'second',
  4843. '/option',
  4844. ['option' => ['value' => '2']],
  4845. 'third',
  4846. '/option',
  4847. '/select'
  4848. ];
  4849. $this->assertHtml($expected, $result);
  4850. $result = $this->Form->select(
  4851. 'Model.multi_field',
  4852. $options,
  4853. ['multiple' => 'multiple', 'form' => 'my-form']
  4854. );
  4855. $this->assertHtml($expected, $result);
  4856. $result = $this->Form->select(
  4857. 'Model.multi_field',
  4858. $options,
  4859. ['form' => 'my-form', 'multiple' => false]
  4860. );
  4861. $this->assertNotContains('multiple', $result);
  4862. }
  4863. /**
  4864. * testCheckboxZeroValue method
  4865. *
  4866. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4867. *
  4868. * @return void
  4869. */
  4870. public function testCheckboxZeroValue()
  4871. {
  4872. $result = $this->Form->control('User.get_spam', [
  4873. 'type' => 'checkbox',
  4874. 'value' => '0',
  4875. 'hiddenField' => '1',
  4876. ]);
  4877. $expected = [
  4878. 'div' => ['class' => 'input checkbox'],
  4879. 'label' => ['for' => 'user-get-spam'],
  4880. ['input' => [
  4881. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4882. 'value' => '1'
  4883. ]],
  4884. ['input' => [
  4885. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4886. 'value' => '0', 'id' => 'user-get-spam'
  4887. ]],
  4888. 'Get Spam',
  4889. '/label',
  4890. '/div'
  4891. ];
  4892. $this->assertHtml($expected, $result);
  4893. }
  4894. /**
  4895. * testHabtmSelectBox method
  4896. *
  4897. * Test generation of habtm select boxes.
  4898. *
  4899. * @return void
  4900. */
  4901. public function testHabtmSelectBox()
  4902. {
  4903. $this->loadFixtures('Articles');
  4904. $options = [
  4905. 1 => 'blue',
  4906. 2 => 'red',
  4907. 3 => 'green'
  4908. ];
  4909. $tags = [
  4910. new Entity(['id' => 1, 'name' => 'blue']),
  4911. new Entity(['id' => 3, 'name' => 'green'])
  4912. ];
  4913. $article = new Article(['tags' => $tags]);
  4914. $this->Form->create($article);
  4915. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4916. $expected = [
  4917. 'div' => ['class' => 'input select'],
  4918. 'label' => ['for' => 'tags-ids'],
  4919. 'Tags',
  4920. '/label',
  4921. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4922. 'select' => [
  4923. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4924. 'multiple' => 'multiple'
  4925. ],
  4926. ['option' => ['value' => '1', 'selected' => 'selected']],
  4927. 'blue',
  4928. '/option',
  4929. ['option' => ['value' => '2']],
  4930. 'red',
  4931. '/option',
  4932. ['option' => ['value' => '3', 'selected' => 'selected']],
  4933. 'green',
  4934. '/option',
  4935. '/select',
  4936. '/div'
  4937. ];
  4938. $this->assertHtml($expected, $result);
  4939. // make sure only 50 is selected, and not 50f5c0cf
  4940. $options = [
  4941. '1' => 'blue',
  4942. '50f5c0cf' => 'red',
  4943. '50' => 'green'
  4944. ];
  4945. $tags = [
  4946. new Entity(['id' => 1, 'name' => 'blue']),
  4947. new Entity(['id' => 50, 'name' => 'green'])
  4948. ];
  4949. $article = new Article(['tags' => $tags]);
  4950. $this->Form->create($article);
  4951. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4952. $expected = [
  4953. 'div' => ['class' => 'input select'],
  4954. 'label' => ['for' => 'tags-ids'],
  4955. 'Tags',
  4956. '/label',
  4957. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4958. 'select' => [
  4959. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4960. 'multiple' => 'multiple'
  4961. ],
  4962. ['option' => ['value' => '1', 'selected' => 'selected']],
  4963. 'blue',
  4964. '/option',
  4965. ['option' => ['value' => '50f5c0cf']],
  4966. 'red',
  4967. '/option',
  4968. ['option' => ['value' => '50', 'selected' => 'selected']],
  4969. 'green',
  4970. '/option',
  4971. '/select',
  4972. '/div'
  4973. ];
  4974. $this->assertHtml($expected, $result);
  4975. $spacecraft = [
  4976. 1 => 'Orion',
  4977. 2 => 'Helios'
  4978. ];
  4979. $this->View->viewVars['spacecraft'] = $spacecraft;
  4980. $this->Form->create();
  4981. $result = $this->Form->control('spacecraft._ids');
  4982. $expected = [
  4983. 'div' => ['class' => 'input select'],
  4984. 'label' => ['for' => 'spacecraft-ids'],
  4985. 'Spacecraft',
  4986. '/label',
  4987. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4988. 'select' => [
  4989. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4990. 'multiple' => 'multiple'
  4991. ],
  4992. ['option' => ['value' => '1']],
  4993. 'Orion',
  4994. '/option',
  4995. ['option' => ['value' => '2']],
  4996. 'Helios',
  4997. '/option',
  4998. '/select',
  4999. '/div'
  5000. ];
  5001. $this->assertHtml($expected, $result);
  5002. }
  5003. /**
  5004. * testErrorsForBelongsToManySelect method
  5005. *
  5006. * Tests that errors for belongsToMany select fields are being
  5007. * picked up properly.
  5008. *
  5009. * @return void
  5010. */
  5011. public function testErrorsForBelongsToManySelect()
  5012. {
  5013. $spacecraft = [
  5014. 1 => 'Orion',
  5015. 2 => 'Helios'
  5016. ];
  5017. $this->View->viewVars['spacecraft'] = $spacecraft;
  5018. $article = new Article();
  5019. $article->setError('spacecraft', ['Invalid']);
  5020. $this->Form->create($article);
  5021. $result = $this->Form->control('spacecraft._ids');
  5022. $expected = [
  5023. ['div' => ['class' => 'input select error']],
  5024. 'label' => ['for' => 'spacecraft-ids'],
  5025. 'Spacecraft',
  5026. '/label',
  5027. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  5028. 'select' => [
  5029. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  5030. 'multiple' => 'multiple'
  5031. ],
  5032. ['option' => ['value' => '1']],
  5033. 'Orion',
  5034. '/option',
  5035. ['option' => ['value' => '2']],
  5036. 'Helios',
  5037. '/option',
  5038. '/select',
  5039. ['div' => ['class' => 'error-message']],
  5040. 'Invalid',
  5041. '/div',
  5042. '/div'
  5043. ];
  5044. $this->assertHtml($expected, $result);
  5045. }
  5046. /**
  5047. * testSelectMultipleCheckboxes method
  5048. *
  5049. * Test generation of multi select elements in checkbox format.
  5050. *
  5051. * @return void
  5052. */
  5053. public function testSelectMultipleCheckboxes()
  5054. {
  5055. $result = $this->Form->select(
  5056. 'Model.multi_field',
  5057. ['first', 'second', 'third'],
  5058. ['multiple' => 'checkbox']
  5059. );
  5060. $expected = [
  5061. 'input' => [
  5062. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5063. ],
  5064. ['div' => ['class' => 'checkbox']],
  5065. ['label' => ['for' => 'model-multi-field-0']],
  5066. ['input' => [
  5067. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5068. 'value' => '0', 'id' => 'model-multi-field-0'
  5069. ]],
  5070. 'first',
  5071. '/label',
  5072. '/div',
  5073. ['div' => ['class' => 'checkbox']],
  5074. ['label' => ['for' => 'model-multi-field-1']],
  5075. ['input' => [
  5076. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5077. 'value' => '1', 'id' => 'model-multi-field-1'
  5078. ]],
  5079. 'second',
  5080. '/label',
  5081. '/div',
  5082. ['div' => ['class' => 'checkbox']],
  5083. ['label' => ['for' => 'model-multi-field-2']],
  5084. ['input' => [
  5085. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5086. 'value' => '2', 'id' => 'model-multi-field-2'
  5087. ]],
  5088. 'third',
  5089. '/label',
  5090. '/div'
  5091. ];
  5092. $this->assertHtml($expected, $result);
  5093. $result = $this->Form->select(
  5094. 'Model.multi_field',
  5095. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5096. ['multiple' => 'checkbox']
  5097. );
  5098. $expected = [
  5099. 'input' => [
  5100. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5101. ],
  5102. ['div' => ['class' => 'checkbox']],
  5103. ['label' => ['for' => 'model-multi-field-a+']],
  5104. ['input' => [
  5105. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5106. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  5107. ]],
  5108. 'first',
  5109. '/label',
  5110. '/div',
  5111. ['div' => ['class' => 'checkbox']],
  5112. ['label' => ['for' => 'model-multi-field-a++']],
  5113. ['input' => [
  5114. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5115. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  5116. ]],
  5117. 'second',
  5118. '/label',
  5119. '/div',
  5120. ['div' => ['class' => 'checkbox']],
  5121. ['label' => ['for' => 'model-multi-field-a+++']],
  5122. ['input' => [
  5123. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5124. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  5125. ]],
  5126. 'third',
  5127. '/label',
  5128. '/div'
  5129. ];
  5130. $this->assertHtml($expected, $result);
  5131. $result = $this->Form->select(
  5132. 'Model.multi_field',
  5133. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5134. ['multiple' => 'checkbox']
  5135. );
  5136. $expected = [
  5137. 'input' => [
  5138. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5139. ],
  5140. ['div' => ['class' => 'checkbox']],
  5141. ['label' => ['for' => 'model-multi-field-a-b']],
  5142. ['input' => [
  5143. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5144. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5145. ]],
  5146. 'first',
  5147. '/label',
  5148. '/div',
  5149. ['div' => ['class' => 'checkbox']],
  5150. ['label' => ['for' => 'model-multi-field-a-b1']],
  5151. ['input' => [
  5152. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5153. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5154. ]],
  5155. 'second',
  5156. '/label',
  5157. '/div',
  5158. ['div' => ['class' => 'checkbox']],
  5159. ['label' => ['for' => 'model-multi-field-a-b2']],
  5160. ['input' => [
  5161. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5162. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5163. ]],
  5164. 'third',
  5165. '/label',
  5166. '/div'
  5167. ];
  5168. $this->assertHtml($expected, $result);
  5169. }
  5170. /**
  5171. * testSelectMultipleCheckboxRequestData method
  5172. *
  5173. * Ensure that multiCheckbox reads from the request data.
  5174. *
  5175. * @return void
  5176. */
  5177. public function testSelectMultipleCheckboxRequestData()
  5178. {
  5179. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5180. $result = $this->Form->select(
  5181. 'Model.tags',
  5182. ['1' => 'first', 'Array' => 'Array'],
  5183. ['multiple' => 'checkbox']
  5184. );
  5185. $expected = [
  5186. 'input' => [
  5187. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5188. ],
  5189. ['div' => ['class' => 'checkbox']],
  5190. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5191. ['input' => [
  5192. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5193. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5194. ]],
  5195. 'first',
  5196. '/label',
  5197. '/div',
  5198. ['div' => ['class' => 'checkbox']],
  5199. ['label' => ['for' => 'model-tags-array']],
  5200. ['input' => [
  5201. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5202. 'value' => 'Array', 'id' => 'model-tags-array'
  5203. ]],
  5204. 'Array',
  5205. '/label',
  5206. '/div'
  5207. ];
  5208. $this->assertHtml($expected, $result);
  5209. }
  5210. /**
  5211. * testSelectMultipleCheckboxSecurity method
  5212. *
  5213. * Checks the security hash array generated for multiple-input checkbox elements.
  5214. *
  5215. * @return void
  5216. */
  5217. public function testSelectMultipleCheckboxSecurity()
  5218. {
  5219. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  5220. $this->assertEquals([], $this->Form->fields);
  5221. $this->Form->select(
  5222. 'Model.multi_field',
  5223. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5224. ['multiple' => 'checkbox']
  5225. );
  5226. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5227. $result = $this->Form->secure($this->Form->fields);
  5228. $key = '8af36fb34e6f2ef8ba0eb473bb4365ec232f3fe5%3A';
  5229. $this->assertContains('"' . $key . '"', $result);
  5230. }
  5231. /**
  5232. * testSelectMultipleSecureWithNoOptions method
  5233. *
  5234. * Multiple select elements should always be secured as they always participate
  5235. * in the POST data.
  5236. *
  5237. * @return void
  5238. */
  5239. public function testSelectMultipleSecureWithNoOptions()
  5240. {
  5241. $this->assertEquals([], $this->Form->fields);
  5242. $this->Form->select(
  5243. 'Model.select',
  5244. [],
  5245. ['multiple' => true]
  5246. );
  5247. $this->assertEquals(['Model.select'], $this->Form->fields);
  5248. }
  5249. /**
  5250. * testSelectNoSecureWithNoOptions method
  5251. *
  5252. * When a select box has no options it should not be added to the fields list
  5253. * as it always fail post validation.
  5254. *
  5255. * @return void
  5256. */
  5257. public function testSelectNoSecureWithNoOptions()
  5258. {
  5259. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testkey'));
  5260. $this->assertEquals([], $this->Form->fields);
  5261. $this->Form->select(
  5262. 'Model.select',
  5263. []
  5264. );
  5265. $this->assertEquals([], $this->Form->fields);
  5266. $this->Form->select(
  5267. 'Model.user_id',
  5268. [],
  5269. ['empty' => true]
  5270. );
  5271. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5272. }
  5273. /**
  5274. * testControlMultipleCheckboxes method
  5275. *
  5276. * Test control() resulting in multi select elements being generated.
  5277. *
  5278. * @return void
  5279. */
  5280. public function testControlMultipleCheckboxes()
  5281. {
  5282. $result = $this->Form->control('Model.multi_field', [
  5283. 'options' => ['first', 'second', 'third'],
  5284. 'multiple' => 'checkbox'
  5285. ]);
  5286. $expected = [
  5287. ['div' => ['class' => 'input select']],
  5288. ['label' => ['for' => 'model-multi-field']],
  5289. 'Multi Field',
  5290. '/label',
  5291. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5292. ['div' => ['class' => 'checkbox']],
  5293. ['label' => ['for' => 'model-multi-field-0']],
  5294. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5295. 'first',
  5296. '/label',
  5297. '/div',
  5298. ['div' => ['class' => 'checkbox']],
  5299. ['label' => ['for' => 'model-multi-field-1']],
  5300. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5301. 'second',
  5302. '/label',
  5303. '/div',
  5304. ['div' => ['class' => 'checkbox']],
  5305. ['label' => ['for' => 'model-multi-field-2']],
  5306. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5307. 'third',
  5308. '/label',
  5309. '/div',
  5310. '/div'
  5311. ];
  5312. $this->assertHtml($expected, $result);
  5313. $result = $this->Form->control('Model.multi_field', [
  5314. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5315. 'multiple' => 'checkbox'
  5316. ]);
  5317. $expected = [
  5318. ['div' => ['class' => 'input select']],
  5319. ['label' => ['for' => 'model-multi-field']],
  5320. 'Multi Field',
  5321. '/label',
  5322. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5323. ['div' => ['class' => 'checkbox']],
  5324. ['label' => ['for' => 'model-multi-field-a']],
  5325. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5326. 'first',
  5327. '/label',
  5328. '/div',
  5329. ['div' => ['class' => 'checkbox']],
  5330. ['label' => ['for' => 'model-multi-field-b']],
  5331. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5332. 'second',
  5333. '/label',
  5334. '/div',
  5335. ['div' => ['class' => 'checkbox']],
  5336. ['label' => ['for' => 'model-multi-field-c']],
  5337. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5338. 'third',
  5339. '/label',
  5340. '/div',
  5341. '/div'
  5342. ];
  5343. $this->assertHtml($expected, $result);
  5344. }
  5345. /**
  5346. * testSelectHiddenFieldOmission method
  5347. *
  5348. * Test that select() with 'hiddenField' => false omits the hidden field.
  5349. *
  5350. * @return void
  5351. */
  5352. public function testSelectHiddenFieldOmission()
  5353. {
  5354. $result = $this->Form->select(
  5355. 'Model.multi_field',
  5356. ['first', 'second'],
  5357. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5358. );
  5359. $this->assertNotContains('type="hidden"', $result);
  5360. }
  5361. /**
  5362. * testSelectCheckboxMultipleOverrideName method
  5363. *
  5364. * Test that select() with multiple = checkbox works with overriding name attribute.
  5365. *
  5366. * @return void
  5367. */
  5368. public function testSelectCheckboxMultipleOverrideName()
  5369. {
  5370. $result = $this->Form->select('category', ['1', '2'], [
  5371. 'multiple' => 'checkbox',
  5372. 'name' => 'fish',
  5373. ]);
  5374. $expected = [
  5375. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5376. ['div' => ['class' => 'checkbox']],
  5377. ['label' => ['for' => 'fish-0']],
  5378. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5379. '1',
  5380. '/label',
  5381. '/div',
  5382. ['div' => ['class' => 'checkbox']],
  5383. ['label' => ['for' => 'fish-1']],
  5384. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5385. '2',
  5386. '/label',
  5387. '/div'
  5388. ];
  5389. $this->assertHtml($expected, $result);
  5390. $result = $this->Form->multiCheckbox(
  5391. 'category',
  5392. new Collection(['1', '2']),
  5393. ['name' => 'fish']
  5394. );
  5395. $this->assertHtml($expected, $result);
  5396. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5397. 'name' => 'fish',
  5398. ]);
  5399. $this->assertHtml($expected, $result);
  5400. }
  5401. /**
  5402. * testControlMultiCheckbox method
  5403. *
  5404. * Test that control() works with multicheckbox.
  5405. *
  5406. * @return void
  5407. */
  5408. public function testControlMultiCheckbox()
  5409. {
  5410. $result = $this->Form->control('category', [
  5411. 'type' => 'multicheckbox',
  5412. 'options' => ['1', '2'],
  5413. ]);
  5414. $expected = [
  5415. ['div' => ['class' => 'input multicheckbox']],
  5416. '<label',
  5417. 'Category',
  5418. '/label',
  5419. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5420. ['div' => ['class' => 'checkbox']],
  5421. ['label' => ['for' => 'category-0']],
  5422. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5423. '1',
  5424. '/label',
  5425. '/div',
  5426. ['div' => ['class' => 'checkbox']],
  5427. ['label' => ['for' => 'category-1']],
  5428. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5429. '2',
  5430. '/label',
  5431. '/div',
  5432. '/div',
  5433. ];
  5434. $this->assertHtml($expected, $result);
  5435. }
  5436. /**
  5437. * testCheckbox method
  5438. *
  5439. * Test generation of checkboxes.
  5440. *
  5441. * @return void
  5442. */
  5443. public function testCheckbox()
  5444. {
  5445. $result = $this->Form->checkbox('Model.field');
  5446. $expected = [
  5447. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5448. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5449. ];
  5450. $this->assertHtml($expected, $result);
  5451. $result = $this->Form->checkbox('Model.field', [
  5452. 'id' => 'theID',
  5453. 'value' => 'myvalue',
  5454. 'form' => 'my-form',
  5455. ]);
  5456. $expected = [
  5457. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5458. ['input' => [
  5459. 'type' => 'checkbox', 'name' => 'Model[field]',
  5460. 'value' => 'myvalue', 'id' => 'theID',
  5461. 'form' => 'my-form',
  5462. ]]
  5463. ];
  5464. $this->assertHtml($expected, $result);
  5465. }
  5466. /**
  5467. * testCheckboxDefaultValue method
  5468. *
  5469. * Test default value setting on checkbox() method.
  5470. *
  5471. * @return void
  5472. */
  5473. public function testCheckboxDefaultValue()
  5474. {
  5475. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5476. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5477. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5478. $this->assertHtml($expected, $result);
  5479. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5480. $this->Form->create();
  5481. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5482. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5483. $this->assertHtml($expected, $result);
  5484. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5485. $this->Form->create();
  5486. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5487. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5488. $this->assertHtml($expected, $result);
  5489. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5490. $this->Form->create();
  5491. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5492. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5493. $this->assertHtml($expected, $result);
  5494. $Articles = $this->getTableLocator()->get('Articles');
  5495. $Articles->getSchema()->addColumn(
  5496. 'published',
  5497. ['type' => 'boolean', 'null' => false, 'default' => true]
  5498. );
  5499. $this->Form->create($Articles->newEntity());
  5500. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5501. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5502. $this->assertHtml($expected, $result);
  5503. }
  5504. /**
  5505. * testCheckboxCheckedAndError method
  5506. *
  5507. * Test checkbox being checked or having errors.
  5508. *
  5509. * @return void
  5510. */
  5511. public function testCheckboxCheckedAndError()
  5512. {
  5513. $this->article['errors'] = [
  5514. 'published' => true
  5515. ];
  5516. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5517. $this->Form->create($this->article);
  5518. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5519. $expected = [
  5520. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5521. ['input' => [
  5522. 'type' => 'checkbox',
  5523. 'name' => 'published',
  5524. 'value' => 'myvalue',
  5525. 'id' => 'theID',
  5526. 'checked' => 'checked',
  5527. 'class' => 'form-error'
  5528. ]]
  5529. ];
  5530. $this->assertHtml($expected, $result);
  5531. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5532. $this->Form->create($this->article);
  5533. $result = $this->Form->checkbox('published');
  5534. $expected = [
  5535. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5536. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5537. ];
  5538. $this->assertHtml($expected, $result);
  5539. }
  5540. /**
  5541. * testCheckboxCustomNameAttribute method
  5542. *
  5543. * Test checkbox() with a custom name attribute.
  5544. *
  5545. * @return void
  5546. */
  5547. public function testCheckboxCustomNameAttribute()
  5548. {
  5549. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5550. $expected = [
  5551. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5552. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5553. ];
  5554. $this->assertHtml($expected, $result);
  5555. }
  5556. /**
  5557. * testCheckboxHiddenField method
  5558. *
  5559. * Test that the hidden input for checkboxes can be omitted or set to a
  5560. * specific value.
  5561. *
  5562. * @return void
  5563. */
  5564. public function testCheckboxHiddenField()
  5565. {
  5566. $result = $this->Form->checkbox('UserForm.something', [
  5567. 'hiddenField' => false
  5568. ]);
  5569. $expected = [
  5570. 'input' => [
  5571. 'type' => 'checkbox',
  5572. 'name' => 'UserForm[something]',
  5573. 'value' => '1'
  5574. ],
  5575. ];
  5576. $this->assertHtml($expected, $result);
  5577. $result = $this->Form->checkbox('UserForm.something', [
  5578. 'value' => 'Y',
  5579. 'hiddenField' => 'N',
  5580. ]);
  5581. $expected = [
  5582. ['input' => [
  5583. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5584. 'value' => 'N'
  5585. ]],
  5586. ['input' => [
  5587. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5588. 'value' => 'Y'
  5589. ]],
  5590. ];
  5591. $this->assertHtml($expected, $result);
  5592. }
  5593. /**
  5594. * testTime method
  5595. *
  5596. * Test the time type.
  5597. *
  5598. * @return void
  5599. */
  5600. public function testTime()
  5601. {
  5602. $result = $this->Form->time('start_time', [
  5603. 'timeFormat' => 12,
  5604. 'interval' => 5,
  5605. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5606. ]);
  5607. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5608. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5609. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5610. $this->assertNotContains('year', $result);
  5611. $this->assertNotContains('month', $result);
  5612. $this->assertNotContains('day', $result);
  5613. $result = $this->Form->time('start_time', [
  5614. 'timeFormat' => 12,
  5615. 'interval' => 5,
  5616. 'value' => '2014-03-08 16:30:00'
  5617. ]);
  5618. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5619. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5620. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5621. $this->assertNotContains('year', $result);
  5622. $this->assertNotContains('month', $result);
  5623. $this->assertNotContains('day', $result);
  5624. }
  5625. /**
  5626. * testTimeFormat24NoMeridian method
  5627. *
  5628. * Ensure that timeFormat=24 has no merdian.
  5629. *
  5630. * @return void.
  5631. */
  5632. public function testTimeFormat24NoMeridian()
  5633. {
  5634. $result = $this->Form->time('start_time', [
  5635. 'timeFormat' => 24,
  5636. 'interval' => 5,
  5637. 'value' => '2014-03-08 16:30:00'
  5638. ]);
  5639. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5640. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5641. $this->assertNotContains('meridian', $result);
  5642. $this->assertNotContains('pm', $result);
  5643. $this->assertNotContains('year', $result);
  5644. $this->assertNotContains('month', $result);
  5645. $this->assertNotContains('day', $result);
  5646. }
  5647. /**
  5648. * testDate method
  5649. *
  5650. * Test the date type.
  5651. *
  5652. * @return void
  5653. */
  5654. public function testDate()
  5655. {
  5656. $result = $this->Form->date('start_day', [
  5657. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5658. ]);
  5659. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5660. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5661. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5662. $this->assertNotContains('hour', $result);
  5663. $this->assertNotContains('minute', $result);
  5664. $this->assertNotContains('second', $result);
  5665. $this->assertNotContains('meridian', $result);
  5666. }
  5667. /**
  5668. * testDateTime method
  5669. *
  5670. * Test generation of date/time select elements.
  5671. *
  5672. * @return void
  5673. */
  5674. public function testDateTime()
  5675. {
  5676. extract($this->dateRegex);
  5677. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5678. $now = strtotime('now');
  5679. $expected = [
  5680. ['select' => ['name' => 'Contact[date][year]']],
  5681. ['option' => ['value' => '']],
  5682. '/option',
  5683. $yearsRegex,
  5684. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5685. date('Y', $now),
  5686. '/option',
  5687. '*/select',
  5688. ['select' => ['name' => 'Contact[date][month]']],
  5689. ['option' => ['value' => '']],
  5690. '/option',
  5691. $monthsRegex,
  5692. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5693. date('F', $now),
  5694. '/option',
  5695. '*/select',
  5696. ['select' => ['name' => 'Contact[date][day]']],
  5697. ['option' => ['value' => '']],
  5698. '/option',
  5699. $daysRegex,
  5700. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5701. date('j', $now),
  5702. '/option',
  5703. '*/select',
  5704. ['select' => ['name' => 'Contact[date][hour]']],
  5705. ['option' => ['value' => '']],
  5706. '/option',
  5707. $hoursRegex,
  5708. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5709. date('G', $now),
  5710. '/option',
  5711. '*/select',
  5712. ['select' => ['name' => 'Contact[date][minute]']],
  5713. ['option' => ['value' => '']],
  5714. '/option',
  5715. $minutesRegex,
  5716. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5717. date('i', $now),
  5718. '/option',
  5719. '*/select',
  5720. ];
  5721. $this->assertHtml($expected, $result);
  5722. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5723. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5724. $now = strtotime('now');
  5725. $expected = [
  5726. ['select' => ['name' => 'Contact[date][year]']],
  5727. $yearsRegex,
  5728. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5729. date('Y', $now),
  5730. '/option',
  5731. '*/select',
  5732. ['select' => ['name' => 'Contact[date][month]']],
  5733. $monthsRegex,
  5734. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5735. date('F', $now),
  5736. '/option',
  5737. '*/select',
  5738. ['select' => ['name' => 'Contact[date][day]']],
  5739. $daysRegex,
  5740. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5741. date('j', $now),
  5742. '/option',
  5743. '*/select',
  5744. ['select' => ['name' => 'Contact[date][hour]']],
  5745. $hoursRegex,
  5746. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5747. date('G', $now),
  5748. '/option',
  5749. '*/select',
  5750. ['select' => ['name' => 'Contact[date][minute]']],
  5751. $minutesRegex,
  5752. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5753. date('i', $now),
  5754. '/option',
  5755. '*/select',
  5756. ];
  5757. $this->assertHtml($expected, $result);
  5758. }
  5759. /**
  5760. * testDateTimeSecured method
  5761. *
  5762. * Test that datetime fields are added to protected fields list.
  5763. *
  5764. * @return void
  5765. */
  5766. public function testDateTimeSecured()
  5767. {
  5768. $this->View->setRequest(
  5769. $this->View->getRequest()->withParam('_Token', ['unlockedFields' => []])
  5770. );
  5771. $this->Form->dateTime('Contact.date');
  5772. $expected = [
  5773. 'Contact.date.year',
  5774. 'Contact.date.month',
  5775. 'Contact.date.day',
  5776. 'Contact.date.hour',
  5777. 'Contact.date.minute',
  5778. ];
  5779. $this->assertEquals($expected, $this->Form->fields);
  5780. $this->Form->fields = [];
  5781. $this->Form->date('Contact.published');
  5782. $expected = [
  5783. 'Contact.published.year',
  5784. 'Contact.published.month',
  5785. 'Contact.published.day',
  5786. ];
  5787. $this->assertEquals($expected, $this->Form->fields);
  5788. }
  5789. /**
  5790. * testDateTimeSecuredDisabled method
  5791. *
  5792. * Test that datetime fields are added to protected fields list.
  5793. *
  5794. * @return void
  5795. */
  5796. public function testDateTimeSecuredDisabled()
  5797. {
  5798. $this->View->setRequest(
  5799. $this->View->getRequest()->withParam('_Token', ['unlockedFields' => []])
  5800. );
  5801. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5802. $expected = [];
  5803. $this->assertEquals($expected, $this->Form->fields);
  5804. $this->Form->fields = [];
  5805. $this->Form->date('Contact.published', ['secure' => false]);
  5806. $expected = [];
  5807. $this->assertEquals($expected, $this->Form->fields);
  5808. }
  5809. /**
  5810. * testDatetimeEmpty method
  5811. *
  5812. * Test empty defaulting to true for datetime.
  5813. *
  5814. * @return void
  5815. */
  5816. public function testDatetimeEmpty()
  5817. {
  5818. extract($this->dateRegex);
  5819. $result = $this->Form->dateTime('Contact.date', [
  5820. 'timeFormat' => 12,
  5821. 'empty' => true,
  5822. 'default' => true
  5823. ]);
  5824. $expected = [
  5825. ['select' => ['name' => 'Contact[date][year]']],
  5826. $yearsRegex,
  5827. ['option' => ['value' => '']],
  5828. '/option',
  5829. '*/select',
  5830. ['select' => ['name' => 'Contact[date][month]']],
  5831. $monthsRegex,
  5832. ['option' => ['value' => '']],
  5833. '/option',
  5834. '*/select',
  5835. ['select' => ['name' => 'Contact[date][day]']],
  5836. $daysRegex,
  5837. ['option' => ['value' => '']],
  5838. '/option',
  5839. '*/select',
  5840. ['select' => ['name' => 'Contact[date][hour]']],
  5841. $hoursRegex,
  5842. ['option' => ['value' => '']],
  5843. '/option',
  5844. '*/select',
  5845. ['select' => ['name' => 'Contact[date][minute]']],
  5846. $minutesRegex,
  5847. ['option' => ['value' => '']],
  5848. '/option',
  5849. '*/select',
  5850. ['select' => ['name' => 'Contact[date][meridian]']],
  5851. $meridianRegex,
  5852. ['option' => ['value' => '']],
  5853. '/option',
  5854. '*/select'
  5855. ];
  5856. $this->assertHtml($expected, $result);
  5857. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5858. }
  5859. /**
  5860. * testDatetimeMinuteInterval method
  5861. *
  5862. * Test datetime with interval option.
  5863. *
  5864. * @return void
  5865. */
  5866. public function testDatetimeMinuteInterval()
  5867. {
  5868. extract($this->dateRegex);
  5869. $result = $this->Form->dateTime('Contact.date', [
  5870. 'interval' => 5,
  5871. 'value' => ''
  5872. ]);
  5873. $expected = [
  5874. ['select' => ['name' => 'Contact[date][year]']],
  5875. $yearsRegex,
  5876. ['option' => ['selected' => 'selected', 'value' => '']],
  5877. '/option',
  5878. '*/select',
  5879. ['select' => ['name' => 'Contact[date][month]']],
  5880. $monthsRegex,
  5881. ['option' => ['selected' => 'selected', 'value' => '']],
  5882. '/option',
  5883. '*/select',
  5884. ['select' => ['name' => 'Contact[date][day]']],
  5885. $daysRegex,
  5886. ['option' => ['selected' => 'selected', 'value' => '']],
  5887. '/option',
  5888. '*/select',
  5889. ['select' => ['name' => 'Contact[date][hour]']],
  5890. $hoursRegex,
  5891. ['option' => ['selected' => 'selected', 'value' => '']],
  5892. '/option',
  5893. '*/select',
  5894. ['select' => ['name' => 'Contact[date][minute]']],
  5895. $minutesRegex,
  5896. ['option' => ['selected' => 'selected', 'value' => '']],
  5897. '/option',
  5898. ['option' => ['value' => '00']],
  5899. '00',
  5900. '/option',
  5901. ['option' => ['value' => '05']],
  5902. '05',
  5903. '/option',
  5904. ['option' => ['value' => '10']],
  5905. '10',
  5906. '/option',
  5907. '*/select',
  5908. ];
  5909. $this->assertHtml($expected, $result);
  5910. }
  5911. /**
  5912. * testDateTimeRounding method
  5913. *
  5914. * Test dateTime with rounding.
  5915. *
  5916. * @return void
  5917. */
  5918. public function testDateTimeRounding()
  5919. {
  5920. $this->View->setRequest($this->View->getRequest()->withData('Contact', [
  5921. 'date' => [
  5922. 'day' => '13',
  5923. 'month' => '12',
  5924. 'year' => '2010',
  5925. 'hour' => '04',
  5926. 'minute' => '19',
  5927. 'meridian' => 'AM'
  5928. ]
  5929. ]));
  5930. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5931. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5932. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5933. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5934. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5935. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5936. }
  5937. /**
  5938. * testDatetimeWithDefault method
  5939. *
  5940. * Test that datetime() and default values work.
  5941. *
  5942. * @return void
  5943. */
  5944. public function testDatetimeWithDefault()
  5945. {
  5946. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5947. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5948. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5949. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5950. $result = $this->Form->dateTime('Contact.updated', [
  5951. 'default' => '2009-06-01 11:15:30'
  5952. ]);
  5953. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5954. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5955. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5956. }
  5957. /**
  5958. * testDateTimeAllZeros method
  5959. *
  5960. * Test datetime() with all zeros.
  5961. *
  5962. * @return void
  5963. */
  5964. public function testDateTimeAllZeros()
  5965. {
  5966. $result = $this->Form->dateTime('Contact.date', [
  5967. 'timeFormat' => false,
  5968. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5969. 'value' => '0000-00-00'
  5970. ]);
  5971. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5972. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5973. }
  5974. /**
  5975. * testDateTimeEmptyAsArray method
  5976. *
  5977. * @return void
  5978. */
  5979. public function testDateTimeEmptyAsArray()
  5980. {
  5981. $result = $this->Form->dateTime('Contact.date', [
  5982. 'empty' => [
  5983. 'day' => 'DAY',
  5984. 'month' => 'MONTH',
  5985. 'year' => 'YEAR',
  5986. 'hour' => 'HOUR',
  5987. 'minute' => 'MINUTE',
  5988. 'meridian' => false
  5989. ],
  5990. 'default' => true
  5991. ]);
  5992. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5993. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5994. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5995. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5996. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5997. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5998. $result = $this->Form->dateTime('Contact.date', [
  5999. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  6000. 'default' => true
  6001. ]);
  6002. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  6003. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  6004. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  6005. }
  6006. /**
  6007. * testFormDateTimeMulti method
  6008. *
  6009. * Test multiple datetime element generation.
  6010. *
  6011. * @return void
  6012. */
  6013. public function testFormDateTimeMulti()
  6014. {
  6015. extract($this->dateRegex);
  6016. $result = $this->Form->dateTime('Contact.1.updated');
  6017. $this->assertContains('Contact[1][updated][month]', $result);
  6018. $this->assertContains('Contact[1][updated][day]', $result);
  6019. $this->assertContains('Contact[1][updated][year]', $result);
  6020. $this->assertContains('Contact[1][updated][hour]', $result);
  6021. $this->assertContains('Contact[1][updated][minute]', $result);
  6022. }
  6023. /**
  6024. * testDateTimeLabelIdMatchesFirstControl method
  6025. *
  6026. * When changing the date format, the label should always focus the first select box when
  6027. * clicked.
  6028. *
  6029. * @return void
  6030. */
  6031. public function testDateTimeLabelIdMatchesFirstControl()
  6032. {
  6033. $result = $this->Form->control('Model.date', ['type' => 'date']);
  6034. $this->assertContains('<label>Date</label>', $result);
  6035. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  6036. $this->assertContains('<label>Date</label>', $result);
  6037. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  6038. $this->assertContains('<label>Date</label>', $result);
  6039. }
  6040. /**
  6041. * testDateTimeSecondOptions method
  6042. *
  6043. * Test datetime second=true.
  6044. *
  6045. * @return void
  6046. */
  6047. public function testDateTimeSecondOptions()
  6048. {
  6049. $result = $this->Form->dateTime('updated', ['second' => true]);
  6050. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6051. $result = $this->Form->dateTime('updated', ['second' => []]);
  6052. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6053. $result = $this->Form->dateTime('updated', ['second' => null]);
  6054. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6055. $result = $this->Form->dateTime('updated', ['second' => false]);
  6056. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6057. }
  6058. /**
  6059. * testMonth method
  6060. *
  6061. * Test generation of a month input.
  6062. *
  6063. * @return void
  6064. */
  6065. public function testMonth()
  6066. {
  6067. $result = $this->Form->month('Model.field', ['value' => '']);
  6068. $expected = [
  6069. ['select' => ['name' => 'Model[field][month]']],
  6070. ['option' => ['value' => '', 'selected' => 'selected']],
  6071. '/option',
  6072. ['option' => ['value' => '01']],
  6073. date('F', strtotime('2008-01-01 00:00:00')),
  6074. '/option',
  6075. ['option' => ['value' => '02']],
  6076. date('F', strtotime('2008-02-01 00:00:00')),
  6077. '/option',
  6078. '*/select',
  6079. ];
  6080. $this->assertHtml($expected, $result);
  6081. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  6082. $expected = [
  6083. ['select' => ['name' => 'Model[field][month]']],
  6084. ['option' => ['selected' => 'selected', 'value' => '']],
  6085. '/option',
  6086. ['option' => ['value' => '01']],
  6087. date('F', strtotime('2008-01-01 00:00:00')),
  6088. '/option',
  6089. ['option' => ['value' => '02']],
  6090. date('F', strtotime('2008-02-01 00:00:00')),
  6091. '/option',
  6092. '*/select',
  6093. ];
  6094. $this->assertHtml($expected, $result);
  6095. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6096. $expected = [
  6097. ['select' => ['name' => 'Model[field][month]']],
  6098. ['option' => ['selected' => 'selected', 'value' => '']],
  6099. '/option',
  6100. ['option' => ['value' => '01']],
  6101. '1',
  6102. '/option',
  6103. ['option' => ['value' => '02']],
  6104. '2',
  6105. '/option',
  6106. '*/select',
  6107. ];
  6108. $this->assertHtml($expected, $result);
  6109. $monthNames = [
  6110. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6111. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  6112. ];
  6113. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6114. $expected = [
  6115. ['select' => ['name' => 'Model[field][month]']],
  6116. ['option' => ['value' => '']],
  6117. '/option',
  6118. ['option' => ['value' => '01', 'selected' => 'selected']],
  6119. 'Jan',
  6120. '/option',
  6121. ['option' => ['value' => '02']],
  6122. 'Feb',
  6123. '/option',
  6124. '*/select',
  6125. ];
  6126. $this->assertHtml($expected, $result);
  6127. $this->View->setRequest(
  6128. $this->View->getRequest()->withData('Project.release', '2050-02-10')
  6129. );
  6130. $this->Form->create();
  6131. $result = $this->Form->month('Project.release');
  6132. $expected = [
  6133. ['select' => ['name' => 'Project[release][month]']],
  6134. ['option' => ['value' => '']],
  6135. '/option',
  6136. ['option' => ['value' => '01']],
  6137. 'January',
  6138. '/option',
  6139. ['option' => ['value' => '02', 'selected' => 'selected']],
  6140. 'February',
  6141. '/option',
  6142. '*/select',
  6143. ];
  6144. $this->assertHtml($expected, $result);
  6145. $result = $this->Form->month('Contact.published', [
  6146. 'empty' => 'Published on',
  6147. ]);
  6148. $this->assertContains('Published on', $result);
  6149. }
  6150. /**
  6151. * testDay method
  6152. *
  6153. * Test generation of a day input.
  6154. *
  6155. * @return void
  6156. */
  6157. public function testDay()
  6158. {
  6159. extract($this->dateRegex);
  6160. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6161. $expected = [
  6162. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6163. ['option' => ['selected' => 'selected', 'value' => '']],
  6164. '/option',
  6165. ['option' => ['value' => '01']],
  6166. '1',
  6167. '/option',
  6168. ['option' => ['value' => '02']],
  6169. '2',
  6170. '/option',
  6171. $daysRegex,
  6172. '/select',
  6173. ];
  6174. $this->assertHtml($expected, $result);
  6175. $this->View->setRequest(
  6176. $this->View->getRequest()->withData('Model.field', '2006-10-10 23:12:32')
  6177. );
  6178. $this->Form->create();
  6179. $result = $this->Form->day('Model.field');
  6180. $expected = [
  6181. ['select' => ['name' => 'Model[field][day]']],
  6182. ['option' => ['value' => '']],
  6183. '/option',
  6184. ['option' => ['value' => '01']],
  6185. '1',
  6186. '/option',
  6187. ['option' => ['value' => '02']],
  6188. '2',
  6189. '/option',
  6190. $daysRegex,
  6191. ['option' => ['value' => '10', 'selected' => 'selected']],
  6192. '10',
  6193. '/option',
  6194. $daysRegex,
  6195. '/select',
  6196. ];
  6197. $this->assertHtml($expected, $result);
  6198. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6199. $this->Form->create();
  6200. $result = $this->Form->day('Model.field', ['value' => '10']);
  6201. $expected = [
  6202. ['select' => ['name' => 'Model[field][day]']],
  6203. ['option' => ['value' => '']],
  6204. '/option',
  6205. ['option' => ['value' => '01']],
  6206. '1',
  6207. '/option',
  6208. ['option' => ['value' => '02']],
  6209. '2',
  6210. '/option',
  6211. $daysRegex,
  6212. ['option' => ['value' => '10', 'selected' => 'selected']],
  6213. '10',
  6214. '/option',
  6215. $daysRegex,
  6216. '/select',
  6217. ];
  6218. $this->assertHtml($expected, $result);
  6219. $this->View->setRequest(
  6220. $this->View->getRequest()->withData('Project.release', '2050-10-10')
  6221. );
  6222. $this->Form->create();
  6223. $result = $this->Form->day('Project.release');
  6224. $expected = [
  6225. ['select' => ['name' => 'Project[release][day]']],
  6226. ['option' => ['value' => '']],
  6227. '/option',
  6228. ['option' => ['value' => '01']],
  6229. '1',
  6230. '/option',
  6231. ['option' => ['value' => '02']],
  6232. '2',
  6233. '/option',
  6234. $daysRegex,
  6235. ['option' => ['value' => '10', 'selected' => 'selected']],
  6236. '10',
  6237. '/option',
  6238. $daysRegex,
  6239. '/select',
  6240. ];
  6241. $this->assertHtml($expected, $result);
  6242. $result = $this->Form->day('Contact.published', [
  6243. 'empty' => 'Published on',
  6244. ]);
  6245. $this->assertContains('Published on', $result);
  6246. }
  6247. /**
  6248. * testMinute method
  6249. *
  6250. * Test generation of a minute input.
  6251. *
  6252. * @return void
  6253. */
  6254. public function testMinute()
  6255. {
  6256. extract($this->dateRegex);
  6257. $result = $this->Form->minute('Model.field', ['value' => '']);
  6258. $expected = [
  6259. ['select' => ['name' => 'Model[field][minute]']],
  6260. ['option' => ['selected' => 'selected', 'value' => '']],
  6261. '/option',
  6262. ['option' => ['value' => '00']],
  6263. '00',
  6264. '/option',
  6265. ['option' => ['value' => '01']],
  6266. '01',
  6267. '/option',
  6268. ['option' => ['value' => '02']],
  6269. '02',
  6270. '/option',
  6271. $minutesRegex,
  6272. '/select',
  6273. ];
  6274. $this->assertHtml($expected, $result);
  6275. $this->View->setRequest(
  6276. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6277. );
  6278. $this->Form->create();
  6279. $result = $this->Form->minute('Model.field');
  6280. $expected = [
  6281. ['select' => ['name' => 'Model[field][minute]']],
  6282. ['option' => ['value' => '']],
  6283. '/option',
  6284. ['option' => ['value' => '00']],
  6285. '00',
  6286. '/option',
  6287. ['option' => ['value' => '01']],
  6288. '01',
  6289. '/option',
  6290. ['option' => ['value' => '02']],
  6291. '02',
  6292. '/option',
  6293. $minutesRegex,
  6294. ['option' => ['value' => '12', 'selected' => 'selected']],
  6295. '12',
  6296. '/option',
  6297. $minutesRegex,
  6298. '/select',
  6299. ];
  6300. $this->assertHtml($expected, $result);
  6301. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6302. $this->Form->create();
  6303. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6304. $expected = [
  6305. ['select' => ['name' => 'Model[field][minute]']],
  6306. ['option' => ['selected' => 'selected', 'value' => '']],
  6307. '/option',
  6308. ['option' => ['value' => '00']],
  6309. '00',
  6310. '/option',
  6311. ['option' => ['value' => '05']],
  6312. '05',
  6313. '/option',
  6314. ['option' => ['value' => '10']],
  6315. '10',
  6316. '/option',
  6317. $minutesRegex,
  6318. '/select',
  6319. ];
  6320. $this->assertHtml($expected, $result);
  6321. $this->View->setRequest(
  6322. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:10:32')
  6323. );
  6324. $this->Form->create();
  6325. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6326. $expected = [
  6327. ['select' => ['name' => 'Model[field][minute]']],
  6328. ['option' => ['value' => '']],
  6329. '/option',
  6330. ['option' => ['value' => '00']],
  6331. '00',
  6332. '/option',
  6333. ['option' => ['value' => '05']],
  6334. '05',
  6335. '/option',
  6336. ['option' => ['value' => '10', 'selected' => 'selected']],
  6337. '10',
  6338. '/option',
  6339. $minutesRegex,
  6340. '/select',
  6341. ];
  6342. $this->assertHtml($expected, $result);
  6343. }
  6344. /**
  6345. * testMeridian method
  6346. *
  6347. * Test generating an input for the meridian.
  6348. *
  6349. * @return void
  6350. */
  6351. public function testMeridian()
  6352. {
  6353. extract($this->dateRegex);
  6354. $now = new \DateTime();
  6355. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6356. $expected = [
  6357. ['select' => ['name' => 'Model[field][meridian]']],
  6358. ['option' => ['value' => '']],
  6359. '/option',
  6360. $meridianRegex,
  6361. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6362. $now->format('a'),
  6363. '/option',
  6364. '*/select'
  6365. ];
  6366. $this->assertHtml($expected, $result);
  6367. }
  6368. /**
  6369. * testHour method
  6370. *
  6371. * Test generation of an hour input.
  6372. *
  6373. * @return void
  6374. */
  6375. public function testHour()
  6376. {
  6377. extract($this->dateRegex);
  6378. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6379. $expected = [
  6380. ['select' => ['name' => 'Model[field][hour]']],
  6381. ['option' => ['selected' => 'selected', 'value' => '']],
  6382. '/option',
  6383. ['option' => ['value' => '01']],
  6384. '1',
  6385. '/option',
  6386. ['option' => ['value' => '02']],
  6387. '2',
  6388. '/option',
  6389. $hoursRegex,
  6390. '/select',
  6391. ];
  6392. $this->assertHtml($expected, $result);
  6393. $this->View->setRequest(
  6394. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6395. );
  6396. $this->Form->create();
  6397. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6398. $expected = [
  6399. ['select' => ['name' => 'Model[field][hour]']],
  6400. ['option' => ['value' => '']],
  6401. '/option',
  6402. ['option' => ['value' => '01']],
  6403. '1',
  6404. '/option',
  6405. ['option' => ['value' => '02']],
  6406. '2',
  6407. '/option',
  6408. $hoursRegex,
  6409. ['option' => ['value' => '12', 'selected' => 'selected']],
  6410. '12',
  6411. '/option',
  6412. '/select',
  6413. ];
  6414. $this->assertHtml($expected, $result);
  6415. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6416. $this->Form->create();
  6417. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6418. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6419. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6420. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6421. $this->View->setRequest(
  6422. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6423. );
  6424. $this->Form->create();
  6425. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6426. $expected = [
  6427. ['select' => ['name' => 'Model[field][hour]']],
  6428. ['option' => ['value' => '']],
  6429. '/option',
  6430. ['option' => ['value' => '00', 'selected' => 'selected']],
  6431. '0',
  6432. '/option',
  6433. ['option' => ['value' => '01']],
  6434. '1',
  6435. '/option',
  6436. ['option' => ['value' => '02']],
  6437. '2',
  6438. '/option',
  6439. $hoursRegex,
  6440. '/select',
  6441. ];
  6442. $this->assertHtml($expected, $result);
  6443. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  6444. $this->Form->create();
  6445. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6446. $thisHour = date('H');
  6447. $optValue = date('G');
  6448. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6449. $this->View->setRequest(
  6450. $this->View->getRequest()->withData('Model.field', '2050-10-10 01:12:32')
  6451. );
  6452. $this->Form->create();
  6453. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6454. $expected = [
  6455. ['select' => ['name' => 'Model[field][hour]']],
  6456. ['option' => ['value' => '']],
  6457. '/option',
  6458. ['option' => ['value' => '00']],
  6459. '0',
  6460. '/option',
  6461. ['option' => ['value' => '01', 'selected' => 'selected']],
  6462. '1',
  6463. '/option',
  6464. ['option' => ['value' => '02']],
  6465. '2',
  6466. '/option',
  6467. $hoursRegex,
  6468. '/select',
  6469. ];
  6470. $this->assertHtml($expected, $result);
  6471. }
  6472. /**
  6473. * testYear method
  6474. *
  6475. * Test generation of a year input.
  6476. *
  6477. * @return void
  6478. */
  6479. public function testYear()
  6480. {
  6481. $this->View->setRequest(
  6482. $this->View->getRequest()->withData('Contact.published', '2006-10-10')
  6483. );
  6484. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6485. $expected = [
  6486. ['select' => ['name' => 'Model[field][year]']],
  6487. ['option' => ['selected' => 'selected', 'value' => '']],
  6488. '/option',
  6489. ['option' => ['value' => '2007']],
  6490. '2007',
  6491. '/option',
  6492. ['option' => ['value' => '2006']],
  6493. '2006',
  6494. '/option',
  6495. '/select',
  6496. ];
  6497. $this->assertHtml($expected, $result);
  6498. $result = $this->Form->year('Model.field', [
  6499. 'value' => '',
  6500. 'minYear' => 2006,
  6501. 'maxYear' => 2007,
  6502. 'orderYear' => 'asc'
  6503. ]);
  6504. $expected = [
  6505. ['select' => ['name' => 'Model[field][year]']],
  6506. ['option' => ['selected' => 'selected', 'value' => '']],
  6507. '/option',
  6508. ['option' => ['value' => '2006']],
  6509. '2006',
  6510. '/option',
  6511. ['option' => ['value' => '2007']],
  6512. '2007',
  6513. '/option',
  6514. '/select',
  6515. ];
  6516. $this->assertHtml($expected, $result);
  6517. $result = $this->Form->year('Contact.published', [
  6518. 'empty' => false,
  6519. 'minYear' => 2006,
  6520. 'maxYear' => 2007,
  6521. ]);
  6522. $expected = [
  6523. ['select' => ['name' => 'Contact[published][year]']],
  6524. ['option' => ['value' => '2007']],
  6525. '2007',
  6526. '/option',
  6527. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6528. '2006',
  6529. '/option',
  6530. '/select',
  6531. ];
  6532. $this->assertHtml($expected, $result);
  6533. $result = $this->Form->year('Contact.published', [
  6534. 'empty' => 'Published on',
  6535. ]);
  6536. $this->assertContains('Published on', $result);
  6537. }
  6538. /**
  6539. * testControlDatetimePreEpoch method
  6540. *
  6541. * Test minYear being prior to the unix epoch.
  6542. *
  6543. * @return void
  6544. */
  6545. public function testControlDatetimePreEpoch()
  6546. {
  6547. $start = date('Y') - 80;
  6548. $end = date('Y') - 18;
  6549. $result = $this->Form->control('birth_year', [
  6550. 'type' => 'date',
  6551. 'label' => 'Birth Year',
  6552. 'minYear' => $start,
  6553. 'maxYear' => $end,
  6554. 'month' => false,
  6555. 'day' => false,
  6556. ]);
  6557. $this->assertContains('value="' . $start . '">' . $start, $result);
  6558. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6559. $this->assertNotContains('value="00">00', $result);
  6560. }
  6561. /**
  6562. * test control() datetime & required attributes
  6563. *
  6564. * @return void
  6565. */
  6566. public function testControlDatetimeRequired()
  6567. {
  6568. $result = $this->Form->control('birthday', [
  6569. 'type' => 'date',
  6570. 'required' => true
  6571. ]);
  6572. $this->assertContains(
  6573. '<select name="birthday[year]" required="required"',
  6574. $result
  6575. );
  6576. }
  6577. /**
  6578. * testYearAutoExpandRange method
  6579. *
  6580. * @return void
  6581. */
  6582. public function testYearAutoExpandRange()
  6583. {
  6584. $this->View->setRequest($this->View->getRequest()->withData('User.birthday', '1930-10-10'));
  6585. $result = $this->Form->year('User.birthday');
  6586. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6587. $result = $matches[1];
  6588. $expected = range(date('Y') + 5, 1930);
  6589. $this->assertEquals($expected, $result);
  6590. $this->View->setRequest($this->View->getRequest()->withData('Project.release', '2050-10-10'));
  6591. $this->Form->create();
  6592. $result = $this->Form->year('Project.release');
  6593. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6594. $result = $matches[1];
  6595. $expected = range(2050, date('Y') - 5);
  6596. $this->assertEquals($expected, $result);
  6597. $this->View->setRequest($this->View->getRequest()->withData('Project.release', '1881-10-10'));
  6598. $this->Form->create();
  6599. $result = $this->Form->year('Project.release', [
  6600. 'minYear' => 1890,
  6601. 'maxYear' => 1900
  6602. ]);
  6603. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6604. $result = $matches[1];
  6605. $expected = range(1900, 1881);
  6606. $this->assertEquals($expected, $result);
  6607. }
  6608. /**
  6609. * testControlDate method
  6610. *
  6611. * Test that control() accepts the type of date and passes options in.
  6612. *
  6613. * @return void
  6614. */
  6615. public function testControlDate()
  6616. {
  6617. $this->View->setRequest($this->View->getRequest()->withParsedBody([
  6618. 'month_year' => ['month' => date('m')],
  6619. ]));
  6620. $this->Form->create($this->article);
  6621. $result = $this->Form->control('month_year', [
  6622. 'label' => false,
  6623. 'type' => 'date',
  6624. 'minYear' => 2006,
  6625. 'maxYear' => 2008
  6626. ]);
  6627. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6628. $this->assertNotContains('value="2008" selected="selected"', $result);
  6629. }
  6630. /**
  6631. * testControlLabelFalse method
  6632. *
  6633. * Test the label option being set to false.
  6634. *
  6635. * @return void
  6636. */
  6637. public function testControlLabelFalse()
  6638. {
  6639. $this->Form->create($this->article);
  6640. $result = $this->Form->control('title', ['label' => false]);
  6641. $expected = [
  6642. 'div' => ['class' => 'input text required'],
  6643. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6644. '/div'
  6645. ];
  6646. $this->assertHtml($expected, $result);
  6647. }
  6648. /**
  6649. * testControlDateMaxYear method
  6650. *
  6651. * Let's say we want to only allow users born from 2006 to 2008 to register
  6652. * This being the first signup page, we still don't have any data.
  6653. *
  6654. * @return void
  6655. */
  6656. public function testControlDateMaxYear()
  6657. {
  6658. $this->Form->create($this->article);
  6659. $result = $this->Form->control('birthday', [
  6660. 'label' => false,
  6661. 'type' => 'date',
  6662. 'minYear' => 2006,
  6663. 'maxYear' => 2008,
  6664. 'default' => true
  6665. ]);
  6666. $this->assertContains('value="2008" selected="selected"', $result);
  6667. $this->assertContains('value="2006"', $result);
  6668. $this->assertNotContains('value="2005"', $result);
  6669. $this->assertNotContains('value="2009"', $result);
  6670. }
  6671. /**
  6672. * testTextArea method
  6673. *
  6674. * Test generation of a textarea input.
  6675. *
  6676. * @return void
  6677. */
  6678. public function testTextArea()
  6679. {
  6680. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  6681. $result = $this->Form->textarea('field');
  6682. $expected = [
  6683. 'textarea' => ['name' => 'field', 'rows' => 5],
  6684. 'some test data',
  6685. '/textarea',
  6686. ];
  6687. $this->assertHtml($expected, $result);
  6688. $result = $this->Form->textarea('user.bio');
  6689. $expected = [
  6690. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6691. '/textarea',
  6692. ];
  6693. $this->assertHtml($expected, $result);
  6694. $this->View->setRequest($this->View->getRequest()
  6695. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6696. $this->Form->create();
  6697. $result = $this->Form->textarea('field');
  6698. $expected = [
  6699. 'textarea' => ['name' => 'field', 'rows' => 5],
  6700. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6701. '/textarea',
  6702. ];
  6703. $this->assertHtml($expected, $result);
  6704. $this->View->setRequest($this->View->getRequest()->withData(
  6705. 'Model.field',
  6706. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  6707. ));
  6708. $this->Form->create();
  6709. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6710. $expected = [
  6711. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6712. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6713. '/textarea',
  6714. ];
  6715. $this->assertHtml($expected, $result);
  6716. $result = $this->Form->textarea('0.OtherModel.field');
  6717. $expected = [
  6718. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6719. '/textarea'
  6720. ];
  6721. $this->assertHtml($expected, $result);
  6722. }
  6723. /**
  6724. * testTextAreaWithStupidCharacters method
  6725. *
  6726. * Test text area with non-ascii characters.
  6727. *
  6728. * @return void
  6729. */
  6730. public function testTextAreaWithStupidCharacters()
  6731. {
  6732. $result = $this->Form->textarea('Post.content', [
  6733. 'value' => 'GREAT®',
  6734. 'rows' => '15',
  6735. 'cols' => '75'
  6736. ]);
  6737. $expected = [
  6738. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6739. 'GREAT®',
  6740. '/textarea',
  6741. ];
  6742. $this->assertHtml($expected, $result);
  6743. }
  6744. /**
  6745. * testTextAreaMaxLength method
  6746. *
  6747. * Test textareas maxlength read from schema.
  6748. *
  6749. * @return void
  6750. */
  6751. public function testTextAreaMaxLength()
  6752. {
  6753. $this->Form->create([
  6754. 'schema' => [
  6755. 'stuff' => ['type' => 'string', 'length' => 10],
  6756. ]
  6757. ]);
  6758. $result = $this->Form->control('other', ['type' => 'textarea']);
  6759. $expected = [
  6760. 'div' => ['class' => 'input textarea'],
  6761. 'label' => ['for' => 'other'],
  6762. 'Other',
  6763. '/label',
  6764. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6765. '/textarea',
  6766. '/div'
  6767. ];
  6768. $this->assertHtml($expected, $result);
  6769. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6770. $expected = [
  6771. 'div' => ['class' => 'input textarea'],
  6772. 'label' => ['for' => 'stuff'],
  6773. 'Stuff',
  6774. '/label',
  6775. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6776. '/textarea',
  6777. '/div'
  6778. ];
  6779. $this->assertHtml($expected, $result);
  6780. }
  6781. /**
  6782. * testHiddenField method
  6783. *
  6784. * Test generation of a hidden input.
  6785. *
  6786. * @return void
  6787. */
  6788. public function testHiddenField()
  6789. {
  6790. $this->article['errors'] = [
  6791. 'field' => true
  6792. ];
  6793. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  6794. $this->Form->create($this->article);
  6795. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6796. $expected = [
  6797. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6798. $this->assertHtml($expected, $result);
  6799. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6800. $expected = [
  6801. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6802. ];
  6803. $this->assertHtml($expected, $result);
  6804. }
  6805. /**
  6806. * testFileUploadField method
  6807. *
  6808. * Test generation of a file upload input.
  6809. *
  6810. * @return void
  6811. */
  6812. public function testFileUploadField()
  6813. {
  6814. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6815. $result = $this->Form->file('Model.upload');
  6816. $this->assertHtml($expected, $result);
  6817. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  6818. 'name' => '', 'type' => '', 'tmp_name' => '',
  6819. 'error' => 4, 'size' => 0
  6820. ]));
  6821. $result = $this->Form->file('Model.upload');
  6822. $this->assertHtml($expected, $result);
  6823. $this->View->setRequest(
  6824. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6825. );
  6826. $result = $this->Form->file('Model.upload');
  6827. $this->assertHtml($expected, $result);
  6828. }
  6829. /**
  6830. * testFileUploadOnOtherModel method
  6831. *
  6832. * Test File upload input on a model not used in create().
  6833. *
  6834. * @return void
  6835. */
  6836. public function testFileUploadOnOtherModel()
  6837. {
  6838. $this->Form->create($this->article, ['type' => 'file']);
  6839. $result = $this->Form->file('ValidateProfile.city');
  6840. $expected = [
  6841. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6842. ];
  6843. $this->assertHtml($expected, $result);
  6844. }
  6845. /**
  6846. * testButton method
  6847. *
  6848. * Test generation of a form button.
  6849. *
  6850. * @return void
  6851. */
  6852. public function testButton()
  6853. {
  6854. $result = $this->Form->button('Hi');
  6855. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6856. $this->assertHtml($expected, $result);
  6857. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6858. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6859. $this->assertHtml($expected, $result);
  6860. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6861. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6862. $this->assertHtml($expected, $result);
  6863. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6864. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6865. $this->assertHtml($expected, $result);
  6866. $result = $this->Form->button('No type', ['type' => false]);
  6867. $expected = ['button' => [], 'No type', '/button'];
  6868. $this->assertHtml($expected, $result);
  6869. $result = $this->Form->button('Upload Text', [
  6870. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6871. 'escape' => false
  6872. ]);
  6873. $this->assertNotRegExp('/\&039/', $result);
  6874. }
  6875. /**
  6876. * testButtonUnlockedByDefault method
  6877. *
  6878. * Test that button() makes unlocked fields by default.
  6879. *
  6880. * @return void
  6881. */
  6882. public function testButtonUnlockedByDefault()
  6883. {
  6884. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'secured'));
  6885. $this->Form->button('Save', ['name' => 'save']);
  6886. $this->Form->button('Clear');
  6887. $result = $this->Form->unlockField();
  6888. $this->assertEquals(['save'], $result);
  6889. }
  6890. /**
  6891. * Test generation of a form button with confirm message.
  6892. *
  6893. * @return void
  6894. */
  6895. public function testButtonWithConfirm()
  6896. {
  6897. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6898. $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm(&quot;Confirm me!&quot;)) { return true; } return false;'], 'Hi', '/button'];
  6899. $this->assertHtml($expected, $result);
  6900. }
  6901. /**
  6902. * testPostButton method
  6903. *
  6904. * @return void
  6905. */
  6906. public function testPostButton()
  6907. {
  6908. $result = $this->Form->postButton('Hi', '/controller/action');
  6909. $expected = [
  6910. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6911. 'div' => ['style' => 'display:none;'],
  6912. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6913. '/div',
  6914. 'button' => ['type' => 'submit'],
  6915. 'Hi',
  6916. '/button',
  6917. '/form'
  6918. ];
  6919. $this->assertHtml($expected, $result);
  6920. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6921. $this->assertContains('<input type="hidden" name="extra" value="value"', $result);
  6922. }
  6923. /**
  6924. * testPostButtonMethodType method
  6925. *
  6926. * @return void
  6927. */
  6928. public function testPostButtonMethodType()
  6929. {
  6930. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6931. $expected = [
  6932. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6933. 'div' => ['style' => 'display:none;'],
  6934. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6935. '/div',
  6936. 'button' => ['type' => 'submit'],
  6937. 'Hi',
  6938. '/button',
  6939. '/form'
  6940. ];
  6941. $this->assertHtml($expected, $result);
  6942. }
  6943. /**
  6944. * testPostButtonFormOptions method
  6945. *
  6946. * @return void
  6947. */
  6948. public function testPostButtonFormOptions()
  6949. {
  6950. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6951. $expected = [
  6952. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6953. 'div' => ['style' => 'display:none;'],
  6954. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6955. '/div',
  6956. 'button' => ['type' => 'submit'],
  6957. 'Hi',
  6958. '/button',
  6959. '/form'
  6960. ];
  6961. $this->assertHtml($expected, $result);
  6962. }
  6963. /**
  6964. * testPostButtonNestedData method
  6965. *
  6966. * Test using postButton with N dimensional data.
  6967. *
  6968. * @return void
  6969. */
  6970. public function testPostButtonNestedData()
  6971. {
  6972. $data = [
  6973. 'one' => [
  6974. 'two' => [
  6975. 3, 4, 5
  6976. ]
  6977. ]
  6978. ];
  6979. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6980. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6981. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6982. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6983. }
  6984. /**
  6985. * testSecurePostButton method
  6986. *
  6987. * Test that postButton adds _Token fields.
  6988. *
  6989. * @return void
  6990. */
  6991. public function testSecurePostButton()
  6992. {
  6993. $this->View->setRequest($this->View->getRequest()
  6994. ->withParam('_csrfToken', 'testkey')
  6995. ->withParam('_Token.unlockedFields', []));
  6996. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6997. $tokenDebug = urlencode(json_encode([
  6998. '/posts/delete/1',
  6999. [],
  7000. []
  7001. ]));
  7002. $expected = [
  7003. 'form' => [
  7004. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  7005. ],
  7006. ['div' => ['style' => 'display:none;']],
  7007. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7008. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7009. '/div',
  7010. 'button' => ['type' => 'submit'],
  7011. 'Delete',
  7012. '/button',
  7013. ['div' => ['style' => 'display:none;']],
  7014. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7015. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7016. ['input' => [
  7017. 'type' => 'hidden',
  7018. 'name' => '_Token[debug]',
  7019. 'value' => $tokenDebug,
  7020. 'autocomplete' => 'off',
  7021. ]],
  7022. '/div',
  7023. '/form',
  7024. ];
  7025. $this->assertHtml($expected, $result);
  7026. }
  7027. /**
  7028. * testPostLink method
  7029. *
  7030. * @return void
  7031. */
  7032. public function testPostLink()
  7033. {
  7034. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7035. $expected = [
  7036. 'form' => [
  7037. 'method' => 'post', 'action' => '/posts/delete/1',
  7038. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7039. ],
  7040. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7041. '/form',
  7042. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7043. 'Delete',
  7044. '/a'
  7045. ];
  7046. $this->assertHtml($expected, $result);
  7047. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  7048. $expected = [
  7049. 'form' => [
  7050. 'method' => 'post', 'action' => '/posts/delete/1',
  7051. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7052. ],
  7053. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  7054. '/form',
  7055. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7056. 'Delete',
  7057. '/a'
  7058. ];
  7059. $this->assertHtml($expected, $result);
  7060. $result = $this->Form->postLink(
  7061. 'Delete',
  7062. '/posts/delete/1',
  7063. ['target' => '_blank', 'class' => 'btn btn-danger']
  7064. );
  7065. $expected = [
  7066. 'form' => [
  7067. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  7068. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7069. ],
  7070. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7071. '/form',
  7072. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7073. 'Delete',
  7074. '/a'
  7075. ];
  7076. $this->assertHtml($expected, $result);
  7077. }
  7078. /**
  7079. * testPostLinkWithConfirm method
  7080. *
  7081. * Test the confirm option for postLink().
  7082. *
  7083. * @return void
  7084. */
  7085. public function testPostLinkWithConfirm()
  7086. {
  7087. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  7088. $expected = [
  7089. 'form' => [
  7090. 'method' => 'post', 'action' => '/posts/delete/1',
  7091. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7092. ],
  7093. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7094. '/form',
  7095. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  7096. 'Delete',
  7097. '/a'
  7098. ];
  7099. $this->assertHtml($expected, $result);
  7100. $result = $this->Form->postLink(
  7101. 'Delete',
  7102. '/posts/delete/1',
  7103. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  7104. );
  7105. $expected = [
  7106. 'form' => [
  7107. 'method' => 'post', 'action' => '/posts/delete/1',
  7108. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7109. ],
  7110. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7111. '/form',
  7112. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  7113. 'Delete',
  7114. '/a'
  7115. ];
  7116. $this->assertHtml($expected, $result);
  7117. $this->Form->setTemplates(['confirmJs' => 'if (confirm({{confirmMessage}})) { $(\'form[name="{{formName}}"]\').submit();};']);
  7118. $result = $this->Form->postLink(
  7119. 'Delete',
  7120. '/posts/delete/1',
  7121. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  7122. );
  7123. $expected = [
  7124. 'form' => [
  7125. 'method' => 'post', 'action' => '/posts/delete/1',
  7126. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7127. ],
  7128. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7129. '/form',
  7130. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\("Confirm this deletion\?"\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/'],
  7131. 'Delete',
  7132. '/a'
  7133. ];
  7134. $this->assertHtml($expected, $result);
  7135. }
  7136. /**
  7137. * testPostLinkWithQuery method
  7138. *
  7139. * Test postLink() with query string args.
  7140. *
  7141. * @return void
  7142. */
  7143. public function testPostLinkWithQuery()
  7144. {
  7145. $result = $this->Form->postLink(
  7146. 'Delete',
  7147. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  7148. );
  7149. $expected = [
  7150. 'form' => [
  7151. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  7152. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7153. ],
  7154. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7155. '/form',
  7156. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7157. 'Delete',
  7158. '/a'
  7159. ];
  7160. $this->assertHtml($expected, $result);
  7161. }
  7162. /**
  7163. * testPostLinkWithData method
  7164. *
  7165. * Test postLink with additional data.
  7166. *
  7167. * @return void
  7168. */
  7169. public function testPostLinkWithData()
  7170. {
  7171. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7172. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7173. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7174. $this->Form->create($entity);
  7175. $this->Form->end();
  7176. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7177. $this->assertContains(
  7178. '<input type="hidden" name="name" value="show"',
  7179. $result,
  7180. 'should not contain entity data.'
  7181. );
  7182. }
  7183. /**
  7184. * testPostLinkSecurityHash method
  7185. *
  7186. * Test that security hashes for postLink include the url.
  7187. *
  7188. * @return void
  7189. */
  7190. public function testPostLinkSecurityHash()
  7191. {
  7192. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7193. $hash .= '%3Aid';
  7194. $this->View->setRequest($this->View->getRequest()->withParam('_Token.key', 'test'));
  7195. $result = $this->Form->postLink(
  7196. 'Delete',
  7197. '/posts/delete/1',
  7198. ['data' => ['id' => 1]]
  7199. );
  7200. $tokenDebug = urlencode(json_encode([
  7201. '/posts/delete/1',
  7202. [
  7203. 'id' => 1
  7204. ],
  7205. []
  7206. ]));
  7207. $expected = [
  7208. 'form' => [
  7209. 'method' => 'post', 'action' => '/posts/delete/1',
  7210. 'name', 'style' => 'display:none;'
  7211. ],
  7212. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7213. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7214. 'div' => ['style' => 'display:none;'],
  7215. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7216. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7217. ['input' => [
  7218. 'type' => 'hidden',
  7219. 'name' => '_Token[debug]',
  7220. 'value' => $tokenDebug,
  7221. 'autocomplete' => 'off',
  7222. ]],
  7223. '/div',
  7224. '/form',
  7225. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7226. 'Delete',
  7227. '/a'
  7228. ];
  7229. $this->assertHtml($expected, $result);
  7230. }
  7231. /**
  7232. * testPostLinkSecurityHashBlockMode method
  7233. *
  7234. * Test that postLink doesn't modify the fields in the containing form.
  7235. *
  7236. * postLink() calls inside open forms should not modify the field list
  7237. * for the form.
  7238. *
  7239. * @return void
  7240. */
  7241. public function testPostLinkSecurityHashBlockMode()
  7242. {
  7243. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  7244. $hash .= '%3A';
  7245. $this->View->setRequest($this->View->getRequest()->withParam('_Token.key', 'test'));
  7246. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7247. $this->Form->control('title');
  7248. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7249. $result = $this->View->fetch('postLink');
  7250. $this->assertEquals(['title'], $this->Form->fields);
  7251. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7252. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7253. }
  7254. /**
  7255. * testPostLinkSecurityHashNoDebugMode method
  7256. *
  7257. * Test that security does not include debug token if debug is false.
  7258. *
  7259. * @return void
  7260. */
  7261. public function testPostLinkSecurityHashNoDebugMode()
  7262. {
  7263. Configure::write('debug', false);
  7264. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7265. $hash .= '%3Aid';
  7266. $this->View->setRequest($this->View->getRequest()
  7267. ->withParam('_Token.key', 'test'));
  7268. $result = $this->Form->postLink(
  7269. 'Delete',
  7270. '/posts/delete/1',
  7271. ['data' => ['id' => 1]]
  7272. );
  7273. $expected = [
  7274. 'form' => [
  7275. 'method' => 'post', 'action' => '/posts/delete/1',
  7276. 'name', 'style' => 'display:none;'
  7277. ],
  7278. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7279. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7280. 'div' => ['style' => 'display:none;'],
  7281. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7282. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7283. '/div',
  7284. '/form',
  7285. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7286. 'Delete',
  7287. '/a'
  7288. ];
  7289. $this->assertHtml($expected, $result);
  7290. }
  7291. /**
  7292. * testPostLinkNestedData method
  7293. *
  7294. * Test using postLink with N dimensional data.
  7295. *
  7296. * @return void
  7297. */
  7298. public function testPostLinkNestedData()
  7299. {
  7300. $data = [
  7301. 'one' => [
  7302. 'two' => [
  7303. 3, 4, 5
  7304. ]
  7305. ]
  7306. ];
  7307. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7308. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7309. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7310. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7311. }
  7312. /**
  7313. * testPostLinkAfterGetForm method
  7314. *
  7315. * Test creating postLinks after a GET form.
  7316. *
  7317. * @return void
  7318. */
  7319. public function testPostLinkAfterGetForm()
  7320. {
  7321. $this->View->setRequest($this->View->getRequest()
  7322. ->withParam('_csrfToken', 'testkey')
  7323. ->withParam('_Token', 'val'));
  7324. $this->Form->create($this->article, ['type' => 'get']);
  7325. $this->Form->end();
  7326. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7327. $tokenDebug = urlencode(json_encode([
  7328. '/posts/delete/1',
  7329. [],
  7330. []
  7331. ]));
  7332. $expected = [
  7333. 'form' => [
  7334. 'method' => 'post', 'action' => '/posts/delete/1',
  7335. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7336. ],
  7337. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7338. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7339. 'div' => ['style' => 'display:none;'],
  7340. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7341. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7342. ['input' => [
  7343. 'type' => 'hidden', 'name' => '_Token[debug]',
  7344. 'value' => $tokenDebug,
  7345. 'autocomplete' => 'off'
  7346. ]],
  7347. '/div',
  7348. '/form',
  7349. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7350. 'Delete',
  7351. '/a'
  7352. ];
  7353. $this->assertHtml($expected, $result);
  7354. }
  7355. /**
  7356. * testPostLinkFormBuffer method
  7357. *
  7358. * Test that postLink adds form tags to view block.
  7359. *
  7360. * @return void
  7361. */
  7362. public function testPostLinkFormBuffer()
  7363. {
  7364. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7365. $expected = [
  7366. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7367. 'Delete',
  7368. '/a'
  7369. ];
  7370. $this->assertHtml($expected, $result);
  7371. $result = $this->View->fetch('postLink');
  7372. $expected = [
  7373. 'form' => [
  7374. 'method' => 'post', 'action' => '/posts/delete/1',
  7375. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7376. ],
  7377. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7378. '/form'
  7379. ];
  7380. $this->assertHtml($expected, $result);
  7381. $result = $this->Form->postLink(
  7382. 'Delete',
  7383. '/posts/delete/2',
  7384. ['block' => true, 'method' => 'DELETE']
  7385. );
  7386. $expected = [
  7387. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7388. 'Delete',
  7389. '/a'
  7390. ];
  7391. $this->assertHtml($expected, $result);
  7392. $result = $this->View->fetch('postLink');
  7393. $expected = [
  7394. 'form' => [
  7395. 'method' => 'post', 'action' => '/posts/delete/1',
  7396. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7397. ],
  7398. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7399. '/form',
  7400. [
  7401. 'form' => [
  7402. 'method' => 'post', 'action' => '/posts/delete/2',
  7403. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7404. ],
  7405. ],
  7406. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7407. '/form'
  7408. ];
  7409. $this->assertHtml($expected, $result);
  7410. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7411. $expected = [
  7412. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7413. 'Delete',
  7414. '/a'
  7415. ];
  7416. $this->assertHtml($expected, $result);
  7417. $result = $this->View->fetch('foobar');
  7418. $expected = [
  7419. 'form' => [
  7420. 'method' => 'post', 'action' => '/posts/delete/1',
  7421. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7422. ],
  7423. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7424. '/form'
  7425. ];
  7426. $this->assertHtml($expected, $result);
  7427. }
  7428. /**
  7429. * testSubmitButton method
  7430. *
  7431. * @return void
  7432. */
  7433. public function testSubmitButton()
  7434. {
  7435. $result = $this->Form->submit('');
  7436. $expected = [
  7437. 'div' => ['class' => 'submit'],
  7438. 'input' => ['type' => 'submit', 'value' => ''],
  7439. '/div'
  7440. ];
  7441. $this->assertHtml($expected, $result);
  7442. $result = $this->Form->submit('Test Submit');
  7443. $expected = [
  7444. 'div' => ['class' => 'submit'],
  7445. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7446. '/div'
  7447. ];
  7448. $this->assertHtml($expected, $result);
  7449. $result = $this->Form->submit('Next >');
  7450. $expected = [
  7451. 'div' => ['class' => 'submit'],
  7452. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7453. '/div'
  7454. ];
  7455. $this->assertHtml($expected, $result);
  7456. $result = $this->Form->submit('Next >', ['escape' => false]);
  7457. $expected = [
  7458. 'div' => ['class' => 'submit'],
  7459. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7460. '/div'
  7461. ];
  7462. $this->assertHtml($expected, $result);
  7463. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7464. $expected = [
  7465. 'div' => ['class' => 'submit'],
  7466. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7467. '/div'
  7468. ];
  7469. $this->assertHtml($expected, $result);
  7470. }
  7471. /**
  7472. * testSubmitImage method
  7473. *
  7474. * Test image submit types.
  7475. *
  7476. * @return void
  7477. */
  7478. public function testSubmitImage()
  7479. {
  7480. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7481. $expected = [
  7482. 'div' => ['class' => 'submit'],
  7483. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7484. '/div'
  7485. ];
  7486. $this->assertHtml($expected, $result);
  7487. $result = $this->Form->submit('/relative/cake.power.gif');
  7488. $expected = [
  7489. 'div' => ['class' => 'submit'],
  7490. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7491. '/div'
  7492. ];
  7493. $this->assertHtml($expected, $result);
  7494. $result = $this->Form->submit('cake.power.gif');
  7495. $expected = [
  7496. 'div' => ['class' => 'submit'],
  7497. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7498. '/div'
  7499. ];
  7500. $this->assertHtml($expected, $result);
  7501. $result = $this->Form->submit('Not.an.image');
  7502. $expected = [
  7503. 'div' => ['class' => 'submit'],
  7504. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7505. '/div'
  7506. ];
  7507. $this->assertHtml($expected, $result);
  7508. }
  7509. /**
  7510. * testSubmitUnlockedByDefault method
  7511. *
  7512. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7513. * be submitted at a time.
  7514. *
  7515. * @return void
  7516. */
  7517. public function testSubmitUnlockedByDefault()
  7518. {
  7519. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'secured'));
  7520. $this->Form->submit('Go go');
  7521. $this->Form->submit('Save', ['name' => 'save']);
  7522. $result = $this->Form->unlockField();
  7523. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7524. }
  7525. /**
  7526. * testSubmitImageTimestamp method
  7527. *
  7528. * Test submit image with timestamps.
  7529. *
  7530. * @return void
  7531. */
  7532. public function testSubmitImageTimestamp()
  7533. {
  7534. Configure::write('Asset.timestamp', 'force');
  7535. $result = $this->Form->submit('cake.power.gif');
  7536. $expected = [
  7537. 'div' => ['class' => 'submit'],
  7538. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7539. '/div'
  7540. ];
  7541. $this->assertHtml($expected, $result);
  7542. }
  7543. /**
  7544. * testDateTimeWithGetForms method
  7545. *
  7546. * Test that datetime() works with GET style forms.
  7547. *
  7548. * @return void
  7549. */
  7550. public function testDateTimeWithGetForms()
  7551. {
  7552. extract($this->dateRegex);
  7553. $this->Form->create($this->article, ['type' => 'get']);
  7554. $result = $this->Form->datetime('created');
  7555. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7556. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7557. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7558. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7559. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7560. }
  7561. /**
  7562. * testForMagicControlNonExistingNorValidated method
  7563. *
  7564. * @return void
  7565. */
  7566. public function testForMagicControlNonExistingNorValidated()
  7567. {
  7568. $this->Form->create($this->article);
  7569. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7570. $result = $this->Form->control('non_existing_nor_validated');
  7571. $expected = [
  7572. 'label' => ['for' => 'non-existing-nor-validated'],
  7573. 'Non Existing Nor Validated',
  7574. '/label',
  7575. 'input' => [
  7576. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7577. 'id' => 'non-existing-nor-validated'
  7578. ]
  7579. ];
  7580. $this->assertHtml($expected, $result);
  7581. $result = $this->Form->control('non_existing_nor_validated', [
  7582. 'val' => 'my value'
  7583. ]);
  7584. $expected = [
  7585. 'label' => ['for' => 'non-existing-nor-validated'],
  7586. 'Non Existing Nor Validated',
  7587. '/label',
  7588. 'input' => [
  7589. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7590. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7591. ]
  7592. ];
  7593. $this->assertHtml($expected, $result);
  7594. $this->View->setRequest(
  7595. $this->View->getRequest()->withData('non_existing_nor_validated', 'CakePHP magic')
  7596. );
  7597. $this->Form->create($this->article);
  7598. $result = $this->Form->control('non_existing_nor_validated');
  7599. $expected = [
  7600. 'label' => ['for' => 'non-existing-nor-validated'],
  7601. 'Non Existing Nor Validated',
  7602. '/label',
  7603. 'input' => [
  7604. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7605. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7606. ]
  7607. ];
  7608. $this->assertHtml($expected, $result);
  7609. }
  7610. /**
  7611. * testFormMagicControlLabel method
  7612. *
  7613. * @return void
  7614. */
  7615. public function testFormMagicControlLabel()
  7616. {
  7617. $this->getTableLocator()->get('Contacts', [
  7618. 'className' => __NAMESPACE__ . '\ContactsTable'
  7619. ]);
  7620. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7621. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7622. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7623. $expected = [
  7624. 'label' => ['for' => 'contacts-name'],
  7625. 'My label',
  7626. '/label',
  7627. 'input' => [
  7628. 'type' => 'text', 'name' => 'Contacts[name]',
  7629. 'id' => 'contacts-name', 'maxlength' => '255'
  7630. ]
  7631. ];
  7632. $this->assertHtml($expected, $result);
  7633. $result = $this->Form->control('name', [
  7634. 'label' => ['class' => 'mandatory']
  7635. ]);
  7636. $expected = [
  7637. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7638. 'Name',
  7639. '/label',
  7640. 'input' => [
  7641. 'type' => 'text', 'name' => 'name',
  7642. 'id' => 'name', 'maxlength' => '255'
  7643. ]
  7644. ];
  7645. $this->assertHtml($expected, $result);
  7646. $result = $this->Form->control('name', [
  7647. 'div' => false,
  7648. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7649. ]);
  7650. $expected = [
  7651. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7652. 'My label',
  7653. '/label',
  7654. 'input' => [
  7655. 'type' => 'text', 'name' => 'name',
  7656. 'id' => 'name', 'maxlength' => '255'
  7657. ]
  7658. ];
  7659. $this->assertHtml($expected, $result);
  7660. $result = $this->Form->control('Contact.name', [
  7661. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7662. ]);
  7663. $expected = [
  7664. 'label' => ['for' => 'my_id'],
  7665. 'Name',
  7666. '/label',
  7667. 'input' => [
  7668. 'type' => 'text', 'name' => 'Contact[name]',
  7669. 'id' => 'my_id', 'maxlength' => '255'
  7670. ]
  7671. ];
  7672. $this->assertHtml($expected, $result);
  7673. $result = $this->Form->control('1.id');
  7674. $expected = ['input' => [
  7675. 'type' => 'hidden', 'name' => '1[id]',
  7676. 'id' => '1-id'
  7677. ]];
  7678. $this->assertHtml($expected, $result);
  7679. $result = $this->Form->control('1.name');
  7680. $expected = [
  7681. 'label' => ['for' => '1-name'],
  7682. 'Name',
  7683. '/label',
  7684. 'input' => [
  7685. 'type' => 'text', 'name' => '1[name]',
  7686. 'id' => '1-name', 'maxlength' => '255'
  7687. ]
  7688. ];
  7689. $this->assertHtml($expected, $result);
  7690. }
  7691. /**
  7692. * testFormEnd method
  7693. *
  7694. * @return void
  7695. */
  7696. public function testFormEnd()
  7697. {
  7698. $this->assertEquals('</form>', $this->Form->end());
  7699. }
  7700. /**
  7701. * testMultiRecordForm method
  7702. *
  7703. * Test the generation of fields for a multi record form.
  7704. *
  7705. * @return void
  7706. */
  7707. public function testMultiRecordForm()
  7708. {
  7709. $this->loadFixtures('Articles', 'Comments');
  7710. $articles = $this->getTableLocator()->get('Articles');
  7711. $articles->hasMany('Comments');
  7712. $comment = new Entity(['comment' => 'Value']);
  7713. $article = new Article(['comments' => [$comment]]);
  7714. $this->Form->create([$article]);
  7715. $result = $this->Form->control('0.comments.1.comment');
  7716. //@codingStandardsIgnoreStart
  7717. $expected = [
  7718. 'div' => ['class' => 'input textarea'],
  7719. 'label' => ['for' => '0-comments-1-comment'],
  7720. 'Comment',
  7721. '/label',
  7722. 'textarea' => [
  7723. 'name',
  7724. 'id' => '0-comments-1-comment',
  7725. 'rows' => 5
  7726. ],
  7727. '/textarea',
  7728. '/div'
  7729. ];
  7730. //@codingStandardsIgnoreEnd
  7731. $this->assertHtml($expected, $result);
  7732. $result = $this->Form->control('0.comments.0.comment');
  7733. //@codingStandardsIgnoreStart
  7734. $expected = [
  7735. 'div' => ['class' => 'input textarea'],
  7736. 'label' => ['for' => '0-comments-0-comment'],
  7737. 'Comment',
  7738. '/label',
  7739. 'textarea' => [
  7740. 'name',
  7741. 'id' => '0-comments-0-comment',
  7742. 'rows' => 5
  7743. ],
  7744. 'Value',
  7745. '/textarea',
  7746. '/div'
  7747. ];
  7748. //@codingStandardsIgnoreEnd
  7749. $this->assertHtml($expected, $result);
  7750. $comment->setError('comment', ['Not valid']);
  7751. $result = $this->Form->control('0.comments.0.comment');
  7752. //@codingStandardsIgnoreStart
  7753. $expected = [
  7754. 'div' => ['class' => 'input textarea error'],
  7755. 'label' => ['for' => '0-comments-0-comment'],
  7756. 'Comment',
  7757. '/label',
  7758. 'textarea' => [
  7759. 'name',
  7760. 'class' => 'form-error',
  7761. 'id' => '0-comments-0-comment',
  7762. 'rows' => 5
  7763. ],
  7764. 'Value',
  7765. '/textarea',
  7766. ['div' => ['class' => 'error-message']],
  7767. 'Not valid',
  7768. '/div',
  7769. '/div'
  7770. ];
  7771. //@codingStandardsIgnoreEnd
  7772. $this->assertHtml($expected, $result);
  7773. $this->getTableLocator()->get('Comments')
  7774. ->getValidator('default')
  7775. ->allowEmpty('comment', false);
  7776. $result = $this->Form->control('0.comments.1.comment');
  7777. //@codingStandardsIgnoreStart
  7778. $expected = [
  7779. 'div' => ['class' => 'input textarea required'],
  7780. 'label' => ['for' => '0-comments-1-comment'],
  7781. 'Comment',
  7782. '/label',
  7783. 'textarea' => [
  7784. 'name',
  7785. 'required' => 'required',
  7786. 'id' => '0-comments-1-comment',
  7787. 'rows' => 5,
  7788. ],
  7789. '/textarea',
  7790. '/div'
  7791. ];
  7792. //@codingStandardsIgnoreEnd
  7793. $this->assertHtml($expected, $result);
  7794. }
  7795. /**
  7796. * testHtml5Controls method
  7797. *
  7798. * Test that some html5 inputs + FormHelper::__call() work.
  7799. *
  7800. * @return void
  7801. */
  7802. public function testHtml5Controls()
  7803. {
  7804. $result = $this->Form->email('User.email');
  7805. $expected = [
  7806. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7807. ];
  7808. $this->assertHtml($expected, $result);
  7809. $result = $this->Form->search('User.query');
  7810. $expected = [
  7811. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7812. ];
  7813. $this->assertHtml($expected, $result);
  7814. $result = $this->Form->search('User.query', ['value' => 'test']);
  7815. $expected = [
  7816. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7817. ];
  7818. $this->assertHtml($expected, $result);
  7819. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7820. $expected = [
  7821. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7822. ];
  7823. $this->assertHtml($expected, $result);
  7824. }
  7825. /**
  7826. * testHtml5ControlWithControl method
  7827. *
  7828. * Test accessing html5 inputs through control().
  7829. *
  7830. * @return void
  7831. */
  7832. public function testHtml5ControlWithControl()
  7833. {
  7834. $this->Form->create();
  7835. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7836. $result = $this->Form->control('website', [
  7837. 'type' => 'url',
  7838. 'val' => 'http://domain.tld',
  7839. 'label' => false
  7840. ]);
  7841. $expected = [
  7842. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7843. ];
  7844. $this->assertHtml($expected, $result);
  7845. }
  7846. /**
  7847. * testHtml5ControlException method
  7848. *
  7849. * Test errors when field name is missing.
  7850. *
  7851. * @return void
  7852. */
  7853. public function testHtml5ControlException()
  7854. {
  7855. $this->expectException(\Cake\Core\Exception\Exception::class);
  7856. $this->Form->email();
  7857. }
  7858. /**
  7859. * tests fields that are required use custom validation messages
  7860. *
  7861. * @return void
  7862. */
  7863. public function testHtml5ErrorMessage()
  7864. {
  7865. $this->Form->setConfig('autoSetCustomValidity', true);
  7866. $validator = (new \Cake\Validation\Validator())
  7867. ->requirePresence('email', true, 'Custom error message')
  7868. ->requirePresence('password')
  7869. ->alphaNumeric('password')
  7870. ->notBlank('phone');
  7871. $table = $this->getTableLocator()->get('Contacts', [
  7872. 'className' => __NAMESPACE__ . '\ContactsTable'
  7873. ]);
  7874. $table->setValidator('default', $validator);
  7875. $contact = new Entity();
  7876. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7877. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7878. $result = $this->Form->control('password');
  7879. $expected = [
  7880. 'label' => ['for' => 'password'],
  7881. 'Password',
  7882. '/label',
  7883. 'input' => [
  7884. 'id' => 'password',
  7885. 'name' => 'password',
  7886. 'type' => 'password',
  7887. 'value' => '',
  7888. 'required' => 'required',
  7889. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7890. 'oninvalid' => 'this.setCustomValidity(&#039;This field is required&#039;)',
  7891. ]
  7892. ];
  7893. $this->assertHtml($expected, $result);
  7894. $result = $this->Form->control('phone');
  7895. $expected = [
  7896. 'label' => ['for' => 'phone'],
  7897. 'Phone',
  7898. '/label',
  7899. 'input' => [
  7900. 'id' => 'phone',
  7901. 'name' => 'phone',
  7902. 'type' => 'tel',
  7903. 'value' => '',
  7904. 'maxlength' => 255,
  7905. 'required' => 'required',
  7906. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7907. 'oninvalid' => 'this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  7908. ]
  7909. ];
  7910. $this->assertHtml($expected, $result);
  7911. $result = $this->Form->control('email');
  7912. $expected = [
  7913. 'label' => ['for' => 'email'],
  7914. 'Email',
  7915. '/label',
  7916. 'input' => [
  7917. 'id' => 'email',
  7918. 'name' => 'email',
  7919. 'type' => 'email',
  7920. 'value' => '',
  7921. 'maxlength' => 255,
  7922. 'required' => 'required',
  7923. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7924. 'oninvalid' => 'this.setCustomValidity(&#039;Custom error message&#039;)',
  7925. ]
  7926. ];
  7927. $this->assertHtml($expected, $result);
  7928. }
  7929. /**
  7930. * tests that custom validation messages are in templateVars
  7931. *
  7932. * @return void
  7933. */
  7934. public function testHtml5ErrorMessageInTemplateVars()
  7935. {
  7936. $validator = (new \Cake\Validation\Validator())
  7937. ->requirePresence('email', true, 'Custom error "message" & entities')
  7938. ->requirePresence('password')
  7939. ->alphaNumeric('password')
  7940. ->notBlank('phone');
  7941. $table = $this->getTableLocator()->get('Contacts', [
  7942. 'className' => __NAMESPACE__ . '\ContactsTable'
  7943. ]);
  7944. $table->setValidator('default', $validator);
  7945. $contact = new Entity();
  7946. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7947. $this->Form->setTemplates([
  7948. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7949. 'inputContainer' => '{{content}}'
  7950. ]);
  7951. $result = $this->Form->control('password');
  7952. $expected = [
  7953. 'label' => ['for' => 'password'],
  7954. 'Password',
  7955. '/label',
  7956. 'input' => [
  7957. 'id' => 'password',
  7958. 'name' => 'password',
  7959. 'type' => 'password',
  7960. 'value' => '',
  7961. 'required' => 'required',
  7962. 'data-message' => 'This field is required',
  7963. ]
  7964. ];
  7965. $this->assertHtml($expected, $result);
  7966. $result = $this->Form->control('phone');
  7967. $expected = [
  7968. 'label' => ['for' => 'phone'],
  7969. 'Phone',
  7970. '/label',
  7971. 'input' => [
  7972. 'id' => 'phone',
  7973. 'name' => 'phone',
  7974. 'type' => 'tel',
  7975. 'value' => '',
  7976. 'maxlength' => 255,
  7977. 'required' => 'required',
  7978. 'data-message' => 'This field cannot be left empty',
  7979. ],
  7980. ];
  7981. $this->assertHtml($expected, $result);
  7982. $result = $this->Form->control('email', [
  7983. 'templateVars' => [
  7984. 'custom' => 'data-custom="1"'
  7985. ]
  7986. ]);
  7987. $expected = [
  7988. 'label' => ['for' => 'email'],
  7989. 'Email',
  7990. '/label',
  7991. 'input' => [
  7992. 'id' => 'email',
  7993. 'name' => 'email',
  7994. 'type' => 'email',
  7995. 'value' => '',
  7996. 'maxlength' => 255,
  7997. 'required' => 'required',
  7998. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7999. 'data-custom' => '1',
  8000. ],
  8001. ];
  8002. $this->assertHtml($expected, $result);
  8003. }
  8004. /**
  8005. * testRequiredAttribute method
  8006. *
  8007. * Tests that formhelper sets required attributes.
  8008. *
  8009. * @return void
  8010. */
  8011. public function testRequiredAttribute()
  8012. {
  8013. $this->article['required'] = [
  8014. 'title' => true,
  8015. 'body' => false,
  8016. ];
  8017. $this->Form->create($this->article);
  8018. $result = $this->Form->control('title');
  8019. $expected = [
  8020. 'div' => ['class' => 'input text required'],
  8021. 'label' => ['for' => 'title'],
  8022. 'Title',
  8023. '/label',
  8024. 'input' => [
  8025. 'type' => 'text',
  8026. 'name' => 'title',
  8027. 'id' => 'title',
  8028. 'required' => 'required',
  8029. ],
  8030. '/div'
  8031. ];
  8032. $this->assertHtml($expected, $result);
  8033. $result = $this->Form->control('title', ['required' => false]);
  8034. $this->assertNotContains('required', $result);
  8035. $result = $this->Form->control('body');
  8036. $expected = [
  8037. 'div' => ['class' => 'input text'],
  8038. 'label' => ['for' => 'body'],
  8039. 'Body',
  8040. '/label',
  8041. 'input' => [
  8042. 'type' => 'text',
  8043. 'name' => 'body',
  8044. 'id' => 'body',
  8045. ],
  8046. '/div'
  8047. ];
  8048. $this->assertHtml($expected, $result);
  8049. $result = $this->Form->control('body', ['required' => true]);
  8050. $this->assertContains('required', $result);
  8051. }
  8052. /**
  8053. * testControlsNotNested method
  8054. *
  8055. * Tests that it is possible to put inputs outside of the label.
  8056. *
  8057. * @return void
  8058. */
  8059. public function testControlsNotNested()
  8060. {
  8061. $this->Form->setTemplates([
  8062. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  8063. 'formGroup' => '{{input}}{{label}}',
  8064. ]);
  8065. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  8066. $expected = [
  8067. 'div' => ['class' => 'input checkbox'],
  8068. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  8069. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  8070. 'label' => ['for' => 'foo'],
  8071. 'Foo',
  8072. '/label',
  8073. '/div'
  8074. ];
  8075. $this->assertHtml($expected, $result);
  8076. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  8077. $expected = [
  8078. 'div' => ['class' => 'input checkbox'],
  8079. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  8080. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  8081. '/div'
  8082. ];
  8083. $this->assertHtml($expected, $result);
  8084. $result = $this->Form->control('confirm', [
  8085. 'type' => 'radio',
  8086. 'options' => ['Y' => 'Yes', 'N' => 'No']
  8087. ]);
  8088. $expected = [
  8089. 'div' => ['class' => 'input radio'],
  8090. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  8091. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  8092. ['label' => ['for' => 'confirm-y']],
  8093. 'Yes',
  8094. '/label',
  8095. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  8096. ['label' => ['for' => 'confirm-n']],
  8097. 'No',
  8098. '/label',
  8099. '<label',
  8100. 'Confirm',
  8101. '/label',
  8102. '/div',
  8103. ];
  8104. $this->assertHtml($expected, $result);
  8105. $result = $this->Form->select('category', ['1', '2'], [
  8106. 'multiple' => 'checkbox',
  8107. 'name' => 'fish',
  8108. ]);
  8109. $expected = [
  8110. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  8111. ['div' => ['class' => 'checkbox']],
  8112. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  8113. ['label' => ['for' => 'fish-0']],
  8114. '1',
  8115. '/label',
  8116. '/div',
  8117. ['div' => ['class' => 'checkbox']],
  8118. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  8119. ['label' => ['for' => 'fish-1']],
  8120. '2',
  8121. '/label',
  8122. '/div'
  8123. ];
  8124. $this->assertHtml($expected, $result);
  8125. }
  8126. /**
  8127. * testControlContainerTemplates method
  8128. *
  8129. * Test that *Container templates are used by input.
  8130. *
  8131. * @return void
  8132. */
  8133. public function testControlContainerTemplates()
  8134. {
  8135. $this->Form->setTemplates([
  8136. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  8137. 'radioContainer' => '<div class="rad">{{content}}</div>',
  8138. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  8139. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  8140. ]);
  8141. $this->article['errors'] = [
  8142. 'Article' => ['published' => 'error message']
  8143. ];
  8144. $this->Form->create($this->article);
  8145. $result = $this->Form->control('accept', [
  8146. 'type' => 'checkbox'
  8147. ]);
  8148. $expected = [
  8149. 'div' => ['class' => 'check'],
  8150. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  8151. 'label' => ['for' => 'accept'],
  8152. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  8153. 'Accept',
  8154. '/label',
  8155. '/div'
  8156. ];
  8157. $this->assertHtml($expected, $result);
  8158. $result = $this->Form->control('accept', [
  8159. 'type' => 'radio',
  8160. 'options' => ['Y', 'N']
  8161. ]);
  8162. $this->assertContains('<div class="rad">', $result);
  8163. $result = $this->Form->control('Article.published', [
  8164. 'type' => 'radio',
  8165. 'options' => ['Y', 'N']
  8166. ]);
  8167. $this->assertContains('<div class="rad err">', $result);
  8168. $result = $this->Form->control('Article.created', [
  8169. 'type' => 'datetime',
  8170. ]);
  8171. $this->assertContains('<div class="dt">', $result);
  8172. }
  8173. /**
  8174. * testFormGroupTemplates method
  8175. *
  8176. * Test that *Container templates are used by input.
  8177. *
  8178. * @return void
  8179. */
  8180. public function testFormGroupTemplates()
  8181. {
  8182. $this->Form->setTemplates([
  8183. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  8184. ]);
  8185. $this->Form->create($this->article);
  8186. $result = $this->Form->control('accept', [
  8187. 'type' => 'radio',
  8188. 'options' => ['Y', 'N']
  8189. ]);
  8190. $this->assertContains('<div class="radio">', $result);
  8191. }
  8192. /**
  8193. * testResetTemplates method
  8194. *
  8195. * Test resetting templates.
  8196. *
  8197. * @return void
  8198. */
  8199. public function testResetTemplates()
  8200. {
  8201. $this->Form->setTemplates(['input' => '<input/>']);
  8202. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  8203. $this->Form->resetTemplates();
  8204. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  8205. }
  8206. /**
  8207. * testContext method
  8208. *
  8209. * Test the context method.
  8210. *
  8211. * @return void
  8212. */
  8213. public function testContext()
  8214. {
  8215. $result = $this->Form->context();
  8216. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  8217. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  8218. $this->assertSame($mock, $this->Form->context($mock));
  8219. $this->assertSame($mock, $this->Form->context());
  8220. }
  8221. /**
  8222. * testAutoDomId method
  8223. *
  8224. * @return void
  8225. */
  8226. public function testAutoDomId()
  8227. {
  8228. $result = $this->Form->text('field', ['id' => true]);
  8229. $expected = [
  8230. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  8231. ];
  8232. $this->assertHtml($expected, $result);
  8233. // Ensure id => doesn't cause problem when multiple inputs are generated.
  8234. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  8235. $expected = [
  8236. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  8237. ['label' => ['for' => 'field-0']],
  8238. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  8239. 'option A',
  8240. '/label',
  8241. ['label' => ['for' => 'field-1']],
  8242. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  8243. 'option B',
  8244. '/label',
  8245. ];
  8246. $this->assertHtml($expected, $result);
  8247. $result = $this->Form->select(
  8248. 'multi_field',
  8249. ['first', 'second'],
  8250. ['multiple' => 'checkbox', 'id' => true]
  8251. );
  8252. $expected = [
  8253. 'input' => [
  8254. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  8255. ],
  8256. ['div' => ['class' => 'checkbox']],
  8257. ['label' => ['for' => 'multi-field-0']],
  8258. ['input' => [
  8259. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8260. 'value' => '0', 'id' => 'multi-field-0'
  8261. ]],
  8262. 'first',
  8263. '/label',
  8264. '/div',
  8265. ['div' => ['class' => 'checkbox']],
  8266. ['label' => ['for' => 'multi-field-1']],
  8267. ['input' => [
  8268. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8269. 'value' => '1', 'id' => 'multi-field-1'
  8270. ]],
  8271. 'second',
  8272. '/label',
  8273. '/div',
  8274. ];
  8275. $this->assertHtml($expected, $result);
  8276. }
  8277. /**
  8278. * Test the basic setters and getters for value sources
  8279. *
  8280. * @return void
  8281. */
  8282. public function testFormValueSourcesSettersGetters()
  8283. {
  8284. $this->View->setRequest($this->View->getRequest()
  8285. ->withData('id', '1')
  8286. ->withQueryParams(['id' => '2']));
  8287. $expected = ['context'];
  8288. $result = $this->Form->getValueSources();
  8289. $this->assertEquals($expected, $result);
  8290. $expected = ['query', 'data', 'context'];
  8291. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  8292. $result = $this->Form->getValueSources();
  8293. $this->assertEquals($expected, $result);
  8294. $this->Form->setValueSources(['context']);
  8295. $expected = '1';
  8296. $result = $this->Form->getSourceValue('id');
  8297. $this->assertEquals($expected, $result);
  8298. $this->Form->setValueSources('query');
  8299. $expected = ['query'];
  8300. $result = $this->Form->getValueSources();
  8301. $this->assertEquals($expected, $result);
  8302. $expected = '2';
  8303. $result = $this->Form->getSourceValue('id');
  8304. $this->assertEquals($expected, $result);
  8305. $this->Form->setValueSources(['data']);
  8306. $expected = '1';
  8307. $result = $this->Form->getSourceValue('id');
  8308. $this->assertEquals($expected, $result);
  8309. $this->Form->setValueSources(['query', 'data']);
  8310. $expected = '2';
  8311. $result = $this->Form->getSourceValue('id');
  8312. $this->assertEquals($expected, $result);
  8313. }
  8314. /**
  8315. * Tests the different input rendering values based on sources values switching
  8316. *
  8317. * @return void
  8318. */
  8319. public function testFormValueSourcesSingleSwitchRendering()
  8320. {
  8321. $this->loadFixtures('Articles');
  8322. $articles = $this->getTableLocator()->get('Articles');
  8323. $article = new Article();
  8324. $articles->patchEntity($article, ['id' => '3']);
  8325. $this->Form->create($article);
  8326. $this->Form->setValueSources(['context']);
  8327. $result = $this->Form->control('id');
  8328. $expected = [
  8329. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8330. ];
  8331. $this->assertHtml($expected, $result);
  8332. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  8333. $this->Form->setValueSources(['query']);
  8334. $result = $this->Form->control('id');
  8335. $expected = [
  8336. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8337. ];
  8338. $this->assertHtml($expected, $result);
  8339. $this->View->setRequest($this->View->getRequest()
  8340. ->withQueryParams(['id' => '5a'])
  8341. ->withData('id', '5b'));
  8342. $this->Form->setValueSources(['context']);
  8343. $this->Form->create($article);
  8344. $result = $this->Form->control('id');
  8345. $expected = [
  8346. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8347. ];
  8348. $this->assertHtml($expected, $result);
  8349. $this->Form->setValueSources(['data']);
  8350. $this->Form->create($article);
  8351. $result = $this->Form->control('id');
  8352. $expected = [
  8353. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8354. ];
  8355. $this->assertHtml($expected, $result);
  8356. $this->Form->setValueSources(['query']);
  8357. $result = $this->Form->control('id');
  8358. $expected = [
  8359. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8360. ];
  8361. $this->assertHtml($expected, $result);
  8362. }
  8363. /**
  8364. * Tests the different input rendering values based on sources values switching while supplying
  8365. * an entity (base context) and multiple sources (such as data, query)
  8366. *
  8367. * @return void
  8368. */
  8369. public function testFormValueSourcesListSwitchRendering()
  8370. {
  8371. $this->loadFixtures('Articles');
  8372. $articles = $this->getTableLocator()->get('Articles');
  8373. $article = new Article();
  8374. $articles->patchEntity($article, ['id' => '3']);
  8375. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  8376. $this->Form->create($article);
  8377. $this->Form->setValueSources(['context', 'query']);
  8378. $result = $this->Form->control('id');
  8379. $expected = [
  8380. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8381. ];
  8382. $this->assertHtml($expected, $result);
  8383. $this->Form->setValueSources(['query', 'context']);
  8384. $result = $this->Form->control('id');
  8385. $expected = [
  8386. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8387. ];
  8388. $this->assertHtml($expected, $result);
  8389. $this->Form->setValueSources(['data', 'query', 'context']);
  8390. $result = $this->Form->control('id');
  8391. $expected = [
  8392. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8393. ];
  8394. $this->assertHtml($expected, $result);
  8395. $this->View->setRequest($this->View->getRequest()
  8396. ->withData('id', '8')
  8397. ->withQueryParams(['id' => '9']));
  8398. $this->Form->setValueSources(['data', 'query', 'context']);
  8399. $result = $this->Form->control('id');
  8400. $expected = [
  8401. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8402. ];
  8403. $this->assertHtml($expected, $result);
  8404. }
  8405. /**
  8406. * Test the different form input renderings based on values sources switchings through form options
  8407. *
  8408. * @return void
  8409. */
  8410. public function testFormValueSourcesSwitchViaOptionsRendering()
  8411. {
  8412. $this->loadFixtures('Articles');
  8413. $articles = $this->getTableLocator()->get('Articles');
  8414. $article = new Article();
  8415. $articles->patchEntity($article, ['id' => '3']);
  8416. $this->View->setRequest(
  8417. $this->View->getRequest()->withData('id', 4)->withQueryParams(['id' => 5])
  8418. );
  8419. $this->Form->create($article, ['valueSources' => 'query']);
  8420. $result = $this->Form->control('id');
  8421. $expected = [
  8422. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8423. ];
  8424. $this->assertHtml($expected, $result);
  8425. $result = $this->Form->getSourceValue('id');
  8426. $this->assertEquals('5', $result);
  8427. $this->Form->setValueSources(['context']);
  8428. $this->Form->create($article, ['valueSources' => 'query']);
  8429. $result = $this->Form->control('id');
  8430. $expected = [
  8431. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8432. ];
  8433. $this->assertHtml($expected, $result);
  8434. $result = $this->Form->getSourceValue('id');
  8435. $this->assertEquals('5', $result);
  8436. $this->Form->setValueSources(['query']);
  8437. $this->Form->create($article, ['valueSources' => 'data']);
  8438. $result = $this->Form->control('id');
  8439. $expected = [
  8440. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8441. ];
  8442. $this->assertHtml($expected, $result);
  8443. $result = $this->Form->getSourceValue('id');
  8444. $this->assertEquals('4', $result);
  8445. $this->Form->setValueSources(['query']);
  8446. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8447. $result = $this->Form->control('id');
  8448. $expected = [
  8449. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8450. ];
  8451. $this->assertHtml($expected, $result);
  8452. $result = $this->Form->getSourceValue('id');
  8453. $this->assertEquals('4', $result);
  8454. }
  8455. /**
  8456. * Test the different form input renderings based on values sources switchings through form options
  8457. *
  8458. * @return void
  8459. */
  8460. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8461. {
  8462. $this->loadFixtures('Articles');
  8463. $articles = $this->getTableLocator()->get('Articles');
  8464. $article = new Article();
  8465. $articles->patchEntity($article, ['id' => '3']);
  8466. $this->View->setRequest(
  8467. $this->View->getRequest()->withData('id', 10)->withQueryParams(['id' => 11])
  8468. );
  8469. $this->Form->setValueSources(['context'])
  8470. ->create($article, ['valueSources' => ['query', 'data']]);
  8471. $result = $this->Form->control('id');
  8472. $expected = [
  8473. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8474. ];
  8475. $this->assertHtml($expected, $result);
  8476. $result = $this->Form->getSourceValue('id');
  8477. $this->assertEquals('11', $result);
  8478. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  8479. $this->Form->setValueSources(['context'])
  8480. ->create($article, ['valueSources' => ['query', 'data']]);
  8481. $result = $this->Form->control('id');
  8482. $expected = [
  8483. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8484. ];
  8485. $this->assertHtml($expected, $result);
  8486. $result = $this->Form->getSourceValue('id');
  8487. $this->assertEquals('10', $result);
  8488. }
  8489. /**
  8490. * Test the different form values sources resetting through From::end();
  8491. *
  8492. * @return void
  8493. */
  8494. public function testFormValueSourcesResetViaEnd()
  8495. {
  8496. $expected = ['context'];
  8497. $result = $this->Form->getValueSources();
  8498. $this->assertEquals($expected, $result);
  8499. $expected = ['query', 'context', 'data'];
  8500. $this->Form->setValueSources(['query', 'context', 'data']);
  8501. $result = $this->Form->getValueSources();
  8502. $this->assertEquals($expected, $result);
  8503. $this->Form->create();
  8504. $result = $this->Form->getValueSources();
  8505. $this->assertEquals($expected, $result);
  8506. $this->Form->end();
  8507. $result = $this->Form->getValueSources();
  8508. $this->assertEquals(['context'], $result);
  8509. }
  8510. /**
  8511. * Test sources values defaults handling
  8512. *
  8513. * @return void
  8514. */
  8515. public function testFormValueSourcesDefaults()
  8516. {
  8517. $this->View->setRequest(
  8518. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  8519. );
  8520. $this->Form->create();
  8521. $result = $this->Form->password('password');
  8522. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8523. $this->assertHtml($expected, $result);
  8524. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8525. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8526. $this->assertHtml($expected, $result);
  8527. $this->Form->setValueSources('query');
  8528. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8529. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8530. $this->assertHtml($expected, $result);
  8531. $this->Form->setValueSources('data');
  8532. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8533. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8534. $this->assertHtml($expected, $result);
  8535. }
  8536. /**
  8537. * Test sources values schema defaults handling
  8538. *
  8539. * @return void
  8540. */
  8541. public function testSourcesValueDoesntExistPassThrough()
  8542. {
  8543. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  8544. $articles = $this->getTableLocator()->get('Articles');
  8545. $entity = $articles->newEntity();
  8546. $this->Form->create($entity);
  8547. $this->Form->setValueSources(['query', 'context']);
  8548. $result = $this->Form->getSourceValue('category');
  8549. $this->assertEquals('sesame-cookies', $result);
  8550. $this->Form->setValueSources(['context', 'query']);
  8551. $result = $this->Form->getSourceValue('category');
  8552. $this->assertEquals('sesame-cookies', $result);
  8553. }
  8554. /**
  8555. * testNestedLabelInput method
  8556. *
  8557. * Test the `nestedInput` parameter
  8558. *
  8559. * @group deprecated
  8560. * @return void
  8561. */
  8562. public function testNestedLabelInput()
  8563. {
  8564. $this->deprecated(function () {
  8565. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8566. $expected = [
  8567. 'div' => ['class' => 'input text'],
  8568. 'label' => ['for' => 'foo'],
  8569. ['input' => [
  8570. 'type' => 'text',
  8571. 'name' => 'foo',
  8572. 'id' => 'foo'
  8573. ]],
  8574. 'Foo',
  8575. '/label',
  8576. '/div'
  8577. ];
  8578. $this->assertHtml($expected, $result);
  8579. });
  8580. }
  8581. /**
  8582. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8583. *
  8584. * This test makes sure `false` excludes the label from the render
  8585. *
  8586. * @return void
  8587. */
  8588. public function testControlLabelManipulationDisableLabels()
  8589. {
  8590. $result = $this->Form->control('test', [
  8591. 'type' => 'radio',
  8592. 'options' => ['A', 'B'],
  8593. 'labelOptions' => false
  8594. ]);
  8595. $expected = [
  8596. ['div' => ['class' => 'input radio']],
  8597. '<label',
  8598. 'Test',
  8599. '/label',
  8600. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8601. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8602. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8603. '/div',
  8604. ];
  8605. $this->assertHtml($expected, $result);
  8606. $result = $this->Form->control('checkbox1', [
  8607. 'label' => 'My checkboxes',
  8608. 'multiple' => 'checkbox',
  8609. 'type' => 'select',
  8610. 'options' => [
  8611. ['text' => 'First Checkbox', 'value' => 1],
  8612. ['text' => 'Second Checkbox', 'value' => 2]
  8613. ],
  8614. 'labelOptions' => false
  8615. ]);
  8616. $expected = [
  8617. ['div' => ['class' => 'input select']],
  8618. ['label' => ['for' => 'checkbox1']],
  8619. 'My checkboxes',
  8620. '/label',
  8621. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8622. ['div' => ['class' => 'checkbox']],
  8623. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8624. '/div',
  8625. ['div' => ['class' => 'checkbox']],
  8626. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8627. '/div',
  8628. '/div'
  8629. ];
  8630. $this->assertHtml($expected, $result);
  8631. }
  8632. /**
  8633. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8634. *
  8635. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8636. * added to the class if checked.
  8637. *
  8638. * Also checks to make sure any custom attributes are rendered correctly
  8639. *
  8640. * @return void
  8641. */
  8642. public function testControlLabelManipulationRadios()
  8643. {
  8644. $result = $this->Form->control('test', [
  8645. 'type' => 'radio',
  8646. 'options' => ['A', 'B'],
  8647. 'labelOptions' => ['class' => 'custom-class']
  8648. ]);
  8649. $expected = [
  8650. ['div' => ['class' => 'input radio']],
  8651. '<label',
  8652. 'Test',
  8653. '/label',
  8654. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8655. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8656. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8657. 'A',
  8658. '/label',
  8659. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8660. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8661. 'B',
  8662. '/label',
  8663. '/div',
  8664. ];
  8665. $this->assertHtml($expected, $result);
  8666. $result = $this->Form->control('test', [
  8667. 'type' => 'radio',
  8668. 'options' => ['A', 'B'],
  8669. 'value' => 1,
  8670. 'labelOptions' => ['class' => 'custom-class']
  8671. ]);
  8672. $expected = [
  8673. ['div' => ['class' => 'input radio']],
  8674. '<label',
  8675. 'Test',
  8676. '/label',
  8677. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8678. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8679. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8680. 'A',
  8681. '/label',
  8682. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8683. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8684. 'B',
  8685. '/label',
  8686. '/div',
  8687. ];
  8688. $this->assertHtml($expected, $result);
  8689. $result = $this->Form->control('test', [
  8690. 'type' => 'radio',
  8691. 'options' => ['A', 'B'],
  8692. 'value' => 1,
  8693. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8694. ]);
  8695. $expected = [
  8696. ['div' => ['class' => 'input radio']],
  8697. '<label',
  8698. 'Test',
  8699. '/label',
  8700. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8701. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8702. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8703. 'A',
  8704. '/label',
  8705. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8706. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8707. 'B',
  8708. '/label',
  8709. '/div',
  8710. ];
  8711. $this->assertHtml($expected, $result);
  8712. $result = $this->Form->radio('test', ['A', 'B'], [
  8713. 'label' => [
  8714. 'class' => ['custom-class', 'another-class'],
  8715. 'data-name' => 'bob'
  8716. ],
  8717. 'value' => 1
  8718. ]);
  8719. $expected = [
  8720. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8721. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8722. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8723. 'A',
  8724. '/label',
  8725. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8726. ['input' => [
  8727. 'type' => 'radio',
  8728. 'name' => 'test',
  8729. 'value' => '1',
  8730. 'id' => 'test-1',
  8731. 'checked' => 'checked'
  8732. ]],
  8733. 'B',
  8734. '/label',
  8735. ];
  8736. $this->assertHtml($expected, $result);
  8737. }
  8738. /**
  8739. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8740. *
  8741. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8742. * added to the class if checked.
  8743. *
  8744. * Also checks to make sure any custom attributes are rendered correctly
  8745. *
  8746. * @return void
  8747. */
  8748. public function testControlLabelManipulationCheckboxes()
  8749. {
  8750. $result = $this->Form->control('checkbox1', [
  8751. 'label' => 'My checkboxes',
  8752. 'multiple' => 'checkbox',
  8753. 'type' => 'select',
  8754. 'options' => [
  8755. ['text' => 'First Checkbox', 'value' => 1],
  8756. ['text' => 'Second Checkbox', 'value' => 2]
  8757. ],
  8758. 'labelOptions' => ['class' => 'custom-class'],
  8759. 'value' => ['1']
  8760. ]);
  8761. $expected = [
  8762. ['div' => ['class' => 'input select']],
  8763. ['label' => ['for' => 'checkbox1']],
  8764. 'My checkboxes',
  8765. '/label',
  8766. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8767. ['div' => ['class' => 'checkbox']],
  8768. ['label' => [
  8769. 'class' => 'custom-class selected',
  8770. 'for' => 'checkbox1-1'
  8771. ]],
  8772. ['input' => [
  8773. 'type' => 'checkbox',
  8774. 'name' => 'checkbox1[]',
  8775. 'value' => '1',
  8776. 'id' => 'checkbox1-1',
  8777. 'checked' => 'checked'
  8778. ]],
  8779. 'First Checkbox',
  8780. '/label',
  8781. '/div',
  8782. ['div' => ['class' => 'checkbox']],
  8783. ['label' => [
  8784. 'class' => 'custom-class',
  8785. 'for' => 'checkbox1-2'
  8786. ]],
  8787. ['input' => [
  8788. 'type' => 'checkbox',
  8789. 'name' => 'checkbox1[]',
  8790. 'value' => '2',
  8791. 'id' => 'checkbox1-2'
  8792. ]],
  8793. 'Second Checkbox',
  8794. '/label',
  8795. '/div',
  8796. '/div'
  8797. ];
  8798. $this->assertHtml($expected, $result);
  8799. $result = $this->Form->control('checkbox1', [
  8800. 'label' => 'My checkboxes',
  8801. 'multiple' => 'checkbox',
  8802. 'type' => 'select',
  8803. 'options' => [
  8804. ['text' => 'First Checkbox', 'value' => 1],
  8805. ['text' => 'Second Checkbox', 'value' => 2]
  8806. ],
  8807. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8808. 'value' => ['1']
  8809. ]);
  8810. $expected = [
  8811. ['div' => ['class' => 'input select']],
  8812. ['label' => ['for' => 'checkbox1']],
  8813. 'My checkboxes',
  8814. '/label',
  8815. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8816. ['div' => ['class' => 'checkbox']],
  8817. ['label' => [
  8818. 'class' => 'custom-class another-class selected',
  8819. 'data-name' => 'bob',
  8820. 'for' => 'checkbox1-1'
  8821. ]],
  8822. ['input' => [
  8823. 'type' => 'checkbox',
  8824. 'name' => 'checkbox1[]',
  8825. 'value' => '1',
  8826. 'id' => 'checkbox1-1',
  8827. 'checked' => 'checked'
  8828. ]],
  8829. 'First Checkbox',
  8830. '/label',
  8831. '/div',
  8832. ['div' => ['class' => 'checkbox']],
  8833. ['label' => [
  8834. 'class' => 'custom-class another-class',
  8835. 'data-name' => 'bob',
  8836. 'for' => 'checkbox1-2'
  8837. ]],
  8838. ['input' => [
  8839. 'type' => 'checkbox',
  8840. 'name' => 'checkbox1[]',
  8841. 'value' => '2',
  8842. 'id' => 'checkbox1-2'
  8843. ]],
  8844. 'Second Checkbox',
  8845. '/label',
  8846. '/div',
  8847. '/div'
  8848. ];
  8849. $this->assertHtml($expected, $result);
  8850. }
  8851. }