FormHelperTest.php 220 KB

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