FormHelperTest.php 240 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @since CakePHP(tm) v 1.2.0.4206
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\View\Helper;
  18. use Cake\Controller\Controller;
  19. use Cake\Core\App;
  20. use Cake\Core\Configure;
  21. use Cake\Core\Plugin;
  22. use Cake\Network\Request;
  23. use Cake\ORM\Entity;
  24. use Cake\ORM\Table;
  25. use Cake\ORM\TableRegistry;
  26. use Cake\Routing\Router;
  27. use Cake\TestSuite\TestCase;
  28. use Cake\Utility\Security;
  29. use Cake\View\Helper\FormHelper;
  30. use Cake\View\Helper\HtmlHelper;
  31. use Cake\View\View;
  32. /**
  33. * Test stub.
  34. */
  35. class Article extends Entity {
  36. }
  37. /**
  38. * ContactTestController class
  39. *
  40. */
  41. class ContactTestController extends Controller {
  42. /**
  43. * uses property
  44. *
  45. * @var mixed null
  46. */
  47. public $uses = null;
  48. }
  49. /**
  50. * Contact class
  51. *
  52. */
  53. class ContactsTable extends Table {
  54. /**
  55. * Default schema
  56. *
  57. * @var array
  58. */
  59. protected $_schema = array(
  60. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  61. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  65. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  66. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  67. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  68. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  69. );
  70. /**
  71. * validate property
  72. *
  73. * @var array
  74. */
  75. public $validate = array(
  76. 'non_existing' => array(),
  77. 'idontexist' => array(),
  78. 'imrequired' => array('_allowEmpty' => false, array('rule' => array('between', 5, 30))),
  79. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  80. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  81. 'imrequiredonboth' => array(
  82. 'required' => array('rule' => 'alphaNumeric'),
  83. ),
  84. 'string_required' => 'notEmpty',
  85. 'imalsorequired' => array('_allowEmpty' => false, array('rule' => 'alphaNumeric')),
  86. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  87. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  88. 'imnotrequired' => array(
  89. '_allowEmpty' => true,
  90. array('rule' => 'alphaNumeric')
  91. ),
  92. 'imalsonotrequired' => array(
  93. '_allowEmpty' => true,
  94. 'alpha' => array('rule' => 'alphaNumeric'),
  95. 'between' => array('rule' => array('between', 5, 30)),
  96. ),
  97. 'imnotrequiredeither' => array(
  98. '_allowEmpty' => true,
  99. array('rule' => array('between', 5, 30))
  100. ),
  101. 'iamrequiredalways' => array(
  102. 'email' => array('rule' => 'email'),
  103. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  104. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  105. ),
  106. 'boolean_field' => array('rule' => 'boolean')
  107. );
  108. /**
  109. * schema method
  110. *
  111. * @return void
  112. */
  113. public function setSchema($schema) {
  114. $this->_schema = $schema;
  115. }
  116. /**
  117. * hasAndBelongsToMany property
  118. *
  119. * @var array
  120. */
  121. public $hasAndBelongsToMany = array(
  122. 'ContactTag' => array(
  123. 'className' => 'Cake\Test\TestCase\View\Helper\ContactTag',
  124. 'with' => 'Cake\Test\TestCase\View\Helper\ContactTagsContact'
  125. )
  126. );
  127. /**
  128. * hasAndBelongsToMany property
  129. *
  130. * @var array
  131. */
  132. public $belongsTo = array(
  133. 'User' => array('className' => 'UserForm'
  134. ));
  135. }
  136. /**
  137. * ContactTagsContact class
  138. *
  139. */
  140. class ContactTagsContactsTable extends Table {
  141. /**
  142. * Default schema
  143. *
  144. * @var array
  145. */
  146. protected $_schema = array(
  147. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  148. 'contact_tag_id' => array(
  149. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  150. )
  151. );
  152. /**
  153. * schema method
  154. *
  155. * @return void
  156. */
  157. public function setSchema($schema) {
  158. $this->_schema = $schema;
  159. }
  160. }
  161. /**
  162. * ContactNonStandardPk class
  163. *
  164. */
  165. class ContactNonStandardPk extends ContactsTable {
  166. /**
  167. * primaryKey property
  168. *
  169. * @var string
  170. */
  171. public $primaryKey = 'pk';
  172. /**
  173. * schema method
  174. *
  175. * @return void
  176. */
  177. public function schema($field = false) {
  178. $this->_schema = parent::schema();
  179. $this->_schema['pk'] = $this->_schema['id'];
  180. unset($this->_schema['id']);
  181. return $this->_schema;
  182. }
  183. }
  184. /**
  185. * ContactTag class
  186. *
  187. */
  188. class ContactTagsTable extends Table {
  189. /**
  190. * schema definition
  191. *
  192. * @var array
  193. */
  194. protected $_schema = array(
  195. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  196. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  197. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  198. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  199. );
  200. }
  201. /**
  202. * UserForm class
  203. *
  204. */
  205. class UserFormsTable extends Table {
  206. /**
  207. * hasMany property
  208. *
  209. * @var array
  210. */
  211. public $hasMany = array(
  212. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  213. ));
  214. /**
  215. * schema definition
  216. *
  217. * @var array
  218. */
  219. protected $_schema = array(
  220. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  221. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  222. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  223. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  224. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  225. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  226. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  227. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  228. );
  229. }
  230. /**
  231. * OpenidUrl class
  232. *
  233. */
  234. class OpenidUrlsTable extends Table {
  235. /**
  236. * belongsTo property
  237. *
  238. * @var array
  239. */
  240. public $belongsTo = array('UserForm' => array(
  241. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  242. ));
  243. /**
  244. * validate property
  245. *
  246. * @var array
  247. */
  248. public $validate = array('openid_not_registered' => array());
  249. /**
  250. * schema method
  251. *
  252. * @var array
  253. */
  254. protected $_schema = array(
  255. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  256. 'user_form_id' => array(
  257. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  258. ),
  259. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  260. );
  261. /**
  262. * beforeValidate method
  263. *
  264. * @return void
  265. */
  266. public function beforeValidate($options = array()) {
  267. $this->invalidate('openid_not_registered');
  268. return true;
  269. }
  270. }
  271. /**
  272. * ValidateUser class
  273. *
  274. */
  275. class ValidateUsersTable extends Table {
  276. /**
  277. * hasOne property
  278. *
  279. * @var array
  280. */
  281. public $hasOne = array('ValidateProfile' => array(
  282. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  283. ));
  284. /**
  285. * schema method
  286. *
  287. * @var array
  288. */
  289. protected $_schema = array(
  290. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  291. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  292. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  293. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  294. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => '6,3'),
  295. 'ratio' => array('type' => 'decimal', 'null' => false, 'length' => '10,6'),
  296. 'population' => array('type' => 'decimal', 'null' => false, 'length' => '15,0'),
  297. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  298. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  299. );
  300. /**
  301. * beforeValidate method
  302. *
  303. * @param array $options
  304. * @return void
  305. */
  306. public function beforeValidate($options = array()) {
  307. $this->invalidate('email');
  308. return false;
  309. }
  310. }
  311. /**
  312. * ValidateProfile class
  313. *
  314. */
  315. class ValidateProfilesTable extends Table {
  316. /**
  317. * useTable property
  318. *
  319. * @var boolean
  320. */
  321. public $useTable = false;
  322. /**
  323. * schema property
  324. *
  325. * @var array
  326. */
  327. protected $_schema = array(
  328. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  329. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  330. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  331. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  332. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  333. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  334. );
  335. /**
  336. * hasOne property
  337. *
  338. * @var array
  339. */
  340. public $hasOne = array('ValidateItem' => array(
  341. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  342. ));
  343. /**
  344. * belongsTo property
  345. *
  346. * @var array
  347. */
  348. public $belongsTo = array('ValidateUser' => array(
  349. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  350. ));
  351. /**
  352. * beforeValidate method
  353. *
  354. * @return void
  355. */
  356. public function beforeValidate($options = array()) {
  357. $this->invalidate('full_name');
  358. $this->invalidate('city');
  359. return false;
  360. }
  361. }
  362. /**
  363. * ValidateItem class
  364. *
  365. */
  366. class ValidateItemsTable extends Table {
  367. /**
  368. * schema property
  369. *
  370. * @var array
  371. */
  372. protected $_schema = array(
  373. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  374. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  375. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  376. 'description' => array(
  377. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  378. ),
  379. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  380. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  381. );
  382. /**
  383. * belongsTo property
  384. *
  385. * @var array
  386. */
  387. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  388. /**
  389. * beforeValidate method
  390. *
  391. * @return void
  392. */
  393. public function beforeValidate($options = array()) {
  394. $this->invalidate('description');
  395. return false;
  396. }
  397. }
  398. /**
  399. * TestMail class
  400. *
  401. */
  402. class TestMailsTable extends Table {
  403. }
  404. /**
  405. * FormHelperTest class
  406. *
  407. * @property FormHelper $Form
  408. */
  409. class FormHelperTest extends TestCase {
  410. /**
  411. * Fixtures to be used
  412. *
  413. * @var array
  414. */
  415. public $fixtures = array('core.article');
  416. /**
  417. * Do not load the fixtures by default
  418. *
  419. * @var boolean
  420. */
  421. public $autoFixtures = false;
  422. /**
  423. * setUp method
  424. *
  425. * @return void
  426. */
  427. public function setUp() {
  428. parent::setUp();
  429. Configure::write('Config.language', 'eng');
  430. Configure::write('App.base', '');
  431. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  432. Configure::delete('Asset');
  433. $this->View = new View(null);
  434. $this->Form = new FormHelper($this->View);
  435. $this->Form->Html = new HtmlHelper($this->View);
  436. $this->Form->request = new Request('articles/add');
  437. $this->Form->request->here = '/articles/add';
  438. $this->Form->request['controller'] = 'articles';
  439. $this->Form->request['action'] = 'add';
  440. $this->Form->request->webroot = '';
  441. $this->Form->request->base = '';
  442. $this->dateRegex = array(
  443. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  444. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  445. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  446. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  447. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  448. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  449. );
  450. $this->article = [
  451. 'schema' => [
  452. 'id' => ['type' => 'integer'],
  453. 'author_id' => ['type' => 'integer', 'null' => true],
  454. 'title' => ['type' => 'string', 'null' => true],
  455. 'body' => 'text',
  456. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  457. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  458. ],
  459. 'required' => [
  460. 'author_id' => true,
  461. 'title' => true,
  462. ]
  463. ];
  464. Configure::write('Security.salt', 'foo!');
  465. Router::connect('/:controller', array('action' => 'index'));
  466. Router::connect('/:controller/:action/*');
  467. }
  468. /**
  469. * tearDown method
  470. *
  471. * @return void
  472. */
  473. public function tearDown() {
  474. parent::tearDown();
  475. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  476. }
  477. /**
  478. * Test registering a new widget class and rendering it.
  479. *
  480. * @return void
  481. */
  482. public function testAddWidgetAndRenderWidget() {
  483. $data = [
  484. 'val' => 1
  485. ];
  486. $mock = $this->getMock('Cake\View\Widget\WidgetInterface');
  487. $this->assertNull($this->Form->addWidget('test', $mock));
  488. $mock->expects($this->once())
  489. ->method('render')
  490. ->with($data)
  491. ->will($this->returnValue('HTML'));
  492. $result = $this->Form->widget('test', $data);
  493. $this->assertEquals('HTML', $result);
  494. }
  495. /**
  496. * Test registering an invalid widget class.
  497. *
  498. * @expectedException \RuntimeException
  499. * @return void
  500. */
  501. public function testAddWidgetInvalid() {
  502. $mock = new \StdClass();
  503. $this->Form->addWidget('test', $mock);
  504. $this->Form->widget('test');
  505. }
  506. /**
  507. * Test adding a new context class.
  508. *
  509. * @return void
  510. */
  511. public function testAddContextProvider() {
  512. $context = 'My data';
  513. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  514. $this->assertInstanceOf('Cake\Network\Request', $request);
  515. $this->assertEquals($context, $data['entity']);
  516. return $this->getMock('Cake\View\Form\ContextInterface');
  517. });
  518. $this->Form->create($context);
  519. }
  520. /**
  521. * Test adding an invalid context class.
  522. *
  523. * @expectedException RuntimeException
  524. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  525. * @return void
  526. */
  527. public function testAddContextProviderInvalid() {
  528. $context = 'My data';
  529. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  530. return new \StdClass();
  531. });
  532. $this->Form->create($context);
  533. }
  534. /**
  535. * Provides context options for create().
  536. *
  537. * @return array
  538. */
  539. public function contextSelectionProvider() {
  540. $entity = new Article();
  541. $collection = $this->getMock('Cake\Collection\Collection', ['extract'], [[$entity]]);
  542. $data = [
  543. 'schema' => [
  544. 'title' => ['type' => 'string']
  545. ]
  546. ];
  547. return [
  548. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  549. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  550. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  551. 'none' => [null, 'Cake\View\Form\NullContext'],
  552. 'false' => [false, 'Cake\View\Form\NullContext'],
  553. ];
  554. }
  555. /**
  556. * Test default context selection in create()
  557. *
  558. * @dataProvider contextSelectionProvider
  559. * @return void
  560. */
  561. public function testCreateContextSelectionBuiltIn($data, $class) {
  562. $this->loadFixtures('Article');
  563. $this->Form->create($data);
  564. $this->assertInstanceOf($class, $this->Form->context());
  565. }
  566. /**
  567. * Data provider for type option.
  568. *
  569. * @return array
  570. */
  571. public static function requestTypeProvider() {
  572. return [
  573. // type, method, override
  574. ['post', 'post', 'POST'],
  575. ['put', 'post', 'PUT'],
  576. ['patch', 'post', 'PATCH'],
  577. ['delete', 'post', 'DELETE'],
  578. ];
  579. }
  580. /**
  581. * Test creating file forms.
  582. *
  583. * @return void
  584. */
  585. public function testCreateFile() {
  586. $encoding = strtolower(Configure::read('App.encoding'));
  587. $result = $this->Form->create(false, array('type' => 'file'));
  588. $expected = array(
  589. 'form' => array(
  590. 'method' => 'post', 'action' => '/articles/add',
  591. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  592. ),
  593. 'div' => array('style' => 'display:none;'),
  594. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  595. '/div'
  596. );
  597. $this->assertTags($result, $expected);
  598. }
  599. /**
  600. * Test creating GET forms.
  601. *
  602. * @return void
  603. */
  604. public function testCreateGet() {
  605. $encoding = strtolower(Configure::read('App.encoding'));
  606. $result = $this->Form->create(false, array('type' => 'get'));
  607. $expected = array('form' => array(
  608. 'method' => 'get', 'action' => '/articles/add',
  609. 'accept-charset' => $encoding
  610. ));
  611. $this->assertTags($result, $expected);
  612. }
  613. /**
  614. * test the create() method
  615. *
  616. * @dataProvider requestTypeProvider
  617. * @return void
  618. */
  619. public function testCreateTypeOptions($type, $method, $override) {
  620. $encoding = strtolower(Configure::read('App.encoding'));
  621. $result = $this->Form->create(false, array('type' => $type));
  622. $expected = array(
  623. 'form' => array(
  624. 'method' => $method, 'action' => '/articles/add',
  625. 'accept-charset' => $encoding
  626. ),
  627. 'div' => array('style' => 'display:none;'),
  628. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => $override),
  629. '/div'
  630. );
  631. $this->assertTags($result, $expected);
  632. }
  633. /**
  634. * Test opening a form for an update operation.
  635. *
  636. * @return void
  637. */
  638. public function testCreateUpdateForm() {
  639. $encoding = strtolower(Configure::read('App.encoding'));
  640. $this->Form->request->here = '/articles/edit/1';
  641. $this->Form->request['action'] = 'edit';
  642. $this->article['defaults']['id'] = 1;
  643. $result = $this->Form->create($this->article);
  644. $expected = array(
  645. 'form' => array(
  646. 'method' => 'post', 'action' => '/articles/edit/1',
  647. 'accept-charset' => $encoding
  648. ),
  649. 'div' => array('style' => 'display:none;'),
  650. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  651. '/div'
  652. );
  653. $this->assertTags($result, $expected);
  654. }
  655. /**
  656. * Test the onsubmit option for create()
  657. *
  658. * @return void
  659. */
  660. public function testCreateOnSubmit() {
  661. $this->Form->request->data = [];
  662. $this->Form->request['controller'] = 'articles';
  663. $result = $this->Form->create($this->article, ['url' => ['action' => 'index', 'param'], 'default' => false]);
  664. $expected = array(
  665. 'form' => array(
  666. 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/articles/index/param',
  667. 'accept-charset' => 'utf-8'
  668. ),
  669. 'div' => array('style' => 'display:none;'),
  670. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  671. '/div'
  672. );
  673. $this->assertTags($result, $expected);
  674. $this->Form->request->data = [];
  675. $this->Form->request['controller'] = 'articles';
  676. $result = $this->Form->create($this->article, array(
  677. 'url' => array('action' => 'index', 'param'),
  678. 'default' => false,
  679. 'onsubmit' => 'someFunction();'
  680. ));
  681. $expected = array(
  682. 'form' => array(
  683. 'method' => 'post',
  684. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  685. 'action' => '/articles/index/param',
  686. 'accept-charset' => 'utf-8'
  687. ),
  688. 'div' => array('style' => 'display:none;'),
  689. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  690. '/div'
  691. );
  692. $this->assertTags($result, $expected);
  693. }
  694. /**
  695. * test create() with automatic url generation
  696. *
  697. * @return void
  698. */
  699. public function testCreateAutoUrl() {
  700. $encoding = strtolower(Configure::read('App.encoding'));
  701. $this->Form->request['action'] = 'delete';
  702. $this->Form->request->here = '/articles/delete/10';
  703. $this->Form->request->base = '';
  704. $result = $this->Form->create($this->article);
  705. $expected = array(
  706. 'form' => array(
  707. 'method' => 'post', 'action' => '/articles/delete/10',
  708. 'accept-charset' => $encoding
  709. ),
  710. 'div' => array('style' => 'display:none;'),
  711. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  712. '/div'
  713. );
  714. $this->assertTags($result, $expected);
  715. $this->article['defaults'] = ['id' => 1];
  716. $this->Form->request->here = '/articles/edit/1';
  717. $this->Form->request['action'] = 'delete';
  718. $result = $this->Form->create($this->article, ['action' => 'edit']);
  719. $expected = array(
  720. 'form' => array(
  721. 'method' => 'post',
  722. 'action' => '/articles/edit/1',
  723. 'accept-charset' => $encoding
  724. ),
  725. 'div' => array('style' => 'display:none;'),
  726. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  727. '/div'
  728. );
  729. $this->assertTags($result, $expected);
  730. $this->Form->request['action'] = 'add';
  731. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  732. $expected = array(
  733. 'form' => array(
  734. 'method' => 'post',
  735. 'action' => '/articles/publish/1',
  736. 'accept-charset' => $encoding
  737. ),
  738. 'div' => array('style' => 'display:none;'),
  739. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  740. '/div'
  741. );
  742. $this->assertTags($result, $expected);
  743. $result = $this->Form->create($this->article, array('url' => '/articles/publish'));
  744. $expected = array(
  745. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  746. 'div' => array('style' => 'display:none;'),
  747. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  748. '/div'
  749. );
  750. $this->assertTags($result, $expected);
  751. $this->Form->request['controller'] = 'pages';
  752. $result = $this->Form->create($this->article, array('action' => 'signup'));
  753. $expected = array(
  754. 'form' => array(
  755. 'method' => 'post', 'action' => '/pages/signup/1',
  756. 'accept-charset' => $encoding
  757. ),
  758. 'div' => array('style' => 'display:none;'),
  759. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  760. '/div'
  761. );
  762. $this->assertTags($result, $expected);
  763. }
  764. /**
  765. * test create() with a custom route
  766. *
  767. * @return void
  768. */
  769. public function testCreateCustomRoute() {
  770. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  771. $encoding = strtolower(Configure::read('App.encoding'));
  772. $this->Form->request['controller'] = 'users';
  773. $result = $this->Form->create(false, array('action' => 'login'));
  774. $expected = array(
  775. 'form' => array(
  776. 'method' => 'post', 'action' => '/login',
  777. 'accept-charset' => $encoding
  778. ),
  779. 'div' => array('style' => 'display:none;'),
  780. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  781. '/div'
  782. );
  783. $this->assertTags($result, $expected);
  784. }
  785. /**
  786. * test that inputDefaults are stored and used.
  787. *
  788. * @return void
  789. */
  790. public function testCreateWithInputDefaults() {
  791. $this->markTestIncomplete('Need to revisit once models work again.');
  792. $this->Form->create('User', array(
  793. 'inputDefaults' => array(
  794. 'div' => false,
  795. 'label' => false,
  796. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  797. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  798. )
  799. ));
  800. $result = $this->Form->input('username');
  801. $expected = array(
  802. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername')
  803. );
  804. $this->assertTags($result, $expected);
  805. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  806. $expected = array(
  807. 'div' => array('class' => 'input text'),
  808. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  809. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  810. '/div'
  811. );
  812. $this->assertTags($result, $expected);
  813. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  814. $expected = array(
  815. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  816. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  817. );
  818. $this->assertTags($result, $expected);
  819. $this->Form->create('User', array(
  820. 'inputDefaults' => array(
  821. 'div' => false,
  822. 'label' => array('class' => 'nice', 'for' => 'changed'),
  823. )
  824. ));
  825. $result = $this->Form->input('username', array('div' => true));
  826. $expected = array(
  827. 'div' => array('class' => 'input text'),
  828. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  829. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  830. '/div'
  831. );
  832. $this->assertTags($result, $expected);
  833. }
  834. /**
  835. * test automatic accept-charset overriding
  836. *
  837. * @return void
  838. */
  839. public function testCreateWithAcceptCharset() {
  840. $result = $this->Form->create($this->article, array(
  841. 'type' => 'post', 'action' => 'index', 'encoding' => 'iso-8859-1'
  842. )
  843. );
  844. $expected = array(
  845. 'form' => array(
  846. 'method' => 'post', 'action' => '/articles',
  847. 'accept-charset' => 'iso-8859-1'
  848. ),
  849. 'div' => array('style' => 'display:none;'),
  850. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  851. '/div'
  852. );
  853. $this->assertTags($result, $expected);
  854. }
  855. /**
  856. * Test base form URL when url param is passed with multiple parameters (&)
  857. *
  858. */
  859. public function testCreateQuerystringrequest() {
  860. $encoding = strtolower(Configure::read('App.encoding'));
  861. $result = $this->Form->create($this->article, array(
  862. 'type' => 'post',
  863. 'escape' => false,
  864. 'url' => array(
  865. 'controller' => 'controller',
  866. 'action' => 'action',
  867. '?' => array('param1' => 'value1', 'param2' => 'value2')
  868. )
  869. ));
  870. $expected = array(
  871. 'form' => array(
  872. 'method' => 'post',
  873. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  874. 'accept-charset' => $encoding
  875. ),
  876. 'div' => array('style' => 'display:none;'),
  877. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  878. '/div'
  879. );
  880. $this->assertTags($result, $expected);
  881. $result = $this->Form->create($this->article, array(
  882. 'type' => 'post',
  883. 'url' => array(
  884. 'controller' => 'controller',
  885. 'action' => 'action',
  886. '?' => array('param1' => 'value1', 'param2' => 'value2')
  887. )
  888. ));
  889. $this->assertTags($result, $expected);
  890. }
  891. /**
  892. * test that create() doesn't cause errors by multiple id's being in the primary key
  893. * as could happen with multiple select or checkboxes.
  894. *
  895. * @return void
  896. */
  897. public function testCreateWithMultipleIdInData() {
  898. $encoding = strtolower(Configure::read('App.encoding'));
  899. $this->Form->request->data['Article']['id'] = array(1, 2);
  900. $result = $this->Form->create($this->article);
  901. $expected = array(
  902. 'form' => array(
  903. 'method' => 'post',
  904. 'action' => '/articles/add',
  905. 'accept-charset' => $encoding
  906. ),
  907. 'div' => array('style' => 'display:none;'),
  908. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  909. '/div'
  910. );
  911. $this->assertTags($result, $expected);
  912. }
  913. /**
  914. * test that create() doesn't add in extra passed params.
  915. *
  916. * @return void
  917. */
  918. public function testCreatePassedArgs() {
  919. $encoding = strtolower(Configure::read('App.encoding'));
  920. $this->Form->request->data['Article']['id'] = 1;
  921. $result = $this->Form->create($this->article, array(
  922. 'type' => 'post',
  923. 'escape' => false,
  924. 'url' => array(
  925. 'action' => 'edit',
  926. 'myparam'
  927. )
  928. ));
  929. $expected = array(
  930. 'form' => array(
  931. 'method' => 'post',
  932. 'action' => '/articles/edit/myparam',
  933. 'accept-charset' => $encoding
  934. ),
  935. 'div' => array('style' => 'display:none;'),
  936. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  937. '/div'
  938. );
  939. $this->assertTags($result, $expected);
  940. }
  941. /**
  942. * test creating a get form, and get form inputs.
  943. *
  944. * @return void
  945. */
  946. public function testGetFormCreate() {
  947. $encoding = strtolower(Configure::read('App.encoding'));
  948. $result = $this->Form->create($this->article, array('type' => 'get'));
  949. $this->assertTags($result, array('form' => array(
  950. 'method' => 'get', 'action' => '/articles/add',
  951. 'accept-charset' => $encoding
  952. )));
  953. $result = $this->Form->text('title');
  954. $this->assertTags($result, array('input' => array(
  955. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  956. )));
  957. $result = $this->Form->password('password');
  958. $this->assertTags($result, array('input' => array(
  959. 'name' => 'password', 'type' => 'password'
  960. )));
  961. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  962. $result = $this->Form->text('user_form');
  963. $this->assertTags($result, array('input' => array(
  964. 'name' => 'user_form', 'type' => 'text'
  965. )));
  966. }
  967. /**
  968. * test get form, and inputs when the model param is false
  969. *
  970. * @return void
  971. */
  972. public function testGetFormWithFalseModel() {
  973. $encoding = strtolower(Configure::read('App.encoding'));
  974. $this->Form->request['controller'] = 'contact_test';
  975. $result = $this->Form->create(false, array(
  976. 'type' => 'get', 'url' => array('controller' => 'contact_test')
  977. ));
  978. $expected = array('form' => array(
  979. 'method' => 'get', 'action' => '/contact_test/add',
  980. 'accept-charset' => $encoding
  981. ));
  982. $this->assertTags($result, $expected);
  983. $result = $this->Form->text('reason');
  984. $expected = array(
  985. 'input' => array('type' => 'text', 'name' => 'reason')
  986. );
  987. $this->assertTags($result, $expected);
  988. }
  989. /**
  990. * testFormCreateWithSecurity method
  991. *
  992. * Test form->create() with security key.
  993. *
  994. * @return void
  995. */
  996. public function testCreateWithSecurity() {
  997. $this->Form->request->params['_csrfToken'] = 'testKey';
  998. $encoding = strtolower(Configure::read('App.encoding'));
  999. $result = $this->Form->create($this->article, [
  1000. 'url' => '/articles/publish',
  1001. ]);
  1002. $expected = array(
  1003. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  1004. 'div' => array('style' => 'display:none;'),
  1005. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1006. array('input' => array(
  1007. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  1008. )),
  1009. '/div'
  1010. );
  1011. $this->assertTags($result, $expected);
  1012. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1013. $expected['form']['id'] = 'MyForm';
  1014. $this->assertTags($result, $expected);
  1015. }
  1016. /**
  1017. * testFormCreateGetNoSecurity method
  1018. *
  1019. * Test form->create() with no security key as its a get form
  1020. *
  1021. * @return void
  1022. */
  1023. public function testCreateEndGetNoSecurity() {
  1024. $this->Form->request->params['_csrfToken'] = 'testKey';
  1025. $encoding = strtolower(Configure::read('App.encoding'));
  1026. $article = new Article();
  1027. $result = $this->Form->create($article, [
  1028. 'type' => 'get',
  1029. 'url' => '/contacts/add'
  1030. ]);
  1031. $this->assertNotContains('testKey', $result);
  1032. $result = $this->Form->end('Save');
  1033. $this->assertNotContains('testKey', $result);
  1034. }
  1035. /**
  1036. * test that create() clears the fields property so it starts fresh
  1037. *
  1038. * @return void
  1039. */
  1040. public function testCreateClearingFields() {
  1041. $this->Form->fields = array('model_id');
  1042. $this->Form->create($this->article);
  1043. $this->assertEquals(array(), $this->Form->fields);
  1044. }
  1045. /**
  1046. * Tests form hash generation with model-less data
  1047. *
  1048. * @return void
  1049. */
  1050. public function testValidateHashNoModel() {
  1051. $this->Form->request->params['_Token'] = 'foo';
  1052. $result = $this->Form->secure(array('anything'));
  1053. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1054. }
  1055. /**
  1056. * Tests that models with identical field names get resolved properly
  1057. *
  1058. * @return void
  1059. */
  1060. public function testDuplicateFieldNameResolution() {
  1061. $this->markTestIncomplete('Need to revisit once models work again.');
  1062. $result = $this->Form->create('ValidateUser');
  1063. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  1064. $result = $this->Form->input('ValidateItem.name');
  1065. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  1066. $result = $this->Form->input('ValidateUser.name');
  1067. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  1068. $this->assertContains('name="ValidateUser[name]"', $result);
  1069. $this->assertContains('type="text"', $result);
  1070. $result = $this->Form->input('ValidateItem.name');
  1071. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  1072. $this->assertContains('name="ValidateItem[name]"', $result);
  1073. $this->assertContains('<textarea', $result);
  1074. $result = $this->Form->input('name');
  1075. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  1076. $this->assertContains('name="ValidateUser[name]"', $result);
  1077. $this->assertContains('type="text"', $result);
  1078. }
  1079. /**
  1080. * Tests that hidden fields generated for checkboxes don't get locked
  1081. *
  1082. * @return void
  1083. */
  1084. public function testNoCheckboxLocking() {
  1085. $this->Form->request->params['_Token'] = 'foo';
  1086. $this->assertSame([], $this->Form->fields);
  1087. $this->Form->checkbox('check', array('value' => '1'));
  1088. $this->assertSame($this->Form->fields, array('check'));
  1089. }
  1090. /**
  1091. * testFormSecurityFields method
  1092. *
  1093. * Test generation of secure form hash generation.
  1094. *
  1095. * @return void
  1096. */
  1097. public function testFormSecurityFields() {
  1098. $this->markTestIncomplete('Need to revisit once models work again.');
  1099. $key = 'testKey';
  1100. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  1101. $this->Form->request->params['_csrfToken'] = $key;
  1102. $result = $this->Form->secure($fields);
  1103. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  1104. $hash .= ':' . 'Model.valid';
  1105. $hash = urlencode($hash);
  1106. $expected = array(
  1107. 'div' => array('style' => 'display:none;'),
  1108. array('input' => array(
  1109. 'type' => 'hidden', 'name' => '_Token[fields]',
  1110. 'value' => $hash
  1111. )),
  1112. array('input' => array(
  1113. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1114. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1115. )),
  1116. '/div'
  1117. );
  1118. $this->assertTags($result, $expected);
  1119. $path = CAKE . 'Test/TestApp/Config/';
  1120. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  1121. $result = $this->Form->secure($fields);
  1122. $expected = array(
  1123. 'div' => array('class' => 'hidden'),
  1124. array('input' => array(
  1125. 'type' => 'hidden', 'name' => '_Token[fields]',
  1126. 'value' => $hash
  1127. )),
  1128. array('input' => array(
  1129. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1130. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1131. )),
  1132. '/div'
  1133. );
  1134. $this->assertTags($result, $expected);
  1135. }
  1136. /**
  1137. * Tests correct generation of number fields for double and float fields
  1138. *
  1139. * @return void
  1140. */
  1141. public function testTextFieldGenerationForFloats() {
  1142. $this->markTestIncomplete('Need to revisit once models work again.');
  1143. $this->article['schema'] = [
  1144. 'foo' => [
  1145. 'type' => 'float',
  1146. 'null' => false,
  1147. 'default' => null,
  1148. 'length' => 10
  1149. ]
  1150. ];
  1151. $this->Form->create('Contact');
  1152. $result = $this->Form->input('foo');
  1153. $expected = array(
  1154. 'div' => array('class' => 'input number'),
  1155. 'label' => array('for' => 'ContactFoo'),
  1156. 'Foo',
  1157. '/label',
  1158. array('input' => array(
  1159. 'type' => 'number',
  1160. 'name' => 'Contact[foo]',
  1161. 'id' => 'ContactFoo',
  1162. 'step' => 'any'
  1163. )),
  1164. '/div'
  1165. );
  1166. $this->assertTags($result, $expected);
  1167. $result = $this->Form->input('foo', array('step' => 0.5));
  1168. $expected = array(
  1169. 'div' => array('class' => 'input number'),
  1170. 'label' => array('for' => 'ContactFoo'),
  1171. 'Foo',
  1172. '/label',
  1173. array('input' => array(
  1174. 'type' => 'number',
  1175. 'name' => 'Contact[foo]',
  1176. 'id' => 'ContactFoo',
  1177. 'step' => '0.5'
  1178. )),
  1179. '/div'
  1180. );
  1181. $this->assertTags($result, $expected);
  1182. }
  1183. /**
  1184. * Tests correct generation of number fields for integer fields
  1185. *
  1186. * @return void
  1187. */
  1188. public function testTextFieldTypeNumberGenerationForIntegers() {
  1189. $this->markTestIncomplete('Need to revisit once models work again.');
  1190. $model->setSchema(array('foo' => array(
  1191. 'type' => 'integer',
  1192. 'null' => false,
  1193. 'default' => null,
  1194. 'length' => null
  1195. )));
  1196. $this->Form->create('Contact');
  1197. $result = $this->Form->input('foo');
  1198. $expected = array(
  1199. 'div' => array('class' => 'input number'),
  1200. 'label' => array('for' => 'ContactFoo'),
  1201. 'Foo',
  1202. '/label',
  1203. array('input' => array(
  1204. 'type' => 'number', 'name' => 'Contact[foo]',
  1205. 'id' => 'ContactFoo'
  1206. )),
  1207. '/div'
  1208. );
  1209. $this->assertTags($result, $expected);
  1210. }
  1211. /**
  1212. * Tests correct generation of file upload fields for binary fields
  1213. *
  1214. * @return void
  1215. */
  1216. public function testFileUploadFieldTypeGenerationForBinaries() {
  1217. $this->markTestIncomplete('Need to revisit once models work again.');
  1218. $model->setSchema(array('foo' => array(
  1219. 'type' => 'binary',
  1220. 'null' => false,
  1221. 'default' => null,
  1222. 'length' => 1024
  1223. )));
  1224. $this->Form->create('Contact');
  1225. $result = $this->Form->input('foo');
  1226. $expected = array(
  1227. 'div' => array('class' => 'input file'),
  1228. 'label' => array('for' => 'ContactFoo'),
  1229. 'Foo',
  1230. '/label',
  1231. array('input' => array(
  1232. 'type' => 'file', 'name' => 'data[Contact][foo]',
  1233. 'id' => 'ContactFoo'
  1234. )),
  1235. '/div'
  1236. );
  1237. $this->assertTags($result, $expected);
  1238. }
  1239. /**
  1240. * testFormSecurityMultipleFields method
  1241. *
  1242. * Test secure() with multiple row form. Ensure hash is correct.
  1243. *
  1244. * @return void
  1245. */
  1246. public function testFormSecurityMultipleFields() {
  1247. $this->Form->request->params['_Token'] = 'foo';
  1248. $fields = array(
  1249. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1250. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1251. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1252. );
  1253. $result = $this->Form->secure($fields);
  1254. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1255. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1256. $expected = array(
  1257. 'div' => array('style' => 'display:none;'),
  1258. array('input' => array(
  1259. 'type' => 'hidden', 'name' => '_Token[fields]',
  1260. 'value' => $hash
  1261. )),
  1262. array('input' => array(
  1263. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1264. 'value' => ''
  1265. )),
  1266. '/div'
  1267. );
  1268. $this->assertTags($result, $expected);
  1269. }
  1270. /**
  1271. * testFormSecurityMultipleSubmitButtons
  1272. *
  1273. * test form submit generation and ensure that _Token is only created on end()
  1274. *
  1275. * @return void
  1276. */
  1277. public function testFormSecurityMultipleSubmitButtons() {
  1278. $this->markTestIncomplete('Need to revisit once models work again.');
  1279. $key = 'testKey';
  1280. $this->Form->request->params['_csrfToken'] = $key;
  1281. $this->Form->create('Addresses');
  1282. $this->Form->input('Address.title');
  1283. $this->Form->input('Address.first_name');
  1284. $result = $this->Form->submit('Save', array('name' => 'save'));
  1285. $expected = array(
  1286. 'div' => array('class' => 'submit'),
  1287. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  1288. '/div',
  1289. );
  1290. $this->assertTags($result, $expected);
  1291. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  1292. $expected = array(
  1293. 'div' => array('class' => 'submit'),
  1294. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  1295. '/div',
  1296. );
  1297. $this->assertTags($result, $expected);
  1298. $result = $this->Form->end(null);
  1299. $expected = array(
  1300. 'div' => array('style' => 'display:none;'),
  1301. array('input' => array(
  1302. 'type' => 'hidden', 'name' => '_Token[fields]',
  1303. 'value' => 'preg:/.+/'
  1304. )),
  1305. array('input' => array(
  1306. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1307. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  1308. )),
  1309. '/div'
  1310. );
  1311. $this->assertTags($result, $expected);
  1312. }
  1313. /**
  1314. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1315. *
  1316. * @return void
  1317. */
  1318. public function testSecurityButtonNestedNamed() {
  1319. $key = 'testKey';
  1320. $this->Form->request->params['_csrfToken'] = $key;
  1321. $this->Form->create('Addresses');
  1322. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1323. $result = $this->Form->unlockField();
  1324. $this->assertEquals(array('Address.button'), $result);
  1325. }
  1326. /**
  1327. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1328. *
  1329. * @return void
  1330. */
  1331. public function testSecuritySubmitNestedNamed() {
  1332. $this->Form->request->params['_Token'] = 'testKey';
  1333. $this->Form->create($this->article);
  1334. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1335. $result = $this->Form->unlockField();
  1336. $this->assertEquals(array('Address.button'), $result);
  1337. }
  1338. /**
  1339. * Test that the correct fields are unlocked for image submits with no names.
  1340. *
  1341. * @return void
  1342. */
  1343. public function testSecuritySubmitImageNoName() {
  1344. $key = 'testKey';
  1345. $this->Form->request->params['_Token'] = 'testKey';
  1346. $this->Form->create(false);
  1347. $result = $this->Form->submit('save.png');
  1348. $expected = array(
  1349. 'div' => array('class' => 'submit'),
  1350. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  1351. '/div'
  1352. );
  1353. $this->assertTags($result, $expected);
  1354. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  1355. }
  1356. /**
  1357. * Test that the correct fields are unlocked for image submits with names.
  1358. *
  1359. * @return void
  1360. */
  1361. public function testSecuritySubmitImageName() {
  1362. $this->Form->request->params['_Token'] = 'testKey';
  1363. $this->Form->create(null);
  1364. $result = $this->Form->submit('save.png', array('name' => 'test'));
  1365. $expected = array(
  1366. 'div' => array('class' => 'submit'),
  1367. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  1368. '/div'
  1369. );
  1370. $this->assertTags($result, $expected);
  1371. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  1372. }
  1373. /**
  1374. * testFormSecurityMultipleInputFields method
  1375. *
  1376. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1377. *
  1378. * @return void
  1379. */
  1380. public function testFormSecurityMultipleInputFields() {
  1381. $this->markTestIncomplete('Need to revisit once models work again.');
  1382. $key = 'testKey';
  1383. $this->Form->request->params['_csrfToken'] = $key;
  1384. $this->Form->create('Addresses');
  1385. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1386. $this->Form->input('Addresses.0.title');
  1387. $this->Form->input('Addresses.0.first_name');
  1388. $this->Form->input('Addresses.0.last_name');
  1389. $this->Form->input('Addresses.0.address');
  1390. $this->Form->input('Addresses.0.city');
  1391. $this->Form->input('Addresses.0.phone');
  1392. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  1393. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1394. $this->Form->input('Addresses.1.title');
  1395. $this->Form->input('Addresses.1.first_name');
  1396. $this->Form->input('Addresses.1.last_name');
  1397. $this->Form->input('Addresses.1.address');
  1398. $this->Form->input('Addresses.1.city');
  1399. $this->Form->input('Addresses.1.phone');
  1400. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  1401. $result = $this->Form->secure($this->Form->fields);
  1402. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  1403. $expected = array(
  1404. 'div' => array('style' => 'display:none;'),
  1405. array('input' => array(
  1406. 'type' => 'hidden', 'name' => '_Token[fields]',
  1407. 'value' => $hash
  1408. )),
  1409. array('input' => array(
  1410. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1411. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1412. )),
  1413. '/div'
  1414. );
  1415. $this->assertTags($result, $expected);
  1416. }
  1417. /**
  1418. * Test form security with Model.field.0 style inputs
  1419. *
  1420. * @return void
  1421. */
  1422. public function testFormSecurityArrayFields() {
  1423. $this->markTestIncomplete('Need to revisit once models work again.');
  1424. $key = 'testKey';
  1425. $this->Form->request->params['_csrfToken'] = $key;
  1426. $this->Form->create('Address');
  1427. $this->Form->input('Address.primary.1');
  1428. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1429. $this->Form->input('Address.secondary.1.0');
  1430. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1431. }
  1432. /**
  1433. * testFormSecurityMultipleInputDisabledFields method
  1434. *
  1435. * test secure form generation with multiple records and disabled fields.
  1436. *
  1437. * @return void
  1438. */
  1439. public function testFormSecurityMultipleInputDisabledFields() {
  1440. $this->markTestIncomplete('Need to revisit once models work again.');
  1441. $key = 'testKey';
  1442. $this->Form->request->params['_csrfToken'] = $key;
  1443. $this->Form->request->params['_Token'] = array(
  1444. 'unlockedFields' => array('first_name', 'address')
  1445. );
  1446. $this->Form->create();
  1447. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1448. $this->Form->input('Addresses.0.title');
  1449. $this->Form->input('Addresses.0.first_name');
  1450. $this->Form->input('Addresses.0.last_name');
  1451. $this->Form->input('Addresses.0.address');
  1452. $this->Form->input('Addresses.0.city');
  1453. $this->Form->input('Addresses.0.phone');
  1454. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1455. $this->Form->input('Addresses.1.title');
  1456. $this->Form->input('Addresses.1.first_name');
  1457. $this->Form->input('Addresses.1.last_name');
  1458. $this->Form->input('Addresses.1.address');
  1459. $this->Form->input('Addresses.1.city');
  1460. $this->Form->input('Addresses.1.phone');
  1461. $result = $this->Form->secure($this->Form->fields);
  1462. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1463. $expected = array(
  1464. 'div' => array('style' => 'display:none;'),
  1465. array('input' => array(
  1466. 'type' => 'hidden', 'name' => '_Token[fields]',
  1467. 'value' => $hash
  1468. )),
  1469. array('input' => array(
  1470. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1471. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1472. )),
  1473. '/div'
  1474. );
  1475. $this->assertTags($result, $expected);
  1476. }
  1477. /**
  1478. * testFormSecurityInputDisabledFields method
  1479. *
  1480. * Test single record form with disabled fields.
  1481. *
  1482. * @return void
  1483. */
  1484. public function testFormSecurityInputUnlockedFields() {
  1485. $this->markTestIncomplete('Need to revisit once models work again.');
  1486. $key = 'testKey';
  1487. $this->Form->request->params['_csrfToken'] = $key;
  1488. $this->Form->request['_Token'] = array(
  1489. 'unlockedFields' => array('first_name', 'address')
  1490. );
  1491. $this->Form->create();
  1492. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1493. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1494. $this->Form->input('Addresses.title');
  1495. $this->Form->input('Addresses.first_name');
  1496. $this->Form->input('Addresses.last_name');
  1497. $this->Form->input('Addresses.address');
  1498. $this->Form->input('Addresses.city');
  1499. $this->Form->input('Addresses.phone');
  1500. $result = $this->Form->fields;
  1501. $expected = array(
  1502. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1503. 'Addresses.city', 'Addresses.phone'
  1504. );
  1505. $this->assertEquals($expected, $result);
  1506. $result = $this->Form->secure($expected);
  1507. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1508. $expected = array(
  1509. 'div' => array('style' => 'display:none;'),
  1510. array('input' => array(
  1511. 'type' => 'hidden', 'name' => '_Token[fields]',
  1512. 'value' => $hash
  1513. )),
  1514. array('input' => array(
  1515. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1516. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1517. )),
  1518. '/div'
  1519. );
  1520. $this->assertTags($result, $expected);
  1521. }
  1522. /**
  1523. * test securing inputs with custom name attributes.
  1524. *
  1525. * @return void
  1526. */
  1527. public function testFormSecureWithCustomNameAttribute() {
  1528. $this->Form->request->params['_Token'] = 'testKey';
  1529. $this->Form->text('UserForm.published', array('name' => 'User[custom]'));
  1530. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1531. $this->Form->text('UserForm.published', array('name' => 'User[custom][another][value]'));
  1532. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1533. }
  1534. /**
  1535. * testFormSecuredInput method
  1536. *
  1537. * Test generation of entire secure form, assertions made on input() output.
  1538. *
  1539. * @return void
  1540. */
  1541. public function testFormSecuredInput() {
  1542. $this->markTestIncomplete('Need to revisit once models work again.');
  1543. $this->Form->request->params['_csrfToken'] = 'testKey';
  1544. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1545. $encoding = strtolower(Configure::read('App.encoding'));
  1546. $expected = array(
  1547. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1548. 'div' => array('style' => 'display:none;'),
  1549. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1550. array('input' => array(
  1551. 'type' => 'hidden', 'name' => '_csrfToken',
  1552. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1553. )),
  1554. '/div'
  1555. );
  1556. $this->assertTags($result, $expected);
  1557. $result = $this->Form->input('ValidateUser.ratio');
  1558. $expected = array(
  1559. 'div' => array('class'),
  1560. 'label' => array('for'),
  1561. 'Ratio',
  1562. '/label',
  1563. 'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
  1564. '/div',
  1565. );
  1566. $this->assertTags($result, $expected);
  1567. $result = $this->Form->input('ValidateUser.population');
  1568. $expected = array(
  1569. 'div' => array('class'),
  1570. 'label' => array('for'),
  1571. 'Population',
  1572. '/label',
  1573. 'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
  1574. '/div',
  1575. );
  1576. $this->assertTags($result, $expected);
  1577. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1578. $expected = array(
  1579. 'div' => array('class' => 'input text'),
  1580. 'label' => array('for' => 'UserFormPublished'),
  1581. 'Published',
  1582. '/label',
  1583. array('input' => array(
  1584. 'type' => 'text', 'name' => 'UserForm[published]',
  1585. 'id' => 'UserFormPublished'
  1586. )),
  1587. '/div'
  1588. );
  1589. $this->assertTags($result, $expected);
  1590. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1591. $expected = array(
  1592. 'div' => array('class' => 'input text'),
  1593. 'label' => array('for' => 'UserFormOther'),
  1594. 'Other',
  1595. '/label',
  1596. array('input' => array(
  1597. 'type' => 'text', 'name' => 'UserForm[other]',
  1598. 'id' => 'UserFormOther'
  1599. )),
  1600. '/div'
  1601. );
  1602. $this->assertTags($result, $expected);
  1603. $result = $this->Form->hidden('UserForm.stuff');
  1604. $expected = array(
  1605. 'input' => array(
  1606. 'type' => 'hidden', 'name' => 'UserForm[stuff]',
  1607. 'id' => 'UserFormStuff'
  1608. ));
  1609. $this->assertTags($result, $expected);
  1610. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1611. $expected = array('input' => array(
  1612. 'type' => 'hidden', 'name' => 'UserForm[hidden]',
  1613. 'value' => '0', 'id' => 'UserFormHidden'
  1614. ));
  1615. $this->assertTags($result, $expected);
  1616. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1617. $expected = array(
  1618. 'div' => array('class' => 'input checkbox'),
  1619. array('input' => array(
  1620. 'type' => 'hidden', 'name' => 'UserForm[something]',
  1621. 'value' => '0', 'id' => 'UserFormSomething_'
  1622. )),
  1623. array('input' => array(
  1624. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  1625. 'value' => '1', 'id' => 'UserFormSomething'
  1626. )),
  1627. 'label' => array('for' => 'UserFormSomething'),
  1628. 'Something',
  1629. '/label',
  1630. '/div'
  1631. );
  1632. $this->assertTags($result, $expected);
  1633. $result = $this->Form->fields;
  1634. $expected = array(
  1635. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1636. 'UserForm.hidden' => '0', 'UserForm.something'
  1637. );
  1638. $this->assertEquals($expected, $result);
  1639. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1640. $result = $this->Form->secure($this->Form->fields);
  1641. $expected = array(
  1642. 'div' => array('style' => 'display:none;'),
  1643. array('input' => array(
  1644. 'type' => 'hidden', 'name' => '_Token[fields]',
  1645. 'value' => $hash
  1646. )),
  1647. array('input' => array(
  1648. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1649. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1650. )),
  1651. '/div'
  1652. );
  1653. $this->assertTags($result, $expected);
  1654. }
  1655. /**
  1656. * Test secured inputs with custom names.
  1657. *
  1658. * @return void
  1659. */
  1660. public function testSecuredInputCustomName() {
  1661. $this->markTestIncomplete('Need to revisit once models work again.');
  1662. $this->Form->request->params['_csrfToken'] = 'testKey';
  1663. $this->assertEquals(array(), $this->Form->fields);
  1664. $this->Form->input('text_input', array(
  1665. 'name' => 'data[Option][General.default_role]',
  1666. ));
  1667. $expected = array('Option.General.default_role');
  1668. $this->assertEquals($expected, $this->Form->fields);
  1669. $this->Form->input('select_box', array(
  1670. 'name' => 'data[Option][General.select_role]',
  1671. 'type' => 'select',
  1672. 'options' => array(1, 2),
  1673. ));
  1674. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1675. $this->assertEquals($expected, $this->Form->fields);
  1676. }
  1677. /**
  1678. * Tests that the correct keys are added to the field hash index
  1679. *
  1680. * @return void
  1681. */
  1682. public function testFormSecuredFileInput() {
  1683. $this->assertEquals(array(), $this->Form->fields);
  1684. $this->Form->file('Attachment.file');
  1685. $expected = array(
  1686. 'Attachment.file.name', 'Attachment.file.type',
  1687. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1688. 'Attachment.file.size'
  1689. );
  1690. $this->assertEquals($expected, $this->Form->fields);
  1691. }
  1692. /**
  1693. * test that multiple selects keys are added to field hash
  1694. *
  1695. * @return void
  1696. */
  1697. public function testFormSecuredMultipleSelect() {
  1698. $this->Form->request->params['_csrfToken'] = 'testKey';
  1699. $this->assertEquals(array(), $this->Form->fields);
  1700. $options = array('1' => 'one', '2' => 'two');
  1701. $this->Form->select('Model.select', $options);
  1702. $expected = array('Model.select');
  1703. $this->assertEquals($expected, $this->Form->fields);
  1704. $this->Form->fields = array();
  1705. $this->Form->select('Model.select', $options, array('multiple' => true));
  1706. $this->assertEquals($expected, $this->Form->fields);
  1707. }
  1708. /**
  1709. * testFormSecuredRadio method
  1710. *
  1711. * @return void
  1712. */
  1713. public function testFormSecuredRadio() {
  1714. $this->Form->request->params['_Token'] = 'testKey';
  1715. $this->assertEquals(array(), $this->Form->fields);
  1716. $options = array('1' => 'option1', '2' => 'option2');
  1717. $this->Form->radio('Test.test', $options);
  1718. $expected = array('Test.test');
  1719. $this->assertEquals($expected, $this->Form->fields);
  1720. }
  1721. /**
  1722. * Test that when disabled is in a list based attribute array it works.
  1723. *
  1724. * @return void
  1725. */
  1726. public function testFormSecuredAndDisabledNotAssoc() {
  1727. $this->Form->request->params['_Token'] = 'testKey';
  1728. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1729. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1730. $this->Form->text('Model.text', array('disabled'));
  1731. $this->Form->textarea('Model.textarea', array('disabled'));
  1732. $this->Form->password('Model.password', array('disabled'));
  1733. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1734. $expected = array(
  1735. 'Model.radio' => ''
  1736. );
  1737. $this->assertEquals($expected, $this->Form->fields);
  1738. }
  1739. /**
  1740. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1741. * hashing.
  1742. *
  1743. * @return void
  1744. */
  1745. public function testFormSecuredAndDisabled() {
  1746. $this->Form->request->params['_Token'] = 'testKey';
  1747. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1748. $this->Form->text('Model.text', array('disabled' => true));
  1749. $this->Form->password('Model.text', array('disabled' => true));
  1750. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1751. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1752. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1753. $this->Form->year('Model.year', array('disabled' => true));
  1754. $this->Form->month('Model.month', array('disabled' => true));
  1755. $this->Form->day('Model.day', array('disabled' => true));
  1756. $this->Form->hour('Model.hour', array('disabled' => true));
  1757. $this->Form->minute('Model.minute', array('disabled' => true));
  1758. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1759. $expected = array(
  1760. 'Model.radio' => ''
  1761. );
  1762. $this->assertEquals($expected, $this->Form->fields);
  1763. }
  1764. /**
  1765. * testDisableSecurityUsingForm method
  1766. *
  1767. * @return void
  1768. */
  1769. public function testDisableSecurityUsingForm() {
  1770. $this->markTestIncomplete('Need to revisit once models work again.');
  1771. $this->Form->request->params['_csrfToken'] = 'testKey';
  1772. $this->Form->request['_Token'] = array(
  1773. 'disabledFields' => array()
  1774. );
  1775. $this->Form->create();
  1776. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1777. $this->Form->input('Addresses.title');
  1778. $this->Form->input('Addresses.first_name', array('secure' => false));
  1779. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1780. $this->Form->input('Addresses.zip', array(
  1781. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1782. ));
  1783. $result = $this->Form->fields;
  1784. $expected = array(
  1785. 'Addresses.id' => '123456', 'Addresses.title',
  1786. );
  1787. $this->assertEquals($expected, $result);
  1788. }
  1789. /**
  1790. * test disableField
  1791. *
  1792. * @return void
  1793. */
  1794. public function testUnlockFieldAddsToList() {
  1795. $this->Form->request['_Token'] = array(
  1796. 'unlockedFields' => array()
  1797. );
  1798. $this->Form->unlockField('Contact.name');
  1799. $this->Form->text('Contact.name');
  1800. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1801. $this->assertEquals(array(), $this->Form->fields);
  1802. }
  1803. /**
  1804. * test unlockField removing from fields array.
  1805. *
  1806. * @return void
  1807. */
  1808. public function testUnlockFieldRemovingFromFields() {
  1809. $this->Form->request['_Token'] = array(
  1810. 'unlockedFields' => array()
  1811. );
  1812. $this->Form->create($this->article);
  1813. $this->Form->hidden('Article.id', array('value' => 1));
  1814. $this->Form->text('Article.title');
  1815. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  1816. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  1817. $this->Form->unlockField('Article.title');
  1818. $this->Form->unlockField('Article.id');
  1819. $this->assertEquals(array(), $this->Form->fields);
  1820. }
  1821. /**
  1822. * Test validation errors.
  1823. *
  1824. * @return void
  1825. */
  1826. public function testPasswordValidation() {
  1827. $this->markTestIncomplete('Need to revisit once models work again.');
  1828. $Contact->validationErrors['password'] = array('Please provide a password');
  1829. $result = $this->Form->input('Contact.password');
  1830. $expected = array(
  1831. 'div' => array('class' => 'input password error'),
  1832. 'label' => array('for' => 'ContactPassword'),
  1833. 'Password',
  1834. '/label',
  1835. 'input' => array(
  1836. 'type' => 'password', 'name' => 'Contact[password]',
  1837. 'id' => 'ContactPassword', 'class' => 'form-error'
  1838. ),
  1839. array('div' => array('class' => 'error-message')),
  1840. 'Please provide a password',
  1841. '/div',
  1842. '/div'
  1843. );
  1844. $this->assertTags($result, $expected);
  1845. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1846. $expected = array(
  1847. 'div' => array('class' => 'input password error'),
  1848. 'label' => array('for' => 'ContactPassword'),
  1849. 'Password',
  1850. '/label',
  1851. 'input' => array(
  1852. 'type' => 'password', 'name' => 'Contact[password]',
  1853. 'id' => 'ContactPassword', 'class' => 'form-error'
  1854. ),
  1855. '/div'
  1856. );
  1857. $this->assertTags($result, $expected);
  1858. }
  1859. /**
  1860. * Test validation errors, when validation message is an empty string.
  1861. *
  1862. * @return void
  1863. */
  1864. public function testEmptyErrorValidation() {
  1865. $this->markTestIncomplete('Need to revisit once models work again.');
  1866. $this->Form->validationErrors['Contact']['password'] = '';
  1867. $result = $this->Form->input('Contact.password');
  1868. $expected = array(
  1869. 'div' => array('class' => 'input password error'),
  1870. 'label' => array('for' => 'ContactPassword'),
  1871. 'Password',
  1872. '/label',
  1873. 'input' => array(
  1874. 'type' => 'password', 'name' => 'Contact[password]',
  1875. 'id' => 'ContactPassword', 'class' => 'form-error'
  1876. ),
  1877. array('div' => array('class' => 'error-message')),
  1878. array(),
  1879. '/div',
  1880. '/div'
  1881. );
  1882. $this->assertTags($result, $expected);
  1883. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1884. $expected = array(
  1885. 'div' => array('class' => 'input password error'),
  1886. 'label' => array('for' => 'ContactPassword'),
  1887. 'Password',
  1888. '/label',
  1889. 'input' => array(
  1890. 'type' => 'password', 'name' => 'Contact[password]',
  1891. 'id' => 'ContactPassword', 'class' => 'form-error'
  1892. ),
  1893. '/div'
  1894. );
  1895. $this->assertTags($result, $expected);
  1896. }
  1897. /**
  1898. * Test validation errors, when calling input() overriding validation message by an empty string.
  1899. *
  1900. * @return void
  1901. */
  1902. public function testEmptyInputErrorValidation() {
  1903. $this->markTestIncomplete('Need to revisit once models work again.');
  1904. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1905. $result = $this->Form->input('Contact.password', array('error' => ''));
  1906. $expected = array(
  1907. 'div' => array('class' => 'input password error'),
  1908. 'label' => array('for' => 'ContactPassword'),
  1909. 'Password',
  1910. '/label',
  1911. 'input' => array(
  1912. 'type' => 'password', 'name' => 'Contact[password]',
  1913. 'id' => 'ContactPassword', 'class' => 'form-error'
  1914. ),
  1915. array('div' => array('class' => 'error-message')),
  1916. array(),
  1917. '/div',
  1918. '/div'
  1919. );
  1920. $this->assertTags($result, $expected);
  1921. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1922. $expected = array(
  1923. 'div' => array('class' => 'input password error'),
  1924. 'label' => array('for' => 'ContactPassword'),
  1925. 'Password',
  1926. '/label',
  1927. 'input' => array(
  1928. 'type' => 'password', 'name' => 'Contact[password]',
  1929. 'id' => 'ContactPassword', 'class' => 'form-error'
  1930. ),
  1931. '/div'
  1932. );
  1933. $this->assertTags($result, $expected);
  1934. }
  1935. /**
  1936. * testFormValidationAssociated method
  1937. *
  1938. * test display of form errors in conjunction with model::validates.
  1939. *
  1940. * @return void
  1941. */
  1942. public function testFormValidationAssociated() {
  1943. $this->markTestIncomplete('Need to revisit once models work again.');
  1944. $data = array(
  1945. 'UserForm' => array('name' => 'user'),
  1946. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1947. );
  1948. $result = $this->UserForm->OpenidUrl->create($data);
  1949. $this->assertFalse(empty($result));
  1950. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1951. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1952. $encoding = strtolower(Configure::read('App.encoding'));
  1953. $expected = array(
  1954. 'form' => array(
  1955. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1956. 'accept-charset' => $encoding
  1957. ),
  1958. 'div' => array('style' => 'display:none;'),
  1959. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1960. '/div'
  1961. );
  1962. $this->assertTags($result, $expected);
  1963. $result = $this->Form->error(
  1964. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1965. );
  1966. $this->assertEquals('Error, not registered', $result);
  1967. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1968. }
  1969. /**
  1970. * testFormValidationAssociatedFirstLevel method
  1971. *
  1972. * test form error display with associated model.
  1973. *
  1974. * @return void
  1975. */
  1976. public function testFormValidationAssociatedFirstLevel() {
  1977. $this->markTestIncomplete('Need to revisit once models work again.');
  1978. $data = array(
  1979. 'ValidateUser' => array('name' => 'mariano'),
  1980. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1981. );
  1982. $result = $this->ValidateUser->create($data);
  1983. $this->assertFalse(empty($result));
  1984. $this->assertFalse($this->ValidateUser->validates());
  1985. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1986. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1987. $encoding = strtolower(Configure::read('App.encoding'));
  1988. $expected = array(
  1989. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1990. 'div' => array('style' => 'display:none;'),
  1991. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1992. '/div'
  1993. );
  1994. $this->assertTags($result, $expected);
  1995. $result = $this->Form->error(
  1996. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1997. );
  1998. $this->assertEquals('Invalid email', $result);
  1999. $result = $this->Form->error(
  2000. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  2001. );
  2002. $this->assertEquals('Invalid name', $result);
  2003. $result = $this->Form->error(
  2004. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  2005. );
  2006. $this->assertEquals('Invalid city', $result);
  2007. unset($this->ValidateUser->ValidateProfile);
  2008. unset($this->ValidateUser);
  2009. }
  2010. /**
  2011. * testFormValidationAssociatedSecondLevel method
  2012. *
  2013. * test form error display with associated model.
  2014. *
  2015. * @return void
  2016. */
  2017. public function testFormValidationAssociatedSecondLevel() {
  2018. $this->markTestIncomplete('Need to revisit once models work again.');
  2019. $data = array(
  2020. 'ValidateUser' => array('name' => 'mariano'),
  2021. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  2022. 'ValidateItem' => array('name' => 'Item')
  2023. );
  2024. $result = $this->ValidateUser->create($data);
  2025. $this->assertFalse(empty($result));
  2026. $this->assertFalse($this->ValidateUser->validates());
  2027. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  2028. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  2029. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  2030. $encoding = strtolower(Configure::read('App.encoding'));
  2031. $expected = array(
  2032. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  2033. 'div' => array('style' => 'display:none;'),
  2034. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  2035. '/div'
  2036. );
  2037. $this->assertTags($result, $expected);
  2038. $result = $this->Form->error(
  2039. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  2040. );
  2041. $this->assertEquals('Invalid email', $result);
  2042. $result = $this->Form->error(
  2043. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  2044. );
  2045. $this->assertEquals('Invalid name', $result);
  2046. $result = $this->Form->error(
  2047. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  2048. );
  2049. $result = $this->Form->error(
  2050. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  2051. );
  2052. $this->assertEquals('Invalid description', $result);
  2053. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  2054. unset($this->ValidateUser->ValidateProfile);
  2055. unset($this->ValidateUser);
  2056. }
  2057. /**
  2058. * testFormValidationMultiRecord method
  2059. *
  2060. * test form error display with multiple records.
  2061. *
  2062. * @return void
  2063. */
  2064. public function testFormValidationMultiRecord() {
  2065. $this->markTestIncomplete('Need to revisit once models work again.');
  2066. $Contact->validationErrors[2] = array(
  2067. 'name' => array('The provided value is invalid')
  2068. );
  2069. $result = $this->Form->input('Contact.2.name');
  2070. $expected = array(
  2071. 'div' => array('class' => 'input text error'),
  2072. 'label' => array('for' => 'Contact2Name'),
  2073. 'Name',
  2074. '/label',
  2075. 'input' => array(
  2076. 'type' => 'text', 'name' => 'Contact[2][name]', 'id' => 'Contact2Name',
  2077. 'class' => 'form-error', 'maxlength' => 255
  2078. ),
  2079. array('div' => array('class' => 'error-message')),
  2080. 'The provided value is invalid',
  2081. '/div',
  2082. '/div'
  2083. );
  2084. $this->assertTags($result, $expected);
  2085. }
  2086. /**
  2087. * testMultipleInputValidation method
  2088. *
  2089. * test multiple record form validation error display.
  2090. *
  2091. * @return void
  2092. */
  2093. public function testMultipleInputValidation() {
  2094. $this->markTestIncomplete('Need to revisit once models work again.');
  2095. $Address->validationErrors[0] = array(
  2096. 'title' => array('This field cannot be empty'),
  2097. 'first_name' => array('This field cannot be empty')
  2098. );
  2099. $Address->validationErrors[1] = array(
  2100. 'last_name' => array('You must have a last name')
  2101. );
  2102. $this->Form->create();
  2103. $result = $this->Form->input('Address.0.title');
  2104. $expected = array(
  2105. 'div' => array('class'),
  2106. 'label' => array('for'),
  2107. 'preg:/[^<]+/',
  2108. '/label',
  2109. 'input' => array(
  2110. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  2111. ),
  2112. array('div' => array('class' => 'error-message')),
  2113. 'This field cannot be empty',
  2114. '/div',
  2115. '/div'
  2116. );
  2117. $this->assertTags($result, $expected);
  2118. $result = $this->Form->input('Address.0.first_name');
  2119. $expected = array(
  2120. 'div' => array('class'),
  2121. 'label' => array('for'),
  2122. 'preg:/[^<]+/',
  2123. '/label',
  2124. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  2125. array('div' => array('class' => 'error-message')),
  2126. 'This field cannot be empty',
  2127. '/div',
  2128. '/div'
  2129. );
  2130. $this->assertTags($result, $expected);
  2131. $result = $this->Form->input('Address.0.last_name');
  2132. $expected = array(
  2133. 'div' => array('class'),
  2134. 'label' => array('for'),
  2135. 'preg:/[^<]+/',
  2136. '/label',
  2137. 'input' => array('type' => 'text', 'name', 'id'),
  2138. '/div'
  2139. );
  2140. $this->assertTags($result, $expected);
  2141. $result = $this->Form->input('Address.1.last_name');
  2142. $expected = array(
  2143. 'div' => array('class'),
  2144. 'label' => array('for'),
  2145. 'preg:/[^<]+/',
  2146. '/label',
  2147. 'input' => array(
  2148. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  2149. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  2150. ),
  2151. array('div' => array('class' => 'error-message')),
  2152. 'You must have a last name',
  2153. '/div',
  2154. '/div'
  2155. );
  2156. $this->assertTags($result, $expected);
  2157. }
  2158. /**
  2159. * testInput method
  2160. *
  2161. * Test various incarnations of input().
  2162. *
  2163. * @return void
  2164. */
  2165. public function testInput() {
  2166. $this->markTestIncomplete('Need to revisit once models work again.');
  2167. $result = $this->Form->input('ValidateUser.balance');
  2168. $expected = array(
  2169. 'div' => array('class'),
  2170. 'label' => array('for'),
  2171. 'Balance',
  2172. '/label',
  2173. 'input' => array('name', 'type' => 'number', 'id'),
  2174. '/div',
  2175. );
  2176. $this->assertTags($result, $expected);
  2177. $result = $this->Form->input('ValidateUser.cost_decimal');
  2178. $expected = array(
  2179. 'div' => array('class'),
  2180. 'label' => array('for'),
  2181. 'Cost Decimal',
  2182. '/label',
  2183. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  2184. '/div',
  2185. );
  2186. $this->assertTags($result, $expected);
  2187. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  2188. $expected = array(
  2189. 'div' => array('class' => 'input email'),
  2190. 'label' => array('for' => 'custom'),
  2191. 'Email',
  2192. '/label',
  2193. array('input' => array(
  2194. 'type' => 'email', 'name' => 'Contact[email]',
  2195. 'id' => 'custom', 'maxlength' => 255
  2196. )),
  2197. '/div'
  2198. );
  2199. $this->assertTags($result, $expected);
  2200. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  2201. $expected = array(
  2202. '<div',
  2203. 'label' => array('for' => 'ContactEmail'),
  2204. 'Email',
  2205. '/label',
  2206. array('input' => array(
  2207. 'type' => 'email', 'name' => 'Contact[email]',
  2208. 'id' => 'ContactEmail', 'maxlength' => 255
  2209. )),
  2210. '/div'
  2211. );
  2212. $this->assertTags($result, $expected);
  2213. $result = $this->Form->hidden('Contact.idontexist');
  2214. $expected = array('input' => array(
  2215. 'type' => 'hidden', 'name' => 'Contact[idontexist]',
  2216. 'id' => 'ContactIdontexist'
  2217. ));
  2218. $this->assertTags($result, $expected);
  2219. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  2220. $expected = array(
  2221. 'div' => array('class' => 'input text'),
  2222. 'label' => array('for' => 'ContactEmail'),
  2223. 'Email',
  2224. '/label',
  2225. array('input' => array(
  2226. 'type' => 'text', 'name' => 'Contact[email]',
  2227. 'id' => 'ContactEmail'
  2228. )),
  2229. '/div'
  2230. );
  2231. $this->assertTags($result, $expected);
  2232. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  2233. $expected = array(
  2234. 'div' => array('class' => 'input text'),
  2235. 'label' => array('for' => 'Contact5Email'),
  2236. 'Email',
  2237. '/label',
  2238. array('input' => array(
  2239. 'type' => 'text', 'name' => 'Contact[5][email]',
  2240. 'id' => 'Contact5Email'
  2241. )),
  2242. '/div'
  2243. );
  2244. $this->assertTags($result, $expected);
  2245. $result = $this->Form->input('Contact.password');
  2246. $expected = array(
  2247. 'div' => array('class' => 'input password'),
  2248. 'label' => array('for' => 'ContactPassword'),
  2249. 'Password',
  2250. '/label',
  2251. array('input' => array(
  2252. 'type' => 'password', 'name' => 'Contact[password]',
  2253. 'id' => 'ContactPassword'
  2254. )),
  2255. '/div'
  2256. );
  2257. $this->assertTags($result, $expected);
  2258. $result = $this->Form->input('Contact.email', array(
  2259. 'type' => 'file', 'class' => 'textbox'
  2260. ));
  2261. $expected = array(
  2262. 'div' => array('class' => 'input file'),
  2263. 'label' => array('for' => 'ContactEmail'),
  2264. 'Email',
  2265. '/label',
  2266. array('input' => array(
  2267. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2268. 'id' => 'ContactEmail'
  2269. )),
  2270. '/div'
  2271. );
  2272. $this->assertTags($result, $expected);
  2273. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  2274. $result = $this->Form->input('Contact.phone');
  2275. $expected = array(
  2276. 'div' => array('class' => 'input tel'),
  2277. 'label' => array('for' => 'ContactPhone'),
  2278. 'Phone',
  2279. '/label',
  2280. array('input' => array(
  2281. 'type' => 'tel', 'name' => 'Contact[phone]',
  2282. 'value' => 'Hello &amp; World &gt; weird chars',
  2283. 'id' => 'ContactPhone', 'maxlength' => 255
  2284. )),
  2285. '/div'
  2286. );
  2287. $this->assertTags($result, $expected);
  2288. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2289. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  2290. $expected = array(
  2291. 'div' => array('class' => 'input text'),
  2292. 'label' => array('for' => 'myId'),
  2293. 'Field',
  2294. '/label',
  2295. 'input' => array(
  2296. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2297. 'value' => 'My value', 'id' => 'myId'
  2298. ),
  2299. '/div'
  2300. );
  2301. $this->assertTags($result, $expected);
  2302. unset($this->Form->request->data);
  2303. $Contact->validationErrors['field'] = array('Badness!');
  2304. $result = $this->Form->input('Contact.field');
  2305. $expected = array(
  2306. 'div' => array('class' => 'input text error'),
  2307. 'label' => array('for' => 'ContactField'),
  2308. 'Field',
  2309. '/label',
  2310. 'input' => array(
  2311. 'type' => 'text', 'name' => 'Contact[field]',
  2312. 'id' => 'ContactField', 'class' => 'form-error'
  2313. ),
  2314. array('div' => array('class' => 'error-message')),
  2315. 'Badness!',
  2316. '/div',
  2317. '/div'
  2318. );
  2319. $this->assertTags($result, $expected);
  2320. $result = $this->Form->input('Contact.field', array(
  2321. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  2322. ));
  2323. $expected = array(
  2324. 'label' => array('for' => 'ContactField'),
  2325. 'Field',
  2326. '/label',
  2327. 'input' => array(
  2328. 'type' => 'text', 'name' => 'Contact[field]',
  2329. 'id' => 'ContactField', 'class' => 'form-error'
  2330. ),
  2331. array('span' => array('class' => 'error-message')),
  2332. 'Badness!',
  2333. '/span'
  2334. );
  2335. $this->assertTags($result, $expected);
  2336. $result = $this->Form->input('Contact.field', array(
  2337. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  2338. ));
  2339. $expected = array(
  2340. 'div' => array('class' => 'input text error'),
  2341. 'label' => array('for' => 'ContactField'),
  2342. 'Field',
  2343. '/label',
  2344. 'input' => array(
  2345. 'type' => 'text', 'name' => 'Contact[field]',
  2346. 'id' => 'ContactField', 'class' => 'form-error'
  2347. ),
  2348. array('div' => array('class' => 'error')),
  2349. 'Badness!',
  2350. '/div'
  2351. );
  2352. $this->assertTags($result, $expected);
  2353. $result = $this->Form->input('Contact.field', array(
  2354. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  2355. ));
  2356. $expected = array(
  2357. 'span' => array('class' => 'input text error'),
  2358. 'label' => array('for' => 'ContactField'),
  2359. 'Field',
  2360. '/label',
  2361. 'input' => array(
  2362. 'type' => 'text', 'name' => 'Contact[field]',
  2363. 'id' => 'ContactField', 'class' => 'form-error'
  2364. ),
  2365. 'Badness!',
  2366. '/span'
  2367. );
  2368. $this->assertTags($result, $expected);
  2369. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  2370. $expected = array(
  2371. 'div' => array('class' => 'input text error'),
  2372. 'label' => array('for' => 'ContactField'),
  2373. 'Field',
  2374. '/label',
  2375. 'input' => array(
  2376. 'type' => 'text', 'name' => 'Contact[field]',
  2377. 'id' => 'ContactField', 'class' => 'form-error'
  2378. ),
  2379. 'A message to you, Rudy',
  2380. array('div' => array('class' => 'error-message')),
  2381. 'Badness!',
  2382. '/div',
  2383. '/div'
  2384. );
  2385. $this->assertTags($result, $expected);
  2386. $this->Form->setEntity(null);
  2387. $this->Form->setEntity('Contact.field');
  2388. $result = $this->Form->input('Contact.field', array(
  2389. 'after' => 'A message to you, Rudy', 'error' => false
  2390. ));
  2391. $expected = array(
  2392. 'div' => array('class' => 'input text'),
  2393. 'label' => array('for' => 'ContactField'),
  2394. 'Field',
  2395. '/label',
  2396. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  2397. 'A message to you, Rudy',
  2398. '/div'
  2399. );
  2400. $this->assertTags($result, $expected);
  2401. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  2402. $expected = array(
  2403. 'div' => array('class' => 'input text'),
  2404. 'label' => array('for' => 'ObjectField'),
  2405. 'Field',
  2406. '/label',
  2407. 'input' => array('type' => 'text', 'name' => 'Object[field]', 'id' => 'ObjectField'),
  2408. 'A message to you, Rudy',
  2409. '/div'
  2410. );
  2411. $this->assertTags($result, $expected);
  2412. $Contact->validationErrors['field'] = array('minLength');
  2413. $result = $this->Form->input('Contact.field', array(
  2414. 'error' => array(
  2415. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2416. 'maxLength' => 'login too large'
  2417. )
  2418. ));
  2419. $expected = array(
  2420. 'div' => array('class' => 'input text error'),
  2421. 'label' => array('for' => 'ContactField'),
  2422. 'Field',
  2423. '/label',
  2424. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  2425. array('div' => array('class' => 'error-message')),
  2426. 'Le login doit contenir au moins 2 caractères',
  2427. '/div',
  2428. '/div'
  2429. );
  2430. $this->assertTags($result, $expected);
  2431. $Contact->validationErrors['field'] = array('maxLength');
  2432. $result = $this->Form->input('Contact.field', array(
  2433. 'error' => array(
  2434. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  2435. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2436. 'maxLength' => 'login too large',
  2437. )
  2438. ));
  2439. $expected = array(
  2440. 'div' => array('class' => 'input text error'),
  2441. 'label' => array('for' => 'ContactField'),
  2442. 'Field',
  2443. '/label',
  2444. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  2445. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  2446. 'login too large',
  2447. '/span',
  2448. '/div'
  2449. );
  2450. $this->assertTags($result, $expected);
  2451. }
  2452. /**
  2453. * Test that inputs with 0 can be created.
  2454. *
  2455. * @return void
  2456. */
  2457. public function testInputZero() {
  2458. $this->markTestIncomplete('Need to revisit once models work again.');
  2459. $this->Form->create('User');
  2460. $result = $this->Form->input('0');
  2461. $expected = array(
  2462. 'div' => array('class' => 'input text'),
  2463. 'label' => array('for' => 'User0'), '/label',
  2464. 'input' => array('type' => 'text', 'name' => 'User[0]', 'id' => 'User0'),
  2465. '/div'
  2466. );
  2467. $this->assertTags($result, $expected);
  2468. }
  2469. /**
  2470. * test input() with checkbox creation
  2471. *
  2472. * @return void
  2473. */
  2474. public function testInputCheckbox() {
  2475. $this->markTestIncomplete('Need to revisit once models work again.');
  2476. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  2477. $expected = array(
  2478. 'div' => array('class' => 'input checkbox'),
  2479. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  2480. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2481. '/div'
  2482. );
  2483. $this->assertTags($result, $expected);
  2484. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2485. $expected = array(
  2486. 'div' => array('class' => 'input checkbox'),
  2487. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  2488. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2489. '/div'
  2490. );
  2491. $this->assertTags($result, $expected);
  2492. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2493. $expected = array(
  2494. 'div' => array('class' => 'input checkbox'),
  2495. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  2496. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2497. '/div'
  2498. );
  2499. $this->assertTags($result, $expected);
  2500. $result = $this->Form->input('User.disabled', array(
  2501. 'label' => 'Disabled',
  2502. 'type' => 'checkbox',
  2503. 'data-foo' => 'disabled'
  2504. ));
  2505. $expected = array(
  2506. 'div' => array('class' => 'input checkbox'),
  2507. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  2508. array('input' => array(
  2509. 'type' => 'checkbox',
  2510. 'name' => 'data[User][disabled]',
  2511. 'value' => '1',
  2512. 'id' => 'UserDisabled',
  2513. 'data-foo' => 'disabled'
  2514. )),
  2515. 'label' => array('for' => 'UserDisabled'),
  2516. 'Disabled',
  2517. '/label',
  2518. '/div'
  2519. );
  2520. $this->assertTags($result, $expected);
  2521. }
  2522. /**
  2523. * test form->input() with time types.
  2524. *
  2525. */
  2526. public function testInputTime() {
  2527. $this->markTestIncomplete('Need to revisit once models work again.');
  2528. extract($this->dateRegex);
  2529. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2530. $result = explode(':', $result);
  2531. $this->assertRegExp('/option value="23"/', $result[0]);
  2532. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2533. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2534. $result = explode(':', $result);
  2535. $this->assertRegExp('/option value="23"/', $result[0]);
  2536. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2537. $result = $this->Form->input('Model.field', array(
  2538. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2539. ));
  2540. $result = explode(':', $result);
  2541. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2542. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2543. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2544. $result = $this->Form->input('Model.field', array(
  2545. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2546. ));
  2547. $result = explode(':', $result);
  2548. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2549. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2550. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2551. $result = $this->Form->input('prueba', array(
  2552. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2553. 'maxYear' => date('Y') + 1, 'interval' => 15
  2554. ));
  2555. $result = explode(':', $result);
  2556. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2557. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2558. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2559. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2560. $result = $this->Form->input('Random.start_time', array(
  2561. 'type' => 'time',
  2562. 'selected' => '18:15'
  2563. ));
  2564. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2565. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2566. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2567. $result = $this->Form->input('published', array('type' => 'time'));
  2568. $now = strtotime('now');
  2569. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2570. $now = strtotime('2013-03-09 00:42:21');
  2571. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2572. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2573. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2574. }
  2575. /**
  2576. * Test interval + selected near the hour roll over.
  2577. *
  2578. * @return void
  2579. */
  2580. public function testTimeSelectedWithInterval() {
  2581. $this->markTestIncomplete('Need to revisit once models work again.');
  2582. $result = $this->Form->input('Model.start_time', array(
  2583. 'type' => 'time',
  2584. 'interval' => 15,
  2585. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2586. ));
  2587. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2588. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2589. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2590. $result = $this->Form->input('Model.start_time', array(
  2591. 'type' => 'time',
  2592. 'interval' => 15,
  2593. 'selected' => '2012-10-23 15:57:00'
  2594. ));
  2595. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2596. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2597. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2598. $result = $this->Form->input('Model.start_time', array(
  2599. 'timeFormat' => 24,
  2600. 'type' => 'time',
  2601. 'interval' => 15,
  2602. 'selected' => '15:57'
  2603. ));
  2604. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2605. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2606. $result = $this->Form->input('Model.start_time', array(
  2607. 'timeFormat' => 24,
  2608. 'type' => 'time',
  2609. 'interval' => 15,
  2610. 'selected' => '23:57'
  2611. ));
  2612. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2613. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2614. $result = $this->Form->input('Model.created', array(
  2615. 'timeFormat' => 24,
  2616. 'type' => 'datetime',
  2617. 'interval' => 15,
  2618. 'selected' => '2012-09-30 23:56'
  2619. ));
  2620. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2621. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2622. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2623. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2624. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2625. }
  2626. /**
  2627. * Test time with selected values around 12:xx:xx
  2628. *
  2629. * @return void
  2630. */
  2631. public function testTimeSelectedWithIntervalTwelve() {
  2632. $this->markTestIncomplete('Need to revisit once models work again.');
  2633. $result = $this->Form->input('Model.start_time', array(
  2634. 'type' => 'time',
  2635. 'timeFormat' => 12,
  2636. 'interval' => 15,
  2637. 'selected' => '00:00:00'
  2638. ));
  2639. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2640. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2641. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2642. $result = $this->Form->input('Model.start_time', array(
  2643. 'type' => 'time',
  2644. 'timeFormat' => 12,
  2645. 'interval' => 15,
  2646. 'selected' => '12:00:00'
  2647. ));
  2648. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2649. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2650. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2651. $result = $this->Form->input('Model.start_time', array(
  2652. 'type' => 'time',
  2653. 'timeFormat' => 12,
  2654. 'interval' => 15,
  2655. 'selected' => '12:15:00'
  2656. ));
  2657. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2658. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2659. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2660. }
  2661. /**
  2662. * Test interval & timeFormat = 12
  2663. *
  2664. * @return void
  2665. */
  2666. public function testInputTimeWithIntervalAnd12HourFormat() {
  2667. $this->markTestIncomplete('Need to revisit once models work again.');
  2668. $result = $this->Form->input('Model.start_time', array(
  2669. 'type' => 'time',
  2670. 'timeFormat' => 12,
  2671. 'interval' => 5,
  2672. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2673. ));
  2674. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2675. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2676. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2677. $result = $this->Form->input('Model.start_time', array(
  2678. 'type' => 'time',
  2679. 'timeFormat' => '12',
  2680. 'interval' => 5,
  2681. 'selected' => '2013-04-19 16:30:00'
  2682. ));
  2683. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2684. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2685. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2686. $result = $this->Form->input('Model.start_time', array(
  2687. 'type' => 'time',
  2688. 'timeFormat' => '12',
  2689. 'interval' => 10,
  2690. 'selected' => '2013-05-19 00:33:00'
  2691. ));
  2692. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2693. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2694. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2695. $result = $this->Form->input('Model.start_time', array(
  2696. 'type' => 'time',
  2697. 'timeFormat' => '12',
  2698. 'interval' => 10,
  2699. 'selected' => '2013-05-19 13:33:00'
  2700. ));
  2701. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2702. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2703. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2704. $result = $this->Form->input('Model.start_time', array(
  2705. 'type' => 'time',
  2706. 'timeFormat' => '12',
  2707. 'interval' => 10,
  2708. 'selected' => '2013-05-19 01:33:00'
  2709. ));
  2710. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2711. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2712. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2713. }
  2714. /**
  2715. * test form->input() with datetime, date and time types
  2716. *
  2717. * @return void
  2718. */
  2719. public function testInputDatetime() {
  2720. $this->markTestIncomplete('Need to revisit once models work again.');
  2721. extract($this->dateRegex);
  2722. $result = $this->Form->input('prueba', array(
  2723. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2724. 'maxYear' => date('Y') + 1, 'interval' => 15
  2725. ));
  2726. $result = explode(':', $result);
  2727. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2728. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2729. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2730. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2731. //related to ticket #5013
  2732. $result = $this->Form->input('Contact.date', array(
  2733. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2734. ));
  2735. $this->assertRegExp('/class="customClass"/', $result);
  2736. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2737. $result = $this->Form->input('Contact.date', array(
  2738. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2739. ));
  2740. $this->assertRegExp('/id="customIdDay"/', $result);
  2741. $this->assertRegExp('/id="customIdMonth"/', $result);
  2742. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2743. $result = $this->Form->input('Model.field', array(
  2744. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2745. ));
  2746. $this->assertRegExp('/id="customIDDay"/', $result);
  2747. $this->assertRegExp('/id="customIDHour"/', $result);
  2748. $result = explode('</select><select', $result);
  2749. $result = explode(':', $result[1]);
  2750. $this->assertRegExp('/option value="23"/', $result[0]);
  2751. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2752. $result = $this->Form->input('Model.field', array(
  2753. 'type' => 'datetime', 'timeFormat' => 12
  2754. ));
  2755. $result = explode('</select><select', $result);
  2756. $result = explode(':', $result[1]);
  2757. $this->assertRegExp('/option value="12"/', $result[0]);
  2758. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2759. $this->Form->request->data = array('Contact' => array('created' => null));
  2760. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2761. $expected = array(
  2762. 'div' => array('class' => 'input date'),
  2763. 'label' => array('for' => 'ContactCreatedMonth'),
  2764. 'Created',
  2765. '/label',
  2766. array('select' => array('name' => 'Contact[created][month]', 'id' => 'ContactCreatedMonth')),
  2767. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2768. $monthsRegex,
  2769. '/select', '-',
  2770. array('select' => array('name' => 'Contact[created][day]', 'id' => 'ContactCreatedDay')),
  2771. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2772. $daysRegex,
  2773. '/select', '-',
  2774. array('select' => array('name' => 'Contact[created][year]', 'id' => 'ContactCreatedYear')),
  2775. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2776. $yearsRegex,
  2777. '/select',
  2778. '/div'
  2779. );
  2780. $this->assertTags($result, $expected);
  2781. $this->Form->request->data = array('Contact' => array('created' => null));
  2782. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2783. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2784. $this->Form->request->data = array('Contact' => array('created' => null));
  2785. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2786. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2787. $result = $this->Form->input('Contact.created', array(
  2788. 'type' => 'date',
  2789. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2790. 'timeFormat' => 'NONE'
  2791. ));
  2792. $this->assertRegExp('/for\="created-month"/', $result);
  2793. }
  2794. /**
  2795. * Test generating checkboxes with disabled elements.
  2796. *
  2797. * @return void
  2798. */
  2799. public function testInputCheckboxWithDisabledElements() {
  2800. $this->markTestIncomplete('Need to revisit once models work again.');
  2801. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2802. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2803. $expected = array(
  2804. array('div' => array('class' => 'input select')),
  2805. array('label' => array('for' => "ContactMultiple")),
  2806. 'Multiple',
  2807. '/label',
  2808. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2809. array('div' => array('class' => 'checkbox')),
  2810. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2811. array('label' => array('for' => "ContactMultiple1")),
  2812. 'One',
  2813. '/label',
  2814. '/div',
  2815. array('div' => array('class' => 'checkbox')),
  2816. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2817. array('label' => array('for' => "ContactMultiple2")),
  2818. 'Two',
  2819. '/label',
  2820. '/div',
  2821. array('div' => array('class' => 'checkbox')),
  2822. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2823. array('label' => array('for' => "ContactMultiple3")),
  2824. 'Three',
  2825. '/label',
  2826. '/div',
  2827. '/div'
  2828. );
  2829. $this->assertTags($result, $expected);
  2830. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2831. $expected = array(
  2832. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  2833. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2834. 'label' => array('for' => 'ModelField12'),
  2835. 'half',
  2836. '/label'
  2837. );
  2838. $this->assertTags($result, $expected);
  2839. $disabled = array('2', 3);
  2840. $expected = array(
  2841. array('div' => array('class' => 'input select')),
  2842. array('label' => array('for' => "ContactMultiple")),
  2843. 'Multiple',
  2844. '/label',
  2845. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2846. array('div' => array('class' => 'checkbox')),
  2847. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2848. array('label' => array('for' => "ContactMultiple1")),
  2849. 'One',
  2850. '/label',
  2851. '/div',
  2852. array('div' => array('class' => 'checkbox')),
  2853. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2854. array('label' => array('for' => "ContactMultiple2")),
  2855. 'Two',
  2856. '/label',
  2857. '/div',
  2858. array('div' => array('class' => 'checkbox')),
  2859. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2860. array('label' => array('for' => "ContactMultiple3")),
  2861. 'Three',
  2862. '/label',
  2863. '/div',
  2864. '/div'
  2865. );
  2866. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2867. $this->assertTags($result, $expected);
  2868. // make sure 50 does only disable 50, and not 50f5c0cf
  2869. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2870. $disabled = array(50);
  2871. $expected = array(
  2872. array('div' => array('class' => 'input select')),
  2873. array('label' => array('for' => "ContactMultiple")),
  2874. 'Multiple',
  2875. '/label',
  2876. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2877. array('div' => array('class' => 'checkbox')),
  2878. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2879. array('label' => array('for' => "ContactMultiple50")),
  2880. 'Fifty',
  2881. '/label',
  2882. '/div',
  2883. array('div' => array('class' => 'checkbox')),
  2884. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2885. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2886. 'Stringy',
  2887. '/label',
  2888. '/div',
  2889. '/div'
  2890. );
  2891. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2892. $this->assertTags($result, $expected);
  2893. }
  2894. /**
  2895. * test input name with leading integer, ensure attributes are generated correctly.
  2896. *
  2897. * @return void
  2898. */
  2899. public function testInputWithLeadingInteger() {
  2900. $result = $this->Form->text('0.Node.title');
  2901. $expected = array(
  2902. 'input' => array('name' => '0[Node][title]', 'type' => 'text')
  2903. );
  2904. $this->assertTags($result, $expected);
  2905. }
  2906. /**
  2907. * test form->input() with select type inputs.
  2908. *
  2909. * @return void
  2910. */
  2911. public function testInputSelectType() {
  2912. $this->markTestIncomplete('Need to revisit once models work again.');
  2913. $result = $this->Form->input('email', array(
  2914. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2915. );
  2916. $expected = array(
  2917. 'div' => array('class' => 'input select'),
  2918. 'label' => array('for' => 'email'),
  2919. 'Email',
  2920. '/label',
  2921. array('select' => array('name' => 'email', 'id' => 'email')),
  2922. array('option' => array('value' => '')),
  2923. '/option',
  2924. array('option' => array('value' => 'è')),
  2925. 'Firést',
  2926. '/option',
  2927. array('option' => array('value' => 'é')),
  2928. 'Secoènd',
  2929. '/option',
  2930. '/select',
  2931. '/div'
  2932. );
  2933. $this->assertTags($result, $expected);
  2934. $result = $this->Form->input('email', array(
  2935. 'options' => array('First', 'Second'), 'empty' => true)
  2936. );
  2937. $expected = array(
  2938. 'div' => array('class' => 'input select'),
  2939. 'label' => array('for' => 'email'),
  2940. 'Email',
  2941. '/label',
  2942. array('select' => array('name' => 'email', 'id' => 'email')),
  2943. array('option' => array('value' => '')),
  2944. '/option',
  2945. array('option' => array('value' => '0')),
  2946. 'First',
  2947. '/option',
  2948. array('option' => array('value' => '1')),
  2949. 'Second',
  2950. '/option',
  2951. '/select',
  2952. '/div'
  2953. );
  2954. $this->assertTags($result, $expected);
  2955. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2956. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2957. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2958. $expected = array(
  2959. 'div' => array('class' => 'input select'),
  2960. 'label' => array('for' => 'ModelUserId'),
  2961. 'User',
  2962. '/label',
  2963. 'select' => array('name' => 'Model[user_id]', 'id' => 'ModelUserId'),
  2964. array('option' => array('value' => '')),
  2965. '/option',
  2966. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2967. 'good',
  2968. '/option',
  2969. array('option' => array('value' => 'other')),
  2970. 'bad',
  2971. '/option',
  2972. '/select',
  2973. '/div'
  2974. );
  2975. $this->assertTags($result, $expected);
  2976. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2977. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2978. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2979. $expected = array(
  2980. 'div' => array('class' => 'input select'),
  2981. 'label' => array('for' => 'ThingUserId'),
  2982. 'User',
  2983. '/label',
  2984. 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'),
  2985. array('option' => array('value' => '')),
  2986. 'Some Empty',
  2987. '/option',
  2988. array('option' => array('value' => 'value')),
  2989. 'good',
  2990. '/option',
  2991. array('option' => array('value' => 'other')),
  2992. 'bad',
  2993. '/option',
  2994. '/select',
  2995. '/div'
  2996. );
  2997. $this->assertTags($result, $expected);
  2998. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2999. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  3000. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  3001. $expected = array(
  3002. 'div' => array('class' => 'input select'),
  3003. 'label' => array('for' => 'ThingUserId'),
  3004. 'User',
  3005. '/label',
  3006. 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'),
  3007. array('option' => array('value' => '')),
  3008. 'Some Empty',
  3009. '/option',
  3010. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3011. 'good',
  3012. '/option',
  3013. array('option' => array('value' => 'other')),
  3014. 'bad',
  3015. '/option',
  3016. '/select',
  3017. '/div'
  3018. );
  3019. $this->assertTags($result, $expected);
  3020. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3021. $this->Form->request->data = array('User' => array('User' => array('value')));
  3022. $result = $this->Form->input('User.User', array('empty' => true));
  3023. $expected = array(
  3024. 'div' => array('class' => 'input select'),
  3025. 'label' => array('for' => 'UserUser'),
  3026. 'User',
  3027. '/label',
  3028. 'input' => array('type' => 'hidden', 'name' => 'User[User]', 'value' => '', 'id' => 'UserUser_'),
  3029. 'select' => array('name' => 'User[User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  3030. array('option' => array('value' => '')),
  3031. '/option',
  3032. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3033. 'good',
  3034. '/option',
  3035. array('option' => array('value' => 'other')),
  3036. 'bad',
  3037. '/option',
  3038. '/select',
  3039. '/div'
  3040. );
  3041. $this->assertTags($result, $expected);
  3042. $this->Form->data = array();
  3043. $result = $this->Form->input('Publisher.id', array(
  3044. 'label' => 'Publisher',
  3045. 'type' => 'select',
  3046. 'multiple' => 'checkbox',
  3047. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  3048. ));
  3049. $expected = array(
  3050. array('div' => array('class' => 'input select')),
  3051. array('label' => array('for' => 'PublisherId')),
  3052. 'Publisher',
  3053. '/label',
  3054. 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'PublisherId'),
  3055. array('div' => array('class' => 'checkbox')),
  3056. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  3057. array('label' => array('for' => 'PublisherIdValue1')),
  3058. 'Label 1',
  3059. '/label',
  3060. '/div',
  3061. array('div' => array('class' => 'checkbox')),
  3062. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  3063. array('label' => array('for' => 'PublisherIdValue2')),
  3064. 'Label 2',
  3065. '/label',
  3066. '/div',
  3067. '/div'
  3068. );
  3069. $this->assertTags($result, $expected);
  3070. }
  3071. /**
  3072. * test that input() and a non standard primary key makes a hidden input by default.
  3073. *
  3074. * @return void
  3075. */
  3076. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  3077. $this->markTestIncomplete('Need to revisit once models work again.');
  3078. $this->Form->create('User');
  3079. $this->Form->fieldset = array(
  3080. 'User' => array(
  3081. 'fields' => array(
  3082. 'model_id' => array('type' => 'integer')
  3083. ),
  3084. 'validates' => array(),
  3085. 'key' => 'model_id'
  3086. )
  3087. );
  3088. $result = $this->Form->input('model_id');
  3089. $expected = array(
  3090. 'input' => array('type' => 'hidden', 'name' => 'User[model_id]', 'id' => 'UserModelId'),
  3091. );
  3092. $this->assertTags($result, $expected);
  3093. }
  3094. /**
  3095. * test that overriding the magic select type widget is possible
  3096. *
  3097. * @return void
  3098. */
  3099. public function testInputOverridingMagicSelectType() {
  3100. $this->markTestIncomplete('Need to revisit once models work again.');
  3101. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3102. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  3103. $expected = array(
  3104. 'div' => array('class' => 'input text'),
  3105. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  3106. 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  3107. '/div'
  3108. );
  3109. $this->assertTags($result, $expected);
  3110. //Check that magic types still work for plural/singular vars
  3111. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  3112. $result = $this->Form->input('Model.type');
  3113. $expected = array(
  3114. 'div' => array('class' => 'input select'),
  3115. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  3116. 'select' => array('name' => 'Model[type]', 'id' => 'ModelType'),
  3117. array('option' => array('value' => 'value')), 'good', '/option',
  3118. array('option' => array('value' => 'other')), 'bad', '/option',
  3119. '/select',
  3120. '/div'
  3121. );
  3122. $this->assertTags($result, $expected);
  3123. }
  3124. /**
  3125. * Test that inferred types do not override developer input
  3126. *
  3127. * @return void
  3128. */
  3129. public function testInputMagicTypeDoesNotOverride() {
  3130. $this->markTestIncomplete('Need to revisit once models work again.');
  3131. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3132. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  3133. $expected = array(
  3134. 'div' => array('class' => 'input checkbox'),
  3135. array('input' => array(
  3136. 'type' => 'hidden',
  3137. 'name' => 'data[Model][user]',
  3138. 'id' => 'ModelUser_',
  3139. 'value' => 0,
  3140. )),
  3141. array('input' => array(
  3142. 'name' => 'data[Model][user]',
  3143. 'type' => 'checkbox',
  3144. 'id' => 'ModelUser',
  3145. 'value' => 1
  3146. )),
  3147. 'label' => array('for' => 'ModelUser'), 'User', '/label',
  3148. '/div'
  3149. );
  3150. $this->assertTags($result, $expected);
  3151. }
  3152. /**
  3153. * Test that magic input() selects are created for type=number
  3154. *
  3155. * @return void
  3156. */
  3157. public function testInputMagicSelectForTypeNumber() {
  3158. $this->markTestIncomplete('Need to revisit once models work again.');
  3159. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  3160. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  3161. $result = $this->Form->input('ValidateUser.balance');
  3162. $expected = array(
  3163. 'div' => array('class' => 'input select'),
  3164. 'label' => array('for' => 'ValidateUserBalance'),
  3165. 'Balance',
  3166. '/label',
  3167. 'select' => array('name' => 'ValidateUser[balance]', 'id' => 'ValidateUserBalance'),
  3168. array('option' => array('value' => '0')),
  3169. 'nothing',
  3170. '/option',
  3171. array('option' => array('value' => '1', 'selected' => 'selected')),
  3172. 'some',
  3173. '/option',
  3174. array('option' => array('value' => '100')),
  3175. 'a lot',
  3176. '/option',
  3177. '/select',
  3178. '/div'
  3179. );
  3180. $this->assertTags($result, $expected);
  3181. }
  3182. /**
  3183. * Test that magic input() selects can easily be converted into radio types without error.
  3184. *
  3185. * @return void
  3186. */
  3187. public function testInputMagicSelectChangeToRadio() {
  3188. $this->markTestIncomplete('Need to revisit once models work again.');
  3189. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3190. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  3191. $this->assertRegExp('/input type="radio"/', $result);
  3192. }
  3193. /**
  3194. * fields with the same name as the model should work.
  3195. *
  3196. * @return void
  3197. */
  3198. public function testInputWithMatchingFieldAndModelName() {
  3199. $this->markTestIncomplete('Need to revisit once models work again.');
  3200. $this->Form->create('User');
  3201. $this->Form->fieldset = array(
  3202. 'User' => array(
  3203. 'fields' => array(
  3204. 'User' => array('type' => 'text')
  3205. ),
  3206. 'validates' => array(),
  3207. 'key' => 'id'
  3208. )
  3209. );
  3210. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  3211. $result = $this->Form->input('User', array('type' => 'text'));
  3212. $expected = array(
  3213. 'div' => array('class' => 'input text'),
  3214. 'label' => array('for' => 'UserUser'), 'User', '/label',
  3215. 'input' => array('name' => 'User[User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  3216. '/div'
  3217. );
  3218. $this->assertTags($result, $expected);
  3219. }
  3220. /**
  3221. * testFormInputs method
  3222. *
  3223. * test correct results from form::inputs().
  3224. *
  3225. * @return void
  3226. */
  3227. public function testFormInputs() {
  3228. $this->markTestIncomplete('Need to revisit once models work again.');
  3229. $this->Form->create('Cake\Test\TestCase\View\Helper\Contact');
  3230. $result = $this->Form->inputs('The Legend');
  3231. $expected = array(
  3232. '<fieldset',
  3233. '<legend',
  3234. 'The Legend',
  3235. '/legend',
  3236. '*/fieldset',
  3237. );
  3238. $this->assertTags($result, $expected);
  3239. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  3240. $expected = array(
  3241. 'fieldset' => array('class' => 'classy-stuff'),
  3242. '<legend',
  3243. 'Field of Dreams',
  3244. '/legend',
  3245. '*/fieldset'
  3246. );
  3247. $this->assertTags($result, $expected);
  3248. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  3249. $this->assertTags($result, $expected);
  3250. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  3251. $this->assertTags($result, $expected);
  3252. $this->Form->create('Contact');
  3253. $this->Form->request['prefix'] = 'admin';
  3254. $this->Form->request['action'] = 'admin_edit';
  3255. $result = $this->Form->inputs();
  3256. $expected = array(
  3257. '<fieldset',
  3258. '<legend',
  3259. 'Edit Contact',
  3260. '/legend',
  3261. '*/fieldset',
  3262. );
  3263. $this->assertTags($result, $expected);
  3264. $this->Form->create('Contact');
  3265. $result = $this->Form->inputs(false);
  3266. $expected = array(
  3267. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3268. array('div' => array('class' => 'input text')),
  3269. '*/div',
  3270. array('div' => array('class' => 'input email')),
  3271. '*/div',
  3272. array('div' => array('class' => 'input tel')),
  3273. '*/div',
  3274. array('div' => array('class' => 'input password')),
  3275. '*/div',
  3276. array('div' => array('class' => 'input date')),
  3277. '*/div',
  3278. array('div' => array('class' => 'input date')),
  3279. '*/div',
  3280. array('div' => array('class' => 'input datetime')),
  3281. '*/div',
  3282. array('div' => array('class' => 'input number')),
  3283. '*/div',
  3284. array('div' => array('class' => 'input select')),
  3285. '*/div',
  3286. );
  3287. $this->assertTags($result, $expected);
  3288. $this->Form->create('Contact');
  3289. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  3290. $expected = array(
  3291. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3292. array('div' => array('class' => 'input text')),
  3293. '*/div',
  3294. array('div' => array('class' => 'input email')),
  3295. '*/div',
  3296. array('div' => array('class' => 'input tel')),
  3297. '*/div',
  3298. array('div' => array('class' => 'input password')),
  3299. '*/div',
  3300. array('div' => array('class' => 'input date')),
  3301. '*/div',
  3302. array('div' => array('class' => 'input date')),
  3303. '*/div',
  3304. array('div' => array('class' => 'input datetime')),
  3305. '*/div',
  3306. array('div' => array('class' => 'input number')),
  3307. '*/div',
  3308. array('div' => array('class' => 'input select')),
  3309. '*/div',
  3310. );
  3311. $this->assertTags($result, $expected);
  3312. $this->Form->create('Contact');
  3313. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  3314. $this->assertTags($result, $expected);
  3315. $this->Form->create('Contact');
  3316. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  3317. $expected = array(
  3318. 'fieldset' => array(),
  3319. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3320. array('div' => array('class' => 'input text')),
  3321. '*/div',
  3322. array('div' => array('class' => 'input email')),
  3323. '*/div',
  3324. array('div' => array('class' => 'input tel')),
  3325. '*/div',
  3326. array('div' => array('class' => 'input password')),
  3327. '*/div',
  3328. array('div' => array('class' => 'input date')),
  3329. '*/div',
  3330. array('div' => array('class' => 'input date')),
  3331. '*/div',
  3332. array('div' => array('class' => 'input datetime')),
  3333. '*/div',
  3334. array('div' => array('class' => 'input number')),
  3335. '*/div',
  3336. array('div' => array('class' => 'input select')),
  3337. '*/div',
  3338. '/fieldset'
  3339. );
  3340. $this->assertTags($result, $expected);
  3341. $this->Form->create('Contact');
  3342. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  3343. $expected = array(
  3344. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3345. array('div' => array('class' => 'input text')),
  3346. '*/div',
  3347. array('div' => array('class' => 'input email')),
  3348. '*/div',
  3349. array('div' => array('class' => 'input tel')),
  3350. '*/div',
  3351. array('div' => array('class' => 'input password')),
  3352. '*/div',
  3353. array('div' => array('class' => 'input date')),
  3354. '*/div',
  3355. array('div' => array('class' => 'input date')),
  3356. '*/div',
  3357. array('div' => array('class' => 'input datetime')),
  3358. '*/div',
  3359. array('div' => array('class' => 'input number')),
  3360. '*/div',
  3361. array('div' => array('class' => 'input select')),
  3362. '*/div',
  3363. );
  3364. $this->assertTags($result, $expected);
  3365. $this->Form->create('Contact');
  3366. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  3367. $this->assertTags($result, $expected);
  3368. $this->Form->create('Contact');
  3369. $result = $this->Form->inputs('Hello');
  3370. $expected = array(
  3371. 'fieldset' => array(),
  3372. 'legend' => array(),
  3373. 'Hello',
  3374. '/legend',
  3375. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3376. array('div' => array('class' => 'input text')),
  3377. '*/div',
  3378. array('div' => array('class' => 'input email')),
  3379. '*/div',
  3380. array('div' => array('class' => 'input tel')),
  3381. '*/div',
  3382. array('div' => array('class' => 'input password')),
  3383. '*/div',
  3384. array('div' => array('class' => 'input date')),
  3385. '*/div',
  3386. array('div' => array('class' => 'input date')),
  3387. '*/div',
  3388. array('div' => array('class' => 'input datetime')),
  3389. '*/div',
  3390. array('div' => array('class' => 'input number')),
  3391. '*/div',
  3392. array('div' => array('class' => 'input select')),
  3393. '*/div',
  3394. '/fieldset'
  3395. );
  3396. $this->assertTags($result, $expected);
  3397. $this->Form->create('Contact');
  3398. $result = $this->Form->inputs(array('legend' => 'Hello'));
  3399. $expected = array(
  3400. 'fieldset' => array(),
  3401. 'legend' => array(),
  3402. 'Hello',
  3403. '/legend',
  3404. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3405. array('div' => array('class' => 'input text')),
  3406. '*/div',
  3407. array('div' => array('class' => 'input email')),
  3408. '*/div',
  3409. array('div' => array('class' => 'input tel')),
  3410. '*/div',
  3411. array('div' => array('class' => 'input password')),
  3412. '*/div',
  3413. array('div' => array('class' => 'input date')),
  3414. '*/div',
  3415. array('div' => array('class' => 'input date')),
  3416. '*/div',
  3417. array('div' => array('class' => 'input datetime')),
  3418. '*/div',
  3419. array('div' => array('class' => 'input number')),
  3420. '*/div',
  3421. array('div' => array('class' => 'input select')),
  3422. '*/div',
  3423. '/fieldset'
  3424. );
  3425. $this->assertTags($result, $expected);
  3426. $this->Form->create('Contact');
  3427. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  3428. $this->assertTags($result, $expected);
  3429. $this->Form->end();
  3430. $this->Form->create(false);
  3431. $expected = array(
  3432. 'fieldset' => array(),
  3433. array('div' => array('class' => 'input text')),
  3434. 'label' => array('for' => 'foo'),
  3435. 'Foo',
  3436. '/label',
  3437. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  3438. '*/div',
  3439. '/fieldset'
  3440. );
  3441. $result = $this->Form->inputs(
  3442. array('foo' => array('type' => 'text')),
  3443. array(),
  3444. array('legend' => false)
  3445. );
  3446. $this->assertTags($result, $expected);
  3447. }
  3448. /**
  3449. * Tests inputs() works with plugin models
  3450. *
  3451. * @return void
  3452. */
  3453. public function testInputsPluginModel() {
  3454. $this->markTestIncomplete('Need to revisit once models work again.');
  3455. $this->loadFixtures('Post');
  3456. Plugin::load('TestPlugin');
  3457. $this->Form->request['models'] = array(
  3458. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  3459. );
  3460. $this->Form->create('TestPlugin.TestPluginPost');
  3461. $result = $this->Form->inputs();
  3462. $this->assertContains('TestPluginPost[id]', $result);
  3463. $this->assertContains('TestPluginPost[author_id]', $result);
  3464. $this->assertContains('TestPluginPost[title]', $result);
  3465. $this->assertEquals('TestPluginPost', $this->Form->model());
  3466. }
  3467. /**
  3468. * testSelectAsCheckbox method
  3469. *
  3470. * test multi-select widget with checkbox formatting.
  3471. *
  3472. * @return void
  3473. */
  3474. public function testSelectAsCheckbox() {
  3475. $result = $this->Form->select(
  3476. 'Model.multi_field',
  3477. array('first', 'second', 'third'),
  3478. array('multiple' => 'checkbox', 'value' => array(0, 1))
  3479. );
  3480. $expected = array(
  3481. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3482. array('div' => array('class' => 'checkbox')),
  3483. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0')),
  3484. array('label' => array('for' => 'model-multi-field-0', 'class' => 'selected')),
  3485. 'first',
  3486. '/label',
  3487. '/div',
  3488. array('div' => array('class' => 'checkbox')),
  3489. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1')),
  3490. array('label' => array('for' => 'model-multi-field-1', 'class' => 'selected')),
  3491. 'second',
  3492. '/label',
  3493. '/div',
  3494. array('div' => array('class' => 'checkbox')),
  3495. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  3496. array('label' => array('for' => 'model-multi-field-2')),
  3497. 'third',
  3498. '/label',
  3499. '/div',
  3500. );
  3501. $this->assertTags($result, $expected);
  3502. $result = $this->Form->select(
  3503. 'Model.multi_field',
  3504. array('1/2' => 'half'),
  3505. array('multiple' => 'checkbox')
  3506. );
  3507. $expected = array(
  3508. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3509. array('div' => array('class' => 'checkbox')),
  3510. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2')),
  3511. array('label' => array('for' => 'model-multi-field-1-2')),
  3512. 'half',
  3513. '/label',
  3514. '/div',
  3515. );
  3516. $this->assertTags($result, $expected);
  3517. }
  3518. /**
  3519. * testLabel method
  3520. *
  3521. * test label generation.
  3522. *
  3523. * @return void
  3524. */
  3525. public function testLabel() {
  3526. $result = $this->Form->label('Person.name');
  3527. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  3528. $result = $this->Form->label('Person.name');
  3529. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  3530. $result = $this->Form->label('Person.first_name');
  3531. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'First Name', '/label'));
  3532. $result = $this->Form->label('Person.first_name', 'Your first name');
  3533. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'Your first name', '/label'));
  3534. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  3535. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class'), 'Your first name', '/label'));
  3536. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  3537. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  3538. $result = $this->Form->label('Person.first_name', '');
  3539. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), '/label'));
  3540. $result = $this->Form->label('Person.2.name', '');
  3541. $this->assertTags($result, array('label' => array('for' => 'person-2-name'), '/label'));
  3542. }
  3543. /**
  3544. * testTextbox method
  3545. *
  3546. * test textbox element generation
  3547. *
  3548. * @return void
  3549. */
  3550. public function testTextbox() {
  3551. $result = $this->Form->text('Model.field');
  3552. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]')));
  3553. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3554. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]')));
  3555. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3556. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID')));
  3557. }
  3558. /**
  3559. * Test that text() hooks up with request data and error fields.
  3560. *
  3561. * @return void
  3562. */
  3563. public function testTextBoxDataAndError() {
  3564. $this->article['errors'] = [
  3565. 'Contact' => ['text' => 'wrong']
  3566. ];
  3567. $this->Form->create($this->article);
  3568. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3569. $result = $this->Form->text('Model.text');
  3570. $expected = [
  3571. 'input' => [
  3572. 'type' => 'text',
  3573. 'name' => 'Model[text]',
  3574. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3575. ]
  3576. ];
  3577. $this->assertTags($result, $expected);
  3578. $this->Form->request->data['Contact']['text'] = 'test';
  3579. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3580. $expected = [
  3581. 'input' => [
  3582. 'type' => 'text',
  3583. 'name' => 'Contact[text]',
  3584. 'value' => 'test',
  3585. 'id' => 'theID',
  3586. 'class' => 'form-error'
  3587. ]
  3588. ];
  3589. $this->assertTags($result, $expected);
  3590. }
  3591. /**
  3592. * testDefaultValue method
  3593. *
  3594. * Test default value setting
  3595. *
  3596. * @return void
  3597. */
  3598. public function testTextDefaultValue() {
  3599. $this->Form->request->data['Model']['field'] = 'test';
  3600. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3601. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3602. $this->assertTags($result, $expected);
  3603. unset($this->Form->request->data['Model']['field']);
  3604. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3605. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3606. $this->assertTags($result, $expected);
  3607. }
  3608. /**
  3609. * testError method
  3610. *
  3611. * Test field error generation
  3612. *
  3613. * @return void
  3614. */
  3615. public function testError() {
  3616. $this->article['errors'] = [
  3617. 'Article' => ['field' => 'email']
  3618. ];
  3619. $this->Form->create($this->article);
  3620. $result = $this->Form->error('Article.field');
  3621. $expected = [
  3622. ['div' => ['class' => 'error-message']],
  3623. 'email',
  3624. '/div',
  3625. ];
  3626. $this->assertTags($result, $expected);
  3627. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  3628. $expected = [
  3629. ['div' => ['class' => 'error-message']],
  3630. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3631. '/div',
  3632. ];
  3633. $this->assertTags($result, $expected);
  3634. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3635. $expected = [
  3636. ['div' => ['class' => 'error-message']],
  3637. '<strong', 'Badness!', '/strong',
  3638. '/div',
  3639. ];
  3640. $this->assertTags($result, $expected);
  3641. }
  3642. /**
  3643. * Test error with nested lists.
  3644. *
  3645. * @return void
  3646. */
  3647. public function testErrorMessages() {
  3648. $this->article['errors'] = [
  3649. 'Article' => ['field' => 'email']
  3650. ];
  3651. $this->Form->create($this->article);
  3652. $result = $this->Form->error('Article.field', array(
  3653. 'email' => 'No good!'
  3654. ));
  3655. $expected = array(
  3656. 'div' => array('class' => 'error-message'),
  3657. 'No good!',
  3658. '/div'
  3659. );
  3660. $this->assertTags($result, $expected);
  3661. }
  3662. /**
  3663. * test error() with multiple messages.
  3664. *
  3665. * @return void
  3666. */
  3667. public function testErrorMultipleMessages() {
  3668. $this->article['errors'] = [
  3669. 'field' => ['notEmpty', 'email', 'Something else']
  3670. ];
  3671. $this->Form->create($this->article);
  3672. $result = $this->Form->error('field', array(
  3673. 'notEmpty' => 'Cannot be empty',
  3674. 'email' => 'No good!'
  3675. ));
  3676. $expected = array(
  3677. 'div' => array('class' => 'error-message'),
  3678. 'ul' => array(),
  3679. '<li', 'Cannot be empty', '/li',
  3680. '<li', 'No good!', '/li',
  3681. '<li', 'Something else', '/li',
  3682. '/ul',
  3683. '/div'
  3684. );
  3685. $this->assertTags($result, $expected);
  3686. }
  3687. /**
  3688. * test error options when using form->input();
  3689. *
  3690. * @return void
  3691. */
  3692. public function testInputErrorEscape() {
  3693. $this->markTestIncomplete('Need to revisit once models work again.');
  3694. $this->Form->create('ValidateProfile');
  3695. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3696. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3697. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3698. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3699. $this->assertRegExp('/required<br>/', $result);
  3700. }
  3701. /**
  3702. * testPassword method
  3703. *
  3704. * Test password element generation
  3705. *
  3706. * @return void
  3707. */
  3708. public function testPassword() {
  3709. $this->article['errors'] = [
  3710. 'Contact' => [
  3711. 'passwd' => 1
  3712. ]
  3713. ];
  3714. $this->Form->create($this->article);
  3715. $result = $this->Form->password('Contact.field');
  3716. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]')));
  3717. $this->Form->request->data['Contact']['passwd'] = 'test';
  3718. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3719. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3720. }
  3721. /**
  3722. * testRadio method
  3723. *
  3724. * Test radio element set generation
  3725. *
  3726. * @return void
  3727. */
  3728. public function testRadio() {
  3729. $result = $this->Form->radio('Model.field', array('option A'));
  3730. $expected = array(
  3731. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3732. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3733. 'label' => array('for' => 'model-field-0'),
  3734. 'option A',
  3735. '/label'
  3736. );
  3737. $this->assertTags($result, $expected);
  3738. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3739. $expected = array(
  3740. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3741. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3742. array('label' => array('for' => 'model-field-0')),
  3743. 'option A',
  3744. '/label',
  3745. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1')),
  3746. array('label' => array('for' => 'model-field-1')),
  3747. 'option B',
  3748. '/label',
  3749. );
  3750. $this->assertTags($result, $expected);
  3751. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3752. $expected = array(
  3753. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''),
  3754. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male')),
  3755. array('label' => array('for' => 'employee-gender-male')),
  3756. 'Male',
  3757. '/label',
  3758. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female')),
  3759. array('label' => array('for' => 'employee-gender-female')),
  3760. 'Female',
  3761. '/label',
  3762. );
  3763. $this->assertTags($result, $expected);
  3764. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  3765. $expected = array(
  3766. array('input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '')),
  3767. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0')),
  3768. array('label' => array('for' => 'model-custom-0')),
  3769. 'option A',
  3770. '/label',
  3771. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1')),
  3772. array('label' => array('for' => 'model-custom-1')),
  3773. 'option B',
  3774. '/label',
  3775. );
  3776. $this->assertTags($result, $expected);
  3777. }
  3778. /**
  3779. * test generating radio input inside label ala twitter bootstrap
  3780. *
  3781. * @return void
  3782. */
  3783. public function testRadioInputInsideLabel() {
  3784. $this->Form->templates([
  3785. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3786. 'radioContainer' => '{{label}}'
  3787. ]);
  3788. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  3789. $expected = [
  3790. ['input' => [
  3791. 'type' => 'hidden',
  3792. 'name' => 'Model[field]',
  3793. 'value' => ''
  3794. ]],
  3795. ['label' => ['for' => 'model-field-0']],
  3796. ['input' => [
  3797. 'type' => 'radio',
  3798. 'name' => 'Model[field]',
  3799. 'value' => '0',
  3800. 'id' => 'model-field-0'
  3801. ]],
  3802. 'option A',
  3803. '/label',
  3804. ['label' => ['for' => 'model-field-1']],
  3805. ['input' => [
  3806. 'type' => 'radio',
  3807. 'name' => 'Model[field]',
  3808. 'value' => '1',
  3809. 'id' => 'model-field-1'
  3810. ]],
  3811. 'option B',
  3812. '/label',
  3813. ];
  3814. $this->assertTags($result, $expected);
  3815. }
  3816. /**
  3817. * test disabling the hidden input for radio buttons
  3818. *
  3819. * @return void
  3820. */
  3821. public function testRadioHiddenInputDisabling() {
  3822. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3823. $expected = array(
  3824. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'),
  3825. 'label' => array('for' => 'model-1-field-0'),
  3826. 'option A',
  3827. '/label'
  3828. );
  3829. $this->assertTags($result, $expected);
  3830. }
  3831. /**
  3832. * testDomIdSuffix method
  3833. *
  3834. * @return void
  3835. */
  3836. public function testDomIdSuffix() {
  3837. $this->markTestIncomplete('Need to revisit once models work again.');
  3838. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs');
  3839. $this->assertEquals('1StringWith1$-dollarSigns', $result);
  3840. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>');
  3841. $this->assertEquals('AbcX=FooY=Bar', $result);
  3842. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'xhtml');
  3843. $this->assertEquals('1StringWith1-dollarSigns', $result);
  3844. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>', 'xhtml');
  3845. $this->assertEquals('AbcXFooYBar', $result);
  3846. }
  3847. /**
  3848. * testDomIdSuffixCollisionResolvement()
  3849. *
  3850. * @return void
  3851. */
  3852. public function testDomIdSuffixCollisionResolvement() {
  3853. $this->markTestIncomplete('Need to revisit once models work again.');
  3854. $result = $this->Form->domIdSuffix('a>b');
  3855. $this->assertEquals('AB', $result);
  3856. $result = $this->Form->domIdSuffix('a<b');
  3857. $this->assertEquals('AB1', $result);
  3858. $result = $this->Form->domIdSuffix('a\'b');
  3859. $this->assertEquals('AB2', $result);
  3860. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3861. $this->assertEquals('1StringWith1-dollar', $result);
  3862. $result = $this->Form->domIdSuffix('1 string with 1€-dollar', 'xhtml');
  3863. $this->assertEquals('1StringWith1-dollar1', $result);
  3864. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3865. $this->assertEquals('1StringWith1-dollar2', $result);
  3866. }
  3867. /**
  3868. * testSelect method
  3869. *
  3870. * Test select element generation.
  3871. *
  3872. * @return void
  3873. */
  3874. public function testSelect() {
  3875. $result = $this->Form->select('Model.field', array());
  3876. $expected = array(
  3877. 'select' => array('name' => 'Model[field]'),
  3878. '/select'
  3879. );
  3880. $this->assertTags($result, $expected);
  3881. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3882. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3883. $expected = array(
  3884. 'select' => array('name' => 'Model[field]'),
  3885. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3886. 'good',
  3887. '/option',
  3888. array('option' => array('value' => 'other')),
  3889. 'bad',
  3890. '/option',
  3891. '/select'
  3892. );
  3893. $this->assertTags($result, $expected);
  3894. $this->Form->request->data = array();
  3895. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3896. $expected = array(
  3897. 'select' => array('name' => 'Model[field]'),
  3898. array('option' => array('value' => 'value')),
  3899. 'good',
  3900. '/option',
  3901. array('option' => array('value' => 'other')),
  3902. 'bad',
  3903. '/option',
  3904. '/select'
  3905. );
  3906. $this->assertTags($result, $expected);
  3907. $result = $this->Form->select(
  3908. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3909. array('empty' => false)
  3910. );
  3911. $expected = array(
  3912. 'select' => array('name' => 'Model[field]'),
  3913. array('option' => array('value' => 'first')),
  3914. 'first &quot;html&quot; &lt;chars&gt;',
  3915. '/option',
  3916. array('option' => array('value' => 'second')),
  3917. 'value',
  3918. '/option',
  3919. '/select'
  3920. );
  3921. $this->assertTags($result, $expected);
  3922. $result = $this->Form->select(
  3923. 'Model.field',
  3924. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3925. array('escape' => false, 'empty' => false)
  3926. );
  3927. $expected = array(
  3928. 'select' => array('name' => 'Model[field]'),
  3929. array('option' => array('value' => 'first')),
  3930. 'first "html" <chars>',
  3931. '/option',
  3932. array('option' => array('value' => 'second')),
  3933. 'value',
  3934. '/option',
  3935. '/select'
  3936. );
  3937. $this->assertTags($result, $expected);
  3938. $options = array(
  3939. array('value' => 'first', 'text' => 'First'),
  3940. array('value' => 'first', 'text' => 'Another First'),
  3941. );
  3942. $result = $this->Form->select(
  3943. 'Model.field',
  3944. $options,
  3945. array('escape' => false, 'empty' => false)
  3946. );
  3947. $expected = array(
  3948. 'select' => array('name' => 'Model[field]'),
  3949. array('option' => array('value' => 'first')),
  3950. 'First',
  3951. '/option',
  3952. array('option' => array('value' => 'first')),
  3953. 'Another First',
  3954. '/option',
  3955. '/select'
  3956. );
  3957. $this->assertTags($result, $expected);
  3958. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3959. $result = $this->Form->select(
  3960. 'Model.contact_id',
  3961. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3962. array('escape' => false, 'empty' => 'pick something')
  3963. );
  3964. $expected = array(
  3965. 'select' => array('name' => 'Model[contact_id]'),
  3966. array('option' => array('value' => '')), 'pick something', '/option',
  3967. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3968. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3969. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3970. '/select'
  3971. );
  3972. $this->assertTags($result, $expected);
  3973. $this->Form->request->data['Model']['field'] = 0;
  3974. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3975. $expected = array(
  3976. 'select' => array('name' => 'Model[field]'),
  3977. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3978. array('option' => array('value' => '1')), 'Yes', '/option',
  3979. '/select'
  3980. );
  3981. $this->assertTags($result, $expected);
  3982. }
  3983. /**
  3984. * test select() with required and disabled attributes.
  3985. *
  3986. * @return void
  3987. */
  3988. public function testSelectRequired() {
  3989. $this->article['required'] = [
  3990. 'user_id' => true
  3991. ];
  3992. $this->Form->create($this->article);
  3993. $result = $this->Form->select('user_id', array('option A'));
  3994. $expected = array(
  3995. 'select' => array(
  3996. 'name' => 'user_id',
  3997. 'required' => 'required'
  3998. ),
  3999. array('option' => array('value' => '0')), 'option A', '/option',
  4000. '/select'
  4001. );
  4002. $this->assertTags($result, $expected);
  4003. $result = $this->Form->select('user_id', array('option A'), array('disabled' => true));
  4004. $expected = array(
  4005. 'select' => array(
  4006. 'name' => 'user_id',
  4007. 'disabled' => 'disabled'
  4008. ),
  4009. array('option' => array('value' => '0')), 'option A', '/option',
  4010. '/select'
  4011. );
  4012. $this->assertTags($result, $expected);
  4013. }
  4014. /**
  4015. * testNestedSelect method
  4016. *
  4017. * test select element generation with optgroups
  4018. *
  4019. * @return void
  4020. */
  4021. public function testNestedSelect() {
  4022. $result = $this->Form->select(
  4023. 'Model.field',
  4024. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4025. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4026. )), array('empty' => false)
  4027. );
  4028. $expected = array(
  4029. 'select' => array('name' => 'Model[field]'),
  4030. array('option' => array('value' => 1)),
  4031. 'One',
  4032. '/option',
  4033. array('option' => array('value' => 2)),
  4034. 'Two',
  4035. '/option',
  4036. array('optgroup' => array('label' => 'Three')),
  4037. array('option' => array('value' => 3)),
  4038. 'Three',
  4039. '/option',
  4040. array('option' => array('value' => 4)),
  4041. 'Four',
  4042. '/option',
  4043. array('option' => array('value' => 5)),
  4044. 'Five',
  4045. '/option',
  4046. '/optgroup',
  4047. '/select'
  4048. );
  4049. $this->assertTags($result, $expected);
  4050. }
  4051. /**
  4052. * testSelectMultiple method
  4053. *
  4054. * test generation of multiple select elements
  4055. *
  4056. * @return void
  4057. */
  4058. public function testSelectMultiple() {
  4059. $options = array('first', 'second', 'third');
  4060. $result = $this->Form->select(
  4061. 'Model.multi_field',
  4062. $options,
  4063. array('multiple' => true)
  4064. );
  4065. $expected = array(
  4066. 'input' => array(
  4067. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4068. ),
  4069. 'select' => array(
  4070. 'name' => 'Model[multi_field][]',
  4071. 'multiple' => 'multiple'
  4072. ),
  4073. array('option' => array('value' => '0')),
  4074. 'first',
  4075. '/option',
  4076. array('option' => array('value' => '1')),
  4077. 'second',
  4078. '/option',
  4079. array('option' => array('value' => '2')),
  4080. 'third',
  4081. '/option',
  4082. '/select'
  4083. );
  4084. $this->assertTags($result, $expected);
  4085. $result = $this->Form->select(
  4086. 'Model.multi_field',
  4087. $options,
  4088. array('multiple' => 'multiple')
  4089. );
  4090. $this->assertTags($result, $expected);
  4091. }
  4092. /**
  4093. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4094. *
  4095. * @return void
  4096. */
  4097. public function testCheckboxZeroValue() {
  4098. $this->markTestIncomplete('Need to revisit once models work again.');
  4099. $result = $this->Form->input('User.get_spam', array(
  4100. 'type' => 'checkbox',
  4101. 'value' => '0',
  4102. 'hiddenField' => '1',
  4103. ));
  4104. $expected = array(
  4105. 'div' => array('class' => 'input checkbox'),
  4106. array('input' => array(
  4107. 'type' => 'hidden', 'name' => 'data[User][get_spam]',
  4108. 'value' => '1', 'id' => 'UserGetSpam_'
  4109. )),
  4110. array('input' => array(
  4111. 'type' => 'checkbox', 'name' => 'data[User][get_spam]',
  4112. 'value' => '0', 'id' => 'UserGetSpam'
  4113. )),
  4114. 'label' => array('for' => 'UserGetSpam'),
  4115. 'Get Spam',
  4116. '/label',
  4117. '/div'
  4118. );
  4119. $this->assertTags($result, $expected);
  4120. }
  4121. /**
  4122. * test generation of habtm select boxes.
  4123. *
  4124. * @return void
  4125. */
  4126. public function testHabtmSelectBox() {
  4127. $this->markTestIncomplete('Need to revisit once models work again.');
  4128. $this->View->viewVars['contactTags'] = array(
  4129. 1 => 'blue',
  4130. 2 => 'red',
  4131. 3 => 'green'
  4132. );
  4133. $this->Form->request->data = array(
  4134. 'Contact' => array(),
  4135. 'ContactTag' => array(
  4136. array(
  4137. 'id' => '1',
  4138. 'name' => 'blue'
  4139. ),
  4140. array(
  4141. 'id' => 3,
  4142. 'name' => 'green'
  4143. )
  4144. )
  4145. );
  4146. $this->Form->create('Contact');
  4147. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4148. $expected = array(
  4149. 'input' => array(
  4150. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4151. ),
  4152. 'select' => array(
  4153. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  4154. 'multiple' => 'multiple'
  4155. ),
  4156. array('option' => array('value' => '1', 'selected' => 'selected')),
  4157. 'blue',
  4158. '/option',
  4159. array('option' => array('value' => '2')),
  4160. 'red',
  4161. '/option',
  4162. array('option' => array('value' => '3', 'selected' => 'selected')),
  4163. 'green',
  4164. '/option',
  4165. '/select'
  4166. );
  4167. $this->assertTags($result, $expected);
  4168. // make sure only 50 is selected, and not 50f5c0cf
  4169. $this->View->viewVars['contactTags'] = array(
  4170. '1' => 'blue',
  4171. '50f5c0cf' => 'red',
  4172. '50' => 'green'
  4173. );
  4174. $this->Form->request->data = array(
  4175. 'Contact' => array(),
  4176. 'ContactTag' => array(
  4177. array(
  4178. 'id' => 1,
  4179. 'name' => 'blue'
  4180. ),
  4181. array(
  4182. 'id' => 50,
  4183. 'name' => 'green'
  4184. )
  4185. )
  4186. );
  4187. $this->Form->create('Contact');
  4188. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4189. $expected = array(
  4190. 'input' => array(
  4191. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4192. ),
  4193. 'select' => array(
  4194. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  4195. 'multiple' => 'multiple'
  4196. ),
  4197. array('option' => array('value' => '1', 'selected' => 'selected')),
  4198. 'blue',
  4199. '/option',
  4200. array('option' => array('value' => '50f5c0cf')),
  4201. 'red',
  4202. '/option',
  4203. array('option' => array('value' => '50', 'selected' => 'selected')),
  4204. 'green',
  4205. '/option',
  4206. '/select'
  4207. );
  4208. $this->assertTags($result, $expected);
  4209. }
  4210. /**
  4211. * test generation of multi select elements in checkbox format
  4212. *
  4213. * @return void
  4214. */
  4215. public function testSelectMultipleCheckboxes() {
  4216. $result = $this->Form->select(
  4217. 'Model.multi_field',
  4218. array('first', 'second', 'third'),
  4219. array('multiple' => 'checkbox')
  4220. );
  4221. $expected = array(
  4222. 'input' => array(
  4223. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4224. ),
  4225. array('div' => array('class' => 'checkbox')),
  4226. array('input' => array(
  4227. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4228. 'value' => '0', 'id' => 'model-multi-field-0'
  4229. )),
  4230. array('label' => array('for' => 'model-multi-field-0')),
  4231. 'first',
  4232. '/label',
  4233. '/div',
  4234. array('div' => array('class' => 'checkbox')),
  4235. array('input' => array(
  4236. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4237. 'value' => '1', 'id' => 'model-multi-field-1'
  4238. )),
  4239. array('label' => array('for' => 'model-multi-field-1')),
  4240. 'second',
  4241. '/label',
  4242. '/div',
  4243. array('div' => array('class' => 'checkbox')),
  4244. array('input' => array(
  4245. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4246. 'value' => '2', 'id' => 'model-multi-field-2'
  4247. )),
  4248. array('label' => array('for' => 'model-multi-field-2')),
  4249. 'third',
  4250. '/label',
  4251. '/div'
  4252. );
  4253. $this->assertTags($result, $expected);
  4254. $result = $this->Form->select(
  4255. 'Model.multi_field',
  4256. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  4257. array('multiple' => 'checkbox')
  4258. );
  4259. $expected = array(
  4260. 'input' => array(
  4261. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4262. ),
  4263. array('div' => array('class' => 'checkbox')),
  4264. array('input' => array(
  4265. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4266. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  4267. )),
  4268. array('label' => array('for' => 'model-multi-field-a+')),
  4269. 'first',
  4270. '/label',
  4271. '/div',
  4272. array('div' => array('class' => 'checkbox')),
  4273. array('input' => array(
  4274. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4275. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  4276. )),
  4277. array('label' => array('for' => 'model-multi-field-a++')),
  4278. 'second',
  4279. '/label',
  4280. '/div',
  4281. array('div' => array('class' => 'checkbox')),
  4282. array('input' => array(
  4283. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4284. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  4285. )),
  4286. array('label' => array('for' => 'model-multi-field-a+++')),
  4287. 'third',
  4288. '/label',
  4289. '/div'
  4290. );
  4291. $this->assertTags($result, $expected);
  4292. $result = $this->Form->select(
  4293. 'Model.multi_field',
  4294. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4295. array('multiple' => 'checkbox')
  4296. );
  4297. $expected = array(
  4298. 'input' => array(
  4299. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4300. ),
  4301. array('div' => array('class' => 'checkbox')),
  4302. array('input' => array(
  4303. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4304. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  4305. )),
  4306. array('label' => array('for' => 'model-multi-field-a-b')),
  4307. 'first',
  4308. '/label',
  4309. '/div',
  4310. array('div' => array('class' => 'checkbox')),
  4311. array('input' => array(
  4312. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4313. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  4314. )),
  4315. array('label' => array('for' => 'model-multi-field-a-b1')),
  4316. 'second',
  4317. '/label',
  4318. '/div',
  4319. array('div' => array('class' => 'checkbox')),
  4320. array('input' => array(
  4321. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4322. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  4323. )),
  4324. array('label' => array('for' => 'model-multi-field-a-b2')),
  4325. 'third',
  4326. '/label',
  4327. '/div'
  4328. );
  4329. $this->assertTags($result, $expected);
  4330. }
  4331. /**
  4332. * Ensure that multiCheckbox reads from the request data.
  4333. *
  4334. * @return void
  4335. */
  4336. public function testSelectMultipleCheckboxRequestData() {
  4337. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4338. $result = $this->Form->select(
  4339. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4340. );
  4341. $expected = array(
  4342. 'input' => array(
  4343. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  4344. ),
  4345. array('div' => array('class' => 'checkbox')),
  4346. array('input' => array(
  4347. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4348. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  4349. )),
  4350. array('label' => array('for' => 'model-tags-1', 'class' => 'selected')),
  4351. 'first',
  4352. '/label',
  4353. '/div',
  4354. array('div' => array('class' => 'checkbox')),
  4355. array('input' => array(
  4356. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4357. 'value' => 'Array', 'id' => 'model-tags-array'
  4358. )),
  4359. array('label' => array('for' => 'model-tags-array')),
  4360. 'Array',
  4361. '/label',
  4362. '/div'
  4363. );
  4364. $this->assertTags($result, $expected);
  4365. }
  4366. /**
  4367. * Checks the security hash array generated for multiple-input checkbox elements
  4368. *
  4369. * @return void
  4370. */
  4371. public function testSelectMultipleCheckboxSecurity() {
  4372. $this->Form->request->params['_Token'] = 'testKey';
  4373. $this->assertEquals(array(), $this->Form->fields);
  4374. $result = $this->Form->select(
  4375. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4376. array('multiple' => 'checkbox')
  4377. );
  4378. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4379. $result = $this->Form->secure($this->Form->fields);
  4380. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4381. $this->assertRegExp('/"' . $key . '"/', $result);
  4382. }
  4383. /**
  4384. * Multiple select elements should always be secured as they always participate
  4385. * in the POST data.
  4386. *
  4387. * @return void
  4388. */
  4389. public function testSelectMultipleSecureWithNoOptions() {
  4390. $this->assertEquals(array(), $this->Form->fields);
  4391. $this->Form->select(
  4392. 'Model.select',
  4393. array(),
  4394. array('multiple' => true)
  4395. );
  4396. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4397. }
  4398. /**
  4399. * When a select box has no options it should not be added to the fields list
  4400. * as it always fail post validation.
  4401. *
  4402. * @return void
  4403. */
  4404. public function testSelectNoSecureWithNoOptions() {
  4405. $this->Form->request->params['_Token'] = 'testkey';
  4406. $this->assertEquals([], $this->Form->fields);
  4407. $this->Form->select(
  4408. 'Model.select',
  4409. []
  4410. );
  4411. $this->assertEquals([], $this->Form->fields);
  4412. $this->Form->select(
  4413. 'Model.user_id',
  4414. [],
  4415. ['empty' => true]
  4416. );
  4417. $this->assertEquals(array('Model.user_id'), $this->Form->fields);
  4418. }
  4419. /**
  4420. * testInputMultipleCheckboxes method
  4421. *
  4422. * test input() resulting in multi select elements being generated.
  4423. *
  4424. * @return void
  4425. */
  4426. public function testInputMultipleCheckboxes() {
  4427. $this->markTestIncomplete('Need to revisit once models work again.');
  4428. $result = $this->Form->input('Model.multi_field', array(
  4429. 'options' => array('first', 'second', 'third'),
  4430. 'multiple' => 'checkbox'
  4431. ));
  4432. $expected = array(
  4433. array('div' => array('class' => 'input select')),
  4434. array('label' => array('for' => 'ModelMultiField')),
  4435. 'Multi Field',
  4436. '/label',
  4437. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4438. array('div' => array('class' => 'checkbox')),
  4439. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4440. array('label' => array('for' => 'ModelMultiField0')),
  4441. 'first',
  4442. '/label',
  4443. '/div',
  4444. array('div' => array('class' => 'checkbox')),
  4445. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4446. array('label' => array('for' => 'ModelMultiField1')),
  4447. 'second',
  4448. '/label',
  4449. '/div',
  4450. array('div' => array('class' => 'checkbox')),
  4451. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4452. array('label' => array('for' => 'ModelMultiField2')),
  4453. 'third',
  4454. '/label',
  4455. '/div',
  4456. '/div'
  4457. );
  4458. $this->assertTags($result, $expected);
  4459. $result = $this->Form->input('Model.multi_field', array(
  4460. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4461. 'multiple' => 'checkbox'
  4462. ));
  4463. $expected = array(
  4464. array('div' => array('class' => 'input select')),
  4465. array('label' => array('for' => 'ModelMultiField')),
  4466. 'Multi Field',
  4467. '/label',
  4468. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4469. array('div' => array('class' => 'checkbox')),
  4470. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4471. array('label' => array('for' => 'ModelMultiFieldA')),
  4472. 'first',
  4473. '/label',
  4474. '/div',
  4475. array('div' => array('class' => 'checkbox')),
  4476. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4477. array('label' => array('for' => 'ModelMultiFieldB')),
  4478. 'second',
  4479. '/label',
  4480. '/div',
  4481. array('div' => array('class' => 'checkbox')),
  4482. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4483. array('label' => array('for' => 'ModelMultiFieldC')),
  4484. 'third',
  4485. '/label',
  4486. '/div',
  4487. '/div'
  4488. );
  4489. $this->assertTags($result, $expected);
  4490. $result = $this->Form->input('Model.multi_field', array(
  4491. 'options' => array('1' => 'first'),
  4492. 'multiple' => 'checkbox',
  4493. 'label' => false,
  4494. 'div' => false
  4495. ));
  4496. $expected = array(
  4497. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4498. array('div' => array('class' => 'checkbox')),
  4499. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4500. array('label' => array('for' => 'ModelMultiField1')),
  4501. 'first',
  4502. '/label',
  4503. '/div'
  4504. );
  4505. $this->assertTags($result, $expected);
  4506. $result = $this->Form->input('Model.multi_field', array(
  4507. 'options' => array('2' => 'second'),
  4508. 'multiple' => 'checkbox',
  4509. 'label' => false,
  4510. 'div' => false
  4511. ));
  4512. $expected = array(
  4513. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4514. array('div' => array('class' => 'checkbox')),
  4515. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4516. array('label' => array('for' => 'ModelMultiField2')),
  4517. 'second',
  4518. '/label',
  4519. '/div'
  4520. );
  4521. $this->assertTags($result, $expected);
  4522. }
  4523. /**
  4524. * testSelectHiddenFieldOmission method
  4525. *
  4526. * test that select() with 'hiddenField' => false omits the hidden field
  4527. *
  4528. * @return void
  4529. */
  4530. public function testSelectHiddenFieldOmission() {
  4531. $result = $this->Form->select('Model.multi_field',
  4532. array('first', 'second'),
  4533. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4534. );
  4535. $this->assertNotContains('type="hidden"', $result);
  4536. }
  4537. /**
  4538. * test that select() with multiple = checkbox works with overriding name attribute.
  4539. *
  4540. * @return void
  4541. */
  4542. public function testSelectCheckboxMultipleOverrideName() {
  4543. $result = $this->Form->select('category', ['1', '2'], [
  4544. 'multiple' => 'checkbox',
  4545. 'name' => 'fish',
  4546. ]);
  4547. $expected = array(
  4548. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => ''),
  4549. array('div' => array('class' => 'checkbox')),
  4550. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0')),
  4551. array('label' => array('for' => 'fish-0')), '1', '/label',
  4552. '/div',
  4553. array('div' => array('class' => 'checkbox')),
  4554. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1')),
  4555. array('label' => array('for' => 'fish-1')), '2', '/label',
  4556. '/div'
  4557. );
  4558. $this->assertTags($result, $expected);
  4559. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  4560. 'name' => 'fish',
  4561. ]);
  4562. $this->assertTags($result, $expected);
  4563. }
  4564. /**
  4565. * testCheckbox method
  4566. *
  4567. * Test generation of checkboxes
  4568. *
  4569. * @return void
  4570. */
  4571. public function testCheckbox() {
  4572. $result = $this->Form->checkbox('Model.field');
  4573. $expected = array(
  4574. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  4575. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'))
  4576. );
  4577. $this->assertTags($result, $expected);
  4578. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4579. $expected = array(
  4580. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  4581. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => 'myvalue', 'id' => 'theID'))
  4582. );
  4583. $this->assertTags($result, $expected);
  4584. }
  4585. /**
  4586. * testCheckboxDefaultValue method
  4587. *
  4588. * Test default value setting on checkbox() method
  4589. *
  4590. * @return void
  4591. */
  4592. public function testCheckboxDefaultValue() {
  4593. $this->Form->request->data['Model']['field'] = false;
  4594. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  4595. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  4596. unset($this->Form->request->data['Model']['field']);
  4597. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  4598. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  4599. $this->Form->request->data['Model']['field'] = true;
  4600. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  4601. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  4602. unset($this->Form->request->data['Model']['field']);
  4603. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  4604. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  4605. }
  4606. /**
  4607. * Test checkbox being checked or having errors.
  4608. *
  4609. * @return void
  4610. */
  4611. public function testCheckboxCheckedAndError() {
  4612. $this->article['errors'] = [
  4613. 'published' => true
  4614. ];
  4615. $this->Form->request->data['published'] = 'myvalue';
  4616. $this->Form->create($this->article);
  4617. $result = $this->Form->checkbox('published', array('id' => 'theID', 'value' => 'myvalue'));
  4618. $expected = array(
  4619. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4620. array('input' => array(
  4621. 'type' => 'checkbox',
  4622. 'name' => 'published',
  4623. 'value' => 'myvalue',
  4624. 'id' => 'theID',
  4625. 'checked' => 'checked',
  4626. 'class' => 'form-error'
  4627. ))
  4628. );
  4629. $this->assertTags($result, $expected);
  4630. $this->Form->request->data['published'] = '';
  4631. $result = $this->Form->checkbox('published');
  4632. $expected = array(
  4633. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4634. array('input' => array('type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error'))
  4635. );
  4636. $this->assertTags($result, $expected);
  4637. }
  4638. /**
  4639. * test checkbox() with a custom name attribute
  4640. *
  4641. * @return void
  4642. */
  4643. public function testCheckboxCustomNameAttribute() {
  4644. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4645. $expected = array(
  4646. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0'),
  4647. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1'))
  4648. );
  4649. $this->assertTags($result, $expected);
  4650. }
  4651. /**
  4652. * Test that the hidden input for checkboxes can be omitted or set to a
  4653. * specific value.
  4654. *
  4655. * @return void
  4656. */
  4657. public function testCheckboxHiddenField() {
  4658. $result = $this->Form->checkbox('UserForm.something', array(
  4659. 'hiddenField' => false
  4660. ));
  4661. $expected = array(
  4662. 'input' => array(
  4663. 'type' => 'checkbox',
  4664. 'name' => 'UserForm[something]',
  4665. 'value' => '1'
  4666. ),
  4667. );
  4668. $this->assertTags($result, $expected);
  4669. $result = $this->Form->checkbox('UserForm.something', array(
  4670. 'value' => 'Y',
  4671. 'hiddenField' => 'N',
  4672. ));
  4673. $expected = array(
  4674. array('input' => array(
  4675. 'type' => 'hidden', 'name' => 'UserForm[something]',
  4676. 'value' => 'N'
  4677. )),
  4678. array('input' => array(
  4679. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  4680. 'value' => 'Y'
  4681. )),
  4682. );
  4683. $this->assertTags($result, $expected);
  4684. }
  4685. /**
  4686. * testDateTime method
  4687. *
  4688. * Test generation of date/time select elements
  4689. *
  4690. * @return void
  4691. */
  4692. public function testDateTime() {
  4693. extract($this->dateRegex);
  4694. $result = $this->Form->dateTime('Contact.date', array('empty' => false));
  4695. $now = strtotime('now');
  4696. $expected = array(
  4697. array('select' => array('name' => 'Contact[date][month]')),
  4698. $monthsRegex,
  4699. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4700. date('F', $now),
  4701. '/option',
  4702. '*/select',
  4703. array('select' => array('name' => 'Contact[date][day]')),
  4704. $daysRegex,
  4705. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4706. date('j', $now),
  4707. '/option',
  4708. '*/select',
  4709. array('select' => array('name' => 'Contact[date][year]')),
  4710. $yearsRegex,
  4711. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4712. date('Y', $now),
  4713. '/option',
  4714. '*/select',
  4715. array('select' => array('name' => 'Contact[date][hour]')),
  4716. $hoursRegex,
  4717. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4718. date('g', $now),
  4719. '/option',
  4720. '*/select',
  4721. array('select' => array('name' => 'Contact[date][minute]')),
  4722. $minutesRegex,
  4723. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4724. date('i', $now),
  4725. '/option',
  4726. '*/select',
  4727. array('select' => array('name' => 'Contact[date][meridian]')),
  4728. $meridianRegex,
  4729. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4730. date('a', $now),
  4731. '/option',
  4732. '*/select'
  4733. );
  4734. $this->assertTags($result, $expected);
  4735. }
  4736. /**
  4737. * Test empty defaulting to true for datetime.
  4738. *
  4739. * @return void
  4740. */
  4741. public function testDatetimeEmpty() {
  4742. extract($this->dateRegex);
  4743. $now = strtotime('now');
  4744. $result = $this->Form->dateTime('Contact.date', array(
  4745. 'empty' => true,
  4746. ));
  4747. $expected = array(
  4748. array('select' => array('name' => 'Contact[date][month]')),
  4749. $monthsRegex,
  4750. array('option' => array('value' => '')),
  4751. '/option',
  4752. '*/select',
  4753. array('select' => array('name' => 'Contact[date][day]')),
  4754. $daysRegex,
  4755. array('option' => array('value' => '')),
  4756. '/option',
  4757. '*/select',
  4758. array('select' => array('name' => 'Contact[date][year]')),
  4759. $yearsRegex,
  4760. array('option' => array('value' => '')),
  4761. '/option',
  4762. '*/select',
  4763. array('select' => array('name' => 'Contact[date][hour]')),
  4764. $hoursRegex,
  4765. array('option' => array('value' => '')),
  4766. '/option',
  4767. '*/select',
  4768. array('select' => array('name' => 'Contact[date][minute]')),
  4769. $minutesRegex,
  4770. array('option' => array('value' => '')),
  4771. '/option',
  4772. '*/select',
  4773. array('select' => array('name' => 'Contact[date][meridian]')),
  4774. $meridianRegex,
  4775. array('option' => array('value' => '')),
  4776. '/option',
  4777. '*/select'
  4778. );
  4779. $this->assertTags($result, $expected);
  4780. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4781. }
  4782. /**
  4783. * Test datetime with interval option.
  4784. *
  4785. * @return void
  4786. */
  4787. public function testDatetimeMinuteInterval() {
  4788. extract($this->dateRegex);
  4789. $now = strtotime('now');
  4790. $result = $this->Form->dateTime('Contact.date', array(
  4791. 'interval' => 5,
  4792. 'value' => ''
  4793. ));
  4794. $expected = array(
  4795. array('select' => array('name' => 'Contact[date][month]')),
  4796. $monthsRegex,
  4797. array('option' => array('selected' => 'selected', 'value' => '')),
  4798. '/option',
  4799. '*/select',
  4800. array('select' => array('name' => 'Contact[date][day]')),
  4801. $daysRegex,
  4802. array('option' => array('selected' => 'selected', 'value' => '')),
  4803. '/option',
  4804. '*/select',
  4805. array('select' => array('name' => 'Contact[date][year]')),
  4806. $yearsRegex,
  4807. array('option' => array('selected' => 'selected', 'value' => '')),
  4808. '/option',
  4809. '*/select',
  4810. array('select' => array('name' => 'Contact[date][hour]')),
  4811. $hoursRegex,
  4812. array('option' => array('selected' => 'selected', 'value' => '')),
  4813. '/option',
  4814. '*/select',
  4815. array('select' => array('name' => 'Contact[date][minute]')),
  4816. $minutesRegex,
  4817. array('option' => array('selected' => 'selected', 'value' => '')),
  4818. '/option',
  4819. array('option' => array('value' => '00')),
  4820. '00',
  4821. '/option',
  4822. array('option' => array('value' => '05')),
  4823. '05',
  4824. '/option',
  4825. array('option' => array('value' => '10')),
  4826. '10',
  4827. '/option',
  4828. '*/select',
  4829. );
  4830. $this->assertTags($result, $expected);
  4831. }
  4832. /**
  4833. * Test dateTime with rounding
  4834. *
  4835. * @return void
  4836. */
  4837. public function testDateTimeRounding() {
  4838. $this->Form->request->data['Contact'] = array(
  4839. 'date' => array(
  4840. 'day' => '13',
  4841. 'month' => '12',
  4842. 'year' => '2010',
  4843. 'hour' => '04',
  4844. 'minute' => '19',
  4845. 'meridian' => 'AM'
  4846. )
  4847. );
  4848. $result = $this->Form->dateTime('Contact.date', array('interval' => 15));
  4849. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4850. $result = $this->Form->dateTime('Contact.date', array('interval' => 15, 'round' => 'up'));
  4851. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  4852. $result = $this->Form->dateTime('Contact.date', array('interval' => 5, 'round' => 'down'));
  4853. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4854. }
  4855. /**
  4856. * test that datetime() and default values work.
  4857. *
  4858. * @return void
  4859. */
  4860. public function testDatetimeWithDefault() {
  4861. $this->markTestIncomplete('Need to revisit soon.');
  4862. $result = $this->Form->dateTime('Contact.updated', array('value' => '2009-06-01 11:15:30'));
  4863. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4864. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4865. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4866. $result = $this->Form->dateTime('Contact.updated', array(
  4867. 'default' => '2009-06-01 11:15:30'
  4868. ));
  4869. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4870. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4871. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4872. }
  4873. /**
  4874. * testDateTime all zeros
  4875. *
  4876. * @return void
  4877. */
  4878. public function testDateTimeAllZeros() {
  4879. $result = $this->Form->dateTime('Contact.date', array(
  4880. 'timeFormat' => false,
  4881. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  4882. 'value' => '0000-00-00'
  4883. ));
  4884. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  4885. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  4886. }
  4887. /**
  4888. * testDateTimeEmptyAsArray
  4889. *
  4890. * @return void
  4891. */
  4892. public function testDateTimeEmptyAsArray() {
  4893. $result = $this->Form->dateTime('Contact.date', array(
  4894. 'empty' => array(
  4895. 'day' => 'DAY',
  4896. 'month' => 'MONTH',
  4897. 'year' => 'YEAR',
  4898. 'hour' => 'HOUR',
  4899. 'minute' => 'MINUTE',
  4900. 'meridian' => false
  4901. )
  4902. ));
  4903. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4904. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4905. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4906. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4907. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4908. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4909. $result = $this->Form->dateTime('Contact.date', array(
  4910. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4911. ));
  4912. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4913. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4914. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4915. }
  4916. /**
  4917. * testFormDateTimeMulti method
  4918. *
  4919. * test multiple datetime element generation
  4920. *
  4921. * @return void
  4922. */
  4923. public function testFormDateTimeMulti() {
  4924. extract($this->dateRegex);
  4925. $result = $this->Form->dateTime('Contact.1.updated');
  4926. $this->assertContains('Contact[1][updated][month]', $result);
  4927. $this->assertContains('Contact[1][updated][day]', $result);
  4928. $this->assertContains('Contact[1][updated][year]', $result);
  4929. $this->assertContains('Contact[1][updated][hour]', $result);
  4930. $this->assertContains('Contact[1][updated][minute]', $result);
  4931. $this->assertContains('Contact[1][updated][meridian]', $result);
  4932. }
  4933. /**
  4934. * When changing the date format, the label should always focus the first select box when
  4935. * clicked.
  4936. *
  4937. * @return void
  4938. */
  4939. public function testDateTimeLabelIdMatchesFirstInput() {
  4940. $this->markTestIncomplete('Need to revisit once models work again.');
  4941. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4942. $this->assertContains('label for="ModelDateMonth"', $result);
  4943. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4944. $this->assertContains('label for="ModelDateDay"', $result);
  4945. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4946. $this->assertContains('label for="ModelDateYear"', $result);
  4947. }
  4948. /**
  4949. * testMonth method
  4950. *
  4951. * @return void
  4952. */
  4953. public function testMonth() {
  4954. $result = $this->Form->month('Model.field', ['value' => '']);
  4955. $expected = array(
  4956. array('select' => array('name' => 'Model[field][month]')),
  4957. array('option' => array('value' => '', 'selected' => 'selected')),
  4958. '/option',
  4959. array('option' => array('value' => '01')),
  4960. date('F', strtotime('2008-01-01 00:00:00')),
  4961. '/option',
  4962. array('option' => array('value' => '02')),
  4963. date('F', strtotime('2008-02-01 00:00:00')),
  4964. '/option',
  4965. '*/select',
  4966. );
  4967. $this->assertTags($result, $expected);
  4968. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  4969. $expected = array(
  4970. array('select' => array('name' => 'Model[field][month]')),
  4971. array('option' => array('selected' => 'selected', 'value' => '')),
  4972. '/option',
  4973. array('option' => array('value' => '01')),
  4974. date('F', strtotime('2008-01-01 00:00:00')),
  4975. '/option',
  4976. array('option' => array('value' => '02')),
  4977. date('F', strtotime('2008-02-01 00:00:00')),
  4978. '/option',
  4979. '*/select',
  4980. );
  4981. $this->assertTags($result, $expected);
  4982. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  4983. $expected = array(
  4984. array('select' => array('name' => 'Model[field][month]')),
  4985. array('option' => array('selected' => 'selected', 'value' => '')),
  4986. '/option',
  4987. array('option' => array('value' => '01')),
  4988. '1',
  4989. '/option',
  4990. array('option' => array('value' => '02')),
  4991. '2',
  4992. '/option',
  4993. '*/select',
  4994. );
  4995. $this->assertTags($result, $expected);
  4996. $monthNames = [
  4997. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4998. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  4999. ];
  5000. $result = $this->Form->month('Model.field', array('value' => '1', 'monthNames' => $monthNames));
  5001. $expected = array(
  5002. array('select' => array('name' => 'Model[field][month]')),
  5003. array('option' => array('value' => '')),
  5004. '/option',
  5005. array('option' => array('value' => '01', 'selected' => 'selected')),
  5006. 'Jan',
  5007. '/option',
  5008. array('option' => array('value' => '02')),
  5009. 'Feb',
  5010. '/option',
  5011. '*/select',
  5012. );
  5013. $this->assertTags($result, $expected);
  5014. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5015. $result = $this->Form->month('Project.release');
  5016. $expected = array(
  5017. array('select' => array('name' => 'Project[release][month]')),
  5018. array('option' => array('value' => '')),
  5019. '/option',
  5020. array('option' => array('value' => '01')),
  5021. 'January',
  5022. '/option',
  5023. array('option' => array('value' => '02', 'selected' => 'selected')),
  5024. 'February',
  5025. '/option',
  5026. '*/select',
  5027. );
  5028. $this->assertTags($result, $expected);
  5029. }
  5030. /**
  5031. * testDay method
  5032. *
  5033. * @return void
  5034. */
  5035. public function testDay() {
  5036. extract($this->dateRegex);
  5037. $result = $this->Form->day('Model.field', array('value' => false));
  5038. $expected = array(
  5039. array('select' => array('name' => 'Model[field][day]')),
  5040. array('option' => array('selected' => 'selected', 'value' => '')),
  5041. '/option',
  5042. array('option' => array('value' => '01')),
  5043. '1',
  5044. '/option',
  5045. array('option' => array('value' => '02')),
  5046. '2',
  5047. '/option',
  5048. $daysRegex,
  5049. '/select',
  5050. );
  5051. $this->assertTags($result, $expected);
  5052. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5053. $result = $this->Form->day('Model.field');
  5054. $expected = array(
  5055. array('select' => array('name' => 'Model[field][day]')),
  5056. array('option' => array('value' => '')),
  5057. '/option',
  5058. array('option' => array('value' => '01')),
  5059. '1',
  5060. '/option',
  5061. array('option' => array('value' => '02')),
  5062. '2',
  5063. '/option',
  5064. $daysRegex,
  5065. array('option' => array('value' => '10', 'selected' => 'selected')),
  5066. '10',
  5067. '/option',
  5068. $daysRegex,
  5069. '/select',
  5070. );
  5071. $this->assertTags($result, $expected);
  5072. $this->Form->request->data['Model']['field'] = '';
  5073. $result = $this->Form->day('Model.field', array('value' => '10'));
  5074. $expected = array(
  5075. array('select' => array('name' => 'Model[field][day]')),
  5076. array('option' => array('value' => '')),
  5077. '/option',
  5078. array('option' => array('value' => '01')),
  5079. '1',
  5080. '/option',
  5081. array('option' => array('value' => '02')),
  5082. '2',
  5083. '/option',
  5084. $daysRegex,
  5085. array('option' => array('value' => '10', 'selected' => 'selected')),
  5086. '10',
  5087. '/option',
  5088. $daysRegex,
  5089. '/select',
  5090. );
  5091. $this->assertTags($result, $expected);
  5092. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5093. $result = $this->Form->day('Project.release');
  5094. $expected = array(
  5095. array('select' => array('name' => 'Project[release][day]')),
  5096. array('option' => array('value' => '')),
  5097. '/option',
  5098. array('option' => array('value' => '01')),
  5099. '1',
  5100. '/option',
  5101. array('option' => array('value' => '02')),
  5102. '2',
  5103. '/option',
  5104. $daysRegex,
  5105. array('option' => array('value' => '10', 'selected' => 'selected')),
  5106. '10',
  5107. '/option',
  5108. $daysRegex,
  5109. '/select',
  5110. );
  5111. $this->assertTags($result, $expected);
  5112. }
  5113. /**
  5114. * testMinute method
  5115. *
  5116. * @return void
  5117. */
  5118. public function testMinute() {
  5119. extract($this->dateRegex);
  5120. $result = $this->Form->minute('Model.field', ['value' => '']);
  5121. $expected = array(
  5122. array('select' => array('name' => 'Model[field][minute]')),
  5123. array('option' => array('selected' => 'selected', 'value' => '')),
  5124. '/option',
  5125. array('option' => array('value' => '00')),
  5126. '00',
  5127. '/option',
  5128. array('option' => array('value' => '01')),
  5129. '01',
  5130. '/option',
  5131. array('option' => array('value' => '02')),
  5132. '02',
  5133. '/option',
  5134. $minutesRegex,
  5135. '/select',
  5136. );
  5137. $this->assertTags($result, $expected);
  5138. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5139. $result = $this->Form->minute('Model.field');
  5140. $expected = array(
  5141. array('select' => array('name' => 'Model[field][minute]')),
  5142. array('option' => array('value' => '')),
  5143. '/option',
  5144. array('option' => array('value' => '00')),
  5145. '00',
  5146. '/option',
  5147. array('option' => array('value' => '01')),
  5148. '01',
  5149. '/option',
  5150. array('option' => array('value' => '02')),
  5151. '02',
  5152. '/option',
  5153. $minutesRegex,
  5154. array('option' => array('value' => '12', 'selected' => 'selected')),
  5155. '12',
  5156. '/option',
  5157. $minutesRegex,
  5158. '/select',
  5159. );
  5160. $this->assertTags($result, $expected);
  5161. $this->Form->request->data['Model']['field'] = '';
  5162. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5163. $expected = array(
  5164. array('select' => array('name' => 'Model[field][minute]')),
  5165. array('option' => array('selected' => 'selected', 'value' => '')),
  5166. '/option',
  5167. array('option' => array('value' => '00')),
  5168. '00',
  5169. '/option',
  5170. array('option' => array('value' => '05')),
  5171. '05',
  5172. '/option',
  5173. array('option' => array('value' => '10')),
  5174. '10',
  5175. '/option',
  5176. $minutesRegex,
  5177. '/select',
  5178. );
  5179. $this->assertTags($result, $expected);
  5180. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5181. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5182. $expected = array(
  5183. array('select' => array('name' => 'Model[field][minute]')),
  5184. array('option' => array('value' => '')),
  5185. '/option',
  5186. array('option' => array('value' => '00')),
  5187. '00',
  5188. '/option',
  5189. array('option' => array('value' => '05')),
  5190. '05',
  5191. '/option',
  5192. array('option' => array('value' => '10', 'selected' => 'selected')),
  5193. '10',
  5194. '/option',
  5195. $minutesRegex,
  5196. '/select',
  5197. );
  5198. $this->assertTags($result, $expected);
  5199. }
  5200. /**
  5201. * testHour method
  5202. *
  5203. * @return void
  5204. */
  5205. public function testHour() {
  5206. extract($this->dateRegex);
  5207. $result = $this->Form->hour('Model.field', ['value' => '']);
  5208. $expected = array(
  5209. array('select' => array('name' => 'Model[field][hour]')),
  5210. array('option' => array('selected' => 'selected', 'value' => '')),
  5211. '/option',
  5212. array('option' => array('value' => '01')),
  5213. '1',
  5214. '/option',
  5215. array('option' => array('value' => '02')),
  5216. '2',
  5217. '/option',
  5218. $hoursRegex,
  5219. '/select',
  5220. );
  5221. $this->assertTags($result, $expected);
  5222. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5223. $result = $this->Form->hour('Model.field', ['format' => 12]);
  5224. $expected = array(
  5225. array('select' => array('name' => 'Model[field][hour]')),
  5226. array('option' => array('value' => '')),
  5227. '/option',
  5228. array('option' => array('value' => '01')),
  5229. '1',
  5230. '/option',
  5231. array('option' => array('value' => '02')),
  5232. '2',
  5233. '/option',
  5234. $hoursRegex,
  5235. array('option' => array('value' => '12', 'selected' => 'selected')),
  5236. '12',
  5237. '/option',
  5238. '/select',
  5239. );
  5240. $this->assertTags($result, $expected);
  5241. $this->Form->request->data['Model']['field'] = '';
  5242. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => '23'));
  5243. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5244. $result = $this->Form->hour('Model.field', array('format' => 12, 'value' => '23'));
  5245. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5246. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5247. $result = $this->Form->hour('Model.field', ['format' => 24]);
  5248. $expected = array(
  5249. array('select' => array('name' => 'Model[field][hour]')),
  5250. array('option' => array('value' => '')),
  5251. '/option',
  5252. array('option' => array('value' => '00', 'selected' => 'selected')),
  5253. '0',
  5254. '/option',
  5255. array('option' => array('value' => '01')),
  5256. '1',
  5257. '/option',
  5258. array('option' => array('value' => '02')),
  5259. '2',
  5260. '/option',
  5261. $hoursRegex,
  5262. '/select',
  5263. );
  5264. $this->assertTags($result, $expected);
  5265. unset($this->Form->request->data['Model']['field']);
  5266. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => 'now'));
  5267. $thisHour = date('H');
  5268. $optValue = date('G');
  5269. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5270. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  5271. $result = $this->Form->hour('Model.field', ['format' => 24]);
  5272. $expected = array(
  5273. array('select' => array('name' => 'Model[field][hour]')),
  5274. array('option' => array('value' => '')),
  5275. '/option',
  5276. array('option' => array('value' => '00')),
  5277. '0',
  5278. '/option',
  5279. array('option' => array('value' => '01', 'selected' => 'selected')),
  5280. '1',
  5281. '/option',
  5282. array('option' => array('value' => '02')),
  5283. '2',
  5284. '/option',
  5285. $hoursRegex,
  5286. '/select',
  5287. );
  5288. $this->assertTags($result, $expected);
  5289. }
  5290. /**
  5291. * testYear method
  5292. *
  5293. * @return void
  5294. */
  5295. public function testYear() {
  5296. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  5297. $expected = array(
  5298. array('select' => array('name' => 'Model[field][year]')),
  5299. array('option' => array('selected' => 'selected', 'value' => '')),
  5300. '/option',
  5301. array('option' => array('value' => '2007')),
  5302. '2007',
  5303. '/option',
  5304. array('option' => array('value' => '2006')),
  5305. '2006',
  5306. '/option',
  5307. '/select',
  5308. );
  5309. $this->assertTags($result, $expected);
  5310. $result = $this->Form->year('Model.field', [
  5311. 'value' => '',
  5312. 'minYear' => 2006,
  5313. 'maxYear' => 2007,
  5314. 'orderYear' => 'asc'
  5315. ]);
  5316. $expected = array(
  5317. array('select' => array('name' => 'Model[field][year]')),
  5318. array('option' => array('selected' => 'selected', 'value' => '')),
  5319. '/option',
  5320. array('option' => array('value' => '2006')),
  5321. '2006',
  5322. '/option',
  5323. array('option' => array('value' => '2007')),
  5324. '2007',
  5325. '/option',
  5326. '/select',
  5327. );
  5328. $this->assertTags($result, $expected);
  5329. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5330. $result = $this->Form->year('Contact.published', [
  5331. 'empty' => false,
  5332. 'minYear' => 2006,
  5333. 'maxYear' => 2007,
  5334. ]);
  5335. $expected = array(
  5336. array('select' => array('name' => 'Contact[published][year]')),
  5337. array('option' => array('value' => '2007')),
  5338. '2007',
  5339. '/option',
  5340. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5341. '2006',
  5342. '/option',
  5343. '/select',
  5344. );
  5345. $this->assertTags($result, $expected);
  5346. }
  5347. /**
  5348. * testYearAutoExpandRange method
  5349. *
  5350. * @return void
  5351. */
  5352. public function testYearAutoExpandRange() {
  5353. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  5354. $result = $this->Form->year('User.birthday');
  5355. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5356. $result = $matches[1];
  5357. $expected = range(date('Y') + 5, 1930);
  5358. $this->assertEquals($expected, $result);
  5359. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5360. $result = $this->Form->year('Project.release');
  5361. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5362. $result = $matches[1];
  5363. $expected = range(2050, date('Y') - 5);
  5364. $this->assertEquals($expected, $result);
  5365. $this->Form->request->data['Project']['release'] = '1881-10-10';
  5366. $result = $this->Form->year('Project.release', [
  5367. 'minYear' => 1890,
  5368. 'maxYear' => 1900
  5369. ]);
  5370. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5371. $result = $matches[1];
  5372. $expected = range(1900, 1881);
  5373. $this->assertEquals($expected, $result);
  5374. }
  5375. /**
  5376. * testInputDate method
  5377. *
  5378. * Test various inputs with type date and different dateFormat values.
  5379. * Failing to provide a dateFormat key should not error.
  5380. * It should simply not pre-select any value then.
  5381. *
  5382. * @return void
  5383. */
  5384. public function testInputDate() {
  5385. $this->markTestIncomplete('Need to revisit once models work again.');
  5386. $this->Form->request->data = array(
  5387. 'User' => array(
  5388. 'month_year' => array('month' => date('m')),
  5389. 'just_year' => array('month' => date('m')),
  5390. 'just_month' => array('year' => date('Y')),
  5391. 'just_day' => array('month' => date('m')),
  5392. )
  5393. );
  5394. $this->Form->create('User');
  5395. $result = $this->Form->input('month_year',
  5396. array(
  5397. 'label' => false,
  5398. 'div' => false,
  5399. 'type' => 'date',
  5400. 'dateFormat' => 'MY',
  5401. 'minYear' => 2006,
  5402. 'maxYear' => 2008
  5403. )
  5404. );
  5405. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  5406. $this->assertNotContains('value="2008" selected="selected"', $result);
  5407. $result = $this->Form->input('just_year',
  5408. array(
  5409. 'type' => 'date',
  5410. 'label' => false,
  5411. 'dateFormat' => 'Y',
  5412. 'minYear' => date('Y'),
  5413. 'maxYear' => date('Y', strtotime('+20 years'))
  5414. )
  5415. );
  5416. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  5417. $result = $this->Form->input('just_month',
  5418. array(
  5419. 'type' => 'date',
  5420. 'label' => false,
  5421. 'dateFormat' => 'M',
  5422. 'empty' => false,
  5423. )
  5424. );
  5425. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  5426. $result = $this->Form->input('just_day',
  5427. array(
  5428. 'type' => 'date',
  5429. 'label' => false,
  5430. 'dateFormat' => 'D',
  5431. 'empty' => false,
  5432. )
  5433. );
  5434. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  5435. }
  5436. /**
  5437. * testInputDateMaxYear method
  5438. *
  5439. * Let's say we want to only allow users born from 2006 to 2008 to register
  5440. * This being the first singup page, we still don't have any data
  5441. *
  5442. * @return void
  5443. */
  5444. public function testInputDateMaxYear() {
  5445. $this->markTestIncomplete('Need to revisit once models work again.');
  5446. $this->Form->request->data = array();
  5447. $this->Form->create('User');
  5448. $result = $this->Form->input('birthday',
  5449. array(
  5450. 'label' => false,
  5451. 'div' => false,
  5452. 'type' => 'date',
  5453. 'dateFormat' => 'DMY',
  5454. 'minYear' => 2006,
  5455. 'maxYear' => 2008
  5456. )
  5457. );
  5458. $this->assertContains('value="2008" selected="selected"', $result);
  5459. }
  5460. /**
  5461. * testTextArea method
  5462. *
  5463. * @return void
  5464. */
  5465. public function testTextArea() {
  5466. $this->Form->request->data = array('field' => 'some test data');
  5467. $result = $this->Form->textarea('field');
  5468. $expected = array(
  5469. 'textarea' => array('name' => 'field'),
  5470. 'some test data',
  5471. '/textarea',
  5472. );
  5473. $this->assertTags($result, $expected);
  5474. $result = $this->Form->textarea('user.bio');
  5475. $expected = array(
  5476. 'textarea' => array('name' => 'user[bio]'),
  5477. '/textarea',
  5478. );
  5479. $this->assertTags($result, $expected);
  5480. $this->Form->request->data = array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  5481. $result = $this->Form->textarea('field');
  5482. $expected = array(
  5483. 'textarea' => array('name' => 'field'),
  5484. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5485. '/textarea',
  5486. );
  5487. $this->assertTags($result, $expected);
  5488. $this->Form->request->data = [
  5489. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  5490. ];
  5491. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5492. $expected = array(
  5493. 'textarea' => array('name' => 'Model[field]'),
  5494. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5495. '/textarea',
  5496. );
  5497. $this->assertTags($result, $expected);
  5498. $result = $this->Form->textarea('0.OtherModel.field');
  5499. $expected = array(
  5500. 'textarea' => array('name' => '0[OtherModel][field]'),
  5501. '/textarea'
  5502. );
  5503. $this->assertTags($result, $expected);
  5504. }
  5505. /**
  5506. * testTextAreaWithStupidCharacters method
  5507. *
  5508. * test text area with non-ascii characters
  5509. *
  5510. * @return void
  5511. */
  5512. public function testTextAreaWithStupidCharacters() {
  5513. $result = $this->Form->textarea('Post.content', [
  5514. 'value' => "GREAT®",
  5515. 'rows' => '15',
  5516. 'cols' => '75'
  5517. ]);
  5518. $expected = [
  5519. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5520. 'GREAT®',
  5521. '/textarea',
  5522. ];
  5523. $this->assertTags($result, $expected);
  5524. }
  5525. /**
  5526. * testHiddenField method
  5527. *
  5528. * @return void
  5529. */
  5530. public function testHiddenField() {
  5531. $this->article['errors'] = [
  5532. 'field' => true
  5533. ];
  5534. $this->Form->request->data['field'] = 'test';
  5535. $this->Form->create($this->article);
  5536. $result = $this->Form->hidden('field', array('id' => 'theID'));
  5537. $this->assertTags($result, array(
  5538. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test'))
  5539. );
  5540. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5541. $expected = [
  5542. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  5543. ];
  5544. $this->assertTags($result, $expected);
  5545. }
  5546. /**
  5547. * testFileUploadField method
  5548. *
  5549. * @return void
  5550. */
  5551. public function testFileUploadField() {
  5552. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5553. $result = $this->Form->file('Model.upload');
  5554. $this->assertTags($result, $expected);
  5555. $this->Form->request->data['Model']['upload'] = [
  5556. 'name' => '', 'type' => '', 'tmp_name' => '',
  5557. 'error' => 4, 'size' => 0
  5558. ];
  5559. $result = $this->Form->file('Model.upload');
  5560. $this->assertTags($result, $expected);
  5561. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  5562. $result = $this->Form->file('Model.upload');
  5563. $this->assertTags($result, $expected);
  5564. }
  5565. /**
  5566. * test File upload input on a model not used in create();
  5567. *
  5568. * @return void
  5569. */
  5570. public function testFileUploadOnOtherModel() {
  5571. $this->Form->create($this->article, array('type' => 'file'));
  5572. $result = $this->Form->file('ValidateProfile.city');
  5573. $expected = array(
  5574. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]')
  5575. );
  5576. $this->assertTags($result, $expected);
  5577. }
  5578. /**
  5579. * testButton method
  5580. *
  5581. * @return void
  5582. */
  5583. public function testButton() {
  5584. $result = $this->Form->button('Hi');
  5585. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5586. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5587. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5588. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5589. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5590. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5591. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5592. $result = $this->Form->button('No type', array('type' => false));
  5593. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5594. $result = $this->Form->button('Upload Text', array(
  5595. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  5596. 'escape' => false
  5597. ));
  5598. $this->assertNotRegExp('/\&039/', $result);
  5599. }
  5600. /**
  5601. * Test that button() makes unlocked fields by default.
  5602. *
  5603. * @return void
  5604. */
  5605. public function testButtonUnlockedByDefault() {
  5606. $this->Form->request->params['_csrfToken'] = 'secured';
  5607. $this->Form->button('Save', array('name' => 'save'));
  5608. $this->Form->button('Clear');
  5609. $result = $this->Form->unlockField();
  5610. $this->assertEquals(array('save'), $result);
  5611. }
  5612. /**
  5613. * testPostButton method
  5614. *
  5615. * @return void
  5616. */
  5617. public function testPostButton() {
  5618. $result = $this->Form->postButton('Hi', '/controller/action');
  5619. $this->assertTags($result, array(
  5620. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5621. 'div' => array('style' => 'display:none;'),
  5622. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5623. '/div',
  5624. 'button' => array('type' => 'submit'),
  5625. 'Hi',
  5626. '/button',
  5627. '/form'
  5628. ));
  5629. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5630. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  5631. }
  5632. /**
  5633. * Test using postButton with N dimensional data.
  5634. *
  5635. * @return void
  5636. */
  5637. public function testPostButtonNestedData() {
  5638. $data = array(
  5639. 'one' => array(
  5640. 'two' => array(
  5641. 3, 4, 5
  5642. )
  5643. )
  5644. );
  5645. $result = $this->Form->postButton('Send', '/', array('data' => $data));
  5646. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5647. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5648. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5649. }
  5650. /**
  5651. * Test that postButton adds _Token fields.
  5652. *
  5653. * @return void
  5654. */
  5655. public function testSecurePostButton() {
  5656. $this->Form->request->params['_csrfToken'] = 'testkey';
  5657. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5658. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5659. $expected = array(
  5660. 'form' => array(
  5661. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5662. ),
  5663. array('div' => array('style' => 'display:none;')),
  5664. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5665. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5666. '/div',
  5667. 'button' => array('type' => 'submit'),
  5668. 'Delete',
  5669. '/button',
  5670. array('div' => array('style' => 'display:none;')),
  5671. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5672. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5673. '/div',
  5674. '/form',
  5675. );
  5676. $this->assertTags($result, $expected);
  5677. }
  5678. /**
  5679. * testPostLink method
  5680. *
  5681. * @return void
  5682. */
  5683. public function testPostLink() {
  5684. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5685. $this->assertTags($result, array(
  5686. 'form' => array(
  5687. 'method' => 'post', 'action' => '/posts/delete/1',
  5688. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5689. ),
  5690. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5691. '/form',
  5692. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5693. 'Delete',
  5694. '/a'
  5695. ));
  5696. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5697. $this->assertTags($result, array(
  5698. 'form' => array(
  5699. 'method' => 'post', 'action' => '/posts/delete/1',
  5700. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5701. ),
  5702. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5703. '/form',
  5704. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5705. 'Delete',
  5706. '/a'
  5707. ));
  5708. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5709. $this->assertTags($result, array(
  5710. 'form' => array(
  5711. 'method' => 'post', 'action' => '/posts/delete/1',
  5712. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5713. ),
  5714. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5715. '/form',
  5716. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5717. 'Delete',
  5718. '/a'
  5719. ));
  5720. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  5721. $this->assertTags($result, array(
  5722. 'form' => array(
  5723. 'method' => 'post', 'action' => '/posts/delete/1',
  5724. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5725. ),
  5726. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5727. '/form',
  5728. '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;/'),
  5729. 'Delete',
  5730. '/a'
  5731. ));
  5732. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5733. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  5734. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  5735. $this->assertTags($result, array(
  5736. 'form' => array(
  5737. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  5738. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5739. ),
  5740. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5741. '/form',
  5742. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5743. 'Delete',
  5744. '/a'
  5745. ));
  5746. $result = $this->Form->postLink(
  5747. '',
  5748. array('controller' => 'items', 'action' => 'delete', 10),
  5749. array('class' => 'btn btn-danger', 'escape' => false),
  5750. 'Confirm thing'
  5751. );
  5752. $this->assertTags($result, array(
  5753. 'form' => array(
  5754. 'method' => 'post', 'action' => '/items/delete/10',
  5755. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5756. ),
  5757. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5758. '/form',
  5759. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5760. '/a'
  5761. ));
  5762. }
  5763. /**
  5764. * Test using postLink with N dimensional data.
  5765. *
  5766. * @return void
  5767. */
  5768. public function testPostLinkNestedData() {
  5769. $data = array(
  5770. 'one' => array(
  5771. 'two' => array(
  5772. 3, 4, 5
  5773. )
  5774. )
  5775. );
  5776. $result = $this->Form->postLink('Send', '/', array('data' => $data));
  5777. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5778. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5779. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5780. }
  5781. /**
  5782. * test creating postLinks after a GET form.
  5783. *
  5784. * @return void
  5785. */
  5786. public function testPostLinkAfterGetForm() {
  5787. $this->Form->request->params['_csrfToken'] = 'testkey';
  5788. $this->Form->request->params['_Token'] = 'val';
  5789. $this->Form->create($this->article, array('type' => 'get'));
  5790. $this->Form->end();
  5791. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5792. $this->assertTags($result, array(
  5793. 'form' => array(
  5794. 'method' => 'post', 'action' => '/posts/delete/1',
  5795. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5796. ),
  5797. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5798. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5799. 'div' => array('style' => 'display:none;'),
  5800. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5801. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5802. '/div',
  5803. '/form',
  5804. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5805. 'Delete',
  5806. '/a'
  5807. ));
  5808. }
  5809. /**
  5810. * Test that postLink adds form tags to view block
  5811. *
  5812. * @return void
  5813. */
  5814. public function testPostLinkFormBuffer() {
  5815. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
  5816. $this->assertTags($result, array(
  5817. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5818. 'Delete',
  5819. '/a'
  5820. ));
  5821. $result = $this->View->fetch('postLink');
  5822. $this->assertTags($result, array(
  5823. 'form' => array(
  5824. 'method' => 'post', 'action' => '/posts/delete/1',
  5825. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5826. ),
  5827. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5828. '/form'
  5829. ));
  5830. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  5831. array('inline' => false, 'method' => 'DELETE')
  5832. );
  5833. $this->assertTags($result, array(
  5834. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5835. 'Delete',
  5836. '/a'
  5837. ));
  5838. $result = $this->View->fetch('postLink');
  5839. $this->assertTags($result, array(
  5840. 'form' => array(
  5841. 'method' => 'post', 'action' => '/posts/delete/1',
  5842. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5843. ),
  5844. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5845. '/form',
  5846. array(
  5847. 'form' => array(
  5848. 'method' => 'post', 'action' => '/posts/delete/2',
  5849. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5850. ),
  5851. ),
  5852. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  5853. '/form'
  5854. ));
  5855. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  5856. $this->assertTags($result, array(
  5857. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5858. 'Delete',
  5859. '/a'
  5860. ));
  5861. $result = $this->View->fetch('foobar');
  5862. $this->assertTags($result, array(
  5863. 'form' => array(
  5864. 'method' => 'post', 'action' => '/posts/delete/1',
  5865. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5866. ),
  5867. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5868. '/form'
  5869. ));
  5870. }
  5871. /**
  5872. * testSubmitButton method
  5873. *
  5874. * @return void
  5875. */
  5876. public function testSubmitButton() {
  5877. $result = $this->Form->submit('');
  5878. $expected = array(
  5879. 'div' => array('class' => 'submit'),
  5880. 'input' => array('type' => 'submit', 'value' => ''),
  5881. '/div'
  5882. );
  5883. $this->assertTags($result, $expected);
  5884. $result = $this->Form->submit('Test Submit');
  5885. $expected = array(
  5886. 'div' => array('class' => 'submit'),
  5887. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5888. '/div'
  5889. );
  5890. $this->assertTags($result, $expected);
  5891. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5892. $expected = array(
  5893. 'span' => array('class' => 'submit'),
  5894. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5895. '/span'
  5896. );
  5897. $this->assertTags($result, $expected);
  5898. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5899. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5900. $this->assertTags($result, $expected);
  5901. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5902. $expected = array(
  5903. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5904. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5905. '/div'
  5906. );
  5907. $this->assertTags($result, $expected);
  5908. $result = $this->Form->submit('Next >');
  5909. $expected = array(
  5910. 'div' => array('class' => 'submit'),
  5911. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5912. '/div'
  5913. );
  5914. $this->assertTags($result, $expected);
  5915. $result = $this->Form->submit('Next >', array('escape' => false));
  5916. $expected = array(
  5917. 'div' => array('class' => 'submit'),
  5918. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5919. '/div'
  5920. );
  5921. $this->assertTags($result, $expected);
  5922. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5923. $expected = array(
  5924. 'div' => array('class' => 'submit'),
  5925. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5926. '/div'
  5927. );
  5928. $this->assertTags($result, $expected);
  5929. $before = '--before--';
  5930. $after = '--after--';
  5931. $result = $this->Form->submit('Test', array('before' => $before));
  5932. $expected = array(
  5933. 'div' => array('class' => 'submit'),
  5934. '--before--',
  5935. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5936. '/div'
  5937. );
  5938. $this->assertTags($result, $expected);
  5939. $result = $this->Form->submit('Test', array('after' => $after));
  5940. $expected = array(
  5941. 'div' => array('class' => 'submit'),
  5942. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5943. '--after--',
  5944. '/div'
  5945. );
  5946. $this->assertTags($result, $expected);
  5947. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5948. $expected = array(
  5949. 'div' => array('class' => 'submit'),
  5950. '--before--',
  5951. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5952. '--after--',
  5953. '/div'
  5954. );
  5955. $this->assertTags($result, $expected);
  5956. }
  5957. /**
  5958. * test image submit types.
  5959. *
  5960. * @return void
  5961. */
  5962. public function testSubmitImage() {
  5963. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5964. $expected = array(
  5965. 'div' => array('class' => 'submit'),
  5966. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5967. '/div'
  5968. );
  5969. $this->assertTags($result, $expected);
  5970. $result = $this->Form->submit('/relative/cake.power.gif');
  5971. $expected = array(
  5972. 'div' => array('class' => 'submit'),
  5973. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5974. '/div'
  5975. );
  5976. $this->assertTags($result, $expected);
  5977. $result = $this->Form->submit('cake.power.gif');
  5978. $expected = array(
  5979. 'div' => array('class' => 'submit'),
  5980. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5981. '/div'
  5982. );
  5983. $this->assertTags($result, $expected);
  5984. $result = $this->Form->submit('Not.an.image');
  5985. $expected = array(
  5986. 'div' => array('class' => 'submit'),
  5987. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5988. '/div'
  5989. );
  5990. $this->assertTags($result, $expected);
  5991. $after = '--after--';
  5992. $before = '--before--';
  5993. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5994. $expected = array(
  5995. 'div' => array('class' => 'submit'),
  5996. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5997. '--after--',
  5998. '/div'
  5999. );
  6000. $this->assertTags($result, $expected);
  6001. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6002. $expected = array(
  6003. 'div' => array('class' => 'submit'),
  6004. '--before--',
  6005. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6006. '/div'
  6007. );
  6008. $this->assertTags($result, $expected);
  6009. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6010. $expected = array(
  6011. 'div' => array('class' => 'submit'),
  6012. '--before--',
  6013. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6014. '--after--',
  6015. '/div'
  6016. );
  6017. $this->assertTags($result, $expected);
  6018. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6019. $expected = array(
  6020. 'div' => array('class' => 'submit'),
  6021. '--before--',
  6022. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6023. '--after--',
  6024. '/div'
  6025. );
  6026. $this->assertTags($result, $expected);
  6027. }
  6028. /**
  6029. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6030. * be submitted at a time.
  6031. *
  6032. * @return void
  6033. */
  6034. public function testSubmitUnlockedByDefault() {
  6035. $this->Form->request->params['_Token'] = 'secured';
  6036. $this->Form->submit('Go go');
  6037. $this->Form->submit('Save', array('name' => 'save'));
  6038. $result = $this->Form->unlockField();
  6039. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6040. }
  6041. /**
  6042. * Test submit image with timestamps.
  6043. *
  6044. * @return void
  6045. */
  6046. public function testSubmitImageTimestamp() {
  6047. Configure::write('Asset.timestamp', 'force');
  6048. $result = $this->Form->submit('cake.power.gif');
  6049. $expected = array(
  6050. 'div' => array('class' => 'submit'),
  6051. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6052. '/div'
  6053. );
  6054. $this->assertTags($result, $expected);
  6055. }
  6056. /**
  6057. * test that datetime() works with GET style forms.
  6058. *
  6059. * @return void
  6060. */
  6061. public function testDateTimeWithGetForms() {
  6062. $this->markTestIncomplete('Need to revisit once models work again.');
  6063. extract($this->dateRegex);
  6064. $this->Form->create('Contact', array('type' => 'get'));
  6065. $result = $this->Form->datetime('created');
  6066. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6067. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6068. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6069. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6070. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6071. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6072. }
  6073. /**
  6074. * testEditFormWithData method
  6075. *
  6076. * test auto populating form elements from submitted data.
  6077. *
  6078. * @return void
  6079. */
  6080. public function testEditFormWithData() {
  6081. $this->markTestIncomplete('Need to revisit once models work again.');
  6082. $this->Form->request->data = array('Person' => array(
  6083. 'id' => 1,
  6084. 'first_name' => 'Nate',
  6085. 'last_name' => 'Abele',
  6086. 'email' => 'nate@example.com'
  6087. ));
  6088. $this->Form->request->addParams(array(
  6089. 'models' => array('Person'),
  6090. 'controller' => 'people',
  6091. 'action' => 'add'
  6092. ));
  6093. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6094. $this->Form->create();
  6095. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6096. $expected = array(
  6097. 'input' => array('type' => 'hidden', 'name' => 'People[People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6098. 'select' => array(
  6099. 'name' => 'People[People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6100. ),
  6101. array('option' => array('value' => 1)), 'Nate', '/option',
  6102. array('option' => array('value' => 2)), 'Garrett', '/option',
  6103. array('option' => array('value' => 3)), 'Larry', '/option',
  6104. '/select'
  6105. );
  6106. $this->assertTags($result, $expected);
  6107. }
  6108. /**
  6109. * Test that required fields are created for various types of validation.
  6110. *
  6111. * @return void
  6112. */
  6113. public function testFormInputRequiredDetection() {
  6114. $this->markTestIncomplete('Need to revisit once models work again.');
  6115. $this->Form->create('Contact');
  6116. $result = $this->Form->input('Contact.non_existing');
  6117. $expected = array(
  6118. 'div' => array('class' => 'input text'),
  6119. 'label' => array('for' => 'ContactNonExisting'),
  6120. 'Non Existing',
  6121. '/label',
  6122. 'input' => array(
  6123. 'type' => 'text', 'name' => 'Contact[non_existing]',
  6124. 'id' => 'ContactNonExisting'
  6125. ),
  6126. '/div'
  6127. );
  6128. $this->assertTags($result, $expected);
  6129. $result = $this->Form->input('Contact.imrequired');
  6130. $expected = array(
  6131. 'div' => array('class' => 'input text required'),
  6132. 'label' => array('for' => 'ContactImrequired'),
  6133. 'Imrequired',
  6134. '/label',
  6135. 'input' => array(
  6136. 'type' => 'text', 'name' => 'Contact[imrequired]',
  6137. 'id' => 'ContactImrequired',
  6138. 'required' => 'required'
  6139. ),
  6140. '/div'
  6141. );
  6142. $this->assertTags($result, $expected);
  6143. $result = $this->Form->input('Contact.imalsorequired');
  6144. $expected = array(
  6145. 'div' => array('class' => 'input text required'),
  6146. 'label' => array('for' => 'ContactImalsorequired'),
  6147. 'Imalsorequired',
  6148. '/label',
  6149. 'input' => array(
  6150. 'type' => 'text', 'name' => 'Contact[imalsorequired]',
  6151. 'id' => 'ContactImalsorequired',
  6152. 'required' => 'required'
  6153. ),
  6154. '/div'
  6155. );
  6156. $this->assertTags($result, $expected);
  6157. $result = $this->Form->input('Contact.imrequiredtoo');
  6158. $expected = array(
  6159. 'div' => array('class' => 'input text required'),
  6160. 'label' => array('for' => 'ContactImrequiredtoo'),
  6161. 'Imrequiredtoo',
  6162. '/label',
  6163. 'input' => array(
  6164. 'type' => 'text', 'name' => 'Contact[imrequiredtoo]',
  6165. 'id' => 'ContactImrequiredtoo',
  6166. 'required' => 'required'
  6167. ),
  6168. '/div'
  6169. );
  6170. $this->assertTags($result, $expected);
  6171. $result = $this->Form->input('Contact.required_one');
  6172. $expected = array(
  6173. 'div' => array('class' => 'input text required'),
  6174. 'label' => array('for' => 'ContactRequiredOne'),
  6175. 'Required One',
  6176. '/label',
  6177. 'input' => array(
  6178. 'type' => 'text', 'name' => 'Contact[required_one]',
  6179. 'id' => 'ContactRequiredOne',
  6180. 'required' => 'required'
  6181. ),
  6182. '/div'
  6183. );
  6184. $this->assertTags($result, $expected);
  6185. $result = $this->Form->input('Contact.string_required');
  6186. $expected = array(
  6187. 'div' => array('class' => 'input text required'),
  6188. 'label' => array('for' => 'ContactStringRequired'),
  6189. 'String Required',
  6190. '/label',
  6191. 'input' => array(
  6192. 'type' => 'text', 'name' => 'Contact[string_required]',
  6193. 'id' => 'ContactStringRequired',
  6194. 'required' => 'required'
  6195. ),
  6196. '/div'
  6197. );
  6198. $this->assertTags($result, $expected);
  6199. $result = $this->Form->input('Contact.imnotrequired');
  6200. $expected = array(
  6201. 'div' => array('class' => 'input text'),
  6202. 'label' => array('for' => 'ContactImnotrequired'),
  6203. 'Imnotrequired',
  6204. '/label',
  6205. 'input' => array(
  6206. 'type' => 'text', 'name' => 'Contact[imnotrequired]',
  6207. 'id' => 'ContactImnotrequired'
  6208. ),
  6209. '/div'
  6210. );
  6211. $this->assertTags($result, $expected);
  6212. $result = $this->Form->input('Contact.imalsonotrequired');
  6213. $expected = array(
  6214. 'div' => array('class' => 'input text'),
  6215. 'label' => array('for' => 'ContactImalsonotrequired'),
  6216. 'Imalsonotrequired',
  6217. '/label',
  6218. 'input' => array(
  6219. 'type' => 'text', 'name' => 'Contact[imalsonotrequired]',
  6220. 'id' => 'ContactImalsonotrequired'
  6221. ),
  6222. '/div'
  6223. );
  6224. $this->assertTags($result, $expected);
  6225. $result = $this->Form->input('Contact.imalsonotrequired2');
  6226. $expected = array(
  6227. 'div' => array('class' => 'input text'),
  6228. 'label' => array('for' => 'ContactImalsonotrequired2'),
  6229. 'Imalsonotrequired2',
  6230. '/label',
  6231. 'input' => array(
  6232. 'type' => 'text', 'name' => 'Contact[imalsonotrequired2]',
  6233. 'id' => 'ContactImalsonotrequired2'
  6234. ),
  6235. '/div'
  6236. );
  6237. $this->assertTags($result, $expected);
  6238. $result = $this->Form->input('Contact.imnotrequiredeither');
  6239. $expected = array(
  6240. 'div' => array('class' => 'input text'),
  6241. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6242. 'Imnotrequiredeither',
  6243. '/label',
  6244. 'input' => array(
  6245. 'type' => 'text', 'name' => 'Contact[imnotrequiredeither]',
  6246. 'id' => 'ContactImnotrequiredeither'
  6247. ),
  6248. '/div'
  6249. );
  6250. $this->assertTags($result, $expected);
  6251. $result = $this->Form->input('Contact.iamrequiredalways');
  6252. $expected = array(
  6253. 'div' => array('class' => 'input text required'),
  6254. 'label' => array('for' => 'ContactIamrequiredalways'),
  6255. 'Iamrequiredalways',
  6256. '/label',
  6257. 'input' => array(
  6258. 'type' => 'text', 'name' => 'Contact[iamrequiredalways]',
  6259. 'id' => 'ContactIamrequiredalways',
  6260. 'required' => 'required'
  6261. ),
  6262. '/div'
  6263. );
  6264. $this->assertTags($result, $expected);
  6265. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  6266. $expected = array(
  6267. 'div' => array('class' => 'input checkbox required'),
  6268. array('input' => array(
  6269. 'type' => 'hidden',
  6270. 'name' => 'Contact[boolean_field]',
  6271. 'id' => 'ContactBooleanField_',
  6272. 'value' => '0'
  6273. )),
  6274. array('input' => array(
  6275. 'type' => 'checkbox',
  6276. 'name' => 'Contact[boolean_field]',
  6277. 'value' => '1',
  6278. 'id' => 'ContactBooleanField'
  6279. )),
  6280. 'label' => array('for' => 'ContactBooleanField'),
  6281. 'Boolean Field',
  6282. '/label',
  6283. '/div'
  6284. );
  6285. $this->assertTags($result, $expected);
  6286. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  6287. $expected = array(
  6288. 'div' => array('class' => 'input checkbox required'),
  6289. array('input' => array(
  6290. 'type' => 'hidden',
  6291. 'name' => 'Contact[boolean_field]',
  6292. 'id' => 'ContactBooleanField_',
  6293. 'value' => '0'
  6294. )),
  6295. array('input' => array(
  6296. 'type' => 'checkbox',
  6297. 'name' => 'Contact[boolean_field]',
  6298. 'value' => '1',
  6299. 'id' => 'ContactBooleanField',
  6300. 'required' => 'required'
  6301. )),
  6302. 'label' => array('for' => 'ContactBooleanField'),
  6303. 'Boolean Field',
  6304. '/label',
  6305. '/div'
  6306. );
  6307. $this->assertTags($result, $expected);
  6308. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  6309. $expected = array(
  6310. 'div' => array('class' => 'input file required'),
  6311. 'label' => array('for' => 'ContactIamrequiredalways'),
  6312. 'Iamrequiredalways',
  6313. '/label',
  6314. 'input' => array(
  6315. 'type' => 'file',
  6316. 'name' => 'data[Contact][iamrequiredalways]',
  6317. 'id' => 'ContactIamrequiredalways',
  6318. 'required' => 'required'
  6319. ),
  6320. '/div'
  6321. );
  6322. $this->assertTags($result, $expected);
  6323. }
  6324. /**
  6325. * Test that required fields are created when only using ModelValidator::add().
  6326. *
  6327. * @return void
  6328. */
  6329. public function testFormInputRequiredDetectionModelValidator() {
  6330. $this->markTestIncomplete('Need to revisit once models work again.');
  6331. $this->Form->create('ContactTag');
  6332. $result = $this->Form->input('ContactTag.iwillberequired');
  6333. $expected = array(
  6334. 'div' => array('class' => 'input text required'),
  6335. 'label' => array('for' => 'ContactTagIwillberequired'),
  6336. 'Iwillberequired',
  6337. '/label',
  6338. 'input' => array(
  6339. 'name' => 'data[ContactTag][iwillberequired]',
  6340. 'type' => 'text',
  6341. 'id' => 'ContactTagIwillberequired',
  6342. 'required' => 'required'
  6343. ),
  6344. '/div'
  6345. );
  6346. $this->assertTags($result, $expected);
  6347. }
  6348. /**
  6349. * testFormMagicInput method
  6350. *
  6351. * @return void
  6352. */
  6353. public function testFormMagicInput() {
  6354. $this->markTestIncomplete('Need to revisit once models work again.');
  6355. $encoding = strtolower(Configure::read('App.encoding'));
  6356. $result = $this->Form->create('Contact');
  6357. $expected = array(
  6358. 'form' => array(
  6359. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6360. 'accept-charset' => $encoding
  6361. ),
  6362. 'div' => array('style' => 'display:none;'),
  6363. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6364. '/div'
  6365. );
  6366. $this->assertTags($result, $expected);
  6367. $result = $this->Form->input('name');
  6368. $expected = array(
  6369. 'div' => array('class' => 'input text'),
  6370. 'label' => array('for' => 'ContactName'),
  6371. 'Name',
  6372. '/label',
  6373. 'input' => array(
  6374. 'type' => 'text', 'name' => 'Contact[name]',
  6375. 'id' => 'ContactName', 'maxlength' => '255'
  6376. ),
  6377. '/div'
  6378. );
  6379. $this->assertTags($result, $expected);
  6380. $result = $this->Form->input('non_existing_field_in_contact_model');
  6381. $expected = array(
  6382. 'div' => array('class' => 'input text'),
  6383. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6384. 'Non Existing Field In Contact Model',
  6385. '/label',
  6386. 'input' => array(
  6387. 'type' => 'text', 'name' => 'Contact[non_existing_field_in_contact_model]',
  6388. 'id' => 'ContactNonExistingFieldInContactModel'
  6389. ),
  6390. '/div'
  6391. );
  6392. $this->assertTags($result, $expected);
  6393. $result = $this->Form->input('Address.street');
  6394. $expected = array(
  6395. 'div' => array('class' => 'input text'),
  6396. 'label' => array('for' => 'AddressStreet'),
  6397. 'Street',
  6398. '/label',
  6399. 'input' => array(
  6400. 'type' => 'text', 'name' => 'Address[street]',
  6401. 'id' => 'AddressStreet'
  6402. ),
  6403. '/div'
  6404. );
  6405. $this->assertTags($result, $expected);
  6406. $result = $this->Form->input('Address.non_existing_field_in_model');
  6407. $expected = array(
  6408. 'div' => array('class' => 'input text'),
  6409. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6410. 'Non Existing Field In Model',
  6411. '/label',
  6412. 'input' => array(
  6413. 'type' => 'text', 'name' => 'Address[non_existing_field_in_model]',
  6414. 'id' => 'AddressNonExistingFieldInModel'
  6415. ),
  6416. '/div'
  6417. );
  6418. $this->assertTags($result, $expected);
  6419. $result = $this->Form->input('name', array('div' => false));
  6420. $expected = array(
  6421. 'label' => array('for' => 'ContactName'),
  6422. 'Name',
  6423. '/label',
  6424. 'input' => array(
  6425. 'type' => 'text', 'name' => 'Contact[name]',
  6426. 'id' => 'ContactName', 'maxlength' => '255'
  6427. )
  6428. );
  6429. $this->assertTags($result, $expected);
  6430. extract($this->dateRegex);
  6431. $now = strtotime('now');
  6432. $result = $this->Form->input('Contact.published', array('div' => false));
  6433. $expected = array(
  6434. 'label' => array('for' => 'ContactPublishedMonth'),
  6435. 'Published',
  6436. '/label',
  6437. array('select' => array(
  6438. 'name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth'
  6439. )),
  6440. $monthsRegex,
  6441. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6442. date('F', $now),
  6443. '/option',
  6444. '*/select',
  6445. '-',
  6446. array('select' => array(
  6447. 'name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay'
  6448. )),
  6449. $daysRegex,
  6450. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6451. date('j', $now),
  6452. '/option',
  6453. '*/select',
  6454. '-',
  6455. array('select' => array(
  6456. 'name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear'
  6457. )),
  6458. $yearsRegex,
  6459. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6460. date('Y', $now),
  6461. '*/select'
  6462. );
  6463. $this->assertTags($result, $expected);
  6464. $result = $this->Form->input('Contact.updated', array('div' => false));
  6465. $expected = array(
  6466. 'label' => array('for' => 'ContactUpdatedMonth'),
  6467. 'Updated',
  6468. '/label',
  6469. array('select' => array(
  6470. 'name' => 'Contact[updated][month]', 'id' => 'ContactUpdatedMonth'
  6471. )),
  6472. $monthsRegex,
  6473. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6474. date('F', $now),
  6475. '/option',
  6476. '*/select',
  6477. '-',
  6478. array('select' => array(
  6479. 'name' => 'Contact[updated][day]', 'id' => 'ContactUpdatedDay'
  6480. )),
  6481. $daysRegex,
  6482. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6483. date('j', $now),
  6484. '/option',
  6485. '*/select',
  6486. '-',
  6487. array('select' => array(
  6488. 'name' => 'Contact[updated][year]', 'id' => 'ContactUpdatedYear'
  6489. )),
  6490. $yearsRegex,
  6491. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6492. date('Y', $now),
  6493. '*/select'
  6494. );
  6495. $this->assertTags($result, $expected);
  6496. $result = $this->Form->input('UserForm.stuff');
  6497. $expected = array(
  6498. 'div' => array('class' => 'input text'),
  6499. 'label' => array('for' => 'UserFormStuff'),
  6500. 'Stuff',
  6501. '/label',
  6502. 'input' => array(
  6503. 'type' => 'text', 'name' => 'UserForm[stuff]',
  6504. 'id' => 'UserFormStuff', 'maxlength' => 10
  6505. ),
  6506. '/div'
  6507. );
  6508. $this->assertTags($result, $expected);
  6509. }
  6510. /**
  6511. * testForMagicInputNonExistingNorValidated method
  6512. *
  6513. * @return void
  6514. */
  6515. public function testForMagicInputNonExistingNorValidated() {
  6516. $this->markTestIncomplete('Need to revisit once models work again.');
  6517. $encoding = strtolower(Configure::read('App.encoding'));
  6518. $result = $this->Form->create('Contact');
  6519. $expected = array(
  6520. 'form' => array(
  6521. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6522. 'accept-charset' => $encoding
  6523. ),
  6524. 'div' => array('style' => 'display:none;'),
  6525. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6526. '/div'
  6527. );
  6528. $this->assertTags($result, $expected);
  6529. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6530. $expected = array(
  6531. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6532. 'Non Existing Nor Validated',
  6533. '/label',
  6534. 'input' => array(
  6535. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  6536. 'id' => 'ContactNonExistingNorValidated'
  6537. )
  6538. );
  6539. $this->assertTags($result, $expected);
  6540. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6541. 'div' => false, 'value' => 'my value'
  6542. ));
  6543. $expected = array(
  6544. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6545. 'Non Existing Nor Validated',
  6546. '/label',
  6547. 'input' => array(
  6548. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  6549. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6550. )
  6551. );
  6552. $this->assertTags($result, $expected);
  6553. $this->Form->request->data = array(
  6554. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6555. ));
  6556. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6557. $expected = array(
  6558. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6559. 'Non Existing Nor Validated',
  6560. '/label',
  6561. 'input' => array(
  6562. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  6563. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6564. )
  6565. );
  6566. $this->assertTags($result, $expected);
  6567. }
  6568. /**
  6569. * testFormMagicInputLabel method
  6570. *
  6571. * @return void
  6572. */
  6573. public function testFormMagicInputLabel() {
  6574. $this->markTestIncomplete('Need to revisit once models work again.');
  6575. $encoding = strtolower(Configure::read('App.encoding'));
  6576. $result = $this->Form->create('Contact');
  6577. $expected = array(
  6578. 'form' => array(
  6579. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6580. 'accept-charset' => $encoding
  6581. ),
  6582. 'div' => array('style' => 'display:none;'),
  6583. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6584. '/div'
  6585. );
  6586. $this->assertTags($result, $expected);
  6587. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6588. $this->assertTags($result, array('input' => array(
  6589. 'name' => 'Contact[name]', 'type' => 'text',
  6590. 'id' => 'ContactName', 'maxlength' => '255')
  6591. ));
  6592. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6593. $expected = array(
  6594. 'label' => array('for' => 'ContactName'),
  6595. 'My label',
  6596. '/label',
  6597. 'input' => array(
  6598. 'type' => 'text', 'name' => 'Contact[name]',
  6599. 'id' => 'ContactName', 'maxlength' => '255'
  6600. )
  6601. );
  6602. $this->assertTags($result, $expected);
  6603. $result = $this->Form->input('Contact.name', array(
  6604. 'div' => false, 'label' => array('class' => 'mandatory')
  6605. ));
  6606. $expected = array(
  6607. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6608. 'Name',
  6609. '/label',
  6610. 'input' => array(
  6611. 'type' => 'text', 'name' => 'Contact[name]',
  6612. 'id' => 'ContactName', 'maxlength' => '255'
  6613. )
  6614. );
  6615. $this->assertTags($result, $expected);
  6616. $result = $this->Form->input('Contact.name', array(
  6617. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6618. ));
  6619. $expected = array(
  6620. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6621. 'My label',
  6622. '/label',
  6623. 'input' => array(
  6624. 'type' => 'text', 'name' => 'Contact[name]',
  6625. 'id' => 'ContactName', 'maxlength' => '255'
  6626. )
  6627. );
  6628. $this->assertTags($result, $expected);
  6629. $result = $this->Form->input('Contact.name', array(
  6630. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6631. ));
  6632. $expected = array(
  6633. 'label' => array('for' => 'my_id'),
  6634. 'Name',
  6635. '/label',
  6636. 'input' => array(
  6637. 'type' => 'text', 'name' => 'Contact[name]',
  6638. 'id' => 'my_id', 'maxlength' => '255'
  6639. )
  6640. );
  6641. $this->assertTags($result, $expected);
  6642. $result = $this->Form->input('1.id');
  6643. $this->assertTags($result, array('input' => array(
  6644. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  6645. 'id' => 'Contact1Id'
  6646. )));
  6647. $result = $this->Form->input("1.name");
  6648. $expected = array(
  6649. 'div' => array('class' => 'input text'),
  6650. 'label' => array('for' => 'Contact1Name'),
  6651. 'Name',
  6652. '/label',
  6653. 'input' => array(
  6654. 'type' => 'text', 'name' => 'Contact[1][name]',
  6655. 'id' => 'Contact1Name', 'maxlength' => '255'
  6656. ),
  6657. '/div'
  6658. );
  6659. $this->assertTags($result, $expected);
  6660. $result = $this->Form->input('Contact.1.id');
  6661. $this->assertTags($result, array(
  6662. 'input' => array(
  6663. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  6664. 'id' => 'Contact1Id'
  6665. )
  6666. ));
  6667. $result = $this->Form->input("Model.1.name");
  6668. $expected = array(
  6669. 'div' => array('class' => 'input text'),
  6670. 'label' => array('for' => 'Model1Name'),
  6671. 'Name',
  6672. '/label',
  6673. 'input' => array(
  6674. 'type' => 'text', 'name' => 'Model[1][name]',
  6675. 'id' => 'Model1Name'
  6676. ),
  6677. '/div'
  6678. );
  6679. $this->assertTags($result, $expected);
  6680. }
  6681. /**
  6682. * testFormEnd method
  6683. *
  6684. * @return void
  6685. */
  6686. public function testFormEnd() {
  6687. $this->assertEquals('</form>', $this->Form->end());
  6688. }
  6689. /**
  6690. * testMultipleFormWithIdFields method
  6691. *
  6692. * @return void
  6693. */
  6694. public function testMultipleFormWithIdFields() {
  6695. $this->markTestIncomplete('Need to revisit once models work again.');
  6696. $this->Form->create('UserForm');
  6697. $result = $this->Form->input('id');
  6698. $this->assertTags($result, array('input' => array(
  6699. 'type' => 'hidden', 'name' => 'UserForm[id]', 'id' => 'UserFormId'
  6700. )));
  6701. $result = $this->Form->input('ValidateItem.id');
  6702. $this->assertTags($result, array('input' => array(
  6703. 'type' => 'hidden', 'name' => 'ValidateItem[id]',
  6704. 'id' => 'ValidateItemId'
  6705. )));
  6706. $result = $this->Form->input('ValidateUser.id');
  6707. $this->assertTags($result, array('input' => array(
  6708. 'type' => 'hidden', 'name' => 'ValidateUser[id]',
  6709. 'id' => 'ValidateUserId'
  6710. )));
  6711. }
  6712. /**
  6713. * testDbLessModel method
  6714. *
  6715. * @return void
  6716. */
  6717. public function testDbLessModel() {
  6718. $this->markTestIncomplete('Need to revisit once models work again.');
  6719. $this->Form->create('TestMail');
  6720. $result = $this->Form->input('name');
  6721. $expected = array(
  6722. 'div' => array('class' => 'input text'),
  6723. 'label' => array('for' => 'TestMailName'),
  6724. 'Name',
  6725. '/label',
  6726. 'input' => array(
  6727. 'name' => 'TestMail[name]', 'type' => 'text',
  6728. 'id' => 'TestMailName'
  6729. ),
  6730. '/div'
  6731. );
  6732. $this->assertTags($result, $expected);
  6733. $this->Form->create('TestMail');
  6734. $result = $this->Form->input('name');
  6735. $expected = array(
  6736. 'div' => array('class' => 'input text'),
  6737. 'label' => array('for' => 'TestMailName'),
  6738. 'Name',
  6739. '/label',
  6740. 'input' => array(
  6741. 'name' => 'TestMail[name]', 'type' => 'text',
  6742. 'id' => 'TestMailName'
  6743. ),
  6744. '/div'
  6745. );
  6746. $this->assertTags($result, $expected);
  6747. }
  6748. /**
  6749. * testBrokenness method
  6750. *
  6751. * @return void
  6752. */
  6753. public function testBrokenness() {
  6754. $this->markTestIncomplete('Need to revisit once models work again.');
  6755. /*
  6756. * #4 This test has two parents and four children. By default (as of r7117) both
  6757. * parents are show but the first parent is missing a child. This is the inconsistency
  6758. * in the default behaviour - one parent has all children, the other does not - dependent
  6759. * on the data values.
  6760. */
  6761. $result = $this->Form->select('Model.field', array(
  6762. 'Fred' => array(
  6763. 'freds_son_1' => 'Fred',
  6764. 'freds_son_2' => 'Freddie'
  6765. ),
  6766. 'Bert' => array(
  6767. 'berts_son_1' => 'Albert',
  6768. 'berts_son_2' => 'Bertie')
  6769. ),
  6770. array('showParents' => true, 'empty' => false)
  6771. );
  6772. $expected = array(
  6773. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  6774. array('optgroup' => array('label' => 'Fred')),
  6775. array('option' => array('value' => 'freds_son_1')),
  6776. 'Fred',
  6777. '/option',
  6778. array('option' => array('value' => 'freds_son_2')),
  6779. 'Freddie',
  6780. '/option',
  6781. '/optgroup',
  6782. array('optgroup' => array('label' => 'Bert')),
  6783. array('option' => array('value' => 'berts_son_1')),
  6784. 'Albert',
  6785. '/option',
  6786. array('option' => array('value' => 'berts_son_2')),
  6787. 'Bertie',
  6788. '/option',
  6789. '/optgroup',
  6790. '/select'
  6791. );
  6792. $this->assertTags($result, $expected);
  6793. /*
  6794. * #2 This is structurally identical to the test above (#1) - only the parent name has
  6795. * changed, so we should expect the same select list data, just with a different name
  6796. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  6797. * This is where data corruption can occur, because when a select value is missing from
  6798. * a list a form will substitute the first value in the list - without the user knowing.
  6799. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  6800. * affect the availability of 3 => 'Three' as a valid option.
  6801. */
  6802. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  6803. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  6804. ));
  6805. $result = $this->Form->select(
  6806. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  6807. );
  6808. $expected = array(
  6809. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  6810. array('option' => array('value' => 1)),
  6811. 'One',
  6812. '/option',
  6813. array('option' => array('value' => 2)),
  6814. 'Two',
  6815. '/option',
  6816. array('optgroup' => array('label' => 'Three')),
  6817. array('option' => array('value' => 3)),
  6818. 'Three',
  6819. '/option',
  6820. array('option' => array('value' => 4)),
  6821. 'Four',
  6822. '/option',
  6823. array('option' => array('value' => 5)),
  6824. 'Five',
  6825. '/option',
  6826. '/optgroup',
  6827. '/select'
  6828. );
  6829. $this->assertTags($result, $expected);
  6830. }
  6831. /**
  6832. * Test the generation of fields for a multi record form.
  6833. *
  6834. * @return void
  6835. */
  6836. public function testMultiRecordForm() {
  6837. $this->markTestIncomplete('Need to revisit once models work again.');
  6838. $this->Form->create('ValidateProfile');
  6839. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  6840. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  6841. $expected = array(
  6842. 'div' => array('class' => 'input textarea'),
  6843. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  6844. 'Name',
  6845. '/label',
  6846. 'textarea' => array(
  6847. 'id' => 'ValidateProfile1ValidateItem2Name',
  6848. 'name' => 'ValidateProfile[1][ValidateItem][2][name]',
  6849. 'cols' => 30,
  6850. 'rows' => 6
  6851. ),
  6852. 'Value',
  6853. '/textarea',
  6854. '/div'
  6855. );
  6856. $this->assertTags($result, $expected);
  6857. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  6858. $expected = array(
  6859. 'div' => array('class' => 'input date'),
  6860. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  6861. 'Created',
  6862. '/label',
  6863. array('select' => array(
  6864. 'name' => 'ValidateProfile[1][ValidateItem][2][created][month]',
  6865. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  6866. )
  6867. ),
  6868. array('option' => array('value' => '')), '/option',
  6869. $this->dateRegex['monthsRegex'],
  6870. '/select', '-',
  6871. array('select' => array(
  6872. 'name' => 'ValidateProfile[1][ValidateItem][2][created][day]',
  6873. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  6874. )
  6875. ),
  6876. array('option' => array('value' => '')), '/option',
  6877. $this->dateRegex['daysRegex'],
  6878. '/select', '-',
  6879. array('select' => array(
  6880. 'name' => 'ValidateProfile[1][ValidateItem][2][created][year]',
  6881. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  6882. )
  6883. ),
  6884. array('option' => array('value' => '')), '/option',
  6885. $this->dateRegex['yearsRegex'],
  6886. '/select',
  6887. '/div'
  6888. );
  6889. $this->assertTags($result, $expected);
  6890. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  6891. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  6892. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  6893. $expected = array(
  6894. 'div' => array('class' => 'input select error'),
  6895. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  6896. 'Profile',
  6897. '/label',
  6898. 'select' => array(
  6899. 'name' => 'ValidateProfile[1][ValidateItem][2][profile_id]',
  6900. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  6901. 'class' => 'form-error'
  6902. ),
  6903. '/select',
  6904. array('div' => array('class' => 'error-message')),
  6905. 'Error',
  6906. '/div',
  6907. '/div'
  6908. );
  6909. $this->assertTags($result, $expected);
  6910. }
  6911. /**
  6912. * test the correct display of multi-record form validation errors.
  6913. *
  6914. * @return void
  6915. */
  6916. public function testMultiRecordFormValidationErrors() {
  6917. $this->markTestIncomplete('Need to revisit once models work again.');
  6918. $this->Form->create('ValidateProfile');
  6919. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  6920. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  6921. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  6922. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  6923. $result = $this->Form->error('ValidateProfile.2.city');
  6924. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6925. $result = $this->Form->error('2.city');
  6926. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6927. }
  6928. /**
  6929. * test the correct display of multi-record form validation errors.
  6930. *
  6931. * @return void
  6932. */
  6933. public function testSaveManyRecordFormValidationErrors() {
  6934. $this->markTestIncomplete('Need to revisit once models work again.');
  6935. $this->Form->create('ValidateUser');
  6936. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  6937. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  6938. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  6939. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  6940. $result = $this->Form->error('ValidateUser.0.city');
  6941. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6942. }
  6943. /**
  6944. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  6945. *
  6946. * @return void
  6947. */
  6948. public function testInputTemplate() {
  6949. $this->markTestIncomplete('Need to revisit once models work again.');
  6950. $result = $this->Form->input('Contact.email', array(
  6951. 'type' => 'text', 'format' => array('input')
  6952. ));
  6953. $expected = array(
  6954. 'div' => array('class' => 'input text'),
  6955. 'input' => array(
  6956. 'type' => 'text', 'name' => 'Contact[email]',
  6957. 'id' => 'ContactEmail'
  6958. ),
  6959. '/div'
  6960. );
  6961. $this->assertTags($result, $expected);
  6962. $result = $this->Form->input('Contact.email', array(
  6963. 'type' => 'text', 'format' => array('input', 'label'),
  6964. 'label' => '<em>Email (required)</em>'
  6965. ));
  6966. $expected = array(
  6967. 'div' => array('class' => 'input text'),
  6968. array('input' => array(
  6969. 'type' => 'text', 'name' => 'Contact[email]',
  6970. 'id' => 'ContactEmail'
  6971. )),
  6972. 'label' => array('for' => 'ContactEmail'),
  6973. 'em' => array(),
  6974. 'Email (required)',
  6975. '/em',
  6976. '/label',
  6977. '/div'
  6978. );
  6979. $this->assertTags($result, $expected);
  6980. $result = $this->Form->input('Contact.email', array(
  6981. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  6982. 'between' => '<div>Something in the middle</div>',
  6983. 'after' => '<span>Some text at the end</span>'
  6984. ));
  6985. $expected = array(
  6986. 'div' => array('class' => 'input text'),
  6987. array('input' => array(
  6988. 'type' => 'text', 'name' => 'Contact[email]',
  6989. 'id' => 'ContactEmail'
  6990. )),
  6991. array('div' => array()),
  6992. 'Something in the middle',
  6993. '/div',
  6994. 'label' => array('for' => 'ContactEmail'),
  6995. 'Email',
  6996. '/label',
  6997. 'span' => array(),
  6998. 'Some text at the end',
  6999. '/span',
  7000. '/div'
  7001. );
  7002. $this->assertTags($result, $expected);
  7003. $result = $this->Form->input('Contact.method', array(
  7004. 'type' => 'radio',
  7005. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7006. 'between' => 'I am between',
  7007. ));
  7008. $expected = array(
  7009. 'div' => array('class' => 'input radio'),
  7010. 'fieldset' => array(),
  7011. 'legend' => array(),
  7012. 'Method',
  7013. '/legend',
  7014. 'I am between',
  7015. 'input' => array(
  7016. 'type' => 'hidden', 'name' => 'Contact[method]',
  7017. 'value' => '', 'id' => 'ContactMethod_'
  7018. ),
  7019. array('input' => array(
  7020. 'type' => 'radio', 'name' => 'Contact[method]',
  7021. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7022. )),
  7023. array('label' => array('for' => 'ContactMethodEmail')),
  7024. 'Email',
  7025. '/label',
  7026. array('input' => array(
  7027. 'type' => 'radio', 'name' => 'Contact[method]',
  7028. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7029. )),
  7030. array('label' => array('for' => 'ContactMethodPigeon')),
  7031. 'Pigeon',
  7032. '/label',
  7033. '/fieldset',
  7034. '/div',
  7035. );
  7036. $this->assertTags($result, $expected);
  7037. }
  7038. /**
  7039. * test that some html5 inputs + FormHelper::__call() work
  7040. *
  7041. * @return void
  7042. */
  7043. public function testHtml5Inputs() {
  7044. $result = $this->Form->email('User.email');
  7045. $expected = array(
  7046. 'input' => array('type' => 'email', 'name' => 'User[email]')
  7047. );
  7048. $this->assertTags($result, $expected);
  7049. $result = $this->Form->search('User.query');
  7050. $expected = array(
  7051. 'input' => array('type' => 'search', 'name' => 'User[query]')
  7052. );
  7053. $this->assertTags($result, $expected);
  7054. $result = $this->Form->search('User.query', array('value' => 'test'));
  7055. $expected = array(
  7056. 'input' => array('type' => 'search', 'name' => 'User[query]', 'value' => 'test')
  7057. );
  7058. $this->assertTags($result, $expected);
  7059. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7060. $expected = array(
  7061. 'input' => array('type' => 'text', 'name' => 'User[query]', 'value' => 'test')
  7062. );
  7063. $this->assertTags($result, $expected);
  7064. }
  7065. /**
  7066. * Test accessing html5 inputs through input().
  7067. *
  7068. * @return void
  7069. */
  7070. public function testHtml5InputWithInput() {
  7071. $this->markTestIncomplete('Need to revisit once models work again.');
  7072. $result = $this->Form->input('User.website', array(
  7073. 'type' => 'url',
  7074. 'value' => 'http://domain.tld',
  7075. 'div' => false,
  7076. 'label' => false));
  7077. $expected = array(
  7078. 'input' => array('type' => 'url', 'name' => 'User[website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7079. );
  7080. $this->assertTags($result, $expected);
  7081. }
  7082. /**
  7083. * Test errors when field name is missing.
  7084. *
  7085. * @expectedException \Cake\Error\Exception
  7086. * @return void
  7087. */
  7088. public function testHtml5InputException() {
  7089. $this->Form->email();
  7090. }
  7091. /**
  7092. * Tests that a model can be loaded from the model names passed in the request object
  7093. *
  7094. * @return void
  7095. */
  7096. public function testIntrospectModelFromRequest() {
  7097. $this->markTestIncomplete('Need to revisit once models work again.');
  7098. $this->loadFixtures('Post');
  7099. App::build(array(
  7100. 'Plugin' => array(CAKE . 'Test/TestApp/Plugin/')
  7101. ));
  7102. Plugin::load('TestPlugin');
  7103. $this->Form->request['models'] = array(
  7104. 'TestPluginPost' => array(
  7105. 'plugin' => 'TestPlugin',
  7106. 'className' => 'TestPluginPost'
  7107. )
  7108. );
  7109. $this->Form->create('TestPluginPost');
  7110. Plugin::unload();
  7111. }
  7112. /**
  7113. * Tests that it is possible to set the validation errors directly in the helper for a field
  7114. *
  7115. * @return void
  7116. */
  7117. public function testCustomValidationErrors() {
  7118. $this->markTestIncomplete('Need to revisit once models work again.');
  7119. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7120. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7121. $this->assertEquals('Badness!', $result);
  7122. }
  7123. /**
  7124. * Tests that the 'on' key validates as expected on create
  7125. *
  7126. * @return void
  7127. */
  7128. public function testRequiredOnCreate() {
  7129. $this->markTestIncomplete('Need to revisit once models work again.');
  7130. $this->Form->create('Contact');
  7131. $result = $this->Form->input('Contact.imrequiredonupdate');
  7132. $expected = array(
  7133. 'div' => array('class' => 'input text'),
  7134. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7135. 'Imrequiredonupdate',
  7136. '/label',
  7137. 'input' => array(
  7138. 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
  7139. 'id' => 'ContactImrequiredonupdate'
  7140. ),
  7141. '/div'
  7142. );
  7143. $this->assertTags($result, $expected);
  7144. $result = $this->Form->input('Contact.imrequiredoncreate');
  7145. $expected = array(
  7146. 'div' => array('class' => 'input text required'),
  7147. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7148. 'Imrequiredoncreate',
  7149. '/label',
  7150. 'input' => array(
  7151. 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
  7152. 'id' => 'ContactImrequiredoncreate',
  7153. 'required' => 'required'
  7154. ),
  7155. '/div'
  7156. );
  7157. $this->assertTags($result, $expected);
  7158. $result = $this->Form->input('Contact.imrequiredonboth');
  7159. $expected = array(
  7160. 'div' => array('class' => 'input text required'),
  7161. 'label' => array('for' => 'ContactImrequiredonboth'),
  7162. 'Imrequiredonboth',
  7163. '/label',
  7164. 'input' => array(
  7165. 'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
  7166. 'id' => 'ContactImrequiredonboth',
  7167. 'required' => 'required'
  7168. ),
  7169. '/div'
  7170. );
  7171. $this->assertTags($result, $expected);
  7172. $this->Form->inputDefaults(array('required' => false));
  7173. $result = $this->Form->input('Contact.imrequired');
  7174. $expected = array(
  7175. 'div' => array('class' => 'input text'),
  7176. 'label' => array('for' => 'ContactImrequired'),
  7177. 'Imrequired',
  7178. '/label',
  7179. 'input' => array(
  7180. 'type' => 'text', 'name' => 'Contact[imrequired]',
  7181. 'id' => 'ContactImrequired'
  7182. ),
  7183. '/div'
  7184. );
  7185. $this->assertTags($result, $expected);
  7186. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  7187. $this->assertTags($result, $expected);
  7188. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  7189. $expected = array(
  7190. 'div' => array('class' => 'input text required'),
  7191. 'label' => array('for' => 'ContactImrequired'),
  7192. 'Imrequired',
  7193. '/label',
  7194. 'input' => array(
  7195. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7196. 'id' => 'ContactImrequired'
  7197. ),
  7198. '/div'
  7199. );
  7200. $this->assertTags($result, $expected);
  7201. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  7202. $this->assertTags($result, $expected);
  7203. }
  7204. /**
  7205. * Tests that the 'on' key validates as expected on update
  7206. *
  7207. * @return void
  7208. */
  7209. public function testRequiredOnUpdate() {
  7210. $this->markTestIncomplete('Need to revisit once models work again.');
  7211. $this->Form->request->data['Contact']['id'] = 1;
  7212. $this->Form->create('Contact');
  7213. $result = $this->Form->input('Contact.imrequiredonupdate');
  7214. $expected = array(
  7215. 'div' => array('class' => 'input text required'),
  7216. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7217. 'Imrequiredonupdate',
  7218. '/label',
  7219. 'input' => array(
  7220. 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
  7221. 'id' => 'ContactImrequiredonupdate',
  7222. 'required' => 'required'
  7223. ),
  7224. '/div'
  7225. );
  7226. $this->assertTags($result, $expected);
  7227. $result = $this->Form->input('Contact.imrequiredoncreate');
  7228. $expected = array(
  7229. 'div' => array('class' => 'input text'),
  7230. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7231. 'Imrequiredoncreate',
  7232. '/label',
  7233. 'input' => array(
  7234. 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
  7235. 'id' => 'ContactImrequiredoncreate'
  7236. ),
  7237. '/div'
  7238. );
  7239. $this->assertTags($result, $expected);
  7240. $result = $this->Form->input('Contact.imrequiredonboth');
  7241. $expected = array(
  7242. 'div' => array('class' => 'input text required'),
  7243. 'label' => array('for' => 'ContactImrequiredonboth'),
  7244. 'Imrequiredonboth',
  7245. '/label',
  7246. 'input' => array(
  7247. 'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
  7248. 'id' => 'ContactImrequiredonboth',
  7249. 'required' => 'required'
  7250. ),
  7251. '/div'
  7252. );
  7253. $this->assertTags($result, $expected);
  7254. $result = $this->Form->input('Contact.imrequired');
  7255. $expected = array(
  7256. 'div' => array('class' => 'input text required'),
  7257. 'label' => array('for' => 'ContactImrequired'),
  7258. 'Imrequired',
  7259. '/label',
  7260. 'input' => array(
  7261. 'type' => 'text', 'name' => 'Contact[imrequired]',
  7262. 'id' => 'ContactImrequired',
  7263. 'required' => 'required'
  7264. ),
  7265. '/div'
  7266. );
  7267. $this->assertTags($result, $expected);
  7268. }
  7269. /**
  7270. * Test inputDefaults setter and getter
  7271. *
  7272. * @return void
  7273. */
  7274. public function testInputDefaults() {
  7275. $this->markTestIncomplete('Need to revisit once models work again.');
  7276. $this->Form->create('Contact');
  7277. $this->Form->inputDefaults(array(
  7278. 'label' => false,
  7279. 'div' => array(
  7280. 'style' => 'color: #000;'
  7281. )
  7282. ));
  7283. $result = $this->Form->input('Contact.field1');
  7284. $expected = array(
  7285. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7286. 'input' => array(
  7287. 'type' => 'text', 'name' => 'Contact[field1]',
  7288. 'id' => 'ContactField1'
  7289. ),
  7290. '/div'
  7291. );
  7292. $this->assertTags($result, $expected);
  7293. $this->Form->inputDefaults(array(
  7294. 'div' => false,
  7295. 'label' => 'Label',
  7296. ));
  7297. $result = $this->Form->input('Contact.field1');
  7298. $expected = array(
  7299. 'label' => array('for' => 'ContactField1'),
  7300. 'Label',
  7301. '/label',
  7302. 'input' => array(
  7303. 'type' => 'text', 'name' => 'Contact[field1]',
  7304. 'id' => 'ContactField1'
  7305. ),
  7306. );
  7307. $this->assertTags($result, $expected);
  7308. $this->Form->inputDefaults(array(
  7309. 'label' => false,
  7310. ), true);
  7311. $result = $this->Form->input('Contact.field1');
  7312. $expected = array(
  7313. 'input' => array(
  7314. 'type' => 'text', 'name' => 'Contact[field1]',
  7315. 'id' => 'ContactField1'
  7316. ),
  7317. );
  7318. $this->assertTags($result, $expected);
  7319. $result = $this->Form->inputDefaults();
  7320. $expected = array(
  7321. 'div' => false,
  7322. 'label' => false,
  7323. );
  7324. $this->assertEquals($expected, $result);
  7325. }
  7326. }