FormHelperTest.php 211 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216
  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->markTestIncomplete('Need to revisit once models work again.');
  1065. $this->article['schema'] = [
  1066. 'foo' => [
  1067. 'type' => 'float',
  1068. 'null' => false,
  1069. 'default' => null,
  1070. 'length' => 10
  1071. ]
  1072. ];
  1073. $this->Form->create('Contact');
  1074. $result = $this->Form->input('foo');
  1075. $expected = array(
  1076. 'div' => array('class' => 'input number'),
  1077. 'label' => array('for' => 'ContactFoo'),
  1078. 'Foo',
  1079. '/label',
  1080. array('input' => array(
  1081. 'type' => 'number',
  1082. 'name' => 'Contact[foo]',
  1083. 'id' => 'ContactFoo',
  1084. 'step' => 'any'
  1085. )),
  1086. '/div'
  1087. );
  1088. $this->assertTags($result, $expected);
  1089. $result = $this->Form->input('foo', array('step' => 0.5));
  1090. $expected = array(
  1091. 'div' => array('class' => 'input number'),
  1092. 'label' => array('for' => 'ContactFoo'),
  1093. 'Foo',
  1094. '/label',
  1095. array('input' => array(
  1096. 'type' => 'number',
  1097. 'name' => 'Contact[foo]',
  1098. 'id' => 'ContactFoo',
  1099. 'step' => '0.5'
  1100. )),
  1101. '/div'
  1102. );
  1103. $this->assertTags($result, $expected);
  1104. }
  1105. /**
  1106. * Tests correct generation of number fields for integer fields
  1107. *
  1108. * @return void
  1109. */
  1110. public function testTextFieldTypeNumberGenerationForIntegers() {
  1111. TableRegistry::get('Contacts', [
  1112. 'className' => __NAMESPACE__ . '\ContactsTable'
  1113. ]);
  1114. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1115. $result = $this->Form->input('age');
  1116. $expected = array(
  1117. 'div' => array('class' => 'input number'),
  1118. 'label' => array('for' => 'age'),
  1119. 'Age',
  1120. '/label',
  1121. array('input' => array(
  1122. 'type' => 'number', 'name' => 'age',
  1123. 'id' => 'age'
  1124. )),
  1125. '/div'
  1126. );
  1127. $this->assertTags($result, $expected);
  1128. }
  1129. /**
  1130. * Tests correct generation of file upload fields for binary fields
  1131. *
  1132. * @return void
  1133. */
  1134. public function testFileUploadFieldTypeGenerationForBinaries() {
  1135. $table = TableRegistry::get('Contacts', [
  1136. 'className' => __NAMESPACE__ . '\ContactsTable'
  1137. ]);
  1138. $table->schema(array('foo' => array(
  1139. 'type' => 'binary',
  1140. 'null' => false,
  1141. 'default' => null,
  1142. 'length' => 1024
  1143. )));
  1144. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1145. $result = $this->Form->input('foo');
  1146. $expected = array(
  1147. 'div' => array('class' => 'input file'),
  1148. 'label' => array('for' => 'foo'),
  1149. 'Foo',
  1150. '/label',
  1151. array('input' => array(
  1152. 'type' => 'file', 'name' => 'foo',
  1153. 'id' => 'foo'
  1154. )),
  1155. '/div'
  1156. );
  1157. $this->assertTags($result, $expected);
  1158. }
  1159. /**
  1160. * testFormSecurityMultipleFields method
  1161. *
  1162. * Test secure() with multiple row form. Ensure hash is correct.
  1163. *
  1164. * @return void
  1165. */
  1166. public function testFormSecurityMultipleFields() {
  1167. $this->Form->request->params['_Token'] = 'foo';
  1168. $fields = array(
  1169. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1170. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1171. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1172. );
  1173. $result = $this->Form->secure($fields);
  1174. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1175. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1176. $expected = array(
  1177. 'div' => array('style' => 'display:none;'),
  1178. array('input' => array(
  1179. 'type' => 'hidden', 'name' => '_Token[fields]',
  1180. 'value' => $hash
  1181. )),
  1182. array('input' => array(
  1183. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1184. 'value' => ''
  1185. )),
  1186. '/div'
  1187. );
  1188. $this->assertTags($result, $expected);
  1189. }
  1190. /**
  1191. * testFormSecurityMultipleSubmitButtons
  1192. *
  1193. * test form submit generation and ensure that _Token is only created on end()
  1194. *
  1195. * @return void
  1196. */
  1197. public function testFormSecurityMultipleSubmitButtons() {
  1198. $this->Form->request->params['_Token'] = 'testKey';
  1199. $this->Form->create($this->article);
  1200. $this->Form->text('Address.title');
  1201. $this->Form->text('Address.first_name');
  1202. $result = $this->Form->submit('Save', array('name' => 'save'));
  1203. $expected = array(
  1204. 'div' => array('class' => 'submit'),
  1205. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  1206. '/div',
  1207. );
  1208. $this->assertTags($result, $expected);
  1209. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  1210. $expected = array(
  1211. 'div' => array('class' => 'submit'),
  1212. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  1213. '/div',
  1214. );
  1215. $this->assertTags($result, $expected);
  1216. $result = $this->Form->end();
  1217. $expected = array(
  1218. 'div' => array('style' => 'display:none;'),
  1219. array('input' => array(
  1220. 'type' => 'hidden',
  1221. 'name' => '_Token[fields]',
  1222. 'value'
  1223. )),
  1224. array('input' => array(
  1225. 'type' => 'hidden',
  1226. 'name' => '_Token[unlocked]',
  1227. 'value' => 'cancel%7Csave'
  1228. )),
  1229. '/div'
  1230. );
  1231. $this->assertTags($result, $expected);
  1232. }
  1233. /**
  1234. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1235. *
  1236. * @return void
  1237. */
  1238. public function testSecurityButtonNestedNamed() {
  1239. $key = 'testKey';
  1240. $this->Form->request->params['_csrfToken'] = $key;
  1241. $this->Form->create('Addresses');
  1242. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1243. $result = $this->Form->unlockField();
  1244. $this->assertEquals(array('Address.button'), $result);
  1245. }
  1246. /**
  1247. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1248. *
  1249. * @return void
  1250. */
  1251. public function testSecuritySubmitNestedNamed() {
  1252. $this->Form->request->params['_Token'] = 'testKey';
  1253. $this->Form->create($this->article);
  1254. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1255. $result = $this->Form->unlockField();
  1256. $this->assertEquals(array('Address.button'), $result);
  1257. }
  1258. /**
  1259. * Test that the correct fields are unlocked for image submits with no names.
  1260. *
  1261. * @return void
  1262. */
  1263. public function testSecuritySubmitImageNoName() {
  1264. $key = 'testKey';
  1265. $this->Form->request->params['_Token'] = 'testKey';
  1266. $this->Form->create(false);
  1267. $result = $this->Form->submit('save.png');
  1268. $expected = array(
  1269. 'div' => array('class' => 'submit'),
  1270. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  1271. '/div'
  1272. );
  1273. $this->assertTags($result, $expected);
  1274. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  1275. }
  1276. /**
  1277. * Test that the correct fields are unlocked for image submits with names.
  1278. *
  1279. * @return void
  1280. */
  1281. public function testSecuritySubmitImageName() {
  1282. $this->Form->request->params['_Token'] = 'testKey';
  1283. $this->Form->create(null);
  1284. $result = $this->Form->submit('save.png', array('name' => 'test'));
  1285. $expected = array(
  1286. 'div' => array('class' => 'submit'),
  1287. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  1288. '/div'
  1289. );
  1290. $this->assertTags($result, $expected);
  1291. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  1292. }
  1293. /**
  1294. * testFormSecurityMultipleInputFields method
  1295. *
  1296. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1297. *
  1298. * @return void
  1299. */
  1300. public function testFormSecurityMultipleInputFields() {
  1301. $this->markTestIncomplete('Need to revisit once models work again.');
  1302. $key = 'testKey';
  1303. $this->Form->request->params['_csrfToken'] = $key;
  1304. $this->Form->create('Addresses');
  1305. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1306. $this->Form->input('Addresses.0.title');
  1307. $this->Form->input('Addresses.0.first_name');
  1308. $this->Form->input('Addresses.0.last_name');
  1309. $this->Form->input('Addresses.0.address');
  1310. $this->Form->input('Addresses.0.city');
  1311. $this->Form->input('Addresses.0.phone');
  1312. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  1313. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1314. $this->Form->input('Addresses.1.title');
  1315. $this->Form->input('Addresses.1.first_name');
  1316. $this->Form->input('Addresses.1.last_name');
  1317. $this->Form->input('Addresses.1.address');
  1318. $this->Form->input('Addresses.1.city');
  1319. $this->Form->input('Addresses.1.phone');
  1320. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  1321. $result = $this->Form->secure($this->Form->fields);
  1322. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  1323. $expected = array(
  1324. 'div' => array('style' => 'display:none;'),
  1325. array('input' => array(
  1326. 'type' => 'hidden', 'name' => '_Token[fields]',
  1327. 'value' => $hash
  1328. )),
  1329. array('input' => array(
  1330. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1331. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1332. )),
  1333. '/div'
  1334. );
  1335. $this->assertTags($result, $expected);
  1336. }
  1337. /**
  1338. * Test form security with Model.field.0 style inputs
  1339. *
  1340. * @return void
  1341. */
  1342. public function testFormSecurityArrayFields() {
  1343. $this->Form->request->params['_Token'] = 'testKey';
  1344. $this->Form->create();
  1345. $this->Form->text('Address.primary.1');
  1346. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1347. $this->Form->text('Address.secondary.1.0');
  1348. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1349. }
  1350. /**
  1351. * testFormSecurityMultipleInputDisabledFields method
  1352. *
  1353. * test secure form generation with multiple records and disabled fields.
  1354. *
  1355. * @return void
  1356. */
  1357. public function testFormSecurityMultipleInputDisabledFields() {
  1358. $this->Form->request->params['_Token'] = array(
  1359. 'unlockedFields' => array('first_name', 'address')
  1360. );
  1361. $this->Form->create();
  1362. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1363. $this->Form->text('Addresses.0.title');
  1364. $this->Form->text('Addresses.0.first_name');
  1365. $this->Form->text('Addresses.0.last_name');
  1366. $this->Form->text('Addresses.0.address');
  1367. $this->Form->text('Addresses.0.city');
  1368. $this->Form->text('Addresses.0.phone');
  1369. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1370. $this->Form->text('Addresses.1.title');
  1371. $this->Form->text('Addresses.1.first_name');
  1372. $this->Form->text('Addresses.1.last_name');
  1373. $this->Form->text('Addresses.1.address');
  1374. $this->Form->text('Addresses.1.city');
  1375. $this->Form->text('Addresses.1.phone');
  1376. $result = $this->Form->secure($this->Form->fields);
  1377. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1378. $expected = array(
  1379. 'div' => array('style' => 'display:none;'),
  1380. array('input' => array(
  1381. 'type' => 'hidden',
  1382. 'name' => '_Token[fields]',
  1383. 'value' => $hash
  1384. )),
  1385. array('input' => array(
  1386. 'type' => 'hidden',
  1387. 'name' => '_Token[unlocked]',
  1388. 'value' => 'address%7Cfirst_name',
  1389. )),
  1390. '/div'
  1391. );
  1392. $this->assertTags($result, $expected);
  1393. }
  1394. /**
  1395. * testFormSecurityInputDisabledFields method
  1396. *
  1397. * Test single record form with disabled fields.
  1398. *
  1399. * @return void
  1400. */
  1401. public function testFormSecurityInputUnlockedFields() {
  1402. $this->Form->request['_Token'] = array(
  1403. 'unlockedFields' => array('first_name', 'address')
  1404. );
  1405. $this->Form->create();
  1406. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1407. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1408. $this->Form->text('Addresses.title');
  1409. $this->Form->text('Addresses.first_name');
  1410. $this->Form->text('Addresses.last_name');
  1411. $this->Form->text('Addresses.address');
  1412. $this->Form->text('Addresses.city');
  1413. $this->Form->text('Addresses.phone');
  1414. $result = $this->Form->fields;
  1415. $expected = array(
  1416. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1417. 'Addresses.city', 'Addresses.phone'
  1418. );
  1419. $this->assertEquals($expected, $result);
  1420. $result = $this->Form->secure($expected);
  1421. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1422. $expected = array(
  1423. 'div' => array('style' => 'display:none;'),
  1424. array('input' => array(
  1425. 'type' => 'hidden',
  1426. 'name' => '_Token[fields]',
  1427. 'value' => $hash
  1428. )),
  1429. array('input' => array(
  1430. 'type' => 'hidden',
  1431. 'name' => '_Token[unlocked]',
  1432. 'value' => 'address%7Cfirst_name',
  1433. )),
  1434. '/div'
  1435. );
  1436. $this->assertTags($result, $expected);
  1437. }
  1438. /**
  1439. * test securing inputs with custom name attributes.
  1440. *
  1441. * @return void
  1442. */
  1443. public function testFormSecureWithCustomNameAttribute() {
  1444. $this->Form->request->params['_Token'] = 'testKey';
  1445. $this->Form->text('UserForm.published', array('name' => 'User[custom]'));
  1446. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1447. $this->Form->text('UserForm.published', array('name' => 'User[custom][another][value]'));
  1448. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1449. }
  1450. /**
  1451. * testFormSecuredInput method
  1452. *
  1453. * Test generation of entire secure form, assertions made on input() output.
  1454. *
  1455. * @return void
  1456. */
  1457. public function testFormSecuredInput() {
  1458. $this->markTestIncomplete('Need to revisit once models work again.');
  1459. $this->Form->request->params['_csrfToken'] = 'testKey';
  1460. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1461. $encoding = strtolower(Configure::read('App.encoding'));
  1462. $expected = array(
  1463. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1464. 'div' => array('style' => 'display:none;'),
  1465. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1466. array('input' => array(
  1467. 'type' => 'hidden', 'name' => '_csrfToken',
  1468. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1469. )),
  1470. '/div'
  1471. );
  1472. $this->assertTags($result, $expected);
  1473. $result = $this->Form->input('ValidateUser.ratio');
  1474. $expected = array(
  1475. 'div' => array('class'),
  1476. 'label' => array('for'),
  1477. 'Ratio',
  1478. '/label',
  1479. 'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
  1480. '/div',
  1481. );
  1482. $this->assertTags($result, $expected);
  1483. $result = $this->Form->input('ValidateUser.population');
  1484. $expected = array(
  1485. 'div' => array('class'),
  1486. 'label' => array('for'),
  1487. 'Population',
  1488. '/label',
  1489. 'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
  1490. '/div',
  1491. );
  1492. $this->assertTags($result, $expected);
  1493. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1494. $expected = array(
  1495. 'div' => array('class' => 'input text'),
  1496. 'label' => array('for' => 'UserFormPublished'),
  1497. 'Published',
  1498. '/label',
  1499. array('input' => array(
  1500. 'type' => 'text', 'name' => 'UserForm[published]',
  1501. 'id' => 'UserFormPublished'
  1502. )),
  1503. '/div'
  1504. );
  1505. $this->assertTags($result, $expected);
  1506. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1507. $expected = array(
  1508. 'div' => array('class' => 'input text'),
  1509. 'label' => array('for' => 'UserFormOther'),
  1510. 'Other',
  1511. '/label',
  1512. array('input' => array(
  1513. 'type' => 'text', 'name' => 'UserForm[other]',
  1514. 'id' => 'UserFormOther'
  1515. )),
  1516. '/div'
  1517. );
  1518. $this->assertTags($result, $expected);
  1519. $result = $this->Form->hidden('UserForm.stuff');
  1520. $expected = array(
  1521. 'input' => array(
  1522. 'type' => 'hidden', 'name' => 'UserForm[stuff]',
  1523. 'id' => 'UserFormStuff'
  1524. ));
  1525. $this->assertTags($result, $expected);
  1526. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1527. $expected = array('input' => array(
  1528. 'type' => 'hidden', 'name' => 'UserForm[hidden]',
  1529. 'value' => '0', 'id' => 'UserFormHidden'
  1530. ));
  1531. $this->assertTags($result, $expected);
  1532. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1533. $expected = array(
  1534. 'div' => array('class' => 'input checkbox'),
  1535. array('input' => array(
  1536. 'type' => 'hidden', 'name' => 'UserForm[something]',
  1537. 'value' => '0', 'id' => 'UserFormSomething_'
  1538. )),
  1539. array('input' => array(
  1540. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  1541. 'value' => '1', 'id' => 'UserFormSomething'
  1542. )),
  1543. 'label' => array('for' => 'UserFormSomething'),
  1544. 'Something',
  1545. '/label',
  1546. '/div'
  1547. );
  1548. $this->assertTags($result, $expected);
  1549. $result = $this->Form->fields;
  1550. $expected = array(
  1551. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1552. 'UserForm.hidden' => '0', 'UserForm.something'
  1553. );
  1554. $this->assertEquals($expected, $result);
  1555. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1556. $result = $this->Form->secure($this->Form->fields);
  1557. $expected = array(
  1558. 'div' => array('style' => 'display:none;'),
  1559. array('input' => array(
  1560. 'type' => 'hidden', 'name' => '_Token[fields]',
  1561. 'value' => $hash
  1562. )),
  1563. array('input' => array(
  1564. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1565. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1566. )),
  1567. '/div'
  1568. );
  1569. $this->assertTags($result, $expected);
  1570. }
  1571. /**
  1572. * Test secured inputs with custom names.
  1573. *
  1574. * @return void
  1575. */
  1576. public function testSecuredInputCustomName() {
  1577. $this->Form->request->params['_Token'] = 'testKey';
  1578. $this->assertEquals(array(), $this->Form->fields);
  1579. $this->Form->text('text_input', array(
  1580. 'name' => 'Option[General.default_role]',
  1581. ));
  1582. $expected = array('Option.General.default_role');
  1583. $this->assertEquals($expected, $this->Form->fields);
  1584. $this->Form->select('select_box', [1, 2], [
  1585. 'name' => 'Option[General.select_role]',
  1586. ]);
  1587. $expected = ['Option.General.default_role', 'Option.General.select_role'];
  1588. $this->assertEquals($expected, $this->Form->fields);
  1589. }
  1590. /**
  1591. * Tests that the correct keys are added to the field hash index
  1592. *
  1593. * @return void
  1594. */
  1595. public function testFormSecuredFileInput() {
  1596. $this->assertEquals(array(), $this->Form->fields);
  1597. $this->Form->file('Attachment.file');
  1598. $expected = array(
  1599. 'Attachment.file.name', 'Attachment.file.type',
  1600. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1601. 'Attachment.file.size'
  1602. );
  1603. $this->assertEquals($expected, $this->Form->fields);
  1604. }
  1605. /**
  1606. * test that multiple selects keys are added to field hash
  1607. *
  1608. * @return void
  1609. */
  1610. public function testFormSecuredMultipleSelect() {
  1611. $this->Form->request->params['_csrfToken'] = 'testKey';
  1612. $this->assertEquals(array(), $this->Form->fields);
  1613. $options = array('1' => 'one', '2' => 'two');
  1614. $this->Form->select('Model.select', $options);
  1615. $expected = array('Model.select');
  1616. $this->assertEquals($expected, $this->Form->fields);
  1617. $this->Form->fields = array();
  1618. $this->Form->select('Model.select', $options, array('multiple' => true));
  1619. $this->assertEquals($expected, $this->Form->fields);
  1620. }
  1621. /**
  1622. * testFormSecuredRadio method
  1623. *
  1624. * @return void
  1625. */
  1626. public function testFormSecuredRadio() {
  1627. $this->Form->request->params['_Token'] = 'testKey';
  1628. $this->assertEquals(array(), $this->Form->fields);
  1629. $options = array('1' => 'option1', '2' => 'option2');
  1630. $this->Form->radio('Test.test', $options);
  1631. $expected = array('Test.test');
  1632. $this->assertEquals($expected, $this->Form->fields);
  1633. }
  1634. /**
  1635. * Test that when disabled is in a list based attribute array it works.
  1636. *
  1637. * @return void
  1638. */
  1639. public function testFormSecuredAndDisabledNotAssoc() {
  1640. $this->Form->request->params['_Token'] = 'testKey';
  1641. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1642. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1643. $this->Form->text('Model.text', array('disabled'));
  1644. $this->Form->textarea('Model.textarea', array('disabled'));
  1645. $this->Form->password('Model.password', array('disabled'));
  1646. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1647. $expected = array(
  1648. 'Model.radio' => ''
  1649. );
  1650. $this->assertEquals($expected, $this->Form->fields);
  1651. }
  1652. /**
  1653. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1654. * hashing.
  1655. *
  1656. * @return void
  1657. */
  1658. public function testFormSecuredAndDisabled() {
  1659. $this->Form->request->params['_Token'] = 'testKey';
  1660. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1661. $this->Form->text('Model.text', array('disabled' => true));
  1662. $this->Form->password('Model.text', array('disabled' => true));
  1663. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1664. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1665. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1666. $this->Form->year('Model.year', array('disabled' => true));
  1667. $this->Form->month('Model.month', array('disabled' => true));
  1668. $this->Form->day('Model.day', array('disabled' => true));
  1669. $this->Form->hour('Model.hour', array('disabled' => true));
  1670. $this->Form->minute('Model.minute', array('disabled' => true));
  1671. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1672. $expected = array(
  1673. 'Model.radio' => ''
  1674. );
  1675. $this->assertEquals($expected, $this->Form->fields);
  1676. }
  1677. /**
  1678. * testDisableSecurityUsingForm method
  1679. *
  1680. * @return void
  1681. */
  1682. public function testDisableSecurityUsingForm() {
  1683. $this->Form->request['_Token'] = [
  1684. 'disabledFields' => []
  1685. ];
  1686. $this->Form->create();
  1687. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1688. $this->Form->text('Addresses.title');
  1689. $this->Form->text('Addresses.first_name', ['secure' => false]);
  1690. $this->Form->textarea('Addresses.city', ['secure' => false]);
  1691. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  1692. $result = $this->Form->fields;
  1693. $expected = [
  1694. 'Addresses.id' => '123456', 'Addresses.title',
  1695. ];
  1696. $this->assertEquals($expected, $result);
  1697. }
  1698. /**
  1699. * test disableField
  1700. *
  1701. * @return void
  1702. */
  1703. public function testUnlockFieldAddsToList() {
  1704. $this->Form->request['_Token'] = array(
  1705. 'unlockedFields' => array()
  1706. );
  1707. $this->Form->unlockField('Contact.name');
  1708. $this->Form->text('Contact.name');
  1709. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1710. $this->assertEquals(array(), $this->Form->fields);
  1711. }
  1712. /**
  1713. * test unlockField removing from fields array.
  1714. *
  1715. * @return void
  1716. */
  1717. public function testUnlockFieldRemovingFromFields() {
  1718. $this->Form->request['_Token'] = array(
  1719. 'unlockedFields' => array()
  1720. );
  1721. $this->Form->create($this->article);
  1722. $this->Form->hidden('Article.id', array('value' => 1));
  1723. $this->Form->text('Article.title');
  1724. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  1725. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  1726. $this->Form->unlockField('Article.title');
  1727. $this->Form->unlockField('Article.id');
  1728. $this->assertEquals(array(), $this->Form->fields);
  1729. }
  1730. /**
  1731. * Test validation errors.
  1732. *
  1733. * @return void
  1734. */
  1735. public function testPasswordValidation() {
  1736. $this->markTestIncomplete('Need to revisit once models work again.');
  1737. $Contact->validationErrors['password'] = array('Please provide a password');
  1738. $result = $this->Form->input('Contact.password');
  1739. $expected = array(
  1740. 'div' => array('class' => 'input password error'),
  1741. 'label' => array('for' => 'ContactPassword'),
  1742. 'Password',
  1743. '/label',
  1744. 'input' => array(
  1745. 'type' => 'password', 'name' => 'Contact[password]',
  1746. 'id' => 'ContactPassword', 'class' => 'form-error'
  1747. ),
  1748. array('div' => array('class' => 'error-message')),
  1749. 'Please provide a password',
  1750. '/div',
  1751. '/div'
  1752. );
  1753. $this->assertTags($result, $expected);
  1754. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1755. $expected = array(
  1756. 'div' => array('class' => 'input password error'),
  1757. 'label' => array('for' => 'ContactPassword'),
  1758. 'Password',
  1759. '/label',
  1760. 'input' => array(
  1761. 'type' => 'password', 'name' => 'Contact[password]',
  1762. 'id' => 'ContactPassword', 'class' => 'form-error'
  1763. ),
  1764. '/div'
  1765. );
  1766. $this->assertTags($result, $expected);
  1767. }
  1768. /**
  1769. * Test validation errors, when validation message is an empty string.
  1770. *
  1771. * @return void
  1772. */
  1773. public function testEmptyErrorValidation() {
  1774. $this->markTestIncomplete('Need to revisit once models work again.');
  1775. $this->Form->validationErrors['Contact']['password'] = '';
  1776. $result = $this->Form->input('Contact.password');
  1777. $expected = array(
  1778. 'div' => array('class' => 'input password error'),
  1779. 'label' => array('for' => 'ContactPassword'),
  1780. 'Password',
  1781. '/label',
  1782. 'input' => array(
  1783. 'type' => 'password', 'name' => 'Contact[password]',
  1784. 'id' => 'ContactPassword', 'class' => 'form-error'
  1785. ),
  1786. array('div' => array('class' => 'error-message')),
  1787. array(),
  1788. '/div',
  1789. '/div'
  1790. );
  1791. $this->assertTags($result, $expected);
  1792. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1793. $expected = array(
  1794. 'div' => array('class' => 'input password error'),
  1795. 'label' => array('for' => 'ContactPassword'),
  1796. 'Password',
  1797. '/label',
  1798. 'input' => array(
  1799. 'type' => 'password', 'name' => 'Contact[password]',
  1800. 'id' => 'ContactPassword', 'class' => 'form-error'
  1801. ),
  1802. '/div'
  1803. );
  1804. $this->assertTags($result, $expected);
  1805. }
  1806. /**
  1807. * Test validation errors, when calling input() overriding validation message by an empty string.
  1808. *
  1809. * @return void
  1810. */
  1811. public function testEmptyInputErrorValidation() {
  1812. $this->markTestIncomplete('Need to revisit once models work again.');
  1813. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1814. $result = $this->Form->input('Contact.password', array('error' => ''));
  1815. $expected = array(
  1816. 'div' => array('class' => 'input password error'),
  1817. 'label' => array('for' => 'ContactPassword'),
  1818. 'Password',
  1819. '/label',
  1820. 'input' => array(
  1821. 'type' => 'password', 'name' => 'Contact[password]',
  1822. 'id' => 'ContactPassword', 'class' => 'form-error'
  1823. ),
  1824. array('div' => array('class' => 'error-message')),
  1825. array(),
  1826. '/div',
  1827. '/div'
  1828. );
  1829. $this->assertTags($result, $expected);
  1830. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1831. $expected = array(
  1832. 'div' => array('class' => 'input password error'),
  1833. 'label' => array('for' => 'ContactPassword'),
  1834. 'Password',
  1835. '/label',
  1836. 'input' => array(
  1837. 'type' => 'password', 'name' => 'Contact[password]',
  1838. 'id' => 'ContactPassword', 'class' => 'form-error'
  1839. ),
  1840. '/div'
  1841. );
  1842. $this->assertTags($result, $expected);
  1843. }
  1844. /**
  1845. * testFormValidationAssociated method
  1846. *
  1847. * test display of form errors in conjunction with model::validates.
  1848. *
  1849. * @return void
  1850. */
  1851. public function testFormValidationAssociated() {
  1852. $this->markTestIncomplete('Need to revisit once models work again.');
  1853. $data = array(
  1854. 'UserForm' => array('name' => 'user'),
  1855. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1856. );
  1857. $result = $this->UserForm->OpenidUrl->create($data);
  1858. $this->assertFalse(empty($result));
  1859. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1860. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1861. $encoding = strtolower(Configure::read('App.encoding'));
  1862. $expected = array(
  1863. 'form' => array(
  1864. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1865. 'accept-charset' => $encoding
  1866. ),
  1867. 'div' => array('style' => 'display:none;'),
  1868. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1869. '/div'
  1870. );
  1871. $this->assertTags($result, $expected);
  1872. $result = $this->Form->error(
  1873. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1874. );
  1875. $this->assertEquals('Error, not registered', $result);
  1876. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1877. }
  1878. /**
  1879. * testFormValidationAssociatedFirstLevel method
  1880. *
  1881. * test form error display with associated model.
  1882. *
  1883. * @return void
  1884. */
  1885. public function testFormValidationAssociatedFirstLevel() {
  1886. $this->markTestIncomplete('Need to revisit once models work again.');
  1887. $data = array(
  1888. 'ValidateUser' => array('name' => 'mariano'),
  1889. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1890. );
  1891. $result = $this->ValidateUser->create($data);
  1892. $this->assertFalse(empty($result));
  1893. $this->assertFalse($this->ValidateUser->validates());
  1894. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1895. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1896. $encoding = strtolower(Configure::read('App.encoding'));
  1897. $expected = array(
  1898. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1899. 'div' => array('style' => 'display:none;'),
  1900. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1901. '/div'
  1902. );
  1903. $this->assertTags($result, $expected);
  1904. $result = $this->Form->error(
  1905. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1906. );
  1907. $this->assertEquals('Invalid email', $result);
  1908. $result = $this->Form->error(
  1909. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1910. );
  1911. $this->assertEquals('Invalid name', $result);
  1912. $result = $this->Form->error(
  1913. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1914. );
  1915. $this->assertEquals('Invalid city', $result);
  1916. unset($this->ValidateUser->ValidateProfile);
  1917. unset($this->ValidateUser);
  1918. }
  1919. /**
  1920. * testFormValidationAssociatedSecondLevel method
  1921. *
  1922. * test form error display with associated model.
  1923. *
  1924. * @return void
  1925. */
  1926. public function testFormValidationAssociatedSecondLevel() {
  1927. $this->markTestIncomplete('Need to revisit once models work again.');
  1928. $data = array(
  1929. 'ValidateUser' => array('name' => 'mariano'),
  1930. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1931. 'ValidateItem' => array('name' => 'Item')
  1932. );
  1933. $result = $this->ValidateUser->create($data);
  1934. $this->assertFalse(empty($result));
  1935. $this->assertFalse($this->ValidateUser->validates());
  1936. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1937. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1938. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1939. $encoding = strtolower(Configure::read('App.encoding'));
  1940. $expected = array(
  1941. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1942. 'div' => array('style' => 'display:none;'),
  1943. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1944. '/div'
  1945. );
  1946. $this->assertTags($result, $expected);
  1947. $result = $this->Form->error(
  1948. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1949. );
  1950. $this->assertEquals('Invalid email', $result);
  1951. $result = $this->Form->error(
  1952. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1953. );
  1954. $this->assertEquals('Invalid name', $result);
  1955. $result = $this->Form->error(
  1956. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1957. );
  1958. $result = $this->Form->error(
  1959. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1960. );
  1961. $this->assertEquals('Invalid description', $result);
  1962. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1963. unset($this->ValidateUser->ValidateProfile);
  1964. unset($this->ValidateUser);
  1965. }
  1966. /**
  1967. * testFormValidationMultiRecord method
  1968. *
  1969. * test form error display with multiple records.
  1970. *
  1971. * @return void
  1972. */
  1973. public function testFormValidationMultiRecord() {
  1974. $this->markTestIncomplete('Need to revisit once models work again.');
  1975. $Contact->validationErrors[2] = array(
  1976. 'name' => array('The provided value is invalid')
  1977. );
  1978. $result = $this->Form->input('Contact.2.name');
  1979. $expected = array(
  1980. 'div' => array('class' => 'input text error'),
  1981. 'label' => array('for' => 'Contact2Name'),
  1982. 'Name',
  1983. '/label',
  1984. 'input' => array(
  1985. 'type' => 'text', 'name' => 'Contact[2][name]', 'id' => 'Contact2Name',
  1986. 'class' => 'form-error', 'maxlength' => 255
  1987. ),
  1988. array('div' => array('class' => 'error-message')),
  1989. 'The provided value is invalid',
  1990. '/div',
  1991. '/div'
  1992. );
  1993. $this->assertTags($result, $expected);
  1994. }
  1995. /**
  1996. * testMultipleInputValidation method
  1997. *
  1998. * test multiple record form validation error display.
  1999. *
  2000. * @return void
  2001. */
  2002. public function testMultipleInputValidation() {
  2003. $this->markTestIncomplete('Need to revisit once models work again.');
  2004. $Address->validationErrors[0] = array(
  2005. 'title' => array('This field cannot be empty'),
  2006. 'first_name' => array('This field cannot be empty')
  2007. );
  2008. $Address->validationErrors[1] = array(
  2009. 'last_name' => array('You must have a last name')
  2010. );
  2011. $this->Form->create();
  2012. $result = $this->Form->input('Address.0.title');
  2013. $expected = array(
  2014. 'div' => array('class'),
  2015. 'label' => array('for'),
  2016. 'preg:/[^<]+/',
  2017. '/label',
  2018. 'input' => array(
  2019. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  2020. ),
  2021. array('div' => array('class' => 'error-message')),
  2022. 'This field cannot be empty',
  2023. '/div',
  2024. '/div'
  2025. );
  2026. $this->assertTags($result, $expected);
  2027. $result = $this->Form->input('Address.0.first_name');
  2028. $expected = array(
  2029. 'div' => array('class'),
  2030. 'label' => array('for'),
  2031. 'preg:/[^<]+/',
  2032. '/label',
  2033. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  2034. array('div' => array('class' => 'error-message')),
  2035. 'This field cannot be empty',
  2036. '/div',
  2037. '/div'
  2038. );
  2039. $this->assertTags($result, $expected);
  2040. $result = $this->Form->input('Address.0.last_name');
  2041. $expected = array(
  2042. 'div' => array('class'),
  2043. 'label' => array('for'),
  2044. 'preg:/[^<]+/',
  2045. '/label',
  2046. 'input' => array('type' => 'text', 'name', 'id'),
  2047. '/div'
  2048. );
  2049. $this->assertTags($result, $expected);
  2050. $result = $this->Form->input('Address.1.last_name');
  2051. $expected = array(
  2052. 'div' => array('class'),
  2053. 'label' => array('for'),
  2054. 'preg:/[^<]+/',
  2055. '/label',
  2056. 'input' => array(
  2057. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  2058. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  2059. ),
  2060. array('div' => array('class' => 'error-message')),
  2061. 'You must have a last name',
  2062. '/div',
  2063. '/div'
  2064. );
  2065. $this->assertTags($result, $expected);
  2066. }
  2067. /**
  2068. * testInput method
  2069. *
  2070. * Test various incarnations of input().
  2071. *
  2072. * @return void
  2073. */
  2074. public function testInput() {
  2075. TableRegistry::get('ValidateUsers', [
  2076. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2077. ]);
  2078. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2079. $result = $this->Form->input('ValidateUsers.balance');
  2080. $expected = array(
  2081. 'div' => array('class'),
  2082. 'label' => array('for'),
  2083. 'Balance',
  2084. '/label',
  2085. 'input' => array('name', 'type' => 'number', 'id', 'step'),
  2086. '/div',
  2087. );
  2088. $this->assertTags($result, $expected);
  2089. $result = $this->Form->input('ValidateUser.cost_decimal');
  2090. $expected = array(
  2091. 'div' => array('class'),
  2092. 'label' => array('for'),
  2093. 'Cost Decimal',
  2094. '/label',
  2095. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  2096. '/div',
  2097. );
  2098. $this->assertTags($result, $expected);
  2099. }
  2100. /**
  2101. * Tests the input method and passing custom options
  2102. *
  2103. * @return void
  2104. */
  2105. public function testInputCustomization() {
  2106. TableRegistry::get('Contacts', [
  2107. 'className' => __NAMESPACE__ . '\ContactsTable'
  2108. ]);
  2109. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2110. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  2111. $expected = array(
  2112. 'div' => array('class' => 'input email'),
  2113. 'label' => array('for' => 'custom'),
  2114. 'Email',
  2115. '/label',
  2116. array('input' => array(
  2117. 'type' => 'email', 'name' => 'Contact[email]',
  2118. 'id' => 'custom', 'maxlength' => 255
  2119. )),
  2120. '/div'
  2121. );
  2122. $this->assertTags($result, $expected);
  2123. $result = $this->Form->input('Contact.email', array(
  2124. 'templates' => ['groupContainer' => '<div>{{content}}</div>']
  2125. ));
  2126. $expected = array(
  2127. '<div',
  2128. 'label' => array('for' => 'contact-email'),
  2129. 'Email',
  2130. '/label',
  2131. array('input' => array(
  2132. 'type' => 'email', 'name' => 'Contact[email]',
  2133. 'id' => 'contact-email', 'maxlength' => 255
  2134. )),
  2135. '/div'
  2136. );
  2137. $this->assertTags($result, $expected);
  2138. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  2139. $expected = array(
  2140. 'div' => array('class' => 'input text'),
  2141. 'label' => array('for' => 'contact-email'),
  2142. 'Email',
  2143. '/label',
  2144. array('input' => array(
  2145. 'type' => 'text', 'name' => 'Contact[email]',
  2146. 'id' => 'contact-email', 'maxlength' => '255'
  2147. )),
  2148. '/div'
  2149. );
  2150. $this->assertTags($result, $expected);
  2151. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  2152. $expected = array(
  2153. 'div' => array('class' => 'input text'),
  2154. 'label' => array('for' => 'contact-5-email'),
  2155. 'Email',
  2156. '/label',
  2157. array('input' => array(
  2158. 'type' => 'text', 'name' => 'Contact[5][email]',
  2159. 'id' => 'contact-5-email', 'maxlength' => '255'
  2160. )),
  2161. '/div'
  2162. );
  2163. $this->assertTags($result, $expected);
  2164. $result = $this->Form->input('Contact.password');
  2165. $expected = array(
  2166. 'div' => array('class' => 'input password'),
  2167. 'label' => array('for' => 'contact-password'),
  2168. 'Password',
  2169. '/label',
  2170. array('input' => array(
  2171. 'type' => 'password', 'name' => 'Contact[password]',
  2172. 'id' => 'contact-password'
  2173. )),
  2174. '/div'
  2175. );
  2176. $this->assertTags($result, $expected);
  2177. $result = $this->Form->input('Contact.email', array(
  2178. 'type' => 'file', 'class' => 'textbox'
  2179. ));
  2180. $expected = array(
  2181. 'div' => array('class' => 'input file'),
  2182. 'label' => array('for' => 'contact-email'),
  2183. 'Email',
  2184. '/label',
  2185. array('input' => array(
  2186. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2187. 'id' => 'contact-email'
  2188. )),
  2189. '/div'
  2190. );
  2191. $this->assertTags($result, $expected);
  2192. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2193. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2194. $result = $this->Form->input('Contact.phone');
  2195. $expected = array(
  2196. 'div' => array('class' => 'input tel'),
  2197. 'label' => array('for' => 'contact-phone'),
  2198. 'Phone',
  2199. '/label',
  2200. array('input' => array(
  2201. 'type' => 'tel', 'name' => 'Contact[phone]',
  2202. 'value' => 'Hello &amp; World &gt; weird chars',
  2203. 'id' => 'contact-phone', 'maxlength' => 255
  2204. )),
  2205. '/div'
  2206. );
  2207. $this->assertTags($result, $expected);
  2208. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2209. $this->Form->create();
  2210. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  2211. $expected = array(
  2212. 'div' => array('class' => 'input text'),
  2213. 'label' => array('for' => 'myId'),
  2214. 'Field',
  2215. '/label',
  2216. 'input' => array(
  2217. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2218. 'value' => 'My value', 'id' => 'myId'
  2219. ),
  2220. '/div'
  2221. );
  2222. $this->assertTags($result, $expected);
  2223. unset($this->Form->request->data);
  2224. $entity->errors('field', 'Badness!');
  2225. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2226. $result = $this->Form->input('Contact.field');
  2227. $expected = array(
  2228. 'div' => array('class' => 'input text error'),
  2229. 'label' => array('for' => 'contact-field'),
  2230. 'Field',
  2231. '/label',
  2232. 'input' => array(
  2233. 'type' => 'text', 'name' => 'Contact[field]',
  2234. 'id' => 'contact-field', 'class' => 'form-error'
  2235. ),
  2236. array('div' => array('class' => 'error-message')),
  2237. 'Badness!',
  2238. '/div',
  2239. '/div'
  2240. );
  2241. $this->assertTags($result, $expected);
  2242. $result = $this->Form->input('Contact.field', array(
  2243. 'templates' => [
  2244. 'groupContainerError' => '{{content}}{{error}}',
  2245. 'error' => '<span class="error-message">{{content}}</span>'
  2246. ]
  2247. ));
  2248. $expected = array(
  2249. 'label' => array('for' => 'contact-field'),
  2250. 'Field',
  2251. '/label',
  2252. 'input' => array(
  2253. 'type' => 'text', 'name' => 'Contact[field]',
  2254. 'id' => 'contact-field', 'class' => 'form-error'
  2255. ),
  2256. array('span' => array('class' => 'error-message')),
  2257. 'Badness!',
  2258. '/span'
  2259. );
  2260. $this->assertTags($result, $expected);
  2261. $entity->errors('field', ['minLength']);
  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. 'Le login doit contenir au moins 2 caractères',
  2276. '/div',
  2277. '/div'
  2278. );
  2279. $this->assertTags($result, $expected);
  2280. $entity->errors('field', ['maxLength']);
  2281. $result = $this->Form->input('Contact.field', array(
  2282. 'error' => array(
  2283. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2284. 'maxLength' => 'login too large',
  2285. )
  2286. ));
  2287. $expected = array(
  2288. 'div' => array('class' => 'input text error'),
  2289. 'label' => array('for' => 'contact-field'),
  2290. 'Field',
  2291. '/label',
  2292. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'contact-field', 'class' => 'form-error'),
  2293. array('div' => array('class' => 'error-message')),
  2294. 'login too large',
  2295. '/div',
  2296. '/div'
  2297. );
  2298. $this->assertTags($result, $expected);
  2299. }
  2300. /**
  2301. * Test that inputs with 0 can be created.
  2302. *
  2303. * @return void
  2304. */
  2305. public function testInputZero() {
  2306. TableRegistry::get('Contacts', [
  2307. 'className' => __NAMESPACE__ . '\ContactsTable'
  2308. ]);
  2309. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2310. $result = $this->Form->input('0');
  2311. $expected = array(
  2312. 'div' => array('class' => 'input text'),
  2313. 'label' => array('for' => '0'), '/label',
  2314. 'input' => array('type' => 'text', 'name' => '0', 'id' => '0'),
  2315. '/div'
  2316. );
  2317. $this->assertTags($result, $expected);
  2318. }
  2319. /**
  2320. * test input() with checkbox creation
  2321. *
  2322. * @return void
  2323. */
  2324. public function testInputCheckbox() {
  2325. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  2326. $expected = array(
  2327. 'div' => array('class' => 'input checkbox'),
  2328. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2329. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2330. '/div'
  2331. );
  2332. $this->assertTags($result, $expected);
  2333. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2334. $expected = array(
  2335. 'div' => array('class' => 'input checkbox'),
  2336. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2337. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2338. '/div'
  2339. );
  2340. $this->assertTags($result, $expected);
  2341. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2342. $expected = array(
  2343. 'div' => array('class' => 'input checkbox'),
  2344. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2345. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2346. '/div'
  2347. );
  2348. $this->assertTags($result, $expected);
  2349. $result = $this->Form->input('User.disabled', array(
  2350. 'label' => 'Disabled',
  2351. 'type' => 'checkbox',
  2352. 'data-foo' => 'disabled'
  2353. ));
  2354. $expected = array(
  2355. 'div' => array('class' => 'input checkbox'),
  2356. 'input' => array('type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'),
  2357. array('input' => array(
  2358. 'type' => 'checkbox',
  2359. 'name' => 'User[disabled]',
  2360. 'value' => '1',
  2361. 'id' => 'user-disabled',
  2362. 'data-foo' => 'disabled'
  2363. )),
  2364. 'label' => array('for' => 'user-disabled'),
  2365. 'Disabled',
  2366. '/label',
  2367. '/div'
  2368. );
  2369. $this->assertTags($result, $expected);
  2370. }
  2371. /**
  2372. * test form->input() with datetime
  2373. *
  2374. * @return void
  2375. */
  2376. public function testInputDatetime() {
  2377. $this->Form = $this->getMock(
  2378. 'Cake\View\Helper\FormHelper',
  2379. ['datetime'],
  2380. [new View(null)]
  2381. );
  2382. $this->Form->expects($this->once())->method('datetime')
  2383. ->with('prueba', [
  2384. 'type' => 'datetime',
  2385. 'timeFormat' => 24,
  2386. 'minYear' => 2008,
  2387. 'maxYear' => 2011,
  2388. 'interval' => 15,
  2389. 'options' => null,
  2390. 'empty' => false,
  2391. 'id' => 'prueba'
  2392. ])
  2393. ->will($this->returnValue('This is it!'));
  2394. $result = $this->Form->input('prueba', array(
  2395. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2396. 'maxYear' => 2011, 'interval' => 15
  2397. ));
  2398. $expected = array(
  2399. 'div' => array('class' => 'input datetime'),
  2400. 'label' => array('for' => 'prueba'),
  2401. 'Prueba',
  2402. '/label',
  2403. 'This is it!',
  2404. '/div'
  2405. );
  2406. $this->assertTags($result, $expected);
  2407. }
  2408. /**
  2409. * Test generating checkboxes with disabled elements.
  2410. *
  2411. * @return void
  2412. */
  2413. public function testInputCheckboxWithDisabledElements() {
  2414. $this->markTestIncomplete('Need to revisit once models work again.');
  2415. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2416. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2417. $expected = array(
  2418. array('div' => array('class' => 'input select')),
  2419. array('label' => array('for' => "ContactMultiple")),
  2420. 'Multiple',
  2421. '/label',
  2422. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2423. array('div' => array('class' => 'checkbox')),
  2424. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2425. array('label' => array('for' => "ContactMultiple1")),
  2426. 'One',
  2427. '/label',
  2428. '/div',
  2429. array('div' => array('class' => 'checkbox')),
  2430. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2431. array('label' => array('for' => "ContactMultiple2")),
  2432. 'Two',
  2433. '/label',
  2434. '/div',
  2435. array('div' => array('class' => 'checkbox')),
  2436. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2437. array('label' => array('for' => "ContactMultiple3")),
  2438. 'Three',
  2439. '/label',
  2440. '/div',
  2441. '/div'
  2442. );
  2443. $this->assertTags($result, $expected);
  2444. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2445. $expected = array(
  2446. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  2447. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2448. 'label' => array('for' => 'ModelField12'),
  2449. 'half',
  2450. '/label'
  2451. );
  2452. $this->assertTags($result, $expected);
  2453. $disabled = array('2', 3);
  2454. $expected = array(
  2455. array('div' => array('class' => 'input select')),
  2456. array('label' => array('for' => "ContactMultiple")),
  2457. 'Multiple',
  2458. '/label',
  2459. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2460. array('div' => array('class' => 'checkbox')),
  2461. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2462. array('label' => array('for' => "ContactMultiple1")),
  2463. 'One',
  2464. '/label',
  2465. '/div',
  2466. array('div' => array('class' => 'checkbox')),
  2467. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2468. array('label' => array('for' => "ContactMultiple2")),
  2469. 'Two',
  2470. '/label',
  2471. '/div',
  2472. array('div' => array('class' => 'checkbox')),
  2473. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2474. array('label' => array('for' => "ContactMultiple3")),
  2475. 'Three',
  2476. '/label',
  2477. '/div',
  2478. '/div'
  2479. );
  2480. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2481. $this->assertTags($result, $expected);
  2482. // make sure 50 does only disable 50, and not 50f5c0cf
  2483. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2484. $disabled = array(50);
  2485. $expected = array(
  2486. array('div' => array('class' => 'input select')),
  2487. array('label' => array('for' => "ContactMultiple")),
  2488. 'Multiple',
  2489. '/label',
  2490. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2491. array('div' => array('class' => 'checkbox')),
  2492. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2493. array('label' => array('for' => "ContactMultiple50")),
  2494. 'Fifty',
  2495. '/label',
  2496. '/div',
  2497. array('div' => array('class' => 'checkbox')),
  2498. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2499. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2500. 'Stringy',
  2501. '/label',
  2502. '/div',
  2503. '/div'
  2504. );
  2505. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2506. $this->assertTags($result, $expected);
  2507. }
  2508. /**
  2509. * test input name with leading integer, ensure attributes are generated correctly.
  2510. *
  2511. * @return void
  2512. */
  2513. public function testInputWithLeadingInteger() {
  2514. $result = $this->Form->text('0.Node.title');
  2515. $expected = array(
  2516. 'input' => array('name' => '0[Node][title]', 'type' => 'text')
  2517. );
  2518. $this->assertTags($result, $expected);
  2519. }
  2520. /**
  2521. * test form->input() with select type inputs.
  2522. *
  2523. * @return void
  2524. */
  2525. public function testInputSelectType() {
  2526. $this->markTestIncomplete('A test here is throwing fatal error, fix later');
  2527. $result = $this->Form->input('email', array(
  2528. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2529. );
  2530. $expected = array(
  2531. 'div' => array('class' => 'input select'),
  2532. 'label' => array('for' => 'email'),
  2533. 'Email',
  2534. '/label',
  2535. array('select' => array('name' => 'email', 'id' => 'email')),
  2536. array('option' => array('value' => '')),
  2537. '/option',
  2538. array('option' => array('value' => 'è')),
  2539. 'Firést',
  2540. '/option',
  2541. array('option' => array('value' => 'é')),
  2542. 'Secoènd',
  2543. '/option',
  2544. '/select',
  2545. '/div'
  2546. );
  2547. $this->assertTags($result, $expected);
  2548. $result = $this->Form->input('email', array(
  2549. 'options' => array('First', 'Second'), 'empty' => true)
  2550. );
  2551. $expected = array(
  2552. 'div' => array('class' => 'input select'),
  2553. 'label' => array('for' => 'email'),
  2554. 'Email',
  2555. '/label',
  2556. array('select' => array('name' => 'email', 'id' => 'email')),
  2557. array('option' => array('value' => '')),
  2558. '/option',
  2559. array('option' => array('value' => '0')),
  2560. 'First',
  2561. '/option',
  2562. array('option' => array('value' => '1')),
  2563. 'Second',
  2564. '/option',
  2565. '/select',
  2566. '/div'
  2567. );
  2568. $this->assertTags($result, $expected);
  2569. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2570. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2571. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2572. $expected = array(
  2573. 'div' => array('class' => 'input select'),
  2574. 'label' => array('for' => 'ModelUserId'),
  2575. 'User',
  2576. '/label',
  2577. 'select' => array('name' => 'Model[user_id]', 'id' => 'ModelUserId'),
  2578. array('option' => array('value' => '')),
  2579. '/option',
  2580. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2581. 'good',
  2582. '/option',
  2583. array('option' => array('value' => 'other')),
  2584. 'bad',
  2585. '/option',
  2586. '/select',
  2587. '/div'
  2588. );
  2589. $this->assertTags($result, $expected);
  2590. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2591. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2592. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2593. $expected = array(
  2594. 'div' => array('class' => 'input select'),
  2595. 'label' => array('for' => 'ThingUserId'),
  2596. 'User',
  2597. '/label',
  2598. 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'),
  2599. array('option' => array('value' => '')),
  2600. 'Some Empty',
  2601. '/option',
  2602. array('option' => array('value' => 'value')),
  2603. 'good',
  2604. '/option',
  2605. array('option' => array('value' => 'other')),
  2606. 'bad',
  2607. '/option',
  2608. '/select',
  2609. '/div'
  2610. );
  2611. $this->assertTags($result, $expected);
  2612. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2613. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2614. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2615. $expected = array(
  2616. 'div' => array('class' => 'input select'),
  2617. 'label' => array('for' => 'ThingUserId'),
  2618. 'User',
  2619. '/label',
  2620. 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'),
  2621. array('option' => array('value' => '')),
  2622. 'Some Empty',
  2623. '/option',
  2624. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2625. 'good',
  2626. '/option',
  2627. array('option' => array('value' => 'other')),
  2628. 'bad',
  2629. '/option',
  2630. '/select',
  2631. '/div'
  2632. );
  2633. $this->assertTags($result, $expected);
  2634. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2635. $this->Form->request->data = array('User' => array('User' => array('value')));
  2636. $result = $this->Form->input('User.User', array('empty' => true));
  2637. $expected = array(
  2638. 'div' => array('class' => 'input select'),
  2639. 'label' => array('for' => 'UserUser'),
  2640. 'User',
  2641. '/label',
  2642. 'input' => array('type' => 'hidden', 'name' => 'User[User]', 'value' => '', 'id' => 'UserUser_'),
  2643. 'select' => array('name' => 'User[User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2644. array('option' => array('value' => '')),
  2645. '/option',
  2646. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2647. 'good',
  2648. '/option',
  2649. array('option' => array('value' => 'other')),
  2650. 'bad',
  2651. '/option',
  2652. '/select',
  2653. '/div'
  2654. );
  2655. $this->assertTags($result, $expected);
  2656. $this->Form->data = array();
  2657. $result = $this->Form->input('Publisher.id', array(
  2658. 'label' => 'Publisher',
  2659. 'type' => 'select',
  2660. 'multiple' => 'checkbox',
  2661. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2662. ));
  2663. $expected = array(
  2664. array('div' => array('class' => 'input select')),
  2665. array('label' => array('for' => 'PublisherId')),
  2666. 'Publisher',
  2667. '/label',
  2668. 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'PublisherId'),
  2669. array('div' => array('class' => 'checkbox')),
  2670. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2671. array('label' => array('for' => 'PublisherIdValue1')),
  2672. 'Label 1',
  2673. '/label',
  2674. '/div',
  2675. array('div' => array('class' => 'checkbox')),
  2676. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2677. array('label' => array('for' => 'PublisherIdValue2')),
  2678. 'Label 2',
  2679. '/label',
  2680. '/div',
  2681. '/div'
  2682. );
  2683. $this->assertTags($result, $expected);
  2684. }
  2685. /**
  2686. * test that input() and a non standard primary key makes a hidden input by default.
  2687. *
  2688. * @return void
  2689. */
  2690. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2691. $this->markTestIncomplete('Need to revisit once models work again.');
  2692. $this->Form->create('User');
  2693. $this->Form->fieldset = array(
  2694. 'User' => array(
  2695. 'fields' => array(
  2696. 'model_id' => array('type' => 'integer')
  2697. ),
  2698. 'validates' => array(),
  2699. 'key' => 'model_id'
  2700. )
  2701. );
  2702. $result = $this->Form->input('model_id');
  2703. $expected = array(
  2704. 'input' => array('type' => 'hidden', 'name' => 'User[model_id]', 'id' => 'UserModelId'),
  2705. );
  2706. $this->assertTags($result, $expected);
  2707. }
  2708. /**
  2709. * test that overriding the magic select type widget is possible
  2710. *
  2711. * @return void
  2712. */
  2713. public function testInputOverridingMagicSelectType() {
  2714. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2715. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2716. $expected = array(
  2717. 'div' => array('class' => 'input text'),
  2718. 'label' => array('for' => 'model-user-id'), 'User', '/label',
  2719. 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'),
  2720. '/div'
  2721. );
  2722. $this->assertTags($result, $expected);
  2723. //Check that magic types still work for plural/singular vars
  2724. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2725. $result = $this->Form->input('Model.type');
  2726. $expected = array(
  2727. 'div' => array('class' => 'input select'),
  2728. 'label' => array('for' => 'model-type'), 'Type', '/label',
  2729. 'select' => array('name' => 'Model[type]', 'id' => 'model-type'),
  2730. array('option' => array('value' => 'value')), 'good', '/option',
  2731. array('option' => array('value' => 'other')), 'bad', '/option',
  2732. '/select',
  2733. '/div'
  2734. );
  2735. $this->assertTags($result, $expected);
  2736. }
  2737. /**
  2738. * Test that inferred types do not override developer input
  2739. *
  2740. * @return void
  2741. */
  2742. public function testInputMagicTypeDoesNotOverride() {
  2743. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2744. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2745. $expected = array(
  2746. 'div' => array('class' => 'input checkbox'),
  2747. array('input' => array(
  2748. 'type' => 'hidden',
  2749. 'name' => 'Model[user]',
  2750. 'value' => 0,
  2751. )),
  2752. array('input' => array(
  2753. 'name' => 'Model[user]',
  2754. 'type' => 'checkbox',
  2755. 'id' => 'model-user',
  2756. 'value' => 1
  2757. )),
  2758. 'label' => array('for' => 'model-user'), 'User', '/label',
  2759. '/div'
  2760. );
  2761. $this->assertTags($result, $expected);
  2762. }
  2763. /**
  2764. * Test that magic input() selects are created for type=number
  2765. *
  2766. * @return void
  2767. */
  2768. public function testInputMagicSelectForTypeNumber() {
  2769. TableRegistry::get('ValidateUsers', [
  2770. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2771. ]);
  2772. $entity = new Entity(['balance' => 1]);
  2773. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  2774. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2775. $result = $this->Form->input('ValidateUser.balance');
  2776. $expected = array(
  2777. 'div' => array('class' => 'input select'),
  2778. 'label' => array('for' => 'validateuser-balance'),
  2779. 'Balance',
  2780. '/label',
  2781. 'select' => array('name' => 'ValidateUser[balance]', 'id' => 'validateuser-balance'),
  2782. array('option' => array('value' => '0')),
  2783. 'nothing',
  2784. '/option',
  2785. array('option' => array('value' => '1', 'selected' => 'selected')),
  2786. 'some',
  2787. '/option',
  2788. array('option' => array('value' => '100')),
  2789. 'a lot',
  2790. '/option',
  2791. '/select',
  2792. '/div'
  2793. );
  2794. $this->assertTags($result, $expected);
  2795. }
  2796. /**
  2797. * Test that magic input() selects can easily be converted into radio types without error.
  2798. *
  2799. * @return void
  2800. */
  2801. public function testInputMagicSelectChangeToRadio() {
  2802. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2803. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2804. $this->assertRegExp('/input type="radio"/', $result);
  2805. }
  2806. /**
  2807. * fields with the same name as the model should work.
  2808. *
  2809. * @return void
  2810. */
  2811. public function testInputWithMatchingFieldAndModelName() {
  2812. $this->markTestIncomplete('Need to revisit once models work again.');
  2813. $this->Form->create('User');
  2814. $this->Form->fieldset = array(
  2815. 'User' => array(
  2816. 'fields' => array(
  2817. 'User' => array('type' => 'text')
  2818. ),
  2819. 'validates' => array(),
  2820. 'key' => 'id'
  2821. )
  2822. );
  2823. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2824. $result = $this->Form->input('User', array('type' => 'text'));
  2825. $expected = array(
  2826. 'div' => array('class' => 'input text'),
  2827. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2828. 'input' => array('name' => 'User[User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2829. '/div'
  2830. );
  2831. $this->assertTags($result, $expected);
  2832. }
  2833. /**
  2834. * testFormInputs method
  2835. *
  2836. * test correct results from form::inputs().
  2837. *
  2838. * @return void
  2839. */
  2840. public function testFormInputs() {
  2841. $this->markTestIncomplete('Need to revisit once models work again.');
  2842. $this->Form->create('Cake\Test\TestCase\View\Helper\Contact');
  2843. $result = $this->Form->inputs('The Legend');
  2844. $expected = array(
  2845. '<fieldset',
  2846. '<legend',
  2847. 'The Legend',
  2848. '/legend',
  2849. '*/fieldset',
  2850. );
  2851. $this->assertTags($result, $expected);
  2852. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2853. $expected = array(
  2854. 'fieldset' => array('class' => 'classy-stuff'),
  2855. '<legend',
  2856. 'Field of Dreams',
  2857. '/legend',
  2858. '*/fieldset'
  2859. );
  2860. $this->assertTags($result, $expected);
  2861. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2862. $this->assertTags($result, $expected);
  2863. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2864. $this->assertTags($result, $expected);
  2865. $this->Form->create('Contact');
  2866. $this->Form->request['prefix'] = 'admin';
  2867. $this->Form->request['action'] = 'admin_edit';
  2868. $result = $this->Form->inputs();
  2869. $expected = array(
  2870. '<fieldset',
  2871. '<legend',
  2872. 'Edit Contact',
  2873. '/legend',
  2874. '*/fieldset',
  2875. );
  2876. $this->assertTags($result, $expected);
  2877. $this->Form->create('Contact');
  2878. $result = $this->Form->inputs(false);
  2879. $expected = array(
  2880. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2881. array('div' => array('class' => 'input text')),
  2882. '*/div',
  2883. array('div' => array('class' => 'input email')),
  2884. '*/div',
  2885. array('div' => array('class' => 'input tel')),
  2886. '*/div',
  2887. array('div' => array('class' => 'input password')),
  2888. '*/div',
  2889. array('div' => array('class' => 'input date')),
  2890. '*/div',
  2891. array('div' => array('class' => 'input date')),
  2892. '*/div',
  2893. array('div' => array('class' => 'input datetime')),
  2894. '*/div',
  2895. array('div' => array('class' => 'input number')),
  2896. '*/div',
  2897. array('div' => array('class' => 'input select')),
  2898. '*/div',
  2899. );
  2900. $this->assertTags($result, $expected);
  2901. $this->Form->create('Contact');
  2902. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2903. $expected = array(
  2904. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2905. array('div' => array('class' => 'input text')),
  2906. '*/div',
  2907. array('div' => array('class' => 'input email')),
  2908. '*/div',
  2909. array('div' => array('class' => 'input tel')),
  2910. '*/div',
  2911. array('div' => array('class' => 'input password')),
  2912. '*/div',
  2913. array('div' => array('class' => 'input date')),
  2914. '*/div',
  2915. array('div' => array('class' => 'input date')),
  2916. '*/div',
  2917. array('div' => array('class' => 'input datetime')),
  2918. '*/div',
  2919. array('div' => array('class' => 'input number')),
  2920. '*/div',
  2921. array('div' => array('class' => 'input select')),
  2922. '*/div',
  2923. );
  2924. $this->assertTags($result, $expected);
  2925. $this->Form->create('Contact');
  2926. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2927. $this->assertTags($result, $expected);
  2928. $this->Form->create('Contact');
  2929. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2930. $expected = array(
  2931. 'fieldset' => array(),
  2932. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2933. array('div' => array('class' => 'input text')),
  2934. '*/div',
  2935. array('div' => array('class' => 'input email')),
  2936. '*/div',
  2937. array('div' => array('class' => 'input tel')),
  2938. '*/div',
  2939. array('div' => array('class' => 'input password')),
  2940. '*/div',
  2941. array('div' => array('class' => 'input date')),
  2942. '*/div',
  2943. array('div' => array('class' => 'input date')),
  2944. '*/div',
  2945. array('div' => array('class' => 'input datetime')),
  2946. '*/div',
  2947. array('div' => array('class' => 'input number')),
  2948. '*/div',
  2949. array('div' => array('class' => 'input select')),
  2950. '*/div',
  2951. '/fieldset'
  2952. );
  2953. $this->assertTags($result, $expected);
  2954. $this->Form->create('Contact');
  2955. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2956. $expected = array(
  2957. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2958. array('div' => array('class' => 'input text')),
  2959. '*/div',
  2960. array('div' => array('class' => 'input email')),
  2961. '*/div',
  2962. array('div' => array('class' => 'input tel')),
  2963. '*/div',
  2964. array('div' => array('class' => 'input password')),
  2965. '*/div',
  2966. array('div' => array('class' => 'input date')),
  2967. '*/div',
  2968. array('div' => array('class' => 'input date')),
  2969. '*/div',
  2970. array('div' => array('class' => 'input datetime')),
  2971. '*/div',
  2972. array('div' => array('class' => 'input number')),
  2973. '*/div',
  2974. array('div' => array('class' => 'input select')),
  2975. '*/div',
  2976. );
  2977. $this->assertTags($result, $expected);
  2978. $this->Form->create('Contact');
  2979. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2980. $this->assertTags($result, $expected);
  2981. $this->Form->create('Contact');
  2982. $result = $this->Form->inputs('Hello');
  2983. $expected = array(
  2984. 'fieldset' => array(),
  2985. 'legend' => array(),
  2986. 'Hello',
  2987. '/legend',
  2988. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2989. array('div' => array('class' => 'input text')),
  2990. '*/div',
  2991. array('div' => array('class' => 'input email')),
  2992. '*/div',
  2993. array('div' => array('class' => 'input tel')),
  2994. '*/div',
  2995. array('div' => array('class' => 'input password')),
  2996. '*/div',
  2997. array('div' => array('class' => 'input date')),
  2998. '*/div',
  2999. array('div' => array('class' => 'input date')),
  3000. '*/div',
  3001. array('div' => array('class' => 'input datetime')),
  3002. '*/div',
  3003. array('div' => array('class' => 'input number')),
  3004. '*/div',
  3005. array('div' => array('class' => 'input select')),
  3006. '*/div',
  3007. '/fieldset'
  3008. );
  3009. $this->assertTags($result, $expected);
  3010. $this->Form->create('Contact');
  3011. $result = $this->Form->inputs(array('legend' => 'Hello'));
  3012. $expected = array(
  3013. 'fieldset' => array(),
  3014. 'legend' => array(),
  3015. 'Hello',
  3016. '/legend',
  3017. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3018. array('div' => array('class' => 'input text')),
  3019. '*/div',
  3020. array('div' => array('class' => 'input email')),
  3021. '*/div',
  3022. array('div' => array('class' => 'input tel')),
  3023. '*/div',
  3024. array('div' => array('class' => 'input password')),
  3025. '*/div',
  3026. array('div' => array('class' => 'input date')),
  3027. '*/div',
  3028. array('div' => array('class' => 'input date')),
  3029. '*/div',
  3030. array('div' => array('class' => 'input datetime')),
  3031. '*/div',
  3032. array('div' => array('class' => 'input number')),
  3033. '*/div',
  3034. array('div' => array('class' => 'input select')),
  3035. '*/div',
  3036. '/fieldset'
  3037. );
  3038. $this->assertTags($result, $expected);
  3039. $this->Form->create('Contact');
  3040. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  3041. $this->assertTags($result, $expected);
  3042. $this->Form->end();
  3043. $this->Form->create(false);
  3044. $expected = array(
  3045. 'fieldset' => array(),
  3046. array('div' => array('class' => 'input text')),
  3047. 'label' => array('for' => 'foo'),
  3048. 'Foo',
  3049. '/label',
  3050. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  3051. '*/div',
  3052. '/fieldset'
  3053. );
  3054. $result = $this->Form->inputs(
  3055. array('foo' => array('type' => 'text')),
  3056. array(),
  3057. array('legend' => false)
  3058. );
  3059. $this->assertTags($result, $expected);
  3060. }
  3061. /**
  3062. * Tests inputs() works with plugin models
  3063. *
  3064. * @return void
  3065. */
  3066. public function testInputsPluginModel() {
  3067. $this->markTestIncomplete('Need to revisit once models work again.');
  3068. $this->loadFixtures('Post');
  3069. Plugin::load('TestPlugin');
  3070. $this->Form->request['models'] = array(
  3071. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  3072. );
  3073. $this->Form->create('TestPlugin.TestPluginPost');
  3074. $result = $this->Form->inputs();
  3075. $this->assertContains('TestPluginPost[id]', $result);
  3076. $this->assertContains('TestPluginPost[author_id]', $result);
  3077. $this->assertContains('TestPluginPost[title]', $result);
  3078. $this->assertEquals('TestPluginPost', $this->Form->model());
  3079. }
  3080. /**
  3081. * testSelectAsCheckbox method
  3082. *
  3083. * test multi-select widget with checkbox formatting.
  3084. *
  3085. * @return void
  3086. */
  3087. public function testSelectAsCheckbox() {
  3088. $result = $this->Form->select(
  3089. 'Model.multi_field',
  3090. array('first', 'second', 'third'),
  3091. array('multiple' => 'checkbox', 'value' => array(0, 1))
  3092. );
  3093. $expected = array(
  3094. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3095. array('div' => array('class' => 'checkbox')),
  3096. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0')),
  3097. array('label' => array('for' => 'model-multi-field-0', 'class' => 'selected')),
  3098. 'first',
  3099. '/label',
  3100. '/div',
  3101. array('div' => array('class' => 'checkbox')),
  3102. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1')),
  3103. array('label' => array('for' => 'model-multi-field-1', 'class' => 'selected')),
  3104. 'second',
  3105. '/label',
  3106. '/div',
  3107. array('div' => array('class' => 'checkbox')),
  3108. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  3109. array('label' => array('for' => 'model-multi-field-2')),
  3110. 'third',
  3111. '/label',
  3112. '/div',
  3113. );
  3114. $this->assertTags($result, $expected);
  3115. $result = $this->Form->select(
  3116. 'Model.multi_field',
  3117. array('1/2' => 'half'),
  3118. array('multiple' => 'checkbox')
  3119. );
  3120. $expected = array(
  3121. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3122. array('div' => array('class' => 'checkbox')),
  3123. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2')),
  3124. array('label' => array('for' => 'model-multi-field-1-2')),
  3125. 'half',
  3126. '/label',
  3127. '/div',
  3128. );
  3129. $this->assertTags($result, $expected);
  3130. }
  3131. /**
  3132. * testLabel method
  3133. *
  3134. * test label generation.
  3135. *
  3136. * @return void
  3137. */
  3138. public function testLabel() {
  3139. $result = $this->Form->label('Person.name');
  3140. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  3141. $result = $this->Form->label('Person.name');
  3142. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  3143. $result = $this->Form->label('Person.first_name');
  3144. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'First Name', '/label'));
  3145. $result = $this->Form->label('Person.first_name', 'Your first name');
  3146. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'Your first name', '/label'));
  3147. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  3148. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class'), 'Your first name', '/label'));
  3149. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  3150. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  3151. $result = $this->Form->label('Person.first_name', '');
  3152. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), '/label'));
  3153. $result = $this->Form->label('Person.2.name', '');
  3154. $this->assertTags($result, array('label' => array('for' => 'person-2-name'), '/label'));
  3155. }
  3156. /**
  3157. * testTextbox method
  3158. *
  3159. * test textbox element generation
  3160. *
  3161. * @return void
  3162. */
  3163. public function testTextbox() {
  3164. $result = $this->Form->text('Model.field');
  3165. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]')));
  3166. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3167. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]')));
  3168. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3169. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID')));
  3170. }
  3171. /**
  3172. * Test that text() hooks up with request data and error fields.
  3173. *
  3174. * @return void
  3175. */
  3176. public function testTextBoxDataAndError() {
  3177. $this->article['errors'] = [
  3178. 'Contact' => ['text' => 'wrong']
  3179. ];
  3180. $this->Form->create($this->article);
  3181. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3182. $result = $this->Form->text('Model.text');
  3183. $expected = [
  3184. 'input' => [
  3185. 'type' => 'text',
  3186. 'name' => 'Model[text]',
  3187. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3188. ]
  3189. ];
  3190. $this->assertTags($result, $expected);
  3191. $this->Form->request->data['Contact']['text'] = 'test';
  3192. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3193. $expected = [
  3194. 'input' => [
  3195. 'type' => 'text',
  3196. 'name' => 'Contact[text]',
  3197. 'value' => 'test',
  3198. 'id' => 'theID',
  3199. 'class' => 'form-error'
  3200. ]
  3201. ];
  3202. $this->assertTags($result, $expected);
  3203. }
  3204. /**
  3205. * testDefaultValue method
  3206. *
  3207. * Test default value setting
  3208. *
  3209. * @return void
  3210. */
  3211. public function testTextDefaultValue() {
  3212. $this->Form->request->data['Model']['field'] = 'test';
  3213. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3214. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3215. $this->assertTags($result, $expected);
  3216. unset($this->Form->request->data['Model']['field']);
  3217. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3218. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3219. $this->assertTags($result, $expected);
  3220. }
  3221. /**
  3222. * testError method
  3223. *
  3224. * Test field error generation
  3225. *
  3226. * @return void
  3227. */
  3228. public function testError() {
  3229. $this->article['errors'] = [
  3230. 'Article' => ['field' => 'email']
  3231. ];
  3232. $this->Form->create($this->article);
  3233. $result = $this->Form->error('Article.field');
  3234. $expected = [
  3235. ['div' => ['class' => 'error-message']],
  3236. 'email',
  3237. '/div',
  3238. ];
  3239. $this->assertTags($result, $expected);
  3240. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  3241. $expected = [
  3242. ['div' => ['class' => 'error-message']],
  3243. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3244. '/div',
  3245. ];
  3246. $this->assertTags($result, $expected);
  3247. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3248. $expected = [
  3249. ['div' => ['class' => 'error-message']],
  3250. '<strong', 'Badness!', '/strong',
  3251. '/div',
  3252. ];
  3253. $this->assertTags($result, $expected);
  3254. }
  3255. /**
  3256. * Test error with nested lists.
  3257. *
  3258. * @return void
  3259. */
  3260. public function testErrorMessages() {
  3261. $this->article['errors'] = [
  3262. 'Article' => ['field' => 'email']
  3263. ];
  3264. $this->Form->create($this->article);
  3265. $result = $this->Form->error('Article.field', array(
  3266. 'email' => 'No good!'
  3267. ));
  3268. $expected = array(
  3269. 'div' => array('class' => 'error-message'),
  3270. 'No good!',
  3271. '/div'
  3272. );
  3273. $this->assertTags($result, $expected);
  3274. }
  3275. /**
  3276. * test error() with multiple messages.
  3277. *
  3278. * @return void
  3279. */
  3280. public function testErrorMultipleMessages() {
  3281. $this->article['errors'] = [
  3282. 'field' => ['notEmpty', 'email', 'Something else']
  3283. ];
  3284. $this->Form->create($this->article);
  3285. $result = $this->Form->error('field', array(
  3286. 'notEmpty' => 'Cannot be empty',
  3287. 'email' => 'No good!'
  3288. ));
  3289. $expected = array(
  3290. 'div' => array('class' => 'error-message'),
  3291. 'ul' => array(),
  3292. '<li', 'Cannot be empty', '/li',
  3293. '<li', 'No good!', '/li',
  3294. '<li', 'Something else', '/li',
  3295. '/ul',
  3296. '/div'
  3297. );
  3298. $this->assertTags($result, $expected);
  3299. }
  3300. /**
  3301. * test error options when using form->input();
  3302. *
  3303. * @return void
  3304. */
  3305. public function testInputErrorEscape() {
  3306. $this->markTestIncomplete('Need to revisit once models work again.');
  3307. $this->Form->create('ValidateProfile');
  3308. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3309. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3310. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3311. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3312. $this->assertRegExp('/required<br>/', $result);
  3313. }
  3314. /**
  3315. * testPassword method
  3316. *
  3317. * Test password element generation
  3318. *
  3319. * @return void
  3320. */
  3321. public function testPassword() {
  3322. $this->article['errors'] = [
  3323. 'Contact' => [
  3324. 'passwd' => 1
  3325. ]
  3326. ];
  3327. $this->Form->create($this->article);
  3328. $result = $this->Form->password('Contact.field');
  3329. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]')));
  3330. $this->Form->request->data['Contact']['passwd'] = 'test';
  3331. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3332. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3333. }
  3334. /**
  3335. * testRadio method
  3336. *
  3337. * Test radio element set generation
  3338. *
  3339. * @return void
  3340. */
  3341. public function testRadio() {
  3342. $result = $this->Form->radio('Model.field', array('option A'));
  3343. $expected = array(
  3344. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3345. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3346. 'label' => array('for' => 'model-field-0'),
  3347. 'option A',
  3348. '/label'
  3349. );
  3350. $this->assertTags($result, $expected);
  3351. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3352. $expected = array(
  3353. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3354. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3355. array('label' => array('for' => 'model-field-0')),
  3356. 'option A',
  3357. '/label',
  3358. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1')),
  3359. array('label' => array('for' => 'model-field-1')),
  3360. 'option B',
  3361. '/label',
  3362. );
  3363. $this->assertTags($result, $expected);
  3364. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3365. $expected = array(
  3366. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''),
  3367. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male')),
  3368. array('label' => array('for' => 'employee-gender-male')),
  3369. 'Male',
  3370. '/label',
  3371. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female')),
  3372. array('label' => array('for' => 'employee-gender-female')),
  3373. 'Female',
  3374. '/label',
  3375. );
  3376. $this->assertTags($result, $expected);
  3377. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  3378. $expected = array(
  3379. array('input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '')),
  3380. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0')),
  3381. array('label' => array('for' => 'model-custom-0')),
  3382. 'option A',
  3383. '/label',
  3384. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1')),
  3385. array('label' => array('for' => 'model-custom-1')),
  3386. 'option B',
  3387. '/label',
  3388. );
  3389. $this->assertTags($result, $expected);
  3390. }
  3391. /**
  3392. * test generating radio input inside label ala twitter bootstrap
  3393. *
  3394. * @return void
  3395. */
  3396. public function testRadioInputInsideLabel() {
  3397. $this->Form->templates([
  3398. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3399. 'radioContainer' => '{{label}}'
  3400. ]);
  3401. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  3402. $expected = [
  3403. ['input' => [
  3404. 'type' => 'hidden',
  3405. 'name' => 'Model[field]',
  3406. 'value' => ''
  3407. ]],
  3408. ['label' => ['for' => 'model-field-0']],
  3409. ['input' => [
  3410. 'type' => 'radio',
  3411. 'name' => 'Model[field]',
  3412. 'value' => '0',
  3413. 'id' => 'model-field-0'
  3414. ]],
  3415. 'option A',
  3416. '/label',
  3417. ['label' => ['for' => 'model-field-1']],
  3418. ['input' => [
  3419. 'type' => 'radio',
  3420. 'name' => 'Model[field]',
  3421. 'value' => '1',
  3422. 'id' => 'model-field-1'
  3423. ]],
  3424. 'option B',
  3425. '/label',
  3426. ];
  3427. $this->assertTags($result, $expected);
  3428. }
  3429. /**
  3430. * test disabling the hidden input for radio buttons
  3431. *
  3432. * @return void
  3433. */
  3434. public function testRadioHiddenInputDisabling() {
  3435. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3436. $expected = array(
  3437. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'),
  3438. 'label' => array('for' => 'model-1-field-0'),
  3439. 'option A',
  3440. '/label'
  3441. );
  3442. $this->assertTags($result, $expected);
  3443. }
  3444. /**
  3445. * testSelect method
  3446. *
  3447. * Test select element generation.
  3448. *
  3449. * @return void
  3450. */
  3451. public function testSelect() {
  3452. $result = $this->Form->select('Model.field', array());
  3453. $expected = array(
  3454. 'select' => array('name' => 'Model[field]'),
  3455. '/select'
  3456. );
  3457. $this->assertTags($result, $expected);
  3458. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3459. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3460. $expected = array(
  3461. 'select' => array('name' => 'Model[field]'),
  3462. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3463. 'good',
  3464. '/option',
  3465. array('option' => array('value' => 'other')),
  3466. 'bad',
  3467. '/option',
  3468. '/select'
  3469. );
  3470. $this->assertTags($result, $expected);
  3471. $this->Form->request->data = array();
  3472. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3473. $expected = array(
  3474. 'select' => array('name' => 'Model[field]'),
  3475. array('option' => array('value' => 'value')),
  3476. 'good',
  3477. '/option',
  3478. array('option' => array('value' => 'other')),
  3479. 'bad',
  3480. '/option',
  3481. '/select'
  3482. );
  3483. $this->assertTags($result, $expected);
  3484. $result = $this->Form->select(
  3485. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3486. array('empty' => false)
  3487. );
  3488. $expected = array(
  3489. 'select' => array('name' => 'Model[field]'),
  3490. array('option' => array('value' => 'first')),
  3491. 'first &quot;html&quot; &lt;chars&gt;',
  3492. '/option',
  3493. array('option' => array('value' => 'second')),
  3494. 'value',
  3495. '/option',
  3496. '/select'
  3497. );
  3498. $this->assertTags($result, $expected);
  3499. $result = $this->Form->select(
  3500. 'Model.field',
  3501. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3502. array('escape' => false, 'empty' => false)
  3503. );
  3504. $expected = array(
  3505. 'select' => array('name' => 'Model[field]'),
  3506. array('option' => array('value' => 'first')),
  3507. 'first "html" <chars>',
  3508. '/option',
  3509. array('option' => array('value' => 'second')),
  3510. 'value',
  3511. '/option',
  3512. '/select'
  3513. );
  3514. $this->assertTags($result, $expected);
  3515. $options = array(
  3516. array('value' => 'first', 'text' => 'First'),
  3517. array('value' => 'first', 'text' => 'Another First'),
  3518. );
  3519. $result = $this->Form->select(
  3520. 'Model.field',
  3521. $options,
  3522. array('escape' => false, 'empty' => false)
  3523. );
  3524. $expected = array(
  3525. 'select' => array('name' => 'Model[field]'),
  3526. array('option' => array('value' => 'first')),
  3527. 'First',
  3528. '/option',
  3529. array('option' => array('value' => 'first')),
  3530. 'Another First',
  3531. '/option',
  3532. '/select'
  3533. );
  3534. $this->assertTags($result, $expected);
  3535. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3536. $result = $this->Form->select(
  3537. 'Model.contact_id',
  3538. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3539. array('escape' => false, 'empty' => 'pick something')
  3540. );
  3541. $expected = array(
  3542. 'select' => array('name' => 'Model[contact_id]'),
  3543. array('option' => array('value' => '')), 'pick something', '/option',
  3544. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3545. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3546. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3547. '/select'
  3548. );
  3549. $this->assertTags($result, $expected);
  3550. $this->Form->request->data['Model']['field'] = 0;
  3551. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3552. $expected = array(
  3553. 'select' => array('name' => 'Model[field]'),
  3554. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3555. array('option' => array('value' => '1')), 'Yes', '/option',
  3556. '/select'
  3557. );
  3558. $this->assertTags($result, $expected);
  3559. }
  3560. /**
  3561. * test select() with required and disabled attributes.
  3562. *
  3563. * @return void
  3564. */
  3565. public function testSelectRequired() {
  3566. $this->article['required'] = [
  3567. 'user_id' => true
  3568. ];
  3569. $this->Form->create($this->article);
  3570. $result = $this->Form->select('user_id', array('option A'));
  3571. $expected = array(
  3572. 'select' => array(
  3573. 'name' => 'user_id',
  3574. 'required' => 'required'
  3575. ),
  3576. array('option' => array('value' => '0')), 'option A', '/option',
  3577. '/select'
  3578. );
  3579. $this->assertTags($result, $expected);
  3580. $result = $this->Form->select('user_id', array('option A'), array('disabled' => true));
  3581. $expected = array(
  3582. 'select' => array(
  3583. 'name' => 'user_id',
  3584. 'disabled' => 'disabled'
  3585. ),
  3586. array('option' => array('value' => '0')), 'option A', '/option',
  3587. '/select'
  3588. );
  3589. $this->assertTags($result, $expected);
  3590. }
  3591. /**
  3592. * testNestedSelect method
  3593. *
  3594. * test select element generation with optgroups
  3595. *
  3596. * @return void
  3597. */
  3598. public function testNestedSelect() {
  3599. $result = $this->Form->select(
  3600. 'Model.field',
  3601. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3602. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3603. )), array('empty' => false)
  3604. );
  3605. $expected = array(
  3606. 'select' => array('name' => 'Model[field]'),
  3607. array('option' => array('value' => 1)),
  3608. 'One',
  3609. '/option',
  3610. array('option' => array('value' => 2)),
  3611. 'Two',
  3612. '/option',
  3613. array('optgroup' => array('label' => 'Three')),
  3614. array('option' => array('value' => 3)),
  3615. 'Three',
  3616. '/option',
  3617. array('option' => array('value' => 4)),
  3618. 'Four',
  3619. '/option',
  3620. array('option' => array('value' => 5)),
  3621. 'Five',
  3622. '/option',
  3623. '/optgroup',
  3624. '/select'
  3625. );
  3626. $this->assertTags($result, $expected);
  3627. }
  3628. /**
  3629. * testSelectMultiple method
  3630. *
  3631. * test generation of multiple select elements
  3632. *
  3633. * @return void
  3634. */
  3635. public function testSelectMultiple() {
  3636. $options = array('first', 'second', 'third');
  3637. $result = $this->Form->select(
  3638. 'Model.multi_field',
  3639. $options,
  3640. array('multiple' => true)
  3641. );
  3642. $expected = array(
  3643. 'input' => array(
  3644. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3645. ),
  3646. 'select' => array(
  3647. 'name' => 'Model[multi_field][]',
  3648. 'multiple' => 'multiple'
  3649. ),
  3650. array('option' => array('value' => '0')),
  3651. 'first',
  3652. '/option',
  3653. array('option' => array('value' => '1')),
  3654. 'second',
  3655. '/option',
  3656. array('option' => array('value' => '2')),
  3657. 'third',
  3658. '/option',
  3659. '/select'
  3660. );
  3661. $this->assertTags($result, $expected);
  3662. $result = $this->Form->select(
  3663. 'Model.multi_field',
  3664. $options,
  3665. array('multiple' => 'multiple')
  3666. );
  3667. $this->assertTags($result, $expected);
  3668. }
  3669. /**
  3670. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  3671. *
  3672. * @return void
  3673. */
  3674. public function testCheckboxZeroValue() {
  3675. $this->markTestIncomplete('Need to revisit once models work again.');
  3676. $result = $this->Form->input('User.get_spam', array(
  3677. 'type' => 'checkbox',
  3678. 'value' => '0',
  3679. 'hiddenField' => '1',
  3680. ));
  3681. $expected = array(
  3682. 'div' => array('class' => 'input checkbox'),
  3683. array('input' => array(
  3684. 'type' => 'hidden', 'name' => 'data[User][get_spam]',
  3685. 'value' => '1', 'id' => 'UserGetSpam_'
  3686. )),
  3687. array('input' => array(
  3688. 'type' => 'checkbox', 'name' => 'data[User][get_spam]',
  3689. 'value' => '0', 'id' => 'UserGetSpam'
  3690. )),
  3691. 'label' => array('for' => 'UserGetSpam'),
  3692. 'Get Spam',
  3693. '/label',
  3694. '/div'
  3695. );
  3696. $this->assertTags($result, $expected);
  3697. }
  3698. /**
  3699. * test generation of habtm select boxes.
  3700. *
  3701. * @return void
  3702. */
  3703. public function testHabtmSelectBox() {
  3704. $this->markTestIncomplete('Need to revisit once models work again.');
  3705. $this->View->viewVars['contactTags'] = array(
  3706. 1 => 'blue',
  3707. 2 => 'red',
  3708. 3 => 'green'
  3709. );
  3710. $this->Form->request->data = array(
  3711. 'Contact' => array(),
  3712. 'ContactTag' => array(
  3713. array(
  3714. 'id' => '1',
  3715. 'name' => 'blue'
  3716. ),
  3717. array(
  3718. 'id' => 3,
  3719. 'name' => 'green'
  3720. )
  3721. )
  3722. );
  3723. $this->Form->create('Contact');
  3724. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3725. $expected = array(
  3726. 'input' => array(
  3727. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3728. ),
  3729. 'select' => array(
  3730. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  3731. 'multiple' => 'multiple'
  3732. ),
  3733. array('option' => array('value' => '1', 'selected' => 'selected')),
  3734. 'blue',
  3735. '/option',
  3736. array('option' => array('value' => '2')),
  3737. 'red',
  3738. '/option',
  3739. array('option' => array('value' => '3', 'selected' => 'selected')),
  3740. 'green',
  3741. '/option',
  3742. '/select'
  3743. );
  3744. $this->assertTags($result, $expected);
  3745. // make sure only 50 is selected, and not 50f5c0cf
  3746. $this->View->viewVars['contactTags'] = array(
  3747. '1' => 'blue',
  3748. '50f5c0cf' => 'red',
  3749. '50' => 'green'
  3750. );
  3751. $this->Form->request->data = array(
  3752. 'Contact' => array(),
  3753. 'ContactTag' => array(
  3754. array(
  3755. 'id' => 1,
  3756. 'name' => 'blue'
  3757. ),
  3758. array(
  3759. 'id' => 50,
  3760. 'name' => 'green'
  3761. )
  3762. )
  3763. );
  3764. $this->Form->create('Contact');
  3765. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3766. $expected = array(
  3767. 'input' => array(
  3768. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3769. ),
  3770. 'select' => array(
  3771. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  3772. 'multiple' => 'multiple'
  3773. ),
  3774. array('option' => array('value' => '1', 'selected' => 'selected')),
  3775. 'blue',
  3776. '/option',
  3777. array('option' => array('value' => '50f5c0cf')),
  3778. 'red',
  3779. '/option',
  3780. array('option' => array('value' => '50', 'selected' => 'selected')),
  3781. 'green',
  3782. '/option',
  3783. '/select'
  3784. );
  3785. $this->assertTags($result, $expected);
  3786. }
  3787. /**
  3788. * test generation of multi select elements in checkbox format
  3789. *
  3790. * @return void
  3791. */
  3792. public function testSelectMultipleCheckboxes() {
  3793. $result = $this->Form->select(
  3794. 'Model.multi_field',
  3795. array('first', 'second', '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' => '0', 'id' => 'model-multi-field-0'
  3806. )),
  3807. array('label' => array('for' => 'model-multi-field-0')),
  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' => '1', 'id' => 'model-multi-field-1'
  3815. )),
  3816. array('label' => array('for' => 'model-multi-field-1')),
  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' => '2', 'id' => 'model-multi-field-2'
  3824. )),
  3825. array('label' => array('for' => 'model-multi-field-2')),
  3826. 'third',
  3827. '/label',
  3828. '/div'
  3829. );
  3830. $this->assertTags($result, $expected);
  3831. $result = $this->Form->select(
  3832. 'Model.multi_field',
  3833. array('a+' => 'first', 'a++' => 'second', 'a+++' => '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+', 'id' => 'model-multi-field-a+'
  3844. )),
  3845. array('label' => array('for' => 'model-multi-field-a+')),
  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++', 'id' => 'model-multi-field-a++'
  3853. )),
  3854. array('label' => array('for' => 'model-multi-field-a++')),
  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+++', 'id' => 'model-multi-field-a+++'
  3862. )),
  3863. array('label' => array('for' => 'model-multi-field-a+++')),
  3864. 'third',
  3865. '/label',
  3866. '/div'
  3867. );
  3868. $this->assertTags($result, $expected);
  3869. $result = $this->Form->select(
  3870. 'Model.multi_field',
  3871. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  3872. array('multiple' => 'checkbox')
  3873. );
  3874. $expected = array(
  3875. 'input' => array(
  3876. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3877. ),
  3878. array('div' => array('class' => 'checkbox')),
  3879. array('input' => array(
  3880. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3881. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  3882. )),
  3883. array('label' => array('for' => 'model-multi-field-a-b')),
  3884. 'first',
  3885. '/label',
  3886. '/div',
  3887. array('div' => array('class' => 'checkbox')),
  3888. array('input' => array(
  3889. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3890. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  3891. )),
  3892. array('label' => array('for' => 'model-multi-field-a-b1')),
  3893. 'second',
  3894. '/label',
  3895. '/div',
  3896. array('div' => array('class' => 'checkbox')),
  3897. array('input' => array(
  3898. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3899. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  3900. )),
  3901. array('label' => array('for' => 'model-multi-field-a-b2')),
  3902. 'third',
  3903. '/label',
  3904. '/div'
  3905. );
  3906. $this->assertTags($result, $expected);
  3907. }
  3908. /**
  3909. * Ensure that multiCheckbox reads from the request data.
  3910. *
  3911. * @return void
  3912. */
  3913. public function testSelectMultipleCheckboxRequestData() {
  3914. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3915. $result = $this->Form->select(
  3916. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3917. );
  3918. $expected = array(
  3919. 'input' => array(
  3920. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  3921. ),
  3922. array('div' => array('class' => 'checkbox')),
  3923. array('input' => array(
  3924. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3925. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  3926. )),
  3927. array('label' => array('for' => 'model-tags-1', 'class' => 'selected')),
  3928. 'first',
  3929. '/label',
  3930. '/div',
  3931. array('div' => array('class' => 'checkbox')),
  3932. array('input' => array(
  3933. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3934. 'value' => 'Array', 'id' => 'model-tags-array'
  3935. )),
  3936. array('label' => array('for' => 'model-tags-array')),
  3937. 'Array',
  3938. '/label',
  3939. '/div'
  3940. );
  3941. $this->assertTags($result, $expected);
  3942. }
  3943. /**
  3944. * Checks the security hash array generated for multiple-input checkbox elements
  3945. *
  3946. * @return void
  3947. */
  3948. public function testSelectMultipleCheckboxSecurity() {
  3949. $this->Form->request->params['_Token'] = 'testKey';
  3950. $this->assertEquals(array(), $this->Form->fields);
  3951. $result = $this->Form->select(
  3952. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3953. array('multiple' => 'checkbox')
  3954. );
  3955. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3956. $result = $this->Form->secure($this->Form->fields);
  3957. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3958. $this->assertRegExp('/"' . $key . '"/', $result);
  3959. }
  3960. /**
  3961. * Multiple select elements should always be secured as they always participate
  3962. * in the POST data.
  3963. *
  3964. * @return void
  3965. */
  3966. public function testSelectMultipleSecureWithNoOptions() {
  3967. $this->assertEquals(array(), $this->Form->fields);
  3968. $this->Form->select(
  3969. 'Model.select',
  3970. array(),
  3971. array('multiple' => true)
  3972. );
  3973. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3974. }
  3975. /**
  3976. * When a select box has no options it should not be added to the fields list
  3977. * as it always fail post validation.
  3978. *
  3979. * @return void
  3980. */
  3981. public function testSelectNoSecureWithNoOptions() {
  3982. $this->Form->request->params['_Token'] = 'testkey';
  3983. $this->assertEquals([], $this->Form->fields);
  3984. $this->Form->select(
  3985. 'Model.select',
  3986. []
  3987. );
  3988. $this->assertEquals([], $this->Form->fields);
  3989. $this->Form->select(
  3990. 'Model.user_id',
  3991. [],
  3992. ['empty' => true]
  3993. );
  3994. $this->assertEquals(array('Model.user_id'), $this->Form->fields);
  3995. }
  3996. /**
  3997. * testInputMultipleCheckboxes method
  3998. *
  3999. * test input() resulting in multi select elements being generated.
  4000. *
  4001. * @return void
  4002. */
  4003. public function testInputMultipleCheckboxes() {
  4004. $this->markTestIncomplete('Need to revisit once models work again.');
  4005. $result = $this->Form->input('Model.multi_field', array(
  4006. 'options' => array('first', 'second', 'third'),
  4007. 'multiple' => 'checkbox'
  4008. ));
  4009. $expected = array(
  4010. array('div' => array('class' => 'input select')),
  4011. array('label' => array('for' => 'ModelMultiField')),
  4012. 'Multi Field',
  4013. '/label',
  4014. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4015. array('div' => array('class' => 'checkbox')),
  4016. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4017. array('label' => array('for' => 'ModelMultiField0')),
  4018. 'first',
  4019. '/label',
  4020. '/div',
  4021. array('div' => array('class' => 'checkbox')),
  4022. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4023. array('label' => array('for' => 'ModelMultiField1')),
  4024. 'second',
  4025. '/label',
  4026. '/div',
  4027. array('div' => array('class' => 'checkbox')),
  4028. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4029. array('label' => array('for' => 'ModelMultiField2')),
  4030. 'third',
  4031. '/label',
  4032. '/div',
  4033. '/div'
  4034. );
  4035. $this->assertTags($result, $expected);
  4036. $result = $this->Form->input('Model.multi_field', array(
  4037. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4038. 'multiple' => 'checkbox'
  4039. ));
  4040. $expected = array(
  4041. array('div' => array('class' => 'input select')),
  4042. array('label' => array('for' => 'ModelMultiField')),
  4043. 'Multi Field',
  4044. '/label',
  4045. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4046. array('div' => array('class' => 'checkbox')),
  4047. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4048. array('label' => array('for' => 'ModelMultiFieldA')),
  4049. 'first',
  4050. '/label',
  4051. '/div',
  4052. array('div' => array('class' => 'checkbox')),
  4053. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4054. array('label' => array('for' => 'ModelMultiFieldB')),
  4055. 'second',
  4056. '/label',
  4057. '/div',
  4058. array('div' => array('class' => 'checkbox')),
  4059. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4060. array('label' => array('for' => 'ModelMultiFieldC')),
  4061. 'third',
  4062. '/label',
  4063. '/div',
  4064. '/div'
  4065. );
  4066. $this->assertTags($result, $expected);
  4067. $result = $this->Form->input('Model.multi_field', array(
  4068. 'options' => array('1' => 'first'),
  4069. 'multiple' => 'checkbox',
  4070. 'label' => false,
  4071. 'div' => false
  4072. ));
  4073. $expected = array(
  4074. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4075. array('div' => array('class' => 'checkbox')),
  4076. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4077. array('label' => array('for' => 'ModelMultiField1')),
  4078. 'first',
  4079. '/label',
  4080. '/div'
  4081. );
  4082. $this->assertTags($result, $expected);
  4083. $result = $this->Form->input('Model.multi_field', array(
  4084. 'options' => array('2' => 'second'),
  4085. 'multiple' => 'checkbox',
  4086. 'label' => false,
  4087. 'div' => false
  4088. ));
  4089. $expected = array(
  4090. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4091. array('div' => array('class' => 'checkbox')),
  4092. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4093. array('label' => array('for' => 'ModelMultiField2')),
  4094. 'second',
  4095. '/label',
  4096. '/div'
  4097. );
  4098. $this->assertTags($result, $expected);
  4099. }
  4100. /**
  4101. * testSelectHiddenFieldOmission method
  4102. *
  4103. * test that select() with 'hiddenField' => false omits the hidden field
  4104. *
  4105. * @return void
  4106. */
  4107. public function testSelectHiddenFieldOmission() {
  4108. $result = $this->Form->select('Model.multi_field',
  4109. array('first', 'second'),
  4110. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4111. );
  4112. $this->assertNotContains('type="hidden"', $result);
  4113. }
  4114. /**
  4115. * test that select() with multiple = checkbox works with overriding name attribute.
  4116. *
  4117. * @return void
  4118. */
  4119. public function testSelectCheckboxMultipleOverrideName() {
  4120. $result = $this->Form->select('category', ['1', '2'], [
  4121. 'multiple' => 'checkbox',
  4122. 'name' => 'fish',
  4123. ]);
  4124. $expected = array(
  4125. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => ''),
  4126. array('div' => array('class' => 'checkbox')),
  4127. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0')),
  4128. array('label' => array('for' => 'fish-0')), '1', '/label',
  4129. '/div',
  4130. array('div' => array('class' => 'checkbox')),
  4131. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1')),
  4132. array('label' => array('for' => 'fish-1')), '2', '/label',
  4133. '/div'
  4134. );
  4135. $this->assertTags($result, $expected);
  4136. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  4137. 'name' => 'fish',
  4138. ]);
  4139. $this->assertTags($result, $expected);
  4140. }
  4141. /**
  4142. * testCheckbox method
  4143. *
  4144. * Test generation of checkboxes
  4145. *
  4146. * @return void
  4147. */
  4148. public function testCheckbox() {
  4149. $result = $this->Form->checkbox('Model.field');
  4150. $expected = array(
  4151. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  4152. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'))
  4153. );
  4154. $this->assertTags($result, $expected);
  4155. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4156. $expected = array(
  4157. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  4158. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => 'myvalue', 'id' => 'theID'))
  4159. );
  4160. $this->assertTags($result, $expected);
  4161. }
  4162. /**
  4163. * testCheckboxDefaultValue method
  4164. *
  4165. * Test default value setting on checkbox() method
  4166. *
  4167. * @return void
  4168. */
  4169. public function testCheckboxDefaultValue() {
  4170. $this->Form->request->data['Model']['field'] = false;
  4171. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  4172. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  4173. unset($this->Form->request->data['Model']['field']);
  4174. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  4175. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  4176. $this->Form->request->data['Model']['field'] = true;
  4177. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  4178. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  4179. unset($this->Form->request->data['Model']['field']);
  4180. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  4181. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  4182. }
  4183. /**
  4184. * Test checkbox being checked or having errors.
  4185. *
  4186. * @return void
  4187. */
  4188. public function testCheckboxCheckedAndError() {
  4189. $this->article['errors'] = [
  4190. 'published' => true
  4191. ];
  4192. $this->Form->request->data['published'] = 'myvalue';
  4193. $this->Form->create($this->article);
  4194. $result = $this->Form->checkbox('published', array('id' => 'theID', 'value' => 'myvalue'));
  4195. $expected = array(
  4196. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4197. array('input' => array(
  4198. 'type' => 'checkbox',
  4199. 'name' => 'published',
  4200. 'value' => 'myvalue',
  4201. 'id' => 'theID',
  4202. 'checked' => 'checked',
  4203. 'class' => 'form-error'
  4204. ))
  4205. );
  4206. $this->assertTags($result, $expected);
  4207. $this->Form->request->data['published'] = '';
  4208. $result = $this->Form->checkbox('published');
  4209. $expected = array(
  4210. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4211. array('input' => array('type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error'))
  4212. );
  4213. $this->assertTags($result, $expected);
  4214. }
  4215. /**
  4216. * test checkbox() with a custom name attribute
  4217. *
  4218. * @return void
  4219. */
  4220. public function testCheckboxCustomNameAttribute() {
  4221. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4222. $expected = array(
  4223. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0'),
  4224. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1'))
  4225. );
  4226. $this->assertTags($result, $expected);
  4227. }
  4228. /**
  4229. * Test that the hidden input for checkboxes can be omitted or set to a
  4230. * specific value.
  4231. *
  4232. * @return void
  4233. */
  4234. public function testCheckboxHiddenField() {
  4235. $result = $this->Form->checkbox('UserForm.something', array(
  4236. 'hiddenField' => false
  4237. ));
  4238. $expected = array(
  4239. 'input' => array(
  4240. 'type' => 'checkbox',
  4241. 'name' => 'UserForm[something]',
  4242. 'value' => '1'
  4243. ),
  4244. );
  4245. $this->assertTags($result, $expected);
  4246. $result = $this->Form->checkbox('UserForm.something', array(
  4247. 'value' => 'Y',
  4248. 'hiddenField' => 'N',
  4249. ));
  4250. $expected = array(
  4251. array('input' => array(
  4252. 'type' => 'hidden', 'name' => 'UserForm[something]',
  4253. 'value' => 'N'
  4254. )),
  4255. array('input' => array(
  4256. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  4257. 'value' => 'Y'
  4258. )),
  4259. );
  4260. $this->assertTags($result, $expected);
  4261. }
  4262. /**
  4263. * testDateTime method
  4264. *
  4265. * Test generation of date/time select elements
  4266. *
  4267. * @return void
  4268. */
  4269. public function testDateTime() {
  4270. extract($this->dateRegex);
  4271. $result = $this->Form->dateTime('Contact.date', array('empty' => false));
  4272. $now = strtotime('now');
  4273. $expected = array(
  4274. array('select' => array('name' => 'Contact[date][month]')),
  4275. $monthsRegex,
  4276. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4277. date('F', $now),
  4278. '/option',
  4279. '*/select',
  4280. array('select' => array('name' => 'Contact[date][day]')),
  4281. $daysRegex,
  4282. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4283. date('j', $now),
  4284. '/option',
  4285. '*/select',
  4286. array('select' => array('name' => 'Contact[date][year]')),
  4287. $yearsRegex,
  4288. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4289. date('Y', $now),
  4290. '/option',
  4291. '*/select',
  4292. array('select' => array('name' => 'Contact[date][hour]')),
  4293. $hoursRegex,
  4294. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4295. date('g', $now),
  4296. '/option',
  4297. '*/select',
  4298. array('select' => array('name' => 'Contact[date][minute]')),
  4299. $minutesRegex,
  4300. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4301. date('i', $now),
  4302. '/option',
  4303. '*/select',
  4304. array('select' => array('name' => 'Contact[date][meridian]')),
  4305. $meridianRegex,
  4306. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4307. date('a', $now),
  4308. '/option',
  4309. '*/select'
  4310. );
  4311. $this->assertTags($result, $expected);
  4312. }
  4313. /**
  4314. * Test empty defaulting to true for datetime.
  4315. *
  4316. * @return void
  4317. */
  4318. public function testDatetimeEmpty() {
  4319. extract($this->dateRegex);
  4320. $now = strtotime('now');
  4321. $result = $this->Form->dateTime('Contact.date', array(
  4322. 'empty' => true,
  4323. ));
  4324. $expected = array(
  4325. array('select' => array('name' => 'Contact[date][month]')),
  4326. $monthsRegex,
  4327. array('option' => array('value' => '')),
  4328. '/option',
  4329. '*/select',
  4330. array('select' => array('name' => 'Contact[date][day]')),
  4331. $daysRegex,
  4332. array('option' => array('value' => '')),
  4333. '/option',
  4334. '*/select',
  4335. array('select' => array('name' => 'Contact[date][year]')),
  4336. $yearsRegex,
  4337. array('option' => array('value' => '')),
  4338. '/option',
  4339. '*/select',
  4340. array('select' => array('name' => 'Contact[date][hour]')),
  4341. $hoursRegex,
  4342. array('option' => array('value' => '')),
  4343. '/option',
  4344. '*/select',
  4345. array('select' => array('name' => 'Contact[date][minute]')),
  4346. $minutesRegex,
  4347. array('option' => array('value' => '')),
  4348. '/option',
  4349. '*/select',
  4350. array('select' => array('name' => 'Contact[date][meridian]')),
  4351. $meridianRegex,
  4352. array('option' => array('value' => '')),
  4353. '/option',
  4354. '*/select'
  4355. );
  4356. $this->assertTags($result, $expected);
  4357. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4358. }
  4359. /**
  4360. * Test datetime with interval option.
  4361. *
  4362. * @return void
  4363. */
  4364. public function testDatetimeMinuteInterval() {
  4365. extract($this->dateRegex);
  4366. $now = strtotime('now');
  4367. $result = $this->Form->dateTime('Contact.date', array(
  4368. 'interval' => 5,
  4369. 'value' => ''
  4370. ));
  4371. $expected = array(
  4372. array('select' => array('name' => 'Contact[date][month]')),
  4373. $monthsRegex,
  4374. array('option' => array('selected' => 'selected', 'value' => '')),
  4375. '/option',
  4376. '*/select',
  4377. array('select' => array('name' => 'Contact[date][day]')),
  4378. $daysRegex,
  4379. array('option' => array('selected' => 'selected', 'value' => '')),
  4380. '/option',
  4381. '*/select',
  4382. array('select' => array('name' => 'Contact[date][year]')),
  4383. $yearsRegex,
  4384. array('option' => array('selected' => 'selected', 'value' => '')),
  4385. '/option',
  4386. '*/select',
  4387. array('select' => array('name' => 'Contact[date][hour]')),
  4388. $hoursRegex,
  4389. array('option' => array('selected' => 'selected', 'value' => '')),
  4390. '/option',
  4391. '*/select',
  4392. array('select' => array('name' => 'Contact[date][minute]')),
  4393. $minutesRegex,
  4394. array('option' => array('selected' => 'selected', 'value' => '')),
  4395. '/option',
  4396. array('option' => array('value' => '00')),
  4397. '00',
  4398. '/option',
  4399. array('option' => array('value' => '05')),
  4400. '05',
  4401. '/option',
  4402. array('option' => array('value' => '10')),
  4403. '10',
  4404. '/option',
  4405. '*/select',
  4406. );
  4407. $this->assertTags($result, $expected);
  4408. }
  4409. /**
  4410. * Test dateTime with rounding
  4411. *
  4412. * @return void
  4413. */
  4414. public function testDateTimeRounding() {
  4415. $this->Form->request->data['Contact'] = array(
  4416. 'date' => array(
  4417. 'day' => '13',
  4418. 'month' => '12',
  4419. 'year' => '2010',
  4420. 'hour' => '04',
  4421. 'minute' => '19',
  4422. 'meridian' => 'AM'
  4423. )
  4424. );
  4425. $result = $this->Form->dateTime('Contact.date', array('interval' => 15));
  4426. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4427. $result = $this->Form->dateTime('Contact.date', array('interval' => 15, 'round' => 'up'));
  4428. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  4429. $result = $this->Form->dateTime('Contact.date', array('interval' => 5, 'round' => 'down'));
  4430. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4431. }
  4432. /**
  4433. * test that datetime() and default values work.
  4434. *
  4435. * @return void
  4436. */
  4437. public function testDatetimeWithDefault() {
  4438. $this->markTestIncomplete('Need to revisit soon.');
  4439. $result = $this->Form->dateTime('Contact.updated', array('value' => '2009-06-01 11:15:30'));
  4440. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4441. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4442. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4443. $result = $this->Form->dateTime('Contact.updated', array(
  4444. 'default' => '2009-06-01 11:15:30'
  4445. ));
  4446. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4447. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4448. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4449. }
  4450. /**
  4451. * testDateTime all zeros
  4452. *
  4453. * @return void
  4454. */
  4455. public function testDateTimeAllZeros() {
  4456. $result = $this->Form->dateTime('Contact.date', array(
  4457. 'timeFormat' => false,
  4458. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  4459. 'value' => '0000-00-00'
  4460. ));
  4461. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  4462. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  4463. }
  4464. /**
  4465. * testDateTimeEmptyAsArray
  4466. *
  4467. * @return void
  4468. */
  4469. public function testDateTimeEmptyAsArray() {
  4470. $result = $this->Form->dateTime('Contact.date', array(
  4471. 'empty' => array(
  4472. 'day' => 'DAY',
  4473. 'month' => 'MONTH',
  4474. 'year' => 'YEAR',
  4475. 'hour' => 'HOUR',
  4476. 'minute' => 'MINUTE',
  4477. 'meridian' => false
  4478. )
  4479. ));
  4480. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4481. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4482. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4483. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4484. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4485. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4486. $result = $this->Form->dateTime('Contact.date', array(
  4487. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4488. ));
  4489. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4490. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4491. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4492. }
  4493. /**
  4494. * testFormDateTimeMulti method
  4495. *
  4496. * test multiple datetime element generation
  4497. *
  4498. * @return void
  4499. */
  4500. public function testFormDateTimeMulti() {
  4501. extract($this->dateRegex);
  4502. $result = $this->Form->dateTime('Contact.1.updated');
  4503. $this->assertContains('Contact[1][updated][month]', $result);
  4504. $this->assertContains('Contact[1][updated][day]', $result);
  4505. $this->assertContains('Contact[1][updated][year]', $result);
  4506. $this->assertContains('Contact[1][updated][hour]', $result);
  4507. $this->assertContains('Contact[1][updated][minute]', $result);
  4508. $this->assertContains('Contact[1][updated][meridian]', $result);
  4509. }
  4510. /**
  4511. * When changing the date format, the label should always focus the first select box when
  4512. * clicked.
  4513. *
  4514. * @return void
  4515. */
  4516. public function testDateTimeLabelIdMatchesFirstInput() {
  4517. $this->markTestIncomplete('Need to revisit once models work again.');
  4518. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4519. $this->assertContains('label for="ModelDateMonth"', $result);
  4520. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4521. $this->assertContains('label for="ModelDateDay"', $result);
  4522. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4523. $this->assertContains('label for="ModelDateYear"', $result);
  4524. }
  4525. /**
  4526. * testMonth method
  4527. *
  4528. * @return void
  4529. */
  4530. public function testMonth() {
  4531. $result = $this->Form->month('Model.field', ['value' => '']);
  4532. $expected = array(
  4533. array('select' => array('name' => 'Model[field][month]')),
  4534. array('option' => array('value' => '', 'selected' => 'selected')),
  4535. '/option',
  4536. array('option' => array('value' => '01')),
  4537. date('F', strtotime('2008-01-01 00:00:00')),
  4538. '/option',
  4539. array('option' => array('value' => '02')),
  4540. date('F', strtotime('2008-02-01 00:00:00')),
  4541. '/option',
  4542. '*/select',
  4543. );
  4544. $this->assertTags($result, $expected);
  4545. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  4546. $expected = array(
  4547. array('select' => array('name' => 'Model[field][month]')),
  4548. array('option' => array('selected' => 'selected', 'value' => '')),
  4549. '/option',
  4550. array('option' => array('value' => '01')),
  4551. date('F', strtotime('2008-01-01 00:00:00')),
  4552. '/option',
  4553. array('option' => array('value' => '02')),
  4554. date('F', strtotime('2008-02-01 00:00:00')),
  4555. '/option',
  4556. '*/select',
  4557. );
  4558. $this->assertTags($result, $expected);
  4559. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  4560. $expected = array(
  4561. array('select' => array('name' => 'Model[field][month]')),
  4562. array('option' => array('selected' => 'selected', 'value' => '')),
  4563. '/option',
  4564. array('option' => array('value' => '01')),
  4565. '1',
  4566. '/option',
  4567. array('option' => array('value' => '02')),
  4568. '2',
  4569. '/option',
  4570. '*/select',
  4571. );
  4572. $this->assertTags($result, $expected);
  4573. $monthNames = [
  4574. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4575. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  4576. ];
  4577. $result = $this->Form->month('Model.field', array('value' => '1', 'monthNames' => $monthNames));
  4578. $expected = array(
  4579. array('select' => array('name' => 'Model[field][month]')),
  4580. array('option' => array('value' => '')),
  4581. '/option',
  4582. array('option' => array('value' => '01', 'selected' => 'selected')),
  4583. 'Jan',
  4584. '/option',
  4585. array('option' => array('value' => '02')),
  4586. 'Feb',
  4587. '/option',
  4588. '*/select',
  4589. );
  4590. $this->assertTags($result, $expected);
  4591. $this->Form->request->data['Project']['release'] = '2050-02-10';
  4592. $result = $this->Form->month('Project.release');
  4593. $expected = array(
  4594. array('select' => array('name' => 'Project[release][month]')),
  4595. array('option' => array('value' => '')),
  4596. '/option',
  4597. array('option' => array('value' => '01')),
  4598. 'January',
  4599. '/option',
  4600. array('option' => array('value' => '02', 'selected' => 'selected')),
  4601. 'February',
  4602. '/option',
  4603. '*/select',
  4604. );
  4605. $this->assertTags($result, $expected);
  4606. }
  4607. /**
  4608. * testDay method
  4609. *
  4610. * @return void
  4611. */
  4612. public function testDay() {
  4613. extract($this->dateRegex);
  4614. $result = $this->Form->day('Model.field', array('value' => false));
  4615. $expected = array(
  4616. array('select' => array('name' => 'Model[field][day]')),
  4617. array('option' => array('selected' => 'selected', 'value' => '')),
  4618. '/option',
  4619. array('option' => array('value' => '01')),
  4620. '1',
  4621. '/option',
  4622. array('option' => array('value' => '02')),
  4623. '2',
  4624. '/option',
  4625. $daysRegex,
  4626. '/select',
  4627. );
  4628. $this->assertTags($result, $expected);
  4629. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4630. $result = $this->Form->day('Model.field');
  4631. $expected = array(
  4632. array('select' => array('name' => 'Model[field][day]')),
  4633. array('option' => array('value' => '')),
  4634. '/option',
  4635. array('option' => array('value' => '01')),
  4636. '1',
  4637. '/option',
  4638. array('option' => array('value' => '02')),
  4639. '2',
  4640. '/option',
  4641. $daysRegex,
  4642. array('option' => array('value' => '10', 'selected' => 'selected')),
  4643. '10',
  4644. '/option',
  4645. $daysRegex,
  4646. '/select',
  4647. );
  4648. $this->assertTags($result, $expected);
  4649. $this->Form->request->data['Model']['field'] = '';
  4650. $result = $this->Form->day('Model.field', array('value' => '10'));
  4651. $expected = array(
  4652. array('select' => array('name' => 'Model[field][day]')),
  4653. array('option' => array('value' => '')),
  4654. '/option',
  4655. array('option' => array('value' => '01')),
  4656. '1',
  4657. '/option',
  4658. array('option' => array('value' => '02')),
  4659. '2',
  4660. '/option',
  4661. $daysRegex,
  4662. array('option' => array('value' => '10', 'selected' => 'selected')),
  4663. '10',
  4664. '/option',
  4665. $daysRegex,
  4666. '/select',
  4667. );
  4668. $this->assertTags($result, $expected);
  4669. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4670. $result = $this->Form->day('Project.release');
  4671. $expected = array(
  4672. array('select' => array('name' => 'Project[release][day]')),
  4673. array('option' => array('value' => '')),
  4674. '/option',
  4675. array('option' => array('value' => '01')),
  4676. '1',
  4677. '/option',
  4678. array('option' => array('value' => '02')),
  4679. '2',
  4680. '/option',
  4681. $daysRegex,
  4682. array('option' => array('value' => '10', 'selected' => 'selected')),
  4683. '10',
  4684. '/option',
  4685. $daysRegex,
  4686. '/select',
  4687. );
  4688. $this->assertTags($result, $expected);
  4689. }
  4690. /**
  4691. * testMinute method
  4692. *
  4693. * @return void
  4694. */
  4695. public function testMinute() {
  4696. extract($this->dateRegex);
  4697. $result = $this->Form->minute('Model.field', ['value' => '']);
  4698. $expected = array(
  4699. array('select' => array('name' => 'Model[field][minute]')),
  4700. array('option' => array('selected' => 'selected', 'value' => '')),
  4701. '/option',
  4702. array('option' => array('value' => '00')),
  4703. '00',
  4704. '/option',
  4705. array('option' => array('value' => '01')),
  4706. '01',
  4707. '/option',
  4708. array('option' => array('value' => '02')),
  4709. '02',
  4710. '/option',
  4711. $minutesRegex,
  4712. '/select',
  4713. );
  4714. $this->assertTags($result, $expected);
  4715. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4716. $result = $this->Form->minute('Model.field');
  4717. $expected = array(
  4718. array('select' => array('name' => 'Model[field][minute]')),
  4719. array('option' => array('value' => '')),
  4720. '/option',
  4721. array('option' => array('value' => '00')),
  4722. '00',
  4723. '/option',
  4724. array('option' => array('value' => '01')),
  4725. '01',
  4726. '/option',
  4727. array('option' => array('value' => '02')),
  4728. '02',
  4729. '/option',
  4730. $minutesRegex,
  4731. array('option' => array('value' => '12', 'selected' => 'selected')),
  4732. '12',
  4733. '/option',
  4734. $minutesRegex,
  4735. '/select',
  4736. );
  4737. $this->assertTags($result, $expected);
  4738. $this->Form->request->data['Model']['field'] = '';
  4739. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4740. $expected = array(
  4741. array('select' => array('name' => 'Model[field][minute]')),
  4742. array('option' => array('selected' => 'selected', 'value' => '')),
  4743. '/option',
  4744. array('option' => array('value' => '00')),
  4745. '00',
  4746. '/option',
  4747. array('option' => array('value' => '05')),
  4748. '05',
  4749. '/option',
  4750. array('option' => array('value' => '10')),
  4751. '10',
  4752. '/option',
  4753. $minutesRegex,
  4754. '/select',
  4755. );
  4756. $this->assertTags($result, $expected);
  4757. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  4758. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4759. $expected = array(
  4760. array('select' => array('name' => 'Model[field][minute]')),
  4761. array('option' => array('value' => '')),
  4762. '/option',
  4763. array('option' => array('value' => '00')),
  4764. '00',
  4765. '/option',
  4766. array('option' => array('value' => '05')),
  4767. '05',
  4768. '/option',
  4769. array('option' => array('value' => '10', 'selected' => 'selected')),
  4770. '10',
  4771. '/option',
  4772. $minutesRegex,
  4773. '/select',
  4774. );
  4775. $this->assertTags($result, $expected);
  4776. }
  4777. /**
  4778. * Test generating an input for the meridian.
  4779. *
  4780. * @return void
  4781. */
  4782. public function testMeridian() {
  4783. extract($this->dateRegex);
  4784. $now = time();
  4785. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  4786. $expected = [
  4787. array('select' => array('name' => 'Model[field][meridian]')),
  4788. array('option' => array('value' => '')),
  4789. '/option',
  4790. $meridianRegex,
  4791. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4792. date('a', $now),
  4793. '/option',
  4794. '*/select'
  4795. ];
  4796. $this->assertTags($result, $expected);
  4797. }
  4798. /**
  4799. * testHour method
  4800. *
  4801. * @return void
  4802. */
  4803. public function testHour() {
  4804. extract($this->dateRegex);
  4805. $result = $this->Form->hour('Model.field', ['value' => '']);
  4806. $expected = array(
  4807. array('select' => array('name' => 'Model[field][hour]')),
  4808. array('option' => array('selected' => 'selected', 'value' => '')),
  4809. '/option',
  4810. array('option' => array('value' => '01')),
  4811. '1',
  4812. '/option',
  4813. array('option' => array('value' => '02')),
  4814. '2',
  4815. '/option',
  4816. $hoursRegex,
  4817. '/select',
  4818. );
  4819. $this->assertTags($result, $expected);
  4820. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4821. $result = $this->Form->hour('Model.field', ['format' => 12]);
  4822. $expected = array(
  4823. array('select' => array('name' => 'Model[field][hour]')),
  4824. array('option' => array('value' => '')),
  4825. '/option',
  4826. array('option' => array('value' => '01')),
  4827. '1',
  4828. '/option',
  4829. array('option' => array('value' => '02')),
  4830. '2',
  4831. '/option',
  4832. $hoursRegex,
  4833. array('option' => array('value' => '12', 'selected' => 'selected')),
  4834. '12',
  4835. '/option',
  4836. '/select',
  4837. );
  4838. $this->assertTags($result, $expected);
  4839. $this->Form->request->data['Model']['field'] = '';
  4840. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => '23'));
  4841. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  4842. $result = $this->Form->hour('Model.field', array('format' => 12, 'value' => '23'));
  4843. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  4844. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4845. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4846. $expected = array(
  4847. array('select' => array('name' => 'Model[field][hour]')),
  4848. array('option' => array('value' => '')),
  4849. '/option',
  4850. array('option' => array('value' => '00', 'selected' => 'selected')),
  4851. '0',
  4852. '/option',
  4853. array('option' => array('value' => '01')),
  4854. '1',
  4855. '/option',
  4856. array('option' => array('value' => '02')),
  4857. '2',
  4858. '/option',
  4859. $hoursRegex,
  4860. '/select',
  4861. );
  4862. $this->assertTags($result, $expected);
  4863. unset($this->Form->request->data['Model']['field']);
  4864. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => 'now'));
  4865. $thisHour = date('H');
  4866. $optValue = date('G');
  4867. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  4868. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  4869. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4870. $expected = array(
  4871. array('select' => array('name' => 'Model[field][hour]')),
  4872. array('option' => array('value' => '')),
  4873. '/option',
  4874. array('option' => array('value' => '00')),
  4875. '0',
  4876. '/option',
  4877. array('option' => array('value' => '01', 'selected' => 'selected')),
  4878. '1',
  4879. '/option',
  4880. array('option' => array('value' => '02')),
  4881. '2',
  4882. '/option',
  4883. $hoursRegex,
  4884. '/select',
  4885. );
  4886. $this->assertTags($result, $expected);
  4887. }
  4888. /**
  4889. * testYear method
  4890. *
  4891. * @return void
  4892. */
  4893. public function testYear() {
  4894. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  4895. $expected = array(
  4896. array('select' => array('name' => 'Model[field][year]')),
  4897. array('option' => array('selected' => 'selected', 'value' => '')),
  4898. '/option',
  4899. array('option' => array('value' => '2007')),
  4900. '2007',
  4901. '/option',
  4902. array('option' => array('value' => '2006')),
  4903. '2006',
  4904. '/option',
  4905. '/select',
  4906. );
  4907. $this->assertTags($result, $expected);
  4908. $result = $this->Form->year('Model.field', [
  4909. 'value' => '',
  4910. 'minYear' => 2006,
  4911. 'maxYear' => 2007,
  4912. 'orderYear' => 'asc'
  4913. ]);
  4914. $expected = array(
  4915. array('select' => array('name' => 'Model[field][year]')),
  4916. array('option' => array('selected' => 'selected', 'value' => '')),
  4917. '/option',
  4918. array('option' => array('value' => '2006')),
  4919. '2006',
  4920. '/option',
  4921. array('option' => array('value' => '2007')),
  4922. '2007',
  4923. '/option',
  4924. '/select',
  4925. );
  4926. $this->assertTags($result, $expected);
  4927. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  4928. $result = $this->Form->year('Contact.published', [
  4929. 'empty' => false,
  4930. 'minYear' => 2006,
  4931. 'maxYear' => 2007,
  4932. ]);
  4933. $expected = array(
  4934. array('select' => array('name' => 'Contact[published][year]')),
  4935. array('option' => array('value' => '2007')),
  4936. '2007',
  4937. '/option',
  4938. array('option' => array('value' => '2006', 'selected' => 'selected')),
  4939. '2006',
  4940. '/option',
  4941. '/select',
  4942. );
  4943. $this->assertTags($result, $expected);
  4944. }
  4945. /**
  4946. * testYearAutoExpandRange method
  4947. *
  4948. * @return void
  4949. */
  4950. public function testYearAutoExpandRange() {
  4951. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  4952. $result = $this->Form->year('User.birthday');
  4953. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4954. $result = $matches[1];
  4955. $expected = range(date('Y') + 5, 1930);
  4956. $this->assertEquals($expected, $result);
  4957. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4958. $result = $this->Form->year('Project.release');
  4959. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4960. $result = $matches[1];
  4961. $expected = range(2050, date('Y') - 5);
  4962. $this->assertEquals($expected, $result);
  4963. $this->Form->request->data['Project']['release'] = '1881-10-10';
  4964. $result = $this->Form->year('Project.release', [
  4965. 'minYear' => 1890,
  4966. 'maxYear' => 1900
  4967. ]);
  4968. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4969. $result = $matches[1];
  4970. $expected = range(1900, 1881);
  4971. $this->assertEquals($expected, $result);
  4972. }
  4973. /**
  4974. * testInputDate method
  4975. *
  4976. * Test various inputs with type date and different dateFormat values.
  4977. * Failing to provide a dateFormat key should not error.
  4978. * It should simply not pre-select any value then.
  4979. *
  4980. * @return void
  4981. */
  4982. public function testInputDate() {
  4983. $this->markTestIncomplete('Need to revisit once models work again.');
  4984. $this->Form->request->data = array(
  4985. 'User' => array(
  4986. 'month_year' => array('month' => date('m')),
  4987. 'just_year' => array('month' => date('m')),
  4988. 'just_month' => array('year' => date('Y')),
  4989. 'just_day' => array('month' => date('m')),
  4990. )
  4991. );
  4992. $this->Form->create('User');
  4993. $result = $this->Form->input('month_year',
  4994. array(
  4995. 'label' => false,
  4996. 'div' => false,
  4997. 'type' => 'date',
  4998. 'dateFormat' => 'MY',
  4999. 'minYear' => 2006,
  5000. 'maxYear' => 2008
  5001. )
  5002. );
  5003. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  5004. $this->assertNotContains('value="2008" selected="selected"', $result);
  5005. $result = $this->Form->input('just_year',
  5006. array(
  5007. 'type' => 'date',
  5008. 'label' => false,
  5009. 'dateFormat' => 'Y',
  5010. 'minYear' => date('Y'),
  5011. 'maxYear' => date('Y', strtotime('+20 years'))
  5012. )
  5013. );
  5014. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  5015. $result = $this->Form->input('just_month',
  5016. array(
  5017. 'type' => 'date',
  5018. 'label' => false,
  5019. 'dateFormat' => 'M',
  5020. 'empty' => false,
  5021. )
  5022. );
  5023. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  5024. $result = $this->Form->input('just_day',
  5025. array(
  5026. 'type' => 'date',
  5027. 'label' => false,
  5028. 'dateFormat' => 'D',
  5029. 'empty' => false,
  5030. )
  5031. );
  5032. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  5033. }
  5034. /**
  5035. * testInputDateMaxYear method
  5036. *
  5037. * Let's say we want to only allow users born from 2006 to 2008 to register
  5038. * This being the first singup page, we still don't have any data
  5039. *
  5040. * @return void
  5041. */
  5042. public function testInputDateMaxYear() {
  5043. $this->markTestIncomplete('Need to revisit once models work again.');
  5044. $this->Form->request->data = array();
  5045. $this->Form->create('User');
  5046. $result = $this->Form->input('birthday',
  5047. array(
  5048. 'label' => false,
  5049. 'div' => false,
  5050. 'type' => 'date',
  5051. 'dateFormat' => 'DMY',
  5052. 'minYear' => 2006,
  5053. 'maxYear' => 2008
  5054. )
  5055. );
  5056. $this->assertContains('value="2008" selected="selected"', $result);
  5057. }
  5058. /**
  5059. * testTextArea method
  5060. *
  5061. * @return void
  5062. */
  5063. public function testTextArea() {
  5064. $this->Form->request->data = array('field' => 'some test data');
  5065. $result = $this->Form->textarea('field');
  5066. $expected = array(
  5067. 'textarea' => array('name' => 'field'),
  5068. 'some test data',
  5069. '/textarea',
  5070. );
  5071. $this->assertTags($result, $expected);
  5072. $result = $this->Form->textarea('user.bio');
  5073. $expected = array(
  5074. 'textarea' => array('name' => 'user[bio]'),
  5075. '/textarea',
  5076. );
  5077. $this->assertTags($result, $expected);
  5078. $this->Form->request->data = array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  5079. $result = $this->Form->textarea('field');
  5080. $expected = array(
  5081. 'textarea' => array('name' => 'field'),
  5082. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5083. '/textarea',
  5084. );
  5085. $this->assertTags($result, $expected);
  5086. $this->Form->request->data = [
  5087. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  5088. ];
  5089. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5090. $expected = array(
  5091. 'textarea' => array('name' => 'Model[field]'),
  5092. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5093. '/textarea',
  5094. );
  5095. $this->assertTags($result, $expected);
  5096. $result = $this->Form->textarea('0.OtherModel.field');
  5097. $expected = array(
  5098. 'textarea' => array('name' => '0[OtherModel][field]'),
  5099. '/textarea'
  5100. );
  5101. $this->assertTags($result, $expected);
  5102. }
  5103. /**
  5104. * testTextAreaWithStupidCharacters method
  5105. *
  5106. * test text area with non-ascii characters
  5107. *
  5108. * @return void
  5109. */
  5110. public function testTextAreaWithStupidCharacters() {
  5111. $result = $this->Form->textarea('Post.content', [
  5112. 'value' => "GREAT®",
  5113. 'rows' => '15',
  5114. 'cols' => '75'
  5115. ]);
  5116. $expected = [
  5117. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5118. 'GREAT®',
  5119. '/textarea',
  5120. ];
  5121. $this->assertTags($result, $expected);
  5122. }
  5123. /**
  5124. * testHiddenField method
  5125. *
  5126. * @return void
  5127. */
  5128. public function testHiddenField() {
  5129. $this->article['errors'] = [
  5130. 'field' => true
  5131. ];
  5132. $this->Form->request->data['field'] = 'test';
  5133. $this->Form->create($this->article);
  5134. $result = $this->Form->hidden('field', array('id' => 'theID'));
  5135. $this->assertTags($result, array(
  5136. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test'))
  5137. );
  5138. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5139. $expected = [
  5140. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  5141. ];
  5142. $this->assertTags($result, $expected);
  5143. }
  5144. /**
  5145. * testFileUploadField method
  5146. *
  5147. * @return void
  5148. */
  5149. public function testFileUploadField() {
  5150. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5151. $result = $this->Form->file('Model.upload');
  5152. $this->assertTags($result, $expected);
  5153. $this->Form->request->data['Model']['upload'] = [
  5154. 'name' => '', 'type' => '', 'tmp_name' => '',
  5155. 'error' => 4, 'size' => 0
  5156. ];
  5157. $result = $this->Form->file('Model.upload');
  5158. $this->assertTags($result, $expected);
  5159. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  5160. $result = $this->Form->file('Model.upload');
  5161. $this->assertTags($result, $expected);
  5162. }
  5163. /**
  5164. * test File upload input on a model not used in create();
  5165. *
  5166. * @return void
  5167. */
  5168. public function testFileUploadOnOtherModel() {
  5169. $this->Form->create($this->article, array('type' => 'file'));
  5170. $result = $this->Form->file('ValidateProfile.city');
  5171. $expected = array(
  5172. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]')
  5173. );
  5174. $this->assertTags($result, $expected);
  5175. }
  5176. /**
  5177. * testButton method
  5178. *
  5179. * @return void
  5180. */
  5181. public function testButton() {
  5182. $result = $this->Form->button('Hi');
  5183. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5184. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5185. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5186. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5187. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5188. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5189. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5190. $result = $this->Form->button('No type', array('type' => false));
  5191. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5192. $result = $this->Form->button('Upload Text', array(
  5193. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  5194. 'escape' => false
  5195. ));
  5196. $this->assertNotRegExp('/\&039/', $result);
  5197. }
  5198. /**
  5199. * Test that button() makes unlocked fields by default.
  5200. *
  5201. * @return void
  5202. */
  5203. public function testButtonUnlockedByDefault() {
  5204. $this->Form->request->params['_csrfToken'] = 'secured';
  5205. $this->Form->button('Save', array('name' => 'save'));
  5206. $this->Form->button('Clear');
  5207. $result = $this->Form->unlockField();
  5208. $this->assertEquals(array('save'), $result);
  5209. }
  5210. /**
  5211. * testPostButton method
  5212. *
  5213. * @return void
  5214. */
  5215. public function testPostButton() {
  5216. $result = $this->Form->postButton('Hi', '/controller/action');
  5217. $this->assertTags($result, array(
  5218. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5219. 'div' => array('style' => 'display:none;'),
  5220. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5221. '/div',
  5222. 'button' => array('type' => 'submit'),
  5223. 'Hi',
  5224. '/button',
  5225. '/form'
  5226. ));
  5227. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5228. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  5229. }
  5230. /**
  5231. * Test using postButton with N dimensional data.
  5232. *
  5233. * @return void
  5234. */
  5235. public function testPostButtonNestedData() {
  5236. $data = array(
  5237. 'one' => array(
  5238. 'two' => array(
  5239. 3, 4, 5
  5240. )
  5241. )
  5242. );
  5243. $result = $this->Form->postButton('Send', '/', array('data' => $data));
  5244. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5245. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5246. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5247. }
  5248. /**
  5249. * Test that postButton adds _Token fields.
  5250. *
  5251. * @return void
  5252. */
  5253. public function testSecurePostButton() {
  5254. $this->Form->request->params['_csrfToken'] = 'testkey';
  5255. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5256. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5257. $expected = array(
  5258. 'form' => array(
  5259. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5260. ),
  5261. array('div' => array('style' => 'display:none;')),
  5262. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5263. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5264. '/div',
  5265. 'button' => array('type' => 'submit'),
  5266. 'Delete',
  5267. '/button',
  5268. array('div' => array('style' => 'display:none;')),
  5269. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5270. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5271. '/div',
  5272. '/form',
  5273. );
  5274. $this->assertTags($result, $expected);
  5275. }
  5276. /**
  5277. * testPostLink method
  5278. *
  5279. * @return void
  5280. */
  5281. public function testPostLink() {
  5282. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5283. $this->assertTags($result, array(
  5284. 'form' => array(
  5285. 'method' => 'post', 'action' => '/posts/delete/1',
  5286. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5287. ),
  5288. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5289. '/form',
  5290. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5291. 'Delete',
  5292. '/a'
  5293. ));
  5294. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5295. $this->assertTags($result, array(
  5296. 'form' => array(
  5297. 'method' => 'post', 'action' => '/posts/delete/1',
  5298. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5299. ),
  5300. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5301. '/form',
  5302. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5303. 'Delete',
  5304. '/a'
  5305. ));
  5306. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5307. $this->assertTags($result, array(
  5308. 'form' => array(
  5309. 'method' => 'post', 'action' => '/posts/delete/1',
  5310. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5311. ),
  5312. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5313. '/form',
  5314. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5315. 'Delete',
  5316. '/a'
  5317. ));
  5318. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  5319. $this->assertTags($result, array(
  5320. 'form' => array(
  5321. 'method' => 'post', 'action' => '/posts/delete/1',
  5322. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5323. ),
  5324. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5325. '/form',
  5326. '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;/'),
  5327. 'Delete',
  5328. '/a'
  5329. ));
  5330. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5331. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  5332. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  5333. $this->assertTags($result, array(
  5334. 'form' => array(
  5335. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  5336. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5337. ),
  5338. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5339. '/form',
  5340. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5341. 'Delete',
  5342. '/a'
  5343. ));
  5344. $result = $this->Form->postLink(
  5345. '',
  5346. array('controller' => 'items', 'action' => 'delete', 10),
  5347. array('class' => 'btn btn-danger', 'escape' => false),
  5348. 'Confirm thing'
  5349. );
  5350. $this->assertTags($result, array(
  5351. 'form' => array(
  5352. 'method' => 'post', 'action' => '/items/delete/10',
  5353. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5354. ),
  5355. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5356. '/form',
  5357. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5358. '/a'
  5359. ));
  5360. }
  5361. /**
  5362. * Test using postLink with N dimensional data.
  5363. *
  5364. * @return void
  5365. */
  5366. public function testPostLinkNestedData() {
  5367. $data = array(
  5368. 'one' => array(
  5369. 'two' => array(
  5370. 3, 4, 5
  5371. )
  5372. )
  5373. );
  5374. $result = $this->Form->postLink('Send', '/', array('data' => $data));
  5375. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5376. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5377. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5378. }
  5379. /**
  5380. * test creating postLinks after a GET form.
  5381. *
  5382. * @return void
  5383. */
  5384. public function testPostLinkAfterGetForm() {
  5385. $this->Form->request->params['_csrfToken'] = 'testkey';
  5386. $this->Form->request->params['_Token'] = 'val';
  5387. $this->Form->create($this->article, array('type' => 'get'));
  5388. $this->Form->end();
  5389. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5390. $this->assertTags($result, array(
  5391. 'form' => array(
  5392. 'method' => 'post', 'action' => '/posts/delete/1',
  5393. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5394. ),
  5395. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5396. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5397. 'div' => array('style' => 'display:none;'),
  5398. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5399. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5400. '/div',
  5401. '/form',
  5402. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5403. 'Delete',
  5404. '/a'
  5405. ));
  5406. }
  5407. /**
  5408. * Test that postLink adds form tags to view block
  5409. *
  5410. * @return void
  5411. */
  5412. public function testPostLinkFormBuffer() {
  5413. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
  5414. $this->assertTags($result, array(
  5415. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5416. 'Delete',
  5417. '/a'
  5418. ));
  5419. $result = $this->View->fetch('postLink');
  5420. $this->assertTags($result, array(
  5421. 'form' => array(
  5422. 'method' => 'post', 'action' => '/posts/delete/1',
  5423. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5424. ),
  5425. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5426. '/form'
  5427. ));
  5428. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  5429. array('inline' => false, 'method' => 'DELETE')
  5430. );
  5431. $this->assertTags($result, array(
  5432. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5433. 'Delete',
  5434. '/a'
  5435. ));
  5436. $result = $this->View->fetch('postLink');
  5437. $this->assertTags($result, array(
  5438. 'form' => array(
  5439. 'method' => 'post', 'action' => '/posts/delete/1',
  5440. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5441. ),
  5442. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5443. '/form',
  5444. array(
  5445. 'form' => array(
  5446. 'method' => 'post', 'action' => '/posts/delete/2',
  5447. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5448. ),
  5449. ),
  5450. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  5451. '/form'
  5452. ));
  5453. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  5454. $this->assertTags($result, array(
  5455. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5456. 'Delete',
  5457. '/a'
  5458. ));
  5459. $result = $this->View->fetch('foobar');
  5460. $this->assertTags($result, array(
  5461. 'form' => array(
  5462. 'method' => 'post', 'action' => '/posts/delete/1',
  5463. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5464. ),
  5465. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5466. '/form'
  5467. ));
  5468. }
  5469. /**
  5470. * testSubmitButton method
  5471. *
  5472. * @return void
  5473. */
  5474. public function testSubmitButton() {
  5475. $result = $this->Form->submit('');
  5476. $expected = array(
  5477. 'div' => array('class' => 'submit'),
  5478. 'input' => array('type' => 'submit', 'value' => ''),
  5479. '/div'
  5480. );
  5481. $this->assertTags($result, $expected);
  5482. $result = $this->Form->submit('Test Submit');
  5483. $expected = array(
  5484. 'div' => array('class' => 'submit'),
  5485. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5486. '/div'
  5487. );
  5488. $this->assertTags($result, $expected);
  5489. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5490. $expected = array(
  5491. 'span' => array('class' => 'submit'),
  5492. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5493. '/span'
  5494. );
  5495. $this->assertTags($result, $expected);
  5496. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5497. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5498. $this->assertTags($result, $expected);
  5499. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5500. $expected = array(
  5501. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5502. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5503. '/div'
  5504. );
  5505. $this->assertTags($result, $expected);
  5506. $result = $this->Form->submit('Next >');
  5507. $expected = array(
  5508. 'div' => array('class' => 'submit'),
  5509. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5510. '/div'
  5511. );
  5512. $this->assertTags($result, $expected);
  5513. $result = $this->Form->submit('Next >', array('escape' => false));
  5514. $expected = array(
  5515. 'div' => array('class' => 'submit'),
  5516. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5517. '/div'
  5518. );
  5519. $this->assertTags($result, $expected);
  5520. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5521. $expected = array(
  5522. 'div' => array('class' => 'submit'),
  5523. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5524. '/div'
  5525. );
  5526. $this->assertTags($result, $expected);
  5527. $before = '--before--';
  5528. $after = '--after--';
  5529. $result = $this->Form->submit('Test', array('before' => $before));
  5530. $expected = array(
  5531. 'div' => array('class' => 'submit'),
  5532. '--before--',
  5533. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5534. '/div'
  5535. );
  5536. $this->assertTags($result, $expected);
  5537. $result = $this->Form->submit('Test', array('after' => $after));
  5538. $expected = array(
  5539. 'div' => array('class' => 'submit'),
  5540. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5541. '--after--',
  5542. '/div'
  5543. );
  5544. $this->assertTags($result, $expected);
  5545. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5546. $expected = array(
  5547. 'div' => array('class' => 'submit'),
  5548. '--before--',
  5549. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5550. '--after--',
  5551. '/div'
  5552. );
  5553. $this->assertTags($result, $expected);
  5554. }
  5555. /**
  5556. * test image submit types.
  5557. *
  5558. * @return void
  5559. */
  5560. public function testSubmitImage() {
  5561. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5562. $expected = array(
  5563. 'div' => array('class' => 'submit'),
  5564. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5565. '/div'
  5566. );
  5567. $this->assertTags($result, $expected);
  5568. $result = $this->Form->submit('/relative/cake.power.gif');
  5569. $expected = array(
  5570. 'div' => array('class' => 'submit'),
  5571. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5572. '/div'
  5573. );
  5574. $this->assertTags($result, $expected);
  5575. $result = $this->Form->submit('cake.power.gif');
  5576. $expected = array(
  5577. 'div' => array('class' => 'submit'),
  5578. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5579. '/div'
  5580. );
  5581. $this->assertTags($result, $expected);
  5582. $result = $this->Form->submit('Not.an.image');
  5583. $expected = array(
  5584. 'div' => array('class' => 'submit'),
  5585. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5586. '/div'
  5587. );
  5588. $this->assertTags($result, $expected);
  5589. $after = '--after--';
  5590. $before = '--before--';
  5591. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5592. $expected = array(
  5593. 'div' => array('class' => 'submit'),
  5594. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5595. '--after--',
  5596. '/div'
  5597. );
  5598. $this->assertTags($result, $expected);
  5599. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5600. $expected = array(
  5601. 'div' => array('class' => 'submit'),
  5602. '--before--',
  5603. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5604. '/div'
  5605. );
  5606. $this->assertTags($result, $expected);
  5607. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5608. $expected = array(
  5609. 'div' => array('class' => 'submit'),
  5610. '--before--',
  5611. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5612. '--after--',
  5613. '/div'
  5614. );
  5615. $this->assertTags($result, $expected);
  5616. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5617. $expected = array(
  5618. 'div' => array('class' => 'submit'),
  5619. '--before--',
  5620. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5621. '--after--',
  5622. '/div'
  5623. );
  5624. $this->assertTags($result, $expected);
  5625. }
  5626. /**
  5627. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5628. * be submitted at a time.
  5629. *
  5630. * @return void
  5631. */
  5632. public function testSubmitUnlockedByDefault() {
  5633. $this->Form->request->params['_Token'] = 'secured';
  5634. $this->Form->submit('Go go');
  5635. $this->Form->submit('Save', array('name' => 'save'));
  5636. $result = $this->Form->unlockField();
  5637. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5638. }
  5639. /**
  5640. * Test submit image with timestamps.
  5641. *
  5642. * @return void
  5643. */
  5644. public function testSubmitImageTimestamp() {
  5645. Configure::write('Asset.timestamp', 'force');
  5646. $result = $this->Form->submit('cake.power.gif');
  5647. $expected = array(
  5648. 'div' => array('class' => 'submit'),
  5649. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5650. '/div'
  5651. );
  5652. $this->assertTags($result, $expected);
  5653. }
  5654. /**
  5655. * test that datetime() works with GET style forms.
  5656. *
  5657. * @return void
  5658. */
  5659. public function testDateTimeWithGetForms() {
  5660. $this->markTestIncomplete('Need to revisit once models work again.');
  5661. extract($this->dateRegex);
  5662. $this->Form->create('Contact', array('type' => 'get'));
  5663. $result = $this->Form->datetime('created');
  5664. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  5665. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  5666. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  5667. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  5668. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  5669. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  5670. }
  5671. /**
  5672. * testEditFormWithData method
  5673. *
  5674. * test auto populating form elements from submitted data.
  5675. *
  5676. * @return void
  5677. */
  5678. public function testEditFormWithData() {
  5679. $this->markTestIncomplete('Need to revisit once models work again.');
  5680. $this->Form->request->data = array('Person' => array(
  5681. 'id' => 1,
  5682. 'first_name' => 'Nate',
  5683. 'last_name' => 'Abele',
  5684. 'email' => 'nate@example.com'
  5685. ));
  5686. $this->Form->request->addParams(array(
  5687. 'models' => array('Person'),
  5688. 'controller' => 'people',
  5689. 'action' => 'add'
  5690. ));
  5691. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  5692. $this->Form->create();
  5693. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  5694. $expected = array(
  5695. 'input' => array('type' => 'hidden', 'name' => 'People[People]', 'value' => '', 'id' => 'PeoplePeople_'),
  5696. 'select' => array(
  5697. 'name' => 'People[People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  5698. ),
  5699. array('option' => array('value' => 1)), 'Nate', '/option',
  5700. array('option' => array('value' => 2)), 'Garrett', '/option',
  5701. array('option' => array('value' => 3)), 'Larry', '/option',
  5702. '/select'
  5703. );
  5704. $this->assertTags($result, $expected);
  5705. }
  5706. /**
  5707. * Test that required fields are created for various types of validation.
  5708. *
  5709. * @return void
  5710. */
  5711. public function testFormInputRequiredDetection() {
  5712. $this->markTestIncomplete('Need to revisit once models work again.');
  5713. $this->Form->create('Contact');
  5714. $result = $this->Form->input('Contact.non_existing');
  5715. $expected = array(
  5716. 'div' => array('class' => 'input text'),
  5717. 'label' => array('for' => 'ContactNonExisting'),
  5718. 'Non Existing',
  5719. '/label',
  5720. 'input' => array(
  5721. 'type' => 'text', 'name' => 'Contact[non_existing]',
  5722. 'id' => 'ContactNonExisting'
  5723. ),
  5724. '/div'
  5725. );
  5726. $this->assertTags($result, $expected);
  5727. $result = $this->Form->input('Contact.imrequired');
  5728. $expected = array(
  5729. 'div' => array('class' => 'input text required'),
  5730. 'label' => array('for' => 'ContactImrequired'),
  5731. 'Imrequired',
  5732. '/label',
  5733. 'input' => array(
  5734. 'type' => 'text', 'name' => 'Contact[imrequired]',
  5735. 'id' => 'ContactImrequired',
  5736. 'required' => 'required'
  5737. ),
  5738. '/div'
  5739. );
  5740. $this->assertTags($result, $expected);
  5741. $result = $this->Form->input('Contact.imalsorequired');
  5742. $expected = array(
  5743. 'div' => array('class' => 'input text required'),
  5744. 'label' => array('for' => 'ContactImalsorequired'),
  5745. 'Imalsorequired',
  5746. '/label',
  5747. 'input' => array(
  5748. 'type' => 'text', 'name' => 'Contact[imalsorequired]',
  5749. 'id' => 'ContactImalsorequired',
  5750. 'required' => 'required'
  5751. ),
  5752. '/div'
  5753. );
  5754. $this->assertTags($result, $expected);
  5755. $result = $this->Form->input('Contact.imrequiredtoo');
  5756. $expected = array(
  5757. 'div' => array('class' => 'input text required'),
  5758. 'label' => array('for' => 'ContactImrequiredtoo'),
  5759. 'Imrequiredtoo',
  5760. '/label',
  5761. 'input' => array(
  5762. 'type' => 'text', 'name' => 'Contact[imrequiredtoo]',
  5763. 'id' => 'ContactImrequiredtoo',
  5764. 'required' => 'required'
  5765. ),
  5766. '/div'
  5767. );
  5768. $this->assertTags($result, $expected);
  5769. $result = $this->Form->input('Contact.required_one');
  5770. $expected = array(
  5771. 'div' => array('class' => 'input text required'),
  5772. 'label' => array('for' => 'ContactRequiredOne'),
  5773. 'Required One',
  5774. '/label',
  5775. 'input' => array(
  5776. 'type' => 'text', 'name' => 'Contact[required_one]',
  5777. 'id' => 'ContactRequiredOne',
  5778. 'required' => 'required'
  5779. ),
  5780. '/div'
  5781. );
  5782. $this->assertTags($result, $expected);
  5783. $result = $this->Form->input('Contact.string_required');
  5784. $expected = array(
  5785. 'div' => array('class' => 'input text required'),
  5786. 'label' => array('for' => 'ContactStringRequired'),
  5787. 'String Required',
  5788. '/label',
  5789. 'input' => array(
  5790. 'type' => 'text', 'name' => 'Contact[string_required]',
  5791. 'id' => 'ContactStringRequired',
  5792. 'required' => 'required'
  5793. ),
  5794. '/div'
  5795. );
  5796. $this->assertTags($result, $expected);
  5797. $result = $this->Form->input('Contact.imnotrequired');
  5798. $expected = array(
  5799. 'div' => array('class' => 'input text'),
  5800. 'label' => array('for' => 'ContactImnotrequired'),
  5801. 'Imnotrequired',
  5802. '/label',
  5803. 'input' => array(
  5804. 'type' => 'text', 'name' => 'Contact[imnotrequired]',
  5805. 'id' => 'ContactImnotrequired'
  5806. ),
  5807. '/div'
  5808. );
  5809. $this->assertTags($result, $expected);
  5810. $result = $this->Form->input('Contact.imalsonotrequired');
  5811. $expected = array(
  5812. 'div' => array('class' => 'input text'),
  5813. 'label' => array('for' => 'ContactImalsonotrequired'),
  5814. 'Imalsonotrequired',
  5815. '/label',
  5816. 'input' => array(
  5817. 'type' => 'text', 'name' => 'Contact[imalsonotrequired]',
  5818. 'id' => 'ContactImalsonotrequired'
  5819. ),
  5820. '/div'
  5821. );
  5822. $this->assertTags($result, $expected);
  5823. $result = $this->Form->input('Contact.imalsonotrequired2');
  5824. $expected = array(
  5825. 'div' => array('class' => 'input text'),
  5826. 'label' => array('for' => 'ContactImalsonotrequired2'),
  5827. 'Imalsonotrequired2',
  5828. '/label',
  5829. 'input' => array(
  5830. 'type' => 'text', 'name' => 'Contact[imalsonotrequired2]',
  5831. 'id' => 'ContactImalsonotrequired2'
  5832. ),
  5833. '/div'
  5834. );
  5835. $this->assertTags($result, $expected);
  5836. $result = $this->Form->input('Contact.imnotrequiredeither');
  5837. $expected = array(
  5838. 'div' => array('class' => 'input text'),
  5839. 'label' => array('for' => 'ContactImnotrequiredeither'),
  5840. 'Imnotrequiredeither',
  5841. '/label',
  5842. 'input' => array(
  5843. 'type' => 'text', 'name' => 'Contact[imnotrequiredeither]',
  5844. 'id' => 'ContactImnotrequiredeither'
  5845. ),
  5846. '/div'
  5847. );
  5848. $this->assertTags($result, $expected);
  5849. $result = $this->Form->input('Contact.iamrequiredalways');
  5850. $expected = array(
  5851. 'div' => array('class' => 'input text required'),
  5852. 'label' => array('for' => 'ContactIamrequiredalways'),
  5853. 'Iamrequiredalways',
  5854. '/label',
  5855. 'input' => array(
  5856. 'type' => 'text', 'name' => 'Contact[iamrequiredalways]',
  5857. 'id' => 'ContactIamrequiredalways',
  5858. 'required' => 'required'
  5859. ),
  5860. '/div'
  5861. );
  5862. $this->assertTags($result, $expected);
  5863. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  5864. $expected = array(
  5865. 'div' => array('class' => 'input checkbox required'),
  5866. array('input' => array(
  5867. 'type' => 'hidden',
  5868. 'name' => 'Contact[boolean_field]',
  5869. 'id' => 'ContactBooleanField_',
  5870. 'value' => '0'
  5871. )),
  5872. array('input' => array(
  5873. 'type' => 'checkbox',
  5874. 'name' => 'Contact[boolean_field]',
  5875. 'value' => '1',
  5876. 'id' => 'ContactBooleanField'
  5877. )),
  5878. 'label' => array('for' => 'ContactBooleanField'),
  5879. 'Boolean Field',
  5880. '/label',
  5881. '/div'
  5882. );
  5883. $this->assertTags($result, $expected);
  5884. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  5885. $expected = array(
  5886. 'div' => array('class' => 'input checkbox required'),
  5887. array('input' => array(
  5888. 'type' => 'hidden',
  5889. 'name' => 'Contact[boolean_field]',
  5890. 'id' => 'ContactBooleanField_',
  5891. 'value' => '0'
  5892. )),
  5893. array('input' => array(
  5894. 'type' => 'checkbox',
  5895. 'name' => 'Contact[boolean_field]',
  5896. 'value' => '1',
  5897. 'id' => 'ContactBooleanField',
  5898. 'required' => 'required'
  5899. )),
  5900. 'label' => array('for' => 'ContactBooleanField'),
  5901. 'Boolean Field',
  5902. '/label',
  5903. '/div'
  5904. );
  5905. $this->assertTags($result, $expected);
  5906. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  5907. $expected = array(
  5908. 'div' => array('class' => 'input file required'),
  5909. 'label' => array('for' => 'ContactIamrequiredalways'),
  5910. 'Iamrequiredalways',
  5911. '/label',
  5912. 'input' => array(
  5913. 'type' => 'file',
  5914. 'name' => 'data[Contact][iamrequiredalways]',
  5915. 'id' => 'ContactIamrequiredalways',
  5916. 'required' => 'required'
  5917. ),
  5918. '/div'
  5919. );
  5920. $this->assertTags($result, $expected);
  5921. }
  5922. /**
  5923. * Test that required fields are created when only using ModelValidator::add().
  5924. *
  5925. * @return void
  5926. */
  5927. public function testFormInputRequiredDetectionModelValidator() {
  5928. $this->markTestIncomplete('Need to revisit once models work again.');
  5929. $this->Form->create('ContactTag');
  5930. $result = $this->Form->input('ContactTag.iwillberequired');
  5931. $expected = array(
  5932. 'div' => array('class' => 'input text required'),
  5933. 'label' => array('for' => 'ContactTagIwillberequired'),
  5934. 'Iwillberequired',
  5935. '/label',
  5936. 'input' => array(
  5937. 'name' => 'data[ContactTag][iwillberequired]',
  5938. 'type' => 'text',
  5939. 'id' => 'ContactTagIwillberequired',
  5940. 'required' => 'required'
  5941. ),
  5942. '/div'
  5943. );
  5944. $this->assertTags($result, $expected);
  5945. }
  5946. /**
  5947. * testFormMagicInput method
  5948. *
  5949. * @return void
  5950. */
  5951. public function testFormMagicInput() {
  5952. $this->markTestIncomplete('Need to revisit once models work again.');
  5953. $encoding = strtolower(Configure::read('App.encoding'));
  5954. TableRegistry::get('Contacts', [
  5955. 'className' => __NAMESPACE__ . '\ContactsTable'
  5956. ]);
  5957. $result = $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  5958. $expected = array(
  5959. 'form' => array(
  5960. 'method' => 'post', 'action' => '/articles/add',
  5961. 'accept-charset' => $encoding
  5962. ),
  5963. 'div' => array('style' => 'display:none;'),
  5964. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5965. '/div'
  5966. );
  5967. $this->assertTags($result, $expected);
  5968. $result = $this->Form->input('name');
  5969. $expected = array(
  5970. 'div' => array('class' => 'input text'),
  5971. 'label' => array('for' => 'name'),
  5972. 'Name',
  5973. '/label',
  5974. 'input' => array(
  5975. 'type' => 'text', 'name' => 'name',
  5976. 'id' => 'name', 'maxlength' => '255'
  5977. ),
  5978. '/div'
  5979. );
  5980. $this->assertTags($result, $expected);
  5981. $result = $this->Form->input('non_existing_field_in_contact_model');
  5982. $expected = array(
  5983. 'div' => array('class' => 'input text'),
  5984. 'label' => array('for' => 'non-existing-field-in-contact-model'),
  5985. 'Non Existing Field In Contact Model',
  5986. '/label',
  5987. 'input' => array(
  5988. 'type' => 'text', 'name' => 'non_existing_field_in_contact_model',
  5989. 'id' => 'non-existing-field-in-contact-model'
  5990. ),
  5991. '/div'
  5992. );
  5993. $this->assertTags($result, $expected);
  5994. extract($this->dateRegex);
  5995. $now = strtotime('now');
  5996. $result = $this->Form->input('Contact.published', array(
  5997. 'templates' => ['groupContainer' => '{{label}}{{input}}']
  5998. ));
  5999. $expected = array(
  6000. 'label' => array('for' => 'ContactPublishedMonth'),
  6001. 'Published',
  6002. '/label',
  6003. array('select' => array(
  6004. 'name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth'
  6005. )),
  6006. $monthsRegex,
  6007. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6008. date('F', $now),
  6009. '/option',
  6010. '*/select',
  6011. '-',
  6012. array('select' => array(
  6013. 'name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay'
  6014. )),
  6015. $daysRegex,
  6016. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6017. date('j', $now),
  6018. '/option',
  6019. '*/select',
  6020. '-',
  6021. array('select' => array(
  6022. 'name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear'
  6023. )),
  6024. $yearsRegex,
  6025. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6026. date('Y', $now),
  6027. '*/select'
  6028. );
  6029. $this->assertTags($result, $expected);
  6030. $result = $this->Form->input('Contact.updated', array('div' => false));
  6031. $expected = array(
  6032. 'label' => array('for' => 'ContactUpdatedMonth'),
  6033. 'Updated',
  6034. '/label',
  6035. array('select' => array(
  6036. 'name' => 'Contact[updated][month]', 'id' => 'ContactUpdatedMonth'
  6037. )),
  6038. $monthsRegex,
  6039. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6040. date('F', $now),
  6041. '/option',
  6042. '*/select',
  6043. '-',
  6044. array('select' => array(
  6045. 'name' => 'Contact[updated][day]', 'id' => 'ContactUpdatedDay'
  6046. )),
  6047. $daysRegex,
  6048. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6049. date('j', $now),
  6050. '/option',
  6051. '*/select',
  6052. '-',
  6053. array('select' => array(
  6054. 'name' => 'Contact[updated][year]', 'id' => 'ContactUpdatedYear'
  6055. )),
  6056. $yearsRegex,
  6057. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6058. date('Y', $now),
  6059. '*/select'
  6060. );
  6061. $this->assertTags($result, $expected);
  6062. $result = $this->Form->input('UserForm.stuff');
  6063. $expected = array(
  6064. 'div' => array('class' => 'input text'),
  6065. 'label' => array('for' => 'UserFormStuff'),
  6066. 'Stuff',
  6067. '/label',
  6068. 'input' => array(
  6069. 'type' => 'text', 'name' => 'UserForm[stuff]',
  6070. 'id' => 'UserFormStuff', 'maxlength' => 10
  6071. ),
  6072. '/div'
  6073. );
  6074. $this->assertTags($result, $expected);
  6075. }
  6076. /**
  6077. * testForMagicInputNonExistingNorValidated method
  6078. *
  6079. * @return void
  6080. */
  6081. public function testForMagicInputNonExistingNorValidated() {
  6082. $this->markTestIncomplete('Need to revisit once models work again.');
  6083. $encoding = strtolower(Configure::read('App.encoding'));
  6084. $result = $this->Form->create('Contact');
  6085. $expected = array(
  6086. 'form' => array(
  6087. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6088. 'accept-charset' => $encoding
  6089. ),
  6090. 'div' => array('style' => 'display:none;'),
  6091. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6092. '/div'
  6093. );
  6094. $this->assertTags($result, $expected);
  6095. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6096. $expected = array(
  6097. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6098. 'Non Existing Nor Validated',
  6099. '/label',
  6100. 'input' => array(
  6101. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  6102. 'id' => 'ContactNonExistingNorValidated'
  6103. )
  6104. );
  6105. $this->assertTags($result, $expected);
  6106. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6107. 'div' => false, 'value' => 'my value'
  6108. ));
  6109. $expected = array(
  6110. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6111. 'Non Existing Nor Validated',
  6112. '/label',
  6113. 'input' => array(
  6114. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  6115. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6116. )
  6117. );
  6118. $this->assertTags($result, $expected);
  6119. $this->Form->request->data = array(
  6120. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6121. ));
  6122. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6123. $expected = array(
  6124. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6125. 'Non Existing Nor Validated',
  6126. '/label',
  6127. 'input' => array(
  6128. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  6129. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6130. )
  6131. );
  6132. $this->assertTags($result, $expected);
  6133. }
  6134. /**
  6135. * testFormMagicInputLabel method
  6136. *
  6137. * @return void
  6138. */
  6139. public function testFormMagicInputLabel() {
  6140. $this->markTestIncomplete('Need to revisit once models work again.');
  6141. TableRegistry::get('Contacts', [
  6142. 'className' => __NAMESPACE__ . '\ContactsTable'
  6143. ]);
  6144. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6145. $this->Form->templates(['groupContainer' => '{{content}}']);
  6146. $result = $this->Form->input('Contacts.name', array('label' => 'My label'));
  6147. $expected = array(
  6148. 'label' => array('for' => 'contacts-name'),
  6149. 'My label',
  6150. '/label',
  6151. 'input' => array(
  6152. 'type' => 'text', 'name' => 'Contacts[name]',
  6153. 'id' => 'contacts-name', 'maxlength' => '255'
  6154. )
  6155. );
  6156. $this->assertTags($result, $expected);
  6157. $result = $this->Form->input('name', array(
  6158. 'label' => array('class' => 'mandatory')
  6159. ));
  6160. $expected = array(
  6161. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  6162. 'Name',
  6163. '/label',
  6164. 'input' => array(
  6165. 'type' => 'text', 'name' => 'name',
  6166. 'id' => 'name', 'maxlength' => '255'
  6167. )
  6168. );
  6169. $this->assertTags($result, $expected);
  6170. $result = $this->Form->input('name', array(
  6171. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6172. ));
  6173. $expected = array(
  6174. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  6175. 'My label',
  6176. '/label',
  6177. 'input' => array(
  6178. 'type' => 'text', 'name' => 'name',
  6179. 'id' => 'name', 'maxlength' => '255'
  6180. )
  6181. );
  6182. $this->assertTags($result, $expected);
  6183. $result = $this->Form->input('Contact.name', array(
  6184. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6185. ));
  6186. $expected = array(
  6187. 'label' => array('for' => 'my_id'),
  6188. 'Name',
  6189. '/label',
  6190. 'input' => array(
  6191. 'type' => 'text', 'name' => 'Contact[name]',
  6192. 'id' => 'my_id', 'maxlength' => '255'
  6193. )
  6194. );
  6195. $this->assertTags($result, $expected);
  6196. $result = $this->Form->input('1.id');
  6197. $this->assertTags($result, array('input' => array(
  6198. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  6199. 'id' => 'Contact1Id'
  6200. )));
  6201. $result = $this->Form->input("1.name");
  6202. $expected = array(
  6203. 'div' => array('class' => 'input text'),
  6204. 'label' => array('for' => 'Contact1Name'),
  6205. 'Name',
  6206. '/label',
  6207. 'input' => array(
  6208. 'type' => 'text', 'name' => 'Contact[1][name]',
  6209. 'id' => 'Contact1Name', 'maxlength' => '255'
  6210. ),
  6211. '/div'
  6212. );
  6213. $this->assertTags($result, $expected);
  6214. $result = $this->Form->input('Contact.1.id');
  6215. $this->assertTags($result, array(
  6216. 'input' => array(
  6217. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  6218. 'id' => 'Contact1Id'
  6219. )
  6220. ));
  6221. $result = $this->Form->input("Model.1.name");
  6222. $expected = array(
  6223. 'div' => array('class' => 'input text'),
  6224. 'label' => array('for' => 'Model1Name'),
  6225. 'Name',
  6226. '/label',
  6227. 'input' => array(
  6228. 'type' => 'text', 'name' => 'Model[1][name]',
  6229. 'id' => 'Model1Name'
  6230. ),
  6231. '/div'
  6232. );
  6233. $this->assertTags($result, $expected);
  6234. }
  6235. /**
  6236. * testFormEnd method
  6237. *
  6238. * @return void
  6239. */
  6240. public function testFormEnd() {
  6241. $this->assertEquals('</form>', $this->Form->end());
  6242. }
  6243. /**
  6244. * testMultipleFormWithIdFields method
  6245. *
  6246. * @return void
  6247. */
  6248. public function testMultipleFormWithIdFields() {
  6249. $this->markTestIncomplete('Need to revisit once models work again.');
  6250. $this->Form->create('UserForm');
  6251. $result = $this->Form->input('id');
  6252. $this->assertTags($result, array('input' => array(
  6253. 'type' => 'hidden', 'name' => 'UserForm[id]', 'id' => 'UserFormId'
  6254. )));
  6255. $result = $this->Form->input('ValidateItem.id');
  6256. $this->assertTags($result, array('input' => array(
  6257. 'type' => 'hidden', 'name' => 'ValidateItem[id]',
  6258. 'id' => 'ValidateItemId'
  6259. )));
  6260. $result = $this->Form->input('ValidateUser.id');
  6261. $this->assertTags($result, array('input' => array(
  6262. 'type' => 'hidden', 'name' => 'ValidateUser[id]',
  6263. 'id' => 'ValidateUserId'
  6264. )));
  6265. }
  6266. /**
  6267. * testDbLessModel method
  6268. *
  6269. * @return void
  6270. */
  6271. public function testDbLessModel() {
  6272. $this->markTestIncomplete('Need to revisit once models work again.');
  6273. $this->Form->create('TestMail');
  6274. $result = $this->Form->input('name');
  6275. $expected = array(
  6276. 'div' => array('class' => 'input text'),
  6277. 'label' => array('for' => 'TestMailName'),
  6278. 'Name',
  6279. '/label',
  6280. 'input' => array(
  6281. 'name' => 'TestMail[name]', 'type' => 'text',
  6282. 'id' => 'TestMailName'
  6283. ),
  6284. '/div'
  6285. );
  6286. $this->assertTags($result, $expected);
  6287. $this->Form->create('TestMail');
  6288. $result = $this->Form->input('name');
  6289. $expected = array(
  6290. 'div' => array('class' => 'input text'),
  6291. 'label' => array('for' => 'TestMailName'),
  6292. 'Name',
  6293. '/label',
  6294. 'input' => array(
  6295. 'name' => 'TestMail[name]', 'type' => 'text',
  6296. 'id' => 'TestMailName'
  6297. ),
  6298. '/div'
  6299. );
  6300. $this->assertTags($result, $expected);
  6301. }
  6302. /**
  6303. * Test the generation of fields for a multi record form.
  6304. *
  6305. * @return void
  6306. */
  6307. public function testMultiRecordForm() {
  6308. $this->markTestIncomplete('Need to revisit once models work again.');
  6309. $this->Form->create('ValidateProfile');
  6310. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  6311. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  6312. $expected = array(
  6313. 'div' => array('class' => 'input textarea'),
  6314. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  6315. 'Name',
  6316. '/label',
  6317. 'textarea' => array(
  6318. 'id' => 'ValidateProfile1ValidateItem2Name',
  6319. 'name' => 'ValidateProfile[1][ValidateItem][2][name]',
  6320. 'cols' => 30,
  6321. 'rows' => 6
  6322. ),
  6323. 'Value',
  6324. '/textarea',
  6325. '/div'
  6326. );
  6327. $this->assertTags($result, $expected);
  6328. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  6329. $expected = array(
  6330. 'div' => array('class' => 'input date'),
  6331. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  6332. 'Created',
  6333. '/label',
  6334. array('select' => array(
  6335. 'name' => 'ValidateProfile[1][ValidateItem][2][created][month]',
  6336. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  6337. )
  6338. ),
  6339. array('option' => array('value' => '')), '/option',
  6340. $this->dateRegex['monthsRegex'],
  6341. '/select', '-',
  6342. array('select' => array(
  6343. 'name' => 'ValidateProfile[1][ValidateItem][2][created][day]',
  6344. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  6345. )
  6346. ),
  6347. array('option' => array('value' => '')), '/option',
  6348. $this->dateRegex['daysRegex'],
  6349. '/select', '-',
  6350. array('select' => array(
  6351. 'name' => 'ValidateProfile[1][ValidateItem][2][created][year]',
  6352. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  6353. )
  6354. ),
  6355. array('option' => array('value' => '')), '/option',
  6356. $this->dateRegex['yearsRegex'],
  6357. '/select',
  6358. '/div'
  6359. );
  6360. $this->assertTags($result, $expected);
  6361. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  6362. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  6363. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  6364. $expected = array(
  6365. 'div' => array('class' => 'input select error'),
  6366. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  6367. 'Profile',
  6368. '/label',
  6369. 'select' => array(
  6370. 'name' => 'ValidateProfile[1][ValidateItem][2][profile_id]',
  6371. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  6372. 'class' => 'form-error'
  6373. ),
  6374. '/select',
  6375. array('div' => array('class' => 'error-message')),
  6376. 'Error',
  6377. '/div',
  6378. '/div'
  6379. );
  6380. $this->assertTags($result, $expected);
  6381. }
  6382. /**
  6383. * test the correct display of multi-record form validation errors.
  6384. *
  6385. * @return void
  6386. */
  6387. public function testMultiRecordFormValidationErrors() {
  6388. $this->markTestIncomplete('Need to revisit once models work again.');
  6389. $this->Form->create('ValidateProfile');
  6390. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  6391. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  6392. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  6393. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  6394. $result = $this->Form->error('ValidateProfile.2.city');
  6395. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6396. $result = $this->Form->error('2.city');
  6397. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6398. }
  6399. /**
  6400. * test the correct display of multi-record form validation errors.
  6401. *
  6402. * @return void
  6403. */
  6404. public function testSaveManyRecordFormValidationErrors() {
  6405. $this->markTestIncomplete('Need to revisit once models work again.');
  6406. $this->Form->create('ValidateUser');
  6407. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  6408. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  6409. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  6410. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  6411. $result = $this->Form->error('ValidateUser.0.city');
  6412. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6413. }
  6414. /**
  6415. * test that some html5 inputs + FormHelper::__call() work
  6416. *
  6417. * @return void
  6418. */
  6419. public function testHtml5Inputs() {
  6420. $result = $this->Form->email('User.email');
  6421. $expected = array(
  6422. 'input' => array('type' => 'email', 'name' => 'User[email]')
  6423. );
  6424. $this->assertTags($result, $expected);
  6425. $result = $this->Form->search('User.query');
  6426. $expected = array(
  6427. 'input' => array('type' => 'search', 'name' => 'User[query]')
  6428. );
  6429. $this->assertTags($result, $expected);
  6430. $result = $this->Form->search('User.query', array('value' => 'test'));
  6431. $expected = array(
  6432. 'input' => array('type' => 'search', 'name' => 'User[query]', 'value' => 'test')
  6433. );
  6434. $this->assertTags($result, $expected);
  6435. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  6436. $expected = array(
  6437. 'input' => array('type' => 'text', 'name' => 'User[query]', 'value' => 'test')
  6438. );
  6439. $this->assertTags($result, $expected);
  6440. }
  6441. /**
  6442. * Test accessing html5 inputs through input().
  6443. *
  6444. * @return void
  6445. */
  6446. public function testHtml5InputWithInput() {
  6447. $this->Form->create();
  6448. $this->Form->templates(['groupContainer' => '{{content}}']);
  6449. $result = $this->Form->input('website', array(
  6450. 'type' => 'url',
  6451. 'val' => 'http://domain.tld',
  6452. 'label' => false
  6453. ));
  6454. $expected = array(
  6455. 'input' => array('type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld')
  6456. );
  6457. $this->assertTags($result, $expected);
  6458. }
  6459. /**
  6460. * Test errors when field name is missing.
  6461. *
  6462. * @expectedException \Cake\Error\Exception
  6463. * @return void
  6464. */
  6465. public function testHtml5InputException() {
  6466. $this->Form->email();
  6467. }
  6468. /**
  6469. * Tests that the 'on' key validates as expected on create
  6470. *
  6471. * @return void
  6472. */
  6473. public function testRequiredOnCreate() {
  6474. $this->markTestIncomplete('Need to revisit once models work again.');
  6475. $this->Form->create('Contact');
  6476. $result = $this->Form->input('Contact.imrequiredonupdate');
  6477. $expected = array(
  6478. 'div' => array('class' => 'input text'),
  6479. 'label' => array('for' => 'ContactImrequiredonupdate'),
  6480. 'Imrequiredonupdate',
  6481. '/label',
  6482. 'input' => array(
  6483. 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
  6484. 'id' => 'ContactImrequiredonupdate'
  6485. ),
  6486. '/div'
  6487. );
  6488. $this->assertTags($result, $expected);
  6489. $result = $this->Form->input('Contact.imrequiredoncreate');
  6490. $expected = array(
  6491. 'div' => array('class' => 'input text required'),
  6492. 'label' => array('for' => 'ContactImrequiredoncreate'),
  6493. 'Imrequiredoncreate',
  6494. '/label',
  6495. 'input' => array(
  6496. 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
  6497. 'id' => 'ContactImrequiredoncreate',
  6498. 'required' => 'required'
  6499. ),
  6500. '/div'
  6501. );
  6502. $this->assertTags($result, $expected);
  6503. $result = $this->Form->input('Contact.imrequiredonboth');
  6504. $expected = array(
  6505. 'div' => array('class' => 'input text required'),
  6506. 'label' => array('for' => 'ContactImrequiredonboth'),
  6507. 'Imrequiredonboth',
  6508. '/label',
  6509. 'input' => array(
  6510. 'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
  6511. 'id' => 'ContactImrequiredonboth',
  6512. 'required' => 'required'
  6513. ),
  6514. '/div'
  6515. );
  6516. $this->assertTags($result, $expected);
  6517. $this->Form->inputDefaults(array('required' => false));
  6518. $result = $this->Form->input('Contact.imrequired');
  6519. $expected = array(
  6520. 'div' => array('class' => 'input text'),
  6521. 'label' => array('for' => 'ContactImrequired'),
  6522. 'Imrequired',
  6523. '/label',
  6524. 'input' => array(
  6525. 'type' => 'text', 'name' => 'Contact[imrequired]',
  6526. 'id' => 'ContactImrequired'
  6527. ),
  6528. '/div'
  6529. );
  6530. $this->assertTags($result, $expected);
  6531. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  6532. $this->assertTags($result, $expected);
  6533. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  6534. $expected = array(
  6535. 'div' => array('class' => 'input text required'),
  6536. 'label' => array('for' => 'ContactImrequired'),
  6537. 'Imrequired',
  6538. '/label',
  6539. 'input' => array(
  6540. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6541. 'id' => 'ContactImrequired'
  6542. ),
  6543. '/div'
  6544. );
  6545. $this->assertTags($result, $expected);
  6546. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  6547. $this->assertTags($result, $expected);
  6548. }
  6549. /**
  6550. * Tests that the 'on' key validates as expected on update
  6551. *
  6552. * @return void
  6553. */
  6554. public function testRequiredOnUpdate() {
  6555. $this->markTestIncomplete('Need to revisit once models work again.');
  6556. $this->Form->request->data['Contact']['id'] = 1;
  6557. $this->Form->create('Contact');
  6558. $result = $this->Form->input('Contact.imrequiredonupdate');
  6559. $expected = array(
  6560. 'div' => array('class' => 'input text required'),
  6561. 'label' => array('for' => 'ContactImrequiredonupdate'),
  6562. 'Imrequiredonupdate',
  6563. '/label',
  6564. 'input' => array(
  6565. 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
  6566. 'id' => 'ContactImrequiredonupdate',
  6567. 'required' => 'required'
  6568. ),
  6569. '/div'
  6570. );
  6571. $this->assertTags($result, $expected);
  6572. $result = $this->Form->input('Contact.imrequiredoncreate');
  6573. $expected = array(
  6574. 'div' => array('class' => 'input text'),
  6575. 'label' => array('for' => 'ContactImrequiredoncreate'),
  6576. 'Imrequiredoncreate',
  6577. '/label',
  6578. 'input' => array(
  6579. 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
  6580. 'id' => 'ContactImrequiredoncreate'
  6581. ),
  6582. '/div'
  6583. );
  6584. $this->assertTags($result, $expected);
  6585. $result = $this->Form->input('Contact.imrequiredonboth');
  6586. $expected = array(
  6587. 'div' => array('class' => 'input text required'),
  6588. 'label' => array('for' => 'ContactImrequiredonboth'),
  6589. 'Imrequiredonboth',
  6590. '/label',
  6591. 'input' => array(
  6592. 'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
  6593. 'id' => 'ContactImrequiredonboth',
  6594. 'required' => 'required'
  6595. ),
  6596. '/div'
  6597. );
  6598. $this->assertTags($result, $expected);
  6599. $result = $this->Form->input('Contact.imrequired');
  6600. $expected = array(
  6601. 'div' => array('class' => 'input text required'),
  6602. 'label' => array('for' => 'ContactImrequired'),
  6603. 'Imrequired',
  6604. '/label',
  6605. 'input' => array(
  6606. 'type' => 'text', 'name' => 'Contact[imrequired]',
  6607. 'id' => 'ContactImrequired',
  6608. 'required' => 'required'
  6609. ),
  6610. '/div'
  6611. );
  6612. $this->assertTags($result, $expected);
  6613. }
  6614. }