FormHelperTest.php 302 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199
  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\ORM\TableRegistry;
  23. use Cake\Routing\Router;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\Security;
  26. use Cake\View\Helper\FormHelper;
  27. use Cake\View\View;
  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->schema($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->schema($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. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  134. $this->View = new View();
  135. $this->Form = new FormHelper($this->View);
  136. $request = new ServerRequest([
  137. 'webroot' => '',
  138. 'base' => '',
  139. 'url' => '/articles/add',
  140. 'params' => [
  141. 'controller' => 'articles',
  142. 'action' => 'add',
  143. ]
  144. ]);
  145. $this->Form->Url->request = $this->Form->request = $request;
  146. $this->dateRegex = [
  147. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  148. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  149. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  150. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  151. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  152. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  153. ];
  154. $this->article = [
  155. 'schema' => [
  156. 'id' => ['type' => 'integer'],
  157. 'author_id' => ['type' => 'integer', 'null' => true],
  158. 'title' => ['type' => 'string', 'null' => true],
  159. 'body' => 'text',
  160. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  161. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  162. ],
  163. 'required' => [
  164. 'author_id' => true,
  165. 'title' => true,
  166. ]
  167. ];
  168. Security::salt('foo!');
  169. Router::connect('/:controller', ['action' => 'index']);
  170. Router::connect('/:controller/:action/*');
  171. }
  172. /**
  173. * tearDown method
  174. *
  175. * @return void
  176. */
  177. public function tearDown()
  178. {
  179. parent::tearDown();
  180. unset($this->Form, $this->Controller, $this->View);
  181. TableRegistry::clear();
  182. }
  183. /**
  184. * Test construct() with the templates option.
  185. *
  186. * @return void
  187. */
  188. public function testConstructTemplatesFile()
  189. {
  190. $helper = new FormHelper($this->View, [
  191. 'templates' => 'htmlhelper_tags'
  192. ]);
  193. $result = $helper->control('name');
  194. $this->assertContains('<input', $result);
  195. }
  196. /**
  197. * Test that when specifying custom widgets the config array for that widget
  198. * is overwritten instead of merged.
  199. *
  200. * @return void
  201. */
  202. public function testConstructWithWidgets()
  203. {
  204. $config = [
  205. 'widgets' => [
  206. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  207. ]
  208. ];
  209. $helper = new FormHelper($this->View, $config);
  210. $registry = $helper->widgetRegistry();
  211. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  212. }
  213. /**
  214. * Test that when specifying custom widgets config file and it should be
  215. * added to widgets array. WidgetRegistry will load widgets in constructor.
  216. *
  217. * @return void
  218. */
  219. public function testConstructWithWidgetsConfig()
  220. {
  221. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  222. $registry = $helper->widgetRegistry();
  223. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  224. }
  225. /**
  226. * Test registering a new widget class and rendering it.
  227. *
  228. * @return void
  229. */
  230. public function testAddWidgetAndRenderWidget()
  231. {
  232. $data = [
  233. 'val' => 1
  234. ];
  235. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  236. $this->Form->addWidget('test', $mock);
  237. $mock->expects($this->once())
  238. ->method('render')
  239. ->with($data)
  240. ->will($this->returnValue('HTML'));
  241. $result = $this->Form->widget('test', $data);
  242. $this->assertEquals('HTML', $result);
  243. }
  244. /**
  245. * Test that secureFields() of widget is called after calling render(),
  246. * not before.
  247. *
  248. * @return void
  249. */
  250. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  251. {
  252. $data = [
  253. 'val' => 1,
  254. 'name' => 'test'
  255. ];
  256. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  257. $this->Form->addWidget('test', $mock);
  258. $mock->expects($this->at(0))
  259. ->method('render')
  260. ->with($data)
  261. ->will($this->returnValue('HTML'));
  262. $mock->expects($this->at(1))
  263. ->method('secureFields')
  264. ->with($data)
  265. ->will($this->returnValue(['test']));
  266. $result = $this->Form->widget('test', $data + ['secure' => true]);
  267. $this->assertEquals('HTML', $result);
  268. }
  269. /**
  270. * Test that empty string is not added to secure fields list when
  271. * rendering input widget without name.
  272. *
  273. * @return void
  274. */
  275. public function testRenderingWidgetWithEmptyName()
  276. {
  277. $this->assertEquals([], $this->Form->fields);
  278. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  279. $this->assertEquals('<select name=""></select>', $result);
  280. $this->assertEquals([], $this->Form->fields);
  281. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  282. $this->assertEquals('<select name="0"></select>', $result);
  283. $this->assertEquals(['0'], $this->Form->fields);
  284. }
  285. /**
  286. * Test registering an invalid widget class.
  287. *
  288. * @expectedException \RuntimeException
  289. * @return void
  290. */
  291. public function testAddWidgetInvalid()
  292. {
  293. $mock = new \StdClass();
  294. $this->Form->addWidget('test', $mock);
  295. $this->Form->widget('test');
  296. }
  297. /**
  298. * Test adding a new context class.
  299. *
  300. * @return void
  301. */
  302. public function testAddContextProvider()
  303. {
  304. $context = 'My data';
  305. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  306. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  307. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  308. $this->assertEquals($context, $data['entity']);
  309. return $stub;
  310. });
  311. $this->Form->create($context);
  312. $result = $this->Form->context();
  313. $this->assertSame($stub, $result);
  314. }
  315. /**
  316. * Test replacing a context class.
  317. *
  318. * @return void
  319. */
  320. public function testAddContextProviderReplace()
  321. {
  322. $entity = new Article();
  323. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  324. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  325. return $stub;
  326. });
  327. $this->Form->create($entity);
  328. $result = $this->Form->context();
  329. $this->assertSame($stub, $result);
  330. }
  331. /**
  332. * Test overriding a context class.
  333. *
  334. * @return void
  335. */
  336. public function testAddContextProviderAdd()
  337. {
  338. $entity = new Article();
  339. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  340. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  341. if ($data['entity'] instanceof Entity) {
  342. return $stub;
  343. }
  344. });
  345. $this->Form->create($entity);
  346. $result = $this->Form->context();
  347. $this->assertSame($stub, $result);
  348. }
  349. /**
  350. * Test adding an invalid context class.
  351. *
  352. * @expectedException \RuntimeException
  353. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  354. * @return void
  355. */
  356. public function testAddContextProviderInvalid()
  357. {
  358. $context = 'My data';
  359. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  360. return new \StdClass();
  361. });
  362. $this->Form->create($context);
  363. }
  364. /**
  365. * Provides context options for create().
  366. *
  367. * @return array
  368. */
  369. public function contextSelectionProvider()
  370. {
  371. $entity = new Article();
  372. $collection = new Collection([$entity]);
  373. $emptyCollection = new Collection([]);
  374. $arrayObject = new \ArrayObject([]);
  375. $data = [
  376. 'schema' => [
  377. 'title' => ['type' => 'string']
  378. ]
  379. ];
  380. $form = new Form();
  381. return [
  382. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  383. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  384. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  385. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  386. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  387. 'form' => [$form, 'Cake\View\Form\FormContext'],
  388. 'none' => [null, 'Cake\View\Form\NullContext'],
  389. 'false' => [false, 'Cake\View\Form\NullContext'],
  390. ];
  391. }
  392. /**
  393. * Test default context selection in create()
  394. *
  395. * @dataProvider contextSelectionProvider
  396. * @return void
  397. */
  398. public function testCreateContextSelectionBuiltIn($data, $class)
  399. {
  400. $this->loadFixtures('Articles');
  401. $this->Form->create($data);
  402. $this->assertInstanceOf($class, $this->Form->context());
  403. }
  404. /**
  405. * Data provider for type option.
  406. *
  407. * @return array
  408. */
  409. public static function requestTypeProvider()
  410. {
  411. return [
  412. // type, method, override
  413. ['post', 'post', 'POST'],
  414. ['put', 'post', 'PUT'],
  415. ['patch', 'post', 'PATCH'],
  416. ['delete', 'post', 'DELETE'],
  417. ];
  418. }
  419. /**
  420. * Test creating file forms.
  421. *
  422. * @return void
  423. */
  424. public function testCreateFile()
  425. {
  426. $encoding = strtolower(Configure::read('App.encoding'));
  427. $result = $this->Form->create(false, ['type' => 'file']);
  428. $expected = [
  429. 'form' => [
  430. 'method' => 'post', 'action' => '/articles/add',
  431. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  432. ],
  433. 'div' => ['style' => 'display:none;'],
  434. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  435. '/div'
  436. ];
  437. $this->assertHtml($expected, $result);
  438. }
  439. /**
  440. * Test creating GET forms.
  441. *
  442. * @return void
  443. */
  444. public function testCreateGet()
  445. {
  446. $encoding = strtolower(Configure::read('App.encoding'));
  447. $result = $this->Form->create(false, ['type' => 'get']);
  448. $expected = ['form' => [
  449. 'method' => 'get', 'action' => '/articles/add',
  450. 'accept-charset' => $encoding
  451. ]];
  452. $this->assertHtml($expected, $result);
  453. }
  454. /**
  455. * Test explicit method/enctype options.
  456. *
  457. * Explicit method overwrites inferred method from 'type'
  458. *
  459. * @return void
  460. */
  461. public function testCreateExplicitMethodEnctype()
  462. {
  463. $encoding = strtolower(Configure::read('App.encoding'));
  464. $result = $this->Form->create(false, [
  465. 'type' => 'get',
  466. 'method' => 'put',
  467. 'enctype' => 'multipart/form-data'
  468. ]);
  469. $expected = ['form' => [
  470. 'method' => 'put',
  471. 'action' => '/articles/add',
  472. 'enctype' => 'multipart/form-data',
  473. 'accept-charset' => $encoding
  474. ]];
  475. $this->assertHtml($expected, $result);
  476. }
  477. /**
  478. * Test create() with the templates option.
  479. *
  480. * @return void
  481. */
  482. public function testCreateTemplatesArray()
  483. {
  484. $result = $this->Form->create($this->article, [
  485. 'templates' => [
  486. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  487. ]
  488. ]);
  489. $expected = [
  490. 'form' => [
  491. 'class' => 'form-horizontal',
  492. 'method' => 'post',
  493. 'action' => '/articles/add',
  494. 'accept-charset' => 'utf-8'
  495. ]
  496. ];
  497. $this->assertHtml($expected, $result);
  498. }
  499. /**
  500. * Test create() with the templates option.
  501. *
  502. * @return void
  503. */
  504. public function testCreateTemplatesFile()
  505. {
  506. $result = $this->Form->create($this->article, [
  507. 'templates' => 'htmlhelper_tags',
  508. ]);
  509. $expected = [
  510. 'start form',
  511. 'div' => ['class' => 'hidden'],
  512. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  513. '/div'
  514. ];
  515. $this->assertHtml($expected, $result);
  516. }
  517. /**
  518. * Test that create() and end() restore templates.
  519. *
  520. * @return void
  521. */
  522. public function testCreateEndRestoreTemplates()
  523. {
  524. $this->Form->create($this->article, [
  525. 'templates' => ['input' => 'custom input element']
  526. ]);
  527. $this->Form->end();
  528. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  529. }
  530. /**
  531. * Test using template vars in various templates used by control() method.
  532. *
  533. * @return void
  534. */
  535. public function testControlTemplateVars()
  536. {
  537. $result = $this->Form->control('text', [
  538. 'templates' => [
  539. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  540. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  541. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  542. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  543. ],
  544. 'templateVars' => [
  545. 'forinput' => 'in-input',
  546. 'forlabel' => 'in-label',
  547. 'forgroup' => 'in-group',
  548. 'forcontainer' => 'in-container'
  549. ]
  550. ]);
  551. $expected = [
  552. 'div' => ['class'],
  553. 'label' => ['for'],
  554. 'Text in-label',
  555. '/label',
  556. 'in-group',
  557. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  558. 'in-container',
  559. '/div',
  560. ];
  561. $this->assertHtml($expected, $result);
  562. }
  563. /**
  564. * Test ensuring template variables work in template files loaded
  565. * during control().
  566. *
  567. * @return void
  568. */
  569. public function testControlTemplatesFromFile()
  570. {
  571. $result = $this->Form->control('title', [
  572. 'templates' => 'test_templates',
  573. 'templateVars' => [
  574. 'forcontainer' => 'container-data'
  575. ]
  576. ]);
  577. $expected = [
  578. 'div' => ['class'],
  579. 'label' => ['for'],
  580. 'Title',
  581. '/label',
  582. 'input' => ['name', 'type' => 'text', 'id'],
  583. 'container-data',
  584. '/div',
  585. ];
  586. $this->assertHtml($expected, $result);
  587. }
  588. /**
  589. * Test using template vars in inputSubmit and submitContainer template.
  590. *
  591. * @return void
  592. */
  593. public function testSubmitTemplateVars()
  594. {
  595. $this->Form->templates([
  596. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  597. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  598. ]);
  599. $result = $this->Form->submit('Submit', [
  600. 'templateVars' => [
  601. 'forinput' => 'in-input',
  602. 'forcontainer' => 'in-container'
  603. ]
  604. ]);
  605. $expected = [
  606. 'div' => ['class'],
  607. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  608. 'in-container',
  609. '/div',
  610. ];
  611. $this->assertHtml($expected, $result);
  612. }
  613. /**
  614. * test the create() method
  615. *
  616. * @dataProvider requestTypeProvider
  617. * @return void
  618. */
  619. public function testCreateTypeOptions($type, $method, $override)
  620. {
  621. $encoding = strtolower(Configure::read('App.encoding'));
  622. $result = $this->Form->create(false, ['type' => $type]);
  623. $expected = [
  624. 'form' => [
  625. 'method' => $method, 'action' => '/articles/add',
  626. 'accept-charset' => $encoding
  627. ],
  628. 'div' => ['style' => 'display:none;'],
  629. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  630. '/div'
  631. ];
  632. $this->assertHtml($expected, $result);
  633. }
  634. /**
  635. * Test using template vars in Create (formStart template)
  636. *
  637. * @return void
  638. */
  639. public function testCreateTemplateVars()
  640. {
  641. $result = $this->Form->create($this->article, [
  642. 'templates' => [
  643. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  644. ],
  645. 'templateVars' => ['header' => 'headertext']
  646. ]);
  647. $expected = [
  648. 'h4' => ['class'],
  649. 'headertext',
  650. '/h4',
  651. 'form' => [
  652. 'method' => 'post',
  653. 'action' => '/articles/add',
  654. 'accept-charset' => 'utf-8'
  655. ]
  656. ];
  657. $this->assertHtml($expected, $result);
  658. }
  659. /**
  660. * Test opening a form for an update operation.
  661. *
  662. * @return void
  663. */
  664. public function testCreateUpdateForm()
  665. {
  666. $encoding = strtolower(Configure::read('App.encoding'));
  667. $this->Form->request->here = '/articles/edit/1';
  668. $this->Form->request->params['action'] = 'edit';
  669. $this->article['defaults']['id'] = 1;
  670. $result = $this->Form->create($this->article);
  671. $expected = [
  672. 'form' => [
  673. 'method' => 'post',
  674. 'action' => '/articles/edit/1',
  675. 'accept-charset' => $encoding
  676. ],
  677. 'div' => ['style' => 'display:none;'],
  678. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  679. '/div'
  680. ];
  681. $this->assertHtml($expected, $result);
  682. }
  683. /**
  684. * test create() with automatic url generation
  685. *
  686. * @return void
  687. */
  688. public function testCreateAutoUrl()
  689. {
  690. $encoding = strtolower(Configure::read('App.encoding'));
  691. $this->Form->request->params['action'] = 'delete';
  692. $this->Form->request->here = '/articles/delete/10';
  693. $this->Form->request->base = '';
  694. $result = $this->Form->create($this->article);
  695. $expected = [
  696. 'form' => [
  697. 'method' => 'post', 'action' => '/articles/delete/10',
  698. 'accept-charset' => $encoding
  699. ],
  700. 'div' => ['style' => 'display:none;'],
  701. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  702. '/div'
  703. ];
  704. $this->assertHtml($expected, $result);
  705. $this->article['defaults'] = ['id' => 1];
  706. $this->Form->request->here = '/articles/edit/1';
  707. $this->Form->request->params['action'] = 'delete';
  708. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  709. $expected = [
  710. 'form' => [
  711. 'method' => 'post',
  712. 'action' => '/articles/edit/1',
  713. 'accept-charset' => $encoding
  714. ],
  715. 'div' => ['style' => 'display:none;'],
  716. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  717. '/div'
  718. ];
  719. $this->assertHtml($expected, $result);
  720. $this->Form->request->params['action'] = 'add';
  721. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  722. $expected = [
  723. 'form' => [
  724. 'method' => 'post',
  725. 'action' => '/articles/publish/1',
  726. 'accept-charset' => $encoding
  727. ],
  728. 'div' => ['style' => 'display:none;'],
  729. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  730. '/div'
  731. ];
  732. $this->assertHtml($expected, $result);
  733. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  734. $expected = [
  735. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  736. 'div' => ['style' => 'display:none;'],
  737. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  738. '/div'
  739. ];
  740. $this->assertHtml($expected, $result);
  741. $this->Form->request->params['controller'] = 'Pages';
  742. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  743. $expected = [
  744. 'form' => [
  745. 'method' => 'post', 'action' => '/Pages/signup/1',
  746. 'accept-charset' => $encoding
  747. ],
  748. 'div' => ['style' => 'display:none;'],
  749. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  750. '/div'
  751. ];
  752. $this->assertHtml($expected, $result);
  753. }
  754. /**
  755. * Test create() with no URL (no "action" attribute for <form> tag)
  756. *
  757. * @return void
  758. */
  759. public function testCreateNoUrl()
  760. {
  761. $result = $this->Form->create(false, ['url' => false]);
  762. $expected = [
  763. 'form' => [
  764. 'method' => 'post',
  765. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  766. ],
  767. 'div' => ['style' => 'display:none;'],
  768. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  769. '/div'
  770. ];
  771. $this->assertHtml($expected, $result);
  772. }
  773. /**
  774. * test create() with a custom route
  775. *
  776. * @return void
  777. */
  778. public function testCreateCustomRoute()
  779. {
  780. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  781. $encoding = strtolower(Configure::read('App.encoding'));
  782. $this->Form->request->params['controller'] = 'users';
  783. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  784. $expected = [
  785. 'form' => [
  786. 'method' => 'post', 'action' => '/login',
  787. 'accept-charset' => $encoding
  788. ],
  789. 'div' => ['style' => 'display:none;'],
  790. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  791. '/div'
  792. ];
  793. $this->assertHtml($expected, $result);
  794. Router::connect(
  795. '/new-article',
  796. ['controller' => 'articles', 'action' => 'myaction'],
  797. ['_name' => 'my-route']
  798. );
  799. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  800. $expected = [
  801. 'form' => [
  802. 'method' => 'post', 'action' => '/new-article',
  803. 'accept-charset' => $encoding,
  804. ],
  805. 'div' => ['style' => 'display:none;'],
  806. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  807. '/div'
  808. ];
  809. $this->assertHtml($expected, $result);
  810. }
  811. /**
  812. * test automatic accept-charset overriding
  813. *
  814. * @return void
  815. */
  816. public function testCreateWithAcceptCharset()
  817. {
  818. $result = $this->Form->create(
  819. $this->article,
  820. [
  821. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  822. ]
  823. );
  824. $expected = [
  825. 'form' => [
  826. 'method' => 'post', 'action' => '/articles',
  827. 'accept-charset' => 'iso-8859-1'
  828. ],
  829. 'div' => ['style' => 'display:none;'],
  830. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  831. '/div'
  832. ];
  833. $this->assertHtml($expected, $result);
  834. }
  835. /**
  836. * Test base form URL when url param is passed with multiple parameters (&)
  837. *
  838. */
  839. public function testCreateQueryStringRequest()
  840. {
  841. $encoding = strtolower(Configure::read('App.encoding'));
  842. $result = $this->Form->create($this->article, [
  843. 'type' => 'post',
  844. 'escape' => false,
  845. 'url' => [
  846. 'controller' => 'controller',
  847. 'action' => 'action',
  848. '?' => ['param1' => 'value1', 'param2' => 'value2']
  849. ]
  850. ]);
  851. $expected = [
  852. 'form' => [
  853. 'method' => 'post',
  854. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  855. 'accept-charset' => $encoding
  856. ],
  857. 'div' => ['style' => 'display:none;'],
  858. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  859. '/div'
  860. ];
  861. $this->assertHtml($expected, $result);
  862. $result = $this->Form->create($this->article, [
  863. 'type' => 'post',
  864. 'url' => [
  865. 'controller' => 'controller',
  866. 'action' => 'action',
  867. '?' => ['param1' => 'value1', 'param2' => 'value2']
  868. ]
  869. ]);
  870. $this->assertHtml($expected, $result);
  871. }
  872. /**
  873. * test that create() doesn't cause errors by multiple id's being in the primary key
  874. * as could happen with multiple select or checkboxes.
  875. *
  876. * @return void
  877. */
  878. public function testCreateWithMultipleIdInData()
  879. {
  880. $encoding = strtolower(Configure::read('App.encoding'));
  881. $this->Form->request->data['Article']['id'] = [1, 2];
  882. $result = $this->Form->create($this->article);
  883. $expected = [
  884. 'form' => [
  885. 'method' => 'post',
  886. 'action' => '/articles/add',
  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. }
  895. /**
  896. * test that create() doesn't add in extra passed params.
  897. *
  898. * @return void
  899. */
  900. public function testCreatePassedArgs()
  901. {
  902. $encoding = strtolower(Configure::read('App.encoding'));
  903. $this->Form->request->data['Article']['id'] = 1;
  904. $result = $this->Form->create($this->article, [
  905. 'type' => 'post',
  906. 'escape' => false,
  907. 'url' => [
  908. 'action' => 'edit',
  909. 'myparam'
  910. ]
  911. ]);
  912. $expected = [
  913. 'form' => [
  914. 'method' => 'post',
  915. 'action' => '/articles/edit/myparam',
  916. 'accept-charset' => $encoding
  917. ],
  918. 'div' => ['style' => 'display:none;'],
  919. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  920. '/div'
  921. ];
  922. $this->assertHtml($expected, $result);
  923. }
  924. /**
  925. * test creating a get form, and get form inputs.
  926. *
  927. * @return void
  928. */
  929. public function testGetFormCreate()
  930. {
  931. $encoding = strtolower(Configure::read('App.encoding'));
  932. $result = $this->Form->create($this->article, ['type' => 'get']);
  933. $expected = ['form' => [
  934. 'method' => 'get', 'action' => '/articles/add',
  935. 'accept-charset' => $encoding
  936. ]];
  937. $this->assertHtml($expected, $result);
  938. $result = $this->Form->text('title');
  939. $expected = ['input' => [
  940. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  941. ]];
  942. $this->assertHtml($expected, $result);
  943. $result = $this->Form->password('password');
  944. $expected = ['input' => [
  945. 'name' => 'password', 'type' => 'password'
  946. ]];
  947. $this->assertHtml($expected, $result);
  948. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  949. $result = $this->Form->text('user_form');
  950. $expected = ['input' => [
  951. 'name' => 'user_form', 'type' => 'text'
  952. ]];
  953. $this->assertHtml($expected, $result);
  954. }
  955. /**
  956. * test get form, and inputs when the model param is false
  957. *
  958. * @return void
  959. */
  960. public function testGetFormWithFalseModel()
  961. {
  962. $encoding = strtolower(Configure::read('App.encoding'));
  963. $this->Form->request->params['controller'] = 'contact_test';
  964. $result = $this->Form->create(false, [
  965. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  966. ]);
  967. $expected = ['form' => [
  968. 'method' => 'get', 'action' => '/contact_test/add',
  969. 'accept-charset' => $encoding
  970. ]];
  971. $this->assertHtml($expected, $result);
  972. $result = $this->Form->text('reason');
  973. $expected = [
  974. 'input' => ['type' => 'text', 'name' => 'reason']
  975. ];
  976. $this->assertHtml($expected, $result);
  977. }
  978. /**
  979. * testFormCreateWithSecurity method
  980. *
  981. * Test form->create() with security key.
  982. *
  983. * @return void
  984. */
  985. public function testCreateWithSecurity()
  986. {
  987. $this->Form->request->params['_csrfToken'] = 'testKey';
  988. $encoding = strtolower(Configure::read('App.encoding'));
  989. $result = $this->Form->create($this->article, [
  990. 'url' => '/articles/publish',
  991. ]);
  992. $expected = [
  993. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  994. 'div' => ['style' => 'display:none;'],
  995. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  996. ['input' => [
  997. 'type' => 'hidden',
  998. 'name' => '_csrfToken',
  999. 'value' => 'testKey',
  1000. 'autocomplete' => 'off',
  1001. ]],
  1002. '/div'
  1003. ];
  1004. $this->assertHtml($expected, $result);
  1005. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1006. $expected['form']['id'] = 'MyForm';
  1007. $this->assertHtml($expected, $result);
  1008. }
  1009. /**
  1010. * testFormCreateGetNoSecurity method
  1011. *
  1012. * Test form->create() with no security key as its a get form
  1013. *
  1014. * @return void
  1015. */
  1016. public function testCreateEndGetNoSecurity()
  1017. {
  1018. $this->Form->request->params['_csrfToken'] = 'testKey';
  1019. $article = new Article();
  1020. $result = $this->Form->create($article, [
  1021. 'type' => 'get',
  1022. 'url' => '/contacts/add'
  1023. ]);
  1024. $this->assertNotContains('testKey', $result);
  1025. $result = $this->Form->end();
  1026. $this->assertNotContains('testKey', $result);
  1027. }
  1028. /**
  1029. * test that create() clears the fields property so it starts fresh
  1030. *
  1031. * @return void
  1032. */
  1033. public function testCreateClearingFields()
  1034. {
  1035. $this->Form->fields = ['model_id'];
  1036. $this->Form->create($this->article);
  1037. $this->assertEquals([], $this->Form->fields);
  1038. }
  1039. /**
  1040. * Tests form hash generation with model-less data
  1041. *
  1042. * @return void
  1043. */
  1044. public function testValidateHashNoModel()
  1045. {
  1046. $this->Form->request->params['_Token'] = 'foo';
  1047. $result = $this->Form->secure(['anything']);
  1048. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1049. }
  1050. /**
  1051. * Tests that hidden fields generated for checkboxes don't get locked
  1052. *
  1053. * @return void
  1054. */
  1055. public function testNoCheckboxLocking()
  1056. {
  1057. $this->Form->request->params['_Token'] = 'foo';
  1058. $this->assertSame([], $this->Form->fields);
  1059. $this->Form->checkbox('check', ['value' => '1']);
  1060. $this->assertSame($this->Form->fields, ['check']);
  1061. }
  1062. /**
  1063. * testFormSecurityFields method
  1064. *
  1065. * Test generation of secure form hash generation.
  1066. *
  1067. * @return void
  1068. */
  1069. public function testFormSecurityFields()
  1070. {
  1071. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1072. $this->Form->request->params['_Token'] = 'testKey';
  1073. $result = $this->Form->secure($fields);
  1074. $hash = Security::hash(serialize($fields) . Security::salt());
  1075. $hash .= ':' . 'Model.valid';
  1076. $hash = urlencode($hash);
  1077. $tokenDebug = urlencode(json_encode([
  1078. '',
  1079. $fields,
  1080. []
  1081. ]));
  1082. $expected = [
  1083. 'div' => ['style' => 'display:none;'],
  1084. ['input' => [
  1085. 'type' => 'hidden',
  1086. 'name' => '_Token[fields]',
  1087. 'value' => $hash,
  1088. 'autocomplete' => 'off',
  1089. ]],
  1090. ['input' => [
  1091. 'type' => 'hidden',
  1092. 'name' => '_Token[unlocked]',
  1093. 'value' => '',
  1094. 'autocomplete' => 'off',
  1095. ]],
  1096. ['input' => [
  1097. 'type' => 'hidden',
  1098. 'name' => '_Token[debug]',
  1099. 'value' => $tokenDebug,
  1100. 'autocomplete' => 'off',
  1101. ]],
  1102. '/div'
  1103. ];
  1104. $this->assertHtml($expected, $result);
  1105. }
  1106. /**
  1107. * testFormSecurityFields method
  1108. *
  1109. * Test debug token is not generated if debug is false
  1110. *
  1111. * @return void
  1112. */
  1113. public function testFormSecurityFieldsNoDebugMode()
  1114. {
  1115. Configure::write('debug', false);
  1116. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1117. $this->Form->request->params['_Token'] = 'testKey';
  1118. $result = $this->Form->secure($fields);
  1119. $hash = Security::hash(serialize($fields) . Security::salt());
  1120. $hash .= ':' . 'Model.valid';
  1121. $hash = urlencode($hash);
  1122. $expected = [
  1123. 'div' => ['style' => 'display:none;'],
  1124. ['input' => [
  1125. 'type' => 'hidden',
  1126. 'name' => '_Token[fields]',
  1127. 'autocomplete' => 'off',
  1128. 'value' => $hash
  1129. ]],
  1130. ['input' => [
  1131. 'type' => 'hidden',
  1132. 'name' => '_Token[unlocked]',
  1133. 'autocomplete' => 'off',
  1134. 'value' => '',
  1135. ]],
  1136. '/div'
  1137. ];
  1138. $this->assertHtml($expected, $result);
  1139. }
  1140. /**
  1141. * Tests correct generation of number fields for double and float fields
  1142. *
  1143. * @return void
  1144. */
  1145. public function testTextFieldGenerationForFloats()
  1146. {
  1147. $this->article['schema'] = [
  1148. 'foo' => [
  1149. 'type' => 'float',
  1150. 'null' => false,
  1151. 'default' => null,
  1152. 'length' => 10
  1153. ]
  1154. ];
  1155. $this->Form->create($this->article);
  1156. $result = $this->Form->control('foo');
  1157. $expected = [
  1158. 'div' => ['class' => 'input number'],
  1159. 'label' => ['for' => 'foo'],
  1160. 'Foo',
  1161. '/label',
  1162. ['input' => [
  1163. 'type' => 'number',
  1164. 'name' => 'foo',
  1165. 'id' => 'foo',
  1166. 'step' => 'any'
  1167. ]],
  1168. '/div'
  1169. ];
  1170. $this->assertHtml($expected, $result);
  1171. $result = $this->Form->control('foo', ['step' => 0.5]);
  1172. $expected = [
  1173. 'div' => ['class' => 'input number'],
  1174. 'label' => ['for' => 'foo'],
  1175. 'Foo',
  1176. '/label',
  1177. ['input' => [
  1178. 'type' => 'number',
  1179. 'name' => 'foo',
  1180. 'id' => 'foo',
  1181. 'step' => '0.5'
  1182. ]],
  1183. '/div'
  1184. ];
  1185. $this->assertHtml($expected, $result);
  1186. }
  1187. /**
  1188. * Tests correct generation of number fields for integer fields
  1189. *
  1190. * @return void
  1191. */
  1192. public function testTextFieldTypeNumberGenerationForIntegers()
  1193. {
  1194. TableRegistry::get('Contacts', [
  1195. 'className' => __NAMESPACE__ . '\ContactsTable'
  1196. ]);
  1197. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1198. $result = $this->Form->control('age');
  1199. $expected = [
  1200. 'div' => ['class' => 'input number'],
  1201. 'label' => ['for' => 'age'],
  1202. 'Age',
  1203. '/label',
  1204. ['input' => [
  1205. 'type' => 'number', 'name' => 'age',
  1206. 'id' => 'age'
  1207. ]],
  1208. '/div'
  1209. ];
  1210. $this->assertHtml($expected, $result);
  1211. }
  1212. /**
  1213. * Tests correct generation of file upload fields for binary fields
  1214. *
  1215. * @return void
  1216. */
  1217. public function testFileUploadFieldTypeGenerationForBinaries()
  1218. {
  1219. $table = TableRegistry::get('Contacts', [
  1220. 'className' => __NAMESPACE__ . '\ContactsTable'
  1221. ]);
  1222. $table->schema(['foo' => [
  1223. 'type' => 'binary',
  1224. 'null' => false,
  1225. 'default' => null,
  1226. 'length' => 1024
  1227. ]]);
  1228. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1229. $result = $this->Form->control('foo');
  1230. $expected = [
  1231. 'div' => ['class' => 'input file'],
  1232. 'label' => ['for' => 'foo'],
  1233. 'Foo',
  1234. '/label',
  1235. ['input' => [
  1236. 'type' => 'file', 'name' => 'foo',
  1237. 'id' => 'foo'
  1238. ]],
  1239. '/div'
  1240. ];
  1241. $this->assertHtml($expected, $result);
  1242. }
  1243. /**
  1244. * testFormSecurityMultipleFields method
  1245. *
  1246. * Test secure() with multiple row form. Ensure hash is correct.
  1247. *
  1248. * @return void
  1249. */
  1250. public function testFormSecurityMultipleFields()
  1251. {
  1252. $this->Form->request->params['_Token'] = 'foo';
  1253. $fields = [
  1254. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1255. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1256. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1257. ];
  1258. $result = $this->Form->secure($fields);
  1259. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1260. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1261. $tokenDebug = urlencode(json_encode([
  1262. '',
  1263. $fields,
  1264. []
  1265. ]));
  1266. $expected = [
  1267. 'div' => ['style' => 'display:none;'],
  1268. ['input' => [
  1269. 'type' => 'hidden',
  1270. 'name' => '_Token[fields]',
  1271. 'value' => $hash,
  1272. 'autocomplete' => 'off',
  1273. ]],
  1274. ['input' => [
  1275. 'type' => 'hidden',
  1276. 'name' => '_Token[unlocked]',
  1277. 'autocomplete' => 'off',
  1278. 'value' => ''
  1279. ]],
  1280. ['input' => [
  1281. 'type' => 'hidden',
  1282. 'name' => '_Token[debug]',
  1283. 'value' => $tokenDebug,
  1284. 'autocomplete' => 'off',
  1285. ]],
  1286. '/div'
  1287. ];
  1288. $this->assertHtml($expected, $result);
  1289. }
  1290. /**
  1291. * testFormSecurityMultipleSubmitButtons
  1292. *
  1293. * test form submit generation and ensure that _Token is only created on end()
  1294. *
  1295. * @return void
  1296. */
  1297. public function testFormSecurityMultipleSubmitButtons()
  1298. {
  1299. $this->Form->request->params['_Token'] = 'testKey';
  1300. $this->Form->create($this->article);
  1301. $this->Form->text('Address.title');
  1302. $this->Form->text('Address.first_name');
  1303. $result = $this->Form->submit('Save', ['name' => 'save']);
  1304. $expected = [
  1305. 'div' => ['class' => 'submit'],
  1306. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1307. '/div',
  1308. ];
  1309. $this->assertHtml($expected, $result);
  1310. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1311. $expected = [
  1312. 'div' => ['class' => 'submit'],
  1313. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1314. '/div',
  1315. ];
  1316. $this->assertHtml($expected, $result);
  1317. $result = $this->Form->end();
  1318. $tokenDebug = urlencode(json_encode([
  1319. '/articles/add',
  1320. [
  1321. 'Address.title',
  1322. 'Address.first_name',
  1323. ],
  1324. ['save', 'cancel']
  1325. ]));
  1326. $expected = [
  1327. 'div' => ['style' => 'display:none;'],
  1328. ['input' => [
  1329. 'type' => 'hidden',
  1330. 'name' => '_Token[fields]',
  1331. 'autocomplete',
  1332. 'value'
  1333. ]],
  1334. ['input' => [
  1335. 'type' => 'hidden',
  1336. 'name' => '_Token[unlocked]',
  1337. 'value' => 'cancel%7Csave',
  1338. 'autocomplete' => 'off',
  1339. ]],
  1340. ['input' => [
  1341. 'type' => 'hidden',
  1342. 'name' => '_Token[debug]',
  1343. 'value' => $tokenDebug,
  1344. 'autocomplete' => 'off',
  1345. ]],
  1346. '/div'
  1347. ];
  1348. $this->assertHtml($expected, $result);
  1349. }
  1350. /**
  1351. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1352. *
  1353. * @return void
  1354. */
  1355. public function testSecurityButtonNestedNamed()
  1356. {
  1357. $key = 'testKey';
  1358. $this->Form->request->params['_csrfToken'] = $key;
  1359. $this->Form->create('Addresses');
  1360. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1361. $result = $this->Form->unlockField();
  1362. $this->assertEquals(['Address.button'], $result);
  1363. }
  1364. /**
  1365. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1366. *
  1367. * @return void
  1368. */
  1369. public function testSecuritySubmitNestedNamed()
  1370. {
  1371. $this->Form->request->params['_Token'] = 'testKey';
  1372. $this->Form->create($this->article);
  1373. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1374. $result = $this->Form->unlockField();
  1375. $this->assertEquals(['Address.button'], $result);
  1376. }
  1377. /**
  1378. * Test that the correct fields are unlocked for image submits with no names.
  1379. *
  1380. * @return void
  1381. */
  1382. public function testSecuritySubmitImageNoName()
  1383. {
  1384. $this->Form->request->params['_Token'] = 'testKey';
  1385. $this->Form->create(false);
  1386. $result = $this->Form->submit('save.png');
  1387. $expected = [
  1388. 'div' => ['class' => 'submit'],
  1389. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1390. '/div'
  1391. ];
  1392. $this->assertHtml($expected, $result);
  1393. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1394. }
  1395. /**
  1396. * Test that the correct fields are unlocked for image submits with names.
  1397. *
  1398. * @return void
  1399. */
  1400. public function testSecuritySubmitImageName()
  1401. {
  1402. $this->Form->request->params['_Token'] = 'testKey';
  1403. $this->Form->create(null);
  1404. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1405. $expected = [
  1406. 'div' => ['class' => 'submit'],
  1407. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1408. '/div'
  1409. ];
  1410. $this->assertHtml($expected, $result);
  1411. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1412. }
  1413. /**
  1414. * testFormSecurityMultipleControlFields method
  1415. *
  1416. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1417. *
  1418. * @return void
  1419. */
  1420. public function testFormSecurityMultipleControlFields()
  1421. {
  1422. $this->Form->request->params['_Token'] = 'testKey';
  1423. $this->Form->create();
  1424. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1425. $this->Form->control('Addresses.0.title');
  1426. $this->Form->control('Addresses.0.first_name');
  1427. $this->Form->control('Addresses.0.last_name');
  1428. $this->Form->control('Addresses.0.address');
  1429. $this->Form->control('Addresses.0.city');
  1430. $this->Form->control('Addresses.0.phone');
  1431. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1432. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1433. $this->Form->control('Addresses.1.title');
  1434. $this->Form->control('Addresses.1.first_name');
  1435. $this->Form->control('Addresses.1.last_name');
  1436. $this->Form->control('Addresses.1.address');
  1437. $this->Form->control('Addresses.1.city');
  1438. $this->Form->control('Addresses.1.phone');
  1439. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1440. $result = $this->Form->secure($this->Form->fields);
  1441. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1442. $tokenDebug = urlencode(json_encode([
  1443. '/articles/add',
  1444. [
  1445. 'Addresses.0.id' => '123456',
  1446. 'Addresses.0.title',
  1447. 'Addresses.0.first_name',
  1448. 'Addresses.0.last_name',
  1449. 'Addresses.0.address',
  1450. 'Addresses.0.city',
  1451. 'Addresses.0.phone',
  1452. 'Addresses.0.primary',
  1453. 'Addresses.1.id' => '654321',
  1454. 'Addresses.1.title',
  1455. 'Addresses.1.first_name',
  1456. 'Addresses.1.last_name',
  1457. 'Addresses.1.address',
  1458. 'Addresses.1.city',
  1459. 'Addresses.1.phone',
  1460. 'Addresses.1.primary',
  1461. ],
  1462. []
  1463. ]));
  1464. $expected = [
  1465. 'div' => ['style' => 'display:none;'],
  1466. ['input' => [
  1467. 'type' => 'hidden',
  1468. 'name' => '_Token[fields]',
  1469. 'value' => $hash,
  1470. 'autocomplete' => 'off',
  1471. ]],
  1472. ['input' => [
  1473. 'type' => 'hidden',
  1474. 'name' => '_Token[unlocked]',
  1475. 'autocomplete' => 'off',
  1476. 'value' => ''
  1477. ]],
  1478. ['input' => [
  1479. 'type' => 'hidden',
  1480. 'name' => '_Token[debug]',
  1481. 'value' => $tokenDebug,
  1482. 'autocomplete' => 'off',
  1483. ]],
  1484. '/div'
  1485. ];
  1486. $this->assertHtml($expected, $result);
  1487. }
  1488. /**
  1489. * testFormSecurityArrayFields method
  1490. *
  1491. * Test form security with Model.field.0 style inputs.
  1492. *
  1493. * @return void
  1494. */
  1495. public function testFormSecurityArrayFields()
  1496. {
  1497. $this->Form->request->params['_Token'] = 'testKey';
  1498. $this->Form->create();
  1499. $this->Form->text('Address.primary.1');
  1500. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1501. $this->Form->text('Address.secondary.1.0');
  1502. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1503. }
  1504. /**
  1505. * testFormSecurityMultipleControlDisabledFields method
  1506. *
  1507. * Test secure form generation with multiple records and disabled fields.
  1508. *
  1509. * @return void
  1510. */
  1511. public function testFormSecurityMultipleControlDisabledFields()
  1512. {
  1513. $this->Form->request->params['_Token'] = [
  1514. 'unlockedFields' => ['first_name', 'address']
  1515. ];
  1516. $this->Form->create();
  1517. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1518. $this->Form->text('Addresses.0.title');
  1519. $this->Form->text('Addresses.0.first_name');
  1520. $this->Form->text('Addresses.0.last_name');
  1521. $this->Form->text('Addresses.0.address');
  1522. $this->Form->text('Addresses.0.city');
  1523. $this->Form->text('Addresses.0.phone');
  1524. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1525. $this->Form->text('Addresses.1.title');
  1526. $this->Form->text('Addresses.1.first_name');
  1527. $this->Form->text('Addresses.1.last_name');
  1528. $this->Form->text('Addresses.1.address');
  1529. $this->Form->text('Addresses.1.city');
  1530. $this->Form->text('Addresses.1.phone');
  1531. $result = $this->Form->secure($this->Form->fields);
  1532. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1533. $tokenDebug = urlencode(json_encode([
  1534. '/articles/add',
  1535. [
  1536. 'Addresses.0.id' => '123456',
  1537. 'Addresses.0.title',
  1538. 'Addresses.0.last_name',
  1539. 'Addresses.0.city',
  1540. 'Addresses.0.phone',
  1541. 'Addresses.1.id' => '654321',
  1542. 'Addresses.1.title',
  1543. 'Addresses.1.last_name',
  1544. 'Addresses.1.city',
  1545. 'Addresses.1.phone'
  1546. ],
  1547. [
  1548. 'first_name',
  1549. 'address'
  1550. ]
  1551. ]));
  1552. $expected = [
  1553. 'div' => ['style' => 'display:none;'],
  1554. ['input' => [
  1555. 'type' => 'hidden',
  1556. 'name' => '_Token[fields]',
  1557. 'autocomplete' => 'off',
  1558. 'value' => $hash
  1559. ]],
  1560. ['input' => [
  1561. 'type' => 'hidden',
  1562. 'name' => '_Token[unlocked]',
  1563. 'autocomplete' => 'off',
  1564. 'value' => 'address%7Cfirst_name',
  1565. ]],
  1566. ['input' => [
  1567. 'type' => 'hidden',
  1568. 'name' => '_Token[debug]',
  1569. 'autocomplete' => 'off',
  1570. 'value' => $tokenDebug,
  1571. ]],
  1572. '/div'
  1573. ];
  1574. $this->assertHtml($expected, $result);
  1575. }
  1576. /**
  1577. * testFormSecurityControlDisabledFields method
  1578. *
  1579. * Test single record form with disabled fields.
  1580. *
  1581. * @return void
  1582. */
  1583. public function testFormSecurityControlUnlockedFields()
  1584. {
  1585. $this->Form->request->params['_Token'] = [
  1586. 'unlockedFields' => ['first_name', 'address']
  1587. ];
  1588. $this->Form->create();
  1589. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1590. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1591. $this->Form->text('Addresses.title');
  1592. $this->Form->text('Addresses.first_name');
  1593. $this->Form->text('Addresses.last_name');
  1594. $this->Form->text('Addresses.address');
  1595. $this->Form->text('Addresses.city');
  1596. $this->Form->text('Addresses.phone');
  1597. $result = $this->Form->fields;
  1598. $expected = [
  1599. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1600. 'Addresses.city', 'Addresses.phone'
  1601. ];
  1602. $this->assertEquals($expected, $result);
  1603. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1604. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1605. $tokenDebug = urlencode(json_encode([
  1606. '/articles/add',
  1607. [
  1608. 'Addresses.id' => '123456',
  1609. 'Addresses.title',
  1610. 'Addresses.last_name',
  1611. 'Addresses.city',
  1612. 'Addresses.phone'
  1613. ],
  1614. [
  1615. 'first_name',
  1616. 'address'
  1617. ]
  1618. ]));
  1619. $expected = [
  1620. 'div' => ['style' => 'display:none;'],
  1621. ['input' => [
  1622. 'type' => 'hidden',
  1623. 'name' => '_Token[fields]',
  1624. 'value' => $hash,
  1625. 'autocomplete' => 'off',
  1626. 'data-foo' => 'bar',
  1627. ]],
  1628. ['input' => [
  1629. 'type' => 'hidden',
  1630. 'name' => '_Token[unlocked]',
  1631. 'value' => 'address%7Cfirst_name',
  1632. 'autocomplete' => 'off',
  1633. 'data-foo' => 'bar',
  1634. ]],
  1635. ['input' => [
  1636. 'type' => 'hidden', 'name' => '_Token[debug]',
  1637. 'value' => $tokenDebug,
  1638. 'autocomplete' => 'off',
  1639. 'data-foo' => 'bar'
  1640. ]],
  1641. '/div'
  1642. ];
  1643. $this->assertHtml($expected, $result);
  1644. }
  1645. /**
  1646. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1647. *
  1648. * Test single record form with debugSecurity param.
  1649. *
  1650. * @return void
  1651. */
  1652. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1653. {
  1654. $this->Form->request->params['_Token'] = [
  1655. 'unlockedFields' => ['first_name', 'address']
  1656. ];
  1657. $this->Form->create();
  1658. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1659. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1660. $this->Form->text('Addresses.title');
  1661. $this->Form->text('Addresses.first_name');
  1662. $this->Form->text('Addresses.last_name');
  1663. $this->Form->text('Addresses.address');
  1664. $this->Form->text('Addresses.city');
  1665. $this->Form->text('Addresses.phone');
  1666. $result = $this->Form->fields;
  1667. $expected = [
  1668. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1669. 'Addresses.city', 'Addresses.phone'
  1670. ];
  1671. $this->assertEquals($expected, $result);
  1672. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1673. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1674. $tokenDebug = urlencode(json_encode([
  1675. '/articles/add',
  1676. [
  1677. 'Addresses.id' => '123456',
  1678. 'Addresses.title',
  1679. 'Addresses.last_name',
  1680. 'Addresses.city',
  1681. 'Addresses.phone'
  1682. ],
  1683. [
  1684. 'first_name',
  1685. 'address'
  1686. ]
  1687. ]));
  1688. $expected = [
  1689. 'div' => ['style' => 'display:none;'],
  1690. ['input' => [
  1691. 'type' => 'hidden',
  1692. 'name' => '_Token[fields]',
  1693. 'value' => $hash,
  1694. 'autocomplete' => 'off',
  1695. 'data-foo' => 'bar',
  1696. ]],
  1697. ['input' => [
  1698. 'type' => 'hidden',
  1699. 'name' => '_Token[unlocked]',
  1700. 'value' => 'address%7Cfirst_name',
  1701. 'autocomplete' => 'off',
  1702. 'data-foo' => 'bar',
  1703. ]],
  1704. ['input' => [
  1705. 'type' => 'hidden', 'name' => '_Token[debug]',
  1706. 'value' => $tokenDebug,
  1707. 'autocomplete' => 'off',
  1708. 'data-foo' => 'bar'
  1709. ]],
  1710. '/div'
  1711. ];
  1712. $this->assertHtml($expected, $result);
  1713. }
  1714. /**
  1715. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1716. *
  1717. * Debug is false, debugSecurity is true -> no debug
  1718. *
  1719. * @return void
  1720. */
  1721. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1722. {
  1723. $this->Form->request->params['_Token'] = [
  1724. 'unlockedFields' => ['first_name', 'address']
  1725. ];
  1726. $this->Form->create();
  1727. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1728. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1729. $this->Form->text('Addresses.title');
  1730. $this->Form->text('Addresses.first_name');
  1731. $this->Form->text('Addresses.last_name');
  1732. $this->Form->text('Addresses.address');
  1733. $this->Form->text('Addresses.city');
  1734. $this->Form->text('Addresses.phone');
  1735. $result = $this->Form->fields;
  1736. $expected = [
  1737. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1738. 'Addresses.city', 'Addresses.phone'
  1739. ];
  1740. $this->assertEquals($expected, $result);
  1741. Configure::write('debug', false);
  1742. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1743. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1744. $expected = [
  1745. 'div' => ['style' => 'display:none;'],
  1746. ['input' => [
  1747. 'type' => 'hidden',
  1748. 'name' => '_Token[fields]',
  1749. 'value' => $hash,
  1750. 'autocomplete' => 'off',
  1751. 'data-foo' => 'bar',
  1752. ]],
  1753. ['input' => [
  1754. 'type' => 'hidden',
  1755. 'name' => '_Token[unlocked]',
  1756. 'value' => 'address%7Cfirst_name',
  1757. 'autocomplete' => 'off',
  1758. 'data-foo' => 'bar',
  1759. ]],
  1760. '/div'
  1761. ];
  1762. $this->assertHtml($expected, $result);
  1763. }
  1764. /**
  1765. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1766. *
  1767. * Test single record form with debugSecurity param.
  1768. *
  1769. * @return void
  1770. */
  1771. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1772. {
  1773. $this->Form->request->params['_Token'] = [
  1774. 'unlockedFields' => ['first_name', 'address']
  1775. ];
  1776. $this->Form->create();
  1777. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1778. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1779. $this->Form->text('Addresses.title');
  1780. $this->Form->text('Addresses.first_name');
  1781. $this->Form->text('Addresses.last_name');
  1782. $this->Form->text('Addresses.address');
  1783. $this->Form->text('Addresses.city');
  1784. $this->Form->text('Addresses.phone');
  1785. $result = $this->Form->fields;
  1786. $expected = [
  1787. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1788. 'Addresses.city', 'Addresses.phone'
  1789. ];
  1790. $this->assertEquals($expected, $result);
  1791. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1792. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1793. $expected = [
  1794. 'div' => ['style' => 'display:none;'],
  1795. ['input' => [
  1796. 'type' => 'hidden',
  1797. 'name' => '_Token[fields]',
  1798. 'value' => $hash,
  1799. 'autocomplete' => 'off',
  1800. 'data-foo' => 'bar',
  1801. ]],
  1802. ['input' => [
  1803. 'type' => 'hidden',
  1804. 'name' => '_Token[unlocked]',
  1805. 'value' => 'address%7Cfirst_name',
  1806. 'autocomplete' => 'off',
  1807. 'data-foo' => 'bar',
  1808. ]],
  1809. '/div'
  1810. ];
  1811. $this->assertHtml($expected, $result);
  1812. }
  1813. /**
  1814. * testFormSecureWithCustomNameAttribute method
  1815. *
  1816. * Test securing inputs with custom name attributes.
  1817. *
  1818. * @return void
  1819. */
  1820. public function testFormSecureWithCustomNameAttribute()
  1821. {
  1822. $this->Form->request->params['_Token'] = 'testKey';
  1823. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1824. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1825. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1826. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1827. }
  1828. /**
  1829. * testFormSecuredControl method
  1830. *
  1831. * Test generation of entire secure form, assertions made on control() output.
  1832. *
  1833. * @return void
  1834. */
  1835. public function testFormSecuredControl()
  1836. {
  1837. $this->Form->request->params['_csrfToken'] = 'testKey';
  1838. $this->Form->request->params['_Token'] = 'stuff';
  1839. $this->article['schema'] = [
  1840. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1841. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1842. ];
  1843. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1844. $encoding = strtolower(Configure::read('App.encoding'));
  1845. $expected = [
  1846. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1847. 'div' => ['style' => 'display:none;'],
  1848. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1849. ['input' => [
  1850. 'type' => 'hidden',
  1851. 'name' => '_csrfToken',
  1852. 'value' => 'testKey',
  1853. 'autocomplete' => 'off'
  1854. ]],
  1855. '/div'
  1856. ];
  1857. $this->assertHtml($expected, $result);
  1858. $result = $this->Form->control('ratio');
  1859. $expected = [
  1860. 'div' => ['class'],
  1861. 'label' => ['for'],
  1862. 'Ratio',
  1863. '/label',
  1864. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1865. '/div',
  1866. ];
  1867. $this->assertHtml($expected, $result);
  1868. $result = $this->Form->control('population');
  1869. $expected = [
  1870. 'div' => ['class'],
  1871. 'label' => ['for'],
  1872. 'Population',
  1873. '/label',
  1874. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1875. '/div',
  1876. ];
  1877. $this->assertHtml($expected, $result);
  1878. $result = $this->Form->control('published', ['type' => 'text']);
  1879. $expected = [
  1880. 'div' => ['class' => 'input text'],
  1881. 'label' => ['for' => 'published'],
  1882. 'Published',
  1883. '/label',
  1884. ['input' => [
  1885. 'type' => 'text',
  1886. 'name' => 'published',
  1887. 'id' => 'published'
  1888. ]],
  1889. '/div'
  1890. ];
  1891. $this->assertHtml($expected, $result);
  1892. $result = $this->Form->control('other', ['type' => 'text']);
  1893. $expected = [
  1894. 'div' => ['class' => 'input text'],
  1895. 'label' => ['for' => 'other'],
  1896. 'Other',
  1897. '/label',
  1898. ['input' => [
  1899. 'type' => 'text',
  1900. 'name' => 'other',
  1901. 'id',
  1902. ]],
  1903. '/div'
  1904. ];
  1905. $this->assertHtml($expected, $result);
  1906. $result = $this->Form->hidden('stuff');
  1907. $expected = [
  1908. 'input' => [
  1909. 'type' => 'hidden',
  1910. 'name' => 'stuff'
  1911. ]
  1912. ];
  1913. $this->assertHtml($expected, $result);
  1914. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1915. $expected = ['input' => [
  1916. 'type' => 'hidden',
  1917. 'name' => 'hidden',
  1918. 'value' => '0'
  1919. ]];
  1920. $this->assertHtml($expected, $result);
  1921. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1922. $expected = [
  1923. 'div' => ['class' => 'input checkbox'],
  1924. ['input' => [
  1925. 'type' => 'hidden',
  1926. 'name' => 'something',
  1927. 'value' => '0'
  1928. ]],
  1929. 'label' => ['for' => 'something'],
  1930. ['input' => [
  1931. 'type' => 'checkbox',
  1932. 'name' => 'something',
  1933. 'value' => '1',
  1934. 'id' => 'something'
  1935. ]],
  1936. 'Something',
  1937. '/label',
  1938. '/div'
  1939. ];
  1940. $this->assertHtml($expected, $result);
  1941. $result = $this->Form->fields;
  1942. $expectedFields = [
  1943. 'ratio',
  1944. 'population',
  1945. 'published',
  1946. 'other',
  1947. 'stuff' => '',
  1948. 'hidden' => '0',
  1949. 'something'
  1950. ];
  1951. $this->assertEquals($expectedFields, $result);
  1952. $result = $this->Form->secure($this->Form->fields);
  1953. $tokenDebug = urlencode(json_encode([
  1954. '/articles/add',
  1955. $expectedFields,
  1956. []
  1957. ]));
  1958. $expected = [
  1959. 'div' => ['style' => 'display:none;'],
  1960. ['input' => [
  1961. 'type' => 'hidden',
  1962. 'name' => '_Token[fields]',
  1963. 'value',
  1964. 'autocomplete'
  1965. ]],
  1966. ['input' => [
  1967. 'type' => 'hidden',
  1968. 'name' => '_Token[unlocked]',
  1969. 'value' => '',
  1970. 'autocomplete' => 'off'
  1971. ]],
  1972. ['input' => [
  1973. 'type' => 'hidden', 'name' => '_Token[debug]',
  1974. 'value' => $tokenDebug,
  1975. 'autocomplete' => 'off'
  1976. ]],
  1977. '/div'
  1978. ];
  1979. $this->assertHtml($expected, $result);
  1980. }
  1981. /**
  1982. * testSecuredControlCustomName method
  1983. *
  1984. * Test secured inputs with custom names.
  1985. *
  1986. * @return void
  1987. */
  1988. public function testSecuredControlCustomName()
  1989. {
  1990. $this->Form->request->params['_Token'] = 'testKey';
  1991. $this->assertEquals([], $this->Form->fields);
  1992. $this->Form->text('text_input', [
  1993. 'name' => 'Option[General.default_role]',
  1994. ]);
  1995. $expected = ['Option.General.default_role'];
  1996. $this->assertEquals($expected, $this->Form->fields);
  1997. $this->Form->select('select_box', [1, 2], [
  1998. 'name' => 'Option[General.select_role]',
  1999. ]);
  2000. $expected[] = 'Option.General.select_role';
  2001. $this->assertEquals($expected, $this->Form->fields);
  2002. $this->Form->text('other.things[]');
  2003. $expected[] = 'other.things';
  2004. $this->assertEquals($expected, $this->Form->fields);
  2005. }
  2006. /**
  2007. * testSecuredControlDuplicate method
  2008. *
  2009. * Test that a hidden field followed by a visible field
  2010. * undoes the hidden field locking.
  2011. *
  2012. * @return void
  2013. */
  2014. public function testSecuredControlDuplicate()
  2015. {
  2016. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2017. $this->assertEquals([], $this->Form->fields);
  2018. $this->Form->control('text_val', [
  2019. 'type' => 'hidden',
  2020. 'value' => 'some text',
  2021. ]);
  2022. $expected = ['text_val' => 'some text'];
  2023. $this->assertEquals($expected, $this->Form->fields);
  2024. $this->Form->control('text_val', [
  2025. 'type' => 'text',
  2026. ]);
  2027. $expected = ['text_val'];
  2028. $this->assertEquals($expected, $this->Form->fields);
  2029. }
  2030. /**
  2031. * testFormSecuredFileControl method
  2032. *
  2033. * Tests that the correct keys are added to the field hash index.
  2034. *
  2035. * @return void
  2036. */
  2037. public function testFormSecuredFileControl()
  2038. {
  2039. $this->assertEquals([], $this->Form->fields);
  2040. $this->Form->file('Attachment.file');
  2041. $expected = [
  2042. 'Attachment.file.name', 'Attachment.file.type',
  2043. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2044. 'Attachment.file.size'
  2045. ];
  2046. $this->assertEquals($expected, $this->Form->fields);
  2047. }
  2048. /**
  2049. * testFormSecuredMultipleSelect method
  2050. *
  2051. * Test that multiple selects keys are added to field hash.
  2052. *
  2053. * @return void
  2054. */
  2055. public function testFormSecuredMultipleSelect()
  2056. {
  2057. $this->Form->request->params['_csrfToken'] = 'testKey';
  2058. $this->assertEquals([], $this->Form->fields);
  2059. $options = ['1' => 'one', '2' => 'two'];
  2060. $this->Form->select('Model.select', $options);
  2061. $expected = ['Model.select'];
  2062. $this->assertEquals($expected, $this->Form->fields);
  2063. $this->Form->fields = [];
  2064. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2065. $this->assertEquals($expected, $this->Form->fields);
  2066. }
  2067. /**
  2068. * testFormSecuredRadio method
  2069. *
  2070. * @return void
  2071. */
  2072. public function testFormSecuredRadio()
  2073. {
  2074. $this->Form->request->params['_Token'] = 'testKey';
  2075. $this->assertEquals([], $this->Form->fields);
  2076. $options = ['1' => 'option1', '2' => 'option2'];
  2077. $this->Form->radio('Test.test', $options);
  2078. $expected = ['Test.test'];
  2079. $this->assertEquals($expected, $this->Form->fields);
  2080. $this->Form->radio('Test.all', $options, [
  2081. 'disabled' => ['option1', 'option2']
  2082. ]);
  2083. $expected = ['Test.test', 'Test.all' => ''];
  2084. $this->assertEquals($expected, $this->Form->fields);
  2085. $this->Form->radio('Test.some', $options, [
  2086. 'disabled' => ['option1']
  2087. ]);
  2088. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2089. $this->assertEquals($expected, $this->Form->fields);
  2090. }
  2091. /**
  2092. * testFormSecuredAndDisabledNotAssoc method
  2093. *
  2094. * Test that when disabled is in a list based attribute array it works.
  2095. *
  2096. * @return void
  2097. */
  2098. public function testFormSecuredAndDisabledNotAssoc()
  2099. {
  2100. $this->Form->request->params['_Token'] = 'testKey';
  2101. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2102. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2103. $this->Form->text('Model.text', ['disabled']);
  2104. $this->Form->textarea('Model.textarea', ['disabled']);
  2105. $this->Form->password('Model.password', ['disabled']);
  2106. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2107. $expected = [
  2108. 'Model.radio' => ''
  2109. ];
  2110. $this->assertEquals($expected, $this->Form->fields);
  2111. }
  2112. /**
  2113. * testFormSecuredAndDisabled method
  2114. *
  2115. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2116. * hashing.
  2117. *
  2118. * @return void
  2119. */
  2120. public function testFormSecuredAndDisabled()
  2121. {
  2122. $this->Form->request->params['_Token'] = 'testKey';
  2123. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2124. $this->Form->text('Model.text', ['disabled' => true]);
  2125. $this->Form->password('Model.text', ['disabled' => true]);
  2126. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2127. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2128. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2129. $this->Form->year('Model.year', ['disabled' => true]);
  2130. $this->Form->month('Model.month', ['disabled' => true]);
  2131. $this->Form->day('Model.day', ['disabled' => true]);
  2132. $this->Form->hour('Model.hour', ['disabled' => true]);
  2133. $this->Form->minute('Model.minute', ['disabled' => true]);
  2134. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2135. $expected = [
  2136. 'Model.radio' => ''
  2137. ];
  2138. $this->assertEquals($expected, $this->Form->fields);
  2139. }
  2140. /**
  2141. * testDisableSecurityUsingForm method
  2142. *
  2143. * @return void
  2144. */
  2145. public function testDisableSecurityUsingForm()
  2146. {
  2147. $this->Form->request->params['_Token'] = [
  2148. 'disabledFields' => []
  2149. ];
  2150. $this->Form->create();
  2151. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2152. $this->Form->text('Addresses.title');
  2153. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2154. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2155. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2156. $result = $this->Form->fields;
  2157. $expected = [
  2158. 'Addresses.id' => '123456', 'Addresses.title',
  2159. ];
  2160. $this->assertEquals($expected, $result);
  2161. }
  2162. /**
  2163. * testUnlockFieldAddsToList method
  2164. *
  2165. * Test disableField.
  2166. *
  2167. * @return void
  2168. */
  2169. public function testUnlockFieldAddsToList()
  2170. {
  2171. $this->Form->request->params['_Token'] = [
  2172. 'unlockedFields' => []
  2173. ];
  2174. $this->Form->unlockField('Contact.name');
  2175. $this->Form->text('Contact.name');
  2176. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2177. $this->assertEquals([], $this->Form->fields);
  2178. }
  2179. /**
  2180. * testUnlockFieldRemovingFromFields method
  2181. *
  2182. * Test unlockField removing from fields array.
  2183. *
  2184. * @return void
  2185. */
  2186. public function testUnlockFieldRemovingFromFields()
  2187. {
  2188. $this->Form->request->params['_Token'] = [
  2189. 'unlockedFields' => []
  2190. ];
  2191. $this->Form->create($this->article);
  2192. $this->Form->hidden('Article.id', ['value' => 1]);
  2193. $this->Form->text('Article.title');
  2194. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2195. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  2196. $this->Form->unlockField('Article.title');
  2197. $this->Form->unlockField('Article.id');
  2198. $this->assertEquals([], $this->Form->fields);
  2199. }
  2200. /**
  2201. * testResetUnlockFields method
  2202. *
  2203. * Test reset unlockFields, when create new form.
  2204. *
  2205. * @return void
  2206. */
  2207. public function testResetUnlockFields()
  2208. {
  2209. $this->Form->request->params['_Token'] = [
  2210. 'key' => 'testKey',
  2211. 'unlockedFields' => []
  2212. ];
  2213. $this->Form->unlockField('Contact.id');
  2214. $this->Form->create('Contact');
  2215. $this->Form->hidden('Contact.id', ['value' => 1]);
  2216. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2217. $this->Form->end();
  2218. $this->Form->create('Contact');
  2219. $this->Form->hidden('Contact.id', ['value' => 1]);
  2220. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2221. }
  2222. /**
  2223. * testSecuredFormUrlIgnoresHost method
  2224. *
  2225. * Test that only the path + query elements of a form's URL show up in their hash.
  2226. *
  2227. * @return void
  2228. */
  2229. public function testSecuredFormUrlIgnoresHost()
  2230. {
  2231. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2232. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  2233. $this->Form->create($this->article, [
  2234. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2235. ]);
  2236. $result = $this->Form->secure();
  2237. $this->assertContains($expected, $result);
  2238. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2239. $result = $this->Form->secure();
  2240. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2241. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2242. $result = $this->Form->secure();
  2243. $this->assertContains($expected, $result, 'URL path + query should work.');
  2244. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2245. $result = $this->Form->secure();
  2246. $this->assertNotContains($expected, $result, 'URL is different');
  2247. }
  2248. /**
  2249. * testSecuredFormUrlHasHtmlAndIdentifier method
  2250. *
  2251. * Test that URL, HTML and identifier show up in their hashes.
  2252. *
  2253. * @return void
  2254. */
  2255. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2256. {
  2257. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2258. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  2259. $this->Form->create($this->article, [
  2260. 'url' => [
  2261. 'controller' => 'articles',
  2262. 'action' => 'view',
  2263. '?' => [
  2264. 'page' => 1,
  2265. 'limit' => 10,
  2266. 'html' => '<>"',
  2267. ],
  2268. '#' => 'result',
  2269. ],
  2270. ]);
  2271. $result = $this->Form->secure();
  2272. $this->assertContains($expected, $result);
  2273. $this->Form->create($this->article, [
  2274. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2275. ]);
  2276. $result = $this->Form->secure();
  2277. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2278. $this->Form->create($this->article, [
  2279. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2280. ]);
  2281. $result = $this->Form->secure();
  2282. $this->assertContains($expected, $result, 'URL path + query should work.');
  2283. }
  2284. /**
  2285. * testErrorMessageDisplay method
  2286. *
  2287. * Test error message display.
  2288. *
  2289. * @return void
  2290. */
  2291. public function testErrorMessageDisplay()
  2292. {
  2293. $this->article['errors'] = [
  2294. 'Article' => [
  2295. 'title' => 'error message',
  2296. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2297. ]
  2298. ];
  2299. $this->Form->create($this->article);
  2300. $result = $this->Form->control('Article.title');
  2301. $expected = [
  2302. 'div' => ['class' => 'input text error'],
  2303. 'label' => ['for' => 'article-title'],
  2304. 'Title',
  2305. '/label',
  2306. 'input' => [
  2307. 'type' => 'text', 'name' => 'Article[title]',
  2308. 'id' => 'article-title', 'class' => 'form-error'
  2309. ],
  2310. ['div' => ['class' => 'error-message']],
  2311. 'error message',
  2312. '/div',
  2313. '/div'
  2314. ];
  2315. $this->assertHtml($expected, $result);
  2316. $result = $this->Form->control('Article.title', [
  2317. 'templates' => [
  2318. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2319. ]
  2320. ]);
  2321. $expected = [
  2322. 'div' => ['class' => 'input text error'],
  2323. 'label' => ['for' => 'article-title'],
  2324. 'Title',
  2325. '/label',
  2326. 'input' => [
  2327. 'type' => 'text', 'name' => 'Article[title]',
  2328. 'id' => 'article-title', 'class' => 'form-error'
  2329. ],
  2330. '/div'
  2331. ];
  2332. $this->assertHtml($expected, $result);
  2333. $result = $this->Form->control('Article.content');
  2334. $expected = [
  2335. 'div' => ['class' => 'input text error'],
  2336. 'label' => ['for' => 'article-content'],
  2337. 'Content',
  2338. '/label',
  2339. 'input' => [
  2340. 'type' => 'text', 'name' => 'Article[content]',
  2341. 'id' => 'article-content', 'class' => 'form-error'
  2342. ],
  2343. ['div' => ['class' => 'error-message']],
  2344. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2345. '/div',
  2346. '/div'
  2347. ];
  2348. $this->assertHtml($expected, $result);
  2349. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2350. $expected = [
  2351. 'div' => ['class' => 'input text error'],
  2352. 'label' => ['for' => 'article-content'],
  2353. 'Content',
  2354. '/label',
  2355. 'input' => [
  2356. 'type' => 'text', 'name' => 'Article[content]',
  2357. 'id' => 'article-content', 'class' => 'form-error'
  2358. ],
  2359. ['div' => ['class' => 'error-message']],
  2360. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2361. '/div',
  2362. '/div'
  2363. ];
  2364. $this->assertHtml($expected, $result);
  2365. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2366. $expected = [
  2367. 'div' => ['class' => 'input text error'],
  2368. 'label' => ['for' => 'article-content'],
  2369. 'Content',
  2370. '/label',
  2371. 'input' => [
  2372. 'type' => 'text', 'name' => 'Article[content]',
  2373. 'id' => 'article-content', 'class' => 'form-error'
  2374. ],
  2375. ['div' => ['class' => 'error-message']],
  2376. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2377. '/div',
  2378. '/div'
  2379. ];
  2380. $this->assertHtml($expected, $result);
  2381. }
  2382. /**
  2383. * testEmptyErrorValidation method
  2384. *
  2385. * Test validation errors, when validation message is an empty string.
  2386. *
  2387. * @return void
  2388. */
  2389. public function testEmptyErrorValidation()
  2390. {
  2391. $this->article['errors'] = [
  2392. 'Article' => ['title' => '']
  2393. ];
  2394. $this->Form->create($this->article);
  2395. $result = $this->Form->control('Article.title');
  2396. $expected = [
  2397. 'div' => ['class' => 'input text error'],
  2398. 'label' => ['for' => 'article-title'],
  2399. 'Title',
  2400. '/label',
  2401. 'input' => [
  2402. 'type' => 'text', 'name' => 'Article[title]',
  2403. 'id' => 'article-title', 'class' => 'form-error'
  2404. ],
  2405. ['div' => ['class' => 'error-message']],
  2406. [],
  2407. '/div',
  2408. '/div'
  2409. ];
  2410. $this->assertHtml($expected, $result);
  2411. }
  2412. /**
  2413. * testEmptyControlErrorValidation method
  2414. *
  2415. * Test validation errors, when calling control() overriding validation message by an empty string.
  2416. *
  2417. * @return void
  2418. */
  2419. public function testEmptyControlErrorValidation()
  2420. {
  2421. $this->article['errors'] = [
  2422. 'Article' => ['title' => 'error message']
  2423. ];
  2424. $this->Form->create($this->article);
  2425. $result = $this->Form->control('Article.title', ['error' => '']);
  2426. $expected = [
  2427. 'div' => ['class' => 'input text error'],
  2428. 'label' => ['for' => 'article-title'],
  2429. 'Title',
  2430. '/label',
  2431. 'input' => [
  2432. 'type' => 'text', 'name' => 'Article[title]',
  2433. 'id' => 'article-title', 'class' => 'form-error'
  2434. ],
  2435. ['div' => ['class' => 'error-message']],
  2436. [],
  2437. '/div',
  2438. '/div'
  2439. ];
  2440. $this->assertHtml($expected, $result);
  2441. }
  2442. /**
  2443. * testControlErrorMessage method
  2444. *
  2445. * Test validation errors, when calling control() overriding validation messages.
  2446. *
  2447. * @return void
  2448. */
  2449. public function testControlErrorMessage()
  2450. {
  2451. $this->article['errors'] = [
  2452. 'title' => ['error message']
  2453. ];
  2454. $this->Form->create($this->article);
  2455. $result = $this->Form->control('title', [
  2456. 'error' => 'Custom error!'
  2457. ]);
  2458. $expected = [
  2459. 'div' => ['class' => 'input text required error'],
  2460. 'label' => ['for' => 'title'],
  2461. 'Title',
  2462. '/label',
  2463. 'input' => [
  2464. 'type' => 'text', 'name' => 'title',
  2465. 'id' => 'title', 'class' => 'form-error',
  2466. 'required' => 'required',
  2467. ],
  2468. ['div' => ['class' => 'error-message']],
  2469. 'Custom error!',
  2470. '/div',
  2471. '/div'
  2472. ];
  2473. $this->assertHtml($expected, $result);
  2474. $result = $this->Form->control('title', [
  2475. 'error' => ['error message' => 'Custom error!']
  2476. ]);
  2477. $expected = [
  2478. 'div' => ['class' => 'input text required error'],
  2479. 'label' => ['for' => 'title'],
  2480. 'Title',
  2481. '/label',
  2482. 'input' => [
  2483. 'type' => 'text',
  2484. 'name' => 'title',
  2485. 'id' => 'title',
  2486. 'class' => 'form-error',
  2487. 'required' => 'required'
  2488. ],
  2489. ['div' => ['class' => 'error-message']],
  2490. 'Custom error!',
  2491. '/div',
  2492. '/div'
  2493. ];
  2494. $this->assertHtml($expected, $result);
  2495. }
  2496. /**
  2497. * testFormValidationAssociated method
  2498. *
  2499. * Tests displaying errors for nested entities.
  2500. *
  2501. * @return void
  2502. */
  2503. public function testFormValidationAssociated()
  2504. {
  2505. $nested = new Entity(['foo' => 'bar']);
  2506. $nested->errors('foo', ['not a valid bar']);
  2507. $entity = new Entity(['nested' => $nested]);
  2508. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2509. $result = $this->Form->error('nested.foo');
  2510. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2511. }
  2512. /**
  2513. * testFormValidationAssociatedSecondLevel method
  2514. *
  2515. * Test form error display with associated model.
  2516. *
  2517. * @return void
  2518. */
  2519. public function testFormValidationAssociatedSecondLevel()
  2520. {
  2521. $inner = new Entity(['bar' => 'baz']);
  2522. $nested = new Entity(['foo' => $inner]);
  2523. $entity = new Entity(['nested' => $nested]);
  2524. $inner->errors('bar', ['not a valid one']);
  2525. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2526. $result = $this->Form->error('nested.foo.bar');
  2527. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2528. }
  2529. /**
  2530. * testFormValidationMultiRecord method
  2531. *
  2532. * Test form error display with multiple records.
  2533. *
  2534. * @return void
  2535. */
  2536. public function testFormValidationMultiRecord()
  2537. {
  2538. $one = new Entity();
  2539. $two = new Entity();
  2540. TableRegistry::get('Contacts', [
  2541. 'className' => __NAMESPACE__ . '\ContactsTable'
  2542. ]);
  2543. $one->set('email', '');
  2544. $one->errors('email', ['invalid email']);
  2545. $two->set('name', '');
  2546. $two->errors('name', ['This is wrong']);
  2547. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2548. $result = $this->Form->control('0.email');
  2549. $expected = [
  2550. 'div' => ['class' => 'input email error'],
  2551. 'label' => ['for' => '0-email'],
  2552. 'Email',
  2553. '/label',
  2554. 'input' => [
  2555. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2556. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2557. ],
  2558. ['div' => ['class' => 'error-message']],
  2559. 'invalid email',
  2560. '/div',
  2561. '/div'
  2562. ];
  2563. $this->assertHtml($expected, $result);
  2564. $result = $this->Form->control('1.name');
  2565. $expected = [
  2566. 'div' => ['class' => 'input text error'],
  2567. 'label' => ['for' => '1-name'],
  2568. 'Name',
  2569. '/label',
  2570. 'input' => [
  2571. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2572. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2573. ],
  2574. ['div' => ['class' => 'error-message']],
  2575. 'This is wrong',
  2576. '/div',
  2577. '/div'
  2578. ];
  2579. $this->assertHtml($expected, $result);
  2580. }
  2581. /**
  2582. * testControl method
  2583. *
  2584. * Test various incarnations of control().
  2585. *
  2586. * @return void
  2587. */
  2588. public function testControl()
  2589. {
  2590. TableRegistry::get('ValidateUsers', [
  2591. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2592. ]);
  2593. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2594. $result = $this->Form->control('ValidateUsers.balance');
  2595. $expected = [
  2596. 'div' => ['class'],
  2597. 'label' => ['for'],
  2598. 'Balance',
  2599. '/label',
  2600. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2601. '/div',
  2602. ];
  2603. $this->assertHtml($expected, $result);
  2604. $result = $this->Form->control('ValidateUser.cost_decimal');
  2605. $expected = [
  2606. 'div' => ['class'],
  2607. 'label' => ['for'],
  2608. 'Cost Decimal',
  2609. '/label',
  2610. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2611. '/div',
  2612. ];
  2613. $this->assertHtml($expected, $result);
  2614. $result = $this->Form->control('ValidateUser.null_decimal');
  2615. $expected = [
  2616. 'div' => ['class'],
  2617. 'label' => ['for'],
  2618. 'Null Decimal',
  2619. '/label',
  2620. 'input' => ['name', 'type' => 'number', 'id'],
  2621. '/div',
  2622. ];
  2623. $this->assertHtml($expected, $result);
  2624. }
  2625. /**
  2626. * testControlCustomization method
  2627. *
  2628. * Tests the input method and passing custom options.
  2629. *
  2630. * @return void
  2631. */
  2632. public function testControlCustomization()
  2633. {
  2634. TableRegistry::get('Contacts', [
  2635. 'className' => __NAMESPACE__ . '\ContactsTable'
  2636. ]);
  2637. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2638. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2639. $expected = [
  2640. 'div' => ['class' => 'input email'],
  2641. 'label' => ['for' => 'custom'],
  2642. 'Email',
  2643. '/label',
  2644. ['input' => [
  2645. 'type' => 'email', 'name' => 'Contact[email]',
  2646. 'id' => 'custom', 'maxlength' => 255
  2647. ]],
  2648. '/div'
  2649. ];
  2650. $this->assertHtml($expected, $result);
  2651. $result = $this->Form->control('Contact.email', [
  2652. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2653. ]);
  2654. $expected = [
  2655. '<div',
  2656. 'label' => ['for' => 'contact-email'],
  2657. 'Email',
  2658. '/label',
  2659. ['input' => [
  2660. 'type' => 'email', 'name' => 'Contact[email]',
  2661. 'id' => 'contact-email', 'maxlength' => 255
  2662. ]],
  2663. '/div'
  2664. ];
  2665. $this->assertHtml($expected, $result);
  2666. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2667. $expected = [
  2668. 'div' => ['class' => 'input text'],
  2669. 'label' => ['for' => 'contact-email'],
  2670. 'Email',
  2671. '/label',
  2672. ['input' => [
  2673. 'type' => 'text', 'name' => 'Contact[email]',
  2674. 'id' => 'contact-email', 'maxlength' => '255'
  2675. ]],
  2676. '/div'
  2677. ];
  2678. $this->assertHtml($expected, $result);
  2679. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2680. $expected = [
  2681. 'div' => ['class' => 'input text'],
  2682. 'label' => ['for' => 'contact-5-email'],
  2683. 'Email',
  2684. '/label',
  2685. ['input' => [
  2686. 'type' => 'text', 'name' => 'Contact[5][email]',
  2687. 'id' => 'contact-5-email', 'maxlength' => '255'
  2688. ]],
  2689. '/div'
  2690. ];
  2691. $this->assertHtml($expected, $result);
  2692. $result = $this->Form->control('Contact.password');
  2693. $expected = [
  2694. 'div' => ['class' => 'input password'],
  2695. 'label' => ['for' => 'contact-password'],
  2696. 'Password',
  2697. '/label',
  2698. ['input' => [
  2699. 'type' => 'password', 'name' => 'Contact[password]',
  2700. 'id' => 'contact-password'
  2701. ]],
  2702. '/div'
  2703. ];
  2704. $this->assertHtml($expected, $result);
  2705. $result = $this->Form->control('Contact.email', [
  2706. 'type' => 'file', 'class' => 'textbox'
  2707. ]);
  2708. $expected = [
  2709. 'div' => ['class' => 'input file'],
  2710. 'label' => ['for' => 'contact-email'],
  2711. 'Email',
  2712. '/label',
  2713. ['input' => [
  2714. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2715. 'id' => 'contact-email'
  2716. ]],
  2717. '/div'
  2718. ];
  2719. $this->assertHtml($expected, $result);
  2720. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2721. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2722. $result = $this->Form->control('phone');
  2723. $expected = [
  2724. 'div' => ['class' => 'input tel'],
  2725. 'label' => ['for' => 'phone'],
  2726. 'Phone',
  2727. '/label',
  2728. ['input' => [
  2729. 'type' => 'tel', 'name' => 'phone',
  2730. 'value' => 'Hello &amp; World &gt; weird chars',
  2731. 'id' => 'phone', 'maxlength' => 255
  2732. ]],
  2733. '/div'
  2734. ];
  2735. $this->assertHtml($expected, $result);
  2736. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2737. $this->Form->create();
  2738. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2739. $expected = [
  2740. 'div' => ['class' => 'input text'],
  2741. 'label' => ['for' => 'myId'],
  2742. 'Field',
  2743. '/label',
  2744. 'input' => [
  2745. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2746. 'value' => 'My value', 'id' => 'myId'
  2747. ],
  2748. '/div'
  2749. ];
  2750. $this->assertHtml($expected, $result);
  2751. $this->Form->request->data = [];
  2752. $entity->errors('field', 'Badness!');
  2753. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2754. $result = $this->Form->control('field');
  2755. $expected = [
  2756. 'div' => ['class' => 'input text error'],
  2757. 'label' => ['for' => 'field'],
  2758. 'Field',
  2759. '/label',
  2760. 'input' => [
  2761. 'type' => 'text', 'name' => 'field',
  2762. 'id' => 'field', 'class' => 'form-error'
  2763. ],
  2764. ['div' => ['class' => 'error-message']],
  2765. 'Badness!',
  2766. '/div',
  2767. '/div'
  2768. ];
  2769. $this->assertHtml($expected, $result);
  2770. $result = $this->Form->control('field', [
  2771. 'templates' => [
  2772. 'inputContainerError' => '{{content}}{{error}}',
  2773. 'error' => '<span class="error-message">{{content}}</span>'
  2774. ]
  2775. ]);
  2776. $expected = [
  2777. 'label' => ['for' => 'field'],
  2778. 'Field',
  2779. '/label',
  2780. 'input' => [
  2781. 'type' => 'text', 'name' => 'field',
  2782. 'id' => 'field', 'class' => 'form-error'
  2783. ],
  2784. ['span' => ['class' => 'error-message']],
  2785. 'Badness!',
  2786. '/span'
  2787. ];
  2788. $this->assertHtml($expected, $result);
  2789. $entity->errors('field', ['minLength'], true);
  2790. $result = $this->Form->control('field', [
  2791. 'error' => [
  2792. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2793. 'maxLength' => 'login too large'
  2794. ]
  2795. ]);
  2796. $expected = [
  2797. 'div' => ['class' => 'input text error'],
  2798. 'label' => ['for' => 'field'],
  2799. 'Field',
  2800. '/label',
  2801. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2802. ['div' => ['class' => 'error-message']],
  2803. 'Le login doit contenir au moins 2 caractères',
  2804. '/div',
  2805. '/div'
  2806. ];
  2807. $this->assertHtml($expected, $result);
  2808. $entity->errors('field', ['maxLength'], true);
  2809. $result = $this->Form->control('field', [
  2810. 'error' => [
  2811. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2812. 'maxLength' => 'login too large',
  2813. ]
  2814. ]);
  2815. $expected = [
  2816. 'div' => ['class' => 'input text error'],
  2817. 'label' => ['for' => 'field'],
  2818. 'Field',
  2819. '/label',
  2820. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2821. ['div' => ['class' => 'error-message']],
  2822. 'login too large',
  2823. '/div',
  2824. '/div'
  2825. ];
  2826. $this->assertHtml($expected, $result);
  2827. }
  2828. /**
  2829. * testControlWithTemplateFile method
  2830. *
  2831. * Test that control() accepts a template file.
  2832. *
  2833. * @return void
  2834. */
  2835. public function testControlWithTemplateFile()
  2836. {
  2837. $result = $this->Form->control('field', [
  2838. 'templates' => 'htmlhelper_tags'
  2839. ]);
  2840. $expected = [
  2841. 'label' => ['for' => 'field'],
  2842. 'Field',
  2843. '/label',
  2844. 'input' => [
  2845. 'type' => 'text', 'name' => 'field',
  2846. 'id' => 'field'
  2847. ],
  2848. ];
  2849. $this->assertHtml($expected, $result);
  2850. }
  2851. /**
  2852. * testNestedControlsEndWithBrackets method
  2853. *
  2854. * Test that nested inputs end with brackets.
  2855. *
  2856. * @return void
  2857. */
  2858. public function testNestedControlsEndWithBrackets()
  2859. {
  2860. $result = $this->Form->text('nested.text[]');
  2861. $expected = [
  2862. 'input' => [
  2863. 'type' => 'text', 'name' => 'nested[text][]'
  2864. ],
  2865. ];
  2866. $this->assertHtml($expected, $result);
  2867. $result = $this->Form->file('nested.file[]');
  2868. $expected = [
  2869. 'input' => [
  2870. 'type' => 'file', 'name' => 'nested[file][]'
  2871. ],
  2872. ];
  2873. $this->assertHtml($expected, $result);
  2874. }
  2875. /**
  2876. * testCreateIdPrefix method
  2877. *
  2878. * Test id prefix.
  2879. *
  2880. * @return void
  2881. */
  2882. public function testCreateIdPrefix()
  2883. {
  2884. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2885. $result = $this->Form->control('field');
  2886. $expected = [
  2887. 'div' => ['class' => 'input text'],
  2888. 'label' => ['for' => 'prefix-field'],
  2889. 'Field',
  2890. '/label',
  2891. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2892. '/div'
  2893. ];
  2894. $this->assertHtml($expected, $result);
  2895. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2896. $expected = [
  2897. 'div' => ['class' => 'input text'],
  2898. 'label' => ['for' => 'custom-id'],
  2899. 'Field',
  2900. '/label',
  2901. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2902. '/div'
  2903. ];
  2904. $this->assertHtml($expected, $result);
  2905. $result = $this->Form->radio('Model.field', ['option A']);
  2906. $expected = [
  2907. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2908. 'label' => ['for' => 'prefix-model-field-0'],
  2909. ['input' => [
  2910. 'type' => 'radio',
  2911. 'name' => 'Model[field]',
  2912. 'value' => '0',
  2913. 'id' => 'prefix-model-field-0'
  2914. ]],
  2915. 'option A',
  2916. '/label'
  2917. ];
  2918. $this->assertHtml($expected, $result);
  2919. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2920. $expected = [
  2921. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2922. 'label' => ['for' => 'prefix-model-field-0'],
  2923. ['input' => [
  2924. 'type' => 'radio',
  2925. 'name' => 'Model[field]',
  2926. 'value' => '0',
  2927. 'id' => 'prefix-model-field-0'
  2928. ]],
  2929. 'option A',
  2930. '/label'
  2931. ];
  2932. $this->assertHtml($expected, $result);
  2933. $result = $this->Form->select(
  2934. 'Model.multi_field',
  2935. ['first'],
  2936. ['multiple' => 'checkbox']
  2937. );
  2938. $expected = [
  2939. 'input' => [
  2940. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2941. ],
  2942. ['div' => ['class' => 'checkbox']],
  2943. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2944. ['input' => [
  2945. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2946. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2947. ]],
  2948. 'first',
  2949. '/label',
  2950. '/div',
  2951. ];
  2952. $this->assertHtml($expected, $result);
  2953. $this->Form->end();
  2954. $result = $this->Form->control('field');
  2955. $expected = [
  2956. 'div' => ['class' => 'input text'],
  2957. 'label' => ['for' => 'field'],
  2958. 'Field',
  2959. '/label',
  2960. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2961. '/div'
  2962. ];
  2963. $this->assertHtml($expected, $result);
  2964. }
  2965. /**
  2966. * testControlZero method
  2967. *
  2968. * Test that inputs with 0 can be created.
  2969. *
  2970. * @return void
  2971. */
  2972. public function testControlZero()
  2973. {
  2974. TableRegistry::get('Contacts', [
  2975. 'className' => __NAMESPACE__ . '\ContactsTable'
  2976. ]);
  2977. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2978. $result = $this->Form->control('0');
  2979. $expected = [
  2980. 'div' => ['class' => 'input text'],
  2981. 'label' => ['for' => '0'], '/label',
  2982. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2983. '/div'
  2984. ];
  2985. $this->assertHtml($expected, $result);
  2986. }
  2987. /**
  2988. * testControlCheckbox method
  2989. *
  2990. * Test control() with checkbox creation.
  2991. *
  2992. * @return void
  2993. */
  2994. public function testControlCheckbox()
  2995. {
  2996. $articles = TableRegistry::get('Articles');
  2997. $articles->schema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  2998. $article = $articles->newEntity();
  2999. $this->Form->create($article);
  3000. $result = $this->Form->control('Articles.active');
  3001. $expected = [
  3002. 'div' => ['class' => 'input checkbox'],
  3003. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3004. 'label' => ['for' => 'articles-active'],
  3005. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3006. 'Active',
  3007. '/label',
  3008. '/div'
  3009. ];
  3010. $this->assertHtml($expected, $result);
  3011. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3012. $expected = [
  3013. 'div' => ['class' => 'input checkbox'],
  3014. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3015. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3016. '/div'
  3017. ];
  3018. $this->assertHtml($expected, $result);
  3019. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3020. $expected = [
  3021. 'div' => ['class' => 'input checkbox'],
  3022. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3023. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3024. '/div'
  3025. ];
  3026. $this->assertHtml($expected, $result);
  3027. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3028. $expected = [
  3029. 'div' => ['class' => 'input checkbox'],
  3030. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3031. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3032. '/div'
  3033. ];
  3034. $this->assertHtml($expected, $result);
  3035. $result = $this->Form->control('Articles.disabled', [
  3036. 'label' => 'Disabled',
  3037. 'type' => 'checkbox',
  3038. 'data-foo' => 'disabled'
  3039. ]);
  3040. $expected = [
  3041. 'div' => ['class' => 'input checkbox'],
  3042. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3043. 'label' => ['for' => 'articles-disabled'],
  3044. ['input' => [
  3045. 'type' => 'checkbox',
  3046. 'name' => 'Articles[disabled]',
  3047. 'value' => '1',
  3048. 'id' => 'articles-disabled',
  3049. 'data-foo' => 'disabled'
  3050. ]],
  3051. 'Disabled',
  3052. '/label',
  3053. '/div'
  3054. ];
  3055. $this->assertHtml($expected, $result);
  3056. $result = $this->Form->control('Articles.confirm', [
  3057. 'label' => 'Confirm <b>me</b>!',
  3058. 'type' => 'checkbox',
  3059. 'escape' => false
  3060. ]);
  3061. $expected = [
  3062. 'div' => ['class' => 'input checkbox'],
  3063. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3064. 'label' => ['for' => 'articles-confirm'],
  3065. ['input' => [
  3066. 'type' => 'checkbox',
  3067. 'name' => 'Articles[confirm]',
  3068. 'value' => '1',
  3069. 'id' => 'articles-confirm',
  3070. ]],
  3071. 'Confirm <b>me</b>!',
  3072. '/label',
  3073. '/div'
  3074. ];
  3075. $this->assertHtml($expected, $result);
  3076. }
  3077. /**
  3078. * testControlHidden method
  3079. *
  3080. * Test that control() does not create wrapping div and label tag for hidden fields.
  3081. *
  3082. * @return void
  3083. */
  3084. public function testControlHidden()
  3085. {
  3086. TableRegistry::get('ValidateUsers', [
  3087. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3088. ]);
  3089. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3090. $result = $this->Form->control('ValidateUser.id');
  3091. $expected = [
  3092. 'input' => ['name', 'type' => 'hidden', 'id']
  3093. ];
  3094. $this->assertHtml($expected, $result);
  3095. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3096. $expected = [
  3097. 'input' => ['name', 'type' => 'hidden', 'id']
  3098. ];
  3099. $this->assertHtml($expected, $result);
  3100. }
  3101. /**
  3102. * testControlDatetime method
  3103. *
  3104. * Test form->control() with datetime.
  3105. *
  3106. * @return void
  3107. */
  3108. public function testControlDatetime()
  3109. {
  3110. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3111. ->setMethods(['datetime'])
  3112. ->setConstructorArgs([new View()])
  3113. ->getMock();
  3114. $this->Form->expects($this->once())->method('datetime')
  3115. ->with('prueba', [
  3116. 'type' => 'datetime',
  3117. 'timeFormat' => 24,
  3118. 'minYear' => 2008,
  3119. 'maxYear' => 2011,
  3120. 'interval' => 15,
  3121. 'options' => null,
  3122. 'empty' => false,
  3123. 'id' => 'prueba',
  3124. 'required' => false,
  3125. 'templateVars' => []
  3126. ])
  3127. ->will($this->returnValue('This is it!'));
  3128. $result = $this->Form->control('prueba', [
  3129. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3130. 'maxYear' => 2011, 'interval' => 15
  3131. ]);
  3132. $expected = [
  3133. 'div' => ['class' => 'input datetime'],
  3134. '<label',
  3135. 'Prueba',
  3136. '/label',
  3137. 'This is it!',
  3138. '/div'
  3139. ];
  3140. $this->assertHtml($expected, $result);
  3141. }
  3142. /**
  3143. * testControlDatetimeIdPrefix method
  3144. *
  3145. * Test form->control() with datetime with id prefix.
  3146. *
  3147. * @return void
  3148. */
  3149. public function testControlDatetimeIdPrefix()
  3150. {
  3151. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3152. ->setMethods(['datetime'])
  3153. ->setConstructorArgs([new View()])
  3154. ->getMock();
  3155. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3156. $this->Form->expects($this->once())->method('datetime')
  3157. ->with('prueba', [
  3158. 'type' => 'datetime',
  3159. 'timeFormat' => 24,
  3160. 'minYear' => 2008,
  3161. 'maxYear' => 2011,
  3162. 'interval' => 15,
  3163. 'options' => null,
  3164. 'empty' => false,
  3165. 'id' => 'prefix-prueba',
  3166. 'required' => false,
  3167. 'templateVars' => []
  3168. ])
  3169. ->will($this->returnValue('This is it!'));
  3170. $result = $this->Form->control('prueba', [
  3171. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3172. 'maxYear' => 2011, 'interval' => 15
  3173. ]);
  3174. $expected = [
  3175. 'div' => ['class' => 'input datetime'],
  3176. '<label',
  3177. 'Prueba',
  3178. '/label',
  3179. 'This is it!',
  3180. '/div'
  3181. ];
  3182. $this->assertHtml($expected, $result);
  3183. }
  3184. /**
  3185. * testControlCheckboxWithDisabledElements method
  3186. *
  3187. * Test generating checkboxes with disabled elements.
  3188. *
  3189. * @return void
  3190. */
  3191. public function testControlCheckboxWithDisabledElements()
  3192. {
  3193. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3194. $result = $this->Form->control('Contact.multiple', [
  3195. 'multiple' => 'checkbox',
  3196. 'disabled' => 'disabled',
  3197. 'options' => $options
  3198. ]);
  3199. $expected = [
  3200. ['div' => ['class' => 'input select']],
  3201. ['label' => ['for' => "contact-multiple"]],
  3202. 'Multiple',
  3203. '/label',
  3204. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'disabled' => 'disabled', 'value' => '']],
  3205. ['div' => ['class' => 'checkbox']],
  3206. ['label' => ['for' => "contact-multiple-1"]],
  3207. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1"]],
  3208. 'One',
  3209. '/label',
  3210. '/div',
  3211. ['div' => ['class' => 'checkbox']],
  3212. ['label' => ['for' => "contact-multiple-2"]],
  3213. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2"]],
  3214. 'Two',
  3215. '/label',
  3216. '/div',
  3217. ['div' => ['class' => 'checkbox']],
  3218. ['label' => ['for' => "contact-multiple-3"]],
  3219. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3"]],
  3220. 'Three',
  3221. '/label',
  3222. '/div',
  3223. '/div'
  3224. ];
  3225. $this->assertHtml($expected, $result);
  3226. // make sure 50 does only disable 50, and not 50f5c0cf
  3227. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3228. $disabled = [50];
  3229. $expected = [
  3230. ['div' => ['class' => 'input select']],
  3231. ['label' => ['for' => "contact-multiple"]],
  3232. 'Multiple',
  3233. '/label',
  3234. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '']],
  3235. ['div' => ['class' => 'checkbox']],
  3236. ['label' => ['for' => "contact-multiple-50"]],
  3237. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50"]],
  3238. 'Fifty',
  3239. '/label',
  3240. '/div',
  3241. ['div' => ['class' => 'checkbox']],
  3242. ['label' => ['for' => "contact-multiple-50f5c0cf"]],
  3243. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf"]],
  3244. 'Stringy',
  3245. '/label',
  3246. '/div',
  3247. '/div'
  3248. ];
  3249. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3250. $this->assertHtml($expected, $result);
  3251. }
  3252. /**
  3253. * testControlWithLeadingInteger method
  3254. *
  3255. * Test input name with leading integer, ensure attributes are generated correctly.
  3256. *
  3257. * @return void
  3258. */
  3259. public function testControlWithLeadingInteger()
  3260. {
  3261. $result = $this->Form->text('0.Node.title');
  3262. $expected = [
  3263. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3264. ];
  3265. $this->assertHtml($expected, $result);
  3266. }
  3267. /**
  3268. * testControlSelectType method
  3269. *
  3270. * Test form->control() with select type inputs.
  3271. *
  3272. * @return void
  3273. */
  3274. public function testControlSelectType()
  3275. {
  3276. $result = $this->Form->control(
  3277. 'email',
  3278. [
  3279. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3280. );
  3281. $expected = [
  3282. 'div' => ['class' => 'input select'],
  3283. 'label' => ['for' => 'email'],
  3284. 'Email',
  3285. '/label',
  3286. ['select' => ['name' => 'email', 'id' => 'email']],
  3287. ['option' => ['value' => '']],
  3288. '/option',
  3289. ['option' => ['value' => 'è']],
  3290. 'Firést',
  3291. '/option',
  3292. ['option' => ['value' => 'é']],
  3293. 'Secoènd',
  3294. '/option',
  3295. '/select',
  3296. '/div'
  3297. ];
  3298. $this->assertHtml($expected, $result);
  3299. $result = $this->Form->control(
  3300. 'email',
  3301. [
  3302. 'options' => ['First', 'Second'], 'empty' => true]
  3303. );
  3304. $expected = [
  3305. 'div' => ['class' => 'input select'],
  3306. 'label' => ['for' => 'email'],
  3307. 'Email',
  3308. '/label',
  3309. ['select' => ['name' => 'email', 'id' => 'email']],
  3310. ['option' => ['value' => '']],
  3311. '/option',
  3312. ['option' => ['value' => '0']],
  3313. 'First',
  3314. '/option',
  3315. ['option' => ['value' => '1']],
  3316. 'Second',
  3317. '/option',
  3318. '/select',
  3319. '/div'
  3320. ];
  3321. $this->assertHtml($expected, $result);
  3322. $result = $this->Form->control('email', [
  3323. 'type' => 'select',
  3324. 'options' => new \ArrayObject(['First', 'Second']),
  3325. 'empty' => true
  3326. ]);
  3327. $this->assertHtml($expected, $result);
  3328. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3329. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3330. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3331. $expected = [
  3332. 'div' => ['class' => 'input select'],
  3333. 'label' => ['for' => 'model-user-id'],
  3334. 'User',
  3335. '/label',
  3336. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3337. ['option' => ['value' => '']],
  3338. '/option',
  3339. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3340. 'good',
  3341. '/option',
  3342. ['option' => ['value' => 'other']],
  3343. 'bad',
  3344. '/option',
  3345. '/select',
  3346. '/div'
  3347. ];
  3348. $this->assertHtml($expected, $result);
  3349. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3350. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3351. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3352. $expected = [
  3353. 'div' => ['class' => 'input select'],
  3354. 'label' => ['for' => 'thing-user-id'],
  3355. 'User',
  3356. '/label',
  3357. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3358. ['option' => ['value' => '']],
  3359. 'Some Empty',
  3360. '/option',
  3361. ['option' => ['value' => 'value']],
  3362. 'good',
  3363. '/option',
  3364. ['option' => ['value' => 'other']],
  3365. 'bad',
  3366. '/option',
  3367. '/select',
  3368. '/div'
  3369. ];
  3370. $this->assertHtml($expected, $result);
  3371. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3372. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3373. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3374. $expected = [
  3375. 'div' => ['class' => 'input select'],
  3376. 'label' => ['for' => 'thing-user-id'],
  3377. 'User',
  3378. '/label',
  3379. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3380. ['option' => ['value' => '']],
  3381. 'Some Empty',
  3382. '/option',
  3383. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3384. 'good',
  3385. '/option',
  3386. ['option' => ['value' => 'other']],
  3387. 'bad',
  3388. '/option',
  3389. '/select',
  3390. '/div'
  3391. ];
  3392. $this->assertHtml($expected, $result);
  3393. $this->Form->data = [];
  3394. $result = $this->Form->control('Publisher.id', [
  3395. 'label' => 'Publisher',
  3396. 'type' => 'select',
  3397. 'multiple' => 'checkbox',
  3398. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3399. ]);
  3400. $expected = [
  3401. ['div' => ['class' => 'input select']],
  3402. ['label' => ['for' => 'publisher-id']],
  3403. 'Publisher',
  3404. '/label',
  3405. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3406. ['div' => ['class' => 'checkbox']],
  3407. ['label' => ['for' => 'publisher-id-value-1']],
  3408. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3409. 'Label 1',
  3410. '/label',
  3411. '/div',
  3412. ['div' => ['class' => 'checkbox']],
  3413. ['label' => ['for' => 'publisher-id-value-2']],
  3414. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3415. 'Label 2',
  3416. '/label',
  3417. '/div',
  3418. '/div'
  3419. ];
  3420. $this->assertHtml($expected, $result);
  3421. }
  3422. /**
  3423. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3424. *
  3425. * Test that control() and a non standard primary key makes a hidden input by default.
  3426. *
  3427. * @return void
  3428. */
  3429. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3430. {
  3431. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3432. $this->Form->create($this->article);
  3433. $result = $this->Form->control('title');
  3434. $expected = [
  3435. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3436. ];
  3437. $this->assertHtml($expected, $result);
  3438. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3439. $this->Form->create($this->article);
  3440. $result = $this->Form->control('title');
  3441. $expected = [
  3442. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3443. ];
  3444. $this->assertHtml($expected, $result);
  3445. $result = $this->Form->control('body');
  3446. $expected = [
  3447. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3448. ];
  3449. $this->assertHtml($expected, $result);
  3450. }
  3451. /**
  3452. * testControlOverridingMagicSelectType method
  3453. *
  3454. * Test that overriding the magic select type widget is possible.
  3455. *
  3456. * @return void
  3457. */
  3458. public function testControlOverridingMagicSelectType()
  3459. {
  3460. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3461. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3462. $expected = [
  3463. 'div' => ['class' => 'input text'],
  3464. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3465. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3466. '/div'
  3467. ];
  3468. $this->assertHtml($expected, $result);
  3469. //Check that magic types still work for plural/singular vars
  3470. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3471. $result = $this->Form->control('Model.type');
  3472. $expected = [
  3473. 'div' => ['class' => 'input select'],
  3474. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3475. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3476. ['option' => ['value' => 'value']], 'good', '/option',
  3477. ['option' => ['value' => 'other']], 'bad', '/option',
  3478. '/select',
  3479. '/div'
  3480. ];
  3481. $this->assertHtml($expected, $result);
  3482. }
  3483. /**
  3484. * testControlMagicTypeDoesNotOverride method
  3485. *
  3486. * Test that inferred types do not override developer input.
  3487. *
  3488. * @return void
  3489. */
  3490. public function testControlMagicTypeDoesNotOverride()
  3491. {
  3492. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3493. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3494. $expected = [
  3495. 'div' => ['class' => 'input checkbox'],
  3496. ['input' => [
  3497. 'type' => 'hidden',
  3498. 'name' => 'Model[user]',
  3499. 'value' => 0,
  3500. ]],
  3501. 'label' => ['for' => 'model-user'],
  3502. ['input' => [
  3503. 'name' => 'Model[user]',
  3504. 'type' => 'checkbox',
  3505. 'id' => 'model-user',
  3506. 'value' => 1
  3507. ]],
  3508. 'User',
  3509. '/label',
  3510. '/div'
  3511. ];
  3512. $this->assertHtml($expected, $result);
  3513. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3514. $options = [
  3515. 1 => 'blue',
  3516. 2 => 'red'
  3517. ];
  3518. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3519. $expected = [
  3520. 'div' => ['class' => 'input select'],
  3521. 'label' => ['for' => 'tags-ids'],
  3522. 'Tags',
  3523. '/label',
  3524. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3525. ['div' => ['class' => 'checkbox']],
  3526. ['label' => ['for' => 'tags-ids-1']],
  3527. ['input' => [
  3528. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3529. 'value' => '1', 'name' => 'tags[_ids][]'
  3530. ]],
  3531. 'blue',
  3532. '/label',
  3533. '/div',
  3534. ['div' => ['class' => 'checkbox']],
  3535. ['label' => ['for' => 'tags-ids-2']],
  3536. ['input' => [
  3537. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3538. 'value' => '2', 'name' => 'tags[_ids][]'
  3539. ]],
  3540. 'red',
  3541. '/label',
  3542. '/div',
  3543. '/div'
  3544. ];
  3545. $this->assertHtml($expected, $result);
  3546. }
  3547. /**
  3548. * testControlMagicSelectForTypeNumber method
  3549. *
  3550. * Test that magic control() selects are created for type=number.
  3551. *
  3552. * @return void
  3553. */
  3554. public function testControlMagicSelectForTypeNumber()
  3555. {
  3556. TableRegistry::get('ValidateUsers', [
  3557. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3558. ]);
  3559. $entity = new Entity(['balance' => 1]);
  3560. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3561. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3562. $result = $this->Form->control('balance');
  3563. $expected = [
  3564. 'div' => ['class' => 'input select'],
  3565. 'label' => ['for' => 'balance'],
  3566. 'Balance',
  3567. '/label',
  3568. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3569. ['option' => ['value' => '0']],
  3570. 'nothing',
  3571. '/option',
  3572. ['option' => ['value' => '1', 'selected' => 'selected']],
  3573. 'some',
  3574. '/option',
  3575. ['option' => ['value' => '100']],
  3576. 'a lot',
  3577. '/option',
  3578. '/select',
  3579. '/div'
  3580. ];
  3581. $this->assertHtml($expected, $result);
  3582. }
  3583. /**
  3584. * testInvalidControlTypeOption method
  3585. *
  3586. * Test invalid 'input' type option to control() function.
  3587. *
  3588. * @expectedException \RuntimeException
  3589. * @expectedExceptionMessage Invalid type 'input' used for field 'text'
  3590. * @return void
  3591. */
  3592. public function testInvalidControlTypeOption()
  3593. {
  3594. $this->Form->control('text', ['type' => 'input']);
  3595. }
  3596. /**
  3597. * testControlMagicSelectChangeToRadio method
  3598. *
  3599. * Test that magic control() selects can easily be converted into radio types without error.
  3600. *
  3601. * @return void
  3602. */
  3603. public function testControlMagicSelectChangeToRadio()
  3604. {
  3605. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3606. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3607. $this->assertContains('input type="radio"', $result);
  3608. }
  3609. /**
  3610. * testFormControlSubmit method
  3611. *
  3612. * Test correct results for form::control() and type submit.
  3613. *
  3614. * @return void
  3615. */
  3616. public function testFormControlSubmit()
  3617. {
  3618. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3619. $expected = [
  3620. 'div' => ['class' => 'submit'],
  3621. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3622. '/div'
  3623. ];
  3624. $this->assertHtml($expected, $result);
  3625. }
  3626. /**
  3627. * testFormControls method
  3628. *
  3629. * Test correct results from Form::controls().
  3630. *
  3631. * @return void
  3632. */
  3633. public function testFormControlsLegendFieldset()
  3634. {
  3635. $this->Form->create($this->article);
  3636. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3637. $expected = [
  3638. '<fieldset',
  3639. '<legend',
  3640. 'The Legend',
  3641. '/legend',
  3642. '*/fieldset',
  3643. ];
  3644. $this->assertHtml($expected, $result);
  3645. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3646. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3647. $this->assertContains('<fieldset>', $result);
  3648. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3649. $this->assertNotContains('<legend>', $result);
  3650. $this->assertNotContains('<fieldset>', $result);
  3651. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3652. $this->assertNotContains('<legend>', $result);
  3653. $this->assertNotContains('<fieldset>', $result);
  3654. $this->Form->create($this->article);
  3655. $this->Form->request->params['prefix'] = 'admin';
  3656. $this->Form->request->params['action'] = 'admin_edit';
  3657. $this->Form->request->params['controller'] = 'articles';
  3658. $result = $this->Form->allControls();
  3659. $expected = [
  3660. '<fieldset',
  3661. '<legend',
  3662. 'New Article',
  3663. '/legend',
  3664. '*/fieldset',
  3665. ];
  3666. $this->assertHtml($expected, $result);
  3667. $this->Form->create($this->article);
  3668. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3669. $expected = [
  3670. '<fieldset',
  3671. '<legend',
  3672. 'The Legend',
  3673. '/legend',
  3674. '*/fieldset',
  3675. ];
  3676. $this->assertHtml($expected, $result);
  3677. $this->Form->create($this->article);
  3678. $result = $this->Form->allControls([], [
  3679. 'fieldset' => [
  3680. 'class' => 'some-class some-other-class',
  3681. 'disabled' => true,
  3682. 'data-param' => 'a-param'
  3683. ],
  3684. 'legend' => 'The Legend'
  3685. ]);
  3686. $expected = [
  3687. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3688. '<legend',
  3689. 'The Legend',
  3690. '/legend',
  3691. '*/fieldset',
  3692. ];
  3693. $this->assertHtml($expected, $result);
  3694. }
  3695. /**
  3696. * testFormControls method
  3697. *
  3698. * Test the controls() method.
  3699. *
  3700. * @return void
  3701. */
  3702. public function testFormControls()
  3703. {
  3704. $this->Form->create($this->article);
  3705. $result = $this->Form->allControls();
  3706. $expected = [
  3707. '<fieldset',
  3708. '<legend', 'New Article', '/legend',
  3709. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3710. ['div' => ['class' => 'input select required']],
  3711. '*/div',
  3712. ['div' => ['class' => 'input text required']],
  3713. '*/div',
  3714. ['div' => ['class' => 'input text']],
  3715. '*/div',
  3716. ['div' => ['class' => 'input text']],
  3717. '*/div',
  3718. '/fieldset',
  3719. ];
  3720. $this->assertHtml($expected, $result);
  3721. $result = $this->Form->allControls([
  3722. 'published' => ['type' => 'boolean']
  3723. ]);
  3724. $expected = [
  3725. '<fieldset',
  3726. '<legend', 'New Article', '/legend',
  3727. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3728. ['div' => ['class' => 'input select required']],
  3729. '*/div',
  3730. ['div' => ['class' => 'input text required']],
  3731. '*/div',
  3732. ['div' => ['class' => 'input text']],
  3733. '*/div',
  3734. ['div' => ['class' => 'input boolean']],
  3735. '*/div',
  3736. '/fieldset',
  3737. ];
  3738. $this->assertHtml($expected, $result);
  3739. $this->Form->create($this->article);
  3740. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3741. $expected = [
  3742. 'fieldset' => [],
  3743. 'legend' => [],
  3744. 'Hello',
  3745. '/legend',
  3746. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3747. ['div' => ['class' => 'input select required']],
  3748. '*/div',
  3749. ['div' => ['class' => 'input text required']],
  3750. '*/div',
  3751. ['div' => ['class' => 'input text']],
  3752. '*/div',
  3753. ['div' => ['class' => 'input text']],
  3754. '*/div',
  3755. '/fieldset'
  3756. ];
  3757. $this->assertHtml($expected, $result);
  3758. $this->Form->create(false);
  3759. $expected = [
  3760. 'fieldset' => [],
  3761. ['div' => ['class' => 'input text']],
  3762. 'label' => ['for' => 'foo'],
  3763. 'Foo',
  3764. '/label',
  3765. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3766. '*/div',
  3767. '/fieldset'
  3768. ];
  3769. $result = $this->Form->allControls(
  3770. ['foo' => ['type' => 'text']],
  3771. ['legend' => false]
  3772. );
  3773. $this->assertHtml($expected, $result);
  3774. }
  3775. /**
  3776. * testFormControlsBlacklist method
  3777. *
  3778. * @return void
  3779. */
  3780. public function testFormControlsBlacklist()
  3781. {
  3782. $this->Form->create($this->article);
  3783. $result = $this->Form->allControls([
  3784. 'id' => false
  3785. ]);
  3786. $expected = [
  3787. '<fieldset',
  3788. '<legend', 'New Article', '/legend',
  3789. ['div' => ['class' => 'input select required']],
  3790. '*/div',
  3791. ['div' => ['class' => 'input text required']],
  3792. '*/div',
  3793. ['div' => ['class' => 'input text']],
  3794. '*/div',
  3795. ['div' => ['class' => 'input text']],
  3796. '*/div',
  3797. '/fieldset',
  3798. ];
  3799. $this->assertHtml($expected, $result);
  3800. $this->Form->create($this->article);
  3801. $result = $this->Form->allControls([
  3802. 'id' => []
  3803. ]);
  3804. $expected = [
  3805. '<fieldset',
  3806. '<legend', 'New Article', '/legend',
  3807. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3808. ['div' => ['class' => 'input select required']],
  3809. '*/div',
  3810. ['div' => ['class' => 'input text required']],
  3811. '*/div',
  3812. ['div' => ['class' => 'input text']],
  3813. '*/div',
  3814. ['div' => ['class' => 'input text']],
  3815. '*/div',
  3816. '/fieldset',
  3817. ];
  3818. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3819. }
  3820. /**
  3821. * testSelectAsCheckbox method
  3822. *
  3823. * Test multi-select widget with checkbox formatting.
  3824. *
  3825. * @return void
  3826. */
  3827. public function testSelectAsCheckbox()
  3828. {
  3829. $result = $this->Form->select(
  3830. 'Model.multi_field',
  3831. ['first', 'second', 'third'],
  3832. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3833. );
  3834. $expected = [
  3835. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3836. ['div' => ['class' => 'checkbox']],
  3837. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3838. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3839. 'first',
  3840. '/label',
  3841. '/div',
  3842. ['div' => ['class' => 'checkbox']],
  3843. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3844. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3845. 'second',
  3846. '/label',
  3847. '/div',
  3848. ['div' => ['class' => 'checkbox']],
  3849. ['label' => ['for' => 'model-multi-field-2']],
  3850. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3851. 'third',
  3852. '/label',
  3853. '/div',
  3854. ];
  3855. $this->assertHtml($expected, $result);
  3856. $result = $this->Form->select(
  3857. 'Model.multi_field',
  3858. ['1/2' => 'half'],
  3859. ['multiple' => 'checkbox']
  3860. );
  3861. $expected = [
  3862. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3863. ['div' => ['class' => 'checkbox']],
  3864. ['label' => ['for' => 'model-multi-field-1-2']],
  3865. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3866. 'half',
  3867. '/label',
  3868. '/div',
  3869. ];
  3870. $this->assertHtml($expected, $result);
  3871. }
  3872. /**
  3873. * testLabel method
  3874. *
  3875. * Test label generation.
  3876. *
  3877. * @return void
  3878. */
  3879. public function testLabel()
  3880. {
  3881. $result = $this->Form->label('Person.name');
  3882. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3883. $this->assertHtml($expected, $result);
  3884. $result = $this->Form->label('Person.name');
  3885. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3886. $this->assertHtml($expected, $result);
  3887. $result = $this->Form->label('Person.first_name');
  3888. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3889. $this->assertHtml($expected, $result);
  3890. $result = $this->Form->label('Person.first_name', 'Your first name');
  3891. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3892. $this->assertHtml($expected, $result);
  3893. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3894. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3895. $this->assertHtml($expected, $result);
  3896. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3897. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3898. $this->assertHtml($expected, $result);
  3899. $result = $this->Form->label('Person.first_name', '');
  3900. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3901. $this->assertHtml($expected, $result);
  3902. $result = $this->Form->label('Person.2.name', '');
  3903. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3904. $this->assertHtml($expected, $result);
  3905. }
  3906. /**
  3907. * testLabelContainControl method
  3908. *
  3909. * Test that label() can accept an input with the correct template vars.
  3910. *
  3911. * @return void
  3912. */
  3913. public function testLabelContainControl()
  3914. {
  3915. $this->Form->templates([
  3916. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3917. ]);
  3918. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3919. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3920. ]);
  3921. $expected = [
  3922. 'label' => ['for' => 'person-accept-terms'],
  3923. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3924. 'Accept',
  3925. '/label',
  3926. ];
  3927. $this->assertHtml($expected, $result);
  3928. }
  3929. /**
  3930. * testTextbox method
  3931. *
  3932. * Test textbox element generation.
  3933. *
  3934. * @return void
  3935. */
  3936. public function testTextbox()
  3937. {
  3938. $result = $this->Form->text('Model.field');
  3939. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3940. $this->assertHtml($expected, $result);
  3941. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3942. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3943. $this->assertHtml($expected, $result);
  3944. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3945. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3946. $this->assertHtml($expected, $result);
  3947. }
  3948. /**
  3949. * testTextBoxDataAndError method
  3950. *
  3951. * Test that text() hooks up with request data and error fields.
  3952. *
  3953. * @return void
  3954. */
  3955. public function testTextBoxDataAndError()
  3956. {
  3957. $this->article['errors'] = [
  3958. 'Contact' => ['text' => 'wrong']
  3959. ];
  3960. $this->Form->create($this->article);
  3961. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3962. $result = $this->Form->text('Model.text');
  3963. $expected = [
  3964. 'input' => [
  3965. 'type' => 'text',
  3966. 'name' => 'Model[text]',
  3967. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3968. ]
  3969. ];
  3970. $this->assertHtml($expected, $result);
  3971. $this->Form->request->data['Contact']['text'] = 'test';
  3972. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3973. $expected = [
  3974. 'input' => [
  3975. 'type' => 'text',
  3976. 'name' => 'Contact[text]',
  3977. 'value' => 'test',
  3978. 'id' => 'theID',
  3979. 'class' => 'form-error'
  3980. ]
  3981. ];
  3982. $this->assertHtml($expected, $result);
  3983. }
  3984. /**
  3985. * testDefaultValue method
  3986. *
  3987. * Test default value setting.
  3988. *
  3989. * @return void
  3990. */
  3991. public function testTextDefaultValue()
  3992. {
  3993. $this->Form->request->data['Model']['field'] = 'test';
  3994. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3995. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3996. $this->assertHtml($expected, $result);
  3997. unset($this->Form->request->data['Model']['field']);
  3998. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3999. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4000. $this->assertHtml($expected, $result);
  4001. $Articles = TableRegistry::get('Articles');
  4002. $title = $Articles->schema()->column('title');
  4003. $Articles->schema()->addColumn(
  4004. 'title',
  4005. ['default' => 'default title'] + $title
  4006. );
  4007. $entity = $Articles->newEntity();
  4008. $this->Form->create($entity);
  4009. // Get default value from schema
  4010. $result = $this->Form->text('title');
  4011. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4012. $this->assertHtml($expected, $result);
  4013. // Don't get value from schema
  4014. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4015. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4016. $this->assertHtml($expected, $result);
  4017. // Custom default value overrides default value from schema
  4018. $result = $this->Form->text('title', ['default' => 'override default']);
  4019. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4020. $this->assertHtml($expected, $result);
  4021. // Default value from schema is used only for new entities.
  4022. $entity->isNew(false);
  4023. $result = $this->Form->text('title');
  4024. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4025. $this->assertHtml($expected, $result);
  4026. }
  4027. /**
  4028. * testError method
  4029. *
  4030. * Test field error generation.
  4031. *
  4032. * @return void
  4033. */
  4034. public function testError()
  4035. {
  4036. $this->article['errors'] = [
  4037. 'Article' => ['field' => 'email']
  4038. ];
  4039. $this->Form->create($this->article);
  4040. $result = $this->Form->error('Article.field');
  4041. $expected = [
  4042. ['div' => ['class' => 'error-message']],
  4043. 'email',
  4044. '/div',
  4045. ];
  4046. $this->assertHtml($expected, $result);
  4047. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  4048. $expected = [
  4049. ['div' => ['class' => 'error-message']],
  4050. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4051. '/div',
  4052. ];
  4053. $this->assertHtml($expected, $result);
  4054. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  4055. $expected = [
  4056. ['div' => ['class' => 'error-message']],
  4057. '<strong', 'Badness!', '/strong',
  4058. '/div',
  4059. ];
  4060. $this->assertHtml($expected, $result);
  4061. }
  4062. /**
  4063. * testErrorRuleName method
  4064. *
  4065. * Test error translation can use rule names for translating.
  4066. *
  4067. * @return void
  4068. */
  4069. public function testErrorRuleName()
  4070. {
  4071. $this->article['errors'] = [
  4072. 'Article' => [
  4073. 'field' => ['email' => 'Your email was not good']
  4074. ]
  4075. ];
  4076. $this->Form->create($this->article);
  4077. $result = $this->Form->error('Article.field');
  4078. $expected = [
  4079. ['div' => ['class' => 'error-message']],
  4080. 'Your email was not good',
  4081. '/div',
  4082. ];
  4083. $this->assertHtml($expected, $result);
  4084. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4085. $expected = [
  4086. ['div' => ['class' => 'error-message']],
  4087. 'Email in use',
  4088. '/div',
  4089. ];
  4090. $this->assertHtml($expected, $result);
  4091. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4092. $expected = [
  4093. ['div' => ['class' => 'error-message']],
  4094. 'Email in use',
  4095. '/div',
  4096. ];
  4097. $this->assertHtml($expected, $result);
  4098. $result = $this->Form->error('Article.field', [
  4099. 'email' => 'Key is preferred',
  4100. 'Your email was not good' => 'Email in use'
  4101. ]);
  4102. $expected = [
  4103. ['div' => ['class' => 'error-message']],
  4104. 'Key is preferred',
  4105. '/div',
  4106. ];
  4107. $this->assertHtml($expected, $result);
  4108. }
  4109. /**
  4110. * testErrorMessages method
  4111. *
  4112. * Test error with nested lists.
  4113. *
  4114. * @return void
  4115. */
  4116. public function testErrorMessages()
  4117. {
  4118. $this->article['errors'] = [
  4119. 'Article' => ['field' => 'email']
  4120. ];
  4121. $this->Form->create($this->article);
  4122. $result = $this->Form->error('Article.field', [
  4123. 'email' => 'No good!'
  4124. ]);
  4125. $expected = [
  4126. 'div' => ['class' => 'error-message'],
  4127. 'No good!',
  4128. '/div'
  4129. ];
  4130. $this->assertHtml($expected, $result);
  4131. }
  4132. /**
  4133. * testErrorMultipleMessages method
  4134. *
  4135. * Test error() with multiple messages.
  4136. *
  4137. * @return void
  4138. */
  4139. public function testErrorMultipleMessages()
  4140. {
  4141. $this->article['errors'] = [
  4142. 'field' => ['notBlank', 'email', 'Something else']
  4143. ];
  4144. $this->Form->create($this->article);
  4145. $result = $this->Form->error('field', [
  4146. 'notBlank' => 'Cannot be empty',
  4147. 'email' => 'No good!'
  4148. ]);
  4149. $expected = [
  4150. 'div' => ['class' => 'error-message'],
  4151. 'ul' => [],
  4152. '<li', 'Cannot be empty', '/li',
  4153. '<li', 'No good!', '/li',
  4154. '<li', 'Something else', '/li',
  4155. '/ul',
  4156. '/div'
  4157. ];
  4158. $this->assertHtml($expected, $result);
  4159. }
  4160. /**
  4161. * testPassword method
  4162. *
  4163. * Test password element generation.
  4164. *
  4165. * @return void
  4166. */
  4167. public function testPassword()
  4168. {
  4169. $this->article['errors'] = [
  4170. 'Contact' => [
  4171. 'passwd' => 1
  4172. ]
  4173. ];
  4174. $this->Form->create($this->article);
  4175. $result = $this->Form->password('Contact.field');
  4176. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4177. $this->assertHtml($expected, $result);
  4178. $this->Form->request->data['Contact']['passwd'] = 'test';
  4179. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4180. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4181. $this->assertHtml($expected, $result);
  4182. }
  4183. /**
  4184. * testRadio method
  4185. *
  4186. * Test radio element set generation.
  4187. *
  4188. * @return void
  4189. */
  4190. public function testRadio()
  4191. {
  4192. $result = $this->Form->radio('Model.field', ['option A']);
  4193. $expected = [
  4194. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4195. 'label' => ['for' => 'model-field-0'],
  4196. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4197. 'option A',
  4198. '/label'
  4199. ];
  4200. $this->assertHtml($expected, $result);
  4201. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4202. $this->assertHtml($expected, $result);
  4203. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4204. $expected = [
  4205. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4206. ['label' => ['for' => 'model-field-0']],
  4207. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4208. 'option A',
  4209. '/label',
  4210. ['label' => ['for' => 'model-field-1']],
  4211. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4212. 'option B',
  4213. '/label',
  4214. ];
  4215. $this->assertHtml($expected, $result);
  4216. $result = $this->Form->radio(
  4217. 'Employee.gender',
  4218. ['male' => 'Male', 'female' => 'Female'],
  4219. ['form' => 'my-form']
  4220. );
  4221. $expected = [
  4222. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4223. ['label' => ['for' => 'employee-gender-male']],
  4224. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4225. 'Male',
  4226. '/label',
  4227. ['label' => ['for' => 'employee-gender-female']],
  4228. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4229. 'Female',
  4230. '/label',
  4231. ];
  4232. $this->assertHtml($expected, $result);
  4233. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4234. $expected = [
  4235. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4236. ['label' => ['for' => 'model-custom-0']],
  4237. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4238. 'option A',
  4239. '/label',
  4240. ['label' => ['for' => 'model-custom-1']],
  4241. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4242. 'option B',
  4243. '/label',
  4244. ];
  4245. $this->assertHtml($expected, $result);
  4246. $result = $this->Form->radio(
  4247. 'Employee.gender',
  4248. [
  4249. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4250. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4251. ]
  4252. );
  4253. $expected = [
  4254. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4255. ['label' => ['for' => 'employee-gender-male']],
  4256. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4257. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4258. 'Male',
  4259. '/label',
  4260. ['label' => ['for' => 'employee-gender-female']],
  4261. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4262. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4263. 'Female',
  4264. '/label',
  4265. ];
  4266. $this->assertHtml($expected, $result);
  4267. }
  4268. /**
  4269. * Test radio with complex options and empty disabled data.
  4270. *
  4271. * @return void
  4272. */
  4273. public function testRadioComplexDisabled()
  4274. {
  4275. $options = [
  4276. ['value' => 'r', 'text' => 'red'],
  4277. ['value' => 'b', 'text' => 'blue'],
  4278. ];
  4279. $attrs = ['disabled' => []];
  4280. $result = $this->Form->radio('Model.field', $options, $attrs);
  4281. $expected = [
  4282. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4283. ['label' => ['for' => 'model-field-r']],
  4284. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4285. 'red',
  4286. '/label',
  4287. ['label' => ['for' => 'model-field-b']],
  4288. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4289. 'blue',
  4290. '/label',
  4291. ];
  4292. $this->assertHtml($expected, $result);
  4293. $attrs = ['disabled' => ['r']];
  4294. $result = $this->Form->radio('Model.field', $options, $attrs);
  4295. $this->assertContains('disabled="disabled"', $result);
  4296. }
  4297. /**
  4298. * testRadioDefaultValue method
  4299. *
  4300. * Test default value setting on radio() method.
  4301. *
  4302. * @return void
  4303. */
  4304. public function testRadioDefaultValue()
  4305. {
  4306. $Articles = TableRegistry::get('Articles');
  4307. $title = $Articles->schema()->column('title');
  4308. $Articles->schema()->addColumn(
  4309. 'title',
  4310. ['default' => '1'] + $title
  4311. );
  4312. $this->Form->create($Articles->newEntity());
  4313. $result = $this->Form->radio('title', ['option A', 'option B']);
  4314. $expected = [
  4315. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4316. ['label' => ['for' => 'title-0']],
  4317. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4318. 'option A',
  4319. '/label',
  4320. ['label' => ['for' => 'title-1']],
  4321. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4322. 'option B',
  4323. '/label',
  4324. ];
  4325. $this->assertHtml($expected, $result);
  4326. }
  4327. /**
  4328. * testControlRadio method
  4329. *
  4330. * Test that input works with radio types.
  4331. *
  4332. * @return void
  4333. */
  4334. public function testControlRadio()
  4335. {
  4336. $result = $this->Form->control('test', [
  4337. 'type' => 'radio',
  4338. 'options' => ['A', 'B'],
  4339. ]);
  4340. $expected = [
  4341. ['div' => ['class' => 'input radio']],
  4342. '<label',
  4343. 'Test',
  4344. '/label',
  4345. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4346. ['label' => ['for' => 'test-0']],
  4347. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4348. 'A',
  4349. '/label',
  4350. ['label' => ['for' => 'test-1']],
  4351. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4352. 'B',
  4353. '/label',
  4354. '/div',
  4355. ];
  4356. $this->assertHtml($expected, $result);
  4357. $result = $this->Form->control('test', [
  4358. 'type' => 'radio',
  4359. 'options' => ['A', 'B'],
  4360. 'value' => '0'
  4361. ]);
  4362. $expected = [
  4363. ['div' => ['class' => 'input radio']],
  4364. '<label',
  4365. 'Test',
  4366. '/label',
  4367. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4368. ['label' => ['for' => 'test-0']],
  4369. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4370. 'A',
  4371. '/label',
  4372. ['label' => ['for' => 'test-1']],
  4373. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4374. 'B',
  4375. '/label',
  4376. '/div',
  4377. ];
  4378. $this->assertHtml($expected, $result);
  4379. $result = $this->Form->control('test', [
  4380. 'type' => 'radio',
  4381. 'options' => ['A', 'B'],
  4382. 'label' => false
  4383. ]);
  4384. $expected = [
  4385. ['div' => ['class' => 'input radio']],
  4386. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4387. ['label' => ['for' => 'test-0']],
  4388. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4389. 'A',
  4390. '/label',
  4391. ['label' => ['for' => 'test-1']],
  4392. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4393. 'B',
  4394. '/label',
  4395. '/div',
  4396. ];
  4397. $this->assertHtml($expected, $result);
  4398. }
  4399. /**
  4400. * testRadioNoLabel method
  4401. *
  4402. * Test that radio() works with label = false.
  4403. *
  4404. * @return void
  4405. */
  4406. public function testRadioNoLabel()
  4407. {
  4408. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4409. $expected = [
  4410. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4411. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4412. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4413. ];
  4414. $this->assertHtml($expected, $result);
  4415. }
  4416. /**
  4417. * testRadioControlInsideLabel method
  4418. *
  4419. * Test generating radio input inside label ala twitter bootstrap.
  4420. *
  4421. * @return void
  4422. */
  4423. public function testRadioControlInsideLabel()
  4424. {
  4425. $this->Form->templates([
  4426. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4427. 'radioWrapper' => '{{label}}'
  4428. ]);
  4429. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4430. //@codingStandardsIgnoreStart
  4431. $expected = [
  4432. ['input' => [
  4433. 'type' => 'hidden',
  4434. 'name' => 'Model[field]',
  4435. 'value' => ''
  4436. ]],
  4437. ['label' => ['for' => 'model-field-0']],
  4438. ['input' => [
  4439. 'type' => 'radio',
  4440. 'name' => 'Model[field]',
  4441. 'value' => '0',
  4442. 'id' => 'model-field-0'
  4443. ]],
  4444. 'option A',
  4445. '/label',
  4446. ['label' => ['for' => 'model-field-1']],
  4447. ['input' => [
  4448. 'type' => 'radio',
  4449. 'name' => 'Model[field]',
  4450. 'value' => '1',
  4451. 'id' => 'model-field-1'
  4452. ]],
  4453. 'option B',
  4454. '/label',
  4455. ];
  4456. //@codingStandardsIgnoreEnd
  4457. $this->assertHtml($expected, $result);
  4458. }
  4459. /**
  4460. * testRadioHiddenControlDisabling method
  4461. *
  4462. * Test disabling the hidden input for radio buttons.
  4463. *
  4464. * @return void
  4465. */
  4466. public function testRadioHiddenControlDisabling()
  4467. {
  4468. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4469. $expected = [
  4470. 'label' => ['for' => 'model-1-field-0'],
  4471. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4472. 'option A',
  4473. '/label'
  4474. ];
  4475. $this->assertHtml($expected, $result);
  4476. }
  4477. /**
  4478. * testRadioOutOfRange method
  4479. *
  4480. * Test radio element set generation.
  4481. *
  4482. * @return void
  4483. */
  4484. public function testRadioOutOfRange()
  4485. {
  4486. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4487. $expected = [
  4488. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4489. 'label' => ['for' => 'model-field-v'],
  4490. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4491. 'value',
  4492. '/label'
  4493. ];
  4494. $this->assertHtml($expected, $result);
  4495. }
  4496. /**
  4497. * testSelect method
  4498. *
  4499. * Test select element generation.
  4500. *
  4501. * @return void
  4502. */
  4503. public function testSelect()
  4504. {
  4505. $result = $this->Form->select('Model.field', []);
  4506. $expected = [
  4507. 'select' => ['name' => 'Model[field]'],
  4508. '/select'
  4509. ];
  4510. $this->assertHtml($expected, $result);
  4511. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4512. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4513. $expected = [
  4514. 'select' => ['name' => 'Model[field]'],
  4515. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4516. 'good',
  4517. '/option',
  4518. ['option' => ['value' => 'other']],
  4519. 'bad',
  4520. '/option',
  4521. '/select'
  4522. ];
  4523. $this->assertHtml($expected, $result);
  4524. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4525. $this->assertHtml($expected, $result);
  4526. $this->Form->request->data = [];
  4527. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4528. $expected = [
  4529. 'select' => ['name' => 'Model[field]'],
  4530. ['option' => ['value' => 'value']],
  4531. 'good',
  4532. '/option',
  4533. ['option' => ['value' => 'other']],
  4534. 'bad',
  4535. '/option',
  4536. '/select'
  4537. ];
  4538. $this->assertHtml($expected, $result);
  4539. $options = [
  4540. ['value' => 'first', 'text' => 'First'],
  4541. ['value' => 'first', 'text' => 'Another First'],
  4542. ];
  4543. $result = $this->Form->select(
  4544. 'Model.field',
  4545. $options,
  4546. ['escape' => false, 'empty' => false]
  4547. );
  4548. $expected = [
  4549. 'select' => ['name' => 'Model[field]'],
  4550. ['option' => ['value' => 'first']],
  4551. 'First',
  4552. '/option',
  4553. ['option' => ['value' => 'first']],
  4554. 'Another First',
  4555. '/option',
  4556. '/select'
  4557. ];
  4558. $this->assertHtml($expected, $result);
  4559. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4560. $result = $this->Form->select(
  4561. 'Model.contact_id',
  4562. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4563. ['escape' => false, 'empty' => 'pick something']
  4564. );
  4565. $expected = [
  4566. 'select' => ['name' => 'Model[contact_id]'],
  4567. ['option' => ['value' => '']], 'pick something', '/option',
  4568. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4569. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4570. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4571. '/select'
  4572. ];
  4573. $this->assertHtml($expected, $result);
  4574. $this->Form->request->data['Model']['field'] = 0;
  4575. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4576. $expected = [
  4577. 'select' => ['name' => 'Model[field]'],
  4578. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4579. ['option' => ['value' => '1']], 'Yes', '/option',
  4580. '/select'
  4581. ];
  4582. $this->assertHtml($expected, $result);
  4583. }
  4584. /**
  4585. * testSelectEscapeHtml method
  4586. *
  4587. * Test that select() escapes HTML.
  4588. *
  4589. * @return void
  4590. */
  4591. public function testSelectEscapeHtml()
  4592. {
  4593. $result = $this->Form->select(
  4594. 'Model.field',
  4595. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4596. ['empty' => false]
  4597. );
  4598. $expected = [
  4599. 'select' => ['name' => 'Model[field]'],
  4600. ['option' => ['value' => 'first']],
  4601. 'first &quot;html&quot; &lt;chars&gt;',
  4602. '/option',
  4603. ['option' => ['value' => 'second']],
  4604. 'value',
  4605. '/option',
  4606. '/select'
  4607. ];
  4608. $this->assertHtml($expected, $result);
  4609. $result = $this->Form->select(
  4610. 'Model.field',
  4611. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4612. ['escape' => false, 'empty' => false]
  4613. );
  4614. $expected = [
  4615. 'select' => ['name' => 'Model[field]'],
  4616. ['option' => ['value' => 'first']],
  4617. 'first "html" <chars>',
  4618. '/option',
  4619. ['option' => ['value' => 'second']],
  4620. 'value',
  4621. '/option',
  4622. '/select'
  4623. ];
  4624. $this->assertHtml($expected, $result);
  4625. }
  4626. /**
  4627. * testSelectRequired method
  4628. *
  4629. * Test select() with required and disabled attributes.
  4630. *
  4631. * @return void
  4632. */
  4633. public function testSelectRequired()
  4634. {
  4635. $this->article['required'] = [
  4636. 'user_id' => true
  4637. ];
  4638. $this->Form->create($this->article);
  4639. $result = $this->Form->select('user_id', ['option A']);
  4640. $expected = [
  4641. 'select' => [
  4642. 'name' => 'user_id',
  4643. 'required' => 'required'
  4644. ],
  4645. ['option' => ['value' => '0']], 'option A', '/option',
  4646. '/select'
  4647. ];
  4648. $this->assertHtml($expected, $result);
  4649. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4650. $expected = [
  4651. 'select' => [
  4652. 'name' => 'user_id',
  4653. 'disabled' => 'disabled'
  4654. ],
  4655. ['option' => ['value' => '0']], 'option A', '/option',
  4656. '/select'
  4657. ];
  4658. $this->assertHtml($expected, $result);
  4659. }
  4660. /**
  4661. * testNestedSelect method
  4662. *
  4663. * Test select element generation with optgroups.
  4664. *
  4665. * @return void
  4666. */
  4667. public function testNestedSelect()
  4668. {
  4669. $result = $this->Form->select(
  4670. 'Model.field',
  4671. [1 => 'One', 2 => 'Two', 'Three' => [
  4672. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4673. ]],
  4674. ['empty' => false]
  4675. );
  4676. $expected = [
  4677. 'select' => ['name' => 'Model[field]'],
  4678. ['option' => ['value' => 1]],
  4679. 'One',
  4680. '/option',
  4681. ['option' => ['value' => 2]],
  4682. 'Two',
  4683. '/option',
  4684. ['optgroup' => ['label' => 'Three']],
  4685. ['option' => ['value' => 3]],
  4686. 'Three',
  4687. '/option',
  4688. ['option' => ['value' => 4]],
  4689. 'Four',
  4690. '/option',
  4691. ['option' => ['value' => 5]],
  4692. 'Five',
  4693. '/option',
  4694. '/optgroup',
  4695. '/select'
  4696. ];
  4697. $this->assertHtml($expected, $result);
  4698. }
  4699. /**
  4700. * testSelectMultiple method
  4701. *
  4702. * Test generation of multiple select elements.
  4703. *
  4704. * @return void
  4705. */
  4706. public function testSelectMultiple()
  4707. {
  4708. $options = ['first', 'second', 'third'];
  4709. $result = $this->Form->select(
  4710. 'Model.multi_field',
  4711. $options,
  4712. ['form' => 'my-form', 'multiple' => true]
  4713. );
  4714. $expected = [
  4715. 'input' => [
  4716. 'type' => 'hidden',
  4717. 'name' => 'Model[multi_field]',
  4718. 'value' => '',
  4719. 'form' => 'my-form',
  4720. ],
  4721. 'select' => [
  4722. 'name' => 'Model[multi_field][]',
  4723. 'multiple' => 'multiple',
  4724. 'form' => 'my-form',
  4725. ],
  4726. ['option' => ['value' => '0']],
  4727. 'first',
  4728. '/option',
  4729. ['option' => ['value' => '1']],
  4730. 'second',
  4731. '/option',
  4732. ['option' => ['value' => '2']],
  4733. 'third',
  4734. '/option',
  4735. '/select'
  4736. ];
  4737. $this->assertHtml($expected, $result);
  4738. $result = $this->Form->select(
  4739. 'Model.multi_field',
  4740. $options,
  4741. ['multiple' => 'multiple', 'form' => 'my-form']
  4742. );
  4743. $this->assertHtml($expected, $result);
  4744. }
  4745. /**
  4746. * testCheckboxZeroValue method
  4747. *
  4748. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4749. *
  4750. * @return void
  4751. */
  4752. public function testCheckboxZeroValue()
  4753. {
  4754. $result = $this->Form->control('User.get_spam', [
  4755. 'type' => 'checkbox',
  4756. 'value' => '0',
  4757. 'hiddenField' => '1',
  4758. ]);
  4759. $expected = [
  4760. 'div' => ['class' => 'input checkbox'],
  4761. 'label' => ['for' => 'user-get-spam'],
  4762. ['input' => [
  4763. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4764. 'value' => '1'
  4765. ]],
  4766. ['input' => [
  4767. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4768. 'value' => '0', 'id' => 'user-get-spam'
  4769. ]],
  4770. 'Get Spam',
  4771. '/label',
  4772. '/div'
  4773. ];
  4774. $this->assertHtml($expected, $result);
  4775. }
  4776. /**
  4777. * testHabtmSelectBox method
  4778. *
  4779. * Test generation of habtm select boxes.
  4780. *
  4781. * @return void
  4782. */
  4783. public function testHabtmSelectBox()
  4784. {
  4785. $this->loadFixtures('Articles');
  4786. $options = [
  4787. 1 => 'blue',
  4788. 2 => 'red',
  4789. 3 => 'green'
  4790. ];
  4791. $tags = [
  4792. new Entity(['id' => 1, 'name' => 'blue']),
  4793. new Entity(['id' => 3, 'name' => 'green'])
  4794. ];
  4795. $article = new Article(['tags' => $tags]);
  4796. $this->Form->create($article);
  4797. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4798. $expected = [
  4799. 'div' => ['class' => 'input select'],
  4800. 'label' => ['for' => 'tags-ids'],
  4801. 'Tags',
  4802. '/label',
  4803. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4804. 'select' => [
  4805. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4806. 'multiple' => 'multiple'
  4807. ],
  4808. ['option' => ['value' => '1', 'selected' => 'selected']],
  4809. 'blue',
  4810. '/option',
  4811. ['option' => ['value' => '2']],
  4812. 'red',
  4813. '/option',
  4814. ['option' => ['value' => '3', 'selected' => 'selected']],
  4815. 'green',
  4816. '/option',
  4817. '/select',
  4818. '/div'
  4819. ];
  4820. $this->assertHtml($expected, $result);
  4821. // make sure only 50 is selected, and not 50f5c0cf
  4822. $options = [
  4823. '1' => 'blue',
  4824. '50f5c0cf' => 'red',
  4825. '50' => 'green'
  4826. ];
  4827. $tags = [
  4828. new Entity(['id' => 1, 'name' => 'blue']),
  4829. new Entity(['id' => 50, 'name' => 'green'])
  4830. ];
  4831. $article = new Article(['tags' => $tags]);
  4832. $this->Form->create($article);
  4833. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4834. $expected = [
  4835. 'div' => ['class' => 'input select'],
  4836. 'label' => ['for' => 'tags-ids'],
  4837. 'Tags',
  4838. '/label',
  4839. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4840. 'select' => [
  4841. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4842. 'multiple' => 'multiple'
  4843. ],
  4844. ['option' => ['value' => '1', 'selected' => 'selected']],
  4845. 'blue',
  4846. '/option',
  4847. ['option' => ['value' => '50f5c0cf']],
  4848. 'red',
  4849. '/option',
  4850. ['option' => ['value' => '50', 'selected' => 'selected']],
  4851. 'green',
  4852. '/option',
  4853. '/select',
  4854. '/div'
  4855. ];
  4856. $this->assertHtml($expected, $result);
  4857. $spacecraft = [
  4858. 1 => 'Orion',
  4859. 2 => 'Helios'
  4860. ];
  4861. $this->View->viewVars['spacecraft'] = $spacecraft;
  4862. $this->Form->create();
  4863. $result = $this->Form->control('spacecraft._ids');
  4864. $expected = [
  4865. 'div' => ['class' => 'input select'],
  4866. 'label' => ['for' => 'spacecraft-ids'],
  4867. 'Spacecraft',
  4868. '/label',
  4869. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4870. 'select' => [
  4871. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4872. 'multiple' => 'multiple'
  4873. ],
  4874. ['option' => ['value' => '1']],
  4875. 'Orion',
  4876. '/option',
  4877. ['option' => ['value' => '2']],
  4878. 'Helios',
  4879. '/option',
  4880. '/select',
  4881. '/div'
  4882. ];
  4883. $this->assertHtml($expected, $result);
  4884. }
  4885. /**
  4886. * testErrorsForBelongsToManySelect method
  4887. *
  4888. * Tests that errors for belongsToMany select fields are being
  4889. * picked up properly.
  4890. *
  4891. * @return void
  4892. */
  4893. public function testErrorsForBelongsToManySelect()
  4894. {
  4895. $spacecraft = [
  4896. 1 => 'Orion',
  4897. 2 => 'Helios'
  4898. ];
  4899. $this->View->viewVars['spacecraft'] = $spacecraft;
  4900. $article = new Article();
  4901. $article->errors('spacecraft', ['Invalid']);
  4902. $this->Form->create($article);
  4903. $result = $this->Form->control('spacecraft._ids');
  4904. $expected = [
  4905. ['div' => ['class' => 'input select error']],
  4906. 'label' => ['for' => 'spacecraft-ids'],
  4907. 'Spacecraft',
  4908. '/label',
  4909. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4910. 'select' => [
  4911. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4912. 'multiple' => 'multiple'
  4913. ],
  4914. ['option' => ['value' => '1']],
  4915. 'Orion',
  4916. '/option',
  4917. ['option' => ['value' => '2']],
  4918. 'Helios',
  4919. '/option',
  4920. '/select',
  4921. ['div' => ['class' => 'error-message']],
  4922. 'Invalid',
  4923. '/div',
  4924. '/div'
  4925. ];
  4926. $this->assertHtml($expected, $result);
  4927. }
  4928. /**
  4929. * testSelectMultipleCheckboxes method
  4930. *
  4931. * Test generation of multi select elements in checkbox format.
  4932. *
  4933. * @return void
  4934. */
  4935. public function testSelectMultipleCheckboxes()
  4936. {
  4937. $result = $this->Form->select(
  4938. 'Model.multi_field',
  4939. ['first', 'second', 'third'],
  4940. ['multiple' => 'checkbox']
  4941. );
  4942. $expected = [
  4943. 'input' => [
  4944. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4945. ],
  4946. ['div' => ['class' => 'checkbox']],
  4947. ['label' => ['for' => 'model-multi-field-0']],
  4948. ['input' => [
  4949. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4950. 'value' => '0', 'id' => 'model-multi-field-0'
  4951. ]],
  4952. 'first',
  4953. '/label',
  4954. '/div',
  4955. ['div' => ['class' => 'checkbox']],
  4956. ['label' => ['for' => 'model-multi-field-1']],
  4957. ['input' => [
  4958. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4959. 'value' => '1', 'id' => 'model-multi-field-1'
  4960. ]],
  4961. 'second',
  4962. '/label',
  4963. '/div',
  4964. ['div' => ['class' => 'checkbox']],
  4965. ['label' => ['for' => 'model-multi-field-2']],
  4966. ['input' => [
  4967. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4968. 'value' => '2', 'id' => 'model-multi-field-2'
  4969. ]],
  4970. 'third',
  4971. '/label',
  4972. '/div'
  4973. ];
  4974. $this->assertHtml($expected, $result);
  4975. $result = $this->Form->select(
  4976. 'Model.multi_field',
  4977. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4978. ['multiple' => 'checkbox']
  4979. );
  4980. $expected = [
  4981. 'input' => [
  4982. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4983. ],
  4984. ['div' => ['class' => 'checkbox']],
  4985. ['label' => ['for' => 'model-multi-field-a+']],
  4986. ['input' => [
  4987. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4988. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  4989. ]],
  4990. 'first',
  4991. '/label',
  4992. '/div',
  4993. ['div' => ['class' => 'checkbox']],
  4994. ['label' => ['for' => 'model-multi-field-a++']],
  4995. ['input' => [
  4996. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4997. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  4998. ]],
  4999. 'second',
  5000. '/label',
  5001. '/div',
  5002. ['div' => ['class' => 'checkbox']],
  5003. ['label' => ['for' => 'model-multi-field-a+++']],
  5004. ['input' => [
  5005. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5006. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  5007. ]],
  5008. 'third',
  5009. '/label',
  5010. '/div'
  5011. ];
  5012. $this->assertHtml($expected, $result);
  5013. $result = $this->Form->select(
  5014. 'Model.multi_field',
  5015. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5016. ['multiple' => 'checkbox']
  5017. );
  5018. $expected = [
  5019. 'input' => [
  5020. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5021. ],
  5022. ['div' => ['class' => 'checkbox']],
  5023. ['label' => ['for' => 'model-multi-field-a-b']],
  5024. ['input' => [
  5025. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5026. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5027. ]],
  5028. 'first',
  5029. '/label',
  5030. '/div',
  5031. ['div' => ['class' => 'checkbox']],
  5032. ['label' => ['for' => 'model-multi-field-a-b1']],
  5033. ['input' => [
  5034. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5035. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5036. ]],
  5037. 'second',
  5038. '/label',
  5039. '/div',
  5040. ['div' => ['class' => 'checkbox']],
  5041. ['label' => ['for' => 'model-multi-field-a-b2']],
  5042. ['input' => [
  5043. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5044. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5045. ]],
  5046. 'third',
  5047. '/label',
  5048. '/div'
  5049. ];
  5050. $this->assertHtml($expected, $result);
  5051. }
  5052. /**
  5053. * testSelectMultipleCheckboxRequestData method
  5054. *
  5055. * Ensure that multiCheckbox reads from the request data.
  5056. *
  5057. * @return void
  5058. */
  5059. public function testSelectMultipleCheckboxRequestData()
  5060. {
  5061. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  5062. $result = $this->Form->select(
  5063. 'Model.tags',
  5064. ['1' => 'first', 'Array' => 'Array'],
  5065. ['multiple' => 'checkbox']
  5066. );
  5067. $expected = [
  5068. 'input' => [
  5069. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5070. ],
  5071. ['div' => ['class' => 'checkbox']],
  5072. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5073. ['input' => [
  5074. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5075. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5076. ]],
  5077. 'first',
  5078. '/label',
  5079. '/div',
  5080. ['div' => ['class' => 'checkbox']],
  5081. ['label' => ['for' => 'model-tags-array']],
  5082. ['input' => [
  5083. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5084. 'value' => 'Array', 'id' => 'model-tags-array'
  5085. ]],
  5086. 'Array',
  5087. '/label',
  5088. '/div'
  5089. ];
  5090. $this->assertHtml($expected, $result);
  5091. }
  5092. /**
  5093. * testSelectMultipleCheckboxSecurity method
  5094. *
  5095. * Checks the security hash array generated for multiple-input checkbox elements.
  5096. *
  5097. * @return void
  5098. */
  5099. public function testSelectMultipleCheckboxSecurity()
  5100. {
  5101. $this->Form->request->params['_Token'] = 'testKey';
  5102. $this->assertEquals([], $this->Form->fields);
  5103. $this->Form->select(
  5104. 'Model.multi_field',
  5105. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5106. ['multiple' => 'checkbox']
  5107. );
  5108. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5109. $result = $this->Form->secure($this->Form->fields);
  5110. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  5111. $this->assertRegExp('/"' . $key . '"/', $result);
  5112. }
  5113. /**
  5114. * testSelectMultipleSecureWithNoOptions method
  5115. *
  5116. * Multiple select elements should always be secured as they always participate
  5117. * in the POST data.
  5118. *
  5119. * @return void
  5120. */
  5121. public function testSelectMultipleSecureWithNoOptions()
  5122. {
  5123. $this->assertEquals([], $this->Form->fields);
  5124. $this->Form->select(
  5125. 'Model.select',
  5126. [],
  5127. ['multiple' => true]
  5128. );
  5129. $this->assertEquals(['Model.select'], $this->Form->fields);
  5130. }
  5131. /**
  5132. * testSelectNoSecureWithNoOptions method
  5133. *
  5134. * When a select box has no options it should not be added to the fields list
  5135. * as it always fail post validation.
  5136. *
  5137. * @return void
  5138. */
  5139. public function testSelectNoSecureWithNoOptions()
  5140. {
  5141. $this->Form->request->params['_Token'] = 'testkey';
  5142. $this->assertEquals([], $this->Form->fields);
  5143. $this->Form->select(
  5144. 'Model.select',
  5145. []
  5146. );
  5147. $this->assertEquals([], $this->Form->fields);
  5148. $this->Form->select(
  5149. 'Model.user_id',
  5150. [],
  5151. ['empty' => true]
  5152. );
  5153. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5154. }
  5155. /**
  5156. * testControlMultipleCheckboxes method
  5157. *
  5158. * Test control() resulting in multi select elements being generated.
  5159. *
  5160. * @return void
  5161. */
  5162. public function testControlMultipleCheckboxes()
  5163. {
  5164. $result = $this->Form->control('Model.multi_field', [
  5165. 'options' => ['first', 'second', 'third'],
  5166. 'multiple' => 'checkbox'
  5167. ]);
  5168. $expected = [
  5169. ['div' => ['class' => 'input select']],
  5170. ['label' => ['for' => 'model-multi-field']],
  5171. 'Multi Field',
  5172. '/label',
  5173. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5174. ['div' => ['class' => 'checkbox']],
  5175. ['label' => ['for' => 'model-multi-field-0']],
  5176. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5177. 'first',
  5178. '/label',
  5179. '/div',
  5180. ['div' => ['class' => 'checkbox']],
  5181. ['label' => ['for' => 'model-multi-field-1']],
  5182. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5183. 'second',
  5184. '/label',
  5185. '/div',
  5186. ['div' => ['class' => 'checkbox']],
  5187. ['label' => ['for' => 'model-multi-field-2']],
  5188. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5189. 'third',
  5190. '/label',
  5191. '/div',
  5192. '/div'
  5193. ];
  5194. $this->assertHtml($expected, $result);
  5195. $result = $this->Form->control('Model.multi_field', [
  5196. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5197. 'multiple' => 'checkbox'
  5198. ]);
  5199. $expected = [
  5200. ['div' => ['class' => 'input select']],
  5201. ['label' => ['for' => 'model-multi-field']],
  5202. 'Multi Field',
  5203. '/label',
  5204. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5205. ['div' => ['class' => 'checkbox']],
  5206. ['label' => ['for' => 'model-multi-field-a']],
  5207. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5208. 'first',
  5209. '/label',
  5210. '/div',
  5211. ['div' => ['class' => 'checkbox']],
  5212. ['label' => ['for' => 'model-multi-field-b']],
  5213. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5214. 'second',
  5215. '/label',
  5216. '/div',
  5217. ['div' => ['class' => 'checkbox']],
  5218. ['label' => ['for' => 'model-multi-field-c']],
  5219. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5220. 'third',
  5221. '/label',
  5222. '/div',
  5223. '/div'
  5224. ];
  5225. $this->assertHtml($expected, $result);
  5226. }
  5227. /**
  5228. * testSelectHiddenFieldOmission method
  5229. *
  5230. * Test that select() with 'hiddenField' => false omits the hidden field.
  5231. *
  5232. * @return void
  5233. */
  5234. public function testSelectHiddenFieldOmission()
  5235. {
  5236. $result = $this->Form->select(
  5237. 'Model.multi_field',
  5238. ['first', 'second'],
  5239. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5240. );
  5241. $this->assertNotContains('type="hidden"', $result);
  5242. }
  5243. /**
  5244. * testSelectCheckboxMultipleOverrideName method
  5245. *
  5246. * Test that select() with multiple = checkbox works with overriding name attribute.
  5247. *
  5248. * @return void
  5249. */
  5250. public function testSelectCheckboxMultipleOverrideName()
  5251. {
  5252. $result = $this->Form->select('category', ['1', '2'], [
  5253. 'multiple' => 'checkbox',
  5254. 'name' => 'fish',
  5255. ]);
  5256. $expected = [
  5257. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5258. ['div' => ['class' => 'checkbox']],
  5259. ['label' => ['for' => 'fish-0']],
  5260. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5261. '1',
  5262. '/label',
  5263. '/div',
  5264. ['div' => ['class' => 'checkbox']],
  5265. ['label' => ['for' => 'fish-1']],
  5266. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5267. '2',
  5268. '/label',
  5269. '/div'
  5270. ];
  5271. $this->assertHtml($expected, $result);
  5272. $result = $this->Form->multiCheckbox(
  5273. 'category',
  5274. new Collection(['1', '2']),
  5275. ['name' => 'fish']
  5276. );
  5277. $this->assertHtml($expected, $result);
  5278. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5279. 'name' => 'fish',
  5280. ]);
  5281. $this->assertHtml($expected, $result);
  5282. }
  5283. /**
  5284. * testControlMultiCheckbox method
  5285. *
  5286. * Test that control() works with multicheckbox.
  5287. *
  5288. * @return void
  5289. */
  5290. public function testControlMultiCheckbox()
  5291. {
  5292. $result = $this->Form->control('category', [
  5293. 'type' => 'multicheckbox',
  5294. 'options' => ['1', '2'],
  5295. ]);
  5296. $expected = [
  5297. ['div' => ['class' => 'input multicheckbox']],
  5298. '<label',
  5299. 'Category',
  5300. '/label',
  5301. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5302. ['div' => ['class' => 'checkbox']],
  5303. ['label' => ['for' => 'category-0']],
  5304. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5305. '1',
  5306. '/label',
  5307. '/div',
  5308. ['div' => ['class' => 'checkbox']],
  5309. ['label' => ['for' => 'category-1']],
  5310. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5311. '2',
  5312. '/label',
  5313. '/div',
  5314. '/div',
  5315. ];
  5316. $this->assertHtml($expected, $result);
  5317. }
  5318. /**
  5319. * testCheckbox method
  5320. *
  5321. * Test generation of checkboxes.
  5322. *
  5323. * @return void
  5324. */
  5325. public function testCheckbox()
  5326. {
  5327. $result = $this->Form->checkbox('Model.field');
  5328. $expected = [
  5329. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5330. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5331. ];
  5332. $this->assertHtml($expected, $result);
  5333. $result = $this->Form->checkbox('Model.field', [
  5334. 'id' => 'theID',
  5335. 'value' => 'myvalue',
  5336. 'form' => 'my-form',
  5337. ]);
  5338. $expected = [
  5339. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5340. ['input' => [
  5341. 'type' => 'checkbox', 'name' => 'Model[field]',
  5342. 'value' => 'myvalue', 'id' => 'theID',
  5343. 'form' => 'my-form',
  5344. ]]
  5345. ];
  5346. $this->assertHtml($expected, $result);
  5347. }
  5348. /**
  5349. * testCheckboxDefaultValue method
  5350. *
  5351. * Test default value setting on checkbox() method.
  5352. *
  5353. * @return void
  5354. */
  5355. public function testCheckboxDefaultValue()
  5356. {
  5357. $this->Form->request->data['Model']['field'] = false;
  5358. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5359. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5360. $this->assertHtml($expected, $result);
  5361. unset($this->Form->request->data['Model']['field']);
  5362. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5363. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5364. $this->assertHtml($expected, $result);
  5365. $this->Form->request->data['Model']['field'] = true;
  5366. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5367. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5368. $this->assertHtml($expected, $result);
  5369. unset($this->Form->request->data['Model']['field']);
  5370. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5371. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5372. $this->assertHtml($expected, $result);
  5373. $Articles = TableRegistry::get('Articles');
  5374. $Articles->schema()->addColumn(
  5375. 'published',
  5376. ['type' => 'boolean', 'null' => false, 'default' => true]
  5377. );
  5378. $this->Form->create($Articles->newEntity());
  5379. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5380. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5381. $this->assertHtml($expected, $result);
  5382. }
  5383. /**
  5384. * testCheckboxCheckedAndError method
  5385. *
  5386. * Test checkbox being checked or having errors.
  5387. *
  5388. * @return void
  5389. */
  5390. public function testCheckboxCheckedAndError()
  5391. {
  5392. $this->article['errors'] = [
  5393. 'published' => true
  5394. ];
  5395. $this->Form->request->data['published'] = 'myvalue';
  5396. $this->Form->create($this->article);
  5397. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5398. $expected = [
  5399. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5400. ['input' => [
  5401. 'type' => 'checkbox',
  5402. 'name' => 'published',
  5403. 'value' => 'myvalue',
  5404. 'id' => 'theID',
  5405. 'checked' => 'checked',
  5406. 'class' => 'form-error'
  5407. ]]
  5408. ];
  5409. $this->assertHtml($expected, $result);
  5410. $this->Form->request->data['published'] = '';
  5411. $result = $this->Form->checkbox('published');
  5412. $expected = [
  5413. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5414. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5415. ];
  5416. $this->assertHtml($expected, $result);
  5417. }
  5418. /**
  5419. * testCheckboxCustomNameAttribute method
  5420. *
  5421. * Test checkbox() with a custom name attribute.
  5422. *
  5423. * @return void
  5424. */
  5425. public function testCheckboxCustomNameAttribute()
  5426. {
  5427. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5428. $expected = [
  5429. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5430. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5431. ];
  5432. $this->assertHtml($expected, $result);
  5433. }
  5434. /**
  5435. * testCheckboxHiddenField method
  5436. *
  5437. * Test that the hidden input for checkboxes can be omitted or set to a
  5438. * specific value.
  5439. *
  5440. * @return void
  5441. */
  5442. public function testCheckboxHiddenField()
  5443. {
  5444. $result = $this->Form->checkbox('UserForm.something', [
  5445. 'hiddenField' => false
  5446. ]);
  5447. $expected = [
  5448. 'input' => [
  5449. 'type' => 'checkbox',
  5450. 'name' => 'UserForm[something]',
  5451. 'value' => '1'
  5452. ],
  5453. ];
  5454. $this->assertHtml($expected, $result);
  5455. $result = $this->Form->checkbox('UserForm.something', [
  5456. 'value' => 'Y',
  5457. 'hiddenField' => 'N',
  5458. ]);
  5459. $expected = [
  5460. ['input' => [
  5461. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5462. 'value' => 'N'
  5463. ]],
  5464. ['input' => [
  5465. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5466. 'value' => 'Y'
  5467. ]],
  5468. ];
  5469. $this->assertHtml($expected, $result);
  5470. }
  5471. /**
  5472. * testTime method
  5473. *
  5474. * Test the time type.
  5475. *
  5476. * @return void
  5477. */
  5478. public function testTime()
  5479. {
  5480. $result = $this->Form->time('start_time', [
  5481. 'timeFormat' => 12,
  5482. 'interval' => 5,
  5483. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5484. ]);
  5485. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5486. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5487. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5488. $this->assertNotContains('year', $result);
  5489. $this->assertNotContains('month', $result);
  5490. $this->assertNotContains('day', $result);
  5491. $result = $this->Form->time('start_time', [
  5492. 'timeFormat' => 12,
  5493. 'interval' => 5,
  5494. 'value' => '2014-03-08 16:30:00'
  5495. ]);
  5496. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5497. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5498. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5499. $this->assertNotContains('year', $result);
  5500. $this->assertNotContains('month', $result);
  5501. $this->assertNotContains('day', $result);
  5502. }
  5503. /**
  5504. * testTimeFormat24NoMeridian method
  5505. *
  5506. * Ensure that timeFormat=24 has no merdian.
  5507. *
  5508. * @return void.
  5509. */
  5510. public function testTimeFormat24NoMeridian()
  5511. {
  5512. $result = $this->Form->time('start_time', [
  5513. 'timeFormat' => 24,
  5514. 'interval' => 5,
  5515. 'value' => '2014-03-08 16:30:00'
  5516. ]);
  5517. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5518. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5519. $this->assertNotContains('meridian', $result);
  5520. $this->assertNotContains('pm', $result);
  5521. $this->assertNotContains('year', $result);
  5522. $this->assertNotContains('month', $result);
  5523. $this->assertNotContains('day', $result);
  5524. }
  5525. /**
  5526. * testDate method
  5527. *
  5528. * Test the date type.
  5529. *
  5530. * @return void
  5531. */
  5532. public function testDate()
  5533. {
  5534. $result = $this->Form->date('start_day', [
  5535. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5536. ]);
  5537. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5538. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5539. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5540. $this->assertNotContains('hour', $result);
  5541. $this->assertNotContains('minute', $result);
  5542. $this->assertNotContains('second', $result);
  5543. $this->assertNotContains('meridian', $result);
  5544. }
  5545. /**
  5546. * testDateTime method
  5547. *
  5548. * Test generation of date/time select elements.
  5549. *
  5550. * @return void
  5551. */
  5552. public function testDateTime()
  5553. {
  5554. extract($this->dateRegex);
  5555. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5556. $now = strtotime('now');
  5557. $expected = [
  5558. ['select' => ['name' => 'Contact[date][year]']],
  5559. ['option' => ['value' => '']],
  5560. '/option',
  5561. $yearsRegex,
  5562. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5563. date('Y', $now),
  5564. '/option',
  5565. '*/select',
  5566. ['select' => ['name' => 'Contact[date][month]']],
  5567. ['option' => ['value' => '']],
  5568. '/option',
  5569. $monthsRegex,
  5570. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5571. date('F', $now),
  5572. '/option',
  5573. '*/select',
  5574. ['select' => ['name' => 'Contact[date][day]']],
  5575. ['option' => ['value' => '']],
  5576. '/option',
  5577. $daysRegex,
  5578. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5579. date('j', $now),
  5580. '/option',
  5581. '*/select',
  5582. ['select' => ['name' => 'Contact[date][hour]']],
  5583. ['option' => ['value' => '']],
  5584. '/option',
  5585. $hoursRegex,
  5586. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5587. date('G', $now),
  5588. '/option',
  5589. '*/select',
  5590. ['select' => ['name' => 'Contact[date][minute]']],
  5591. ['option' => ['value' => '']],
  5592. '/option',
  5593. $minutesRegex,
  5594. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5595. date('i', $now),
  5596. '/option',
  5597. '*/select',
  5598. ];
  5599. $this->assertHtml($expected, $result);
  5600. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5601. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5602. $now = strtotime('now');
  5603. $expected = [
  5604. ['select' => ['name' => 'Contact[date][year]']],
  5605. $yearsRegex,
  5606. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5607. date('Y', $now),
  5608. '/option',
  5609. '*/select',
  5610. ['select' => ['name' => 'Contact[date][month]']],
  5611. $monthsRegex,
  5612. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5613. date('F', $now),
  5614. '/option',
  5615. '*/select',
  5616. ['select' => ['name' => 'Contact[date][day]']],
  5617. $daysRegex,
  5618. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5619. date('j', $now),
  5620. '/option',
  5621. '*/select',
  5622. ['select' => ['name' => 'Contact[date][hour]']],
  5623. $hoursRegex,
  5624. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5625. date('G', $now),
  5626. '/option',
  5627. '*/select',
  5628. ['select' => ['name' => 'Contact[date][minute]']],
  5629. $minutesRegex,
  5630. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5631. date('i', $now),
  5632. '/option',
  5633. '*/select',
  5634. ];
  5635. $this->assertHtml($expected, $result);
  5636. }
  5637. /**
  5638. * testDateTimeSecured method
  5639. *
  5640. * Test that datetime fields are added to protected fields list.
  5641. *
  5642. * @return void
  5643. */
  5644. public function testDateTimeSecured()
  5645. {
  5646. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5647. $this->Form->dateTime('Contact.date');
  5648. $expected = [
  5649. 'Contact.date.year',
  5650. 'Contact.date.month',
  5651. 'Contact.date.day',
  5652. 'Contact.date.hour',
  5653. 'Contact.date.minute',
  5654. ];
  5655. $this->assertEquals($expected, $this->Form->fields);
  5656. $this->Form->fields = [];
  5657. $this->Form->date('Contact.published');
  5658. $expected = [
  5659. 'Contact.published.year',
  5660. 'Contact.published.month',
  5661. 'Contact.published.day',
  5662. ];
  5663. $this->assertEquals($expected, $this->Form->fields);
  5664. }
  5665. /**
  5666. * testDateTimeSecuredDisabled method
  5667. *
  5668. * Test that datetime fields are added to protected fields list.
  5669. *
  5670. * @return void
  5671. */
  5672. public function testDateTimeSecuredDisabled()
  5673. {
  5674. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5675. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5676. $expected = [];
  5677. $this->assertEquals($expected, $this->Form->fields);
  5678. $this->Form->fields = [];
  5679. $this->Form->date('Contact.published', ['secure' => false]);
  5680. $expected = [];
  5681. $this->assertEquals($expected, $this->Form->fields);
  5682. }
  5683. /**
  5684. * testDatetimeEmpty method
  5685. *
  5686. * Test empty defaulting to true for datetime.
  5687. *
  5688. * @return void
  5689. */
  5690. public function testDatetimeEmpty()
  5691. {
  5692. extract($this->dateRegex);
  5693. $result = $this->Form->dateTime('Contact.date', [
  5694. 'timeFormat' => 12,
  5695. 'empty' => true,
  5696. 'default' => true
  5697. ]);
  5698. $expected = [
  5699. ['select' => ['name' => 'Contact[date][year]']],
  5700. $yearsRegex,
  5701. ['option' => ['value' => '']],
  5702. '/option',
  5703. '*/select',
  5704. ['select' => ['name' => 'Contact[date][month]']],
  5705. $monthsRegex,
  5706. ['option' => ['value' => '']],
  5707. '/option',
  5708. '*/select',
  5709. ['select' => ['name' => 'Contact[date][day]']],
  5710. $daysRegex,
  5711. ['option' => ['value' => '']],
  5712. '/option',
  5713. '*/select',
  5714. ['select' => ['name' => 'Contact[date][hour]']],
  5715. $hoursRegex,
  5716. ['option' => ['value' => '']],
  5717. '/option',
  5718. '*/select',
  5719. ['select' => ['name' => 'Contact[date][minute]']],
  5720. $minutesRegex,
  5721. ['option' => ['value' => '']],
  5722. '/option',
  5723. '*/select',
  5724. ['select' => ['name' => 'Contact[date][meridian]']],
  5725. $meridianRegex,
  5726. ['option' => ['value' => '']],
  5727. '/option',
  5728. '*/select'
  5729. ];
  5730. $this->assertHtml($expected, $result);
  5731. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5732. }
  5733. /**
  5734. * testDatetimeMinuteInterval method
  5735. *
  5736. * Test datetime with interval option.
  5737. *
  5738. * @return void
  5739. */
  5740. public function testDatetimeMinuteInterval()
  5741. {
  5742. extract($this->dateRegex);
  5743. $result = $this->Form->dateTime('Contact.date', [
  5744. 'interval' => 5,
  5745. 'value' => ''
  5746. ]);
  5747. $expected = [
  5748. ['select' => ['name' => 'Contact[date][year]']],
  5749. $yearsRegex,
  5750. ['option' => ['selected' => 'selected', 'value' => '']],
  5751. '/option',
  5752. '*/select',
  5753. ['select' => ['name' => 'Contact[date][month]']],
  5754. $monthsRegex,
  5755. ['option' => ['selected' => 'selected', 'value' => '']],
  5756. '/option',
  5757. '*/select',
  5758. ['select' => ['name' => 'Contact[date][day]']],
  5759. $daysRegex,
  5760. ['option' => ['selected' => 'selected', 'value' => '']],
  5761. '/option',
  5762. '*/select',
  5763. ['select' => ['name' => 'Contact[date][hour]']],
  5764. $hoursRegex,
  5765. ['option' => ['selected' => 'selected', 'value' => '']],
  5766. '/option',
  5767. '*/select',
  5768. ['select' => ['name' => 'Contact[date][minute]']],
  5769. $minutesRegex,
  5770. ['option' => ['selected' => 'selected', 'value' => '']],
  5771. '/option',
  5772. ['option' => ['value' => '00']],
  5773. '00',
  5774. '/option',
  5775. ['option' => ['value' => '05']],
  5776. '05',
  5777. '/option',
  5778. ['option' => ['value' => '10']],
  5779. '10',
  5780. '/option',
  5781. '*/select',
  5782. ];
  5783. $this->assertHtml($expected, $result);
  5784. }
  5785. /**
  5786. * testDateTimeRounding method
  5787. *
  5788. * Test dateTime with rounding.
  5789. *
  5790. * @return void
  5791. */
  5792. public function testDateTimeRounding()
  5793. {
  5794. $this->Form->request->data['Contact'] = [
  5795. 'date' => [
  5796. 'day' => '13',
  5797. 'month' => '12',
  5798. 'year' => '2010',
  5799. 'hour' => '04',
  5800. 'minute' => '19',
  5801. 'meridian' => 'AM'
  5802. ]
  5803. ];
  5804. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5805. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5806. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5807. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5808. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5809. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5810. }
  5811. /**
  5812. * testDatetimeWithDefault method
  5813. *
  5814. * Test that datetime() and default values work.
  5815. *
  5816. * @return void
  5817. */
  5818. public function testDatetimeWithDefault()
  5819. {
  5820. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5821. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5822. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5823. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5824. $result = $this->Form->dateTime('Contact.updated', [
  5825. 'default' => '2009-06-01 11:15:30'
  5826. ]);
  5827. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5828. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5829. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5830. }
  5831. /**
  5832. * testDateTimeAllZeros method
  5833. *
  5834. * Test datetime() with all zeros.
  5835. *
  5836. * @return void
  5837. */
  5838. public function testDateTimeAllZeros()
  5839. {
  5840. $result = $this->Form->dateTime('Contact.date', [
  5841. 'timeFormat' => false,
  5842. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5843. 'value' => '0000-00-00'
  5844. ]);
  5845. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5846. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5847. }
  5848. /**
  5849. * testDateTimeEmptyAsArray method
  5850. *
  5851. * @return void
  5852. */
  5853. public function testDateTimeEmptyAsArray()
  5854. {
  5855. $result = $this->Form->dateTime('Contact.date', [
  5856. 'empty' => [
  5857. 'day' => 'DAY',
  5858. 'month' => 'MONTH',
  5859. 'year' => 'YEAR',
  5860. 'hour' => 'HOUR',
  5861. 'minute' => 'MINUTE',
  5862. 'meridian' => false
  5863. ],
  5864. 'default' => true
  5865. ]);
  5866. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5867. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5868. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5869. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5870. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5871. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5872. $result = $this->Form->dateTime('Contact.date', [
  5873. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5874. 'default' => true
  5875. ]);
  5876. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5877. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5878. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5879. }
  5880. /**
  5881. * testFormDateTimeMulti method
  5882. *
  5883. * Test multiple datetime element generation.
  5884. *
  5885. * @return void
  5886. */
  5887. public function testFormDateTimeMulti()
  5888. {
  5889. extract($this->dateRegex);
  5890. $result = $this->Form->dateTime('Contact.1.updated');
  5891. $this->assertContains('Contact[1][updated][month]', $result);
  5892. $this->assertContains('Contact[1][updated][day]', $result);
  5893. $this->assertContains('Contact[1][updated][year]', $result);
  5894. $this->assertContains('Contact[1][updated][hour]', $result);
  5895. $this->assertContains('Contact[1][updated][minute]', $result);
  5896. }
  5897. /**
  5898. * testDateTimeLabelIdMatchesFirstControl method
  5899. *
  5900. * When changing the date format, the label should always focus the first select box when
  5901. * clicked.
  5902. *
  5903. * @return void
  5904. */
  5905. public function testDateTimeLabelIdMatchesFirstControl()
  5906. {
  5907. $result = $this->Form->control('Model.date', ['type' => 'date']);
  5908. $this->assertContains('<label>Date</label>', $result);
  5909. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5910. $this->assertContains('<label>Date</label>', $result);
  5911. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5912. $this->assertContains('<label>Date</label>', $result);
  5913. }
  5914. /**
  5915. * testDateTimeSecondOptions method
  5916. *
  5917. * Test datetime second=true.
  5918. *
  5919. * @return void
  5920. */
  5921. public function testDateTimeSecondOptions()
  5922. {
  5923. $result = $this->Form->dateTime('updated', ['second' => true]);
  5924. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5925. $result = $this->Form->dateTime('updated', ['second' => []]);
  5926. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5927. $result = $this->Form->dateTime('updated', ['second' => null]);
  5928. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5929. $result = $this->Form->dateTime('updated', ['second' => false]);
  5930. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5931. }
  5932. /**
  5933. * testMonth method
  5934. *
  5935. * Test generation of a month input.
  5936. *
  5937. * @return void
  5938. */
  5939. public function testMonth()
  5940. {
  5941. $result = $this->Form->month('Model.field', ['value' => '']);
  5942. $expected = [
  5943. ['select' => ['name' => 'Model[field][month]']],
  5944. ['option' => ['value' => '', 'selected' => 'selected']],
  5945. '/option',
  5946. ['option' => ['value' => '01']],
  5947. date('F', strtotime('2008-01-01 00:00:00')),
  5948. '/option',
  5949. ['option' => ['value' => '02']],
  5950. date('F', strtotime('2008-02-01 00:00:00')),
  5951. '/option',
  5952. '*/select',
  5953. ];
  5954. $this->assertHtml($expected, $result);
  5955. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  5956. $expected = [
  5957. ['select' => ['name' => 'Model[field][month]']],
  5958. ['option' => ['selected' => 'selected', 'value' => '']],
  5959. '/option',
  5960. ['option' => ['value' => '01']],
  5961. date('F', strtotime('2008-01-01 00:00:00')),
  5962. '/option',
  5963. ['option' => ['value' => '02']],
  5964. date('F', strtotime('2008-02-01 00:00:00')),
  5965. '/option',
  5966. '*/select',
  5967. ];
  5968. $this->assertHtml($expected, $result);
  5969. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  5970. $expected = [
  5971. ['select' => ['name' => 'Model[field][month]']],
  5972. ['option' => ['selected' => 'selected', 'value' => '']],
  5973. '/option',
  5974. ['option' => ['value' => '01']],
  5975. '1',
  5976. '/option',
  5977. ['option' => ['value' => '02']],
  5978. '2',
  5979. '/option',
  5980. '*/select',
  5981. ];
  5982. $this->assertHtml($expected, $result);
  5983. $monthNames = [
  5984. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5985. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  5986. ];
  5987. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  5988. $expected = [
  5989. ['select' => ['name' => 'Model[field][month]']],
  5990. ['option' => ['value' => '']],
  5991. '/option',
  5992. ['option' => ['value' => '01', 'selected' => 'selected']],
  5993. 'Jan',
  5994. '/option',
  5995. ['option' => ['value' => '02']],
  5996. 'Feb',
  5997. '/option',
  5998. '*/select',
  5999. ];
  6000. $this->assertHtml($expected, $result);
  6001. $this->Form->request->data['Project']['release'] = '2050-02-10';
  6002. $result = $this->Form->month('Project.release');
  6003. $expected = [
  6004. ['select' => ['name' => 'Project[release][month]']],
  6005. ['option' => ['value' => '']],
  6006. '/option',
  6007. ['option' => ['value' => '01']],
  6008. 'January',
  6009. '/option',
  6010. ['option' => ['value' => '02', 'selected' => 'selected']],
  6011. 'February',
  6012. '/option',
  6013. '*/select',
  6014. ];
  6015. $this->assertHtml($expected, $result);
  6016. $result = $this->Form->month('Contact.published', [
  6017. 'empty' => 'Published on',
  6018. ]);
  6019. $this->assertContains('Published on', $result);
  6020. }
  6021. /**
  6022. * testDay method
  6023. *
  6024. * Test generation of a day input.
  6025. *
  6026. * @return void
  6027. */
  6028. public function testDay()
  6029. {
  6030. extract($this->dateRegex);
  6031. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6032. $expected = [
  6033. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6034. ['option' => ['selected' => 'selected', 'value' => '']],
  6035. '/option',
  6036. ['option' => ['value' => '01']],
  6037. '1',
  6038. '/option',
  6039. ['option' => ['value' => '02']],
  6040. '2',
  6041. '/option',
  6042. $daysRegex,
  6043. '/select',
  6044. ];
  6045. $this->assertHtml($expected, $result);
  6046. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6047. $result = $this->Form->day('Model.field');
  6048. $expected = [
  6049. ['select' => ['name' => 'Model[field][day]']],
  6050. ['option' => ['value' => '']],
  6051. '/option',
  6052. ['option' => ['value' => '01']],
  6053. '1',
  6054. '/option',
  6055. ['option' => ['value' => '02']],
  6056. '2',
  6057. '/option',
  6058. $daysRegex,
  6059. ['option' => ['value' => '10', 'selected' => 'selected']],
  6060. '10',
  6061. '/option',
  6062. $daysRegex,
  6063. '/select',
  6064. ];
  6065. $this->assertHtml($expected, $result);
  6066. $this->Form->request->data['Model']['field'] = '';
  6067. $result = $this->Form->day('Model.field', ['value' => '10']);
  6068. $expected = [
  6069. ['select' => ['name' => 'Model[field][day]']],
  6070. ['option' => ['value' => '']],
  6071. '/option',
  6072. ['option' => ['value' => '01']],
  6073. '1',
  6074. '/option',
  6075. ['option' => ['value' => '02']],
  6076. '2',
  6077. '/option',
  6078. $daysRegex,
  6079. ['option' => ['value' => '10', 'selected' => 'selected']],
  6080. '10',
  6081. '/option',
  6082. $daysRegex,
  6083. '/select',
  6084. ];
  6085. $this->assertHtml($expected, $result);
  6086. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6087. $result = $this->Form->day('Project.release');
  6088. $expected = [
  6089. ['select' => ['name' => 'Project[release][day]']],
  6090. ['option' => ['value' => '']],
  6091. '/option',
  6092. ['option' => ['value' => '01']],
  6093. '1',
  6094. '/option',
  6095. ['option' => ['value' => '02']],
  6096. '2',
  6097. '/option',
  6098. $daysRegex,
  6099. ['option' => ['value' => '10', 'selected' => 'selected']],
  6100. '10',
  6101. '/option',
  6102. $daysRegex,
  6103. '/select',
  6104. ];
  6105. $this->assertHtml($expected, $result);
  6106. $result = $this->Form->day('Contact.published', [
  6107. 'empty' => 'Published on',
  6108. ]);
  6109. $this->assertContains('Published on', $result);
  6110. }
  6111. /**
  6112. * testMinute method
  6113. *
  6114. * Test generation of a minute input.
  6115. *
  6116. * @return void
  6117. */
  6118. public function testMinute()
  6119. {
  6120. extract($this->dateRegex);
  6121. $result = $this->Form->minute('Model.field', ['value' => '']);
  6122. $expected = [
  6123. ['select' => ['name' => 'Model[field][minute]']],
  6124. ['option' => ['selected' => 'selected', 'value' => '']],
  6125. '/option',
  6126. ['option' => ['value' => '00']],
  6127. '00',
  6128. '/option',
  6129. ['option' => ['value' => '01']],
  6130. '01',
  6131. '/option',
  6132. ['option' => ['value' => '02']],
  6133. '02',
  6134. '/option',
  6135. $minutesRegex,
  6136. '/select',
  6137. ];
  6138. $this->assertHtml($expected, $result);
  6139. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6140. $result = $this->Form->minute('Model.field');
  6141. $expected = [
  6142. ['select' => ['name' => 'Model[field][minute]']],
  6143. ['option' => ['value' => '']],
  6144. '/option',
  6145. ['option' => ['value' => '00']],
  6146. '00',
  6147. '/option',
  6148. ['option' => ['value' => '01']],
  6149. '01',
  6150. '/option',
  6151. ['option' => ['value' => '02']],
  6152. '02',
  6153. '/option',
  6154. $minutesRegex,
  6155. ['option' => ['value' => '12', 'selected' => 'selected']],
  6156. '12',
  6157. '/option',
  6158. $minutesRegex,
  6159. '/select',
  6160. ];
  6161. $this->assertHtml($expected, $result);
  6162. $this->Form->request->data['Model']['field'] = '';
  6163. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6164. $expected = [
  6165. ['select' => ['name' => 'Model[field][minute]']],
  6166. ['option' => ['selected' => 'selected', 'value' => '']],
  6167. '/option',
  6168. ['option' => ['value' => '00']],
  6169. '00',
  6170. '/option',
  6171. ['option' => ['value' => '05']],
  6172. '05',
  6173. '/option',
  6174. ['option' => ['value' => '10']],
  6175. '10',
  6176. '/option',
  6177. $minutesRegex,
  6178. '/select',
  6179. ];
  6180. $this->assertHtml($expected, $result);
  6181. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6182. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6183. $expected = [
  6184. ['select' => ['name' => 'Model[field][minute]']],
  6185. ['option' => ['value' => '']],
  6186. '/option',
  6187. ['option' => ['value' => '00']],
  6188. '00',
  6189. '/option',
  6190. ['option' => ['value' => '05']],
  6191. '05',
  6192. '/option',
  6193. ['option' => ['value' => '10', 'selected' => 'selected']],
  6194. '10',
  6195. '/option',
  6196. $minutesRegex,
  6197. '/select',
  6198. ];
  6199. $this->assertHtml($expected, $result);
  6200. }
  6201. /**
  6202. * testMeridian method
  6203. *
  6204. * Test generating an input for the meridian.
  6205. *
  6206. * @return void
  6207. */
  6208. public function testMeridian()
  6209. {
  6210. extract($this->dateRegex);
  6211. $now = new \DateTime();
  6212. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6213. $expected = [
  6214. ['select' => ['name' => 'Model[field][meridian]']],
  6215. ['option' => ['value' => '']],
  6216. '/option',
  6217. $meridianRegex,
  6218. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6219. $now->format('a'),
  6220. '/option',
  6221. '*/select'
  6222. ];
  6223. $this->assertHtml($expected, $result);
  6224. }
  6225. /**
  6226. * testHour method
  6227. *
  6228. * Test generation of an hour input.
  6229. *
  6230. * @return void
  6231. */
  6232. public function testHour()
  6233. {
  6234. extract($this->dateRegex);
  6235. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6236. $expected = [
  6237. ['select' => ['name' => 'Model[field][hour]']],
  6238. ['option' => ['selected' => 'selected', 'value' => '']],
  6239. '/option',
  6240. ['option' => ['value' => '01']],
  6241. '1',
  6242. '/option',
  6243. ['option' => ['value' => '02']],
  6244. '2',
  6245. '/option',
  6246. $hoursRegex,
  6247. '/select',
  6248. ];
  6249. $this->assertHtml($expected, $result);
  6250. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6251. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6252. $expected = [
  6253. ['select' => ['name' => 'Model[field][hour]']],
  6254. ['option' => ['value' => '']],
  6255. '/option',
  6256. ['option' => ['value' => '01']],
  6257. '1',
  6258. '/option',
  6259. ['option' => ['value' => '02']],
  6260. '2',
  6261. '/option',
  6262. $hoursRegex,
  6263. ['option' => ['value' => '12', 'selected' => 'selected']],
  6264. '12',
  6265. '/option',
  6266. '/select',
  6267. ];
  6268. $this->assertHtml($expected, $result);
  6269. $this->Form->request->data['Model']['field'] = '';
  6270. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6271. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6272. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6273. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6274. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6275. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6276. $expected = [
  6277. ['select' => ['name' => 'Model[field][hour]']],
  6278. ['option' => ['value' => '']],
  6279. '/option',
  6280. ['option' => ['value' => '00', 'selected' => 'selected']],
  6281. '0',
  6282. '/option',
  6283. ['option' => ['value' => '01']],
  6284. '1',
  6285. '/option',
  6286. ['option' => ['value' => '02']],
  6287. '2',
  6288. '/option',
  6289. $hoursRegex,
  6290. '/select',
  6291. ];
  6292. $this->assertHtml($expected, $result);
  6293. unset($this->Form->request->data['Model']['field']);
  6294. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6295. $thisHour = date('H');
  6296. $optValue = date('G');
  6297. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6298. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6299. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6300. $expected = [
  6301. ['select' => ['name' => 'Model[field][hour]']],
  6302. ['option' => ['value' => '']],
  6303. '/option',
  6304. ['option' => ['value' => '00']],
  6305. '0',
  6306. '/option',
  6307. ['option' => ['value' => '01', 'selected' => 'selected']],
  6308. '1',
  6309. '/option',
  6310. ['option' => ['value' => '02']],
  6311. '2',
  6312. '/option',
  6313. $hoursRegex,
  6314. '/select',
  6315. ];
  6316. $this->assertHtml($expected, $result);
  6317. }
  6318. /**
  6319. * testYear method
  6320. *
  6321. * Test generation of a year input.
  6322. *
  6323. * @return void
  6324. */
  6325. public function testYear()
  6326. {
  6327. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6328. $expected = [
  6329. ['select' => ['name' => 'Model[field][year]']],
  6330. ['option' => ['selected' => 'selected', 'value' => '']],
  6331. '/option',
  6332. ['option' => ['value' => '2007']],
  6333. '2007',
  6334. '/option',
  6335. ['option' => ['value' => '2006']],
  6336. '2006',
  6337. '/option',
  6338. '/select',
  6339. ];
  6340. $this->assertHtml($expected, $result);
  6341. $result = $this->Form->year('Model.field', [
  6342. 'value' => '',
  6343. 'minYear' => 2006,
  6344. 'maxYear' => 2007,
  6345. 'orderYear' => 'asc'
  6346. ]);
  6347. $expected = [
  6348. ['select' => ['name' => 'Model[field][year]']],
  6349. ['option' => ['selected' => 'selected', 'value' => '']],
  6350. '/option',
  6351. ['option' => ['value' => '2006']],
  6352. '2006',
  6353. '/option',
  6354. ['option' => ['value' => '2007']],
  6355. '2007',
  6356. '/option',
  6357. '/select',
  6358. ];
  6359. $this->assertHtml($expected, $result);
  6360. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6361. $result = $this->Form->year('Contact.published', [
  6362. 'empty' => false,
  6363. 'minYear' => 2006,
  6364. 'maxYear' => 2007,
  6365. ]);
  6366. $expected = [
  6367. ['select' => ['name' => 'Contact[published][year]']],
  6368. ['option' => ['value' => '2007']],
  6369. '2007',
  6370. '/option',
  6371. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6372. '2006',
  6373. '/option',
  6374. '/select',
  6375. ];
  6376. $this->assertHtml($expected, $result);
  6377. $result = $this->Form->year('Contact.published', [
  6378. 'empty' => 'Published on',
  6379. ]);
  6380. $this->assertContains('Published on', $result);
  6381. }
  6382. /**
  6383. * testControlDatetimePreEpoch method
  6384. *
  6385. * Test minYear being prior to the unix epoch.
  6386. *
  6387. * @return void
  6388. */
  6389. public function testControlDatetimePreEpoch()
  6390. {
  6391. $start = date('Y') - 80;
  6392. $end = date('Y') - 18;
  6393. $result = $this->Form->control('birth_year', [
  6394. 'type' => 'date',
  6395. 'label' => 'Birth Year',
  6396. 'minYear' => $start,
  6397. 'maxYear' => $end,
  6398. 'month' => false,
  6399. 'day' => false,
  6400. ]);
  6401. $this->assertContains('value="' . $start . '">' . $start, $result);
  6402. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6403. $this->assertNotContains('value="00">00', $result);
  6404. }
  6405. /**
  6406. * testYearAutoExpandRange method
  6407. *
  6408. * @return void
  6409. */
  6410. public function testYearAutoExpandRange()
  6411. {
  6412. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6413. $result = $this->Form->year('User.birthday');
  6414. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6415. $result = $matches[1];
  6416. $expected = range(date('Y') + 5, 1930);
  6417. $this->assertEquals($expected, $result);
  6418. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6419. $result = $this->Form->year('Project.release');
  6420. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6421. $result = $matches[1];
  6422. $expected = range(2050, date('Y') - 5);
  6423. $this->assertEquals($expected, $result);
  6424. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6425. $result = $this->Form->year('Project.release', [
  6426. 'minYear' => 1890,
  6427. 'maxYear' => 1900
  6428. ]);
  6429. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6430. $result = $matches[1];
  6431. $expected = range(1900, 1881);
  6432. $this->assertEquals($expected, $result);
  6433. }
  6434. /**
  6435. * testControlDate method
  6436. *
  6437. * Test that control() accepts the type of date and passes options in.
  6438. *
  6439. * @return void
  6440. */
  6441. public function testControlDate()
  6442. {
  6443. $this->Form->request->data = [
  6444. 'month_year' => ['month' => date('m')],
  6445. ];
  6446. $this->Form->create($this->article);
  6447. $result = $this->Form->control('month_year', [
  6448. 'label' => false,
  6449. 'type' => 'date',
  6450. 'minYear' => 2006,
  6451. 'maxYear' => 2008
  6452. ]);
  6453. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6454. $this->assertNotContains('value="2008" selected="selected"', $result);
  6455. }
  6456. /**
  6457. * testControlLabelFalse method
  6458. *
  6459. * Test the label option being set to false.
  6460. *
  6461. * @return void
  6462. */
  6463. public function testControlLabelFalse()
  6464. {
  6465. $this->Form->create($this->article);
  6466. $result = $this->Form->control('title', ['label' => false]);
  6467. $expected = [
  6468. 'div' => ['class' => 'input text required'],
  6469. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6470. '/div'
  6471. ];
  6472. $this->assertHtml($expected, $result);
  6473. }
  6474. /**
  6475. * testControlDateMaxYear method
  6476. *
  6477. * Let's say we want to only allow users born from 2006 to 2008 to register
  6478. * This being the first signup page, we still don't have any data.
  6479. *
  6480. * @return void
  6481. */
  6482. public function testControlDateMaxYear()
  6483. {
  6484. $this->Form->request->data = [];
  6485. $this->Form->create($this->article);
  6486. $result = $this->Form->control('birthday', [
  6487. 'label' => false,
  6488. 'type' => 'date',
  6489. 'minYear' => 2006,
  6490. 'maxYear' => 2008,
  6491. 'default' => true
  6492. ]);
  6493. $this->assertContains('value="2008" selected="selected"', $result);
  6494. $this->assertContains('value="2006"', $result);
  6495. $this->assertNotContains('value="2005"', $result);
  6496. $this->assertNotContains('value="2009"', $result);
  6497. }
  6498. /**
  6499. * testTextArea method
  6500. *
  6501. * Test generation of a textarea input.
  6502. *
  6503. * @return void
  6504. */
  6505. public function testTextArea()
  6506. {
  6507. $this->Form->request->data = ['field' => 'some test data'];
  6508. $result = $this->Form->textarea('field');
  6509. $expected = [
  6510. 'textarea' => ['name' => 'field', 'rows' => 5],
  6511. 'some test data',
  6512. '/textarea',
  6513. ];
  6514. $this->assertHtml($expected, $result);
  6515. $result = $this->Form->textarea('user.bio');
  6516. $expected = [
  6517. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6518. '/textarea',
  6519. ];
  6520. $this->assertHtml($expected, $result);
  6521. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6522. $result = $this->Form->textarea('field');
  6523. $expected = [
  6524. 'textarea' => ['name' => 'field', 'rows' => 5],
  6525. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6526. '/textarea',
  6527. ];
  6528. $this->assertHtml($expected, $result);
  6529. $this->Form->request->data = [
  6530. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6531. ];
  6532. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6533. $expected = [
  6534. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6535. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6536. '/textarea',
  6537. ];
  6538. $this->assertHtml($expected, $result);
  6539. $result = $this->Form->textarea('0.OtherModel.field');
  6540. $expected = [
  6541. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6542. '/textarea'
  6543. ];
  6544. $this->assertHtml($expected, $result);
  6545. }
  6546. /**
  6547. * testTextAreaWithStupidCharacters method
  6548. *
  6549. * Test text area with non-ascii characters.
  6550. *
  6551. * @return void
  6552. */
  6553. public function testTextAreaWithStupidCharacters()
  6554. {
  6555. $result = $this->Form->textarea('Post.content', [
  6556. 'value' => "GREAT®",
  6557. 'rows' => '15',
  6558. 'cols' => '75'
  6559. ]);
  6560. $expected = [
  6561. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6562. 'GREAT®',
  6563. '/textarea',
  6564. ];
  6565. $this->assertHtml($expected, $result);
  6566. }
  6567. /**
  6568. * testTextAreaMaxLength method
  6569. *
  6570. * Test textareas maxlength read from schema.
  6571. *
  6572. * @return void
  6573. */
  6574. public function testTextAreaMaxLength()
  6575. {
  6576. $this->Form->create([
  6577. 'schema' => [
  6578. 'stuff' => ['type' => 'string', 'length' => 10],
  6579. ]
  6580. ]);
  6581. $result = $this->Form->control('other', ['type' => 'textarea']);
  6582. $expected = [
  6583. 'div' => ['class' => 'input textarea'],
  6584. 'label' => ['for' => 'other'],
  6585. 'Other',
  6586. '/label',
  6587. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6588. '/textarea',
  6589. '/div'
  6590. ];
  6591. $this->assertHtml($expected, $result);
  6592. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6593. $expected = [
  6594. 'div' => ['class' => 'input textarea'],
  6595. 'label' => ['for' => 'stuff'],
  6596. 'Stuff',
  6597. '/label',
  6598. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6599. '/textarea',
  6600. '/div'
  6601. ];
  6602. $this->assertHtml($expected, $result);
  6603. }
  6604. /**
  6605. * testHiddenField method
  6606. *
  6607. * Test generation of a hidden input.
  6608. *
  6609. * @return void
  6610. */
  6611. public function testHiddenField()
  6612. {
  6613. $this->article['errors'] = [
  6614. 'field' => true
  6615. ];
  6616. $this->Form->request->data['field'] = 'test';
  6617. $this->Form->create($this->article);
  6618. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6619. $expected = [
  6620. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6621. $this->assertHtml($expected, $result);
  6622. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6623. $expected = [
  6624. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6625. ];
  6626. $this->assertHtml($expected, $result);
  6627. }
  6628. /**
  6629. * testFileUploadField method
  6630. *
  6631. * Test generation of a file upload input.
  6632. *
  6633. * @return void
  6634. */
  6635. public function testFileUploadField()
  6636. {
  6637. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6638. $result = $this->Form->file('Model.upload');
  6639. $this->assertHtml($expected, $result);
  6640. $this->Form->request->data['Model']['upload'] = [
  6641. 'name' => '', 'type' => '', 'tmp_name' => '',
  6642. 'error' => 4, 'size' => 0
  6643. ];
  6644. $result = $this->Form->file('Model.upload');
  6645. $this->assertHtml($expected, $result);
  6646. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6647. $result = $this->Form->file('Model.upload');
  6648. $this->assertHtml($expected, $result);
  6649. }
  6650. /**
  6651. * testFileUploadOnOtherModel method
  6652. *
  6653. * Test File upload input on a model not used in create().
  6654. *
  6655. * @return void
  6656. */
  6657. public function testFileUploadOnOtherModel()
  6658. {
  6659. $this->Form->create($this->article, ['type' => 'file']);
  6660. $result = $this->Form->file('ValidateProfile.city');
  6661. $expected = [
  6662. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6663. ];
  6664. $this->assertHtml($expected, $result);
  6665. }
  6666. /**
  6667. * testButton method
  6668. *
  6669. * Test generation of a form button.
  6670. *
  6671. * @return void
  6672. */
  6673. public function testButton()
  6674. {
  6675. $result = $this->Form->button('Hi');
  6676. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6677. $this->assertHtml($expected, $result);
  6678. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6679. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6680. $this->assertHtml($expected, $result);
  6681. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6682. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6683. $this->assertHtml($expected, $result);
  6684. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6685. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6686. $this->assertHtml($expected, $result);
  6687. $result = $this->Form->button('No type', ['type' => false]);
  6688. $expected = ['button' => [], 'No type', '/button'];
  6689. $this->assertHtml($expected, $result);
  6690. $result = $this->Form->button('Upload Text', [
  6691. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6692. 'escape' => false
  6693. ]);
  6694. $this->assertNotRegExp('/\&039/', $result);
  6695. }
  6696. /**
  6697. * testButtonUnlockedByDefault method
  6698. *
  6699. * Test that button() makes unlocked fields by default.
  6700. *
  6701. * @return void
  6702. */
  6703. public function testButtonUnlockedByDefault()
  6704. {
  6705. $this->Form->request->params['_csrfToken'] = 'secured';
  6706. $this->Form->button('Save', ['name' => 'save']);
  6707. $this->Form->button('Clear');
  6708. $result = $this->Form->unlockField();
  6709. $this->assertEquals(['save'], $result);
  6710. }
  6711. /**
  6712. * testPostButton method
  6713. *
  6714. * @return void
  6715. */
  6716. public function testPostButton()
  6717. {
  6718. $result = $this->Form->postButton('Hi', '/controller/action');
  6719. $expected = [
  6720. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6721. 'div' => ['style' => 'display:none;'],
  6722. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6723. '/div',
  6724. 'button' => ['type' => 'submit'],
  6725. 'Hi',
  6726. '/button',
  6727. '/form'
  6728. ];
  6729. $this->assertHtml($expected, $result);
  6730. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6731. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6732. }
  6733. /**
  6734. * testPostButtonMethodType method
  6735. *
  6736. * @return void
  6737. */
  6738. public function testPostButtonMethodType()
  6739. {
  6740. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6741. $expected = [
  6742. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6743. 'div' => ['style' => 'display:none;'],
  6744. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6745. '/div',
  6746. 'button' => ['type' => 'submit'],
  6747. 'Hi',
  6748. '/button',
  6749. '/form'
  6750. ];
  6751. $this->assertHtml($expected, $result);
  6752. }
  6753. /**
  6754. * testPostButtonFormOptions method
  6755. *
  6756. * @return void
  6757. */
  6758. public function testPostButtonFormOptions()
  6759. {
  6760. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6761. $expected = [
  6762. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6763. 'div' => ['style' => 'display:none;'],
  6764. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6765. '/div',
  6766. 'button' => ['type' => 'submit'],
  6767. 'Hi',
  6768. '/button',
  6769. '/form'
  6770. ];
  6771. $this->assertHtml($expected, $result);
  6772. }
  6773. /**
  6774. * testPostButtonNestedData method
  6775. *
  6776. * Test using postButton with N dimensional data.
  6777. *
  6778. * @return void
  6779. */
  6780. public function testPostButtonNestedData()
  6781. {
  6782. $data = [
  6783. 'one' => [
  6784. 'two' => [
  6785. 3, 4, 5
  6786. ]
  6787. ]
  6788. ];
  6789. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6790. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6791. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6792. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6793. }
  6794. /**
  6795. * testSecurePostButton method
  6796. *
  6797. * Test that postButton adds _Token fields.
  6798. *
  6799. * @return void
  6800. */
  6801. public function testSecurePostButton()
  6802. {
  6803. $this->Form->request->params['_csrfToken'] = 'testkey';
  6804. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6805. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6806. $tokenDebug = urlencode(json_encode([
  6807. '/posts/delete/1',
  6808. [],
  6809. []
  6810. ]));
  6811. $expected = [
  6812. 'form' => [
  6813. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6814. ],
  6815. ['div' => ['style' => 'display:none;']],
  6816. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6817. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6818. '/div',
  6819. 'button' => ['type' => 'submit'],
  6820. 'Delete',
  6821. '/button',
  6822. ['div' => ['style' => 'display:none;']],
  6823. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6824. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6825. ['input' => [
  6826. 'type' => 'hidden',
  6827. 'name' => '_Token[debug]',
  6828. 'value' => $tokenDebug,
  6829. 'autocomplete' => 'off',
  6830. ]],
  6831. '/div',
  6832. '/form',
  6833. ];
  6834. $this->assertHtml($expected, $result);
  6835. }
  6836. /**
  6837. * testPostLink method
  6838. *
  6839. * @return void
  6840. */
  6841. public function testPostLink()
  6842. {
  6843. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6844. $expected = [
  6845. 'form' => [
  6846. 'method' => 'post', 'action' => '/posts/delete/1',
  6847. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6848. ],
  6849. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6850. '/form',
  6851. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6852. 'Delete',
  6853. '/a'
  6854. ];
  6855. $this->assertHtml($expected, $result);
  6856. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6857. $expected = [
  6858. 'form' => [
  6859. 'method' => 'post', 'action' => '/posts/delete/1',
  6860. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6861. ],
  6862. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6863. '/form',
  6864. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6865. 'Delete',
  6866. '/a'
  6867. ];
  6868. $this->assertHtml($expected, $result);
  6869. $result = $this->Form->postLink(
  6870. 'Delete',
  6871. '/posts/delete/1',
  6872. ['target' => '_blank', 'class' => 'btn btn-danger']
  6873. );
  6874. $expected = [
  6875. 'form' => [
  6876. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6877. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6878. ],
  6879. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6880. '/form',
  6881. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6882. 'Delete',
  6883. '/a'
  6884. ];
  6885. $this->assertHtml($expected, $result);
  6886. }
  6887. /**
  6888. * testPostLinkWithConfirm method
  6889. *
  6890. * Test the confirm option for postLink().
  6891. *
  6892. * @return void
  6893. */
  6894. public function testPostLinkWithConfirm()
  6895. {
  6896. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6897. $expected = [
  6898. 'form' => [
  6899. 'method' => 'post', 'action' => '/posts/delete/1',
  6900. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6901. ],
  6902. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6903. '/form',
  6904. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6905. 'Delete',
  6906. '/a'
  6907. ];
  6908. $this->assertHtml($expected, $result);
  6909. $result = $this->Form->postLink(
  6910. 'Delete',
  6911. '/posts/delete/1',
  6912. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6913. );
  6914. $expected = [
  6915. 'form' => [
  6916. 'method' => 'post', 'action' => '/posts/delete/1',
  6917. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6918. ],
  6919. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6920. '/form',
  6921. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6922. 'Delete',
  6923. '/a'
  6924. ];
  6925. $this->assertHtml($expected, $result);
  6926. }
  6927. /**
  6928. * testPostLinkWithQuery method
  6929. *
  6930. * Test postLink() with query string args.
  6931. *
  6932. * @return void
  6933. */
  6934. public function testPostLinkWithQuery()
  6935. {
  6936. $result = $this->Form->postLink(
  6937. 'Delete',
  6938. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6939. );
  6940. $expected = [
  6941. 'form' => [
  6942. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6943. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6944. ],
  6945. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6946. '/form',
  6947. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6948. 'Delete',
  6949. '/a'
  6950. ];
  6951. $this->assertHtml($expected, $result);
  6952. }
  6953. /**
  6954. * testPostLinkWithData method
  6955. *
  6956. * Test postLink with additional data.
  6957. *
  6958. * @return void
  6959. */
  6960. public function testPostLinkWithData()
  6961. {
  6962. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6963. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  6964. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6965. $this->Form->create($entity);
  6966. $this->Form->end();
  6967. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6968. $this->assertContains(
  6969. '<input type="hidden" name="name" value="show"',
  6970. $result,
  6971. 'should not contain entity data.'
  6972. );
  6973. }
  6974. /**
  6975. * testPostLinkSecurityHash method
  6976. *
  6977. * Test that security hashes for postLink include the url.
  6978. *
  6979. * @return void
  6980. */
  6981. public function testPostLinkSecurityHash()
  6982. {
  6983. $hash = Security::hash(
  6984. '/posts/delete/1' .
  6985. serialize(['id' => '1']) .
  6986. '' .
  6987. Security::salt()
  6988. );
  6989. $hash .= '%3Aid';
  6990. $this->Form->request->params['_Token']['key'] = 'test';
  6991. $result = $this->Form->postLink(
  6992. 'Delete',
  6993. '/posts/delete/1',
  6994. ['data' => ['id' => 1]]
  6995. );
  6996. $tokenDebug = urlencode(json_encode([
  6997. '/posts/delete/1',
  6998. [
  6999. 'id' => 1
  7000. ],
  7001. []
  7002. ]));
  7003. $expected = [
  7004. 'form' => [
  7005. 'method' => 'post', 'action' => '/posts/delete/1',
  7006. 'name', 'style' => 'display:none;'
  7007. ],
  7008. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7009. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7010. 'div' => ['style' => 'display:none;'],
  7011. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7012. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7013. ['input' => [
  7014. 'type' => 'hidden',
  7015. 'name' => '_Token[debug]',
  7016. 'value' => $tokenDebug,
  7017. 'autocomplete' => 'off',
  7018. ]],
  7019. '/div',
  7020. '/form',
  7021. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7022. 'Delete',
  7023. '/a'
  7024. ];
  7025. $this->assertHtml($expected, $result);
  7026. }
  7027. /**
  7028. * testPostLinkSecurityHashBlockMode method
  7029. *
  7030. * Test that postLink doesn't modify the fields in the containing form.
  7031. *
  7032. * postLink() calls inside open forms should not modify the field list
  7033. * for the form.
  7034. *
  7035. * @return void
  7036. */
  7037. public function testPostLinkSecurityHashBlockMode()
  7038. {
  7039. $hash = Security::hash(
  7040. '/posts/delete/1' .
  7041. serialize([]) .
  7042. '' .
  7043. Security::salt()
  7044. );
  7045. $hash .= '%3A';
  7046. $this->Form->request->params['_Token']['key'] = 'test';
  7047. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7048. $this->Form->control('title');
  7049. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7050. $result = $this->View->fetch('postLink');
  7051. $this->assertEquals(['title'], $this->Form->fields);
  7052. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7053. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7054. }
  7055. /**
  7056. * testPostLinkSecurityHashNoDebugMode method
  7057. *
  7058. * Test that security does not include debug token if debug is false.
  7059. *
  7060. * @return void
  7061. */
  7062. public function testPostLinkSecurityHashNoDebugMode()
  7063. {
  7064. Configure::write('debug', false);
  7065. $hash = Security::hash(
  7066. '/posts/delete/1' .
  7067. serialize(['id' => '1']) .
  7068. '' .
  7069. Security::salt()
  7070. );
  7071. $hash .= '%3Aid';
  7072. $this->Form->request->params['_Token']['key'] = 'test';
  7073. $result = $this->Form->postLink(
  7074. 'Delete',
  7075. '/posts/delete/1',
  7076. ['data' => ['id' => 1]]
  7077. );
  7078. $expected = [
  7079. 'form' => [
  7080. 'method' => 'post', 'action' => '/posts/delete/1',
  7081. 'name', 'style' => 'display:none;'
  7082. ],
  7083. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7084. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7085. 'div' => ['style' => 'display:none;'],
  7086. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7087. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7088. '/div',
  7089. '/form',
  7090. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7091. 'Delete',
  7092. '/a'
  7093. ];
  7094. $this->assertHtml($expected, $result);
  7095. }
  7096. /**
  7097. * testPostLinkNestedData method
  7098. *
  7099. * Test using postLink with N dimensional data.
  7100. *
  7101. * @return void
  7102. */
  7103. public function testPostLinkNestedData()
  7104. {
  7105. $data = [
  7106. 'one' => [
  7107. 'two' => [
  7108. 3, 4, 5
  7109. ]
  7110. ]
  7111. ];
  7112. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7113. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7114. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7115. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7116. }
  7117. /**
  7118. * testPostLinkAfterGetForm method
  7119. *
  7120. * Test creating postLinks after a GET form.
  7121. *
  7122. * @return void
  7123. */
  7124. public function testPostLinkAfterGetForm()
  7125. {
  7126. $this->Form->request->params['_csrfToken'] = 'testkey';
  7127. $this->Form->request->params['_Token'] = 'val';
  7128. $this->Form->create($this->article, ['type' => 'get']);
  7129. $this->Form->end();
  7130. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7131. $tokenDebug = urlencode(json_encode([
  7132. '/posts/delete/1',
  7133. [],
  7134. []
  7135. ]));
  7136. $expected = [
  7137. 'form' => [
  7138. 'method' => 'post', 'action' => '/posts/delete/1',
  7139. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7140. ],
  7141. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7142. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7143. 'div' => ['style' => 'display:none;'],
  7144. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7145. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7146. ['input' => [
  7147. 'type' => 'hidden', 'name' => '_Token[debug]',
  7148. 'value' => $tokenDebug,
  7149. 'autocomplete' => 'off'
  7150. ]],
  7151. '/div',
  7152. '/form',
  7153. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7154. 'Delete',
  7155. '/a'
  7156. ];
  7157. $this->assertHtml($expected, $result);
  7158. }
  7159. /**
  7160. * testPostLinkFormBuffer method
  7161. *
  7162. * Test that postLink adds form tags to view block.
  7163. *
  7164. * @return void
  7165. */
  7166. public function testPostLinkFormBuffer()
  7167. {
  7168. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7169. $expected = [
  7170. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7171. 'Delete',
  7172. '/a'
  7173. ];
  7174. $this->assertHtml($expected, $result);
  7175. $result = $this->View->fetch('postLink');
  7176. $expected = [
  7177. 'form' => [
  7178. 'method' => 'post', 'action' => '/posts/delete/1',
  7179. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7180. ],
  7181. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7182. '/form'
  7183. ];
  7184. $this->assertHtml($expected, $result);
  7185. $result = $this->Form->postLink(
  7186. 'Delete',
  7187. '/posts/delete/2',
  7188. ['block' => true, 'method' => 'DELETE']
  7189. );
  7190. $expected = [
  7191. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7192. 'Delete',
  7193. '/a'
  7194. ];
  7195. $this->assertHtml($expected, $result);
  7196. $result = $this->View->fetch('postLink');
  7197. $expected = [
  7198. 'form' => [
  7199. 'method' => 'post', 'action' => '/posts/delete/1',
  7200. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7201. ],
  7202. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7203. '/form',
  7204. [
  7205. 'form' => [
  7206. 'method' => 'post', 'action' => '/posts/delete/2',
  7207. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7208. ],
  7209. ],
  7210. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7211. '/form'
  7212. ];
  7213. $this->assertHtml($expected, $result);
  7214. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7215. $expected = [
  7216. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7217. 'Delete',
  7218. '/a'
  7219. ];
  7220. $this->assertHtml($expected, $result);
  7221. $result = $this->View->fetch('foobar');
  7222. $expected = [
  7223. 'form' => [
  7224. 'method' => 'post', 'action' => '/posts/delete/1',
  7225. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7226. ],
  7227. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7228. '/form'
  7229. ];
  7230. $this->assertHtml($expected, $result);
  7231. }
  7232. /**
  7233. * testSubmitButton method
  7234. *
  7235. * @return void
  7236. */
  7237. public function testSubmitButton()
  7238. {
  7239. $result = $this->Form->submit('');
  7240. $expected = [
  7241. 'div' => ['class' => 'submit'],
  7242. 'input' => ['type' => 'submit', 'value' => ''],
  7243. '/div'
  7244. ];
  7245. $this->assertHtml($expected, $result);
  7246. $result = $this->Form->submit('Test Submit');
  7247. $expected = [
  7248. 'div' => ['class' => 'submit'],
  7249. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7250. '/div'
  7251. ];
  7252. $this->assertHtml($expected, $result);
  7253. $result = $this->Form->submit('Next >');
  7254. $expected = [
  7255. 'div' => ['class' => 'submit'],
  7256. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7257. '/div'
  7258. ];
  7259. $this->assertHtml($expected, $result);
  7260. $result = $this->Form->submit('Next >', ['escape' => false]);
  7261. $expected = [
  7262. 'div' => ['class' => 'submit'],
  7263. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7264. '/div'
  7265. ];
  7266. $this->assertHtml($expected, $result);
  7267. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7268. $expected = [
  7269. 'div' => ['class' => 'submit'],
  7270. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7271. '/div'
  7272. ];
  7273. $this->assertHtml($expected, $result);
  7274. }
  7275. /**
  7276. * testSubmitImage method
  7277. *
  7278. * Test image submit types.
  7279. *
  7280. * @return void
  7281. */
  7282. public function testSubmitImage()
  7283. {
  7284. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7285. $expected = [
  7286. 'div' => ['class' => 'submit'],
  7287. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7288. '/div'
  7289. ];
  7290. $this->assertHtml($expected, $result);
  7291. $result = $this->Form->submit('/relative/cake.power.gif');
  7292. $expected = [
  7293. 'div' => ['class' => 'submit'],
  7294. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7295. '/div'
  7296. ];
  7297. $this->assertHtml($expected, $result);
  7298. $result = $this->Form->submit('cake.power.gif');
  7299. $expected = [
  7300. 'div' => ['class' => 'submit'],
  7301. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7302. '/div'
  7303. ];
  7304. $this->assertHtml($expected, $result);
  7305. $result = $this->Form->submit('Not.an.image');
  7306. $expected = [
  7307. 'div' => ['class' => 'submit'],
  7308. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7309. '/div'
  7310. ];
  7311. $this->assertHtml($expected, $result);
  7312. }
  7313. /**
  7314. * testSubmitUnlockedByDefault method
  7315. *
  7316. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7317. * be submitted at a time.
  7318. *
  7319. * @return void
  7320. */
  7321. public function testSubmitUnlockedByDefault()
  7322. {
  7323. $this->Form->request->params['_Token'] = 'secured';
  7324. $this->Form->submit('Go go');
  7325. $this->Form->submit('Save', ['name' => 'save']);
  7326. $result = $this->Form->unlockField();
  7327. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7328. }
  7329. /**
  7330. * testSubmitImageTimestamp method
  7331. *
  7332. * Test submit image with timestamps.
  7333. *
  7334. * @return void
  7335. */
  7336. public function testSubmitImageTimestamp()
  7337. {
  7338. Configure::write('Asset.timestamp', 'force');
  7339. $result = $this->Form->submit('cake.power.gif');
  7340. $expected = [
  7341. 'div' => ['class' => 'submit'],
  7342. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7343. '/div'
  7344. ];
  7345. $this->assertHtml($expected, $result);
  7346. }
  7347. /**
  7348. * testDateTimeWithGetForms method
  7349. *
  7350. * Test that datetime() works with GET style forms.
  7351. *
  7352. * @return void
  7353. */
  7354. public function testDateTimeWithGetForms()
  7355. {
  7356. extract($this->dateRegex);
  7357. $this->Form->create($this->article, ['type' => 'get']);
  7358. $result = $this->Form->datetime('created');
  7359. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7360. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7361. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7362. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7363. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7364. }
  7365. /**
  7366. * testForMagicControlNonExistingNorValidated method
  7367. *
  7368. * @return void
  7369. */
  7370. public function testForMagicControlNonExistingNorValidated()
  7371. {
  7372. $this->Form->create($this->article);
  7373. $this->Form->templates(['inputContainer' => '{{content}}']);
  7374. $result = $this->Form->control('non_existing_nor_validated');
  7375. $expected = [
  7376. 'label' => ['for' => 'non-existing-nor-validated'],
  7377. 'Non Existing Nor Validated',
  7378. '/label',
  7379. 'input' => [
  7380. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7381. 'id' => 'non-existing-nor-validated'
  7382. ]
  7383. ];
  7384. $this->assertHtml($expected, $result);
  7385. $result = $this->Form->control('non_existing_nor_validated', [
  7386. 'val' => 'my value'
  7387. ]);
  7388. $expected = [
  7389. 'label' => ['for' => 'non-existing-nor-validated'],
  7390. 'Non Existing Nor Validated',
  7391. '/label',
  7392. 'input' => [
  7393. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7394. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7395. ]
  7396. ];
  7397. $this->assertHtml($expected, $result);
  7398. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7399. $result = $this->Form->control('non_existing_nor_validated');
  7400. $expected = [
  7401. 'label' => ['for' => 'non-existing-nor-validated'],
  7402. 'Non Existing Nor Validated',
  7403. '/label',
  7404. 'input' => [
  7405. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7406. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7407. ]
  7408. ];
  7409. $this->assertHtml($expected, $result);
  7410. }
  7411. /**
  7412. * testFormMagicControlLabel method
  7413. *
  7414. * @return void
  7415. */
  7416. public function testFormMagicControlLabel()
  7417. {
  7418. TableRegistry::get('Contacts', [
  7419. 'className' => __NAMESPACE__ . '\ContactsTable'
  7420. ]);
  7421. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7422. $this->Form->templates(['inputContainer' => '{{content}}']);
  7423. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7424. $expected = [
  7425. 'label' => ['for' => 'contacts-name'],
  7426. 'My label',
  7427. '/label',
  7428. 'input' => [
  7429. 'type' => 'text', 'name' => 'Contacts[name]',
  7430. 'id' => 'contacts-name', 'maxlength' => '255'
  7431. ]
  7432. ];
  7433. $this->assertHtml($expected, $result);
  7434. $result = $this->Form->control('name', [
  7435. 'label' => ['class' => 'mandatory']
  7436. ]);
  7437. $expected = [
  7438. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7439. 'Name',
  7440. '/label',
  7441. 'input' => [
  7442. 'type' => 'text', 'name' => 'name',
  7443. 'id' => 'name', 'maxlength' => '255'
  7444. ]
  7445. ];
  7446. $this->assertHtml($expected, $result);
  7447. $result = $this->Form->control('name', [
  7448. 'div' => false,
  7449. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7450. ]);
  7451. $expected = [
  7452. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7453. 'My label',
  7454. '/label',
  7455. 'input' => [
  7456. 'type' => 'text', 'name' => 'name',
  7457. 'id' => 'name', 'maxlength' => '255'
  7458. ]
  7459. ];
  7460. $this->assertHtml($expected, $result);
  7461. $result = $this->Form->control('Contact.name', [
  7462. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7463. ]);
  7464. $expected = [
  7465. 'label' => ['for' => 'my_id'],
  7466. 'Name',
  7467. '/label',
  7468. 'input' => [
  7469. 'type' => 'text', 'name' => 'Contact[name]',
  7470. 'id' => 'my_id', 'maxlength' => '255'
  7471. ]
  7472. ];
  7473. $this->assertHtml($expected, $result);
  7474. $result = $this->Form->control('1.id');
  7475. $expected = ['input' => [
  7476. 'type' => 'hidden', 'name' => '1[id]',
  7477. 'id' => '1-id'
  7478. ]];
  7479. $this->assertHtml($expected, $result);
  7480. $result = $this->Form->control("1.name");
  7481. $expected = [
  7482. 'label' => ['for' => '1-name'],
  7483. 'Name',
  7484. '/label',
  7485. 'input' => [
  7486. 'type' => 'text', 'name' => '1[name]',
  7487. 'id' => '1-name', 'maxlength' => '255'
  7488. ]
  7489. ];
  7490. $this->assertHtml($expected, $result);
  7491. }
  7492. /**
  7493. * testFormEnd method
  7494. *
  7495. * @return void
  7496. */
  7497. public function testFormEnd()
  7498. {
  7499. $this->assertEquals('</form>', $this->Form->end());
  7500. }
  7501. /**
  7502. * testMultiRecordForm method
  7503. *
  7504. * Test the generation of fields for a multi record form.
  7505. *
  7506. * @return void
  7507. */
  7508. public function testMultiRecordForm()
  7509. {
  7510. $this->loadFixtures('Articles', 'Comments');
  7511. $articles = TableRegistry::get('Articles');
  7512. $articles->hasMany('Comments');
  7513. $comment = new Entity(['comment' => 'Value']);
  7514. $article = new Article(['comments' => [$comment]]);
  7515. $this->Form->create([$article]);
  7516. $result = $this->Form->control('0.comments.1.comment');
  7517. //@codingStandardsIgnoreStart
  7518. $expected = [
  7519. 'div' => ['class' => 'input textarea'],
  7520. 'label' => ['for' => '0-comments-1-comment'],
  7521. 'Comment',
  7522. '/label',
  7523. 'textarea' => [
  7524. 'name',
  7525. 'id' => '0-comments-1-comment',
  7526. 'rows' => 5
  7527. ],
  7528. '/textarea',
  7529. '/div'
  7530. ];
  7531. //@codingStandardsIgnoreEnd
  7532. $this->assertHtml($expected, $result);
  7533. $result = $this->Form->control('0.comments.0.comment');
  7534. //@codingStandardsIgnoreStart
  7535. $expected = [
  7536. 'div' => ['class' => 'input textarea'],
  7537. 'label' => ['for' => '0-comments-0-comment'],
  7538. 'Comment',
  7539. '/label',
  7540. 'textarea' => [
  7541. 'name',
  7542. 'id' => '0-comments-0-comment',
  7543. 'rows' => 5
  7544. ],
  7545. 'Value',
  7546. '/textarea',
  7547. '/div'
  7548. ];
  7549. //@codingStandardsIgnoreEnd
  7550. $this->assertHtml($expected, $result);
  7551. $comment->errors('comment', ['Not valid']);
  7552. $result = $this->Form->control('0.comments.0.comment');
  7553. //@codingStandardsIgnoreStart
  7554. $expected = [
  7555. 'div' => ['class' => 'input textarea error'],
  7556. 'label' => ['for' => '0-comments-0-comment'],
  7557. 'Comment',
  7558. '/label',
  7559. 'textarea' => [
  7560. 'name',
  7561. 'class' => 'form-error',
  7562. 'id' => '0-comments-0-comment',
  7563. 'rows' => 5
  7564. ],
  7565. 'Value',
  7566. '/textarea',
  7567. ['div' => ['class' => 'error-message']],
  7568. 'Not valid',
  7569. '/div',
  7570. '/div'
  7571. ];
  7572. //@codingStandardsIgnoreEnd
  7573. $this->assertHtml($expected, $result);
  7574. TableRegistry::get('Comments')
  7575. ->validator('default')
  7576. ->allowEmpty('comment', false);
  7577. $result = $this->Form->control('0.comments.1.comment');
  7578. //@codingStandardsIgnoreStart
  7579. $expected = [
  7580. 'div' => ['class' => 'input textarea required'],
  7581. 'label' => ['for' => '0-comments-1-comment'],
  7582. 'Comment',
  7583. '/label',
  7584. 'textarea' => [
  7585. 'name',
  7586. 'required' => 'required',
  7587. 'id' => '0-comments-1-comment',
  7588. 'rows' => 5
  7589. ],
  7590. '/textarea',
  7591. '/div'
  7592. ];
  7593. //@codingStandardsIgnoreEnd
  7594. $this->assertHtml($expected, $result);
  7595. }
  7596. /**
  7597. * testHtml5Controls method
  7598. *
  7599. * Test that some html5 inputs + FormHelper::__call() work.
  7600. *
  7601. * @return void
  7602. */
  7603. public function testHtml5Controls()
  7604. {
  7605. $result = $this->Form->email('User.email');
  7606. $expected = [
  7607. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7608. ];
  7609. $this->assertHtml($expected, $result);
  7610. $result = $this->Form->search('User.query');
  7611. $expected = [
  7612. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7613. ];
  7614. $this->assertHtml($expected, $result);
  7615. $result = $this->Form->search('User.query', ['value' => 'test']);
  7616. $expected = [
  7617. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7618. ];
  7619. $this->assertHtml($expected, $result);
  7620. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7621. $expected = [
  7622. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7623. ];
  7624. $this->assertHtml($expected, $result);
  7625. }
  7626. /**
  7627. * testHtml5ControlWithControl method
  7628. *
  7629. * Test accessing html5 inputs through control().
  7630. *
  7631. * @return void
  7632. */
  7633. public function testHtml5ControlWithControl()
  7634. {
  7635. $this->Form->create();
  7636. $this->Form->templates(['inputContainer' => '{{content}}']);
  7637. $result = $this->Form->control('website', [
  7638. 'type' => 'url',
  7639. 'val' => 'http://domain.tld',
  7640. 'label' => false
  7641. ]);
  7642. $expected = [
  7643. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7644. ];
  7645. $this->assertHtml($expected, $result);
  7646. }
  7647. /**
  7648. * testHtml5ControlException method
  7649. *
  7650. * Test errors when field name is missing.
  7651. *
  7652. * @expectedException \Cake\Core\Exception\Exception
  7653. * @return void
  7654. */
  7655. public function testHtml5ControlException()
  7656. {
  7657. $this->Form->email();
  7658. }
  7659. /**
  7660. * testRequiredAttribute method
  7661. *
  7662. * Tests that formhelper sets required attributes.
  7663. *
  7664. * @return void
  7665. */
  7666. public function testRequiredAttribute()
  7667. {
  7668. $this->article['required'] = [
  7669. 'title' => true,
  7670. 'body' => false,
  7671. ];
  7672. $this->Form->create($this->article);
  7673. $result = $this->Form->control('title');
  7674. $expected = [
  7675. 'div' => ['class' => 'input text required'],
  7676. 'label' => ['for' => 'title'],
  7677. 'Title',
  7678. '/label',
  7679. 'input' => [
  7680. 'type' => 'text',
  7681. 'name' => 'title',
  7682. 'id' => 'title',
  7683. 'required' => 'required',
  7684. ],
  7685. '/div'
  7686. ];
  7687. $this->assertHtml($expected, $result);
  7688. $result = $this->Form->control('title', ['required' => false]);
  7689. $this->assertNotContains('required', $result);
  7690. $result = $this->Form->control('body');
  7691. $expected = [
  7692. 'div' => ['class' => 'input text'],
  7693. 'label' => ['for' => 'body'],
  7694. 'Body',
  7695. '/label',
  7696. 'input' => [
  7697. 'type' => 'text',
  7698. 'name' => 'body',
  7699. 'id' => 'body',
  7700. ],
  7701. '/div'
  7702. ];
  7703. $this->assertHtml($expected, $result);
  7704. $result = $this->Form->control('body', ['required' => true]);
  7705. $this->assertContains('required', $result);
  7706. }
  7707. /**
  7708. * testControlsNotNested method
  7709. *
  7710. * Tests that it is possible to put inputs outside of the label.
  7711. *
  7712. * @return void
  7713. */
  7714. public function testControlsNotNested()
  7715. {
  7716. $this->Form->templates([
  7717. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7718. 'formGroup' => '{{input}}{{label}}',
  7719. ]);
  7720. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7721. $expected = [
  7722. 'div' => ['class' => 'input checkbox'],
  7723. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7724. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7725. 'label' => ['for' => 'foo'],
  7726. 'Foo',
  7727. '/label',
  7728. '/div'
  7729. ];
  7730. $this->assertHtml($expected, $result);
  7731. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7732. $expected = [
  7733. 'div' => ['class' => 'input checkbox'],
  7734. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7735. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7736. '/div'
  7737. ];
  7738. $this->assertHtml($expected, $result);
  7739. $result = $this->Form->control('confirm', [
  7740. 'type' => 'radio',
  7741. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7742. ]);
  7743. $expected = [
  7744. 'div' => ['class' => 'input radio'],
  7745. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7746. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7747. ['label' => ['for' => 'confirm-y']],
  7748. 'Yes',
  7749. '/label',
  7750. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7751. ['label' => ['for' => 'confirm-n']],
  7752. 'No',
  7753. '/label',
  7754. '<label',
  7755. 'Confirm',
  7756. '/label',
  7757. '/div',
  7758. ];
  7759. $this->assertHtml($expected, $result);
  7760. $result = $this->Form->select('category', ['1', '2'], [
  7761. 'multiple' => 'checkbox',
  7762. 'name' => 'fish',
  7763. ]);
  7764. $expected = [
  7765. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7766. ['div' => ['class' => 'checkbox']],
  7767. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7768. ['label' => ['for' => 'fish-0']],
  7769. '1',
  7770. '/label',
  7771. '/div',
  7772. ['div' => ['class' => 'checkbox']],
  7773. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7774. ['label' => ['for' => 'fish-1']],
  7775. '2',
  7776. '/label',
  7777. '/div'
  7778. ];
  7779. $this->assertHtml($expected, $result);
  7780. }
  7781. /**
  7782. * testControlContainerTemplates method
  7783. *
  7784. * Test that *Container templates are used by input.
  7785. *
  7786. * @return void
  7787. */
  7788. public function testControlContainerTemplates()
  7789. {
  7790. $this->Form->templates([
  7791. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7792. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7793. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7794. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7795. ]);
  7796. $this->article['errors'] = [
  7797. 'Article' => ['published' => 'error message']
  7798. ];
  7799. $this->Form->create($this->article);
  7800. $result = $this->Form->control('accept', [
  7801. 'type' => 'checkbox'
  7802. ]);
  7803. $expected = [
  7804. 'div' => ['class' => 'check'],
  7805. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7806. 'label' => ['for' => 'accept'],
  7807. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7808. 'Accept',
  7809. '/label',
  7810. '/div'
  7811. ];
  7812. $this->assertHtml($expected, $result);
  7813. $result = $this->Form->control('accept', [
  7814. 'type' => 'radio',
  7815. 'options' => ['Y', 'N']
  7816. ]);
  7817. $this->assertContains('<div class="rad">', $result);
  7818. $result = $this->Form->control('Article.published', [
  7819. 'type' => 'radio',
  7820. 'options' => ['Y', 'N']
  7821. ]);
  7822. $this->assertContains('<div class="rad err">', $result);
  7823. $result = $this->Form->control('Article.created', [
  7824. 'type' => 'datetime',
  7825. ]);
  7826. $this->assertContains('<div class="dt">', $result);
  7827. }
  7828. /**
  7829. * testFormGroupTemplates method
  7830. *
  7831. * Test that *Container templates are used by input.
  7832. *
  7833. * @return void
  7834. */
  7835. public function testFormGroupTemplates()
  7836. {
  7837. $this->Form->templates([
  7838. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7839. ]);
  7840. $this->Form->create($this->article);
  7841. $result = $this->Form->control('accept', [
  7842. 'type' => 'radio',
  7843. 'options' => ['Y', 'N']
  7844. ]);
  7845. $this->assertContains('<div class="radio">', $result);
  7846. }
  7847. /**
  7848. * testResetTemplates method
  7849. *
  7850. * Test resetting templates.
  7851. *
  7852. * @return void
  7853. */
  7854. public function testResetTemplates()
  7855. {
  7856. $this->Form->templates(['input' => '<input/>']);
  7857. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7858. $this->Form->resetTemplates();
  7859. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7860. }
  7861. /**
  7862. * testContext method
  7863. *
  7864. * Test the context method.
  7865. *
  7866. * @return void
  7867. */
  7868. public function testContext()
  7869. {
  7870. $result = $this->Form->context();
  7871. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7872. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7873. $this->assertSame($mock, $this->Form->context($mock));
  7874. $this->assertSame($mock, $this->Form->context());
  7875. }
  7876. /**
  7877. * testAutoDomId method
  7878. *
  7879. * @return void
  7880. */
  7881. public function testAutoDomId()
  7882. {
  7883. $result = $this->Form->text('field', ['id' => true]);
  7884. $expected = [
  7885. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7886. ];
  7887. $this->assertHtml($expected, $result);
  7888. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7889. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7890. $expected = [
  7891. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7892. ['label' => ['for' => 'field-0']],
  7893. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7894. 'option A',
  7895. '/label',
  7896. ['label' => ['for' => 'field-1']],
  7897. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7898. 'option B',
  7899. '/label',
  7900. ];
  7901. $this->assertHtml($expected, $result);
  7902. $result = $this->Form->select(
  7903. 'multi_field',
  7904. ['first', 'second'],
  7905. ['multiple' => 'checkbox', 'id' => true]
  7906. );
  7907. $expected = [
  7908. 'input' => [
  7909. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7910. ],
  7911. ['div' => ['class' => 'checkbox']],
  7912. ['label' => ['for' => 'multi-field-0']],
  7913. ['input' => [
  7914. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7915. 'value' => '0', 'id' => 'multi-field-0'
  7916. ]],
  7917. 'first',
  7918. '/label',
  7919. '/div',
  7920. ['div' => ['class' => 'checkbox']],
  7921. ['label' => ['for' => 'multi-field-1']],
  7922. ['input' => [
  7923. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7924. 'value' => '1', 'id' => 'multi-field-1'
  7925. ]],
  7926. 'second',
  7927. '/label',
  7928. '/div',
  7929. ];
  7930. $this->assertHtml($expected, $result);
  7931. }
  7932. /**
  7933. * Test the basic setters and getters for value sources
  7934. *
  7935. * @return void
  7936. */
  7937. public function testFormValueSourcesSettersGetters()
  7938. {
  7939. $expected = ['context'];
  7940. $result = $this->Form->getValueSources();
  7941. $this->assertEquals($expected, $result);
  7942. $expected = null;
  7943. $result = $this->Form->getSourceValue('id');
  7944. $this->assertEquals($expected, $result);
  7945. $expected = ['query', 'data', 'context'];
  7946. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7947. $result = $this->Form->getValueSources();
  7948. $this->assertEquals($expected, $result);
  7949. $this->Form->request->data['id'] = '1';
  7950. $this->Form->request->query['id'] = '2';
  7951. $this->Form->setValueSources(['context']);
  7952. $expected = '1';
  7953. $result = $this->Form->getSourceValue('id');
  7954. $this->assertEquals($expected, $result);
  7955. $this->Form->setValueSources('query');
  7956. $expected = ['query'];
  7957. $result = $this->Form->getValueSources();
  7958. $this->assertEquals($expected, $result);
  7959. $expected = '2';
  7960. $result = $this->Form->getSourceValue('id');
  7961. $this->assertEquals($expected, $result);
  7962. $this->Form->setValueSources(['data']);
  7963. $expected = '1';
  7964. $result = $this->Form->getSourceValue('id');
  7965. $this->assertEquals($expected, $result);
  7966. $this->Form->setValueSources(['query', 'data']);
  7967. $expected = '2';
  7968. $result = $this->Form->getSourceValue('id');
  7969. $this->assertEquals($expected, $result);
  7970. }
  7971. /**
  7972. * Tests the different input rendering values based on sources values switching
  7973. *
  7974. * @return void
  7975. */
  7976. public function testFormValueSourcesSingleSwitchRendering()
  7977. {
  7978. $this->loadFixtures('Articles');
  7979. $articles = TableRegistry::get('Articles');
  7980. $article = new Article();
  7981. $articles->patchEntity($article, ['id' => '3']);
  7982. $this->Form->create($article);
  7983. $this->Form->setValueSources(['context']);
  7984. $result = $this->Form->control('id');
  7985. $expected = [
  7986. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7987. ];
  7988. $this->assertHtml($expected, $result);
  7989. $this->Form->request->query['id'] = '5';
  7990. $this->Form->setValueSources(['query']);
  7991. $result = $this->Form->control('id');
  7992. $expected = [
  7993. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7994. ];
  7995. $this->assertHtml($expected, $result);
  7996. $this->Form->request->query['id'] = '5a';
  7997. $this->Form->request->data['id'] = '5b';
  7998. $this->Form->setValueSources(['context']);
  7999. $this->Form->create($article);
  8000. $result = $this->Form->control('id');
  8001. $expected = [
  8002. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8003. ];
  8004. $this->assertHtml($expected, $result);
  8005. $this->Form->setValueSources(['data']);
  8006. $this->Form->create($article);
  8007. $result = $this->Form->control('id');
  8008. $expected = [
  8009. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8010. ];
  8011. $this->assertHtml($expected, $result);
  8012. $this->Form->setValueSources(['query']);
  8013. $result = $this->Form->control('id');
  8014. $expected = [
  8015. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8016. ];
  8017. $this->assertHtml($expected, $result);
  8018. }
  8019. /**
  8020. * Tests the different input rendering values based on sources values switching while supplying
  8021. * an entity (base context) and multiple sources (such as data, query)
  8022. *
  8023. * @return void
  8024. */
  8025. public function testFormValueSourcesListSwitchRendering()
  8026. {
  8027. $this->loadFixtures('Articles');
  8028. $articles = TableRegistry::get('Articles');
  8029. $article = new Article();
  8030. $articles->patchEntity($article, ['id' => '3']);
  8031. $this->Form->request->query['id'] = '9';
  8032. $this->Form->create($article);
  8033. $this->Form->setValueSources(['context', 'query']);
  8034. $result = $this->Form->control('id');
  8035. $expected = [
  8036. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8037. ];
  8038. $this->assertHtml($expected, $result);
  8039. $this->Form->setValueSources(['query', 'context']);
  8040. $result = $this->Form->control('id');
  8041. $expected = [
  8042. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8043. ];
  8044. $this->assertHtml($expected, $result);
  8045. $this->Form->setValueSources(['data', 'query', 'context']);
  8046. $result = $this->Form->control('id');
  8047. $expected = [
  8048. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8049. ];
  8050. $this->assertHtml($expected, $result);
  8051. $this->Form->request->data['id'] = '8';
  8052. $this->Form->request->query['id'] = '9';
  8053. $this->Form->setValueSources(['data', 'query', 'context']);
  8054. $result = $this->Form->control('id');
  8055. $expected = [
  8056. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8057. ];
  8058. $this->assertHtml($expected, $result);
  8059. }
  8060. /**
  8061. * Test the different form input renderings based on values sources switchings through form options
  8062. *
  8063. * @return void
  8064. */
  8065. public function testFormValueSourcesSwitchViaOptionsRendering()
  8066. {
  8067. $this->loadFixtures('Articles');
  8068. $articles = TableRegistry::get('Articles');
  8069. $article = new Article();
  8070. $articles->patchEntity($article, ['id' => '3']);
  8071. $this->Form->request->data['id'] = '4';
  8072. $this->Form->request->query['id'] = '5';
  8073. $this->Form->create($article, ['valueSources' => 'query']);
  8074. $result = $this->Form->control('id');
  8075. $expected = [
  8076. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8077. ];
  8078. $this->assertHtml($expected, $result);
  8079. $result = $this->Form->getSourceValue('id');
  8080. $this->assertEquals('5', $result);
  8081. $this->Form->setValueSources(['context']);
  8082. $this->Form->create($article, ['valueSources' => 'query']);
  8083. $result = $this->Form->control('id');
  8084. $expected = [
  8085. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8086. ];
  8087. $this->assertHtml($expected, $result);
  8088. $result = $this->Form->getSourceValue('id');
  8089. $this->assertEquals('5', $result);
  8090. $this->Form->setValueSources(['query']);
  8091. $this->Form->create($article, ['valueSources' => 'data']);
  8092. $result = $this->Form->control('id');
  8093. $expected = [
  8094. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8095. ];
  8096. $this->assertHtml($expected, $result);
  8097. $result = $this->Form->getSourceValue('id');
  8098. $this->assertEquals('4', $result);
  8099. $this->Form->setValueSources(['query']);
  8100. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8101. $result = $this->Form->control('id');
  8102. $expected = [
  8103. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8104. ];
  8105. $this->assertHtml($expected, $result);
  8106. $result = $this->Form->getSourceValue('id');
  8107. $this->assertEquals('4', $result);
  8108. }
  8109. /**
  8110. * Test the different form input renderings based on values sources switchings through form options
  8111. *
  8112. * @return void
  8113. */
  8114. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8115. {
  8116. $this->loadFixtures('Articles');
  8117. $articles = TableRegistry::get('Articles');
  8118. $article = new Article();
  8119. $articles->patchEntity($article, ['id' => '3']);
  8120. $this->Form->request->data['id'] = '10';
  8121. $this->Form->request->query['id'] = '11';
  8122. $this->Form->setValueSources(['context'])
  8123. ->create($article, ['valueSources' => ['query', 'data']]);
  8124. $result = $this->Form->control('id');
  8125. $expected = [
  8126. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8127. ];
  8128. $this->assertHtml($expected, $result);
  8129. $result = $this->Form->getSourceValue('id');
  8130. $this->assertEquals('11', $result);
  8131. unset($this->Form->request->query['id']);
  8132. $this->Form->setValueSources(['context'])
  8133. ->create($article, ['valueSources' => ['query', 'data']]);
  8134. $result = $this->Form->control('id');
  8135. $expected = [
  8136. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8137. ];
  8138. $this->assertHtml($expected, $result);
  8139. $result = $this->Form->getSourceValue('id');
  8140. $this->assertEquals('10', $result);
  8141. }
  8142. /**
  8143. * Test the different form values sources resetting through From::end();
  8144. *
  8145. * @return void
  8146. */
  8147. public function testFormValueSourcesResetViaEnd()
  8148. {
  8149. $expected = ['context'];
  8150. $result = $this->Form->getValueSources();
  8151. $this->assertEquals($expected, $result);
  8152. $expected = ['query', 'context', 'data'];
  8153. $this->Form->setValueSources(['query', 'context', 'data']);
  8154. $result = $this->Form->getValueSources();
  8155. $this->assertEquals($expected, $result);
  8156. $this->Form->create();
  8157. $result = $this->Form->getValueSources();
  8158. $this->assertEquals($expected, $result);
  8159. $this->Form->end();
  8160. $result = $this->Form->getValueSources();
  8161. $this->assertEquals(['context'], $result);
  8162. }
  8163. /**
  8164. * Test sources values defaults handling
  8165. *
  8166. * @return void
  8167. */
  8168. public function testFormValueSourcesDefaults()
  8169. {
  8170. $this->Form->request->query['password'] = 'open Sesame';
  8171. $this->Form->create();
  8172. $result = $this->Form->password('password');
  8173. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8174. $this->assertHtml($expected, $result);
  8175. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8176. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8177. $this->assertHtml($expected, $result);
  8178. $this->Form->setValueSources('query');
  8179. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8180. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8181. $this->assertHtml($expected, $result);
  8182. $this->Form->setValueSources('data');
  8183. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8184. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8185. $this->assertHtml($expected, $result);
  8186. }
  8187. /**
  8188. * Test sources values schema defaults handling
  8189. *
  8190. * @return void
  8191. */
  8192. public function testSourcesValueDoesntExistPassThrough()
  8193. {
  8194. $this->Form->request->query['category'] = 'sesame-cookies';
  8195. $articles = TableRegistry::get('Articles');
  8196. $entity = $articles->newEntity();
  8197. $this->Form->create($entity);
  8198. $this->Form->setValueSources(['query', 'context']);
  8199. $result = $this->Form->getSourceValue('category');
  8200. $this->assertEquals('sesame-cookies', $result);
  8201. $this->Form->setValueSources(['context', 'query']);
  8202. $result = $this->Form->getSourceValue('category');
  8203. $this->assertEquals('sesame-cookies', $result);
  8204. }
  8205. /**
  8206. * testNestedLabelInput method
  8207. *
  8208. * Test the `nestedInput` parameter
  8209. *
  8210. * @return void
  8211. */
  8212. public function testNestedLabelInput()
  8213. {
  8214. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8215. $expected = [
  8216. 'div' => ['class' => 'input text'],
  8217. 'label' => ['for' => 'foo'],
  8218. ['input' => [
  8219. 'type' => 'text',
  8220. 'name' => 'foo',
  8221. 'id' => 'foo'
  8222. ]],
  8223. 'Foo',
  8224. '/label',
  8225. '/div'
  8226. ];
  8227. $this->assertHtml($expected, $result);
  8228. }
  8229. /**
  8230. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8231. *
  8232. * This test makes sure `false` excludes the label from the render
  8233. *
  8234. * @return void
  8235. */
  8236. public function testInputLabelManipulationDisableLabels()
  8237. {
  8238. $result = $this->Form->input('test', [
  8239. 'type' => 'radio',
  8240. 'options' => ['A', 'B'],
  8241. 'labelOptions' => false
  8242. ]);
  8243. $expected = [
  8244. ['div' => ['class' => 'input radio']],
  8245. '<label',
  8246. 'Test',
  8247. '/label',
  8248. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8249. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8250. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8251. '/div',
  8252. ];
  8253. $this->assertHtml($expected, $result);
  8254. $result = $this->Form->input('checkbox1', [
  8255. 'label' => 'My checkboxes',
  8256. 'multiple' => 'checkbox',
  8257. 'type' => 'select',
  8258. 'options' => [
  8259. ['text' => 'First Checkbox', 'value' => 1],
  8260. ['text' => 'Second Checkbox', 'value' => 2]
  8261. ],
  8262. 'labelOptions' => false
  8263. ]);
  8264. $expected = [
  8265. ['div' => ['class' => 'input select']],
  8266. ['label' => ['for' => 'checkbox1']],
  8267. 'My checkboxes',
  8268. '/label',
  8269. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8270. ['div' => ['class' => 'checkbox']],
  8271. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8272. '/div',
  8273. ['div' => ['class' => 'checkbox']],
  8274. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8275. '/div',
  8276. '/div'
  8277. ];
  8278. $this->assertHtml($expected, $result);
  8279. }
  8280. /**
  8281. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8282. *
  8283. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8284. * added to the class if checked.
  8285. *
  8286. * Also checks to make sure any custom attributes are rendered correctly
  8287. *
  8288. * @return void
  8289. */
  8290. public function testInputLabelManipulationRadios()
  8291. {
  8292. $result = $this->Form->input('test', [
  8293. 'type' => 'radio',
  8294. 'options' => ['A', 'B'],
  8295. 'labelOptions' => ['class' => 'custom-class']
  8296. ]);
  8297. $expected = [
  8298. ['div' => ['class' => 'input radio']],
  8299. '<label',
  8300. 'Test',
  8301. '/label',
  8302. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8303. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8304. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8305. 'A',
  8306. '/label',
  8307. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8308. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8309. 'B',
  8310. '/label',
  8311. '/div',
  8312. ];
  8313. $this->assertHtml($expected, $result);
  8314. $result = $this->Form->input('test', [
  8315. 'type' => 'radio',
  8316. 'options' => ['A', 'B'],
  8317. 'value' => 1,
  8318. 'labelOptions' => ['class' => 'custom-class']
  8319. ]);
  8320. $expected = [
  8321. ['div' => ['class' => 'input radio']],
  8322. '<label',
  8323. 'Test',
  8324. '/label',
  8325. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8326. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8327. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8328. 'A',
  8329. '/label',
  8330. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8331. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8332. 'B',
  8333. '/label',
  8334. '/div',
  8335. ];
  8336. $this->assertHtml($expected, $result);
  8337. $result = $this->Form->input('test', [
  8338. 'type' => 'radio',
  8339. 'options' => ['A', 'B'],
  8340. 'value' => 1,
  8341. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8342. ]);
  8343. $expected = [
  8344. ['div' => ['class' => 'input radio']],
  8345. '<label',
  8346. 'Test',
  8347. '/label',
  8348. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8349. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8350. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8351. 'A',
  8352. '/label',
  8353. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8354. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8355. 'B',
  8356. '/label',
  8357. '/div',
  8358. ];
  8359. $this->assertHtml($expected, $result);
  8360. $result = $this->Form->radio('test', ['A', 'B'], [
  8361. 'label' => [
  8362. 'class' => ['custom-class', 'another-class'],
  8363. 'data-name' => 'bob'
  8364. ],
  8365. 'value' => 1
  8366. ]);
  8367. $expected = [
  8368. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8369. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8370. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8371. 'A',
  8372. '/label',
  8373. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8374. ['input' => [
  8375. 'type' => 'radio',
  8376. 'name' => 'test',
  8377. 'value' => '1',
  8378. 'id' => 'test-1',
  8379. 'checked' => 'checked'
  8380. ]],
  8381. 'B',
  8382. '/label',
  8383. ];
  8384. $this->assertHtml($expected, $result);
  8385. }
  8386. /**
  8387. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8388. *
  8389. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8390. * added to the class if checked.
  8391. *
  8392. * Also checks to make sure any custom attributes are rendered correctly
  8393. *
  8394. * @return void
  8395. */
  8396. public function testInputLabelManipulationCheckboxes()
  8397. {
  8398. $result = $this->Form->input('checkbox1', [
  8399. 'label' => 'My checkboxes',
  8400. 'multiple' => 'checkbox',
  8401. 'type' => 'select',
  8402. 'options' => [
  8403. ['text' => 'First Checkbox', 'value' => 1],
  8404. ['text' => 'Second Checkbox', 'value' => 2]
  8405. ],
  8406. 'labelOptions' => ['class' => 'custom-class'],
  8407. 'value' => ['1']
  8408. ]);
  8409. $expected = [
  8410. ['div' => ['class' => 'input select']],
  8411. ['label' => ['for' => 'checkbox1']],
  8412. 'My checkboxes',
  8413. '/label',
  8414. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8415. ['div' => ['class' => 'checkbox']],
  8416. ['label' => [
  8417. 'class' => 'custom-class selected',
  8418. 'for' => 'checkbox1-1'
  8419. ]],
  8420. ['input' => [
  8421. 'type' => 'checkbox',
  8422. 'name' => 'checkbox1[]',
  8423. 'value' => '1',
  8424. 'id' => 'checkbox1-1',
  8425. 'checked' => 'checked'
  8426. ]],
  8427. 'First Checkbox',
  8428. '/label',
  8429. '/div',
  8430. ['div' => ['class' => 'checkbox']],
  8431. ['label' => [
  8432. 'class' => 'custom-class',
  8433. 'for' => 'checkbox1-2'
  8434. ]],
  8435. ['input' => [
  8436. 'type' => 'checkbox',
  8437. 'name' => 'checkbox1[]',
  8438. 'value' => '2',
  8439. 'id' => 'checkbox1-2'
  8440. ]],
  8441. 'Second Checkbox',
  8442. '/label',
  8443. '/div',
  8444. '/div'
  8445. ];
  8446. $this->assertHtml($expected, $result);
  8447. $result = $this->Form->input('checkbox1', [
  8448. 'label' => 'My checkboxes',
  8449. 'multiple' => 'checkbox',
  8450. 'type' => 'select',
  8451. 'options' => [
  8452. ['text' => 'First Checkbox', 'value' => 1],
  8453. ['text' => 'Second Checkbox', 'value' => 2]
  8454. ],
  8455. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8456. 'value' => ['1']
  8457. ]);
  8458. $expected = [
  8459. ['div' => ['class' => 'input select']],
  8460. ['label' => ['for' => 'checkbox1']],
  8461. 'My checkboxes',
  8462. '/label',
  8463. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8464. ['div' => ['class' => 'checkbox']],
  8465. ['label' => [
  8466. 'class' => 'custom-class another-class selected',
  8467. 'data-name' => 'bob',
  8468. 'for' => 'checkbox1-1'
  8469. ]],
  8470. ['input' => [
  8471. 'type' => 'checkbox',
  8472. 'name' => 'checkbox1[]',
  8473. 'value' => '1',
  8474. 'id' => 'checkbox1-1',
  8475. 'checked' => 'checked'
  8476. ]],
  8477. 'First Checkbox',
  8478. '/label',
  8479. '/div',
  8480. ['div' => ['class' => 'checkbox']],
  8481. ['label' => [
  8482. 'class' => 'custom-class another-class',
  8483. 'data-name' => 'bob',
  8484. 'for' => 'checkbox1-2'
  8485. ]],
  8486. ['input' => [
  8487. 'type' => 'checkbox',
  8488. 'name' => 'checkbox1[]',
  8489. 'value' => '2',
  8490. 'id' => 'checkbox1-2'
  8491. ]],
  8492. 'Second Checkbox',
  8493. '/label',
  8494. '/div',
  8495. '/div'
  8496. ];
  8497. $this->assertHtml($expected, $result);
  8498. }
  8499. }