FormHelperTest.php 214 KB

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