FormHelperTest.php 196 KB

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