FormHelperTest.php 240 KB

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