FormHelperTest.php 305 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Collection\Collection;
  17. use Cake\Core\Configure;
  18. use Cake\Form\Form;
  19. use Cake\Http\ServerRequest;
  20. use Cake\ORM\Entity;
  21. use Cake\ORM\Table;
  22. use Cake\ORM\TableRegistry;
  23. use Cake\Routing\Router;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\Security;
  26. use Cake\View\Helper\FormHelper;
  27. use Cake\View\View;
  28. /**
  29. * Test stub.
  30. */
  31. class Article extends Entity
  32. {
  33. }
  34. /**
  35. * Contact class
  36. */
  37. class ContactsTable extends Table
  38. {
  39. /**
  40. * Default schema
  41. *
  42. * @var array
  43. */
  44. protected $_schema = [
  45. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  46. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  47. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  48. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  51. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  52. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  53. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  54. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  55. ];
  56. /**
  57. * Initializes the schema
  58. *
  59. * @return void
  60. */
  61. public function initialize(array $config)
  62. {
  63. $this->schema($this->_schema);
  64. }
  65. }
  66. /**
  67. * ValidateUser class
  68. */
  69. class ValidateUsersTable extends Table
  70. {
  71. /**
  72. * schema method
  73. *
  74. * @var array
  75. */
  76. protected $_schema = [
  77. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  78. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  79. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  80. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  81. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  82. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  83. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  84. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  85. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  86. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  87. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  88. ];
  89. /**
  90. * Initializes the schema
  91. *
  92. * @return void
  93. */
  94. public function initialize(array $config)
  95. {
  96. $this->schema($this->_schema);
  97. }
  98. }
  99. /**
  100. * FormHelperTest class
  101. *
  102. * @property \Cake\View\Helper\FormHelper $Form
  103. * @property \Cake\View\View $View
  104. */
  105. class FormHelperTest extends TestCase
  106. {
  107. /**
  108. * Fixtures to be used
  109. *
  110. * @var array
  111. */
  112. public $fixtures = ['core.articles', 'core.comments'];
  113. /**
  114. * Do not load the fixtures by default
  115. *
  116. * @var bool
  117. */
  118. public $autoFixtures = false;
  119. /**
  120. * @var array
  121. */
  122. protected $article = [];
  123. /**
  124. * setUp method
  125. *
  126. * @return void
  127. */
  128. public function setUp()
  129. {
  130. parent::setUp();
  131. Configure::write('Config.language', 'eng');
  132. Configure::write('App.base', '');
  133. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  134. $this->View = new View();
  135. $this->Form = new FormHelper($this->View);
  136. $request = new ServerRequest([
  137. 'webroot' => '',
  138. 'base' => '',
  139. 'url' => '/articles/add',
  140. 'params' => [
  141. 'controller' => 'articles',
  142. 'action' => 'add',
  143. ]
  144. ]);
  145. $this->Form->Url->request = $this->Form->request = $request;
  146. $this->dateRegex = [
  147. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  148. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  149. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  150. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  151. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  152. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  153. ];
  154. $this->article = [
  155. 'schema' => [
  156. 'id' => ['type' => 'integer'],
  157. 'author_id' => ['type' => 'integer', 'null' => true],
  158. 'title' => ['type' => 'string', 'null' => true],
  159. 'body' => 'text',
  160. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  161. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  162. ],
  163. 'required' => [
  164. 'author_id' => true,
  165. 'title' => true,
  166. ]
  167. ];
  168. Security::salt('foo!');
  169. Router::connect('/:controller', ['action' => 'index']);
  170. Router::connect('/:controller/:action/*');
  171. }
  172. /**
  173. * tearDown method
  174. *
  175. * @return void
  176. */
  177. public function tearDown()
  178. {
  179. parent::tearDown();
  180. unset($this->Form, $this->Controller, $this->View);
  181. TableRegistry::clear();
  182. }
  183. /**
  184. * Test construct() with the templates option.
  185. *
  186. * @return void
  187. */
  188. public function testConstructTemplatesFile()
  189. {
  190. $helper = new FormHelper($this->View, [
  191. 'templates' => 'htmlhelper_tags'
  192. ]);
  193. $result = $helper->control('name');
  194. $this->assertContains('<input', $result);
  195. }
  196. /**
  197. * Test that when specifying custom widgets the config array for that widget
  198. * is overwritten instead of merged.
  199. *
  200. * @return void
  201. */
  202. public function testConstructWithWidgets()
  203. {
  204. $config = [
  205. 'widgets' => [
  206. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  207. ]
  208. ];
  209. $helper = new FormHelper($this->View, $config);
  210. $registry = $helper->widgetRegistry();
  211. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  212. }
  213. /**
  214. * Test that when specifying custom widgets config file and it should be
  215. * added to widgets array. WidgetRegistry will load widgets in constructor.
  216. *
  217. * @return void
  218. */
  219. public function testConstructWithWidgetsConfig()
  220. {
  221. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  222. $registry = $helper->widgetRegistry();
  223. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  224. }
  225. /**
  226. * Test registering a new widget class and rendering it.
  227. *
  228. * @return void
  229. */
  230. public function testAddWidgetAndRenderWidget()
  231. {
  232. $data = [
  233. 'val' => 1
  234. ];
  235. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  236. $this->Form->addWidget('test', $mock);
  237. $mock->expects($this->once())
  238. ->method('render')
  239. ->with($data)
  240. ->will($this->returnValue('HTML'));
  241. $result = $this->Form->widget('test', $data);
  242. $this->assertEquals('HTML', $result);
  243. }
  244. /**
  245. * Test that secureFields() of widget is called after calling render(),
  246. * not before.
  247. *
  248. * @return void
  249. */
  250. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  251. {
  252. $data = [
  253. 'val' => 1,
  254. 'name' => 'test'
  255. ];
  256. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  257. $this->Form->addWidget('test', $mock);
  258. $mock->expects($this->at(0))
  259. ->method('render')
  260. ->with($data)
  261. ->will($this->returnValue('HTML'));
  262. $mock->expects($this->at(1))
  263. ->method('secureFields')
  264. ->with($data)
  265. ->will($this->returnValue(['test']));
  266. $result = $this->Form->widget('test', $data + ['secure' => true]);
  267. $this->assertEquals('HTML', $result);
  268. }
  269. /**
  270. * Test that empty string is not added to secure fields list when
  271. * rendering input widget without name.
  272. *
  273. * @return void
  274. */
  275. public function testRenderingWidgetWithEmptyName()
  276. {
  277. $this->assertEquals([], $this->Form->fields);
  278. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  279. $this->assertEquals('<select name=""></select>', $result);
  280. $this->assertEquals([], $this->Form->fields);
  281. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  282. $this->assertEquals('<select name="0"></select>', $result);
  283. $this->assertEquals(['0'], $this->Form->fields);
  284. }
  285. /**
  286. * Test registering an invalid widget class.
  287. *
  288. * @return void
  289. */
  290. public function testAddWidgetInvalid()
  291. {
  292. $this->expectException(\RuntimeException::class);
  293. $mock = new \StdClass();
  294. $this->Form->addWidget('test', $mock);
  295. $this->Form->widget('test');
  296. }
  297. /**
  298. * Test adding a new context class.
  299. *
  300. * @return void
  301. */
  302. public function testAddContextProvider()
  303. {
  304. $context = 'My data';
  305. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  306. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  307. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  308. $this->assertEquals($context, $data['entity']);
  309. return $stub;
  310. });
  311. $this->Form->create($context);
  312. $result = $this->Form->context();
  313. $this->assertSame($stub, $result);
  314. }
  315. /**
  316. * Test replacing a context class.
  317. *
  318. * @return void
  319. */
  320. public function testAddContextProviderReplace()
  321. {
  322. $entity = new Article();
  323. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  324. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  325. return $stub;
  326. });
  327. $this->Form->create($entity);
  328. $result = $this->Form->context();
  329. $this->assertSame($stub, $result);
  330. }
  331. /**
  332. * Test overriding a context class.
  333. *
  334. * @return void
  335. */
  336. public function testAddContextProviderAdd()
  337. {
  338. $entity = new Article();
  339. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  340. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  341. if ($data['entity'] instanceof Entity) {
  342. return $stub;
  343. }
  344. });
  345. $this->Form->create($entity);
  346. $result = $this->Form->context();
  347. $this->assertSame($stub, $result);
  348. }
  349. /**
  350. * Test adding an invalid context class.
  351. *
  352. * @return void
  353. */
  354. public function testAddContextProviderInvalid()
  355. {
  356. $this->expectException(\RuntimeException::class);
  357. $this->expectExceptionMessage('Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.');
  358. $context = 'My data';
  359. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  360. return new \StdClass();
  361. });
  362. $this->Form->create($context);
  363. }
  364. /**
  365. * Provides context options for create().
  366. *
  367. * @return array
  368. */
  369. public function contextSelectionProvider()
  370. {
  371. $entity = new Article();
  372. $collection = new Collection([$entity]);
  373. $emptyCollection = new Collection([]);
  374. $arrayObject = new \ArrayObject([]);
  375. $data = [
  376. 'schema' => [
  377. 'title' => ['type' => 'string']
  378. ]
  379. ];
  380. $form = new Form();
  381. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  382. return [
  383. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  384. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  385. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  386. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  387. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  388. 'form' => [$form, 'Cake\View\Form\FormContext'],
  389. 'none' => [null, 'Cake\View\Form\NullContext'],
  390. 'false' => [false, 'Cake\View\Form\NullContext'],
  391. 'custom' => [$custom, get_class($custom)]
  392. ];
  393. }
  394. /**
  395. * Test default context selection in create()
  396. *
  397. * @dataProvider contextSelectionProvider
  398. * @return void
  399. */
  400. public function testCreateContextSelectionBuiltIn($data, $class)
  401. {
  402. $this->loadFixtures('Articles');
  403. $this->Form->create($data);
  404. $this->assertInstanceOf($class, $this->Form->context());
  405. }
  406. /**
  407. * Data provider for type option.
  408. *
  409. * @return array
  410. */
  411. public static function requestTypeProvider()
  412. {
  413. return [
  414. // type, method, override
  415. ['post', 'post', 'POST'],
  416. ['put', 'post', 'PUT'],
  417. ['patch', 'post', 'PATCH'],
  418. ['delete', 'post', 'DELETE'],
  419. ];
  420. }
  421. /**
  422. * Test creating file forms.
  423. *
  424. * @return void
  425. */
  426. public function testCreateFile()
  427. {
  428. $encoding = strtolower(Configure::read('App.encoding'));
  429. $result = $this->Form->create(false, ['type' => 'file']);
  430. $expected = [
  431. 'form' => [
  432. 'method' => 'post', 'action' => '/articles/add',
  433. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  434. ],
  435. 'div' => ['style' => 'display:none;'],
  436. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  437. '/div'
  438. ];
  439. $this->assertHtml($expected, $result);
  440. }
  441. /**
  442. * Test creating GET forms.
  443. *
  444. * @return void
  445. */
  446. public function testCreateGet()
  447. {
  448. $encoding = strtolower(Configure::read('App.encoding'));
  449. $result = $this->Form->create(false, ['type' => 'get']);
  450. $expected = ['form' => [
  451. 'method' => 'get', 'action' => '/articles/add',
  452. 'accept-charset' => $encoding
  453. ]];
  454. $this->assertHtml($expected, $result);
  455. }
  456. /**
  457. * Test explicit method/enctype options.
  458. *
  459. * Explicit method overwrites inferred method from 'type'
  460. *
  461. * @return void
  462. */
  463. public function testCreateExplicitMethodEnctype()
  464. {
  465. $encoding = strtolower(Configure::read('App.encoding'));
  466. $result = $this->Form->create(false, [
  467. 'type' => 'get',
  468. 'method' => 'put',
  469. 'enctype' => 'multipart/form-data'
  470. ]);
  471. $expected = ['form' => [
  472. 'method' => 'put',
  473. 'action' => '/articles/add',
  474. 'enctype' => 'multipart/form-data',
  475. 'accept-charset' => $encoding
  476. ]];
  477. $this->assertHtml($expected, $result);
  478. }
  479. /**
  480. * Test create() with the templates option.
  481. *
  482. * @return void
  483. */
  484. public function testCreateTemplatesArray()
  485. {
  486. $result = $this->Form->create($this->article, [
  487. 'templates' => [
  488. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  489. ]
  490. ]);
  491. $expected = [
  492. 'form' => [
  493. 'class' => 'form-horizontal',
  494. 'method' => 'post',
  495. 'action' => '/articles/add',
  496. 'accept-charset' => 'utf-8'
  497. ]
  498. ];
  499. $this->assertHtml($expected, $result);
  500. }
  501. /**
  502. * Test create() with the templates option.
  503. *
  504. * @return void
  505. */
  506. public function testCreateTemplatesFile()
  507. {
  508. $result = $this->Form->create($this->article, [
  509. 'templates' => 'htmlhelper_tags',
  510. ]);
  511. $expected = [
  512. 'start form',
  513. 'div' => ['class' => 'hidden'],
  514. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  515. '/div'
  516. ];
  517. $this->assertHtml($expected, $result);
  518. }
  519. /**
  520. * Test that create() and end() restore templates.
  521. *
  522. * @return void
  523. */
  524. public function testCreateEndRestoreTemplates()
  525. {
  526. $this->Form->create($this->article, [
  527. 'templates' => ['input' => 'custom input element']
  528. ]);
  529. $this->Form->end();
  530. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  531. }
  532. /**
  533. * Test using template vars in various templates used by control() method.
  534. *
  535. * @return void
  536. */
  537. public function testControlTemplateVars()
  538. {
  539. $result = $this->Form->control('text', [
  540. 'templates' => [
  541. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  542. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  543. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  544. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  545. ],
  546. 'templateVars' => [
  547. 'forinput' => 'in-input',
  548. 'forlabel' => 'in-label',
  549. 'forgroup' => 'in-group',
  550. 'forcontainer' => 'in-container'
  551. ]
  552. ]);
  553. $expected = [
  554. 'div' => ['class'],
  555. 'label' => ['for'],
  556. 'Text in-label',
  557. '/label',
  558. 'in-group',
  559. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  560. 'in-container',
  561. '/div',
  562. ];
  563. $this->assertHtml($expected, $result);
  564. }
  565. /**
  566. * Test ensuring template variables work in template files loaded
  567. * during control().
  568. *
  569. * @return void
  570. */
  571. public function testControlTemplatesFromFile()
  572. {
  573. $result = $this->Form->control('title', [
  574. 'templates' => 'test_templates',
  575. 'templateVars' => [
  576. 'forcontainer' => 'container-data'
  577. ]
  578. ]);
  579. $expected = [
  580. 'div' => ['class'],
  581. 'label' => ['for'],
  582. 'Title',
  583. '/label',
  584. 'input' => ['name', 'type' => 'text', 'id'],
  585. 'container-data',
  586. '/div',
  587. ];
  588. $this->assertHtml($expected, $result);
  589. }
  590. /**
  591. * Test using template vars in inputSubmit and submitContainer template.
  592. *
  593. * @return void
  594. */
  595. public function testSubmitTemplateVars()
  596. {
  597. $this->Form->templates([
  598. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  599. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  600. ]);
  601. $result = $this->Form->submit('Submit', [
  602. 'templateVars' => [
  603. 'forinput' => 'in-input',
  604. 'forcontainer' => 'in-container'
  605. ]
  606. ]);
  607. $expected = [
  608. 'div' => ['class'],
  609. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  610. 'in-container',
  611. '/div',
  612. ];
  613. $this->assertHtml($expected, $result);
  614. }
  615. /**
  616. * test the create() method
  617. *
  618. * @dataProvider requestTypeProvider
  619. * @return void
  620. */
  621. public function testCreateTypeOptions($type, $method, $override)
  622. {
  623. $encoding = strtolower(Configure::read('App.encoding'));
  624. $result = $this->Form->create(false, ['type' => $type]);
  625. $expected = [
  626. 'form' => [
  627. 'method' => $method, 'action' => '/articles/add',
  628. 'accept-charset' => $encoding
  629. ],
  630. 'div' => ['style' => 'display:none;'],
  631. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  632. '/div'
  633. ];
  634. $this->assertHtml($expected, $result);
  635. }
  636. /**
  637. * Test using template vars in Create (formStart template)
  638. *
  639. * @return void
  640. */
  641. public function testCreateTemplateVars()
  642. {
  643. $result = $this->Form->create($this->article, [
  644. 'templates' => [
  645. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  646. ],
  647. 'templateVars' => ['header' => 'headertext']
  648. ]);
  649. $expected = [
  650. 'h4' => ['class'],
  651. 'headertext',
  652. '/h4',
  653. 'form' => [
  654. 'method' => 'post',
  655. 'action' => '/articles/add',
  656. 'accept-charset' => 'utf-8'
  657. ]
  658. ];
  659. $this->assertHtml($expected, $result);
  660. }
  661. /**
  662. * Test opening a form for an update operation.
  663. *
  664. * @return void
  665. */
  666. public function testCreateUpdateForm()
  667. {
  668. $encoding = strtolower(Configure::read('App.encoding'));
  669. $this->Form->request->here = '/articles/edit/1';
  670. $this->Form->request->params['action'] = 'edit';
  671. $this->article['defaults']['id'] = 1;
  672. $result = $this->Form->create($this->article);
  673. $expected = [
  674. 'form' => [
  675. 'method' => 'post',
  676. 'action' => '/articles/edit/1',
  677. 'accept-charset' => $encoding
  678. ],
  679. 'div' => ['style' => 'display:none;'],
  680. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  681. '/div'
  682. ];
  683. $this->assertHtml($expected, $result);
  684. }
  685. /**
  686. * test create() with automatic url generation
  687. *
  688. * @return void
  689. */
  690. public function testCreateAutoUrl()
  691. {
  692. $encoding = strtolower(Configure::read('App.encoding'));
  693. $this->Form->request->params['action'] = 'delete';
  694. $this->Form->request->here = '/articles/delete/10';
  695. $this->Form->request->base = '';
  696. $result = $this->Form->create($this->article);
  697. $expected = [
  698. 'form' => [
  699. 'method' => 'post', 'action' => '/articles/delete/10',
  700. 'accept-charset' => $encoding
  701. ],
  702. 'div' => ['style' => 'display:none;'],
  703. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  704. '/div'
  705. ];
  706. $this->assertHtml($expected, $result);
  707. $this->article['defaults'] = ['id' => 1];
  708. $this->Form->request->here = '/articles/edit/1';
  709. $this->Form->request->params['action'] = 'delete';
  710. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  711. $expected = [
  712. 'form' => [
  713. 'method' => 'post',
  714. 'action' => '/articles/edit/1',
  715. 'accept-charset' => $encoding
  716. ],
  717. 'div' => ['style' => 'display:none;'],
  718. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  719. '/div'
  720. ];
  721. $this->assertHtml($expected, $result);
  722. $this->Form->request->params['action'] = 'add';
  723. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  724. $expected = [
  725. 'form' => [
  726. 'method' => 'post',
  727. 'action' => '/articles/publish/1',
  728. 'accept-charset' => $encoding
  729. ],
  730. 'div' => ['style' => 'display:none;'],
  731. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  732. '/div'
  733. ];
  734. $this->assertHtml($expected, $result);
  735. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  736. $expected = [
  737. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  738. 'div' => ['style' => 'display:none;'],
  739. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  740. '/div'
  741. ];
  742. $this->assertHtml($expected, $result);
  743. $this->Form->request->params['controller'] = 'Pages';
  744. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  745. $expected = [
  746. 'form' => [
  747. 'method' => 'post', 'action' => '/Pages/signup/1',
  748. 'accept-charset' => $encoding
  749. ],
  750. 'div' => ['style' => 'display:none;'],
  751. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  752. '/div'
  753. ];
  754. $this->assertHtml($expected, $result);
  755. }
  756. /**
  757. * Test create() with no URL (no "action" attribute for <form> tag)
  758. *
  759. * @return void
  760. */
  761. public function testCreateNoUrl()
  762. {
  763. $result = $this->Form->create(false, ['url' => false]);
  764. $expected = [
  765. 'form' => [
  766. 'method' => 'post',
  767. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  768. ],
  769. 'div' => ['style' => 'display:none;'],
  770. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  771. '/div'
  772. ];
  773. $this->assertHtml($expected, $result);
  774. }
  775. /**
  776. * test create() with a custom route
  777. *
  778. * @return void
  779. */
  780. public function testCreateCustomRoute()
  781. {
  782. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  783. $encoding = strtolower(Configure::read('App.encoding'));
  784. $this->Form->request->params['controller'] = 'users';
  785. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  786. $expected = [
  787. 'form' => [
  788. 'method' => 'post', 'action' => '/login',
  789. 'accept-charset' => $encoding
  790. ],
  791. 'div' => ['style' => 'display:none;'],
  792. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  793. '/div'
  794. ];
  795. $this->assertHtml($expected, $result);
  796. Router::connect(
  797. '/new-article',
  798. ['controller' => 'articles', 'action' => 'myaction'],
  799. ['_name' => 'my-route']
  800. );
  801. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  802. $expected = [
  803. 'form' => [
  804. 'method' => 'post', 'action' => '/new-article',
  805. 'accept-charset' => $encoding,
  806. ],
  807. 'div' => ['style' => 'display:none;'],
  808. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  809. '/div'
  810. ];
  811. $this->assertHtml($expected, $result);
  812. }
  813. /**
  814. * test automatic accept-charset overriding
  815. *
  816. * @return void
  817. */
  818. public function testCreateWithAcceptCharset()
  819. {
  820. $result = $this->Form->create(
  821. $this->article,
  822. [
  823. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  824. ]
  825. );
  826. $expected = [
  827. 'form' => [
  828. 'method' => 'post', 'action' => '/articles',
  829. 'accept-charset' => 'iso-8859-1'
  830. ],
  831. 'div' => ['style' => 'display:none;'],
  832. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  833. '/div'
  834. ];
  835. $this->assertHtml($expected, $result);
  836. }
  837. /**
  838. * Test base form URL when url param is passed with multiple parameters (&)
  839. *
  840. */
  841. public function testCreateQueryStringRequest()
  842. {
  843. $encoding = strtolower(Configure::read('App.encoding'));
  844. $result = $this->Form->create($this->article, [
  845. 'type' => 'post',
  846. 'escape' => false,
  847. 'url' => [
  848. 'controller' => 'controller',
  849. 'action' => 'action',
  850. '?' => ['param1' => 'value1', 'param2' => 'value2']
  851. ]
  852. ]);
  853. $expected = [
  854. 'form' => [
  855. 'method' => 'post',
  856. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  857. 'accept-charset' => $encoding
  858. ],
  859. 'div' => ['style' => 'display:none;'],
  860. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  861. '/div'
  862. ];
  863. $this->assertHtml($expected, $result);
  864. $result = $this->Form->create($this->article, [
  865. 'type' => 'post',
  866. 'url' => [
  867. 'controller' => 'controller',
  868. 'action' => 'action',
  869. '?' => ['param1' => 'value1', 'param2' => 'value2']
  870. ]
  871. ]);
  872. $this->assertHtml($expected, $result);
  873. }
  874. /**
  875. * test that create() doesn't cause errors by multiple id's being in the primary key
  876. * as could happen with multiple select or checkboxes.
  877. *
  878. * @return void
  879. */
  880. public function testCreateWithMultipleIdInData()
  881. {
  882. $encoding = strtolower(Configure::read('App.encoding'));
  883. $this->Form->request->data['Article']['id'] = [1, 2];
  884. $result = $this->Form->create($this->article);
  885. $expected = [
  886. 'form' => [
  887. 'method' => 'post',
  888. 'action' => '/articles/add',
  889. 'accept-charset' => $encoding
  890. ],
  891. 'div' => ['style' => 'display:none;'],
  892. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  893. '/div'
  894. ];
  895. $this->assertHtml($expected, $result);
  896. }
  897. /**
  898. * test that create() doesn't add in extra passed params.
  899. *
  900. * @return void
  901. */
  902. public function testCreatePassedArgs()
  903. {
  904. $encoding = strtolower(Configure::read('App.encoding'));
  905. $this->Form->request->data['Article']['id'] = 1;
  906. $result = $this->Form->create($this->article, [
  907. 'type' => 'post',
  908. 'escape' => false,
  909. 'url' => [
  910. 'action' => 'edit',
  911. 'myparam'
  912. ]
  913. ]);
  914. $expected = [
  915. 'form' => [
  916. 'method' => 'post',
  917. 'action' => '/articles/edit/myparam',
  918. 'accept-charset' => $encoding
  919. ],
  920. 'div' => ['style' => 'display:none;'],
  921. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  922. '/div'
  923. ];
  924. $this->assertHtml($expected, $result);
  925. }
  926. /**
  927. * test creating a get form, and get form inputs.
  928. *
  929. * @return void
  930. */
  931. public function testGetFormCreate()
  932. {
  933. $encoding = strtolower(Configure::read('App.encoding'));
  934. $result = $this->Form->create($this->article, ['type' => 'get']);
  935. $expected = ['form' => [
  936. 'method' => 'get', 'action' => '/articles/add',
  937. 'accept-charset' => $encoding
  938. ]];
  939. $this->assertHtml($expected, $result);
  940. $result = $this->Form->text('title');
  941. $expected = ['input' => [
  942. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  943. ]];
  944. $this->assertHtml($expected, $result);
  945. $result = $this->Form->password('password');
  946. $expected = ['input' => [
  947. 'name' => 'password', 'type' => 'password'
  948. ]];
  949. $this->assertHtml($expected, $result);
  950. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  951. $result = $this->Form->text('user_form');
  952. $expected = ['input' => [
  953. 'name' => 'user_form', 'type' => 'text'
  954. ]];
  955. $this->assertHtml($expected, $result);
  956. }
  957. /**
  958. * test get form, and inputs when the model param is false
  959. *
  960. * @return void
  961. */
  962. public function testGetFormWithFalseModel()
  963. {
  964. $encoding = strtolower(Configure::read('App.encoding'));
  965. $this->Form->request->params['controller'] = 'contact_test';
  966. $result = $this->Form->create(false, [
  967. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  968. ]);
  969. $expected = ['form' => [
  970. 'method' => 'get', 'action' => '/contact_test/add',
  971. 'accept-charset' => $encoding
  972. ]];
  973. $this->assertHtml($expected, $result);
  974. $result = $this->Form->text('reason');
  975. $expected = [
  976. 'input' => ['type' => 'text', 'name' => 'reason']
  977. ];
  978. $this->assertHtml($expected, $result);
  979. }
  980. /**
  981. * testFormCreateWithSecurity method
  982. *
  983. * Test form->create() with security key.
  984. *
  985. * @return void
  986. */
  987. public function testCreateWithSecurity()
  988. {
  989. $this->Form->request->params['_csrfToken'] = 'testKey';
  990. $encoding = strtolower(Configure::read('App.encoding'));
  991. $result = $this->Form->create($this->article, [
  992. 'url' => '/articles/publish',
  993. ]);
  994. $expected = [
  995. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  996. 'div' => ['style' => 'display:none;'],
  997. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  998. ['input' => [
  999. 'type' => 'hidden',
  1000. 'name' => '_csrfToken',
  1001. 'value' => 'testKey',
  1002. 'autocomplete' => 'off',
  1003. ]],
  1004. '/div'
  1005. ];
  1006. $this->assertHtml($expected, $result);
  1007. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1008. $expected['form']['id'] = 'MyForm';
  1009. $this->assertHtml($expected, $result);
  1010. }
  1011. /**
  1012. * testFormCreateGetNoSecurity method
  1013. *
  1014. * Test form->create() with no security key as its a get form
  1015. *
  1016. * @return void
  1017. */
  1018. public function testCreateEndGetNoSecurity()
  1019. {
  1020. $this->Form->request->params['_csrfToken'] = 'testKey';
  1021. $article = new Article();
  1022. $result = $this->Form->create($article, [
  1023. 'type' => 'get',
  1024. 'url' => '/contacts/add'
  1025. ]);
  1026. $this->assertNotContains('testKey', $result);
  1027. $result = $this->Form->end();
  1028. $this->assertNotContains('testKey', $result);
  1029. }
  1030. /**
  1031. * test that create() clears the fields property so it starts fresh
  1032. *
  1033. * @return void
  1034. */
  1035. public function testCreateClearingFields()
  1036. {
  1037. $this->Form->fields = ['model_id'];
  1038. $this->Form->create($this->article);
  1039. $this->assertEquals([], $this->Form->fields);
  1040. }
  1041. /**
  1042. * Tests form hash generation with model-less data
  1043. *
  1044. * @return void
  1045. */
  1046. public function testValidateHashNoModel()
  1047. {
  1048. $this->Form->request->params['_Token'] = 'foo';
  1049. $result = $this->Form->secure(['anything']);
  1050. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1051. }
  1052. /**
  1053. * Tests that hidden fields generated for checkboxes don't get locked
  1054. *
  1055. * @return void
  1056. */
  1057. public function testNoCheckboxLocking()
  1058. {
  1059. $this->Form->request->params['_Token'] = 'foo';
  1060. $this->assertSame([], $this->Form->fields);
  1061. $this->Form->checkbox('check', ['value' => '1']);
  1062. $this->assertSame($this->Form->fields, ['check']);
  1063. }
  1064. /**
  1065. * testFormSecurityFields method
  1066. *
  1067. * Test generation of secure form hash generation.
  1068. *
  1069. * @return void
  1070. */
  1071. public function testFormSecurityFields()
  1072. {
  1073. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1074. $this->Form->request->params['_Token'] = 'testKey';
  1075. $result = $this->Form->secure($fields);
  1076. $hash = Security::hash(serialize($fields) . Security::salt());
  1077. $hash .= ':' . 'Model.valid';
  1078. $hash = urlencode($hash);
  1079. $tokenDebug = urlencode(json_encode([
  1080. '',
  1081. $fields,
  1082. []
  1083. ]));
  1084. $expected = [
  1085. 'div' => ['style' => 'display:none;'],
  1086. ['input' => [
  1087. 'type' => 'hidden',
  1088. 'name' => '_Token[fields]',
  1089. 'value' => $hash,
  1090. 'autocomplete' => 'off',
  1091. ]],
  1092. ['input' => [
  1093. 'type' => 'hidden',
  1094. 'name' => '_Token[unlocked]',
  1095. 'value' => '',
  1096. 'autocomplete' => 'off',
  1097. ]],
  1098. ['input' => [
  1099. 'type' => 'hidden',
  1100. 'name' => '_Token[debug]',
  1101. 'value' => $tokenDebug,
  1102. 'autocomplete' => 'off',
  1103. ]],
  1104. '/div'
  1105. ];
  1106. $this->assertHtml($expected, $result);
  1107. }
  1108. /**
  1109. * testFormSecurityFields method
  1110. *
  1111. * Test debug token is not generated if debug is false
  1112. *
  1113. * @return void
  1114. */
  1115. public function testFormSecurityFieldsNoDebugMode()
  1116. {
  1117. Configure::write('debug', false);
  1118. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1119. $this->Form->request->params['_Token'] = 'testKey';
  1120. $result = $this->Form->secure($fields);
  1121. $hash = Security::hash(serialize($fields) . Security::salt());
  1122. $hash .= ':' . 'Model.valid';
  1123. $hash = urlencode($hash);
  1124. $expected = [
  1125. 'div' => ['style' => 'display:none;'],
  1126. ['input' => [
  1127. 'type' => 'hidden',
  1128. 'name' => '_Token[fields]',
  1129. 'autocomplete' => 'off',
  1130. 'value' => $hash
  1131. ]],
  1132. ['input' => [
  1133. 'type' => 'hidden',
  1134. 'name' => '_Token[unlocked]',
  1135. 'autocomplete' => 'off',
  1136. 'value' => '',
  1137. ]],
  1138. '/div'
  1139. ];
  1140. $this->assertHtml($expected, $result);
  1141. }
  1142. /**
  1143. * Tests correct generation of number fields for smallint
  1144. *
  1145. * @return void
  1146. */
  1147. public function testTextFieldGenerationForSmallint()
  1148. {
  1149. $this->article['schema'] = [
  1150. 'foo' => [
  1151. 'type' => 'smallinteger',
  1152. 'null' => false,
  1153. 'default' => null,
  1154. 'length' => 10
  1155. ]
  1156. ];
  1157. $this->Form->create($this->article);
  1158. $result = $this->Form->control('foo');
  1159. $this->assertContains('class="input number"', $result);
  1160. $this->assertContains('type="number"', $result);
  1161. }
  1162. /**
  1163. * Tests correct generation of number fields for tinyint
  1164. *
  1165. * @return void
  1166. */
  1167. public function testTextFieldGenerationForTinyint()
  1168. {
  1169. $this->article['schema'] = [
  1170. 'foo' => [
  1171. 'type' => 'tinyinteger',
  1172. 'null' => false,
  1173. 'default' => null,
  1174. 'length' => 10
  1175. ]
  1176. ];
  1177. $this->Form->create($this->article);
  1178. $result = $this->Form->control('foo');
  1179. $this->assertContains('class="input number"', $result);
  1180. $this->assertContains('type="number"', $result);
  1181. }
  1182. /**
  1183. * Tests correct generation of number fields for double and float fields
  1184. *
  1185. * @return void
  1186. */
  1187. public function testTextFieldGenerationForFloats()
  1188. {
  1189. $this->article['schema'] = [
  1190. 'foo' => [
  1191. 'type' => 'float',
  1192. 'null' => false,
  1193. 'default' => null,
  1194. 'length' => 10
  1195. ]
  1196. ];
  1197. $this->Form->create($this->article);
  1198. $result = $this->Form->control('foo');
  1199. $expected = [
  1200. 'div' => ['class' => 'input number'],
  1201. 'label' => ['for' => 'foo'],
  1202. 'Foo',
  1203. '/label',
  1204. ['input' => [
  1205. 'type' => 'number',
  1206. 'name' => 'foo',
  1207. 'id' => 'foo',
  1208. 'step' => 'any'
  1209. ]],
  1210. '/div'
  1211. ];
  1212. $this->assertHtml($expected, $result);
  1213. $result = $this->Form->control('foo', ['step' => 0.5]);
  1214. $expected = [
  1215. 'div' => ['class' => 'input number'],
  1216. 'label' => ['for' => 'foo'],
  1217. 'Foo',
  1218. '/label',
  1219. ['input' => [
  1220. 'type' => 'number',
  1221. 'name' => 'foo',
  1222. 'id' => 'foo',
  1223. 'step' => '0.5'
  1224. ]],
  1225. '/div'
  1226. ];
  1227. $this->assertHtml($expected, $result);
  1228. }
  1229. /**
  1230. * Tests correct generation of number fields for integer fields
  1231. *
  1232. * @return void
  1233. */
  1234. public function testTextFieldTypeNumberGenerationForIntegers()
  1235. {
  1236. TableRegistry::get('Contacts', [
  1237. 'className' => __NAMESPACE__ . '\ContactsTable'
  1238. ]);
  1239. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1240. $result = $this->Form->control('age');
  1241. $expected = [
  1242. 'div' => ['class' => 'input number'],
  1243. 'label' => ['for' => 'age'],
  1244. 'Age',
  1245. '/label',
  1246. ['input' => [
  1247. 'type' => 'number', 'name' => 'age',
  1248. 'id' => 'age'
  1249. ]],
  1250. '/div'
  1251. ];
  1252. $this->assertHtml($expected, $result);
  1253. }
  1254. /**
  1255. * Tests correct generation of file upload fields for binary fields
  1256. *
  1257. * @return void
  1258. */
  1259. public function testFileUploadFieldTypeGenerationForBinaries()
  1260. {
  1261. $table = TableRegistry::get('Contacts', [
  1262. 'className' => __NAMESPACE__ . '\ContactsTable'
  1263. ]);
  1264. $table->schema(['foo' => [
  1265. 'type' => 'binary',
  1266. 'null' => false,
  1267. 'default' => null,
  1268. 'length' => 1024
  1269. ]]);
  1270. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1271. $result = $this->Form->control('foo');
  1272. $expected = [
  1273. 'div' => ['class' => 'input file'],
  1274. 'label' => ['for' => 'foo'],
  1275. 'Foo',
  1276. '/label',
  1277. ['input' => [
  1278. 'type' => 'file', 'name' => 'foo',
  1279. 'id' => 'foo'
  1280. ]],
  1281. '/div'
  1282. ];
  1283. $this->assertHtml($expected, $result);
  1284. }
  1285. /**
  1286. * testFormSecurityMultipleFields method
  1287. *
  1288. * Test secure() with multiple row form. Ensure hash is correct.
  1289. *
  1290. * @return void
  1291. */
  1292. public function testFormSecurityMultipleFields()
  1293. {
  1294. $this->Form->request->params['_Token'] = 'foo';
  1295. $fields = [
  1296. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1297. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1298. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1299. ];
  1300. $result = $this->Form->secure($fields);
  1301. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1302. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1303. $tokenDebug = urlencode(json_encode([
  1304. '',
  1305. $fields,
  1306. []
  1307. ]));
  1308. $expected = [
  1309. 'div' => ['style' => 'display:none;'],
  1310. ['input' => [
  1311. 'type' => 'hidden',
  1312. 'name' => '_Token[fields]',
  1313. 'value' => $hash,
  1314. 'autocomplete' => 'off',
  1315. ]],
  1316. ['input' => [
  1317. 'type' => 'hidden',
  1318. 'name' => '_Token[unlocked]',
  1319. 'autocomplete' => 'off',
  1320. 'value' => ''
  1321. ]],
  1322. ['input' => [
  1323. 'type' => 'hidden',
  1324. 'name' => '_Token[debug]',
  1325. 'value' => $tokenDebug,
  1326. 'autocomplete' => 'off',
  1327. ]],
  1328. '/div'
  1329. ];
  1330. $this->assertHtml($expected, $result);
  1331. }
  1332. /**
  1333. * testFormSecurityMultipleSubmitButtons
  1334. *
  1335. * test form submit generation and ensure that _Token is only created on end()
  1336. *
  1337. * @return void
  1338. */
  1339. public function testFormSecurityMultipleSubmitButtons()
  1340. {
  1341. $this->Form->request->params['_Token'] = 'testKey';
  1342. $this->Form->create($this->article);
  1343. $this->Form->text('Address.title');
  1344. $this->Form->text('Address.first_name');
  1345. $result = $this->Form->submit('Save', ['name' => 'save']);
  1346. $expected = [
  1347. 'div' => ['class' => 'submit'],
  1348. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1349. '/div',
  1350. ];
  1351. $this->assertHtml($expected, $result);
  1352. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1353. $expected = [
  1354. 'div' => ['class' => 'submit'],
  1355. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1356. '/div',
  1357. ];
  1358. $this->assertHtml($expected, $result);
  1359. $result = $this->Form->end();
  1360. $tokenDebug = urlencode(json_encode([
  1361. '/articles/add',
  1362. [
  1363. 'Address.title',
  1364. 'Address.first_name',
  1365. ],
  1366. ['save', 'cancel']
  1367. ]));
  1368. $expected = [
  1369. 'div' => ['style' => 'display:none;'],
  1370. ['input' => [
  1371. 'type' => 'hidden',
  1372. 'name' => '_Token[fields]',
  1373. 'autocomplete',
  1374. 'value'
  1375. ]],
  1376. ['input' => [
  1377. 'type' => 'hidden',
  1378. 'name' => '_Token[unlocked]',
  1379. 'value' => 'cancel%7Csave',
  1380. 'autocomplete' => 'off',
  1381. ]],
  1382. ['input' => [
  1383. 'type' => 'hidden',
  1384. 'name' => '_Token[debug]',
  1385. 'value' => $tokenDebug,
  1386. 'autocomplete' => 'off',
  1387. ]],
  1388. '/div'
  1389. ];
  1390. $this->assertHtml($expected, $result);
  1391. }
  1392. /**
  1393. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1394. *
  1395. * @return void
  1396. */
  1397. public function testSecurityButtonNestedNamed()
  1398. {
  1399. $key = 'testKey';
  1400. $this->Form->request->params['_csrfToken'] = $key;
  1401. $this->Form->create('Addresses');
  1402. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1403. $result = $this->Form->unlockField();
  1404. $this->assertEquals(['Address.button'], $result);
  1405. }
  1406. /**
  1407. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1408. *
  1409. * @return void
  1410. */
  1411. public function testSecuritySubmitNestedNamed()
  1412. {
  1413. $this->Form->request->params['_Token'] = 'testKey';
  1414. $this->Form->create($this->article);
  1415. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1416. $result = $this->Form->unlockField();
  1417. $this->assertEquals(['Address.button'], $result);
  1418. }
  1419. /**
  1420. * Test that the correct fields are unlocked for image submits with no names.
  1421. *
  1422. * @return void
  1423. */
  1424. public function testSecuritySubmitImageNoName()
  1425. {
  1426. $this->Form->request->params['_Token'] = 'testKey';
  1427. $this->Form->create(false);
  1428. $result = $this->Form->submit('save.png');
  1429. $expected = [
  1430. 'div' => ['class' => 'submit'],
  1431. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1432. '/div'
  1433. ];
  1434. $this->assertHtml($expected, $result);
  1435. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1436. }
  1437. /**
  1438. * Test that the correct fields are unlocked for image submits with names.
  1439. *
  1440. * @return void
  1441. */
  1442. public function testSecuritySubmitImageName()
  1443. {
  1444. $this->Form->request->params['_Token'] = 'testKey';
  1445. $this->Form->create(null);
  1446. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1447. $expected = [
  1448. 'div' => ['class' => 'submit'],
  1449. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1450. '/div'
  1451. ];
  1452. $this->assertHtml($expected, $result);
  1453. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1454. }
  1455. /**
  1456. * testFormSecurityMultipleControlFields method
  1457. *
  1458. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1459. *
  1460. * @return void
  1461. */
  1462. public function testFormSecurityMultipleControlFields()
  1463. {
  1464. $this->Form->request->params['_Token'] = 'testKey';
  1465. $this->Form->create();
  1466. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1467. $this->Form->control('Addresses.0.title');
  1468. $this->Form->control('Addresses.0.first_name');
  1469. $this->Form->control('Addresses.0.last_name');
  1470. $this->Form->control('Addresses.0.address');
  1471. $this->Form->control('Addresses.0.city');
  1472. $this->Form->control('Addresses.0.phone');
  1473. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1474. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1475. $this->Form->control('Addresses.1.title');
  1476. $this->Form->control('Addresses.1.first_name');
  1477. $this->Form->control('Addresses.1.last_name');
  1478. $this->Form->control('Addresses.1.address');
  1479. $this->Form->control('Addresses.1.city');
  1480. $this->Form->control('Addresses.1.phone');
  1481. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1482. $result = $this->Form->secure($this->Form->fields);
  1483. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1484. $tokenDebug = urlencode(json_encode([
  1485. '/articles/add',
  1486. [
  1487. 'Addresses.0.id' => '123456',
  1488. 'Addresses.0.title',
  1489. 'Addresses.0.first_name',
  1490. 'Addresses.0.last_name',
  1491. 'Addresses.0.address',
  1492. 'Addresses.0.city',
  1493. 'Addresses.0.phone',
  1494. 'Addresses.0.primary',
  1495. 'Addresses.1.id' => '654321',
  1496. 'Addresses.1.title',
  1497. 'Addresses.1.first_name',
  1498. 'Addresses.1.last_name',
  1499. 'Addresses.1.address',
  1500. 'Addresses.1.city',
  1501. 'Addresses.1.phone',
  1502. 'Addresses.1.primary',
  1503. ],
  1504. []
  1505. ]));
  1506. $expected = [
  1507. 'div' => ['style' => 'display:none;'],
  1508. ['input' => [
  1509. 'type' => 'hidden',
  1510. 'name' => '_Token[fields]',
  1511. 'value' => $hash,
  1512. 'autocomplete' => 'off',
  1513. ]],
  1514. ['input' => [
  1515. 'type' => 'hidden',
  1516. 'name' => '_Token[unlocked]',
  1517. 'autocomplete' => 'off',
  1518. 'value' => ''
  1519. ]],
  1520. ['input' => [
  1521. 'type' => 'hidden',
  1522. 'name' => '_Token[debug]',
  1523. 'value' => $tokenDebug,
  1524. 'autocomplete' => 'off',
  1525. ]],
  1526. '/div'
  1527. ];
  1528. $this->assertHtml($expected, $result);
  1529. }
  1530. /**
  1531. * testFormSecurityArrayFields method
  1532. *
  1533. * Test form security with Model.field.0 style inputs.
  1534. *
  1535. * @return void
  1536. */
  1537. public function testFormSecurityArrayFields()
  1538. {
  1539. $this->Form->request->params['_Token'] = 'testKey';
  1540. $this->Form->create();
  1541. $this->Form->text('Address.primary.1');
  1542. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1543. $this->Form->text('Address.secondary.1.0');
  1544. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1545. }
  1546. /**
  1547. * testFormSecurityMultipleControlDisabledFields method
  1548. *
  1549. * Test secure form generation with multiple records and disabled fields.
  1550. *
  1551. * @return void
  1552. */
  1553. public function testFormSecurityMultipleControlDisabledFields()
  1554. {
  1555. $this->Form->request->params['_Token'] = [
  1556. 'unlockedFields' => ['first_name', 'address']
  1557. ];
  1558. $this->Form->create();
  1559. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1560. $this->Form->text('Addresses.0.title');
  1561. $this->Form->text('Addresses.0.first_name');
  1562. $this->Form->text('Addresses.0.last_name');
  1563. $this->Form->text('Addresses.0.address');
  1564. $this->Form->text('Addresses.0.city');
  1565. $this->Form->text('Addresses.0.phone');
  1566. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1567. $this->Form->text('Addresses.1.title');
  1568. $this->Form->text('Addresses.1.first_name');
  1569. $this->Form->text('Addresses.1.last_name');
  1570. $this->Form->text('Addresses.1.address');
  1571. $this->Form->text('Addresses.1.city');
  1572. $this->Form->text('Addresses.1.phone');
  1573. $result = $this->Form->secure($this->Form->fields);
  1574. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1575. $tokenDebug = urlencode(json_encode([
  1576. '/articles/add',
  1577. [
  1578. 'Addresses.0.id' => '123456',
  1579. 'Addresses.0.title',
  1580. 'Addresses.0.last_name',
  1581. 'Addresses.0.city',
  1582. 'Addresses.0.phone',
  1583. 'Addresses.1.id' => '654321',
  1584. 'Addresses.1.title',
  1585. 'Addresses.1.last_name',
  1586. 'Addresses.1.city',
  1587. 'Addresses.1.phone'
  1588. ],
  1589. [
  1590. 'first_name',
  1591. 'address'
  1592. ]
  1593. ]));
  1594. $expected = [
  1595. 'div' => ['style' => 'display:none;'],
  1596. ['input' => [
  1597. 'type' => 'hidden',
  1598. 'name' => '_Token[fields]',
  1599. 'autocomplete' => 'off',
  1600. 'value' => $hash
  1601. ]],
  1602. ['input' => [
  1603. 'type' => 'hidden',
  1604. 'name' => '_Token[unlocked]',
  1605. 'autocomplete' => 'off',
  1606. 'value' => 'address%7Cfirst_name',
  1607. ]],
  1608. ['input' => [
  1609. 'type' => 'hidden',
  1610. 'name' => '_Token[debug]',
  1611. 'autocomplete' => 'off',
  1612. 'value' => $tokenDebug,
  1613. ]],
  1614. '/div'
  1615. ];
  1616. $this->assertHtml($expected, $result);
  1617. }
  1618. /**
  1619. * testFormSecurityControlDisabledFields method
  1620. *
  1621. * Test single record form with disabled fields.
  1622. *
  1623. * @return void
  1624. */
  1625. public function testFormSecurityControlUnlockedFields()
  1626. {
  1627. $this->Form->request->params['_Token'] = [
  1628. 'unlockedFields' => ['first_name', 'address']
  1629. ];
  1630. $this->Form->create();
  1631. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1632. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1633. $this->Form->text('Addresses.title');
  1634. $this->Form->text('Addresses.first_name');
  1635. $this->Form->text('Addresses.last_name');
  1636. $this->Form->text('Addresses.address');
  1637. $this->Form->text('Addresses.city');
  1638. $this->Form->text('Addresses.phone');
  1639. $result = $this->Form->fields;
  1640. $expected = [
  1641. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1642. 'Addresses.city', 'Addresses.phone'
  1643. ];
  1644. $this->assertEquals($expected, $result);
  1645. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1646. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1647. $tokenDebug = urlencode(json_encode([
  1648. '/articles/add',
  1649. [
  1650. 'Addresses.id' => '123456',
  1651. 'Addresses.title',
  1652. 'Addresses.last_name',
  1653. 'Addresses.city',
  1654. 'Addresses.phone'
  1655. ],
  1656. [
  1657. 'first_name',
  1658. 'address'
  1659. ]
  1660. ]));
  1661. $expected = [
  1662. 'div' => ['style' => 'display:none;'],
  1663. ['input' => [
  1664. 'type' => 'hidden',
  1665. 'name' => '_Token[fields]',
  1666. 'value' => $hash,
  1667. 'autocomplete' => 'off',
  1668. 'data-foo' => 'bar',
  1669. ]],
  1670. ['input' => [
  1671. 'type' => 'hidden',
  1672. 'name' => '_Token[unlocked]',
  1673. 'value' => 'address%7Cfirst_name',
  1674. 'autocomplete' => 'off',
  1675. 'data-foo' => 'bar',
  1676. ]],
  1677. ['input' => [
  1678. 'type' => 'hidden', 'name' => '_Token[debug]',
  1679. 'value' => $tokenDebug,
  1680. 'autocomplete' => 'off',
  1681. 'data-foo' => 'bar'
  1682. ]],
  1683. '/div'
  1684. ];
  1685. $this->assertHtml($expected, $result);
  1686. }
  1687. /**
  1688. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1689. *
  1690. * Test single record form with debugSecurity param.
  1691. *
  1692. * @return void
  1693. */
  1694. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1695. {
  1696. $this->Form->request->params['_Token'] = [
  1697. 'unlockedFields' => ['first_name', 'address']
  1698. ];
  1699. $this->Form->create();
  1700. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1701. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1702. $this->Form->text('Addresses.title');
  1703. $this->Form->text('Addresses.first_name');
  1704. $this->Form->text('Addresses.last_name');
  1705. $this->Form->text('Addresses.address');
  1706. $this->Form->text('Addresses.city');
  1707. $this->Form->text('Addresses.phone');
  1708. $result = $this->Form->fields;
  1709. $expected = [
  1710. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1711. 'Addresses.city', 'Addresses.phone'
  1712. ];
  1713. $this->assertEquals($expected, $result);
  1714. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1715. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1716. $tokenDebug = urlencode(json_encode([
  1717. '/articles/add',
  1718. [
  1719. 'Addresses.id' => '123456',
  1720. 'Addresses.title',
  1721. 'Addresses.last_name',
  1722. 'Addresses.city',
  1723. 'Addresses.phone'
  1724. ],
  1725. [
  1726. 'first_name',
  1727. 'address'
  1728. ]
  1729. ]));
  1730. $expected = [
  1731. 'div' => ['style' => 'display:none;'],
  1732. ['input' => [
  1733. 'type' => 'hidden',
  1734. 'name' => '_Token[fields]',
  1735. 'value' => $hash,
  1736. 'autocomplete' => 'off',
  1737. 'data-foo' => 'bar',
  1738. ]],
  1739. ['input' => [
  1740. 'type' => 'hidden',
  1741. 'name' => '_Token[unlocked]',
  1742. 'value' => 'address%7Cfirst_name',
  1743. 'autocomplete' => 'off',
  1744. 'data-foo' => 'bar',
  1745. ]],
  1746. ['input' => [
  1747. 'type' => 'hidden', 'name' => '_Token[debug]',
  1748. 'value' => $tokenDebug,
  1749. 'autocomplete' => 'off',
  1750. 'data-foo' => 'bar'
  1751. ]],
  1752. '/div'
  1753. ];
  1754. $this->assertHtml($expected, $result);
  1755. }
  1756. /**
  1757. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1758. *
  1759. * Debug is false, debugSecurity is true -> no debug
  1760. *
  1761. * @return void
  1762. */
  1763. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1764. {
  1765. $this->Form->request->params['_Token'] = [
  1766. 'unlockedFields' => ['first_name', 'address']
  1767. ];
  1768. $this->Form->create();
  1769. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1770. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1771. $this->Form->text('Addresses.title');
  1772. $this->Form->text('Addresses.first_name');
  1773. $this->Form->text('Addresses.last_name');
  1774. $this->Form->text('Addresses.address');
  1775. $this->Form->text('Addresses.city');
  1776. $this->Form->text('Addresses.phone');
  1777. $result = $this->Form->fields;
  1778. $expected = [
  1779. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1780. 'Addresses.city', 'Addresses.phone'
  1781. ];
  1782. $this->assertEquals($expected, $result);
  1783. Configure::write('debug', false);
  1784. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1785. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1786. $expected = [
  1787. 'div' => ['style' => 'display:none;'],
  1788. ['input' => [
  1789. 'type' => 'hidden',
  1790. 'name' => '_Token[fields]',
  1791. 'value' => $hash,
  1792. 'autocomplete' => 'off',
  1793. 'data-foo' => 'bar',
  1794. ]],
  1795. ['input' => [
  1796. 'type' => 'hidden',
  1797. 'name' => '_Token[unlocked]',
  1798. 'value' => 'address%7Cfirst_name',
  1799. 'autocomplete' => 'off',
  1800. 'data-foo' => 'bar',
  1801. ]],
  1802. '/div'
  1803. ];
  1804. $this->assertHtml($expected, $result);
  1805. }
  1806. /**
  1807. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1808. *
  1809. * Test single record form with debugSecurity param.
  1810. *
  1811. * @return void
  1812. */
  1813. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1814. {
  1815. $this->Form->request->params['_Token'] = [
  1816. 'unlockedFields' => ['first_name', 'address']
  1817. ];
  1818. $this->Form->create();
  1819. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1820. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1821. $this->Form->text('Addresses.title');
  1822. $this->Form->text('Addresses.first_name');
  1823. $this->Form->text('Addresses.last_name');
  1824. $this->Form->text('Addresses.address');
  1825. $this->Form->text('Addresses.city');
  1826. $this->Form->text('Addresses.phone');
  1827. $result = $this->Form->fields;
  1828. $expected = [
  1829. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1830. 'Addresses.city', 'Addresses.phone'
  1831. ];
  1832. $this->assertEquals($expected, $result);
  1833. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1834. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1835. $expected = [
  1836. 'div' => ['style' => 'display:none;'],
  1837. ['input' => [
  1838. 'type' => 'hidden',
  1839. 'name' => '_Token[fields]',
  1840. 'value' => $hash,
  1841. 'autocomplete' => 'off',
  1842. 'data-foo' => 'bar',
  1843. ]],
  1844. ['input' => [
  1845. 'type' => 'hidden',
  1846. 'name' => '_Token[unlocked]',
  1847. 'value' => 'address%7Cfirst_name',
  1848. 'autocomplete' => 'off',
  1849. 'data-foo' => 'bar',
  1850. ]],
  1851. '/div'
  1852. ];
  1853. $this->assertHtml($expected, $result);
  1854. }
  1855. /**
  1856. * testFormSecureWithCustomNameAttribute method
  1857. *
  1858. * Test securing inputs with custom name attributes.
  1859. *
  1860. * @return void
  1861. */
  1862. public function testFormSecureWithCustomNameAttribute()
  1863. {
  1864. $this->Form->request->params['_Token'] = 'testKey';
  1865. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1866. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1867. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1868. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1869. }
  1870. /**
  1871. * testFormSecuredControl method
  1872. *
  1873. * Test generation of entire secure form, assertions made on control() output.
  1874. *
  1875. * @return void
  1876. */
  1877. public function testFormSecuredControl()
  1878. {
  1879. $this->Form->request->params['_csrfToken'] = 'testKey';
  1880. $this->Form->request->params['_Token'] = 'stuff';
  1881. $this->article['schema'] = [
  1882. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1883. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1884. ];
  1885. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1886. $encoding = strtolower(Configure::read('App.encoding'));
  1887. $expected = [
  1888. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1889. 'div' => ['style' => 'display:none;'],
  1890. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1891. ['input' => [
  1892. 'type' => 'hidden',
  1893. 'name' => '_csrfToken',
  1894. 'value' => 'testKey',
  1895. 'autocomplete' => 'off'
  1896. ]],
  1897. '/div'
  1898. ];
  1899. $this->assertHtml($expected, $result);
  1900. $result = $this->Form->control('ratio');
  1901. $expected = [
  1902. 'div' => ['class'],
  1903. 'label' => ['for'],
  1904. 'Ratio',
  1905. '/label',
  1906. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1907. '/div',
  1908. ];
  1909. $this->assertHtml($expected, $result);
  1910. $result = $this->Form->control('population');
  1911. $expected = [
  1912. 'div' => ['class'],
  1913. 'label' => ['for'],
  1914. 'Population',
  1915. '/label',
  1916. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1917. '/div',
  1918. ];
  1919. $this->assertHtml($expected, $result);
  1920. $result = $this->Form->control('published', ['type' => 'text']);
  1921. $expected = [
  1922. 'div' => ['class' => 'input text'],
  1923. 'label' => ['for' => 'published'],
  1924. 'Published',
  1925. '/label',
  1926. ['input' => [
  1927. 'type' => 'text',
  1928. 'name' => 'published',
  1929. 'id' => 'published'
  1930. ]],
  1931. '/div'
  1932. ];
  1933. $this->assertHtml($expected, $result);
  1934. $result = $this->Form->control('other', ['type' => 'text']);
  1935. $expected = [
  1936. 'div' => ['class' => 'input text'],
  1937. 'label' => ['for' => 'other'],
  1938. 'Other',
  1939. '/label',
  1940. ['input' => [
  1941. 'type' => 'text',
  1942. 'name' => 'other',
  1943. 'id',
  1944. ]],
  1945. '/div'
  1946. ];
  1947. $this->assertHtml($expected, $result);
  1948. $result = $this->Form->hidden('stuff');
  1949. $expected = [
  1950. 'input' => [
  1951. 'type' => 'hidden',
  1952. 'name' => 'stuff'
  1953. ]
  1954. ];
  1955. $this->assertHtml($expected, $result);
  1956. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1957. $expected = ['input' => [
  1958. 'type' => 'hidden',
  1959. 'name' => 'hidden',
  1960. 'value' => '0'
  1961. ]];
  1962. $this->assertHtml($expected, $result);
  1963. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1964. $expected = [
  1965. 'div' => ['class' => 'input checkbox'],
  1966. ['input' => [
  1967. 'type' => 'hidden',
  1968. 'name' => 'something',
  1969. 'value' => '0'
  1970. ]],
  1971. 'label' => ['for' => 'something'],
  1972. ['input' => [
  1973. 'type' => 'checkbox',
  1974. 'name' => 'something',
  1975. 'value' => '1',
  1976. 'id' => 'something'
  1977. ]],
  1978. 'Something',
  1979. '/label',
  1980. '/div'
  1981. ];
  1982. $this->assertHtml($expected, $result);
  1983. $result = $this->Form->fields;
  1984. $expectedFields = [
  1985. 'ratio',
  1986. 'population',
  1987. 'published',
  1988. 'other',
  1989. 'stuff' => '',
  1990. 'hidden' => '0',
  1991. 'something'
  1992. ];
  1993. $this->assertEquals($expectedFields, $result);
  1994. $result = $this->Form->secure($this->Form->fields);
  1995. $tokenDebug = urlencode(json_encode([
  1996. '/articles/add',
  1997. $expectedFields,
  1998. []
  1999. ]));
  2000. $expected = [
  2001. 'div' => ['style' => 'display:none;'],
  2002. ['input' => [
  2003. 'type' => 'hidden',
  2004. 'name' => '_Token[fields]',
  2005. 'value',
  2006. 'autocomplete'
  2007. ]],
  2008. ['input' => [
  2009. 'type' => 'hidden',
  2010. 'name' => '_Token[unlocked]',
  2011. 'value' => '',
  2012. 'autocomplete' => 'off'
  2013. ]],
  2014. ['input' => [
  2015. 'type' => 'hidden', 'name' => '_Token[debug]',
  2016. 'value' => $tokenDebug,
  2017. 'autocomplete' => 'off'
  2018. ]],
  2019. '/div'
  2020. ];
  2021. $this->assertHtml($expected, $result);
  2022. }
  2023. /**
  2024. * testSecuredControlCustomName method
  2025. *
  2026. * Test secured inputs with custom names.
  2027. *
  2028. * @return void
  2029. */
  2030. public function testSecuredControlCustomName()
  2031. {
  2032. $this->Form->request->params['_Token'] = 'testKey';
  2033. $this->assertEquals([], $this->Form->fields);
  2034. $this->Form->text('text_input', [
  2035. 'name' => 'Option[General.default_role]',
  2036. ]);
  2037. $expected = ['Option.General.default_role'];
  2038. $this->assertEquals($expected, $this->Form->fields);
  2039. $this->Form->select('select_box', [1, 2], [
  2040. 'name' => 'Option[General.select_role]',
  2041. ]);
  2042. $expected[] = 'Option.General.select_role';
  2043. $this->assertEquals($expected, $this->Form->fields);
  2044. $this->Form->text('other.things[]');
  2045. $expected[] = 'other.things';
  2046. $this->assertEquals($expected, $this->Form->fields);
  2047. }
  2048. /**
  2049. * testSecuredControlDuplicate method
  2050. *
  2051. * Test that a hidden field followed by a visible field
  2052. * undoes the hidden field locking.
  2053. *
  2054. * @return void
  2055. */
  2056. public function testSecuredControlDuplicate()
  2057. {
  2058. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2059. $this->assertEquals([], $this->Form->fields);
  2060. $this->Form->control('text_val', [
  2061. 'type' => 'hidden',
  2062. 'value' => 'some text',
  2063. ]);
  2064. $expected = ['text_val' => 'some text'];
  2065. $this->assertEquals($expected, $this->Form->fields);
  2066. $this->Form->control('text_val', [
  2067. 'type' => 'text',
  2068. ]);
  2069. $expected = ['text_val'];
  2070. $this->assertEquals($expected, $this->Form->fields);
  2071. }
  2072. /**
  2073. * testFormSecuredFileControl method
  2074. *
  2075. * Tests that the correct keys are added to the field hash index.
  2076. *
  2077. * @return void
  2078. */
  2079. public function testFormSecuredFileControl()
  2080. {
  2081. $this->assertEquals([], $this->Form->fields);
  2082. $this->Form->file('Attachment.file');
  2083. $expected = [
  2084. 'Attachment.file.name', 'Attachment.file.type',
  2085. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2086. 'Attachment.file.size'
  2087. ];
  2088. $this->assertEquals($expected, $this->Form->fields);
  2089. }
  2090. /**
  2091. * testFormSecuredMultipleSelect method
  2092. *
  2093. * Test that multiple selects keys are added to field hash.
  2094. *
  2095. * @return void
  2096. */
  2097. public function testFormSecuredMultipleSelect()
  2098. {
  2099. $this->Form->request->params['_csrfToken'] = 'testKey';
  2100. $this->assertEquals([], $this->Form->fields);
  2101. $options = ['1' => 'one', '2' => 'two'];
  2102. $this->Form->select('Model.select', $options);
  2103. $expected = ['Model.select'];
  2104. $this->assertEquals($expected, $this->Form->fields);
  2105. $this->Form->fields = [];
  2106. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2107. $this->assertEquals($expected, $this->Form->fields);
  2108. }
  2109. /**
  2110. * testFormSecuredRadio method
  2111. *
  2112. * @return void
  2113. */
  2114. public function testFormSecuredRadio()
  2115. {
  2116. $this->Form->request->params['_Token'] = 'testKey';
  2117. $this->assertEquals([], $this->Form->fields);
  2118. $options = ['1' => 'option1', '2' => 'option2'];
  2119. $this->Form->radio('Test.test', $options);
  2120. $expected = ['Test.test'];
  2121. $this->assertEquals($expected, $this->Form->fields);
  2122. $this->Form->radio('Test.all', $options, [
  2123. 'disabled' => ['option1', 'option2']
  2124. ]);
  2125. $expected = ['Test.test', 'Test.all' => ''];
  2126. $this->assertEquals($expected, $this->Form->fields);
  2127. $this->Form->radio('Test.some', $options, [
  2128. 'disabled' => ['option1']
  2129. ]);
  2130. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2131. $this->assertEquals($expected, $this->Form->fields);
  2132. }
  2133. /**
  2134. * testFormSecuredAndDisabledNotAssoc method
  2135. *
  2136. * Test that when disabled is in a list based attribute array it works.
  2137. *
  2138. * @return void
  2139. */
  2140. public function testFormSecuredAndDisabledNotAssoc()
  2141. {
  2142. $this->Form->request->params['_Token'] = 'testKey';
  2143. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2144. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2145. $this->Form->text('Model.text', ['disabled']);
  2146. $this->Form->textarea('Model.textarea', ['disabled']);
  2147. $this->Form->password('Model.password', ['disabled']);
  2148. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2149. $expected = [
  2150. 'Model.radio' => ''
  2151. ];
  2152. $this->assertEquals($expected, $this->Form->fields);
  2153. }
  2154. /**
  2155. * testFormSecuredAndDisabled method
  2156. *
  2157. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2158. * hashing.
  2159. *
  2160. * @return void
  2161. */
  2162. public function testFormSecuredAndDisabled()
  2163. {
  2164. $this->Form->request->params['_Token'] = 'testKey';
  2165. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2166. $this->Form->text('Model.text', ['disabled' => true]);
  2167. $this->Form->password('Model.text', ['disabled' => true]);
  2168. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2169. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2170. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2171. $this->Form->year('Model.year', ['disabled' => true]);
  2172. $this->Form->month('Model.month', ['disabled' => true]);
  2173. $this->Form->day('Model.day', ['disabled' => true]);
  2174. $this->Form->hour('Model.hour', ['disabled' => true]);
  2175. $this->Form->minute('Model.minute', ['disabled' => true]);
  2176. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2177. $expected = [
  2178. 'Model.radio' => ''
  2179. ];
  2180. $this->assertEquals($expected, $this->Form->fields);
  2181. }
  2182. /**
  2183. * testDisableSecurityUsingForm method
  2184. *
  2185. * @return void
  2186. */
  2187. public function testDisableSecurityUsingForm()
  2188. {
  2189. $this->Form->request->params['_Token'] = [
  2190. 'disabledFields' => []
  2191. ];
  2192. $this->Form->create();
  2193. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2194. $this->Form->text('Addresses.title');
  2195. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2196. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2197. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2198. $result = $this->Form->fields;
  2199. $expected = [
  2200. 'Addresses.id' => '123456', 'Addresses.title',
  2201. ];
  2202. $this->assertEquals($expected, $result);
  2203. }
  2204. /**
  2205. * testUnlockFieldAddsToList method
  2206. *
  2207. * Test disableField.
  2208. *
  2209. * @return void
  2210. */
  2211. public function testUnlockFieldAddsToList()
  2212. {
  2213. $this->Form->request->params['_Token'] = [
  2214. 'unlockedFields' => []
  2215. ];
  2216. $this->Form->unlockField('Contact.name');
  2217. $this->Form->text('Contact.name');
  2218. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2219. $this->assertEquals([], $this->Form->fields);
  2220. }
  2221. /**
  2222. * testUnlockFieldRemovingFromFields method
  2223. *
  2224. * Test unlockField removing from fields array.
  2225. *
  2226. * @return void
  2227. */
  2228. public function testUnlockFieldRemovingFromFields()
  2229. {
  2230. $this->Form->request->params['_Token'] = [
  2231. 'unlockedFields' => []
  2232. ];
  2233. $this->Form->create($this->article);
  2234. $this->Form->hidden('Article.id', ['value' => 1]);
  2235. $this->Form->text('Article.title');
  2236. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2237. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  2238. $this->Form->unlockField('Article.title');
  2239. $this->Form->unlockField('Article.id');
  2240. $this->assertEquals([], $this->Form->fields);
  2241. }
  2242. /**
  2243. * testResetUnlockFields method
  2244. *
  2245. * Test reset unlockFields, when create new form.
  2246. *
  2247. * @return void
  2248. */
  2249. public function testResetUnlockFields()
  2250. {
  2251. $this->Form->request->params['_Token'] = [
  2252. 'key' => 'testKey',
  2253. 'unlockedFields' => []
  2254. ];
  2255. $this->Form->unlockField('Contact.id');
  2256. $this->Form->create('Contact');
  2257. $this->Form->hidden('Contact.id', ['value' => 1]);
  2258. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2259. $this->Form->end();
  2260. $this->Form->create('Contact');
  2261. $this->Form->hidden('Contact.id', ['value' => 1]);
  2262. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2263. }
  2264. /**
  2265. * testSecuredFormUrlIgnoresHost method
  2266. *
  2267. * Test that only the path + query elements of a form's URL show up in their hash.
  2268. *
  2269. * @return void
  2270. */
  2271. public function testSecuredFormUrlIgnoresHost()
  2272. {
  2273. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2274. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  2275. $this->Form->create($this->article, [
  2276. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2277. ]);
  2278. $result = $this->Form->secure();
  2279. $this->assertContains($expected, $result);
  2280. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2281. $result = $this->Form->secure();
  2282. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2283. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2284. $result = $this->Form->secure();
  2285. $this->assertContains($expected, $result, 'URL path + query should work.');
  2286. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2287. $result = $this->Form->secure();
  2288. $this->assertNotContains($expected, $result, 'URL is different');
  2289. }
  2290. /**
  2291. * testSecuredFormUrlHasHtmlAndIdentifier method
  2292. *
  2293. * Test that URL, HTML and identifier show up in their hashes.
  2294. *
  2295. * @return void
  2296. */
  2297. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2298. {
  2299. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2300. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  2301. $this->Form->create($this->article, [
  2302. 'url' => [
  2303. 'controller' => 'articles',
  2304. 'action' => 'view',
  2305. '?' => [
  2306. 'page' => 1,
  2307. 'limit' => 10,
  2308. 'html' => '<>"',
  2309. ],
  2310. '#' => 'result',
  2311. ],
  2312. ]);
  2313. $result = $this->Form->secure();
  2314. $this->assertContains($expected, $result);
  2315. $this->Form->create($this->article, [
  2316. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2317. ]);
  2318. $result = $this->Form->secure();
  2319. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2320. $this->Form->create($this->article, [
  2321. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2322. ]);
  2323. $result = $this->Form->secure();
  2324. $this->assertContains($expected, $result, 'URL path + query should work.');
  2325. }
  2326. /**
  2327. * testErrorMessageDisplay method
  2328. *
  2329. * Test error message display.
  2330. *
  2331. * @return void
  2332. */
  2333. public function testErrorMessageDisplay()
  2334. {
  2335. $this->article['errors'] = [
  2336. 'Article' => [
  2337. 'title' => 'error message',
  2338. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2339. ]
  2340. ];
  2341. $this->Form->create($this->article);
  2342. $result = $this->Form->control('Article.title');
  2343. $expected = [
  2344. 'div' => ['class' => 'input text error'],
  2345. 'label' => ['for' => 'article-title'],
  2346. 'Title',
  2347. '/label',
  2348. 'input' => [
  2349. 'type' => 'text', 'name' => 'Article[title]',
  2350. 'id' => 'article-title', 'class' => 'form-error'
  2351. ],
  2352. ['div' => ['class' => 'error-message']],
  2353. 'error message',
  2354. '/div',
  2355. '/div'
  2356. ];
  2357. $this->assertHtml($expected, $result);
  2358. $result = $this->Form->control('Article.title', [
  2359. 'templates' => [
  2360. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2361. ]
  2362. ]);
  2363. $expected = [
  2364. 'div' => ['class' => 'input text error'],
  2365. 'label' => ['for' => 'article-title'],
  2366. 'Title',
  2367. '/label',
  2368. 'input' => [
  2369. 'type' => 'text', 'name' => 'Article[title]',
  2370. 'id' => 'article-title', 'class' => 'form-error'
  2371. ],
  2372. '/div'
  2373. ];
  2374. $this->assertHtml($expected, $result);
  2375. $result = $this->Form->control('Article.content');
  2376. $expected = [
  2377. 'div' => ['class' => 'input text error'],
  2378. 'label' => ['for' => 'article-content'],
  2379. 'Content',
  2380. '/label',
  2381. 'input' => [
  2382. 'type' => 'text', 'name' => 'Article[content]',
  2383. 'id' => 'article-content', 'class' => 'form-error'
  2384. ],
  2385. ['div' => ['class' => 'error-message']],
  2386. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2387. '/div',
  2388. '/div'
  2389. ];
  2390. $this->assertHtml($expected, $result);
  2391. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2392. $expected = [
  2393. 'div' => ['class' => 'input text error'],
  2394. 'label' => ['for' => 'article-content'],
  2395. 'Content',
  2396. '/label',
  2397. 'input' => [
  2398. 'type' => 'text', 'name' => 'Article[content]',
  2399. 'id' => 'article-content', 'class' => 'form-error'
  2400. ],
  2401. ['div' => ['class' => 'error-message']],
  2402. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2403. '/div',
  2404. '/div'
  2405. ];
  2406. $this->assertHtml($expected, $result);
  2407. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2408. $expected = [
  2409. 'div' => ['class' => 'input text error'],
  2410. 'label' => ['for' => 'article-content'],
  2411. 'Content',
  2412. '/label',
  2413. 'input' => [
  2414. 'type' => 'text', 'name' => 'Article[content]',
  2415. 'id' => 'article-content', 'class' => 'form-error'
  2416. ],
  2417. ['div' => ['class' => 'error-message']],
  2418. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2419. '/div',
  2420. '/div'
  2421. ];
  2422. $this->assertHtml($expected, $result);
  2423. }
  2424. /**
  2425. * testEmptyErrorValidation method
  2426. *
  2427. * Test validation errors, when validation message is an empty string.
  2428. *
  2429. * @return void
  2430. */
  2431. public function testEmptyErrorValidation()
  2432. {
  2433. $this->article['errors'] = [
  2434. 'Article' => ['title' => '']
  2435. ];
  2436. $this->Form->create($this->article);
  2437. $result = $this->Form->control('Article.title');
  2438. $expected = [
  2439. 'div' => ['class' => 'input text error'],
  2440. 'label' => ['for' => 'article-title'],
  2441. 'Title',
  2442. '/label',
  2443. 'input' => [
  2444. 'type' => 'text', 'name' => 'Article[title]',
  2445. 'id' => 'article-title', 'class' => 'form-error'
  2446. ],
  2447. ['div' => ['class' => 'error-message']],
  2448. [],
  2449. '/div',
  2450. '/div'
  2451. ];
  2452. $this->assertHtml($expected, $result);
  2453. }
  2454. /**
  2455. * testEmptyControlErrorValidation method
  2456. *
  2457. * Test validation errors, when calling control() overriding validation message by an empty string.
  2458. *
  2459. * @return void
  2460. */
  2461. public function testEmptyControlErrorValidation()
  2462. {
  2463. $this->article['errors'] = [
  2464. 'Article' => ['title' => 'error message']
  2465. ];
  2466. $this->Form->create($this->article);
  2467. $result = $this->Form->control('Article.title', ['error' => '']);
  2468. $expected = [
  2469. 'div' => ['class' => 'input text error'],
  2470. 'label' => ['for' => 'article-title'],
  2471. 'Title',
  2472. '/label',
  2473. 'input' => [
  2474. 'type' => 'text', 'name' => 'Article[title]',
  2475. 'id' => 'article-title', 'class' => 'form-error'
  2476. ],
  2477. ['div' => ['class' => 'error-message']],
  2478. [],
  2479. '/div',
  2480. '/div'
  2481. ];
  2482. $this->assertHtml($expected, $result);
  2483. }
  2484. /**
  2485. * testControlErrorMessage method
  2486. *
  2487. * Test validation errors, when calling control() overriding validation messages.
  2488. *
  2489. * @return void
  2490. */
  2491. public function testControlErrorMessage()
  2492. {
  2493. $this->article['errors'] = [
  2494. 'title' => ['error message']
  2495. ];
  2496. $this->Form->create($this->article);
  2497. $result = $this->Form->control('title', [
  2498. 'error' => 'Custom error!'
  2499. ]);
  2500. $expected = [
  2501. 'div' => ['class' => 'input text required error'],
  2502. 'label' => ['for' => 'title'],
  2503. 'Title',
  2504. '/label',
  2505. 'input' => [
  2506. 'type' => 'text', 'name' => 'title',
  2507. 'id' => 'title', 'class' => 'form-error',
  2508. 'required' => 'required',
  2509. ],
  2510. ['div' => ['class' => 'error-message']],
  2511. 'Custom error!',
  2512. '/div',
  2513. '/div'
  2514. ];
  2515. $this->assertHtml($expected, $result);
  2516. $result = $this->Form->control('title', [
  2517. 'error' => ['error message' => 'Custom error!']
  2518. ]);
  2519. $expected = [
  2520. 'div' => ['class' => 'input text required error'],
  2521. 'label' => ['for' => 'title'],
  2522. 'Title',
  2523. '/label',
  2524. 'input' => [
  2525. 'type' => 'text',
  2526. 'name' => 'title',
  2527. 'id' => 'title',
  2528. 'class' => 'form-error',
  2529. 'required' => 'required'
  2530. ],
  2531. ['div' => ['class' => 'error-message']],
  2532. 'Custom error!',
  2533. '/div',
  2534. '/div'
  2535. ];
  2536. $this->assertHtml($expected, $result);
  2537. }
  2538. /**
  2539. * testFormValidationAssociated method
  2540. *
  2541. * Tests displaying errors for nested entities.
  2542. *
  2543. * @return void
  2544. */
  2545. public function testFormValidationAssociated()
  2546. {
  2547. $nested = new Entity(['foo' => 'bar']);
  2548. $nested->errors('foo', ['not a valid bar']);
  2549. $entity = new Entity(['nested' => $nested]);
  2550. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2551. $result = $this->Form->error('nested.foo');
  2552. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2553. }
  2554. /**
  2555. * testFormValidationAssociatedSecondLevel method
  2556. *
  2557. * Test form error display with associated model.
  2558. *
  2559. * @return void
  2560. */
  2561. public function testFormValidationAssociatedSecondLevel()
  2562. {
  2563. $inner = new Entity(['bar' => 'baz']);
  2564. $nested = new Entity(['foo' => $inner]);
  2565. $entity = new Entity(['nested' => $nested]);
  2566. $inner->errors('bar', ['not a valid one']);
  2567. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2568. $result = $this->Form->error('nested.foo.bar');
  2569. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2570. }
  2571. /**
  2572. * testFormValidationMultiRecord method
  2573. *
  2574. * Test form error display with multiple records.
  2575. *
  2576. * @return void
  2577. */
  2578. public function testFormValidationMultiRecord()
  2579. {
  2580. $one = new Entity();
  2581. $two = new Entity();
  2582. TableRegistry::get('Contacts', [
  2583. 'className' => __NAMESPACE__ . '\ContactsTable'
  2584. ]);
  2585. $one->set('email', '');
  2586. $one->errors('email', ['invalid email']);
  2587. $two->set('name', '');
  2588. $two->errors('name', ['This is wrong']);
  2589. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2590. $result = $this->Form->control('0.email');
  2591. $expected = [
  2592. 'div' => ['class' => 'input email error'],
  2593. 'label' => ['for' => '0-email'],
  2594. 'Email',
  2595. '/label',
  2596. 'input' => [
  2597. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2598. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2599. ],
  2600. ['div' => ['class' => 'error-message']],
  2601. 'invalid email',
  2602. '/div',
  2603. '/div'
  2604. ];
  2605. $this->assertHtml($expected, $result);
  2606. $result = $this->Form->control('1.name');
  2607. $expected = [
  2608. 'div' => ['class' => 'input text error'],
  2609. 'label' => ['for' => '1-name'],
  2610. 'Name',
  2611. '/label',
  2612. 'input' => [
  2613. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2614. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2615. ],
  2616. ['div' => ['class' => 'error-message']],
  2617. 'This is wrong',
  2618. '/div',
  2619. '/div'
  2620. ];
  2621. $this->assertHtml($expected, $result);
  2622. }
  2623. /**
  2624. * testControl method
  2625. *
  2626. * Test various incarnations of control().
  2627. *
  2628. * @return void
  2629. */
  2630. public function testControl()
  2631. {
  2632. TableRegistry::get('ValidateUsers', [
  2633. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2634. ]);
  2635. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2636. $result = $this->Form->control('ValidateUsers.balance');
  2637. $expected = [
  2638. 'div' => ['class'],
  2639. 'label' => ['for'],
  2640. 'Balance',
  2641. '/label',
  2642. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2643. '/div',
  2644. ];
  2645. $this->assertHtml($expected, $result);
  2646. $result = $this->Form->control('ValidateUser.cost_decimal');
  2647. $expected = [
  2648. 'div' => ['class'],
  2649. 'label' => ['for'],
  2650. 'Cost Decimal',
  2651. '/label',
  2652. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2653. '/div',
  2654. ];
  2655. $this->assertHtml($expected, $result);
  2656. $result = $this->Form->control('ValidateUser.null_decimal');
  2657. $expected = [
  2658. 'div' => ['class'],
  2659. 'label' => ['for'],
  2660. 'Null Decimal',
  2661. '/label',
  2662. 'input' => ['name', 'type' => 'number', 'id'],
  2663. '/div',
  2664. ];
  2665. $this->assertHtml($expected, $result);
  2666. }
  2667. /**
  2668. * testControlCustomization method
  2669. *
  2670. * Tests the input method and passing custom options.
  2671. *
  2672. * @return void
  2673. */
  2674. public function testControlCustomization()
  2675. {
  2676. TableRegistry::get('Contacts', [
  2677. 'className' => __NAMESPACE__ . '\ContactsTable'
  2678. ]);
  2679. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2680. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2681. $expected = [
  2682. 'div' => ['class' => 'input email'],
  2683. 'label' => ['for' => 'custom'],
  2684. 'Email',
  2685. '/label',
  2686. ['input' => [
  2687. 'type' => 'email', 'name' => 'Contact[email]',
  2688. 'id' => 'custom', 'maxlength' => 255
  2689. ]],
  2690. '/div'
  2691. ];
  2692. $this->assertHtml($expected, $result);
  2693. $result = $this->Form->control('Contact.email', [
  2694. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2695. ]);
  2696. $expected = [
  2697. '<div',
  2698. 'label' => ['for' => 'contact-email'],
  2699. 'Email',
  2700. '/label',
  2701. ['input' => [
  2702. 'type' => 'email', 'name' => 'Contact[email]',
  2703. 'id' => 'contact-email', 'maxlength' => 255
  2704. ]],
  2705. '/div'
  2706. ];
  2707. $this->assertHtml($expected, $result);
  2708. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2709. $expected = [
  2710. 'div' => ['class' => 'input text'],
  2711. 'label' => ['for' => 'contact-email'],
  2712. 'Email',
  2713. '/label',
  2714. ['input' => [
  2715. 'type' => 'text', 'name' => 'Contact[email]',
  2716. 'id' => 'contact-email', 'maxlength' => '255'
  2717. ]],
  2718. '/div'
  2719. ];
  2720. $this->assertHtml($expected, $result);
  2721. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2722. $expected = [
  2723. 'div' => ['class' => 'input text'],
  2724. 'label' => ['for' => 'contact-5-email'],
  2725. 'Email',
  2726. '/label',
  2727. ['input' => [
  2728. 'type' => 'text', 'name' => 'Contact[5][email]',
  2729. 'id' => 'contact-5-email', 'maxlength' => '255'
  2730. ]],
  2731. '/div'
  2732. ];
  2733. $this->assertHtml($expected, $result);
  2734. $result = $this->Form->control('Contact.password');
  2735. $expected = [
  2736. 'div' => ['class' => 'input password'],
  2737. 'label' => ['for' => 'contact-password'],
  2738. 'Password',
  2739. '/label',
  2740. ['input' => [
  2741. 'type' => 'password', 'name' => 'Contact[password]',
  2742. 'id' => 'contact-password'
  2743. ]],
  2744. '/div'
  2745. ];
  2746. $this->assertHtml($expected, $result);
  2747. $result = $this->Form->control('Contact.email', [
  2748. 'type' => 'file', 'class' => 'textbox'
  2749. ]);
  2750. $expected = [
  2751. 'div' => ['class' => 'input file'],
  2752. 'label' => ['for' => 'contact-email'],
  2753. 'Email',
  2754. '/label',
  2755. ['input' => [
  2756. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2757. 'id' => 'contact-email'
  2758. ]],
  2759. '/div'
  2760. ];
  2761. $this->assertHtml($expected, $result);
  2762. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2763. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2764. $result = $this->Form->control('phone');
  2765. $expected = [
  2766. 'div' => ['class' => 'input tel'],
  2767. 'label' => ['for' => 'phone'],
  2768. 'Phone',
  2769. '/label',
  2770. ['input' => [
  2771. 'type' => 'tel', 'name' => 'phone',
  2772. 'value' => 'Hello &amp; World &gt; weird chars',
  2773. 'id' => 'phone', 'maxlength' => 255
  2774. ]],
  2775. '/div'
  2776. ];
  2777. $this->assertHtml($expected, $result);
  2778. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2779. $this->Form->create();
  2780. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2781. $expected = [
  2782. 'div' => ['class' => 'input text'],
  2783. 'label' => ['for' => 'myId'],
  2784. 'Field',
  2785. '/label',
  2786. 'input' => [
  2787. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2788. 'value' => 'My value', 'id' => 'myId'
  2789. ],
  2790. '/div'
  2791. ];
  2792. $this->assertHtml($expected, $result);
  2793. $this->Form->request->data = [];
  2794. $entity->errors('field', 'Badness!');
  2795. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2796. $result = $this->Form->control('field');
  2797. $expected = [
  2798. 'div' => ['class' => 'input text error'],
  2799. 'label' => ['for' => 'field'],
  2800. 'Field',
  2801. '/label',
  2802. 'input' => [
  2803. 'type' => 'text', 'name' => 'field',
  2804. 'id' => 'field', 'class' => 'form-error'
  2805. ],
  2806. ['div' => ['class' => 'error-message']],
  2807. 'Badness!',
  2808. '/div',
  2809. '/div'
  2810. ];
  2811. $this->assertHtml($expected, $result);
  2812. $result = $this->Form->control('field', [
  2813. 'templates' => [
  2814. 'inputContainerError' => '{{content}}{{error}}',
  2815. 'error' => '<span class="error-message">{{content}}</span>'
  2816. ]
  2817. ]);
  2818. $expected = [
  2819. 'label' => ['for' => 'field'],
  2820. 'Field',
  2821. '/label',
  2822. 'input' => [
  2823. 'type' => 'text', 'name' => 'field',
  2824. 'id' => 'field', 'class' => 'form-error'
  2825. ],
  2826. ['span' => ['class' => 'error-message']],
  2827. 'Badness!',
  2828. '/span'
  2829. ];
  2830. $this->assertHtml($expected, $result);
  2831. $entity->errors('field', ['minLength'], true);
  2832. $result = $this->Form->control('field', [
  2833. 'error' => [
  2834. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2835. 'maxLength' => 'login too large'
  2836. ]
  2837. ]);
  2838. $expected = [
  2839. 'div' => ['class' => 'input text error'],
  2840. 'label' => ['for' => 'field'],
  2841. 'Field',
  2842. '/label',
  2843. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2844. ['div' => ['class' => 'error-message']],
  2845. 'Le login doit contenir au moins 2 caractères',
  2846. '/div',
  2847. '/div'
  2848. ];
  2849. $this->assertHtml($expected, $result);
  2850. $entity->errors('field', ['maxLength'], true);
  2851. $result = $this->Form->control('field', [
  2852. 'error' => [
  2853. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2854. 'maxLength' => 'login too large',
  2855. ]
  2856. ]);
  2857. $expected = [
  2858. 'div' => ['class' => 'input text error'],
  2859. 'label' => ['for' => 'field'],
  2860. 'Field',
  2861. '/label',
  2862. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2863. ['div' => ['class' => 'error-message']],
  2864. 'login too large',
  2865. '/div',
  2866. '/div'
  2867. ];
  2868. $this->assertHtml($expected, $result);
  2869. }
  2870. /**
  2871. * testControlWithTemplateFile method
  2872. *
  2873. * Test that control() accepts a template file.
  2874. *
  2875. * @return void
  2876. */
  2877. public function testControlWithTemplateFile()
  2878. {
  2879. $result = $this->Form->control('field', [
  2880. 'templates' => 'htmlhelper_tags'
  2881. ]);
  2882. $expected = [
  2883. 'label' => ['for' => 'field'],
  2884. 'Field',
  2885. '/label',
  2886. 'input' => [
  2887. 'type' => 'text', 'name' => 'field',
  2888. 'id' => 'field'
  2889. ],
  2890. ];
  2891. $this->assertHtml($expected, $result);
  2892. }
  2893. /**
  2894. * testNestedControlsEndWithBrackets method
  2895. *
  2896. * Test that nested inputs end with brackets.
  2897. *
  2898. * @return void
  2899. */
  2900. public function testNestedControlsEndWithBrackets()
  2901. {
  2902. $result = $this->Form->text('nested.text[]');
  2903. $expected = [
  2904. 'input' => [
  2905. 'type' => 'text', 'name' => 'nested[text][]'
  2906. ],
  2907. ];
  2908. $this->assertHtml($expected, $result);
  2909. $result = $this->Form->file('nested.file[]');
  2910. $expected = [
  2911. 'input' => [
  2912. 'type' => 'file', 'name' => 'nested[file][]'
  2913. ],
  2914. ];
  2915. $this->assertHtml($expected, $result);
  2916. }
  2917. /**
  2918. * testCreateIdPrefix method
  2919. *
  2920. * Test id prefix.
  2921. *
  2922. * @return void
  2923. */
  2924. public function testCreateIdPrefix()
  2925. {
  2926. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2927. $result = $this->Form->control('field');
  2928. $expected = [
  2929. 'div' => ['class' => 'input text'],
  2930. 'label' => ['for' => 'prefix-field'],
  2931. 'Field',
  2932. '/label',
  2933. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2934. '/div'
  2935. ];
  2936. $this->assertHtml($expected, $result);
  2937. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2938. $expected = [
  2939. 'div' => ['class' => 'input text'],
  2940. 'label' => ['for' => 'custom-id'],
  2941. 'Field',
  2942. '/label',
  2943. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2944. '/div'
  2945. ];
  2946. $this->assertHtml($expected, $result);
  2947. $result = $this->Form->radio('Model.field', ['option A']);
  2948. $expected = [
  2949. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2950. 'label' => ['for' => 'prefix-model-field-0'],
  2951. ['input' => [
  2952. 'type' => 'radio',
  2953. 'name' => 'Model[field]',
  2954. 'value' => '0',
  2955. 'id' => 'prefix-model-field-0'
  2956. ]],
  2957. 'option A',
  2958. '/label'
  2959. ];
  2960. $this->assertHtml($expected, $result);
  2961. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2962. $expected = [
  2963. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2964. 'label' => ['for' => 'prefix-model-field-0'],
  2965. ['input' => [
  2966. 'type' => 'radio',
  2967. 'name' => 'Model[field]',
  2968. 'value' => '0',
  2969. 'id' => 'prefix-model-field-0'
  2970. ]],
  2971. 'option A',
  2972. '/label'
  2973. ];
  2974. $this->assertHtml($expected, $result);
  2975. $result = $this->Form->select(
  2976. 'Model.multi_field',
  2977. ['first'],
  2978. ['multiple' => 'checkbox']
  2979. );
  2980. $expected = [
  2981. 'input' => [
  2982. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2983. ],
  2984. ['div' => ['class' => 'checkbox']],
  2985. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2986. ['input' => [
  2987. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2988. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2989. ]],
  2990. 'first',
  2991. '/label',
  2992. '/div',
  2993. ];
  2994. $this->assertHtml($expected, $result);
  2995. $this->Form->end();
  2996. $result = $this->Form->control('field');
  2997. $expected = [
  2998. 'div' => ['class' => 'input text'],
  2999. 'label' => ['for' => 'field'],
  3000. 'Field',
  3001. '/label',
  3002. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3003. '/div'
  3004. ];
  3005. $this->assertHtml($expected, $result);
  3006. }
  3007. /**
  3008. * testControlZero method
  3009. *
  3010. * Test that inputs with 0 can be created.
  3011. *
  3012. * @return void
  3013. */
  3014. public function testControlZero()
  3015. {
  3016. TableRegistry::get('Contacts', [
  3017. 'className' => __NAMESPACE__ . '\ContactsTable'
  3018. ]);
  3019. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3020. $result = $this->Form->control('0');
  3021. $expected = [
  3022. 'div' => ['class' => 'input text'],
  3023. 'label' => ['for' => '0'], '/label',
  3024. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3025. '/div'
  3026. ];
  3027. $this->assertHtml($expected, $result);
  3028. }
  3029. /**
  3030. * testControlCheckbox method
  3031. *
  3032. * Test control() with checkbox creation.
  3033. *
  3034. * @return void
  3035. */
  3036. public function testControlCheckbox()
  3037. {
  3038. $articles = TableRegistry::get('Articles');
  3039. $articles->schema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3040. $article = $articles->newEntity();
  3041. $this->Form->create($article);
  3042. $result = $this->Form->control('Articles.active');
  3043. $expected = [
  3044. 'div' => ['class' => 'input checkbox'],
  3045. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3046. 'label' => ['for' => 'articles-active'],
  3047. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3048. 'Active',
  3049. '/label',
  3050. '/div'
  3051. ];
  3052. $this->assertHtml($expected, $result);
  3053. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3054. $expected = [
  3055. 'div' => ['class' => 'input checkbox'],
  3056. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3057. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3058. '/div'
  3059. ];
  3060. $this->assertHtml($expected, $result);
  3061. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3062. $expected = [
  3063. 'div' => ['class' => 'input checkbox'],
  3064. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3065. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3066. '/div'
  3067. ];
  3068. $this->assertHtml($expected, $result);
  3069. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3070. $expected = [
  3071. 'div' => ['class' => 'input checkbox'],
  3072. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3073. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3074. '/div'
  3075. ];
  3076. $this->assertHtml($expected, $result);
  3077. $result = $this->Form->control('Articles.disabled', [
  3078. 'label' => 'Disabled',
  3079. 'type' => 'checkbox',
  3080. 'data-foo' => 'disabled'
  3081. ]);
  3082. $expected = [
  3083. 'div' => ['class' => 'input checkbox'],
  3084. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3085. 'label' => ['for' => 'articles-disabled'],
  3086. ['input' => [
  3087. 'type' => 'checkbox',
  3088. 'name' => 'Articles[disabled]',
  3089. 'value' => '1',
  3090. 'id' => 'articles-disabled',
  3091. 'data-foo' => 'disabled'
  3092. ]],
  3093. 'Disabled',
  3094. '/label',
  3095. '/div'
  3096. ];
  3097. $this->assertHtml($expected, $result);
  3098. $result = $this->Form->control('Articles.confirm', [
  3099. 'label' => 'Confirm <b>me</b>!',
  3100. 'type' => 'checkbox',
  3101. 'escape' => false
  3102. ]);
  3103. $expected = [
  3104. 'div' => ['class' => 'input checkbox'],
  3105. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3106. 'label' => ['for' => 'articles-confirm'],
  3107. ['input' => [
  3108. 'type' => 'checkbox',
  3109. 'name' => 'Articles[confirm]',
  3110. 'value' => '1',
  3111. 'id' => 'articles-confirm',
  3112. ]],
  3113. 'Confirm <b>me</b>!',
  3114. '/label',
  3115. '/div'
  3116. ];
  3117. $this->assertHtml($expected, $result);
  3118. }
  3119. /**
  3120. * testControlHidden method
  3121. *
  3122. * Test that control() does not create wrapping div and label tag for hidden fields.
  3123. *
  3124. * @return void
  3125. */
  3126. public function testControlHidden()
  3127. {
  3128. TableRegistry::get('ValidateUsers', [
  3129. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3130. ]);
  3131. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3132. $result = $this->Form->control('ValidateUser.id');
  3133. $expected = [
  3134. 'input' => ['name', 'type' => 'hidden', 'id']
  3135. ];
  3136. $this->assertHtml($expected, $result);
  3137. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3138. $expected = [
  3139. 'input' => ['name', 'type' => 'hidden', 'id']
  3140. ];
  3141. $this->assertHtml($expected, $result);
  3142. }
  3143. /**
  3144. * testControlDatetime method
  3145. *
  3146. * Test form->control() with datetime.
  3147. *
  3148. * @return void
  3149. */
  3150. public function testControlDatetime()
  3151. {
  3152. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3153. ->setMethods(['datetime'])
  3154. ->setConstructorArgs([new View()])
  3155. ->getMock();
  3156. $this->Form->expects($this->once())->method('datetime')
  3157. ->with('prueba', [
  3158. 'type' => 'datetime',
  3159. 'timeFormat' => 24,
  3160. 'minYear' => 2008,
  3161. 'maxYear' => 2011,
  3162. 'interval' => 15,
  3163. 'options' => null,
  3164. 'empty' => false,
  3165. 'id' => 'prueba',
  3166. 'required' => false,
  3167. 'templateVars' => []
  3168. ])
  3169. ->will($this->returnValue('This is it!'));
  3170. $result = $this->Form->control('prueba', [
  3171. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3172. 'maxYear' => 2011, 'interval' => 15
  3173. ]);
  3174. $expected = [
  3175. 'div' => ['class' => 'input datetime'],
  3176. '<label',
  3177. 'Prueba',
  3178. '/label',
  3179. 'This is it!',
  3180. '/div'
  3181. ];
  3182. $this->assertHtml($expected, $result);
  3183. }
  3184. /**
  3185. * testControlDatetimeIdPrefix method
  3186. *
  3187. * Test form->control() with datetime with id prefix.
  3188. *
  3189. * @return void
  3190. */
  3191. public function testControlDatetimeIdPrefix()
  3192. {
  3193. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3194. ->setMethods(['datetime'])
  3195. ->setConstructorArgs([new View()])
  3196. ->getMock();
  3197. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3198. $this->Form->expects($this->once())->method('datetime')
  3199. ->with('prueba', [
  3200. 'type' => 'datetime',
  3201. 'timeFormat' => 24,
  3202. 'minYear' => 2008,
  3203. 'maxYear' => 2011,
  3204. 'interval' => 15,
  3205. 'options' => null,
  3206. 'empty' => false,
  3207. 'id' => 'prefix-prueba',
  3208. 'required' => false,
  3209. 'templateVars' => []
  3210. ])
  3211. ->will($this->returnValue('This is it!'));
  3212. $result = $this->Form->control('prueba', [
  3213. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3214. 'maxYear' => 2011, 'interval' => 15
  3215. ]);
  3216. $expected = [
  3217. 'div' => ['class' => 'input datetime'],
  3218. '<label',
  3219. 'Prueba',
  3220. '/label',
  3221. 'This is it!',
  3222. '/div'
  3223. ];
  3224. $this->assertHtml($expected, $result);
  3225. }
  3226. /**
  3227. * testControlCheckboxWithDisabledElements method
  3228. *
  3229. * Test generating checkboxes with disabled elements.
  3230. *
  3231. * @return void
  3232. */
  3233. public function testControlCheckboxWithDisabledElements()
  3234. {
  3235. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3236. $result = $this->Form->control('Contact.multiple', [
  3237. 'multiple' => 'checkbox',
  3238. 'disabled' => 'disabled',
  3239. 'options' => $options
  3240. ]);
  3241. $expected = [
  3242. ['div' => ['class' => 'input select']],
  3243. ['label' => ['for' => 'contact-multiple']],
  3244. 'Multiple',
  3245. '/label',
  3246. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3247. ['div' => ['class' => 'checkbox']],
  3248. ['label' => ['for' => 'contact-multiple-1']],
  3249. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3250. 'One',
  3251. '/label',
  3252. '/div',
  3253. ['div' => ['class' => 'checkbox']],
  3254. ['label' => ['for' => 'contact-multiple-2']],
  3255. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3256. 'Two',
  3257. '/label',
  3258. '/div',
  3259. ['div' => ['class' => 'checkbox']],
  3260. ['label' => ['for' => 'contact-multiple-3']],
  3261. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3262. 'Three',
  3263. '/label',
  3264. '/div',
  3265. '/div'
  3266. ];
  3267. $this->assertHtml($expected, $result);
  3268. // make sure 50 does only disable 50, and not 50f5c0cf
  3269. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3270. $disabled = [50];
  3271. $expected = [
  3272. ['div' => ['class' => 'input select']],
  3273. ['label' => ['for' => 'contact-multiple']],
  3274. 'Multiple',
  3275. '/label',
  3276. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3277. ['div' => ['class' => 'checkbox']],
  3278. ['label' => ['for' => 'contact-multiple-50']],
  3279. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3280. 'Fifty',
  3281. '/label',
  3282. '/div',
  3283. ['div' => ['class' => 'checkbox']],
  3284. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3285. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3286. 'Stringy',
  3287. '/label',
  3288. '/div',
  3289. '/div'
  3290. ];
  3291. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3292. $this->assertHtml($expected, $result);
  3293. }
  3294. /**
  3295. * testControlWithLeadingInteger method
  3296. *
  3297. * Test input name with leading integer, ensure attributes are generated correctly.
  3298. *
  3299. * @return void
  3300. */
  3301. public function testControlWithLeadingInteger()
  3302. {
  3303. $result = $this->Form->text('0.Node.title');
  3304. $expected = [
  3305. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3306. ];
  3307. $this->assertHtml($expected, $result);
  3308. }
  3309. /**
  3310. * testControlSelectType method
  3311. *
  3312. * Test form->control() with select type inputs.
  3313. *
  3314. * @return void
  3315. */
  3316. public function testControlSelectType()
  3317. {
  3318. $result = $this->Form->control(
  3319. 'email',
  3320. [
  3321. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3322. );
  3323. $expected = [
  3324. 'div' => ['class' => 'input select'],
  3325. 'label' => ['for' => 'email'],
  3326. 'Email',
  3327. '/label',
  3328. ['select' => ['name' => 'email', 'id' => 'email']],
  3329. ['option' => ['value' => '']],
  3330. '/option',
  3331. ['option' => ['value' => 'è']],
  3332. 'Firést',
  3333. '/option',
  3334. ['option' => ['value' => 'é']],
  3335. 'Secoènd',
  3336. '/option',
  3337. '/select',
  3338. '/div'
  3339. ];
  3340. $this->assertHtml($expected, $result);
  3341. $result = $this->Form->control(
  3342. 'email',
  3343. [
  3344. 'options' => ['First', 'Second'], 'empty' => true]
  3345. );
  3346. $expected = [
  3347. 'div' => ['class' => 'input select'],
  3348. 'label' => ['for' => 'email'],
  3349. 'Email',
  3350. '/label',
  3351. ['select' => ['name' => 'email', 'id' => 'email']],
  3352. ['option' => ['value' => '']],
  3353. '/option',
  3354. ['option' => ['value' => '0']],
  3355. 'First',
  3356. '/option',
  3357. ['option' => ['value' => '1']],
  3358. 'Second',
  3359. '/option',
  3360. '/select',
  3361. '/div'
  3362. ];
  3363. $this->assertHtml($expected, $result);
  3364. $result = $this->Form->control('email', [
  3365. 'type' => 'select',
  3366. 'options' => new \ArrayObject(['First', 'Second']),
  3367. 'empty' => true
  3368. ]);
  3369. $this->assertHtml($expected, $result);
  3370. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3371. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3372. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3373. $expected = [
  3374. 'div' => ['class' => 'input select'],
  3375. 'label' => ['for' => 'model-user-id'],
  3376. 'User',
  3377. '/label',
  3378. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3379. ['option' => ['value' => '']],
  3380. '/option',
  3381. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3382. 'good',
  3383. '/option',
  3384. ['option' => ['value' => 'other']],
  3385. 'bad',
  3386. '/option',
  3387. '/select',
  3388. '/div'
  3389. ];
  3390. $this->assertHtml($expected, $result);
  3391. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3392. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3393. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3394. $expected = [
  3395. 'div' => ['class' => 'input select'],
  3396. 'label' => ['for' => 'thing-user-id'],
  3397. 'User',
  3398. '/label',
  3399. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3400. ['option' => ['value' => '']],
  3401. 'Some Empty',
  3402. '/option',
  3403. ['option' => ['value' => 'value']],
  3404. 'good',
  3405. '/option',
  3406. ['option' => ['value' => 'other']],
  3407. 'bad',
  3408. '/option',
  3409. '/select',
  3410. '/div'
  3411. ];
  3412. $this->assertHtml($expected, $result);
  3413. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3414. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3415. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3416. $expected = [
  3417. 'div' => ['class' => 'input select'],
  3418. 'label' => ['for' => 'thing-user-id'],
  3419. 'User',
  3420. '/label',
  3421. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3422. ['option' => ['value' => '']],
  3423. 'Some Empty',
  3424. '/option',
  3425. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3426. 'good',
  3427. '/option',
  3428. ['option' => ['value' => 'other']],
  3429. 'bad',
  3430. '/option',
  3431. '/select',
  3432. '/div'
  3433. ];
  3434. $this->assertHtml($expected, $result);
  3435. $this->Form->data = [];
  3436. $result = $this->Form->control('Publisher.id', [
  3437. 'label' => 'Publisher',
  3438. 'type' => 'select',
  3439. 'multiple' => 'checkbox',
  3440. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3441. ]);
  3442. $expected = [
  3443. ['div' => ['class' => 'input select']],
  3444. ['label' => ['for' => 'publisher-id']],
  3445. 'Publisher',
  3446. '/label',
  3447. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3448. ['div' => ['class' => 'checkbox']],
  3449. ['label' => ['for' => 'publisher-id-value-1']],
  3450. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3451. 'Label 1',
  3452. '/label',
  3453. '/div',
  3454. ['div' => ['class' => 'checkbox']],
  3455. ['label' => ['for' => 'publisher-id-value-2']],
  3456. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3457. 'Label 2',
  3458. '/label',
  3459. '/div',
  3460. '/div'
  3461. ];
  3462. $this->assertHtml($expected, $result);
  3463. }
  3464. /**
  3465. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3466. *
  3467. * Test that control() and a non standard primary key makes a hidden input by default.
  3468. *
  3469. * @return void
  3470. */
  3471. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3472. {
  3473. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3474. $this->Form->create($this->article);
  3475. $result = $this->Form->control('title');
  3476. $expected = [
  3477. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3478. ];
  3479. $this->assertHtml($expected, $result);
  3480. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3481. $this->Form->create($this->article);
  3482. $result = $this->Form->control('title');
  3483. $expected = [
  3484. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3485. ];
  3486. $this->assertHtml($expected, $result);
  3487. $result = $this->Form->control('body');
  3488. $expected = [
  3489. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3490. ];
  3491. $this->assertHtml($expected, $result);
  3492. }
  3493. /**
  3494. * testControlOverridingMagicSelectType method
  3495. *
  3496. * Test that overriding the magic select type widget is possible.
  3497. *
  3498. * @return void
  3499. */
  3500. public function testControlOverridingMagicSelectType()
  3501. {
  3502. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3503. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3504. $expected = [
  3505. 'div' => ['class' => 'input text'],
  3506. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3507. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3508. '/div'
  3509. ];
  3510. $this->assertHtml($expected, $result);
  3511. //Check that magic types still work for plural/singular vars
  3512. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3513. $result = $this->Form->control('Model.type');
  3514. $expected = [
  3515. 'div' => ['class' => 'input select'],
  3516. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3517. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3518. ['option' => ['value' => 'value']], 'good', '/option',
  3519. ['option' => ['value' => 'other']], 'bad', '/option',
  3520. '/select',
  3521. '/div'
  3522. ];
  3523. $this->assertHtml($expected, $result);
  3524. }
  3525. /**
  3526. * testControlMagicTypeDoesNotOverride method
  3527. *
  3528. * Test that inferred types do not override developer input.
  3529. *
  3530. * @return void
  3531. */
  3532. public function testControlMagicTypeDoesNotOverride()
  3533. {
  3534. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3535. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3536. $expected = [
  3537. 'div' => ['class' => 'input checkbox'],
  3538. ['input' => [
  3539. 'type' => 'hidden',
  3540. 'name' => 'Model[user]',
  3541. 'value' => 0,
  3542. ]],
  3543. 'label' => ['for' => 'model-user'],
  3544. ['input' => [
  3545. 'name' => 'Model[user]',
  3546. 'type' => 'checkbox',
  3547. 'id' => 'model-user',
  3548. 'value' => 1
  3549. ]],
  3550. 'User',
  3551. '/label',
  3552. '/div'
  3553. ];
  3554. $this->assertHtml($expected, $result);
  3555. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3556. $options = [
  3557. 1 => 'blue',
  3558. 2 => 'red'
  3559. ];
  3560. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3561. $expected = [
  3562. 'div' => ['class' => 'input select'],
  3563. 'label' => ['for' => 'tags-ids'],
  3564. 'Tags',
  3565. '/label',
  3566. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3567. ['div' => ['class' => 'checkbox']],
  3568. ['label' => ['for' => 'tags-ids-1']],
  3569. ['input' => [
  3570. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3571. 'value' => '1', 'name' => 'tags[_ids][]'
  3572. ]],
  3573. 'blue',
  3574. '/label',
  3575. '/div',
  3576. ['div' => ['class' => 'checkbox']],
  3577. ['label' => ['for' => 'tags-ids-2']],
  3578. ['input' => [
  3579. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3580. 'value' => '2', 'name' => 'tags[_ids][]'
  3581. ]],
  3582. 'red',
  3583. '/label',
  3584. '/div',
  3585. '/div'
  3586. ];
  3587. $this->assertHtml($expected, $result);
  3588. }
  3589. /**
  3590. * testControlMagicSelectForTypeNumber method
  3591. *
  3592. * Test that magic control() selects are created for type=number.
  3593. *
  3594. * @return void
  3595. */
  3596. public function testControlMagicSelectForTypeNumber()
  3597. {
  3598. TableRegistry::get('ValidateUsers', [
  3599. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3600. ]);
  3601. $entity = new Entity(['balance' => 1]);
  3602. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3603. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3604. $result = $this->Form->control('balance');
  3605. $expected = [
  3606. 'div' => ['class' => 'input select'],
  3607. 'label' => ['for' => 'balance'],
  3608. 'Balance',
  3609. '/label',
  3610. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3611. ['option' => ['value' => '0']],
  3612. 'nothing',
  3613. '/option',
  3614. ['option' => ['value' => '1', 'selected' => 'selected']],
  3615. 'some',
  3616. '/option',
  3617. ['option' => ['value' => '100']],
  3618. 'a lot',
  3619. '/option',
  3620. '/select',
  3621. '/div'
  3622. ];
  3623. $this->assertHtml($expected, $result);
  3624. }
  3625. /**
  3626. * testInvalidControlTypeOption method
  3627. *
  3628. * Test invalid 'input' type option to control() function.
  3629. *
  3630. * @return void
  3631. */
  3632. public function testInvalidControlTypeOption()
  3633. {
  3634. $this->expectException(\RuntimeException::class);
  3635. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3636. $this->Form->control('text', ['type' => 'input']);
  3637. }
  3638. /**
  3639. * testControlMagicSelectChangeToRadio method
  3640. *
  3641. * Test that magic control() selects can easily be converted into radio types without error.
  3642. *
  3643. * @return void
  3644. */
  3645. public function testControlMagicSelectChangeToRadio()
  3646. {
  3647. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3648. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3649. $this->assertContains('input type="radio"', $result);
  3650. }
  3651. /**
  3652. * testFormControlSubmit method
  3653. *
  3654. * Test correct results for form::control() and type submit.
  3655. *
  3656. * @return void
  3657. */
  3658. public function testFormControlSubmit()
  3659. {
  3660. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3661. $expected = [
  3662. 'div' => ['class' => 'submit'],
  3663. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3664. '/div'
  3665. ];
  3666. $this->assertHtml($expected, $result);
  3667. }
  3668. /**
  3669. * testFormControls method
  3670. *
  3671. * Test correct results from Form::controls().
  3672. *
  3673. * @return void
  3674. */
  3675. public function testFormControlsLegendFieldset()
  3676. {
  3677. $this->Form->create($this->article);
  3678. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3679. $expected = [
  3680. '<fieldset',
  3681. '<legend',
  3682. 'The Legend',
  3683. '/legend',
  3684. '*/fieldset',
  3685. ];
  3686. $this->assertHtml($expected, $result);
  3687. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3688. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3689. $this->assertContains('<fieldset>', $result);
  3690. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3691. $this->assertNotContains('<legend>', $result);
  3692. $this->assertNotContains('<fieldset>', $result);
  3693. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3694. $this->assertNotContains('<legend>', $result);
  3695. $this->assertNotContains('<fieldset>', $result);
  3696. $this->Form->create($this->article);
  3697. $this->Form->request->params['prefix'] = 'admin';
  3698. $this->Form->request->params['action'] = 'admin_edit';
  3699. $this->Form->request->params['controller'] = 'articles';
  3700. $result = $this->Form->allControls();
  3701. $expected = [
  3702. '<fieldset',
  3703. '<legend',
  3704. 'New Article',
  3705. '/legend',
  3706. '*/fieldset',
  3707. ];
  3708. $this->assertHtml($expected, $result);
  3709. $this->Form->create($this->article);
  3710. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3711. $expected = [
  3712. '<fieldset',
  3713. '<legend',
  3714. 'The Legend',
  3715. '/legend',
  3716. '*/fieldset',
  3717. ];
  3718. $this->assertHtml($expected, $result);
  3719. $this->Form->create($this->article);
  3720. $result = $this->Form->allControls([], [
  3721. 'fieldset' => [
  3722. 'class' => 'some-class some-other-class',
  3723. 'disabled' => true,
  3724. 'data-param' => 'a-param'
  3725. ],
  3726. 'legend' => 'The Legend'
  3727. ]);
  3728. $expected = [
  3729. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3730. '<legend',
  3731. 'The Legend',
  3732. '/legend',
  3733. '*/fieldset',
  3734. ];
  3735. $this->assertHtml($expected, $result);
  3736. }
  3737. /**
  3738. * testFormControls method
  3739. *
  3740. * Test the controls() method.
  3741. *
  3742. * @return void
  3743. */
  3744. public function testFormControls()
  3745. {
  3746. $this->Form->create($this->article);
  3747. $result = $this->Form->allControls();
  3748. $expected = [
  3749. '<fieldset',
  3750. '<legend', 'New Article', '/legend',
  3751. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3752. ['div' => ['class' => 'input select required']],
  3753. '*/div',
  3754. ['div' => ['class' => 'input text required']],
  3755. '*/div',
  3756. ['div' => ['class' => 'input text']],
  3757. '*/div',
  3758. ['div' => ['class' => 'input text']],
  3759. '*/div',
  3760. '/fieldset',
  3761. ];
  3762. $this->assertHtml($expected, $result);
  3763. $result = $this->Form->allControls([
  3764. 'published' => ['type' => 'boolean']
  3765. ]);
  3766. $expected = [
  3767. '<fieldset',
  3768. '<legend', 'New Article', '/legend',
  3769. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3770. ['div' => ['class' => 'input select required']],
  3771. '*/div',
  3772. ['div' => ['class' => 'input text required']],
  3773. '*/div',
  3774. ['div' => ['class' => 'input text']],
  3775. '*/div',
  3776. ['div' => ['class' => 'input boolean']],
  3777. '*/div',
  3778. '/fieldset',
  3779. ];
  3780. $this->assertHtml($expected, $result);
  3781. $this->Form->create($this->article);
  3782. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3783. $expected = [
  3784. 'fieldset' => [],
  3785. 'legend' => [],
  3786. 'Hello',
  3787. '/legend',
  3788. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3789. ['div' => ['class' => 'input select required']],
  3790. '*/div',
  3791. ['div' => ['class' => 'input text required']],
  3792. '*/div',
  3793. ['div' => ['class' => 'input text']],
  3794. '*/div',
  3795. ['div' => ['class' => 'input text']],
  3796. '*/div',
  3797. '/fieldset'
  3798. ];
  3799. $this->assertHtml($expected, $result);
  3800. $this->Form->create(false);
  3801. $expected = [
  3802. 'fieldset' => [],
  3803. ['div' => ['class' => 'input text']],
  3804. 'label' => ['for' => 'foo'],
  3805. 'Foo',
  3806. '/label',
  3807. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3808. '*/div',
  3809. '/fieldset'
  3810. ];
  3811. $result = $this->Form->allControls(
  3812. ['foo' => ['type' => 'text']],
  3813. ['legend' => false]
  3814. );
  3815. $this->assertHtml($expected, $result);
  3816. }
  3817. /**
  3818. * testFormControlsBlacklist method
  3819. *
  3820. * @return void
  3821. */
  3822. public function testFormControlsBlacklist()
  3823. {
  3824. $this->Form->create($this->article);
  3825. $result = $this->Form->allControls([
  3826. 'id' => false
  3827. ]);
  3828. $expected = [
  3829. '<fieldset',
  3830. '<legend', 'New Article', '/legend',
  3831. ['div' => ['class' => 'input select required']],
  3832. '*/div',
  3833. ['div' => ['class' => 'input text required']],
  3834. '*/div',
  3835. ['div' => ['class' => 'input text']],
  3836. '*/div',
  3837. ['div' => ['class' => 'input text']],
  3838. '*/div',
  3839. '/fieldset',
  3840. ];
  3841. $this->assertHtml($expected, $result);
  3842. $this->Form->create($this->article);
  3843. $result = $this->Form->allControls([
  3844. 'id' => []
  3845. ]);
  3846. $expected = [
  3847. '<fieldset',
  3848. '<legend', 'New Article', '/legend',
  3849. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3850. ['div' => ['class' => 'input select required']],
  3851. '*/div',
  3852. ['div' => ['class' => 'input text required']],
  3853. '*/div',
  3854. ['div' => ['class' => 'input text']],
  3855. '*/div',
  3856. ['div' => ['class' => 'input text']],
  3857. '*/div',
  3858. '/fieldset',
  3859. ];
  3860. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3861. }
  3862. /**
  3863. * testSelectAsCheckbox method
  3864. *
  3865. * Test multi-select widget with checkbox formatting.
  3866. *
  3867. * @return void
  3868. */
  3869. public function testSelectAsCheckbox()
  3870. {
  3871. $result = $this->Form->select(
  3872. 'Model.multi_field',
  3873. ['first', 'second', 'third'],
  3874. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3875. );
  3876. $expected = [
  3877. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3878. ['div' => ['class' => 'checkbox']],
  3879. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3880. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3881. 'first',
  3882. '/label',
  3883. '/div',
  3884. ['div' => ['class' => 'checkbox']],
  3885. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3886. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3887. 'second',
  3888. '/label',
  3889. '/div',
  3890. ['div' => ['class' => 'checkbox']],
  3891. ['label' => ['for' => 'model-multi-field-2']],
  3892. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3893. 'third',
  3894. '/label',
  3895. '/div',
  3896. ];
  3897. $this->assertHtml($expected, $result);
  3898. $result = $this->Form->select(
  3899. 'Model.multi_field',
  3900. ['1/2' => 'half'],
  3901. ['multiple' => 'checkbox']
  3902. );
  3903. $expected = [
  3904. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3905. ['div' => ['class' => 'checkbox']],
  3906. ['label' => ['for' => 'model-multi-field-1-2']],
  3907. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3908. 'half',
  3909. '/label',
  3910. '/div',
  3911. ];
  3912. $this->assertHtml($expected, $result);
  3913. }
  3914. /**
  3915. * testLabel method
  3916. *
  3917. * Test label generation.
  3918. *
  3919. * @return void
  3920. */
  3921. public function testLabel()
  3922. {
  3923. $result = $this->Form->label('Person.name');
  3924. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3925. $this->assertHtml($expected, $result);
  3926. $result = $this->Form->label('Person.name');
  3927. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3928. $this->assertHtml($expected, $result);
  3929. $result = $this->Form->label('Person.first_name');
  3930. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3931. $this->assertHtml($expected, $result);
  3932. $result = $this->Form->label('Person.first_name', 'Your first name');
  3933. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3934. $this->assertHtml($expected, $result);
  3935. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3936. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3937. $this->assertHtml($expected, $result);
  3938. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3939. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3940. $this->assertHtml($expected, $result);
  3941. $result = $this->Form->label('Person.first_name', '');
  3942. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3943. $this->assertHtml($expected, $result);
  3944. $result = $this->Form->label('Person.2.name', '');
  3945. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3946. $this->assertHtml($expected, $result);
  3947. }
  3948. /**
  3949. * testLabelContainControl method
  3950. *
  3951. * Test that label() can accept an input with the correct template vars.
  3952. *
  3953. * @return void
  3954. */
  3955. public function testLabelContainControl()
  3956. {
  3957. $this->Form->templates([
  3958. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3959. ]);
  3960. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3961. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3962. ]);
  3963. $expected = [
  3964. 'label' => ['for' => 'person-accept-terms'],
  3965. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3966. 'Accept',
  3967. '/label',
  3968. ];
  3969. $this->assertHtml($expected, $result);
  3970. }
  3971. /**
  3972. * testTextbox method
  3973. *
  3974. * Test textbox element generation.
  3975. *
  3976. * @return void
  3977. */
  3978. public function testTextbox()
  3979. {
  3980. $result = $this->Form->text('Model.field');
  3981. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3982. $this->assertHtml($expected, $result);
  3983. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3984. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3985. $this->assertHtml($expected, $result);
  3986. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3987. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3988. $this->assertHtml($expected, $result);
  3989. }
  3990. /**
  3991. * testTextBoxDataAndError method
  3992. *
  3993. * Test that text() hooks up with request data and error fields.
  3994. *
  3995. * @return void
  3996. */
  3997. public function testTextBoxDataAndError()
  3998. {
  3999. $this->article['errors'] = [
  4000. 'Contact' => ['text' => 'wrong']
  4001. ];
  4002. $this->Form->create($this->article);
  4003. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  4004. $result = $this->Form->text('Model.text');
  4005. $expected = [
  4006. 'input' => [
  4007. 'type' => 'text',
  4008. 'name' => 'Model[text]',
  4009. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4010. ]
  4011. ];
  4012. $this->assertHtml($expected, $result);
  4013. $this->Form->request->data['Contact']['text'] = 'test';
  4014. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4015. $expected = [
  4016. 'input' => [
  4017. 'type' => 'text',
  4018. 'name' => 'Contact[text]',
  4019. 'value' => 'test',
  4020. 'id' => 'theID',
  4021. 'class' => 'form-error'
  4022. ]
  4023. ];
  4024. $this->assertHtml($expected, $result);
  4025. }
  4026. /**
  4027. * testDefaultValue method
  4028. *
  4029. * Test default value setting.
  4030. *
  4031. * @return void
  4032. */
  4033. public function testTextDefaultValue()
  4034. {
  4035. $this->Form->request->data['Model']['field'] = 'test';
  4036. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4037. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4038. $this->assertHtml($expected, $result);
  4039. unset($this->Form->request->data['Model']['field']);
  4040. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4041. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4042. $this->assertHtml($expected, $result);
  4043. $Articles = TableRegistry::get('Articles');
  4044. $title = $Articles->schema()->column('title');
  4045. $Articles->schema()->addColumn(
  4046. 'title',
  4047. ['default' => 'default title'] + $title
  4048. );
  4049. $entity = $Articles->newEntity();
  4050. $this->Form->create($entity);
  4051. // Get default value from schema
  4052. $result = $this->Form->text('title');
  4053. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4054. $this->assertHtml($expected, $result);
  4055. // Don't get value from schema
  4056. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4057. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4058. $this->assertHtml($expected, $result);
  4059. // Custom default value overrides default value from schema
  4060. $result = $this->Form->text('title', ['default' => 'override default']);
  4061. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4062. $this->assertHtml($expected, $result);
  4063. // Default value from schema is used only for new entities.
  4064. $entity->isNew(false);
  4065. $result = $this->Form->text('title');
  4066. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4067. $this->assertHtml($expected, $result);
  4068. }
  4069. /**
  4070. * testError method
  4071. *
  4072. * Test field error generation.
  4073. *
  4074. * @return void
  4075. */
  4076. public function testError()
  4077. {
  4078. $this->article['errors'] = [
  4079. 'Article' => ['field' => 'email']
  4080. ];
  4081. $this->Form->create($this->article);
  4082. $result = $this->Form->error('Article.field');
  4083. $expected = [
  4084. ['div' => ['class' => 'error-message']],
  4085. 'email',
  4086. '/div',
  4087. ];
  4088. $this->assertHtml($expected, $result);
  4089. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4090. $expected = [
  4091. ['div' => ['class' => 'error-message']],
  4092. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4093. '/div',
  4094. ];
  4095. $this->assertHtml($expected, $result);
  4096. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4097. $expected = [
  4098. ['div' => ['class' => 'error-message']],
  4099. '<strong', 'Badness!', '/strong',
  4100. '/div',
  4101. ];
  4102. $this->assertHtml($expected, $result);
  4103. }
  4104. /**
  4105. * testErrorRuleName method
  4106. *
  4107. * Test error translation can use rule names for translating.
  4108. *
  4109. * @return void
  4110. */
  4111. public function testErrorRuleName()
  4112. {
  4113. $this->article['errors'] = [
  4114. 'Article' => [
  4115. 'field' => ['email' => 'Your email was not good']
  4116. ]
  4117. ];
  4118. $this->Form->create($this->article);
  4119. $result = $this->Form->error('Article.field');
  4120. $expected = [
  4121. ['div' => ['class' => 'error-message']],
  4122. 'Your email was not good',
  4123. '/div',
  4124. ];
  4125. $this->assertHtml($expected, $result);
  4126. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4127. $expected = [
  4128. ['div' => ['class' => 'error-message']],
  4129. 'Email in use',
  4130. '/div',
  4131. ];
  4132. $this->assertHtml($expected, $result);
  4133. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4134. $expected = [
  4135. ['div' => ['class' => 'error-message']],
  4136. 'Email in use',
  4137. '/div',
  4138. ];
  4139. $this->assertHtml($expected, $result);
  4140. $result = $this->Form->error('Article.field', [
  4141. 'email' => 'Key is preferred',
  4142. 'Your email was not good' => 'Email in use'
  4143. ]);
  4144. $expected = [
  4145. ['div' => ['class' => 'error-message']],
  4146. 'Key is preferred',
  4147. '/div',
  4148. ];
  4149. $this->assertHtml($expected, $result);
  4150. }
  4151. /**
  4152. * testErrorMessages method
  4153. *
  4154. * Test error with nested lists.
  4155. *
  4156. * @return void
  4157. */
  4158. public function testErrorMessages()
  4159. {
  4160. $this->article['errors'] = [
  4161. 'Article' => ['field' => 'email']
  4162. ];
  4163. $this->Form->create($this->article);
  4164. $result = $this->Form->error('Article.field', [
  4165. 'email' => 'No good!'
  4166. ]);
  4167. $expected = [
  4168. 'div' => ['class' => 'error-message'],
  4169. 'No good!',
  4170. '/div'
  4171. ];
  4172. $this->assertHtml($expected, $result);
  4173. }
  4174. /**
  4175. * testErrorMultipleMessages method
  4176. *
  4177. * Test error() with multiple messages.
  4178. *
  4179. * @return void
  4180. */
  4181. public function testErrorMultipleMessages()
  4182. {
  4183. $this->article['errors'] = [
  4184. 'field' => ['notBlank', 'email', 'Something else']
  4185. ];
  4186. $this->Form->create($this->article);
  4187. $result = $this->Form->error('field', [
  4188. 'notBlank' => 'Cannot be empty',
  4189. 'email' => 'No good!'
  4190. ]);
  4191. $expected = [
  4192. 'div' => ['class' => 'error-message'],
  4193. 'ul' => [],
  4194. '<li', 'Cannot be empty', '/li',
  4195. '<li', 'No good!', '/li',
  4196. '<li', 'Something else', '/li',
  4197. '/ul',
  4198. '/div'
  4199. ];
  4200. $this->assertHtml($expected, $result);
  4201. }
  4202. /**
  4203. * testPassword method
  4204. *
  4205. * Test password element generation.
  4206. *
  4207. * @return void
  4208. */
  4209. public function testPassword()
  4210. {
  4211. $this->article['errors'] = [
  4212. 'Contact' => [
  4213. 'passwd' => 1
  4214. ]
  4215. ];
  4216. $this->Form->create($this->article);
  4217. $result = $this->Form->password('Contact.field');
  4218. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4219. $this->assertHtml($expected, $result);
  4220. $this->Form->request->data['Contact']['passwd'] = 'test';
  4221. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4222. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4223. $this->assertHtml($expected, $result);
  4224. }
  4225. /**
  4226. * testRadio method
  4227. *
  4228. * Test radio element set generation.
  4229. *
  4230. * @return void
  4231. */
  4232. public function testRadio()
  4233. {
  4234. $result = $this->Form->radio('Model.field', ['option A']);
  4235. $expected = [
  4236. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4237. 'label' => ['for' => 'model-field-0'],
  4238. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4239. 'option A',
  4240. '/label'
  4241. ];
  4242. $this->assertHtml($expected, $result);
  4243. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4244. $this->assertHtml($expected, $result);
  4245. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4246. $expected = [
  4247. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4248. ['label' => ['for' => 'model-field-0']],
  4249. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4250. 'option A',
  4251. '/label',
  4252. ['label' => ['for' => 'model-field-1']],
  4253. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4254. 'option B',
  4255. '/label',
  4256. ];
  4257. $this->assertHtml($expected, $result);
  4258. $result = $this->Form->radio(
  4259. 'Employee.gender',
  4260. ['male' => 'Male', 'female' => 'Female'],
  4261. ['form' => 'my-form']
  4262. );
  4263. $expected = [
  4264. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4265. ['label' => ['for' => 'employee-gender-male']],
  4266. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4267. 'Male',
  4268. '/label',
  4269. ['label' => ['for' => 'employee-gender-female']],
  4270. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4271. 'Female',
  4272. '/label',
  4273. ];
  4274. $this->assertHtml($expected, $result);
  4275. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4276. $expected = [
  4277. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4278. ['label' => ['for' => 'model-custom-0']],
  4279. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4280. 'option A',
  4281. '/label',
  4282. ['label' => ['for' => 'model-custom-1']],
  4283. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4284. 'option B',
  4285. '/label',
  4286. ];
  4287. $this->assertHtml($expected, $result);
  4288. $result = $this->Form->radio(
  4289. 'Employee.gender',
  4290. [
  4291. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4292. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4293. ]
  4294. );
  4295. $expected = [
  4296. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4297. ['label' => ['for' => 'employee-gender-male']],
  4298. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4299. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4300. 'Male',
  4301. '/label',
  4302. ['label' => ['for' => 'employee-gender-female']],
  4303. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4304. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4305. 'Female',
  4306. '/label',
  4307. ];
  4308. $this->assertHtml($expected, $result);
  4309. }
  4310. /**
  4311. * Test radio with complex options and empty disabled data.
  4312. *
  4313. * @return void
  4314. */
  4315. public function testRadioComplexDisabled()
  4316. {
  4317. $options = [
  4318. ['value' => 'r', 'text' => 'red'],
  4319. ['value' => 'b', 'text' => 'blue'],
  4320. ];
  4321. $attrs = ['disabled' => []];
  4322. $result = $this->Form->radio('Model.field', $options, $attrs);
  4323. $expected = [
  4324. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4325. ['label' => ['for' => 'model-field-r']],
  4326. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4327. 'red',
  4328. '/label',
  4329. ['label' => ['for' => 'model-field-b']],
  4330. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4331. 'blue',
  4332. '/label',
  4333. ];
  4334. $this->assertHtml($expected, $result);
  4335. $attrs = ['disabled' => ['r']];
  4336. $result = $this->Form->radio('Model.field', $options, $attrs);
  4337. $this->assertContains('disabled="disabled"', $result);
  4338. }
  4339. /**
  4340. * testRadioDefaultValue method
  4341. *
  4342. * Test default value setting on radio() method.
  4343. *
  4344. * @return void
  4345. */
  4346. public function testRadioDefaultValue()
  4347. {
  4348. $Articles = TableRegistry::get('Articles');
  4349. $title = $Articles->schema()->column('title');
  4350. $Articles->schema()->addColumn(
  4351. 'title',
  4352. ['default' => '1'] + $title
  4353. );
  4354. $this->Form->create($Articles->newEntity());
  4355. $result = $this->Form->radio('title', ['option A', 'option B']);
  4356. $expected = [
  4357. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4358. ['label' => ['for' => 'title-0']],
  4359. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4360. 'option A',
  4361. '/label',
  4362. ['label' => ['for' => 'title-1']],
  4363. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4364. 'option B',
  4365. '/label',
  4366. ];
  4367. $this->assertHtml($expected, $result);
  4368. }
  4369. /**
  4370. * Test setting a hiddenField value on radio buttons.
  4371. *
  4372. * @return void
  4373. */
  4374. public function testRadioHiddenFieldValue()
  4375. {
  4376. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4377. $expected = [
  4378. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4379. 'label' => ['for' => 'title-0'],
  4380. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4381. 'option A',
  4382. '/label',
  4383. ];
  4384. $this->assertHtml($expected, $result);
  4385. }
  4386. /**
  4387. * testControlRadio method
  4388. *
  4389. * Test that input works with radio types.
  4390. *
  4391. * @return void
  4392. */
  4393. public function testControlRadio()
  4394. {
  4395. $result = $this->Form->control('test', [
  4396. 'type' => 'radio',
  4397. 'options' => ['A', 'B'],
  4398. ]);
  4399. $expected = [
  4400. ['div' => ['class' => 'input radio']],
  4401. '<label',
  4402. 'Test',
  4403. '/label',
  4404. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4405. ['label' => ['for' => 'test-0']],
  4406. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4407. 'A',
  4408. '/label',
  4409. ['label' => ['for' => 'test-1']],
  4410. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4411. 'B',
  4412. '/label',
  4413. '/div',
  4414. ];
  4415. $this->assertHtml($expected, $result);
  4416. $result = $this->Form->control('test', [
  4417. 'type' => 'radio',
  4418. 'options' => ['A', 'B'],
  4419. 'value' => '0'
  4420. ]);
  4421. $expected = [
  4422. ['div' => ['class' => 'input radio']],
  4423. '<label',
  4424. 'Test',
  4425. '/label',
  4426. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4427. ['label' => ['for' => 'test-0']],
  4428. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4429. 'A',
  4430. '/label',
  4431. ['label' => ['for' => 'test-1']],
  4432. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4433. 'B',
  4434. '/label',
  4435. '/div',
  4436. ];
  4437. $this->assertHtml($expected, $result);
  4438. $result = $this->Form->control('test', [
  4439. 'type' => 'radio',
  4440. 'options' => ['A', 'B'],
  4441. 'label' => false
  4442. ]);
  4443. $expected = [
  4444. ['div' => ['class' => 'input radio']],
  4445. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4446. ['label' => ['for' => 'test-0']],
  4447. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4448. 'A',
  4449. '/label',
  4450. ['label' => ['for' => 'test-1']],
  4451. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4452. 'B',
  4453. '/label',
  4454. '/div',
  4455. ];
  4456. $this->assertHtml($expected, $result);
  4457. }
  4458. /**
  4459. * testRadioNoLabel method
  4460. *
  4461. * Test that radio() works with label = false.
  4462. *
  4463. * @return void
  4464. */
  4465. public function testRadioNoLabel()
  4466. {
  4467. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4468. $expected = [
  4469. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4470. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4471. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4472. ];
  4473. $this->assertHtml($expected, $result);
  4474. }
  4475. /**
  4476. * testRadioControlInsideLabel method
  4477. *
  4478. * Test generating radio input inside label ala twitter bootstrap.
  4479. *
  4480. * @return void
  4481. */
  4482. public function testRadioControlInsideLabel()
  4483. {
  4484. $this->Form->templates([
  4485. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4486. 'radioWrapper' => '{{label}}'
  4487. ]);
  4488. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4489. //@codingStandardsIgnoreStart
  4490. $expected = [
  4491. ['input' => [
  4492. 'type' => 'hidden',
  4493. 'name' => 'Model[field]',
  4494. 'value' => ''
  4495. ]],
  4496. ['label' => ['for' => 'model-field-0']],
  4497. ['input' => [
  4498. 'type' => 'radio',
  4499. 'name' => 'Model[field]',
  4500. 'value' => '0',
  4501. 'id' => 'model-field-0'
  4502. ]],
  4503. 'option A',
  4504. '/label',
  4505. ['label' => ['for' => 'model-field-1']],
  4506. ['input' => [
  4507. 'type' => 'radio',
  4508. 'name' => 'Model[field]',
  4509. 'value' => '1',
  4510. 'id' => 'model-field-1'
  4511. ]],
  4512. 'option B',
  4513. '/label',
  4514. ];
  4515. //@codingStandardsIgnoreEnd
  4516. $this->assertHtml($expected, $result);
  4517. }
  4518. /**
  4519. * testRadioHiddenControlDisabling method
  4520. *
  4521. * Test disabling the hidden input for radio buttons.
  4522. *
  4523. * @return void
  4524. */
  4525. public function testRadioHiddenControlDisabling()
  4526. {
  4527. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4528. $expected = [
  4529. 'label' => ['for' => 'model-1-field-0'],
  4530. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4531. 'option A',
  4532. '/label'
  4533. ];
  4534. $this->assertHtml($expected, $result);
  4535. }
  4536. /**
  4537. * testRadioOutOfRange method
  4538. *
  4539. * Test radio element set generation.
  4540. *
  4541. * @return void
  4542. */
  4543. public function testRadioOutOfRange()
  4544. {
  4545. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4546. $expected = [
  4547. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4548. 'label' => ['for' => 'model-field-v'],
  4549. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4550. 'value',
  4551. '/label'
  4552. ];
  4553. $this->assertHtml($expected, $result);
  4554. }
  4555. /**
  4556. * testSelect method
  4557. *
  4558. * Test select element generation.
  4559. *
  4560. * @return void
  4561. */
  4562. public function testSelect()
  4563. {
  4564. $result = $this->Form->select('Model.field', []);
  4565. $expected = [
  4566. 'select' => ['name' => 'Model[field]'],
  4567. '/select'
  4568. ];
  4569. $this->assertHtml($expected, $result);
  4570. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4571. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4572. $expected = [
  4573. 'select' => ['name' => 'Model[field]'],
  4574. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4575. 'good',
  4576. '/option',
  4577. ['option' => ['value' => 'other']],
  4578. 'bad',
  4579. '/option',
  4580. '/select'
  4581. ];
  4582. $this->assertHtml($expected, $result);
  4583. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4584. $this->assertHtml($expected, $result);
  4585. $this->Form->request->data = [];
  4586. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4587. $expected = [
  4588. 'select' => ['name' => 'Model[field]'],
  4589. ['option' => ['value' => 'value']],
  4590. 'good',
  4591. '/option',
  4592. ['option' => ['value' => 'other']],
  4593. 'bad',
  4594. '/option',
  4595. '/select'
  4596. ];
  4597. $this->assertHtml($expected, $result);
  4598. $options = [
  4599. ['value' => 'first', 'text' => 'First'],
  4600. ['value' => 'first', 'text' => 'Another First'],
  4601. ];
  4602. $result = $this->Form->select(
  4603. 'Model.field',
  4604. $options,
  4605. ['escape' => false, 'empty' => false]
  4606. );
  4607. $expected = [
  4608. 'select' => ['name' => 'Model[field]'],
  4609. ['option' => ['value' => 'first']],
  4610. 'First',
  4611. '/option',
  4612. ['option' => ['value' => 'first']],
  4613. 'Another First',
  4614. '/option',
  4615. '/select'
  4616. ];
  4617. $this->assertHtml($expected, $result);
  4618. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4619. $result = $this->Form->select(
  4620. 'Model.contact_id',
  4621. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4622. ['escape' => false, 'empty' => 'pick something']
  4623. );
  4624. $expected = [
  4625. 'select' => ['name' => 'Model[contact_id]'],
  4626. ['option' => ['value' => '']], 'pick something', '/option',
  4627. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4628. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4629. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4630. '/select'
  4631. ];
  4632. $this->assertHtml($expected, $result);
  4633. $this->Form->request->data['Model']['field'] = 0;
  4634. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4635. $expected = [
  4636. 'select' => ['name' => 'Model[field]'],
  4637. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4638. ['option' => ['value' => '1']], 'Yes', '/option',
  4639. '/select'
  4640. ];
  4641. $this->assertHtml($expected, $result);
  4642. }
  4643. /**
  4644. * testSelectEscapeHtml method
  4645. *
  4646. * Test that select() escapes HTML.
  4647. *
  4648. * @return void
  4649. */
  4650. public function testSelectEscapeHtml()
  4651. {
  4652. $result = $this->Form->select(
  4653. 'Model.field',
  4654. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4655. ['empty' => false]
  4656. );
  4657. $expected = [
  4658. 'select' => ['name' => 'Model[field]'],
  4659. ['option' => ['value' => 'first']],
  4660. 'first &quot;html&quot; &lt;chars&gt;',
  4661. '/option',
  4662. ['option' => ['value' => 'second']],
  4663. 'value',
  4664. '/option',
  4665. '/select'
  4666. ];
  4667. $this->assertHtml($expected, $result);
  4668. $result = $this->Form->select(
  4669. 'Model.field',
  4670. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4671. ['escape' => false, 'empty' => false]
  4672. );
  4673. $expected = [
  4674. 'select' => ['name' => 'Model[field]'],
  4675. ['option' => ['value' => 'first']],
  4676. 'first "html" <chars>',
  4677. '/option',
  4678. ['option' => ['value' => 'second']],
  4679. 'value',
  4680. '/option',
  4681. '/select'
  4682. ];
  4683. $this->assertHtml($expected, $result);
  4684. }
  4685. /**
  4686. * testSelectRequired method
  4687. *
  4688. * Test select() with required and disabled attributes.
  4689. *
  4690. * @return void
  4691. */
  4692. public function testSelectRequired()
  4693. {
  4694. $this->article['required'] = [
  4695. 'user_id' => true
  4696. ];
  4697. $this->Form->create($this->article);
  4698. $result = $this->Form->select('user_id', ['option A']);
  4699. $expected = [
  4700. 'select' => [
  4701. 'name' => 'user_id',
  4702. 'required' => 'required'
  4703. ],
  4704. ['option' => ['value' => '0']], 'option A', '/option',
  4705. '/select'
  4706. ];
  4707. $this->assertHtml($expected, $result);
  4708. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4709. $expected = [
  4710. 'select' => [
  4711. 'name' => 'user_id',
  4712. 'disabled' => 'disabled'
  4713. ],
  4714. ['option' => ['value' => '0']], 'option A', '/option',
  4715. '/select'
  4716. ];
  4717. $this->assertHtml($expected, $result);
  4718. }
  4719. /**
  4720. * testNestedSelect method
  4721. *
  4722. * Test select element generation with optgroups.
  4723. *
  4724. * @return void
  4725. */
  4726. public function testNestedSelect()
  4727. {
  4728. $result = $this->Form->select(
  4729. 'Model.field',
  4730. [1 => 'One', 2 => 'Two', 'Three' => [
  4731. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4732. ]],
  4733. ['empty' => false]
  4734. );
  4735. $expected = [
  4736. 'select' => ['name' => 'Model[field]'],
  4737. ['option' => ['value' => 1]],
  4738. 'One',
  4739. '/option',
  4740. ['option' => ['value' => 2]],
  4741. 'Two',
  4742. '/option',
  4743. ['optgroup' => ['label' => 'Three']],
  4744. ['option' => ['value' => 3]],
  4745. 'Three',
  4746. '/option',
  4747. ['option' => ['value' => 4]],
  4748. 'Four',
  4749. '/option',
  4750. ['option' => ['value' => 5]],
  4751. 'Five',
  4752. '/option',
  4753. '/optgroup',
  4754. '/select'
  4755. ];
  4756. $this->assertHtml($expected, $result);
  4757. }
  4758. /**
  4759. * testSelectMultiple method
  4760. *
  4761. * Test generation of multiple select elements.
  4762. *
  4763. * @return void
  4764. */
  4765. public function testSelectMultiple()
  4766. {
  4767. $options = ['first', 'second', 'third'];
  4768. $result = $this->Form->select(
  4769. 'Model.multi_field',
  4770. $options,
  4771. ['form' => 'my-form', 'multiple' => true]
  4772. );
  4773. $expected = [
  4774. 'input' => [
  4775. 'type' => 'hidden',
  4776. 'name' => 'Model[multi_field]',
  4777. 'value' => '',
  4778. 'form' => 'my-form',
  4779. ],
  4780. 'select' => [
  4781. 'name' => 'Model[multi_field][]',
  4782. 'multiple' => 'multiple',
  4783. 'form' => 'my-form',
  4784. ],
  4785. ['option' => ['value' => '0']],
  4786. 'first',
  4787. '/option',
  4788. ['option' => ['value' => '1']],
  4789. 'second',
  4790. '/option',
  4791. ['option' => ['value' => '2']],
  4792. 'third',
  4793. '/option',
  4794. '/select'
  4795. ];
  4796. $this->assertHtml($expected, $result);
  4797. $result = $this->Form->select(
  4798. 'Model.multi_field',
  4799. $options,
  4800. ['multiple' => 'multiple', 'form' => 'my-form']
  4801. );
  4802. $this->assertHtml($expected, $result);
  4803. }
  4804. /**
  4805. * testCheckboxZeroValue method
  4806. *
  4807. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4808. *
  4809. * @return void
  4810. */
  4811. public function testCheckboxZeroValue()
  4812. {
  4813. $result = $this->Form->control('User.get_spam', [
  4814. 'type' => 'checkbox',
  4815. 'value' => '0',
  4816. 'hiddenField' => '1',
  4817. ]);
  4818. $expected = [
  4819. 'div' => ['class' => 'input checkbox'],
  4820. 'label' => ['for' => 'user-get-spam'],
  4821. ['input' => [
  4822. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4823. 'value' => '1'
  4824. ]],
  4825. ['input' => [
  4826. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4827. 'value' => '0', 'id' => 'user-get-spam'
  4828. ]],
  4829. 'Get Spam',
  4830. '/label',
  4831. '/div'
  4832. ];
  4833. $this->assertHtml($expected, $result);
  4834. }
  4835. /**
  4836. * testHabtmSelectBox method
  4837. *
  4838. * Test generation of habtm select boxes.
  4839. *
  4840. * @return void
  4841. */
  4842. public function testHabtmSelectBox()
  4843. {
  4844. $this->loadFixtures('Articles');
  4845. $options = [
  4846. 1 => 'blue',
  4847. 2 => 'red',
  4848. 3 => 'green'
  4849. ];
  4850. $tags = [
  4851. new Entity(['id' => 1, 'name' => 'blue']),
  4852. new Entity(['id' => 3, 'name' => 'green'])
  4853. ];
  4854. $article = new Article(['tags' => $tags]);
  4855. $this->Form->create($article);
  4856. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4857. $expected = [
  4858. 'div' => ['class' => 'input select'],
  4859. 'label' => ['for' => 'tags-ids'],
  4860. 'Tags',
  4861. '/label',
  4862. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4863. 'select' => [
  4864. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4865. 'multiple' => 'multiple'
  4866. ],
  4867. ['option' => ['value' => '1', 'selected' => 'selected']],
  4868. 'blue',
  4869. '/option',
  4870. ['option' => ['value' => '2']],
  4871. 'red',
  4872. '/option',
  4873. ['option' => ['value' => '3', 'selected' => 'selected']],
  4874. 'green',
  4875. '/option',
  4876. '/select',
  4877. '/div'
  4878. ];
  4879. $this->assertHtml($expected, $result);
  4880. // make sure only 50 is selected, and not 50f5c0cf
  4881. $options = [
  4882. '1' => 'blue',
  4883. '50f5c0cf' => 'red',
  4884. '50' => 'green'
  4885. ];
  4886. $tags = [
  4887. new Entity(['id' => 1, 'name' => 'blue']),
  4888. new Entity(['id' => 50, 'name' => 'green'])
  4889. ];
  4890. $article = new Article(['tags' => $tags]);
  4891. $this->Form->create($article);
  4892. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4893. $expected = [
  4894. 'div' => ['class' => 'input select'],
  4895. 'label' => ['for' => 'tags-ids'],
  4896. 'Tags',
  4897. '/label',
  4898. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4899. 'select' => [
  4900. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4901. 'multiple' => 'multiple'
  4902. ],
  4903. ['option' => ['value' => '1', 'selected' => 'selected']],
  4904. 'blue',
  4905. '/option',
  4906. ['option' => ['value' => '50f5c0cf']],
  4907. 'red',
  4908. '/option',
  4909. ['option' => ['value' => '50', 'selected' => 'selected']],
  4910. 'green',
  4911. '/option',
  4912. '/select',
  4913. '/div'
  4914. ];
  4915. $this->assertHtml($expected, $result);
  4916. $spacecraft = [
  4917. 1 => 'Orion',
  4918. 2 => 'Helios'
  4919. ];
  4920. $this->View->viewVars['spacecraft'] = $spacecraft;
  4921. $this->Form->create();
  4922. $result = $this->Form->control('spacecraft._ids');
  4923. $expected = [
  4924. 'div' => ['class' => 'input select'],
  4925. 'label' => ['for' => 'spacecraft-ids'],
  4926. 'Spacecraft',
  4927. '/label',
  4928. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4929. 'select' => [
  4930. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4931. 'multiple' => 'multiple'
  4932. ],
  4933. ['option' => ['value' => '1']],
  4934. 'Orion',
  4935. '/option',
  4936. ['option' => ['value' => '2']],
  4937. 'Helios',
  4938. '/option',
  4939. '/select',
  4940. '/div'
  4941. ];
  4942. $this->assertHtml($expected, $result);
  4943. }
  4944. /**
  4945. * testErrorsForBelongsToManySelect method
  4946. *
  4947. * Tests that errors for belongsToMany select fields are being
  4948. * picked up properly.
  4949. *
  4950. * @return void
  4951. */
  4952. public function testErrorsForBelongsToManySelect()
  4953. {
  4954. $spacecraft = [
  4955. 1 => 'Orion',
  4956. 2 => 'Helios'
  4957. ];
  4958. $this->View->viewVars['spacecraft'] = $spacecraft;
  4959. $article = new Article();
  4960. $article->errors('spacecraft', ['Invalid']);
  4961. $this->Form->create($article);
  4962. $result = $this->Form->control('spacecraft._ids');
  4963. $expected = [
  4964. ['div' => ['class' => 'input select error']],
  4965. 'label' => ['for' => 'spacecraft-ids'],
  4966. 'Spacecraft',
  4967. '/label',
  4968. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4969. 'select' => [
  4970. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4971. 'multiple' => 'multiple'
  4972. ],
  4973. ['option' => ['value' => '1']],
  4974. 'Orion',
  4975. '/option',
  4976. ['option' => ['value' => '2']],
  4977. 'Helios',
  4978. '/option',
  4979. '/select',
  4980. ['div' => ['class' => 'error-message']],
  4981. 'Invalid',
  4982. '/div',
  4983. '/div'
  4984. ];
  4985. $this->assertHtml($expected, $result);
  4986. }
  4987. /**
  4988. * testSelectMultipleCheckboxes method
  4989. *
  4990. * Test generation of multi select elements in checkbox format.
  4991. *
  4992. * @return void
  4993. */
  4994. public function testSelectMultipleCheckboxes()
  4995. {
  4996. $result = $this->Form->select(
  4997. 'Model.multi_field',
  4998. ['first', 'second', 'third'],
  4999. ['multiple' => 'checkbox']
  5000. );
  5001. $expected = [
  5002. 'input' => [
  5003. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5004. ],
  5005. ['div' => ['class' => 'checkbox']],
  5006. ['label' => ['for' => 'model-multi-field-0']],
  5007. ['input' => [
  5008. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5009. 'value' => '0', 'id' => 'model-multi-field-0'
  5010. ]],
  5011. 'first',
  5012. '/label',
  5013. '/div',
  5014. ['div' => ['class' => 'checkbox']],
  5015. ['label' => ['for' => 'model-multi-field-1']],
  5016. ['input' => [
  5017. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5018. 'value' => '1', 'id' => 'model-multi-field-1'
  5019. ]],
  5020. 'second',
  5021. '/label',
  5022. '/div',
  5023. ['div' => ['class' => 'checkbox']],
  5024. ['label' => ['for' => 'model-multi-field-2']],
  5025. ['input' => [
  5026. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5027. 'value' => '2', 'id' => 'model-multi-field-2'
  5028. ]],
  5029. 'third',
  5030. '/label',
  5031. '/div'
  5032. ];
  5033. $this->assertHtml($expected, $result);
  5034. $result = $this->Form->select(
  5035. 'Model.multi_field',
  5036. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5037. ['multiple' => 'checkbox']
  5038. );
  5039. $expected = [
  5040. 'input' => [
  5041. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5042. ],
  5043. ['div' => ['class' => 'checkbox']],
  5044. ['label' => ['for' => 'model-multi-field-a+']],
  5045. ['input' => [
  5046. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5047. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  5048. ]],
  5049. 'first',
  5050. '/label',
  5051. '/div',
  5052. ['div' => ['class' => 'checkbox']],
  5053. ['label' => ['for' => 'model-multi-field-a++']],
  5054. ['input' => [
  5055. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5056. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  5057. ]],
  5058. 'second',
  5059. '/label',
  5060. '/div',
  5061. ['div' => ['class' => 'checkbox']],
  5062. ['label' => ['for' => 'model-multi-field-a+++']],
  5063. ['input' => [
  5064. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5065. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  5066. ]],
  5067. 'third',
  5068. '/label',
  5069. '/div'
  5070. ];
  5071. $this->assertHtml($expected, $result);
  5072. $result = $this->Form->select(
  5073. 'Model.multi_field',
  5074. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5075. ['multiple' => 'checkbox']
  5076. );
  5077. $expected = [
  5078. 'input' => [
  5079. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5080. ],
  5081. ['div' => ['class' => 'checkbox']],
  5082. ['label' => ['for' => 'model-multi-field-a-b']],
  5083. ['input' => [
  5084. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5085. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5086. ]],
  5087. 'first',
  5088. '/label',
  5089. '/div',
  5090. ['div' => ['class' => 'checkbox']],
  5091. ['label' => ['for' => 'model-multi-field-a-b1']],
  5092. ['input' => [
  5093. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5094. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5095. ]],
  5096. 'second',
  5097. '/label',
  5098. '/div',
  5099. ['div' => ['class' => 'checkbox']],
  5100. ['label' => ['for' => 'model-multi-field-a-b2']],
  5101. ['input' => [
  5102. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5103. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5104. ]],
  5105. 'third',
  5106. '/label',
  5107. '/div'
  5108. ];
  5109. $this->assertHtml($expected, $result);
  5110. }
  5111. /**
  5112. * testSelectMultipleCheckboxRequestData method
  5113. *
  5114. * Ensure that multiCheckbox reads from the request data.
  5115. *
  5116. * @return void
  5117. */
  5118. public function testSelectMultipleCheckboxRequestData()
  5119. {
  5120. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  5121. $result = $this->Form->select(
  5122. 'Model.tags',
  5123. ['1' => 'first', 'Array' => 'Array'],
  5124. ['multiple' => 'checkbox']
  5125. );
  5126. $expected = [
  5127. 'input' => [
  5128. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5129. ],
  5130. ['div' => ['class' => 'checkbox']],
  5131. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5132. ['input' => [
  5133. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5134. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5135. ]],
  5136. 'first',
  5137. '/label',
  5138. '/div',
  5139. ['div' => ['class' => 'checkbox']],
  5140. ['label' => ['for' => 'model-tags-array']],
  5141. ['input' => [
  5142. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5143. 'value' => 'Array', 'id' => 'model-tags-array'
  5144. ]],
  5145. 'Array',
  5146. '/label',
  5147. '/div'
  5148. ];
  5149. $this->assertHtml($expected, $result);
  5150. }
  5151. /**
  5152. * testSelectMultipleCheckboxSecurity method
  5153. *
  5154. * Checks the security hash array generated for multiple-input checkbox elements.
  5155. *
  5156. * @return void
  5157. */
  5158. public function testSelectMultipleCheckboxSecurity()
  5159. {
  5160. $this->Form->request->params['_Token'] = 'testKey';
  5161. $this->assertEquals([], $this->Form->fields);
  5162. $this->Form->select(
  5163. 'Model.multi_field',
  5164. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5165. ['multiple' => 'checkbox']
  5166. );
  5167. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5168. $result = $this->Form->secure($this->Form->fields);
  5169. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  5170. $this->assertRegExp('/"' . $key . '"/', $result);
  5171. }
  5172. /**
  5173. * testSelectMultipleSecureWithNoOptions method
  5174. *
  5175. * Multiple select elements should always be secured as they always participate
  5176. * in the POST data.
  5177. *
  5178. * @return void
  5179. */
  5180. public function testSelectMultipleSecureWithNoOptions()
  5181. {
  5182. $this->assertEquals([], $this->Form->fields);
  5183. $this->Form->select(
  5184. 'Model.select',
  5185. [],
  5186. ['multiple' => true]
  5187. );
  5188. $this->assertEquals(['Model.select'], $this->Form->fields);
  5189. }
  5190. /**
  5191. * testSelectNoSecureWithNoOptions method
  5192. *
  5193. * When a select box has no options it should not be added to the fields list
  5194. * as it always fail post validation.
  5195. *
  5196. * @return void
  5197. */
  5198. public function testSelectNoSecureWithNoOptions()
  5199. {
  5200. $this->Form->request->params['_Token'] = 'testkey';
  5201. $this->assertEquals([], $this->Form->fields);
  5202. $this->Form->select(
  5203. 'Model.select',
  5204. []
  5205. );
  5206. $this->assertEquals([], $this->Form->fields);
  5207. $this->Form->select(
  5208. 'Model.user_id',
  5209. [],
  5210. ['empty' => true]
  5211. );
  5212. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5213. }
  5214. /**
  5215. * testControlMultipleCheckboxes method
  5216. *
  5217. * Test control() resulting in multi select elements being generated.
  5218. *
  5219. * @return void
  5220. */
  5221. public function testControlMultipleCheckboxes()
  5222. {
  5223. $result = $this->Form->control('Model.multi_field', [
  5224. 'options' => ['first', 'second', 'third'],
  5225. 'multiple' => 'checkbox'
  5226. ]);
  5227. $expected = [
  5228. ['div' => ['class' => 'input select']],
  5229. ['label' => ['for' => 'model-multi-field']],
  5230. 'Multi Field',
  5231. '/label',
  5232. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5233. ['div' => ['class' => 'checkbox']],
  5234. ['label' => ['for' => 'model-multi-field-0']],
  5235. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5236. 'first',
  5237. '/label',
  5238. '/div',
  5239. ['div' => ['class' => 'checkbox']],
  5240. ['label' => ['for' => 'model-multi-field-1']],
  5241. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5242. 'second',
  5243. '/label',
  5244. '/div',
  5245. ['div' => ['class' => 'checkbox']],
  5246. ['label' => ['for' => 'model-multi-field-2']],
  5247. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5248. 'third',
  5249. '/label',
  5250. '/div',
  5251. '/div'
  5252. ];
  5253. $this->assertHtml($expected, $result);
  5254. $result = $this->Form->control('Model.multi_field', [
  5255. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5256. 'multiple' => 'checkbox'
  5257. ]);
  5258. $expected = [
  5259. ['div' => ['class' => 'input select']],
  5260. ['label' => ['for' => 'model-multi-field']],
  5261. 'Multi Field',
  5262. '/label',
  5263. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5264. ['div' => ['class' => 'checkbox']],
  5265. ['label' => ['for' => 'model-multi-field-a']],
  5266. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5267. 'first',
  5268. '/label',
  5269. '/div',
  5270. ['div' => ['class' => 'checkbox']],
  5271. ['label' => ['for' => 'model-multi-field-b']],
  5272. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5273. 'second',
  5274. '/label',
  5275. '/div',
  5276. ['div' => ['class' => 'checkbox']],
  5277. ['label' => ['for' => 'model-multi-field-c']],
  5278. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5279. 'third',
  5280. '/label',
  5281. '/div',
  5282. '/div'
  5283. ];
  5284. $this->assertHtml($expected, $result);
  5285. }
  5286. /**
  5287. * testSelectHiddenFieldOmission method
  5288. *
  5289. * Test that select() with 'hiddenField' => false omits the hidden field.
  5290. *
  5291. * @return void
  5292. */
  5293. public function testSelectHiddenFieldOmission()
  5294. {
  5295. $result = $this->Form->select(
  5296. 'Model.multi_field',
  5297. ['first', 'second'],
  5298. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5299. );
  5300. $this->assertNotContains('type="hidden"', $result);
  5301. }
  5302. /**
  5303. * testSelectCheckboxMultipleOverrideName method
  5304. *
  5305. * Test that select() with multiple = checkbox works with overriding name attribute.
  5306. *
  5307. * @return void
  5308. */
  5309. public function testSelectCheckboxMultipleOverrideName()
  5310. {
  5311. $result = $this->Form->select('category', ['1', '2'], [
  5312. 'multiple' => 'checkbox',
  5313. 'name' => 'fish',
  5314. ]);
  5315. $expected = [
  5316. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5317. ['div' => ['class' => 'checkbox']],
  5318. ['label' => ['for' => 'fish-0']],
  5319. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5320. '1',
  5321. '/label',
  5322. '/div',
  5323. ['div' => ['class' => 'checkbox']],
  5324. ['label' => ['for' => 'fish-1']],
  5325. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5326. '2',
  5327. '/label',
  5328. '/div'
  5329. ];
  5330. $this->assertHtml($expected, $result);
  5331. $result = $this->Form->multiCheckbox(
  5332. 'category',
  5333. new Collection(['1', '2']),
  5334. ['name' => 'fish']
  5335. );
  5336. $this->assertHtml($expected, $result);
  5337. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5338. 'name' => 'fish',
  5339. ]);
  5340. $this->assertHtml($expected, $result);
  5341. }
  5342. /**
  5343. * testControlMultiCheckbox method
  5344. *
  5345. * Test that control() works with multicheckbox.
  5346. *
  5347. * @return void
  5348. */
  5349. public function testControlMultiCheckbox()
  5350. {
  5351. $result = $this->Form->control('category', [
  5352. 'type' => 'multicheckbox',
  5353. 'options' => ['1', '2'],
  5354. ]);
  5355. $expected = [
  5356. ['div' => ['class' => 'input multicheckbox']],
  5357. '<label',
  5358. 'Category',
  5359. '/label',
  5360. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5361. ['div' => ['class' => 'checkbox']],
  5362. ['label' => ['for' => 'category-0']],
  5363. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5364. '1',
  5365. '/label',
  5366. '/div',
  5367. ['div' => ['class' => 'checkbox']],
  5368. ['label' => ['for' => 'category-1']],
  5369. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5370. '2',
  5371. '/label',
  5372. '/div',
  5373. '/div',
  5374. ];
  5375. $this->assertHtml($expected, $result);
  5376. }
  5377. /**
  5378. * testCheckbox method
  5379. *
  5380. * Test generation of checkboxes.
  5381. *
  5382. * @return void
  5383. */
  5384. public function testCheckbox()
  5385. {
  5386. $result = $this->Form->checkbox('Model.field');
  5387. $expected = [
  5388. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5389. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5390. ];
  5391. $this->assertHtml($expected, $result);
  5392. $result = $this->Form->checkbox('Model.field', [
  5393. 'id' => 'theID',
  5394. 'value' => 'myvalue',
  5395. 'form' => 'my-form',
  5396. ]);
  5397. $expected = [
  5398. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5399. ['input' => [
  5400. 'type' => 'checkbox', 'name' => 'Model[field]',
  5401. 'value' => 'myvalue', 'id' => 'theID',
  5402. 'form' => 'my-form',
  5403. ]]
  5404. ];
  5405. $this->assertHtml($expected, $result);
  5406. }
  5407. /**
  5408. * testCheckboxDefaultValue method
  5409. *
  5410. * Test default value setting on checkbox() method.
  5411. *
  5412. * @return void
  5413. */
  5414. public function testCheckboxDefaultValue()
  5415. {
  5416. $this->Form->request->data['Model']['field'] = false;
  5417. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5418. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5419. $this->assertHtml($expected, $result);
  5420. unset($this->Form->request->data['Model']['field']);
  5421. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5422. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5423. $this->assertHtml($expected, $result);
  5424. $this->Form->request->data['Model']['field'] = true;
  5425. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5426. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5427. $this->assertHtml($expected, $result);
  5428. unset($this->Form->request->data['Model']['field']);
  5429. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5430. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5431. $this->assertHtml($expected, $result);
  5432. $Articles = TableRegistry::get('Articles');
  5433. $Articles->schema()->addColumn(
  5434. 'published',
  5435. ['type' => 'boolean', 'null' => false, 'default' => true]
  5436. );
  5437. $this->Form->create($Articles->newEntity());
  5438. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5439. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5440. $this->assertHtml($expected, $result);
  5441. }
  5442. /**
  5443. * testCheckboxCheckedAndError method
  5444. *
  5445. * Test checkbox being checked or having errors.
  5446. *
  5447. * @return void
  5448. */
  5449. public function testCheckboxCheckedAndError()
  5450. {
  5451. $this->article['errors'] = [
  5452. 'published' => true
  5453. ];
  5454. $this->Form->request->data['published'] = 'myvalue';
  5455. $this->Form->create($this->article);
  5456. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5457. $expected = [
  5458. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5459. ['input' => [
  5460. 'type' => 'checkbox',
  5461. 'name' => 'published',
  5462. 'value' => 'myvalue',
  5463. 'id' => 'theID',
  5464. 'checked' => 'checked',
  5465. 'class' => 'form-error'
  5466. ]]
  5467. ];
  5468. $this->assertHtml($expected, $result);
  5469. $this->Form->request->data['published'] = '';
  5470. $result = $this->Form->checkbox('published');
  5471. $expected = [
  5472. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5473. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5474. ];
  5475. $this->assertHtml($expected, $result);
  5476. }
  5477. /**
  5478. * testCheckboxCustomNameAttribute method
  5479. *
  5480. * Test checkbox() with a custom name attribute.
  5481. *
  5482. * @return void
  5483. */
  5484. public function testCheckboxCustomNameAttribute()
  5485. {
  5486. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5487. $expected = [
  5488. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5489. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5490. ];
  5491. $this->assertHtml($expected, $result);
  5492. }
  5493. /**
  5494. * testCheckboxHiddenField method
  5495. *
  5496. * Test that the hidden input for checkboxes can be omitted or set to a
  5497. * specific value.
  5498. *
  5499. * @return void
  5500. */
  5501. public function testCheckboxHiddenField()
  5502. {
  5503. $result = $this->Form->checkbox('UserForm.something', [
  5504. 'hiddenField' => false
  5505. ]);
  5506. $expected = [
  5507. 'input' => [
  5508. 'type' => 'checkbox',
  5509. 'name' => 'UserForm[something]',
  5510. 'value' => '1'
  5511. ],
  5512. ];
  5513. $this->assertHtml($expected, $result);
  5514. $result = $this->Form->checkbox('UserForm.something', [
  5515. 'value' => 'Y',
  5516. 'hiddenField' => 'N',
  5517. ]);
  5518. $expected = [
  5519. ['input' => [
  5520. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5521. 'value' => 'N'
  5522. ]],
  5523. ['input' => [
  5524. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5525. 'value' => 'Y'
  5526. ]],
  5527. ];
  5528. $this->assertHtml($expected, $result);
  5529. }
  5530. /**
  5531. * testTime method
  5532. *
  5533. * Test the time type.
  5534. *
  5535. * @return void
  5536. */
  5537. public function testTime()
  5538. {
  5539. $result = $this->Form->time('start_time', [
  5540. 'timeFormat' => 12,
  5541. 'interval' => 5,
  5542. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5543. ]);
  5544. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5545. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5546. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5547. $this->assertNotContains('year', $result);
  5548. $this->assertNotContains('month', $result);
  5549. $this->assertNotContains('day', $result);
  5550. $result = $this->Form->time('start_time', [
  5551. 'timeFormat' => 12,
  5552. 'interval' => 5,
  5553. 'value' => '2014-03-08 16:30:00'
  5554. ]);
  5555. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5556. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5557. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5558. $this->assertNotContains('year', $result);
  5559. $this->assertNotContains('month', $result);
  5560. $this->assertNotContains('day', $result);
  5561. }
  5562. /**
  5563. * testTimeFormat24NoMeridian method
  5564. *
  5565. * Ensure that timeFormat=24 has no merdian.
  5566. *
  5567. * @return void.
  5568. */
  5569. public function testTimeFormat24NoMeridian()
  5570. {
  5571. $result = $this->Form->time('start_time', [
  5572. 'timeFormat' => 24,
  5573. 'interval' => 5,
  5574. 'value' => '2014-03-08 16:30:00'
  5575. ]);
  5576. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5577. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5578. $this->assertNotContains('meridian', $result);
  5579. $this->assertNotContains('pm', $result);
  5580. $this->assertNotContains('year', $result);
  5581. $this->assertNotContains('month', $result);
  5582. $this->assertNotContains('day', $result);
  5583. }
  5584. /**
  5585. * testDate method
  5586. *
  5587. * Test the date type.
  5588. *
  5589. * @return void
  5590. */
  5591. public function testDate()
  5592. {
  5593. $result = $this->Form->date('start_day', [
  5594. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5595. ]);
  5596. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5597. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5598. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5599. $this->assertNotContains('hour', $result);
  5600. $this->assertNotContains('minute', $result);
  5601. $this->assertNotContains('second', $result);
  5602. $this->assertNotContains('meridian', $result);
  5603. }
  5604. /**
  5605. * testDateTime method
  5606. *
  5607. * Test generation of date/time select elements.
  5608. *
  5609. * @return void
  5610. */
  5611. public function testDateTime()
  5612. {
  5613. extract($this->dateRegex);
  5614. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5615. $now = strtotime('now');
  5616. $expected = [
  5617. ['select' => ['name' => 'Contact[date][year]']],
  5618. ['option' => ['value' => '']],
  5619. '/option',
  5620. $yearsRegex,
  5621. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5622. date('Y', $now),
  5623. '/option',
  5624. '*/select',
  5625. ['select' => ['name' => 'Contact[date][month]']],
  5626. ['option' => ['value' => '']],
  5627. '/option',
  5628. $monthsRegex,
  5629. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5630. date('F', $now),
  5631. '/option',
  5632. '*/select',
  5633. ['select' => ['name' => 'Contact[date][day]']],
  5634. ['option' => ['value' => '']],
  5635. '/option',
  5636. $daysRegex,
  5637. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5638. date('j', $now),
  5639. '/option',
  5640. '*/select',
  5641. ['select' => ['name' => 'Contact[date][hour]']],
  5642. ['option' => ['value' => '']],
  5643. '/option',
  5644. $hoursRegex,
  5645. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5646. date('G', $now),
  5647. '/option',
  5648. '*/select',
  5649. ['select' => ['name' => 'Contact[date][minute]']],
  5650. ['option' => ['value' => '']],
  5651. '/option',
  5652. $minutesRegex,
  5653. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5654. date('i', $now),
  5655. '/option',
  5656. '*/select',
  5657. ];
  5658. $this->assertHtml($expected, $result);
  5659. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5660. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5661. $now = strtotime('now');
  5662. $expected = [
  5663. ['select' => ['name' => 'Contact[date][year]']],
  5664. $yearsRegex,
  5665. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5666. date('Y', $now),
  5667. '/option',
  5668. '*/select',
  5669. ['select' => ['name' => 'Contact[date][month]']],
  5670. $monthsRegex,
  5671. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5672. date('F', $now),
  5673. '/option',
  5674. '*/select',
  5675. ['select' => ['name' => 'Contact[date][day]']],
  5676. $daysRegex,
  5677. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5678. date('j', $now),
  5679. '/option',
  5680. '*/select',
  5681. ['select' => ['name' => 'Contact[date][hour]']],
  5682. $hoursRegex,
  5683. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5684. date('G', $now),
  5685. '/option',
  5686. '*/select',
  5687. ['select' => ['name' => 'Contact[date][minute]']],
  5688. $minutesRegex,
  5689. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5690. date('i', $now),
  5691. '/option',
  5692. '*/select',
  5693. ];
  5694. $this->assertHtml($expected, $result);
  5695. }
  5696. /**
  5697. * testDateTimeSecured method
  5698. *
  5699. * Test that datetime fields are added to protected fields list.
  5700. *
  5701. * @return void
  5702. */
  5703. public function testDateTimeSecured()
  5704. {
  5705. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5706. $this->Form->dateTime('Contact.date');
  5707. $expected = [
  5708. 'Contact.date.year',
  5709. 'Contact.date.month',
  5710. 'Contact.date.day',
  5711. 'Contact.date.hour',
  5712. 'Contact.date.minute',
  5713. ];
  5714. $this->assertEquals($expected, $this->Form->fields);
  5715. $this->Form->fields = [];
  5716. $this->Form->date('Contact.published');
  5717. $expected = [
  5718. 'Contact.published.year',
  5719. 'Contact.published.month',
  5720. 'Contact.published.day',
  5721. ];
  5722. $this->assertEquals($expected, $this->Form->fields);
  5723. }
  5724. /**
  5725. * testDateTimeSecuredDisabled method
  5726. *
  5727. * Test that datetime fields are added to protected fields list.
  5728. *
  5729. * @return void
  5730. */
  5731. public function testDateTimeSecuredDisabled()
  5732. {
  5733. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5734. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5735. $expected = [];
  5736. $this->assertEquals($expected, $this->Form->fields);
  5737. $this->Form->fields = [];
  5738. $this->Form->date('Contact.published', ['secure' => false]);
  5739. $expected = [];
  5740. $this->assertEquals($expected, $this->Form->fields);
  5741. }
  5742. /**
  5743. * testDatetimeEmpty method
  5744. *
  5745. * Test empty defaulting to true for datetime.
  5746. *
  5747. * @return void
  5748. */
  5749. public function testDatetimeEmpty()
  5750. {
  5751. extract($this->dateRegex);
  5752. $result = $this->Form->dateTime('Contact.date', [
  5753. 'timeFormat' => 12,
  5754. 'empty' => true,
  5755. 'default' => true
  5756. ]);
  5757. $expected = [
  5758. ['select' => ['name' => 'Contact[date][year]']],
  5759. $yearsRegex,
  5760. ['option' => ['value' => '']],
  5761. '/option',
  5762. '*/select',
  5763. ['select' => ['name' => 'Contact[date][month]']],
  5764. $monthsRegex,
  5765. ['option' => ['value' => '']],
  5766. '/option',
  5767. '*/select',
  5768. ['select' => ['name' => 'Contact[date][day]']],
  5769. $daysRegex,
  5770. ['option' => ['value' => '']],
  5771. '/option',
  5772. '*/select',
  5773. ['select' => ['name' => 'Contact[date][hour]']],
  5774. $hoursRegex,
  5775. ['option' => ['value' => '']],
  5776. '/option',
  5777. '*/select',
  5778. ['select' => ['name' => 'Contact[date][minute]']],
  5779. $minutesRegex,
  5780. ['option' => ['value' => '']],
  5781. '/option',
  5782. '*/select',
  5783. ['select' => ['name' => 'Contact[date][meridian]']],
  5784. $meridianRegex,
  5785. ['option' => ['value' => '']],
  5786. '/option',
  5787. '*/select'
  5788. ];
  5789. $this->assertHtml($expected, $result);
  5790. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5791. }
  5792. /**
  5793. * testDatetimeMinuteInterval method
  5794. *
  5795. * Test datetime with interval option.
  5796. *
  5797. * @return void
  5798. */
  5799. public function testDatetimeMinuteInterval()
  5800. {
  5801. extract($this->dateRegex);
  5802. $result = $this->Form->dateTime('Contact.date', [
  5803. 'interval' => 5,
  5804. 'value' => ''
  5805. ]);
  5806. $expected = [
  5807. ['select' => ['name' => 'Contact[date][year]']],
  5808. $yearsRegex,
  5809. ['option' => ['selected' => 'selected', 'value' => '']],
  5810. '/option',
  5811. '*/select',
  5812. ['select' => ['name' => 'Contact[date][month]']],
  5813. $monthsRegex,
  5814. ['option' => ['selected' => 'selected', 'value' => '']],
  5815. '/option',
  5816. '*/select',
  5817. ['select' => ['name' => 'Contact[date][day]']],
  5818. $daysRegex,
  5819. ['option' => ['selected' => 'selected', 'value' => '']],
  5820. '/option',
  5821. '*/select',
  5822. ['select' => ['name' => 'Contact[date][hour]']],
  5823. $hoursRegex,
  5824. ['option' => ['selected' => 'selected', 'value' => '']],
  5825. '/option',
  5826. '*/select',
  5827. ['select' => ['name' => 'Contact[date][minute]']],
  5828. $minutesRegex,
  5829. ['option' => ['selected' => 'selected', 'value' => '']],
  5830. '/option',
  5831. ['option' => ['value' => '00']],
  5832. '00',
  5833. '/option',
  5834. ['option' => ['value' => '05']],
  5835. '05',
  5836. '/option',
  5837. ['option' => ['value' => '10']],
  5838. '10',
  5839. '/option',
  5840. '*/select',
  5841. ];
  5842. $this->assertHtml($expected, $result);
  5843. }
  5844. /**
  5845. * testDateTimeRounding method
  5846. *
  5847. * Test dateTime with rounding.
  5848. *
  5849. * @return void
  5850. */
  5851. public function testDateTimeRounding()
  5852. {
  5853. $this->Form->request->data['Contact'] = [
  5854. 'date' => [
  5855. 'day' => '13',
  5856. 'month' => '12',
  5857. 'year' => '2010',
  5858. 'hour' => '04',
  5859. 'minute' => '19',
  5860. 'meridian' => 'AM'
  5861. ]
  5862. ];
  5863. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5864. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5865. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5866. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5867. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5868. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5869. }
  5870. /**
  5871. * testDatetimeWithDefault method
  5872. *
  5873. * Test that datetime() and default values work.
  5874. *
  5875. * @return void
  5876. */
  5877. public function testDatetimeWithDefault()
  5878. {
  5879. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5880. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5881. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5882. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5883. $result = $this->Form->dateTime('Contact.updated', [
  5884. 'default' => '2009-06-01 11:15:30'
  5885. ]);
  5886. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5887. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5888. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5889. }
  5890. /**
  5891. * testDateTimeAllZeros method
  5892. *
  5893. * Test datetime() with all zeros.
  5894. *
  5895. * @return void
  5896. */
  5897. public function testDateTimeAllZeros()
  5898. {
  5899. $result = $this->Form->dateTime('Contact.date', [
  5900. 'timeFormat' => false,
  5901. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5902. 'value' => '0000-00-00'
  5903. ]);
  5904. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5905. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5906. }
  5907. /**
  5908. * testDateTimeEmptyAsArray method
  5909. *
  5910. * @return void
  5911. */
  5912. public function testDateTimeEmptyAsArray()
  5913. {
  5914. $result = $this->Form->dateTime('Contact.date', [
  5915. 'empty' => [
  5916. 'day' => 'DAY',
  5917. 'month' => 'MONTH',
  5918. 'year' => 'YEAR',
  5919. 'hour' => 'HOUR',
  5920. 'minute' => 'MINUTE',
  5921. 'meridian' => false
  5922. ],
  5923. 'default' => true
  5924. ]);
  5925. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5926. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5927. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5928. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5929. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5930. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5931. $result = $this->Form->dateTime('Contact.date', [
  5932. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5933. 'default' => true
  5934. ]);
  5935. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5936. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5937. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5938. }
  5939. /**
  5940. * testFormDateTimeMulti method
  5941. *
  5942. * Test multiple datetime element generation.
  5943. *
  5944. * @return void
  5945. */
  5946. public function testFormDateTimeMulti()
  5947. {
  5948. extract($this->dateRegex);
  5949. $result = $this->Form->dateTime('Contact.1.updated');
  5950. $this->assertContains('Contact[1][updated][month]', $result);
  5951. $this->assertContains('Contact[1][updated][day]', $result);
  5952. $this->assertContains('Contact[1][updated][year]', $result);
  5953. $this->assertContains('Contact[1][updated][hour]', $result);
  5954. $this->assertContains('Contact[1][updated][minute]', $result);
  5955. }
  5956. /**
  5957. * testDateTimeLabelIdMatchesFirstControl method
  5958. *
  5959. * When changing the date format, the label should always focus the first select box when
  5960. * clicked.
  5961. *
  5962. * @return void
  5963. */
  5964. public function testDateTimeLabelIdMatchesFirstControl()
  5965. {
  5966. $result = $this->Form->control('Model.date', ['type' => 'date']);
  5967. $this->assertContains('<label>Date</label>', $result);
  5968. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5969. $this->assertContains('<label>Date</label>', $result);
  5970. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5971. $this->assertContains('<label>Date</label>', $result);
  5972. }
  5973. /**
  5974. * testDateTimeSecondOptions method
  5975. *
  5976. * Test datetime second=true.
  5977. *
  5978. * @return void
  5979. */
  5980. public function testDateTimeSecondOptions()
  5981. {
  5982. $result = $this->Form->dateTime('updated', ['second' => true]);
  5983. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5984. $result = $this->Form->dateTime('updated', ['second' => []]);
  5985. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5986. $result = $this->Form->dateTime('updated', ['second' => null]);
  5987. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5988. $result = $this->Form->dateTime('updated', ['second' => false]);
  5989. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5990. }
  5991. /**
  5992. * testMonth method
  5993. *
  5994. * Test generation of a month input.
  5995. *
  5996. * @return void
  5997. */
  5998. public function testMonth()
  5999. {
  6000. $result = $this->Form->month('Model.field', ['value' => '']);
  6001. $expected = [
  6002. ['select' => ['name' => 'Model[field][month]']],
  6003. ['option' => ['value' => '', 'selected' => 'selected']],
  6004. '/option',
  6005. ['option' => ['value' => '01']],
  6006. date('F', strtotime('2008-01-01 00:00:00')),
  6007. '/option',
  6008. ['option' => ['value' => '02']],
  6009. date('F', strtotime('2008-02-01 00:00:00')),
  6010. '/option',
  6011. '*/select',
  6012. ];
  6013. $this->assertHtml($expected, $result);
  6014. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  6015. $expected = [
  6016. ['select' => ['name' => 'Model[field][month]']],
  6017. ['option' => ['selected' => 'selected', 'value' => '']],
  6018. '/option',
  6019. ['option' => ['value' => '01']],
  6020. date('F', strtotime('2008-01-01 00:00:00')),
  6021. '/option',
  6022. ['option' => ['value' => '02']],
  6023. date('F', strtotime('2008-02-01 00:00:00')),
  6024. '/option',
  6025. '*/select',
  6026. ];
  6027. $this->assertHtml($expected, $result);
  6028. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6029. $expected = [
  6030. ['select' => ['name' => 'Model[field][month]']],
  6031. ['option' => ['selected' => 'selected', 'value' => '']],
  6032. '/option',
  6033. ['option' => ['value' => '01']],
  6034. '1',
  6035. '/option',
  6036. ['option' => ['value' => '02']],
  6037. '2',
  6038. '/option',
  6039. '*/select',
  6040. ];
  6041. $this->assertHtml($expected, $result);
  6042. $monthNames = [
  6043. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6044. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  6045. ];
  6046. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6047. $expected = [
  6048. ['select' => ['name' => 'Model[field][month]']],
  6049. ['option' => ['value' => '']],
  6050. '/option',
  6051. ['option' => ['value' => '01', 'selected' => 'selected']],
  6052. 'Jan',
  6053. '/option',
  6054. ['option' => ['value' => '02']],
  6055. 'Feb',
  6056. '/option',
  6057. '*/select',
  6058. ];
  6059. $this->assertHtml($expected, $result);
  6060. $this->Form->request->data['Project']['release'] = '2050-02-10';
  6061. $result = $this->Form->month('Project.release');
  6062. $expected = [
  6063. ['select' => ['name' => 'Project[release][month]']],
  6064. ['option' => ['value' => '']],
  6065. '/option',
  6066. ['option' => ['value' => '01']],
  6067. 'January',
  6068. '/option',
  6069. ['option' => ['value' => '02', 'selected' => 'selected']],
  6070. 'February',
  6071. '/option',
  6072. '*/select',
  6073. ];
  6074. $this->assertHtml($expected, $result);
  6075. $result = $this->Form->month('Contact.published', [
  6076. 'empty' => 'Published on',
  6077. ]);
  6078. $this->assertContains('Published on', $result);
  6079. }
  6080. /**
  6081. * testDay method
  6082. *
  6083. * Test generation of a day input.
  6084. *
  6085. * @return void
  6086. */
  6087. public function testDay()
  6088. {
  6089. extract($this->dateRegex);
  6090. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6091. $expected = [
  6092. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6093. ['option' => ['selected' => 'selected', 'value' => '']],
  6094. '/option',
  6095. ['option' => ['value' => '01']],
  6096. '1',
  6097. '/option',
  6098. ['option' => ['value' => '02']],
  6099. '2',
  6100. '/option',
  6101. $daysRegex,
  6102. '/select',
  6103. ];
  6104. $this->assertHtml($expected, $result);
  6105. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6106. $result = $this->Form->day('Model.field');
  6107. $expected = [
  6108. ['select' => ['name' => 'Model[field][day]']],
  6109. ['option' => ['value' => '']],
  6110. '/option',
  6111. ['option' => ['value' => '01']],
  6112. '1',
  6113. '/option',
  6114. ['option' => ['value' => '02']],
  6115. '2',
  6116. '/option',
  6117. $daysRegex,
  6118. ['option' => ['value' => '10', 'selected' => 'selected']],
  6119. '10',
  6120. '/option',
  6121. $daysRegex,
  6122. '/select',
  6123. ];
  6124. $this->assertHtml($expected, $result);
  6125. $this->Form->request->data['Model']['field'] = '';
  6126. $result = $this->Form->day('Model.field', ['value' => '10']);
  6127. $expected = [
  6128. ['select' => ['name' => 'Model[field][day]']],
  6129. ['option' => ['value' => '']],
  6130. '/option',
  6131. ['option' => ['value' => '01']],
  6132. '1',
  6133. '/option',
  6134. ['option' => ['value' => '02']],
  6135. '2',
  6136. '/option',
  6137. $daysRegex,
  6138. ['option' => ['value' => '10', 'selected' => 'selected']],
  6139. '10',
  6140. '/option',
  6141. $daysRegex,
  6142. '/select',
  6143. ];
  6144. $this->assertHtml($expected, $result);
  6145. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6146. $result = $this->Form->day('Project.release');
  6147. $expected = [
  6148. ['select' => ['name' => 'Project[release][day]']],
  6149. ['option' => ['value' => '']],
  6150. '/option',
  6151. ['option' => ['value' => '01']],
  6152. '1',
  6153. '/option',
  6154. ['option' => ['value' => '02']],
  6155. '2',
  6156. '/option',
  6157. $daysRegex,
  6158. ['option' => ['value' => '10', 'selected' => 'selected']],
  6159. '10',
  6160. '/option',
  6161. $daysRegex,
  6162. '/select',
  6163. ];
  6164. $this->assertHtml($expected, $result);
  6165. $result = $this->Form->day('Contact.published', [
  6166. 'empty' => 'Published on',
  6167. ]);
  6168. $this->assertContains('Published on', $result);
  6169. }
  6170. /**
  6171. * testMinute method
  6172. *
  6173. * Test generation of a minute input.
  6174. *
  6175. * @return void
  6176. */
  6177. public function testMinute()
  6178. {
  6179. extract($this->dateRegex);
  6180. $result = $this->Form->minute('Model.field', ['value' => '']);
  6181. $expected = [
  6182. ['select' => ['name' => 'Model[field][minute]']],
  6183. ['option' => ['selected' => 'selected', 'value' => '']],
  6184. '/option',
  6185. ['option' => ['value' => '00']],
  6186. '00',
  6187. '/option',
  6188. ['option' => ['value' => '01']],
  6189. '01',
  6190. '/option',
  6191. ['option' => ['value' => '02']],
  6192. '02',
  6193. '/option',
  6194. $minutesRegex,
  6195. '/select',
  6196. ];
  6197. $this->assertHtml($expected, $result);
  6198. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6199. $result = $this->Form->minute('Model.field');
  6200. $expected = [
  6201. ['select' => ['name' => 'Model[field][minute]']],
  6202. ['option' => ['value' => '']],
  6203. '/option',
  6204. ['option' => ['value' => '00']],
  6205. '00',
  6206. '/option',
  6207. ['option' => ['value' => '01']],
  6208. '01',
  6209. '/option',
  6210. ['option' => ['value' => '02']],
  6211. '02',
  6212. '/option',
  6213. $minutesRegex,
  6214. ['option' => ['value' => '12', 'selected' => 'selected']],
  6215. '12',
  6216. '/option',
  6217. $minutesRegex,
  6218. '/select',
  6219. ];
  6220. $this->assertHtml($expected, $result);
  6221. $this->Form->request->data['Model']['field'] = '';
  6222. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6223. $expected = [
  6224. ['select' => ['name' => 'Model[field][minute]']],
  6225. ['option' => ['selected' => 'selected', 'value' => '']],
  6226. '/option',
  6227. ['option' => ['value' => '00']],
  6228. '00',
  6229. '/option',
  6230. ['option' => ['value' => '05']],
  6231. '05',
  6232. '/option',
  6233. ['option' => ['value' => '10']],
  6234. '10',
  6235. '/option',
  6236. $minutesRegex,
  6237. '/select',
  6238. ];
  6239. $this->assertHtml($expected, $result);
  6240. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6241. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6242. $expected = [
  6243. ['select' => ['name' => 'Model[field][minute]']],
  6244. ['option' => ['value' => '']],
  6245. '/option',
  6246. ['option' => ['value' => '00']],
  6247. '00',
  6248. '/option',
  6249. ['option' => ['value' => '05']],
  6250. '05',
  6251. '/option',
  6252. ['option' => ['value' => '10', 'selected' => 'selected']],
  6253. '10',
  6254. '/option',
  6255. $minutesRegex,
  6256. '/select',
  6257. ];
  6258. $this->assertHtml($expected, $result);
  6259. }
  6260. /**
  6261. * testMeridian method
  6262. *
  6263. * Test generating an input for the meridian.
  6264. *
  6265. * @return void
  6266. */
  6267. public function testMeridian()
  6268. {
  6269. extract($this->dateRegex);
  6270. $now = new \DateTime();
  6271. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6272. $expected = [
  6273. ['select' => ['name' => 'Model[field][meridian]']],
  6274. ['option' => ['value' => '']],
  6275. '/option',
  6276. $meridianRegex,
  6277. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6278. $now->format('a'),
  6279. '/option',
  6280. '*/select'
  6281. ];
  6282. $this->assertHtml($expected, $result);
  6283. }
  6284. /**
  6285. * testHour method
  6286. *
  6287. * Test generation of an hour input.
  6288. *
  6289. * @return void
  6290. */
  6291. public function testHour()
  6292. {
  6293. extract($this->dateRegex);
  6294. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6295. $expected = [
  6296. ['select' => ['name' => 'Model[field][hour]']],
  6297. ['option' => ['selected' => 'selected', 'value' => '']],
  6298. '/option',
  6299. ['option' => ['value' => '01']],
  6300. '1',
  6301. '/option',
  6302. ['option' => ['value' => '02']],
  6303. '2',
  6304. '/option',
  6305. $hoursRegex,
  6306. '/select',
  6307. ];
  6308. $this->assertHtml($expected, $result);
  6309. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6310. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6311. $expected = [
  6312. ['select' => ['name' => 'Model[field][hour]']],
  6313. ['option' => ['value' => '']],
  6314. '/option',
  6315. ['option' => ['value' => '01']],
  6316. '1',
  6317. '/option',
  6318. ['option' => ['value' => '02']],
  6319. '2',
  6320. '/option',
  6321. $hoursRegex,
  6322. ['option' => ['value' => '12', 'selected' => 'selected']],
  6323. '12',
  6324. '/option',
  6325. '/select',
  6326. ];
  6327. $this->assertHtml($expected, $result);
  6328. $this->Form->request->data['Model']['field'] = '';
  6329. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6330. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6331. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6332. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6333. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6334. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6335. $expected = [
  6336. ['select' => ['name' => 'Model[field][hour]']],
  6337. ['option' => ['value' => '']],
  6338. '/option',
  6339. ['option' => ['value' => '00', 'selected' => 'selected']],
  6340. '0',
  6341. '/option',
  6342. ['option' => ['value' => '01']],
  6343. '1',
  6344. '/option',
  6345. ['option' => ['value' => '02']],
  6346. '2',
  6347. '/option',
  6348. $hoursRegex,
  6349. '/select',
  6350. ];
  6351. $this->assertHtml($expected, $result);
  6352. unset($this->Form->request->data['Model']['field']);
  6353. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6354. $thisHour = date('H');
  6355. $optValue = date('G');
  6356. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6357. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6358. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6359. $expected = [
  6360. ['select' => ['name' => 'Model[field][hour]']],
  6361. ['option' => ['value' => '']],
  6362. '/option',
  6363. ['option' => ['value' => '00']],
  6364. '0',
  6365. '/option',
  6366. ['option' => ['value' => '01', 'selected' => 'selected']],
  6367. '1',
  6368. '/option',
  6369. ['option' => ['value' => '02']],
  6370. '2',
  6371. '/option',
  6372. $hoursRegex,
  6373. '/select',
  6374. ];
  6375. $this->assertHtml($expected, $result);
  6376. }
  6377. /**
  6378. * testYear method
  6379. *
  6380. * Test generation of a year input.
  6381. *
  6382. * @return void
  6383. */
  6384. public function testYear()
  6385. {
  6386. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6387. $expected = [
  6388. ['select' => ['name' => 'Model[field][year]']],
  6389. ['option' => ['selected' => 'selected', 'value' => '']],
  6390. '/option',
  6391. ['option' => ['value' => '2007']],
  6392. '2007',
  6393. '/option',
  6394. ['option' => ['value' => '2006']],
  6395. '2006',
  6396. '/option',
  6397. '/select',
  6398. ];
  6399. $this->assertHtml($expected, $result);
  6400. $result = $this->Form->year('Model.field', [
  6401. 'value' => '',
  6402. 'minYear' => 2006,
  6403. 'maxYear' => 2007,
  6404. 'orderYear' => 'asc'
  6405. ]);
  6406. $expected = [
  6407. ['select' => ['name' => 'Model[field][year]']],
  6408. ['option' => ['selected' => 'selected', 'value' => '']],
  6409. '/option',
  6410. ['option' => ['value' => '2006']],
  6411. '2006',
  6412. '/option',
  6413. ['option' => ['value' => '2007']],
  6414. '2007',
  6415. '/option',
  6416. '/select',
  6417. ];
  6418. $this->assertHtml($expected, $result);
  6419. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6420. $result = $this->Form->year('Contact.published', [
  6421. 'empty' => false,
  6422. 'minYear' => 2006,
  6423. 'maxYear' => 2007,
  6424. ]);
  6425. $expected = [
  6426. ['select' => ['name' => 'Contact[published][year]']],
  6427. ['option' => ['value' => '2007']],
  6428. '2007',
  6429. '/option',
  6430. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6431. '2006',
  6432. '/option',
  6433. '/select',
  6434. ];
  6435. $this->assertHtml($expected, $result);
  6436. $result = $this->Form->year('Contact.published', [
  6437. 'empty' => 'Published on',
  6438. ]);
  6439. $this->assertContains('Published on', $result);
  6440. }
  6441. /**
  6442. * testControlDatetimePreEpoch method
  6443. *
  6444. * Test minYear being prior to the unix epoch.
  6445. *
  6446. * @return void
  6447. */
  6448. public function testControlDatetimePreEpoch()
  6449. {
  6450. $start = date('Y') - 80;
  6451. $end = date('Y') - 18;
  6452. $result = $this->Form->control('birth_year', [
  6453. 'type' => 'date',
  6454. 'label' => 'Birth Year',
  6455. 'minYear' => $start,
  6456. 'maxYear' => $end,
  6457. 'month' => false,
  6458. 'day' => false,
  6459. ]);
  6460. $this->assertContains('value="' . $start . '">' . $start, $result);
  6461. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6462. $this->assertNotContains('value="00">00', $result);
  6463. }
  6464. /**
  6465. * test control() datetime & required attributes
  6466. *
  6467. * @return void
  6468. */
  6469. public function testControlDatetimeRequired()
  6470. {
  6471. $result = $this->Form->control('birthday', [
  6472. 'type' => 'date',
  6473. 'required' => true
  6474. ]);
  6475. $this->assertContains(
  6476. '<select name="birthday[year]" required="required"',
  6477. $result
  6478. );
  6479. }
  6480. /**
  6481. * testYearAutoExpandRange method
  6482. *
  6483. * @return void
  6484. */
  6485. public function testYearAutoExpandRange()
  6486. {
  6487. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6488. $result = $this->Form->year('User.birthday');
  6489. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6490. $result = $matches[1];
  6491. $expected = range(date('Y') + 5, 1930);
  6492. $this->assertEquals($expected, $result);
  6493. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6494. $result = $this->Form->year('Project.release');
  6495. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6496. $result = $matches[1];
  6497. $expected = range(2050, date('Y') - 5);
  6498. $this->assertEquals($expected, $result);
  6499. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6500. $result = $this->Form->year('Project.release', [
  6501. 'minYear' => 1890,
  6502. 'maxYear' => 1900
  6503. ]);
  6504. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6505. $result = $matches[1];
  6506. $expected = range(1900, 1881);
  6507. $this->assertEquals($expected, $result);
  6508. }
  6509. /**
  6510. * testControlDate method
  6511. *
  6512. * Test that control() accepts the type of date and passes options in.
  6513. *
  6514. * @return void
  6515. */
  6516. public function testControlDate()
  6517. {
  6518. $this->Form->request->data = [
  6519. 'month_year' => ['month' => date('m')],
  6520. ];
  6521. $this->Form->create($this->article);
  6522. $result = $this->Form->control('month_year', [
  6523. 'label' => false,
  6524. 'type' => 'date',
  6525. 'minYear' => 2006,
  6526. 'maxYear' => 2008
  6527. ]);
  6528. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6529. $this->assertNotContains('value="2008" selected="selected"', $result);
  6530. }
  6531. /**
  6532. * testControlLabelFalse method
  6533. *
  6534. * Test the label option being set to false.
  6535. *
  6536. * @return void
  6537. */
  6538. public function testControlLabelFalse()
  6539. {
  6540. $this->Form->create($this->article);
  6541. $result = $this->Form->control('title', ['label' => false]);
  6542. $expected = [
  6543. 'div' => ['class' => 'input text required'],
  6544. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6545. '/div'
  6546. ];
  6547. $this->assertHtml($expected, $result);
  6548. }
  6549. /**
  6550. * testControlDateMaxYear method
  6551. *
  6552. * Let's say we want to only allow users born from 2006 to 2008 to register
  6553. * This being the first signup page, we still don't have any data.
  6554. *
  6555. * @return void
  6556. */
  6557. public function testControlDateMaxYear()
  6558. {
  6559. $this->Form->request->data = [];
  6560. $this->Form->create($this->article);
  6561. $result = $this->Form->control('birthday', [
  6562. 'label' => false,
  6563. 'type' => 'date',
  6564. 'minYear' => 2006,
  6565. 'maxYear' => 2008,
  6566. 'default' => true
  6567. ]);
  6568. $this->assertContains('value="2008" selected="selected"', $result);
  6569. $this->assertContains('value="2006"', $result);
  6570. $this->assertNotContains('value="2005"', $result);
  6571. $this->assertNotContains('value="2009"', $result);
  6572. }
  6573. /**
  6574. * testTextArea method
  6575. *
  6576. * Test generation of a textarea input.
  6577. *
  6578. * @return void
  6579. */
  6580. public function testTextArea()
  6581. {
  6582. $this->Form->request->data = ['field' => 'some test data'];
  6583. $result = $this->Form->textarea('field');
  6584. $expected = [
  6585. 'textarea' => ['name' => 'field', 'rows' => 5],
  6586. 'some test data',
  6587. '/textarea',
  6588. ];
  6589. $this->assertHtml($expected, $result);
  6590. $result = $this->Form->textarea('user.bio');
  6591. $expected = [
  6592. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6593. '/textarea',
  6594. ];
  6595. $this->assertHtml($expected, $result);
  6596. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6597. $result = $this->Form->textarea('field');
  6598. $expected = [
  6599. 'textarea' => ['name' => 'field', 'rows' => 5],
  6600. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6601. '/textarea',
  6602. ];
  6603. $this->assertHtml($expected, $result);
  6604. $this->Form->request->data = [
  6605. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6606. ];
  6607. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6608. $expected = [
  6609. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6610. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6611. '/textarea',
  6612. ];
  6613. $this->assertHtml($expected, $result);
  6614. $result = $this->Form->textarea('0.OtherModel.field');
  6615. $expected = [
  6616. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6617. '/textarea'
  6618. ];
  6619. $this->assertHtml($expected, $result);
  6620. }
  6621. /**
  6622. * testTextAreaWithStupidCharacters method
  6623. *
  6624. * Test text area with non-ascii characters.
  6625. *
  6626. * @return void
  6627. */
  6628. public function testTextAreaWithStupidCharacters()
  6629. {
  6630. $result = $this->Form->textarea('Post.content', [
  6631. 'value' => 'GREAT®',
  6632. 'rows' => '15',
  6633. 'cols' => '75'
  6634. ]);
  6635. $expected = [
  6636. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6637. 'GREAT®',
  6638. '/textarea',
  6639. ];
  6640. $this->assertHtml($expected, $result);
  6641. }
  6642. /**
  6643. * testTextAreaMaxLength method
  6644. *
  6645. * Test textareas maxlength read from schema.
  6646. *
  6647. * @return void
  6648. */
  6649. public function testTextAreaMaxLength()
  6650. {
  6651. $this->Form->create([
  6652. 'schema' => [
  6653. 'stuff' => ['type' => 'string', 'length' => 10],
  6654. ]
  6655. ]);
  6656. $result = $this->Form->control('other', ['type' => 'textarea']);
  6657. $expected = [
  6658. 'div' => ['class' => 'input textarea'],
  6659. 'label' => ['for' => 'other'],
  6660. 'Other',
  6661. '/label',
  6662. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6663. '/textarea',
  6664. '/div'
  6665. ];
  6666. $this->assertHtml($expected, $result);
  6667. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6668. $expected = [
  6669. 'div' => ['class' => 'input textarea'],
  6670. 'label' => ['for' => 'stuff'],
  6671. 'Stuff',
  6672. '/label',
  6673. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6674. '/textarea',
  6675. '/div'
  6676. ];
  6677. $this->assertHtml($expected, $result);
  6678. }
  6679. /**
  6680. * testHiddenField method
  6681. *
  6682. * Test generation of a hidden input.
  6683. *
  6684. * @return void
  6685. */
  6686. public function testHiddenField()
  6687. {
  6688. $this->article['errors'] = [
  6689. 'field' => true
  6690. ];
  6691. $this->Form->request->data['field'] = 'test';
  6692. $this->Form->create($this->article);
  6693. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6694. $expected = [
  6695. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6696. $this->assertHtml($expected, $result);
  6697. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6698. $expected = [
  6699. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6700. ];
  6701. $this->assertHtml($expected, $result);
  6702. }
  6703. /**
  6704. * testFileUploadField method
  6705. *
  6706. * Test generation of a file upload input.
  6707. *
  6708. * @return void
  6709. */
  6710. public function testFileUploadField()
  6711. {
  6712. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6713. $result = $this->Form->file('Model.upload');
  6714. $this->assertHtml($expected, $result);
  6715. $this->Form->request->data['Model']['upload'] = [
  6716. 'name' => '', 'type' => '', 'tmp_name' => '',
  6717. 'error' => 4, 'size' => 0
  6718. ];
  6719. $result = $this->Form->file('Model.upload');
  6720. $this->assertHtml($expected, $result);
  6721. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6722. $result = $this->Form->file('Model.upload');
  6723. $this->assertHtml($expected, $result);
  6724. }
  6725. /**
  6726. * testFileUploadOnOtherModel method
  6727. *
  6728. * Test File upload input on a model not used in create().
  6729. *
  6730. * @return void
  6731. */
  6732. public function testFileUploadOnOtherModel()
  6733. {
  6734. $this->Form->create($this->article, ['type' => 'file']);
  6735. $result = $this->Form->file('ValidateProfile.city');
  6736. $expected = [
  6737. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6738. ];
  6739. $this->assertHtml($expected, $result);
  6740. }
  6741. /**
  6742. * testButton method
  6743. *
  6744. * Test generation of a form button.
  6745. *
  6746. * @return void
  6747. */
  6748. public function testButton()
  6749. {
  6750. $result = $this->Form->button('Hi');
  6751. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6752. $this->assertHtml($expected, $result);
  6753. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6754. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6755. $this->assertHtml($expected, $result);
  6756. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6757. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6758. $this->assertHtml($expected, $result);
  6759. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6760. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6761. $this->assertHtml($expected, $result);
  6762. $result = $this->Form->button('No type', ['type' => false]);
  6763. $expected = ['button' => [], 'No type', '/button'];
  6764. $this->assertHtml($expected, $result);
  6765. $result = $this->Form->button('Upload Text', [
  6766. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6767. 'escape' => false
  6768. ]);
  6769. $this->assertNotRegExp('/\&039/', $result);
  6770. }
  6771. /**
  6772. * testButtonUnlockedByDefault method
  6773. *
  6774. * Test that button() makes unlocked fields by default.
  6775. *
  6776. * @return void
  6777. */
  6778. public function testButtonUnlockedByDefault()
  6779. {
  6780. $this->Form->request->params['_csrfToken'] = 'secured';
  6781. $this->Form->button('Save', ['name' => 'save']);
  6782. $this->Form->button('Clear');
  6783. $result = $this->Form->unlockField();
  6784. $this->assertEquals(['save'], $result);
  6785. }
  6786. /**
  6787. * Test generation of a form button with confirm message.
  6788. *
  6789. * @return void
  6790. */
  6791. public function testButtonWithConfirm()
  6792. {
  6793. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6794. $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm(&quot;Confirm me!&quot;)) { return true; } return false;'], 'Hi', '/button'];
  6795. $this->assertHtml($expected, $result);
  6796. }
  6797. /**
  6798. * testPostButton method
  6799. *
  6800. * @return void
  6801. */
  6802. public function testPostButton()
  6803. {
  6804. $result = $this->Form->postButton('Hi', '/controller/action');
  6805. $expected = [
  6806. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6807. 'div' => ['style' => 'display:none;'],
  6808. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6809. '/div',
  6810. 'button' => ['type' => 'submit'],
  6811. 'Hi',
  6812. '/button',
  6813. '/form'
  6814. ];
  6815. $this->assertHtml($expected, $result);
  6816. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6817. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6818. }
  6819. /**
  6820. * testPostButtonMethodType method
  6821. *
  6822. * @return void
  6823. */
  6824. public function testPostButtonMethodType()
  6825. {
  6826. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6827. $expected = [
  6828. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6829. 'div' => ['style' => 'display:none;'],
  6830. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6831. '/div',
  6832. 'button' => ['type' => 'submit'],
  6833. 'Hi',
  6834. '/button',
  6835. '/form'
  6836. ];
  6837. $this->assertHtml($expected, $result);
  6838. }
  6839. /**
  6840. * testPostButtonFormOptions method
  6841. *
  6842. * @return void
  6843. */
  6844. public function testPostButtonFormOptions()
  6845. {
  6846. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6847. $expected = [
  6848. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6849. 'div' => ['style' => 'display:none;'],
  6850. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6851. '/div',
  6852. 'button' => ['type' => 'submit'],
  6853. 'Hi',
  6854. '/button',
  6855. '/form'
  6856. ];
  6857. $this->assertHtml($expected, $result);
  6858. }
  6859. /**
  6860. * testPostButtonNestedData method
  6861. *
  6862. * Test using postButton with N dimensional data.
  6863. *
  6864. * @return void
  6865. */
  6866. public function testPostButtonNestedData()
  6867. {
  6868. $data = [
  6869. 'one' => [
  6870. 'two' => [
  6871. 3, 4, 5
  6872. ]
  6873. ]
  6874. ];
  6875. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6876. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6877. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6878. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6879. }
  6880. /**
  6881. * testSecurePostButton method
  6882. *
  6883. * Test that postButton adds _Token fields.
  6884. *
  6885. * @return void
  6886. */
  6887. public function testSecurePostButton()
  6888. {
  6889. $this->Form->request->params['_csrfToken'] = 'testkey';
  6890. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6891. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6892. $tokenDebug = urlencode(json_encode([
  6893. '/posts/delete/1',
  6894. [],
  6895. []
  6896. ]));
  6897. $expected = [
  6898. 'form' => [
  6899. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6900. ],
  6901. ['div' => ['style' => 'display:none;']],
  6902. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6903. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6904. '/div',
  6905. 'button' => ['type' => 'submit'],
  6906. 'Delete',
  6907. '/button',
  6908. ['div' => ['style' => 'display:none;']],
  6909. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6910. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6911. ['input' => [
  6912. 'type' => 'hidden',
  6913. 'name' => '_Token[debug]',
  6914. 'value' => $tokenDebug,
  6915. 'autocomplete' => 'off',
  6916. ]],
  6917. '/div',
  6918. '/form',
  6919. ];
  6920. $this->assertHtml($expected, $result);
  6921. }
  6922. /**
  6923. * testPostLink method
  6924. *
  6925. * @return void
  6926. */
  6927. public function testPostLink()
  6928. {
  6929. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6930. $expected = [
  6931. 'form' => [
  6932. 'method' => 'post', 'action' => '/posts/delete/1',
  6933. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6934. ],
  6935. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6936. '/form',
  6937. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6938. 'Delete',
  6939. '/a'
  6940. ];
  6941. $this->assertHtml($expected, $result);
  6942. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6943. $expected = [
  6944. 'form' => [
  6945. 'method' => 'post', 'action' => '/posts/delete/1',
  6946. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6947. ],
  6948. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6949. '/form',
  6950. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6951. 'Delete',
  6952. '/a'
  6953. ];
  6954. $this->assertHtml($expected, $result);
  6955. $result = $this->Form->postLink(
  6956. 'Delete',
  6957. '/posts/delete/1',
  6958. ['target' => '_blank', 'class' => 'btn btn-danger']
  6959. );
  6960. $expected = [
  6961. 'form' => [
  6962. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6963. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6964. ],
  6965. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6966. '/form',
  6967. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6968. 'Delete',
  6969. '/a'
  6970. ];
  6971. $this->assertHtml($expected, $result);
  6972. }
  6973. /**
  6974. * testPostLinkWithConfirm method
  6975. *
  6976. * Test the confirm option for postLink().
  6977. *
  6978. * @return void
  6979. */
  6980. public function testPostLinkWithConfirm()
  6981. {
  6982. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6983. $expected = [
  6984. 'form' => [
  6985. 'method' => 'post', 'action' => '/posts/delete/1',
  6986. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6987. ],
  6988. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6989. '/form',
  6990. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6991. 'Delete',
  6992. '/a'
  6993. ];
  6994. $this->assertHtml($expected, $result);
  6995. $result = $this->Form->postLink(
  6996. 'Delete',
  6997. '/posts/delete/1',
  6998. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6999. );
  7000. $expected = [
  7001. 'form' => [
  7002. 'method' => 'post', 'action' => '/posts/delete/1',
  7003. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7004. ],
  7005. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7006. '/form',
  7007. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  7008. 'Delete',
  7009. '/a'
  7010. ];
  7011. $this->assertHtml($expected, $result);
  7012. }
  7013. /**
  7014. * testPostLinkWithQuery method
  7015. *
  7016. * Test postLink() with query string args.
  7017. *
  7018. * @return void
  7019. */
  7020. public function testPostLinkWithQuery()
  7021. {
  7022. $result = $this->Form->postLink(
  7023. 'Delete',
  7024. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  7025. );
  7026. $expected = [
  7027. 'form' => [
  7028. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  7029. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7030. ],
  7031. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7032. '/form',
  7033. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7034. 'Delete',
  7035. '/a'
  7036. ];
  7037. $this->assertHtml($expected, $result);
  7038. }
  7039. /**
  7040. * testPostLinkWithData method
  7041. *
  7042. * Test postLink with additional data.
  7043. *
  7044. * @return void
  7045. */
  7046. public function testPostLinkWithData()
  7047. {
  7048. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7049. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7050. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7051. $this->Form->create($entity);
  7052. $this->Form->end();
  7053. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7054. $this->assertContains(
  7055. '<input type="hidden" name="name" value="show"',
  7056. $result,
  7057. 'should not contain entity data.'
  7058. );
  7059. }
  7060. /**
  7061. * testPostLinkSecurityHash method
  7062. *
  7063. * Test that security hashes for postLink include the url.
  7064. *
  7065. * @return void
  7066. */
  7067. public function testPostLinkSecurityHash()
  7068. {
  7069. $hash = Security::hash(
  7070. '/posts/delete/1' .
  7071. serialize(['id' => '1']) .
  7072. '' .
  7073. Security::salt()
  7074. );
  7075. $hash .= '%3Aid';
  7076. $this->Form->request->params['_Token']['key'] = 'test';
  7077. $result = $this->Form->postLink(
  7078. 'Delete',
  7079. '/posts/delete/1',
  7080. ['data' => ['id' => 1]]
  7081. );
  7082. $tokenDebug = urlencode(json_encode([
  7083. '/posts/delete/1',
  7084. [
  7085. 'id' => 1
  7086. ],
  7087. []
  7088. ]));
  7089. $expected = [
  7090. 'form' => [
  7091. 'method' => 'post', 'action' => '/posts/delete/1',
  7092. 'name', 'style' => 'display:none;'
  7093. ],
  7094. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7095. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7096. 'div' => ['style' => 'display:none;'],
  7097. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7098. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7099. ['input' => [
  7100. 'type' => 'hidden',
  7101. 'name' => '_Token[debug]',
  7102. 'value' => $tokenDebug,
  7103. 'autocomplete' => 'off',
  7104. ]],
  7105. '/div',
  7106. '/form',
  7107. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7108. 'Delete',
  7109. '/a'
  7110. ];
  7111. $this->assertHtml($expected, $result);
  7112. }
  7113. /**
  7114. * testPostLinkSecurityHashBlockMode method
  7115. *
  7116. * Test that postLink doesn't modify the fields in the containing form.
  7117. *
  7118. * postLink() calls inside open forms should not modify the field list
  7119. * for the form.
  7120. *
  7121. * @return void
  7122. */
  7123. public function testPostLinkSecurityHashBlockMode()
  7124. {
  7125. $hash = Security::hash(
  7126. '/posts/delete/1' .
  7127. serialize([]) .
  7128. '' .
  7129. Security::salt()
  7130. );
  7131. $hash .= '%3A';
  7132. $this->Form->request->params['_Token']['key'] = 'test';
  7133. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7134. $this->Form->control('title');
  7135. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7136. $result = $this->View->fetch('postLink');
  7137. $this->assertEquals(['title'], $this->Form->fields);
  7138. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7139. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7140. }
  7141. /**
  7142. * testPostLinkSecurityHashNoDebugMode method
  7143. *
  7144. * Test that security does not include debug token if debug is false.
  7145. *
  7146. * @return void
  7147. */
  7148. public function testPostLinkSecurityHashNoDebugMode()
  7149. {
  7150. Configure::write('debug', false);
  7151. $hash = Security::hash(
  7152. '/posts/delete/1' .
  7153. serialize(['id' => '1']) .
  7154. '' .
  7155. Security::salt()
  7156. );
  7157. $hash .= '%3Aid';
  7158. $this->Form->request->params['_Token']['key'] = 'test';
  7159. $result = $this->Form->postLink(
  7160. 'Delete',
  7161. '/posts/delete/1',
  7162. ['data' => ['id' => 1]]
  7163. );
  7164. $expected = [
  7165. 'form' => [
  7166. 'method' => 'post', 'action' => '/posts/delete/1',
  7167. 'name', 'style' => 'display:none;'
  7168. ],
  7169. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7170. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7171. 'div' => ['style' => 'display:none;'],
  7172. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7173. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7174. '/div',
  7175. '/form',
  7176. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7177. 'Delete',
  7178. '/a'
  7179. ];
  7180. $this->assertHtml($expected, $result);
  7181. }
  7182. /**
  7183. * testPostLinkNestedData method
  7184. *
  7185. * Test using postLink with N dimensional data.
  7186. *
  7187. * @return void
  7188. */
  7189. public function testPostLinkNestedData()
  7190. {
  7191. $data = [
  7192. 'one' => [
  7193. 'two' => [
  7194. 3, 4, 5
  7195. ]
  7196. ]
  7197. ];
  7198. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7199. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7200. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7201. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7202. }
  7203. /**
  7204. * testPostLinkAfterGetForm method
  7205. *
  7206. * Test creating postLinks after a GET form.
  7207. *
  7208. * @return void
  7209. */
  7210. public function testPostLinkAfterGetForm()
  7211. {
  7212. $this->Form->request->params['_csrfToken'] = 'testkey';
  7213. $this->Form->request->params['_Token'] = 'val';
  7214. $this->Form->create($this->article, ['type' => 'get']);
  7215. $this->Form->end();
  7216. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7217. $tokenDebug = urlencode(json_encode([
  7218. '/posts/delete/1',
  7219. [],
  7220. []
  7221. ]));
  7222. $expected = [
  7223. 'form' => [
  7224. 'method' => 'post', 'action' => '/posts/delete/1',
  7225. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7226. ],
  7227. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7228. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7229. 'div' => ['style' => 'display:none;'],
  7230. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7231. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7232. ['input' => [
  7233. 'type' => 'hidden', 'name' => '_Token[debug]',
  7234. 'value' => $tokenDebug,
  7235. 'autocomplete' => 'off'
  7236. ]],
  7237. '/div',
  7238. '/form',
  7239. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7240. 'Delete',
  7241. '/a'
  7242. ];
  7243. $this->assertHtml($expected, $result);
  7244. }
  7245. /**
  7246. * testPostLinkFormBuffer method
  7247. *
  7248. * Test that postLink adds form tags to view block.
  7249. *
  7250. * @return void
  7251. */
  7252. public function testPostLinkFormBuffer()
  7253. {
  7254. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7255. $expected = [
  7256. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7257. 'Delete',
  7258. '/a'
  7259. ];
  7260. $this->assertHtml($expected, $result);
  7261. $result = $this->View->fetch('postLink');
  7262. $expected = [
  7263. 'form' => [
  7264. 'method' => 'post', 'action' => '/posts/delete/1',
  7265. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7266. ],
  7267. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7268. '/form'
  7269. ];
  7270. $this->assertHtml($expected, $result);
  7271. $result = $this->Form->postLink(
  7272. 'Delete',
  7273. '/posts/delete/2',
  7274. ['block' => true, 'method' => 'DELETE']
  7275. );
  7276. $expected = [
  7277. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7278. 'Delete',
  7279. '/a'
  7280. ];
  7281. $this->assertHtml($expected, $result);
  7282. $result = $this->View->fetch('postLink');
  7283. $expected = [
  7284. 'form' => [
  7285. 'method' => 'post', 'action' => '/posts/delete/1',
  7286. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7287. ],
  7288. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7289. '/form',
  7290. [
  7291. 'form' => [
  7292. 'method' => 'post', 'action' => '/posts/delete/2',
  7293. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7294. ],
  7295. ],
  7296. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7297. '/form'
  7298. ];
  7299. $this->assertHtml($expected, $result);
  7300. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7301. $expected = [
  7302. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7303. 'Delete',
  7304. '/a'
  7305. ];
  7306. $this->assertHtml($expected, $result);
  7307. $result = $this->View->fetch('foobar');
  7308. $expected = [
  7309. 'form' => [
  7310. 'method' => 'post', 'action' => '/posts/delete/1',
  7311. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7312. ],
  7313. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7314. '/form'
  7315. ];
  7316. $this->assertHtml($expected, $result);
  7317. }
  7318. /**
  7319. * testSubmitButton method
  7320. *
  7321. * @return void
  7322. */
  7323. public function testSubmitButton()
  7324. {
  7325. $result = $this->Form->submit('');
  7326. $expected = [
  7327. 'div' => ['class' => 'submit'],
  7328. 'input' => ['type' => 'submit', 'value' => ''],
  7329. '/div'
  7330. ];
  7331. $this->assertHtml($expected, $result);
  7332. $result = $this->Form->submit('Test Submit');
  7333. $expected = [
  7334. 'div' => ['class' => 'submit'],
  7335. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7336. '/div'
  7337. ];
  7338. $this->assertHtml($expected, $result);
  7339. $result = $this->Form->submit('Next >');
  7340. $expected = [
  7341. 'div' => ['class' => 'submit'],
  7342. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7343. '/div'
  7344. ];
  7345. $this->assertHtml($expected, $result);
  7346. $result = $this->Form->submit('Next >', ['escape' => false]);
  7347. $expected = [
  7348. 'div' => ['class' => 'submit'],
  7349. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7350. '/div'
  7351. ];
  7352. $this->assertHtml($expected, $result);
  7353. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7354. $expected = [
  7355. 'div' => ['class' => 'submit'],
  7356. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7357. '/div'
  7358. ];
  7359. $this->assertHtml($expected, $result);
  7360. }
  7361. /**
  7362. * testSubmitImage method
  7363. *
  7364. * Test image submit types.
  7365. *
  7366. * @return void
  7367. */
  7368. public function testSubmitImage()
  7369. {
  7370. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7371. $expected = [
  7372. 'div' => ['class' => 'submit'],
  7373. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7374. '/div'
  7375. ];
  7376. $this->assertHtml($expected, $result);
  7377. $result = $this->Form->submit('/relative/cake.power.gif');
  7378. $expected = [
  7379. 'div' => ['class' => 'submit'],
  7380. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7381. '/div'
  7382. ];
  7383. $this->assertHtml($expected, $result);
  7384. $result = $this->Form->submit('cake.power.gif');
  7385. $expected = [
  7386. 'div' => ['class' => 'submit'],
  7387. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7388. '/div'
  7389. ];
  7390. $this->assertHtml($expected, $result);
  7391. $result = $this->Form->submit('Not.an.image');
  7392. $expected = [
  7393. 'div' => ['class' => 'submit'],
  7394. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7395. '/div'
  7396. ];
  7397. $this->assertHtml($expected, $result);
  7398. }
  7399. /**
  7400. * testSubmitUnlockedByDefault method
  7401. *
  7402. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7403. * be submitted at a time.
  7404. *
  7405. * @return void
  7406. */
  7407. public function testSubmitUnlockedByDefault()
  7408. {
  7409. $this->Form->request->params['_Token'] = 'secured';
  7410. $this->Form->submit('Go go');
  7411. $this->Form->submit('Save', ['name' => 'save']);
  7412. $result = $this->Form->unlockField();
  7413. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7414. }
  7415. /**
  7416. * testSubmitImageTimestamp method
  7417. *
  7418. * Test submit image with timestamps.
  7419. *
  7420. * @return void
  7421. */
  7422. public function testSubmitImageTimestamp()
  7423. {
  7424. Configure::write('Asset.timestamp', 'force');
  7425. $result = $this->Form->submit('cake.power.gif');
  7426. $expected = [
  7427. 'div' => ['class' => 'submit'],
  7428. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7429. '/div'
  7430. ];
  7431. $this->assertHtml($expected, $result);
  7432. }
  7433. /**
  7434. * testDateTimeWithGetForms method
  7435. *
  7436. * Test that datetime() works with GET style forms.
  7437. *
  7438. * @return void
  7439. */
  7440. public function testDateTimeWithGetForms()
  7441. {
  7442. extract($this->dateRegex);
  7443. $this->Form->create($this->article, ['type' => 'get']);
  7444. $result = $this->Form->datetime('created');
  7445. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7446. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7447. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7448. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7449. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7450. }
  7451. /**
  7452. * testForMagicControlNonExistingNorValidated method
  7453. *
  7454. * @return void
  7455. */
  7456. public function testForMagicControlNonExistingNorValidated()
  7457. {
  7458. $this->Form->create($this->article);
  7459. $this->Form->templates(['inputContainer' => '{{content}}']);
  7460. $result = $this->Form->control('non_existing_nor_validated');
  7461. $expected = [
  7462. 'label' => ['for' => 'non-existing-nor-validated'],
  7463. 'Non Existing Nor Validated',
  7464. '/label',
  7465. 'input' => [
  7466. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7467. 'id' => 'non-existing-nor-validated'
  7468. ]
  7469. ];
  7470. $this->assertHtml($expected, $result);
  7471. $result = $this->Form->control('non_existing_nor_validated', [
  7472. 'val' => 'my value'
  7473. ]);
  7474. $expected = [
  7475. 'label' => ['for' => 'non-existing-nor-validated'],
  7476. 'Non Existing Nor Validated',
  7477. '/label',
  7478. 'input' => [
  7479. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7480. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7481. ]
  7482. ];
  7483. $this->assertHtml($expected, $result);
  7484. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7485. $result = $this->Form->control('non_existing_nor_validated');
  7486. $expected = [
  7487. 'label' => ['for' => 'non-existing-nor-validated'],
  7488. 'Non Existing Nor Validated',
  7489. '/label',
  7490. 'input' => [
  7491. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7492. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7493. ]
  7494. ];
  7495. $this->assertHtml($expected, $result);
  7496. }
  7497. /**
  7498. * testFormMagicControlLabel method
  7499. *
  7500. * @return void
  7501. */
  7502. public function testFormMagicControlLabel()
  7503. {
  7504. TableRegistry::get('Contacts', [
  7505. 'className' => __NAMESPACE__ . '\ContactsTable'
  7506. ]);
  7507. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7508. $this->Form->templates(['inputContainer' => '{{content}}']);
  7509. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7510. $expected = [
  7511. 'label' => ['for' => 'contacts-name'],
  7512. 'My label',
  7513. '/label',
  7514. 'input' => [
  7515. 'type' => 'text', 'name' => 'Contacts[name]',
  7516. 'id' => 'contacts-name', 'maxlength' => '255'
  7517. ]
  7518. ];
  7519. $this->assertHtml($expected, $result);
  7520. $result = $this->Form->control('name', [
  7521. 'label' => ['class' => 'mandatory']
  7522. ]);
  7523. $expected = [
  7524. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7525. 'Name',
  7526. '/label',
  7527. 'input' => [
  7528. 'type' => 'text', 'name' => 'name',
  7529. 'id' => 'name', 'maxlength' => '255'
  7530. ]
  7531. ];
  7532. $this->assertHtml($expected, $result);
  7533. $result = $this->Form->control('name', [
  7534. 'div' => false,
  7535. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7536. ]);
  7537. $expected = [
  7538. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7539. 'My label',
  7540. '/label',
  7541. 'input' => [
  7542. 'type' => 'text', 'name' => 'name',
  7543. 'id' => 'name', 'maxlength' => '255'
  7544. ]
  7545. ];
  7546. $this->assertHtml($expected, $result);
  7547. $result = $this->Form->control('Contact.name', [
  7548. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7549. ]);
  7550. $expected = [
  7551. 'label' => ['for' => 'my_id'],
  7552. 'Name',
  7553. '/label',
  7554. 'input' => [
  7555. 'type' => 'text', 'name' => 'Contact[name]',
  7556. 'id' => 'my_id', 'maxlength' => '255'
  7557. ]
  7558. ];
  7559. $this->assertHtml($expected, $result);
  7560. $result = $this->Form->control('1.id');
  7561. $expected = ['input' => [
  7562. 'type' => 'hidden', 'name' => '1[id]',
  7563. 'id' => '1-id'
  7564. ]];
  7565. $this->assertHtml($expected, $result);
  7566. $result = $this->Form->control('1.name');
  7567. $expected = [
  7568. 'label' => ['for' => '1-name'],
  7569. 'Name',
  7570. '/label',
  7571. 'input' => [
  7572. 'type' => 'text', 'name' => '1[name]',
  7573. 'id' => '1-name', 'maxlength' => '255'
  7574. ]
  7575. ];
  7576. $this->assertHtml($expected, $result);
  7577. }
  7578. /**
  7579. * testFormEnd method
  7580. *
  7581. * @return void
  7582. */
  7583. public function testFormEnd()
  7584. {
  7585. $this->assertEquals('</form>', $this->Form->end());
  7586. }
  7587. /**
  7588. * testMultiRecordForm method
  7589. *
  7590. * Test the generation of fields for a multi record form.
  7591. *
  7592. * @return void
  7593. */
  7594. public function testMultiRecordForm()
  7595. {
  7596. $this->loadFixtures('Articles', 'Comments');
  7597. $articles = TableRegistry::get('Articles');
  7598. $articles->hasMany('Comments');
  7599. $comment = new Entity(['comment' => 'Value']);
  7600. $article = new Article(['comments' => [$comment]]);
  7601. $this->Form->create([$article]);
  7602. $result = $this->Form->control('0.comments.1.comment');
  7603. //@codingStandardsIgnoreStart
  7604. $expected = [
  7605. 'div' => ['class' => 'input textarea'],
  7606. 'label' => ['for' => '0-comments-1-comment'],
  7607. 'Comment',
  7608. '/label',
  7609. 'textarea' => [
  7610. 'name',
  7611. 'id' => '0-comments-1-comment',
  7612. 'rows' => 5
  7613. ],
  7614. '/textarea',
  7615. '/div'
  7616. ];
  7617. //@codingStandardsIgnoreEnd
  7618. $this->assertHtml($expected, $result);
  7619. $result = $this->Form->control('0.comments.0.comment');
  7620. //@codingStandardsIgnoreStart
  7621. $expected = [
  7622. 'div' => ['class' => 'input textarea'],
  7623. 'label' => ['for' => '0-comments-0-comment'],
  7624. 'Comment',
  7625. '/label',
  7626. 'textarea' => [
  7627. 'name',
  7628. 'id' => '0-comments-0-comment',
  7629. 'rows' => 5
  7630. ],
  7631. 'Value',
  7632. '/textarea',
  7633. '/div'
  7634. ];
  7635. //@codingStandardsIgnoreEnd
  7636. $this->assertHtml($expected, $result);
  7637. $comment->errors('comment', ['Not valid']);
  7638. $result = $this->Form->control('0.comments.0.comment');
  7639. //@codingStandardsIgnoreStart
  7640. $expected = [
  7641. 'div' => ['class' => 'input textarea error'],
  7642. 'label' => ['for' => '0-comments-0-comment'],
  7643. 'Comment',
  7644. '/label',
  7645. 'textarea' => [
  7646. 'name',
  7647. 'class' => 'form-error',
  7648. 'id' => '0-comments-0-comment',
  7649. 'rows' => 5
  7650. ],
  7651. 'Value',
  7652. '/textarea',
  7653. ['div' => ['class' => 'error-message']],
  7654. 'Not valid',
  7655. '/div',
  7656. '/div'
  7657. ];
  7658. //@codingStandardsIgnoreEnd
  7659. $this->assertHtml($expected, $result);
  7660. TableRegistry::get('Comments')
  7661. ->validator('default')
  7662. ->allowEmpty('comment', false);
  7663. $result = $this->Form->control('0.comments.1.comment');
  7664. //@codingStandardsIgnoreStart
  7665. $expected = [
  7666. 'div' => ['class' => 'input textarea required'],
  7667. 'label' => ['for' => '0-comments-1-comment'],
  7668. 'Comment',
  7669. '/label',
  7670. 'textarea' => [
  7671. 'name',
  7672. 'required' => 'required',
  7673. 'id' => '0-comments-1-comment',
  7674. 'rows' => 5
  7675. ],
  7676. '/textarea',
  7677. '/div'
  7678. ];
  7679. //@codingStandardsIgnoreEnd
  7680. $this->assertHtml($expected, $result);
  7681. }
  7682. /**
  7683. * testHtml5Controls method
  7684. *
  7685. * Test that some html5 inputs + FormHelper::__call() work.
  7686. *
  7687. * @return void
  7688. */
  7689. public function testHtml5Controls()
  7690. {
  7691. $result = $this->Form->email('User.email');
  7692. $expected = [
  7693. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7694. ];
  7695. $this->assertHtml($expected, $result);
  7696. $result = $this->Form->search('User.query');
  7697. $expected = [
  7698. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7699. ];
  7700. $this->assertHtml($expected, $result);
  7701. $result = $this->Form->search('User.query', ['value' => 'test']);
  7702. $expected = [
  7703. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7704. ];
  7705. $this->assertHtml($expected, $result);
  7706. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7707. $expected = [
  7708. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7709. ];
  7710. $this->assertHtml($expected, $result);
  7711. }
  7712. /**
  7713. * testHtml5ControlWithControl method
  7714. *
  7715. * Test accessing html5 inputs through control().
  7716. *
  7717. * @return void
  7718. */
  7719. public function testHtml5ControlWithControl()
  7720. {
  7721. $this->Form->create();
  7722. $this->Form->templates(['inputContainer' => '{{content}}']);
  7723. $result = $this->Form->control('website', [
  7724. 'type' => 'url',
  7725. 'val' => 'http://domain.tld',
  7726. 'label' => false
  7727. ]);
  7728. $expected = [
  7729. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7730. ];
  7731. $this->assertHtml($expected, $result);
  7732. }
  7733. /**
  7734. * testHtml5ControlException method
  7735. *
  7736. * Test errors when field name is missing.
  7737. *
  7738. * @return void
  7739. */
  7740. public function testHtml5ControlException()
  7741. {
  7742. $this->expectException(\Cake\Core\Exception\Exception::class);
  7743. $this->Form->email();
  7744. }
  7745. /**
  7746. * testRequiredAttribute method
  7747. *
  7748. * Tests that formhelper sets required attributes.
  7749. *
  7750. * @return void
  7751. */
  7752. public function testRequiredAttribute()
  7753. {
  7754. $this->article['required'] = [
  7755. 'title' => true,
  7756. 'body' => false,
  7757. ];
  7758. $this->Form->create($this->article);
  7759. $result = $this->Form->control('title');
  7760. $expected = [
  7761. 'div' => ['class' => 'input text required'],
  7762. 'label' => ['for' => 'title'],
  7763. 'Title',
  7764. '/label',
  7765. 'input' => [
  7766. 'type' => 'text',
  7767. 'name' => 'title',
  7768. 'id' => 'title',
  7769. 'required' => 'required',
  7770. ],
  7771. '/div'
  7772. ];
  7773. $this->assertHtml($expected, $result);
  7774. $result = $this->Form->control('title', ['required' => false]);
  7775. $this->assertNotContains('required', $result);
  7776. $result = $this->Form->control('body');
  7777. $expected = [
  7778. 'div' => ['class' => 'input text'],
  7779. 'label' => ['for' => 'body'],
  7780. 'Body',
  7781. '/label',
  7782. 'input' => [
  7783. 'type' => 'text',
  7784. 'name' => 'body',
  7785. 'id' => 'body',
  7786. ],
  7787. '/div'
  7788. ];
  7789. $this->assertHtml($expected, $result);
  7790. $result = $this->Form->control('body', ['required' => true]);
  7791. $this->assertContains('required', $result);
  7792. }
  7793. /**
  7794. * testControlsNotNested method
  7795. *
  7796. * Tests that it is possible to put inputs outside of the label.
  7797. *
  7798. * @return void
  7799. */
  7800. public function testControlsNotNested()
  7801. {
  7802. $this->Form->templates([
  7803. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7804. 'formGroup' => '{{input}}{{label}}',
  7805. ]);
  7806. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7807. $expected = [
  7808. 'div' => ['class' => 'input checkbox'],
  7809. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7810. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7811. 'label' => ['for' => 'foo'],
  7812. 'Foo',
  7813. '/label',
  7814. '/div'
  7815. ];
  7816. $this->assertHtml($expected, $result);
  7817. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7818. $expected = [
  7819. 'div' => ['class' => 'input checkbox'],
  7820. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7821. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7822. '/div'
  7823. ];
  7824. $this->assertHtml($expected, $result);
  7825. $result = $this->Form->control('confirm', [
  7826. 'type' => 'radio',
  7827. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7828. ]);
  7829. $expected = [
  7830. 'div' => ['class' => 'input radio'],
  7831. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7832. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7833. ['label' => ['for' => 'confirm-y']],
  7834. 'Yes',
  7835. '/label',
  7836. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7837. ['label' => ['for' => 'confirm-n']],
  7838. 'No',
  7839. '/label',
  7840. '<label',
  7841. 'Confirm',
  7842. '/label',
  7843. '/div',
  7844. ];
  7845. $this->assertHtml($expected, $result);
  7846. $result = $this->Form->select('category', ['1', '2'], [
  7847. 'multiple' => 'checkbox',
  7848. 'name' => 'fish',
  7849. ]);
  7850. $expected = [
  7851. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7852. ['div' => ['class' => 'checkbox']],
  7853. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7854. ['label' => ['for' => 'fish-0']],
  7855. '1',
  7856. '/label',
  7857. '/div',
  7858. ['div' => ['class' => 'checkbox']],
  7859. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7860. ['label' => ['for' => 'fish-1']],
  7861. '2',
  7862. '/label',
  7863. '/div'
  7864. ];
  7865. $this->assertHtml($expected, $result);
  7866. }
  7867. /**
  7868. * testControlContainerTemplates method
  7869. *
  7870. * Test that *Container templates are used by input.
  7871. *
  7872. * @return void
  7873. */
  7874. public function testControlContainerTemplates()
  7875. {
  7876. $this->Form->templates([
  7877. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7878. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7879. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7880. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7881. ]);
  7882. $this->article['errors'] = [
  7883. 'Article' => ['published' => 'error message']
  7884. ];
  7885. $this->Form->create($this->article);
  7886. $result = $this->Form->control('accept', [
  7887. 'type' => 'checkbox'
  7888. ]);
  7889. $expected = [
  7890. 'div' => ['class' => 'check'],
  7891. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7892. 'label' => ['for' => 'accept'],
  7893. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7894. 'Accept',
  7895. '/label',
  7896. '/div'
  7897. ];
  7898. $this->assertHtml($expected, $result);
  7899. $result = $this->Form->control('accept', [
  7900. 'type' => 'radio',
  7901. 'options' => ['Y', 'N']
  7902. ]);
  7903. $this->assertContains('<div class="rad">', $result);
  7904. $result = $this->Form->control('Article.published', [
  7905. 'type' => 'radio',
  7906. 'options' => ['Y', 'N']
  7907. ]);
  7908. $this->assertContains('<div class="rad err">', $result);
  7909. $result = $this->Form->control('Article.created', [
  7910. 'type' => 'datetime',
  7911. ]);
  7912. $this->assertContains('<div class="dt">', $result);
  7913. }
  7914. /**
  7915. * testFormGroupTemplates method
  7916. *
  7917. * Test that *Container templates are used by input.
  7918. *
  7919. * @return void
  7920. */
  7921. public function testFormGroupTemplates()
  7922. {
  7923. $this->Form->templates([
  7924. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7925. ]);
  7926. $this->Form->create($this->article);
  7927. $result = $this->Form->control('accept', [
  7928. 'type' => 'radio',
  7929. 'options' => ['Y', 'N']
  7930. ]);
  7931. $this->assertContains('<div class="radio">', $result);
  7932. }
  7933. /**
  7934. * testResetTemplates method
  7935. *
  7936. * Test resetting templates.
  7937. *
  7938. * @return void
  7939. */
  7940. public function testResetTemplates()
  7941. {
  7942. $this->Form->templates(['input' => '<input/>']);
  7943. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7944. $this->Form->resetTemplates();
  7945. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7946. }
  7947. /**
  7948. * testContext method
  7949. *
  7950. * Test the context method.
  7951. *
  7952. * @return void
  7953. */
  7954. public function testContext()
  7955. {
  7956. $result = $this->Form->context();
  7957. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7958. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7959. $this->assertSame($mock, $this->Form->context($mock));
  7960. $this->assertSame($mock, $this->Form->context());
  7961. }
  7962. /**
  7963. * testAutoDomId method
  7964. *
  7965. * @return void
  7966. */
  7967. public function testAutoDomId()
  7968. {
  7969. $result = $this->Form->text('field', ['id' => true]);
  7970. $expected = [
  7971. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7972. ];
  7973. $this->assertHtml($expected, $result);
  7974. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7975. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7976. $expected = [
  7977. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7978. ['label' => ['for' => 'field-0']],
  7979. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7980. 'option A',
  7981. '/label',
  7982. ['label' => ['for' => 'field-1']],
  7983. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7984. 'option B',
  7985. '/label',
  7986. ];
  7987. $this->assertHtml($expected, $result);
  7988. $result = $this->Form->select(
  7989. 'multi_field',
  7990. ['first', 'second'],
  7991. ['multiple' => 'checkbox', 'id' => true]
  7992. );
  7993. $expected = [
  7994. 'input' => [
  7995. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7996. ],
  7997. ['div' => ['class' => 'checkbox']],
  7998. ['label' => ['for' => 'multi-field-0']],
  7999. ['input' => [
  8000. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8001. 'value' => '0', 'id' => 'multi-field-0'
  8002. ]],
  8003. 'first',
  8004. '/label',
  8005. '/div',
  8006. ['div' => ['class' => 'checkbox']],
  8007. ['label' => ['for' => 'multi-field-1']],
  8008. ['input' => [
  8009. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8010. 'value' => '1', 'id' => 'multi-field-1'
  8011. ]],
  8012. 'second',
  8013. '/label',
  8014. '/div',
  8015. ];
  8016. $this->assertHtml($expected, $result);
  8017. }
  8018. /**
  8019. * Test the basic setters and getters for value sources
  8020. *
  8021. * @return void
  8022. */
  8023. public function testFormValueSourcesSettersGetters()
  8024. {
  8025. $expected = ['context'];
  8026. $result = $this->Form->getValueSources();
  8027. $this->assertEquals($expected, $result);
  8028. $expected = null;
  8029. $result = $this->Form->getSourceValue('id');
  8030. $this->assertEquals($expected, $result);
  8031. $expected = ['query', 'data', 'context'];
  8032. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  8033. $result = $this->Form->getValueSources();
  8034. $this->assertEquals($expected, $result);
  8035. $this->Form->request->data['id'] = '1';
  8036. $this->Form->request->query['id'] = '2';
  8037. $this->Form->setValueSources(['context']);
  8038. $expected = '1';
  8039. $result = $this->Form->getSourceValue('id');
  8040. $this->assertEquals($expected, $result);
  8041. $this->Form->setValueSources('query');
  8042. $expected = ['query'];
  8043. $result = $this->Form->getValueSources();
  8044. $this->assertEquals($expected, $result);
  8045. $expected = '2';
  8046. $result = $this->Form->getSourceValue('id');
  8047. $this->assertEquals($expected, $result);
  8048. $this->Form->setValueSources(['data']);
  8049. $expected = '1';
  8050. $result = $this->Form->getSourceValue('id');
  8051. $this->assertEquals($expected, $result);
  8052. $this->Form->setValueSources(['query', 'data']);
  8053. $expected = '2';
  8054. $result = $this->Form->getSourceValue('id');
  8055. $this->assertEquals($expected, $result);
  8056. }
  8057. /**
  8058. * Tests the different input rendering values based on sources values switching
  8059. *
  8060. * @return void
  8061. */
  8062. public function testFormValueSourcesSingleSwitchRendering()
  8063. {
  8064. $this->loadFixtures('Articles');
  8065. $articles = TableRegistry::get('Articles');
  8066. $article = new Article();
  8067. $articles->patchEntity($article, ['id' => '3']);
  8068. $this->Form->create($article);
  8069. $this->Form->setValueSources(['context']);
  8070. $result = $this->Form->control('id');
  8071. $expected = [
  8072. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8073. ];
  8074. $this->assertHtml($expected, $result);
  8075. $this->Form->request->query['id'] = '5';
  8076. $this->Form->setValueSources(['query']);
  8077. $result = $this->Form->control('id');
  8078. $expected = [
  8079. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8080. ];
  8081. $this->assertHtml($expected, $result);
  8082. $this->Form->request->query['id'] = '5a';
  8083. $this->Form->request->data['id'] = '5b';
  8084. $this->Form->setValueSources(['context']);
  8085. $this->Form->create($article);
  8086. $result = $this->Form->control('id');
  8087. $expected = [
  8088. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8089. ];
  8090. $this->assertHtml($expected, $result);
  8091. $this->Form->setValueSources(['data']);
  8092. $this->Form->create($article);
  8093. $result = $this->Form->control('id');
  8094. $expected = [
  8095. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8096. ];
  8097. $this->assertHtml($expected, $result);
  8098. $this->Form->setValueSources(['query']);
  8099. $result = $this->Form->control('id');
  8100. $expected = [
  8101. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8102. ];
  8103. $this->assertHtml($expected, $result);
  8104. }
  8105. /**
  8106. * Tests the different input rendering values based on sources values switching while supplying
  8107. * an entity (base context) and multiple sources (such as data, query)
  8108. *
  8109. * @return void
  8110. */
  8111. public function testFormValueSourcesListSwitchRendering()
  8112. {
  8113. $this->loadFixtures('Articles');
  8114. $articles = TableRegistry::get('Articles');
  8115. $article = new Article();
  8116. $articles->patchEntity($article, ['id' => '3']);
  8117. $this->Form->request->query['id'] = '9';
  8118. $this->Form->create($article);
  8119. $this->Form->setValueSources(['context', 'query']);
  8120. $result = $this->Form->control('id');
  8121. $expected = [
  8122. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8123. ];
  8124. $this->assertHtml($expected, $result);
  8125. $this->Form->setValueSources(['query', 'context']);
  8126. $result = $this->Form->control('id');
  8127. $expected = [
  8128. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8129. ];
  8130. $this->assertHtml($expected, $result);
  8131. $this->Form->setValueSources(['data', 'query', 'context']);
  8132. $result = $this->Form->control('id');
  8133. $expected = [
  8134. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8135. ];
  8136. $this->assertHtml($expected, $result);
  8137. $this->Form->request->data['id'] = '8';
  8138. $this->Form->request->query['id'] = '9';
  8139. $this->Form->setValueSources(['data', 'query', 'context']);
  8140. $result = $this->Form->control('id');
  8141. $expected = [
  8142. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8143. ];
  8144. $this->assertHtml($expected, $result);
  8145. }
  8146. /**
  8147. * Test the different form input renderings based on values sources switchings through form options
  8148. *
  8149. * @return void
  8150. */
  8151. public function testFormValueSourcesSwitchViaOptionsRendering()
  8152. {
  8153. $this->loadFixtures('Articles');
  8154. $articles = TableRegistry::get('Articles');
  8155. $article = new Article();
  8156. $articles->patchEntity($article, ['id' => '3']);
  8157. $this->Form->request->data['id'] = '4';
  8158. $this->Form->request->query['id'] = '5';
  8159. $this->Form->create($article, ['valueSources' => 'query']);
  8160. $result = $this->Form->control('id');
  8161. $expected = [
  8162. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8163. ];
  8164. $this->assertHtml($expected, $result);
  8165. $result = $this->Form->getSourceValue('id');
  8166. $this->assertEquals('5', $result);
  8167. $this->Form->setValueSources(['context']);
  8168. $this->Form->create($article, ['valueSources' => 'query']);
  8169. $result = $this->Form->control('id');
  8170. $expected = [
  8171. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8172. ];
  8173. $this->assertHtml($expected, $result);
  8174. $result = $this->Form->getSourceValue('id');
  8175. $this->assertEquals('5', $result);
  8176. $this->Form->setValueSources(['query']);
  8177. $this->Form->create($article, ['valueSources' => 'data']);
  8178. $result = $this->Form->control('id');
  8179. $expected = [
  8180. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8181. ];
  8182. $this->assertHtml($expected, $result);
  8183. $result = $this->Form->getSourceValue('id');
  8184. $this->assertEquals('4', $result);
  8185. $this->Form->setValueSources(['query']);
  8186. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8187. $result = $this->Form->control('id');
  8188. $expected = [
  8189. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8190. ];
  8191. $this->assertHtml($expected, $result);
  8192. $result = $this->Form->getSourceValue('id');
  8193. $this->assertEquals('4', $result);
  8194. }
  8195. /**
  8196. * Test the different form input renderings based on values sources switchings through form options
  8197. *
  8198. * @return void
  8199. */
  8200. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8201. {
  8202. $this->loadFixtures('Articles');
  8203. $articles = TableRegistry::get('Articles');
  8204. $article = new Article();
  8205. $articles->patchEntity($article, ['id' => '3']);
  8206. $this->Form->request->data['id'] = '10';
  8207. $this->Form->request->query['id'] = '11';
  8208. $this->Form->setValueSources(['context'])
  8209. ->create($article, ['valueSources' => ['query', 'data']]);
  8210. $result = $this->Form->control('id');
  8211. $expected = [
  8212. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8213. ];
  8214. $this->assertHtml($expected, $result);
  8215. $result = $this->Form->getSourceValue('id');
  8216. $this->assertEquals('11', $result);
  8217. unset($this->Form->request->query['id']);
  8218. $this->Form->setValueSources(['context'])
  8219. ->create($article, ['valueSources' => ['query', 'data']]);
  8220. $result = $this->Form->control('id');
  8221. $expected = [
  8222. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8223. ];
  8224. $this->assertHtml($expected, $result);
  8225. $result = $this->Form->getSourceValue('id');
  8226. $this->assertEquals('10', $result);
  8227. }
  8228. /**
  8229. * Test the different form values sources resetting through From::end();
  8230. *
  8231. * @return void
  8232. */
  8233. public function testFormValueSourcesResetViaEnd()
  8234. {
  8235. $expected = ['context'];
  8236. $result = $this->Form->getValueSources();
  8237. $this->assertEquals($expected, $result);
  8238. $expected = ['query', 'context', 'data'];
  8239. $this->Form->setValueSources(['query', 'context', 'data']);
  8240. $result = $this->Form->getValueSources();
  8241. $this->assertEquals($expected, $result);
  8242. $this->Form->create();
  8243. $result = $this->Form->getValueSources();
  8244. $this->assertEquals($expected, $result);
  8245. $this->Form->end();
  8246. $result = $this->Form->getValueSources();
  8247. $this->assertEquals(['context'], $result);
  8248. }
  8249. /**
  8250. * Test sources values defaults handling
  8251. *
  8252. * @return void
  8253. */
  8254. public function testFormValueSourcesDefaults()
  8255. {
  8256. $this->Form->request->query['password'] = 'open Sesame';
  8257. $this->Form->create();
  8258. $result = $this->Form->password('password');
  8259. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8260. $this->assertHtml($expected, $result);
  8261. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8262. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8263. $this->assertHtml($expected, $result);
  8264. $this->Form->setValueSources('query');
  8265. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8266. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8267. $this->assertHtml($expected, $result);
  8268. $this->Form->setValueSources('data');
  8269. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8270. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8271. $this->assertHtml($expected, $result);
  8272. }
  8273. /**
  8274. * Test sources values schema defaults handling
  8275. *
  8276. * @return void
  8277. */
  8278. public function testSourcesValueDoesntExistPassThrough()
  8279. {
  8280. $this->Form->request->query['category'] = 'sesame-cookies';
  8281. $articles = TableRegistry::get('Articles');
  8282. $entity = $articles->newEntity();
  8283. $this->Form->create($entity);
  8284. $this->Form->setValueSources(['query', 'context']);
  8285. $result = $this->Form->getSourceValue('category');
  8286. $this->assertEquals('sesame-cookies', $result);
  8287. $this->Form->setValueSources(['context', 'query']);
  8288. $result = $this->Form->getSourceValue('category');
  8289. $this->assertEquals('sesame-cookies', $result);
  8290. }
  8291. /**
  8292. * testNestedLabelInput method
  8293. *
  8294. * Test the `nestedInput` parameter
  8295. *
  8296. * @return void
  8297. */
  8298. public function testNestedLabelInput()
  8299. {
  8300. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8301. $expected = [
  8302. 'div' => ['class' => 'input text'],
  8303. 'label' => ['for' => 'foo'],
  8304. ['input' => [
  8305. 'type' => 'text',
  8306. 'name' => 'foo',
  8307. 'id' => 'foo'
  8308. ]],
  8309. 'Foo',
  8310. '/label',
  8311. '/div'
  8312. ];
  8313. $this->assertHtml($expected, $result);
  8314. }
  8315. /**
  8316. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8317. *
  8318. * This test makes sure `false` excludes the label from the render
  8319. *
  8320. * @return void
  8321. */
  8322. public function testInputLabelManipulationDisableLabels()
  8323. {
  8324. $result = $this->Form->input('test', [
  8325. 'type' => 'radio',
  8326. 'options' => ['A', 'B'],
  8327. 'labelOptions' => false
  8328. ]);
  8329. $expected = [
  8330. ['div' => ['class' => 'input radio']],
  8331. '<label',
  8332. 'Test',
  8333. '/label',
  8334. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8335. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8336. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8337. '/div',
  8338. ];
  8339. $this->assertHtml($expected, $result);
  8340. $result = $this->Form->input('checkbox1', [
  8341. 'label' => 'My checkboxes',
  8342. 'multiple' => 'checkbox',
  8343. 'type' => 'select',
  8344. 'options' => [
  8345. ['text' => 'First Checkbox', 'value' => 1],
  8346. ['text' => 'Second Checkbox', 'value' => 2]
  8347. ],
  8348. 'labelOptions' => false
  8349. ]);
  8350. $expected = [
  8351. ['div' => ['class' => 'input select']],
  8352. ['label' => ['for' => 'checkbox1']],
  8353. 'My checkboxes',
  8354. '/label',
  8355. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8356. ['div' => ['class' => 'checkbox']],
  8357. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8358. '/div',
  8359. ['div' => ['class' => 'checkbox']],
  8360. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8361. '/div',
  8362. '/div'
  8363. ];
  8364. $this->assertHtml($expected, $result);
  8365. }
  8366. /**
  8367. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8368. *
  8369. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8370. * added to the class if checked.
  8371. *
  8372. * Also checks to make sure any custom attributes are rendered correctly
  8373. *
  8374. * @return void
  8375. */
  8376. public function testInputLabelManipulationRadios()
  8377. {
  8378. $result = $this->Form->input('test', [
  8379. 'type' => 'radio',
  8380. 'options' => ['A', 'B'],
  8381. 'labelOptions' => ['class' => 'custom-class']
  8382. ]);
  8383. $expected = [
  8384. ['div' => ['class' => 'input radio']],
  8385. '<label',
  8386. 'Test',
  8387. '/label',
  8388. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8389. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8390. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8391. 'A',
  8392. '/label',
  8393. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8394. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8395. 'B',
  8396. '/label',
  8397. '/div',
  8398. ];
  8399. $this->assertHtml($expected, $result);
  8400. $result = $this->Form->input('test', [
  8401. 'type' => 'radio',
  8402. 'options' => ['A', 'B'],
  8403. 'value' => 1,
  8404. 'labelOptions' => ['class' => 'custom-class']
  8405. ]);
  8406. $expected = [
  8407. ['div' => ['class' => 'input radio']],
  8408. '<label',
  8409. 'Test',
  8410. '/label',
  8411. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8412. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8413. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8414. 'A',
  8415. '/label',
  8416. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8417. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8418. 'B',
  8419. '/label',
  8420. '/div',
  8421. ];
  8422. $this->assertHtml($expected, $result);
  8423. $result = $this->Form->input('test', [
  8424. 'type' => 'radio',
  8425. 'options' => ['A', 'B'],
  8426. 'value' => 1,
  8427. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8428. ]);
  8429. $expected = [
  8430. ['div' => ['class' => 'input radio']],
  8431. '<label',
  8432. 'Test',
  8433. '/label',
  8434. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8435. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8436. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8437. 'A',
  8438. '/label',
  8439. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8440. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8441. 'B',
  8442. '/label',
  8443. '/div',
  8444. ];
  8445. $this->assertHtml($expected, $result);
  8446. $result = $this->Form->radio('test', ['A', 'B'], [
  8447. 'label' => [
  8448. 'class' => ['custom-class', 'another-class'],
  8449. 'data-name' => 'bob'
  8450. ],
  8451. 'value' => 1
  8452. ]);
  8453. $expected = [
  8454. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8455. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8456. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8457. 'A',
  8458. '/label',
  8459. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8460. ['input' => [
  8461. 'type' => 'radio',
  8462. 'name' => 'test',
  8463. 'value' => '1',
  8464. 'id' => 'test-1',
  8465. 'checked' => 'checked'
  8466. ]],
  8467. 'B',
  8468. '/label',
  8469. ];
  8470. $this->assertHtml($expected, $result);
  8471. }
  8472. /**
  8473. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8474. *
  8475. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8476. * added to the class if checked.
  8477. *
  8478. * Also checks to make sure any custom attributes are rendered correctly
  8479. *
  8480. * @return void
  8481. */
  8482. public function testInputLabelManipulationCheckboxes()
  8483. {
  8484. $result = $this->Form->input('checkbox1', [
  8485. 'label' => 'My checkboxes',
  8486. 'multiple' => 'checkbox',
  8487. 'type' => 'select',
  8488. 'options' => [
  8489. ['text' => 'First Checkbox', 'value' => 1],
  8490. ['text' => 'Second Checkbox', 'value' => 2]
  8491. ],
  8492. 'labelOptions' => ['class' => 'custom-class'],
  8493. 'value' => ['1']
  8494. ]);
  8495. $expected = [
  8496. ['div' => ['class' => 'input select']],
  8497. ['label' => ['for' => 'checkbox1']],
  8498. 'My checkboxes',
  8499. '/label',
  8500. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8501. ['div' => ['class' => 'checkbox']],
  8502. ['label' => [
  8503. 'class' => 'custom-class selected',
  8504. 'for' => 'checkbox1-1'
  8505. ]],
  8506. ['input' => [
  8507. 'type' => 'checkbox',
  8508. 'name' => 'checkbox1[]',
  8509. 'value' => '1',
  8510. 'id' => 'checkbox1-1',
  8511. 'checked' => 'checked'
  8512. ]],
  8513. 'First Checkbox',
  8514. '/label',
  8515. '/div',
  8516. ['div' => ['class' => 'checkbox']],
  8517. ['label' => [
  8518. 'class' => 'custom-class',
  8519. 'for' => 'checkbox1-2'
  8520. ]],
  8521. ['input' => [
  8522. 'type' => 'checkbox',
  8523. 'name' => 'checkbox1[]',
  8524. 'value' => '2',
  8525. 'id' => 'checkbox1-2'
  8526. ]],
  8527. 'Second Checkbox',
  8528. '/label',
  8529. '/div',
  8530. '/div'
  8531. ];
  8532. $this->assertHtml($expected, $result);
  8533. $result = $this->Form->input('checkbox1', [
  8534. 'label' => 'My checkboxes',
  8535. 'multiple' => 'checkbox',
  8536. 'type' => 'select',
  8537. 'options' => [
  8538. ['text' => 'First Checkbox', 'value' => 1],
  8539. ['text' => 'Second Checkbox', 'value' => 2]
  8540. ],
  8541. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8542. 'value' => ['1']
  8543. ]);
  8544. $expected = [
  8545. ['div' => ['class' => 'input select']],
  8546. ['label' => ['for' => 'checkbox1']],
  8547. 'My checkboxes',
  8548. '/label',
  8549. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8550. ['div' => ['class' => 'checkbox']],
  8551. ['label' => [
  8552. 'class' => 'custom-class another-class selected',
  8553. 'data-name' => 'bob',
  8554. 'for' => 'checkbox1-1'
  8555. ]],
  8556. ['input' => [
  8557. 'type' => 'checkbox',
  8558. 'name' => 'checkbox1[]',
  8559. 'value' => '1',
  8560. 'id' => 'checkbox1-1',
  8561. 'checked' => 'checked'
  8562. ]],
  8563. 'First Checkbox',
  8564. '/label',
  8565. '/div',
  8566. ['div' => ['class' => 'checkbox']],
  8567. ['label' => [
  8568. 'class' => 'custom-class another-class',
  8569. 'data-name' => 'bob',
  8570. 'for' => 'checkbox1-2'
  8571. ]],
  8572. ['input' => [
  8573. 'type' => 'checkbox',
  8574. 'name' => 'checkbox1[]',
  8575. 'value' => '2',
  8576. 'id' => 'checkbox1-2'
  8577. ]],
  8578. 'Second Checkbox',
  8579. '/label',
  8580. '/div',
  8581. '/div'
  8582. ];
  8583. $this->assertHtml($expected, $result);
  8584. }
  8585. }