FormHelperTest.php 195 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @since CakePHP(tm) v 1.2.0.4206
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\View\Helper;
  18. use Cake\Controller\Controller;
  19. use Cake\Core\App;
  20. use Cake\Core\Configure;
  21. use Cake\Core\Plugin;
  22. use Cake\Network\Request;
  23. use Cake\ORM\Entity;
  24. use Cake\ORM\Table;
  25. use Cake\ORM\TableRegistry;
  26. use Cake\Routing\Router;
  27. use Cake\TestSuite\TestCase;
  28. use Cake\Utility\Security;
  29. use Cake\View\Helper\FormHelper;
  30. use Cake\View\Helper\HtmlHelper;
  31. use Cake\View\View;
  32. /**
  33. * Test stub.
  34. */
  35. class Article extends Entity {
  36. }
  37. /**
  38. * ContactTestController class
  39. *
  40. */
  41. class ContactTestController extends Controller {
  42. /**
  43. * uses property
  44. *
  45. * @var mixed null
  46. */
  47. public $uses = null;
  48. }
  49. /**
  50. * Contact class
  51. *
  52. */
  53. class ContactsTable extends Table {
  54. /**
  55. * Default schema
  56. *
  57. * @var array
  58. */
  59. protected $_schema = array(
  60. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  61. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  65. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  66. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  67. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  68. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null),
  69. '_constraints' => array('primary' => ['type' => 'primary', 'columns' => ['id']])
  70. );
  71. /**
  72. * validate property
  73. *
  74. * @var array
  75. */
  76. public $validate = array(
  77. 'non_existing' => array(),
  78. 'idontexist' => array(),
  79. 'imrequired' => array('_allowEmpty' => false, array('rule' => array('between', 5, 30))),
  80. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  81. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  82. 'imrequiredonboth' => array(
  83. 'required' => array('rule' => 'alphaNumeric'),
  84. ),
  85. 'string_required' => 'notEmpty',
  86. 'imalsorequired' => array('_allowEmpty' => false, array('rule' => 'alphaNumeric')),
  87. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  88. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  89. 'imnotrequired' => array(
  90. '_allowEmpty' => true,
  91. array('rule' => 'alphaNumeric')
  92. ),
  93. 'imalsonotrequired' => array(
  94. '_allowEmpty' => true,
  95. 'alpha' => array('rule' => 'alphaNumeric'),
  96. 'between' => array('rule' => array('between', 5, 30)),
  97. ),
  98. 'imnotrequiredeither' => array(
  99. '_allowEmpty' => true,
  100. array('rule' => array('between', 5, 30))
  101. ),
  102. 'iamrequiredalways' => array(
  103. 'email' => array('rule' => 'email'),
  104. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  105. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  106. ),
  107. 'boolean_field' => array('rule' => 'boolean')
  108. );
  109. /**
  110. * hasAndBelongsToMany property
  111. *
  112. * @var array
  113. */
  114. public $hasAndBelongsToMany = array(
  115. 'ContactTag' => array(
  116. 'className' => 'Cake\Test\TestCase\View\Helper\ContactTag',
  117. 'with' => 'Cake\Test\TestCase\View\Helper\ContactTagsContact'
  118. )
  119. );
  120. /**
  121. * hasAndBelongsToMany property
  122. *
  123. * @var array
  124. */
  125. public $belongsTo = array(
  126. 'User' => array('className' => 'UserForm'
  127. ));
  128. /**
  129. * Initializes the schema and validation rules.
  130. *
  131. * @return void
  132. */
  133. public function initialize(array $config) {
  134. $this->schema($this->_schema);
  135. }
  136. }
  137. /**
  138. * ContactTagsContact class
  139. *
  140. */
  141. class ContactTagsContactsTable extends Table {
  142. /**
  143. * Default schema
  144. *
  145. * @var array
  146. */
  147. protected $_schema = array(
  148. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  149. 'contact_tag_id' => array(
  150. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  151. )
  152. );
  153. /**
  154. * schema method
  155. *
  156. * @return void
  157. */
  158. public function setSchema($schema) {
  159. $this->_schema = $schema;
  160. }
  161. }
  162. /**
  163. * ContactNonStandardPk class
  164. *
  165. */
  166. class ContactNonStandardPk extends ContactsTable {
  167. /**
  168. * primaryKey property
  169. *
  170. * @var string
  171. */
  172. public $primaryKey = 'pk';
  173. /**
  174. * schema method
  175. *
  176. * @return void
  177. */
  178. public function schema($field = false) {
  179. $this->_schema = parent::schema();
  180. $this->_schema['pk'] = $this->_schema['id'];
  181. unset($this->_schema['id']);
  182. return $this->_schema;
  183. }
  184. }
  185. /**
  186. * ContactTag class
  187. *
  188. */
  189. class ContactTagsTable extends Table {
  190. /**
  191. * schema definition
  192. *
  193. * @var array
  194. */
  195. protected $_schema = array(
  196. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  197. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  198. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  199. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  200. );
  201. }
  202. /**
  203. * UserForm class
  204. *
  205. */
  206. class UserFormsTable extends Table {
  207. /**
  208. * hasMany property
  209. *
  210. * @var array
  211. */
  212. public $hasMany = array(
  213. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  214. ));
  215. /**
  216. * schema definition
  217. *
  218. * @var array
  219. */
  220. protected $_schema = array(
  221. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  222. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  223. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  224. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  225. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  226. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  227. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  228. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  229. );
  230. }
  231. /**
  232. * OpenidUrl class
  233. *
  234. */
  235. class OpenidUrlsTable extends Table {
  236. /**
  237. * belongsTo property
  238. *
  239. * @var array
  240. */
  241. public $belongsTo = array('UserForm' => array(
  242. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  243. ));
  244. /**
  245. * validate property
  246. *
  247. * @var array
  248. */
  249. public $validate = array('openid_not_registered' => array());
  250. /**
  251. * schema method
  252. *
  253. * @var array
  254. */
  255. protected $_schema = array(
  256. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  257. 'user_form_id' => array(
  258. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  259. ),
  260. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  261. );
  262. /**
  263. * beforeValidate method
  264. *
  265. * @return void
  266. */
  267. public function beforeValidate($options = array()) {
  268. $this->invalidate('openid_not_registered');
  269. return true;
  270. }
  271. }
  272. /**
  273. * ValidateUser class
  274. *
  275. */
  276. class ValidateUsersTable extends Table {
  277. /**
  278. * hasOne property
  279. *
  280. * @var array
  281. */
  282. public $hasOne = array('ValidateProfile' => array(
  283. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  284. ));
  285. /**
  286. * schema method
  287. *
  288. * @var array
  289. */
  290. protected $_schema = array(
  291. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  292. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  293. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  294. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  295. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => '6,3'),
  296. 'ratio' => array('type' => 'decimal', 'null' => false, 'length' => '10,6'),
  297. 'population' => array('type' => 'decimal', 'null' => false, 'length' => '15,0'),
  298. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  299. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  300. '_constraints' => array('primary' => ['type' => 'primary', 'columns' => ['id']])
  301. );
  302. /**
  303. * Initializes the schema
  304. *
  305. * @return void
  306. */
  307. public function initialize(array $config) {
  308. $this->schema($this->_schema);
  309. }
  310. /**
  311. * beforeValidate method
  312. *
  313. * @param array $options
  314. * @return void
  315. */
  316. public function beforeValidate($options = array()) {
  317. $this->invalidate('email');
  318. return false;
  319. }
  320. }
  321. /**
  322. * ValidateProfile class
  323. *
  324. */
  325. class ValidateProfilesTable extends Table {
  326. /**
  327. * useTable property
  328. *
  329. * @var boolean
  330. */
  331. public $useTable = false;
  332. /**
  333. * schema property
  334. *
  335. * @var array
  336. */
  337. protected $_schema = array(
  338. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  339. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  340. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  341. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  342. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  343. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  344. );
  345. /**
  346. * hasOne property
  347. *
  348. * @var array
  349. */
  350. public $hasOne = array('ValidateItem' => array(
  351. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  352. ));
  353. /**
  354. * belongsTo property
  355. *
  356. * @var array
  357. */
  358. public $belongsTo = array('ValidateUser' => array(
  359. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  360. ));
  361. /**
  362. * beforeValidate method
  363. *
  364. * @return void
  365. */
  366. public function beforeValidate($options = array()) {
  367. $this->invalidate('full_name');
  368. $this->invalidate('city');
  369. return false;
  370. }
  371. }
  372. /**
  373. * ValidateItem class
  374. *
  375. */
  376. class ValidateItemsTable extends Table {
  377. /**
  378. * schema property
  379. *
  380. * @var array
  381. */
  382. protected $_schema = array(
  383. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  384. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  385. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  386. 'description' => array(
  387. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  388. ),
  389. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  390. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  391. );
  392. /**
  393. * belongsTo property
  394. *
  395. * @var array
  396. */
  397. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  398. /**
  399. * beforeValidate method
  400. *
  401. * @return void
  402. */
  403. public function beforeValidate($options = array()) {
  404. $this->invalidate('description');
  405. return false;
  406. }
  407. }
  408. /**
  409. * TestMail class
  410. *
  411. */
  412. class TestMailsTable extends Table {
  413. }
  414. /**
  415. * FormHelperTest class
  416. *
  417. * @property FormHelper $Form
  418. */
  419. class FormHelperTest extends TestCase {
  420. /**
  421. * Fixtures to be used
  422. *
  423. * @var array
  424. */
  425. public $fixtures = array('core.article');
  426. /**
  427. * Do not load the fixtures by default
  428. *
  429. * @var boolean
  430. */
  431. public $autoFixtures = false;
  432. /**
  433. * setUp method
  434. *
  435. * @return void
  436. */
  437. public function setUp() {
  438. parent::setUp();
  439. Configure::write('Config.language', 'eng');
  440. Configure::write('App.base', '');
  441. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  442. Configure::delete('Asset');
  443. $this->View = new View(null);
  444. $this->Form = new FormHelper($this->View);
  445. $this->Form->Html = new HtmlHelper($this->View);
  446. $this->Form->request = new Request('articles/add');
  447. $this->Form->request->here = '/articles/add';
  448. $this->Form->request['controller'] = 'articles';
  449. $this->Form->request['action'] = 'add';
  450. $this->Form->request->webroot = '';
  451. $this->Form->request->base = '';
  452. $this->dateRegex = array(
  453. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  454. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  455. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  456. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  457. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  458. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  459. );
  460. $this->article = [
  461. 'schema' => [
  462. 'id' => ['type' => 'integer'],
  463. 'author_id' => ['type' => 'integer', 'null' => true],
  464. 'title' => ['type' => 'string', 'null' => true],
  465. 'body' => 'text',
  466. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  467. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  468. ],
  469. 'required' => [
  470. 'author_id' => true,
  471. 'title' => true,
  472. ]
  473. ];
  474. Configure::write('Security.salt', 'foo!');
  475. Router::connect('/:controller', array('action' => 'index'));
  476. Router::connect('/:controller/:action/*');
  477. }
  478. /**
  479. * tearDown method
  480. *
  481. * @return void
  482. */
  483. public function tearDown() {
  484. parent::tearDown();
  485. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  486. TableRegistry::clear();
  487. }
  488. /**
  489. * Test registering a new widget class and rendering it.
  490. *
  491. * @return void
  492. */
  493. public function testAddWidgetAndRenderWidget() {
  494. $data = [
  495. 'val' => 1
  496. ];
  497. $mock = $this->getMock('Cake\View\Widget\WidgetInterface');
  498. $this->assertNull($this->Form->addWidget('test', $mock));
  499. $mock->expects($this->once())
  500. ->method('render')
  501. ->with($data)
  502. ->will($this->returnValue('HTML'));
  503. $result = $this->Form->widget('test', $data);
  504. $this->assertEquals('HTML', $result);
  505. }
  506. /**
  507. * Test registering an invalid widget class.
  508. *
  509. * @expectedException \RuntimeException
  510. * @return void
  511. */
  512. public function testAddWidgetInvalid() {
  513. $mock = new \StdClass();
  514. $this->Form->addWidget('test', $mock);
  515. $this->Form->widget('test');
  516. }
  517. /**
  518. * Test adding a new context class.
  519. *
  520. * @return void
  521. */
  522. public function testAddContextProvider() {
  523. $context = 'My data';
  524. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  525. $this->assertInstanceOf('Cake\Network\Request', $request);
  526. $this->assertEquals($context, $data['entity']);
  527. return $this->getMock('Cake\View\Form\ContextInterface');
  528. });
  529. $this->Form->create($context);
  530. }
  531. /**
  532. * Test adding an invalid context class.
  533. *
  534. * @expectedException RuntimeException
  535. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  536. * @return void
  537. */
  538. public function testAddContextProviderInvalid() {
  539. $context = 'My data';
  540. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  541. return new \StdClass();
  542. });
  543. $this->Form->create($context);
  544. }
  545. /**
  546. * Provides context options for create().
  547. *
  548. * @return array
  549. */
  550. public function contextSelectionProvider() {
  551. $entity = new Article();
  552. $collection = $this->getMock('Cake\Collection\Collection', ['extract'], [[$entity]]);
  553. $data = [
  554. 'schema' => [
  555. 'title' => ['type' => 'string']
  556. ]
  557. ];
  558. return [
  559. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  560. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  561. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  562. 'none' => [null, 'Cake\View\Form\NullContext'],
  563. 'false' => [false, 'Cake\View\Form\NullContext'],
  564. ];
  565. }
  566. /**
  567. * Test default context selection in create()
  568. *
  569. * @dataProvider contextSelectionProvider
  570. * @return void
  571. */
  572. public function testCreateContextSelectionBuiltIn($data, $class) {
  573. $this->loadFixtures('Article');
  574. $this->Form->create($data);
  575. $this->assertInstanceOf($class, $this->Form->context());
  576. }
  577. /**
  578. * Data provider for type option.
  579. *
  580. * @return array
  581. */
  582. public static function requestTypeProvider() {
  583. return [
  584. // type, method, override
  585. ['post', 'post', 'POST'],
  586. ['put', 'post', 'PUT'],
  587. ['patch', 'post', 'PATCH'],
  588. ['delete', 'post', 'DELETE'],
  589. ];
  590. }
  591. /**
  592. * Test creating file forms.
  593. *
  594. * @return void
  595. */
  596. public function testCreateFile() {
  597. $encoding = strtolower(Configure::read('App.encoding'));
  598. $result = $this->Form->create(false, array('type' => 'file'));
  599. $expected = array(
  600. 'form' => array(
  601. 'method' => 'post', 'action' => '/articles/add',
  602. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  603. ),
  604. 'div' => array('style' => 'display:none;'),
  605. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  606. '/div'
  607. );
  608. $this->assertTags($result, $expected);
  609. }
  610. /**
  611. * Test creating GET forms.
  612. *
  613. * @return void
  614. */
  615. public function testCreateGet() {
  616. $encoding = strtolower(Configure::read('App.encoding'));
  617. $result = $this->Form->create(false, array('type' => 'get'));
  618. $expected = array('form' => array(
  619. 'method' => 'get', 'action' => '/articles/add',
  620. 'accept-charset' => $encoding
  621. ));
  622. $this->assertTags($result, $expected);
  623. }
  624. /**
  625. * test the create() method
  626. *
  627. * @dataProvider requestTypeProvider
  628. * @return void
  629. */
  630. public function testCreateTypeOptions($type, $method, $override) {
  631. $encoding = strtolower(Configure::read('App.encoding'));
  632. $result = $this->Form->create(false, array('type' => $type));
  633. $expected = array(
  634. 'form' => array(
  635. 'method' => $method, 'action' => '/articles/add',
  636. 'accept-charset' => $encoding
  637. ),
  638. 'div' => array('style' => 'display:none;'),
  639. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => $override),
  640. '/div'
  641. );
  642. $this->assertTags($result, $expected);
  643. }
  644. /**
  645. * Test opening a form for an update operation.
  646. *
  647. * @return void
  648. */
  649. public function testCreateUpdateForm() {
  650. $encoding = strtolower(Configure::read('App.encoding'));
  651. $this->Form->request->here = '/articles/edit/1';
  652. $this->Form->request['action'] = 'edit';
  653. $this->article['defaults']['id'] = 1;
  654. $result = $this->Form->create($this->article);
  655. $expected = array(
  656. 'form' => array(
  657. 'method' => 'post', 'action' => '/articles/edit/1',
  658. 'accept-charset' => $encoding
  659. ),
  660. 'div' => array('style' => 'display:none;'),
  661. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  662. '/div'
  663. );
  664. $this->assertTags($result, $expected);
  665. }
  666. /**
  667. * Test the onsubmit option for create()
  668. *
  669. * @return void
  670. */
  671. public function testCreateOnSubmit() {
  672. $this->Form->request->data = [];
  673. $this->Form->request['controller'] = 'articles';
  674. $result = $this->Form->create($this->article, ['url' => ['action' => 'index', 'param'], 'default' => false]);
  675. $expected = array(
  676. 'form' => array(
  677. 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/articles/index/param',
  678. 'accept-charset' => 'utf-8'
  679. ),
  680. 'div' => array('style' => 'display:none;'),
  681. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  682. '/div'
  683. );
  684. $this->assertTags($result, $expected);
  685. $this->Form->request->data = [];
  686. $this->Form->request['controller'] = 'articles';
  687. $result = $this->Form->create($this->article, array(
  688. 'url' => array('action' => 'index', 'param'),
  689. 'default' => false,
  690. 'onsubmit' => 'someFunction();'
  691. ));
  692. $expected = array(
  693. 'form' => array(
  694. 'method' => 'post',
  695. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  696. 'action' => '/articles/index/param',
  697. 'accept-charset' => 'utf-8'
  698. ),
  699. 'div' => array('style' => 'display:none;'),
  700. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  701. '/div'
  702. );
  703. $this->assertTags($result, $expected);
  704. }
  705. /**
  706. * test create() with automatic url generation
  707. *
  708. * @return void
  709. */
  710. public function testCreateAutoUrl() {
  711. $encoding = strtolower(Configure::read('App.encoding'));
  712. $this->Form->request['action'] = 'delete';
  713. $this->Form->request->here = '/articles/delete/10';
  714. $this->Form->request->base = '';
  715. $result = $this->Form->create($this->article);
  716. $expected = array(
  717. 'form' => array(
  718. 'method' => 'post', 'action' => '/articles/delete/10',
  719. 'accept-charset' => $encoding
  720. ),
  721. 'div' => array('style' => 'display:none;'),
  722. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  723. '/div'
  724. );
  725. $this->assertTags($result, $expected);
  726. $this->article['defaults'] = ['id' => 1];
  727. $this->Form->request->here = '/articles/edit/1';
  728. $this->Form->request['action'] = 'delete';
  729. $result = $this->Form->create($this->article, ['action' => 'edit']);
  730. $expected = array(
  731. 'form' => array(
  732. 'method' => 'post',
  733. 'action' => '/articles/edit/1',
  734. 'accept-charset' => $encoding
  735. ),
  736. 'div' => array('style' => 'display:none;'),
  737. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  738. '/div'
  739. );
  740. $this->assertTags($result, $expected);
  741. $this->Form->request['action'] = 'add';
  742. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  743. $expected = array(
  744. 'form' => array(
  745. 'method' => 'post',
  746. 'action' => '/articles/publish/1',
  747. 'accept-charset' => $encoding
  748. ),
  749. 'div' => array('style' => 'display:none;'),
  750. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  751. '/div'
  752. );
  753. $this->assertTags($result, $expected);
  754. $result = $this->Form->create($this->article, array('url' => '/articles/publish'));
  755. $expected = array(
  756. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  757. 'div' => array('style' => 'display:none;'),
  758. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  759. '/div'
  760. );
  761. $this->assertTags($result, $expected);
  762. $this->Form->request['controller'] = 'pages';
  763. $result = $this->Form->create($this->article, array('action' => 'signup'));
  764. $expected = array(
  765. 'form' => array(
  766. 'method' => 'post', 'action' => '/pages/signup/1',
  767. 'accept-charset' => $encoding
  768. ),
  769. 'div' => array('style' => 'display:none;'),
  770. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  771. '/div'
  772. );
  773. $this->assertTags($result, $expected);
  774. }
  775. /**
  776. * test create() with a custom route
  777. *
  778. * @return void
  779. */
  780. public function testCreateCustomRoute() {
  781. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  782. $encoding = strtolower(Configure::read('App.encoding'));
  783. $this->Form->request['controller'] = 'users';
  784. $result = $this->Form->create(false, array('action' => 'login'));
  785. $expected = array(
  786. 'form' => array(
  787. 'method' => 'post', 'action' => '/login',
  788. 'accept-charset' => $encoding
  789. ),
  790. 'div' => array('style' => 'display:none;'),
  791. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  792. '/div'
  793. );
  794. $this->assertTags($result, $expected);
  795. }
  796. /**
  797. * test automatic accept-charset overriding
  798. *
  799. * @return void
  800. */
  801. public function testCreateWithAcceptCharset() {
  802. $result = $this->Form->create($this->article, array(
  803. 'type' => 'post', 'action' => 'index', 'encoding' => 'iso-8859-1'
  804. )
  805. );
  806. $expected = array(
  807. 'form' => array(
  808. 'method' => 'post', 'action' => '/articles',
  809. 'accept-charset' => 'iso-8859-1'
  810. ),
  811. 'div' => array('style' => 'display:none;'),
  812. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  813. '/div'
  814. );
  815. $this->assertTags($result, $expected);
  816. }
  817. /**
  818. * Test base form URL when url param is passed with multiple parameters (&)
  819. *
  820. */
  821. public function testCreateQuerystringrequest() {
  822. $encoding = strtolower(Configure::read('App.encoding'));
  823. $result = $this->Form->create($this->article, array(
  824. 'type' => 'post',
  825. 'escape' => false,
  826. 'url' => array(
  827. 'controller' => 'controller',
  828. 'action' => 'action',
  829. '?' => array('param1' => 'value1', 'param2' => 'value2')
  830. )
  831. ));
  832. $expected = array(
  833. 'form' => array(
  834. 'method' => 'post',
  835. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  836. 'accept-charset' => $encoding
  837. ),
  838. 'div' => array('style' => 'display:none;'),
  839. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  840. '/div'
  841. );
  842. $this->assertTags($result, $expected);
  843. $result = $this->Form->create($this->article, array(
  844. 'type' => 'post',
  845. 'url' => array(
  846. 'controller' => 'controller',
  847. 'action' => 'action',
  848. '?' => array('param1' => 'value1', 'param2' => 'value2')
  849. )
  850. ));
  851. $this->assertTags($result, $expected);
  852. }
  853. /**
  854. * test that create() doesn't cause errors by multiple id's being in the primary key
  855. * as could happen with multiple select or checkboxes.
  856. *
  857. * @return void
  858. */
  859. public function testCreateWithMultipleIdInData() {
  860. $encoding = strtolower(Configure::read('App.encoding'));
  861. $this->Form->request->data['Article']['id'] = array(1, 2);
  862. $result = $this->Form->create($this->article);
  863. $expected = array(
  864. 'form' => array(
  865. 'method' => 'post',
  866. 'action' => '/articles/add',
  867. 'accept-charset' => $encoding
  868. ),
  869. 'div' => array('style' => 'display:none;'),
  870. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  871. '/div'
  872. );
  873. $this->assertTags($result, $expected);
  874. }
  875. /**
  876. * test that create() doesn't add in extra passed params.
  877. *
  878. * @return void
  879. */
  880. public function testCreatePassedArgs() {
  881. $encoding = strtolower(Configure::read('App.encoding'));
  882. $this->Form->request->data['Article']['id'] = 1;
  883. $result = $this->Form->create($this->article, array(
  884. 'type' => 'post',
  885. 'escape' => false,
  886. 'url' => array(
  887. 'action' => 'edit',
  888. 'myparam'
  889. )
  890. ));
  891. $expected = array(
  892. 'form' => array(
  893. 'method' => 'post',
  894. 'action' => '/articles/edit/myparam',
  895. 'accept-charset' => $encoding
  896. ),
  897. 'div' => array('style' => 'display:none;'),
  898. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  899. '/div'
  900. );
  901. $this->assertTags($result, $expected);
  902. }
  903. /**
  904. * test creating a get form, and get form inputs.
  905. *
  906. * @return void
  907. */
  908. public function testGetFormCreate() {
  909. $encoding = strtolower(Configure::read('App.encoding'));
  910. $result = $this->Form->create($this->article, array('type' => 'get'));
  911. $this->assertTags($result, array('form' => array(
  912. 'method' => 'get', 'action' => '/articles/add',
  913. 'accept-charset' => $encoding
  914. )));
  915. $result = $this->Form->text('title');
  916. $this->assertTags($result, array('input' => array(
  917. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  918. )));
  919. $result = $this->Form->password('password');
  920. $this->assertTags($result, array('input' => array(
  921. 'name' => 'password', 'type' => 'password'
  922. )));
  923. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  924. $result = $this->Form->text('user_form');
  925. $this->assertTags($result, array('input' => array(
  926. 'name' => 'user_form', 'type' => 'text'
  927. )));
  928. }
  929. /**
  930. * test get form, and inputs when the model param is false
  931. *
  932. * @return void
  933. */
  934. public function testGetFormWithFalseModel() {
  935. $encoding = strtolower(Configure::read('App.encoding'));
  936. $this->Form->request['controller'] = 'contact_test';
  937. $result = $this->Form->create(false, array(
  938. 'type' => 'get', 'url' => array('controller' => 'contact_test')
  939. ));
  940. $expected = array('form' => array(
  941. 'method' => 'get', 'action' => '/contact_test/add',
  942. 'accept-charset' => $encoding
  943. ));
  944. $this->assertTags($result, $expected);
  945. $result = $this->Form->text('reason');
  946. $expected = array(
  947. 'input' => array('type' => 'text', 'name' => 'reason')
  948. );
  949. $this->assertTags($result, $expected);
  950. }
  951. /**
  952. * testFormCreateWithSecurity method
  953. *
  954. * Test form->create() with security key.
  955. *
  956. * @return void
  957. */
  958. public function testCreateWithSecurity() {
  959. $this->Form->request->params['_csrfToken'] = 'testKey';
  960. $encoding = strtolower(Configure::read('App.encoding'));
  961. $result = $this->Form->create($this->article, [
  962. 'url' => '/articles/publish',
  963. ]);
  964. $expected = array(
  965. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  966. 'div' => array('style' => 'display:none;'),
  967. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  968. array('input' => array(
  969. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  970. )),
  971. '/div'
  972. );
  973. $this->assertTags($result, $expected);
  974. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  975. $expected['form']['id'] = 'MyForm';
  976. $this->assertTags($result, $expected);
  977. }
  978. /**
  979. * testFormCreateGetNoSecurity method
  980. *
  981. * Test form->create() with no security key as its a get form
  982. *
  983. * @return void
  984. */
  985. public function testCreateEndGetNoSecurity() {
  986. $this->Form->request->params['_csrfToken'] = 'testKey';
  987. $encoding = strtolower(Configure::read('App.encoding'));
  988. $article = new Article();
  989. $result = $this->Form->create($article, [
  990. 'type' => 'get',
  991. 'url' => '/contacts/add'
  992. ]);
  993. $this->assertNotContains('testKey', $result);
  994. $result = $this->Form->end('Save');
  995. $this->assertNotContains('testKey', $result);
  996. }
  997. /**
  998. * test that create() clears the fields property so it starts fresh
  999. *
  1000. * @return void
  1001. */
  1002. public function testCreateClearingFields() {
  1003. $this->Form->fields = array('model_id');
  1004. $this->Form->create($this->article);
  1005. $this->assertEquals(array(), $this->Form->fields);
  1006. }
  1007. /**
  1008. * Tests form hash generation with model-less data
  1009. *
  1010. * @return void
  1011. */
  1012. public function testValidateHashNoModel() {
  1013. $this->Form->request->params['_Token'] = 'foo';
  1014. $result = $this->Form->secure(array('anything'));
  1015. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1016. }
  1017. /**
  1018. * Tests that hidden fields generated for checkboxes don't get locked
  1019. *
  1020. * @return void
  1021. */
  1022. public function testNoCheckboxLocking() {
  1023. $this->Form->request->params['_Token'] = 'foo';
  1024. $this->assertSame([], $this->Form->fields);
  1025. $this->Form->checkbox('check', array('value' => '1'));
  1026. $this->assertSame($this->Form->fields, array('check'));
  1027. }
  1028. /**
  1029. * testFormSecurityFields method
  1030. *
  1031. * Test generation of secure form hash generation.
  1032. *
  1033. * @return void
  1034. */
  1035. public function testFormSecurityFields() {
  1036. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  1037. $this->Form->request->params['_Token'] = 'testKey';
  1038. $result = $this->Form->secure($fields);
  1039. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  1040. $hash .= ':' . 'Model.valid';
  1041. $hash = urlencode($hash);
  1042. $expected = array(
  1043. 'div' => array('style' => 'display:none;'),
  1044. array('input' => array(
  1045. 'type' => 'hidden',
  1046. 'name' => '_Token[fields]',
  1047. 'value' => $hash
  1048. )),
  1049. array('input' => array(
  1050. 'type' => 'hidden',
  1051. 'name' => '_Token[unlocked]',
  1052. 'value' => '',
  1053. )),
  1054. '/div'
  1055. );
  1056. $this->assertTags($result, $expected);
  1057. }
  1058. /**
  1059. * Tests correct generation of number fields for double and float fields
  1060. *
  1061. * @return void
  1062. */
  1063. public function testTextFieldGenerationForFloats() {
  1064. $this->article['schema'] = [
  1065. 'foo' => [
  1066. 'type' => 'float',
  1067. 'null' => false,
  1068. 'default' => null,
  1069. 'length' => 10
  1070. ]
  1071. ];
  1072. $this->Form->create($this->article);
  1073. $result = $this->Form->input('foo');
  1074. $expected = array(
  1075. 'div' => array('class' => 'input number'),
  1076. 'label' => array('for' => 'foo'),
  1077. 'Foo',
  1078. '/label',
  1079. array('input' => array(
  1080. 'type' => 'number',
  1081. 'name' => 'foo',
  1082. 'id' => 'foo',
  1083. 'step' => 'any'
  1084. )),
  1085. '/div'
  1086. );
  1087. $this->assertTags($result, $expected);
  1088. $result = $this->Form->input('foo', array('step' => 0.5));
  1089. $expected = array(
  1090. 'div' => array('class' => 'input number'),
  1091. 'label' => array('for' => 'foo'),
  1092. 'Foo',
  1093. '/label',
  1094. array('input' => array(
  1095. 'type' => 'number',
  1096. 'name' => 'foo',
  1097. 'id' => 'foo',
  1098. 'step' => '0.5'
  1099. )),
  1100. '/div'
  1101. );
  1102. $this->assertTags($result, $expected);
  1103. }
  1104. /**
  1105. * Tests correct generation of number fields for integer fields
  1106. *
  1107. * @return void
  1108. */
  1109. public function testTextFieldTypeNumberGenerationForIntegers() {
  1110. TableRegistry::get('Contacts', [
  1111. 'className' => __NAMESPACE__ . '\ContactsTable'
  1112. ]);
  1113. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1114. $result = $this->Form->input('age');
  1115. $expected = array(
  1116. 'div' => array('class' => 'input number'),
  1117. 'label' => array('for' => 'age'),
  1118. 'Age',
  1119. '/label',
  1120. array('input' => array(
  1121. 'type' => 'number', 'name' => 'age',
  1122. 'id' => 'age'
  1123. )),
  1124. '/div'
  1125. );
  1126. $this->assertTags($result, $expected);
  1127. }
  1128. /**
  1129. * Tests correct generation of file upload fields for binary fields
  1130. *
  1131. * @return void
  1132. */
  1133. public function testFileUploadFieldTypeGenerationForBinaries() {
  1134. $table = TableRegistry::get('Contacts', [
  1135. 'className' => __NAMESPACE__ . '\ContactsTable'
  1136. ]);
  1137. $table->schema(array('foo' => array(
  1138. 'type' => 'binary',
  1139. 'null' => false,
  1140. 'default' => null,
  1141. 'length' => 1024
  1142. )));
  1143. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1144. $result = $this->Form->input('foo');
  1145. $expected = array(
  1146. 'div' => array('class' => 'input file'),
  1147. 'label' => array('for' => 'foo'),
  1148. 'Foo',
  1149. '/label',
  1150. array('input' => array(
  1151. 'type' => 'file', 'name' => 'foo',
  1152. 'id' => 'foo'
  1153. )),
  1154. '/div'
  1155. );
  1156. $this->assertTags($result, $expected);
  1157. }
  1158. /**
  1159. * testFormSecurityMultipleFields method
  1160. *
  1161. * Test secure() with multiple row form. Ensure hash is correct.
  1162. *
  1163. * @return void
  1164. */
  1165. public function testFormSecurityMultipleFields() {
  1166. $this->Form->request->params['_Token'] = 'foo';
  1167. $fields = array(
  1168. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1169. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1170. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1171. );
  1172. $result = $this->Form->secure($fields);
  1173. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1174. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1175. $expected = array(
  1176. 'div' => array('style' => 'display:none;'),
  1177. array('input' => array(
  1178. 'type' => 'hidden', 'name' => '_Token[fields]',
  1179. 'value' => $hash
  1180. )),
  1181. array('input' => array(
  1182. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1183. 'value' => ''
  1184. )),
  1185. '/div'
  1186. );
  1187. $this->assertTags($result, $expected);
  1188. }
  1189. /**
  1190. * testFormSecurityMultipleSubmitButtons
  1191. *
  1192. * test form submit generation and ensure that _Token is only created on end()
  1193. *
  1194. * @return void
  1195. */
  1196. public function testFormSecurityMultipleSubmitButtons() {
  1197. $this->Form->request->params['_Token'] = 'testKey';
  1198. $this->Form->create($this->article);
  1199. $this->Form->text('Address.title');
  1200. $this->Form->text('Address.first_name');
  1201. $result = $this->Form->submit('Save', array('name' => 'save'));
  1202. $expected = array(
  1203. 'div' => array('class' => 'submit'),
  1204. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  1205. '/div',
  1206. );
  1207. $this->assertTags($result, $expected);
  1208. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  1209. $expected = array(
  1210. 'div' => array('class' => 'submit'),
  1211. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  1212. '/div',
  1213. );
  1214. $this->assertTags($result, $expected);
  1215. $result = $this->Form->end();
  1216. $expected = array(
  1217. 'div' => array('style' => 'display:none;'),
  1218. array('input' => array(
  1219. 'type' => 'hidden',
  1220. 'name' => '_Token[fields]',
  1221. 'value'
  1222. )),
  1223. array('input' => array(
  1224. 'type' => 'hidden',
  1225. 'name' => '_Token[unlocked]',
  1226. 'value' => 'cancel%7Csave'
  1227. )),
  1228. '/div'
  1229. );
  1230. $this->assertTags($result, $expected);
  1231. }
  1232. /**
  1233. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1234. *
  1235. * @return void
  1236. */
  1237. public function testSecurityButtonNestedNamed() {
  1238. $key = 'testKey';
  1239. $this->Form->request->params['_csrfToken'] = $key;
  1240. $this->Form->create('Addresses');
  1241. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1242. $result = $this->Form->unlockField();
  1243. $this->assertEquals(array('Address.button'), $result);
  1244. }
  1245. /**
  1246. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1247. *
  1248. * @return void
  1249. */
  1250. public function testSecuritySubmitNestedNamed() {
  1251. $this->Form->request->params['_Token'] = 'testKey';
  1252. $this->Form->create($this->article);
  1253. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1254. $result = $this->Form->unlockField();
  1255. $this->assertEquals(array('Address.button'), $result);
  1256. }
  1257. /**
  1258. * Test that the correct fields are unlocked for image submits with no names.
  1259. *
  1260. * @return void
  1261. */
  1262. public function testSecuritySubmitImageNoName() {
  1263. $key = 'testKey';
  1264. $this->Form->request->params['_Token'] = 'testKey';
  1265. $this->Form->create(false);
  1266. $result = $this->Form->submit('save.png');
  1267. $expected = array(
  1268. 'div' => array('class' => 'submit'),
  1269. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  1270. '/div'
  1271. );
  1272. $this->assertTags($result, $expected);
  1273. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  1274. }
  1275. /**
  1276. * Test that the correct fields are unlocked for image submits with names.
  1277. *
  1278. * @return void
  1279. */
  1280. public function testSecuritySubmitImageName() {
  1281. $this->Form->request->params['_Token'] = 'testKey';
  1282. $this->Form->create(null);
  1283. $result = $this->Form->submit('save.png', array('name' => 'test'));
  1284. $expected = array(
  1285. 'div' => array('class' => 'submit'),
  1286. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  1287. '/div'
  1288. );
  1289. $this->assertTags($result, $expected);
  1290. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  1291. }
  1292. /**
  1293. * testFormSecurityMultipleInputFields method
  1294. *
  1295. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1296. *
  1297. * @return void
  1298. */
  1299. public function testFormSecurityMultipleInputFields() {
  1300. $this->Form->request->params['_Token'] = 'testKey';
  1301. $this->Form->create();
  1302. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1303. $this->Form->input('Addresses.0.title');
  1304. $this->Form->input('Addresses.0.first_name');
  1305. $this->Form->input('Addresses.0.last_name');
  1306. $this->Form->input('Addresses.0.address');
  1307. $this->Form->input('Addresses.0.city');
  1308. $this->Form->input('Addresses.0.phone');
  1309. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  1310. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1311. $this->Form->input('Addresses.1.title');
  1312. $this->Form->input('Addresses.1.first_name');
  1313. $this->Form->input('Addresses.1.last_name');
  1314. $this->Form->input('Addresses.1.address');
  1315. $this->Form->input('Addresses.1.city');
  1316. $this->Form->input('Addresses.1.phone');
  1317. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  1318. $result = $this->Form->secure($this->Form->fields);
  1319. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  1320. $expected = array(
  1321. 'div' => array('style' => 'display:none;'),
  1322. array('input' => array(
  1323. 'type' => 'hidden', 'name' => '_Token[fields]',
  1324. 'value' => $hash
  1325. )),
  1326. array('input' => array(
  1327. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1328. 'value' => ''
  1329. )),
  1330. '/div'
  1331. );
  1332. $this->assertTags($result, $expected);
  1333. }
  1334. /**
  1335. * Test form security with Model.field.0 style inputs
  1336. *
  1337. * @return void
  1338. */
  1339. public function testFormSecurityArrayFields() {
  1340. $this->Form->request->params['_Token'] = 'testKey';
  1341. $this->Form->create();
  1342. $this->Form->text('Address.primary.1');
  1343. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1344. $this->Form->text('Address.secondary.1.0');
  1345. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1346. }
  1347. /**
  1348. * testFormSecurityMultipleInputDisabledFields method
  1349. *
  1350. * test secure form generation with multiple records and disabled fields.
  1351. *
  1352. * @return void
  1353. */
  1354. public function testFormSecurityMultipleInputDisabledFields() {
  1355. $this->Form->request->params['_Token'] = array(
  1356. 'unlockedFields' => array('first_name', 'address')
  1357. );
  1358. $this->Form->create();
  1359. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1360. $this->Form->text('Addresses.0.title');
  1361. $this->Form->text('Addresses.0.first_name');
  1362. $this->Form->text('Addresses.0.last_name');
  1363. $this->Form->text('Addresses.0.address');
  1364. $this->Form->text('Addresses.0.city');
  1365. $this->Form->text('Addresses.0.phone');
  1366. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1367. $this->Form->text('Addresses.1.title');
  1368. $this->Form->text('Addresses.1.first_name');
  1369. $this->Form->text('Addresses.1.last_name');
  1370. $this->Form->text('Addresses.1.address');
  1371. $this->Form->text('Addresses.1.city');
  1372. $this->Form->text('Addresses.1.phone');
  1373. $result = $this->Form->secure($this->Form->fields);
  1374. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1375. $expected = array(
  1376. 'div' => array('style' => 'display:none;'),
  1377. array('input' => array(
  1378. 'type' => 'hidden',
  1379. 'name' => '_Token[fields]',
  1380. 'value' => $hash
  1381. )),
  1382. array('input' => array(
  1383. 'type' => 'hidden',
  1384. 'name' => '_Token[unlocked]',
  1385. 'value' => 'address%7Cfirst_name',
  1386. )),
  1387. '/div'
  1388. );
  1389. $this->assertTags($result, $expected);
  1390. }
  1391. /**
  1392. * testFormSecurityInputDisabledFields method
  1393. *
  1394. * Test single record form with disabled fields.
  1395. *
  1396. * @return void
  1397. */
  1398. public function testFormSecurityInputUnlockedFields() {
  1399. $this->Form->request['_Token'] = array(
  1400. 'unlockedFields' => array('first_name', 'address')
  1401. );
  1402. $this->Form->create();
  1403. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1404. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1405. $this->Form->text('Addresses.title');
  1406. $this->Form->text('Addresses.first_name');
  1407. $this->Form->text('Addresses.last_name');
  1408. $this->Form->text('Addresses.address');
  1409. $this->Form->text('Addresses.city');
  1410. $this->Form->text('Addresses.phone');
  1411. $result = $this->Form->fields;
  1412. $expected = array(
  1413. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1414. 'Addresses.city', 'Addresses.phone'
  1415. );
  1416. $this->assertEquals($expected, $result);
  1417. $result = $this->Form->secure($expected);
  1418. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1419. $expected = array(
  1420. 'div' => array('style' => 'display:none;'),
  1421. array('input' => array(
  1422. 'type' => 'hidden',
  1423. 'name' => '_Token[fields]',
  1424. 'value' => $hash
  1425. )),
  1426. array('input' => array(
  1427. 'type' => 'hidden',
  1428. 'name' => '_Token[unlocked]',
  1429. 'value' => 'address%7Cfirst_name',
  1430. )),
  1431. '/div'
  1432. );
  1433. $this->assertTags($result, $expected);
  1434. }
  1435. /**
  1436. * test securing inputs with custom name attributes.
  1437. *
  1438. * @return void
  1439. */
  1440. public function testFormSecureWithCustomNameAttribute() {
  1441. $this->Form->request->params['_Token'] = 'testKey';
  1442. $this->Form->text('UserForm.published', array('name' => 'User[custom]'));
  1443. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1444. $this->Form->text('UserForm.published', array('name' => 'User[custom][another][value]'));
  1445. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1446. }
  1447. /**
  1448. * testFormSecuredInput method
  1449. *
  1450. * Test generation of entire secure form, assertions made on input() output.
  1451. *
  1452. * @return void
  1453. */
  1454. public function testFormSecuredInput() {
  1455. $this->markTestIncomplete('Need to revisit once models work again.');
  1456. $this->Form->request->params['_csrfToken'] = 'testKey';
  1457. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1458. $encoding = strtolower(Configure::read('App.encoding'));
  1459. $expected = array(
  1460. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1461. 'div' => array('style' => 'display:none;'),
  1462. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1463. array('input' => array(
  1464. 'type' => 'hidden', 'name' => '_csrfToken',
  1465. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1466. )),
  1467. '/div'
  1468. );
  1469. $this->assertTags($result, $expected);
  1470. $result = $this->Form->input('ValidateUser.ratio');
  1471. $expected = array(
  1472. 'div' => array('class'),
  1473. 'label' => array('for'),
  1474. 'Ratio',
  1475. '/label',
  1476. 'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
  1477. '/div',
  1478. );
  1479. $this->assertTags($result, $expected);
  1480. $result = $this->Form->input('ValidateUser.population');
  1481. $expected = array(
  1482. 'div' => array('class'),
  1483. 'label' => array('for'),
  1484. 'Population',
  1485. '/label',
  1486. 'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
  1487. '/div',
  1488. );
  1489. $this->assertTags($result, $expected);
  1490. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1491. $expected = array(
  1492. 'div' => array('class' => 'input text'),
  1493. 'label' => array('for' => 'UserFormPublished'),
  1494. 'Published',
  1495. '/label',
  1496. array('input' => array(
  1497. 'type' => 'text', 'name' => 'UserForm[published]',
  1498. 'id' => 'UserFormPublished'
  1499. )),
  1500. '/div'
  1501. );
  1502. $this->assertTags($result, $expected);
  1503. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1504. $expected = array(
  1505. 'div' => array('class' => 'input text'),
  1506. 'label' => array('for' => 'UserFormOther'),
  1507. 'Other',
  1508. '/label',
  1509. array('input' => array(
  1510. 'type' => 'text', 'name' => 'UserForm[other]',
  1511. 'id' => 'UserFormOther'
  1512. )),
  1513. '/div'
  1514. );
  1515. $this->assertTags($result, $expected);
  1516. $result = $this->Form->hidden('UserForm.stuff');
  1517. $expected = array(
  1518. 'input' => array(
  1519. 'type' => 'hidden', 'name' => 'UserForm[stuff]',
  1520. 'id' => 'UserFormStuff'
  1521. ));
  1522. $this->assertTags($result, $expected);
  1523. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1524. $expected = array('input' => array(
  1525. 'type' => 'hidden', 'name' => 'UserForm[hidden]',
  1526. 'value' => '0', 'id' => 'UserFormHidden'
  1527. ));
  1528. $this->assertTags($result, $expected);
  1529. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1530. $expected = array(
  1531. 'div' => array('class' => 'input checkbox'),
  1532. array('input' => array(
  1533. 'type' => 'hidden', 'name' => 'UserForm[something]',
  1534. 'value' => '0', 'id' => 'UserFormSomething_'
  1535. )),
  1536. array('input' => array(
  1537. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  1538. 'value' => '1', 'id' => 'UserFormSomething'
  1539. )),
  1540. 'label' => array('for' => 'UserFormSomething'),
  1541. 'Something',
  1542. '/label',
  1543. '/div'
  1544. );
  1545. $this->assertTags($result, $expected);
  1546. $result = $this->Form->fields;
  1547. $expected = array(
  1548. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1549. 'UserForm.hidden' => '0', 'UserForm.something'
  1550. );
  1551. $this->assertEquals($expected, $result);
  1552. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1553. $result = $this->Form->secure($this->Form->fields);
  1554. $expected = array(
  1555. 'div' => array('style' => 'display:none;'),
  1556. array('input' => array(
  1557. 'type' => 'hidden', 'name' => '_Token[fields]',
  1558. 'value' => $hash
  1559. )),
  1560. array('input' => array(
  1561. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1562. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1563. )),
  1564. '/div'
  1565. );
  1566. $this->assertTags($result, $expected);
  1567. }
  1568. /**
  1569. * Test secured inputs with custom names.
  1570. *
  1571. * @return void
  1572. */
  1573. public function testSecuredInputCustomName() {
  1574. $this->Form->request->params['_Token'] = 'testKey';
  1575. $this->assertEquals(array(), $this->Form->fields);
  1576. $this->Form->text('text_input', array(
  1577. 'name' => 'Option[General.default_role]',
  1578. ));
  1579. $expected = array('Option.General.default_role');
  1580. $this->assertEquals($expected, $this->Form->fields);
  1581. $this->Form->select('select_box', [1, 2], [
  1582. 'name' => 'Option[General.select_role]',
  1583. ]);
  1584. $expected = ['Option.General.default_role', 'Option.General.select_role'];
  1585. $this->assertEquals($expected, $this->Form->fields);
  1586. }
  1587. /**
  1588. * Tests that the correct keys are added to the field hash index
  1589. *
  1590. * @return void
  1591. */
  1592. public function testFormSecuredFileInput() {
  1593. $this->assertEquals(array(), $this->Form->fields);
  1594. $this->Form->file('Attachment.file');
  1595. $expected = array(
  1596. 'Attachment.file.name', 'Attachment.file.type',
  1597. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1598. 'Attachment.file.size'
  1599. );
  1600. $this->assertEquals($expected, $this->Form->fields);
  1601. }
  1602. /**
  1603. * test that multiple selects keys are added to field hash
  1604. *
  1605. * @return void
  1606. */
  1607. public function testFormSecuredMultipleSelect() {
  1608. $this->Form->request->params['_csrfToken'] = 'testKey';
  1609. $this->assertEquals(array(), $this->Form->fields);
  1610. $options = array('1' => 'one', '2' => 'two');
  1611. $this->Form->select('Model.select', $options);
  1612. $expected = array('Model.select');
  1613. $this->assertEquals($expected, $this->Form->fields);
  1614. $this->Form->fields = array();
  1615. $this->Form->select('Model.select', $options, array('multiple' => true));
  1616. $this->assertEquals($expected, $this->Form->fields);
  1617. }
  1618. /**
  1619. * testFormSecuredRadio method
  1620. *
  1621. * @return void
  1622. */
  1623. public function testFormSecuredRadio() {
  1624. $this->Form->request->params['_Token'] = 'testKey';
  1625. $this->assertEquals(array(), $this->Form->fields);
  1626. $options = array('1' => 'option1', '2' => 'option2');
  1627. $this->Form->radio('Test.test', $options);
  1628. $expected = array('Test.test');
  1629. $this->assertEquals($expected, $this->Form->fields);
  1630. }
  1631. /**
  1632. * Test that when disabled is in a list based attribute array it works.
  1633. *
  1634. * @return void
  1635. */
  1636. public function testFormSecuredAndDisabledNotAssoc() {
  1637. $this->Form->request->params['_Token'] = 'testKey';
  1638. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1639. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1640. $this->Form->text('Model.text', array('disabled'));
  1641. $this->Form->textarea('Model.textarea', array('disabled'));
  1642. $this->Form->password('Model.password', array('disabled'));
  1643. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1644. $expected = array(
  1645. 'Model.radio' => ''
  1646. );
  1647. $this->assertEquals($expected, $this->Form->fields);
  1648. }
  1649. /**
  1650. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1651. * hashing.
  1652. *
  1653. * @return void
  1654. */
  1655. public function testFormSecuredAndDisabled() {
  1656. $this->Form->request->params['_Token'] = 'testKey';
  1657. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1658. $this->Form->text('Model.text', array('disabled' => true));
  1659. $this->Form->password('Model.text', array('disabled' => true));
  1660. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1661. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1662. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1663. $this->Form->year('Model.year', array('disabled' => true));
  1664. $this->Form->month('Model.month', array('disabled' => true));
  1665. $this->Form->day('Model.day', array('disabled' => true));
  1666. $this->Form->hour('Model.hour', array('disabled' => true));
  1667. $this->Form->minute('Model.minute', array('disabled' => true));
  1668. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1669. $expected = array(
  1670. 'Model.radio' => ''
  1671. );
  1672. $this->assertEquals($expected, $this->Form->fields);
  1673. }
  1674. /**
  1675. * testDisableSecurityUsingForm method
  1676. *
  1677. * @return void
  1678. */
  1679. public function testDisableSecurityUsingForm() {
  1680. $this->Form->request['_Token'] = [
  1681. 'disabledFields' => []
  1682. ];
  1683. $this->Form->create();
  1684. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1685. $this->Form->text('Addresses.title');
  1686. $this->Form->text('Addresses.first_name', ['secure' => false]);
  1687. $this->Form->textarea('Addresses.city', ['secure' => false]);
  1688. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  1689. $result = $this->Form->fields;
  1690. $expected = [
  1691. 'Addresses.id' => '123456', 'Addresses.title',
  1692. ];
  1693. $this->assertEquals($expected, $result);
  1694. }
  1695. /**
  1696. * test disableField
  1697. *
  1698. * @return void
  1699. */
  1700. public function testUnlockFieldAddsToList() {
  1701. $this->Form->request['_Token'] = array(
  1702. 'unlockedFields' => array()
  1703. );
  1704. $this->Form->unlockField('Contact.name');
  1705. $this->Form->text('Contact.name');
  1706. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1707. $this->assertEquals(array(), $this->Form->fields);
  1708. }
  1709. /**
  1710. * test unlockField removing from fields array.
  1711. *
  1712. * @return void
  1713. */
  1714. public function testUnlockFieldRemovingFromFields() {
  1715. $this->Form->request['_Token'] = array(
  1716. 'unlockedFields' => array()
  1717. );
  1718. $this->Form->create($this->article);
  1719. $this->Form->hidden('Article.id', array('value' => 1));
  1720. $this->Form->text('Article.title');
  1721. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  1722. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  1723. $this->Form->unlockField('Article.title');
  1724. $this->Form->unlockField('Article.id');
  1725. $this->assertEquals(array(), $this->Form->fields);
  1726. }
  1727. /**
  1728. * test error message display
  1729. *
  1730. * @return void
  1731. */
  1732. public function testErrorMessageDisplay() {
  1733. $this->article['errors'] = [
  1734. 'Article' => ['title' => 'error message']
  1735. ];
  1736. $this->Form->create($this->article);
  1737. $result = $this->Form->input('Article.title');
  1738. $expected = [
  1739. 'div' => ['class' => 'input text error'],
  1740. 'label' => ['for' => 'article-title'],
  1741. 'Title',
  1742. '/label',
  1743. 'input' => [
  1744. 'type' => 'text', 'name' => 'Article[title]',
  1745. 'id' => 'article-title', 'class' => 'form-error'
  1746. ],
  1747. ['div' => ['class' => 'error-message']],
  1748. 'error message',
  1749. '/div',
  1750. '/div'
  1751. ];
  1752. $this->assertTags($result, $expected);
  1753. $result = $this->Form->input('Article.title', [
  1754. 'templates' => [
  1755. 'groupContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  1756. ]
  1757. ]);
  1758. $expected = [
  1759. 'div' => ['class' => 'input text error'],
  1760. 'label' => ['for' => 'article-title'],
  1761. 'Title',
  1762. '/label',
  1763. 'input' => [
  1764. 'type' => 'text', 'name' => 'Article[title]',
  1765. 'id' => 'article-title', 'class' => 'form-error'
  1766. ],
  1767. '/div'
  1768. ];
  1769. $this->assertTags($result, $expected);
  1770. }
  1771. /**
  1772. * Test validation errors, when validation message is an empty string.
  1773. *
  1774. * @return void
  1775. */
  1776. public function testEmptyErrorValidation() {
  1777. $this->article['errors'] = [
  1778. 'Article' => ['title' => '']
  1779. ];
  1780. $this->Form->create($this->article);
  1781. $result = $this->Form->input('Article.title');
  1782. $expected = [
  1783. 'div' => ['class' => 'input text error'],
  1784. 'label' => ['for' => 'article-title'],
  1785. 'Title',
  1786. '/label',
  1787. 'input' => [
  1788. 'type' => 'text', 'name' => 'Article[title]',
  1789. 'id' => 'article-title', 'class' => 'form-error'
  1790. ],
  1791. ['div' => ['class' => 'error-message']],
  1792. [],
  1793. '/div',
  1794. '/div'
  1795. ];
  1796. $this->assertTags($result, $expected);
  1797. }
  1798. /**
  1799. * Test validation errors, when calling input() overriding validation message by an empty string.
  1800. *
  1801. * @return void
  1802. */
  1803. public function testEmptyInputErrorValidation() {
  1804. $this->article['errors'] = [
  1805. 'Article' => ['title' => 'error message']
  1806. ];
  1807. $this->Form->create($this->article);
  1808. $result = $this->Form->input('Article.title', array('error' => ''));
  1809. $expected = [
  1810. 'div' => ['class' => 'input text error'],
  1811. 'label' => ['for' => 'article-title'],
  1812. 'Title',
  1813. '/label',
  1814. 'input' => [
  1815. 'type' => 'text', 'name' => 'Article[title]',
  1816. 'id' => 'article-title', 'class' => 'form-error'
  1817. ],
  1818. ['div' => ['class' => 'error-message']],
  1819. [],
  1820. '/div',
  1821. '/div'
  1822. ];
  1823. $this->assertTags($result, $expected);
  1824. }
  1825. /**
  1826. * testFormValidationAssociated method
  1827. *
  1828. * test display of form errors in conjunction with model::validates.
  1829. *
  1830. * @return void
  1831. */
  1832. public function testFormValidationAssociated() {
  1833. $this->markTestIncomplete('Need to revisit once models work again.');
  1834. $data = array(
  1835. 'UserForm' => array('name' => 'user'),
  1836. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1837. );
  1838. $result = $this->UserForm->OpenidUrl->create($data);
  1839. $this->assertFalse(empty($result));
  1840. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1841. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1842. $encoding = strtolower(Configure::read('App.encoding'));
  1843. $expected = array(
  1844. 'form' => array(
  1845. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1846. 'accept-charset' => $encoding
  1847. ),
  1848. 'div' => array('style' => 'display:none;'),
  1849. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1850. '/div'
  1851. );
  1852. $this->assertTags($result, $expected);
  1853. $result = $this->Form->error(
  1854. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1855. );
  1856. $this->assertEquals('Error, not registered', $result);
  1857. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1858. }
  1859. /**
  1860. * testFormValidationAssociatedFirstLevel method
  1861. *
  1862. * test form error display with associated model.
  1863. *
  1864. * @return void
  1865. */
  1866. public function testFormValidationAssociatedFirstLevel() {
  1867. $this->markTestIncomplete('Need to revisit once models work again.');
  1868. $data = array(
  1869. 'ValidateUser' => array('name' => 'mariano'),
  1870. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1871. );
  1872. $result = $this->ValidateUser->create($data);
  1873. $this->assertFalse(empty($result));
  1874. $this->assertFalse($this->ValidateUser->validates());
  1875. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1876. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1877. $encoding = strtolower(Configure::read('App.encoding'));
  1878. $expected = array(
  1879. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1880. 'div' => array('style' => 'display:none;'),
  1881. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1882. '/div'
  1883. );
  1884. $this->assertTags($result, $expected);
  1885. $result = $this->Form->error(
  1886. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1887. );
  1888. $this->assertEquals('Invalid email', $result);
  1889. $result = $this->Form->error(
  1890. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1891. );
  1892. $this->assertEquals('Invalid name', $result);
  1893. $result = $this->Form->error(
  1894. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1895. );
  1896. $this->assertEquals('Invalid city', $result);
  1897. unset($this->ValidateUser->ValidateProfile);
  1898. unset($this->ValidateUser);
  1899. }
  1900. /**
  1901. * testFormValidationAssociatedSecondLevel method
  1902. *
  1903. * test form error display with associated model.
  1904. *
  1905. * @return void
  1906. */
  1907. public function testFormValidationAssociatedSecondLevel() {
  1908. $this->markTestIncomplete('Need to revisit once models work again.');
  1909. $data = array(
  1910. 'ValidateUser' => array('name' => 'mariano'),
  1911. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1912. 'ValidateItem' => array('name' => 'Item')
  1913. );
  1914. $result = $this->ValidateUser->create($data);
  1915. $this->assertFalse(empty($result));
  1916. $this->assertFalse($this->ValidateUser->validates());
  1917. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1918. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1919. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1920. $encoding = strtolower(Configure::read('App.encoding'));
  1921. $expected = array(
  1922. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1923. 'div' => array('style' => 'display:none;'),
  1924. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1925. '/div'
  1926. );
  1927. $this->assertTags($result, $expected);
  1928. $result = $this->Form->error(
  1929. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1930. );
  1931. $this->assertEquals('Invalid email', $result);
  1932. $result = $this->Form->error(
  1933. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1934. );
  1935. $this->assertEquals('Invalid name', $result);
  1936. $result = $this->Form->error(
  1937. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1938. );
  1939. $result = $this->Form->error(
  1940. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1941. );
  1942. $this->assertEquals('Invalid description', $result);
  1943. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1944. unset($this->ValidateUser->ValidateProfile);
  1945. unset($this->ValidateUser);
  1946. }
  1947. /**
  1948. * testFormValidationMultiRecord method
  1949. *
  1950. * test form error display with multiple records.
  1951. *
  1952. * @return void
  1953. */
  1954. public function testFormValidationMultiRecord() {
  1955. $this->markTestIncomplete('Need to revisit once models work again.');
  1956. $Contact->validationErrors[2] = array(
  1957. 'name' => array('The provided value is invalid')
  1958. );
  1959. $result = $this->Form->input('Contact.2.name');
  1960. $expected = array(
  1961. 'div' => array('class' => 'input text error'),
  1962. 'label' => array('for' => 'Contact2Name'),
  1963. 'Name',
  1964. '/label',
  1965. 'input' => array(
  1966. 'type' => 'text', 'name' => 'Contact[2][name]', 'id' => 'Contact2Name',
  1967. 'class' => 'form-error', 'maxlength' => 255
  1968. ),
  1969. array('div' => array('class' => 'error-message')),
  1970. 'The provided value is invalid',
  1971. '/div',
  1972. '/div'
  1973. );
  1974. $this->assertTags($result, $expected);
  1975. }
  1976. /**
  1977. * testMultipleInputValidation method
  1978. *
  1979. * test multiple record form validation error display.
  1980. *
  1981. * @return void
  1982. */
  1983. public function testMultipleInputValidation() {
  1984. $this->markTestIncomplete('Need to revisit once models work again.');
  1985. $Address->validationErrors[0] = array(
  1986. 'title' => array('This field cannot be empty'),
  1987. 'first_name' => array('This field cannot be empty')
  1988. );
  1989. $Address->validationErrors[1] = array(
  1990. 'last_name' => array('You must have a last name')
  1991. );
  1992. $this->Form->create();
  1993. $result = $this->Form->input('Address.0.title');
  1994. $expected = array(
  1995. 'div' => array('class'),
  1996. 'label' => array('for'),
  1997. 'preg:/[^<]+/',
  1998. '/label',
  1999. 'input' => array(
  2000. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  2001. ),
  2002. array('div' => array('class' => 'error-message')),
  2003. 'This field cannot be empty',
  2004. '/div',
  2005. '/div'
  2006. );
  2007. $this->assertTags($result, $expected);
  2008. $result = $this->Form->input('Address.0.first_name');
  2009. $expected = array(
  2010. 'div' => array('class'),
  2011. 'label' => array('for'),
  2012. 'preg:/[^<]+/',
  2013. '/label',
  2014. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  2015. array('div' => array('class' => 'error-message')),
  2016. 'This field cannot be empty',
  2017. '/div',
  2018. '/div'
  2019. );
  2020. $this->assertTags($result, $expected);
  2021. $result = $this->Form->input('Address.0.last_name');
  2022. $expected = array(
  2023. 'div' => array('class'),
  2024. 'label' => array('for'),
  2025. 'preg:/[^<]+/',
  2026. '/label',
  2027. 'input' => array('type' => 'text', 'name', 'id'),
  2028. '/div'
  2029. );
  2030. $this->assertTags($result, $expected);
  2031. $result = $this->Form->input('Address.1.last_name');
  2032. $expected = array(
  2033. 'div' => array('class'),
  2034. 'label' => array('for'),
  2035. 'preg:/[^<]+/',
  2036. '/label',
  2037. 'input' => array(
  2038. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  2039. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  2040. ),
  2041. array('div' => array('class' => 'error-message')),
  2042. 'You must have a last name',
  2043. '/div',
  2044. '/div'
  2045. );
  2046. $this->assertTags($result, $expected);
  2047. }
  2048. /**
  2049. * testInput method
  2050. *
  2051. * Test various incarnations of input().
  2052. *
  2053. * @return void
  2054. */
  2055. public function testInput() {
  2056. TableRegistry::get('ValidateUsers', [
  2057. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2058. ]);
  2059. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2060. $result = $this->Form->input('ValidateUsers.balance');
  2061. $expected = array(
  2062. 'div' => array('class'),
  2063. 'label' => array('for'),
  2064. 'Balance',
  2065. '/label',
  2066. 'input' => array('name', 'type' => 'number', 'id', 'step'),
  2067. '/div',
  2068. );
  2069. $this->assertTags($result, $expected);
  2070. $result = $this->Form->input('ValidateUser.cost_decimal');
  2071. $expected = array(
  2072. 'div' => array('class'),
  2073. 'label' => array('for'),
  2074. 'Cost Decimal',
  2075. '/label',
  2076. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  2077. '/div',
  2078. );
  2079. $this->assertTags($result, $expected);
  2080. }
  2081. /**
  2082. * Tests the input method and passing custom options
  2083. *
  2084. * @return void
  2085. */
  2086. public function testInputCustomization() {
  2087. TableRegistry::get('Contacts', [
  2088. 'className' => __NAMESPACE__ . '\ContactsTable'
  2089. ]);
  2090. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2091. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  2092. $expected = array(
  2093. 'div' => array('class' => 'input email'),
  2094. 'label' => array('for' => 'custom'),
  2095. 'Email',
  2096. '/label',
  2097. array('input' => array(
  2098. 'type' => 'email', 'name' => 'Contact[email]',
  2099. 'id' => 'custom', 'maxlength' => 255
  2100. )),
  2101. '/div'
  2102. );
  2103. $this->assertTags($result, $expected);
  2104. $result = $this->Form->input('Contact.email', array(
  2105. 'templates' => ['groupContainer' => '<div>{{content}}</div>']
  2106. ));
  2107. $expected = array(
  2108. '<div',
  2109. 'label' => array('for' => 'contact-email'),
  2110. 'Email',
  2111. '/label',
  2112. array('input' => array(
  2113. 'type' => 'email', 'name' => 'Contact[email]',
  2114. 'id' => 'contact-email', 'maxlength' => 255
  2115. )),
  2116. '/div'
  2117. );
  2118. $this->assertTags($result, $expected);
  2119. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  2120. $expected = array(
  2121. 'div' => array('class' => 'input text'),
  2122. 'label' => array('for' => 'contact-email'),
  2123. 'Email',
  2124. '/label',
  2125. array('input' => array(
  2126. 'type' => 'text', 'name' => 'Contact[email]',
  2127. 'id' => 'contact-email', 'maxlength' => '255'
  2128. )),
  2129. '/div'
  2130. );
  2131. $this->assertTags($result, $expected);
  2132. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  2133. $expected = array(
  2134. 'div' => array('class' => 'input text'),
  2135. 'label' => array('for' => 'contact-5-email'),
  2136. 'Email',
  2137. '/label',
  2138. array('input' => array(
  2139. 'type' => 'text', 'name' => 'Contact[5][email]',
  2140. 'id' => 'contact-5-email', 'maxlength' => '255'
  2141. )),
  2142. '/div'
  2143. );
  2144. $this->assertTags($result, $expected);
  2145. $result = $this->Form->input('Contact.password');
  2146. $expected = array(
  2147. 'div' => array('class' => 'input password'),
  2148. 'label' => array('for' => 'contact-password'),
  2149. 'Password',
  2150. '/label',
  2151. array('input' => array(
  2152. 'type' => 'password', 'name' => 'Contact[password]',
  2153. 'id' => 'contact-password'
  2154. )),
  2155. '/div'
  2156. );
  2157. $this->assertTags($result, $expected);
  2158. $result = $this->Form->input('Contact.email', array(
  2159. 'type' => 'file', 'class' => 'textbox'
  2160. ));
  2161. $expected = array(
  2162. 'div' => array('class' => 'input file'),
  2163. 'label' => array('for' => 'contact-email'),
  2164. 'Email',
  2165. '/label',
  2166. array('input' => array(
  2167. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2168. 'id' => 'contact-email'
  2169. )),
  2170. '/div'
  2171. );
  2172. $this->assertTags($result, $expected);
  2173. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2174. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2175. $result = $this->Form->input('Contact.phone');
  2176. $expected = array(
  2177. 'div' => array('class' => 'input tel'),
  2178. 'label' => array('for' => 'contact-phone'),
  2179. 'Phone',
  2180. '/label',
  2181. array('input' => array(
  2182. 'type' => 'tel', 'name' => 'Contact[phone]',
  2183. 'value' => 'Hello &amp; World &gt; weird chars',
  2184. 'id' => 'contact-phone', 'maxlength' => 255
  2185. )),
  2186. '/div'
  2187. );
  2188. $this->assertTags($result, $expected);
  2189. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2190. $this->Form->create();
  2191. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  2192. $expected = array(
  2193. 'div' => array('class' => 'input text'),
  2194. 'label' => array('for' => 'myId'),
  2195. 'Field',
  2196. '/label',
  2197. 'input' => array(
  2198. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2199. 'value' => 'My value', 'id' => 'myId'
  2200. ),
  2201. '/div'
  2202. );
  2203. $this->assertTags($result, $expected);
  2204. unset($this->Form->request->data);
  2205. $entity->errors('field', 'Badness!');
  2206. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2207. $result = $this->Form->input('Contact.field');
  2208. $expected = array(
  2209. 'div' => array('class' => 'input text error'),
  2210. 'label' => array('for' => 'contact-field'),
  2211. 'Field',
  2212. '/label',
  2213. 'input' => array(
  2214. 'type' => 'text', 'name' => 'Contact[field]',
  2215. 'id' => 'contact-field', 'class' => 'form-error'
  2216. ),
  2217. array('div' => array('class' => 'error-message')),
  2218. 'Badness!',
  2219. '/div',
  2220. '/div'
  2221. );
  2222. $this->assertTags($result, $expected);
  2223. $result = $this->Form->input('Contact.field', array(
  2224. 'templates' => [
  2225. 'groupContainerError' => '{{content}}{{error}}',
  2226. 'error' => '<span class="error-message">{{content}}</span>'
  2227. ]
  2228. ));
  2229. $expected = array(
  2230. 'label' => array('for' => 'contact-field'),
  2231. 'Field',
  2232. '/label',
  2233. 'input' => array(
  2234. 'type' => 'text', 'name' => 'Contact[field]',
  2235. 'id' => 'contact-field', 'class' => 'form-error'
  2236. ),
  2237. array('span' => array('class' => 'error-message')),
  2238. 'Badness!',
  2239. '/span'
  2240. );
  2241. $this->assertTags($result, $expected);
  2242. $entity->errors('field', ['minLength']);
  2243. $result = $this->Form->input('Contact.field', array(
  2244. 'error' => array(
  2245. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2246. 'maxLength' => 'login too large'
  2247. )
  2248. ));
  2249. $expected = array(
  2250. 'div' => array('class' => 'input text error'),
  2251. 'label' => array('for' => 'contact-field'),
  2252. 'Field',
  2253. '/label',
  2254. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'contact-field', 'class' => 'form-error'),
  2255. array('div' => array('class' => 'error-message')),
  2256. 'Le login doit contenir au moins 2 caractères',
  2257. '/div',
  2258. '/div'
  2259. );
  2260. $this->assertTags($result, $expected);
  2261. $entity->errors('field', ['maxLength']);
  2262. $result = $this->Form->input('Contact.field', array(
  2263. 'error' => array(
  2264. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2265. 'maxLength' => 'login too large',
  2266. )
  2267. ));
  2268. $expected = array(
  2269. 'div' => array('class' => 'input text error'),
  2270. 'label' => array('for' => 'contact-field'),
  2271. 'Field',
  2272. '/label',
  2273. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'contact-field', 'class' => 'form-error'),
  2274. array('div' => array('class' => 'error-message')),
  2275. 'login too large',
  2276. '/div',
  2277. '/div'
  2278. );
  2279. $this->assertTags($result, $expected);
  2280. }
  2281. /**
  2282. * Test that inputs with 0 can be created.
  2283. *
  2284. * @return void
  2285. */
  2286. public function testInputZero() {
  2287. TableRegistry::get('Contacts', [
  2288. 'className' => __NAMESPACE__ . '\ContactsTable'
  2289. ]);
  2290. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2291. $result = $this->Form->input('0');
  2292. $expected = array(
  2293. 'div' => array('class' => 'input text'),
  2294. 'label' => array('for' => '0'), '/label',
  2295. 'input' => array('type' => 'text', 'name' => '0', 'id' => '0'),
  2296. '/div'
  2297. );
  2298. $this->assertTags($result, $expected);
  2299. }
  2300. /**
  2301. * test input() with checkbox creation
  2302. *
  2303. * @return void
  2304. */
  2305. public function testInputCheckbox() {
  2306. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  2307. $expected = array(
  2308. 'div' => array('class' => 'input checkbox'),
  2309. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2310. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2311. '/div'
  2312. );
  2313. $this->assertTags($result, $expected);
  2314. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2315. $expected = array(
  2316. 'div' => array('class' => 'input checkbox'),
  2317. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2318. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2319. '/div'
  2320. );
  2321. $this->assertTags($result, $expected);
  2322. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2323. $expected = array(
  2324. 'div' => array('class' => 'input checkbox'),
  2325. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2326. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2327. '/div'
  2328. );
  2329. $this->assertTags($result, $expected);
  2330. $result = $this->Form->input('User.disabled', array(
  2331. 'label' => 'Disabled',
  2332. 'type' => 'checkbox',
  2333. 'data-foo' => 'disabled'
  2334. ));
  2335. $expected = array(
  2336. 'div' => array('class' => 'input checkbox'),
  2337. 'input' => array('type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'),
  2338. array('input' => array(
  2339. 'type' => 'checkbox',
  2340. 'name' => 'User[disabled]',
  2341. 'value' => '1',
  2342. 'id' => 'user-disabled',
  2343. 'data-foo' => 'disabled'
  2344. )),
  2345. 'label' => array('for' => 'user-disabled'),
  2346. 'Disabled',
  2347. '/label',
  2348. '/div'
  2349. );
  2350. $this->assertTags($result, $expected);
  2351. }
  2352. /**
  2353. * test form->input() with datetime
  2354. *
  2355. * @return void
  2356. */
  2357. public function testInputDatetime() {
  2358. $this->Form = $this->getMock(
  2359. 'Cake\View\Helper\FormHelper',
  2360. ['datetime'],
  2361. [new View(null)]
  2362. );
  2363. $this->Form->expects($this->once())->method('datetime')
  2364. ->with('prueba', [
  2365. 'type' => 'datetime',
  2366. 'timeFormat' => 24,
  2367. 'minYear' => 2008,
  2368. 'maxYear' => 2011,
  2369. 'interval' => 15,
  2370. 'options' => null,
  2371. 'empty' => false,
  2372. 'id' => 'prueba',
  2373. 'required' => false,
  2374. ])
  2375. ->will($this->returnValue('This is it!'));
  2376. $result = $this->Form->input('prueba', array(
  2377. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2378. 'maxYear' => 2011, 'interval' => 15
  2379. ));
  2380. $expected = array(
  2381. 'div' => array('class' => 'input datetime'),
  2382. 'label' => array('for' => 'prueba'),
  2383. 'Prueba',
  2384. '/label',
  2385. 'This is it!',
  2386. '/div'
  2387. );
  2388. $this->assertTags($result, $expected);
  2389. }
  2390. /**
  2391. * Test generating checkboxes with disabled elements.
  2392. *
  2393. * @return void
  2394. */
  2395. public function testInputCheckboxWithDisabledElements() {
  2396. $this->markTestIncomplete('Need to revisit once models work again.');
  2397. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2398. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2399. $expected = array(
  2400. array('div' => array('class' => 'input select')),
  2401. array('label' => array('for' => "ContactMultiple")),
  2402. 'Multiple',
  2403. '/label',
  2404. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2405. array('div' => array('class' => 'checkbox')),
  2406. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2407. array('label' => array('for' => "ContactMultiple1")),
  2408. 'One',
  2409. '/label',
  2410. '/div',
  2411. array('div' => array('class' => 'checkbox')),
  2412. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2413. array('label' => array('for' => "ContactMultiple2")),
  2414. 'Two',
  2415. '/label',
  2416. '/div',
  2417. array('div' => array('class' => 'checkbox')),
  2418. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2419. array('label' => array('for' => "ContactMultiple3")),
  2420. 'Three',
  2421. '/label',
  2422. '/div',
  2423. '/div'
  2424. );
  2425. $this->assertTags($result, $expected);
  2426. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2427. $expected = array(
  2428. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  2429. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2430. 'label' => array('for' => 'ModelField12'),
  2431. 'half',
  2432. '/label'
  2433. );
  2434. $this->assertTags($result, $expected);
  2435. $disabled = array('2', 3);
  2436. $expected = array(
  2437. array('div' => array('class' => 'input select')),
  2438. array('label' => array('for' => "ContactMultiple")),
  2439. 'Multiple',
  2440. '/label',
  2441. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2442. array('div' => array('class' => 'checkbox')),
  2443. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2444. array('label' => array('for' => "ContactMultiple1")),
  2445. 'One',
  2446. '/label',
  2447. '/div',
  2448. array('div' => array('class' => 'checkbox')),
  2449. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2450. array('label' => array('for' => "ContactMultiple2")),
  2451. 'Two',
  2452. '/label',
  2453. '/div',
  2454. array('div' => array('class' => 'checkbox')),
  2455. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2456. array('label' => array('for' => "ContactMultiple3")),
  2457. 'Three',
  2458. '/label',
  2459. '/div',
  2460. '/div'
  2461. );
  2462. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2463. $this->assertTags($result, $expected);
  2464. // make sure 50 does only disable 50, and not 50f5c0cf
  2465. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2466. $disabled = array(50);
  2467. $expected = array(
  2468. array('div' => array('class' => 'input select')),
  2469. array('label' => array('for' => "ContactMultiple")),
  2470. 'Multiple',
  2471. '/label',
  2472. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2473. array('div' => array('class' => 'checkbox')),
  2474. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2475. array('label' => array('for' => "ContactMultiple50")),
  2476. 'Fifty',
  2477. '/label',
  2478. '/div',
  2479. array('div' => array('class' => 'checkbox')),
  2480. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2481. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2482. 'Stringy',
  2483. '/label',
  2484. '/div',
  2485. '/div'
  2486. );
  2487. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2488. $this->assertTags($result, $expected);
  2489. }
  2490. /**
  2491. * test input name with leading integer, ensure attributes are generated correctly.
  2492. *
  2493. * @return void
  2494. */
  2495. public function testInputWithLeadingInteger() {
  2496. $result = $this->Form->text('0.Node.title');
  2497. $expected = array(
  2498. 'input' => array('name' => '0[Node][title]', 'type' => 'text')
  2499. );
  2500. $this->assertTags($result, $expected);
  2501. }
  2502. /**
  2503. * test form->input() with select type inputs.
  2504. *
  2505. * @return void
  2506. */
  2507. public function testInputSelectType() {
  2508. $result = $this->Form->input('email', array(
  2509. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2510. );
  2511. $expected = array(
  2512. 'div' => array('class' => 'input select'),
  2513. 'label' => array('for' => 'email'),
  2514. 'Email',
  2515. '/label',
  2516. array('select' => array('name' => 'email', 'id' => 'email')),
  2517. array('option' => array('value' => '')),
  2518. '/option',
  2519. array('option' => array('value' => 'è')),
  2520. 'Firést',
  2521. '/option',
  2522. array('option' => array('value' => 'é')),
  2523. 'Secoènd',
  2524. '/option',
  2525. '/select',
  2526. '/div'
  2527. );
  2528. $this->assertTags($result, $expected);
  2529. $result = $this->Form->input('email', array(
  2530. 'options' => array('First', 'Second'), 'empty' => true)
  2531. );
  2532. $expected = array(
  2533. 'div' => array('class' => 'input select'),
  2534. 'label' => array('for' => 'email'),
  2535. 'Email',
  2536. '/label',
  2537. array('select' => array('name' => 'email', 'id' => 'email')),
  2538. array('option' => array('value' => '')),
  2539. '/option',
  2540. array('option' => array('value' => '0')),
  2541. 'First',
  2542. '/option',
  2543. array('option' => array('value' => '1')),
  2544. 'Second',
  2545. '/option',
  2546. '/select',
  2547. '/div'
  2548. );
  2549. $this->assertTags($result, $expected);
  2550. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2551. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2552. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2553. $expected = array(
  2554. 'div' => array('class' => 'input select'),
  2555. 'label' => array('for' => 'model-user-id'),
  2556. 'User',
  2557. '/label',
  2558. 'select' => array('name' => 'Model[user_id]', 'id' => 'model-user-id'),
  2559. array('option' => array('value' => '')),
  2560. '/option',
  2561. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2562. 'good',
  2563. '/option',
  2564. array('option' => array('value' => 'other')),
  2565. 'bad',
  2566. '/option',
  2567. '/select',
  2568. '/div'
  2569. );
  2570. $this->assertTags($result, $expected);
  2571. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2572. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2573. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2574. $expected = array(
  2575. 'div' => array('class' => 'input select'),
  2576. 'label' => array('for' => 'thing-user-id'),
  2577. 'User',
  2578. '/label',
  2579. 'select' => array('name' => 'Thing[user_id]', 'id' => 'thing-user-id'),
  2580. array('option' => array('value' => '')),
  2581. 'Some Empty',
  2582. '/option',
  2583. array('option' => array('value' => 'value')),
  2584. 'good',
  2585. '/option',
  2586. array('option' => array('value' => 'other')),
  2587. 'bad',
  2588. '/option',
  2589. '/select',
  2590. '/div'
  2591. );
  2592. $this->assertTags($result, $expected);
  2593. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2594. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2595. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2596. $expected = array(
  2597. 'div' => array('class' => 'input select'),
  2598. 'label' => array('for' => 'thing-user-id'),
  2599. 'User',
  2600. '/label',
  2601. 'select' => array('name' => 'Thing[user_id]', 'id' => 'thing-user-id'),
  2602. array('option' => array('value' => '')),
  2603. 'Some Empty',
  2604. '/option',
  2605. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2606. 'good',
  2607. '/option',
  2608. array('option' => array('value' => 'other')),
  2609. 'bad',
  2610. '/option',
  2611. '/select',
  2612. '/div'
  2613. );
  2614. $this->assertTags($result, $expected);
  2615. $this->Form->data = array();
  2616. $result = $this->Form->input('Publisher.id', array(
  2617. 'label' => 'Publisher',
  2618. 'type' => 'select',
  2619. 'multiple' => 'checkbox',
  2620. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2621. ));
  2622. $expected = array(
  2623. array('div' => array('class' => 'input select')),
  2624. array('label' => array('for' => 'publisher-id')),
  2625. 'Publisher',
  2626. '/label',
  2627. 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''),
  2628. array('div' => array('class' => 'checkbox')),
  2629. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1')),
  2630. array('label' => array('for' => 'publisher-id-value-1')),
  2631. 'Label 1',
  2632. '/label',
  2633. '/div',
  2634. array('div' => array('class' => 'checkbox')),
  2635. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2')),
  2636. array('label' => array('for' => 'publisher-id-value-2')),
  2637. 'Label 2',
  2638. '/label',
  2639. '/div',
  2640. '/div'
  2641. );
  2642. $this->assertTags($result, $expected);
  2643. }
  2644. /**
  2645. * test that input() and a non standard primary key makes a hidden input by default.
  2646. *
  2647. * @return void
  2648. */
  2649. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2650. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  2651. $this->Form->create($this->article);
  2652. $result = $this->Form->input('title');
  2653. $expected = array(
  2654. 'input' => array('type' => 'hidden', 'name' => 'title', 'id' => 'title'),
  2655. );
  2656. $this->assertTags($result, $expected);
  2657. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  2658. $this->Form->create($this->article);
  2659. $result = $this->Form->input('title');
  2660. $expected = array(
  2661. 'input' => array('type' => 'hidden', 'name' => 'title', 'id' => 'title'),
  2662. );
  2663. $this->assertTags($result, $expected);
  2664. $result = $this->Form->input('body');
  2665. $expected = array(
  2666. 'input' => array('type' => 'hidden', 'name' => 'body', 'id' => 'body'),
  2667. );
  2668. $this->assertTags($result, $expected);
  2669. }
  2670. /**
  2671. * test that overriding the magic select type widget is possible
  2672. *
  2673. * @return void
  2674. */
  2675. public function testInputOverridingMagicSelectType() {
  2676. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2677. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2678. $expected = array(
  2679. 'div' => array('class' => 'input text'),
  2680. 'label' => array('for' => 'model-user-id'), 'User', '/label',
  2681. 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'),
  2682. '/div'
  2683. );
  2684. $this->assertTags($result, $expected);
  2685. //Check that magic types still work for plural/singular vars
  2686. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2687. $result = $this->Form->input('Model.type');
  2688. $expected = array(
  2689. 'div' => array('class' => 'input select'),
  2690. 'label' => array('for' => 'model-type'), 'Type', '/label',
  2691. 'select' => array('name' => 'Model[type]', 'id' => 'model-type'),
  2692. array('option' => array('value' => 'value')), 'good', '/option',
  2693. array('option' => array('value' => 'other')), 'bad', '/option',
  2694. '/select',
  2695. '/div'
  2696. );
  2697. $this->assertTags($result, $expected);
  2698. }
  2699. /**
  2700. * Test that inferred types do not override developer input
  2701. *
  2702. * @return void
  2703. */
  2704. public function testInputMagicTypeDoesNotOverride() {
  2705. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2706. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2707. $expected = array(
  2708. 'div' => array('class' => 'input checkbox'),
  2709. array('input' => array(
  2710. 'type' => 'hidden',
  2711. 'name' => 'Model[user]',
  2712. 'value' => 0,
  2713. )),
  2714. array('input' => array(
  2715. 'name' => 'Model[user]',
  2716. 'type' => 'checkbox',
  2717. 'id' => 'model-user',
  2718. 'value' => 1
  2719. )),
  2720. 'label' => array('for' => 'model-user'), 'User', '/label',
  2721. '/div'
  2722. );
  2723. $this->assertTags($result, $expected);
  2724. }
  2725. /**
  2726. * Test that magic input() selects are created for type=number
  2727. *
  2728. * @return void
  2729. */
  2730. public function testInputMagicSelectForTypeNumber() {
  2731. TableRegistry::get('ValidateUsers', [
  2732. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2733. ]);
  2734. $entity = new Entity(['balance' => 1]);
  2735. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  2736. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2737. $result = $this->Form->input('ValidateUser.balance');
  2738. $expected = array(
  2739. 'div' => array('class' => 'input select'),
  2740. 'label' => array('for' => 'validateuser-balance'),
  2741. 'Balance',
  2742. '/label',
  2743. 'select' => array('name' => 'ValidateUser[balance]', 'id' => 'validateuser-balance'),
  2744. array('option' => array('value' => '0')),
  2745. 'nothing',
  2746. '/option',
  2747. array('option' => array('value' => '1', 'selected' => 'selected')),
  2748. 'some',
  2749. '/option',
  2750. array('option' => array('value' => '100')),
  2751. 'a lot',
  2752. '/option',
  2753. '/select',
  2754. '/div'
  2755. );
  2756. $this->assertTags($result, $expected);
  2757. }
  2758. /**
  2759. * Test that magic input() selects can easily be converted into radio types without error.
  2760. *
  2761. * @return void
  2762. */
  2763. public function testInputMagicSelectChangeToRadio() {
  2764. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2765. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2766. $this->assertRegExp('/input type="radio"/', $result);
  2767. }
  2768. /**
  2769. * fields with the same name as the model should work.
  2770. *
  2771. * @return void
  2772. */
  2773. public function testInputWithMatchingFieldAndModelName() {
  2774. $this->markTestIncomplete('Need to revisit once models work again.');
  2775. $this->Form->create('User');
  2776. $this->Form->fieldset = array(
  2777. 'User' => array(
  2778. 'fields' => array(
  2779. 'User' => array('type' => 'text')
  2780. ),
  2781. 'validates' => array(),
  2782. 'key' => 'id'
  2783. )
  2784. );
  2785. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2786. $result = $this->Form->input('User', array('type' => 'text'));
  2787. $expected = array(
  2788. 'div' => array('class' => 'input text'),
  2789. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2790. 'input' => array('name' => 'User[User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2791. '/div'
  2792. );
  2793. $this->assertTags($result, $expected);
  2794. }
  2795. /**
  2796. * testFormInputs method
  2797. *
  2798. * test correct results from form::inputs().
  2799. *
  2800. * @return void
  2801. */
  2802. public function testFormInputs() {
  2803. $this->markTestIncomplete('Need to revisit once models work again.');
  2804. $this->Form->create('Cake\Test\TestCase\View\Helper\Contact');
  2805. $result = $this->Form->inputs('The Legend');
  2806. $expected = array(
  2807. '<fieldset',
  2808. '<legend',
  2809. 'The Legend',
  2810. '/legend',
  2811. '*/fieldset',
  2812. );
  2813. $this->assertTags($result, $expected);
  2814. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2815. $expected = array(
  2816. 'fieldset' => array('class' => 'classy-stuff'),
  2817. '<legend',
  2818. 'Field of Dreams',
  2819. '/legend',
  2820. '*/fieldset'
  2821. );
  2822. $this->assertTags($result, $expected);
  2823. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2824. $this->assertTags($result, $expected);
  2825. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2826. $this->assertTags($result, $expected);
  2827. $this->Form->create('Contact');
  2828. $this->Form->request['prefix'] = 'admin';
  2829. $this->Form->request['action'] = 'admin_edit';
  2830. $result = $this->Form->inputs();
  2831. $expected = array(
  2832. '<fieldset',
  2833. '<legend',
  2834. 'Edit Contact',
  2835. '/legend',
  2836. '*/fieldset',
  2837. );
  2838. $this->assertTags($result, $expected);
  2839. $this->Form->create('Contact');
  2840. $result = $this->Form->inputs(false);
  2841. $expected = array(
  2842. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2843. array('div' => array('class' => 'input text')),
  2844. '*/div',
  2845. array('div' => array('class' => 'input email')),
  2846. '*/div',
  2847. array('div' => array('class' => 'input tel')),
  2848. '*/div',
  2849. array('div' => array('class' => 'input password')),
  2850. '*/div',
  2851. array('div' => array('class' => 'input date')),
  2852. '*/div',
  2853. array('div' => array('class' => 'input date')),
  2854. '*/div',
  2855. array('div' => array('class' => 'input datetime')),
  2856. '*/div',
  2857. array('div' => array('class' => 'input number')),
  2858. '*/div',
  2859. array('div' => array('class' => 'input select')),
  2860. '*/div',
  2861. );
  2862. $this->assertTags($result, $expected);
  2863. $this->Form->create('Contact');
  2864. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2865. $expected = array(
  2866. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2867. array('div' => array('class' => 'input text')),
  2868. '*/div',
  2869. array('div' => array('class' => 'input email')),
  2870. '*/div',
  2871. array('div' => array('class' => 'input tel')),
  2872. '*/div',
  2873. array('div' => array('class' => 'input password')),
  2874. '*/div',
  2875. array('div' => array('class' => 'input date')),
  2876. '*/div',
  2877. array('div' => array('class' => 'input date')),
  2878. '*/div',
  2879. array('div' => array('class' => 'input datetime')),
  2880. '*/div',
  2881. array('div' => array('class' => 'input number')),
  2882. '*/div',
  2883. array('div' => array('class' => 'input select')),
  2884. '*/div',
  2885. );
  2886. $this->assertTags($result, $expected);
  2887. $this->Form->create('Contact');
  2888. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2889. $this->assertTags($result, $expected);
  2890. $this->Form->create('Contact');
  2891. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2892. $expected = array(
  2893. 'fieldset' => array(),
  2894. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2895. array('div' => array('class' => 'input text')),
  2896. '*/div',
  2897. array('div' => array('class' => 'input email')),
  2898. '*/div',
  2899. array('div' => array('class' => 'input tel')),
  2900. '*/div',
  2901. array('div' => array('class' => 'input password')),
  2902. '*/div',
  2903. array('div' => array('class' => 'input date')),
  2904. '*/div',
  2905. array('div' => array('class' => 'input date')),
  2906. '*/div',
  2907. array('div' => array('class' => 'input datetime')),
  2908. '*/div',
  2909. array('div' => array('class' => 'input number')),
  2910. '*/div',
  2911. array('div' => array('class' => 'input select')),
  2912. '*/div',
  2913. '/fieldset'
  2914. );
  2915. $this->assertTags($result, $expected);
  2916. $this->Form->create('Contact');
  2917. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2918. $expected = array(
  2919. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2920. array('div' => array('class' => 'input text')),
  2921. '*/div',
  2922. array('div' => array('class' => 'input email')),
  2923. '*/div',
  2924. array('div' => array('class' => 'input tel')),
  2925. '*/div',
  2926. array('div' => array('class' => 'input password')),
  2927. '*/div',
  2928. array('div' => array('class' => 'input date')),
  2929. '*/div',
  2930. array('div' => array('class' => 'input date')),
  2931. '*/div',
  2932. array('div' => array('class' => 'input datetime')),
  2933. '*/div',
  2934. array('div' => array('class' => 'input number')),
  2935. '*/div',
  2936. array('div' => array('class' => 'input select')),
  2937. '*/div',
  2938. );
  2939. $this->assertTags($result, $expected);
  2940. $this->Form->create('Contact');
  2941. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2942. $this->assertTags($result, $expected);
  2943. $this->Form->create('Contact');
  2944. $result = $this->Form->inputs('Hello');
  2945. $expected = array(
  2946. 'fieldset' => array(),
  2947. 'legend' => array(),
  2948. 'Hello',
  2949. '/legend',
  2950. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2951. array('div' => array('class' => 'input text')),
  2952. '*/div',
  2953. array('div' => array('class' => 'input email')),
  2954. '*/div',
  2955. array('div' => array('class' => 'input tel')),
  2956. '*/div',
  2957. array('div' => array('class' => 'input password')),
  2958. '*/div',
  2959. array('div' => array('class' => 'input date')),
  2960. '*/div',
  2961. array('div' => array('class' => 'input date')),
  2962. '*/div',
  2963. array('div' => array('class' => 'input datetime')),
  2964. '*/div',
  2965. array('div' => array('class' => 'input number')),
  2966. '*/div',
  2967. array('div' => array('class' => 'input select')),
  2968. '*/div',
  2969. '/fieldset'
  2970. );
  2971. $this->assertTags($result, $expected);
  2972. $this->Form->create('Contact');
  2973. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2974. $expected = array(
  2975. 'fieldset' => array(),
  2976. 'legend' => array(),
  2977. 'Hello',
  2978. '/legend',
  2979. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2980. array('div' => array('class' => 'input text')),
  2981. '*/div',
  2982. array('div' => array('class' => 'input email')),
  2983. '*/div',
  2984. array('div' => array('class' => 'input tel')),
  2985. '*/div',
  2986. array('div' => array('class' => 'input password')),
  2987. '*/div',
  2988. array('div' => array('class' => 'input date')),
  2989. '*/div',
  2990. array('div' => array('class' => 'input date')),
  2991. '*/div',
  2992. array('div' => array('class' => 'input datetime')),
  2993. '*/div',
  2994. array('div' => array('class' => 'input number')),
  2995. '*/div',
  2996. array('div' => array('class' => 'input select')),
  2997. '*/div',
  2998. '/fieldset'
  2999. );
  3000. $this->assertTags($result, $expected);
  3001. $this->Form->create('Contact');
  3002. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  3003. $this->assertTags($result, $expected);
  3004. $this->Form->end();
  3005. $this->Form->create(false);
  3006. $expected = array(
  3007. 'fieldset' => array(),
  3008. array('div' => array('class' => 'input text')),
  3009. 'label' => array('for' => 'foo'),
  3010. 'Foo',
  3011. '/label',
  3012. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  3013. '*/div',
  3014. '/fieldset'
  3015. );
  3016. $result = $this->Form->inputs(
  3017. array('foo' => array('type' => 'text')),
  3018. array(),
  3019. array('legend' => false)
  3020. );
  3021. $this->assertTags($result, $expected);
  3022. }
  3023. /**
  3024. * Tests inputs() works with plugin models
  3025. *
  3026. * @return void
  3027. */
  3028. public function testInputsPluginModel() {
  3029. $this->markTestIncomplete('Need to revisit once models work again.');
  3030. $this->loadFixtures('Post');
  3031. Plugin::load('TestPlugin');
  3032. $this->Form->request['models'] = array(
  3033. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  3034. );
  3035. $this->Form->create('TestPlugin.TestPluginPost');
  3036. $result = $this->Form->inputs();
  3037. $this->assertContains('TestPluginPost[id]', $result);
  3038. $this->assertContains('TestPluginPost[author_id]', $result);
  3039. $this->assertContains('TestPluginPost[title]', $result);
  3040. $this->assertEquals('TestPluginPost', $this->Form->model());
  3041. }
  3042. /**
  3043. * testSelectAsCheckbox method
  3044. *
  3045. * test multi-select widget with checkbox formatting.
  3046. *
  3047. * @return void
  3048. */
  3049. public function testSelectAsCheckbox() {
  3050. $result = $this->Form->select(
  3051. 'Model.multi_field',
  3052. array('first', 'second', 'third'),
  3053. array('multiple' => 'checkbox', 'value' => array(0, 1))
  3054. );
  3055. $expected = array(
  3056. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3057. array('div' => array('class' => 'checkbox')),
  3058. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0')),
  3059. array('label' => array('for' => 'model-multi-field-0', 'class' => 'selected')),
  3060. 'first',
  3061. '/label',
  3062. '/div',
  3063. array('div' => array('class' => 'checkbox')),
  3064. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1')),
  3065. array('label' => array('for' => 'model-multi-field-1', 'class' => 'selected')),
  3066. 'second',
  3067. '/label',
  3068. '/div',
  3069. array('div' => array('class' => 'checkbox')),
  3070. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  3071. array('label' => array('for' => 'model-multi-field-2')),
  3072. 'third',
  3073. '/label',
  3074. '/div',
  3075. );
  3076. $this->assertTags($result, $expected);
  3077. $result = $this->Form->select(
  3078. 'Model.multi_field',
  3079. array('1/2' => 'half'),
  3080. array('multiple' => 'checkbox')
  3081. );
  3082. $expected = array(
  3083. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3084. array('div' => array('class' => 'checkbox')),
  3085. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2')),
  3086. array('label' => array('for' => 'model-multi-field-1-2')),
  3087. 'half',
  3088. '/label',
  3089. '/div',
  3090. );
  3091. $this->assertTags($result, $expected);
  3092. }
  3093. /**
  3094. * testLabel method
  3095. *
  3096. * test label generation.
  3097. *
  3098. * @return void
  3099. */
  3100. public function testLabel() {
  3101. $result = $this->Form->label('Person.name');
  3102. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  3103. $result = $this->Form->label('Person.name');
  3104. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  3105. $result = $this->Form->label('Person.first_name');
  3106. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'First Name', '/label'));
  3107. $result = $this->Form->label('Person.first_name', 'Your first name');
  3108. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'Your first name', '/label'));
  3109. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  3110. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class'), 'Your first name', '/label'));
  3111. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  3112. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  3113. $result = $this->Form->label('Person.first_name', '');
  3114. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), '/label'));
  3115. $result = $this->Form->label('Person.2.name', '');
  3116. $this->assertTags($result, array('label' => array('for' => 'person-2-name'), '/label'));
  3117. }
  3118. /**
  3119. * testTextbox method
  3120. *
  3121. * test textbox element generation
  3122. *
  3123. * @return void
  3124. */
  3125. public function testTextbox() {
  3126. $result = $this->Form->text('Model.field');
  3127. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]')));
  3128. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3129. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]')));
  3130. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3131. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID')));
  3132. }
  3133. /**
  3134. * Test that text() hooks up with request data and error fields.
  3135. *
  3136. * @return void
  3137. */
  3138. public function testTextBoxDataAndError() {
  3139. $this->article['errors'] = [
  3140. 'Contact' => ['text' => 'wrong']
  3141. ];
  3142. $this->Form->create($this->article);
  3143. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3144. $result = $this->Form->text('Model.text');
  3145. $expected = [
  3146. 'input' => [
  3147. 'type' => 'text',
  3148. 'name' => 'Model[text]',
  3149. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3150. ]
  3151. ];
  3152. $this->assertTags($result, $expected);
  3153. $this->Form->request->data['Contact']['text'] = 'test';
  3154. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3155. $expected = [
  3156. 'input' => [
  3157. 'type' => 'text',
  3158. 'name' => 'Contact[text]',
  3159. 'value' => 'test',
  3160. 'id' => 'theID',
  3161. 'class' => 'form-error'
  3162. ]
  3163. ];
  3164. $this->assertTags($result, $expected);
  3165. }
  3166. /**
  3167. * testDefaultValue method
  3168. *
  3169. * Test default value setting
  3170. *
  3171. * @return void
  3172. */
  3173. public function testTextDefaultValue() {
  3174. $this->Form->request->data['Model']['field'] = 'test';
  3175. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3176. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3177. $this->assertTags($result, $expected);
  3178. unset($this->Form->request->data['Model']['field']);
  3179. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3180. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3181. $this->assertTags($result, $expected);
  3182. }
  3183. /**
  3184. * testError method
  3185. *
  3186. * Test field error generation
  3187. *
  3188. * @return void
  3189. */
  3190. public function testError() {
  3191. $this->article['errors'] = [
  3192. 'Article' => ['field' => 'email']
  3193. ];
  3194. $this->Form->create($this->article);
  3195. $result = $this->Form->error('Article.field');
  3196. $expected = [
  3197. ['div' => ['class' => 'error-message']],
  3198. 'email',
  3199. '/div',
  3200. ];
  3201. $this->assertTags($result, $expected);
  3202. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  3203. $expected = [
  3204. ['div' => ['class' => 'error-message']],
  3205. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3206. '/div',
  3207. ];
  3208. $this->assertTags($result, $expected);
  3209. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3210. $expected = [
  3211. ['div' => ['class' => 'error-message']],
  3212. '<strong', 'Badness!', '/strong',
  3213. '/div',
  3214. ];
  3215. $this->assertTags($result, $expected);
  3216. }
  3217. /**
  3218. * Test error with nested lists.
  3219. *
  3220. * @return void
  3221. */
  3222. public function testErrorMessages() {
  3223. $this->article['errors'] = [
  3224. 'Article' => ['field' => 'email']
  3225. ];
  3226. $this->Form->create($this->article);
  3227. $result = $this->Form->error('Article.field', array(
  3228. 'email' => 'No good!'
  3229. ));
  3230. $expected = array(
  3231. 'div' => array('class' => 'error-message'),
  3232. 'No good!',
  3233. '/div'
  3234. );
  3235. $this->assertTags($result, $expected);
  3236. }
  3237. /**
  3238. * test error() with multiple messages.
  3239. *
  3240. * @return void
  3241. */
  3242. public function testErrorMultipleMessages() {
  3243. $this->article['errors'] = [
  3244. 'field' => ['notEmpty', 'email', 'Something else']
  3245. ];
  3246. $this->Form->create($this->article);
  3247. $result = $this->Form->error('field', array(
  3248. 'notEmpty' => 'Cannot be empty',
  3249. 'email' => 'No good!'
  3250. ));
  3251. $expected = array(
  3252. 'div' => array('class' => 'error-message'),
  3253. 'ul' => array(),
  3254. '<li', 'Cannot be empty', '/li',
  3255. '<li', 'No good!', '/li',
  3256. '<li', 'Something else', '/li',
  3257. '/ul',
  3258. '/div'
  3259. );
  3260. $this->assertTags($result, $expected);
  3261. }
  3262. /**
  3263. * test error options when using form->input();
  3264. *
  3265. * @return void
  3266. */
  3267. public function testInputErrorEscape() {
  3268. $this->markTestIncomplete('Need to revisit once models work again.');
  3269. $this->Form->create('ValidateProfile');
  3270. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3271. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3272. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3273. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3274. $this->assertRegExp('/required<br>/', $result);
  3275. }
  3276. /**
  3277. * testPassword method
  3278. *
  3279. * Test password element generation
  3280. *
  3281. * @return void
  3282. */
  3283. public function testPassword() {
  3284. $this->article['errors'] = [
  3285. 'Contact' => [
  3286. 'passwd' => 1
  3287. ]
  3288. ];
  3289. $this->Form->create($this->article);
  3290. $result = $this->Form->password('Contact.field');
  3291. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]')));
  3292. $this->Form->request->data['Contact']['passwd'] = 'test';
  3293. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3294. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3295. }
  3296. /**
  3297. * testRadio method
  3298. *
  3299. * Test radio element set generation
  3300. *
  3301. * @return void
  3302. */
  3303. public function testRadio() {
  3304. $result = $this->Form->radio('Model.field', array('option A'));
  3305. $expected = array(
  3306. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3307. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3308. 'label' => array('for' => 'model-field-0'),
  3309. 'option A',
  3310. '/label'
  3311. );
  3312. $this->assertTags($result, $expected);
  3313. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3314. $expected = array(
  3315. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3316. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3317. array('label' => array('for' => 'model-field-0')),
  3318. 'option A',
  3319. '/label',
  3320. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1')),
  3321. array('label' => array('for' => 'model-field-1')),
  3322. 'option B',
  3323. '/label',
  3324. );
  3325. $this->assertTags($result, $expected);
  3326. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3327. $expected = array(
  3328. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''),
  3329. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male')),
  3330. array('label' => array('for' => 'employee-gender-male')),
  3331. 'Male',
  3332. '/label',
  3333. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female')),
  3334. array('label' => array('for' => 'employee-gender-female')),
  3335. 'Female',
  3336. '/label',
  3337. );
  3338. $this->assertTags($result, $expected);
  3339. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  3340. $expected = array(
  3341. array('input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '')),
  3342. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0')),
  3343. array('label' => array('for' => 'model-custom-0')),
  3344. 'option A',
  3345. '/label',
  3346. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1')),
  3347. array('label' => array('for' => 'model-custom-1')),
  3348. 'option B',
  3349. '/label',
  3350. );
  3351. $this->assertTags($result, $expected);
  3352. }
  3353. /**
  3354. * test generating radio input inside label ala twitter bootstrap
  3355. *
  3356. * @return void
  3357. */
  3358. public function testRadioInputInsideLabel() {
  3359. $this->Form->templates([
  3360. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3361. 'radioContainer' => '{{label}}'
  3362. ]);
  3363. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  3364. $expected = [
  3365. ['input' => [
  3366. 'type' => 'hidden',
  3367. 'name' => 'Model[field]',
  3368. 'value' => ''
  3369. ]],
  3370. ['label' => ['for' => 'model-field-0']],
  3371. ['input' => [
  3372. 'type' => 'radio',
  3373. 'name' => 'Model[field]',
  3374. 'value' => '0',
  3375. 'id' => 'model-field-0'
  3376. ]],
  3377. 'option A',
  3378. '/label',
  3379. ['label' => ['for' => 'model-field-1']],
  3380. ['input' => [
  3381. 'type' => 'radio',
  3382. 'name' => 'Model[field]',
  3383. 'value' => '1',
  3384. 'id' => 'model-field-1'
  3385. ]],
  3386. 'option B',
  3387. '/label',
  3388. ];
  3389. $this->assertTags($result, $expected);
  3390. }
  3391. /**
  3392. * test disabling the hidden input for radio buttons
  3393. *
  3394. * @return void
  3395. */
  3396. public function testRadioHiddenInputDisabling() {
  3397. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3398. $expected = array(
  3399. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'),
  3400. 'label' => array('for' => 'model-1-field-0'),
  3401. 'option A',
  3402. '/label'
  3403. );
  3404. $this->assertTags($result, $expected);
  3405. }
  3406. /**
  3407. * testSelect method
  3408. *
  3409. * Test select element generation.
  3410. *
  3411. * @return void
  3412. */
  3413. public function testSelect() {
  3414. $result = $this->Form->select('Model.field', array());
  3415. $expected = array(
  3416. 'select' => array('name' => 'Model[field]'),
  3417. '/select'
  3418. );
  3419. $this->assertTags($result, $expected);
  3420. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3421. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3422. $expected = array(
  3423. 'select' => array('name' => 'Model[field]'),
  3424. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3425. 'good',
  3426. '/option',
  3427. array('option' => array('value' => 'other')),
  3428. 'bad',
  3429. '/option',
  3430. '/select'
  3431. );
  3432. $this->assertTags($result, $expected);
  3433. $this->Form->request->data = array();
  3434. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3435. $expected = array(
  3436. 'select' => array('name' => 'Model[field]'),
  3437. array('option' => array('value' => 'value')),
  3438. 'good',
  3439. '/option',
  3440. array('option' => array('value' => 'other')),
  3441. 'bad',
  3442. '/option',
  3443. '/select'
  3444. );
  3445. $this->assertTags($result, $expected);
  3446. $result = $this->Form->select(
  3447. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3448. array('empty' => false)
  3449. );
  3450. $expected = array(
  3451. 'select' => array('name' => 'Model[field]'),
  3452. array('option' => array('value' => 'first')),
  3453. 'first &quot;html&quot; &lt;chars&gt;',
  3454. '/option',
  3455. array('option' => array('value' => 'second')),
  3456. 'value',
  3457. '/option',
  3458. '/select'
  3459. );
  3460. $this->assertTags($result, $expected);
  3461. $result = $this->Form->select(
  3462. 'Model.field',
  3463. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3464. array('escape' => false, 'empty' => false)
  3465. );
  3466. $expected = array(
  3467. 'select' => array('name' => 'Model[field]'),
  3468. array('option' => array('value' => 'first')),
  3469. 'first "html" <chars>',
  3470. '/option',
  3471. array('option' => array('value' => 'second')),
  3472. 'value',
  3473. '/option',
  3474. '/select'
  3475. );
  3476. $this->assertTags($result, $expected);
  3477. $options = array(
  3478. array('value' => 'first', 'text' => 'First'),
  3479. array('value' => 'first', 'text' => 'Another First'),
  3480. );
  3481. $result = $this->Form->select(
  3482. 'Model.field',
  3483. $options,
  3484. array('escape' => false, 'empty' => false)
  3485. );
  3486. $expected = array(
  3487. 'select' => array('name' => 'Model[field]'),
  3488. array('option' => array('value' => 'first')),
  3489. 'First',
  3490. '/option',
  3491. array('option' => array('value' => 'first')),
  3492. 'Another First',
  3493. '/option',
  3494. '/select'
  3495. );
  3496. $this->assertTags($result, $expected);
  3497. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3498. $result = $this->Form->select(
  3499. 'Model.contact_id',
  3500. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3501. array('escape' => false, 'empty' => 'pick something')
  3502. );
  3503. $expected = array(
  3504. 'select' => array('name' => 'Model[contact_id]'),
  3505. array('option' => array('value' => '')), 'pick something', '/option',
  3506. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3507. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3508. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3509. '/select'
  3510. );
  3511. $this->assertTags($result, $expected);
  3512. $this->Form->request->data['Model']['field'] = 0;
  3513. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3514. $expected = array(
  3515. 'select' => array('name' => 'Model[field]'),
  3516. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3517. array('option' => array('value' => '1')), 'Yes', '/option',
  3518. '/select'
  3519. );
  3520. $this->assertTags($result, $expected);
  3521. }
  3522. /**
  3523. * test select() with required and disabled attributes.
  3524. *
  3525. * @return void
  3526. */
  3527. public function testSelectRequired() {
  3528. $this->article['required'] = [
  3529. 'user_id' => true
  3530. ];
  3531. $this->Form->create($this->article);
  3532. $result = $this->Form->select('user_id', array('option A'));
  3533. $expected = array(
  3534. 'select' => array(
  3535. 'name' => 'user_id',
  3536. 'required' => 'required'
  3537. ),
  3538. array('option' => array('value' => '0')), 'option A', '/option',
  3539. '/select'
  3540. );
  3541. $this->assertTags($result, $expected);
  3542. $result = $this->Form->select('user_id', array('option A'), array('disabled' => true));
  3543. $expected = array(
  3544. 'select' => array(
  3545. 'name' => 'user_id',
  3546. 'disabled' => 'disabled'
  3547. ),
  3548. array('option' => array('value' => '0')), 'option A', '/option',
  3549. '/select'
  3550. );
  3551. $this->assertTags($result, $expected);
  3552. }
  3553. /**
  3554. * testNestedSelect method
  3555. *
  3556. * test select element generation with optgroups
  3557. *
  3558. * @return void
  3559. */
  3560. public function testNestedSelect() {
  3561. $result = $this->Form->select(
  3562. 'Model.field',
  3563. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3564. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3565. )), array('empty' => false)
  3566. );
  3567. $expected = array(
  3568. 'select' => array('name' => 'Model[field]'),
  3569. array('option' => array('value' => 1)),
  3570. 'One',
  3571. '/option',
  3572. array('option' => array('value' => 2)),
  3573. 'Two',
  3574. '/option',
  3575. array('optgroup' => array('label' => 'Three')),
  3576. array('option' => array('value' => 3)),
  3577. 'Three',
  3578. '/option',
  3579. array('option' => array('value' => 4)),
  3580. 'Four',
  3581. '/option',
  3582. array('option' => array('value' => 5)),
  3583. 'Five',
  3584. '/option',
  3585. '/optgroup',
  3586. '/select'
  3587. );
  3588. $this->assertTags($result, $expected);
  3589. }
  3590. /**
  3591. * testSelectMultiple method
  3592. *
  3593. * test generation of multiple select elements
  3594. *
  3595. * @return void
  3596. */
  3597. public function testSelectMultiple() {
  3598. $options = array('first', 'second', 'third');
  3599. $result = $this->Form->select(
  3600. 'Model.multi_field',
  3601. $options,
  3602. array('multiple' => true)
  3603. );
  3604. $expected = array(
  3605. 'input' => array(
  3606. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3607. ),
  3608. 'select' => array(
  3609. 'name' => 'Model[multi_field][]',
  3610. 'multiple' => 'multiple'
  3611. ),
  3612. array('option' => array('value' => '0')),
  3613. 'first',
  3614. '/option',
  3615. array('option' => array('value' => '1')),
  3616. 'second',
  3617. '/option',
  3618. array('option' => array('value' => '2')),
  3619. 'third',
  3620. '/option',
  3621. '/select'
  3622. );
  3623. $this->assertTags($result, $expected);
  3624. $result = $this->Form->select(
  3625. 'Model.multi_field',
  3626. $options,
  3627. array('multiple' => 'multiple')
  3628. );
  3629. $this->assertTags($result, $expected);
  3630. }
  3631. /**
  3632. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  3633. *
  3634. * @return void
  3635. */
  3636. public function testCheckboxZeroValue() {
  3637. $this->markTestIncomplete('Need to revisit once models work again.');
  3638. $result = $this->Form->input('User.get_spam', array(
  3639. 'type' => 'checkbox',
  3640. 'value' => '0',
  3641. 'hiddenField' => '1',
  3642. ));
  3643. $expected = array(
  3644. 'div' => array('class' => 'input checkbox'),
  3645. array('input' => array(
  3646. 'type' => 'hidden', 'name' => 'data[User][get_spam]',
  3647. 'value' => '1', 'id' => 'UserGetSpam_'
  3648. )),
  3649. array('input' => array(
  3650. 'type' => 'checkbox', 'name' => 'data[User][get_spam]',
  3651. 'value' => '0', 'id' => 'UserGetSpam'
  3652. )),
  3653. 'label' => array('for' => 'UserGetSpam'),
  3654. 'Get Spam',
  3655. '/label',
  3656. '/div'
  3657. );
  3658. $this->assertTags($result, $expected);
  3659. }
  3660. /**
  3661. * test generation of habtm select boxes.
  3662. *
  3663. * @return void
  3664. */
  3665. public function testHabtmSelectBox() {
  3666. $this->markTestIncomplete('Need to revisit once models work again.');
  3667. $this->View->viewVars['contactTags'] = array(
  3668. 1 => 'blue',
  3669. 2 => 'red',
  3670. 3 => 'green'
  3671. );
  3672. $this->Form->request->data = array(
  3673. 'Contact' => array(),
  3674. 'ContactTag' => array(
  3675. array(
  3676. 'id' => '1',
  3677. 'name' => 'blue'
  3678. ),
  3679. array(
  3680. 'id' => 3,
  3681. 'name' => 'green'
  3682. )
  3683. )
  3684. );
  3685. $this->Form->create('Contact');
  3686. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3687. $expected = array(
  3688. 'input' => array(
  3689. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3690. ),
  3691. 'select' => array(
  3692. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  3693. 'multiple' => 'multiple'
  3694. ),
  3695. array('option' => array('value' => '1', 'selected' => 'selected')),
  3696. 'blue',
  3697. '/option',
  3698. array('option' => array('value' => '2')),
  3699. 'red',
  3700. '/option',
  3701. array('option' => array('value' => '3', 'selected' => 'selected')),
  3702. 'green',
  3703. '/option',
  3704. '/select'
  3705. );
  3706. $this->assertTags($result, $expected);
  3707. // make sure only 50 is selected, and not 50f5c0cf
  3708. $this->View->viewVars['contactTags'] = array(
  3709. '1' => 'blue',
  3710. '50f5c0cf' => 'red',
  3711. '50' => 'green'
  3712. );
  3713. $this->Form->request->data = array(
  3714. 'Contact' => array(),
  3715. 'ContactTag' => array(
  3716. array(
  3717. 'id' => 1,
  3718. 'name' => 'blue'
  3719. ),
  3720. array(
  3721. 'id' => 50,
  3722. 'name' => 'green'
  3723. )
  3724. )
  3725. );
  3726. $this->Form->create('Contact');
  3727. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3728. $expected = array(
  3729. 'input' => array(
  3730. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3731. ),
  3732. 'select' => array(
  3733. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  3734. 'multiple' => 'multiple'
  3735. ),
  3736. array('option' => array('value' => '1', 'selected' => 'selected')),
  3737. 'blue',
  3738. '/option',
  3739. array('option' => array('value' => '50f5c0cf')),
  3740. 'red',
  3741. '/option',
  3742. array('option' => array('value' => '50', 'selected' => 'selected')),
  3743. 'green',
  3744. '/option',
  3745. '/select'
  3746. );
  3747. $this->assertTags($result, $expected);
  3748. }
  3749. /**
  3750. * test generation of multi select elements in checkbox format
  3751. *
  3752. * @return void
  3753. */
  3754. public function testSelectMultipleCheckboxes() {
  3755. $result = $this->Form->select(
  3756. 'Model.multi_field',
  3757. array('first', 'second', 'third'),
  3758. array('multiple' => 'checkbox')
  3759. );
  3760. $expected = array(
  3761. 'input' => array(
  3762. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3763. ),
  3764. array('div' => array('class' => 'checkbox')),
  3765. array('input' => array(
  3766. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3767. 'value' => '0', 'id' => 'model-multi-field-0'
  3768. )),
  3769. array('label' => array('for' => 'model-multi-field-0')),
  3770. 'first',
  3771. '/label',
  3772. '/div',
  3773. array('div' => array('class' => 'checkbox')),
  3774. array('input' => array(
  3775. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3776. 'value' => '1', 'id' => 'model-multi-field-1'
  3777. )),
  3778. array('label' => array('for' => 'model-multi-field-1')),
  3779. 'second',
  3780. '/label',
  3781. '/div',
  3782. array('div' => array('class' => 'checkbox')),
  3783. array('input' => array(
  3784. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3785. 'value' => '2', 'id' => 'model-multi-field-2'
  3786. )),
  3787. array('label' => array('for' => 'model-multi-field-2')),
  3788. 'third',
  3789. '/label',
  3790. '/div'
  3791. );
  3792. $this->assertTags($result, $expected);
  3793. $result = $this->Form->select(
  3794. 'Model.multi_field',
  3795. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  3796. array('multiple' => 'checkbox')
  3797. );
  3798. $expected = array(
  3799. 'input' => array(
  3800. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3801. ),
  3802. array('div' => array('class' => 'checkbox')),
  3803. array('input' => array(
  3804. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3805. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  3806. )),
  3807. array('label' => array('for' => 'model-multi-field-a+')),
  3808. 'first',
  3809. '/label',
  3810. '/div',
  3811. array('div' => array('class' => 'checkbox')),
  3812. array('input' => array(
  3813. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3814. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  3815. )),
  3816. array('label' => array('for' => 'model-multi-field-a++')),
  3817. 'second',
  3818. '/label',
  3819. '/div',
  3820. array('div' => array('class' => 'checkbox')),
  3821. array('input' => array(
  3822. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3823. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  3824. )),
  3825. array('label' => array('for' => 'model-multi-field-a+++')),
  3826. 'third',
  3827. '/label',
  3828. '/div'
  3829. );
  3830. $this->assertTags($result, $expected);
  3831. $result = $this->Form->select(
  3832. 'Model.multi_field',
  3833. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  3834. array('multiple' => 'checkbox')
  3835. );
  3836. $expected = array(
  3837. 'input' => array(
  3838. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3839. ),
  3840. array('div' => array('class' => 'checkbox')),
  3841. array('input' => array(
  3842. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3843. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  3844. )),
  3845. array('label' => array('for' => 'model-multi-field-a-b')),
  3846. 'first',
  3847. '/label',
  3848. '/div',
  3849. array('div' => array('class' => 'checkbox')),
  3850. array('input' => array(
  3851. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3852. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  3853. )),
  3854. array('label' => array('for' => 'model-multi-field-a-b1')),
  3855. 'second',
  3856. '/label',
  3857. '/div',
  3858. array('div' => array('class' => 'checkbox')),
  3859. array('input' => array(
  3860. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3861. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  3862. )),
  3863. array('label' => array('for' => 'model-multi-field-a-b2')),
  3864. 'third',
  3865. '/label',
  3866. '/div'
  3867. );
  3868. $this->assertTags($result, $expected);
  3869. }
  3870. /**
  3871. * Ensure that multiCheckbox reads from the request data.
  3872. *
  3873. * @return void
  3874. */
  3875. public function testSelectMultipleCheckboxRequestData() {
  3876. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3877. $result = $this->Form->select(
  3878. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3879. );
  3880. $expected = array(
  3881. 'input' => array(
  3882. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  3883. ),
  3884. array('div' => array('class' => 'checkbox')),
  3885. array('input' => array(
  3886. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3887. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  3888. )),
  3889. array('label' => array('for' => 'model-tags-1', 'class' => 'selected')),
  3890. 'first',
  3891. '/label',
  3892. '/div',
  3893. array('div' => array('class' => 'checkbox')),
  3894. array('input' => array(
  3895. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3896. 'value' => 'Array', 'id' => 'model-tags-array'
  3897. )),
  3898. array('label' => array('for' => 'model-tags-array')),
  3899. 'Array',
  3900. '/label',
  3901. '/div'
  3902. );
  3903. $this->assertTags($result, $expected);
  3904. }
  3905. /**
  3906. * Checks the security hash array generated for multiple-input checkbox elements
  3907. *
  3908. * @return void
  3909. */
  3910. public function testSelectMultipleCheckboxSecurity() {
  3911. $this->Form->request->params['_Token'] = 'testKey';
  3912. $this->assertEquals(array(), $this->Form->fields);
  3913. $result = $this->Form->select(
  3914. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3915. array('multiple' => 'checkbox')
  3916. );
  3917. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3918. $result = $this->Form->secure($this->Form->fields);
  3919. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3920. $this->assertRegExp('/"' . $key . '"/', $result);
  3921. }
  3922. /**
  3923. * Multiple select elements should always be secured as they always participate
  3924. * in the POST data.
  3925. *
  3926. * @return void
  3927. */
  3928. public function testSelectMultipleSecureWithNoOptions() {
  3929. $this->assertEquals(array(), $this->Form->fields);
  3930. $this->Form->select(
  3931. 'Model.select',
  3932. array(),
  3933. array('multiple' => true)
  3934. );
  3935. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3936. }
  3937. /**
  3938. * When a select box has no options it should not be added to the fields list
  3939. * as it always fail post validation.
  3940. *
  3941. * @return void
  3942. */
  3943. public function testSelectNoSecureWithNoOptions() {
  3944. $this->Form->request->params['_Token'] = 'testkey';
  3945. $this->assertEquals([], $this->Form->fields);
  3946. $this->Form->select(
  3947. 'Model.select',
  3948. []
  3949. );
  3950. $this->assertEquals([], $this->Form->fields);
  3951. $this->Form->select(
  3952. 'Model.user_id',
  3953. [],
  3954. ['empty' => true]
  3955. );
  3956. $this->assertEquals(array('Model.user_id'), $this->Form->fields);
  3957. }
  3958. /**
  3959. * testInputMultipleCheckboxes method
  3960. *
  3961. * test input() resulting in multi select elements being generated.
  3962. *
  3963. * @return void
  3964. */
  3965. public function testInputMultipleCheckboxes() {
  3966. $this->markTestIncomplete('Need to revisit once models work again.');
  3967. $result = $this->Form->input('Model.multi_field', array(
  3968. 'options' => array('first', 'second', 'third'),
  3969. 'multiple' => 'checkbox'
  3970. ));
  3971. $expected = array(
  3972. array('div' => array('class' => 'input select')),
  3973. array('label' => array('for' => 'ModelMultiField')),
  3974. 'Multi Field',
  3975. '/label',
  3976. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3977. array('div' => array('class' => 'checkbox')),
  3978. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3979. array('label' => array('for' => 'ModelMultiField0')),
  3980. 'first',
  3981. '/label',
  3982. '/div',
  3983. array('div' => array('class' => 'checkbox')),
  3984. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3985. array('label' => array('for' => 'ModelMultiField1')),
  3986. 'second',
  3987. '/label',
  3988. '/div',
  3989. array('div' => array('class' => 'checkbox')),
  3990. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3991. array('label' => array('for' => 'ModelMultiField2')),
  3992. 'third',
  3993. '/label',
  3994. '/div',
  3995. '/div'
  3996. );
  3997. $this->assertTags($result, $expected);
  3998. $result = $this->Form->input('Model.multi_field', array(
  3999. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4000. 'multiple' => 'checkbox'
  4001. ));
  4002. $expected = array(
  4003. array('div' => array('class' => 'input select')),
  4004. array('label' => array('for' => 'ModelMultiField')),
  4005. 'Multi Field',
  4006. '/label',
  4007. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4008. array('div' => array('class' => 'checkbox')),
  4009. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4010. array('label' => array('for' => 'ModelMultiFieldA')),
  4011. 'first',
  4012. '/label',
  4013. '/div',
  4014. array('div' => array('class' => 'checkbox')),
  4015. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4016. array('label' => array('for' => 'ModelMultiFieldB')),
  4017. 'second',
  4018. '/label',
  4019. '/div',
  4020. array('div' => array('class' => 'checkbox')),
  4021. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4022. array('label' => array('for' => 'ModelMultiFieldC')),
  4023. 'third',
  4024. '/label',
  4025. '/div',
  4026. '/div'
  4027. );
  4028. $this->assertTags($result, $expected);
  4029. $result = $this->Form->input('Model.multi_field', array(
  4030. 'options' => array('1' => 'first'),
  4031. 'multiple' => 'checkbox',
  4032. 'label' => false,
  4033. 'div' => false
  4034. ));
  4035. $expected = array(
  4036. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4037. array('div' => array('class' => 'checkbox')),
  4038. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4039. array('label' => array('for' => 'ModelMultiField1')),
  4040. 'first',
  4041. '/label',
  4042. '/div'
  4043. );
  4044. $this->assertTags($result, $expected);
  4045. $result = $this->Form->input('Model.multi_field', array(
  4046. 'options' => array('2' => 'second'),
  4047. 'multiple' => 'checkbox',
  4048. 'label' => false,
  4049. 'div' => false
  4050. ));
  4051. $expected = array(
  4052. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4053. array('div' => array('class' => 'checkbox')),
  4054. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4055. array('label' => array('for' => 'ModelMultiField2')),
  4056. 'second',
  4057. '/label',
  4058. '/div'
  4059. );
  4060. $this->assertTags($result, $expected);
  4061. }
  4062. /**
  4063. * testSelectHiddenFieldOmission method
  4064. *
  4065. * test that select() with 'hiddenField' => false omits the hidden field
  4066. *
  4067. * @return void
  4068. */
  4069. public function testSelectHiddenFieldOmission() {
  4070. $result = $this->Form->select('Model.multi_field',
  4071. array('first', 'second'),
  4072. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4073. );
  4074. $this->assertNotContains('type="hidden"', $result);
  4075. }
  4076. /**
  4077. * test that select() with multiple = checkbox works with overriding name attribute.
  4078. *
  4079. * @return void
  4080. */
  4081. public function testSelectCheckboxMultipleOverrideName() {
  4082. $result = $this->Form->select('category', ['1', '2'], [
  4083. 'multiple' => 'checkbox',
  4084. 'name' => 'fish',
  4085. ]);
  4086. $expected = array(
  4087. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => ''),
  4088. array('div' => array('class' => 'checkbox')),
  4089. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0')),
  4090. array('label' => array('for' => 'fish-0')), '1', '/label',
  4091. '/div',
  4092. array('div' => array('class' => 'checkbox')),
  4093. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1')),
  4094. array('label' => array('for' => 'fish-1')), '2', '/label',
  4095. '/div'
  4096. );
  4097. $this->assertTags($result, $expected);
  4098. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  4099. 'name' => 'fish',
  4100. ]);
  4101. $this->assertTags($result, $expected);
  4102. }
  4103. /**
  4104. * testCheckbox method
  4105. *
  4106. * Test generation of checkboxes
  4107. *
  4108. * @return void
  4109. */
  4110. public function testCheckbox() {
  4111. $result = $this->Form->checkbox('Model.field');
  4112. $expected = array(
  4113. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  4114. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'))
  4115. );
  4116. $this->assertTags($result, $expected);
  4117. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4118. $expected = array(
  4119. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  4120. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => 'myvalue', 'id' => 'theID'))
  4121. );
  4122. $this->assertTags($result, $expected);
  4123. }
  4124. /**
  4125. * testCheckboxDefaultValue method
  4126. *
  4127. * Test default value setting on checkbox() method
  4128. *
  4129. * @return void
  4130. */
  4131. public function testCheckboxDefaultValue() {
  4132. $this->Form->request->data['Model']['field'] = false;
  4133. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  4134. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  4135. unset($this->Form->request->data['Model']['field']);
  4136. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  4137. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  4138. $this->Form->request->data['Model']['field'] = true;
  4139. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  4140. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  4141. unset($this->Form->request->data['Model']['field']);
  4142. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  4143. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  4144. }
  4145. /**
  4146. * Test checkbox being checked or having errors.
  4147. *
  4148. * @return void
  4149. */
  4150. public function testCheckboxCheckedAndError() {
  4151. $this->article['errors'] = [
  4152. 'published' => true
  4153. ];
  4154. $this->Form->request->data['published'] = 'myvalue';
  4155. $this->Form->create($this->article);
  4156. $result = $this->Form->checkbox('published', array('id' => 'theID', 'value' => 'myvalue'));
  4157. $expected = array(
  4158. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4159. array('input' => array(
  4160. 'type' => 'checkbox',
  4161. 'name' => 'published',
  4162. 'value' => 'myvalue',
  4163. 'id' => 'theID',
  4164. 'checked' => 'checked',
  4165. 'class' => 'form-error'
  4166. ))
  4167. );
  4168. $this->assertTags($result, $expected);
  4169. $this->Form->request->data['published'] = '';
  4170. $result = $this->Form->checkbox('published');
  4171. $expected = array(
  4172. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4173. array('input' => array('type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error'))
  4174. );
  4175. $this->assertTags($result, $expected);
  4176. }
  4177. /**
  4178. * test checkbox() with a custom name attribute
  4179. *
  4180. * @return void
  4181. */
  4182. public function testCheckboxCustomNameAttribute() {
  4183. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4184. $expected = array(
  4185. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0'),
  4186. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1'))
  4187. );
  4188. $this->assertTags($result, $expected);
  4189. }
  4190. /**
  4191. * Test that the hidden input for checkboxes can be omitted or set to a
  4192. * specific value.
  4193. *
  4194. * @return void
  4195. */
  4196. public function testCheckboxHiddenField() {
  4197. $result = $this->Form->checkbox('UserForm.something', array(
  4198. 'hiddenField' => false
  4199. ));
  4200. $expected = array(
  4201. 'input' => array(
  4202. 'type' => 'checkbox',
  4203. 'name' => 'UserForm[something]',
  4204. 'value' => '1'
  4205. ),
  4206. );
  4207. $this->assertTags($result, $expected);
  4208. $result = $this->Form->checkbox('UserForm.something', array(
  4209. 'value' => 'Y',
  4210. 'hiddenField' => 'N',
  4211. ));
  4212. $expected = array(
  4213. array('input' => array(
  4214. 'type' => 'hidden', 'name' => 'UserForm[something]',
  4215. 'value' => 'N'
  4216. )),
  4217. array('input' => array(
  4218. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  4219. 'value' => 'Y'
  4220. )),
  4221. );
  4222. $this->assertTags($result, $expected);
  4223. }
  4224. /**
  4225. * testDateTime method
  4226. *
  4227. * Test generation of date/time select elements
  4228. *
  4229. * @return void
  4230. */
  4231. public function testDateTime() {
  4232. extract($this->dateRegex);
  4233. $result = $this->Form->dateTime('Contact.date', array('empty' => false));
  4234. $now = strtotime('now');
  4235. $expected = array(
  4236. array('select' => array('name' => 'Contact[date][month]')),
  4237. $monthsRegex,
  4238. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4239. date('F', $now),
  4240. '/option',
  4241. '*/select',
  4242. array('select' => array('name' => 'Contact[date][day]')),
  4243. $daysRegex,
  4244. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4245. date('j', $now),
  4246. '/option',
  4247. '*/select',
  4248. array('select' => array('name' => 'Contact[date][year]')),
  4249. $yearsRegex,
  4250. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4251. date('Y', $now),
  4252. '/option',
  4253. '*/select',
  4254. array('select' => array('name' => 'Contact[date][hour]')),
  4255. $hoursRegex,
  4256. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4257. date('g', $now),
  4258. '/option',
  4259. '*/select',
  4260. array('select' => array('name' => 'Contact[date][minute]')),
  4261. $minutesRegex,
  4262. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4263. date('i', $now),
  4264. '/option',
  4265. '*/select',
  4266. array('select' => array('name' => 'Contact[date][meridian]')),
  4267. $meridianRegex,
  4268. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4269. date('a', $now),
  4270. '/option',
  4271. '*/select'
  4272. );
  4273. $this->assertTags($result, $expected);
  4274. }
  4275. /**
  4276. * Test empty defaulting to true for datetime.
  4277. *
  4278. * @return void
  4279. */
  4280. public function testDatetimeEmpty() {
  4281. extract($this->dateRegex);
  4282. $now = strtotime('now');
  4283. $result = $this->Form->dateTime('Contact.date', array(
  4284. 'empty' => true,
  4285. ));
  4286. $expected = array(
  4287. array('select' => array('name' => 'Contact[date][month]')),
  4288. $monthsRegex,
  4289. array('option' => array('value' => '')),
  4290. '/option',
  4291. '*/select',
  4292. array('select' => array('name' => 'Contact[date][day]')),
  4293. $daysRegex,
  4294. array('option' => array('value' => '')),
  4295. '/option',
  4296. '*/select',
  4297. array('select' => array('name' => 'Contact[date][year]')),
  4298. $yearsRegex,
  4299. array('option' => array('value' => '')),
  4300. '/option',
  4301. '*/select',
  4302. array('select' => array('name' => 'Contact[date][hour]')),
  4303. $hoursRegex,
  4304. array('option' => array('value' => '')),
  4305. '/option',
  4306. '*/select',
  4307. array('select' => array('name' => 'Contact[date][minute]')),
  4308. $minutesRegex,
  4309. array('option' => array('value' => '')),
  4310. '/option',
  4311. '*/select',
  4312. array('select' => array('name' => 'Contact[date][meridian]')),
  4313. $meridianRegex,
  4314. array('option' => array('value' => '')),
  4315. '/option',
  4316. '*/select'
  4317. );
  4318. $this->assertTags($result, $expected);
  4319. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4320. }
  4321. /**
  4322. * Test datetime with interval option.
  4323. *
  4324. * @return void
  4325. */
  4326. public function testDatetimeMinuteInterval() {
  4327. extract($this->dateRegex);
  4328. $now = strtotime('now');
  4329. $result = $this->Form->dateTime('Contact.date', array(
  4330. 'interval' => 5,
  4331. 'value' => ''
  4332. ));
  4333. $expected = array(
  4334. array('select' => array('name' => 'Contact[date][month]')),
  4335. $monthsRegex,
  4336. array('option' => array('selected' => 'selected', 'value' => '')),
  4337. '/option',
  4338. '*/select',
  4339. array('select' => array('name' => 'Contact[date][day]')),
  4340. $daysRegex,
  4341. array('option' => array('selected' => 'selected', 'value' => '')),
  4342. '/option',
  4343. '*/select',
  4344. array('select' => array('name' => 'Contact[date][year]')),
  4345. $yearsRegex,
  4346. array('option' => array('selected' => 'selected', 'value' => '')),
  4347. '/option',
  4348. '*/select',
  4349. array('select' => array('name' => 'Contact[date][hour]')),
  4350. $hoursRegex,
  4351. array('option' => array('selected' => 'selected', 'value' => '')),
  4352. '/option',
  4353. '*/select',
  4354. array('select' => array('name' => 'Contact[date][minute]')),
  4355. $minutesRegex,
  4356. array('option' => array('selected' => 'selected', 'value' => '')),
  4357. '/option',
  4358. array('option' => array('value' => '00')),
  4359. '00',
  4360. '/option',
  4361. array('option' => array('value' => '05')),
  4362. '05',
  4363. '/option',
  4364. array('option' => array('value' => '10')),
  4365. '10',
  4366. '/option',
  4367. '*/select',
  4368. );
  4369. $this->assertTags($result, $expected);
  4370. }
  4371. /**
  4372. * Test dateTime with rounding
  4373. *
  4374. * @return void
  4375. */
  4376. public function testDateTimeRounding() {
  4377. $this->Form->request->data['Contact'] = array(
  4378. 'date' => array(
  4379. 'day' => '13',
  4380. 'month' => '12',
  4381. 'year' => '2010',
  4382. 'hour' => '04',
  4383. 'minute' => '19',
  4384. 'meridian' => 'AM'
  4385. )
  4386. );
  4387. $result = $this->Form->dateTime('Contact.date', array('interval' => 15));
  4388. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4389. $result = $this->Form->dateTime('Contact.date', array('interval' => 15, 'round' => 'up'));
  4390. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  4391. $result = $this->Form->dateTime('Contact.date', array('interval' => 5, 'round' => 'down'));
  4392. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4393. }
  4394. /**
  4395. * test that datetime() and default values work.
  4396. *
  4397. * @return void
  4398. */
  4399. public function testDatetimeWithDefault() {
  4400. $result = $this->Form->dateTime('Contact.updated', array('value' => '2009-06-01 11:15:30'));
  4401. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4402. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4403. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4404. $result = $this->Form->dateTime('Contact.updated', array(
  4405. 'default' => '2009-06-01 11:15:30'
  4406. ));
  4407. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4408. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4409. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4410. }
  4411. /**
  4412. * testDateTime all zeros
  4413. *
  4414. * @return void
  4415. */
  4416. public function testDateTimeAllZeros() {
  4417. $result = $this->Form->dateTime('Contact.date', array(
  4418. 'timeFormat' => false,
  4419. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  4420. 'value' => '0000-00-00'
  4421. ));
  4422. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  4423. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  4424. }
  4425. /**
  4426. * testDateTimeEmptyAsArray
  4427. *
  4428. * @return void
  4429. */
  4430. public function testDateTimeEmptyAsArray() {
  4431. $result = $this->Form->dateTime('Contact.date', array(
  4432. 'empty' => array(
  4433. 'day' => 'DAY',
  4434. 'month' => 'MONTH',
  4435. 'year' => 'YEAR',
  4436. 'hour' => 'HOUR',
  4437. 'minute' => 'MINUTE',
  4438. 'meridian' => false
  4439. )
  4440. ));
  4441. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4442. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4443. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4444. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4445. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4446. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4447. $result = $this->Form->dateTime('Contact.date', array(
  4448. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4449. ));
  4450. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4451. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4452. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4453. }
  4454. /**
  4455. * testFormDateTimeMulti method
  4456. *
  4457. * test multiple datetime element generation
  4458. *
  4459. * @return void
  4460. */
  4461. public function testFormDateTimeMulti() {
  4462. extract($this->dateRegex);
  4463. $result = $this->Form->dateTime('Contact.1.updated');
  4464. $this->assertContains('Contact[1][updated][month]', $result);
  4465. $this->assertContains('Contact[1][updated][day]', $result);
  4466. $this->assertContains('Contact[1][updated][year]', $result);
  4467. $this->assertContains('Contact[1][updated][hour]', $result);
  4468. $this->assertContains('Contact[1][updated][minute]', $result);
  4469. $this->assertContains('Contact[1][updated][meridian]', $result);
  4470. }
  4471. /**
  4472. * When changing the date format, the label should always focus the first select box when
  4473. * clicked.
  4474. *
  4475. * @return void
  4476. */
  4477. public function testDateTimeLabelIdMatchesFirstInput() {
  4478. $this->markTestIncomplete('Need to revisit once models work again.');
  4479. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4480. $this->assertContains('label for="ModelDateMonth"', $result);
  4481. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4482. $this->assertContains('label for="ModelDateDay"', $result);
  4483. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4484. $this->assertContains('label for="ModelDateYear"', $result);
  4485. }
  4486. /**
  4487. * testMonth method
  4488. *
  4489. * @return void
  4490. */
  4491. public function testMonth() {
  4492. $result = $this->Form->month('Model.field', ['value' => '']);
  4493. $expected = array(
  4494. array('select' => array('name' => 'Model[field][month]')),
  4495. array('option' => array('value' => '', 'selected' => 'selected')),
  4496. '/option',
  4497. array('option' => array('value' => '01')),
  4498. date('F', strtotime('2008-01-01 00:00:00')),
  4499. '/option',
  4500. array('option' => array('value' => '02')),
  4501. date('F', strtotime('2008-02-01 00:00:00')),
  4502. '/option',
  4503. '*/select',
  4504. );
  4505. $this->assertTags($result, $expected);
  4506. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  4507. $expected = array(
  4508. array('select' => array('name' => 'Model[field][month]')),
  4509. array('option' => array('selected' => 'selected', 'value' => '')),
  4510. '/option',
  4511. array('option' => array('value' => '01')),
  4512. date('F', strtotime('2008-01-01 00:00:00')),
  4513. '/option',
  4514. array('option' => array('value' => '02')),
  4515. date('F', strtotime('2008-02-01 00:00:00')),
  4516. '/option',
  4517. '*/select',
  4518. );
  4519. $this->assertTags($result, $expected);
  4520. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  4521. $expected = array(
  4522. array('select' => array('name' => 'Model[field][month]')),
  4523. array('option' => array('selected' => 'selected', 'value' => '')),
  4524. '/option',
  4525. array('option' => array('value' => '01')),
  4526. '1',
  4527. '/option',
  4528. array('option' => array('value' => '02')),
  4529. '2',
  4530. '/option',
  4531. '*/select',
  4532. );
  4533. $this->assertTags($result, $expected);
  4534. $monthNames = [
  4535. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4536. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  4537. ];
  4538. $result = $this->Form->month('Model.field', array('value' => '1', 'monthNames' => $monthNames));
  4539. $expected = array(
  4540. array('select' => array('name' => 'Model[field][month]')),
  4541. array('option' => array('value' => '')),
  4542. '/option',
  4543. array('option' => array('value' => '01', 'selected' => 'selected')),
  4544. 'Jan',
  4545. '/option',
  4546. array('option' => array('value' => '02')),
  4547. 'Feb',
  4548. '/option',
  4549. '*/select',
  4550. );
  4551. $this->assertTags($result, $expected);
  4552. $this->Form->request->data['Project']['release'] = '2050-02-10';
  4553. $result = $this->Form->month('Project.release');
  4554. $expected = array(
  4555. array('select' => array('name' => 'Project[release][month]')),
  4556. array('option' => array('value' => '')),
  4557. '/option',
  4558. array('option' => array('value' => '01')),
  4559. 'January',
  4560. '/option',
  4561. array('option' => array('value' => '02', 'selected' => 'selected')),
  4562. 'February',
  4563. '/option',
  4564. '*/select',
  4565. );
  4566. $this->assertTags($result, $expected);
  4567. }
  4568. /**
  4569. * testDay method
  4570. *
  4571. * @return void
  4572. */
  4573. public function testDay() {
  4574. extract($this->dateRegex);
  4575. $result = $this->Form->day('Model.field', array('value' => false));
  4576. $expected = array(
  4577. array('select' => array('name' => 'Model[field][day]')),
  4578. array('option' => array('selected' => 'selected', 'value' => '')),
  4579. '/option',
  4580. array('option' => array('value' => '01')),
  4581. '1',
  4582. '/option',
  4583. array('option' => array('value' => '02')),
  4584. '2',
  4585. '/option',
  4586. $daysRegex,
  4587. '/select',
  4588. );
  4589. $this->assertTags($result, $expected);
  4590. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4591. $result = $this->Form->day('Model.field');
  4592. $expected = array(
  4593. array('select' => array('name' => 'Model[field][day]')),
  4594. array('option' => array('value' => '')),
  4595. '/option',
  4596. array('option' => array('value' => '01')),
  4597. '1',
  4598. '/option',
  4599. array('option' => array('value' => '02')),
  4600. '2',
  4601. '/option',
  4602. $daysRegex,
  4603. array('option' => array('value' => '10', 'selected' => 'selected')),
  4604. '10',
  4605. '/option',
  4606. $daysRegex,
  4607. '/select',
  4608. );
  4609. $this->assertTags($result, $expected);
  4610. $this->Form->request->data['Model']['field'] = '';
  4611. $result = $this->Form->day('Model.field', array('value' => '10'));
  4612. $expected = array(
  4613. array('select' => array('name' => 'Model[field][day]')),
  4614. array('option' => array('value' => '')),
  4615. '/option',
  4616. array('option' => array('value' => '01')),
  4617. '1',
  4618. '/option',
  4619. array('option' => array('value' => '02')),
  4620. '2',
  4621. '/option',
  4622. $daysRegex,
  4623. array('option' => array('value' => '10', 'selected' => 'selected')),
  4624. '10',
  4625. '/option',
  4626. $daysRegex,
  4627. '/select',
  4628. );
  4629. $this->assertTags($result, $expected);
  4630. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4631. $result = $this->Form->day('Project.release');
  4632. $expected = array(
  4633. array('select' => array('name' => 'Project[release][day]')),
  4634. array('option' => array('value' => '')),
  4635. '/option',
  4636. array('option' => array('value' => '01')),
  4637. '1',
  4638. '/option',
  4639. array('option' => array('value' => '02')),
  4640. '2',
  4641. '/option',
  4642. $daysRegex,
  4643. array('option' => array('value' => '10', 'selected' => 'selected')),
  4644. '10',
  4645. '/option',
  4646. $daysRegex,
  4647. '/select',
  4648. );
  4649. $this->assertTags($result, $expected);
  4650. }
  4651. /**
  4652. * testMinute method
  4653. *
  4654. * @return void
  4655. */
  4656. public function testMinute() {
  4657. extract($this->dateRegex);
  4658. $result = $this->Form->minute('Model.field', ['value' => '']);
  4659. $expected = array(
  4660. array('select' => array('name' => 'Model[field][minute]')),
  4661. array('option' => array('selected' => 'selected', 'value' => '')),
  4662. '/option',
  4663. array('option' => array('value' => '00')),
  4664. '00',
  4665. '/option',
  4666. array('option' => array('value' => '01')),
  4667. '01',
  4668. '/option',
  4669. array('option' => array('value' => '02')),
  4670. '02',
  4671. '/option',
  4672. $minutesRegex,
  4673. '/select',
  4674. );
  4675. $this->assertTags($result, $expected);
  4676. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4677. $result = $this->Form->minute('Model.field');
  4678. $expected = array(
  4679. array('select' => array('name' => 'Model[field][minute]')),
  4680. array('option' => array('value' => '')),
  4681. '/option',
  4682. array('option' => array('value' => '00')),
  4683. '00',
  4684. '/option',
  4685. array('option' => array('value' => '01')),
  4686. '01',
  4687. '/option',
  4688. array('option' => array('value' => '02')),
  4689. '02',
  4690. '/option',
  4691. $minutesRegex,
  4692. array('option' => array('value' => '12', 'selected' => 'selected')),
  4693. '12',
  4694. '/option',
  4695. $minutesRegex,
  4696. '/select',
  4697. );
  4698. $this->assertTags($result, $expected);
  4699. $this->Form->request->data['Model']['field'] = '';
  4700. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4701. $expected = array(
  4702. array('select' => array('name' => 'Model[field][minute]')),
  4703. array('option' => array('selected' => 'selected', 'value' => '')),
  4704. '/option',
  4705. array('option' => array('value' => '00')),
  4706. '00',
  4707. '/option',
  4708. array('option' => array('value' => '05')),
  4709. '05',
  4710. '/option',
  4711. array('option' => array('value' => '10')),
  4712. '10',
  4713. '/option',
  4714. $minutesRegex,
  4715. '/select',
  4716. );
  4717. $this->assertTags($result, $expected);
  4718. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  4719. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4720. $expected = array(
  4721. array('select' => array('name' => 'Model[field][minute]')),
  4722. array('option' => array('value' => '')),
  4723. '/option',
  4724. array('option' => array('value' => '00')),
  4725. '00',
  4726. '/option',
  4727. array('option' => array('value' => '05')),
  4728. '05',
  4729. '/option',
  4730. array('option' => array('value' => '10', 'selected' => 'selected')),
  4731. '10',
  4732. '/option',
  4733. $minutesRegex,
  4734. '/select',
  4735. );
  4736. $this->assertTags($result, $expected);
  4737. }
  4738. /**
  4739. * Test generating an input for the meridian.
  4740. *
  4741. * @return void
  4742. */
  4743. public function testMeridian() {
  4744. extract($this->dateRegex);
  4745. $now = time();
  4746. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  4747. $expected = [
  4748. array('select' => array('name' => 'Model[field][meridian]')),
  4749. array('option' => array('value' => '')),
  4750. '/option',
  4751. $meridianRegex,
  4752. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4753. date('a', $now),
  4754. '/option',
  4755. '*/select'
  4756. ];
  4757. $this->assertTags($result, $expected);
  4758. }
  4759. /**
  4760. * testHour method
  4761. *
  4762. * @return void
  4763. */
  4764. public function testHour() {
  4765. extract($this->dateRegex);
  4766. $result = $this->Form->hour('Model.field', ['value' => '']);
  4767. $expected = array(
  4768. array('select' => array('name' => 'Model[field][hour]')),
  4769. array('option' => array('selected' => 'selected', 'value' => '')),
  4770. '/option',
  4771. array('option' => array('value' => '01')),
  4772. '1',
  4773. '/option',
  4774. array('option' => array('value' => '02')),
  4775. '2',
  4776. '/option',
  4777. $hoursRegex,
  4778. '/select',
  4779. );
  4780. $this->assertTags($result, $expected);
  4781. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4782. $result = $this->Form->hour('Model.field', ['format' => 12]);
  4783. $expected = array(
  4784. array('select' => array('name' => 'Model[field][hour]')),
  4785. array('option' => array('value' => '')),
  4786. '/option',
  4787. array('option' => array('value' => '01')),
  4788. '1',
  4789. '/option',
  4790. array('option' => array('value' => '02')),
  4791. '2',
  4792. '/option',
  4793. $hoursRegex,
  4794. array('option' => array('value' => '12', 'selected' => 'selected')),
  4795. '12',
  4796. '/option',
  4797. '/select',
  4798. );
  4799. $this->assertTags($result, $expected);
  4800. $this->Form->request->data['Model']['field'] = '';
  4801. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => '23'));
  4802. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  4803. $result = $this->Form->hour('Model.field', array('format' => 12, 'value' => '23'));
  4804. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  4805. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4806. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4807. $expected = array(
  4808. array('select' => array('name' => 'Model[field][hour]')),
  4809. array('option' => array('value' => '')),
  4810. '/option',
  4811. array('option' => array('value' => '00', 'selected' => 'selected')),
  4812. '0',
  4813. '/option',
  4814. array('option' => array('value' => '01')),
  4815. '1',
  4816. '/option',
  4817. array('option' => array('value' => '02')),
  4818. '2',
  4819. '/option',
  4820. $hoursRegex,
  4821. '/select',
  4822. );
  4823. $this->assertTags($result, $expected);
  4824. unset($this->Form->request->data['Model']['field']);
  4825. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => 'now'));
  4826. $thisHour = date('H');
  4827. $optValue = date('G');
  4828. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  4829. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  4830. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4831. $expected = array(
  4832. array('select' => array('name' => 'Model[field][hour]')),
  4833. array('option' => array('value' => '')),
  4834. '/option',
  4835. array('option' => array('value' => '00')),
  4836. '0',
  4837. '/option',
  4838. array('option' => array('value' => '01', 'selected' => 'selected')),
  4839. '1',
  4840. '/option',
  4841. array('option' => array('value' => '02')),
  4842. '2',
  4843. '/option',
  4844. $hoursRegex,
  4845. '/select',
  4846. );
  4847. $this->assertTags($result, $expected);
  4848. }
  4849. /**
  4850. * testYear method
  4851. *
  4852. * @return void
  4853. */
  4854. public function testYear() {
  4855. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  4856. $expected = array(
  4857. array('select' => array('name' => 'Model[field][year]')),
  4858. array('option' => array('selected' => 'selected', 'value' => '')),
  4859. '/option',
  4860. array('option' => array('value' => '2007')),
  4861. '2007',
  4862. '/option',
  4863. array('option' => array('value' => '2006')),
  4864. '2006',
  4865. '/option',
  4866. '/select',
  4867. );
  4868. $this->assertTags($result, $expected);
  4869. $result = $this->Form->year('Model.field', [
  4870. 'value' => '',
  4871. 'minYear' => 2006,
  4872. 'maxYear' => 2007,
  4873. 'orderYear' => 'asc'
  4874. ]);
  4875. $expected = array(
  4876. array('select' => array('name' => 'Model[field][year]')),
  4877. array('option' => array('selected' => 'selected', 'value' => '')),
  4878. '/option',
  4879. array('option' => array('value' => '2006')),
  4880. '2006',
  4881. '/option',
  4882. array('option' => array('value' => '2007')),
  4883. '2007',
  4884. '/option',
  4885. '/select',
  4886. );
  4887. $this->assertTags($result, $expected);
  4888. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  4889. $result = $this->Form->year('Contact.published', [
  4890. 'empty' => false,
  4891. 'minYear' => 2006,
  4892. 'maxYear' => 2007,
  4893. ]);
  4894. $expected = array(
  4895. array('select' => array('name' => 'Contact[published][year]')),
  4896. array('option' => array('value' => '2007')),
  4897. '2007',
  4898. '/option',
  4899. array('option' => array('value' => '2006', 'selected' => 'selected')),
  4900. '2006',
  4901. '/option',
  4902. '/select',
  4903. );
  4904. $this->assertTags($result, $expected);
  4905. }
  4906. /**
  4907. * testYearAutoExpandRange method
  4908. *
  4909. * @return void
  4910. */
  4911. public function testYearAutoExpandRange() {
  4912. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  4913. $result = $this->Form->year('User.birthday');
  4914. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4915. $result = $matches[1];
  4916. $expected = range(date('Y') + 5, 1930);
  4917. $this->assertEquals($expected, $result);
  4918. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4919. $result = $this->Form->year('Project.release');
  4920. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4921. $result = $matches[1];
  4922. $expected = range(2050, date('Y') - 5);
  4923. $this->assertEquals($expected, $result);
  4924. $this->Form->request->data['Project']['release'] = '1881-10-10';
  4925. $result = $this->Form->year('Project.release', [
  4926. 'minYear' => 1890,
  4927. 'maxYear' => 1900
  4928. ]);
  4929. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4930. $result = $matches[1];
  4931. $expected = range(1900, 1881);
  4932. $this->assertEquals($expected, $result);
  4933. }
  4934. /**
  4935. * Test that input() accepts the type of date and passes options in.
  4936. *
  4937. * @return void
  4938. */
  4939. public function testInputDate() {
  4940. $this->Form->request->data = array(
  4941. 'month_year' => array('month' => date('m')),
  4942. );
  4943. $this->Form->create($this->article);
  4944. $result = $this->Form->input('month_year', array(
  4945. 'label' => false,
  4946. 'type' => 'date',
  4947. 'minYear' => 2006,
  4948. 'maxYear' => 2008
  4949. ));
  4950. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  4951. $this->assertNotContains('value="2008" selected="selected"', $result);
  4952. }
  4953. /**
  4954. * testInputDateMaxYear method
  4955. *
  4956. * Let's say we want to only allow users born from 2006 to 2008 to register
  4957. * This being the first singup page, we still don't have any data
  4958. *
  4959. * @return void
  4960. */
  4961. public function testInputDateMaxYear() {
  4962. $this->Form->request->data = [];
  4963. $this->Form->create($this->article);
  4964. $result = $this->Form->input('birthday', array(
  4965. 'label' => false,
  4966. 'type' => 'date',
  4967. 'minYear' => 2006,
  4968. 'maxYear' => 2008
  4969. ));
  4970. $this->assertContains('value="2008" selected="selected"', $result);
  4971. }
  4972. /**
  4973. * testTextArea method
  4974. *
  4975. * @return void
  4976. */
  4977. public function testTextArea() {
  4978. $this->Form->request->data = array('field' => 'some test data');
  4979. $result = $this->Form->textarea('field');
  4980. $expected = array(
  4981. 'textarea' => array('name' => 'field'),
  4982. 'some test data',
  4983. '/textarea',
  4984. );
  4985. $this->assertTags($result, $expected);
  4986. $result = $this->Form->textarea('user.bio');
  4987. $expected = array(
  4988. 'textarea' => array('name' => 'user[bio]'),
  4989. '/textarea',
  4990. );
  4991. $this->assertTags($result, $expected);
  4992. $this->Form->request->data = array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  4993. $result = $this->Form->textarea('field');
  4994. $expected = array(
  4995. 'textarea' => array('name' => 'field'),
  4996. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  4997. '/textarea',
  4998. );
  4999. $this->assertTags($result, $expected);
  5000. $this->Form->request->data = [
  5001. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  5002. ];
  5003. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5004. $expected = array(
  5005. 'textarea' => array('name' => 'Model[field]'),
  5006. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5007. '/textarea',
  5008. );
  5009. $this->assertTags($result, $expected);
  5010. $result = $this->Form->textarea('0.OtherModel.field');
  5011. $expected = array(
  5012. 'textarea' => array('name' => '0[OtherModel][field]'),
  5013. '/textarea'
  5014. );
  5015. $this->assertTags($result, $expected);
  5016. }
  5017. /**
  5018. * testTextAreaWithStupidCharacters method
  5019. *
  5020. * test text area with non-ascii characters
  5021. *
  5022. * @return void
  5023. */
  5024. public function testTextAreaWithStupidCharacters() {
  5025. $result = $this->Form->textarea('Post.content', [
  5026. 'value' => "GREAT®",
  5027. 'rows' => '15',
  5028. 'cols' => '75'
  5029. ]);
  5030. $expected = [
  5031. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5032. 'GREAT®',
  5033. '/textarea',
  5034. ];
  5035. $this->assertTags($result, $expected);
  5036. }
  5037. /**
  5038. * testHiddenField method
  5039. *
  5040. * @return void
  5041. */
  5042. public function testHiddenField() {
  5043. $this->article['errors'] = [
  5044. 'field' => true
  5045. ];
  5046. $this->Form->request->data['field'] = 'test';
  5047. $this->Form->create($this->article);
  5048. $result = $this->Form->hidden('field', array('id' => 'theID'));
  5049. $this->assertTags($result, array(
  5050. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test'))
  5051. );
  5052. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5053. $expected = [
  5054. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  5055. ];
  5056. $this->assertTags($result, $expected);
  5057. }
  5058. /**
  5059. * testFileUploadField method
  5060. *
  5061. * @return void
  5062. */
  5063. public function testFileUploadField() {
  5064. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5065. $result = $this->Form->file('Model.upload');
  5066. $this->assertTags($result, $expected);
  5067. $this->Form->request->data['Model']['upload'] = [
  5068. 'name' => '', 'type' => '', 'tmp_name' => '',
  5069. 'error' => 4, 'size' => 0
  5070. ];
  5071. $result = $this->Form->file('Model.upload');
  5072. $this->assertTags($result, $expected);
  5073. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  5074. $result = $this->Form->file('Model.upload');
  5075. $this->assertTags($result, $expected);
  5076. }
  5077. /**
  5078. * test File upload input on a model not used in create();
  5079. *
  5080. * @return void
  5081. */
  5082. public function testFileUploadOnOtherModel() {
  5083. $this->Form->create($this->article, array('type' => 'file'));
  5084. $result = $this->Form->file('ValidateProfile.city');
  5085. $expected = array(
  5086. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]')
  5087. );
  5088. $this->assertTags($result, $expected);
  5089. }
  5090. /**
  5091. * testButton method
  5092. *
  5093. * @return void
  5094. */
  5095. public function testButton() {
  5096. $result = $this->Form->button('Hi');
  5097. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5098. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5099. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5100. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5101. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5102. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5103. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5104. $result = $this->Form->button('No type', array('type' => false));
  5105. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5106. $result = $this->Form->button('Upload Text', array(
  5107. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  5108. 'escape' => false
  5109. ));
  5110. $this->assertNotRegExp('/\&039/', $result);
  5111. }
  5112. /**
  5113. * Test that button() makes unlocked fields by default.
  5114. *
  5115. * @return void
  5116. */
  5117. public function testButtonUnlockedByDefault() {
  5118. $this->Form->request->params['_csrfToken'] = 'secured';
  5119. $this->Form->button('Save', array('name' => 'save'));
  5120. $this->Form->button('Clear');
  5121. $result = $this->Form->unlockField();
  5122. $this->assertEquals(array('save'), $result);
  5123. }
  5124. /**
  5125. * testPostButton method
  5126. *
  5127. * @return void
  5128. */
  5129. public function testPostButton() {
  5130. $result = $this->Form->postButton('Hi', '/controller/action');
  5131. $this->assertTags($result, array(
  5132. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5133. 'div' => array('style' => 'display:none;'),
  5134. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5135. '/div',
  5136. 'button' => array('type' => 'submit'),
  5137. 'Hi',
  5138. '/button',
  5139. '/form'
  5140. ));
  5141. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5142. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  5143. }
  5144. /**
  5145. * Test using postButton with N dimensional data.
  5146. *
  5147. * @return void
  5148. */
  5149. public function testPostButtonNestedData() {
  5150. $data = array(
  5151. 'one' => array(
  5152. 'two' => array(
  5153. 3, 4, 5
  5154. )
  5155. )
  5156. );
  5157. $result = $this->Form->postButton('Send', '/', array('data' => $data));
  5158. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5159. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5160. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5161. }
  5162. /**
  5163. * Test that postButton adds _Token fields.
  5164. *
  5165. * @return void
  5166. */
  5167. public function testSecurePostButton() {
  5168. $this->Form->request->params['_csrfToken'] = 'testkey';
  5169. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5170. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5171. $expected = array(
  5172. 'form' => array(
  5173. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5174. ),
  5175. array('div' => array('style' => 'display:none;')),
  5176. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5177. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5178. '/div',
  5179. 'button' => array('type' => 'submit'),
  5180. 'Delete',
  5181. '/button',
  5182. array('div' => array('style' => 'display:none;')),
  5183. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5184. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5185. '/div',
  5186. '/form',
  5187. );
  5188. $this->assertTags($result, $expected);
  5189. }
  5190. /**
  5191. * testPostLink method
  5192. *
  5193. * @return void
  5194. */
  5195. public function testPostLink() {
  5196. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5197. $this->assertTags($result, array(
  5198. 'form' => array(
  5199. 'method' => 'post', 'action' => '/posts/delete/1',
  5200. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5201. ),
  5202. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5203. '/form',
  5204. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5205. 'Delete',
  5206. '/a'
  5207. ));
  5208. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5209. $this->assertTags($result, array(
  5210. 'form' => array(
  5211. 'method' => 'post', 'action' => '/posts/delete/1',
  5212. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5213. ),
  5214. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5215. '/form',
  5216. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5217. 'Delete',
  5218. '/a'
  5219. ));
  5220. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5221. $this->assertTags($result, array(
  5222. 'form' => array(
  5223. 'method' => 'post', 'action' => '/posts/delete/1',
  5224. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5225. ),
  5226. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5227. '/form',
  5228. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5229. 'Delete',
  5230. '/a'
  5231. ));
  5232. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  5233. $this->assertTags($result, array(
  5234. 'form' => array(
  5235. 'method' => 'post', 'action' => '/posts/delete/1',
  5236. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5237. ),
  5238. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5239. '/form',
  5240. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;&#039;Confirm&#039; this \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5241. 'Delete',
  5242. '/a'
  5243. ));
  5244. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5245. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  5246. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  5247. $this->assertTags($result, array(
  5248. 'form' => array(
  5249. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  5250. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5251. ),
  5252. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5253. '/form',
  5254. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5255. 'Delete',
  5256. '/a'
  5257. ));
  5258. $result = $this->Form->postLink(
  5259. '',
  5260. array('controller' => 'items', 'action' => 'delete', 10),
  5261. array('class' => 'btn btn-danger', 'escape' => false),
  5262. 'Confirm thing'
  5263. );
  5264. $this->assertTags($result, array(
  5265. 'form' => array(
  5266. 'method' => 'post', 'action' => '/items/delete/10',
  5267. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5268. ),
  5269. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5270. '/form',
  5271. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5272. '/a'
  5273. ));
  5274. }
  5275. /**
  5276. * Test using postLink with N dimensional data.
  5277. *
  5278. * @return void
  5279. */
  5280. public function testPostLinkNestedData() {
  5281. $data = array(
  5282. 'one' => array(
  5283. 'two' => array(
  5284. 3, 4, 5
  5285. )
  5286. )
  5287. );
  5288. $result = $this->Form->postLink('Send', '/', array('data' => $data));
  5289. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5290. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5291. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5292. }
  5293. /**
  5294. * test creating postLinks after a GET form.
  5295. *
  5296. * @return void
  5297. */
  5298. public function testPostLinkAfterGetForm() {
  5299. $this->Form->request->params['_csrfToken'] = 'testkey';
  5300. $this->Form->request->params['_Token'] = 'val';
  5301. $this->Form->create($this->article, array('type' => 'get'));
  5302. $this->Form->end();
  5303. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5304. $this->assertTags($result, array(
  5305. 'form' => array(
  5306. 'method' => 'post', 'action' => '/posts/delete/1',
  5307. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5308. ),
  5309. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5310. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5311. 'div' => array('style' => 'display:none;'),
  5312. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5313. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5314. '/div',
  5315. '/form',
  5316. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5317. 'Delete',
  5318. '/a'
  5319. ));
  5320. }
  5321. /**
  5322. * Test that postLink adds form tags to view block
  5323. *
  5324. * @return void
  5325. */
  5326. public function testPostLinkFormBuffer() {
  5327. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
  5328. $this->assertTags($result, array(
  5329. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5330. 'Delete',
  5331. '/a'
  5332. ));
  5333. $result = $this->View->fetch('postLink');
  5334. $this->assertTags($result, array(
  5335. 'form' => array(
  5336. 'method' => 'post', 'action' => '/posts/delete/1',
  5337. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5338. ),
  5339. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5340. '/form'
  5341. ));
  5342. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  5343. array('inline' => false, 'method' => 'DELETE')
  5344. );
  5345. $this->assertTags($result, array(
  5346. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5347. 'Delete',
  5348. '/a'
  5349. ));
  5350. $result = $this->View->fetch('postLink');
  5351. $this->assertTags($result, array(
  5352. 'form' => array(
  5353. 'method' => 'post', 'action' => '/posts/delete/1',
  5354. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5355. ),
  5356. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5357. '/form',
  5358. array(
  5359. 'form' => array(
  5360. 'method' => 'post', 'action' => '/posts/delete/2',
  5361. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5362. ),
  5363. ),
  5364. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  5365. '/form'
  5366. ));
  5367. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  5368. $this->assertTags($result, array(
  5369. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5370. 'Delete',
  5371. '/a'
  5372. ));
  5373. $result = $this->View->fetch('foobar');
  5374. $this->assertTags($result, array(
  5375. 'form' => array(
  5376. 'method' => 'post', 'action' => '/posts/delete/1',
  5377. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5378. ),
  5379. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5380. '/form'
  5381. ));
  5382. }
  5383. /**
  5384. * testSubmitButton method
  5385. *
  5386. * @return void
  5387. */
  5388. public function testSubmitButton() {
  5389. $result = $this->Form->submit('');
  5390. $expected = array(
  5391. 'div' => array('class' => 'submit'),
  5392. 'input' => array('type' => 'submit', 'value' => ''),
  5393. '/div'
  5394. );
  5395. $this->assertTags($result, $expected);
  5396. $result = $this->Form->submit('Test Submit');
  5397. $expected = array(
  5398. 'div' => array('class' => 'submit'),
  5399. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5400. '/div'
  5401. );
  5402. $this->assertTags($result, $expected);
  5403. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5404. $expected = array(
  5405. 'span' => array('class' => 'submit'),
  5406. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5407. '/span'
  5408. );
  5409. $this->assertTags($result, $expected);
  5410. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5411. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5412. $this->assertTags($result, $expected);
  5413. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5414. $expected = array(
  5415. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5416. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5417. '/div'
  5418. );
  5419. $this->assertTags($result, $expected);
  5420. $result = $this->Form->submit('Next >');
  5421. $expected = array(
  5422. 'div' => array('class' => 'submit'),
  5423. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5424. '/div'
  5425. );
  5426. $this->assertTags($result, $expected);
  5427. $result = $this->Form->submit('Next >', array('escape' => false));
  5428. $expected = array(
  5429. 'div' => array('class' => 'submit'),
  5430. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5431. '/div'
  5432. );
  5433. $this->assertTags($result, $expected);
  5434. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5435. $expected = array(
  5436. 'div' => array('class' => 'submit'),
  5437. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5438. '/div'
  5439. );
  5440. $this->assertTags($result, $expected);
  5441. $before = '--before--';
  5442. $after = '--after--';
  5443. $result = $this->Form->submit('Test', array('before' => $before));
  5444. $expected = array(
  5445. 'div' => array('class' => 'submit'),
  5446. '--before--',
  5447. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5448. '/div'
  5449. );
  5450. $this->assertTags($result, $expected);
  5451. $result = $this->Form->submit('Test', array('after' => $after));
  5452. $expected = array(
  5453. 'div' => array('class' => 'submit'),
  5454. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5455. '--after--',
  5456. '/div'
  5457. );
  5458. $this->assertTags($result, $expected);
  5459. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5460. $expected = array(
  5461. 'div' => array('class' => 'submit'),
  5462. '--before--',
  5463. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5464. '--after--',
  5465. '/div'
  5466. );
  5467. $this->assertTags($result, $expected);
  5468. }
  5469. /**
  5470. * test image submit types.
  5471. *
  5472. * @return void
  5473. */
  5474. public function testSubmitImage() {
  5475. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5476. $expected = array(
  5477. 'div' => array('class' => 'submit'),
  5478. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5479. '/div'
  5480. );
  5481. $this->assertTags($result, $expected);
  5482. $result = $this->Form->submit('/relative/cake.power.gif');
  5483. $expected = array(
  5484. 'div' => array('class' => 'submit'),
  5485. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5486. '/div'
  5487. );
  5488. $this->assertTags($result, $expected);
  5489. $result = $this->Form->submit('cake.power.gif');
  5490. $expected = array(
  5491. 'div' => array('class' => 'submit'),
  5492. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5493. '/div'
  5494. );
  5495. $this->assertTags($result, $expected);
  5496. $result = $this->Form->submit('Not.an.image');
  5497. $expected = array(
  5498. 'div' => array('class' => 'submit'),
  5499. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5500. '/div'
  5501. );
  5502. $this->assertTags($result, $expected);
  5503. $after = '--after--';
  5504. $before = '--before--';
  5505. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5506. $expected = array(
  5507. 'div' => array('class' => 'submit'),
  5508. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5509. '--after--',
  5510. '/div'
  5511. );
  5512. $this->assertTags($result, $expected);
  5513. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5514. $expected = array(
  5515. 'div' => array('class' => 'submit'),
  5516. '--before--',
  5517. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5518. '/div'
  5519. );
  5520. $this->assertTags($result, $expected);
  5521. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5522. $expected = array(
  5523. 'div' => array('class' => 'submit'),
  5524. '--before--',
  5525. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5526. '--after--',
  5527. '/div'
  5528. );
  5529. $this->assertTags($result, $expected);
  5530. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5531. $expected = array(
  5532. 'div' => array('class' => 'submit'),
  5533. '--before--',
  5534. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5535. '--after--',
  5536. '/div'
  5537. );
  5538. $this->assertTags($result, $expected);
  5539. }
  5540. /**
  5541. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5542. * be submitted at a time.
  5543. *
  5544. * @return void
  5545. */
  5546. public function testSubmitUnlockedByDefault() {
  5547. $this->Form->request->params['_Token'] = 'secured';
  5548. $this->Form->submit('Go go');
  5549. $this->Form->submit('Save', array('name' => 'save'));
  5550. $result = $this->Form->unlockField();
  5551. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5552. }
  5553. /**
  5554. * Test submit image with timestamps.
  5555. *
  5556. * @return void
  5557. */
  5558. public function testSubmitImageTimestamp() {
  5559. Configure::write('Asset.timestamp', 'force');
  5560. $result = $this->Form->submit('cake.power.gif');
  5561. $expected = array(
  5562. 'div' => array('class' => 'submit'),
  5563. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5564. '/div'
  5565. );
  5566. $this->assertTags($result, $expected);
  5567. }
  5568. /**
  5569. * test that datetime() works with GET style forms.
  5570. *
  5571. * @return void
  5572. */
  5573. public function testDateTimeWithGetForms() {
  5574. extract($this->dateRegex);
  5575. $this->Form->create($this->article, array('type' => 'get'));
  5576. $result = $this->Form->datetime('created');
  5577. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  5578. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  5579. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  5580. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  5581. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  5582. $this->assertContains('name="created[meridian]"', $result, 'meridian name attribute is wrong.');
  5583. }
  5584. /**
  5585. * testFormMagicInput method
  5586. *
  5587. * @return void
  5588. */
  5589. public function testFormMagicInput() {
  5590. $this->markTestIncomplete('Need to revisit once models work again.');
  5591. $encoding = strtolower(Configure::read('App.encoding'));
  5592. TableRegistry::get('Contacts', [
  5593. 'className' => __NAMESPACE__ . '\ContactsTable'
  5594. ]);
  5595. $result = $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  5596. $expected = array(
  5597. 'form' => array(
  5598. 'method' => 'post', 'action' => '/articles/add',
  5599. 'accept-charset' => $encoding
  5600. ),
  5601. 'div' => array('style' => 'display:none;'),
  5602. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5603. '/div'
  5604. );
  5605. $this->assertTags($result, $expected);
  5606. $result = $this->Form->input('name');
  5607. $expected = array(
  5608. 'div' => array('class' => 'input text'),
  5609. 'label' => array('for' => 'name'),
  5610. 'Name',
  5611. '/label',
  5612. 'input' => array(
  5613. 'type' => 'text', 'name' => 'name',
  5614. 'id' => 'name', 'maxlength' => '255'
  5615. ),
  5616. '/div'
  5617. );
  5618. $this->assertTags($result, $expected);
  5619. $result = $this->Form->input('non_existing_field_in_contact_model');
  5620. $expected = array(
  5621. 'div' => array('class' => 'input text'),
  5622. 'label' => array('for' => 'non-existing-field-in-contact-model'),
  5623. 'Non Existing Field In Contact Model',
  5624. '/label',
  5625. 'input' => array(
  5626. 'type' => 'text', 'name' => 'non_existing_field_in_contact_model',
  5627. 'id' => 'non-existing-field-in-contact-model'
  5628. ),
  5629. '/div'
  5630. );
  5631. $this->assertTags($result, $expected);
  5632. extract($this->dateRegex);
  5633. $now = strtotime('now');
  5634. $result = $this->Form->input('Contact.published', array(
  5635. 'templates' => ['groupContainer' => '{{label}}{{input}}']
  5636. ));
  5637. $expected = array(
  5638. 'label' => array('for' => 'ContactPublishedMonth'),
  5639. 'Published',
  5640. '/label',
  5641. array('select' => array(
  5642. 'name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth'
  5643. )),
  5644. $monthsRegex,
  5645. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5646. date('F', $now),
  5647. '/option',
  5648. '*/select',
  5649. '-',
  5650. array('select' => array(
  5651. 'name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay'
  5652. )),
  5653. $daysRegex,
  5654. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5655. date('j', $now),
  5656. '/option',
  5657. '*/select',
  5658. '-',
  5659. array('select' => array(
  5660. 'name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear'
  5661. )),
  5662. $yearsRegex,
  5663. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5664. date('Y', $now),
  5665. '*/select'
  5666. );
  5667. $this->assertTags($result, $expected);
  5668. $result = $this->Form->input('Contact.updated', array('div' => false));
  5669. $expected = array(
  5670. 'label' => array('for' => 'ContactUpdatedMonth'),
  5671. 'Updated',
  5672. '/label',
  5673. array('select' => array(
  5674. 'name' => 'Contact[updated][month]', 'id' => 'ContactUpdatedMonth'
  5675. )),
  5676. $monthsRegex,
  5677. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5678. date('F', $now),
  5679. '/option',
  5680. '*/select',
  5681. '-',
  5682. array('select' => array(
  5683. 'name' => 'Contact[updated][day]', 'id' => 'ContactUpdatedDay'
  5684. )),
  5685. $daysRegex,
  5686. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5687. date('j', $now),
  5688. '/option',
  5689. '*/select',
  5690. '-',
  5691. array('select' => array(
  5692. 'name' => 'Contact[updated][year]', 'id' => 'ContactUpdatedYear'
  5693. )),
  5694. $yearsRegex,
  5695. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5696. date('Y', $now),
  5697. '*/select'
  5698. );
  5699. $this->assertTags($result, $expected);
  5700. $result = $this->Form->input('UserForm.stuff');
  5701. $expected = array(
  5702. 'div' => array('class' => 'input text'),
  5703. 'label' => array('for' => 'UserFormStuff'),
  5704. 'Stuff',
  5705. '/label',
  5706. 'input' => array(
  5707. 'type' => 'text', 'name' => 'UserForm[stuff]',
  5708. 'id' => 'UserFormStuff', 'maxlength' => 10
  5709. ),
  5710. '/div'
  5711. );
  5712. $this->assertTags($result, $expected);
  5713. }
  5714. /**
  5715. * testForMagicInputNonExistingNorValidated method
  5716. *
  5717. * @return void
  5718. */
  5719. public function testForMagicInputNonExistingNorValidated() {
  5720. $this->markTestIncomplete('Need to revisit once models work again.');
  5721. $encoding = strtolower(Configure::read('App.encoding'));
  5722. $result = $this->Form->create('Contact');
  5723. $expected = array(
  5724. 'form' => array(
  5725. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5726. 'accept-charset' => $encoding
  5727. ),
  5728. 'div' => array('style' => 'display:none;'),
  5729. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5730. '/div'
  5731. );
  5732. $this->assertTags($result, $expected);
  5733. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  5734. $expected = array(
  5735. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  5736. 'Non Existing Nor Validated',
  5737. '/label',
  5738. 'input' => array(
  5739. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  5740. 'id' => 'ContactNonExistingNorValidated'
  5741. )
  5742. );
  5743. $this->assertTags($result, $expected);
  5744. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  5745. 'div' => false, 'value' => 'my value'
  5746. ));
  5747. $expected = array(
  5748. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  5749. 'Non Existing Nor Validated',
  5750. '/label',
  5751. 'input' => array(
  5752. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  5753. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  5754. )
  5755. );
  5756. $this->assertTags($result, $expected);
  5757. $this->Form->request->data = array(
  5758. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  5759. ));
  5760. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  5761. $expected = array(
  5762. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  5763. 'Non Existing Nor Validated',
  5764. '/label',
  5765. 'input' => array(
  5766. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  5767. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  5768. )
  5769. );
  5770. $this->assertTags($result, $expected);
  5771. }
  5772. /**
  5773. * testFormMagicInputLabel method
  5774. *
  5775. * @return void
  5776. */
  5777. public function testFormMagicInputLabel() {
  5778. $this->markTestIncomplete('Need to revisit once models work again.');
  5779. TableRegistry::get('Contacts', [
  5780. 'className' => __NAMESPACE__ . '\ContactsTable'
  5781. ]);
  5782. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  5783. $this->Form->templates(['groupContainer' => '{{content}}']);
  5784. $result = $this->Form->input('Contacts.name', array('label' => 'My label'));
  5785. $expected = array(
  5786. 'label' => array('for' => 'contacts-name'),
  5787. 'My label',
  5788. '/label',
  5789. 'input' => array(
  5790. 'type' => 'text', 'name' => 'Contacts[name]',
  5791. 'id' => 'contacts-name', 'maxlength' => '255'
  5792. )
  5793. );
  5794. $this->assertTags($result, $expected);
  5795. $result = $this->Form->input('name', array(
  5796. 'label' => array('class' => 'mandatory')
  5797. ));
  5798. $expected = array(
  5799. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  5800. 'Name',
  5801. '/label',
  5802. 'input' => array(
  5803. 'type' => 'text', 'name' => 'name',
  5804. 'id' => 'name', 'maxlength' => '255'
  5805. )
  5806. );
  5807. $this->assertTags($result, $expected);
  5808. $result = $this->Form->input('name', array(
  5809. 'div' => false,
  5810. 'label' => array('class' => 'mandatory', 'text' => 'My label')
  5811. ));
  5812. $expected = array(
  5813. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  5814. 'My label',
  5815. '/label',
  5816. 'input' => array(
  5817. 'type' => 'text', 'name' => 'name',
  5818. 'id' => 'name', 'maxlength' => '255'
  5819. )
  5820. );
  5821. $this->assertTags($result, $expected);
  5822. $result = $this->Form->input('Contact.name', array(
  5823. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  5824. ));
  5825. $expected = array(
  5826. 'label' => array('for' => 'my_id'),
  5827. 'Name',
  5828. '/label',
  5829. 'input' => array(
  5830. 'type' => 'text', 'name' => 'Contact[name]',
  5831. 'id' => 'my_id', 'maxlength' => '255'
  5832. )
  5833. );
  5834. $this->assertTags($result, $expected);
  5835. $result = $this->Form->input('1.id');
  5836. $this->assertTags($result, array('input' => array(
  5837. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  5838. 'id' => 'Contact1Id'
  5839. )));
  5840. $result = $this->Form->input("1.name");
  5841. $expected = array(
  5842. 'div' => array('class' => 'input text'),
  5843. 'label' => array('for' => 'Contact1Name'),
  5844. 'Name',
  5845. '/label',
  5846. 'input' => array(
  5847. 'type' => 'text', 'name' => 'Contact[1][name]',
  5848. 'id' => 'Contact1Name', 'maxlength' => '255'
  5849. ),
  5850. '/div'
  5851. );
  5852. $this->assertTags($result, $expected);
  5853. $result = $this->Form->input('Contact.1.id');
  5854. $this->assertTags($result, array(
  5855. 'input' => array(
  5856. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  5857. 'id' => 'Contact1Id'
  5858. )
  5859. ));
  5860. $result = $this->Form->input("Model.1.name");
  5861. $expected = array(
  5862. 'div' => array('class' => 'input text'),
  5863. 'label' => array('for' => 'Model1Name'),
  5864. 'Name',
  5865. '/label',
  5866. 'input' => array(
  5867. 'type' => 'text', 'name' => 'Model[1][name]',
  5868. 'id' => 'Model1Name'
  5869. ),
  5870. '/div'
  5871. );
  5872. $this->assertTags($result, $expected);
  5873. }
  5874. /**
  5875. * testFormEnd method
  5876. *
  5877. * @return void
  5878. */
  5879. public function testFormEnd() {
  5880. $this->assertEquals('</form>', $this->Form->end());
  5881. }
  5882. /**
  5883. * testDbLessModel method
  5884. *
  5885. * @return void
  5886. */
  5887. public function testDbLessModel() {
  5888. $this->markTestIncomplete('Need to revisit once models work again.');
  5889. $this->Form->create('TestMail');
  5890. $result = $this->Form->input('name');
  5891. $expected = array(
  5892. 'div' => array('class' => 'input text'),
  5893. 'label' => array('for' => 'TestMailName'),
  5894. 'Name',
  5895. '/label',
  5896. 'input' => array(
  5897. 'name' => 'TestMail[name]', 'type' => 'text',
  5898. 'id' => 'TestMailName'
  5899. ),
  5900. '/div'
  5901. );
  5902. $this->assertTags($result, $expected);
  5903. $this->Form->create('TestMail');
  5904. $result = $this->Form->input('name');
  5905. $expected = array(
  5906. 'div' => array('class' => 'input text'),
  5907. 'label' => array('for' => 'TestMailName'),
  5908. 'Name',
  5909. '/label',
  5910. 'input' => array(
  5911. 'name' => 'TestMail[name]', 'type' => 'text',
  5912. 'id' => 'TestMailName'
  5913. ),
  5914. '/div'
  5915. );
  5916. $this->assertTags($result, $expected);
  5917. }
  5918. /**
  5919. * Test the generation of fields for a multi record form.
  5920. *
  5921. * @return void
  5922. */
  5923. public function testMultiRecordForm() {
  5924. $this->markTestIncomplete('Need to revisit once models work again.');
  5925. $this->Form->create('ValidateProfile');
  5926. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  5927. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  5928. $expected = array(
  5929. 'div' => array('class' => 'input textarea'),
  5930. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  5931. 'Name',
  5932. '/label',
  5933. 'textarea' => array(
  5934. 'id' => 'ValidateProfile1ValidateItem2Name',
  5935. 'name' => 'ValidateProfile[1][ValidateItem][2][name]',
  5936. 'cols' => 30,
  5937. 'rows' => 6
  5938. ),
  5939. 'Value',
  5940. '/textarea',
  5941. '/div'
  5942. );
  5943. $this->assertTags($result, $expected);
  5944. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  5945. $expected = array(
  5946. 'div' => array('class' => 'input date'),
  5947. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  5948. 'Created',
  5949. '/label',
  5950. array('select' => array(
  5951. 'name' => 'ValidateProfile[1][ValidateItem][2][created][month]',
  5952. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  5953. )
  5954. ),
  5955. array('option' => array('value' => '')), '/option',
  5956. $this->dateRegex['monthsRegex'],
  5957. '/select', '-',
  5958. array('select' => array(
  5959. 'name' => 'ValidateProfile[1][ValidateItem][2][created][day]',
  5960. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  5961. )
  5962. ),
  5963. array('option' => array('value' => '')), '/option',
  5964. $this->dateRegex['daysRegex'],
  5965. '/select', '-',
  5966. array('select' => array(
  5967. 'name' => 'ValidateProfile[1][ValidateItem][2][created][year]',
  5968. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  5969. )
  5970. ),
  5971. array('option' => array('value' => '')), '/option',
  5972. $this->dateRegex['yearsRegex'],
  5973. '/select',
  5974. '/div'
  5975. );
  5976. $this->assertTags($result, $expected);
  5977. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  5978. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  5979. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  5980. $expected = array(
  5981. 'div' => array('class' => 'input select error'),
  5982. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  5983. 'Profile',
  5984. '/label',
  5985. 'select' => array(
  5986. 'name' => 'ValidateProfile[1][ValidateItem][2][profile_id]',
  5987. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  5988. 'class' => 'form-error'
  5989. ),
  5990. '/select',
  5991. array('div' => array('class' => 'error-message')),
  5992. 'Error',
  5993. '/div',
  5994. '/div'
  5995. );
  5996. $this->assertTags($result, $expected);
  5997. }
  5998. /**
  5999. * test the correct display of multi-record form validation errors.
  6000. *
  6001. * @return void
  6002. */
  6003. public function testMultiRecordFormValidationErrors() {
  6004. $this->markTestIncomplete('Need to revisit once models work again.');
  6005. $this->Form->create('ValidateProfile');
  6006. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  6007. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  6008. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  6009. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  6010. $result = $this->Form->error('ValidateProfile.2.city');
  6011. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6012. $result = $this->Form->error('2.city');
  6013. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6014. }
  6015. /**
  6016. * test the correct display of multi-record form validation errors.
  6017. *
  6018. * @return void
  6019. */
  6020. public function testSaveManyRecordFormValidationErrors() {
  6021. $this->markTestIncomplete('Need to revisit once models work again.');
  6022. $this->Form->create('ValidateUser');
  6023. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  6024. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  6025. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  6026. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  6027. $result = $this->Form->error('ValidateUser.0.city');
  6028. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6029. }
  6030. /**
  6031. * test that some html5 inputs + FormHelper::__call() work
  6032. *
  6033. * @return void
  6034. */
  6035. public function testHtml5Inputs() {
  6036. $result = $this->Form->email('User.email');
  6037. $expected = array(
  6038. 'input' => array('type' => 'email', 'name' => 'User[email]')
  6039. );
  6040. $this->assertTags($result, $expected);
  6041. $result = $this->Form->search('User.query');
  6042. $expected = array(
  6043. 'input' => array('type' => 'search', 'name' => 'User[query]')
  6044. );
  6045. $this->assertTags($result, $expected);
  6046. $result = $this->Form->search('User.query', array('value' => 'test'));
  6047. $expected = array(
  6048. 'input' => array('type' => 'search', 'name' => 'User[query]', 'value' => 'test')
  6049. );
  6050. $this->assertTags($result, $expected);
  6051. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  6052. $expected = array(
  6053. 'input' => array('type' => 'text', 'name' => 'User[query]', 'value' => 'test')
  6054. );
  6055. $this->assertTags($result, $expected);
  6056. }
  6057. /**
  6058. * Test accessing html5 inputs through input().
  6059. *
  6060. * @return void
  6061. */
  6062. public function testHtml5InputWithInput() {
  6063. $this->Form->create();
  6064. $this->Form->templates(['groupContainer' => '{{content}}']);
  6065. $result = $this->Form->input('website', array(
  6066. 'type' => 'url',
  6067. 'val' => 'http://domain.tld',
  6068. 'label' => false
  6069. ));
  6070. $expected = array(
  6071. 'input' => array('type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld')
  6072. );
  6073. $this->assertTags($result, $expected);
  6074. }
  6075. /**
  6076. * Test errors when field name is missing.
  6077. *
  6078. * @expectedException \Cake\Error\Exception
  6079. * @return void
  6080. */
  6081. public function testHtml5InputException() {
  6082. $this->Form->email();
  6083. }
  6084. /**
  6085. * Tests that formhelper sets required attributes.
  6086. *
  6087. * @return void
  6088. */
  6089. public function testRequiredAttribute() {
  6090. $this->article['required'] = [
  6091. 'title' => true,
  6092. 'body' => false,
  6093. ];
  6094. $this->Form->create($this->article);
  6095. $result = $this->Form->input('title');
  6096. $expected = array(
  6097. 'div' => array('class' => 'input text required'),
  6098. 'label' => array('for' => 'title'),
  6099. 'Title',
  6100. '/label',
  6101. 'input' => array(
  6102. 'type' => 'text',
  6103. 'name' => 'title',
  6104. 'id' => 'title',
  6105. 'required' => 'required',
  6106. ),
  6107. '/div'
  6108. );
  6109. $this->assertTags($result, $expected);
  6110. $result = $this->Form->input('title', ['required' => false]);
  6111. $this->assertNotContains('required', $result);
  6112. $result = $this->Form->input('body');
  6113. $expected = array(
  6114. 'div' => array('class' => 'input text'),
  6115. 'label' => array('for' => 'body'),
  6116. 'Body',
  6117. '/label',
  6118. 'input' => array(
  6119. 'type' => 'text',
  6120. 'name' => 'body',
  6121. 'id' => 'body',
  6122. ),
  6123. '/div'
  6124. );
  6125. $this->assertTags($result, $expected);
  6126. $result = $this->Form->input('body', ['required' => true]);
  6127. $this->assertContains('required', $result);
  6128. }
  6129. }