FormHelperTest.php 298 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Collection\Collection;
  17. use Cake\Core\Configure;
  18. use Cake\Form\Form;
  19. use Cake\Network\Request;
  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. */
  104. class FormHelperTest extends TestCase
  105. {
  106. /**
  107. * Fixtures to be used
  108. *
  109. * @var array
  110. */
  111. public $fixtures = ['core.articles', 'core.comments'];
  112. /**
  113. * Do not load the fixtures by default
  114. *
  115. * @var bool
  116. */
  117. public $autoFixtures = false;
  118. /**
  119. * setUp method
  120. *
  121. * @return void
  122. */
  123. public function setUp()
  124. {
  125. parent::setUp();
  126. Configure::write('Config.language', 'eng');
  127. Configure::write('App.base', '');
  128. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  129. $this->View = new View();
  130. $this->Form = new FormHelper($this->View);
  131. $request = new Request([
  132. 'webroot' => '',
  133. 'base' => '',
  134. 'url' => '/articles/add',
  135. 'params' => [
  136. 'controller' => 'articles',
  137. 'action' => 'add',
  138. ]
  139. ]);
  140. $this->Form->Url->request = $this->Form->request = $request;
  141. $this->dateRegex = [
  142. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  143. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  144. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  145. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  146. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  147. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  148. ];
  149. $this->article = [
  150. 'schema' => [
  151. 'id' => ['type' => 'integer'],
  152. 'author_id' => ['type' => 'integer', 'null' => true],
  153. 'title' => ['type' => 'string', 'null' => true],
  154. 'body' => 'text',
  155. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  156. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  157. ],
  158. 'required' => [
  159. 'author_id' => true,
  160. 'title' => true,
  161. ]
  162. ];
  163. Security::salt('foo!');
  164. Router::connect('/:controller', ['action' => 'index']);
  165. Router::connect('/:controller/:action/*');
  166. }
  167. /**
  168. * tearDown method
  169. *
  170. * @return void
  171. */
  172. public function tearDown()
  173. {
  174. parent::tearDown();
  175. unset($this->Form, $this->Controller, $this->View);
  176. TableRegistry::clear();
  177. }
  178. /**
  179. * Test construct() with the templates option.
  180. *
  181. * @return void
  182. */
  183. public function testConstructTemplatesFile()
  184. {
  185. $helper = new FormHelper($this->View, [
  186. 'templates' => 'htmlhelper_tags'
  187. ]);
  188. $result = $helper->control('name');
  189. $this->assertContains('<input', $result);
  190. }
  191. /**
  192. * Test that when specifying custom widgets the config array for that widget
  193. * is overwritten instead of merged.
  194. *
  195. * @return void
  196. */
  197. public function testConstructWithWidgets()
  198. {
  199. $config = [
  200. 'widgets' => [
  201. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  202. ]
  203. ];
  204. $helper = new FormHelper($this->View, $config);
  205. $registry = $helper->widgetRegistry();
  206. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  207. }
  208. /**
  209. * Test that when specifying custom widgets config file and it should be
  210. * added to widgets array. WidgetRegistry will load widgets in constructor.
  211. *
  212. * @return void
  213. */
  214. public function testConstructWithWidgetsConfig()
  215. {
  216. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  217. $registry = $helper->widgetRegistry();
  218. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  219. }
  220. /**
  221. * Test registering a new widget class and rendering it.
  222. *
  223. * @return void
  224. */
  225. public function testAddWidgetAndRenderWidget()
  226. {
  227. $data = [
  228. 'val' => 1
  229. ];
  230. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  231. $this->Form->addWidget('test', $mock);
  232. $mock->expects($this->once())
  233. ->method('render')
  234. ->with($data)
  235. ->will($this->returnValue('HTML'));
  236. $result = $this->Form->widget('test', $data);
  237. $this->assertEquals('HTML', $result);
  238. }
  239. /**
  240. * Test that secureFields() of widget is called after calling render(),
  241. * not before.
  242. *
  243. * @return void
  244. */
  245. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  246. {
  247. $data = [
  248. 'val' => 1,
  249. 'name' => 'test'
  250. ];
  251. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  252. $this->Form->addWidget('test', $mock);
  253. $mock->expects($this->at(0))
  254. ->method('render')
  255. ->with($data)
  256. ->will($this->returnValue('HTML'));
  257. $mock->expects($this->at(1))
  258. ->method('secureFields')
  259. ->with($data)
  260. ->will($this->returnValue(['test']));
  261. $result = $this->Form->widget('test', $data + ['secure' => true]);
  262. $this->assertEquals('HTML', $result);
  263. }
  264. /**
  265. * Test that empty string is not added to secure fields list when
  266. * rendering input widget without name.
  267. *
  268. * @return void
  269. */
  270. public function testRenderingWidgetWithEmptyName()
  271. {
  272. $this->assertEquals([], $this->Form->fields);
  273. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  274. $this->assertEquals('<select name=""></select>', $result);
  275. $this->assertEquals([], $this->Form->fields);
  276. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  277. $this->assertEquals('<select name="0"></select>', $result);
  278. $this->assertEquals(['0'], $this->Form->fields);
  279. }
  280. /**
  281. * Test registering an invalid widget class.
  282. *
  283. * @expectedException \RuntimeException
  284. * @return void
  285. */
  286. public function testAddWidgetInvalid()
  287. {
  288. $mock = new \StdClass();
  289. $this->Form->addWidget('test', $mock);
  290. $this->Form->widget('test');
  291. }
  292. /**
  293. * Test adding a new context class.
  294. *
  295. * @return void
  296. */
  297. public function testAddContextProvider()
  298. {
  299. $context = 'My data';
  300. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  301. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  302. $this->assertInstanceOf('Cake\Network\Request', $request);
  303. $this->assertEquals($context, $data['entity']);
  304. return $stub;
  305. });
  306. $this->Form->create($context);
  307. $result = $this->Form->context();
  308. $this->assertSame($stub, $result);
  309. }
  310. /**
  311. * Test replacing a context class.
  312. *
  313. * @return void
  314. */
  315. public function testAddContextProviderReplace()
  316. {
  317. $entity = new Article();
  318. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  319. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  320. return $stub;
  321. });
  322. $this->Form->create($entity);
  323. $result = $this->Form->context();
  324. $this->assertSame($stub, $result);
  325. }
  326. /**
  327. * Test overriding a context class.
  328. *
  329. * @return void
  330. */
  331. public function testAddContextProviderAdd()
  332. {
  333. $entity = new Article();
  334. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  335. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  336. if ($data['entity'] instanceof Entity) {
  337. return $stub;
  338. }
  339. });
  340. $this->Form->create($entity);
  341. $result = $this->Form->context();
  342. $this->assertSame($stub, $result);
  343. }
  344. /**
  345. * Test adding an invalid context class.
  346. *
  347. * @expectedException \RuntimeException
  348. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  349. * @return void
  350. */
  351. public function testAddContextProviderInvalid()
  352. {
  353. $context = 'My data';
  354. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  355. return new \StdClass();
  356. });
  357. $this->Form->create($context);
  358. }
  359. /**
  360. * Provides context options for create().
  361. *
  362. * @return array
  363. */
  364. public function contextSelectionProvider()
  365. {
  366. $entity = new Article();
  367. $collection = new Collection([$entity]);
  368. $emptyCollection = new Collection([]);
  369. $arrayObject = new \ArrayObject([]);
  370. $data = [
  371. 'schema' => [
  372. 'title' => ['type' => 'string']
  373. ]
  374. ];
  375. $form = new Form();
  376. return [
  377. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  378. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  379. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  380. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  381. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  382. 'form' => [$form, 'Cake\View\Form\FormContext'],
  383. 'none' => [null, 'Cake\View\Form\NullContext'],
  384. 'false' => [false, 'Cake\View\Form\NullContext'],
  385. ];
  386. }
  387. /**
  388. * Test default context selection in create()
  389. *
  390. * @dataProvider contextSelectionProvider
  391. * @return void
  392. */
  393. public function testCreateContextSelectionBuiltIn($data, $class)
  394. {
  395. $this->loadFixtures('Articles');
  396. $this->Form->create($data);
  397. $this->assertInstanceOf($class, $this->Form->context());
  398. }
  399. /**
  400. * Data provider for type option.
  401. *
  402. * @return array
  403. */
  404. public static function requestTypeProvider()
  405. {
  406. return [
  407. // type, method, override
  408. ['post', 'post', 'POST'],
  409. ['put', 'post', 'PUT'],
  410. ['patch', 'post', 'PATCH'],
  411. ['delete', 'post', 'DELETE'],
  412. ];
  413. }
  414. /**
  415. * Test creating file forms.
  416. *
  417. * @return void
  418. */
  419. public function testCreateFile()
  420. {
  421. $encoding = strtolower(Configure::read('App.encoding'));
  422. $result = $this->Form->create(false, ['type' => 'file']);
  423. $expected = [
  424. 'form' => [
  425. 'method' => 'post', 'action' => '/articles/add',
  426. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  427. ],
  428. 'div' => ['style' => 'display:none;'],
  429. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  430. '/div'
  431. ];
  432. $this->assertHtml($expected, $result);
  433. }
  434. /**
  435. * Test creating GET forms.
  436. *
  437. * @return void
  438. */
  439. public function testCreateGet()
  440. {
  441. $encoding = strtolower(Configure::read('App.encoding'));
  442. $result = $this->Form->create(false, ['type' => 'get']);
  443. $expected = ['form' => [
  444. 'method' => 'get', 'action' => '/articles/add',
  445. 'accept-charset' => $encoding
  446. ]];
  447. $this->assertHtml($expected, $result);
  448. }
  449. /**
  450. * Test explicit method/enctype options.
  451. *
  452. * Explicit method overwrites inferred method from 'type'
  453. *
  454. * @return void
  455. */
  456. public function testCreateExplicitMethodEnctype()
  457. {
  458. $encoding = strtolower(Configure::read('App.encoding'));
  459. $result = $this->Form->create(false, [
  460. 'type' => 'get',
  461. 'method' => 'put',
  462. 'enctype' => 'multipart/form-data'
  463. ]);
  464. $expected = ['form' => [
  465. 'method' => 'put',
  466. 'action' => '/articles/add',
  467. 'enctype' => 'multipart/form-data',
  468. 'accept-charset' => $encoding
  469. ]];
  470. $this->assertHtml($expected, $result);
  471. }
  472. /**
  473. * Test create() with the templates option.
  474. *
  475. * @return void
  476. */
  477. public function testCreateTemplatesArray()
  478. {
  479. $result = $this->Form->create($this->article, [
  480. 'templates' => [
  481. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  482. ]
  483. ]);
  484. $expected = [
  485. 'form' => [
  486. 'class' => 'form-horizontal',
  487. 'method' => 'post',
  488. 'action' => '/articles/add',
  489. 'accept-charset' => 'utf-8'
  490. ]
  491. ];
  492. $this->assertHtml($expected, $result);
  493. }
  494. /**
  495. * Test create() with the templates option.
  496. *
  497. * @return void
  498. */
  499. public function testCreateTemplatesFile()
  500. {
  501. $result = $this->Form->create($this->article, [
  502. 'templates' => 'htmlhelper_tags',
  503. ]);
  504. $expected = [
  505. 'start form',
  506. 'div' => ['class' => 'hidden'],
  507. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  508. '/div'
  509. ];
  510. $this->assertHtml($expected, $result);
  511. }
  512. /**
  513. * Test that create() and end() restore templates.
  514. *
  515. * @return void
  516. */
  517. public function testCreateEndRestoreTemplates()
  518. {
  519. $this->Form->create($this->article, [
  520. 'templates' => ['input' => 'custom input element']
  521. ]);
  522. $this->Form->end();
  523. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  524. }
  525. /**
  526. * Test using template vars in various templates used by control() method.
  527. *
  528. * @return void
  529. */
  530. public function testControlTemplateVars()
  531. {
  532. $result = $this->Form->control('text', [
  533. 'templates' => [
  534. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  535. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  536. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  537. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  538. ],
  539. 'templateVars' => [
  540. 'forinput' => 'in-input',
  541. 'forlabel' => 'in-label',
  542. 'forgroup' => 'in-group',
  543. 'forcontainer' => 'in-container'
  544. ]
  545. ]);
  546. $expected = [
  547. 'div' => ['class'],
  548. 'label' => ['for'],
  549. 'Text in-label',
  550. '/label',
  551. 'in-group',
  552. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  553. 'in-container',
  554. '/div',
  555. ];
  556. $this->assertHtml($expected, $result);
  557. }
  558. /**
  559. * Test ensuring template variables work in template files loaded
  560. * during control().
  561. *
  562. * @return void
  563. */
  564. public function testControlTemplatesFromFile()
  565. {
  566. $result = $this->Form->control('title', [
  567. 'templates' => 'test_templates',
  568. 'templateVars' => [
  569. 'forcontainer' => 'container-data'
  570. ]
  571. ]);
  572. $expected = [
  573. 'div' => ['class'],
  574. 'label' => ['for'],
  575. 'Title',
  576. '/label',
  577. 'input' => ['name', 'type' => 'text', 'id'],
  578. 'container-data',
  579. '/div',
  580. ];
  581. $this->assertHtml($expected, $result);
  582. }
  583. /**
  584. * Test using template vars in inputSubmit and submitContainer template.
  585. *
  586. * @return void
  587. */
  588. public function testSubmitTemplateVars()
  589. {
  590. $this->Form->templates([
  591. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  592. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  593. ]);
  594. $result = $this->Form->submit('Submit', [
  595. 'templateVars' => [
  596. 'forinput' => 'in-input',
  597. 'forcontainer' => 'in-container'
  598. ]
  599. ]);
  600. $expected = [
  601. 'div' => ['class'],
  602. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  603. 'in-container',
  604. '/div',
  605. ];
  606. $this->assertHtml($expected, $result);
  607. }
  608. /**
  609. * test the create() method
  610. *
  611. * @dataProvider requestTypeProvider
  612. * @return void
  613. */
  614. public function testCreateTypeOptions($type, $method, $override)
  615. {
  616. $encoding = strtolower(Configure::read('App.encoding'));
  617. $result = $this->Form->create(false, ['type' => $type]);
  618. $expected = [
  619. 'form' => [
  620. 'method' => $method, 'action' => '/articles/add',
  621. 'accept-charset' => $encoding
  622. ],
  623. 'div' => ['style' => 'display:none;'],
  624. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  625. '/div'
  626. ];
  627. $this->assertHtml($expected, $result);
  628. }
  629. /**
  630. * Test using template vars in Create (formStart template)
  631. *
  632. * @return void
  633. */
  634. public function testCreateTemplateVars()
  635. {
  636. $result = $this->Form->create($this->article, [
  637. 'templates' => [
  638. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  639. ],
  640. 'templateVars' => ['header' => 'headertext']
  641. ]);
  642. $expected = [
  643. 'h4' => ['class'],
  644. 'headertext',
  645. '/h4',
  646. 'form' => [
  647. 'method' => 'post',
  648. 'action' => '/articles/add',
  649. 'accept-charset' => 'utf-8'
  650. ]
  651. ];
  652. $this->assertHtml($expected, $result);
  653. }
  654. /**
  655. * Test opening a form for an update operation.
  656. *
  657. * @return void
  658. */
  659. public function testCreateUpdateForm()
  660. {
  661. $encoding = strtolower(Configure::read('App.encoding'));
  662. $this->Form->request->here = '/articles/edit/1';
  663. $this->Form->request->params['action'] = 'edit';
  664. $this->article['defaults']['id'] = 1;
  665. $result = $this->Form->create($this->article);
  666. $expected = [
  667. 'form' => [
  668. 'method' => 'post',
  669. 'action' => '/articles/edit/1',
  670. 'accept-charset' => $encoding
  671. ],
  672. 'div' => ['style' => 'display:none;'],
  673. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  674. '/div'
  675. ];
  676. $this->assertHtml($expected, $result);
  677. }
  678. /**
  679. * test create() with automatic url generation
  680. *
  681. * @return void
  682. */
  683. public function testCreateAutoUrl()
  684. {
  685. $encoding = strtolower(Configure::read('App.encoding'));
  686. $this->Form->request->params['action'] = 'delete';
  687. $this->Form->request->here = '/articles/delete/10';
  688. $this->Form->request->base = '';
  689. $result = $this->Form->create($this->article);
  690. $expected = [
  691. 'form' => [
  692. 'method' => 'post', 'action' => '/articles/delete/10',
  693. 'accept-charset' => $encoding
  694. ],
  695. 'div' => ['style' => 'display:none;'],
  696. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  697. '/div'
  698. ];
  699. $this->assertHtml($expected, $result);
  700. $this->article['defaults'] = ['id' => 1];
  701. $this->Form->request->here = '/articles/edit/1';
  702. $this->Form->request->params['action'] = 'delete';
  703. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  704. $expected = [
  705. 'form' => [
  706. 'method' => 'post',
  707. 'action' => '/articles/edit/1',
  708. 'accept-charset' => $encoding
  709. ],
  710. 'div' => ['style' => 'display:none;'],
  711. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  712. '/div'
  713. ];
  714. $this->assertHtml($expected, $result);
  715. $this->Form->request->params['action'] = 'add';
  716. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  717. $expected = [
  718. 'form' => [
  719. 'method' => 'post',
  720. 'action' => '/articles/publish/1',
  721. 'accept-charset' => $encoding
  722. ],
  723. 'div' => ['style' => 'display:none;'],
  724. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  725. '/div'
  726. ];
  727. $this->assertHtml($expected, $result);
  728. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  729. $expected = [
  730. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  731. 'div' => ['style' => 'display:none;'],
  732. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  733. '/div'
  734. ];
  735. $this->assertHtml($expected, $result);
  736. $this->Form->request->params['controller'] = 'Pages';
  737. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  738. $expected = [
  739. 'form' => [
  740. 'method' => 'post', 'action' => '/Pages/signup/1',
  741. 'accept-charset' => $encoding
  742. ],
  743. 'div' => ['style' => 'display:none;'],
  744. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  745. '/div'
  746. ];
  747. $this->assertHtml($expected, $result);
  748. }
  749. /**
  750. * Test create() with no URL (no "action" attribute for <form> tag)
  751. *
  752. * @return void
  753. */
  754. public function testCreateNoUrl()
  755. {
  756. $result = $this->Form->create(false, ['url' => false]);
  757. $expected = [
  758. 'form' => [
  759. 'method' => 'post',
  760. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  761. ],
  762. 'div' => ['style' => 'display:none;'],
  763. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  764. '/div'
  765. ];
  766. $this->assertHtml($expected, $result);
  767. }
  768. /**
  769. * test create() with a custom route
  770. *
  771. * @return void
  772. */
  773. public function testCreateCustomRoute()
  774. {
  775. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  776. $encoding = strtolower(Configure::read('App.encoding'));
  777. $this->Form->request->params['controller'] = 'users';
  778. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  779. $expected = [
  780. 'form' => [
  781. 'method' => 'post', 'action' => '/login',
  782. 'accept-charset' => $encoding
  783. ],
  784. 'div' => ['style' => 'display:none;'],
  785. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  786. '/div'
  787. ];
  788. $this->assertHtml($expected, $result);
  789. Router::connect(
  790. '/new-article',
  791. ['controller' => 'articles', 'action' => 'myaction'],
  792. ['_name' => 'my-route']
  793. );
  794. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  795. $expected = [
  796. 'form' => [
  797. 'method' => 'post', 'action' => '/new-article',
  798. 'accept-charset' => $encoding,
  799. ],
  800. 'div' => ['style' => 'display:none;'],
  801. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  802. '/div'
  803. ];
  804. $this->assertHtml($expected, $result);
  805. }
  806. /**
  807. * test automatic accept-charset overriding
  808. *
  809. * @return void
  810. */
  811. public function testCreateWithAcceptCharset()
  812. {
  813. $result = $this->Form->create(
  814. $this->article,
  815. [
  816. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  817. ]
  818. );
  819. $expected = [
  820. 'form' => [
  821. 'method' => 'post', 'action' => '/articles',
  822. 'accept-charset' => 'iso-8859-1'
  823. ],
  824. 'div' => ['style' => 'display:none;'],
  825. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  826. '/div'
  827. ];
  828. $this->assertHtml($expected, $result);
  829. }
  830. /**
  831. * Test base form URL when url param is passed with multiple parameters (&)
  832. *
  833. */
  834. public function testCreateQueryStringRequest()
  835. {
  836. $encoding = strtolower(Configure::read('App.encoding'));
  837. $result = $this->Form->create($this->article, [
  838. 'type' => 'post',
  839. 'escape' => false,
  840. 'url' => [
  841. 'controller' => 'controller',
  842. 'action' => 'action',
  843. '?' => ['param1' => 'value1', 'param2' => 'value2']
  844. ]
  845. ]);
  846. $expected = [
  847. 'form' => [
  848. 'method' => 'post',
  849. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  850. 'accept-charset' => $encoding
  851. ],
  852. 'div' => ['style' => 'display:none;'],
  853. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  854. '/div'
  855. ];
  856. $this->assertHtml($expected, $result);
  857. $result = $this->Form->create($this->article, [
  858. 'type' => 'post',
  859. 'url' => [
  860. 'controller' => 'controller',
  861. 'action' => 'action',
  862. '?' => ['param1' => 'value1', 'param2' => 'value2']
  863. ]
  864. ]);
  865. $this->assertHtml($expected, $result);
  866. }
  867. /**
  868. * test that create() doesn't cause errors by multiple id's being in the primary key
  869. * as could happen with multiple select or checkboxes.
  870. *
  871. * @return void
  872. */
  873. public function testCreateWithMultipleIdInData()
  874. {
  875. $encoding = strtolower(Configure::read('App.encoding'));
  876. $this->Form->request->data['Article']['id'] = [1, 2];
  877. $result = $this->Form->create($this->article);
  878. $expected = [
  879. 'form' => [
  880. 'method' => 'post',
  881. 'action' => '/articles/add',
  882. 'accept-charset' => $encoding
  883. ],
  884. 'div' => ['style' => 'display:none;'],
  885. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  886. '/div'
  887. ];
  888. $this->assertHtml($expected, $result);
  889. }
  890. /**
  891. * test that create() doesn't add in extra passed params.
  892. *
  893. * @return void
  894. */
  895. public function testCreatePassedArgs()
  896. {
  897. $encoding = strtolower(Configure::read('App.encoding'));
  898. $this->Form->request->data['Article']['id'] = 1;
  899. $result = $this->Form->create($this->article, [
  900. 'type' => 'post',
  901. 'escape' => false,
  902. 'url' => [
  903. 'action' => 'edit',
  904. 'myparam'
  905. ]
  906. ]);
  907. $expected = [
  908. 'form' => [
  909. 'method' => 'post',
  910. 'action' => '/articles/edit/myparam',
  911. 'accept-charset' => $encoding
  912. ],
  913. 'div' => ['style' => 'display:none;'],
  914. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  915. '/div'
  916. ];
  917. $this->assertHtml($expected, $result);
  918. }
  919. /**
  920. * test creating a get form, and get form inputs.
  921. *
  922. * @return void
  923. */
  924. public function testGetFormCreate()
  925. {
  926. $encoding = strtolower(Configure::read('App.encoding'));
  927. $result = $this->Form->create($this->article, ['type' => 'get']);
  928. $expected = ['form' => [
  929. 'method' => 'get', 'action' => '/articles/add',
  930. 'accept-charset' => $encoding
  931. ]];
  932. $this->assertHtml($expected, $result);
  933. $result = $this->Form->text('title');
  934. $expected = ['input' => [
  935. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  936. ]];
  937. $this->assertHtml($expected, $result);
  938. $result = $this->Form->password('password');
  939. $expected = ['input' => [
  940. 'name' => 'password', 'type' => 'password'
  941. ]];
  942. $this->assertHtml($expected, $result);
  943. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  944. $result = $this->Form->text('user_form');
  945. $expected = ['input' => [
  946. 'name' => 'user_form', 'type' => 'text'
  947. ]];
  948. $this->assertHtml($expected, $result);
  949. }
  950. /**
  951. * test get form, and inputs when the model param is false
  952. *
  953. * @return void
  954. */
  955. public function testGetFormWithFalseModel()
  956. {
  957. $encoding = strtolower(Configure::read('App.encoding'));
  958. $this->Form->request->params['controller'] = 'contact_test';
  959. $result = $this->Form->create(false, [
  960. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  961. ]);
  962. $expected = ['form' => [
  963. 'method' => 'get', 'action' => '/contact_test/add',
  964. 'accept-charset' => $encoding
  965. ]];
  966. $this->assertHtml($expected, $result);
  967. $result = $this->Form->text('reason');
  968. $expected = [
  969. 'input' => ['type' => 'text', 'name' => 'reason']
  970. ];
  971. $this->assertHtml($expected, $result);
  972. }
  973. /**
  974. * testFormCreateWithSecurity method
  975. *
  976. * Test form->create() with security key.
  977. *
  978. * @return void
  979. */
  980. public function testCreateWithSecurity()
  981. {
  982. $this->Form->request->params['_csrfToken'] = 'testKey';
  983. $encoding = strtolower(Configure::read('App.encoding'));
  984. $result = $this->Form->create($this->article, [
  985. 'url' => '/articles/publish',
  986. ]);
  987. $expected = [
  988. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  989. 'div' => ['style' => 'display:none;'],
  990. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  991. ['input' => [
  992. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  993. ]],
  994. '/div'
  995. ];
  996. $this->assertHtml($expected, $result);
  997. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  998. $expected['form']['id'] = 'MyForm';
  999. $this->assertHtml($expected, $result);
  1000. }
  1001. /**
  1002. * testFormCreateGetNoSecurity method
  1003. *
  1004. * Test form->create() with no security key as its a get form
  1005. *
  1006. * @return void
  1007. */
  1008. public function testCreateEndGetNoSecurity()
  1009. {
  1010. $this->Form->request->params['_csrfToken'] = 'testKey';
  1011. $article = new Article();
  1012. $result = $this->Form->create($article, [
  1013. 'type' => 'get',
  1014. 'url' => '/contacts/add'
  1015. ]);
  1016. $this->assertNotContains('testKey', $result);
  1017. $result = $this->Form->end();
  1018. $this->assertNotContains('testKey', $result);
  1019. }
  1020. /**
  1021. * test that create() clears the fields property so it starts fresh
  1022. *
  1023. * @return void
  1024. */
  1025. public function testCreateClearingFields()
  1026. {
  1027. $this->Form->fields = ['model_id'];
  1028. $this->Form->create($this->article);
  1029. $this->assertEquals([], $this->Form->fields);
  1030. }
  1031. /**
  1032. * Tests form hash generation with model-less data
  1033. *
  1034. * @return void
  1035. */
  1036. public function testValidateHashNoModel()
  1037. {
  1038. $this->Form->request->params['_Token'] = 'foo';
  1039. $result = $this->Form->secure(['anything']);
  1040. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1041. }
  1042. /**
  1043. * Tests that hidden fields generated for checkboxes don't get locked
  1044. *
  1045. * @return void
  1046. */
  1047. public function testNoCheckboxLocking()
  1048. {
  1049. $this->Form->request->params['_Token'] = 'foo';
  1050. $this->assertSame([], $this->Form->fields);
  1051. $this->Form->checkbox('check', ['value' => '1']);
  1052. $this->assertSame($this->Form->fields, ['check']);
  1053. }
  1054. /**
  1055. * testFormSecurityFields method
  1056. *
  1057. * Test generation of secure form hash generation.
  1058. *
  1059. * @return void
  1060. */
  1061. public function testFormSecurityFields()
  1062. {
  1063. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1064. $this->Form->request->params['_Token'] = 'testKey';
  1065. $result = $this->Form->secure($fields);
  1066. $hash = Security::hash(serialize($fields) . Security::salt());
  1067. $hash .= ':' . 'Model.valid';
  1068. $hash = urlencode($hash);
  1069. $tokenDebug = urlencode(json_encode([
  1070. '',
  1071. $fields,
  1072. []
  1073. ]));
  1074. $expected = [
  1075. 'div' => ['style' => 'display:none;'],
  1076. ['input' => [
  1077. 'type' => 'hidden',
  1078. 'name' => '_Token[fields]',
  1079. 'value' => $hash
  1080. ]],
  1081. ['input' => [
  1082. 'type' => 'hidden',
  1083. 'name' => '_Token[unlocked]',
  1084. 'value' => '',
  1085. ]],
  1086. ['input' => [
  1087. 'type' => 'hidden',
  1088. 'name' => '_Token[debug]',
  1089. 'value' => $tokenDebug
  1090. ]],
  1091. '/div'
  1092. ];
  1093. $this->assertHtml($expected, $result);
  1094. }
  1095. /**
  1096. * testFormSecurityFields method
  1097. *
  1098. * Test debug token is not generated if debug is false
  1099. *
  1100. * @return void
  1101. */
  1102. public function testFormSecurityFieldsNoDebugMode()
  1103. {
  1104. Configure::write('debug', false);
  1105. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1106. $this->Form->request->params['_Token'] = 'testKey';
  1107. $result = $this->Form->secure($fields);
  1108. $hash = Security::hash(serialize($fields) . Security::salt());
  1109. $hash .= ':' . 'Model.valid';
  1110. $hash = urlencode($hash);
  1111. $expected = [
  1112. 'div' => ['style' => 'display:none;'],
  1113. ['input' => [
  1114. 'type' => 'hidden',
  1115. 'name' => '_Token[fields]',
  1116. 'value' => $hash
  1117. ]],
  1118. ['input' => [
  1119. 'type' => 'hidden',
  1120. 'name' => '_Token[unlocked]',
  1121. 'value' => '',
  1122. ]],
  1123. '/div'
  1124. ];
  1125. $this->assertHtml($expected, $result);
  1126. }
  1127. /**
  1128. * Tests correct generation of number fields for double and float fields
  1129. *
  1130. * @return void
  1131. */
  1132. public function testTextFieldGenerationForFloats()
  1133. {
  1134. $this->article['schema'] = [
  1135. 'foo' => [
  1136. 'type' => 'float',
  1137. 'null' => false,
  1138. 'default' => null,
  1139. 'length' => 10
  1140. ]
  1141. ];
  1142. $this->Form->create($this->article);
  1143. $result = $this->Form->control('foo');
  1144. $expected = [
  1145. 'div' => ['class' => 'input number'],
  1146. 'label' => ['for' => 'foo'],
  1147. 'Foo',
  1148. '/label',
  1149. ['input' => [
  1150. 'type' => 'number',
  1151. 'name' => 'foo',
  1152. 'id' => 'foo',
  1153. 'step' => 'any'
  1154. ]],
  1155. '/div'
  1156. ];
  1157. $this->assertHtml($expected, $result);
  1158. $result = $this->Form->control('foo', ['step' => 0.5]);
  1159. $expected = [
  1160. 'div' => ['class' => 'input number'],
  1161. 'label' => ['for' => 'foo'],
  1162. 'Foo',
  1163. '/label',
  1164. ['input' => [
  1165. 'type' => 'number',
  1166. 'name' => 'foo',
  1167. 'id' => 'foo',
  1168. 'step' => '0.5'
  1169. ]],
  1170. '/div'
  1171. ];
  1172. $this->assertHtml($expected, $result);
  1173. }
  1174. /**
  1175. * Tests correct generation of number fields for integer fields
  1176. *
  1177. * @return void
  1178. */
  1179. public function testTextFieldTypeNumberGenerationForIntegers()
  1180. {
  1181. TableRegistry::get('Contacts', [
  1182. 'className' => __NAMESPACE__ . '\ContactsTable'
  1183. ]);
  1184. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1185. $result = $this->Form->control('age');
  1186. $expected = [
  1187. 'div' => ['class' => 'input number'],
  1188. 'label' => ['for' => 'age'],
  1189. 'Age',
  1190. '/label',
  1191. ['input' => [
  1192. 'type' => 'number', 'name' => 'age',
  1193. 'id' => 'age'
  1194. ]],
  1195. '/div'
  1196. ];
  1197. $this->assertHtml($expected, $result);
  1198. }
  1199. /**
  1200. * Tests correct generation of file upload fields for binary fields
  1201. *
  1202. * @return void
  1203. */
  1204. public function testFileUploadFieldTypeGenerationForBinaries()
  1205. {
  1206. $table = TableRegistry::get('Contacts', [
  1207. 'className' => __NAMESPACE__ . '\ContactsTable'
  1208. ]);
  1209. $table->schema(['foo' => [
  1210. 'type' => 'binary',
  1211. 'null' => false,
  1212. 'default' => null,
  1213. 'length' => 1024
  1214. ]]);
  1215. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1216. $result = $this->Form->control('foo');
  1217. $expected = [
  1218. 'div' => ['class' => 'input file'],
  1219. 'label' => ['for' => 'foo'],
  1220. 'Foo',
  1221. '/label',
  1222. ['input' => [
  1223. 'type' => 'file', 'name' => 'foo',
  1224. 'id' => 'foo'
  1225. ]],
  1226. '/div'
  1227. ];
  1228. $this->assertHtml($expected, $result);
  1229. }
  1230. /**
  1231. * testFormSecurityMultipleFields method
  1232. *
  1233. * Test secure() with multiple row form. Ensure hash is correct.
  1234. *
  1235. * @return void
  1236. */
  1237. public function testFormSecurityMultipleFields()
  1238. {
  1239. $this->Form->request->params['_Token'] = 'foo';
  1240. $fields = [
  1241. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1242. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1243. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1244. ];
  1245. $result = $this->Form->secure($fields);
  1246. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1247. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1248. $tokenDebug = urlencode(json_encode([
  1249. '',
  1250. $fields,
  1251. []
  1252. ]));
  1253. $expected = [
  1254. 'div' => ['style' => 'display:none;'],
  1255. ['input' => [
  1256. 'type' => 'hidden', 'name' => '_Token[fields]',
  1257. 'value' => $hash
  1258. ]],
  1259. ['input' => [
  1260. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1261. 'value' => ''
  1262. ]],
  1263. ['input' => [
  1264. 'type' => 'hidden', 'name' => '_Token[debug]',
  1265. 'value' => $tokenDebug,
  1266. ]],
  1267. '/div'
  1268. ];
  1269. $this->assertHtml($expected, $result);
  1270. }
  1271. /**
  1272. * testFormSecurityMultipleSubmitButtons
  1273. *
  1274. * test form submit generation and ensure that _Token is only created on end()
  1275. *
  1276. * @return void
  1277. */
  1278. public function testFormSecurityMultipleSubmitButtons()
  1279. {
  1280. $this->Form->request->params['_Token'] = 'testKey';
  1281. $this->Form->create($this->article);
  1282. $this->Form->text('Address.title');
  1283. $this->Form->text('Address.first_name');
  1284. $result = $this->Form->submit('Save', ['name' => 'save']);
  1285. $expected = [
  1286. 'div' => ['class' => 'submit'],
  1287. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1288. '/div',
  1289. ];
  1290. $this->assertHtml($expected, $result);
  1291. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1292. $expected = [
  1293. 'div' => ['class' => 'submit'],
  1294. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1295. '/div',
  1296. ];
  1297. $this->assertHtml($expected, $result);
  1298. $result = $this->Form->end();
  1299. $tokenDebug = urlencode(json_encode([
  1300. '/articles/add',
  1301. [
  1302. 'Address.title',
  1303. 'Address.first_name',
  1304. ],
  1305. ['save', 'cancel']
  1306. ]));
  1307. $expected = [
  1308. 'div' => ['style' => 'display:none;'],
  1309. ['input' => [
  1310. 'type' => 'hidden',
  1311. 'name' => '_Token[fields]',
  1312. 'value'
  1313. ]],
  1314. ['input' => [
  1315. 'type' => 'hidden',
  1316. 'name' => '_Token[unlocked]',
  1317. 'value' => 'cancel%7Csave'
  1318. ]],
  1319. ['input' => [
  1320. 'type' => 'hidden', 'name' => '_Token[debug]',
  1321. 'value' => $tokenDebug,
  1322. ]],
  1323. '/div'
  1324. ];
  1325. $this->assertHtml($expected, $result);
  1326. }
  1327. /**
  1328. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1329. *
  1330. * @return void
  1331. */
  1332. public function testSecurityButtonNestedNamed()
  1333. {
  1334. $key = 'testKey';
  1335. $this->Form->request->params['_csrfToken'] = $key;
  1336. $this->Form->create('Addresses');
  1337. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1338. $result = $this->Form->unlockField();
  1339. $this->assertEquals(['Address.button'], $result);
  1340. }
  1341. /**
  1342. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1343. *
  1344. * @return void
  1345. */
  1346. public function testSecuritySubmitNestedNamed()
  1347. {
  1348. $this->Form->request->params['_Token'] = 'testKey';
  1349. $this->Form->create($this->article);
  1350. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1351. $result = $this->Form->unlockField();
  1352. $this->assertEquals(['Address.button'], $result);
  1353. }
  1354. /**
  1355. * Test that the correct fields are unlocked for image submits with no names.
  1356. *
  1357. * @return void
  1358. */
  1359. public function testSecuritySubmitImageNoName()
  1360. {
  1361. $this->Form->request->params['_Token'] = 'testKey';
  1362. $this->Form->create(false);
  1363. $result = $this->Form->submit('save.png');
  1364. $expected = [
  1365. 'div' => ['class' => 'submit'],
  1366. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1367. '/div'
  1368. ];
  1369. $this->assertHtml($expected, $result);
  1370. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1371. }
  1372. /**
  1373. * Test that the correct fields are unlocked for image submits with names.
  1374. *
  1375. * @return void
  1376. */
  1377. public function testSecuritySubmitImageName()
  1378. {
  1379. $this->Form->request->params['_Token'] = 'testKey';
  1380. $this->Form->create(null);
  1381. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1382. $expected = [
  1383. 'div' => ['class' => 'submit'],
  1384. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1385. '/div'
  1386. ];
  1387. $this->assertHtml($expected, $result);
  1388. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1389. }
  1390. /**
  1391. * testFormSecurityMultipleControlFields method
  1392. *
  1393. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1394. *
  1395. * @return void
  1396. */
  1397. public function testFormSecurityMultipleControlFields()
  1398. {
  1399. $this->Form->request->params['_Token'] = 'testKey';
  1400. $this->Form->create();
  1401. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1402. $this->Form->control('Addresses.0.title');
  1403. $this->Form->control('Addresses.0.first_name');
  1404. $this->Form->control('Addresses.0.last_name');
  1405. $this->Form->control('Addresses.0.address');
  1406. $this->Form->control('Addresses.0.city');
  1407. $this->Form->control('Addresses.0.phone');
  1408. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1409. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1410. $this->Form->control('Addresses.1.title');
  1411. $this->Form->control('Addresses.1.first_name');
  1412. $this->Form->control('Addresses.1.last_name');
  1413. $this->Form->control('Addresses.1.address');
  1414. $this->Form->control('Addresses.1.city');
  1415. $this->Form->control('Addresses.1.phone');
  1416. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1417. $result = $this->Form->secure($this->Form->fields);
  1418. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1419. $tokenDebug = urlencode(json_encode([
  1420. '/articles/add',
  1421. [
  1422. 'Addresses.0.id' => '123456',
  1423. 'Addresses.0.title',
  1424. 'Addresses.0.first_name',
  1425. 'Addresses.0.last_name',
  1426. 'Addresses.0.address',
  1427. 'Addresses.0.city',
  1428. 'Addresses.0.phone',
  1429. 'Addresses.0.primary',
  1430. 'Addresses.1.id' => '654321',
  1431. 'Addresses.1.title',
  1432. 'Addresses.1.first_name',
  1433. 'Addresses.1.last_name',
  1434. 'Addresses.1.address',
  1435. 'Addresses.1.city',
  1436. 'Addresses.1.phone',
  1437. 'Addresses.1.primary',
  1438. ],
  1439. []
  1440. ]));
  1441. $expected = [
  1442. 'div' => ['style' => 'display:none;'],
  1443. ['input' => [
  1444. 'type' => 'hidden', 'name' => '_Token[fields]',
  1445. 'value' => $hash
  1446. ]],
  1447. ['input' => [
  1448. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1449. 'value' => ''
  1450. ]],
  1451. ['input' => [
  1452. 'type' => 'hidden', 'name' => '_Token[debug]',
  1453. 'value' => $tokenDebug,
  1454. ]],
  1455. '/div'
  1456. ];
  1457. $this->assertHtml($expected, $result);
  1458. }
  1459. /**
  1460. * testFormSecurityArrayFields method
  1461. *
  1462. * Test form security with Model.field.0 style inputs.
  1463. *
  1464. * @return void
  1465. */
  1466. public function testFormSecurityArrayFields()
  1467. {
  1468. $this->Form->request->params['_Token'] = 'testKey';
  1469. $this->Form->create();
  1470. $this->Form->text('Address.primary.1');
  1471. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1472. $this->Form->text('Address.secondary.1.0');
  1473. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1474. }
  1475. /**
  1476. * testFormSecurityMultipleControlDisabledFields method
  1477. *
  1478. * Test secure form generation with multiple records and disabled fields.
  1479. *
  1480. * @return void
  1481. */
  1482. public function testFormSecurityMultipleControlDisabledFields()
  1483. {
  1484. $this->Form->request->params['_Token'] = [
  1485. 'unlockedFields' => ['first_name', 'address']
  1486. ];
  1487. $this->Form->create();
  1488. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1489. $this->Form->text('Addresses.0.title');
  1490. $this->Form->text('Addresses.0.first_name');
  1491. $this->Form->text('Addresses.0.last_name');
  1492. $this->Form->text('Addresses.0.address');
  1493. $this->Form->text('Addresses.0.city');
  1494. $this->Form->text('Addresses.0.phone');
  1495. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1496. $this->Form->text('Addresses.1.title');
  1497. $this->Form->text('Addresses.1.first_name');
  1498. $this->Form->text('Addresses.1.last_name');
  1499. $this->Form->text('Addresses.1.address');
  1500. $this->Form->text('Addresses.1.city');
  1501. $this->Form->text('Addresses.1.phone');
  1502. $result = $this->Form->secure($this->Form->fields);
  1503. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1504. $tokenDebug = urlencode(json_encode([
  1505. '/articles/add',
  1506. [
  1507. 'Addresses.0.id' => '123456',
  1508. 'Addresses.0.title',
  1509. 'Addresses.0.last_name',
  1510. 'Addresses.0.city',
  1511. 'Addresses.0.phone',
  1512. 'Addresses.1.id' => '654321',
  1513. 'Addresses.1.title',
  1514. 'Addresses.1.last_name',
  1515. 'Addresses.1.city',
  1516. 'Addresses.1.phone'
  1517. ],
  1518. [
  1519. 'first_name',
  1520. 'address'
  1521. ]
  1522. ]));
  1523. $expected = [
  1524. 'div' => ['style' => 'display:none;'],
  1525. ['input' => [
  1526. 'type' => 'hidden',
  1527. 'name' => '_Token[fields]',
  1528. 'value' => $hash
  1529. ]],
  1530. ['input' => [
  1531. 'type' => 'hidden',
  1532. 'name' => '_Token[unlocked]',
  1533. 'value' => 'address%7Cfirst_name',
  1534. ]],
  1535. ['input' => [
  1536. 'type' => 'hidden', 'name' => '_Token[debug]',
  1537. 'value' => $tokenDebug,
  1538. ]],
  1539. '/div'
  1540. ];
  1541. $this->assertHtml($expected, $result);
  1542. }
  1543. /**
  1544. * testFormSecurityControlDisabledFields method
  1545. *
  1546. * Test single record form with disabled fields.
  1547. *
  1548. * @return void
  1549. */
  1550. public function testFormSecurityControlUnlockedFields()
  1551. {
  1552. $this->Form->request->params['_Token'] = [
  1553. 'unlockedFields' => ['first_name', 'address']
  1554. ];
  1555. $this->Form->create();
  1556. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1557. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1558. $this->Form->text('Addresses.title');
  1559. $this->Form->text('Addresses.first_name');
  1560. $this->Form->text('Addresses.last_name');
  1561. $this->Form->text('Addresses.address');
  1562. $this->Form->text('Addresses.city');
  1563. $this->Form->text('Addresses.phone');
  1564. $result = $this->Form->fields;
  1565. $expected = [
  1566. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1567. 'Addresses.city', 'Addresses.phone'
  1568. ];
  1569. $this->assertEquals($expected, $result);
  1570. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1571. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1572. $tokenDebug = urlencode(json_encode([
  1573. '/articles/add',
  1574. [
  1575. 'Addresses.id' => '123456',
  1576. 'Addresses.title',
  1577. 'Addresses.last_name',
  1578. 'Addresses.city',
  1579. 'Addresses.phone'
  1580. ],
  1581. [
  1582. 'first_name',
  1583. 'address'
  1584. ]
  1585. ]));
  1586. $expected = [
  1587. 'div' => ['style' => 'display:none;'],
  1588. ['input' => [
  1589. 'type' => 'hidden',
  1590. 'name' => '_Token[fields]',
  1591. 'value' => $hash,
  1592. 'data-foo' => 'bar',
  1593. ]],
  1594. ['input' => [
  1595. 'type' => 'hidden',
  1596. 'name' => '_Token[unlocked]',
  1597. 'value' => 'address%7Cfirst_name',
  1598. 'data-foo' => 'bar',
  1599. ]],
  1600. ['input' => [
  1601. 'type' => 'hidden', 'name' => '_Token[debug]',
  1602. 'value' => $tokenDebug,
  1603. 'data-foo' => 'bar'
  1604. ]],
  1605. '/div'
  1606. ];
  1607. $this->assertHtml($expected, $result);
  1608. }
  1609. /**
  1610. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1611. *
  1612. * Test single record form with debugSecurity param.
  1613. *
  1614. * @return void
  1615. */
  1616. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1617. {
  1618. $this->Form->request->params['_Token'] = [
  1619. 'unlockedFields' => ['first_name', 'address']
  1620. ];
  1621. $this->Form->create();
  1622. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1623. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1624. $this->Form->text('Addresses.title');
  1625. $this->Form->text('Addresses.first_name');
  1626. $this->Form->text('Addresses.last_name');
  1627. $this->Form->text('Addresses.address');
  1628. $this->Form->text('Addresses.city');
  1629. $this->Form->text('Addresses.phone');
  1630. $result = $this->Form->fields;
  1631. $expected = [
  1632. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1633. 'Addresses.city', 'Addresses.phone'
  1634. ];
  1635. $this->assertEquals($expected, $result);
  1636. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1637. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1638. $tokenDebug = urlencode(json_encode([
  1639. '/articles/add',
  1640. [
  1641. 'Addresses.id' => '123456',
  1642. 'Addresses.title',
  1643. 'Addresses.last_name',
  1644. 'Addresses.city',
  1645. 'Addresses.phone'
  1646. ],
  1647. [
  1648. 'first_name',
  1649. 'address'
  1650. ]
  1651. ]));
  1652. $expected = [
  1653. 'div' => ['style' => 'display:none;'],
  1654. ['input' => [
  1655. 'type' => 'hidden',
  1656. 'name' => '_Token[fields]',
  1657. 'value' => $hash,
  1658. 'data-foo' => 'bar',
  1659. ]],
  1660. ['input' => [
  1661. 'type' => 'hidden',
  1662. 'name' => '_Token[unlocked]',
  1663. 'value' => 'address%7Cfirst_name',
  1664. 'data-foo' => 'bar',
  1665. ]],
  1666. ['input' => [
  1667. 'type' => 'hidden', 'name' => '_Token[debug]',
  1668. 'value' => $tokenDebug,
  1669. 'data-foo' => 'bar'
  1670. ]],
  1671. '/div'
  1672. ];
  1673. $this->assertHtml($expected, $result);
  1674. }
  1675. /**
  1676. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1677. *
  1678. * Debug is false, debugSecurity is true -> no debug
  1679. *
  1680. * @return void
  1681. */
  1682. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1683. {
  1684. $this->Form->request->params['_Token'] = [
  1685. 'unlockedFields' => ['first_name', 'address']
  1686. ];
  1687. $this->Form->create();
  1688. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1689. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1690. $this->Form->text('Addresses.title');
  1691. $this->Form->text('Addresses.first_name');
  1692. $this->Form->text('Addresses.last_name');
  1693. $this->Form->text('Addresses.address');
  1694. $this->Form->text('Addresses.city');
  1695. $this->Form->text('Addresses.phone');
  1696. $result = $this->Form->fields;
  1697. $expected = [
  1698. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1699. 'Addresses.city', 'Addresses.phone'
  1700. ];
  1701. $this->assertEquals($expected, $result);
  1702. Configure::write('debug', false);
  1703. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1704. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1705. $expected = [
  1706. 'div' => ['style' => 'display:none;'],
  1707. ['input' => [
  1708. 'type' => 'hidden',
  1709. 'name' => '_Token[fields]',
  1710. 'value' => $hash,
  1711. 'data-foo' => 'bar',
  1712. ]],
  1713. ['input' => [
  1714. 'type' => 'hidden',
  1715. 'name' => '_Token[unlocked]',
  1716. 'value' => 'address%7Cfirst_name',
  1717. 'data-foo' => 'bar',
  1718. ]],
  1719. '/div'
  1720. ];
  1721. $this->assertHtml($expected, $result);
  1722. }
  1723. /**
  1724. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1725. *
  1726. * Test single record form with debugSecurity param.
  1727. *
  1728. * @return void
  1729. */
  1730. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1731. {
  1732. $this->Form->request->params['_Token'] = [
  1733. 'unlockedFields' => ['first_name', 'address']
  1734. ];
  1735. $this->Form->create();
  1736. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1737. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1738. $this->Form->text('Addresses.title');
  1739. $this->Form->text('Addresses.first_name');
  1740. $this->Form->text('Addresses.last_name');
  1741. $this->Form->text('Addresses.address');
  1742. $this->Form->text('Addresses.city');
  1743. $this->Form->text('Addresses.phone');
  1744. $result = $this->Form->fields;
  1745. $expected = [
  1746. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1747. 'Addresses.city', 'Addresses.phone'
  1748. ];
  1749. $this->assertEquals($expected, $result);
  1750. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1751. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1752. $expected = [
  1753. 'div' => ['style' => 'display:none;'],
  1754. ['input' => [
  1755. 'type' => 'hidden',
  1756. 'name' => '_Token[fields]',
  1757. 'value' => $hash,
  1758. 'data-foo' => 'bar',
  1759. ]],
  1760. ['input' => [
  1761. 'type' => 'hidden',
  1762. 'name' => '_Token[unlocked]',
  1763. 'value' => 'address%7Cfirst_name',
  1764. 'data-foo' => 'bar',
  1765. ]],
  1766. '/div'
  1767. ];
  1768. $this->assertHtml($expected, $result);
  1769. }
  1770. /**
  1771. * testFormSecureWithCustomNameAttribute method
  1772. *
  1773. * Test securing inputs with custom name attributes.
  1774. *
  1775. * @return void
  1776. */
  1777. public function testFormSecureWithCustomNameAttribute()
  1778. {
  1779. $this->Form->request->params['_Token'] = 'testKey';
  1780. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1781. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1782. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1783. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1784. }
  1785. /**
  1786. * testFormSecuredControl method
  1787. *
  1788. * Test generation of entire secure form, assertions made on control() output.
  1789. *
  1790. * @return void
  1791. */
  1792. public function testFormSecuredControl()
  1793. {
  1794. $this->Form->request->params['_csrfToken'] = 'testKey';
  1795. $this->Form->request->params['_Token'] = 'stuff';
  1796. $this->article['schema'] = [
  1797. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1798. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1799. ];
  1800. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1801. $encoding = strtolower(Configure::read('App.encoding'));
  1802. $expected = [
  1803. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1804. 'div' => ['style' => 'display:none;'],
  1805. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1806. ['input' => [
  1807. 'type' => 'hidden',
  1808. 'name' => '_csrfToken',
  1809. 'value' => 'testKey'
  1810. ]],
  1811. '/div'
  1812. ];
  1813. $this->assertHtml($expected, $result);
  1814. $result = $this->Form->control('ratio');
  1815. $expected = [
  1816. 'div' => ['class'],
  1817. 'label' => ['for'],
  1818. 'Ratio',
  1819. '/label',
  1820. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1821. '/div',
  1822. ];
  1823. $this->assertHtml($expected, $result);
  1824. $result = $this->Form->control('population');
  1825. $expected = [
  1826. 'div' => ['class'],
  1827. 'label' => ['for'],
  1828. 'Population',
  1829. '/label',
  1830. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1831. '/div',
  1832. ];
  1833. $this->assertHtml($expected, $result);
  1834. $result = $this->Form->control('published', ['type' => 'text']);
  1835. $expected = [
  1836. 'div' => ['class' => 'input text'],
  1837. 'label' => ['for' => 'published'],
  1838. 'Published',
  1839. '/label',
  1840. ['input' => [
  1841. 'type' => 'text',
  1842. 'name' => 'published',
  1843. 'id' => 'published'
  1844. ]],
  1845. '/div'
  1846. ];
  1847. $this->assertHtml($expected, $result);
  1848. $result = $this->Form->control('other', ['type' => 'text']);
  1849. $expected = [
  1850. 'div' => ['class' => 'input text'],
  1851. 'label' => ['for' => 'other'],
  1852. 'Other',
  1853. '/label',
  1854. ['input' => [
  1855. 'type' => 'text',
  1856. 'name' => 'other',
  1857. 'id',
  1858. ]],
  1859. '/div'
  1860. ];
  1861. $this->assertHtml($expected, $result);
  1862. $result = $this->Form->hidden('stuff');
  1863. $expected = [
  1864. 'input' => [
  1865. 'type' => 'hidden',
  1866. 'name' => 'stuff'
  1867. ]
  1868. ];
  1869. $this->assertHtml($expected, $result);
  1870. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1871. $expected = ['input' => [
  1872. 'type' => 'hidden',
  1873. 'name' => 'hidden',
  1874. 'value' => '0'
  1875. ]];
  1876. $this->assertHtml($expected, $result);
  1877. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1878. $expected = [
  1879. 'div' => ['class' => 'input checkbox'],
  1880. ['input' => [
  1881. 'type' => 'hidden',
  1882. 'name' => 'something',
  1883. 'value' => '0'
  1884. ]],
  1885. 'label' => ['for' => 'something'],
  1886. ['input' => [
  1887. 'type' => 'checkbox',
  1888. 'name' => 'something',
  1889. 'value' => '1',
  1890. 'id' => 'something'
  1891. ]],
  1892. 'Something',
  1893. '/label',
  1894. '/div'
  1895. ];
  1896. $this->assertHtml($expected, $result);
  1897. $result = $this->Form->fields;
  1898. $expectedFields = [
  1899. 'ratio',
  1900. 'population',
  1901. 'published',
  1902. 'other',
  1903. 'stuff' => '',
  1904. 'hidden' => '0',
  1905. 'something'
  1906. ];
  1907. $this->assertEquals($expectedFields, $result);
  1908. $result = $this->Form->secure($this->Form->fields);
  1909. $tokenDebug = urlencode(json_encode([
  1910. '/articles/add',
  1911. $expectedFields,
  1912. []
  1913. ]));
  1914. $expected = [
  1915. 'div' => ['style' => 'display:none;'],
  1916. ['input' => [
  1917. 'type' => 'hidden',
  1918. 'name' => '_Token[fields]',
  1919. 'value'
  1920. ]],
  1921. ['input' => [
  1922. 'type' => 'hidden',
  1923. 'name' => '_Token[unlocked]',
  1924. 'value' => ''
  1925. ]],
  1926. ['input' => [
  1927. 'type' => 'hidden', 'name' => '_Token[debug]',
  1928. 'value' => $tokenDebug,
  1929. ]],
  1930. '/div'
  1931. ];
  1932. $this->assertHtml($expected, $result);
  1933. }
  1934. /**
  1935. * testSecuredControlCustomName method
  1936. *
  1937. * Test secured inputs with custom names.
  1938. *
  1939. * @return void
  1940. */
  1941. public function testSecuredControlCustomName()
  1942. {
  1943. $this->Form->request->params['_Token'] = 'testKey';
  1944. $this->assertEquals([], $this->Form->fields);
  1945. $this->Form->text('text_input', [
  1946. 'name' => 'Option[General.default_role]',
  1947. ]);
  1948. $expected = ['Option.General.default_role'];
  1949. $this->assertEquals($expected, $this->Form->fields);
  1950. $this->Form->select('select_box', [1, 2], [
  1951. 'name' => 'Option[General.select_role]',
  1952. ]);
  1953. $expected[] = 'Option.General.select_role';
  1954. $this->assertEquals($expected, $this->Form->fields);
  1955. $this->Form->text('other.things[]');
  1956. $expected[] = 'other.things';
  1957. $this->assertEquals($expected, $this->Form->fields);
  1958. }
  1959. /**
  1960. * testSecuredControlDuplicate method
  1961. *
  1962. * Test that a hidden field followed by a visible field
  1963. * undoes the hidden field locking.
  1964. *
  1965. * @return void
  1966. */
  1967. public function testSecuredControlDuplicate()
  1968. {
  1969. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  1970. $this->assertEquals([], $this->Form->fields);
  1971. $this->Form->control('text_val', [
  1972. 'type' => 'hidden',
  1973. 'value' => 'some text',
  1974. ]);
  1975. $expected = ['text_val' => 'some text'];
  1976. $this->assertEquals($expected, $this->Form->fields);
  1977. $this->Form->control('text_val', [
  1978. 'type' => 'text',
  1979. ]);
  1980. $expected = ['text_val'];
  1981. $this->assertEquals($expected, $this->Form->fields);
  1982. }
  1983. /**
  1984. * testFormSecuredFileControl method
  1985. *
  1986. * Tests that the correct keys are added to the field hash index.
  1987. *
  1988. * @return void
  1989. */
  1990. public function testFormSecuredFileControl()
  1991. {
  1992. $this->assertEquals([], $this->Form->fields);
  1993. $this->Form->file('Attachment.file');
  1994. $expected = [
  1995. 'Attachment.file.name', 'Attachment.file.type',
  1996. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1997. 'Attachment.file.size'
  1998. ];
  1999. $this->assertEquals($expected, $this->Form->fields);
  2000. }
  2001. /**
  2002. * testFormSecuredMultipleSelect method
  2003. *
  2004. * Test that multiple selects keys are added to field hash.
  2005. *
  2006. * @return void
  2007. */
  2008. public function testFormSecuredMultipleSelect()
  2009. {
  2010. $this->Form->request->params['_csrfToken'] = 'testKey';
  2011. $this->assertEquals([], $this->Form->fields);
  2012. $options = ['1' => 'one', '2' => 'two'];
  2013. $this->Form->select('Model.select', $options);
  2014. $expected = ['Model.select'];
  2015. $this->assertEquals($expected, $this->Form->fields);
  2016. $this->Form->fields = [];
  2017. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2018. $this->assertEquals($expected, $this->Form->fields);
  2019. }
  2020. /**
  2021. * testFormSecuredRadio method
  2022. *
  2023. * @return void
  2024. */
  2025. public function testFormSecuredRadio()
  2026. {
  2027. $this->Form->request->params['_Token'] = 'testKey';
  2028. $this->assertEquals([], $this->Form->fields);
  2029. $options = ['1' => 'option1', '2' => 'option2'];
  2030. $this->Form->radio('Test.test', $options);
  2031. $expected = ['Test.test'];
  2032. $this->assertEquals($expected, $this->Form->fields);
  2033. $this->Form->radio('Test.all', $options, [
  2034. 'disabled' => ['option1', 'option2']
  2035. ]);
  2036. $expected = ['Test.test', 'Test.all' => ''];
  2037. $this->assertEquals($expected, $this->Form->fields);
  2038. $this->Form->radio('Test.some', $options, [
  2039. 'disabled' => ['option1']
  2040. ]);
  2041. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2042. $this->assertEquals($expected, $this->Form->fields);
  2043. }
  2044. /**
  2045. * testFormSecuredAndDisabledNotAssoc method
  2046. *
  2047. * Test that when disabled is in a list based attribute array it works.
  2048. *
  2049. * @return void
  2050. */
  2051. public function testFormSecuredAndDisabledNotAssoc()
  2052. {
  2053. $this->Form->request->params['_Token'] = 'testKey';
  2054. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2055. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2056. $this->Form->text('Model.text', ['disabled']);
  2057. $this->Form->textarea('Model.textarea', ['disabled']);
  2058. $this->Form->password('Model.password', ['disabled']);
  2059. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2060. $expected = [
  2061. 'Model.radio' => ''
  2062. ];
  2063. $this->assertEquals($expected, $this->Form->fields);
  2064. }
  2065. /**
  2066. * testFormSecuredAndDisabled method
  2067. *
  2068. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2069. * hashing.
  2070. *
  2071. * @return void
  2072. */
  2073. public function testFormSecuredAndDisabled()
  2074. {
  2075. $this->Form->request->params['_Token'] = 'testKey';
  2076. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2077. $this->Form->text('Model.text', ['disabled' => true]);
  2078. $this->Form->password('Model.text', ['disabled' => true]);
  2079. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2080. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2081. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2082. $this->Form->year('Model.year', ['disabled' => true]);
  2083. $this->Form->month('Model.month', ['disabled' => true]);
  2084. $this->Form->day('Model.day', ['disabled' => true]);
  2085. $this->Form->hour('Model.hour', ['disabled' => true]);
  2086. $this->Form->minute('Model.minute', ['disabled' => true]);
  2087. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2088. $expected = [
  2089. 'Model.radio' => ''
  2090. ];
  2091. $this->assertEquals($expected, $this->Form->fields);
  2092. }
  2093. /**
  2094. * testDisableSecurityUsingForm method
  2095. *
  2096. * @return void
  2097. */
  2098. public function testDisableSecurityUsingForm()
  2099. {
  2100. $this->Form->request->params['_Token'] = [
  2101. 'disabledFields' => []
  2102. ];
  2103. $this->Form->create();
  2104. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2105. $this->Form->text('Addresses.title');
  2106. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2107. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2108. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2109. $result = $this->Form->fields;
  2110. $expected = [
  2111. 'Addresses.id' => '123456', 'Addresses.title',
  2112. ];
  2113. $this->assertEquals($expected, $result);
  2114. }
  2115. /**
  2116. * testUnlockFieldAddsToList method
  2117. *
  2118. * Test disableField.
  2119. *
  2120. * @return void
  2121. */
  2122. public function testUnlockFieldAddsToList()
  2123. {
  2124. $this->Form->request->params['_Token'] = [
  2125. 'unlockedFields' => []
  2126. ];
  2127. $this->Form->unlockField('Contact.name');
  2128. $this->Form->text('Contact.name');
  2129. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2130. $this->assertEquals([], $this->Form->fields);
  2131. }
  2132. /**
  2133. * testUnlockFieldRemovingFromFields method
  2134. *
  2135. * Test unlockField removing from fields array.
  2136. *
  2137. * @return void
  2138. */
  2139. public function testUnlockFieldRemovingFromFields()
  2140. {
  2141. $this->Form->request->params['_Token'] = [
  2142. 'unlockedFields' => []
  2143. ];
  2144. $this->Form->create($this->article);
  2145. $this->Form->hidden('Article.id', ['value' => 1]);
  2146. $this->Form->text('Article.title');
  2147. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2148. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  2149. $this->Form->unlockField('Article.title');
  2150. $this->Form->unlockField('Article.id');
  2151. $this->assertEquals([], $this->Form->fields);
  2152. }
  2153. /**
  2154. * testResetUnlockFields method
  2155. *
  2156. * Test reset unlockFields, when create new form.
  2157. *
  2158. * @return void
  2159. */
  2160. public function testResetUnlockFields()
  2161. {
  2162. $this->Form->request->params['_Token'] = [
  2163. 'key' => 'testKey',
  2164. 'unlockedFields' => []
  2165. ];
  2166. $this->Form->unlockField('Contact.id');
  2167. $this->Form->create('Contact');
  2168. $this->Form->hidden('Contact.id', ['value' => 1]);
  2169. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2170. $this->Form->end();
  2171. $this->Form->create('Contact');
  2172. $this->Form->hidden('Contact.id', ['value' => 1]);
  2173. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2174. }
  2175. /**
  2176. * testSecuredFormUrlIgnoresHost method
  2177. *
  2178. * Test that only the path + query elements of a form's URL show up in their hash.
  2179. *
  2180. * @return void
  2181. */
  2182. public function testSecuredFormUrlIgnoresHost()
  2183. {
  2184. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2185. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  2186. $this->Form->create($this->article, [
  2187. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2188. ]);
  2189. $result = $this->Form->secure();
  2190. $this->assertContains($expected, $result);
  2191. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2192. $result = $this->Form->secure();
  2193. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2194. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2195. $result = $this->Form->secure();
  2196. $this->assertContains($expected, $result, 'URL path + query should work.');
  2197. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2198. $result = $this->Form->secure();
  2199. $this->assertNotContains($expected, $result, 'URL is different');
  2200. }
  2201. /**
  2202. * testSecuredFormUrlHasHtmlAndIdentifier method
  2203. *
  2204. * Test that URL, HTML and identifier show up in their hashes.
  2205. *
  2206. * @return void
  2207. */
  2208. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2209. {
  2210. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2211. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  2212. $this->Form->create($this->article, [
  2213. 'url' => [
  2214. 'controller' => 'articles',
  2215. 'action' => 'view',
  2216. '?' => [
  2217. 'page' => 1,
  2218. 'limit' => 10,
  2219. 'html' => '<>"',
  2220. ],
  2221. '#' => 'result',
  2222. ],
  2223. ]);
  2224. $result = $this->Form->secure();
  2225. $this->assertContains($expected, $result);
  2226. $this->Form->create($this->article, [
  2227. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2228. ]);
  2229. $result = $this->Form->secure();
  2230. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2231. $this->Form->create($this->article, [
  2232. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2233. ]);
  2234. $result = $this->Form->secure();
  2235. $this->assertContains($expected, $result, 'URL path + query should work.');
  2236. }
  2237. /**
  2238. * testErrorMessageDisplay method
  2239. *
  2240. * Test error message display.
  2241. *
  2242. * @return void
  2243. */
  2244. public function testErrorMessageDisplay()
  2245. {
  2246. $this->article['errors'] = [
  2247. 'Article' => [
  2248. 'title' => 'error message',
  2249. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2250. ]
  2251. ];
  2252. $this->Form->create($this->article);
  2253. $result = $this->Form->control('Article.title');
  2254. $expected = [
  2255. 'div' => ['class' => 'input text error'],
  2256. 'label' => ['for' => 'article-title'],
  2257. 'Title',
  2258. '/label',
  2259. 'input' => [
  2260. 'type' => 'text', 'name' => 'Article[title]',
  2261. 'id' => 'article-title', 'class' => 'form-error'
  2262. ],
  2263. ['div' => ['class' => 'error-message']],
  2264. 'error message',
  2265. '/div',
  2266. '/div'
  2267. ];
  2268. $this->assertHtml($expected, $result);
  2269. $result = $this->Form->control('Article.title', [
  2270. 'templates' => [
  2271. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2272. ]
  2273. ]);
  2274. $expected = [
  2275. 'div' => ['class' => 'input text error'],
  2276. 'label' => ['for' => 'article-title'],
  2277. 'Title',
  2278. '/label',
  2279. 'input' => [
  2280. 'type' => 'text', 'name' => 'Article[title]',
  2281. 'id' => 'article-title', 'class' => 'form-error'
  2282. ],
  2283. '/div'
  2284. ];
  2285. $this->assertHtml($expected, $result);
  2286. $result = $this->Form->control('Article.content');
  2287. $expected = [
  2288. 'div' => ['class' => 'input text error'],
  2289. 'label' => ['for' => 'article-content'],
  2290. 'Content',
  2291. '/label',
  2292. 'input' => [
  2293. 'type' => 'text', 'name' => 'Article[content]',
  2294. 'id' => 'article-content', 'class' => 'form-error'
  2295. ],
  2296. ['div' => ['class' => 'error-message']],
  2297. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2298. '/div',
  2299. '/div'
  2300. ];
  2301. $this->assertHtml($expected, $result);
  2302. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2303. $expected = [
  2304. 'div' => ['class' => 'input text error'],
  2305. 'label' => ['for' => 'article-content'],
  2306. 'Content',
  2307. '/label',
  2308. 'input' => [
  2309. 'type' => 'text', 'name' => 'Article[content]',
  2310. 'id' => 'article-content', 'class' => 'form-error'
  2311. ],
  2312. ['div' => ['class' => 'error-message']],
  2313. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2314. '/div',
  2315. '/div'
  2316. ];
  2317. $this->assertHtml($expected, $result);
  2318. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2319. $expected = [
  2320. 'div' => ['class' => 'input text error'],
  2321. 'label' => ['for' => 'article-content'],
  2322. 'Content',
  2323. '/label',
  2324. 'input' => [
  2325. 'type' => 'text', 'name' => 'Article[content]',
  2326. 'id' => 'article-content', 'class' => 'form-error'
  2327. ],
  2328. ['div' => ['class' => 'error-message']],
  2329. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2330. '/div',
  2331. '/div'
  2332. ];
  2333. $this->assertHtml($expected, $result);
  2334. }
  2335. /**
  2336. * testEmptyErrorValidation method
  2337. *
  2338. * Test validation errors, when validation message is an empty string.
  2339. *
  2340. * @return void
  2341. */
  2342. public function testEmptyErrorValidation()
  2343. {
  2344. $this->article['errors'] = [
  2345. 'Article' => ['title' => '']
  2346. ];
  2347. $this->Form->create($this->article);
  2348. $result = $this->Form->control('Article.title');
  2349. $expected = [
  2350. 'div' => ['class' => 'input text error'],
  2351. 'label' => ['for' => 'article-title'],
  2352. 'Title',
  2353. '/label',
  2354. 'input' => [
  2355. 'type' => 'text', 'name' => 'Article[title]',
  2356. 'id' => 'article-title', 'class' => 'form-error'
  2357. ],
  2358. ['div' => ['class' => 'error-message']],
  2359. [],
  2360. '/div',
  2361. '/div'
  2362. ];
  2363. $this->assertHtml($expected, $result);
  2364. }
  2365. /**
  2366. * testEmptyControlErrorValidation method
  2367. *
  2368. * Test validation errors, when calling control() overriding validation message by an empty string.
  2369. *
  2370. * @return void
  2371. */
  2372. public function testEmptyControlErrorValidation()
  2373. {
  2374. $this->article['errors'] = [
  2375. 'Article' => ['title' => 'error message']
  2376. ];
  2377. $this->Form->create($this->article);
  2378. $result = $this->Form->control('Article.title', ['error' => '']);
  2379. $expected = [
  2380. 'div' => ['class' => 'input text error'],
  2381. 'label' => ['for' => 'article-title'],
  2382. 'Title',
  2383. '/label',
  2384. 'input' => [
  2385. 'type' => 'text', 'name' => 'Article[title]',
  2386. 'id' => 'article-title', 'class' => 'form-error'
  2387. ],
  2388. ['div' => ['class' => 'error-message']],
  2389. [],
  2390. '/div',
  2391. '/div'
  2392. ];
  2393. $this->assertHtml($expected, $result);
  2394. }
  2395. /**
  2396. * testControlErrorMessage method
  2397. *
  2398. * Test validation errors, when calling control() overriding validation messages.
  2399. *
  2400. * @return void
  2401. */
  2402. public function testControlErrorMessage()
  2403. {
  2404. $this->article['errors'] = [
  2405. 'title' => ['error message']
  2406. ];
  2407. $this->Form->create($this->article);
  2408. $result = $this->Form->control('title', [
  2409. 'error' => 'Custom error!'
  2410. ]);
  2411. $expected = [
  2412. 'div' => ['class' => 'input text required error'],
  2413. 'label' => ['for' => 'title'],
  2414. 'Title',
  2415. '/label',
  2416. 'input' => [
  2417. 'type' => 'text', 'name' => 'title',
  2418. 'id' => 'title', 'class' => 'form-error',
  2419. 'required' => 'required',
  2420. ],
  2421. ['div' => ['class' => 'error-message']],
  2422. 'Custom error!',
  2423. '/div',
  2424. '/div'
  2425. ];
  2426. $this->assertHtml($expected, $result);
  2427. $result = $this->Form->control('title', [
  2428. 'error' => ['error message' => 'Custom error!']
  2429. ]);
  2430. $expected = [
  2431. 'div' => ['class' => 'input text required error'],
  2432. 'label' => ['for' => 'title'],
  2433. 'Title',
  2434. '/label',
  2435. 'input' => [
  2436. 'type' => 'text',
  2437. 'name' => 'title',
  2438. 'id' => 'title',
  2439. 'class' => 'form-error',
  2440. 'required' => 'required'
  2441. ],
  2442. ['div' => ['class' => 'error-message']],
  2443. 'Custom error!',
  2444. '/div',
  2445. '/div'
  2446. ];
  2447. $this->assertHtml($expected, $result);
  2448. }
  2449. /**
  2450. * testFormValidationAssociated method
  2451. *
  2452. * Tests displaying errors for nested entities.
  2453. *
  2454. * @return void
  2455. */
  2456. public function testFormValidationAssociated()
  2457. {
  2458. $nested = new Entity(['foo' => 'bar']);
  2459. $nested->errors('foo', ['not a valid bar']);
  2460. $entity = new Entity(['nested' => $nested]);
  2461. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2462. $result = $this->Form->error('nested.foo');
  2463. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2464. }
  2465. /**
  2466. * testFormValidationAssociatedSecondLevel method
  2467. *
  2468. * Test form error display with associated model.
  2469. *
  2470. * @return void
  2471. */
  2472. public function testFormValidationAssociatedSecondLevel()
  2473. {
  2474. $inner = new Entity(['bar' => 'baz']);
  2475. $nested = new Entity(['foo' => $inner]);
  2476. $entity = new Entity(['nested' => $nested]);
  2477. $inner->errors('bar', ['not a valid one']);
  2478. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2479. $result = $this->Form->error('nested.foo.bar');
  2480. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2481. }
  2482. /**
  2483. * testFormValidationMultiRecord method
  2484. *
  2485. * Test form error display with multiple records.
  2486. *
  2487. * @return void
  2488. */
  2489. public function testFormValidationMultiRecord()
  2490. {
  2491. $one = new Entity();
  2492. $two = new Entity();
  2493. TableRegistry::get('Contacts', [
  2494. 'className' => __NAMESPACE__ . '\ContactsTable'
  2495. ]);
  2496. $one->set('email', '');
  2497. $one->errors('email', ['invalid email']);
  2498. $two->set('name', '');
  2499. $two->errors('name', ['This is wrong']);
  2500. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2501. $result = $this->Form->control('0.email');
  2502. $expected = [
  2503. 'div' => ['class' => 'input email error'],
  2504. 'label' => ['for' => '0-email'],
  2505. 'Email',
  2506. '/label',
  2507. 'input' => [
  2508. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2509. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2510. ],
  2511. ['div' => ['class' => 'error-message']],
  2512. 'invalid email',
  2513. '/div',
  2514. '/div'
  2515. ];
  2516. $this->assertHtml($expected, $result);
  2517. $result = $this->Form->control('1.name');
  2518. $expected = [
  2519. 'div' => ['class' => 'input text error'],
  2520. 'label' => ['for' => '1-name'],
  2521. 'Name',
  2522. '/label',
  2523. 'input' => [
  2524. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2525. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2526. ],
  2527. ['div' => ['class' => 'error-message']],
  2528. 'This is wrong',
  2529. '/div',
  2530. '/div'
  2531. ];
  2532. $this->assertHtml($expected, $result);
  2533. }
  2534. /**
  2535. * testControl method
  2536. *
  2537. * Test various incarnations of control().
  2538. *
  2539. * @return void
  2540. */
  2541. public function testControl()
  2542. {
  2543. TableRegistry::get('ValidateUsers', [
  2544. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2545. ]);
  2546. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2547. $result = $this->Form->control('ValidateUsers.balance');
  2548. $expected = [
  2549. 'div' => ['class'],
  2550. 'label' => ['for'],
  2551. 'Balance',
  2552. '/label',
  2553. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2554. '/div',
  2555. ];
  2556. $this->assertHtml($expected, $result);
  2557. $result = $this->Form->control('ValidateUser.cost_decimal');
  2558. $expected = [
  2559. 'div' => ['class'],
  2560. 'label' => ['for'],
  2561. 'Cost Decimal',
  2562. '/label',
  2563. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2564. '/div',
  2565. ];
  2566. $this->assertHtml($expected, $result);
  2567. $result = $this->Form->control('ValidateUser.null_decimal');
  2568. $expected = [
  2569. 'div' => ['class'],
  2570. 'label' => ['for'],
  2571. 'Null Decimal',
  2572. '/label',
  2573. 'input' => ['name', 'type' => 'number', 'id'],
  2574. '/div',
  2575. ];
  2576. $this->assertHtml($expected, $result);
  2577. }
  2578. /**
  2579. * testControlCustomization method
  2580. *
  2581. * Tests the input method and passing custom options.
  2582. *
  2583. * @return void
  2584. */
  2585. public function testControlCustomization()
  2586. {
  2587. TableRegistry::get('Contacts', [
  2588. 'className' => __NAMESPACE__ . '\ContactsTable'
  2589. ]);
  2590. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2591. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2592. $expected = [
  2593. 'div' => ['class' => 'input email'],
  2594. 'label' => ['for' => 'custom'],
  2595. 'Email',
  2596. '/label',
  2597. ['input' => [
  2598. 'type' => 'email', 'name' => 'Contact[email]',
  2599. 'id' => 'custom', 'maxlength' => 255
  2600. ]],
  2601. '/div'
  2602. ];
  2603. $this->assertHtml($expected, $result);
  2604. $result = $this->Form->control('Contact.email', [
  2605. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2606. ]);
  2607. $expected = [
  2608. '<div',
  2609. 'label' => ['for' => 'contact-email'],
  2610. 'Email',
  2611. '/label',
  2612. ['input' => [
  2613. 'type' => 'email', 'name' => 'Contact[email]',
  2614. 'id' => 'contact-email', 'maxlength' => 255
  2615. ]],
  2616. '/div'
  2617. ];
  2618. $this->assertHtml($expected, $result);
  2619. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2620. $expected = [
  2621. 'div' => ['class' => 'input text'],
  2622. 'label' => ['for' => 'contact-email'],
  2623. 'Email',
  2624. '/label',
  2625. ['input' => [
  2626. 'type' => 'text', 'name' => 'Contact[email]',
  2627. 'id' => 'contact-email', 'maxlength' => '255'
  2628. ]],
  2629. '/div'
  2630. ];
  2631. $this->assertHtml($expected, $result);
  2632. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2633. $expected = [
  2634. 'div' => ['class' => 'input text'],
  2635. 'label' => ['for' => 'contact-5-email'],
  2636. 'Email',
  2637. '/label',
  2638. ['input' => [
  2639. 'type' => 'text', 'name' => 'Contact[5][email]',
  2640. 'id' => 'contact-5-email', 'maxlength' => '255'
  2641. ]],
  2642. '/div'
  2643. ];
  2644. $this->assertHtml($expected, $result);
  2645. $result = $this->Form->control('Contact.password');
  2646. $expected = [
  2647. 'div' => ['class' => 'input password'],
  2648. 'label' => ['for' => 'contact-password'],
  2649. 'Password',
  2650. '/label',
  2651. ['input' => [
  2652. 'type' => 'password', 'name' => 'Contact[password]',
  2653. 'id' => 'contact-password'
  2654. ]],
  2655. '/div'
  2656. ];
  2657. $this->assertHtml($expected, $result);
  2658. $result = $this->Form->control('Contact.email', [
  2659. 'type' => 'file', 'class' => 'textbox'
  2660. ]);
  2661. $expected = [
  2662. 'div' => ['class' => 'input file'],
  2663. 'label' => ['for' => 'contact-email'],
  2664. 'Email',
  2665. '/label',
  2666. ['input' => [
  2667. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2668. 'id' => 'contact-email'
  2669. ]],
  2670. '/div'
  2671. ];
  2672. $this->assertHtml($expected, $result);
  2673. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2674. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2675. $result = $this->Form->control('phone');
  2676. $expected = [
  2677. 'div' => ['class' => 'input tel'],
  2678. 'label' => ['for' => 'phone'],
  2679. 'Phone',
  2680. '/label',
  2681. ['input' => [
  2682. 'type' => 'tel', 'name' => 'phone',
  2683. 'value' => 'Hello &amp; World &gt; weird chars',
  2684. 'id' => 'phone', 'maxlength' => 255
  2685. ]],
  2686. '/div'
  2687. ];
  2688. $this->assertHtml($expected, $result);
  2689. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2690. $this->Form->create();
  2691. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2692. $expected = [
  2693. 'div' => ['class' => 'input text'],
  2694. 'label' => ['for' => 'myId'],
  2695. 'Field',
  2696. '/label',
  2697. 'input' => [
  2698. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2699. 'value' => 'My value', 'id' => 'myId'
  2700. ],
  2701. '/div'
  2702. ];
  2703. $this->assertHtml($expected, $result);
  2704. $this->Form->request->data = [];
  2705. $entity->errors('field', 'Badness!');
  2706. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2707. $result = $this->Form->control('field');
  2708. $expected = [
  2709. 'div' => ['class' => 'input text error'],
  2710. 'label' => ['for' => 'field'],
  2711. 'Field',
  2712. '/label',
  2713. 'input' => [
  2714. 'type' => 'text', 'name' => 'field',
  2715. 'id' => 'field', 'class' => 'form-error'
  2716. ],
  2717. ['div' => ['class' => 'error-message']],
  2718. 'Badness!',
  2719. '/div',
  2720. '/div'
  2721. ];
  2722. $this->assertHtml($expected, $result);
  2723. $result = $this->Form->control('field', [
  2724. 'templates' => [
  2725. 'inputContainerError' => '{{content}}{{error}}',
  2726. 'error' => '<span class="error-message">{{content}}</span>'
  2727. ]
  2728. ]);
  2729. $expected = [
  2730. 'label' => ['for' => 'field'],
  2731. 'Field',
  2732. '/label',
  2733. 'input' => [
  2734. 'type' => 'text', 'name' => 'field',
  2735. 'id' => 'field', 'class' => 'form-error'
  2736. ],
  2737. ['span' => ['class' => 'error-message']],
  2738. 'Badness!',
  2739. '/span'
  2740. ];
  2741. $this->assertHtml($expected, $result);
  2742. $entity->errors('field', ['minLength'], true);
  2743. $result = $this->Form->control('field', [
  2744. 'error' => [
  2745. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2746. 'maxLength' => 'login too large'
  2747. ]
  2748. ]);
  2749. $expected = [
  2750. 'div' => ['class' => 'input text error'],
  2751. 'label' => ['for' => 'field'],
  2752. 'Field',
  2753. '/label',
  2754. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2755. ['div' => ['class' => 'error-message']],
  2756. 'Le login doit contenir au moins 2 caractères',
  2757. '/div',
  2758. '/div'
  2759. ];
  2760. $this->assertHtml($expected, $result);
  2761. $entity->errors('field', ['maxLength'], true);
  2762. $result = $this->Form->control('field', [
  2763. 'error' => [
  2764. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2765. 'maxLength' => 'login too large',
  2766. ]
  2767. ]);
  2768. $expected = [
  2769. 'div' => ['class' => 'input text error'],
  2770. 'label' => ['for' => 'field'],
  2771. 'Field',
  2772. '/label',
  2773. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2774. ['div' => ['class' => 'error-message']],
  2775. 'login too large',
  2776. '/div',
  2777. '/div'
  2778. ];
  2779. $this->assertHtml($expected, $result);
  2780. }
  2781. /**
  2782. * testControlWithTemplateFile method
  2783. *
  2784. * Test that control() accepts a template file.
  2785. *
  2786. * @return void
  2787. */
  2788. public function testControlWithTemplateFile()
  2789. {
  2790. $result = $this->Form->control('field', [
  2791. 'templates' => 'htmlhelper_tags'
  2792. ]);
  2793. $expected = [
  2794. 'label' => ['for' => 'field'],
  2795. 'Field',
  2796. '/label',
  2797. 'input' => [
  2798. 'type' => 'text', 'name' => 'field',
  2799. 'id' => 'field'
  2800. ],
  2801. ];
  2802. $this->assertHtml($expected, $result);
  2803. }
  2804. /**
  2805. * testNestedControlsEndWithBrackets method
  2806. *
  2807. * Test that nested inputs end with brackets.
  2808. *
  2809. * @return void
  2810. */
  2811. public function testNestedControlsEndWithBrackets()
  2812. {
  2813. $result = $this->Form->text('nested.text[]');
  2814. $expected = [
  2815. 'input' => [
  2816. 'type' => 'text', 'name' => 'nested[text][]'
  2817. ],
  2818. ];
  2819. $this->assertHtml($expected, $result);
  2820. $result = $this->Form->file('nested.file[]');
  2821. $expected = [
  2822. 'input' => [
  2823. 'type' => 'file', 'name' => 'nested[file][]'
  2824. ],
  2825. ];
  2826. $this->assertHtml($expected, $result);
  2827. }
  2828. /**
  2829. * testCreateIdPrefix method
  2830. *
  2831. * Test id prefix.
  2832. *
  2833. * @return void
  2834. */
  2835. public function testCreateIdPrefix()
  2836. {
  2837. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2838. $result = $this->Form->control('field');
  2839. $expected = [
  2840. 'div' => ['class' => 'input text'],
  2841. 'label' => ['for' => 'prefix-field'],
  2842. 'Field',
  2843. '/label',
  2844. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2845. '/div'
  2846. ];
  2847. $this->assertHtml($expected, $result);
  2848. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2849. $expected = [
  2850. 'div' => ['class' => 'input text'],
  2851. 'label' => ['for' => 'custom-id'],
  2852. 'Field',
  2853. '/label',
  2854. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2855. '/div'
  2856. ];
  2857. $this->assertHtml($expected, $result);
  2858. $result = $this->Form->radio('Model.field', ['option A']);
  2859. $expected = [
  2860. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2861. 'label' => ['for' => 'prefix-model-field-0'],
  2862. ['input' => [
  2863. 'type' => 'radio',
  2864. 'name' => 'Model[field]',
  2865. 'value' => '0',
  2866. 'id' => 'prefix-model-field-0'
  2867. ]],
  2868. 'option A',
  2869. '/label'
  2870. ];
  2871. $this->assertHtml($expected, $result);
  2872. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2873. $expected = [
  2874. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2875. 'label' => ['for' => 'prefix-model-field-0'],
  2876. ['input' => [
  2877. 'type' => 'radio',
  2878. 'name' => 'Model[field]',
  2879. 'value' => '0',
  2880. 'id' => 'prefix-model-field-0'
  2881. ]],
  2882. 'option A',
  2883. '/label'
  2884. ];
  2885. $this->assertHtml($expected, $result);
  2886. $result = $this->Form->select(
  2887. 'Model.multi_field',
  2888. ['first'],
  2889. ['multiple' => 'checkbox']
  2890. );
  2891. $expected = [
  2892. 'input' => [
  2893. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2894. ],
  2895. ['div' => ['class' => 'checkbox']],
  2896. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2897. ['input' => [
  2898. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2899. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2900. ]],
  2901. 'first',
  2902. '/label',
  2903. '/div',
  2904. ];
  2905. $this->assertHtml($expected, $result);
  2906. $this->Form->end();
  2907. $result = $this->Form->control('field');
  2908. $expected = [
  2909. 'div' => ['class' => 'input text'],
  2910. 'label' => ['for' => 'field'],
  2911. 'Field',
  2912. '/label',
  2913. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2914. '/div'
  2915. ];
  2916. $this->assertHtml($expected, $result);
  2917. }
  2918. /**
  2919. * testControlZero method
  2920. *
  2921. * Test that inputs with 0 can be created.
  2922. *
  2923. * @return void
  2924. */
  2925. public function testControlZero()
  2926. {
  2927. TableRegistry::get('Contacts', [
  2928. 'className' => __NAMESPACE__ . '\ContactsTable'
  2929. ]);
  2930. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2931. $result = $this->Form->control('0');
  2932. $expected = [
  2933. 'div' => ['class' => 'input text'],
  2934. 'label' => ['for' => '0'], '/label',
  2935. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2936. '/div'
  2937. ];
  2938. $this->assertHtml($expected, $result);
  2939. }
  2940. /**
  2941. * testControlCheckbox method
  2942. *
  2943. * Test control() with checkbox creation.
  2944. *
  2945. * @return void
  2946. */
  2947. public function testControlCheckbox()
  2948. {
  2949. $result = $this->Form->control('User.active', ['label' => false, 'checked' => true]);
  2950. $expected = [
  2951. 'div' => ['class' => 'input checkbox'],
  2952. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2953. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2954. '/div'
  2955. ];
  2956. $this->assertHtml($expected, $result);
  2957. $result = $this->Form->control('User.active', ['label' => false, 'checked' => 1]);
  2958. $expected = [
  2959. 'div' => ['class' => 'input checkbox'],
  2960. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2961. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2962. '/div'
  2963. ];
  2964. $this->assertHtml($expected, $result);
  2965. $result = $this->Form->control('User.active', ['label' => false, 'checked' => '1']);
  2966. $expected = [
  2967. 'div' => ['class' => 'input checkbox'],
  2968. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2969. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2970. '/div'
  2971. ];
  2972. $this->assertHtml($expected, $result);
  2973. $result = $this->Form->control('User.disabled', [
  2974. 'label' => 'Disabled',
  2975. 'type' => 'checkbox',
  2976. 'data-foo' => 'disabled'
  2977. ]);
  2978. $expected = [
  2979. 'div' => ['class' => 'input checkbox'],
  2980. 'input' => ['type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'],
  2981. 'label' => ['for' => 'user-disabled'],
  2982. ['input' => [
  2983. 'type' => 'checkbox',
  2984. 'name' => 'User[disabled]',
  2985. 'value' => '1',
  2986. 'id' => 'user-disabled',
  2987. 'data-foo' => 'disabled'
  2988. ]],
  2989. 'Disabled',
  2990. '/label',
  2991. '/div'
  2992. ];
  2993. $this->assertHtml($expected, $result);
  2994. $result = $this->Form->control('User.confirm', [
  2995. 'label' => 'Confirm <b>me</b>!',
  2996. 'type' => 'checkbox',
  2997. 'escape' => false
  2998. ]);
  2999. $expected = [
  3000. 'div' => ['class' => 'input checkbox'],
  3001. 'input' => ['type' => 'hidden', 'name' => 'User[confirm]', 'value' => '0'],
  3002. 'label' => ['for' => 'user-confirm'],
  3003. ['input' => [
  3004. 'type' => 'checkbox',
  3005. 'name' => 'User[confirm]',
  3006. 'value' => '1',
  3007. 'id' => 'user-confirm',
  3008. ]],
  3009. 'Confirm <b>me</b>!',
  3010. '/label',
  3011. '/div'
  3012. ];
  3013. $this->assertHtml($expected, $result);
  3014. }
  3015. /**
  3016. * testControlHidden method
  3017. *
  3018. * Test that control() does not create wrapping div and label tag for hidden fields.
  3019. *
  3020. * @return void
  3021. */
  3022. public function testControlHidden()
  3023. {
  3024. TableRegistry::get('ValidateUsers', [
  3025. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3026. ]);
  3027. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3028. $result = $this->Form->control('ValidateUser.id');
  3029. $expected = [
  3030. 'input' => ['name', 'type' => 'hidden', 'id']
  3031. ];
  3032. $this->assertHtml($expected, $result);
  3033. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3034. $expected = [
  3035. 'input' => ['name', 'type' => 'hidden', 'id']
  3036. ];
  3037. $this->assertHtml($expected, $result);
  3038. }
  3039. /**
  3040. * testControlDatetime method
  3041. *
  3042. * Test form->control() with datetime.
  3043. *
  3044. * @return void
  3045. */
  3046. public function testControlDatetime()
  3047. {
  3048. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3049. ->setMethods(['datetime'])
  3050. ->setConstructorArgs([new View()])
  3051. ->getMock();
  3052. $this->Form->expects($this->once())->method('datetime')
  3053. ->with('prueba', [
  3054. 'type' => 'datetime',
  3055. 'timeFormat' => 24,
  3056. 'minYear' => 2008,
  3057. 'maxYear' => 2011,
  3058. 'interval' => 15,
  3059. 'options' => null,
  3060. 'empty' => false,
  3061. 'id' => 'prueba',
  3062. 'required' => false,
  3063. 'templateVars' => []
  3064. ])
  3065. ->will($this->returnValue('This is it!'));
  3066. $result = $this->Form->control('prueba', [
  3067. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3068. 'maxYear' => 2011, 'interval' => 15
  3069. ]);
  3070. $expected = [
  3071. 'div' => ['class' => 'input datetime'],
  3072. '<label',
  3073. 'Prueba',
  3074. '/label',
  3075. 'This is it!',
  3076. '/div'
  3077. ];
  3078. $this->assertHtml($expected, $result);
  3079. }
  3080. /**
  3081. * testControlDatetimeIdPrefix method
  3082. *
  3083. * Test form->control() with datetime with id prefix.
  3084. *
  3085. * @return void
  3086. */
  3087. public function testControlDatetimeIdPrefix()
  3088. {
  3089. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3090. ->setMethods(['datetime'])
  3091. ->setConstructorArgs([new View()])
  3092. ->getMock();
  3093. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3094. $this->Form->expects($this->once())->method('datetime')
  3095. ->with('prueba', [
  3096. 'type' => 'datetime',
  3097. 'timeFormat' => 24,
  3098. 'minYear' => 2008,
  3099. 'maxYear' => 2011,
  3100. 'interval' => 15,
  3101. 'options' => null,
  3102. 'empty' => false,
  3103. 'id' => 'prefix-prueba',
  3104. 'required' => false,
  3105. 'templateVars' => []
  3106. ])
  3107. ->will($this->returnValue('This is it!'));
  3108. $result = $this->Form->control('prueba', [
  3109. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3110. 'maxYear' => 2011, 'interval' => 15
  3111. ]);
  3112. $expected = [
  3113. 'div' => ['class' => 'input datetime'],
  3114. '<label',
  3115. 'Prueba',
  3116. '/label',
  3117. 'This is it!',
  3118. '/div'
  3119. ];
  3120. $this->assertHtml($expected, $result);
  3121. }
  3122. /**
  3123. * testControlCheckboxWithDisabledElements method
  3124. *
  3125. * Test generating checkboxes with disabled elements.
  3126. *
  3127. * @return void
  3128. */
  3129. public function testControlCheckboxWithDisabledElements()
  3130. {
  3131. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3132. $result = $this->Form->control('Contact.multiple', [
  3133. 'multiple' => 'checkbox',
  3134. 'disabled' => 'disabled',
  3135. 'options' => $options
  3136. ]);
  3137. $expected = [
  3138. ['div' => ['class' => 'input select']],
  3139. ['label' => ['for' => "contact-multiple"]],
  3140. 'Multiple',
  3141. '/label',
  3142. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'disabled' => 'disabled', 'value' => '']],
  3143. ['div' => ['class' => 'checkbox']],
  3144. ['label' => ['for' => "contact-multiple-1"]],
  3145. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1"]],
  3146. 'One',
  3147. '/label',
  3148. '/div',
  3149. ['div' => ['class' => 'checkbox']],
  3150. ['label' => ['for' => "contact-multiple-2"]],
  3151. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2"]],
  3152. 'Two',
  3153. '/label',
  3154. '/div',
  3155. ['div' => ['class' => 'checkbox']],
  3156. ['label' => ['for' => "contact-multiple-3"]],
  3157. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3"]],
  3158. 'Three',
  3159. '/label',
  3160. '/div',
  3161. '/div'
  3162. ];
  3163. $this->assertHtml($expected, $result);
  3164. // make sure 50 does only disable 50, and not 50f5c0cf
  3165. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3166. $disabled = [50];
  3167. $expected = [
  3168. ['div' => ['class' => 'input select']],
  3169. ['label' => ['for' => "contact-multiple"]],
  3170. 'Multiple',
  3171. '/label',
  3172. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '']],
  3173. ['div' => ['class' => 'checkbox']],
  3174. ['label' => ['for' => "contact-multiple-50"]],
  3175. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50"]],
  3176. 'Fifty',
  3177. '/label',
  3178. '/div',
  3179. ['div' => ['class' => 'checkbox']],
  3180. ['label' => ['for' => "contact-multiple-50f5c0cf"]],
  3181. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf"]],
  3182. 'Stringy',
  3183. '/label',
  3184. '/div',
  3185. '/div'
  3186. ];
  3187. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3188. $this->assertHtml($expected, $result);
  3189. }
  3190. /**
  3191. * testControlWithLeadingInteger method
  3192. *
  3193. * Test input name with leading integer, ensure attributes are generated correctly.
  3194. *
  3195. * @return void
  3196. */
  3197. public function testControlWithLeadingInteger()
  3198. {
  3199. $result = $this->Form->text('0.Node.title');
  3200. $expected = [
  3201. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3202. ];
  3203. $this->assertHtml($expected, $result);
  3204. }
  3205. /**
  3206. * testControlSelectType method
  3207. *
  3208. * Test form->control() with select type inputs.
  3209. *
  3210. * @return void
  3211. */
  3212. public function testControlSelectType()
  3213. {
  3214. $result = $this->Form->control(
  3215. 'email',
  3216. [
  3217. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3218. );
  3219. $expected = [
  3220. 'div' => ['class' => 'input select'],
  3221. 'label' => ['for' => 'email'],
  3222. 'Email',
  3223. '/label',
  3224. ['select' => ['name' => 'email', 'id' => 'email']],
  3225. ['option' => ['value' => '']],
  3226. '/option',
  3227. ['option' => ['value' => 'è']],
  3228. 'Firést',
  3229. '/option',
  3230. ['option' => ['value' => 'é']],
  3231. 'Secoènd',
  3232. '/option',
  3233. '/select',
  3234. '/div'
  3235. ];
  3236. $this->assertHtml($expected, $result);
  3237. $result = $this->Form->control(
  3238. 'email',
  3239. [
  3240. 'options' => ['First', 'Second'], 'empty' => true]
  3241. );
  3242. $expected = [
  3243. 'div' => ['class' => 'input select'],
  3244. 'label' => ['for' => 'email'],
  3245. 'Email',
  3246. '/label',
  3247. ['select' => ['name' => 'email', 'id' => 'email']],
  3248. ['option' => ['value' => '']],
  3249. '/option',
  3250. ['option' => ['value' => '0']],
  3251. 'First',
  3252. '/option',
  3253. ['option' => ['value' => '1']],
  3254. 'Second',
  3255. '/option',
  3256. '/select',
  3257. '/div'
  3258. ];
  3259. $this->assertHtml($expected, $result);
  3260. $result = $this->Form->control('email', [
  3261. 'type' => 'select',
  3262. 'options' => new \ArrayObject(['First', 'Second']),
  3263. 'empty' => true
  3264. ]);
  3265. $this->assertHtml($expected, $result);
  3266. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3267. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3268. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3269. $expected = [
  3270. 'div' => ['class' => 'input select'],
  3271. 'label' => ['for' => 'model-user-id'],
  3272. 'User',
  3273. '/label',
  3274. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3275. ['option' => ['value' => '']],
  3276. '/option',
  3277. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3278. 'good',
  3279. '/option',
  3280. ['option' => ['value' => 'other']],
  3281. 'bad',
  3282. '/option',
  3283. '/select',
  3284. '/div'
  3285. ];
  3286. $this->assertHtml($expected, $result);
  3287. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3288. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3289. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3290. $expected = [
  3291. 'div' => ['class' => 'input select'],
  3292. 'label' => ['for' => 'thing-user-id'],
  3293. 'User',
  3294. '/label',
  3295. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3296. ['option' => ['value' => '']],
  3297. 'Some Empty',
  3298. '/option',
  3299. ['option' => ['value' => 'value']],
  3300. 'good',
  3301. '/option',
  3302. ['option' => ['value' => 'other']],
  3303. 'bad',
  3304. '/option',
  3305. '/select',
  3306. '/div'
  3307. ];
  3308. $this->assertHtml($expected, $result);
  3309. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3310. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3311. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3312. $expected = [
  3313. 'div' => ['class' => 'input select'],
  3314. 'label' => ['for' => 'thing-user-id'],
  3315. 'User',
  3316. '/label',
  3317. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3318. ['option' => ['value' => '']],
  3319. 'Some Empty',
  3320. '/option',
  3321. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3322. 'good',
  3323. '/option',
  3324. ['option' => ['value' => 'other']],
  3325. 'bad',
  3326. '/option',
  3327. '/select',
  3328. '/div'
  3329. ];
  3330. $this->assertHtml($expected, $result);
  3331. $this->Form->data = [];
  3332. $result = $this->Form->control('Publisher.id', [
  3333. 'label' => 'Publisher',
  3334. 'type' => 'select',
  3335. 'multiple' => 'checkbox',
  3336. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3337. ]);
  3338. $expected = [
  3339. ['div' => ['class' => 'input select']],
  3340. ['label' => ['for' => 'publisher-id']],
  3341. 'Publisher',
  3342. '/label',
  3343. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3344. ['div' => ['class' => 'checkbox']],
  3345. ['label' => ['for' => 'publisher-id-value-1']],
  3346. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3347. 'Label 1',
  3348. '/label',
  3349. '/div',
  3350. ['div' => ['class' => 'checkbox']],
  3351. ['label' => ['for' => 'publisher-id-value-2']],
  3352. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3353. 'Label 2',
  3354. '/label',
  3355. '/div',
  3356. '/div'
  3357. ];
  3358. $this->assertHtml($expected, $result);
  3359. }
  3360. /**
  3361. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3362. *
  3363. * Test that control() and a non standard primary key makes a hidden input by default.
  3364. *
  3365. * @return void
  3366. */
  3367. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3368. {
  3369. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3370. $this->Form->create($this->article);
  3371. $result = $this->Form->control('title');
  3372. $expected = [
  3373. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3374. ];
  3375. $this->assertHtml($expected, $result);
  3376. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3377. $this->Form->create($this->article);
  3378. $result = $this->Form->control('title');
  3379. $expected = [
  3380. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3381. ];
  3382. $this->assertHtml($expected, $result);
  3383. $result = $this->Form->control('body');
  3384. $expected = [
  3385. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3386. ];
  3387. $this->assertHtml($expected, $result);
  3388. }
  3389. /**
  3390. * testControlOverridingMagicSelectType method
  3391. *
  3392. * Test that overriding the magic select type widget is possible.
  3393. *
  3394. * @return void
  3395. */
  3396. public function testControlOverridingMagicSelectType()
  3397. {
  3398. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3399. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3400. $expected = [
  3401. 'div' => ['class' => 'input text'],
  3402. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3403. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3404. '/div'
  3405. ];
  3406. $this->assertHtml($expected, $result);
  3407. //Check that magic types still work for plural/singular vars
  3408. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3409. $result = $this->Form->control('Model.type');
  3410. $expected = [
  3411. 'div' => ['class' => 'input select'],
  3412. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3413. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3414. ['option' => ['value' => 'value']], 'good', '/option',
  3415. ['option' => ['value' => 'other']], 'bad', '/option',
  3416. '/select',
  3417. '/div'
  3418. ];
  3419. $this->assertHtml($expected, $result);
  3420. }
  3421. /**
  3422. * testControlMagicTypeDoesNotOverride method
  3423. *
  3424. * Test that inferred types do not override developer input.
  3425. *
  3426. * @return void
  3427. */
  3428. public function testControlMagicTypeDoesNotOverride()
  3429. {
  3430. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3431. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3432. $expected = [
  3433. 'div' => ['class' => 'input checkbox'],
  3434. ['input' => [
  3435. 'type' => 'hidden',
  3436. 'name' => 'Model[user]',
  3437. 'value' => 0,
  3438. ]],
  3439. 'label' => ['for' => 'model-user'],
  3440. ['input' => [
  3441. 'name' => 'Model[user]',
  3442. 'type' => 'checkbox',
  3443. 'id' => 'model-user',
  3444. 'value' => 1
  3445. ]],
  3446. 'User',
  3447. '/label',
  3448. '/div'
  3449. ];
  3450. $this->assertHtml($expected, $result);
  3451. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3452. $options = [
  3453. 1 => 'blue',
  3454. 2 => 'red'
  3455. ];
  3456. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3457. $expected = [
  3458. 'div' => ['class' => 'input select'],
  3459. 'label' => ['for' => 'tags-ids'],
  3460. 'Tags',
  3461. '/label',
  3462. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3463. ['div' => ['class' => 'checkbox']],
  3464. ['label' => ['for' => 'tags-ids-1']],
  3465. ['input' => [
  3466. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3467. 'value' => '1', 'name' => 'tags[_ids][]'
  3468. ]],
  3469. 'blue',
  3470. '/label',
  3471. '/div',
  3472. ['div' => ['class' => 'checkbox']],
  3473. ['label' => ['for' => 'tags-ids-2']],
  3474. ['input' => [
  3475. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3476. 'value' => '2', 'name' => 'tags[_ids][]'
  3477. ]],
  3478. 'red',
  3479. '/label',
  3480. '/div',
  3481. '/div'
  3482. ];
  3483. $this->assertHtml($expected, $result);
  3484. }
  3485. /**
  3486. * testControlMagicSelectForTypeNumber method
  3487. *
  3488. * Test that magic control() selects are created for type=number.
  3489. *
  3490. * @return void
  3491. */
  3492. public function testControlMagicSelectForTypeNumber()
  3493. {
  3494. TableRegistry::get('ValidateUsers', [
  3495. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3496. ]);
  3497. $entity = new Entity(['balance' => 1]);
  3498. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3499. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3500. $result = $this->Form->control('balance');
  3501. $expected = [
  3502. 'div' => ['class' => 'input select'],
  3503. 'label' => ['for' => 'balance'],
  3504. 'Balance',
  3505. '/label',
  3506. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3507. ['option' => ['value' => '0']],
  3508. 'nothing',
  3509. '/option',
  3510. ['option' => ['value' => '1', 'selected' => 'selected']],
  3511. 'some',
  3512. '/option',
  3513. ['option' => ['value' => '100']],
  3514. 'a lot',
  3515. '/option',
  3516. '/select',
  3517. '/div'
  3518. ];
  3519. $this->assertHtml($expected, $result);
  3520. }
  3521. /**
  3522. * testInvalidControlTypeOption method
  3523. *
  3524. * Test invalid 'input' type option to control() function.
  3525. *
  3526. * @expectedException \RuntimeException
  3527. * @expectedExceptionMessage Invalid type 'input' used for field 'text'
  3528. * @return void
  3529. */
  3530. public function testInvalidControlTypeOption()
  3531. {
  3532. $this->Form->control('text', ['type' => 'input']);
  3533. }
  3534. /**
  3535. * testControlMagicSelectChangeToRadio method
  3536. *
  3537. * Test that magic control() selects can easily be converted into radio types without error.
  3538. *
  3539. * @return void
  3540. */
  3541. public function testControlMagicSelectChangeToRadio()
  3542. {
  3543. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3544. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3545. $this->assertContains('input type="radio"', $result);
  3546. }
  3547. /**
  3548. * testFormControlSubmit method
  3549. *
  3550. * Test correct results for form::control() and type submit.
  3551. *
  3552. * @return void
  3553. */
  3554. public function testFormControlSubmit()
  3555. {
  3556. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3557. $expected = [
  3558. 'div' => ['class' => 'submit'],
  3559. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3560. '/div'
  3561. ];
  3562. $this->assertHtml($expected, $result);
  3563. }
  3564. /**
  3565. * testFormControls method
  3566. *
  3567. * Test correct results from Form::controls().
  3568. *
  3569. * @return void
  3570. */
  3571. public function testFormControlsLegendFieldset()
  3572. {
  3573. $this->Form->create($this->article);
  3574. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3575. $expected = [
  3576. '<fieldset',
  3577. '<legend',
  3578. 'The Legend',
  3579. '/legend',
  3580. '*/fieldset',
  3581. ];
  3582. $this->assertHtml($expected, $result);
  3583. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3584. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3585. $this->assertContains('<fieldset>', $result);
  3586. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3587. $this->assertNotContains('<legend>', $result);
  3588. $this->assertNotContains('<fieldset>', $result);
  3589. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3590. $this->assertNotContains('<legend>', $result);
  3591. $this->assertNotContains('<fieldset>', $result);
  3592. $this->Form->create($this->article);
  3593. $this->Form->request->params['prefix'] = 'admin';
  3594. $this->Form->request->params['action'] = 'admin_edit';
  3595. $this->Form->request->params['controller'] = 'articles';
  3596. $result = $this->Form->allControls();
  3597. $expected = [
  3598. '<fieldset',
  3599. '<legend',
  3600. 'New Article',
  3601. '/legend',
  3602. '*/fieldset',
  3603. ];
  3604. $this->assertHtml($expected, $result);
  3605. $this->Form->create($this->article);
  3606. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3607. $expected = [
  3608. '<fieldset',
  3609. '<legend',
  3610. 'The Legend',
  3611. '/legend',
  3612. '*/fieldset',
  3613. ];
  3614. $this->assertHtml($expected, $result);
  3615. $this->Form->create($this->article);
  3616. $result = $this->Form->allControls([], [
  3617. 'fieldset' => [
  3618. 'class' => 'some-class some-other-class',
  3619. 'disabled' => true,
  3620. 'data-param' => 'a-param'
  3621. ],
  3622. 'legend' => 'The Legend'
  3623. ]);
  3624. $expected = [
  3625. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3626. '<legend',
  3627. 'The Legend',
  3628. '/legend',
  3629. '*/fieldset',
  3630. ];
  3631. $this->assertHtml($expected, $result);
  3632. }
  3633. /**
  3634. * testFormControls method
  3635. *
  3636. * Test the controls() method.
  3637. *
  3638. * @return void
  3639. */
  3640. public function testFormControls()
  3641. {
  3642. $this->Form->create($this->article);
  3643. $result = $this->Form->allControls();
  3644. $expected = [
  3645. '<fieldset',
  3646. '<legend', 'New Article', '/legend',
  3647. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3648. ['div' => ['class' => 'input select required']],
  3649. '*/div',
  3650. ['div' => ['class' => 'input text required']],
  3651. '*/div',
  3652. ['div' => ['class' => 'input text']],
  3653. '*/div',
  3654. ['div' => ['class' => 'input text']],
  3655. '*/div',
  3656. '/fieldset',
  3657. ];
  3658. $this->assertHtml($expected, $result);
  3659. $result = $this->Form->allControls([
  3660. 'published' => ['type' => 'boolean']
  3661. ]);
  3662. $expected = [
  3663. '<fieldset',
  3664. '<legend', 'New Article', '/legend',
  3665. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3666. ['div' => ['class' => 'input select required']],
  3667. '*/div',
  3668. ['div' => ['class' => 'input text required']],
  3669. '*/div',
  3670. ['div' => ['class' => 'input text']],
  3671. '*/div',
  3672. ['div' => ['class' => 'input boolean']],
  3673. '*/div',
  3674. '/fieldset',
  3675. ];
  3676. $this->assertHtml($expected, $result);
  3677. $this->Form->create($this->article);
  3678. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3679. $expected = [
  3680. 'fieldset' => [],
  3681. 'legend' => [],
  3682. 'Hello',
  3683. '/legend',
  3684. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3685. ['div' => ['class' => 'input select required']],
  3686. '*/div',
  3687. ['div' => ['class' => 'input text required']],
  3688. '*/div',
  3689. ['div' => ['class' => 'input text']],
  3690. '*/div',
  3691. ['div' => ['class' => 'input text']],
  3692. '*/div',
  3693. '/fieldset'
  3694. ];
  3695. $this->assertHtml($expected, $result);
  3696. $this->Form->create(false);
  3697. $expected = [
  3698. 'fieldset' => [],
  3699. ['div' => ['class' => 'input text']],
  3700. 'label' => ['for' => 'foo'],
  3701. 'Foo',
  3702. '/label',
  3703. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3704. '*/div',
  3705. '/fieldset'
  3706. ];
  3707. $result = $this->Form->allControls(
  3708. ['foo' => ['type' => 'text']],
  3709. ['legend' => false]
  3710. );
  3711. $this->assertHtml($expected, $result);
  3712. }
  3713. /**
  3714. * testFormControlsBlacklist method
  3715. *
  3716. * @return void
  3717. */
  3718. public function testFormControlsBlacklist()
  3719. {
  3720. $this->Form->create($this->article);
  3721. $result = $this->Form->allControls([
  3722. 'id' => false
  3723. ]);
  3724. $expected = [
  3725. '<fieldset',
  3726. '<legend', 'New Article', '/legend',
  3727. ['div' => ['class' => 'input select required']],
  3728. '*/div',
  3729. ['div' => ['class' => 'input text required']],
  3730. '*/div',
  3731. ['div' => ['class' => 'input text']],
  3732. '*/div',
  3733. ['div' => ['class' => 'input text']],
  3734. '*/div',
  3735. '/fieldset',
  3736. ];
  3737. $this->assertHtml($expected, $result);
  3738. $this->Form->create($this->article);
  3739. $result = $this->Form->allControls([
  3740. 'id' => []
  3741. ]);
  3742. $expected = [
  3743. '<fieldset',
  3744. '<legend', 'New Article', '/legend',
  3745. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3746. ['div' => ['class' => 'input select required']],
  3747. '*/div',
  3748. ['div' => ['class' => 'input text required']],
  3749. '*/div',
  3750. ['div' => ['class' => 'input text']],
  3751. '*/div',
  3752. ['div' => ['class' => 'input text']],
  3753. '*/div',
  3754. '/fieldset',
  3755. ];
  3756. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3757. }
  3758. /**
  3759. * testSelectAsCheckbox method
  3760. *
  3761. * Test multi-select widget with checkbox formatting.
  3762. *
  3763. * @return void
  3764. */
  3765. public function testSelectAsCheckbox()
  3766. {
  3767. $result = $this->Form->select(
  3768. 'Model.multi_field',
  3769. ['first', 'second', 'third'],
  3770. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3771. );
  3772. $expected = [
  3773. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3774. ['div' => ['class' => 'checkbox']],
  3775. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3776. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3777. 'first',
  3778. '/label',
  3779. '/div',
  3780. ['div' => ['class' => 'checkbox']],
  3781. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3782. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3783. 'second',
  3784. '/label',
  3785. '/div',
  3786. ['div' => ['class' => 'checkbox']],
  3787. ['label' => ['for' => 'model-multi-field-2']],
  3788. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3789. 'third',
  3790. '/label',
  3791. '/div',
  3792. ];
  3793. $this->assertHtml($expected, $result);
  3794. $result = $this->Form->select(
  3795. 'Model.multi_field',
  3796. ['1/2' => 'half'],
  3797. ['multiple' => 'checkbox']
  3798. );
  3799. $expected = [
  3800. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3801. ['div' => ['class' => 'checkbox']],
  3802. ['label' => ['for' => 'model-multi-field-1-2']],
  3803. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3804. 'half',
  3805. '/label',
  3806. '/div',
  3807. ];
  3808. $this->assertHtml($expected, $result);
  3809. }
  3810. /**
  3811. * testLabel method
  3812. *
  3813. * Test label generation.
  3814. *
  3815. * @return void
  3816. */
  3817. public function testLabel()
  3818. {
  3819. $result = $this->Form->label('Person.name');
  3820. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3821. $this->assertHtml($expected, $result);
  3822. $result = $this->Form->label('Person.name');
  3823. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3824. $this->assertHtml($expected, $result);
  3825. $result = $this->Form->label('Person.first_name');
  3826. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3827. $this->assertHtml($expected, $result);
  3828. $result = $this->Form->label('Person.first_name', 'Your first name');
  3829. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3830. $this->assertHtml($expected, $result);
  3831. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3832. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3833. $this->assertHtml($expected, $result);
  3834. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3835. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3836. $this->assertHtml($expected, $result);
  3837. $result = $this->Form->label('Person.first_name', '');
  3838. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3839. $this->assertHtml($expected, $result);
  3840. $result = $this->Form->label('Person.2.name', '');
  3841. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3842. $this->assertHtml($expected, $result);
  3843. }
  3844. /**
  3845. * testLabelContainControl method
  3846. *
  3847. * Test that label() can accept an input with the correct template vars.
  3848. *
  3849. * @return void
  3850. */
  3851. public function testLabelContainControl()
  3852. {
  3853. $this->Form->templates([
  3854. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3855. ]);
  3856. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3857. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3858. ]);
  3859. $expected = [
  3860. 'label' => ['for' => 'person-accept-terms'],
  3861. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3862. 'Accept',
  3863. '/label',
  3864. ];
  3865. $this->assertHtml($expected, $result);
  3866. }
  3867. /**
  3868. * testTextbox method
  3869. *
  3870. * Test textbox element generation.
  3871. *
  3872. * @return void
  3873. */
  3874. public function testTextbox()
  3875. {
  3876. $result = $this->Form->text('Model.field');
  3877. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3878. $this->assertHtml($expected, $result);
  3879. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3880. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3881. $this->assertHtml($expected, $result);
  3882. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3883. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3884. $this->assertHtml($expected, $result);
  3885. }
  3886. /**
  3887. * testTextBoxDataAndError method
  3888. *
  3889. * Test that text() hooks up with request data and error fields.
  3890. *
  3891. * @return void
  3892. */
  3893. public function testTextBoxDataAndError()
  3894. {
  3895. $this->article['errors'] = [
  3896. 'Contact' => ['text' => 'wrong']
  3897. ];
  3898. $this->Form->create($this->article);
  3899. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3900. $result = $this->Form->text('Model.text');
  3901. $expected = [
  3902. 'input' => [
  3903. 'type' => 'text',
  3904. 'name' => 'Model[text]',
  3905. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3906. ]
  3907. ];
  3908. $this->assertHtml($expected, $result);
  3909. $this->Form->request->data['Contact']['text'] = 'test';
  3910. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3911. $expected = [
  3912. 'input' => [
  3913. 'type' => 'text',
  3914. 'name' => 'Contact[text]',
  3915. 'value' => 'test',
  3916. 'id' => 'theID',
  3917. 'class' => 'form-error'
  3918. ]
  3919. ];
  3920. $this->assertHtml($expected, $result);
  3921. }
  3922. /**
  3923. * testDefaultValue method
  3924. *
  3925. * Test default value setting.
  3926. *
  3927. * @return void
  3928. */
  3929. public function testTextDefaultValue()
  3930. {
  3931. $this->Form->request->data['Model']['field'] = 'test';
  3932. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3933. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3934. $this->assertHtml($expected, $result);
  3935. unset($this->Form->request->data['Model']['field']);
  3936. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3937. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3938. $this->assertHtml($expected, $result);
  3939. $Articles = TableRegistry::get('Articles');
  3940. $title = $Articles->schema()->column('title');
  3941. $Articles->schema()->addColumn(
  3942. 'title',
  3943. ['default' => 'default title'] + $title
  3944. );
  3945. $entity = $Articles->newEntity();
  3946. $this->Form->create($entity);
  3947. // Get default value from schema
  3948. $result = $this->Form->text('title');
  3949. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  3950. $this->assertHtml($expected, $result);
  3951. // Don't get value from schema
  3952. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3953. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  3954. $this->assertHtml($expected, $result);
  3955. // Custom default value overrides default value from schema
  3956. $result = $this->Form->text('title', ['default' => 'override default']);
  3957. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  3958. $this->assertHtml($expected, $result);
  3959. // Default value from schema is used only for new entities.
  3960. $entity->isNew(false);
  3961. $result = $this->Form->text('title');
  3962. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  3963. $this->assertHtml($expected, $result);
  3964. }
  3965. /**
  3966. * testError method
  3967. *
  3968. * Test field error generation.
  3969. *
  3970. * @return void
  3971. */
  3972. public function testError()
  3973. {
  3974. $this->article['errors'] = [
  3975. 'Article' => ['field' => 'email']
  3976. ];
  3977. $this->Form->create($this->article);
  3978. $result = $this->Form->error('Article.field');
  3979. $expected = [
  3980. ['div' => ['class' => 'error-message']],
  3981. 'email',
  3982. '/div',
  3983. ];
  3984. $this->assertHtml($expected, $result);
  3985. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  3986. $expected = [
  3987. ['div' => ['class' => 'error-message']],
  3988. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3989. '/div',
  3990. ];
  3991. $this->assertHtml($expected, $result);
  3992. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3993. $expected = [
  3994. ['div' => ['class' => 'error-message']],
  3995. '<strong', 'Badness!', '/strong',
  3996. '/div',
  3997. ];
  3998. $this->assertHtml($expected, $result);
  3999. }
  4000. /**
  4001. * testErrorRuleName method
  4002. *
  4003. * Test error translation can use rule names for translating.
  4004. *
  4005. * @return void
  4006. */
  4007. public function testErrorRuleName()
  4008. {
  4009. $this->article['errors'] = [
  4010. 'Article' => [
  4011. 'field' => ['email' => 'Your email was not good']
  4012. ]
  4013. ];
  4014. $this->Form->create($this->article);
  4015. $result = $this->Form->error('Article.field');
  4016. $expected = [
  4017. ['div' => ['class' => 'error-message']],
  4018. 'Your email was not good',
  4019. '/div',
  4020. ];
  4021. $this->assertHtml($expected, $result);
  4022. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4023. $expected = [
  4024. ['div' => ['class' => 'error-message']],
  4025. 'Email in use',
  4026. '/div',
  4027. ];
  4028. $this->assertHtml($expected, $result);
  4029. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4030. $expected = [
  4031. ['div' => ['class' => 'error-message']],
  4032. 'Email in use',
  4033. '/div',
  4034. ];
  4035. $this->assertHtml($expected, $result);
  4036. $result = $this->Form->error('Article.field', [
  4037. 'email' => 'Key is preferred',
  4038. 'Your email was not good' => 'Email in use'
  4039. ]);
  4040. $expected = [
  4041. ['div' => ['class' => 'error-message']],
  4042. 'Key is preferred',
  4043. '/div',
  4044. ];
  4045. $this->assertHtml($expected, $result);
  4046. }
  4047. /**
  4048. * testErrorMessages method
  4049. *
  4050. * Test error with nested lists.
  4051. *
  4052. * @return void
  4053. */
  4054. public function testErrorMessages()
  4055. {
  4056. $this->article['errors'] = [
  4057. 'Article' => ['field' => 'email']
  4058. ];
  4059. $this->Form->create($this->article);
  4060. $result = $this->Form->error('Article.field', [
  4061. 'email' => 'No good!'
  4062. ]);
  4063. $expected = [
  4064. 'div' => ['class' => 'error-message'],
  4065. 'No good!',
  4066. '/div'
  4067. ];
  4068. $this->assertHtml($expected, $result);
  4069. }
  4070. /**
  4071. * testErrorMultipleMessages method
  4072. *
  4073. * Test error() with multiple messages.
  4074. *
  4075. * @return void
  4076. */
  4077. public function testErrorMultipleMessages()
  4078. {
  4079. $this->article['errors'] = [
  4080. 'field' => ['notBlank', 'email', 'Something else']
  4081. ];
  4082. $this->Form->create($this->article);
  4083. $result = $this->Form->error('field', [
  4084. 'notBlank' => 'Cannot be empty',
  4085. 'email' => 'No good!'
  4086. ]);
  4087. $expected = [
  4088. 'div' => ['class' => 'error-message'],
  4089. 'ul' => [],
  4090. '<li', 'Cannot be empty', '/li',
  4091. '<li', 'No good!', '/li',
  4092. '<li', 'Something else', '/li',
  4093. '/ul',
  4094. '/div'
  4095. ];
  4096. $this->assertHtml($expected, $result);
  4097. }
  4098. /**
  4099. * testPassword method
  4100. *
  4101. * Test password element generation.
  4102. *
  4103. * @return void
  4104. */
  4105. public function testPassword()
  4106. {
  4107. $this->article['errors'] = [
  4108. 'Contact' => [
  4109. 'passwd' => 1
  4110. ]
  4111. ];
  4112. $this->Form->create($this->article);
  4113. $result = $this->Form->password('Contact.field');
  4114. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4115. $this->assertHtml($expected, $result);
  4116. $this->Form->request->data['Contact']['passwd'] = 'test';
  4117. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4118. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4119. $this->assertHtml($expected, $result);
  4120. }
  4121. /**
  4122. * testRadio method
  4123. *
  4124. * Test radio element set generation.
  4125. *
  4126. * @return void
  4127. */
  4128. public function testRadio()
  4129. {
  4130. $result = $this->Form->radio('Model.field', ['option A']);
  4131. $expected = [
  4132. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4133. 'label' => ['for' => 'model-field-0'],
  4134. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4135. 'option A',
  4136. '/label'
  4137. ];
  4138. $this->assertHtml($expected, $result);
  4139. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4140. $this->assertHtml($expected, $result);
  4141. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4142. $expected = [
  4143. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4144. ['label' => ['for' => 'model-field-0']],
  4145. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4146. 'option A',
  4147. '/label',
  4148. ['label' => ['for' => 'model-field-1']],
  4149. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4150. 'option B',
  4151. '/label',
  4152. ];
  4153. $this->assertHtml($expected, $result);
  4154. $result = $this->Form->radio(
  4155. 'Employee.gender',
  4156. ['male' => 'Male', 'female' => 'Female'],
  4157. ['form' => 'my-form']
  4158. );
  4159. $expected = [
  4160. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4161. ['label' => ['for' => 'employee-gender-male']],
  4162. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4163. 'Male',
  4164. '/label',
  4165. ['label' => ['for' => 'employee-gender-female']],
  4166. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4167. 'Female',
  4168. '/label',
  4169. ];
  4170. $this->assertHtml($expected, $result);
  4171. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4172. $expected = [
  4173. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4174. ['label' => ['for' => 'model-custom-0']],
  4175. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4176. 'option A',
  4177. '/label',
  4178. ['label' => ['for' => 'model-custom-1']],
  4179. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4180. 'option B',
  4181. '/label',
  4182. ];
  4183. $this->assertHtml($expected, $result);
  4184. $result = $this->Form->radio(
  4185. 'Employee.gender',
  4186. [
  4187. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4188. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4189. ]
  4190. );
  4191. $expected = [
  4192. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4193. ['label' => ['for' => 'employee-gender-male']],
  4194. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4195. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4196. 'Male',
  4197. '/label',
  4198. ['label' => ['for' => 'employee-gender-female']],
  4199. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4200. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4201. 'Female',
  4202. '/label',
  4203. ];
  4204. $this->assertHtml($expected, $result);
  4205. }
  4206. /**
  4207. * testRadioDefaultValue method
  4208. *
  4209. * Test default value setting on radio() method.
  4210. *
  4211. * @return void
  4212. */
  4213. public function testRadioDefaultValue()
  4214. {
  4215. $Articles = TableRegistry::get('Articles');
  4216. $title = $Articles->schema()->column('title');
  4217. $Articles->schema()->addColumn(
  4218. 'title',
  4219. ['default' => '1'] + $title
  4220. );
  4221. $this->Form->create($Articles->newEntity());
  4222. $result = $this->Form->radio('title', ['option A', 'option B']);
  4223. $expected = [
  4224. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4225. ['label' => ['for' => 'title-0']],
  4226. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4227. 'option A',
  4228. '/label',
  4229. ['label' => ['for' => 'title-1']],
  4230. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4231. 'option B',
  4232. '/label',
  4233. ];
  4234. $this->assertHtml($expected, $result);
  4235. }
  4236. /**
  4237. * testControlRadio method
  4238. *
  4239. * Test that input works with radio types.
  4240. *
  4241. * @return void
  4242. */
  4243. public function testControlRadio()
  4244. {
  4245. $result = $this->Form->control('test', [
  4246. 'type' => 'radio',
  4247. 'options' => ['A', 'B'],
  4248. ]);
  4249. $expected = [
  4250. ['div' => ['class' => 'input radio']],
  4251. '<label',
  4252. 'Test',
  4253. '/label',
  4254. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4255. ['label' => ['for' => 'test-0']],
  4256. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4257. 'A',
  4258. '/label',
  4259. ['label' => ['for' => 'test-1']],
  4260. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4261. 'B',
  4262. '/label',
  4263. '/div',
  4264. ];
  4265. $this->assertHtml($expected, $result);
  4266. $result = $this->Form->control('test', [
  4267. 'type' => 'radio',
  4268. 'options' => ['A', 'B'],
  4269. 'value' => '0'
  4270. ]);
  4271. $expected = [
  4272. ['div' => ['class' => 'input radio']],
  4273. '<label',
  4274. 'Test',
  4275. '/label',
  4276. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4277. ['label' => ['for' => 'test-0']],
  4278. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4279. 'A',
  4280. '/label',
  4281. ['label' => ['for' => 'test-1']],
  4282. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4283. 'B',
  4284. '/label',
  4285. '/div',
  4286. ];
  4287. $this->assertHtml($expected, $result);
  4288. $result = $this->Form->control('test', [
  4289. 'type' => 'radio',
  4290. 'options' => ['A', 'B'],
  4291. 'label' => false
  4292. ]);
  4293. $expected = [
  4294. ['div' => ['class' => 'input radio']],
  4295. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4296. ['label' => ['for' => 'test-0']],
  4297. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4298. 'A',
  4299. '/label',
  4300. ['label' => ['for' => 'test-1']],
  4301. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4302. 'B',
  4303. '/label',
  4304. '/div',
  4305. ];
  4306. $this->assertHtml($expected, $result);
  4307. }
  4308. /**
  4309. * testRadioNoLabel method
  4310. *
  4311. * Test that radio() works with label = false.
  4312. *
  4313. * @return void
  4314. */
  4315. public function testRadioNoLabel()
  4316. {
  4317. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4318. $expected = [
  4319. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4320. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4321. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4322. ];
  4323. $this->assertHtml($expected, $result);
  4324. }
  4325. /**
  4326. * testRadioControlInsideLabel method
  4327. *
  4328. * Test generating radio input inside label ala twitter bootstrap.
  4329. *
  4330. * @return void
  4331. */
  4332. public function testRadioControlInsideLabel()
  4333. {
  4334. $this->Form->templates([
  4335. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4336. 'radioWrapper' => '{{label}}'
  4337. ]);
  4338. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4339. //@codingStandardsIgnoreStart
  4340. $expected = [
  4341. ['input' => [
  4342. 'type' => 'hidden',
  4343. 'name' => 'Model[field]',
  4344. 'value' => ''
  4345. ]],
  4346. ['label' => ['for' => 'model-field-0']],
  4347. ['input' => [
  4348. 'type' => 'radio',
  4349. 'name' => 'Model[field]',
  4350. 'value' => '0',
  4351. 'id' => 'model-field-0'
  4352. ]],
  4353. 'option A',
  4354. '/label',
  4355. ['label' => ['for' => 'model-field-1']],
  4356. ['input' => [
  4357. 'type' => 'radio',
  4358. 'name' => 'Model[field]',
  4359. 'value' => '1',
  4360. 'id' => 'model-field-1'
  4361. ]],
  4362. 'option B',
  4363. '/label',
  4364. ];
  4365. //@codingStandardsIgnoreEnd
  4366. $this->assertHtml($expected, $result);
  4367. }
  4368. /**
  4369. * testRadioHiddenControlDisabling method
  4370. *
  4371. * Test disabling the hidden input for radio buttons.
  4372. *
  4373. * @return void
  4374. */
  4375. public function testRadioHiddenControlDisabling()
  4376. {
  4377. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4378. $expected = [
  4379. 'label' => ['for' => 'model-1-field-0'],
  4380. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4381. 'option A',
  4382. '/label'
  4383. ];
  4384. $this->assertHtml($expected, $result);
  4385. }
  4386. /**
  4387. * testRadioOutOfRange method
  4388. *
  4389. * Test radio element set generation.
  4390. *
  4391. * @return void
  4392. */
  4393. public function testRadioOutOfRange()
  4394. {
  4395. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4396. $expected = [
  4397. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4398. 'label' => ['for' => 'model-field-v'],
  4399. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4400. 'value',
  4401. '/label'
  4402. ];
  4403. $this->assertHtml($expected, $result);
  4404. }
  4405. /**
  4406. * testSelect method
  4407. *
  4408. * Test select element generation.
  4409. *
  4410. * @return void
  4411. */
  4412. public function testSelect()
  4413. {
  4414. $result = $this->Form->select('Model.field', []);
  4415. $expected = [
  4416. 'select' => ['name' => 'Model[field]'],
  4417. '/select'
  4418. ];
  4419. $this->assertHtml($expected, $result);
  4420. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4421. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4422. $expected = [
  4423. 'select' => ['name' => 'Model[field]'],
  4424. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4425. 'good',
  4426. '/option',
  4427. ['option' => ['value' => 'other']],
  4428. 'bad',
  4429. '/option',
  4430. '/select'
  4431. ];
  4432. $this->assertHtml($expected, $result);
  4433. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4434. $this->assertHtml($expected, $result);
  4435. $this->Form->request->data = [];
  4436. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4437. $expected = [
  4438. 'select' => ['name' => 'Model[field]'],
  4439. ['option' => ['value' => 'value']],
  4440. 'good',
  4441. '/option',
  4442. ['option' => ['value' => 'other']],
  4443. 'bad',
  4444. '/option',
  4445. '/select'
  4446. ];
  4447. $this->assertHtml($expected, $result);
  4448. $options = [
  4449. ['value' => 'first', 'text' => 'First'],
  4450. ['value' => 'first', 'text' => 'Another First'],
  4451. ];
  4452. $result = $this->Form->select(
  4453. 'Model.field',
  4454. $options,
  4455. ['escape' => false, 'empty' => false]
  4456. );
  4457. $expected = [
  4458. 'select' => ['name' => 'Model[field]'],
  4459. ['option' => ['value' => 'first']],
  4460. 'First',
  4461. '/option',
  4462. ['option' => ['value' => 'first']],
  4463. 'Another First',
  4464. '/option',
  4465. '/select'
  4466. ];
  4467. $this->assertHtml($expected, $result);
  4468. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4469. $result = $this->Form->select(
  4470. 'Model.contact_id',
  4471. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4472. ['escape' => false, 'empty' => 'pick something']
  4473. );
  4474. $expected = [
  4475. 'select' => ['name' => 'Model[contact_id]'],
  4476. ['option' => ['value' => '']], 'pick something', '/option',
  4477. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4478. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4479. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4480. '/select'
  4481. ];
  4482. $this->assertHtml($expected, $result);
  4483. $this->Form->request->data['Model']['field'] = 0;
  4484. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4485. $expected = [
  4486. 'select' => ['name' => 'Model[field]'],
  4487. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4488. ['option' => ['value' => '1']], 'Yes', '/option',
  4489. '/select'
  4490. ];
  4491. $this->assertHtml($expected, $result);
  4492. }
  4493. /**
  4494. * testSelectEscapeHtml method
  4495. *
  4496. * Test that select() escapes HTML.
  4497. *
  4498. * @return void
  4499. */
  4500. public function testSelectEscapeHtml()
  4501. {
  4502. $result = $this->Form->select(
  4503. 'Model.field',
  4504. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4505. ['empty' => false]
  4506. );
  4507. $expected = [
  4508. 'select' => ['name' => 'Model[field]'],
  4509. ['option' => ['value' => 'first']],
  4510. 'first &quot;html&quot; &lt;chars&gt;',
  4511. '/option',
  4512. ['option' => ['value' => 'second']],
  4513. 'value',
  4514. '/option',
  4515. '/select'
  4516. ];
  4517. $this->assertHtml($expected, $result);
  4518. $result = $this->Form->select(
  4519. 'Model.field',
  4520. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4521. ['escape' => false, 'empty' => false]
  4522. );
  4523. $expected = [
  4524. 'select' => ['name' => 'Model[field]'],
  4525. ['option' => ['value' => 'first']],
  4526. 'first "html" <chars>',
  4527. '/option',
  4528. ['option' => ['value' => 'second']],
  4529. 'value',
  4530. '/option',
  4531. '/select'
  4532. ];
  4533. $this->assertHtml($expected, $result);
  4534. }
  4535. /**
  4536. * testSelectRequired method
  4537. *
  4538. * Test select() with required and disabled attributes.
  4539. *
  4540. * @return void
  4541. */
  4542. public function testSelectRequired()
  4543. {
  4544. $this->article['required'] = [
  4545. 'user_id' => true
  4546. ];
  4547. $this->Form->create($this->article);
  4548. $result = $this->Form->select('user_id', ['option A']);
  4549. $expected = [
  4550. 'select' => [
  4551. 'name' => 'user_id',
  4552. 'required' => 'required'
  4553. ],
  4554. ['option' => ['value' => '0']], 'option A', '/option',
  4555. '/select'
  4556. ];
  4557. $this->assertHtml($expected, $result);
  4558. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4559. $expected = [
  4560. 'select' => [
  4561. 'name' => 'user_id',
  4562. 'disabled' => 'disabled'
  4563. ],
  4564. ['option' => ['value' => '0']], 'option A', '/option',
  4565. '/select'
  4566. ];
  4567. $this->assertHtml($expected, $result);
  4568. }
  4569. /**
  4570. * testNestedSelect method
  4571. *
  4572. * Test select element generation with optgroups.
  4573. *
  4574. * @return void
  4575. */
  4576. public function testNestedSelect()
  4577. {
  4578. $result = $this->Form->select(
  4579. 'Model.field',
  4580. [1 => 'One', 2 => 'Two', 'Three' => [
  4581. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4582. ]],
  4583. ['empty' => false]
  4584. );
  4585. $expected = [
  4586. 'select' => ['name' => 'Model[field]'],
  4587. ['option' => ['value' => 1]],
  4588. 'One',
  4589. '/option',
  4590. ['option' => ['value' => 2]],
  4591. 'Two',
  4592. '/option',
  4593. ['optgroup' => ['label' => 'Three']],
  4594. ['option' => ['value' => 3]],
  4595. 'Three',
  4596. '/option',
  4597. ['option' => ['value' => 4]],
  4598. 'Four',
  4599. '/option',
  4600. ['option' => ['value' => 5]],
  4601. 'Five',
  4602. '/option',
  4603. '/optgroup',
  4604. '/select'
  4605. ];
  4606. $this->assertHtml($expected, $result);
  4607. }
  4608. /**
  4609. * testSelectMultiple method
  4610. *
  4611. * Test generation of multiple select elements.
  4612. *
  4613. * @return void
  4614. */
  4615. public function testSelectMultiple()
  4616. {
  4617. $options = ['first', 'second', 'third'];
  4618. $result = $this->Form->select(
  4619. 'Model.multi_field',
  4620. $options,
  4621. ['form' => 'my-form', 'multiple' => true]
  4622. );
  4623. $expected = [
  4624. 'input' => [
  4625. 'type' => 'hidden',
  4626. 'name' => 'Model[multi_field]',
  4627. 'value' => '',
  4628. 'form' => 'my-form',
  4629. ],
  4630. 'select' => [
  4631. 'name' => 'Model[multi_field][]',
  4632. 'multiple' => 'multiple',
  4633. 'form' => 'my-form',
  4634. ],
  4635. ['option' => ['value' => '0']],
  4636. 'first',
  4637. '/option',
  4638. ['option' => ['value' => '1']],
  4639. 'second',
  4640. '/option',
  4641. ['option' => ['value' => '2']],
  4642. 'third',
  4643. '/option',
  4644. '/select'
  4645. ];
  4646. $this->assertHtml($expected, $result);
  4647. $result = $this->Form->select(
  4648. 'Model.multi_field',
  4649. $options,
  4650. ['multiple' => 'multiple', 'form' => 'my-form']
  4651. );
  4652. $this->assertHtml($expected, $result);
  4653. }
  4654. /**
  4655. * testCheckboxZeroValue method
  4656. *
  4657. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4658. *
  4659. * @return void
  4660. */
  4661. public function testCheckboxZeroValue()
  4662. {
  4663. $result = $this->Form->control('User.get_spam', [
  4664. 'type' => 'checkbox',
  4665. 'value' => '0',
  4666. 'hiddenField' => '1',
  4667. ]);
  4668. $expected = [
  4669. 'div' => ['class' => 'input checkbox'],
  4670. 'label' => ['for' => 'user-get-spam'],
  4671. ['input' => [
  4672. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4673. 'value' => '1'
  4674. ]],
  4675. ['input' => [
  4676. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4677. 'value' => '0', 'id' => 'user-get-spam'
  4678. ]],
  4679. 'Get Spam',
  4680. '/label',
  4681. '/div'
  4682. ];
  4683. $this->assertHtml($expected, $result);
  4684. }
  4685. /**
  4686. * testHabtmSelectBox method
  4687. *
  4688. * Test generation of habtm select boxes.
  4689. *
  4690. * @return void
  4691. */
  4692. public function testHabtmSelectBox()
  4693. {
  4694. $this->loadFixtures('Articles');
  4695. $options = [
  4696. 1 => 'blue',
  4697. 2 => 'red',
  4698. 3 => 'green'
  4699. ];
  4700. $tags = [
  4701. new Entity(['id' => 1, 'name' => 'blue']),
  4702. new Entity(['id' => 3, 'name' => 'green'])
  4703. ];
  4704. $article = new Article(['tags' => $tags]);
  4705. $this->Form->create($article);
  4706. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4707. $expected = [
  4708. 'div' => ['class' => 'input select'],
  4709. 'label' => ['for' => 'tags-ids'],
  4710. 'Tags',
  4711. '/label',
  4712. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4713. 'select' => [
  4714. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4715. 'multiple' => 'multiple'
  4716. ],
  4717. ['option' => ['value' => '1', 'selected' => 'selected']],
  4718. 'blue',
  4719. '/option',
  4720. ['option' => ['value' => '2']],
  4721. 'red',
  4722. '/option',
  4723. ['option' => ['value' => '3', 'selected' => 'selected']],
  4724. 'green',
  4725. '/option',
  4726. '/select',
  4727. '/div'
  4728. ];
  4729. $this->assertHtml($expected, $result);
  4730. // make sure only 50 is selected, and not 50f5c0cf
  4731. $options = [
  4732. '1' => 'blue',
  4733. '50f5c0cf' => 'red',
  4734. '50' => 'green'
  4735. ];
  4736. $tags = [
  4737. new Entity(['id' => 1, 'name' => 'blue']),
  4738. new Entity(['id' => 50, 'name' => 'green'])
  4739. ];
  4740. $article = new Article(['tags' => $tags]);
  4741. $this->Form->create($article);
  4742. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4743. $expected = [
  4744. 'div' => ['class' => 'input select'],
  4745. 'label' => ['for' => 'tags-ids'],
  4746. 'Tags',
  4747. '/label',
  4748. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4749. 'select' => [
  4750. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4751. 'multiple' => 'multiple'
  4752. ],
  4753. ['option' => ['value' => '1', 'selected' => 'selected']],
  4754. 'blue',
  4755. '/option',
  4756. ['option' => ['value' => '50f5c0cf']],
  4757. 'red',
  4758. '/option',
  4759. ['option' => ['value' => '50', 'selected' => 'selected']],
  4760. 'green',
  4761. '/option',
  4762. '/select',
  4763. '/div'
  4764. ];
  4765. $this->assertHtml($expected, $result);
  4766. $spacecraft = [
  4767. 1 => 'Orion',
  4768. 2 => 'Helios'
  4769. ];
  4770. $this->View->viewVars['spacecraft'] = $spacecraft;
  4771. $this->Form->create();
  4772. $result = $this->Form->control('spacecraft._ids');
  4773. $expected = [
  4774. 'div' => ['class' => 'input select'],
  4775. 'label' => ['for' => 'spacecraft-ids'],
  4776. 'Spacecraft',
  4777. '/label',
  4778. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4779. 'select' => [
  4780. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4781. 'multiple' => 'multiple'
  4782. ],
  4783. ['option' => ['value' => '1']],
  4784. 'Orion',
  4785. '/option',
  4786. ['option' => ['value' => '2']],
  4787. 'Helios',
  4788. '/option',
  4789. '/select',
  4790. '/div'
  4791. ];
  4792. $this->assertHtml($expected, $result);
  4793. }
  4794. /**
  4795. * testErrorsForBelongsToManySelect method
  4796. *
  4797. * Tests that errors for belongsToMany select fields are being
  4798. * picked up properly.
  4799. *
  4800. * @return void
  4801. */
  4802. public function testErrorsForBelongsToManySelect()
  4803. {
  4804. $spacecraft = [
  4805. 1 => 'Orion',
  4806. 2 => 'Helios'
  4807. ];
  4808. $this->View->viewVars['spacecraft'] = $spacecraft;
  4809. $article = new Article();
  4810. $article->errors('spacecraft', ['Invalid']);
  4811. $this->Form->create($article);
  4812. $result = $this->Form->control('spacecraft._ids');
  4813. $expected = [
  4814. ['div' => ['class' => 'input select error']],
  4815. 'label' => ['for' => 'spacecraft-ids'],
  4816. 'Spacecraft',
  4817. '/label',
  4818. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4819. 'select' => [
  4820. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4821. 'multiple' => 'multiple'
  4822. ],
  4823. ['option' => ['value' => '1']],
  4824. 'Orion',
  4825. '/option',
  4826. ['option' => ['value' => '2']],
  4827. 'Helios',
  4828. '/option',
  4829. '/select',
  4830. ['div' => ['class' => 'error-message']],
  4831. 'Invalid',
  4832. '/div',
  4833. '/div'
  4834. ];
  4835. $this->assertHtml($expected, $result);
  4836. }
  4837. /**
  4838. * testSelectMultipleCheckboxes method
  4839. *
  4840. * Test generation of multi select elements in checkbox format.
  4841. *
  4842. * @return void
  4843. */
  4844. public function testSelectMultipleCheckboxes()
  4845. {
  4846. $result = $this->Form->select(
  4847. 'Model.multi_field',
  4848. ['first', 'second', 'third'],
  4849. ['multiple' => 'checkbox']
  4850. );
  4851. $expected = [
  4852. 'input' => [
  4853. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4854. ],
  4855. ['div' => ['class' => 'checkbox']],
  4856. ['label' => ['for' => 'model-multi-field-0']],
  4857. ['input' => [
  4858. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4859. 'value' => '0', 'id' => 'model-multi-field-0'
  4860. ]],
  4861. 'first',
  4862. '/label',
  4863. '/div',
  4864. ['div' => ['class' => 'checkbox']],
  4865. ['label' => ['for' => 'model-multi-field-1']],
  4866. ['input' => [
  4867. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4868. 'value' => '1', 'id' => 'model-multi-field-1'
  4869. ]],
  4870. 'second',
  4871. '/label',
  4872. '/div',
  4873. ['div' => ['class' => 'checkbox']],
  4874. ['label' => ['for' => 'model-multi-field-2']],
  4875. ['input' => [
  4876. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4877. 'value' => '2', 'id' => 'model-multi-field-2'
  4878. ]],
  4879. 'third',
  4880. '/label',
  4881. '/div'
  4882. ];
  4883. $this->assertHtml($expected, $result);
  4884. $result = $this->Form->select(
  4885. 'Model.multi_field',
  4886. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4887. ['multiple' => 'checkbox']
  4888. );
  4889. $expected = [
  4890. 'input' => [
  4891. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4892. ],
  4893. ['div' => ['class' => 'checkbox']],
  4894. ['label' => ['for' => 'model-multi-field-a+']],
  4895. ['input' => [
  4896. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4897. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  4898. ]],
  4899. 'first',
  4900. '/label',
  4901. '/div',
  4902. ['div' => ['class' => 'checkbox']],
  4903. ['label' => ['for' => 'model-multi-field-a++']],
  4904. ['input' => [
  4905. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4906. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  4907. ]],
  4908. 'second',
  4909. '/label',
  4910. '/div',
  4911. ['div' => ['class' => 'checkbox']],
  4912. ['label' => ['for' => 'model-multi-field-a+++']],
  4913. ['input' => [
  4914. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4915. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  4916. ]],
  4917. 'third',
  4918. '/label',
  4919. '/div'
  4920. ];
  4921. $this->assertHtml($expected, $result);
  4922. $result = $this->Form->select(
  4923. 'Model.multi_field',
  4924. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4925. ['multiple' => 'checkbox']
  4926. );
  4927. $expected = [
  4928. 'input' => [
  4929. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4930. ],
  4931. ['div' => ['class' => 'checkbox']],
  4932. ['label' => ['for' => 'model-multi-field-a-b']],
  4933. ['input' => [
  4934. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4935. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  4936. ]],
  4937. 'first',
  4938. '/label',
  4939. '/div',
  4940. ['div' => ['class' => 'checkbox']],
  4941. ['label' => ['for' => 'model-multi-field-a-b1']],
  4942. ['input' => [
  4943. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4944. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  4945. ]],
  4946. 'second',
  4947. '/label',
  4948. '/div',
  4949. ['div' => ['class' => 'checkbox']],
  4950. ['label' => ['for' => 'model-multi-field-a-b2']],
  4951. ['input' => [
  4952. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4953. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  4954. ]],
  4955. 'third',
  4956. '/label',
  4957. '/div'
  4958. ];
  4959. $this->assertHtml($expected, $result);
  4960. }
  4961. /**
  4962. * testSelectMultipleCheckboxRequestData method
  4963. *
  4964. * Ensure that multiCheckbox reads from the request data.
  4965. *
  4966. * @return void
  4967. */
  4968. public function testSelectMultipleCheckboxRequestData()
  4969. {
  4970. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  4971. $result = $this->Form->select(
  4972. 'Model.tags',
  4973. ['1' => 'first', 'Array' => 'Array'],
  4974. ['multiple' => 'checkbox']
  4975. );
  4976. $expected = [
  4977. 'input' => [
  4978. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  4979. ],
  4980. ['div' => ['class' => 'checkbox']],
  4981. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  4982. ['input' => [
  4983. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4984. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  4985. ]],
  4986. 'first',
  4987. '/label',
  4988. '/div',
  4989. ['div' => ['class' => 'checkbox']],
  4990. ['label' => ['for' => 'model-tags-array']],
  4991. ['input' => [
  4992. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4993. 'value' => 'Array', 'id' => 'model-tags-array'
  4994. ]],
  4995. 'Array',
  4996. '/label',
  4997. '/div'
  4998. ];
  4999. $this->assertHtml($expected, $result);
  5000. }
  5001. /**
  5002. * testSelectMultipleCheckboxSecurity method
  5003. *
  5004. * Checks the security hash array generated for multiple-input checkbox elements.
  5005. *
  5006. * @return void
  5007. */
  5008. public function testSelectMultipleCheckboxSecurity()
  5009. {
  5010. $this->Form->request->params['_Token'] = 'testKey';
  5011. $this->assertEquals([], $this->Form->fields);
  5012. $this->Form->select(
  5013. 'Model.multi_field',
  5014. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5015. ['multiple' => 'checkbox']
  5016. );
  5017. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5018. $result = $this->Form->secure($this->Form->fields);
  5019. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  5020. $this->assertRegExp('/"' . $key . '"/', $result);
  5021. }
  5022. /**
  5023. * testSelectMultipleSecureWithNoOptions method
  5024. *
  5025. * Multiple select elements should always be secured as they always participate
  5026. * in the POST data.
  5027. *
  5028. * @return void
  5029. */
  5030. public function testSelectMultipleSecureWithNoOptions()
  5031. {
  5032. $this->assertEquals([], $this->Form->fields);
  5033. $this->Form->select(
  5034. 'Model.select',
  5035. [],
  5036. ['multiple' => true]
  5037. );
  5038. $this->assertEquals(['Model.select'], $this->Form->fields);
  5039. }
  5040. /**
  5041. * testSelectNoSecureWithNoOptions method
  5042. *
  5043. * When a select box has no options it should not be added to the fields list
  5044. * as it always fail post validation.
  5045. *
  5046. * @return void
  5047. */
  5048. public function testSelectNoSecureWithNoOptions()
  5049. {
  5050. $this->Form->request->params['_Token'] = 'testkey';
  5051. $this->assertEquals([], $this->Form->fields);
  5052. $this->Form->select(
  5053. 'Model.select',
  5054. []
  5055. );
  5056. $this->assertEquals([], $this->Form->fields);
  5057. $this->Form->select(
  5058. 'Model.user_id',
  5059. [],
  5060. ['empty' => true]
  5061. );
  5062. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5063. }
  5064. /**
  5065. * testControlMultipleCheckboxes method
  5066. *
  5067. * Test control() resulting in multi select elements being generated.
  5068. *
  5069. * @return void
  5070. */
  5071. public function testControlMultipleCheckboxes()
  5072. {
  5073. $result = $this->Form->control('Model.multi_field', [
  5074. 'options' => ['first', 'second', 'third'],
  5075. 'multiple' => 'checkbox'
  5076. ]);
  5077. $expected = [
  5078. ['div' => ['class' => 'input select']],
  5079. ['label' => ['for' => 'model-multi-field']],
  5080. 'Multi Field',
  5081. '/label',
  5082. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5083. ['div' => ['class' => 'checkbox']],
  5084. ['label' => ['for' => 'model-multi-field-0']],
  5085. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5086. 'first',
  5087. '/label',
  5088. '/div',
  5089. ['div' => ['class' => 'checkbox']],
  5090. ['label' => ['for' => 'model-multi-field-1']],
  5091. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5092. 'second',
  5093. '/label',
  5094. '/div',
  5095. ['div' => ['class' => 'checkbox']],
  5096. ['label' => ['for' => 'model-multi-field-2']],
  5097. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5098. 'third',
  5099. '/label',
  5100. '/div',
  5101. '/div'
  5102. ];
  5103. $this->assertHtml($expected, $result);
  5104. $result = $this->Form->control('Model.multi_field', [
  5105. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5106. 'multiple' => 'checkbox'
  5107. ]);
  5108. $expected = [
  5109. ['div' => ['class' => 'input select']],
  5110. ['label' => ['for' => 'model-multi-field']],
  5111. 'Multi Field',
  5112. '/label',
  5113. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5114. ['div' => ['class' => 'checkbox']],
  5115. ['label' => ['for' => 'model-multi-field-a']],
  5116. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5117. 'first',
  5118. '/label',
  5119. '/div',
  5120. ['div' => ['class' => 'checkbox']],
  5121. ['label' => ['for' => 'model-multi-field-b']],
  5122. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5123. 'second',
  5124. '/label',
  5125. '/div',
  5126. ['div' => ['class' => 'checkbox']],
  5127. ['label' => ['for' => 'model-multi-field-c']],
  5128. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5129. 'third',
  5130. '/label',
  5131. '/div',
  5132. '/div'
  5133. ];
  5134. $this->assertHtml($expected, $result);
  5135. }
  5136. /**
  5137. * testSelectHiddenFieldOmission method
  5138. *
  5139. * Test that select() with 'hiddenField' => false omits the hidden field.
  5140. *
  5141. * @return void
  5142. */
  5143. public function testSelectHiddenFieldOmission()
  5144. {
  5145. $result = $this->Form->select(
  5146. 'Model.multi_field',
  5147. ['first', 'second'],
  5148. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5149. );
  5150. $this->assertNotContains('type="hidden"', $result);
  5151. }
  5152. /**
  5153. * testSelectCheckboxMultipleOverrideName method
  5154. *
  5155. * Test that select() with multiple = checkbox works with overriding name attribute.
  5156. *
  5157. * @return void
  5158. */
  5159. public function testSelectCheckboxMultipleOverrideName()
  5160. {
  5161. $result = $this->Form->select('category', ['1', '2'], [
  5162. 'multiple' => 'checkbox',
  5163. 'name' => 'fish',
  5164. ]);
  5165. $expected = [
  5166. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5167. ['div' => ['class' => 'checkbox']],
  5168. ['label' => ['for' => 'fish-0']],
  5169. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5170. '1',
  5171. '/label',
  5172. '/div',
  5173. ['div' => ['class' => 'checkbox']],
  5174. ['label' => ['for' => 'fish-1']],
  5175. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5176. '2',
  5177. '/label',
  5178. '/div'
  5179. ];
  5180. $this->assertHtml($expected, $result);
  5181. $result = $this->Form->multiCheckbox(
  5182. 'category',
  5183. new Collection(['1', '2']),
  5184. ['name' => 'fish']
  5185. );
  5186. $this->assertHtml($expected, $result);
  5187. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5188. 'name' => 'fish',
  5189. ]);
  5190. $this->assertHtml($expected, $result);
  5191. }
  5192. /**
  5193. * testControlMultiCheckbox method
  5194. *
  5195. * Test that control() works with multicheckbox.
  5196. *
  5197. * @return void
  5198. */
  5199. public function testControlMultiCheckbox()
  5200. {
  5201. $result = $this->Form->control('category', [
  5202. 'type' => 'multicheckbox',
  5203. 'options' => ['1', '2'],
  5204. ]);
  5205. $expected = [
  5206. ['div' => ['class' => 'input multicheckbox']],
  5207. '<label',
  5208. 'Category',
  5209. '/label',
  5210. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5211. ['div' => ['class' => 'checkbox']],
  5212. ['label' => ['for' => 'category-0']],
  5213. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5214. '1',
  5215. '/label',
  5216. '/div',
  5217. ['div' => ['class' => 'checkbox']],
  5218. ['label' => ['for' => 'category-1']],
  5219. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5220. '2',
  5221. '/label',
  5222. '/div',
  5223. '/div',
  5224. ];
  5225. $this->assertHtml($expected, $result);
  5226. }
  5227. /**
  5228. * testCheckbox method
  5229. *
  5230. * Test generation of checkboxes.
  5231. *
  5232. * @return void
  5233. */
  5234. public function testCheckbox()
  5235. {
  5236. $result = $this->Form->checkbox('Model.field');
  5237. $expected = [
  5238. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5239. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5240. ];
  5241. $this->assertHtml($expected, $result);
  5242. $result = $this->Form->checkbox('Model.field', [
  5243. 'id' => 'theID',
  5244. 'value' => 'myvalue',
  5245. 'form' => 'my-form',
  5246. ]);
  5247. $expected = [
  5248. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5249. ['input' => [
  5250. 'type' => 'checkbox', 'name' => 'Model[field]',
  5251. 'value' => 'myvalue', 'id' => 'theID',
  5252. 'form' => 'my-form',
  5253. ]]
  5254. ];
  5255. $this->assertHtml($expected, $result);
  5256. }
  5257. /**
  5258. * testCheckboxDefaultValue method
  5259. *
  5260. * Test default value setting on checkbox() method.
  5261. *
  5262. * @return void
  5263. */
  5264. public function testCheckboxDefaultValue()
  5265. {
  5266. $this->Form->request->data['Model']['field'] = false;
  5267. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5268. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5269. $this->assertHtml($expected, $result);
  5270. unset($this->Form->request->data['Model']['field']);
  5271. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5272. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5273. $this->assertHtml($expected, $result);
  5274. $this->Form->request->data['Model']['field'] = true;
  5275. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5276. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5277. $this->assertHtml($expected, $result);
  5278. unset($this->Form->request->data['Model']['field']);
  5279. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5280. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5281. $this->assertHtml($expected, $result);
  5282. $Articles = TableRegistry::get('Articles');
  5283. $Articles->schema()->addColumn(
  5284. 'published',
  5285. ['type' => 'boolean', 'null' => false, 'default' => true]
  5286. );
  5287. $this->Form->create($Articles->newEntity());
  5288. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5289. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5290. $this->assertHtml($expected, $result);
  5291. }
  5292. /**
  5293. * testCheckboxCheckedAndError method
  5294. *
  5295. * Test checkbox being checked or having errors.
  5296. *
  5297. * @return void
  5298. */
  5299. public function testCheckboxCheckedAndError()
  5300. {
  5301. $this->article['errors'] = [
  5302. 'published' => true
  5303. ];
  5304. $this->Form->request->data['published'] = 'myvalue';
  5305. $this->Form->create($this->article);
  5306. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5307. $expected = [
  5308. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5309. ['input' => [
  5310. 'type' => 'checkbox',
  5311. 'name' => 'published',
  5312. 'value' => 'myvalue',
  5313. 'id' => 'theID',
  5314. 'checked' => 'checked',
  5315. 'class' => 'form-error'
  5316. ]]
  5317. ];
  5318. $this->assertHtml($expected, $result);
  5319. $this->Form->request->data['published'] = '';
  5320. $result = $this->Form->checkbox('published');
  5321. $expected = [
  5322. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5323. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5324. ];
  5325. $this->assertHtml($expected, $result);
  5326. }
  5327. /**
  5328. * testCheckboxCustomNameAttribute method
  5329. *
  5330. * Test checkbox() with a custom name attribute.
  5331. *
  5332. * @return void
  5333. */
  5334. public function testCheckboxCustomNameAttribute()
  5335. {
  5336. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5337. $expected = [
  5338. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5339. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5340. ];
  5341. $this->assertHtml($expected, $result);
  5342. }
  5343. /**
  5344. * testCheckboxHiddenField method
  5345. *
  5346. * Test that the hidden input for checkboxes can be omitted or set to a
  5347. * specific value.
  5348. *
  5349. * @return void
  5350. */
  5351. public function testCheckboxHiddenField()
  5352. {
  5353. $result = $this->Form->checkbox('UserForm.something', [
  5354. 'hiddenField' => false
  5355. ]);
  5356. $expected = [
  5357. 'input' => [
  5358. 'type' => 'checkbox',
  5359. 'name' => 'UserForm[something]',
  5360. 'value' => '1'
  5361. ],
  5362. ];
  5363. $this->assertHtml($expected, $result);
  5364. $result = $this->Form->checkbox('UserForm.something', [
  5365. 'value' => 'Y',
  5366. 'hiddenField' => 'N',
  5367. ]);
  5368. $expected = [
  5369. ['input' => [
  5370. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5371. 'value' => 'N'
  5372. ]],
  5373. ['input' => [
  5374. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5375. 'value' => 'Y'
  5376. ]],
  5377. ];
  5378. $this->assertHtml($expected, $result);
  5379. }
  5380. /**
  5381. * testTime method
  5382. *
  5383. * Test the time type.
  5384. *
  5385. * @return void
  5386. */
  5387. public function testTime()
  5388. {
  5389. $result = $this->Form->time('start_time', [
  5390. 'timeFormat' => 12,
  5391. 'interval' => 5,
  5392. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5393. ]);
  5394. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5395. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5396. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5397. $this->assertNotContains('year', $result);
  5398. $this->assertNotContains('month', $result);
  5399. $this->assertNotContains('day', $result);
  5400. $result = $this->Form->time('start_time', [
  5401. 'timeFormat' => 12,
  5402. 'interval' => 5,
  5403. 'value' => '2014-03-08 16:30:00'
  5404. ]);
  5405. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5406. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5407. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5408. $this->assertNotContains('year', $result);
  5409. $this->assertNotContains('month', $result);
  5410. $this->assertNotContains('day', $result);
  5411. }
  5412. /**
  5413. * testTimeFormat24NoMeridian method
  5414. *
  5415. * Ensure that timeFormat=24 has no merdian.
  5416. *
  5417. * @return void.
  5418. */
  5419. public function testTimeFormat24NoMeridian()
  5420. {
  5421. $result = $this->Form->time('start_time', [
  5422. 'timeFormat' => 24,
  5423. 'interval' => 5,
  5424. 'value' => '2014-03-08 16:30:00'
  5425. ]);
  5426. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5427. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5428. $this->assertNotContains('meridian', $result);
  5429. $this->assertNotContains('pm', $result);
  5430. $this->assertNotContains('year', $result);
  5431. $this->assertNotContains('month', $result);
  5432. $this->assertNotContains('day', $result);
  5433. }
  5434. /**
  5435. * testDate method
  5436. *
  5437. * Test the date type.
  5438. *
  5439. * @return void
  5440. */
  5441. public function testDate()
  5442. {
  5443. $result = $this->Form->date('start_day', [
  5444. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5445. ]);
  5446. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5447. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5448. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5449. $this->assertNotContains('hour', $result);
  5450. $this->assertNotContains('minute', $result);
  5451. $this->assertNotContains('second', $result);
  5452. $this->assertNotContains('meridian', $result);
  5453. }
  5454. /**
  5455. * testDateTime method
  5456. *
  5457. * Test generation of date/time select elements.
  5458. *
  5459. * @return void
  5460. */
  5461. public function testDateTime()
  5462. {
  5463. extract($this->dateRegex);
  5464. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5465. $now = strtotime('now');
  5466. $expected = [
  5467. ['select' => ['name' => 'Contact[date][year]']],
  5468. ['option' => ['value' => '']],
  5469. '/option',
  5470. $yearsRegex,
  5471. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5472. date('Y', $now),
  5473. '/option',
  5474. '*/select',
  5475. ['select' => ['name' => 'Contact[date][month]']],
  5476. ['option' => ['value' => '']],
  5477. '/option',
  5478. $monthsRegex,
  5479. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5480. date('F', $now),
  5481. '/option',
  5482. '*/select',
  5483. ['select' => ['name' => 'Contact[date][day]']],
  5484. ['option' => ['value' => '']],
  5485. '/option',
  5486. $daysRegex,
  5487. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5488. date('j', $now),
  5489. '/option',
  5490. '*/select',
  5491. ['select' => ['name' => 'Contact[date][hour]']],
  5492. ['option' => ['value' => '']],
  5493. '/option',
  5494. $hoursRegex,
  5495. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5496. date('G', $now),
  5497. '/option',
  5498. '*/select',
  5499. ['select' => ['name' => 'Contact[date][minute]']],
  5500. ['option' => ['value' => '']],
  5501. '/option',
  5502. $minutesRegex,
  5503. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5504. date('i', $now),
  5505. '/option',
  5506. '*/select',
  5507. ];
  5508. $this->assertHtml($expected, $result);
  5509. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5510. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5511. $now = strtotime('now');
  5512. $expected = [
  5513. ['select' => ['name' => 'Contact[date][year]']],
  5514. $yearsRegex,
  5515. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5516. date('Y', $now),
  5517. '/option',
  5518. '*/select',
  5519. ['select' => ['name' => 'Contact[date][month]']],
  5520. $monthsRegex,
  5521. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5522. date('F', $now),
  5523. '/option',
  5524. '*/select',
  5525. ['select' => ['name' => 'Contact[date][day]']],
  5526. $daysRegex,
  5527. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5528. date('j', $now),
  5529. '/option',
  5530. '*/select',
  5531. ['select' => ['name' => 'Contact[date][hour]']],
  5532. $hoursRegex,
  5533. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5534. date('G', $now),
  5535. '/option',
  5536. '*/select',
  5537. ['select' => ['name' => 'Contact[date][minute]']],
  5538. $minutesRegex,
  5539. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5540. date('i', $now),
  5541. '/option',
  5542. '*/select',
  5543. ];
  5544. $this->assertHtml($expected, $result);
  5545. }
  5546. /**
  5547. * testDateTimeSecured method
  5548. *
  5549. * Test that datetime fields are added to protected fields list.
  5550. *
  5551. * @return void
  5552. */
  5553. public function testDateTimeSecured()
  5554. {
  5555. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5556. $this->Form->dateTime('Contact.date');
  5557. $expected = [
  5558. 'Contact.date.year',
  5559. 'Contact.date.month',
  5560. 'Contact.date.day',
  5561. 'Contact.date.hour',
  5562. 'Contact.date.minute',
  5563. ];
  5564. $this->assertEquals($expected, $this->Form->fields);
  5565. $this->Form->fields = [];
  5566. $this->Form->date('Contact.published');
  5567. $expected = [
  5568. 'Contact.published.year',
  5569. 'Contact.published.month',
  5570. 'Contact.published.day',
  5571. ];
  5572. $this->assertEquals($expected, $this->Form->fields);
  5573. }
  5574. /**
  5575. * testDateTimeSecuredDisabled method
  5576. *
  5577. * Test that datetime fields are added to protected fields list.
  5578. *
  5579. * @return void
  5580. */
  5581. public function testDateTimeSecuredDisabled()
  5582. {
  5583. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5584. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5585. $expected = [];
  5586. $this->assertEquals($expected, $this->Form->fields);
  5587. $this->Form->fields = [];
  5588. $this->Form->date('Contact.published', ['secure' => false]);
  5589. $expected = [];
  5590. $this->assertEquals($expected, $this->Form->fields);
  5591. }
  5592. /**
  5593. * testDatetimeEmpty method
  5594. *
  5595. * Test empty defaulting to true for datetime.
  5596. *
  5597. * @return void
  5598. */
  5599. public function testDatetimeEmpty()
  5600. {
  5601. extract($this->dateRegex);
  5602. $result = $this->Form->dateTime('Contact.date', [
  5603. 'timeFormat' => 12,
  5604. 'empty' => true,
  5605. 'default' => true
  5606. ]);
  5607. $expected = [
  5608. ['select' => ['name' => 'Contact[date][year]']],
  5609. $yearsRegex,
  5610. ['option' => ['value' => '']],
  5611. '/option',
  5612. '*/select',
  5613. ['select' => ['name' => 'Contact[date][month]']],
  5614. $monthsRegex,
  5615. ['option' => ['value' => '']],
  5616. '/option',
  5617. '*/select',
  5618. ['select' => ['name' => 'Contact[date][day]']],
  5619. $daysRegex,
  5620. ['option' => ['value' => '']],
  5621. '/option',
  5622. '*/select',
  5623. ['select' => ['name' => 'Contact[date][hour]']],
  5624. $hoursRegex,
  5625. ['option' => ['value' => '']],
  5626. '/option',
  5627. '*/select',
  5628. ['select' => ['name' => 'Contact[date][minute]']],
  5629. $minutesRegex,
  5630. ['option' => ['value' => '']],
  5631. '/option',
  5632. '*/select',
  5633. ['select' => ['name' => 'Contact[date][meridian]']],
  5634. $meridianRegex,
  5635. ['option' => ['value' => '']],
  5636. '/option',
  5637. '*/select'
  5638. ];
  5639. $this->assertHtml($expected, $result);
  5640. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5641. }
  5642. /**
  5643. * testDatetimeMinuteInterval method
  5644. *
  5645. * Test datetime with interval option.
  5646. *
  5647. * @return void
  5648. */
  5649. public function testDatetimeMinuteInterval()
  5650. {
  5651. extract($this->dateRegex);
  5652. $result = $this->Form->dateTime('Contact.date', [
  5653. 'interval' => 5,
  5654. 'value' => ''
  5655. ]);
  5656. $expected = [
  5657. ['select' => ['name' => 'Contact[date][year]']],
  5658. $yearsRegex,
  5659. ['option' => ['selected' => 'selected', 'value' => '']],
  5660. '/option',
  5661. '*/select',
  5662. ['select' => ['name' => 'Contact[date][month]']],
  5663. $monthsRegex,
  5664. ['option' => ['selected' => 'selected', 'value' => '']],
  5665. '/option',
  5666. '*/select',
  5667. ['select' => ['name' => 'Contact[date][day]']],
  5668. $daysRegex,
  5669. ['option' => ['selected' => 'selected', 'value' => '']],
  5670. '/option',
  5671. '*/select',
  5672. ['select' => ['name' => 'Contact[date][hour]']],
  5673. $hoursRegex,
  5674. ['option' => ['selected' => 'selected', 'value' => '']],
  5675. '/option',
  5676. '*/select',
  5677. ['select' => ['name' => 'Contact[date][minute]']],
  5678. $minutesRegex,
  5679. ['option' => ['selected' => 'selected', 'value' => '']],
  5680. '/option',
  5681. ['option' => ['value' => '00']],
  5682. '00',
  5683. '/option',
  5684. ['option' => ['value' => '05']],
  5685. '05',
  5686. '/option',
  5687. ['option' => ['value' => '10']],
  5688. '10',
  5689. '/option',
  5690. '*/select',
  5691. ];
  5692. $this->assertHtml($expected, $result);
  5693. }
  5694. /**
  5695. * testDateTimeRounding method
  5696. *
  5697. * Test dateTime with rounding.
  5698. *
  5699. * @return void
  5700. */
  5701. public function testDateTimeRounding()
  5702. {
  5703. $this->Form->request->data['Contact'] = [
  5704. 'date' => [
  5705. 'day' => '13',
  5706. 'month' => '12',
  5707. 'year' => '2010',
  5708. 'hour' => '04',
  5709. 'minute' => '19',
  5710. 'meridian' => 'AM'
  5711. ]
  5712. ];
  5713. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5714. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5715. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5716. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5717. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5718. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5719. }
  5720. /**
  5721. * testDatetimeWithDefault method
  5722. *
  5723. * Test that datetime() and default values work.
  5724. *
  5725. * @return void
  5726. */
  5727. public function testDatetimeWithDefault()
  5728. {
  5729. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5730. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5731. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5732. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5733. $result = $this->Form->dateTime('Contact.updated', [
  5734. 'default' => '2009-06-01 11:15:30'
  5735. ]);
  5736. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5737. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5738. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5739. }
  5740. /**
  5741. * testDateTimeAllZeros method
  5742. *
  5743. * Test datetime() with all zeros.
  5744. *
  5745. * @return void
  5746. */
  5747. public function testDateTimeAllZeros()
  5748. {
  5749. $result = $this->Form->dateTime('Contact.date', [
  5750. 'timeFormat' => false,
  5751. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5752. 'value' => '0000-00-00'
  5753. ]);
  5754. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5755. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5756. }
  5757. /**
  5758. * testDateTimeEmptyAsArray method
  5759. *
  5760. * @return void
  5761. */
  5762. public function testDateTimeEmptyAsArray()
  5763. {
  5764. $result = $this->Form->dateTime('Contact.date', [
  5765. 'empty' => [
  5766. 'day' => 'DAY',
  5767. 'month' => 'MONTH',
  5768. 'year' => 'YEAR',
  5769. 'hour' => 'HOUR',
  5770. 'minute' => 'MINUTE',
  5771. 'meridian' => false
  5772. ],
  5773. 'default' => true
  5774. ]);
  5775. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5776. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5777. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5778. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5779. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5780. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5781. $result = $this->Form->dateTime('Contact.date', [
  5782. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5783. 'default' => true
  5784. ]);
  5785. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5786. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5787. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5788. }
  5789. /**
  5790. * testFormDateTimeMulti method
  5791. *
  5792. * Test multiple datetime element generation.
  5793. *
  5794. * @return void
  5795. */
  5796. public function testFormDateTimeMulti()
  5797. {
  5798. extract($this->dateRegex);
  5799. $result = $this->Form->dateTime('Contact.1.updated');
  5800. $this->assertContains('Contact[1][updated][month]', $result);
  5801. $this->assertContains('Contact[1][updated][day]', $result);
  5802. $this->assertContains('Contact[1][updated][year]', $result);
  5803. $this->assertContains('Contact[1][updated][hour]', $result);
  5804. $this->assertContains('Contact[1][updated][minute]', $result);
  5805. }
  5806. /**
  5807. * testDateTimeLabelIdMatchesFirstControl method
  5808. *
  5809. * When changing the date format, the label should always focus the first select box when
  5810. * clicked.
  5811. *
  5812. * @return void
  5813. */
  5814. public function testDateTimeLabelIdMatchesFirstControl()
  5815. {
  5816. $result = $this->Form->control('Model.date', ['type' => 'date']);
  5817. $this->assertContains('<label>Date</label>', $result);
  5818. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5819. $this->assertContains('<label>Date</label>', $result);
  5820. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5821. $this->assertContains('<label>Date</label>', $result);
  5822. }
  5823. /**
  5824. * testDateTimeSecondOptions method
  5825. *
  5826. * Test datetime second=true.
  5827. *
  5828. * @return void
  5829. */
  5830. public function testDateTimeSecondOptions()
  5831. {
  5832. $result = $this->Form->dateTime('updated', ['second' => true]);
  5833. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5834. $result = $this->Form->dateTime('updated', ['second' => []]);
  5835. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5836. $result = $this->Form->dateTime('updated', ['second' => null]);
  5837. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5838. $result = $this->Form->dateTime('updated', ['second' => false]);
  5839. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5840. }
  5841. /**
  5842. * testMonth method
  5843. *
  5844. * Test generation of a month input.
  5845. *
  5846. * @return void
  5847. */
  5848. public function testMonth()
  5849. {
  5850. $result = $this->Form->month('Model.field', ['value' => '']);
  5851. $expected = [
  5852. ['select' => ['name' => 'Model[field][month]']],
  5853. ['option' => ['value' => '', 'selected' => 'selected']],
  5854. '/option',
  5855. ['option' => ['value' => '01']],
  5856. date('F', strtotime('2008-01-01 00:00:00')),
  5857. '/option',
  5858. ['option' => ['value' => '02']],
  5859. date('F', strtotime('2008-02-01 00:00:00')),
  5860. '/option',
  5861. '*/select',
  5862. ];
  5863. $this->assertHtml($expected, $result);
  5864. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  5865. $expected = [
  5866. ['select' => ['name' => 'Model[field][month]']],
  5867. ['option' => ['selected' => 'selected', 'value' => '']],
  5868. '/option',
  5869. ['option' => ['value' => '01']],
  5870. date('F', strtotime('2008-01-01 00:00:00')),
  5871. '/option',
  5872. ['option' => ['value' => '02']],
  5873. date('F', strtotime('2008-02-01 00:00:00')),
  5874. '/option',
  5875. '*/select',
  5876. ];
  5877. $this->assertHtml($expected, $result);
  5878. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  5879. $expected = [
  5880. ['select' => ['name' => 'Model[field][month]']],
  5881. ['option' => ['selected' => 'selected', 'value' => '']],
  5882. '/option',
  5883. ['option' => ['value' => '01']],
  5884. '1',
  5885. '/option',
  5886. ['option' => ['value' => '02']],
  5887. '2',
  5888. '/option',
  5889. '*/select',
  5890. ];
  5891. $this->assertHtml($expected, $result);
  5892. $monthNames = [
  5893. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5894. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  5895. ];
  5896. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  5897. $expected = [
  5898. ['select' => ['name' => 'Model[field][month]']],
  5899. ['option' => ['value' => '']],
  5900. '/option',
  5901. ['option' => ['value' => '01', 'selected' => 'selected']],
  5902. 'Jan',
  5903. '/option',
  5904. ['option' => ['value' => '02']],
  5905. 'Feb',
  5906. '/option',
  5907. '*/select',
  5908. ];
  5909. $this->assertHtml($expected, $result);
  5910. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5911. $result = $this->Form->month('Project.release');
  5912. $expected = [
  5913. ['select' => ['name' => 'Project[release][month]']],
  5914. ['option' => ['value' => '']],
  5915. '/option',
  5916. ['option' => ['value' => '01']],
  5917. 'January',
  5918. '/option',
  5919. ['option' => ['value' => '02', 'selected' => 'selected']],
  5920. 'February',
  5921. '/option',
  5922. '*/select',
  5923. ];
  5924. $this->assertHtml($expected, $result);
  5925. $result = $this->Form->month('Contact.published', [
  5926. 'empty' => 'Published on',
  5927. ]);
  5928. $this->assertContains('Published on', $result);
  5929. }
  5930. /**
  5931. * testDay method
  5932. *
  5933. * Test generation of a day input.
  5934. *
  5935. * @return void
  5936. */
  5937. public function testDay()
  5938. {
  5939. extract($this->dateRegex);
  5940. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  5941. $expected = [
  5942. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  5943. ['option' => ['selected' => 'selected', 'value' => '']],
  5944. '/option',
  5945. ['option' => ['value' => '01']],
  5946. '1',
  5947. '/option',
  5948. ['option' => ['value' => '02']],
  5949. '2',
  5950. '/option',
  5951. $daysRegex,
  5952. '/select',
  5953. ];
  5954. $this->assertHtml($expected, $result);
  5955. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5956. $result = $this->Form->day('Model.field');
  5957. $expected = [
  5958. ['select' => ['name' => 'Model[field][day]']],
  5959. ['option' => ['value' => '']],
  5960. '/option',
  5961. ['option' => ['value' => '01']],
  5962. '1',
  5963. '/option',
  5964. ['option' => ['value' => '02']],
  5965. '2',
  5966. '/option',
  5967. $daysRegex,
  5968. ['option' => ['value' => '10', 'selected' => 'selected']],
  5969. '10',
  5970. '/option',
  5971. $daysRegex,
  5972. '/select',
  5973. ];
  5974. $this->assertHtml($expected, $result);
  5975. $this->Form->request->data['Model']['field'] = '';
  5976. $result = $this->Form->day('Model.field', ['value' => '10']);
  5977. $expected = [
  5978. ['select' => ['name' => 'Model[field][day]']],
  5979. ['option' => ['value' => '']],
  5980. '/option',
  5981. ['option' => ['value' => '01']],
  5982. '1',
  5983. '/option',
  5984. ['option' => ['value' => '02']],
  5985. '2',
  5986. '/option',
  5987. $daysRegex,
  5988. ['option' => ['value' => '10', 'selected' => 'selected']],
  5989. '10',
  5990. '/option',
  5991. $daysRegex,
  5992. '/select',
  5993. ];
  5994. $this->assertHtml($expected, $result);
  5995. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5996. $result = $this->Form->day('Project.release');
  5997. $expected = [
  5998. ['select' => ['name' => 'Project[release][day]']],
  5999. ['option' => ['value' => '']],
  6000. '/option',
  6001. ['option' => ['value' => '01']],
  6002. '1',
  6003. '/option',
  6004. ['option' => ['value' => '02']],
  6005. '2',
  6006. '/option',
  6007. $daysRegex,
  6008. ['option' => ['value' => '10', 'selected' => 'selected']],
  6009. '10',
  6010. '/option',
  6011. $daysRegex,
  6012. '/select',
  6013. ];
  6014. $this->assertHtml($expected, $result);
  6015. $result = $this->Form->day('Contact.published', [
  6016. 'empty' => 'Published on',
  6017. ]);
  6018. $this->assertContains('Published on', $result);
  6019. }
  6020. /**
  6021. * testMinute method
  6022. *
  6023. * Test generation of a minute input.
  6024. *
  6025. * @return void
  6026. */
  6027. public function testMinute()
  6028. {
  6029. extract($this->dateRegex);
  6030. $result = $this->Form->minute('Model.field', ['value' => '']);
  6031. $expected = [
  6032. ['select' => ['name' => 'Model[field][minute]']],
  6033. ['option' => ['selected' => 'selected', 'value' => '']],
  6034. '/option',
  6035. ['option' => ['value' => '00']],
  6036. '00',
  6037. '/option',
  6038. ['option' => ['value' => '01']],
  6039. '01',
  6040. '/option',
  6041. ['option' => ['value' => '02']],
  6042. '02',
  6043. '/option',
  6044. $minutesRegex,
  6045. '/select',
  6046. ];
  6047. $this->assertHtml($expected, $result);
  6048. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6049. $result = $this->Form->minute('Model.field');
  6050. $expected = [
  6051. ['select' => ['name' => 'Model[field][minute]']],
  6052. ['option' => ['value' => '']],
  6053. '/option',
  6054. ['option' => ['value' => '00']],
  6055. '00',
  6056. '/option',
  6057. ['option' => ['value' => '01']],
  6058. '01',
  6059. '/option',
  6060. ['option' => ['value' => '02']],
  6061. '02',
  6062. '/option',
  6063. $minutesRegex,
  6064. ['option' => ['value' => '12', 'selected' => 'selected']],
  6065. '12',
  6066. '/option',
  6067. $minutesRegex,
  6068. '/select',
  6069. ];
  6070. $this->assertHtml($expected, $result);
  6071. $this->Form->request->data['Model']['field'] = '';
  6072. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6073. $expected = [
  6074. ['select' => ['name' => 'Model[field][minute]']],
  6075. ['option' => ['selected' => 'selected', 'value' => '']],
  6076. '/option',
  6077. ['option' => ['value' => '00']],
  6078. '00',
  6079. '/option',
  6080. ['option' => ['value' => '05']],
  6081. '05',
  6082. '/option',
  6083. ['option' => ['value' => '10']],
  6084. '10',
  6085. '/option',
  6086. $minutesRegex,
  6087. '/select',
  6088. ];
  6089. $this->assertHtml($expected, $result);
  6090. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6091. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6092. $expected = [
  6093. ['select' => ['name' => 'Model[field][minute]']],
  6094. ['option' => ['value' => '']],
  6095. '/option',
  6096. ['option' => ['value' => '00']],
  6097. '00',
  6098. '/option',
  6099. ['option' => ['value' => '05']],
  6100. '05',
  6101. '/option',
  6102. ['option' => ['value' => '10', 'selected' => 'selected']],
  6103. '10',
  6104. '/option',
  6105. $minutesRegex,
  6106. '/select',
  6107. ];
  6108. $this->assertHtml($expected, $result);
  6109. }
  6110. /**
  6111. * testMeridian method
  6112. *
  6113. * Test generating an input for the meridian.
  6114. *
  6115. * @return void
  6116. */
  6117. public function testMeridian()
  6118. {
  6119. extract($this->dateRegex);
  6120. $now = new \DateTime();
  6121. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6122. $expected = [
  6123. ['select' => ['name' => 'Model[field][meridian]']],
  6124. ['option' => ['value' => '']],
  6125. '/option',
  6126. $meridianRegex,
  6127. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6128. $now->format('a'),
  6129. '/option',
  6130. '*/select'
  6131. ];
  6132. $this->assertHtml($expected, $result);
  6133. }
  6134. /**
  6135. * testHour method
  6136. *
  6137. * Test generation of an hour input.
  6138. *
  6139. * @return void
  6140. */
  6141. public function testHour()
  6142. {
  6143. extract($this->dateRegex);
  6144. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6145. $expected = [
  6146. ['select' => ['name' => 'Model[field][hour]']],
  6147. ['option' => ['selected' => 'selected', 'value' => '']],
  6148. '/option',
  6149. ['option' => ['value' => '01']],
  6150. '1',
  6151. '/option',
  6152. ['option' => ['value' => '02']],
  6153. '2',
  6154. '/option',
  6155. $hoursRegex,
  6156. '/select',
  6157. ];
  6158. $this->assertHtml($expected, $result);
  6159. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6160. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6161. $expected = [
  6162. ['select' => ['name' => 'Model[field][hour]']],
  6163. ['option' => ['value' => '']],
  6164. '/option',
  6165. ['option' => ['value' => '01']],
  6166. '1',
  6167. '/option',
  6168. ['option' => ['value' => '02']],
  6169. '2',
  6170. '/option',
  6171. $hoursRegex,
  6172. ['option' => ['value' => '12', 'selected' => 'selected']],
  6173. '12',
  6174. '/option',
  6175. '/select',
  6176. ];
  6177. $this->assertHtml($expected, $result);
  6178. $this->Form->request->data['Model']['field'] = '';
  6179. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6180. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6181. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6182. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6183. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6184. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6185. $expected = [
  6186. ['select' => ['name' => 'Model[field][hour]']],
  6187. ['option' => ['value' => '']],
  6188. '/option',
  6189. ['option' => ['value' => '00', 'selected' => 'selected']],
  6190. '0',
  6191. '/option',
  6192. ['option' => ['value' => '01']],
  6193. '1',
  6194. '/option',
  6195. ['option' => ['value' => '02']],
  6196. '2',
  6197. '/option',
  6198. $hoursRegex,
  6199. '/select',
  6200. ];
  6201. $this->assertHtml($expected, $result);
  6202. unset($this->Form->request->data['Model']['field']);
  6203. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6204. $thisHour = date('H');
  6205. $optValue = date('G');
  6206. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6207. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6208. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6209. $expected = [
  6210. ['select' => ['name' => 'Model[field][hour]']],
  6211. ['option' => ['value' => '']],
  6212. '/option',
  6213. ['option' => ['value' => '00']],
  6214. '0',
  6215. '/option',
  6216. ['option' => ['value' => '01', 'selected' => 'selected']],
  6217. '1',
  6218. '/option',
  6219. ['option' => ['value' => '02']],
  6220. '2',
  6221. '/option',
  6222. $hoursRegex,
  6223. '/select',
  6224. ];
  6225. $this->assertHtml($expected, $result);
  6226. }
  6227. /**
  6228. * testYear method
  6229. *
  6230. * Test generation of a year input.
  6231. *
  6232. * @return void
  6233. */
  6234. public function testYear()
  6235. {
  6236. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6237. $expected = [
  6238. ['select' => ['name' => 'Model[field][year]']],
  6239. ['option' => ['selected' => 'selected', 'value' => '']],
  6240. '/option',
  6241. ['option' => ['value' => '2007']],
  6242. '2007',
  6243. '/option',
  6244. ['option' => ['value' => '2006']],
  6245. '2006',
  6246. '/option',
  6247. '/select',
  6248. ];
  6249. $this->assertHtml($expected, $result);
  6250. $result = $this->Form->year('Model.field', [
  6251. 'value' => '',
  6252. 'minYear' => 2006,
  6253. 'maxYear' => 2007,
  6254. 'orderYear' => 'asc'
  6255. ]);
  6256. $expected = [
  6257. ['select' => ['name' => 'Model[field][year]']],
  6258. ['option' => ['selected' => 'selected', 'value' => '']],
  6259. '/option',
  6260. ['option' => ['value' => '2006']],
  6261. '2006',
  6262. '/option',
  6263. ['option' => ['value' => '2007']],
  6264. '2007',
  6265. '/option',
  6266. '/select',
  6267. ];
  6268. $this->assertHtml($expected, $result);
  6269. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6270. $result = $this->Form->year('Contact.published', [
  6271. 'empty' => false,
  6272. 'minYear' => 2006,
  6273. 'maxYear' => 2007,
  6274. ]);
  6275. $expected = [
  6276. ['select' => ['name' => 'Contact[published][year]']],
  6277. ['option' => ['value' => '2007']],
  6278. '2007',
  6279. '/option',
  6280. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6281. '2006',
  6282. '/option',
  6283. '/select',
  6284. ];
  6285. $this->assertHtml($expected, $result);
  6286. $result = $this->Form->year('Contact.published', [
  6287. 'empty' => 'Published on',
  6288. ]);
  6289. $this->assertContains('Published on', $result);
  6290. }
  6291. /**
  6292. * testControlDatetimePreEpoch method
  6293. *
  6294. * Test minYear being prior to the unix epoch.
  6295. *
  6296. * @return void
  6297. */
  6298. public function testControlDatetimePreEpoch()
  6299. {
  6300. $start = date('Y') - 80;
  6301. $end = date('Y') - 18;
  6302. $result = $this->Form->control('birth_year', [
  6303. 'type' => 'date',
  6304. 'label' => 'Birth Year',
  6305. 'minYear' => $start,
  6306. 'maxYear' => $end,
  6307. 'month' => false,
  6308. 'day' => false,
  6309. ]);
  6310. $this->assertContains('value="' . $start . '">' . $start, $result);
  6311. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6312. $this->assertNotContains('value="00">00', $result);
  6313. }
  6314. /**
  6315. * testYearAutoExpandRange method
  6316. *
  6317. * @return void
  6318. */
  6319. public function testYearAutoExpandRange()
  6320. {
  6321. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6322. $result = $this->Form->year('User.birthday');
  6323. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6324. $result = $matches[1];
  6325. $expected = range(date('Y') + 5, 1930);
  6326. $this->assertEquals($expected, $result);
  6327. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6328. $result = $this->Form->year('Project.release');
  6329. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6330. $result = $matches[1];
  6331. $expected = range(2050, date('Y') - 5);
  6332. $this->assertEquals($expected, $result);
  6333. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6334. $result = $this->Form->year('Project.release', [
  6335. 'minYear' => 1890,
  6336. 'maxYear' => 1900
  6337. ]);
  6338. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6339. $result = $matches[1];
  6340. $expected = range(1900, 1881);
  6341. $this->assertEquals($expected, $result);
  6342. }
  6343. /**
  6344. * testControlDate method
  6345. *
  6346. * Test that control() accepts the type of date and passes options in.
  6347. *
  6348. * @return void
  6349. */
  6350. public function testControlDate()
  6351. {
  6352. $this->Form->request->data = [
  6353. 'month_year' => ['month' => date('m')],
  6354. ];
  6355. $this->Form->create($this->article);
  6356. $result = $this->Form->control('month_year', [
  6357. 'label' => false,
  6358. 'type' => 'date',
  6359. 'minYear' => 2006,
  6360. 'maxYear' => 2008
  6361. ]);
  6362. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6363. $this->assertNotContains('value="2008" selected="selected"', $result);
  6364. }
  6365. /**
  6366. * testControlLabelFalse method
  6367. *
  6368. * Test the label option being set to false.
  6369. *
  6370. * @return void
  6371. */
  6372. public function testControlLabelFalse()
  6373. {
  6374. $this->Form->create($this->article);
  6375. $result = $this->Form->control('title', ['label' => false]);
  6376. $expected = [
  6377. 'div' => ['class' => 'input text required'],
  6378. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6379. '/div'
  6380. ];
  6381. $this->assertHtml($expected, $result);
  6382. }
  6383. /**
  6384. * testControlDateMaxYear method
  6385. *
  6386. * Let's say we want to only allow users born from 2006 to 2008 to register
  6387. * This being the first signup page, we still don't have any data.
  6388. *
  6389. * @return void
  6390. */
  6391. public function testControlDateMaxYear()
  6392. {
  6393. $this->Form->request->data = [];
  6394. $this->Form->create($this->article);
  6395. $result = $this->Form->control('birthday', [
  6396. 'label' => false,
  6397. 'type' => 'date',
  6398. 'minYear' => 2006,
  6399. 'maxYear' => 2008,
  6400. 'default' => true
  6401. ]);
  6402. $this->assertContains('value="2008" selected="selected"', $result);
  6403. $this->assertContains('value="2006"', $result);
  6404. $this->assertNotContains('value="2005"', $result);
  6405. $this->assertNotContains('value="2009"', $result);
  6406. }
  6407. /**
  6408. * testTextArea method
  6409. *
  6410. * Test generation of a textarea input.
  6411. *
  6412. * @return void
  6413. */
  6414. public function testTextArea()
  6415. {
  6416. $this->Form->request->data = ['field' => 'some test data'];
  6417. $result = $this->Form->textarea('field');
  6418. $expected = [
  6419. 'textarea' => ['name' => 'field', 'rows' => 5],
  6420. 'some test data',
  6421. '/textarea',
  6422. ];
  6423. $this->assertHtml($expected, $result);
  6424. $result = $this->Form->textarea('user.bio');
  6425. $expected = [
  6426. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6427. '/textarea',
  6428. ];
  6429. $this->assertHtml($expected, $result);
  6430. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6431. $result = $this->Form->textarea('field');
  6432. $expected = [
  6433. 'textarea' => ['name' => 'field', 'rows' => 5],
  6434. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6435. '/textarea',
  6436. ];
  6437. $this->assertHtml($expected, $result);
  6438. $this->Form->request->data = [
  6439. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6440. ];
  6441. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6442. $expected = [
  6443. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6444. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6445. '/textarea',
  6446. ];
  6447. $this->assertHtml($expected, $result);
  6448. $result = $this->Form->textarea('0.OtherModel.field');
  6449. $expected = [
  6450. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6451. '/textarea'
  6452. ];
  6453. $this->assertHtml($expected, $result);
  6454. }
  6455. /**
  6456. * testTextAreaWithStupidCharacters method
  6457. *
  6458. * Test text area with non-ascii characters.
  6459. *
  6460. * @return void
  6461. */
  6462. public function testTextAreaWithStupidCharacters()
  6463. {
  6464. $result = $this->Form->textarea('Post.content', [
  6465. 'value' => "GREAT®",
  6466. 'rows' => '15',
  6467. 'cols' => '75'
  6468. ]);
  6469. $expected = [
  6470. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6471. 'GREAT®',
  6472. '/textarea',
  6473. ];
  6474. $this->assertHtml($expected, $result);
  6475. }
  6476. /**
  6477. * testTextAreaMaxLength method
  6478. *
  6479. * Test textareas maxlength read from schema.
  6480. *
  6481. * @return void
  6482. */
  6483. public function testTextAreaMaxLength()
  6484. {
  6485. $this->Form->create([
  6486. 'schema' => [
  6487. 'stuff' => ['type' => 'string', 'length' => 10],
  6488. ]
  6489. ]);
  6490. $result = $this->Form->control('other', ['type' => 'textarea']);
  6491. $expected = [
  6492. 'div' => ['class' => 'input textarea'],
  6493. 'label' => ['for' => 'other'],
  6494. 'Other',
  6495. '/label',
  6496. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6497. '/textarea',
  6498. '/div'
  6499. ];
  6500. $this->assertHtml($expected, $result);
  6501. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6502. $expected = [
  6503. 'div' => ['class' => 'input textarea'],
  6504. 'label' => ['for' => 'stuff'],
  6505. 'Stuff',
  6506. '/label',
  6507. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6508. '/textarea',
  6509. '/div'
  6510. ];
  6511. $this->assertHtml($expected, $result);
  6512. }
  6513. /**
  6514. * testHiddenField method
  6515. *
  6516. * Test generation of a hidden input.
  6517. *
  6518. * @return void
  6519. */
  6520. public function testHiddenField()
  6521. {
  6522. $this->article['errors'] = [
  6523. 'field' => true
  6524. ];
  6525. $this->Form->request->data['field'] = 'test';
  6526. $this->Form->create($this->article);
  6527. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6528. $expected = [
  6529. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6530. $this->assertHtml($expected, $result);
  6531. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6532. $expected = [
  6533. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6534. ];
  6535. $this->assertHtml($expected, $result);
  6536. }
  6537. /**
  6538. * testFileUploadField method
  6539. *
  6540. * Test generation of a file upload input.
  6541. *
  6542. * @return void
  6543. */
  6544. public function testFileUploadField()
  6545. {
  6546. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6547. $result = $this->Form->file('Model.upload');
  6548. $this->assertHtml($expected, $result);
  6549. $this->Form->request->data['Model']['upload'] = [
  6550. 'name' => '', 'type' => '', 'tmp_name' => '',
  6551. 'error' => 4, 'size' => 0
  6552. ];
  6553. $result = $this->Form->file('Model.upload');
  6554. $this->assertHtml($expected, $result);
  6555. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6556. $result = $this->Form->file('Model.upload');
  6557. $this->assertHtml($expected, $result);
  6558. }
  6559. /**
  6560. * testFileUploadOnOtherModel method
  6561. *
  6562. * Test File upload input on a model not used in create().
  6563. *
  6564. * @return void
  6565. */
  6566. public function testFileUploadOnOtherModel()
  6567. {
  6568. $this->Form->create($this->article, ['type' => 'file']);
  6569. $result = $this->Form->file('ValidateProfile.city');
  6570. $expected = [
  6571. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6572. ];
  6573. $this->assertHtml($expected, $result);
  6574. }
  6575. /**
  6576. * testButton method
  6577. *
  6578. * Test generation of a form button.
  6579. *
  6580. * @return void
  6581. */
  6582. public function testButton()
  6583. {
  6584. $result = $this->Form->button('Hi');
  6585. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6586. $this->assertHtml($expected, $result);
  6587. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6588. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6589. $this->assertHtml($expected, $result);
  6590. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6591. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6592. $this->assertHtml($expected, $result);
  6593. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6594. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6595. $this->assertHtml($expected, $result);
  6596. $result = $this->Form->button('No type', ['type' => false]);
  6597. $expected = ['button' => [], 'No type', '/button'];
  6598. $this->assertHtml($expected, $result);
  6599. $result = $this->Form->button('Upload Text', [
  6600. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6601. 'escape' => false
  6602. ]);
  6603. $this->assertNotRegExp('/\&039/', $result);
  6604. }
  6605. /**
  6606. * testButtonUnlockedByDefault method
  6607. *
  6608. * Test that button() makes unlocked fields by default.
  6609. *
  6610. * @return void
  6611. */
  6612. public function testButtonUnlockedByDefault()
  6613. {
  6614. $this->Form->request->params['_csrfToken'] = 'secured';
  6615. $this->Form->button('Save', ['name' => 'save']);
  6616. $this->Form->button('Clear');
  6617. $result = $this->Form->unlockField();
  6618. $this->assertEquals(['save'], $result);
  6619. }
  6620. /**
  6621. * testPostButton method
  6622. *
  6623. * @return void
  6624. */
  6625. public function testPostButton()
  6626. {
  6627. $result = $this->Form->postButton('Hi', '/controller/action');
  6628. $expected = [
  6629. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6630. 'div' => ['style' => 'display:none;'],
  6631. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6632. '/div',
  6633. 'button' => ['type' => 'submit'],
  6634. 'Hi',
  6635. '/button',
  6636. '/form'
  6637. ];
  6638. $this->assertHtml($expected, $result);
  6639. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6640. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6641. }
  6642. /**
  6643. * testPostButtonMethodType method
  6644. *
  6645. * @return void
  6646. */
  6647. public function testPostButtonMethodType()
  6648. {
  6649. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6650. $expected = [
  6651. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6652. 'div' => ['style' => 'display:none;'],
  6653. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6654. '/div',
  6655. 'button' => ['type' => 'submit'],
  6656. 'Hi',
  6657. '/button',
  6658. '/form'
  6659. ];
  6660. $this->assertHtml($expected, $result);
  6661. }
  6662. /**
  6663. * testPostButtonFormOptions method
  6664. *
  6665. * @return void
  6666. */
  6667. public function testPostButtonFormOptions()
  6668. {
  6669. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6670. $expected = [
  6671. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6672. 'div' => ['style' => 'display:none;'],
  6673. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6674. '/div',
  6675. 'button' => ['type' => 'submit'],
  6676. 'Hi',
  6677. '/button',
  6678. '/form'
  6679. ];
  6680. $this->assertHtml($expected, $result);
  6681. }
  6682. /**
  6683. * testPostButtonNestedData method
  6684. *
  6685. * Test using postButton with N dimensional data.
  6686. *
  6687. * @return void
  6688. */
  6689. public function testPostButtonNestedData()
  6690. {
  6691. $data = [
  6692. 'one' => [
  6693. 'two' => [
  6694. 3, 4, 5
  6695. ]
  6696. ]
  6697. ];
  6698. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6699. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6700. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6701. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6702. }
  6703. /**
  6704. * testSecurePostButton method
  6705. *
  6706. * Test that postButton adds _Token fields.
  6707. *
  6708. * @return void
  6709. */
  6710. public function testSecurePostButton()
  6711. {
  6712. $this->Form->request->params['_csrfToken'] = 'testkey';
  6713. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6714. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6715. $tokenDebug = urlencode(json_encode([
  6716. '/posts/delete/1',
  6717. [],
  6718. []
  6719. ]));
  6720. $expected = [
  6721. 'form' => [
  6722. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6723. ],
  6724. ['div' => ['style' => 'display:none;']],
  6725. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6726. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6727. '/div',
  6728. 'button' => ['type' => 'submit'],
  6729. 'Delete',
  6730. '/button',
  6731. ['div' => ['style' => 'display:none;']],
  6732. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6733. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6734. ['input' => [
  6735. 'type' => 'hidden', 'name' => '_Token[debug]',
  6736. 'value' => $tokenDebug,
  6737. ]],
  6738. '/div',
  6739. '/form',
  6740. ];
  6741. $this->assertHtml($expected, $result);
  6742. }
  6743. /**
  6744. * testPostLink method
  6745. *
  6746. * @return void
  6747. */
  6748. public function testPostLink()
  6749. {
  6750. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6751. $expected = [
  6752. 'form' => [
  6753. 'method' => 'post', 'action' => '/posts/delete/1',
  6754. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6755. ],
  6756. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6757. '/form',
  6758. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6759. 'Delete',
  6760. '/a'
  6761. ];
  6762. $this->assertHtml($expected, $result);
  6763. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6764. $expected = [
  6765. 'form' => [
  6766. 'method' => 'post', 'action' => '/posts/delete/1',
  6767. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6768. ],
  6769. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6770. '/form',
  6771. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6772. 'Delete',
  6773. '/a'
  6774. ];
  6775. $this->assertHtml($expected, $result);
  6776. $result = $this->Form->postLink(
  6777. 'Delete',
  6778. '/posts/delete/1',
  6779. ['target' => '_blank', 'class' => 'btn btn-danger']
  6780. );
  6781. $expected = [
  6782. 'form' => [
  6783. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6784. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6785. ],
  6786. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6787. '/form',
  6788. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6789. 'Delete',
  6790. '/a'
  6791. ];
  6792. $this->assertHtml($expected, $result);
  6793. }
  6794. /**
  6795. * testPostLinkWithConfirm method
  6796. *
  6797. * Test the confirm option for postLink().
  6798. *
  6799. * @return void
  6800. */
  6801. public function testPostLinkWithConfirm()
  6802. {
  6803. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6804. $expected = [
  6805. 'form' => [
  6806. 'method' => 'post', 'action' => '/posts/delete/1',
  6807. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6808. ],
  6809. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6810. '/form',
  6811. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6812. 'Delete',
  6813. '/a'
  6814. ];
  6815. $this->assertHtml($expected, $result);
  6816. $result = $this->Form->postLink(
  6817. 'Delete',
  6818. '/posts/delete/1',
  6819. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6820. );
  6821. $expected = [
  6822. 'form' => [
  6823. 'method' => 'post', 'action' => '/posts/delete/1',
  6824. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6825. ],
  6826. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6827. '/form',
  6828. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6829. 'Delete',
  6830. '/a'
  6831. ];
  6832. $this->assertHtml($expected, $result);
  6833. }
  6834. /**
  6835. * testPostLinkWithQuery method
  6836. *
  6837. * Test postLink() with query string args.
  6838. *
  6839. * @return void
  6840. */
  6841. public function testPostLinkWithQuery()
  6842. {
  6843. $result = $this->Form->postLink(
  6844. 'Delete',
  6845. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6846. );
  6847. $expected = [
  6848. 'form' => [
  6849. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6850. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6851. ],
  6852. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6853. '/form',
  6854. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6855. 'Delete',
  6856. '/a'
  6857. ];
  6858. $this->assertHtml($expected, $result);
  6859. }
  6860. /**
  6861. * testPostLinkWithData method
  6862. *
  6863. * Test postLink with additional data.
  6864. *
  6865. * @return void
  6866. */
  6867. public function testPostLinkWithData()
  6868. {
  6869. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6870. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  6871. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6872. $this->Form->create($entity);
  6873. $this->Form->end();
  6874. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6875. $this->assertContains(
  6876. '<input type="hidden" name="name" value="show"',
  6877. $result,
  6878. 'should not contain entity data.'
  6879. );
  6880. }
  6881. /**
  6882. * testPostLinkSecurityHash method
  6883. *
  6884. * Test that security hashes for postLink include the url.
  6885. *
  6886. * @return void
  6887. */
  6888. public function testPostLinkSecurityHash()
  6889. {
  6890. $hash = Security::hash(
  6891. '/posts/delete/1' .
  6892. serialize(['id' => '1']) .
  6893. '' .
  6894. Security::salt()
  6895. );
  6896. $hash .= '%3Aid';
  6897. $this->Form->request->params['_Token']['key'] = 'test';
  6898. $result = $this->Form->postLink(
  6899. 'Delete',
  6900. '/posts/delete/1',
  6901. ['data' => ['id' => 1]]
  6902. );
  6903. $tokenDebug = urlencode(json_encode([
  6904. '/posts/delete/1',
  6905. [
  6906. 'id' => 1
  6907. ],
  6908. []
  6909. ]));
  6910. $expected = [
  6911. 'form' => [
  6912. 'method' => 'post', 'action' => '/posts/delete/1',
  6913. 'name', 'style' => 'display:none;'
  6914. ],
  6915. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6916. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6917. 'div' => ['style' => 'display:none;'],
  6918. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6919. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6920. ['input' => [
  6921. 'type' => 'hidden', 'name' => '_Token[debug]',
  6922. 'value' => $tokenDebug,
  6923. ]],
  6924. '/div',
  6925. '/form',
  6926. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6927. 'Delete',
  6928. '/a'
  6929. ];
  6930. $this->assertHtml($expected, $result);
  6931. }
  6932. /**
  6933. * testPostLinkSecurityHashBlockMode method
  6934. *
  6935. * Test that postLink doesn't modify the fields in the containing form.
  6936. *
  6937. * postLink() calls inside open forms should not modify the field list
  6938. * for the form.
  6939. *
  6940. * @return void
  6941. */
  6942. public function testPostLinkSecurityHashBlockMode()
  6943. {
  6944. $hash = Security::hash(
  6945. '/posts/delete/1' .
  6946. serialize([]) .
  6947. '' .
  6948. Security::salt()
  6949. );
  6950. $hash .= '%3A';
  6951. $this->Form->request->params['_Token']['key'] = 'test';
  6952. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  6953. $this->Form->control('title');
  6954. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6955. $result = $this->View->fetch('postLink');
  6956. $this->assertEquals(['title'], $this->Form->fields);
  6957. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  6958. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  6959. }
  6960. /**
  6961. * testPostLinkSecurityHashNoDebugMode method
  6962. *
  6963. * Test that security does not include debug token if debug is false.
  6964. *
  6965. * @return void
  6966. */
  6967. public function testPostLinkSecurityHashNoDebugMode()
  6968. {
  6969. Configure::write('debug', false);
  6970. $hash = Security::hash(
  6971. '/posts/delete/1' .
  6972. serialize(['id' => '1']) .
  6973. '' .
  6974. Security::salt()
  6975. );
  6976. $hash .= '%3Aid';
  6977. $this->Form->request->params['_Token']['key'] = 'test';
  6978. $result = $this->Form->postLink(
  6979. 'Delete',
  6980. '/posts/delete/1',
  6981. ['data' => ['id' => 1]]
  6982. );
  6983. $expected = [
  6984. 'form' => [
  6985. 'method' => 'post', 'action' => '/posts/delete/1',
  6986. 'name', 'style' => 'display:none;'
  6987. ],
  6988. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6989. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6990. 'div' => ['style' => 'display:none;'],
  6991. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6992. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6993. '/div',
  6994. '/form',
  6995. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6996. 'Delete',
  6997. '/a'
  6998. ];
  6999. $this->assertHtml($expected, $result);
  7000. }
  7001. /**
  7002. * testPostLinkNestedData method
  7003. *
  7004. * Test using postLink with N dimensional data.
  7005. *
  7006. * @return void
  7007. */
  7008. public function testPostLinkNestedData()
  7009. {
  7010. $data = [
  7011. 'one' => [
  7012. 'two' => [
  7013. 3, 4, 5
  7014. ]
  7015. ]
  7016. ];
  7017. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7018. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7019. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7020. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7021. }
  7022. /**
  7023. * testPostLinkAfterGetForm method
  7024. *
  7025. * Test creating postLinks after a GET form.
  7026. *
  7027. * @return void
  7028. */
  7029. public function testPostLinkAfterGetForm()
  7030. {
  7031. $this->Form->request->params['_csrfToken'] = 'testkey';
  7032. $this->Form->request->params['_Token'] = 'val';
  7033. $this->Form->create($this->article, ['type' => 'get']);
  7034. $this->Form->end();
  7035. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7036. $tokenDebug = urlencode(json_encode([
  7037. '/posts/delete/1',
  7038. [],
  7039. []
  7040. ]));
  7041. $expected = [
  7042. 'form' => [
  7043. 'method' => 'post', 'action' => '/posts/delete/1',
  7044. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7045. ],
  7046. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7047. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  7048. 'div' => ['style' => 'display:none;'],
  7049. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  7050. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  7051. ['input' => [
  7052. 'type' => 'hidden', 'name' => '_Token[debug]',
  7053. 'value' => $tokenDebug,
  7054. ]],
  7055. '/div',
  7056. '/form',
  7057. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7058. 'Delete',
  7059. '/a'
  7060. ];
  7061. $this->assertHtml($expected, $result);
  7062. }
  7063. /**
  7064. * testPostLinkFormBuffer method
  7065. *
  7066. * Test that postLink adds form tags to view block.
  7067. *
  7068. * @return void
  7069. */
  7070. public function testPostLinkFormBuffer()
  7071. {
  7072. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7073. $expected = [
  7074. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7075. 'Delete',
  7076. '/a'
  7077. ];
  7078. $this->assertHtml($expected, $result);
  7079. $result = $this->View->fetch('postLink');
  7080. $expected = [
  7081. 'form' => [
  7082. 'method' => 'post', 'action' => '/posts/delete/1',
  7083. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7084. ],
  7085. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7086. '/form'
  7087. ];
  7088. $this->assertHtml($expected, $result);
  7089. $result = $this->Form->postLink(
  7090. 'Delete',
  7091. '/posts/delete/2',
  7092. ['block' => true, 'method' => 'DELETE']
  7093. );
  7094. $expected = [
  7095. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7096. 'Delete',
  7097. '/a'
  7098. ];
  7099. $this->assertHtml($expected, $result);
  7100. $result = $this->View->fetch('postLink');
  7101. $expected = [
  7102. 'form' => [
  7103. 'method' => 'post', 'action' => '/posts/delete/1',
  7104. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7105. ],
  7106. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7107. '/form',
  7108. [
  7109. 'form' => [
  7110. 'method' => 'post', 'action' => '/posts/delete/2',
  7111. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7112. ],
  7113. ],
  7114. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7115. '/form'
  7116. ];
  7117. $this->assertHtml($expected, $result);
  7118. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7119. $expected = [
  7120. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7121. 'Delete',
  7122. '/a'
  7123. ];
  7124. $this->assertHtml($expected, $result);
  7125. $result = $this->View->fetch('foobar');
  7126. $expected = [
  7127. 'form' => [
  7128. 'method' => 'post', 'action' => '/posts/delete/1',
  7129. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7130. ],
  7131. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7132. '/form'
  7133. ];
  7134. $this->assertHtml($expected, $result);
  7135. }
  7136. /**
  7137. * testSubmitButton method
  7138. *
  7139. * @return void
  7140. */
  7141. public function testSubmitButton()
  7142. {
  7143. $result = $this->Form->submit('');
  7144. $expected = [
  7145. 'div' => ['class' => 'submit'],
  7146. 'input' => ['type' => 'submit', 'value' => ''],
  7147. '/div'
  7148. ];
  7149. $this->assertHtml($expected, $result);
  7150. $result = $this->Form->submit('Test Submit');
  7151. $expected = [
  7152. 'div' => ['class' => 'submit'],
  7153. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7154. '/div'
  7155. ];
  7156. $this->assertHtml($expected, $result);
  7157. $result = $this->Form->submit('Next >');
  7158. $expected = [
  7159. 'div' => ['class' => 'submit'],
  7160. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7161. '/div'
  7162. ];
  7163. $this->assertHtml($expected, $result);
  7164. $result = $this->Form->submit('Next >', ['escape' => false]);
  7165. $expected = [
  7166. 'div' => ['class' => 'submit'],
  7167. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7168. '/div'
  7169. ];
  7170. $this->assertHtml($expected, $result);
  7171. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7172. $expected = [
  7173. 'div' => ['class' => 'submit'],
  7174. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7175. '/div'
  7176. ];
  7177. $this->assertHtml($expected, $result);
  7178. }
  7179. /**
  7180. * testSubmitImage method
  7181. *
  7182. * Test image submit types.
  7183. *
  7184. * @return void
  7185. */
  7186. public function testSubmitImage()
  7187. {
  7188. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7189. $expected = [
  7190. 'div' => ['class' => 'submit'],
  7191. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7192. '/div'
  7193. ];
  7194. $this->assertHtml($expected, $result);
  7195. $result = $this->Form->submit('/relative/cake.power.gif');
  7196. $expected = [
  7197. 'div' => ['class' => 'submit'],
  7198. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7199. '/div'
  7200. ];
  7201. $this->assertHtml($expected, $result);
  7202. $result = $this->Form->submit('cake.power.gif');
  7203. $expected = [
  7204. 'div' => ['class' => 'submit'],
  7205. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7206. '/div'
  7207. ];
  7208. $this->assertHtml($expected, $result);
  7209. $result = $this->Form->submit('Not.an.image');
  7210. $expected = [
  7211. 'div' => ['class' => 'submit'],
  7212. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7213. '/div'
  7214. ];
  7215. $this->assertHtml($expected, $result);
  7216. }
  7217. /**
  7218. * testSubmitUnlockedByDefault method
  7219. *
  7220. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7221. * be submitted at a time.
  7222. *
  7223. * @return void
  7224. */
  7225. public function testSubmitUnlockedByDefault()
  7226. {
  7227. $this->Form->request->params['_Token'] = 'secured';
  7228. $this->Form->submit('Go go');
  7229. $this->Form->submit('Save', ['name' => 'save']);
  7230. $result = $this->Form->unlockField();
  7231. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7232. }
  7233. /**
  7234. * testSubmitImageTimestamp method
  7235. *
  7236. * Test submit image with timestamps.
  7237. *
  7238. * @return void
  7239. */
  7240. public function testSubmitImageTimestamp()
  7241. {
  7242. Configure::write('Asset.timestamp', 'force');
  7243. $result = $this->Form->submit('cake.power.gif');
  7244. $expected = [
  7245. 'div' => ['class' => 'submit'],
  7246. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7247. '/div'
  7248. ];
  7249. $this->assertHtml($expected, $result);
  7250. }
  7251. /**
  7252. * testDateTimeWithGetForms method
  7253. *
  7254. * Test that datetime() works with GET style forms.
  7255. *
  7256. * @return void
  7257. */
  7258. public function testDateTimeWithGetForms()
  7259. {
  7260. extract($this->dateRegex);
  7261. $this->Form->create($this->article, ['type' => 'get']);
  7262. $result = $this->Form->datetime('created');
  7263. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7264. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7265. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7266. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7267. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7268. }
  7269. /**
  7270. * testForMagicControlNonExistingNorValidated method
  7271. *
  7272. * @return void
  7273. */
  7274. public function testForMagicControlNonExistingNorValidated()
  7275. {
  7276. $this->Form->create($this->article);
  7277. $this->Form->templates(['inputContainer' => '{{content}}']);
  7278. $result = $this->Form->control('non_existing_nor_validated');
  7279. $expected = [
  7280. 'label' => ['for' => 'non-existing-nor-validated'],
  7281. 'Non Existing Nor Validated',
  7282. '/label',
  7283. 'input' => [
  7284. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7285. 'id' => 'non-existing-nor-validated'
  7286. ]
  7287. ];
  7288. $this->assertHtml($expected, $result);
  7289. $result = $this->Form->control('non_existing_nor_validated', [
  7290. 'val' => 'my value'
  7291. ]);
  7292. $expected = [
  7293. 'label' => ['for' => 'non-existing-nor-validated'],
  7294. 'Non Existing Nor Validated',
  7295. '/label',
  7296. 'input' => [
  7297. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7298. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7299. ]
  7300. ];
  7301. $this->assertHtml($expected, $result);
  7302. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7303. $result = $this->Form->control('non_existing_nor_validated');
  7304. $expected = [
  7305. 'label' => ['for' => 'non-existing-nor-validated'],
  7306. 'Non Existing Nor Validated',
  7307. '/label',
  7308. 'input' => [
  7309. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7310. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7311. ]
  7312. ];
  7313. $this->assertHtml($expected, $result);
  7314. }
  7315. /**
  7316. * testFormMagicControlLabel method
  7317. *
  7318. * @return void
  7319. */
  7320. public function testFormMagicControlLabel()
  7321. {
  7322. TableRegistry::get('Contacts', [
  7323. 'className' => __NAMESPACE__ . '\ContactsTable'
  7324. ]);
  7325. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7326. $this->Form->templates(['inputContainer' => '{{content}}']);
  7327. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7328. $expected = [
  7329. 'label' => ['for' => 'contacts-name'],
  7330. 'My label',
  7331. '/label',
  7332. 'input' => [
  7333. 'type' => 'text', 'name' => 'Contacts[name]',
  7334. 'id' => 'contacts-name', 'maxlength' => '255'
  7335. ]
  7336. ];
  7337. $this->assertHtml($expected, $result);
  7338. $result = $this->Form->control('name', [
  7339. 'label' => ['class' => 'mandatory']
  7340. ]);
  7341. $expected = [
  7342. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7343. 'Name',
  7344. '/label',
  7345. 'input' => [
  7346. 'type' => 'text', 'name' => 'name',
  7347. 'id' => 'name', 'maxlength' => '255'
  7348. ]
  7349. ];
  7350. $this->assertHtml($expected, $result);
  7351. $result = $this->Form->control('name', [
  7352. 'div' => false,
  7353. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7354. ]);
  7355. $expected = [
  7356. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7357. 'My label',
  7358. '/label',
  7359. 'input' => [
  7360. 'type' => 'text', 'name' => 'name',
  7361. 'id' => 'name', 'maxlength' => '255'
  7362. ]
  7363. ];
  7364. $this->assertHtml($expected, $result);
  7365. $result = $this->Form->control('Contact.name', [
  7366. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7367. ]);
  7368. $expected = [
  7369. 'label' => ['for' => 'my_id'],
  7370. 'Name',
  7371. '/label',
  7372. 'input' => [
  7373. 'type' => 'text', 'name' => 'Contact[name]',
  7374. 'id' => 'my_id', 'maxlength' => '255'
  7375. ]
  7376. ];
  7377. $this->assertHtml($expected, $result);
  7378. $result = $this->Form->control('1.id');
  7379. $expected = ['input' => [
  7380. 'type' => 'hidden', 'name' => '1[id]',
  7381. 'id' => '1-id'
  7382. ]];
  7383. $this->assertHtml($expected, $result);
  7384. $result = $this->Form->control("1.name");
  7385. $expected = [
  7386. 'label' => ['for' => '1-name'],
  7387. 'Name',
  7388. '/label',
  7389. 'input' => [
  7390. 'type' => 'text', 'name' => '1[name]',
  7391. 'id' => '1-name', 'maxlength' => '255'
  7392. ]
  7393. ];
  7394. $this->assertHtml($expected, $result);
  7395. }
  7396. /**
  7397. * testFormEnd method
  7398. *
  7399. * @return void
  7400. */
  7401. public function testFormEnd()
  7402. {
  7403. $this->assertEquals('</form>', $this->Form->end());
  7404. }
  7405. /**
  7406. * testMultiRecordForm method
  7407. *
  7408. * Test the generation of fields for a multi record form.
  7409. *
  7410. * @return void
  7411. */
  7412. public function testMultiRecordForm()
  7413. {
  7414. $this->loadFixtures('Articles', 'Comments');
  7415. $articles = TableRegistry::get('Articles');
  7416. $articles->hasMany('Comments');
  7417. $comment = new Entity(['comment' => 'Value']);
  7418. $article = new Article(['comments' => [$comment]]);
  7419. $this->Form->create([$article]);
  7420. $result = $this->Form->control('0.comments.1.comment');
  7421. //@codingStandardsIgnoreStart
  7422. $expected = [
  7423. 'div' => ['class' => 'input textarea'],
  7424. 'label' => ['for' => '0-comments-1-comment'],
  7425. 'Comment',
  7426. '/label',
  7427. 'textarea' => [
  7428. 'name',
  7429. 'id' => '0-comments-1-comment',
  7430. 'rows' => 5
  7431. ],
  7432. '/textarea',
  7433. '/div'
  7434. ];
  7435. //@codingStandardsIgnoreEnd
  7436. $this->assertHtml($expected, $result);
  7437. $result = $this->Form->control('0.comments.0.comment');
  7438. //@codingStandardsIgnoreStart
  7439. $expected = [
  7440. 'div' => ['class' => 'input textarea'],
  7441. 'label' => ['for' => '0-comments-0-comment'],
  7442. 'Comment',
  7443. '/label',
  7444. 'textarea' => [
  7445. 'name',
  7446. 'id' => '0-comments-0-comment',
  7447. 'rows' => 5
  7448. ],
  7449. 'Value',
  7450. '/textarea',
  7451. '/div'
  7452. ];
  7453. //@codingStandardsIgnoreEnd
  7454. $this->assertHtml($expected, $result);
  7455. $comment->errors('comment', ['Not valid']);
  7456. $result = $this->Form->control('0.comments.0.comment');
  7457. //@codingStandardsIgnoreStart
  7458. $expected = [
  7459. 'div' => ['class' => 'input textarea error'],
  7460. 'label' => ['for' => '0-comments-0-comment'],
  7461. 'Comment',
  7462. '/label',
  7463. 'textarea' => [
  7464. 'name',
  7465. 'class' => 'form-error',
  7466. 'id' => '0-comments-0-comment',
  7467. 'rows' => 5
  7468. ],
  7469. 'Value',
  7470. '/textarea',
  7471. ['div' => ['class' => 'error-message']],
  7472. 'Not valid',
  7473. '/div',
  7474. '/div'
  7475. ];
  7476. //@codingStandardsIgnoreEnd
  7477. $this->assertHtml($expected, $result);
  7478. TableRegistry::get('Comments')
  7479. ->validator('default')
  7480. ->allowEmpty('comment', false);
  7481. $result = $this->Form->control('0.comments.1.comment');
  7482. //@codingStandardsIgnoreStart
  7483. $expected = [
  7484. 'div' => ['class' => 'input textarea required'],
  7485. 'label' => ['for' => '0-comments-1-comment'],
  7486. 'Comment',
  7487. '/label',
  7488. 'textarea' => [
  7489. 'name',
  7490. 'required' => 'required',
  7491. 'id' => '0-comments-1-comment',
  7492. 'rows' => 5
  7493. ],
  7494. '/textarea',
  7495. '/div'
  7496. ];
  7497. //@codingStandardsIgnoreEnd
  7498. $this->assertHtml($expected, $result);
  7499. }
  7500. /**
  7501. * testHtml5Controls method
  7502. *
  7503. * Test that some html5 inputs + FormHelper::__call() work.
  7504. *
  7505. * @return void
  7506. */
  7507. public function testHtml5Controls()
  7508. {
  7509. $result = $this->Form->email('User.email');
  7510. $expected = [
  7511. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7512. ];
  7513. $this->assertHtml($expected, $result);
  7514. $result = $this->Form->search('User.query');
  7515. $expected = [
  7516. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7517. ];
  7518. $this->assertHtml($expected, $result);
  7519. $result = $this->Form->search('User.query', ['value' => 'test']);
  7520. $expected = [
  7521. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7522. ];
  7523. $this->assertHtml($expected, $result);
  7524. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7525. $expected = [
  7526. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7527. ];
  7528. $this->assertHtml($expected, $result);
  7529. }
  7530. /**
  7531. * testHtml5ControlWithControl method
  7532. *
  7533. * Test accessing html5 inputs through control().
  7534. *
  7535. * @return void
  7536. */
  7537. public function testHtml5ControlWithControl()
  7538. {
  7539. $this->Form->create();
  7540. $this->Form->templates(['inputContainer' => '{{content}}']);
  7541. $result = $this->Form->control('website', [
  7542. 'type' => 'url',
  7543. 'val' => 'http://domain.tld',
  7544. 'label' => false
  7545. ]);
  7546. $expected = [
  7547. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7548. ];
  7549. $this->assertHtml($expected, $result);
  7550. }
  7551. /**
  7552. * testHtml5ControlException method
  7553. *
  7554. * Test errors when field name is missing.
  7555. *
  7556. * @expectedException \Cake\Core\Exception\Exception
  7557. * @return void
  7558. */
  7559. public function testHtml5ControlException()
  7560. {
  7561. $this->Form->email();
  7562. }
  7563. /**
  7564. * testRequiredAttribute method
  7565. *
  7566. * Tests that formhelper sets required attributes.
  7567. *
  7568. * @return void
  7569. */
  7570. public function testRequiredAttribute()
  7571. {
  7572. $this->article['required'] = [
  7573. 'title' => true,
  7574. 'body' => false,
  7575. ];
  7576. $this->Form->create($this->article);
  7577. $result = $this->Form->control('title');
  7578. $expected = [
  7579. 'div' => ['class' => 'input text required'],
  7580. 'label' => ['for' => 'title'],
  7581. 'Title',
  7582. '/label',
  7583. 'input' => [
  7584. 'type' => 'text',
  7585. 'name' => 'title',
  7586. 'id' => 'title',
  7587. 'required' => 'required',
  7588. ],
  7589. '/div'
  7590. ];
  7591. $this->assertHtml($expected, $result);
  7592. $result = $this->Form->control('title', ['required' => false]);
  7593. $this->assertNotContains('required', $result);
  7594. $result = $this->Form->control('body');
  7595. $expected = [
  7596. 'div' => ['class' => 'input text'],
  7597. 'label' => ['for' => 'body'],
  7598. 'Body',
  7599. '/label',
  7600. 'input' => [
  7601. 'type' => 'text',
  7602. 'name' => 'body',
  7603. 'id' => 'body',
  7604. ],
  7605. '/div'
  7606. ];
  7607. $this->assertHtml($expected, $result);
  7608. $result = $this->Form->control('body', ['required' => true]);
  7609. $this->assertContains('required', $result);
  7610. }
  7611. /**
  7612. * testControlsNotNested method
  7613. *
  7614. * Tests that it is possible to put inputs outside of the label.
  7615. *
  7616. * @return void
  7617. */
  7618. public function testControlsNotNested()
  7619. {
  7620. $this->Form->templates([
  7621. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7622. 'formGroup' => '{{input}}{{label}}',
  7623. ]);
  7624. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7625. $expected = [
  7626. 'div' => ['class' => 'input checkbox'],
  7627. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7628. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7629. 'label' => ['for' => 'foo'],
  7630. 'Foo',
  7631. '/label',
  7632. '/div'
  7633. ];
  7634. $this->assertHtml($expected, $result);
  7635. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7636. $expected = [
  7637. 'div' => ['class' => 'input checkbox'],
  7638. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7639. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7640. '/div'
  7641. ];
  7642. $this->assertHtml($expected, $result);
  7643. $result = $this->Form->control('confirm', [
  7644. 'type' => 'radio',
  7645. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7646. ]);
  7647. $expected = [
  7648. 'div' => ['class' => 'input radio'],
  7649. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7650. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7651. ['label' => ['for' => 'confirm-y']],
  7652. 'Yes',
  7653. '/label',
  7654. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7655. ['label' => ['for' => 'confirm-n']],
  7656. 'No',
  7657. '/label',
  7658. '<label',
  7659. 'Confirm',
  7660. '/label',
  7661. '/div',
  7662. ];
  7663. $this->assertHtml($expected, $result);
  7664. $result = $this->Form->select('category', ['1', '2'], [
  7665. 'multiple' => 'checkbox',
  7666. 'name' => 'fish',
  7667. ]);
  7668. $expected = [
  7669. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7670. ['div' => ['class' => 'checkbox']],
  7671. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7672. ['label' => ['for' => 'fish-0']],
  7673. '1',
  7674. '/label',
  7675. '/div',
  7676. ['div' => ['class' => 'checkbox']],
  7677. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7678. ['label' => ['for' => 'fish-1']],
  7679. '2',
  7680. '/label',
  7681. '/div'
  7682. ];
  7683. $this->assertHtml($expected, $result);
  7684. }
  7685. /**
  7686. * testControlContainerTemplates method
  7687. *
  7688. * Test that *Container templates are used by input.
  7689. *
  7690. * @return void
  7691. */
  7692. public function testControlContainerTemplates()
  7693. {
  7694. $this->Form->templates([
  7695. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7696. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7697. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7698. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7699. ]);
  7700. $this->article['errors'] = [
  7701. 'Article' => ['published' => 'error message']
  7702. ];
  7703. $this->Form->create($this->article);
  7704. $result = $this->Form->control('accept', [
  7705. 'type' => 'checkbox'
  7706. ]);
  7707. $expected = [
  7708. 'div' => ['class' => 'check'],
  7709. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7710. 'label' => ['for' => 'accept'],
  7711. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7712. 'Accept',
  7713. '/label',
  7714. '/div'
  7715. ];
  7716. $this->assertHtml($expected, $result);
  7717. $result = $this->Form->control('accept', [
  7718. 'type' => 'radio',
  7719. 'options' => ['Y', 'N']
  7720. ]);
  7721. $this->assertContains('<div class="rad">', $result);
  7722. $result = $this->Form->control('Article.published', [
  7723. 'type' => 'radio',
  7724. 'options' => ['Y', 'N']
  7725. ]);
  7726. $this->assertContains('<div class="rad err">', $result);
  7727. $result = $this->Form->control('Article.created', [
  7728. 'type' => 'datetime',
  7729. ]);
  7730. $this->assertContains('<div class="dt">', $result);
  7731. }
  7732. /**
  7733. * testFormGroupTemplates method
  7734. *
  7735. * Test that *Container templates are used by input.
  7736. *
  7737. * @return void
  7738. */
  7739. public function testFormGroupTemplates()
  7740. {
  7741. $this->Form->templates([
  7742. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7743. ]);
  7744. $this->Form->create($this->article);
  7745. $result = $this->Form->control('accept', [
  7746. 'type' => 'radio',
  7747. 'options' => ['Y', 'N']
  7748. ]);
  7749. $this->assertContains('<div class="radio">', $result);
  7750. }
  7751. /**
  7752. * testResetTemplates method
  7753. *
  7754. * Test resetting templates.
  7755. *
  7756. * @return void
  7757. */
  7758. public function testResetTemplates()
  7759. {
  7760. $this->Form->templates(['input' => '<input/>']);
  7761. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7762. $this->assertNull($this->Form->resetTemplates());
  7763. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7764. }
  7765. /**
  7766. * testContext method
  7767. *
  7768. * Test the context method.
  7769. *
  7770. * @return void
  7771. */
  7772. public function testContext()
  7773. {
  7774. $result = $this->Form->context();
  7775. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7776. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7777. $this->assertSame($mock, $this->Form->context($mock));
  7778. $this->assertSame($mock, $this->Form->context());
  7779. }
  7780. /**
  7781. * testAutoDomId method
  7782. *
  7783. * @return void
  7784. */
  7785. public function testAutoDomId()
  7786. {
  7787. $result = $this->Form->text('field', ['id' => true]);
  7788. $expected = [
  7789. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7790. ];
  7791. $this->assertHtml($expected, $result);
  7792. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7793. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7794. $expected = [
  7795. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7796. ['label' => ['for' => 'field-0']],
  7797. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7798. 'option A',
  7799. '/label',
  7800. ['label' => ['for' => 'field-1']],
  7801. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7802. 'option B',
  7803. '/label',
  7804. ];
  7805. $this->assertHtml($expected, $result);
  7806. $result = $this->Form->select(
  7807. 'multi_field',
  7808. ['first', 'second'],
  7809. ['multiple' => 'checkbox', 'id' => true]
  7810. );
  7811. $expected = [
  7812. 'input' => [
  7813. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7814. ],
  7815. ['div' => ['class' => 'checkbox']],
  7816. ['label' => ['for' => 'multi-field-0']],
  7817. ['input' => [
  7818. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7819. 'value' => '0', 'id' => 'multi-field-0'
  7820. ]],
  7821. 'first',
  7822. '/label',
  7823. '/div',
  7824. ['div' => ['class' => 'checkbox']],
  7825. ['label' => ['for' => 'multi-field-1']],
  7826. ['input' => [
  7827. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7828. 'value' => '1', 'id' => 'multi-field-1'
  7829. ]],
  7830. 'second',
  7831. '/label',
  7832. '/div',
  7833. ];
  7834. $this->assertHtml($expected, $result);
  7835. }
  7836. /**
  7837. * Test the basic setters and getters for value sources
  7838. *
  7839. * @return void
  7840. */
  7841. public function testFormValueSourcesSettersGetters()
  7842. {
  7843. $expected = ['context'];
  7844. $result = $this->Form->getValueSources();
  7845. $this->assertEquals($expected, $result);
  7846. $expected = null;
  7847. $result = $this->Form->getSourceValue('id');
  7848. $this->assertEquals($expected, $result);
  7849. $expected = ['query', 'data', 'context'];
  7850. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7851. $result = $this->Form->getValueSources();
  7852. $this->assertEquals($expected, $result);
  7853. $this->Form->request->data['id'] = '1';
  7854. $this->Form->request->query['id'] = '2';
  7855. $this->Form->setValueSources(['context']);
  7856. $expected = '1';
  7857. $result = $this->Form->getSourceValue('id');
  7858. $this->assertEquals($expected, $result);
  7859. $this->Form->setValueSources('query');
  7860. $expected = ['query'];
  7861. $result = $this->Form->getValueSources();
  7862. $this->assertEquals($expected, $result);
  7863. $expected = '2';
  7864. $result = $this->Form->getSourceValue('id');
  7865. $this->assertEquals($expected, $result);
  7866. $this->Form->setValueSources(['data']);
  7867. $expected = '1';
  7868. $result = $this->Form->getSourceValue('id');
  7869. $this->assertEquals($expected, $result);
  7870. $this->Form->setValueSources(['query', 'data']);
  7871. $expected = '2';
  7872. $result = $this->Form->getSourceValue('id');
  7873. $this->assertEquals($expected, $result);
  7874. }
  7875. /**
  7876. * Tests the different input rendering values based on sources values switching
  7877. *
  7878. * @return void
  7879. */
  7880. public function testFormValueSourcesSingleSwitchRendering()
  7881. {
  7882. $this->loadFixtures('Articles');
  7883. $articles = TableRegistry::get('Articles');
  7884. $article = new Article();
  7885. $articles->patchEntity($article, ['id' => '3']);
  7886. $this->Form->create($article);
  7887. $this->Form->setValueSources(['context']);
  7888. $result = $this->Form->control('id');
  7889. $expected = [
  7890. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7891. ];
  7892. $this->assertHtml($expected, $result);
  7893. $this->Form->request->query['id'] = '5';
  7894. $this->Form->setValueSources(['query']);
  7895. $result = $this->Form->control('id');
  7896. $expected = [
  7897. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7898. ];
  7899. $this->assertHtml($expected, $result);
  7900. $this->Form->request->query['id'] = '5a';
  7901. $this->Form->request->data['id'] = '5b';
  7902. $this->Form->setValueSources(['context']);
  7903. $this->Form->create($article);
  7904. $result = $this->Form->control('id');
  7905. $expected = [
  7906. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7907. ];
  7908. $this->assertHtml($expected, $result);
  7909. $this->Form->setValueSources(['data']);
  7910. $this->Form->create($article);
  7911. $result = $this->Form->control('id');
  7912. $expected = [
  7913. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7914. ];
  7915. $this->assertHtml($expected, $result);
  7916. $this->Form->setValueSources(['query']);
  7917. $result = $this->Form->control('id');
  7918. $expected = [
  7919. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7920. ];
  7921. $this->assertHtml($expected, $result);
  7922. }
  7923. /**
  7924. * Tests the different input rendering values based on sources values switching while supplying
  7925. * an entity (base context) and multiple sources (such as data, query)
  7926. *
  7927. * @return void
  7928. */
  7929. public function testFormValueSourcesListSwitchRendering()
  7930. {
  7931. $this->loadFixtures('Articles');
  7932. $articles = TableRegistry::get('Articles');
  7933. $article = new Article();
  7934. $articles->patchEntity($article, ['id' => '3']);
  7935. $this->Form->request->query['id'] = '9';
  7936. $this->Form->create($article);
  7937. $this->Form->setValueSources(['context', 'query']);
  7938. $result = $this->Form->control('id');
  7939. $expected = [
  7940. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7941. ];
  7942. $this->assertHtml($expected, $result);
  7943. $this->Form->setValueSources(['query', 'context']);
  7944. $result = $this->Form->control('id');
  7945. $expected = [
  7946. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7947. ];
  7948. $this->assertHtml($expected, $result);
  7949. $this->Form->setValueSources(['data', 'query', 'context']);
  7950. $result = $this->Form->control('id');
  7951. $expected = [
  7952. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7953. ];
  7954. $this->assertHtml($expected, $result);
  7955. $this->Form->request->data['id'] = '8';
  7956. $this->Form->request->query['id'] = '9';
  7957. $this->Form->setValueSources(['data', 'query', 'context']);
  7958. $result = $this->Form->control('id');
  7959. $expected = [
  7960. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7961. ];
  7962. $this->assertHtml($expected, $result);
  7963. }
  7964. /**
  7965. * Test the different form input renderings based on values sources switchings through form options
  7966. *
  7967. * @return void
  7968. */
  7969. public function testFormValueSourcesSwitchViaOptionsRendering()
  7970. {
  7971. $this->loadFixtures('Articles');
  7972. $articles = TableRegistry::get('Articles');
  7973. $article = new Article();
  7974. $articles->patchEntity($article, ['id' => '3']);
  7975. $this->Form->request->data['id'] = '4';
  7976. $this->Form->request->query['id'] = '5';
  7977. $this->Form->create($article, ['valueSources' => 'query']);
  7978. $result = $this->Form->control('id');
  7979. $expected = [
  7980. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7981. ];
  7982. $this->assertHtml($expected, $result);
  7983. $result = $this->Form->getSourceValue('id');
  7984. $this->assertEquals('5', $result);
  7985. $this->Form->setValueSources(['context']);
  7986. $this->Form->create($article, ['valueSources' => 'query']);
  7987. $result = $this->Form->control('id');
  7988. $expected = [
  7989. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7990. ];
  7991. $this->assertHtml($expected, $result);
  7992. $result = $this->Form->getSourceValue('id');
  7993. $this->assertEquals('5', $result);
  7994. $this->Form->setValueSources(['query']);
  7995. $this->Form->create($article, ['valueSources' => 'data']);
  7996. $result = $this->Form->control('id');
  7997. $expected = [
  7998. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7999. ];
  8000. $this->assertHtml($expected, $result);
  8001. $result = $this->Form->getSourceValue('id');
  8002. $this->assertEquals('4', $result);
  8003. $this->Form->setValueSources(['query']);
  8004. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8005. $result = $this->Form->control('id');
  8006. $expected = [
  8007. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8008. ];
  8009. $this->assertHtml($expected, $result);
  8010. $result = $this->Form->getSourceValue('id');
  8011. $this->assertEquals('4', $result);
  8012. }
  8013. /**
  8014. * Test the different form input renderings based on values sources switchings through form options
  8015. *
  8016. * @return void
  8017. */
  8018. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8019. {
  8020. $this->loadFixtures('Articles');
  8021. $articles = TableRegistry::get('Articles');
  8022. $article = new Article();
  8023. $articles->patchEntity($article, ['id' => '3']);
  8024. $this->Form->request->data['id'] = '10';
  8025. $this->Form->request->query['id'] = '11';
  8026. $this->Form->setValueSources(['context'])
  8027. ->create($article, ['valueSources' => ['query', 'data']]);
  8028. $result = $this->Form->control('id');
  8029. $expected = [
  8030. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8031. ];
  8032. $this->assertHtml($expected, $result);
  8033. $result = $this->Form->getSourceValue('id');
  8034. $this->assertEquals('11', $result);
  8035. unset($this->Form->request->query['id']);
  8036. $this->Form->setValueSources(['context'])
  8037. ->create($article, ['valueSources' => ['query', 'data']]);
  8038. $result = $this->Form->control('id');
  8039. $expected = [
  8040. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8041. ];
  8042. $this->assertHtml($expected, $result);
  8043. $result = $this->Form->getSourceValue('id');
  8044. $this->assertEquals('10', $result);
  8045. }
  8046. /**
  8047. * Test the different form values sources resetting through From::end();
  8048. *
  8049. * @return void
  8050. */
  8051. public function testFormValueSourcesResetViaEnd()
  8052. {
  8053. $expected = ['context'];
  8054. $result = $this->Form->getValueSources();
  8055. $this->assertEquals($expected, $result);
  8056. $expected = ['query', 'context', 'data'];
  8057. $this->Form->setValueSources(['query', 'context', 'data']);
  8058. $result = $this->Form->getValueSources();
  8059. $this->assertEquals($expected, $result);
  8060. $this->Form->create();
  8061. $result = $this->Form->getValueSources();
  8062. $this->assertEquals($expected, $result);
  8063. $this->Form->end();
  8064. $result = $this->Form->getValueSources();
  8065. $this->assertEquals(['context'], $result);
  8066. }
  8067. /**
  8068. * Test sources values defaults handling
  8069. *
  8070. * @return void
  8071. */
  8072. public function testFormValueSourcesDefaults()
  8073. {
  8074. $this->Form->request->query['password'] = 'open Sesame';
  8075. $this->Form->create();
  8076. $result = $this->Form->password('password');
  8077. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8078. $this->assertHtml($expected, $result);
  8079. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8080. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8081. $this->assertHtml($expected, $result);
  8082. $this->Form->setValueSources('query');
  8083. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8084. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8085. $this->assertHtml($expected, $result);
  8086. $this->Form->setValueSources('data');
  8087. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8088. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8089. $this->assertHtml($expected, $result);
  8090. }
  8091. /**
  8092. * Test sources values schema defaults handling
  8093. *
  8094. * @return void
  8095. */
  8096. public function testSourcesValueDoesntExistPassThrough()
  8097. {
  8098. $this->Form->request->query['category'] = 'sesame-cookies';
  8099. $articles = TableRegistry::get('Articles');
  8100. $entity = $articles->newEntity();
  8101. $this->Form->create($entity);
  8102. $this->Form->setValueSources(['query', 'context']);
  8103. $result = $this->Form->getSourceValue('category');
  8104. $this->assertEquals('sesame-cookies', $result);
  8105. $this->Form->setValueSources(['context', 'query']);
  8106. $result = $this->Form->getSourceValue('category');
  8107. $this->assertEquals('sesame-cookies', $result);
  8108. }
  8109. /**
  8110. * testNestedLabelInput method
  8111. *
  8112. * Test the `nestedInput` parameter
  8113. *
  8114. * @return void
  8115. */
  8116. public function testNestedLabelInput()
  8117. {
  8118. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8119. $expected = [
  8120. 'div' => ['class' => 'input text'],
  8121. 'label' => ['for' => 'foo'],
  8122. ['input' => [
  8123. 'type' => 'text',
  8124. 'name' => 'foo',
  8125. 'id' => 'foo'
  8126. ]],
  8127. 'Foo',
  8128. '/label',
  8129. '/div'
  8130. ];
  8131. $this->assertHtml($expected, $result);
  8132. }
  8133. /**
  8134. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8135. *
  8136. * This test makes sure `false` excludes the label from the render
  8137. *
  8138. * @return void
  8139. */
  8140. public function testInputLabelManipulationDisableLabels()
  8141. {
  8142. $result = $this->Form->input('test', [
  8143. 'type' => 'radio',
  8144. 'options' => ['A', 'B'],
  8145. 'labelOptions' => false
  8146. ]);
  8147. $expected = [
  8148. ['div' => ['class' => 'input radio']],
  8149. '<label',
  8150. 'Test',
  8151. '/label',
  8152. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8153. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8154. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8155. '/div',
  8156. ];
  8157. $this->assertHtml($expected, $result);
  8158. $result = $this->Form->input('checkbox1', [
  8159. 'label' => 'My checkboxes',
  8160. 'multiple' => 'checkbox',
  8161. 'type' => 'select',
  8162. 'options' => [
  8163. ['text' => 'First Checkbox', 'value' => 1],
  8164. ['text' => 'Second Checkbox', 'value' => 2]
  8165. ],
  8166. 'labelOptions' => false
  8167. ]);
  8168. $expected = [
  8169. ['div' => ['class' => 'input select']],
  8170. ['label' => ['for' => 'checkbox1']],
  8171. 'My checkboxes',
  8172. '/label',
  8173. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8174. ['div' => ['class' => 'checkbox']],
  8175. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8176. '/div',
  8177. ['div' => ['class' => 'checkbox']],
  8178. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8179. '/div',
  8180. '/div'
  8181. ];
  8182. $this->assertHtml($expected, $result);
  8183. }
  8184. /**
  8185. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8186. *
  8187. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8188. * added to the class if checked.
  8189. *
  8190. * Also checks to make sure any custom attributes are rendered correctly
  8191. *
  8192. * @return void
  8193. */
  8194. public function testInputLabelManipulationRadios()
  8195. {
  8196. $result = $this->Form->input('test', [
  8197. 'type' => 'radio',
  8198. 'options' => ['A', 'B'],
  8199. 'labelOptions' => ['class' => 'custom-class']
  8200. ]);
  8201. $expected = [
  8202. ['div' => ['class' => 'input radio']],
  8203. '<label',
  8204. 'Test',
  8205. '/label',
  8206. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8207. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8208. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8209. 'A',
  8210. '/label',
  8211. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8212. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8213. 'B',
  8214. '/label',
  8215. '/div',
  8216. ];
  8217. $this->assertHtml($expected, $result);
  8218. $result = $this->Form->input('test', [
  8219. 'type' => 'radio',
  8220. 'options' => ['A', 'B'],
  8221. 'value' => 1,
  8222. 'labelOptions' => ['class' => 'custom-class']
  8223. ]);
  8224. $expected = [
  8225. ['div' => ['class' => 'input radio']],
  8226. '<label',
  8227. 'Test',
  8228. '/label',
  8229. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8230. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8231. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8232. 'A',
  8233. '/label',
  8234. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8235. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8236. 'B',
  8237. '/label',
  8238. '/div',
  8239. ];
  8240. $this->assertHtml($expected, $result);
  8241. $result = $this->Form->input('test', [
  8242. 'type' => 'radio',
  8243. 'options' => ['A', 'B'],
  8244. 'value' => 1,
  8245. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8246. ]);
  8247. $expected = [
  8248. ['div' => ['class' => 'input radio']],
  8249. '<label',
  8250. 'Test',
  8251. '/label',
  8252. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8253. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8254. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8255. 'A',
  8256. '/label',
  8257. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8258. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8259. 'B',
  8260. '/label',
  8261. '/div',
  8262. ];
  8263. $this->assertHtml($expected, $result);
  8264. $result = $this->Form->radio('test', ['A', 'B'], [
  8265. 'label' => [
  8266. 'class' => ['custom-class', 'another-class'],
  8267. 'data-name' => 'bob'
  8268. ],
  8269. 'value' => 1
  8270. ]);
  8271. $expected = [
  8272. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8273. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8274. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8275. 'A',
  8276. '/label',
  8277. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8278. ['input' => [
  8279. 'type' => 'radio',
  8280. 'name' => 'test',
  8281. 'value' => '1',
  8282. 'id' => 'test-1',
  8283. 'checked' => 'checked'
  8284. ]],
  8285. 'B',
  8286. '/label',
  8287. ];
  8288. $this->assertHtml($expected, $result);
  8289. }
  8290. /**
  8291. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8292. *
  8293. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8294. * added to the class if checked.
  8295. *
  8296. * Also checks to make sure any custom attributes are rendered correctly
  8297. *
  8298. * @return void
  8299. */
  8300. public function testInputLabelManipulationCheckboxes()
  8301. {
  8302. $result = $this->Form->input('checkbox1', [
  8303. 'label' => 'My checkboxes',
  8304. 'multiple' => 'checkbox',
  8305. 'type' => 'select',
  8306. 'options' => [
  8307. ['text' => 'First Checkbox', 'value' => 1],
  8308. ['text' => 'Second Checkbox', 'value' => 2]
  8309. ],
  8310. 'labelOptions' => ['class' => 'custom-class'],
  8311. 'value' => ['1']
  8312. ]);
  8313. $expected = [
  8314. ['div' => ['class' => 'input select']],
  8315. ['label' => ['for' => 'checkbox1']],
  8316. 'My checkboxes',
  8317. '/label',
  8318. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8319. ['div' => ['class' => 'checkbox']],
  8320. ['label' => [
  8321. 'class' => 'custom-class selected',
  8322. 'for' => 'checkbox1-1'
  8323. ]],
  8324. ['input' => [
  8325. 'type' => 'checkbox',
  8326. 'name' => 'checkbox1[]',
  8327. 'value' => '1',
  8328. 'id' => 'checkbox1-1',
  8329. 'checked' => 'checked'
  8330. ]],
  8331. 'First Checkbox',
  8332. '/label',
  8333. '/div',
  8334. ['div' => ['class' => 'checkbox']],
  8335. ['label' => [
  8336. 'class' => 'custom-class',
  8337. 'for' => 'checkbox1-2'
  8338. ]],
  8339. ['input' => [
  8340. 'type' => 'checkbox',
  8341. 'name' => 'checkbox1[]',
  8342. 'value' => '2',
  8343. 'id' => 'checkbox1-2'
  8344. ]],
  8345. 'Second Checkbox',
  8346. '/label',
  8347. '/div',
  8348. '/div'
  8349. ];
  8350. $this->assertHtml($expected, $result);
  8351. $result = $this->Form->input('checkbox1', [
  8352. 'label' => 'My checkboxes',
  8353. 'multiple' => 'checkbox',
  8354. 'type' => 'select',
  8355. 'options' => [
  8356. ['text' => 'First Checkbox', 'value' => 1],
  8357. ['text' => 'Second Checkbox', 'value' => 2]
  8358. ],
  8359. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8360. 'value' => ['1']
  8361. ]);
  8362. $expected = [
  8363. ['div' => ['class' => 'input select']],
  8364. ['label' => ['for' => 'checkbox1']],
  8365. 'My checkboxes',
  8366. '/label',
  8367. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8368. ['div' => ['class' => 'checkbox']],
  8369. ['label' => [
  8370. 'class' => 'custom-class another-class selected',
  8371. 'data-name' => 'bob',
  8372. 'for' => 'checkbox1-1'
  8373. ]],
  8374. ['input' => [
  8375. 'type' => 'checkbox',
  8376. 'name' => 'checkbox1[]',
  8377. 'value' => '1',
  8378. 'id' => 'checkbox1-1',
  8379. 'checked' => 'checked'
  8380. ]],
  8381. 'First Checkbox',
  8382. '/label',
  8383. '/div',
  8384. ['div' => ['class' => 'checkbox']],
  8385. ['label' => [
  8386. 'class' => 'custom-class another-class',
  8387. 'data-name' => 'bob',
  8388. 'for' => 'checkbox1-2'
  8389. ]],
  8390. ['input' => [
  8391. 'type' => 'checkbox',
  8392. 'name' => 'checkbox1[]',
  8393. 'value' => '2',
  8394. 'id' => 'checkbox1-2'
  8395. ]],
  8396. 'Second Checkbox',
  8397. '/label',
  8398. '/div',
  8399. '/div'
  8400. ];
  8401. $this->assertHtml($expected, $result);
  8402. }
  8403. }