FormHelperTest.php 239 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978
  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. );
  70. /**
  71. * validate property
  72. *
  73. * @var array
  74. */
  75. public $validate = array(
  76. 'non_existing' => array(),
  77. 'idontexist' => array(),
  78. 'imrequired' => array('_allowEmpty' => false, array('rule' => array('between', 5, 30))),
  79. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  80. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  81. 'imrequiredonboth' => array(
  82. 'required' => array('rule' => 'alphaNumeric'),
  83. ),
  84. 'string_required' => 'notEmpty',
  85. 'imalsorequired' => array('_allowEmpty' => false, array('rule' => 'alphaNumeric')),
  86. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  87. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  88. 'imnotrequired' => array(
  89. '_allowEmpty' => true,
  90. array('rule' => 'alphaNumeric')
  91. ),
  92. 'imalsonotrequired' => array(
  93. '_allowEmpty' => true,
  94. 'alpha' => array('rule' => 'alphaNumeric'),
  95. 'between' => array('rule' => array('between', 5, 30)),
  96. ),
  97. 'imnotrequiredeither' => array(
  98. '_allowEmpty' => true,
  99. array('rule' => array('between', 5, 30))
  100. ),
  101. 'iamrequiredalways' => array(
  102. 'email' => array('rule' => 'email'),
  103. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  104. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  105. ),
  106. 'boolean_field' => array('rule' => 'boolean')
  107. );
  108. /**
  109. * schema method
  110. *
  111. * @return void
  112. */
  113. public function setSchema($schema) {
  114. $this->_schema = $schema;
  115. }
  116. /**
  117. * hasAndBelongsToMany property
  118. *
  119. * @var array
  120. */
  121. public $hasAndBelongsToMany = array(
  122. 'ContactTag' => array(
  123. 'className' => 'Cake\Test\TestCase\View\Helper\ContactTag',
  124. 'with' => 'Cake\Test\TestCase\View\Helper\ContactTagsContact'
  125. )
  126. );
  127. /**
  128. * hasAndBelongsToMany property
  129. *
  130. * @var array
  131. */
  132. public $belongsTo = array(
  133. 'User' => array('className' => 'UserForm'
  134. ));
  135. }
  136. /**
  137. * ContactTagsContact class
  138. *
  139. */
  140. class ContactTagsContactsTable extends Table {
  141. /**
  142. * Default schema
  143. *
  144. * @var array
  145. */
  146. protected $_schema = array(
  147. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  148. 'contact_tag_id' => array(
  149. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  150. )
  151. );
  152. /**
  153. * schema method
  154. *
  155. * @return void
  156. */
  157. public function setSchema($schema) {
  158. $this->_schema = $schema;
  159. }
  160. }
  161. /**
  162. * ContactNonStandardPk class
  163. *
  164. */
  165. class ContactNonStandardPk extends ContactsTable {
  166. /**
  167. * primaryKey property
  168. *
  169. * @var string
  170. */
  171. public $primaryKey = 'pk';
  172. /**
  173. * schema method
  174. *
  175. * @return void
  176. */
  177. public function schema($field = false) {
  178. $this->_schema = parent::schema();
  179. $this->_schema['pk'] = $this->_schema['id'];
  180. unset($this->_schema['id']);
  181. return $this->_schema;
  182. }
  183. }
  184. /**
  185. * ContactTag class
  186. *
  187. */
  188. class ContactTagsTable extends Table {
  189. /**
  190. * schema definition
  191. *
  192. * @var array
  193. */
  194. protected $_schema = array(
  195. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  196. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  197. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  198. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  199. );
  200. }
  201. /**
  202. * UserForm class
  203. *
  204. */
  205. class UserFormsTable extends Table {
  206. /**
  207. * hasMany property
  208. *
  209. * @var array
  210. */
  211. public $hasMany = array(
  212. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  213. ));
  214. /**
  215. * schema definition
  216. *
  217. * @var array
  218. */
  219. protected $_schema = array(
  220. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  221. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  222. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  223. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  224. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  225. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  226. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  227. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  228. );
  229. }
  230. /**
  231. * OpenidUrl class
  232. *
  233. */
  234. class OpenidUrlsTable extends Table {
  235. /**
  236. * belongsTo property
  237. *
  238. * @var array
  239. */
  240. public $belongsTo = array('UserForm' => array(
  241. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  242. ));
  243. /**
  244. * validate property
  245. *
  246. * @var array
  247. */
  248. public $validate = array('openid_not_registered' => array());
  249. /**
  250. * schema method
  251. *
  252. * @var array
  253. */
  254. protected $_schema = array(
  255. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  256. 'user_form_id' => array(
  257. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  258. ),
  259. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  260. );
  261. /**
  262. * beforeValidate method
  263. *
  264. * @return void
  265. */
  266. public function beforeValidate($options = array()) {
  267. $this->invalidate('openid_not_registered');
  268. return true;
  269. }
  270. }
  271. /**
  272. * ValidateUser class
  273. *
  274. */
  275. class ValidateUsersTable extends Table {
  276. /**
  277. * hasOne property
  278. *
  279. * @var array
  280. */
  281. public $hasOne = array('ValidateProfile' => array(
  282. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  283. ));
  284. /**
  285. * schema method
  286. *
  287. * @var array
  288. */
  289. protected $_schema = array(
  290. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  291. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  292. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  293. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  294. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => '6,3'),
  295. 'ratio' => array('type' => 'decimal', 'null' => false, 'length' => '10,6'),
  296. 'population' => array('type' => 'decimal', 'null' => false, 'length' => '15,0'),
  297. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  298. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  299. );
  300. /**
  301. * beforeValidate method
  302. *
  303. * @param array $options
  304. * @return void
  305. */
  306. public function beforeValidate($options = array()) {
  307. $this->invalidate('email');
  308. return false;
  309. }
  310. }
  311. /**
  312. * ValidateProfile class
  313. *
  314. */
  315. class ValidateProfilesTable extends Table {
  316. /**
  317. * useTable property
  318. *
  319. * @var boolean
  320. */
  321. public $useTable = false;
  322. /**
  323. * schema property
  324. *
  325. * @var array
  326. */
  327. protected $_schema = array(
  328. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  329. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  330. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  331. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  332. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  333. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  334. );
  335. /**
  336. * hasOne property
  337. *
  338. * @var array
  339. */
  340. public $hasOne = array('ValidateItem' => array(
  341. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  342. ));
  343. /**
  344. * belongsTo property
  345. *
  346. * @var array
  347. */
  348. public $belongsTo = array('ValidateUser' => array(
  349. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  350. ));
  351. /**
  352. * beforeValidate method
  353. *
  354. * @return void
  355. */
  356. public function beforeValidate($options = array()) {
  357. $this->invalidate('full_name');
  358. $this->invalidate('city');
  359. return false;
  360. }
  361. }
  362. /**
  363. * ValidateItem class
  364. *
  365. */
  366. class ValidateItemsTable extends Table {
  367. /**
  368. * schema property
  369. *
  370. * @var array
  371. */
  372. protected $_schema = array(
  373. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  374. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  375. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  376. 'description' => array(
  377. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  378. ),
  379. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  380. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  381. );
  382. /**
  383. * belongsTo property
  384. *
  385. * @var array
  386. */
  387. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  388. /**
  389. * beforeValidate method
  390. *
  391. * @return void
  392. */
  393. public function beforeValidate($options = array()) {
  394. $this->invalidate('description');
  395. return false;
  396. }
  397. }
  398. /**
  399. * TestMail class
  400. *
  401. */
  402. class TestMailsTable extends Table {
  403. }
  404. /**
  405. * FormHelperTest class
  406. *
  407. * @property FormHelper $Form
  408. */
  409. class FormHelperTest extends TestCase {
  410. /**
  411. * Fixtures to be used
  412. *
  413. * @var array
  414. */
  415. public $fixtures = array('core.article');
  416. /**
  417. * Do not load the fixtures by default
  418. *
  419. * @var boolean
  420. */
  421. public $autoFixtures = false;
  422. /**
  423. * setUp method
  424. *
  425. * @return void
  426. */
  427. public function setUp() {
  428. parent::setUp();
  429. Configure::write('Config.language', 'eng');
  430. Configure::write('App.base', '');
  431. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  432. Configure::delete('Asset');
  433. $this->View = new View(null);
  434. $this->Form = new FormHelper($this->View);
  435. $this->Form->Html = new HtmlHelper($this->View);
  436. $this->Form->request = new Request('articles/add');
  437. $this->Form->request->here = '/articles/add';
  438. $this->Form->request['controller'] = 'articles';
  439. $this->Form->request['action'] = 'add';
  440. $this->Form->request->webroot = '';
  441. $this->Form->request->base = '';
  442. $this->dateRegex = array(
  443. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  444. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  445. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  446. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  447. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  448. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  449. );
  450. $this->article = [
  451. 'schema' => [
  452. 'id' => ['type' => 'integer'],
  453. 'author_id' => ['type' => 'integer', 'null' => true],
  454. 'title' => ['type' => 'string', 'null' => true],
  455. 'body' => 'text',
  456. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  457. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  458. ],
  459. 'required' => [
  460. 'author_id' => true,
  461. 'title' => true,
  462. ]
  463. ];
  464. Configure::write('Security.salt', 'foo!');
  465. Router::connect('/:controller', array('action' => 'index'));
  466. Router::connect('/:controller/:action/*');
  467. }
  468. /**
  469. * tearDown method
  470. *
  471. * @return void
  472. */
  473. public function tearDown() {
  474. parent::tearDown();
  475. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  476. }
  477. /**
  478. * Test registering a new widget class and rendering it.
  479. *
  480. * @return void
  481. */
  482. public function testAddWidgetAndRenderWidget() {
  483. $data = [
  484. 'val' => 1
  485. ];
  486. $mock = $this->getMock('Cake\View\Widget\WidgetInterface');
  487. $this->assertNull($this->Form->addWidget('test', $mock));
  488. $mock->expects($this->once())
  489. ->method('render')
  490. ->with($data)
  491. ->will($this->returnValue('HTML'));
  492. $result = $this->Form->widget('test', $data);
  493. $this->assertEquals('HTML', $result);
  494. }
  495. /**
  496. * Test registering an invalid widget class.
  497. *
  498. * @expectedException \RuntimeException
  499. * @return void
  500. */
  501. public function testAddWidgetInvalid() {
  502. $mock = new \StdClass();
  503. $this->Form->addWidget('test', $mock);
  504. $this->Form->widget('test');
  505. }
  506. /**
  507. * Test adding a new context class.
  508. *
  509. * @return void
  510. */
  511. public function testAddContextProvider() {
  512. $context = 'My data';
  513. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  514. $this->assertInstanceOf('Cake\Network\Request', $request);
  515. $this->assertEquals($context, $data['entity']);
  516. return $this->getMock('Cake\View\Form\ContextInterface');
  517. });
  518. $this->Form->create($context);
  519. }
  520. /**
  521. * Test adding an invalid context class.
  522. *
  523. * @expectedException RuntimeException
  524. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  525. * @return void
  526. */
  527. public function testAddContextProviderInvalid() {
  528. $context = 'My data';
  529. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  530. return new \StdClass();
  531. });
  532. $this->Form->create($context);
  533. }
  534. /**
  535. * Provides context options for create().
  536. *
  537. * @return array
  538. */
  539. public function contextSelectionProvider() {
  540. $entity = new Article();
  541. $collection = $this->getMock('Cake\Collection\Collection', ['extract'], [[$entity]]);
  542. $data = [
  543. 'schema' => [
  544. 'title' => ['type' => 'string']
  545. ]
  546. ];
  547. return [
  548. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  549. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  550. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  551. 'none' => [null, 'Cake\View\Form\NullContext'],
  552. 'false' => [false, 'Cake\View\Form\NullContext'],
  553. ];
  554. }
  555. /**
  556. * Test default context selection in create()
  557. *
  558. * @dataProvider contextSelectionProvider
  559. * @return void
  560. */
  561. public function testCreateContextSelectionBuiltIn($data, $class) {
  562. $this->loadFixtures('Article');
  563. $this->Form->create($data);
  564. $this->assertInstanceOf($class, $this->Form->context());
  565. }
  566. /**
  567. * Data provider for type option.
  568. *
  569. * @return array
  570. */
  571. public static function requestTypeProvider() {
  572. return [
  573. // type, method, override
  574. ['post', 'post', 'POST'],
  575. ['put', 'post', 'PUT'],
  576. ['patch', 'post', 'PATCH'],
  577. ['delete', 'post', 'DELETE'],
  578. ];
  579. }
  580. /**
  581. * Test creating file forms.
  582. *
  583. * @return void
  584. */
  585. public function testCreateFile() {
  586. $encoding = strtolower(Configure::read('App.encoding'));
  587. $result = $this->Form->create(false, array('type' => 'file'));
  588. $expected = array(
  589. 'form' => array(
  590. 'method' => 'post', 'action' => '/articles/add',
  591. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  592. ),
  593. 'div' => array('style' => 'display:none;'),
  594. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  595. '/div'
  596. );
  597. $this->assertTags($result, $expected);
  598. }
  599. /**
  600. * Test creating GET forms.
  601. *
  602. * @return void
  603. */
  604. public function testCreateGet() {
  605. $encoding = strtolower(Configure::read('App.encoding'));
  606. $result = $this->Form->create(false, array('type' => 'get'));
  607. $expected = array('form' => array(
  608. 'method' => 'get', 'action' => '/articles/add',
  609. 'accept-charset' => $encoding
  610. ));
  611. $this->assertTags($result, $expected);
  612. }
  613. /**
  614. * test the create() method
  615. *
  616. * @dataProvider requestTypeProvider
  617. * @return void
  618. */
  619. public function testCreateTypeOptions($type, $method, $override) {
  620. $encoding = strtolower(Configure::read('App.encoding'));
  621. $result = $this->Form->create(false, array('type' => $type));
  622. $expected = array(
  623. 'form' => array(
  624. 'method' => $method, 'action' => '/articles/add',
  625. 'accept-charset' => $encoding
  626. ),
  627. 'div' => array('style' => 'display:none;'),
  628. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => $override),
  629. '/div'
  630. );
  631. $this->assertTags($result, $expected);
  632. }
  633. /**
  634. * Test opening a form for an update operation.
  635. *
  636. * @return void
  637. */
  638. public function testCreateUpdateForm() {
  639. $encoding = strtolower(Configure::read('App.encoding'));
  640. $this->Form->request->here = '/articles/edit/1';
  641. $this->Form->request['action'] = 'edit';
  642. $this->article['defaults']['id'] = 1;
  643. $result = $this->Form->create($this->article);
  644. $expected = array(
  645. 'form' => array(
  646. 'method' => 'post', 'action' => '/articles/edit/1',
  647. 'accept-charset' => $encoding
  648. ),
  649. 'div' => array('style' => 'display:none;'),
  650. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  651. '/div'
  652. );
  653. $this->assertTags($result, $expected);
  654. }
  655. /**
  656. * Test the onsubmit option for create()
  657. *
  658. * @return void
  659. */
  660. public function testCreateOnSubmit() {
  661. $this->Form->request->data = [];
  662. $this->Form->request['controller'] = 'articles';
  663. $result = $this->Form->create($this->article, ['url' => ['action' => 'index', 'param'], 'default' => false]);
  664. $expected = array(
  665. 'form' => array(
  666. 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/articles/index/param',
  667. 'accept-charset' => 'utf-8'
  668. ),
  669. 'div' => array('style' => 'display:none;'),
  670. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  671. '/div'
  672. );
  673. $this->assertTags($result, $expected);
  674. $this->Form->request->data = [];
  675. $this->Form->request['controller'] = 'articles';
  676. $result = $this->Form->create($this->article, array(
  677. 'url' => array('action' => 'index', 'param'),
  678. 'default' => false,
  679. 'onsubmit' => 'someFunction();'
  680. ));
  681. $expected = array(
  682. 'form' => array(
  683. 'method' => 'post',
  684. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  685. 'action' => '/articles/index/param',
  686. 'accept-charset' => 'utf-8'
  687. ),
  688. 'div' => array('style' => 'display:none;'),
  689. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  690. '/div'
  691. );
  692. $this->assertTags($result, $expected);
  693. }
  694. /**
  695. * test create() with automatic url generation
  696. *
  697. * @return void
  698. */
  699. public function testCreateAutoUrl() {
  700. $encoding = strtolower(Configure::read('App.encoding'));
  701. $this->Form->request['action'] = 'delete';
  702. $this->Form->request->here = '/articles/delete/10';
  703. $this->Form->request->base = '';
  704. $result = $this->Form->create($this->article);
  705. $expected = array(
  706. 'form' => array(
  707. 'method' => 'post', 'action' => '/articles/delete/10',
  708. 'accept-charset' => $encoding
  709. ),
  710. 'div' => array('style' => 'display:none;'),
  711. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  712. '/div'
  713. );
  714. $this->assertTags($result, $expected);
  715. $this->article['defaults'] = ['id' => 1];
  716. $this->Form->request->here = '/articles/edit/1';
  717. $this->Form->request['action'] = 'delete';
  718. $result = $this->Form->create($this->article, ['action' => 'edit']);
  719. $expected = array(
  720. 'form' => array(
  721. 'method' => 'post',
  722. 'action' => '/articles/edit/1',
  723. 'accept-charset' => $encoding
  724. ),
  725. 'div' => array('style' => 'display:none;'),
  726. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  727. '/div'
  728. );
  729. $this->assertTags($result, $expected);
  730. $this->Form->request['action'] = 'add';
  731. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  732. $expected = array(
  733. 'form' => array(
  734. 'method' => 'post',
  735. 'action' => '/articles/publish/1',
  736. 'accept-charset' => $encoding
  737. ),
  738. 'div' => array('style' => 'display:none;'),
  739. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  740. '/div'
  741. );
  742. $this->assertTags($result, $expected);
  743. $result = $this->Form->create($this->article, array('url' => '/articles/publish'));
  744. $expected = array(
  745. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  746. 'div' => array('style' => 'display:none;'),
  747. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  748. '/div'
  749. );
  750. $this->assertTags($result, $expected);
  751. $this->Form->request['controller'] = 'pages';
  752. $result = $this->Form->create($this->article, array('action' => 'signup'));
  753. $expected = array(
  754. 'form' => array(
  755. 'method' => 'post', 'action' => '/pages/signup/1',
  756. 'accept-charset' => $encoding
  757. ),
  758. 'div' => array('style' => 'display:none;'),
  759. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  760. '/div'
  761. );
  762. $this->assertTags($result, $expected);
  763. }
  764. /**
  765. * test create() with a custom route
  766. *
  767. * @return void
  768. */
  769. public function testCreateCustomRoute() {
  770. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  771. $encoding = strtolower(Configure::read('App.encoding'));
  772. $this->Form->request['controller'] = 'users';
  773. $result = $this->Form->create(false, array('action' => 'login'));
  774. $expected = array(
  775. 'form' => array(
  776. 'method' => 'post', 'action' => '/login',
  777. 'accept-charset' => $encoding
  778. ),
  779. 'div' => array('style' => 'display:none;'),
  780. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  781. '/div'
  782. );
  783. $this->assertTags($result, $expected);
  784. }
  785. /**
  786. * test that inputDefaults are stored and used.
  787. *
  788. * @return void
  789. */
  790. public function testCreateWithInputDefaults() {
  791. $this->markTestIncomplete('Need to revisit once models work again.');
  792. $this->Form->create('User', array(
  793. 'inputDefaults' => array(
  794. 'div' => false,
  795. 'label' => false,
  796. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  797. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  798. )
  799. ));
  800. $result = $this->Form->input('username');
  801. $expected = array(
  802. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername')
  803. );
  804. $this->assertTags($result, $expected);
  805. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  806. $expected = array(
  807. 'div' => array('class' => 'input text'),
  808. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  809. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  810. '/div'
  811. );
  812. $this->assertTags($result, $expected);
  813. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  814. $expected = array(
  815. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  816. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  817. );
  818. $this->assertTags($result, $expected);
  819. $this->Form->create('User', array(
  820. 'inputDefaults' => array(
  821. 'div' => false,
  822. 'label' => array('class' => 'nice', 'for' => 'changed'),
  823. )
  824. ));
  825. $result = $this->Form->input('username', array('div' => true));
  826. $expected = array(
  827. 'div' => array('class' => 'input text'),
  828. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  829. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  830. '/div'
  831. );
  832. $this->assertTags($result, $expected);
  833. }
  834. /**
  835. * test automatic accept-charset overriding
  836. *
  837. * @return void
  838. */
  839. public function testCreateWithAcceptCharset() {
  840. $result = $this->Form->create($this->article, array(
  841. 'type' => 'post', 'action' => 'index', 'encoding' => 'iso-8859-1'
  842. )
  843. );
  844. $expected = array(
  845. 'form' => array(
  846. 'method' => 'post', 'action' => '/articles',
  847. 'accept-charset' => 'iso-8859-1'
  848. ),
  849. 'div' => array('style' => 'display:none;'),
  850. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  851. '/div'
  852. );
  853. $this->assertTags($result, $expected);
  854. }
  855. /**
  856. * Test base form URL when url param is passed with multiple parameters (&)
  857. *
  858. */
  859. public function testCreateQuerystringrequest() {
  860. $encoding = strtolower(Configure::read('App.encoding'));
  861. $result = $this->Form->create($this->article, array(
  862. 'type' => 'post',
  863. 'escape' => false,
  864. 'url' => array(
  865. 'controller' => 'controller',
  866. 'action' => 'action',
  867. '?' => array('param1' => 'value1', 'param2' => 'value2')
  868. )
  869. ));
  870. $expected = array(
  871. 'form' => array(
  872. 'method' => 'post',
  873. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  874. 'accept-charset' => $encoding
  875. ),
  876. 'div' => array('style' => 'display:none;'),
  877. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  878. '/div'
  879. );
  880. $this->assertTags($result, $expected);
  881. $result = $this->Form->create($this->article, array(
  882. 'type' => 'post',
  883. 'url' => array(
  884. 'controller' => 'controller',
  885. 'action' => 'action',
  886. '?' => array('param1' => 'value1', 'param2' => 'value2')
  887. )
  888. ));
  889. $this->assertTags($result, $expected);
  890. }
  891. /**
  892. * test that create() doesn't cause errors by multiple id's being in the primary key
  893. * as could happen with multiple select or checkboxes.
  894. *
  895. * @return void
  896. */
  897. public function testCreateWithMultipleIdInData() {
  898. $encoding = strtolower(Configure::read('App.encoding'));
  899. $this->Form->request->data['Article']['id'] = array(1, 2);
  900. $result = $this->Form->create($this->article);
  901. $expected = array(
  902. 'form' => array(
  903. 'method' => 'post',
  904. 'action' => '/articles/add',
  905. 'accept-charset' => $encoding
  906. ),
  907. 'div' => array('style' => 'display:none;'),
  908. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  909. '/div'
  910. );
  911. $this->assertTags($result, $expected);
  912. }
  913. /**
  914. * test that create() doesn't add in extra passed params.
  915. *
  916. * @return void
  917. */
  918. public function testCreatePassedArgs() {
  919. $encoding = strtolower(Configure::read('App.encoding'));
  920. $this->Form->request->data['Article']['id'] = 1;
  921. $result = $this->Form->create($this->article, array(
  922. 'type' => 'post',
  923. 'escape' => false,
  924. 'url' => array(
  925. 'action' => 'edit',
  926. 'myparam'
  927. )
  928. ));
  929. $expected = array(
  930. 'form' => array(
  931. 'method' => 'post',
  932. 'action' => '/articles/edit/myparam',
  933. 'accept-charset' => $encoding
  934. ),
  935. 'div' => array('style' => 'display:none;'),
  936. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  937. '/div'
  938. );
  939. $this->assertTags($result, $expected);
  940. }
  941. /**
  942. * test creating a get form, and get form inputs.
  943. *
  944. * @return void
  945. */
  946. public function testGetFormCreate() {
  947. $encoding = strtolower(Configure::read('App.encoding'));
  948. $result = $this->Form->create($this->article, array('type' => 'get'));
  949. $this->assertTags($result, array('form' => array(
  950. 'method' => 'get', 'action' => '/articles/add',
  951. 'accept-charset' => $encoding
  952. )));
  953. $result = $this->Form->text('title');
  954. $this->assertTags($result, array('input' => array(
  955. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  956. )));
  957. $result = $this->Form->password('password');
  958. $this->assertTags($result, array('input' => array(
  959. 'name' => 'password', 'type' => 'password'
  960. )));
  961. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  962. $result = $this->Form->text('user_form');
  963. $this->assertTags($result, array('input' => array(
  964. 'name' => 'user_form', 'type' => 'text'
  965. )));
  966. }
  967. /**
  968. * test get form, and inputs when the model param is false
  969. *
  970. * @return void
  971. */
  972. public function testGetFormWithFalseModel() {
  973. $encoding = strtolower(Configure::read('App.encoding'));
  974. $this->Form->request['controller'] = 'contact_test';
  975. $result = $this->Form->create(false, array(
  976. 'type' => 'get', 'url' => array('controller' => 'contact_test')
  977. ));
  978. $expected = array('form' => array(
  979. 'method' => 'get', 'action' => '/contact_test/add',
  980. 'accept-charset' => $encoding
  981. ));
  982. $this->assertTags($result, $expected);
  983. $result = $this->Form->text('reason');
  984. $expected = array(
  985. 'input' => array('type' => 'text', 'name' => 'reason')
  986. );
  987. $this->assertTags($result, $expected);
  988. }
  989. /**
  990. * testFormCreateWithSecurity method
  991. *
  992. * Test form->create() with security key.
  993. *
  994. * @return void
  995. */
  996. public function testCreateWithSecurity() {
  997. $this->Form->request->params['_csrfToken'] = 'testKey';
  998. $encoding = strtolower(Configure::read('App.encoding'));
  999. $result = $this->Form->create($this->article, [
  1000. 'url' => '/articles/publish',
  1001. ]);
  1002. $expected = array(
  1003. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  1004. 'div' => array('style' => 'display:none;'),
  1005. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1006. array('input' => array(
  1007. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  1008. )),
  1009. '/div'
  1010. );
  1011. $this->assertTags($result, $expected);
  1012. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1013. $expected['form']['id'] = 'MyForm';
  1014. $this->assertTags($result, $expected);
  1015. }
  1016. /**
  1017. * testFormCreateGetNoSecurity method
  1018. *
  1019. * Test form->create() with no security key as its a get form
  1020. *
  1021. * @return void
  1022. */
  1023. public function testCreateEndGetNoSecurity() {
  1024. $this->Form->request->params['_csrfToken'] = 'testKey';
  1025. $encoding = strtolower(Configure::read('App.encoding'));
  1026. $article = new Article();
  1027. $result = $this->Form->create($article, [
  1028. 'type' => 'get',
  1029. 'url' => '/contacts/add'
  1030. ]);
  1031. $this->assertNotContains('testKey', $result);
  1032. $result = $this->Form->end('Save');
  1033. $this->assertNotContains('testKey', $result);
  1034. }
  1035. /**
  1036. * test that create() clears the fields property so it starts fresh
  1037. *
  1038. * @return void
  1039. */
  1040. public function testCreateClearingFields() {
  1041. $this->Form->fields = array('model_id');
  1042. $this->Form->create($this->article);
  1043. $this->assertEquals(array(), $this->Form->fields);
  1044. }
  1045. /**
  1046. * Tests form hash generation with model-less data
  1047. *
  1048. * @return void
  1049. */
  1050. public function testValidateHashNoModel() {
  1051. $this->Form->request->params['_Token'] = 'foo';
  1052. $result = $this->Form->secure(array('anything'));
  1053. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1054. }
  1055. /**
  1056. * Tests that hidden fields generated for checkboxes don't get locked
  1057. *
  1058. * @return void
  1059. */
  1060. public function testNoCheckboxLocking() {
  1061. $this->Form->request->params['_Token'] = 'foo';
  1062. $this->assertSame([], $this->Form->fields);
  1063. $this->Form->checkbox('check', array('value' => '1'));
  1064. $this->assertSame($this->Form->fields, array('check'));
  1065. }
  1066. /**
  1067. * testFormSecurityFields method
  1068. *
  1069. * Test generation of secure form hash generation.
  1070. *
  1071. * @return void
  1072. */
  1073. public function testFormSecurityFields() {
  1074. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  1075. $this->Form->request->params['_Token'] = 'testKey';
  1076. $result = $this->Form->secure($fields);
  1077. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  1078. $hash .= ':' . 'Model.valid';
  1079. $hash = urlencode($hash);
  1080. $expected = array(
  1081. 'div' => array('style' => 'display:none;'),
  1082. array('input' => array(
  1083. 'type' => 'hidden',
  1084. 'name' => '_Token[fields]',
  1085. 'value' => $hash
  1086. )),
  1087. array('input' => array(
  1088. 'type' => 'hidden',
  1089. 'name' => '_Token[unlocked]',
  1090. 'value' => '',
  1091. )),
  1092. '/div'
  1093. );
  1094. $this->assertTags($result, $expected);
  1095. }
  1096. /**
  1097. * Tests correct generation of number fields for double and float fields
  1098. *
  1099. * @return void
  1100. */
  1101. public function testTextFieldGenerationForFloats() {
  1102. $this->markTestIncomplete('Need to revisit once models work again.');
  1103. $this->article['schema'] = [
  1104. 'foo' => [
  1105. 'type' => 'float',
  1106. 'null' => false,
  1107. 'default' => null,
  1108. 'length' => 10
  1109. ]
  1110. ];
  1111. $this->Form->create('Contact');
  1112. $result = $this->Form->input('foo');
  1113. $expected = array(
  1114. 'div' => array('class' => 'input number'),
  1115. 'label' => array('for' => 'ContactFoo'),
  1116. 'Foo',
  1117. '/label',
  1118. array('input' => array(
  1119. 'type' => 'number',
  1120. 'name' => 'Contact[foo]',
  1121. 'id' => 'ContactFoo',
  1122. 'step' => 'any'
  1123. )),
  1124. '/div'
  1125. );
  1126. $this->assertTags($result, $expected);
  1127. $result = $this->Form->input('foo', array('step' => 0.5));
  1128. $expected = array(
  1129. 'div' => array('class' => 'input number'),
  1130. 'label' => array('for' => 'ContactFoo'),
  1131. 'Foo',
  1132. '/label',
  1133. array('input' => array(
  1134. 'type' => 'number',
  1135. 'name' => 'Contact[foo]',
  1136. 'id' => 'ContactFoo',
  1137. 'step' => '0.5'
  1138. )),
  1139. '/div'
  1140. );
  1141. $this->assertTags($result, $expected);
  1142. }
  1143. /**
  1144. * Tests correct generation of number fields for integer fields
  1145. *
  1146. * @return void
  1147. */
  1148. public function testTextFieldTypeNumberGenerationForIntegers() {
  1149. $this->markTestIncomplete('Need to revisit once models work again.');
  1150. $model->setSchema(array('foo' => array(
  1151. 'type' => 'integer',
  1152. 'null' => false,
  1153. 'default' => null,
  1154. 'length' => null
  1155. )));
  1156. $this->Form->create('Contact');
  1157. $result = $this->Form->input('foo');
  1158. $expected = array(
  1159. 'div' => array('class' => 'input number'),
  1160. 'label' => array('for' => 'ContactFoo'),
  1161. 'Foo',
  1162. '/label',
  1163. array('input' => array(
  1164. 'type' => 'number', 'name' => 'Contact[foo]',
  1165. 'id' => 'ContactFoo'
  1166. )),
  1167. '/div'
  1168. );
  1169. $this->assertTags($result, $expected);
  1170. }
  1171. /**
  1172. * Tests correct generation of file upload fields for binary fields
  1173. *
  1174. * @return void
  1175. */
  1176. public function testFileUploadFieldTypeGenerationForBinaries() {
  1177. $this->markTestIncomplete('Need to revisit once models work again.');
  1178. $model->setSchema(array('foo' => array(
  1179. 'type' => 'binary',
  1180. 'null' => false,
  1181. 'default' => null,
  1182. 'length' => 1024
  1183. )));
  1184. $this->Form->create('Contact');
  1185. $result = $this->Form->input('foo');
  1186. $expected = array(
  1187. 'div' => array('class' => 'input file'),
  1188. 'label' => array('for' => 'ContactFoo'),
  1189. 'Foo',
  1190. '/label',
  1191. array('input' => array(
  1192. 'type' => 'file', 'name' => 'data[Contact][foo]',
  1193. 'id' => 'ContactFoo'
  1194. )),
  1195. '/div'
  1196. );
  1197. $this->assertTags($result, $expected);
  1198. }
  1199. /**
  1200. * testFormSecurityMultipleFields method
  1201. *
  1202. * Test secure() with multiple row form. Ensure hash is correct.
  1203. *
  1204. * @return void
  1205. */
  1206. public function testFormSecurityMultipleFields() {
  1207. $this->Form->request->params['_Token'] = 'foo';
  1208. $fields = array(
  1209. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1210. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1211. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1212. );
  1213. $result = $this->Form->secure($fields);
  1214. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1215. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1216. $expected = array(
  1217. 'div' => array('style' => 'display:none;'),
  1218. array('input' => array(
  1219. 'type' => 'hidden', 'name' => '_Token[fields]',
  1220. 'value' => $hash
  1221. )),
  1222. array('input' => array(
  1223. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1224. 'value' => ''
  1225. )),
  1226. '/div'
  1227. );
  1228. $this->assertTags($result, $expected);
  1229. }
  1230. /**
  1231. * testFormSecurityMultipleSubmitButtons
  1232. *
  1233. * test form submit generation and ensure that _Token is only created on end()
  1234. *
  1235. * @return void
  1236. */
  1237. public function testFormSecurityMultipleSubmitButtons() {
  1238. $this->Form->request->params['_Token'] = 'testKey';
  1239. $this->Form->create($this->article);
  1240. $this->Form->text('Address.title');
  1241. $this->Form->text('Address.first_name');
  1242. $result = $this->Form->submit('Save', array('name' => 'save'));
  1243. $expected = array(
  1244. 'div' => array('class' => 'submit'),
  1245. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  1246. '/div',
  1247. );
  1248. $this->assertTags($result, $expected);
  1249. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  1250. $expected = array(
  1251. 'div' => array('class' => 'submit'),
  1252. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  1253. '/div',
  1254. );
  1255. $this->assertTags($result, $expected);
  1256. $result = $this->Form->end();
  1257. $expected = array(
  1258. 'div' => array('style' => 'display:none;'),
  1259. array('input' => array(
  1260. 'type' => 'hidden',
  1261. 'name' => '_Token[fields]',
  1262. 'value'
  1263. )),
  1264. array('input' => array(
  1265. 'type' => 'hidden',
  1266. 'name' => '_Token[unlocked]',
  1267. 'value' => 'cancel%7Csave'
  1268. )),
  1269. '/div'
  1270. );
  1271. $this->assertTags($result, $expected);
  1272. }
  1273. /**
  1274. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1275. *
  1276. * @return void
  1277. */
  1278. public function testSecurityButtonNestedNamed() {
  1279. $key = 'testKey';
  1280. $this->Form->request->params['_csrfToken'] = $key;
  1281. $this->Form->create('Addresses');
  1282. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1283. $result = $this->Form->unlockField();
  1284. $this->assertEquals(array('Address.button'), $result);
  1285. }
  1286. /**
  1287. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1288. *
  1289. * @return void
  1290. */
  1291. public function testSecuritySubmitNestedNamed() {
  1292. $this->Form->request->params['_Token'] = 'testKey';
  1293. $this->Form->create($this->article);
  1294. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1295. $result = $this->Form->unlockField();
  1296. $this->assertEquals(array('Address.button'), $result);
  1297. }
  1298. /**
  1299. * Test that the correct fields are unlocked for image submits with no names.
  1300. *
  1301. * @return void
  1302. */
  1303. public function testSecuritySubmitImageNoName() {
  1304. $key = 'testKey';
  1305. $this->Form->request->params['_Token'] = 'testKey';
  1306. $this->Form->create(false);
  1307. $result = $this->Form->submit('save.png');
  1308. $expected = array(
  1309. 'div' => array('class' => 'submit'),
  1310. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  1311. '/div'
  1312. );
  1313. $this->assertTags($result, $expected);
  1314. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  1315. }
  1316. /**
  1317. * Test that the correct fields are unlocked for image submits with names.
  1318. *
  1319. * @return void
  1320. */
  1321. public function testSecuritySubmitImageName() {
  1322. $this->Form->request->params['_Token'] = 'testKey';
  1323. $this->Form->create(null);
  1324. $result = $this->Form->submit('save.png', array('name' => 'test'));
  1325. $expected = array(
  1326. 'div' => array('class' => 'submit'),
  1327. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  1328. '/div'
  1329. );
  1330. $this->assertTags($result, $expected);
  1331. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  1332. }
  1333. /**
  1334. * testFormSecurityMultipleInputFields method
  1335. *
  1336. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1337. *
  1338. * @return void
  1339. */
  1340. public function testFormSecurityMultipleInputFields() {
  1341. $this->markTestIncomplete('Need to revisit once models work again.');
  1342. $key = 'testKey';
  1343. $this->Form->request->params['_csrfToken'] = $key;
  1344. $this->Form->create('Addresses');
  1345. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1346. $this->Form->input('Addresses.0.title');
  1347. $this->Form->input('Addresses.0.first_name');
  1348. $this->Form->input('Addresses.0.last_name');
  1349. $this->Form->input('Addresses.0.address');
  1350. $this->Form->input('Addresses.0.city');
  1351. $this->Form->input('Addresses.0.phone');
  1352. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  1353. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1354. $this->Form->input('Addresses.1.title');
  1355. $this->Form->input('Addresses.1.first_name');
  1356. $this->Form->input('Addresses.1.last_name');
  1357. $this->Form->input('Addresses.1.address');
  1358. $this->Form->input('Addresses.1.city');
  1359. $this->Form->input('Addresses.1.phone');
  1360. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  1361. $result = $this->Form->secure($this->Form->fields);
  1362. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  1363. $expected = array(
  1364. 'div' => array('style' => 'display:none;'),
  1365. array('input' => array(
  1366. 'type' => 'hidden', 'name' => '_Token[fields]',
  1367. 'value' => $hash
  1368. )),
  1369. array('input' => array(
  1370. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1371. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1372. )),
  1373. '/div'
  1374. );
  1375. $this->assertTags($result, $expected);
  1376. }
  1377. /**
  1378. * Test form security with Model.field.0 style inputs
  1379. *
  1380. * @return void
  1381. */
  1382. public function testFormSecurityArrayFields() {
  1383. $this->markTestIncomplete('Need to revisit once models work again.');
  1384. $key = 'testKey';
  1385. $this->Form->request->params['_csrfToken'] = $key;
  1386. $this->Form->create('Address');
  1387. $this->Form->input('Address.primary.1');
  1388. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1389. $this->Form->input('Address.secondary.1.0');
  1390. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1391. }
  1392. /**
  1393. * testFormSecurityMultipleInputDisabledFields method
  1394. *
  1395. * test secure form generation with multiple records and disabled fields.
  1396. *
  1397. * @return void
  1398. */
  1399. public function testFormSecurityMultipleInputDisabledFields() {
  1400. $this->markTestIncomplete('Need to revisit once models work again.');
  1401. $key = 'testKey';
  1402. $this->Form->request->params['_csrfToken'] = $key;
  1403. $this->Form->request->params['_Token'] = array(
  1404. 'unlockedFields' => array('first_name', 'address')
  1405. );
  1406. $this->Form->create();
  1407. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1408. $this->Form->input('Addresses.0.title');
  1409. $this->Form->input('Addresses.0.first_name');
  1410. $this->Form->input('Addresses.0.last_name');
  1411. $this->Form->input('Addresses.0.address');
  1412. $this->Form->input('Addresses.0.city');
  1413. $this->Form->input('Addresses.0.phone');
  1414. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1415. $this->Form->input('Addresses.1.title');
  1416. $this->Form->input('Addresses.1.first_name');
  1417. $this->Form->input('Addresses.1.last_name');
  1418. $this->Form->input('Addresses.1.address');
  1419. $this->Form->input('Addresses.1.city');
  1420. $this->Form->input('Addresses.1.phone');
  1421. $result = $this->Form->secure($this->Form->fields);
  1422. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1423. $expected = array(
  1424. 'div' => array('style' => 'display:none;'),
  1425. array('input' => array(
  1426. 'type' => 'hidden', 'name' => '_Token[fields]',
  1427. 'value' => $hash
  1428. )),
  1429. array('input' => array(
  1430. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1431. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1432. )),
  1433. '/div'
  1434. );
  1435. $this->assertTags($result, $expected);
  1436. }
  1437. /**
  1438. * testFormSecurityInputDisabledFields method
  1439. *
  1440. * Test single record form with disabled fields.
  1441. *
  1442. * @return void
  1443. */
  1444. public function testFormSecurityInputUnlockedFields() {
  1445. $this->markTestIncomplete('Need to revisit once models work again.');
  1446. $key = 'testKey';
  1447. $this->Form->request->params['_csrfToken'] = $key;
  1448. $this->Form->request['_Token'] = array(
  1449. 'unlockedFields' => array('first_name', 'address')
  1450. );
  1451. $this->Form->create();
  1452. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1453. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1454. $this->Form->input('Addresses.title');
  1455. $this->Form->input('Addresses.first_name');
  1456. $this->Form->input('Addresses.last_name');
  1457. $this->Form->input('Addresses.address');
  1458. $this->Form->input('Addresses.city');
  1459. $this->Form->input('Addresses.phone');
  1460. $result = $this->Form->fields;
  1461. $expected = array(
  1462. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1463. 'Addresses.city', 'Addresses.phone'
  1464. );
  1465. $this->assertEquals($expected, $result);
  1466. $result = $this->Form->secure($expected);
  1467. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1468. $expected = array(
  1469. 'div' => array('style' => 'display:none;'),
  1470. array('input' => array(
  1471. 'type' => 'hidden', 'name' => '_Token[fields]',
  1472. 'value' => $hash
  1473. )),
  1474. array('input' => array(
  1475. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1476. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1477. )),
  1478. '/div'
  1479. );
  1480. $this->assertTags($result, $expected);
  1481. }
  1482. /**
  1483. * test securing inputs with custom name attributes.
  1484. *
  1485. * @return void
  1486. */
  1487. public function testFormSecureWithCustomNameAttribute() {
  1488. $this->Form->request->params['_Token'] = 'testKey';
  1489. $this->Form->text('UserForm.published', array('name' => 'User[custom]'));
  1490. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1491. $this->Form->text('UserForm.published', array('name' => 'User[custom][another][value]'));
  1492. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1493. }
  1494. /**
  1495. * testFormSecuredInput method
  1496. *
  1497. * Test generation of entire secure form, assertions made on input() output.
  1498. *
  1499. * @return void
  1500. */
  1501. public function testFormSecuredInput() {
  1502. $this->markTestIncomplete('Need to revisit once models work again.');
  1503. $this->Form->request->params['_csrfToken'] = 'testKey';
  1504. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1505. $encoding = strtolower(Configure::read('App.encoding'));
  1506. $expected = array(
  1507. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1508. 'div' => array('style' => 'display:none;'),
  1509. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1510. array('input' => array(
  1511. 'type' => 'hidden', 'name' => '_csrfToken',
  1512. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1513. )),
  1514. '/div'
  1515. );
  1516. $this->assertTags($result, $expected);
  1517. $result = $this->Form->input('ValidateUser.ratio');
  1518. $expected = array(
  1519. 'div' => array('class'),
  1520. 'label' => array('for'),
  1521. 'Ratio',
  1522. '/label',
  1523. 'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
  1524. '/div',
  1525. );
  1526. $this->assertTags($result, $expected);
  1527. $result = $this->Form->input('ValidateUser.population');
  1528. $expected = array(
  1529. 'div' => array('class'),
  1530. 'label' => array('for'),
  1531. 'Population',
  1532. '/label',
  1533. 'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
  1534. '/div',
  1535. );
  1536. $this->assertTags($result, $expected);
  1537. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1538. $expected = array(
  1539. 'div' => array('class' => 'input text'),
  1540. 'label' => array('for' => 'UserFormPublished'),
  1541. 'Published',
  1542. '/label',
  1543. array('input' => array(
  1544. 'type' => 'text', 'name' => 'UserForm[published]',
  1545. 'id' => 'UserFormPublished'
  1546. )),
  1547. '/div'
  1548. );
  1549. $this->assertTags($result, $expected);
  1550. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1551. $expected = array(
  1552. 'div' => array('class' => 'input text'),
  1553. 'label' => array('for' => 'UserFormOther'),
  1554. 'Other',
  1555. '/label',
  1556. array('input' => array(
  1557. 'type' => 'text', 'name' => 'UserForm[other]',
  1558. 'id' => 'UserFormOther'
  1559. )),
  1560. '/div'
  1561. );
  1562. $this->assertTags($result, $expected);
  1563. $result = $this->Form->hidden('UserForm.stuff');
  1564. $expected = array(
  1565. 'input' => array(
  1566. 'type' => 'hidden', 'name' => 'UserForm[stuff]',
  1567. 'id' => 'UserFormStuff'
  1568. ));
  1569. $this->assertTags($result, $expected);
  1570. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1571. $expected = array('input' => array(
  1572. 'type' => 'hidden', 'name' => 'UserForm[hidden]',
  1573. 'value' => '0', 'id' => 'UserFormHidden'
  1574. ));
  1575. $this->assertTags($result, $expected);
  1576. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1577. $expected = array(
  1578. 'div' => array('class' => 'input checkbox'),
  1579. array('input' => array(
  1580. 'type' => 'hidden', 'name' => 'UserForm[something]',
  1581. 'value' => '0', 'id' => 'UserFormSomething_'
  1582. )),
  1583. array('input' => array(
  1584. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  1585. 'value' => '1', 'id' => 'UserFormSomething'
  1586. )),
  1587. 'label' => array('for' => 'UserFormSomething'),
  1588. 'Something',
  1589. '/label',
  1590. '/div'
  1591. );
  1592. $this->assertTags($result, $expected);
  1593. $result = $this->Form->fields;
  1594. $expected = array(
  1595. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1596. 'UserForm.hidden' => '0', 'UserForm.something'
  1597. );
  1598. $this->assertEquals($expected, $result);
  1599. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1600. $result = $this->Form->secure($this->Form->fields);
  1601. $expected = array(
  1602. 'div' => array('style' => 'display:none;'),
  1603. array('input' => array(
  1604. 'type' => 'hidden', 'name' => '_Token[fields]',
  1605. 'value' => $hash
  1606. )),
  1607. array('input' => array(
  1608. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1609. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1610. )),
  1611. '/div'
  1612. );
  1613. $this->assertTags($result, $expected);
  1614. }
  1615. /**
  1616. * Test secured inputs with custom names.
  1617. *
  1618. * @return void
  1619. */
  1620. public function testSecuredInputCustomName() {
  1621. $this->markTestIncomplete('Need to revisit once models work again.');
  1622. $this->Form->request->params['_csrfToken'] = 'testKey';
  1623. $this->assertEquals(array(), $this->Form->fields);
  1624. $this->Form->input('text_input', array(
  1625. 'name' => 'data[Option][General.default_role]',
  1626. ));
  1627. $expected = array('Option.General.default_role');
  1628. $this->assertEquals($expected, $this->Form->fields);
  1629. $this->Form->input('select_box', array(
  1630. 'name' => 'data[Option][General.select_role]',
  1631. 'type' => 'select',
  1632. 'options' => array(1, 2),
  1633. ));
  1634. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1635. $this->assertEquals($expected, $this->Form->fields);
  1636. }
  1637. /**
  1638. * Tests that the correct keys are added to the field hash index
  1639. *
  1640. * @return void
  1641. */
  1642. public function testFormSecuredFileInput() {
  1643. $this->assertEquals(array(), $this->Form->fields);
  1644. $this->Form->file('Attachment.file');
  1645. $expected = array(
  1646. 'Attachment.file.name', 'Attachment.file.type',
  1647. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1648. 'Attachment.file.size'
  1649. );
  1650. $this->assertEquals($expected, $this->Form->fields);
  1651. }
  1652. /**
  1653. * test that multiple selects keys are added to field hash
  1654. *
  1655. * @return void
  1656. */
  1657. public function testFormSecuredMultipleSelect() {
  1658. $this->Form->request->params['_csrfToken'] = 'testKey';
  1659. $this->assertEquals(array(), $this->Form->fields);
  1660. $options = array('1' => 'one', '2' => 'two');
  1661. $this->Form->select('Model.select', $options);
  1662. $expected = array('Model.select');
  1663. $this->assertEquals($expected, $this->Form->fields);
  1664. $this->Form->fields = array();
  1665. $this->Form->select('Model.select', $options, array('multiple' => true));
  1666. $this->assertEquals($expected, $this->Form->fields);
  1667. }
  1668. /**
  1669. * testFormSecuredRadio method
  1670. *
  1671. * @return void
  1672. */
  1673. public function testFormSecuredRadio() {
  1674. $this->Form->request->params['_Token'] = 'testKey';
  1675. $this->assertEquals(array(), $this->Form->fields);
  1676. $options = array('1' => 'option1', '2' => 'option2');
  1677. $this->Form->radio('Test.test', $options);
  1678. $expected = array('Test.test');
  1679. $this->assertEquals($expected, $this->Form->fields);
  1680. }
  1681. /**
  1682. * Test that when disabled is in a list based attribute array it works.
  1683. *
  1684. * @return void
  1685. */
  1686. public function testFormSecuredAndDisabledNotAssoc() {
  1687. $this->Form->request->params['_Token'] = 'testKey';
  1688. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1689. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1690. $this->Form->text('Model.text', array('disabled'));
  1691. $this->Form->textarea('Model.textarea', array('disabled'));
  1692. $this->Form->password('Model.password', array('disabled'));
  1693. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1694. $expected = array(
  1695. 'Model.radio' => ''
  1696. );
  1697. $this->assertEquals($expected, $this->Form->fields);
  1698. }
  1699. /**
  1700. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1701. * hashing.
  1702. *
  1703. * @return void
  1704. */
  1705. public function testFormSecuredAndDisabled() {
  1706. $this->Form->request->params['_Token'] = 'testKey';
  1707. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1708. $this->Form->text('Model.text', array('disabled' => true));
  1709. $this->Form->password('Model.text', array('disabled' => true));
  1710. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1711. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1712. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1713. $this->Form->year('Model.year', array('disabled' => true));
  1714. $this->Form->month('Model.month', array('disabled' => true));
  1715. $this->Form->day('Model.day', array('disabled' => true));
  1716. $this->Form->hour('Model.hour', array('disabled' => true));
  1717. $this->Form->minute('Model.minute', array('disabled' => true));
  1718. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1719. $expected = array(
  1720. 'Model.radio' => ''
  1721. );
  1722. $this->assertEquals($expected, $this->Form->fields);
  1723. }
  1724. /**
  1725. * testDisableSecurityUsingForm method
  1726. *
  1727. * @return void
  1728. */
  1729. public function testDisableSecurityUsingForm() {
  1730. $this->markTestIncomplete('Need to revisit once models work again.');
  1731. $this->Form->request->params['_csrfToken'] = 'testKey';
  1732. $this->Form->request['_Token'] = array(
  1733. 'disabledFields' => array()
  1734. );
  1735. $this->Form->create();
  1736. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1737. $this->Form->input('Addresses.title');
  1738. $this->Form->input('Addresses.first_name', array('secure' => false));
  1739. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1740. $this->Form->input('Addresses.zip', array(
  1741. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1742. ));
  1743. $result = $this->Form->fields;
  1744. $expected = array(
  1745. 'Addresses.id' => '123456', 'Addresses.title',
  1746. );
  1747. $this->assertEquals($expected, $result);
  1748. }
  1749. /**
  1750. * test disableField
  1751. *
  1752. * @return void
  1753. */
  1754. public function testUnlockFieldAddsToList() {
  1755. $this->Form->request['_Token'] = array(
  1756. 'unlockedFields' => array()
  1757. );
  1758. $this->Form->unlockField('Contact.name');
  1759. $this->Form->text('Contact.name');
  1760. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1761. $this->assertEquals(array(), $this->Form->fields);
  1762. }
  1763. /**
  1764. * test unlockField removing from fields array.
  1765. *
  1766. * @return void
  1767. */
  1768. public function testUnlockFieldRemovingFromFields() {
  1769. $this->Form->request['_Token'] = array(
  1770. 'unlockedFields' => array()
  1771. );
  1772. $this->Form->create($this->article);
  1773. $this->Form->hidden('Article.id', array('value' => 1));
  1774. $this->Form->text('Article.title');
  1775. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  1776. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  1777. $this->Form->unlockField('Article.title');
  1778. $this->Form->unlockField('Article.id');
  1779. $this->assertEquals(array(), $this->Form->fields);
  1780. }
  1781. /**
  1782. * Test validation errors.
  1783. *
  1784. * @return void
  1785. */
  1786. public function testPasswordValidation() {
  1787. $this->markTestIncomplete('Need to revisit once models work again.');
  1788. $Contact->validationErrors['password'] = array('Please provide a password');
  1789. $result = $this->Form->input('Contact.password');
  1790. $expected = array(
  1791. 'div' => array('class' => 'input password error'),
  1792. 'label' => array('for' => 'ContactPassword'),
  1793. 'Password',
  1794. '/label',
  1795. 'input' => array(
  1796. 'type' => 'password', 'name' => 'Contact[password]',
  1797. 'id' => 'ContactPassword', 'class' => 'form-error'
  1798. ),
  1799. array('div' => array('class' => 'error-message')),
  1800. 'Please provide a password',
  1801. '/div',
  1802. '/div'
  1803. );
  1804. $this->assertTags($result, $expected);
  1805. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1806. $expected = array(
  1807. 'div' => array('class' => 'input password error'),
  1808. 'label' => array('for' => 'ContactPassword'),
  1809. 'Password',
  1810. '/label',
  1811. 'input' => array(
  1812. 'type' => 'password', 'name' => 'Contact[password]',
  1813. 'id' => 'ContactPassword', 'class' => 'form-error'
  1814. ),
  1815. '/div'
  1816. );
  1817. $this->assertTags($result, $expected);
  1818. }
  1819. /**
  1820. * Test validation errors, when validation message is an empty string.
  1821. *
  1822. * @return void
  1823. */
  1824. public function testEmptyErrorValidation() {
  1825. $this->markTestIncomplete('Need to revisit once models work again.');
  1826. $this->Form->validationErrors['Contact']['password'] = '';
  1827. $result = $this->Form->input('Contact.password');
  1828. $expected = array(
  1829. 'div' => array('class' => 'input password error'),
  1830. 'label' => array('for' => 'ContactPassword'),
  1831. 'Password',
  1832. '/label',
  1833. 'input' => array(
  1834. 'type' => 'password', 'name' => 'Contact[password]',
  1835. 'id' => 'ContactPassword', 'class' => 'form-error'
  1836. ),
  1837. array('div' => array('class' => 'error-message')),
  1838. array(),
  1839. '/div',
  1840. '/div'
  1841. );
  1842. $this->assertTags($result, $expected);
  1843. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1844. $expected = array(
  1845. 'div' => array('class' => 'input password error'),
  1846. 'label' => array('for' => 'ContactPassword'),
  1847. 'Password',
  1848. '/label',
  1849. 'input' => array(
  1850. 'type' => 'password', 'name' => 'Contact[password]',
  1851. 'id' => 'ContactPassword', 'class' => 'form-error'
  1852. ),
  1853. '/div'
  1854. );
  1855. $this->assertTags($result, $expected);
  1856. }
  1857. /**
  1858. * Test validation errors, when calling input() overriding validation message by an empty string.
  1859. *
  1860. * @return void
  1861. */
  1862. public function testEmptyInputErrorValidation() {
  1863. $this->markTestIncomplete('Need to revisit once models work again.');
  1864. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1865. $result = $this->Form->input('Contact.password', array('error' => ''));
  1866. $expected = array(
  1867. 'div' => array('class' => 'input password error'),
  1868. 'label' => array('for' => 'ContactPassword'),
  1869. 'Password',
  1870. '/label',
  1871. 'input' => array(
  1872. 'type' => 'password', 'name' => 'Contact[password]',
  1873. 'id' => 'ContactPassword', 'class' => 'form-error'
  1874. ),
  1875. array('div' => array('class' => 'error-message')),
  1876. array(),
  1877. '/div',
  1878. '/div'
  1879. );
  1880. $this->assertTags($result, $expected);
  1881. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1882. $expected = array(
  1883. 'div' => array('class' => 'input password error'),
  1884. 'label' => array('for' => 'ContactPassword'),
  1885. 'Password',
  1886. '/label',
  1887. 'input' => array(
  1888. 'type' => 'password', 'name' => 'Contact[password]',
  1889. 'id' => 'ContactPassword', 'class' => 'form-error'
  1890. ),
  1891. '/div'
  1892. );
  1893. $this->assertTags($result, $expected);
  1894. }
  1895. /**
  1896. * testFormValidationAssociated method
  1897. *
  1898. * test display of form errors in conjunction with model::validates.
  1899. *
  1900. * @return void
  1901. */
  1902. public function testFormValidationAssociated() {
  1903. $this->markTestIncomplete('Need to revisit once models work again.');
  1904. $data = array(
  1905. 'UserForm' => array('name' => 'user'),
  1906. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1907. );
  1908. $result = $this->UserForm->OpenidUrl->create($data);
  1909. $this->assertFalse(empty($result));
  1910. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1911. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1912. $encoding = strtolower(Configure::read('App.encoding'));
  1913. $expected = array(
  1914. 'form' => array(
  1915. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1916. 'accept-charset' => $encoding
  1917. ),
  1918. 'div' => array('style' => 'display:none;'),
  1919. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1920. '/div'
  1921. );
  1922. $this->assertTags($result, $expected);
  1923. $result = $this->Form->error(
  1924. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1925. );
  1926. $this->assertEquals('Error, not registered', $result);
  1927. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1928. }
  1929. /**
  1930. * testFormValidationAssociatedFirstLevel method
  1931. *
  1932. * test form error display with associated model.
  1933. *
  1934. * @return void
  1935. */
  1936. public function testFormValidationAssociatedFirstLevel() {
  1937. $this->markTestIncomplete('Need to revisit once models work again.');
  1938. $data = array(
  1939. 'ValidateUser' => array('name' => 'mariano'),
  1940. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1941. );
  1942. $result = $this->ValidateUser->create($data);
  1943. $this->assertFalse(empty($result));
  1944. $this->assertFalse($this->ValidateUser->validates());
  1945. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1946. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1947. $encoding = strtolower(Configure::read('App.encoding'));
  1948. $expected = array(
  1949. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1950. 'div' => array('style' => 'display:none;'),
  1951. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1952. '/div'
  1953. );
  1954. $this->assertTags($result, $expected);
  1955. $result = $this->Form->error(
  1956. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1957. );
  1958. $this->assertEquals('Invalid email', $result);
  1959. $result = $this->Form->error(
  1960. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1961. );
  1962. $this->assertEquals('Invalid name', $result);
  1963. $result = $this->Form->error(
  1964. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1965. );
  1966. $this->assertEquals('Invalid city', $result);
  1967. unset($this->ValidateUser->ValidateProfile);
  1968. unset($this->ValidateUser);
  1969. }
  1970. /**
  1971. * testFormValidationAssociatedSecondLevel method
  1972. *
  1973. * test form error display with associated model.
  1974. *
  1975. * @return void
  1976. */
  1977. public function testFormValidationAssociatedSecondLevel() {
  1978. $this->markTestIncomplete('Need to revisit once models work again.');
  1979. $data = array(
  1980. 'ValidateUser' => array('name' => 'mariano'),
  1981. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1982. 'ValidateItem' => array('name' => 'Item')
  1983. );
  1984. $result = $this->ValidateUser->create($data);
  1985. $this->assertFalse(empty($result));
  1986. $this->assertFalse($this->ValidateUser->validates());
  1987. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1988. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1989. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1990. $encoding = strtolower(Configure::read('App.encoding'));
  1991. $expected = array(
  1992. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1993. 'div' => array('style' => 'display:none;'),
  1994. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1995. '/div'
  1996. );
  1997. $this->assertTags($result, $expected);
  1998. $result = $this->Form->error(
  1999. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  2000. );
  2001. $this->assertEquals('Invalid email', $result);
  2002. $result = $this->Form->error(
  2003. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  2004. );
  2005. $this->assertEquals('Invalid name', $result);
  2006. $result = $this->Form->error(
  2007. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  2008. );
  2009. $result = $this->Form->error(
  2010. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  2011. );
  2012. $this->assertEquals('Invalid description', $result);
  2013. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  2014. unset($this->ValidateUser->ValidateProfile);
  2015. unset($this->ValidateUser);
  2016. }
  2017. /**
  2018. * testFormValidationMultiRecord method
  2019. *
  2020. * test form error display with multiple records.
  2021. *
  2022. * @return void
  2023. */
  2024. public function testFormValidationMultiRecord() {
  2025. $this->markTestIncomplete('Need to revisit once models work again.');
  2026. $Contact->validationErrors[2] = array(
  2027. 'name' => array('The provided value is invalid')
  2028. );
  2029. $result = $this->Form->input('Contact.2.name');
  2030. $expected = array(
  2031. 'div' => array('class' => 'input text error'),
  2032. 'label' => array('for' => 'Contact2Name'),
  2033. 'Name',
  2034. '/label',
  2035. 'input' => array(
  2036. 'type' => 'text', 'name' => 'Contact[2][name]', 'id' => 'Contact2Name',
  2037. 'class' => 'form-error', 'maxlength' => 255
  2038. ),
  2039. array('div' => array('class' => 'error-message')),
  2040. 'The provided value is invalid',
  2041. '/div',
  2042. '/div'
  2043. );
  2044. $this->assertTags($result, $expected);
  2045. }
  2046. /**
  2047. * testMultipleInputValidation method
  2048. *
  2049. * test multiple record form validation error display.
  2050. *
  2051. * @return void
  2052. */
  2053. public function testMultipleInputValidation() {
  2054. $this->markTestIncomplete('Need to revisit once models work again.');
  2055. $Address->validationErrors[0] = array(
  2056. 'title' => array('This field cannot be empty'),
  2057. 'first_name' => array('This field cannot be empty')
  2058. );
  2059. $Address->validationErrors[1] = array(
  2060. 'last_name' => array('You must have a last name')
  2061. );
  2062. $this->Form->create();
  2063. $result = $this->Form->input('Address.0.title');
  2064. $expected = array(
  2065. 'div' => array('class'),
  2066. 'label' => array('for'),
  2067. 'preg:/[^<]+/',
  2068. '/label',
  2069. 'input' => array(
  2070. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  2071. ),
  2072. array('div' => array('class' => 'error-message')),
  2073. 'This field cannot be empty',
  2074. '/div',
  2075. '/div'
  2076. );
  2077. $this->assertTags($result, $expected);
  2078. $result = $this->Form->input('Address.0.first_name');
  2079. $expected = array(
  2080. 'div' => array('class'),
  2081. 'label' => array('for'),
  2082. 'preg:/[^<]+/',
  2083. '/label',
  2084. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  2085. array('div' => array('class' => 'error-message')),
  2086. 'This field cannot be empty',
  2087. '/div',
  2088. '/div'
  2089. );
  2090. $this->assertTags($result, $expected);
  2091. $result = $this->Form->input('Address.0.last_name');
  2092. $expected = array(
  2093. 'div' => array('class'),
  2094. 'label' => array('for'),
  2095. 'preg:/[^<]+/',
  2096. '/label',
  2097. 'input' => array('type' => 'text', 'name', 'id'),
  2098. '/div'
  2099. );
  2100. $this->assertTags($result, $expected);
  2101. $result = $this->Form->input('Address.1.last_name');
  2102. $expected = array(
  2103. 'div' => array('class'),
  2104. 'label' => array('for'),
  2105. 'preg:/[^<]+/',
  2106. '/label',
  2107. 'input' => array(
  2108. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  2109. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  2110. ),
  2111. array('div' => array('class' => 'error-message')),
  2112. 'You must have a last name',
  2113. '/div',
  2114. '/div'
  2115. );
  2116. $this->assertTags($result, $expected);
  2117. }
  2118. /**
  2119. * testInput method
  2120. *
  2121. * Test various incarnations of input().
  2122. *
  2123. * @return void
  2124. */
  2125. public function testInput() {
  2126. $this->markTestIncomplete('Need to revisit once models work again.');
  2127. $result = $this->Form->input('ValidateUser.balance');
  2128. $expected = array(
  2129. 'div' => array('class'),
  2130. 'label' => array('for'),
  2131. 'Balance',
  2132. '/label',
  2133. 'input' => array('name', 'type' => 'number', 'id'),
  2134. '/div',
  2135. );
  2136. $this->assertTags($result, $expected);
  2137. $result = $this->Form->input('ValidateUser.cost_decimal');
  2138. $expected = array(
  2139. 'div' => array('class'),
  2140. 'label' => array('for'),
  2141. 'Cost Decimal',
  2142. '/label',
  2143. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  2144. '/div',
  2145. );
  2146. $this->assertTags($result, $expected);
  2147. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  2148. $expected = array(
  2149. 'div' => array('class' => 'input email'),
  2150. 'label' => array('for' => 'custom'),
  2151. 'Email',
  2152. '/label',
  2153. array('input' => array(
  2154. 'type' => 'email', 'name' => 'Contact[email]',
  2155. 'id' => 'custom', 'maxlength' => 255
  2156. )),
  2157. '/div'
  2158. );
  2159. $this->assertTags($result, $expected);
  2160. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  2161. $expected = array(
  2162. '<div',
  2163. 'label' => array('for' => 'ContactEmail'),
  2164. 'Email',
  2165. '/label',
  2166. array('input' => array(
  2167. 'type' => 'email', 'name' => 'Contact[email]',
  2168. 'id' => 'ContactEmail', 'maxlength' => 255
  2169. )),
  2170. '/div'
  2171. );
  2172. $this->assertTags($result, $expected);
  2173. $result = $this->Form->hidden('Contact.idontexist');
  2174. $expected = array('input' => array(
  2175. 'type' => 'hidden', 'name' => 'Contact[idontexist]',
  2176. 'id' => 'ContactIdontexist'
  2177. ));
  2178. $this->assertTags($result, $expected);
  2179. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  2180. $expected = array(
  2181. 'div' => array('class' => 'input text'),
  2182. 'label' => array('for' => 'ContactEmail'),
  2183. 'Email',
  2184. '/label',
  2185. array('input' => array(
  2186. 'type' => 'text', 'name' => 'Contact[email]',
  2187. 'id' => 'ContactEmail'
  2188. )),
  2189. '/div'
  2190. );
  2191. $this->assertTags($result, $expected);
  2192. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  2193. $expected = array(
  2194. 'div' => array('class' => 'input text'),
  2195. 'label' => array('for' => 'Contact5Email'),
  2196. 'Email',
  2197. '/label',
  2198. array('input' => array(
  2199. 'type' => 'text', 'name' => 'Contact[5][email]',
  2200. 'id' => 'Contact5Email'
  2201. )),
  2202. '/div'
  2203. );
  2204. $this->assertTags($result, $expected);
  2205. $result = $this->Form->input('Contact.password');
  2206. $expected = array(
  2207. 'div' => array('class' => 'input password'),
  2208. 'label' => array('for' => 'ContactPassword'),
  2209. 'Password',
  2210. '/label',
  2211. array('input' => array(
  2212. 'type' => 'password', 'name' => 'Contact[password]',
  2213. 'id' => 'ContactPassword'
  2214. )),
  2215. '/div'
  2216. );
  2217. $this->assertTags($result, $expected);
  2218. $result = $this->Form->input('Contact.email', array(
  2219. 'type' => 'file', 'class' => 'textbox'
  2220. ));
  2221. $expected = array(
  2222. 'div' => array('class' => 'input file'),
  2223. 'label' => array('for' => 'ContactEmail'),
  2224. 'Email',
  2225. '/label',
  2226. array('input' => array(
  2227. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2228. 'id' => 'ContactEmail'
  2229. )),
  2230. '/div'
  2231. );
  2232. $this->assertTags($result, $expected);
  2233. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  2234. $result = $this->Form->input('Contact.phone');
  2235. $expected = array(
  2236. 'div' => array('class' => 'input tel'),
  2237. 'label' => array('for' => 'ContactPhone'),
  2238. 'Phone',
  2239. '/label',
  2240. array('input' => array(
  2241. 'type' => 'tel', 'name' => 'Contact[phone]',
  2242. 'value' => 'Hello &amp; World &gt; weird chars',
  2243. 'id' => 'ContactPhone', 'maxlength' => 255
  2244. )),
  2245. '/div'
  2246. );
  2247. $this->assertTags($result, $expected);
  2248. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2249. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  2250. $expected = array(
  2251. 'div' => array('class' => 'input text'),
  2252. 'label' => array('for' => 'myId'),
  2253. 'Field',
  2254. '/label',
  2255. 'input' => array(
  2256. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2257. 'value' => 'My value', 'id' => 'myId'
  2258. ),
  2259. '/div'
  2260. );
  2261. $this->assertTags($result, $expected);
  2262. unset($this->Form->request->data);
  2263. $Contact->validationErrors['field'] = array('Badness!');
  2264. $result = $this->Form->input('Contact.field');
  2265. $expected = array(
  2266. 'div' => array('class' => 'input text error'),
  2267. 'label' => array('for' => 'ContactField'),
  2268. 'Field',
  2269. '/label',
  2270. 'input' => array(
  2271. 'type' => 'text', 'name' => 'Contact[field]',
  2272. 'id' => 'ContactField', 'class' => 'form-error'
  2273. ),
  2274. array('div' => array('class' => 'error-message')),
  2275. 'Badness!',
  2276. '/div',
  2277. '/div'
  2278. );
  2279. $this->assertTags($result, $expected);
  2280. $result = $this->Form->input('Contact.field', array(
  2281. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  2282. ));
  2283. $expected = array(
  2284. 'label' => array('for' => 'ContactField'),
  2285. 'Field',
  2286. '/label',
  2287. 'input' => array(
  2288. 'type' => 'text', 'name' => 'Contact[field]',
  2289. 'id' => 'ContactField', 'class' => 'form-error'
  2290. ),
  2291. array('span' => array('class' => 'error-message')),
  2292. 'Badness!',
  2293. '/span'
  2294. );
  2295. $this->assertTags($result, $expected);
  2296. $result = $this->Form->input('Contact.field', array(
  2297. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  2298. ));
  2299. $expected = array(
  2300. 'div' => array('class' => 'input text error'),
  2301. 'label' => array('for' => 'ContactField'),
  2302. 'Field',
  2303. '/label',
  2304. 'input' => array(
  2305. 'type' => 'text', 'name' => 'Contact[field]',
  2306. 'id' => 'ContactField', 'class' => 'form-error'
  2307. ),
  2308. array('div' => array('class' => 'error')),
  2309. 'Badness!',
  2310. '/div'
  2311. );
  2312. $this->assertTags($result, $expected);
  2313. $result = $this->Form->input('Contact.field', array(
  2314. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  2315. ));
  2316. $expected = array(
  2317. 'span' => array('class' => 'input text error'),
  2318. 'label' => array('for' => 'ContactField'),
  2319. 'Field',
  2320. '/label',
  2321. 'input' => array(
  2322. 'type' => 'text', 'name' => 'Contact[field]',
  2323. 'id' => 'ContactField', 'class' => 'form-error'
  2324. ),
  2325. 'Badness!',
  2326. '/span'
  2327. );
  2328. $this->assertTags($result, $expected);
  2329. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  2330. $expected = array(
  2331. 'div' => array('class' => 'input text error'),
  2332. 'label' => array('for' => 'ContactField'),
  2333. 'Field',
  2334. '/label',
  2335. 'input' => array(
  2336. 'type' => 'text', 'name' => 'Contact[field]',
  2337. 'id' => 'ContactField', 'class' => 'form-error'
  2338. ),
  2339. 'A message to you, Rudy',
  2340. array('div' => array('class' => 'error-message')),
  2341. 'Badness!',
  2342. '/div',
  2343. '/div'
  2344. );
  2345. $this->assertTags($result, $expected);
  2346. $this->Form->setEntity(null);
  2347. $this->Form->setEntity('Contact.field');
  2348. $result = $this->Form->input('Contact.field', array(
  2349. 'after' => 'A message to you, Rudy', 'error' => false
  2350. ));
  2351. $expected = array(
  2352. 'div' => array('class' => 'input text'),
  2353. 'label' => array('for' => 'ContactField'),
  2354. 'Field',
  2355. '/label',
  2356. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  2357. 'A message to you, Rudy',
  2358. '/div'
  2359. );
  2360. $this->assertTags($result, $expected);
  2361. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  2362. $expected = array(
  2363. 'div' => array('class' => 'input text'),
  2364. 'label' => array('for' => 'ObjectField'),
  2365. 'Field',
  2366. '/label',
  2367. 'input' => array('type' => 'text', 'name' => 'Object[field]', 'id' => 'ObjectField'),
  2368. 'A message to you, Rudy',
  2369. '/div'
  2370. );
  2371. $this->assertTags($result, $expected);
  2372. $Contact->validationErrors['field'] = array('minLength');
  2373. $result = $this->Form->input('Contact.field', array(
  2374. 'error' => array(
  2375. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2376. 'maxLength' => 'login too large'
  2377. )
  2378. ));
  2379. $expected = array(
  2380. 'div' => array('class' => 'input text error'),
  2381. 'label' => array('for' => 'ContactField'),
  2382. 'Field',
  2383. '/label',
  2384. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  2385. array('div' => array('class' => 'error-message')),
  2386. 'Le login doit contenir au moins 2 caractères',
  2387. '/div',
  2388. '/div'
  2389. );
  2390. $this->assertTags($result, $expected);
  2391. $Contact->validationErrors['field'] = array('maxLength');
  2392. $result = $this->Form->input('Contact.field', array(
  2393. 'error' => array(
  2394. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  2395. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2396. 'maxLength' => 'login too large',
  2397. )
  2398. ));
  2399. $expected = array(
  2400. 'div' => array('class' => 'input text error'),
  2401. 'label' => array('for' => 'ContactField'),
  2402. 'Field',
  2403. '/label',
  2404. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  2405. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  2406. 'login too large',
  2407. '/span',
  2408. '/div'
  2409. );
  2410. $this->assertTags($result, $expected);
  2411. }
  2412. /**
  2413. * Test that inputs with 0 can be created.
  2414. *
  2415. * @return void
  2416. */
  2417. public function testInputZero() {
  2418. $this->markTestIncomplete('Need to revisit once models work again.');
  2419. $this->Form->create('User');
  2420. $result = $this->Form->input('0');
  2421. $expected = array(
  2422. 'div' => array('class' => 'input text'),
  2423. 'label' => array('for' => 'User0'), '/label',
  2424. 'input' => array('type' => 'text', 'name' => 'User[0]', 'id' => 'User0'),
  2425. '/div'
  2426. );
  2427. $this->assertTags($result, $expected);
  2428. }
  2429. /**
  2430. * test input() with checkbox creation
  2431. *
  2432. * @return void
  2433. */
  2434. public function testInputCheckbox() {
  2435. $this->markTestIncomplete('Need to revisit once models work again.');
  2436. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  2437. $expected = array(
  2438. 'div' => array('class' => 'input checkbox'),
  2439. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  2440. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2441. '/div'
  2442. );
  2443. $this->assertTags($result, $expected);
  2444. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2445. $expected = array(
  2446. 'div' => array('class' => 'input checkbox'),
  2447. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  2448. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2449. '/div'
  2450. );
  2451. $this->assertTags($result, $expected);
  2452. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2453. $expected = array(
  2454. 'div' => array('class' => 'input checkbox'),
  2455. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  2456. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2457. '/div'
  2458. );
  2459. $this->assertTags($result, $expected);
  2460. $result = $this->Form->input('User.disabled', array(
  2461. 'label' => 'Disabled',
  2462. 'type' => 'checkbox',
  2463. 'data-foo' => 'disabled'
  2464. ));
  2465. $expected = array(
  2466. 'div' => array('class' => 'input checkbox'),
  2467. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  2468. array('input' => array(
  2469. 'type' => 'checkbox',
  2470. 'name' => 'data[User][disabled]',
  2471. 'value' => '1',
  2472. 'id' => 'UserDisabled',
  2473. 'data-foo' => 'disabled'
  2474. )),
  2475. 'label' => array('for' => 'UserDisabled'),
  2476. 'Disabled',
  2477. '/label',
  2478. '/div'
  2479. );
  2480. $this->assertTags($result, $expected);
  2481. }
  2482. /**
  2483. * test form->input() with time types.
  2484. *
  2485. */
  2486. public function testInputTime() {
  2487. $this->markTestIncomplete('Need to revisit once models work again.');
  2488. extract($this->dateRegex);
  2489. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2490. $result = explode(':', $result);
  2491. $this->assertRegExp('/option value="23"/', $result[0]);
  2492. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2493. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2494. $result = explode(':', $result);
  2495. $this->assertRegExp('/option value="23"/', $result[0]);
  2496. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2497. $result = $this->Form->input('Model.field', array(
  2498. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2499. ));
  2500. $result = explode(':', $result);
  2501. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2502. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2503. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2504. $result = $this->Form->input('Model.field', array(
  2505. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2506. ));
  2507. $result = explode(':', $result);
  2508. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2509. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2510. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2511. $result = $this->Form->input('prueba', array(
  2512. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2513. 'maxYear' => date('Y') + 1, 'interval' => 15
  2514. ));
  2515. $result = explode(':', $result);
  2516. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2517. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2518. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2519. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2520. $result = $this->Form->input('Random.start_time', array(
  2521. 'type' => 'time',
  2522. 'selected' => '18:15'
  2523. ));
  2524. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2525. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2526. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2527. $result = $this->Form->input('published', array('type' => 'time'));
  2528. $now = strtotime('now');
  2529. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2530. $now = strtotime('2013-03-09 00:42:21');
  2531. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2532. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2533. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2534. }
  2535. /**
  2536. * Test interval + selected near the hour roll over.
  2537. *
  2538. * @return void
  2539. */
  2540. public function testTimeSelectedWithInterval() {
  2541. $this->markTestIncomplete('Need to revisit once models work again.');
  2542. $result = $this->Form->input('Model.start_time', array(
  2543. 'type' => 'time',
  2544. 'interval' => 15,
  2545. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2546. ));
  2547. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2548. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2549. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2550. $result = $this->Form->input('Model.start_time', array(
  2551. 'type' => 'time',
  2552. 'interval' => 15,
  2553. 'selected' => '2012-10-23 15:57:00'
  2554. ));
  2555. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2556. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2557. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2558. $result = $this->Form->input('Model.start_time', array(
  2559. 'timeFormat' => 24,
  2560. 'type' => 'time',
  2561. 'interval' => 15,
  2562. 'selected' => '15:57'
  2563. ));
  2564. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2565. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2566. $result = $this->Form->input('Model.start_time', array(
  2567. 'timeFormat' => 24,
  2568. 'type' => 'time',
  2569. 'interval' => 15,
  2570. 'selected' => '23:57'
  2571. ));
  2572. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2573. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2574. $result = $this->Form->input('Model.created', array(
  2575. 'timeFormat' => 24,
  2576. 'type' => 'datetime',
  2577. 'interval' => 15,
  2578. 'selected' => '2012-09-30 23:56'
  2579. ));
  2580. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2581. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2582. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2583. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2584. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2585. }
  2586. /**
  2587. * Test time with selected values around 12:xx:xx
  2588. *
  2589. * @return void
  2590. */
  2591. public function testTimeSelectedWithIntervalTwelve() {
  2592. $this->markTestIncomplete('Need to revisit once models work again.');
  2593. $result = $this->Form->input('Model.start_time', array(
  2594. 'type' => 'time',
  2595. 'timeFormat' => 12,
  2596. 'interval' => 15,
  2597. 'selected' => '00:00:00'
  2598. ));
  2599. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2600. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2601. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2602. $result = $this->Form->input('Model.start_time', array(
  2603. 'type' => 'time',
  2604. 'timeFormat' => 12,
  2605. 'interval' => 15,
  2606. 'selected' => '12:00:00'
  2607. ));
  2608. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2609. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2610. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2611. $result = $this->Form->input('Model.start_time', array(
  2612. 'type' => 'time',
  2613. 'timeFormat' => 12,
  2614. 'interval' => 15,
  2615. 'selected' => '12:15:00'
  2616. ));
  2617. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2618. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2619. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2620. }
  2621. /**
  2622. * Test interval & timeFormat = 12
  2623. *
  2624. * @return void
  2625. */
  2626. public function testInputTimeWithIntervalAnd12HourFormat() {
  2627. $this->markTestIncomplete('Need to revisit once models work again.');
  2628. $result = $this->Form->input('Model.start_time', array(
  2629. 'type' => 'time',
  2630. 'timeFormat' => 12,
  2631. 'interval' => 5,
  2632. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2633. ));
  2634. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2635. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2636. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2637. $result = $this->Form->input('Model.start_time', array(
  2638. 'type' => 'time',
  2639. 'timeFormat' => '12',
  2640. 'interval' => 5,
  2641. 'selected' => '2013-04-19 16:30:00'
  2642. ));
  2643. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2644. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2645. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2646. $result = $this->Form->input('Model.start_time', array(
  2647. 'type' => 'time',
  2648. 'timeFormat' => '12',
  2649. 'interval' => 10,
  2650. 'selected' => '2013-05-19 00:33:00'
  2651. ));
  2652. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2653. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2654. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2655. $result = $this->Form->input('Model.start_time', array(
  2656. 'type' => 'time',
  2657. 'timeFormat' => '12',
  2658. 'interval' => 10,
  2659. 'selected' => '2013-05-19 13:33:00'
  2660. ));
  2661. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2662. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2663. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2664. $result = $this->Form->input('Model.start_time', array(
  2665. 'type' => 'time',
  2666. 'timeFormat' => '12',
  2667. 'interval' => 10,
  2668. 'selected' => '2013-05-19 01:33:00'
  2669. ));
  2670. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2671. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2672. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2673. }
  2674. /**
  2675. * test form->input() with datetime, date and time types
  2676. *
  2677. * @return void
  2678. */
  2679. public function testInputDatetime() {
  2680. $this->markTestIncomplete('Need to revisit once models work again.');
  2681. extract($this->dateRegex);
  2682. $result = $this->Form->input('prueba', array(
  2683. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2684. 'maxYear' => date('Y') + 1, 'interval' => 15
  2685. ));
  2686. $result = explode(':', $result);
  2687. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2688. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2689. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2690. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2691. //related to ticket #5013
  2692. $result = $this->Form->input('Contact.date', array(
  2693. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2694. ));
  2695. $this->assertRegExp('/class="customClass"/', $result);
  2696. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2697. $result = $this->Form->input('Contact.date', array(
  2698. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2699. ));
  2700. $this->assertRegExp('/id="customIdDay"/', $result);
  2701. $this->assertRegExp('/id="customIdMonth"/', $result);
  2702. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2703. $result = $this->Form->input('Model.field', array(
  2704. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2705. ));
  2706. $this->assertRegExp('/id="customIDDay"/', $result);
  2707. $this->assertRegExp('/id="customIDHour"/', $result);
  2708. $result = explode('</select><select', $result);
  2709. $result = explode(':', $result[1]);
  2710. $this->assertRegExp('/option value="23"/', $result[0]);
  2711. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2712. $result = $this->Form->input('Model.field', array(
  2713. 'type' => 'datetime', 'timeFormat' => 12
  2714. ));
  2715. $result = explode('</select><select', $result);
  2716. $result = explode(':', $result[1]);
  2717. $this->assertRegExp('/option value="12"/', $result[0]);
  2718. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2719. $this->Form->request->data = array('Contact' => array('created' => null));
  2720. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2721. $expected = array(
  2722. 'div' => array('class' => 'input date'),
  2723. 'label' => array('for' => 'ContactCreatedMonth'),
  2724. 'Created',
  2725. '/label',
  2726. array('select' => array('name' => 'Contact[created][month]', 'id' => 'ContactCreatedMonth')),
  2727. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2728. $monthsRegex,
  2729. '/select', '-',
  2730. array('select' => array('name' => 'Contact[created][day]', 'id' => 'ContactCreatedDay')),
  2731. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2732. $daysRegex,
  2733. '/select', '-',
  2734. array('select' => array('name' => 'Contact[created][year]', 'id' => 'ContactCreatedYear')),
  2735. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2736. $yearsRegex,
  2737. '/select',
  2738. '/div'
  2739. );
  2740. $this->assertTags($result, $expected);
  2741. $this->Form->request->data = array('Contact' => array('created' => null));
  2742. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2743. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2744. $this->Form->request->data = array('Contact' => array('created' => null));
  2745. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2746. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2747. $result = $this->Form->input('Contact.created', array(
  2748. 'type' => 'date',
  2749. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2750. 'timeFormat' => 'NONE'
  2751. ));
  2752. $this->assertRegExp('/for\="created-month"/', $result);
  2753. }
  2754. /**
  2755. * Test generating checkboxes with disabled elements.
  2756. *
  2757. * @return void
  2758. */
  2759. public function testInputCheckboxWithDisabledElements() {
  2760. $this->markTestIncomplete('Need to revisit once models work again.');
  2761. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2762. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2763. $expected = array(
  2764. array('div' => array('class' => 'input select')),
  2765. array('label' => array('for' => "ContactMultiple")),
  2766. 'Multiple',
  2767. '/label',
  2768. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2769. array('div' => array('class' => 'checkbox')),
  2770. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2771. array('label' => array('for' => "ContactMultiple1")),
  2772. 'One',
  2773. '/label',
  2774. '/div',
  2775. array('div' => array('class' => 'checkbox')),
  2776. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2777. array('label' => array('for' => "ContactMultiple2")),
  2778. 'Two',
  2779. '/label',
  2780. '/div',
  2781. array('div' => array('class' => 'checkbox')),
  2782. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2783. array('label' => array('for' => "ContactMultiple3")),
  2784. 'Three',
  2785. '/label',
  2786. '/div',
  2787. '/div'
  2788. );
  2789. $this->assertTags($result, $expected);
  2790. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2791. $expected = array(
  2792. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  2793. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2794. 'label' => array('for' => 'ModelField12'),
  2795. 'half',
  2796. '/label'
  2797. );
  2798. $this->assertTags($result, $expected);
  2799. $disabled = array('2', 3);
  2800. $expected = array(
  2801. array('div' => array('class' => 'input select')),
  2802. array('label' => array('for' => "ContactMultiple")),
  2803. 'Multiple',
  2804. '/label',
  2805. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2806. array('div' => array('class' => 'checkbox')),
  2807. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2808. array('label' => array('for' => "ContactMultiple1")),
  2809. 'One',
  2810. '/label',
  2811. '/div',
  2812. array('div' => array('class' => 'checkbox')),
  2813. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2814. array('label' => array('for' => "ContactMultiple2")),
  2815. 'Two',
  2816. '/label',
  2817. '/div',
  2818. array('div' => array('class' => 'checkbox')),
  2819. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2820. array('label' => array('for' => "ContactMultiple3")),
  2821. 'Three',
  2822. '/label',
  2823. '/div',
  2824. '/div'
  2825. );
  2826. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2827. $this->assertTags($result, $expected);
  2828. // make sure 50 does only disable 50, and not 50f5c0cf
  2829. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2830. $disabled = array(50);
  2831. $expected = array(
  2832. array('div' => array('class' => 'input select')),
  2833. array('label' => array('for' => "ContactMultiple")),
  2834. 'Multiple',
  2835. '/label',
  2836. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2837. array('div' => array('class' => 'checkbox')),
  2838. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2839. array('label' => array('for' => "ContactMultiple50")),
  2840. 'Fifty',
  2841. '/label',
  2842. '/div',
  2843. array('div' => array('class' => 'checkbox')),
  2844. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2845. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2846. 'Stringy',
  2847. '/label',
  2848. '/div',
  2849. '/div'
  2850. );
  2851. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2852. $this->assertTags($result, $expected);
  2853. }
  2854. /**
  2855. * test input name with leading integer, ensure attributes are generated correctly.
  2856. *
  2857. * @return void
  2858. */
  2859. public function testInputWithLeadingInteger() {
  2860. $result = $this->Form->text('0.Node.title');
  2861. $expected = array(
  2862. 'input' => array('name' => '0[Node][title]', 'type' => 'text')
  2863. );
  2864. $this->assertTags($result, $expected);
  2865. }
  2866. /**
  2867. * test form->input() with select type inputs.
  2868. *
  2869. * @return void
  2870. */
  2871. public function testInputSelectType() {
  2872. $this->markTestIncomplete('Need to revisit once models work again.');
  2873. $result = $this->Form->input('email', array(
  2874. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2875. );
  2876. $expected = array(
  2877. 'div' => array('class' => 'input select'),
  2878. 'label' => array('for' => 'email'),
  2879. 'Email',
  2880. '/label',
  2881. array('select' => array('name' => 'email', 'id' => 'email')),
  2882. array('option' => array('value' => '')),
  2883. '/option',
  2884. array('option' => array('value' => 'è')),
  2885. 'Firést',
  2886. '/option',
  2887. array('option' => array('value' => 'é')),
  2888. 'Secoènd',
  2889. '/option',
  2890. '/select',
  2891. '/div'
  2892. );
  2893. $this->assertTags($result, $expected);
  2894. $result = $this->Form->input('email', array(
  2895. 'options' => array('First', 'Second'), 'empty' => true)
  2896. );
  2897. $expected = array(
  2898. 'div' => array('class' => 'input select'),
  2899. 'label' => array('for' => 'email'),
  2900. 'Email',
  2901. '/label',
  2902. array('select' => array('name' => 'email', 'id' => 'email')),
  2903. array('option' => array('value' => '')),
  2904. '/option',
  2905. array('option' => array('value' => '0')),
  2906. 'First',
  2907. '/option',
  2908. array('option' => array('value' => '1')),
  2909. 'Second',
  2910. '/option',
  2911. '/select',
  2912. '/div'
  2913. );
  2914. $this->assertTags($result, $expected);
  2915. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2916. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2917. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2918. $expected = array(
  2919. 'div' => array('class' => 'input select'),
  2920. 'label' => array('for' => 'ModelUserId'),
  2921. 'User',
  2922. '/label',
  2923. 'select' => array('name' => 'Model[user_id]', 'id' => 'ModelUserId'),
  2924. array('option' => array('value' => '')),
  2925. '/option',
  2926. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2927. 'good',
  2928. '/option',
  2929. array('option' => array('value' => 'other')),
  2930. 'bad',
  2931. '/option',
  2932. '/select',
  2933. '/div'
  2934. );
  2935. $this->assertTags($result, $expected);
  2936. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2937. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2938. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2939. $expected = array(
  2940. 'div' => array('class' => 'input select'),
  2941. 'label' => array('for' => 'ThingUserId'),
  2942. 'User',
  2943. '/label',
  2944. 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'),
  2945. array('option' => array('value' => '')),
  2946. 'Some Empty',
  2947. '/option',
  2948. array('option' => array('value' => 'value')),
  2949. 'good',
  2950. '/option',
  2951. array('option' => array('value' => 'other')),
  2952. 'bad',
  2953. '/option',
  2954. '/select',
  2955. '/div'
  2956. );
  2957. $this->assertTags($result, $expected);
  2958. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2959. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2960. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2961. $expected = array(
  2962. 'div' => array('class' => 'input select'),
  2963. 'label' => array('for' => 'ThingUserId'),
  2964. 'User',
  2965. '/label',
  2966. 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'),
  2967. array('option' => array('value' => '')),
  2968. 'Some Empty',
  2969. '/option',
  2970. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2971. 'good',
  2972. '/option',
  2973. array('option' => array('value' => 'other')),
  2974. 'bad',
  2975. '/option',
  2976. '/select',
  2977. '/div'
  2978. );
  2979. $this->assertTags($result, $expected);
  2980. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2981. $this->Form->request->data = array('User' => array('User' => array('value')));
  2982. $result = $this->Form->input('User.User', array('empty' => true));
  2983. $expected = array(
  2984. 'div' => array('class' => 'input select'),
  2985. 'label' => array('for' => 'UserUser'),
  2986. 'User',
  2987. '/label',
  2988. 'input' => array('type' => 'hidden', 'name' => 'User[User]', 'value' => '', 'id' => 'UserUser_'),
  2989. 'select' => array('name' => 'User[User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2990. array('option' => array('value' => '')),
  2991. '/option',
  2992. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2993. 'good',
  2994. '/option',
  2995. array('option' => array('value' => 'other')),
  2996. 'bad',
  2997. '/option',
  2998. '/select',
  2999. '/div'
  3000. );
  3001. $this->assertTags($result, $expected);
  3002. $this->Form->data = array();
  3003. $result = $this->Form->input('Publisher.id', array(
  3004. 'label' => 'Publisher',
  3005. 'type' => 'select',
  3006. 'multiple' => 'checkbox',
  3007. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  3008. ));
  3009. $expected = array(
  3010. array('div' => array('class' => 'input select')),
  3011. array('label' => array('for' => 'PublisherId')),
  3012. 'Publisher',
  3013. '/label',
  3014. 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'PublisherId'),
  3015. array('div' => array('class' => 'checkbox')),
  3016. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  3017. array('label' => array('for' => 'PublisherIdValue1')),
  3018. 'Label 1',
  3019. '/label',
  3020. '/div',
  3021. array('div' => array('class' => 'checkbox')),
  3022. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  3023. array('label' => array('for' => 'PublisherIdValue2')),
  3024. 'Label 2',
  3025. '/label',
  3026. '/div',
  3027. '/div'
  3028. );
  3029. $this->assertTags($result, $expected);
  3030. }
  3031. /**
  3032. * test that input() and a non standard primary key makes a hidden input by default.
  3033. *
  3034. * @return void
  3035. */
  3036. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  3037. $this->markTestIncomplete('Need to revisit once models work again.');
  3038. $this->Form->create('User');
  3039. $this->Form->fieldset = array(
  3040. 'User' => array(
  3041. 'fields' => array(
  3042. 'model_id' => array('type' => 'integer')
  3043. ),
  3044. 'validates' => array(),
  3045. 'key' => 'model_id'
  3046. )
  3047. );
  3048. $result = $this->Form->input('model_id');
  3049. $expected = array(
  3050. 'input' => array('type' => 'hidden', 'name' => 'User[model_id]', 'id' => 'UserModelId'),
  3051. );
  3052. $this->assertTags($result, $expected);
  3053. }
  3054. /**
  3055. * test that overriding the magic select type widget is possible
  3056. *
  3057. * @return void
  3058. */
  3059. public function testInputOverridingMagicSelectType() {
  3060. $this->markTestIncomplete('Need to revisit once models work again.');
  3061. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3062. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  3063. $expected = array(
  3064. 'div' => array('class' => 'input text'),
  3065. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  3066. 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  3067. '/div'
  3068. );
  3069. $this->assertTags($result, $expected);
  3070. //Check that magic types still work for plural/singular vars
  3071. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  3072. $result = $this->Form->input('Model.type');
  3073. $expected = array(
  3074. 'div' => array('class' => 'input select'),
  3075. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  3076. 'select' => array('name' => 'Model[type]', 'id' => 'ModelType'),
  3077. array('option' => array('value' => 'value')), 'good', '/option',
  3078. array('option' => array('value' => 'other')), 'bad', '/option',
  3079. '/select',
  3080. '/div'
  3081. );
  3082. $this->assertTags($result, $expected);
  3083. }
  3084. /**
  3085. * Test that inferred types do not override developer input
  3086. *
  3087. * @return void
  3088. */
  3089. public function testInputMagicTypeDoesNotOverride() {
  3090. $this->markTestIncomplete('Need to revisit once models work again.');
  3091. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3092. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  3093. $expected = array(
  3094. 'div' => array('class' => 'input checkbox'),
  3095. array('input' => array(
  3096. 'type' => 'hidden',
  3097. 'name' => 'data[Model][user]',
  3098. 'id' => 'ModelUser_',
  3099. 'value' => 0,
  3100. )),
  3101. array('input' => array(
  3102. 'name' => 'data[Model][user]',
  3103. 'type' => 'checkbox',
  3104. 'id' => 'ModelUser',
  3105. 'value' => 1
  3106. )),
  3107. 'label' => array('for' => 'ModelUser'), 'User', '/label',
  3108. '/div'
  3109. );
  3110. $this->assertTags($result, $expected);
  3111. }
  3112. /**
  3113. * Test that magic input() selects are created for type=number
  3114. *
  3115. * @return void
  3116. */
  3117. public function testInputMagicSelectForTypeNumber() {
  3118. $this->markTestIncomplete('Need to revisit once models work again.');
  3119. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  3120. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  3121. $result = $this->Form->input('ValidateUser.balance');
  3122. $expected = array(
  3123. 'div' => array('class' => 'input select'),
  3124. 'label' => array('for' => 'ValidateUserBalance'),
  3125. 'Balance',
  3126. '/label',
  3127. 'select' => array('name' => 'ValidateUser[balance]', 'id' => 'ValidateUserBalance'),
  3128. array('option' => array('value' => '0')),
  3129. 'nothing',
  3130. '/option',
  3131. array('option' => array('value' => '1', 'selected' => 'selected')),
  3132. 'some',
  3133. '/option',
  3134. array('option' => array('value' => '100')),
  3135. 'a lot',
  3136. '/option',
  3137. '/select',
  3138. '/div'
  3139. );
  3140. $this->assertTags($result, $expected);
  3141. }
  3142. /**
  3143. * Test that magic input() selects can easily be converted into radio types without error.
  3144. *
  3145. * @return void
  3146. */
  3147. public function testInputMagicSelectChangeToRadio() {
  3148. $this->markTestIncomplete('Need to revisit once models work again.');
  3149. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3150. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  3151. $this->assertRegExp('/input type="radio"/', $result);
  3152. }
  3153. /**
  3154. * fields with the same name as the model should work.
  3155. *
  3156. * @return void
  3157. */
  3158. public function testInputWithMatchingFieldAndModelName() {
  3159. $this->markTestIncomplete('Need to revisit once models work again.');
  3160. $this->Form->create('User');
  3161. $this->Form->fieldset = array(
  3162. 'User' => array(
  3163. 'fields' => array(
  3164. 'User' => array('type' => 'text')
  3165. ),
  3166. 'validates' => array(),
  3167. 'key' => 'id'
  3168. )
  3169. );
  3170. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  3171. $result = $this->Form->input('User', array('type' => 'text'));
  3172. $expected = array(
  3173. 'div' => array('class' => 'input text'),
  3174. 'label' => array('for' => 'UserUser'), 'User', '/label',
  3175. 'input' => array('name' => 'User[User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  3176. '/div'
  3177. );
  3178. $this->assertTags($result, $expected);
  3179. }
  3180. /**
  3181. * testFormInputs method
  3182. *
  3183. * test correct results from form::inputs().
  3184. *
  3185. * @return void
  3186. */
  3187. public function testFormInputs() {
  3188. $this->markTestIncomplete('Need to revisit once models work again.');
  3189. $this->Form->create('Cake\Test\TestCase\View\Helper\Contact');
  3190. $result = $this->Form->inputs('The Legend');
  3191. $expected = array(
  3192. '<fieldset',
  3193. '<legend',
  3194. 'The Legend',
  3195. '/legend',
  3196. '*/fieldset',
  3197. );
  3198. $this->assertTags($result, $expected);
  3199. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  3200. $expected = array(
  3201. 'fieldset' => array('class' => 'classy-stuff'),
  3202. '<legend',
  3203. 'Field of Dreams',
  3204. '/legend',
  3205. '*/fieldset'
  3206. );
  3207. $this->assertTags($result, $expected);
  3208. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  3209. $this->assertTags($result, $expected);
  3210. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  3211. $this->assertTags($result, $expected);
  3212. $this->Form->create('Contact');
  3213. $this->Form->request['prefix'] = 'admin';
  3214. $this->Form->request['action'] = 'admin_edit';
  3215. $result = $this->Form->inputs();
  3216. $expected = array(
  3217. '<fieldset',
  3218. '<legend',
  3219. 'Edit Contact',
  3220. '/legend',
  3221. '*/fieldset',
  3222. );
  3223. $this->assertTags($result, $expected);
  3224. $this->Form->create('Contact');
  3225. $result = $this->Form->inputs(false);
  3226. $expected = array(
  3227. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3228. array('div' => array('class' => 'input text')),
  3229. '*/div',
  3230. array('div' => array('class' => 'input email')),
  3231. '*/div',
  3232. array('div' => array('class' => 'input tel')),
  3233. '*/div',
  3234. array('div' => array('class' => 'input password')),
  3235. '*/div',
  3236. array('div' => array('class' => 'input date')),
  3237. '*/div',
  3238. array('div' => array('class' => 'input date')),
  3239. '*/div',
  3240. array('div' => array('class' => 'input datetime')),
  3241. '*/div',
  3242. array('div' => array('class' => 'input number')),
  3243. '*/div',
  3244. array('div' => array('class' => 'input select')),
  3245. '*/div',
  3246. );
  3247. $this->assertTags($result, $expected);
  3248. $this->Form->create('Contact');
  3249. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  3250. $expected = array(
  3251. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3252. array('div' => array('class' => 'input text')),
  3253. '*/div',
  3254. array('div' => array('class' => 'input email')),
  3255. '*/div',
  3256. array('div' => array('class' => 'input tel')),
  3257. '*/div',
  3258. array('div' => array('class' => 'input password')),
  3259. '*/div',
  3260. array('div' => array('class' => 'input date')),
  3261. '*/div',
  3262. array('div' => array('class' => 'input date')),
  3263. '*/div',
  3264. array('div' => array('class' => 'input datetime')),
  3265. '*/div',
  3266. array('div' => array('class' => 'input number')),
  3267. '*/div',
  3268. array('div' => array('class' => 'input select')),
  3269. '*/div',
  3270. );
  3271. $this->assertTags($result, $expected);
  3272. $this->Form->create('Contact');
  3273. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  3274. $this->assertTags($result, $expected);
  3275. $this->Form->create('Contact');
  3276. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  3277. $expected = array(
  3278. 'fieldset' => array(),
  3279. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3280. array('div' => array('class' => 'input text')),
  3281. '*/div',
  3282. array('div' => array('class' => 'input email')),
  3283. '*/div',
  3284. array('div' => array('class' => 'input tel')),
  3285. '*/div',
  3286. array('div' => array('class' => 'input password')),
  3287. '*/div',
  3288. array('div' => array('class' => 'input date')),
  3289. '*/div',
  3290. array('div' => array('class' => 'input date')),
  3291. '*/div',
  3292. array('div' => array('class' => 'input datetime')),
  3293. '*/div',
  3294. array('div' => array('class' => 'input number')),
  3295. '*/div',
  3296. array('div' => array('class' => 'input select')),
  3297. '*/div',
  3298. '/fieldset'
  3299. );
  3300. $this->assertTags($result, $expected);
  3301. $this->Form->create('Contact');
  3302. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  3303. $expected = array(
  3304. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3305. array('div' => array('class' => 'input text')),
  3306. '*/div',
  3307. array('div' => array('class' => 'input email')),
  3308. '*/div',
  3309. array('div' => array('class' => 'input tel')),
  3310. '*/div',
  3311. array('div' => array('class' => 'input password')),
  3312. '*/div',
  3313. array('div' => array('class' => 'input date')),
  3314. '*/div',
  3315. array('div' => array('class' => 'input date')),
  3316. '*/div',
  3317. array('div' => array('class' => 'input datetime')),
  3318. '*/div',
  3319. array('div' => array('class' => 'input number')),
  3320. '*/div',
  3321. array('div' => array('class' => 'input select')),
  3322. '*/div',
  3323. );
  3324. $this->assertTags($result, $expected);
  3325. $this->Form->create('Contact');
  3326. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  3327. $this->assertTags($result, $expected);
  3328. $this->Form->create('Contact');
  3329. $result = $this->Form->inputs('Hello');
  3330. $expected = array(
  3331. 'fieldset' => array(),
  3332. 'legend' => array(),
  3333. 'Hello',
  3334. '/legend',
  3335. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3336. array('div' => array('class' => 'input text')),
  3337. '*/div',
  3338. array('div' => array('class' => 'input email')),
  3339. '*/div',
  3340. array('div' => array('class' => 'input tel')),
  3341. '*/div',
  3342. array('div' => array('class' => 'input password')),
  3343. '*/div',
  3344. array('div' => array('class' => 'input date')),
  3345. '*/div',
  3346. array('div' => array('class' => 'input date')),
  3347. '*/div',
  3348. array('div' => array('class' => 'input datetime')),
  3349. '*/div',
  3350. array('div' => array('class' => 'input number')),
  3351. '*/div',
  3352. array('div' => array('class' => 'input select')),
  3353. '*/div',
  3354. '/fieldset'
  3355. );
  3356. $this->assertTags($result, $expected);
  3357. $this->Form->create('Contact');
  3358. $result = $this->Form->inputs(array('legend' => 'Hello'));
  3359. $expected = array(
  3360. 'fieldset' => array(),
  3361. 'legend' => array(),
  3362. 'Hello',
  3363. '/legend',
  3364. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3365. array('div' => array('class' => 'input text')),
  3366. '*/div',
  3367. array('div' => array('class' => 'input email')),
  3368. '*/div',
  3369. array('div' => array('class' => 'input tel')),
  3370. '*/div',
  3371. array('div' => array('class' => 'input password')),
  3372. '*/div',
  3373. array('div' => array('class' => 'input date')),
  3374. '*/div',
  3375. array('div' => array('class' => 'input date')),
  3376. '*/div',
  3377. array('div' => array('class' => 'input datetime')),
  3378. '*/div',
  3379. array('div' => array('class' => 'input number')),
  3380. '*/div',
  3381. array('div' => array('class' => 'input select')),
  3382. '*/div',
  3383. '/fieldset'
  3384. );
  3385. $this->assertTags($result, $expected);
  3386. $this->Form->create('Contact');
  3387. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  3388. $this->assertTags($result, $expected);
  3389. $this->Form->end();
  3390. $this->Form->create(false);
  3391. $expected = array(
  3392. 'fieldset' => array(),
  3393. array('div' => array('class' => 'input text')),
  3394. 'label' => array('for' => 'foo'),
  3395. 'Foo',
  3396. '/label',
  3397. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  3398. '*/div',
  3399. '/fieldset'
  3400. );
  3401. $result = $this->Form->inputs(
  3402. array('foo' => array('type' => 'text')),
  3403. array(),
  3404. array('legend' => false)
  3405. );
  3406. $this->assertTags($result, $expected);
  3407. }
  3408. /**
  3409. * Tests inputs() works with plugin models
  3410. *
  3411. * @return void
  3412. */
  3413. public function testInputsPluginModel() {
  3414. $this->markTestIncomplete('Need to revisit once models work again.');
  3415. $this->loadFixtures('Post');
  3416. Plugin::load('TestPlugin');
  3417. $this->Form->request['models'] = array(
  3418. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  3419. );
  3420. $this->Form->create('TestPlugin.TestPluginPost');
  3421. $result = $this->Form->inputs();
  3422. $this->assertContains('TestPluginPost[id]', $result);
  3423. $this->assertContains('TestPluginPost[author_id]', $result);
  3424. $this->assertContains('TestPluginPost[title]', $result);
  3425. $this->assertEquals('TestPluginPost', $this->Form->model());
  3426. }
  3427. /**
  3428. * testSelectAsCheckbox method
  3429. *
  3430. * test multi-select widget with checkbox formatting.
  3431. *
  3432. * @return void
  3433. */
  3434. public function testSelectAsCheckbox() {
  3435. $result = $this->Form->select(
  3436. 'Model.multi_field',
  3437. array('first', 'second', 'third'),
  3438. array('multiple' => 'checkbox', 'value' => array(0, 1))
  3439. );
  3440. $expected = array(
  3441. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3442. array('div' => array('class' => 'checkbox')),
  3443. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0')),
  3444. array('label' => array('for' => 'model-multi-field-0', 'class' => 'selected')),
  3445. 'first',
  3446. '/label',
  3447. '/div',
  3448. array('div' => array('class' => 'checkbox')),
  3449. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1')),
  3450. array('label' => array('for' => 'model-multi-field-1', 'class' => 'selected')),
  3451. 'second',
  3452. '/label',
  3453. '/div',
  3454. array('div' => array('class' => 'checkbox')),
  3455. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  3456. array('label' => array('for' => 'model-multi-field-2')),
  3457. 'third',
  3458. '/label',
  3459. '/div',
  3460. );
  3461. $this->assertTags($result, $expected);
  3462. $result = $this->Form->select(
  3463. 'Model.multi_field',
  3464. array('1/2' => 'half'),
  3465. array('multiple' => 'checkbox')
  3466. );
  3467. $expected = array(
  3468. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3469. array('div' => array('class' => 'checkbox')),
  3470. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2')),
  3471. array('label' => array('for' => 'model-multi-field-1-2')),
  3472. 'half',
  3473. '/label',
  3474. '/div',
  3475. );
  3476. $this->assertTags($result, $expected);
  3477. }
  3478. /**
  3479. * testLabel method
  3480. *
  3481. * test label generation.
  3482. *
  3483. * @return void
  3484. */
  3485. public function testLabel() {
  3486. $result = $this->Form->label('Person.name');
  3487. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  3488. $result = $this->Form->label('Person.name');
  3489. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  3490. $result = $this->Form->label('Person.first_name');
  3491. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'First Name', '/label'));
  3492. $result = $this->Form->label('Person.first_name', 'Your first name');
  3493. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'Your first name', '/label'));
  3494. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  3495. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class'), 'Your first name', '/label'));
  3496. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  3497. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  3498. $result = $this->Form->label('Person.first_name', '');
  3499. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), '/label'));
  3500. $result = $this->Form->label('Person.2.name', '');
  3501. $this->assertTags($result, array('label' => array('for' => 'person-2-name'), '/label'));
  3502. }
  3503. /**
  3504. * testTextbox method
  3505. *
  3506. * test textbox element generation
  3507. *
  3508. * @return void
  3509. */
  3510. public function testTextbox() {
  3511. $result = $this->Form->text('Model.field');
  3512. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]')));
  3513. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3514. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]')));
  3515. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3516. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID')));
  3517. }
  3518. /**
  3519. * Test that text() hooks up with request data and error fields.
  3520. *
  3521. * @return void
  3522. */
  3523. public function testTextBoxDataAndError() {
  3524. $this->article['errors'] = [
  3525. 'Contact' => ['text' => 'wrong']
  3526. ];
  3527. $this->Form->create($this->article);
  3528. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3529. $result = $this->Form->text('Model.text');
  3530. $expected = [
  3531. 'input' => [
  3532. 'type' => 'text',
  3533. 'name' => 'Model[text]',
  3534. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3535. ]
  3536. ];
  3537. $this->assertTags($result, $expected);
  3538. $this->Form->request->data['Contact']['text'] = 'test';
  3539. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3540. $expected = [
  3541. 'input' => [
  3542. 'type' => 'text',
  3543. 'name' => 'Contact[text]',
  3544. 'value' => 'test',
  3545. 'id' => 'theID',
  3546. 'class' => 'form-error'
  3547. ]
  3548. ];
  3549. $this->assertTags($result, $expected);
  3550. }
  3551. /**
  3552. * testDefaultValue method
  3553. *
  3554. * Test default value setting
  3555. *
  3556. * @return void
  3557. */
  3558. public function testTextDefaultValue() {
  3559. $this->Form->request->data['Model']['field'] = 'test';
  3560. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3561. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3562. $this->assertTags($result, $expected);
  3563. unset($this->Form->request->data['Model']['field']);
  3564. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3565. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3566. $this->assertTags($result, $expected);
  3567. }
  3568. /**
  3569. * testError method
  3570. *
  3571. * Test field error generation
  3572. *
  3573. * @return void
  3574. */
  3575. public function testError() {
  3576. $this->article['errors'] = [
  3577. 'Article' => ['field' => 'email']
  3578. ];
  3579. $this->Form->create($this->article);
  3580. $result = $this->Form->error('Article.field');
  3581. $expected = [
  3582. ['div' => ['class' => 'error-message']],
  3583. 'email',
  3584. '/div',
  3585. ];
  3586. $this->assertTags($result, $expected);
  3587. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  3588. $expected = [
  3589. ['div' => ['class' => 'error-message']],
  3590. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3591. '/div',
  3592. ];
  3593. $this->assertTags($result, $expected);
  3594. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3595. $expected = [
  3596. ['div' => ['class' => 'error-message']],
  3597. '<strong', 'Badness!', '/strong',
  3598. '/div',
  3599. ];
  3600. $this->assertTags($result, $expected);
  3601. }
  3602. /**
  3603. * Test error with nested lists.
  3604. *
  3605. * @return void
  3606. */
  3607. public function testErrorMessages() {
  3608. $this->article['errors'] = [
  3609. 'Article' => ['field' => 'email']
  3610. ];
  3611. $this->Form->create($this->article);
  3612. $result = $this->Form->error('Article.field', array(
  3613. 'email' => 'No good!'
  3614. ));
  3615. $expected = array(
  3616. 'div' => array('class' => 'error-message'),
  3617. 'No good!',
  3618. '/div'
  3619. );
  3620. $this->assertTags($result, $expected);
  3621. }
  3622. /**
  3623. * test error() with multiple messages.
  3624. *
  3625. * @return void
  3626. */
  3627. public function testErrorMultipleMessages() {
  3628. $this->article['errors'] = [
  3629. 'field' => ['notEmpty', 'email', 'Something else']
  3630. ];
  3631. $this->Form->create($this->article);
  3632. $result = $this->Form->error('field', array(
  3633. 'notEmpty' => 'Cannot be empty',
  3634. 'email' => 'No good!'
  3635. ));
  3636. $expected = array(
  3637. 'div' => array('class' => 'error-message'),
  3638. 'ul' => array(),
  3639. '<li', 'Cannot be empty', '/li',
  3640. '<li', 'No good!', '/li',
  3641. '<li', 'Something else', '/li',
  3642. '/ul',
  3643. '/div'
  3644. );
  3645. $this->assertTags($result, $expected);
  3646. }
  3647. /**
  3648. * test error options when using form->input();
  3649. *
  3650. * @return void
  3651. */
  3652. public function testInputErrorEscape() {
  3653. $this->markTestIncomplete('Need to revisit once models work again.');
  3654. $this->Form->create('ValidateProfile');
  3655. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3656. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3657. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3658. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3659. $this->assertRegExp('/required<br>/', $result);
  3660. }
  3661. /**
  3662. * testPassword method
  3663. *
  3664. * Test password element generation
  3665. *
  3666. * @return void
  3667. */
  3668. public function testPassword() {
  3669. $this->article['errors'] = [
  3670. 'Contact' => [
  3671. 'passwd' => 1
  3672. ]
  3673. ];
  3674. $this->Form->create($this->article);
  3675. $result = $this->Form->password('Contact.field');
  3676. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]')));
  3677. $this->Form->request->data['Contact']['passwd'] = 'test';
  3678. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3679. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3680. }
  3681. /**
  3682. * testRadio method
  3683. *
  3684. * Test radio element set generation
  3685. *
  3686. * @return void
  3687. */
  3688. public function testRadio() {
  3689. $result = $this->Form->radio('Model.field', array('option A'));
  3690. $expected = array(
  3691. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3692. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3693. 'label' => array('for' => 'model-field-0'),
  3694. 'option A',
  3695. '/label'
  3696. );
  3697. $this->assertTags($result, $expected);
  3698. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3699. $expected = array(
  3700. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3701. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3702. array('label' => array('for' => 'model-field-0')),
  3703. 'option A',
  3704. '/label',
  3705. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1')),
  3706. array('label' => array('for' => 'model-field-1')),
  3707. 'option B',
  3708. '/label',
  3709. );
  3710. $this->assertTags($result, $expected);
  3711. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3712. $expected = array(
  3713. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''),
  3714. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male')),
  3715. array('label' => array('for' => 'employee-gender-male')),
  3716. 'Male',
  3717. '/label',
  3718. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female')),
  3719. array('label' => array('for' => 'employee-gender-female')),
  3720. 'Female',
  3721. '/label',
  3722. );
  3723. $this->assertTags($result, $expected);
  3724. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  3725. $expected = array(
  3726. array('input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '')),
  3727. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0')),
  3728. array('label' => array('for' => 'model-custom-0')),
  3729. 'option A',
  3730. '/label',
  3731. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1')),
  3732. array('label' => array('for' => 'model-custom-1')),
  3733. 'option B',
  3734. '/label',
  3735. );
  3736. $this->assertTags($result, $expected);
  3737. }
  3738. /**
  3739. * test generating radio input inside label ala twitter bootstrap
  3740. *
  3741. * @return void
  3742. */
  3743. public function testRadioInputInsideLabel() {
  3744. $this->Form->templates([
  3745. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3746. 'radioContainer' => '{{label}}'
  3747. ]);
  3748. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  3749. $expected = [
  3750. ['input' => [
  3751. 'type' => 'hidden',
  3752. 'name' => 'Model[field]',
  3753. 'value' => ''
  3754. ]],
  3755. ['label' => ['for' => 'model-field-0']],
  3756. ['input' => [
  3757. 'type' => 'radio',
  3758. 'name' => 'Model[field]',
  3759. 'value' => '0',
  3760. 'id' => 'model-field-0'
  3761. ]],
  3762. 'option A',
  3763. '/label',
  3764. ['label' => ['for' => 'model-field-1']],
  3765. ['input' => [
  3766. 'type' => 'radio',
  3767. 'name' => 'Model[field]',
  3768. 'value' => '1',
  3769. 'id' => 'model-field-1'
  3770. ]],
  3771. 'option B',
  3772. '/label',
  3773. ];
  3774. $this->assertTags($result, $expected);
  3775. }
  3776. /**
  3777. * test disabling the hidden input for radio buttons
  3778. *
  3779. * @return void
  3780. */
  3781. public function testRadioHiddenInputDisabling() {
  3782. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3783. $expected = array(
  3784. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'),
  3785. 'label' => array('for' => 'model-1-field-0'),
  3786. 'option A',
  3787. '/label'
  3788. );
  3789. $this->assertTags($result, $expected);
  3790. }
  3791. /**
  3792. * testDomIdSuffix method
  3793. *
  3794. * @return void
  3795. */
  3796. public function testDomIdSuffix() {
  3797. $this->markTestIncomplete('Need to revisit once models work again.');
  3798. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs');
  3799. $this->assertEquals('1StringWith1$-dollarSigns', $result);
  3800. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>');
  3801. $this->assertEquals('AbcX=FooY=Bar', $result);
  3802. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'xhtml');
  3803. $this->assertEquals('1StringWith1-dollarSigns', $result);
  3804. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>', 'xhtml');
  3805. $this->assertEquals('AbcXFooYBar', $result);
  3806. }
  3807. /**
  3808. * testDomIdSuffixCollisionResolvement()
  3809. *
  3810. * @return void
  3811. */
  3812. public function testDomIdSuffixCollisionResolvement() {
  3813. $this->markTestIncomplete('Need to revisit once models work again.');
  3814. $result = $this->Form->domIdSuffix('a>b');
  3815. $this->assertEquals('AB', $result);
  3816. $result = $this->Form->domIdSuffix('a<b');
  3817. $this->assertEquals('AB1', $result);
  3818. $result = $this->Form->domIdSuffix('a\'b');
  3819. $this->assertEquals('AB2', $result);
  3820. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3821. $this->assertEquals('1StringWith1-dollar', $result);
  3822. $result = $this->Form->domIdSuffix('1 string with 1€-dollar', 'xhtml');
  3823. $this->assertEquals('1StringWith1-dollar1', $result);
  3824. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3825. $this->assertEquals('1StringWith1-dollar2', $result);
  3826. }
  3827. /**
  3828. * testSelect method
  3829. *
  3830. * Test select element generation.
  3831. *
  3832. * @return void
  3833. */
  3834. public function testSelect() {
  3835. $result = $this->Form->select('Model.field', array());
  3836. $expected = array(
  3837. 'select' => array('name' => 'Model[field]'),
  3838. '/select'
  3839. );
  3840. $this->assertTags($result, $expected);
  3841. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3842. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3843. $expected = array(
  3844. 'select' => array('name' => 'Model[field]'),
  3845. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3846. 'good',
  3847. '/option',
  3848. array('option' => array('value' => 'other')),
  3849. 'bad',
  3850. '/option',
  3851. '/select'
  3852. );
  3853. $this->assertTags($result, $expected);
  3854. $this->Form->request->data = array();
  3855. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3856. $expected = array(
  3857. 'select' => array('name' => 'Model[field]'),
  3858. array('option' => array('value' => 'value')),
  3859. 'good',
  3860. '/option',
  3861. array('option' => array('value' => 'other')),
  3862. 'bad',
  3863. '/option',
  3864. '/select'
  3865. );
  3866. $this->assertTags($result, $expected);
  3867. $result = $this->Form->select(
  3868. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3869. array('empty' => false)
  3870. );
  3871. $expected = array(
  3872. 'select' => array('name' => 'Model[field]'),
  3873. array('option' => array('value' => 'first')),
  3874. 'first &quot;html&quot; &lt;chars&gt;',
  3875. '/option',
  3876. array('option' => array('value' => 'second')),
  3877. 'value',
  3878. '/option',
  3879. '/select'
  3880. );
  3881. $this->assertTags($result, $expected);
  3882. $result = $this->Form->select(
  3883. 'Model.field',
  3884. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3885. array('escape' => false, 'empty' => false)
  3886. );
  3887. $expected = array(
  3888. 'select' => array('name' => 'Model[field]'),
  3889. array('option' => array('value' => 'first')),
  3890. 'first "html" <chars>',
  3891. '/option',
  3892. array('option' => array('value' => 'second')),
  3893. 'value',
  3894. '/option',
  3895. '/select'
  3896. );
  3897. $this->assertTags($result, $expected);
  3898. $options = array(
  3899. array('value' => 'first', 'text' => 'First'),
  3900. array('value' => 'first', 'text' => 'Another First'),
  3901. );
  3902. $result = $this->Form->select(
  3903. 'Model.field',
  3904. $options,
  3905. array('escape' => false, 'empty' => false)
  3906. );
  3907. $expected = array(
  3908. 'select' => array('name' => 'Model[field]'),
  3909. array('option' => array('value' => 'first')),
  3910. 'First',
  3911. '/option',
  3912. array('option' => array('value' => 'first')),
  3913. 'Another First',
  3914. '/option',
  3915. '/select'
  3916. );
  3917. $this->assertTags($result, $expected);
  3918. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3919. $result = $this->Form->select(
  3920. 'Model.contact_id',
  3921. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3922. array('escape' => false, 'empty' => 'pick something')
  3923. );
  3924. $expected = array(
  3925. 'select' => array('name' => 'Model[contact_id]'),
  3926. array('option' => array('value' => '')), 'pick something', '/option',
  3927. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3928. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3929. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3930. '/select'
  3931. );
  3932. $this->assertTags($result, $expected);
  3933. $this->Form->request->data['Model']['field'] = 0;
  3934. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3935. $expected = array(
  3936. 'select' => array('name' => 'Model[field]'),
  3937. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3938. array('option' => array('value' => '1')), 'Yes', '/option',
  3939. '/select'
  3940. );
  3941. $this->assertTags($result, $expected);
  3942. }
  3943. /**
  3944. * test select() with required and disabled attributes.
  3945. *
  3946. * @return void
  3947. */
  3948. public function testSelectRequired() {
  3949. $this->article['required'] = [
  3950. 'user_id' => true
  3951. ];
  3952. $this->Form->create($this->article);
  3953. $result = $this->Form->select('user_id', array('option A'));
  3954. $expected = array(
  3955. 'select' => array(
  3956. 'name' => 'user_id',
  3957. 'required' => 'required'
  3958. ),
  3959. array('option' => array('value' => '0')), 'option A', '/option',
  3960. '/select'
  3961. );
  3962. $this->assertTags($result, $expected);
  3963. $result = $this->Form->select('user_id', array('option A'), array('disabled' => true));
  3964. $expected = array(
  3965. 'select' => array(
  3966. 'name' => 'user_id',
  3967. 'disabled' => 'disabled'
  3968. ),
  3969. array('option' => array('value' => '0')), 'option A', '/option',
  3970. '/select'
  3971. );
  3972. $this->assertTags($result, $expected);
  3973. }
  3974. /**
  3975. * testNestedSelect method
  3976. *
  3977. * test select element generation with optgroups
  3978. *
  3979. * @return void
  3980. */
  3981. public function testNestedSelect() {
  3982. $result = $this->Form->select(
  3983. 'Model.field',
  3984. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3985. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3986. )), array('empty' => false)
  3987. );
  3988. $expected = array(
  3989. 'select' => array('name' => 'Model[field]'),
  3990. array('option' => array('value' => 1)),
  3991. 'One',
  3992. '/option',
  3993. array('option' => array('value' => 2)),
  3994. 'Two',
  3995. '/option',
  3996. array('optgroup' => array('label' => 'Three')),
  3997. array('option' => array('value' => 3)),
  3998. 'Three',
  3999. '/option',
  4000. array('option' => array('value' => 4)),
  4001. 'Four',
  4002. '/option',
  4003. array('option' => array('value' => 5)),
  4004. 'Five',
  4005. '/option',
  4006. '/optgroup',
  4007. '/select'
  4008. );
  4009. $this->assertTags($result, $expected);
  4010. }
  4011. /**
  4012. * testSelectMultiple method
  4013. *
  4014. * test generation of multiple select elements
  4015. *
  4016. * @return void
  4017. */
  4018. public function testSelectMultiple() {
  4019. $options = array('first', 'second', 'third');
  4020. $result = $this->Form->select(
  4021. 'Model.multi_field',
  4022. $options,
  4023. array('multiple' => true)
  4024. );
  4025. $expected = array(
  4026. 'input' => array(
  4027. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4028. ),
  4029. 'select' => array(
  4030. 'name' => 'Model[multi_field][]',
  4031. 'multiple' => 'multiple'
  4032. ),
  4033. array('option' => array('value' => '0')),
  4034. 'first',
  4035. '/option',
  4036. array('option' => array('value' => '1')),
  4037. 'second',
  4038. '/option',
  4039. array('option' => array('value' => '2')),
  4040. 'third',
  4041. '/option',
  4042. '/select'
  4043. );
  4044. $this->assertTags($result, $expected);
  4045. $result = $this->Form->select(
  4046. 'Model.multi_field',
  4047. $options,
  4048. array('multiple' => 'multiple')
  4049. );
  4050. $this->assertTags($result, $expected);
  4051. }
  4052. /**
  4053. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4054. *
  4055. * @return void
  4056. */
  4057. public function testCheckboxZeroValue() {
  4058. $this->markTestIncomplete('Need to revisit once models work again.');
  4059. $result = $this->Form->input('User.get_spam', array(
  4060. 'type' => 'checkbox',
  4061. 'value' => '0',
  4062. 'hiddenField' => '1',
  4063. ));
  4064. $expected = array(
  4065. 'div' => array('class' => 'input checkbox'),
  4066. array('input' => array(
  4067. 'type' => 'hidden', 'name' => 'data[User][get_spam]',
  4068. 'value' => '1', 'id' => 'UserGetSpam_'
  4069. )),
  4070. array('input' => array(
  4071. 'type' => 'checkbox', 'name' => 'data[User][get_spam]',
  4072. 'value' => '0', 'id' => 'UserGetSpam'
  4073. )),
  4074. 'label' => array('for' => 'UserGetSpam'),
  4075. 'Get Spam',
  4076. '/label',
  4077. '/div'
  4078. );
  4079. $this->assertTags($result, $expected);
  4080. }
  4081. /**
  4082. * test generation of habtm select boxes.
  4083. *
  4084. * @return void
  4085. */
  4086. public function testHabtmSelectBox() {
  4087. $this->markTestIncomplete('Need to revisit once models work again.');
  4088. $this->View->viewVars['contactTags'] = array(
  4089. 1 => 'blue',
  4090. 2 => 'red',
  4091. 3 => 'green'
  4092. );
  4093. $this->Form->request->data = array(
  4094. 'Contact' => array(),
  4095. 'ContactTag' => array(
  4096. array(
  4097. 'id' => '1',
  4098. 'name' => 'blue'
  4099. ),
  4100. array(
  4101. 'id' => 3,
  4102. 'name' => 'green'
  4103. )
  4104. )
  4105. );
  4106. $this->Form->create('Contact');
  4107. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4108. $expected = array(
  4109. 'input' => array(
  4110. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4111. ),
  4112. 'select' => array(
  4113. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  4114. 'multiple' => 'multiple'
  4115. ),
  4116. array('option' => array('value' => '1', 'selected' => 'selected')),
  4117. 'blue',
  4118. '/option',
  4119. array('option' => array('value' => '2')),
  4120. 'red',
  4121. '/option',
  4122. array('option' => array('value' => '3', 'selected' => 'selected')),
  4123. 'green',
  4124. '/option',
  4125. '/select'
  4126. );
  4127. $this->assertTags($result, $expected);
  4128. // make sure only 50 is selected, and not 50f5c0cf
  4129. $this->View->viewVars['contactTags'] = array(
  4130. '1' => 'blue',
  4131. '50f5c0cf' => 'red',
  4132. '50' => 'green'
  4133. );
  4134. $this->Form->request->data = array(
  4135. 'Contact' => array(),
  4136. 'ContactTag' => array(
  4137. array(
  4138. 'id' => 1,
  4139. 'name' => 'blue'
  4140. ),
  4141. array(
  4142. 'id' => 50,
  4143. 'name' => 'green'
  4144. )
  4145. )
  4146. );
  4147. $this->Form->create('Contact');
  4148. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4149. $expected = array(
  4150. 'input' => array(
  4151. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4152. ),
  4153. 'select' => array(
  4154. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  4155. 'multiple' => 'multiple'
  4156. ),
  4157. array('option' => array('value' => '1', 'selected' => 'selected')),
  4158. 'blue',
  4159. '/option',
  4160. array('option' => array('value' => '50f5c0cf')),
  4161. 'red',
  4162. '/option',
  4163. array('option' => array('value' => '50', 'selected' => 'selected')),
  4164. 'green',
  4165. '/option',
  4166. '/select'
  4167. );
  4168. $this->assertTags($result, $expected);
  4169. }
  4170. /**
  4171. * test generation of multi select elements in checkbox format
  4172. *
  4173. * @return void
  4174. */
  4175. public function testSelectMultipleCheckboxes() {
  4176. $result = $this->Form->select(
  4177. 'Model.multi_field',
  4178. array('first', 'second', 'third'),
  4179. array('multiple' => 'checkbox')
  4180. );
  4181. $expected = array(
  4182. 'input' => array(
  4183. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4184. ),
  4185. array('div' => array('class' => 'checkbox')),
  4186. array('input' => array(
  4187. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4188. 'value' => '0', 'id' => 'model-multi-field-0'
  4189. )),
  4190. array('label' => array('for' => 'model-multi-field-0')),
  4191. 'first',
  4192. '/label',
  4193. '/div',
  4194. array('div' => array('class' => 'checkbox')),
  4195. array('input' => array(
  4196. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4197. 'value' => '1', 'id' => 'model-multi-field-1'
  4198. )),
  4199. array('label' => array('for' => 'model-multi-field-1')),
  4200. 'second',
  4201. '/label',
  4202. '/div',
  4203. array('div' => array('class' => 'checkbox')),
  4204. array('input' => array(
  4205. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4206. 'value' => '2', 'id' => 'model-multi-field-2'
  4207. )),
  4208. array('label' => array('for' => 'model-multi-field-2')),
  4209. 'third',
  4210. '/label',
  4211. '/div'
  4212. );
  4213. $this->assertTags($result, $expected);
  4214. $result = $this->Form->select(
  4215. 'Model.multi_field',
  4216. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  4217. array('multiple' => 'checkbox')
  4218. );
  4219. $expected = array(
  4220. 'input' => array(
  4221. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4222. ),
  4223. array('div' => array('class' => 'checkbox')),
  4224. array('input' => array(
  4225. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4226. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  4227. )),
  4228. array('label' => array('for' => 'model-multi-field-a+')),
  4229. 'first',
  4230. '/label',
  4231. '/div',
  4232. array('div' => array('class' => 'checkbox')),
  4233. array('input' => array(
  4234. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4235. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  4236. )),
  4237. array('label' => array('for' => 'model-multi-field-a++')),
  4238. 'second',
  4239. '/label',
  4240. '/div',
  4241. array('div' => array('class' => 'checkbox')),
  4242. array('input' => array(
  4243. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4244. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  4245. )),
  4246. array('label' => array('for' => 'model-multi-field-a+++')),
  4247. 'third',
  4248. '/label',
  4249. '/div'
  4250. );
  4251. $this->assertTags($result, $expected);
  4252. $result = $this->Form->select(
  4253. 'Model.multi_field',
  4254. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4255. array('multiple' => 'checkbox')
  4256. );
  4257. $expected = array(
  4258. 'input' => array(
  4259. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4260. ),
  4261. array('div' => array('class' => 'checkbox')),
  4262. array('input' => array(
  4263. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4264. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  4265. )),
  4266. array('label' => array('for' => 'model-multi-field-a-b')),
  4267. 'first',
  4268. '/label',
  4269. '/div',
  4270. array('div' => array('class' => 'checkbox')),
  4271. array('input' => array(
  4272. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4273. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  4274. )),
  4275. array('label' => array('for' => 'model-multi-field-a-b1')),
  4276. 'second',
  4277. '/label',
  4278. '/div',
  4279. array('div' => array('class' => 'checkbox')),
  4280. array('input' => array(
  4281. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4282. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  4283. )),
  4284. array('label' => array('for' => 'model-multi-field-a-b2')),
  4285. 'third',
  4286. '/label',
  4287. '/div'
  4288. );
  4289. $this->assertTags($result, $expected);
  4290. }
  4291. /**
  4292. * Ensure that multiCheckbox reads from the request data.
  4293. *
  4294. * @return void
  4295. */
  4296. public function testSelectMultipleCheckboxRequestData() {
  4297. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4298. $result = $this->Form->select(
  4299. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4300. );
  4301. $expected = array(
  4302. 'input' => array(
  4303. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  4304. ),
  4305. array('div' => array('class' => 'checkbox')),
  4306. array('input' => array(
  4307. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4308. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  4309. )),
  4310. array('label' => array('for' => 'model-tags-1', 'class' => 'selected')),
  4311. 'first',
  4312. '/label',
  4313. '/div',
  4314. array('div' => array('class' => 'checkbox')),
  4315. array('input' => array(
  4316. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4317. 'value' => 'Array', 'id' => 'model-tags-array'
  4318. )),
  4319. array('label' => array('for' => 'model-tags-array')),
  4320. 'Array',
  4321. '/label',
  4322. '/div'
  4323. );
  4324. $this->assertTags($result, $expected);
  4325. }
  4326. /**
  4327. * Checks the security hash array generated for multiple-input checkbox elements
  4328. *
  4329. * @return void
  4330. */
  4331. public function testSelectMultipleCheckboxSecurity() {
  4332. $this->Form->request->params['_Token'] = 'testKey';
  4333. $this->assertEquals(array(), $this->Form->fields);
  4334. $result = $this->Form->select(
  4335. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4336. array('multiple' => 'checkbox')
  4337. );
  4338. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4339. $result = $this->Form->secure($this->Form->fields);
  4340. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4341. $this->assertRegExp('/"' . $key . '"/', $result);
  4342. }
  4343. /**
  4344. * Multiple select elements should always be secured as they always participate
  4345. * in the POST data.
  4346. *
  4347. * @return void
  4348. */
  4349. public function testSelectMultipleSecureWithNoOptions() {
  4350. $this->assertEquals(array(), $this->Form->fields);
  4351. $this->Form->select(
  4352. 'Model.select',
  4353. array(),
  4354. array('multiple' => true)
  4355. );
  4356. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4357. }
  4358. /**
  4359. * When a select box has no options it should not be added to the fields list
  4360. * as it always fail post validation.
  4361. *
  4362. * @return void
  4363. */
  4364. public function testSelectNoSecureWithNoOptions() {
  4365. $this->Form->request->params['_Token'] = 'testkey';
  4366. $this->assertEquals([], $this->Form->fields);
  4367. $this->Form->select(
  4368. 'Model.select',
  4369. []
  4370. );
  4371. $this->assertEquals([], $this->Form->fields);
  4372. $this->Form->select(
  4373. 'Model.user_id',
  4374. [],
  4375. ['empty' => true]
  4376. );
  4377. $this->assertEquals(array('Model.user_id'), $this->Form->fields);
  4378. }
  4379. /**
  4380. * testInputMultipleCheckboxes method
  4381. *
  4382. * test input() resulting in multi select elements being generated.
  4383. *
  4384. * @return void
  4385. */
  4386. public function testInputMultipleCheckboxes() {
  4387. $this->markTestIncomplete('Need to revisit once models work again.');
  4388. $result = $this->Form->input('Model.multi_field', array(
  4389. 'options' => array('first', 'second', 'third'),
  4390. 'multiple' => 'checkbox'
  4391. ));
  4392. $expected = array(
  4393. array('div' => array('class' => 'input select')),
  4394. array('label' => array('for' => 'ModelMultiField')),
  4395. 'Multi Field',
  4396. '/label',
  4397. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4398. array('div' => array('class' => 'checkbox')),
  4399. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4400. array('label' => array('for' => 'ModelMultiField0')),
  4401. 'first',
  4402. '/label',
  4403. '/div',
  4404. array('div' => array('class' => 'checkbox')),
  4405. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4406. array('label' => array('for' => 'ModelMultiField1')),
  4407. 'second',
  4408. '/label',
  4409. '/div',
  4410. array('div' => array('class' => 'checkbox')),
  4411. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4412. array('label' => array('for' => 'ModelMultiField2')),
  4413. 'third',
  4414. '/label',
  4415. '/div',
  4416. '/div'
  4417. );
  4418. $this->assertTags($result, $expected);
  4419. $result = $this->Form->input('Model.multi_field', array(
  4420. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4421. 'multiple' => 'checkbox'
  4422. ));
  4423. $expected = array(
  4424. array('div' => array('class' => 'input select')),
  4425. array('label' => array('for' => 'ModelMultiField')),
  4426. 'Multi Field',
  4427. '/label',
  4428. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4429. array('div' => array('class' => 'checkbox')),
  4430. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4431. array('label' => array('for' => 'ModelMultiFieldA')),
  4432. 'first',
  4433. '/label',
  4434. '/div',
  4435. array('div' => array('class' => 'checkbox')),
  4436. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4437. array('label' => array('for' => 'ModelMultiFieldB')),
  4438. 'second',
  4439. '/label',
  4440. '/div',
  4441. array('div' => array('class' => 'checkbox')),
  4442. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4443. array('label' => array('for' => 'ModelMultiFieldC')),
  4444. 'third',
  4445. '/label',
  4446. '/div',
  4447. '/div'
  4448. );
  4449. $this->assertTags($result, $expected);
  4450. $result = $this->Form->input('Model.multi_field', array(
  4451. 'options' => array('1' => 'first'),
  4452. 'multiple' => 'checkbox',
  4453. 'label' => false,
  4454. 'div' => false
  4455. ));
  4456. $expected = array(
  4457. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4458. array('div' => array('class' => 'checkbox')),
  4459. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4460. array('label' => array('for' => 'ModelMultiField1')),
  4461. 'first',
  4462. '/label',
  4463. '/div'
  4464. );
  4465. $this->assertTags($result, $expected);
  4466. $result = $this->Form->input('Model.multi_field', array(
  4467. 'options' => array('2' => 'second'),
  4468. 'multiple' => 'checkbox',
  4469. 'label' => false,
  4470. 'div' => false
  4471. ));
  4472. $expected = array(
  4473. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4474. array('div' => array('class' => 'checkbox')),
  4475. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4476. array('label' => array('for' => 'ModelMultiField2')),
  4477. 'second',
  4478. '/label',
  4479. '/div'
  4480. );
  4481. $this->assertTags($result, $expected);
  4482. }
  4483. /**
  4484. * testSelectHiddenFieldOmission method
  4485. *
  4486. * test that select() with 'hiddenField' => false omits the hidden field
  4487. *
  4488. * @return void
  4489. */
  4490. public function testSelectHiddenFieldOmission() {
  4491. $result = $this->Form->select('Model.multi_field',
  4492. array('first', 'second'),
  4493. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4494. );
  4495. $this->assertNotContains('type="hidden"', $result);
  4496. }
  4497. /**
  4498. * test that select() with multiple = checkbox works with overriding name attribute.
  4499. *
  4500. * @return void
  4501. */
  4502. public function testSelectCheckboxMultipleOverrideName() {
  4503. $result = $this->Form->select('category', ['1', '2'], [
  4504. 'multiple' => 'checkbox',
  4505. 'name' => 'fish',
  4506. ]);
  4507. $expected = array(
  4508. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => ''),
  4509. array('div' => array('class' => 'checkbox')),
  4510. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0')),
  4511. array('label' => array('for' => 'fish-0')), '1', '/label',
  4512. '/div',
  4513. array('div' => array('class' => 'checkbox')),
  4514. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1')),
  4515. array('label' => array('for' => 'fish-1')), '2', '/label',
  4516. '/div'
  4517. );
  4518. $this->assertTags($result, $expected);
  4519. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  4520. 'name' => 'fish',
  4521. ]);
  4522. $this->assertTags($result, $expected);
  4523. }
  4524. /**
  4525. * testCheckbox method
  4526. *
  4527. * Test generation of checkboxes
  4528. *
  4529. * @return void
  4530. */
  4531. public function testCheckbox() {
  4532. $result = $this->Form->checkbox('Model.field');
  4533. $expected = array(
  4534. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  4535. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'))
  4536. );
  4537. $this->assertTags($result, $expected);
  4538. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4539. $expected = array(
  4540. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  4541. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => 'myvalue', 'id' => 'theID'))
  4542. );
  4543. $this->assertTags($result, $expected);
  4544. }
  4545. /**
  4546. * testCheckboxDefaultValue method
  4547. *
  4548. * Test default value setting on checkbox() method
  4549. *
  4550. * @return void
  4551. */
  4552. public function testCheckboxDefaultValue() {
  4553. $this->Form->request->data['Model']['field'] = false;
  4554. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  4555. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  4556. unset($this->Form->request->data['Model']['field']);
  4557. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  4558. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  4559. $this->Form->request->data['Model']['field'] = true;
  4560. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  4561. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  4562. unset($this->Form->request->data['Model']['field']);
  4563. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  4564. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  4565. }
  4566. /**
  4567. * Test checkbox being checked or having errors.
  4568. *
  4569. * @return void
  4570. */
  4571. public function testCheckboxCheckedAndError() {
  4572. $this->article['errors'] = [
  4573. 'published' => true
  4574. ];
  4575. $this->Form->request->data['published'] = 'myvalue';
  4576. $this->Form->create($this->article);
  4577. $result = $this->Form->checkbox('published', array('id' => 'theID', 'value' => 'myvalue'));
  4578. $expected = array(
  4579. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4580. array('input' => array(
  4581. 'type' => 'checkbox',
  4582. 'name' => 'published',
  4583. 'value' => 'myvalue',
  4584. 'id' => 'theID',
  4585. 'checked' => 'checked',
  4586. 'class' => 'form-error'
  4587. ))
  4588. );
  4589. $this->assertTags($result, $expected);
  4590. $this->Form->request->data['published'] = '';
  4591. $result = $this->Form->checkbox('published');
  4592. $expected = array(
  4593. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4594. array('input' => array('type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error'))
  4595. );
  4596. $this->assertTags($result, $expected);
  4597. }
  4598. /**
  4599. * test checkbox() with a custom name attribute
  4600. *
  4601. * @return void
  4602. */
  4603. public function testCheckboxCustomNameAttribute() {
  4604. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4605. $expected = array(
  4606. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0'),
  4607. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1'))
  4608. );
  4609. $this->assertTags($result, $expected);
  4610. }
  4611. /**
  4612. * Test that the hidden input for checkboxes can be omitted or set to a
  4613. * specific value.
  4614. *
  4615. * @return void
  4616. */
  4617. public function testCheckboxHiddenField() {
  4618. $result = $this->Form->checkbox('UserForm.something', array(
  4619. 'hiddenField' => false
  4620. ));
  4621. $expected = array(
  4622. 'input' => array(
  4623. 'type' => 'checkbox',
  4624. 'name' => 'UserForm[something]',
  4625. 'value' => '1'
  4626. ),
  4627. );
  4628. $this->assertTags($result, $expected);
  4629. $result = $this->Form->checkbox('UserForm.something', array(
  4630. 'value' => 'Y',
  4631. 'hiddenField' => 'N',
  4632. ));
  4633. $expected = array(
  4634. array('input' => array(
  4635. 'type' => 'hidden', 'name' => 'UserForm[something]',
  4636. 'value' => 'N'
  4637. )),
  4638. array('input' => array(
  4639. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  4640. 'value' => 'Y'
  4641. )),
  4642. );
  4643. $this->assertTags($result, $expected);
  4644. }
  4645. /**
  4646. * testDateTime method
  4647. *
  4648. * Test generation of date/time select elements
  4649. *
  4650. * @return void
  4651. */
  4652. public function testDateTime() {
  4653. extract($this->dateRegex);
  4654. $result = $this->Form->dateTime('Contact.date', array('empty' => false));
  4655. $now = strtotime('now');
  4656. $expected = array(
  4657. array('select' => array('name' => 'Contact[date][month]')),
  4658. $monthsRegex,
  4659. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4660. date('F', $now),
  4661. '/option',
  4662. '*/select',
  4663. array('select' => array('name' => 'Contact[date][day]')),
  4664. $daysRegex,
  4665. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4666. date('j', $now),
  4667. '/option',
  4668. '*/select',
  4669. array('select' => array('name' => 'Contact[date][year]')),
  4670. $yearsRegex,
  4671. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4672. date('Y', $now),
  4673. '/option',
  4674. '*/select',
  4675. array('select' => array('name' => 'Contact[date][hour]')),
  4676. $hoursRegex,
  4677. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4678. date('g', $now),
  4679. '/option',
  4680. '*/select',
  4681. array('select' => array('name' => 'Contact[date][minute]')),
  4682. $minutesRegex,
  4683. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4684. date('i', $now),
  4685. '/option',
  4686. '*/select',
  4687. array('select' => array('name' => 'Contact[date][meridian]')),
  4688. $meridianRegex,
  4689. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4690. date('a', $now),
  4691. '/option',
  4692. '*/select'
  4693. );
  4694. $this->assertTags($result, $expected);
  4695. }
  4696. /**
  4697. * Test empty defaulting to true for datetime.
  4698. *
  4699. * @return void
  4700. */
  4701. public function testDatetimeEmpty() {
  4702. extract($this->dateRegex);
  4703. $now = strtotime('now');
  4704. $result = $this->Form->dateTime('Contact.date', array(
  4705. 'empty' => true,
  4706. ));
  4707. $expected = array(
  4708. array('select' => array('name' => 'Contact[date][month]')),
  4709. $monthsRegex,
  4710. array('option' => array('value' => '')),
  4711. '/option',
  4712. '*/select',
  4713. array('select' => array('name' => 'Contact[date][day]')),
  4714. $daysRegex,
  4715. array('option' => array('value' => '')),
  4716. '/option',
  4717. '*/select',
  4718. array('select' => array('name' => 'Contact[date][year]')),
  4719. $yearsRegex,
  4720. array('option' => array('value' => '')),
  4721. '/option',
  4722. '*/select',
  4723. array('select' => array('name' => 'Contact[date][hour]')),
  4724. $hoursRegex,
  4725. array('option' => array('value' => '')),
  4726. '/option',
  4727. '*/select',
  4728. array('select' => array('name' => 'Contact[date][minute]')),
  4729. $minutesRegex,
  4730. array('option' => array('value' => '')),
  4731. '/option',
  4732. '*/select',
  4733. array('select' => array('name' => 'Contact[date][meridian]')),
  4734. $meridianRegex,
  4735. array('option' => array('value' => '')),
  4736. '/option',
  4737. '*/select'
  4738. );
  4739. $this->assertTags($result, $expected);
  4740. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4741. }
  4742. /**
  4743. * Test datetime with interval option.
  4744. *
  4745. * @return void
  4746. */
  4747. public function testDatetimeMinuteInterval() {
  4748. extract($this->dateRegex);
  4749. $now = strtotime('now');
  4750. $result = $this->Form->dateTime('Contact.date', array(
  4751. 'interval' => 5,
  4752. 'value' => ''
  4753. ));
  4754. $expected = array(
  4755. array('select' => array('name' => 'Contact[date][month]')),
  4756. $monthsRegex,
  4757. array('option' => array('selected' => 'selected', 'value' => '')),
  4758. '/option',
  4759. '*/select',
  4760. array('select' => array('name' => 'Contact[date][day]')),
  4761. $daysRegex,
  4762. array('option' => array('selected' => 'selected', 'value' => '')),
  4763. '/option',
  4764. '*/select',
  4765. array('select' => array('name' => 'Contact[date][year]')),
  4766. $yearsRegex,
  4767. array('option' => array('selected' => 'selected', 'value' => '')),
  4768. '/option',
  4769. '*/select',
  4770. array('select' => array('name' => 'Contact[date][hour]')),
  4771. $hoursRegex,
  4772. array('option' => array('selected' => 'selected', 'value' => '')),
  4773. '/option',
  4774. '*/select',
  4775. array('select' => array('name' => 'Contact[date][minute]')),
  4776. $minutesRegex,
  4777. array('option' => array('selected' => 'selected', 'value' => '')),
  4778. '/option',
  4779. array('option' => array('value' => '00')),
  4780. '00',
  4781. '/option',
  4782. array('option' => array('value' => '05')),
  4783. '05',
  4784. '/option',
  4785. array('option' => array('value' => '10')),
  4786. '10',
  4787. '/option',
  4788. '*/select',
  4789. );
  4790. $this->assertTags($result, $expected);
  4791. }
  4792. /**
  4793. * Test dateTime with rounding
  4794. *
  4795. * @return void
  4796. */
  4797. public function testDateTimeRounding() {
  4798. $this->Form->request->data['Contact'] = array(
  4799. 'date' => array(
  4800. 'day' => '13',
  4801. 'month' => '12',
  4802. 'year' => '2010',
  4803. 'hour' => '04',
  4804. 'minute' => '19',
  4805. 'meridian' => 'AM'
  4806. )
  4807. );
  4808. $result = $this->Form->dateTime('Contact.date', array('interval' => 15));
  4809. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4810. $result = $this->Form->dateTime('Contact.date', array('interval' => 15, 'round' => 'up'));
  4811. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  4812. $result = $this->Form->dateTime('Contact.date', array('interval' => 5, 'round' => 'down'));
  4813. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4814. }
  4815. /**
  4816. * test that datetime() and default values work.
  4817. *
  4818. * @return void
  4819. */
  4820. public function testDatetimeWithDefault() {
  4821. $this->markTestIncomplete('Need to revisit soon.');
  4822. $result = $this->Form->dateTime('Contact.updated', array('value' => '2009-06-01 11:15:30'));
  4823. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4824. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4825. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4826. $result = $this->Form->dateTime('Contact.updated', array(
  4827. 'default' => '2009-06-01 11:15:30'
  4828. ));
  4829. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4830. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4831. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4832. }
  4833. /**
  4834. * testDateTime all zeros
  4835. *
  4836. * @return void
  4837. */
  4838. public function testDateTimeAllZeros() {
  4839. $result = $this->Form->dateTime('Contact.date', array(
  4840. 'timeFormat' => false,
  4841. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  4842. 'value' => '0000-00-00'
  4843. ));
  4844. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  4845. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  4846. }
  4847. /**
  4848. * testDateTimeEmptyAsArray
  4849. *
  4850. * @return void
  4851. */
  4852. public function testDateTimeEmptyAsArray() {
  4853. $result = $this->Form->dateTime('Contact.date', array(
  4854. 'empty' => array(
  4855. 'day' => 'DAY',
  4856. 'month' => 'MONTH',
  4857. 'year' => 'YEAR',
  4858. 'hour' => 'HOUR',
  4859. 'minute' => 'MINUTE',
  4860. 'meridian' => false
  4861. )
  4862. ));
  4863. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4864. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4865. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4866. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4867. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4868. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4869. $result = $this->Form->dateTime('Contact.date', array(
  4870. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4871. ));
  4872. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4873. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4874. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4875. }
  4876. /**
  4877. * testFormDateTimeMulti method
  4878. *
  4879. * test multiple datetime element generation
  4880. *
  4881. * @return void
  4882. */
  4883. public function testFormDateTimeMulti() {
  4884. extract($this->dateRegex);
  4885. $result = $this->Form->dateTime('Contact.1.updated');
  4886. $this->assertContains('Contact[1][updated][month]', $result);
  4887. $this->assertContains('Contact[1][updated][day]', $result);
  4888. $this->assertContains('Contact[1][updated][year]', $result);
  4889. $this->assertContains('Contact[1][updated][hour]', $result);
  4890. $this->assertContains('Contact[1][updated][minute]', $result);
  4891. $this->assertContains('Contact[1][updated][meridian]', $result);
  4892. }
  4893. /**
  4894. * When changing the date format, the label should always focus the first select box when
  4895. * clicked.
  4896. *
  4897. * @return void
  4898. */
  4899. public function testDateTimeLabelIdMatchesFirstInput() {
  4900. $this->markTestIncomplete('Need to revisit once models work again.');
  4901. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4902. $this->assertContains('label for="ModelDateMonth"', $result);
  4903. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4904. $this->assertContains('label for="ModelDateDay"', $result);
  4905. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4906. $this->assertContains('label for="ModelDateYear"', $result);
  4907. }
  4908. /**
  4909. * testMonth method
  4910. *
  4911. * @return void
  4912. */
  4913. public function testMonth() {
  4914. $result = $this->Form->month('Model.field', ['value' => '']);
  4915. $expected = array(
  4916. array('select' => array('name' => 'Model[field][month]')),
  4917. array('option' => array('value' => '', 'selected' => 'selected')),
  4918. '/option',
  4919. array('option' => array('value' => '01')),
  4920. date('F', strtotime('2008-01-01 00:00:00')),
  4921. '/option',
  4922. array('option' => array('value' => '02')),
  4923. date('F', strtotime('2008-02-01 00:00:00')),
  4924. '/option',
  4925. '*/select',
  4926. );
  4927. $this->assertTags($result, $expected);
  4928. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  4929. $expected = array(
  4930. array('select' => array('name' => 'Model[field][month]')),
  4931. array('option' => array('selected' => 'selected', 'value' => '')),
  4932. '/option',
  4933. array('option' => array('value' => '01')),
  4934. date('F', strtotime('2008-01-01 00:00:00')),
  4935. '/option',
  4936. array('option' => array('value' => '02')),
  4937. date('F', strtotime('2008-02-01 00:00:00')),
  4938. '/option',
  4939. '*/select',
  4940. );
  4941. $this->assertTags($result, $expected);
  4942. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  4943. $expected = array(
  4944. array('select' => array('name' => 'Model[field][month]')),
  4945. array('option' => array('selected' => 'selected', 'value' => '')),
  4946. '/option',
  4947. array('option' => array('value' => '01')),
  4948. '1',
  4949. '/option',
  4950. array('option' => array('value' => '02')),
  4951. '2',
  4952. '/option',
  4953. '*/select',
  4954. );
  4955. $this->assertTags($result, $expected);
  4956. $monthNames = [
  4957. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4958. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  4959. ];
  4960. $result = $this->Form->month('Model.field', array('value' => '1', 'monthNames' => $monthNames));
  4961. $expected = array(
  4962. array('select' => array('name' => 'Model[field][month]')),
  4963. array('option' => array('value' => '')),
  4964. '/option',
  4965. array('option' => array('value' => '01', 'selected' => 'selected')),
  4966. 'Jan',
  4967. '/option',
  4968. array('option' => array('value' => '02')),
  4969. 'Feb',
  4970. '/option',
  4971. '*/select',
  4972. );
  4973. $this->assertTags($result, $expected);
  4974. $this->Form->request->data['Project']['release'] = '2050-02-10';
  4975. $result = $this->Form->month('Project.release');
  4976. $expected = array(
  4977. array('select' => array('name' => 'Project[release][month]')),
  4978. array('option' => array('value' => '')),
  4979. '/option',
  4980. array('option' => array('value' => '01')),
  4981. 'January',
  4982. '/option',
  4983. array('option' => array('value' => '02', 'selected' => 'selected')),
  4984. 'February',
  4985. '/option',
  4986. '*/select',
  4987. );
  4988. $this->assertTags($result, $expected);
  4989. }
  4990. /**
  4991. * testDay method
  4992. *
  4993. * @return void
  4994. */
  4995. public function testDay() {
  4996. extract($this->dateRegex);
  4997. $result = $this->Form->day('Model.field', array('value' => false));
  4998. $expected = array(
  4999. array('select' => array('name' => 'Model[field][day]')),
  5000. array('option' => array('selected' => 'selected', 'value' => '')),
  5001. '/option',
  5002. array('option' => array('value' => '01')),
  5003. '1',
  5004. '/option',
  5005. array('option' => array('value' => '02')),
  5006. '2',
  5007. '/option',
  5008. $daysRegex,
  5009. '/select',
  5010. );
  5011. $this->assertTags($result, $expected);
  5012. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5013. $result = $this->Form->day('Model.field');
  5014. $expected = array(
  5015. array('select' => array('name' => 'Model[field][day]')),
  5016. array('option' => array('value' => '')),
  5017. '/option',
  5018. array('option' => array('value' => '01')),
  5019. '1',
  5020. '/option',
  5021. array('option' => array('value' => '02')),
  5022. '2',
  5023. '/option',
  5024. $daysRegex,
  5025. array('option' => array('value' => '10', 'selected' => 'selected')),
  5026. '10',
  5027. '/option',
  5028. $daysRegex,
  5029. '/select',
  5030. );
  5031. $this->assertTags($result, $expected);
  5032. $this->Form->request->data['Model']['field'] = '';
  5033. $result = $this->Form->day('Model.field', array('value' => '10'));
  5034. $expected = array(
  5035. array('select' => array('name' => 'Model[field][day]')),
  5036. array('option' => array('value' => '')),
  5037. '/option',
  5038. array('option' => array('value' => '01')),
  5039. '1',
  5040. '/option',
  5041. array('option' => array('value' => '02')),
  5042. '2',
  5043. '/option',
  5044. $daysRegex,
  5045. array('option' => array('value' => '10', 'selected' => 'selected')),
  5046. '10',
  5047. '/option',
  5048. $daysRegex,
  5049. '/select',
  5050. );
  5051. $this->assertTags($result, $expected);
  5052. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5053. $result = $this->Form->day('Project.release');
  5054. $expected = array(
  5055. array('select' => array('name' => 'Project[release][day]')),
  5056. array('option' => array('value' => '')),
  5057. '/option',
  5058. array('option' => array('value' => '01')),
  5059. '1',
  5060. '/option',
  5061. array('option' => array('value' => '02')),
  5062. '2',
  5063. '/option',
  5064. $daysRegex,
  5065. array('option' => array('value' => '10', 'selected' => 'selected')),
  5066. '10',
  5067. '/option',
  5068. $daysRegex,
  5069. '/select',
  5070. );
  5071. $this->assertTags($result, $expected);
  5072. }
  5073. /**
  5074. * testMinute method
  5075. *
  5076. * @return void
  5077. */
  5078. public function testMinute() {
  5079. extract($this->dateRegex);
  5080. $result = $this->Form->minute('Model.field', ['value' => '']);
  5081. $expected = array(
  5082. array('select' => array('name' => 'Model[field][minute]')),
  5083. array('option' => array('selected' => 'selected', 'value' => '')),
  5084. '/option',
  5085. array('option' => array('value' => '00')),
  5086. '00',
  5087. '/option',
  5088. array('option' => array('value' => '01')),
  5089. '01',
  5090. '/option',
  5091. array('option' => array('value' => '02')),
  5092. '02',
  5093. '/option',
  5094. $minutesRegex,
  5095. '/select',
  5096. );
  5097. $this->assertTags($result, $expected);
  5098. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5099. $result = $this->Form->minute('Model.field');
  5100. $expected = array(
  5101. array('select' => array('name' => 'Model[field][minute]')),
  5102. array('option' => array('value' => '')),
  5103. '/option',
  5104. array('option' => array('value' => '00')),
  5105. '00',
  5106. '/option',
  5107. array('option' => array('value' => '01')),
  5108. '01',
  5109. '/option',
  5110. array('option' => array('value' => '02')),
  5111. '02',
  5112. '/option',
  5113. $minutesRegex,
  5114. array('option' => array('value' => '12', 'selected' => 'selected')),
  5115. '12',
  5116. '/option',
  5117. $minutesRegex,
  5118. '/select',
  5119. );
  5120. $this->assertTags($result, $expected);
  5121. $this->Form->request->data['Model']['field'] = '';
  5122. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5123. $expected = array(
  5124. array('select' => array('name' => 'Model[field][minute]')),
  5125. array('option' => array('selected' => 'selected', 'value' => '')),
  5126. '/option',
  5127. array('option' => array('value' => '00')),
  5128. '00',
  5129. '/option',
  5130. array('option' => array('value' => '05')),
  5131. '05',
  5132. '/option',
  5133. array('option' => array('value' => '10')),
  5134. '10',
  5135. '/option',
  5136. $minutesRegex,
  5137. '/select',
  5138. );
  5139. $this->assertTags($result, $expected);
  5140. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5141. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5142. $expected = array(
  5143. array('select' => array('name' => 'Model[field][minute]')),
  5144. array('option' => array('value' => '')),
  5145. '/option',
  5146. array('option' => array('value' => '00')),
  5147. '00',
  5148. '/option',
  5149. array('option' => array('value' => '05')),
  5150. '05',
  5151. '/option',
  5152. array('option' => array('value' => '10', 'selected' => 'selected')),
  5153. '10',
  5154. '/option',
  5155. $minutesRegex,
  5156. '/select',
  5157. );
  5158. $this->assertTags($result, $expected);
  5159. }
  5160. /**
  5161. * Test generating an input for the meridian.
  5162. *
  5163. * @return void
  5164. */
  5165. public function testMeridian() {
  5166. extract($this->dateRegex);
  5167. $now = time();
  5168. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  5169. $expected = [
  5170. array('select' => array('name' => 'Model[field][meridian]')),
  5171. array('option' => array('value' => '')),
  5172. '/option',
  5173. $meridianRegex,
  5174. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5175. date('a', $now),
  5176. '/option',
  5177. '*/select'
  5178. ];
  5179. $this->assertTags($result, $expected);
  5180. }
  5181. /**
  5182. * testHour method
  5183. *
  5184. * @return void
  5185. */
  5186. public function testHour() {
  5187. extract($this->dateRegex);
  5188. $result = $this->Form->hour('Model.field', ['value' => '']);
  5189. $expected = array(
  5190. array('select' => array('name' => 'Model[field][hour]')),
  5191. array('option' => array('selected' => 'selected', 'value' => '')),
  5192. '/option',
  5193. array('option' => array('value' => '01')),
  5194. '1',
  5195. '/option',
  5196. array('option' => array('value' => '02')),
  5197. '2',
  5198. '/option',
  5199. $hoursRegex,
  5200. '/select',
  5201. );
  5202. $this->assertTags($result, $expected);
  5203. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5204. $result = $this->Form->hour('Model.field', ['format' => 12]);
  5205. $expected = array(
  5206. array('select' => array('name' => 'Model[field][hour]')),
  5207. array('option' => array('value' => '')),
  5208. '/option',
  5209. array('option' => array('value' => '01')),
  5210. '1',
  5211. '/option',
  5212. array('option' => array('value' => '02')),
  5213. '2',
  5214. '/option',
  5215. $hoursRegex,
  5216. array('option' => array('value' => '12', 'selected' => 'selected')),
  5217. '12',
  5218. '/option',
  5219. '/select',
  5220. );
  5221. $this->assertTags($result, $expected);
  5222. $this->Form->request->data['Model']['field'] = '';
  5223. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => '23'));
  5224. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5225. $result = $this->Form->hour('Model.field', array('format' => 12, 'value' => '23'));
  5226. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5227. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5228. $result = $this->Form->hour('Model.field', ['format' => 24]);
  5229. $expected = array(
  5230. array('select' => array('name' => 'Model[field][hour]')),
  5231. array('option' => array('value' => '')),
  5232. '/option',
  5233. array('option' => array('value' => '00', 'selected' => 'selected')),
  5234. '0',
  5235. '/option',
  5236. array('option' => array('value' => '01')),
  5237. '1',
  5238. '/option',
  5239. array('option' => array('value' => '02')),
  5240. '2',
  5241. '/option',
  5242. $hoursRegex,
  5243. '/select',
  5244. );
  5245. $this->assertTags($result, $expected);
  5246. unset($this->Form->request->data['Model']['field']);
  5247. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => 'now'));
  5248. $thisHour = date('H');
  5249. $optValue = date('G');
  5250. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5251. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  5252. $result = $this->Form->hour('Model.field', ['format' => 24]);
  5253. $expected = array(
  5254. array('select' => array('name' => 'Model[field][hour]')),
  5255. array('option' => array('value' => '')),
  5256. '/option',
  5257. array('option' => array('value' => '00')),
  5258. '0',
  5259. '/option',
  5260. array('option' => array('value' => '01', 'selected' => 'selected')),
  5261. '1',
  5262. '/option',
  5263. array('option' => array('value' => '02')),
  5264. '2',
  5265. '/option',
  5266. $hoursRegex,
  5267. '/select',
  5268. );
  5269. $this->assertTags($result, $expected);
  5270. }
  5271. /**
  5272. * testYear method
  5273. *
  5274. * @return void
  5275. */
  5276. public function testYear() {
  5277. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  5278. $expected = array(
  5279. array('select' => array('name' => 'Model[field][year]')),
  5280. array('option' => array('selected' => 'selected', 'value' => '')),
  5281. '/option',
  5282. array('option' => array('value' => '2007')),
  5283. '2007',
  5284. '/option',
  5285. array('option' => array('value' => '2006')),
  5286. '2006',
  5287. '/option',
  5288. '/select',
  5289. );
  5290. $this->assertTags($result, $expected);
  5291. $result = $this->Form->year('Model.field', [
  5292. 'value' => '',
  5293. 'minYear' => 2006,
  5294. 'maxYear' => 2007,
  5295. 'orderYear' => 'asc'
  5296. ]);
  5297. $expected = array(
  5298. array('select' => array('name' => 'Model[field][year]')),
  5299. array('option' => array('selected' => 'selected', 'value' => '')),
  5300. '/option',
  5301. array('option' => array('value' => '2006')),
  5302. '2006',
  5303. '/option',
  5304. array('option' => array('value' => '2007')),
  5305. '2007',
  5306. '/option',
  5307. '/select',
  5308. );
  5309. $this->assertTags($result, $expected);
  5310. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5311. $result = $this->Form->year('Contact.published', [
  5312. 'empty' => false,
  5313. 'minYear' => 2006,
  5314. 'maxYear' => 2007,
  5315. ]);
  5316. $expected = array(
  5317. array('select' => array('name' => 'Contact[published][year]')),
  5318. array('option' => array('value' => '2007')),
  5319. '2007',
  5320. '/option',
  5321. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5322. '2006',
  5323. '/option',
  5324. '/select',
  5325. );
  5326. $this->assertTags($result, $expected);
  5327. }
  5328. /**
  5329. * testYearAutoExpandRange method
  5330. *
  5331. * @return void
  5332. */
  5333. public function testYearAutoExpandRange() {
  5334. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  5335. $result = $this->Form->year('User.birthday');
  5336. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5337. $result = $matches[1];
  5338. $expected = range(date('Y') + 5, 1930);
  5339. $this->assertEquals($expected, $result);
  5340. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5341. $result = $this->Form->year('Project.release');
  5342. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5343. $result = $matches[1];
  5344. $expected = range(2050, date('Y') - 5);
  5345. $this->assertEquals($expected, $result);
  5346. $this->Form->request->data['Project']['release'] = '1881-10-10';
  5347. $result = $this->Form->year('Project.release', [
  5348. 'minYear' => 1890,
  5349. 'maxYear' => 1900
  5350. ]);
  5351. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5352. $result = $matches[1];
  5353. $expected = range(1900, 1881);
  5354. $this->assertEquals($expected, $result);
  5355. }
  5356. /**
  5357. * testInputDate method
  5358. *
  5359. * Test various inputs with type date and different dateFormat values.
  5360. * Failing to provide a dateFormat key should not error.
  5361. * It should simply not pre-select any value then.
  5362. *
  5363. * @return void
  5364. */
  5365. public function testInputDate() {
  5366. $this->markTestIncomplete('Need to revisit once models work again.');
  5367. $this->Form->request->data = array(
  5368. 'User' => array(
  5369. 'month_year' => array('month' => date('m')),
  5370. 'just_year' => array('month' => date('m')),
  5371. 'just_month' => array('year' => date('Y')),
  5372. 'just_day' => array('month' => date('m')),
  5373. )
  5374. );
  5375. $this->Form->create('User');
  5376. $result = $this->Form->input('month_year',
  5377. array(
  5378. 'label' => false,
  5379. 'div' => false,
  5380. 'type' => 'date',
  5381. 'dateFormat' => 'MY',
  5382. 'minYear' => 2006,
  5383. 'maxYear' => 2008
  5384. )
  5385. );
  5386. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  5387. $this->assertNotContains('value="2008" selected="selected"', $result);
  5388. $result = $this->Form->input('just_year',
  5389. array(
  5390. 'type' => 'date',
  5391. 'label' => false,
  5392. 'dateFormat' => 'Y',
  5393. 'minYear' => date('Y'),
  5394. 'maxYear' => date('Y', strtotime('+20 years'))
  5395. )
  5396. );
  5397. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  5398. $result = $this->Form->input('just_month',
  5399. array(
  5400. 'type' => 'date',
  5401. 'label' => false,
  5402. 'dateFormat' => 'M',
  5403. 'empty' => false,
  5404. )
  5405. );
  5406. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  5407. $result = $this->Form->input('just_day',
  5408. array(
  5409. 'type' => 'date',
  5410. 'label' => false,
  5411. 'dateFormat' => 'D',
  5412. 'empty' => false,
  5413. )
  5414. );
  5415. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  5416. }
  5417. /**
  5418. * testInputDateMaxYear method
  5419. *
  5420. * Let's say we want to only allow users born from 2006 to 2008 to register
  5421. * This being the first singup page, we still don't have any data
  5422. *
  5423. * @return void
  5424. */
  5425. public function testInputDateMaxYear() {
  5426. $this->markTestIncomplete('Need to revisit once models work again.');
  5427. $this->Form->request->data = array();
  5428. $this->Form->create('User');
  5429. $result = $this->Form->input('birthday',
  5430. array(
  5431. 'label' => false,
  5432. 'div' => false,
  5433. 'type' => 'date',
  5434. 'dateFormat' => 'DMY',
  5435. 'minYear' => 2006,
  5436. 'maxYear' => 2008
  5437. )
  5438. );
  5439. $this->assertContains('value="2008" selected="selected"', $result);
  5440. }
  5441. /**
  5442. * testTextArea method
  5443. *
  5444. * @return void
  5445. */
  5446. public function testTextArea() {
  5447. $this->Form->request->data = array('field' => 'some test data');
  5448. $result = $this->Form->textarea('field');
  5449. $expected = array(
  5450. 'textarea' => array('name' => 'field'),
  5451. 'some test data',
  5452. '/textarea',
  5453. );
  5454. $this->assertTags($result, $expected);
  5455. $result = $this->Form->textarea('user.bio');
  5456. $expected = array(
  5457. 'textarea' => array('name' => 'user[bio]'),
  5458. '/textarea',
  5459. );
  5460. $this->assertTags($result, $expected);
  5461. $this->Form->request->data = array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  5462. $result = $this->Form->textarea('field');
  5463. $expected = array(
  5464. 'textarea' => array('name' => 'field'),
  5465. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5466. '/textarea',
  5467. );
  5468. $this->assertTags($result, $expected);
  5469. $this->Form->request->data = [
  5470. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  5471. ];
  5472. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5473. $expected = array(
  5474. 'textarea' => array('name' => 'Model[field]'),
  5475. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5476. '/textarea',
  5477. );
  5478. $this->assertTags($result, $expected);
  5479. $result = $this->Form->textarea('0.OtherModel.field');
  5480. $expected = array(
  5481. 'textarea' => array('name' => '0[OtherModel][field]'),
  5482. '/textarea'
  5483. );
  5484. $this->assertTags($result, $expected);
  5485. }
  5486. /**
  5487. * testTextAreaWithStupidCharacters method
  5488. *
  5489. * test text area with non-ascii characters
  5490. *
  5491. * @return void
  5492. */
  5493. public function testTextAreaWithStupidCharacters() {
  5494. $result = $this->Form->textarea('Post.content', [
  5495. 'value' => "GREAT®",
  5496. 'rows' => '15',
  5497. 'cols' => '75'
  5498. ]);
  5499. $expected = [
  5500. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5501. 'GREAT®',
  5502. '/textarea',
  5503. ];
  5504. $this->assertTags($result, $expected);
  5505. }
  5506. /**
  5507. * testHiddenField method
  5508. *
  5509. * @return void
  5510. */
  5511. public function testHiddenField() {
  5512. $this->article['errors'] = [
  5513. 'field' => true
  5514. ];
  5515. $this->Form->request->data['field'] = 'test';
  5516. $this->Form->create($this->article);
  5517. $result = $this->Form->hidden('field', array('id' => 'theID'));
  5518. $this->assertTags($result, array(
  5519. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test'))
  5520. );
  5521. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5522. $expected = [
  5523. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  5524. ];
  5525. $this->assertTags($result, $expected);
  5526. }
  5527. /**
  5528. * testFileUploadField method
  5529. *
  5530. * @return void
  5531. */
  5532. public function testFileUploadField() {
  5533. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5534. $result = $this->Form->file('Model.upload');
  5535. $this->assertTags($result, $expected);
  5536. $this->Form->request->data['Model']['upload'] = [
  5537. 'name' => '', 'type' => '', 'tmp_name' => '',
  5538. 'error' => 4, 'size' => 0
  5539. ];
  5540. $result = $this->Form->file('Model.upload');
  5541. $this->assertTags($result, $expected);
  5542. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  5543. $result = $this->Form->file('Model.upload');
  5544. $this->assertTags($result, $expected);
  5545. }
  5546. /**
  5547. * test File upload input on a model not used in create();
  5548. *
  5549. * @return void
  5550. */
  5551. public function testFileUploadOnOtherModel() {
  5552. $this->Form->create($this->article, array('type' => 'file'));
  5553. $result = $this->Form->file('ValidateProfile.city');
  5554. $expected = array(
  5555. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]')
  5556. );
  5557. $this->assertTags($result, $expected);
  5558. }
  5559. /**
  5560. * testButton method
  5561. *
  5562. * @return void
  5563. */
  5564. public function testButton() {
  5565. $result = $this->Form->button('Hi');
  5566. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5567. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5568. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5569. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5570. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5571. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5572. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5573. $result = $this->Form->button('No type', array('type' => false));
  5574. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5575. $result = $this->Form->button('Upload Text', array(
  5576. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  5577. 'escape' => false
  5578. ));
  5579. $this->assertNotRegExp('/\&039/', $result);
  5580. }
  5581. /**
  5582. * Test that button() makes unlocked fields by default.
  5583. *
  5584. * @return void
  5585. */
  5586. public function testButtonUnlockedByDefault() {
  5587. $this->Form->request->params['_csrfToken'] = 'secured';
  5588. $this->Form->button('Save', array('name' => 'save'));
  5589. $this->Form->button('Clear');
  5590. $result = $this->Form->unlockField();
  5591. $this->assertEquals(array('save'), $result);
  5592. }
  5593. /**
  5594. * testPostButton method
  5595. *
  5596. * @return void
  5597. */
  5598. public function testPostButton() {
  5599. $result = $this->Form->postButton('Hi', '/controller/action');
  5600. $this->assertTags($result, array(
  5601. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5602. 'div' => array('style' => 'display:none;'),
  5603. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5604. '/div',
  5605. 'button' => array('type' => 'submit'),
  5606. 'Hi',
  5607. '/button',
  5608. '/form'
  5609. ));
  5610. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5611. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  5612. }
  5613. /**
  5614. * Test using postButton with N dimensional data.
  5615. *
  5616. * @return void
  5617. */
  5618. public function testPostButtonNestedData() {
  5619. $data = array(
  5620. 'one' => array(
  5621. 'two' => array(
  5622. 3, 4, 5
  5623. )
  5624. )
  5625. );
  5626. $result = $this->Form->postButton('Send', '/', array('data' => $data));
  5627. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5628. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5629. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5630. }
  5631. /**
  5632. * Test that postButton adds _Token fields.
  5633. *
  5634. * @return void
  5635. */
  5636. public function testSecurePostButton() {
  5637. $this->Form->request->params['_csrfToken'] = 'testkey';
  5638. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5639. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5640. $expected = array(
  5641. 'form' => array(
  5642. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5643. ),
  5644. array('div' => array('style' => 'display:none;')),
  5645. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5646. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5647. '/div',
  5648. 'button' => array('type' => 'submit'),
  5649. 'Delete',
  5650. '/button',
  5651. array('div' => array('style' => 'display:none;')),
  5652. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5653. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5654. '/div',
  5655. '/form',
  5656. );
  5657. $this->assertTags($result, $expected);
  5658. }
  5659. /**
  5660. * testPostLink method
  5661. *
  5662. * @return void
  5663. */
  5664. public function testPostLink() {
  5665. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5666. $this->assertTags($result, array(
  5667. 'form' => array(
  5668. 'method' => 'post', 'action' => '/posts/delete/1',
  5669. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5670. ),
  5671. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5672. '/form',
  5673. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5674. 'Delete',
  5675. '/a'
  5676. ));
  5677. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5678. $this->assertTags($result, array(
  5679. 'form' => array(
  5680. 'method' => 'post', 'action' => '/posts/delete/1',
  5681. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5682. ),
  5683. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5684. '/form',
  5685. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5686. 'Delete',
  5687. '/a'
  5688. ));
  5689. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5690. $this->assertTags($result, array(
  5691. 'form' => array(
  5692. 'method' => 'post', 'action' => '/posts/delete/1',
  5693. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5694. ),
  5695. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5696. '/form',
  5697. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5698. 'Delete',
  5699. '/a'
  5700. ));
  5701. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  5702. $this->assertTags($result, array(
  5703. 'form' => array(
  5704. 'method' => 'post', 'action' => '/posts/delete/1',
  5705. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5706. ),
  5707. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5708. '/form',
  5709. '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;/'),
  5710. 'Delete',
  5711. '/a'
  5712. ));
  5713. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5714. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  5715. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  5716. $this->assertTags($result, array(
  5717. 'form' => array(
  5718. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  5719. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5720. ),
  5721. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5722. '/form',
  5723. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5724. 'Delete',
  5725. '/a'
  5726. ));
  5727. $result = $this->Form->postLink(
  5728. '',
  5729. array('controller' => 'items', 'action' => 'delete', 10),
  5730. array('class' => 'btn btn-danger', 'escape' => false),
  5731. 'Confirm thing'
  5732. );
  5733. $this->assertTags($result, array(
  5734. 'form' => array(
  5735. 'method' => 'post', 'action' => '/items/delete/10',
  5736. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5737. ),
  5738. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5739. '/form',
  5740. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5741. '/a'
  5742. ));
  5743. }
  5744. /**
  5745. * Test using postLink with N dimensional data.
  5746. *
  5747. * @return void
  5748. */
  5749. public function testPostLinkNestedData() {
  5750. $data = array(
  5751. 'one' => array(
  5752. 'two' => array(
  5753. 3, 4, 5
  5754. )
  5755. )
  5756. );
  5757. $result = $this->Form->postLink('Send', '/', array('data' => $data));
  5758. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5759. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5760. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5761. }
  5762. /**
  5763. * test creating postLinks after a GET form.
  5764. *
  5765. * @return void
  5766. */
  5767. public function testPostLinkAfterGetForm() {
  5768. $this->Form->request->params['_csrfToken'] = 'testkey';
  5769. $this->Form->request->params['_Token'] = 'val';
  5770. $this->Form->create($this->article, array('type' => 'get'));
  5771. $this->Form->end();
  5772. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5773. $this->assertTags($result, array(
  5774. 'form' => array(
  5775. 'method' => 'post', 'action' => '/posts/delete/1',
  5776. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5777. ),
  5778. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5779. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5780. 'div' => array('style' => 'display:none;'),
  5781. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5782. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5783. '/div',
  5784. '/form',
  5785. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5786. 'Delete',
  5787. '/a'
  5788. ));
  5789. }
  5790. /**
  5791. * Test that postLink adds form tags to view block
  5792. *
  5793. * @return void
  5794. */
  5795. public function testPostLinkFormBuffer() {
  5796. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
  5797. $this->assertTags($result, array(
  5798. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5799. 'Delete',
  5800. '/a'
  5801. ));
  5802. $result = $this->View->fetch('postLink');
  5803. $this->assertTags($result, array(
  5804. 'form' => array(
  5805. 'method' => 'post', 'action' => '/posts/delete/1',
  5806. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5807. ),
  5808. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5809. '/form'
  5810. ));
  5811. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  5812. array('inline' => false, 'method' => 'DELETE')
  5813. );
  5814. $this->assertTags($result, array(
  5815. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5816. 'Delete',
  5817. '/a'
  5818. ));
  5819. $result = $this->View->fetch('postLink');
  5820. $this->assertTags($result, array(
  5821. 'form' => array(
  5822. 'method' => 'post', 'action' => '/posts/delete/1',
  5823. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5824. ),
  5825. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5826. '/form',
  5827. array(
  5828. 'form' => array(
  5829. 'method' => 'post', 'action' => '/posts/delete/2',
  5830. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5831. ),
  5832. ),
  5833. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  5834. '/form'
  5835. ));
  5836. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  5837. $this->assertTags($result, array(
  5838. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5839. 'Delete',
  5840. '/a'
  5841. ));
  5842. $result = $this->View->fetch('foobar');
  5843. $this->assertTags($result, array(
  5844. 'form' => array(
  5845. 'method' => 'post', 'action' => '/posts/delete/1',
  5846. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5847. ),
  5848. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5849. '/form'
  5850. ));
  5851. }
  5852. /**
  5853. * testSubmitButton method
  5854. *
  5855. * @return void
  5856. */
  5857. public function testSubmitButton() {
  5858. $result = $this->Form->submit('');
  5859. $expected = array(
  5860. 'div' => array('class' => 'submit'),
  5861. 'input' => array('type' => 'submit', 'value' => ''),
  5862. '/div'
  5863. );
  5864. $this->assertTags($result, $expected);
  5865. $result = $this->Form->submit('Test Submit');
  5866. $expected = array(
  5867. 'div' => array('class' => 'submit'),
  5868. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5869. '/div'
  5870. );
  5871. $this->assertTags($result, $expected);
  5872. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5873. $expected = array(
  5874. 'span' => array('class' => 'submit'),
  5875. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5876. '/span'
  5877. );
  5878. $this->assertTags($result, $expected);
  5879. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5880. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5881. $this->assertTags($result, $expected);
  5882. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5883. $expected = array(
  5884. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5885. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5886. '/div'
  5887. );
  5888. $this->assertTags($result, $expected);
  5889. $result = $this->Form->submit('Next >');
  5890. $expected = array(
  5891. 'div' => array('class' => 'submit'),
  5892. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5893. '/div'
  5894. );
  5895. $this->assertTags($result, $expected);
  5896. $result = $this->Form->submit('Next >', array('escape' => false));
  5897. $expected = array(
  5898. 'div' => array('class' => 'submit'),
  5899. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5900. '/div'
  5901. );
  5902. $this->assertTags($result, $expected);
  5903. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5904. $expected = array(
  5905. 'div' => array('class' => 'submit'),
  5906. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5907. '/div'
  5908. );
  5909. $this->assertTags($result, $expected);
  5910. $before = '--before--';
  5911. $after = '--after--';
  5912. $result = $this->Form->submit('Test', array('before' => $before));
  5913. $expected = array(
  5914. 'div' => array('class' => 'submit'),
  5915. '--before--',
  5916. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5917. '/div'
  5918. );
  5919. $this->assertTags($result, $expected);
  5920. $result = $this->Form->submit('Test', array('after' => $after));
  5921. $expected = array(
  5922. 'div' => array('class' => 'submit'),
  5923. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5924. '--after--',
  5925. '/div'
  5926. );
  5927. $this->assertTags($result, $expected);
  5928. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5929. $expected = array(
  5930. 'div' => array('class' => 'submit'),
  5931. '--before--',
  5932. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5933. '--after--',
  5934. '/div'
  5935. );
  5936. $this->assertTags($result, $expected);
  5937. }
  5938. /**
  5939. * test image submit types.
  5940. *
  5941. * @return void
  5942. */
  5943. public function testSubmitImage() {
  5944. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5945. $expected = array(
  5946. 'div' => array('class' => 'submit'),
  5947. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5948. '/div'
  5949. );
  5950. $this->assertTags($result, $expected);
  5951. $result = $this->Form->submit('/relative/cake.power.gif');
  5952. $expected = array(
  5953. 'div' => array('class' => 'submit'),
  5954. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5955. '/div'
  5956. );
  5957. $this->assertTags($result, $expected);
  5958. $result = $this->Form->submit('cake.power.gif');
  5959. $expected = array(
  5960. 'div' => array('class' => 'submit'),
  5961. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5962. '/div'
  5963. );
  5964. $this->assertTags($result, $expected);
  5965. $result = $this->Form->submit('Not.an.image');
  5966. $expected = array(
  5967. 'div' => array('class' => 'submit'),
  5968. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5969. '/div'
  5970. );
  5971. $this->assertTags($result, $expected);
  5972. $after = '--after--';
  5973. $before = '--before--';
  5974. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5975. $expected = array(
  5976. 'div' => array('class' => 'submit'),
  5977. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5978. '--after--',
  5979. '/div'
  5980. );
  5981. $this->assertTags($result, $expected);
  5982. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5983. $expected = array(
  5984. 'div' => array('class' => 'submit'),
  5985. '--before--',
  5986. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5987. '/div'
  5988. );
  5989. $this->assertTags($result, $expected);
  5990. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5991. $expected = array(
  5992. 'div' => array('class' => 'submit'),
  5993. '--before--',
  5994. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5995. '--after--',
  5996. '/div'
  5997. );
  5998. $this->assertTags($result, $expected);
  5999. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6000. $expected = array(
  6001. 'div' => array('class' => 'submit'),
  6002. '--before--',
  6003. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6004. '--after--',
  6005. '/div'
  6006. );
  6007. $this->assertTags($result, $expected);
  6008. }
  6009. /**
  6010. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6011. * be submitted at a time.
  6012. *
  6013. * @return void
  6014. */
  6015. public function testSubmitUnlockedByDefault() {
  6016. $this->Form->request->params['_Token'] = 'secured';
  6017. $this->Form->submit('Go go');
  6018. $this->Form->submit('Save', array('name' => 'save'));
  6019. $result = $this->Form->unlockField();
  6020. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6021. }
  6022. /**
  6023. * Test submit image with timestamps.
  6024. *
  6025. * @return void
  6026. */
  6027. public function testSubmitImageTimestamp() {
  6028. Configure::write('Asset.timestamp', 'force');
  6029. $result = $this->Form->submit('cake.power.gif');
  6030. $expected = array(
  6031. 'div' => array('class' => 'submit'),
  6032. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6033. '/div'
  6034. );
  6035. $this->assertTags($result, $expected);
  6036. }
  6037. /**
  6038. * test that datetime() works with GET style forms.
  6039. *
  6040. * @return void
  6041. */
  6042. public function testDateTimeWithGetForms() {
  6043. $this->markTestIncomplete('Need to revisit once models work again.');
  6044. extract($this->dateRegex);
  6045. $this->Form->create('Contact', array('type' => 'get'));
  6046. $result = $this->Form->datetime('created');
  6047. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6048. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6049. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6050. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6051. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6052. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6053. }
  6054. /**
  6055. * testEditFormWithData method
  6056. *
  6057. * test auto populating form elements from submitted data.
  6058. *
  6059. * @return void
  6060. */
  6061. public function testEditFormWithData() {
  6062. $this->markTestIncomplete('Need to revisit once models work again.');
  6063. $this->Form->request->data = array('Person' => array(
  6064. 'id' => 1,
  6065. 'first_name' => 'Nate',
  6066. 'last_name' => 'Abele',
  6067. 'email' => 'nate@example.com'
  6068. ));
  6069. $this->Form->request->addParams(array(
  6070. 'models' => array('Person'),
  6071. 'controller' => 'people',
  6072. 'action' => 'add'
  6073. ));
  6074. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6075. $this->Form->create();
  6076. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6077. $expected = array(
  6078. 'input' => array('type' => 'hidden', 'name' => 'People[People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6079. 'select' => array(
  6080. 'name' => 'People[People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6081. ),
  6082. array('option' => array('value' => 1)), 'Nate', '/option',
  6083. array('option' => array('value' => 2)), 'Garrett', '/option',
  6084. array('option' => array('value' => 3)), 'Larry', '/option',
  6085. '/select'
  6086. );
  6087. $this->assertTags($result, $expected);
  6088. }
  6089. /**
  6090. * Test that required fields are created for various types of validation.
  6091. *
  6092. * @return void
  6093. */
  6094. public function testFormInputRequiredDetection() {
  6095. $this->markTestIncomplete('Need to revisit once models work again.');
  6096. $this->Form->create('Contact');
  6097. $result = $this->Form->input('Contact.non_existing');
  6098. $expected = array(
  6099. 'div' => array('class' => 'input text'),
  6100. 'label' => array('for' => 'ContactNonExisting'),
  6101. 'Non Existing',
  6102. '/label',
  6103. 'input' => array(
  6104. 'type' => 'text', 'name' => 'Contact[non_existing]',
  6105. 'id' => 'ContactNonExisting'
  6106. ),
  6107. '/div'
  6108. );
  6109. $this->assertTags($result, $expected);
  6110. $result = $this->Form->input('Contact.imrequired');
  6111. $expected = array(
  6112. 'div' => array('class' => 'input text required'),
  6113. 'label' => array('for' => 'ContactImrequired'),
  6114. 'Imrequired',
  6115. '/label',
  6116. 'input' => array(
  6117. 'type' => 'text', 'name' => 'Contact[imrequired]',
  6118. 'id' => 'ContactImrequired',
  6119. 'required' => 'required'
  6120. ),
  6121. '/div'
  6122. );
  6123. $this->assertTags($result, $expected);
  6124. $result = $this->Form->input('Contact.imalsorequired');
  6125. $expected = array(
  6126. 'div' => array('class' => 'input text required'),
  6127. 'label' => array('for' => 'ContactImalsorequired'),
  6128. 'Imalsorequired',
  6129. '/label',
  6130. 'input' => array(
  6131. 'type' => 'text', 'name' => 'Contact[imalsorequired]',
  6132. 'id' => 'ContactImalsorequired',
  6133. 'required' => 'required'
  6134. ),
  6135. '/div'
  6136. );
  6137. $this->assertTags($result, $expected);
  6138. $result = $this->Form->input('Contact.imrequiredtoo');
  6139. $expected = array(
  6140. 'div' => array('class' => 'input text required'),
  6141. 'label' => array('for' => 'ContactImrequiredtoo'),
  6142. 'Imrequiredtoo',
  6143. '/label',
  6144. 'input' => array(
  6145. 'type' => 'text', 'name' => 'Contact[imrequiredtoo]',
  6146. 'id' => 'ContactImrequiredtoo',
  6147. 'required' => 'required'
  6148. ),
  6149. '/div'
  6150. );
  6151. $this->assertTags($result, $expected);
  6152. $result = $this->Form->input('Contact.required_one');
  6153. $expected = array(
  6154. 'div' => array('class' => 'input text required'),
  6155. 'label' => array('for' => 'ContactRequiredOne'),
  6156. 'Required One',
  6157. '/label',
  6158. 'input' => array(
  6159. 'type' => 'text', 'name' => 'Contact[required_one]',
  6160. 'id' => 'ContactRequiredOne',
  6161. 'required' => 'required'
  6162. ),
  6163. '/div'
  6164. );
  6165. $this->assertTags($result, $expected);
  6166. $result = $this->Form->input('Contact.string_required');
  6167. $expected = array(
  6168. 'div' => array('class' => 'input text required'),
  6169. 'label' => array('for' => 'ContactStringRequired'),
  6170. 'String Required',
  6171. '/label',
  6172. 'input' => array(
  6173. 'type' => 'text', 'name' => 'Contact[string_required]',
  6174. 'id' => 'ContactStringRequired',
  6175. 'required' => 'required'
  6176. ),
  6177. '/div'
  6178. );
  6179. $this->assertTags($result, $expected);
  6180. $result = $this->Form->input('Contact.imnotrequired');
  6181. $expected = array(
  6182. 'div' => array('class' => 'input text'),
  6183. 'label' => array('for' => 'ContactImnotrequired'),
  6184. 'Imnotrequired',
  6185. '/label',
  6186. 'input' => array(
  6187. 'type' => 'text', 'name' => 'Contact[imnotrequired]',
  6188. 'id' => 'ContactImnotrequired'
  6189. ),
  6190. '/div'
  6191. );
  6192. $this->assertTags($result, $expected);
  6193. $result = $this->Form->input('Contact.imalsonotrequired');
  6194. $expected = array(
  6195. 'div' => array('class' => 'input text'),
  6196. 'label' => array('for' => 'ContactImalsonotrequired'),
  6197. 'Imalsonotrequired',
  6198. '/label',
  6199. 'input' => array(
  6200. 'type' => 'text', 'name' => 'Contact[imalsonotrequired]',
  6201. 'id' => 'ContactImalsonotrequired'
  6202. ),
  6203. '/div'
  6204. );
  6205. $this->assertTags($result, $expected);
  6206. $result = $this->Form->input('Contact.imalsonotrequired2');
  6207. $expected = array(
  6208. 'div' => array('class' => 'input text'),
  6209. 'label' => array('for' => 'ContactImalsonotrequired2'),
  6210. 'Imalsonotrequired2',
  6211. '/label',
  6212. 'input' => array(
  6213. 'type' => 'text', 'name' => 'Contact[imalsonotrequired2]',
  6214. 'id' => 'ContactImalsonotrequired2'
  6215. ),
  6216. '/div'
  6217. );
  6218. $this->assertTags($result, $expected);
  6219. $result = $this->Form->input('Contact.imnotrequiredeither');
  6220. $expected = array(
  6221. 'div' => array('class' => 'input text'),
  6222. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6223. 'Imnotrequiredeither',
  6224. '/label',
  6225. 'input' => array(
  6226. 'type' => 'text', 'name' => 'Contact[imnotrequiredeither]',
  6227. 'id' => 'ContactImnotrequiredeither'
  6228. ),
  6229. '/div'
  6230. );
  6231. $this->assertTags($result, $expected);
  6232. $result = $this->Form->input('Contact.iamrequiredalways');
  6233. $expected = array(
  6234. 'div' => array('class' => 'input text required'),
  6235. 'label' => array('for' => 'ContactIamrequiredalways'),
  6236. 'Iamrequiredalways',
  6237. '/label',
  6238. 'input' => array(
  6239. 'type' => 'text', 'name' => 'Contact[iamrequiredalways]',
  6240. 'id' => 'ContactIamrequiredalways',
  6241. 'required' => 'required'
  6242. ),
  6243. '/div'
  6244. );
  6245. $this->assertTags($result, $expected);
  6246. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  6247. $expected = array(
  6248. 'div' => array('class' => 'input checkbox required'),
  6249. array('input' => array(
  6250. 'type' => 'hidden',
  6251. 'name' => 'Contact[boolean_field]',
  6252. 'id' => 'ContactBooleanField_',
  6253. 'value' => '0'
  6254. )),
  6255. array('input' => array(
  6256. 'type' => 'checkbox',
  6257. 'name' => 'Contact[boolean_field]',
  6258. 'value' => '1',
  6259. 'id' => 'ContactBooleanField'
  6260. )),
  6261. 'label' => array('for' => 'ContactBooleanField'),
  6262. 'Boolean Field',
  6263. '/label',
  6264. '/div'
  6265. );
  6266. $this->assertTags($result, $expected);
  6267. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  6268. $expected = array(
  6269. 'div' => array('class' => 'input checkbox required'),
  6270. array('input' => array(
  6271. 'type' => 'hidden',
  6272. 'name' => 'Contact[boolean_field]',
  6273. 'id' => 'ContactBooleanField_',
  6274. 'value' => '0'
  6275. )),
  6276. array('input' => array(
  6277. 'type' => 'checkbox',
  6278. 'name' => 'Contact[boolean_field]',
  6279. 'value' => '1',
  6280. 'id' => 'ContactBooleanField',
  6281. 'required' => 'required'
  6282. )),
  6283. 'label' => array('for' => 'ContactBooleanField'),
  6284. 'Boolean Field',
  6285. '/label',
  6286. '/div'
  6287. );
  6288. $this->assertTags($result, $expected);
  6289. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  6290. $expected = array(
  6291. 'div' => array('class' => 'input file required'),
  6292. 'label' => array('for' => 'ContactIamrequiredalways'),
  6293. 'Iamrequiredalways',
  6294. '/label',
  6295. 'input' => array(
  6296. 'type' => 'file',
  6297. 'name' => 'data[Contact][iamrequiredalways]',
  6298. 'id' => 'ContactIamrequiredalways',
  6299. 'required' => 'required'
  6300. ),
  6301. '/div'
  6302. );
  6303. $this->assertTags($result, $expected);
  6304. }
  6305. /**
  6306. * Test that required fields are created when only using ModelValidator::add().
  6307. *
  6308. * @return void
  6309. */
  6310. public function testFormInputRequiredDetectionModelValidator() {
  6311. $this->markTestIncomplete('Need to revisit once models work again.');
  6312. $this->Form->create('ContactTag');
  6313. $result = $this->Form->input('ContactTag.iwillberequired');
  6314. $expected = array(
  6315. 'div' => array('class' => 'input text required'),
  6316. 'label' => array('for' => 'ContactTagIwillberequired'),
  6317. 'Iwillberequired',
  6318. '/label',
  6319. 'input' => array(
  6320. 'name' => 'data[ContactTag][iwillberequired]',
  6321. 'type' => 'text',
  6322. 'id' => 'ContactTagIwillberequired',
  6323. 'required' => 'required'
  6324. ),
  6325. '/div'
  6326. );
  6327. $this->assertTags($result, $expected);
  6328. }
  6329. /**
  6330. * testFormMagicInput method
  6331. *
  6332. * @return void
  6333. */
  6334. public function testFormMagicInput() {
  6335. $this->markTestIncomplete('Need to revisit once models work again.');
  6336. $encoding = strtolower(Configure::read('App.encoding'));
  6337. $result = $this->Form->create('Contact');
  6338. $expected = array(
  6339. 'form' => array(
  6340. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6341. 'accept-charset' => $encoding
  6342. ),
  6343. 'div' => array('style' => 'display:none;'),
  6344. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6345. '/div'
  6346. );
  6347. $this->assertTags($result, $expected);
  6348. $result = $this->Form->input('name');
  6349. $expected = array(
  6350. 'div' => array('class' => 'input text'),
  6351. 'label' => array('for' => 'ContactName'),
  6352. 'Name',
  6353. '/label',
  6354. 'input' => array(
  6355. 'type' => 'text', 'name' => 'Contact[name]',
  6356. 'id' => 'ContactName', 'maxlength' => '255'
  6357. ),
  6358. '/div'
  6359. );
  6360. $this->assertTags($result, $expected);
  6361. $result = $this->Form->input('non_existing_field_in_contact_model');
  6362. $expected = array(
  6363. 'div' => array('class' => 'input text'),
  6364. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6365. 'Non Existing Field In Contact Model',
  6366. '/label',
  6367. 'input' => array(
  6368. 'type' => 'text', 'name' => 'Contact[non_existing_field_in_contact_model]',
  6369. 'id' => 'ContactNonExistingFieldInContactModel'
  6370. ),
  6371. '/div'
  6372. );
  6373. $this->assertTags($result, $expected);
  6374. $result = $this->Form->input('Address.street');
  6375. $expected = array(
  6376. 'div' => array('class' => 'input text'),
  6377. 'label' => array('for' => 'AddressStreet'),
  6378. 'Street',
  6379. '/label',
  6380. 'input' => array(
  6381. 'type' => 'text', 'name' => 'Address[street]',
  6382. 'id' => 'AddressStreet'
  6383. ),
  6384. '/div'
  6385. );
  6386. $this->assertTags($result, $expected);
  6387. $result = $this->Form->input('Address.non_existing_field_in_model');
  6388. $expected = array(
  6389. 'div' => array('class' => 'input text'),
  6390. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6391. 'Non Existing Field In Model',
  6392. '/label',
  6393. 'input' => array(
  6394. 'type' => 'text', 'name' => 'Address[non_existing_field_in_model]',
  6395. 'id' => 'AddressNonExistingFieldInModel'
  6396. ),
  6397. '/div'
  6398. );
  6399. $this->assertTags($result, $expected);
  6400. $result = $this->Form->input('name', array('div' => false));
  6401. $expected = array(
  6402. 'label' => array('for' => 'ContactName'),
  6403. 'Name',
  6404. '/label',
  6405. 'input' => array(
  6406. 'type' => 'text', 'name' => 'Contact[name]',
  6407. 'id' => 'ContactName', 'maxlength' => '255'
  6408. )
  6409. );
  6410. $this->assertTags($result, $expected);
  6411. extract($this->dateRegex);
  6412. $now = strtotime('now');
  6413. $result = $this->Form->input('Contact.published', array('div' => false));
  6414. $expected = array(
  6415. 'label' => array('for' => 'ContactPublishedMonth'),
  6416. 'Published',
  6417. '/label',
  6418. array('select' => array(
  6419. 'name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth'
  6420. )),
  6421. $monthsRegex,
  6422. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6423. date('F', $now),
  6424. '/option',
  6425. '*/select',
  6426. '-',
  6427. array('select' => array(
  6428. 'name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay'
  6429. )),
  6430. $daysRegex,
  6431. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6432. date('j', $now),
  6433. '/option',
  6434. '*/select',
  6435. '-',
  6436. array('select' => array(
  6437. 'name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear'
  6438. )),
  6439. $yearsRegex,
  6440. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6441. date('Y', $now),
  6442. '*/select'
  6443. );
  6444. $this->assertTags($result, $expected);
  6445. $result = $this->Form->input('Contact.updated', array('div' => false));
  6446. $expected = array(
  6447. 'label' => array('for' => 'ContactUpdatedMonth'),
  6448. 'Updated',
  6449. '/label',
  6450. array('select' => array(
  6451. 'name' => 'Contact[updated][month]', 'id' => 'ContactUpdatedMonth'
  6452. )),
  6453. $monthsRegex,
  6454. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6455. date('F', $now),
  6456. '/option',
  6457. '*/select',
  6458. '-',
  6459. array('select' => array(
  6460. 'name' => 'Contact[updated][day]', 'id' => 'ContactUpdatedDay'
  6461. )),
  6462. $daysRegex,
  6463. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6464. date('j', $now),
  6465. '/option',
  6466. '*/select',
  6467. '-',
  6468. array('select' => array(
  6469. 'name' => 'Contact[updated][year]', 'id' => 'ContactUpdatedYear'
  6470. )),
  6471. $yearsRegex,
  6472. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6473. date('Y', $now),
  6474. '*/select'
  6475. );
  6476. $this->assertTags($result, $expected);
  6477. $result = $this->Form->input('UserForm.stuff');
  6478. $expected = array(
  6479. 'div' => array('class' => 'input text'),
  6480. 'label' => array('for' => 'UserFormStuff'),
  6481. 'Stuff',
  6482. '/label',
  6483. 'input' => array(
  6484. 'type' => 'text', 'name' => 'UserForm[stuff]',
  6485. 'id' => 'UserFormStuff', 'maxlength' => 10
  6486. ),
  6487. '/div'
  6488. );
  6489. $this->assertTags($result, $expected);
  6490. }
  6491. /**
  6492. * testForMagicInputNonExistingNorValidated method
  6493. *
  6494. * @return void
  6495. */
  6496. public function testForMagicInputNonExistingNorValidated() {
  6497. $this->markTestIncomplete('Need to revisit once models work again.');
  6498. $encoding = strtolower(Configure::read('App.encoding'));
  6499. $result = $this->Form->create('Contact');
  6500. $expected = array(
  6501. 'form' => array(
  6502. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6503. 'accept-charset' => $encoding
  6504. ),
  6505. 'div' => array('style' => 'display:none;'),
  6506. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6507. '/div'
  6508. );
  6509. $this->assertTags($result, $expected);
  6510. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6511. $expected = array(
  6512. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6513. 'Non Existing Nor Validated',
  6514. '/label',
  6515. 'input' => array(
  6516. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  6517. 'id' => 'ContactNonExistingNorValidated'
  6518. )
  6519. );
  6520. $this->assertTags($result, $expected);
  6521. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6522. 'div' => false, 'value' => 'my value'
  6523. ));
  6524. $expected = array(
  6525. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6526. 'Non Existing Nor Validated',
  6527. '/label',
  6528. 'input' => array(
  6529. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  6530. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6531. )
  6532. );
  6533. $this->assertTags($result, $expected);
  6534. $this->Form->request->data = array(
  6535. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6536. ));
  6537. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6538. $expected = array(
  6539. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6540. 'Non Existing Nor Validated',
  6541. '/label',
  6542. 'input' => array(
  6543. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  6544. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6545. )
  6546. );
  6547. $this->assertTags($result, $expected);
  6548. }
  6549. /**
  6550. * testFormMagicInputLabel method
  6551. *
  6552. * @return void
  6553. */
  6554. public function testFormMagicInputLabel() {
  6555. $this->markTestIncomplete('Need to revisit once models work again.');
  6556. $encoding = strtolower(Configure::read('App.encoding'));
  6557. $result = $this->Form->create('Contact');
  6558. $expected = array(
  6559. 'form' => array(
  6560. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6561. 'accept-charset' => $encoding
  6562. ),
  6563. 'div' => array('style' => 'display:none;'),
  6564. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6565. '/div'
  6566. );
  6567. $this->assertTags($result, $expected);
  6568. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6569. $this->assertTags($result, array('input' => array(
  6570. 'name' => 'Contact[name]', 'type' => 'text',
  6571. 'id' => 'ContactName', 'maxlength' => '255')
  6572. ));
  6573. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6574. $expected = array(
  6575. 'label' => array('for' => 'ContactName'),
  6576. 'My label',
  6577. '/label',
  6578. 'input' => array(
  6579. 'type' => 'text', 'name' => 'Contact[name]',
  6580. 'id' => 'ContactName', 'maxlength' => '255'
  6581. )
  6582. );
  6583. $this->assertTags($result, $expected);
  6584. $result = $this->Form->input('Contact.name', array(
  6585. 'div' => false, 'label' => array('class' => 'mandatory')
  6586. ));
  6587. $expected = array(
  6588. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6589. 'Name',
  6590. '/label',
  6591. 'input' => array(
  6592. 'type' => 'text', 'name' => 'Contact[name]',
  6593. 'id' => 'ContactName', 'maxlength' => '255'
  6594. )
  6595. );
  6596. $this->assertTags($result, $expected);
  6597. $result = $this->Form->input('Contact.name', array(
  6598. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6599. ));
  6600. $expected = array(
  6601. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6602. 'My label',
  6603. '/label',
  6604. 'input' => array(
  6605. 'type' => 'text', 'name' => 'Contact[name]',
  6606. 'id' => 'ContactName', 'maxlength' => '255'
  6607. )
  6608. );
  6609. $this->assertTags($result, $expected);
  6610. $result = $this->Form->input('Contact.name', array(
  6611. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6612. ));
  6613. $expected = array(
  6614. 'label' => array('for' => 'my_id'),
  6615. 'Name',
  6616. '/label',
  6617. 'input' => array(
  6618. 'type' => 'text', 'name' => 'Contact[name]',
  6619. 'id' => 'my_id', 'maxlength' => '255'
  6620. )
  6621. );
  6622. $this->assertTags($result, $expected);
  6623. $result = $this->Form->input('1.id');
  6624. $this->assertTags($result, array('input' => array(
  6625. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  6626. 'id' => 'Contact1Id'
  6627. )));
  6628. $result = $this->Form->input("1.name");
  6629. $expected = array(
  6630. 'div' => array('class' => 'input text'),
  6631. 'label' => array('for' => 'Contact1Name'),
  6632. 'Name',
  6633. '/label',
  6634. 'input' => array(
  6635. 'type' => 'text', 'name' => 'Contact[1][name]',
  6636. 'id' => 'Contact1Name', 'maxlength' => '255'
  6637. ),
  6638. '/div'
  6639. );
  6640. $this->assertTags($result, $expected);
  6641. $result = $this->Form->input('Contact.1.id');
  6642. $this->assertTags($result, array(
  6643. 'input' => array(
  6644. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  6645. 'id' => 'Contact1Id'
  6646. )
  6647. ));
  6648. $result = $this->Form->input("Model.1.name");
  6649. $expected = array(
  6650. 'div' => array('class' => 'input text'),
  6651. 'label' => array('for' => 'Model1Name'),
  6652. 'Name',
  6653. '/label',
  6654. 'input' => array(
  6655. 'type' => 'text', 'name' => 'Model[1][name]',
  6656. 'id' => 'Model1Name'
  6657. ),
  6658. '/div'
  6659. );
  6660. $this->assertTags($result, $expected);
  6661. }
  6662. /**
  6663. * testFormEnd method
  6664. *
  6665. * @return void
  6666. */
  6667. public function testFormEnd() {
  6668. $this->assertEquals('</form>', $this->Form->end());
  6669. }
  6670. /**
  6671. * testMultipleFormWithIdFields method
  6672. *
  6673. * @return void
  6674. */
  6675. public function testMultipleFormWithIdFields() {
  6676. $this->markTestIncomplete('Need to revisit once models work again.');
  6677. $this->Form->create('UserForm');
  6678. $result = $this->Form->input('id');
  6679. $this->assertTags($result, array('input' => array(
  6680. 'type' => 'hidden', 'name' => 'UserForm[id]', 'id' => 'UserFormId'
  6681. )));
  6682. $result = $this->Form->input('ValidateItem.id');
  6683. $this->assertTags($result, array('input' => array(
  6684. 'type' => 'hidden', 'name' => 'ValidateItem[id]',
  6685. 'id' => 'ValidateItemId'
  6686. )));
  6687. $result = $this->Form->input('ValidateUser.id');
  6688. $this->assertTags($result, array('input' => array(
  6689. 'type' => 'hidden', 'name' => 'ValidateUser[id]',
  6690. 'id' => 'ValidateUserId'
  6691. )));
  6692. }
  6693. /**
  6694. * testDbLessModel method
  6695. *
  6696. * @return void
  6697. */
  6698. public function testDbLessModel() {
  6699. $this->markTestIncomplete('Need to revisit once models work again.');
  6700. $this->Form->create('TestMail');
  6701. $result = $this->Form->input('name');
  6702. $expected = array(
  6703. 'div' => array('class' => 'input text'),
  6704. 'label' => array('for' => 'TestMailName'),
  6705. 'Name',
  6706. '/label',
  6707. 'input' => array(
  6708. 'name' => 'TestMail[name]', 'type' => 'text',
  6709. 'id' => 'TestMailName'
  6710. ),
  6711. '/div'
  6712. );
  6713. $this->assertTags($result, $expected);
  6714. $this->Form->create('TestMail');
  6715. $result = $this->Form->input('name');
  6716. $expected = array(
  6717. 'div' => array('class' => 'input text'),
  6718. 'label' => array('for' => 'TestMailName'),
  6719. 'Name',
  6720. '/label',
  6721. 'input' => array(
  6722. 'name' => 'TestMail[name]', 'type' => 'text',
  6723. 'id' => 'TestMailName'
  6724. ),
  6725. '/div'
  6726. );
  6727. $this->assertTags($result, $expected);
  6728. }
  6729. /**
  6730. * testBrokenness method
  6731. *
  6732. * @return void
  6733. */
  6734. public function testBrokenness() {
  6735. $this->markTestIncomplete('Need to revisit once models work again.');
  6736. /*
  6737. * #4 This test has two parents and four children. By default (as of r7117) both
  6738. * parents are show but the first parent is missing a child. This is the inconsistency
  6739. * in the default behaviour - one parent has all children, the other does not - dependent
  6740. * on the data values.
  6741. */
  6742. $result = $this->Form->select('Model.field', array(
  6743. 'Fred' => array(
  6744. 'freds_son_1' => 'Fred',
  6745. 'freds_son_2' => 'Freddie'
  6746. ),
  6747. 'Bert' => array(
  6748. 'berts_son_1' => 'Albert',
  6749. 'berts_son_2' => 'Bertie')
  6750. ),
  6751. array('showParents' => true, 'empty' => false)
  6752. );
  6753. $expected = array(
  6754. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  6755. array('optgroup' => array('label' => 'Fred')),
  6756. array('option' => array('value' => 'freds_son_1')),
  6757. 'Fred',
  6758. '/option',
  6759. array('option' => array('value' => 'freds_son_2')),
  6760. 'Freddie',
  6761. '/option',
  6762. '/optgroup',
  6763. array('optgroup' => array('label' => 'Bert')),
  6764. array('option' => array('value' => 'berts_son_1')),
  6765. 'Albert',
  6766. '/option',
  6767. array('option' => array('value' => 'berts_son_2')),
  6768. 'Bertie',
  6769. '/option',
  6770. '/optgroup',
  6771. '/select'
  6772. );
  6773. $this->assertTags($result, $expected);
  6774. /*
  6775. * #2 This is structurally identical to the test above (#1) - only the parent name has
  6776. * changed, so we should expect the same select list data, just with a different name
  6777. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  6778. * This is where data corruption can occur, because when a select value is missing from
  6779. * a list a form will substitute the first value in the list - without the user knowing.
  6780. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  6781. * affect the availability of 3 => 'Three' as a valid option.
  6782. */
  6783. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  6784. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  6785. ));
  6786. $result = $this->Form->select(
  6787. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  6788. );
  6789. $expected = array(
  6790. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  6791. array('option' => array('value' => 1)),
  6792. 'One',
  6793. '/option',
  6794. array('option' => array('value' => 2)),
  6795. 'Two',
  6796. '/option',
  6797. array('optgroup' => array('label' => 'Three')),
  6798. array('option' => array('value' => 3)),
  6799. 'Three',
  6800. '/option',
  6801. array('option' => array('value' => 4)),
  6802. 'Four',
  6803. '/option',
  6804. array('option' => array('value' => 5)),
  6805. 'Five',
  6806. '/option',
  6807. '/optgroup',
  6808. '/select'
  6809. );
  6810. $this->assertTags($result, $expected);
  6811. }
  6812. /**
  6813. * Test the generation of fields for a multi record form.
  6814. *
  6815. * @return void
  6816. */
  6817. public function testMultiRecordForm() {
  6818. $this->markTestIncomplete('Need to revisit once models work again.');
  6819. $this->Form->create('ValidateProfile');
  6820. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  6821. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  6822. $expected = array(
  6823. 'div' => array('class' => 'input textarea'),
  6824. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  6825. 'Name',
  6826. '/label',
  6827. 'textarea' => array(
  6828. 'id' => 'ValidateProfile1ValidateItem2Name',
  6829. 'name' => 'ValidateProfile[1][ValidateItem][2][name]',
  6830. 'cols' => 30,
  6831. 'rows' => 6
  6832. ),
  6833. 'Value',
  6834. '/textarea',
  6835. '/div'
  6836. );
  6837. $this->assertTags($result, $expected);
  6838. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  6839. $expected = array(
  6840. 'div' => array('class' => 'input date'),
  6841. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  6842. 'Created',
  6843. '/label',
  6844. array('select' => array(
  6845. 'name' => 'ValidateProfile[1][ValidateItem][2][created][month]',
  6846. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  6847. )
  6848. ),
  6849. array('option' => array('value' => '')), '/option',
  6850. $this->dateRegex['monthsRegex'],
  6851. '/select', '-',
  6852. array('select' => array(
  6853. 'name' => 'ValidateProfile[1][ValidateItem][2][created][day]',
  6854. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  6855. )
  6856. ),
  6857. array('option' => array('value' => '')), '/option',
  6858. $this->dateRegex['daysRegex'],
  6859. '/select', '-',
  6860. array('select' => array(
  6861. 'name' => 'ValidateProfile[1][ValidateItem][2][created][year]',
  6862. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  6863. )
  6864. ),
  6865. array('option' => array('value' => '')), '/option',
  6866. $this->dateRegex['yearsRegex'],
  6867. '/select',
  6868. '/div'
  6869. );
  6870. $this->assertTags($result, $expected);
  6871. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  6872. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  6873. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  6874. $expected = array(
  6875. 'div' => array('class' => 'input select error'),
  6876. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  6877. 'Profile',
  6878. '/label',
  6879. 'select' => array(
  6880. 'name' => 'ValidateProfile[1][ValidateItem][2][profile_id]',
  6881. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  6882. 'class' => 'form-error'
  6883. ),
  6884. '/select',
  6885. array('div' => array('class' => 'error-message')),
  6886. 'Error',
  6887. '/div',
  6888. '/div'
  6889. );
  6890. $this->assertTags($result, $expected);
  6891. }
  6892. /**
  6893. * test the correct display of multi-record form validation errors.
  6894. *
  6895. * @return void
  6896. */
  6897. public function testMultiRecordFormValidationErrors() {
  6898. $this->markTestIncomplete('Need to revisit once models work again.');
  6899. $this->Form->create('ValidateProfile');
  6900. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  6901. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  6902. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  6903. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  6904. $result = $this->Form->error('ValidateProfile.2.city');
  6905. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6906. $result = $this->Form->error('2.city');
  6907. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6908. }
  6909. /**
  6910. * test the correct display of multi-record form validation errors.
  6911. *
  6912. * @return void
  6913. */
  6914. public function testSaveManyRecordFormValidationErrors() {
  6915. $this->markTestIncomplete('Need to revisit once models work again.');
  6916. $this->Form->create('ValidateUser');
  6917. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  6918. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  6919. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  6920. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  6921. $result = $this->Form->error('ValidateUser.0.city');
  6922. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6923. }
  6924. /**
  6925. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  6926. *
  6927. * @return void
  6928. */
  6929. public function testInputTemplate() {
  6930. $this->markTestIncomplete('Need to revisit once models work again.');
  6931. $result = $this->Form->input('Contact.email', array(
  6932. 'type' => 'text', 'format' => array('input')
  6933. ));
  6934. $expected = array(
  6935. 'div' => array('class' => 'input text'),
  6936. 'input' => array(
  6937. 'type' => 'text', 'name' => 'Contact[email]',
  6938. 'id' => 'ContactEmail'
  6939. ),
  6940. '/div'
  6941. );
  6942. $this->assertTags($result, $expected);
  6943. $result = $this->Form->input('Contact.email', array(
  6944. 'type' => 'text', 'format' => array('input', 'label'),
  6945. 'label' => '<em>Email (required)</em>'
  6946. ));
  6947. $expected = array(
  6948. 'div' => array('class' => 'input text'),
  6949. array('input' => array(
  6950. 'type' => 'text', 'name' => 'Contact[email]',
  6951. 'id' => 'ContactEmail'
  6952. )),
  6953. 'label' => array('for' => 'ContactEmail'),
  6954. 'em' => array(),
  6955. 'Email (required)',
  6956. '/em',
  6957. '/label',
  6958. '/div'
  6959. );
  6960. $this->assertTags($result, $expected);
  6961. $result = $this->Form->input('Contact.email', array(
  6962. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  6963. 'between' => '<div>Something in the middle</div>',
  6964. 'after' => '<span>Some text at the end</span>'
  6965. ));
  6966. $expected = array(
  6967. 'div' => array('class' => 'input text'),
  6968. array('input' => array(
  6969. 'type' => 'text', 'name' => 'Contact[email]',
  6970. 'id' => 'ContactEmail'
  6971. )),
  6972. array('div' => array()),
  6973. 'Something in the middle',
  6974. '/div',
  6975. 'label' => array('for' => 'ContactEmail'),
  6976. 'Email',
  6977. '/label',
  6978. 'span' => array(),
  6979. 'Some text at the end',
  6980. '/span',
  6981. '/div'
  6982. );
  6983. $this->assertTags($result, $expected);
  6984. $result = $this->Form->input('Contact.method', array(
  6985. 'type' => 'radio',
  6986. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  6987. 'between' => 'I am between',
  6988. ));
  6989. $expected = array(
  6990. 'div' => array('class' => 'input radio'),
  6991. 'fieldset' => array(),
  6992. 'legend' => array(),
  6993. 'Method',
  6994. '/legend',
  6995. 'I am between',
  6996. 'input' => array(
  6997. 'type' => 'hidden', 'name' => 'Contact[method]',
  6998. 'value' => '', 'id' => 'ContactMethod_'
  6999. ),
  7000. array('input' => array(
  7001. 'type' => 'radio', 'name' => 'Contact[method]',
  7002. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7003. )),
  7004. array('label' => array('for' => 'ContactMethodEmail')),
  7005. 'Email',
  7006. '/label',
  7007. array('input' => array(
  7008. 'type' => 'radio', 'name' => 'Contact[method]',
  7009. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7010. )),
  7011. array('label' => array('for' => 'ContactMethodPigeon')),
  7012. 'Pigeon',
  7013. '/label',
  7014. '/fieldset',
  7015. '/div',
  7016. );
  7017. $this->assertTags($result, $expected);
  7018. }
  7019. /**
  7020. * test that some html5 inputs + FormHelper::__call() work
  7021. *
  7022. * @return void
  7023. */
  7024. public function testHtml5Inputs() {
  7025. $result = $this->Form->email('User.email');
  7026. $expected = array(
  7027. 'input' => array('type' => 'email', 'name' => 'User[email]')
  7028. );
  7029. $this->assertTags($result, $expected);
  7030. $result = $this->Form->search('User.query');
  7031. $expected = array(
  7032. 'input' => array('type' => 'search', 'name' => 'User[query]')
  7033. );
  7034. $this->assertTags($result, $expected);
  7035. $result = $this->Form->search('User.query', array('value' => 'test'));
  7036. $expected = array(
  7037. 'input' => array('type' => 'search', 'name' => 'User[query]', 'value' => 'test')
  7038. );
  7039. $this->assertTags($result, $expected);
  7040. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7041. $expected = array(
  7042. 'input' => array('type' => 'text', 'name' => 'User[query]', 'value' => 'test')
  7043. );
  7044. $this->assertTags($result, $expected);
  7045. }
  7046. /**
  7047. * Test accessing html5 inputs through input().
  7048. *
  7049. * @return void
  7050. */
  7051. public function testHtml5InputWithInput() {
  7052. $this->markTestIncomplete('Need to revisit once models work again.');
  7053. $result = $this->Form->input('User.website', array(
  7054. 'type' => 'url',
  7055. 'value' => 'http://domain.tld',
  7056. 'div' => false,
  7057. 'label' => false));
  7058. $expected = array(
  7059. 'input' => array('type' => 'url', 'name' => 'User[website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7060. );
  7061. $this->assertTags($result, $expected);
  7062. }
  7063. /**
  7064. * Test errors when field name is missing.
  7065. *
  7066. * @expectedException \Cake\Error\Exception
  7067. * @return void
  7068. */
  7069. public function testHtml5InputException() {
  7070. $this->Form->email();
  7071. }
  7072. /**
  7073. * Tests that a model can be loaded from the model names passed in the request object
  7074. *
  7075. * @return void
  7076. */
  7077. public function testIntrospectModelFromRequest() {
  7078. $this->markTestIncomplete('Need to revisit once models work again.');
  7079. $this->loadFixtures('Post');
  7080. App::build(array(
  7081. 'Plugin' => array(CAKE . 'Test/TestApp/Plugin/')
  7082. ));
  7083. Plugin::load('TestPlugin');
  7084. $this->Form->request['models'] = array(
  7085. 'TestPluginPost' => array(
  7086. 'plugin' => 'TestPlugin',
  7087. 'className' => 'TestPluginPost'
  7088. )
  7089. );
  7090. $this->Form->create('TestPluginPost');
  7091. Plugin::unload();
  7092. }
  7093. /**
  7094. * Tests that it is possible to set the validation errors directly in the helper for a field
  7095. *
  7096. * @return void
  7097. */
  7098. public function testCustomValidationErrors() {
  7099. $this->markTestIncomplete('Need to revisit once models work again.');
  7100. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7101. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7102. $this->assertEquals('Badness!', $result);
  7103. }
  7104. /**
  7105. * Tests that the 'on' key validates as expected on create
  7106. *
  7107. * @return void
  7108. */
  7109. public function testRequiredOnCreate() {
  7110. $this->markTestIncomplete('Need to revisit once models work again.');
  7111. $this->Form->create('Contact');
  7112. $result = $this->Form->input('Contact.imrequiredonupdate');
  7113. $expected = array(
  7114. 'div' => array('class' => 'input text'),
  7115. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7116. 'Imrequiredonupdate',
  7117. '/label',
  7118. 'input' => array(
  7119. 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
  7120. 'id' => 'ContactImrequiredonupdate'
  7121. ),
  7122. '/div'
  7123. );
  7124. $this->assertTags($result, $expected);
  7125. $result = $this->Form->input('Contact.imrequiredoncreate');
  7126. $expected = array(
  7127. 'div' => array('class' => 'input text required'),
  7128. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7129. 'Imrequiredoncreate',
  7130. '/label',
  7131. 'input' => array(
  7132. 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
  7133. 'id' => 'ContactImrequiredoncreate',
  7134. 'required' => 'required'
  7135. ),
  7136. '/div'
  7137. );
  7138. $this->assertTags($result, $expected);
  7139. $result = $this->Form->input('Contact.imrequiredonboth');
  7140. $expected = array(
  7141. 'div' => array('class' => 'input text required'),
  7142. 'label' => array('for' => 'ContactImrequiredonboth'),
  7143. 'Imrequiredonboth',
  7144. '/label',
  7145. 'input' => array(
  7146. 'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
  7147. 'id' => 'ContactImrequiredonboth',
  7148. 'required' => 'required'
  7149. ),
  7150. '/div'
  7151. );
  7152. $this->assertTags($result, $expected);
  7153. $this->Form->inputDefaults(array('required' => false));
  7154. $result = $this->Form->input('Contact.imrequired');
  7155. $expected = array(
  7156. 'div' => array('class' => 'input text'),
  7157. 'label' => array('for' => 'ContactImrequired'),
  7158. 'Imrequired',
  7159. '/label',
  7160. 'input' => array(
  7161. 'type' => 'text', 'name' => 'Contact[imrequired]',
  7162. 'id' => 'ContactImrequired'
  7163. ),
  7164. '/div'
  7165. );
  7166. $this->assertTags($result, $expected);
  7167. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  7168. $this->assertTags($result, $expected);
  7169. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  7170. $expected = array(
  7171. 'div' => array('class' => 'input text required'),
  7172. 'label' => array('for' => 'ContactImrequired'),
  7173. 'Imrequired',
  7174. '/label',
  7175. 'input' => array(
  7176. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7177. 'id' => 'ContactImrequired'
  7178. ),
  7179. '/div'
  7180. );
  7181. $this->assertTags($result, $expected);
  7182. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  7183. $this->assertTags($result, $expected);
  7184. }
  7185. /**
  7186. * Tests that the 'on' key validates as expected on update
  7187. *
  7188. * @return void
  7189. */
  7190. public function testRequiredOnUpdate() {
  7191. $this->markTestIncomplete('Need to revisit once models work again.');
  7192. $this->Form->request->data['Contact']['id'] = 1;
  7193. $this->Form->create('Contact');
  7194. $result = $this->Form->input('Contact.imrequiredonupdate');
  7195. $expected = array(
  7196. 'div' => array('class' => 'input text required'),
  7197. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7198. 'Imrequiredonupdate',
  7199. '/label',
  7200. 'input' => array(
  7201. 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
  7202. 'id' => 'ContactImrequiredonupdate',
  7203. 'required' => 'required'
  7204. ),
  7205. '/div'
  7206. );
  7207. $this->assertTags($result, $expected);
  7208. $result = $this->Form->input('Contact.imrequiredoncreate');
  7209. $expected = array(
  7210. 'div' => array('class' => 'input text'),
  7211. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7212. 'Imrequiredoncreate',
  7213. '/label',
  7214. 'input' => array(
  7215. 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
  7216. 'id' => 'ContactImrequiredoncreate'
  7217. ),
  7218. '/div'
  7219. );
  7220. $this->assertTags($result, $expected);
  7221. $result = $this->Form->input('Contact.imrequiredonboth');
  7222. $expected = array(
  7223. 'div' => array('class' => 'input text required'),
  7224. 'label' => array('for' => 'ContactImrequiredonboth'),
  7225. 'Imrequiredonboth',
  7226. '/label',
  7227. 'input' => array(
  7228. 'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
  7229. 'id' => 'ContactImrequiredonboth',
  7230. 'required' => 'required'
  7231. ),
  7232. '/div'
  7233. );
  7234. $this->assertTags($result, $expected);
  7235. $result = $this->Form->input('Contact.imrequired');
  7236. $expected = array(
  7237. 'div' => array('class' => 'input text required'),
  7238. 'label' => array('for' => 'ContactImrequired'),
  7239. 'Imrequired',
  7240. '/label',
  7241. 'input' => array(
  7242. 'type' => 'text', 'name' => 'Contact[imrequired]',
  7243. 'id' => 'ContactImrequired',
  7244. 'required' => 'required'
  7245. ),
  7246. '/div'
  7247. );
  7248. $this->assertTags($result, $expected);
  7249. }
  7250. /**
  7251. * Test inputDefaults setter and getter
  7252. *
  7253. * @return void
  7254. */
  7255. public function testInputDefaults() {
  7256. $this->markTestIncomplete('Need to revisit once models work again.');
  7257. $this->Form->create('Contact');
  7258. $this->Form->inputDefaults(array(
  7259. 'label' => false,
  7260. 'div' => array(
  7261. 'style' => 'color: #000;'
  7262. )
  7263. ));
  7264. $result = $this->Form->input('Contact.field1');
  7265. $expected = array(
  7266. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7267. 'input' => array(
  7268. 'type' => 'text', 'name' => 'Contact[field1]',
  7269. 'id' => 'ContactField1'
  7270. ),
  7271. '/div'
  7272. );
  7273. $this->assertTags($result, $expected);
  7274. $this->Form->inputDefaults(array(
  7275. 'div' => false,
  7276. 'label' => 'Label',
  7277. ));
  7278. $result = $this->Form->input('Contact.field1');
  7279. $expected = array(
  7280. 'label' => array('for' => 'ContactField1'),
  7281. 'Label',
  7282. '/label',
  7283. 'input' => array(
  7284. 'type' => 'text', 'name' => 'Contact[field1]',
  7285. 'id' => 'ContactField1'
  7286. ),
  7287. );
  7288. $this->assertTags($result, $expected);
  7289. $this->Form->inputDefaults(array(
  7290. 'label' => false,
  7291. ), true);
  7292. $result = $this->Form->input('Contact.field1');
  7293. $expected = array(
  7294. 'input' => array(
  7295. 'type' => 'text', 'name' => 'Contact[field1]',
  7296. 'id' => 'ContactField1'
  7297. ),
  7298. );
  7299. $this->assertTags($result, $expected);
  7300. $result = $this->Form->inputDefaults();
  7301. $expected = array(
  7302. 'div' => false,
  7303. 'label' => false,
  7304. );
  7305. $this->assertEquals($expected, $result);
  7306. }
  7307. }