FormHelperTest.php 238 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @since CakePHP(tm) v 1.2.0.4206
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\View\Helper;
  18. use Cake\Controller\Controller;
  19. use Cake\Core\App;
  20. use Cake\Core\Configure;
  21. use Cake\Core\Plugin;
  22. use Cake\Network\Request;
  23. use Cake\ORM\Entity;
  24. use Cake\ORM\Table;
  25. use Cake\ORM\TableRegistry;
  26. use Cake\Routing\Router;
  27. use Cake\TestSuite\TestCase;
  28. use Cake\Utility\Security;
  29. use Cake\View\Helper\FormHelper;
  30. use Cake\View\Helper\HtmlHelper;
  31. use Cake\View\View;
  32. /**
  33. * Test stub.
  34. */
  35. class Article extends Entity {
  36. }
  37. /**
  38. * ContactTestController class
  39. *
  40. */
  41. class ContactTestController extends Controller {
  42. /**
  43. * uses property
  44. *
  45. * @var mixed null
  46. */
  47. public $uses = null;
  48. }
  49. /**
  50. * Contact class
  51. *
  52. */
  53. class ContactsTable extends Table {
  54. /**
  55. * Default schema
  56. *
  57. * @var array
  58. */
  59. protected $_schema = array(
  60. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  61. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  65. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  66. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  67. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  68. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  69. );
  70. /**
  71. * validate property
  72. *
  73. * @var array
  74. */
  75. public $validate = array(
  76. 'non_existing' => array(),
  77. 'idontexist' => array(),
  78. 'imrequired' => array('_allowEmpty' => false, array('rule' => array('between', 5, 30))),
  79. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  80. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  81. 'imrequiredonboth' => array(
  82. 'required' => array('rule' => 'alphaNumeric'),
  83. ),
  84. 'string_required' => 'notEmpty',
  85. 'imalsorequired' => array('_allowEmpty' => false, array('rule' => 'alphaNumeric')),
  86. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  87. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  88. 'imnotrequired' => array(
  89. '_allowEmpty' => true,
  90. array('rule' => 'alphaNumeric')
  91. ),
  92. 'imalsonotrequired' => array(
  93. '_allowEmpty' => true,
  94. 'alpha' => array('rule' => 'alphaNumeric'),
  95. 'between' => array('rule' => array('between', 5, 30)),
  96. ),
  97. 'imnotrequiredeither' => array(
  98. '_allowEmpty' => true,
  99. array('rule' => array('between', 5, 30))
  100. ),
  101. 'iamrequiredalways' => array(
  102. 'email' => array('rule' => 'email'),
  103. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  104. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  105. ),
  106. 'boolean_field' => array('rule' => 'boolean')
  107. );
  108. /**
  109. * schema method
  110. *
  111. * @return void
  112. */
  113. public function setSchema($schema) {
  114. $this->_schema = $schema;
  115. }
  116. /**
  117. * hasAndBelongsToMany property
  118. *
  119. * @var array
  120. */
  121. public $hasAndBelongsToMany = array(
  122. 'ContactTag' => array(
  123. 'className' => 'Cake\Test\TestCase\View\Helper\ContactTag',
  124. 'with' => 'Cake\Test\TestCase\View\Helper\ContactTagsContact'
  125. )
  126. );
  127. /**
  128. * hasAndBelongsToMany property
  129. *
  130. * @var array
  131. */
  132. public $belongsTo = array(
  133. 'User' => array('className' => 'UserForm'
  134. ));
  135. }
  136. /**
  137. * ContactTagsContact class
  138. *
  139. */
  140. class ContactTagsContactsTable extends Table {
  141. /**
  142. * Default schema
  143. *
  144. * @var array
  145. */
  146. protected $_schema = array(
  147. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  148. 'contact_tag_id' => array(
  149. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  150. )
  151. );
  152. /**
  153. * schema method
  154. *
  155. * @return void
  156. */
  157. public function setSchema($schema) {
  158. $this->_schema = $schema;
  159. }
  160. }
  161. /**
  162. * ContactNonStandardPk class
  163. *
  164. */
  165. class ContactNonStandardPk extends ContactsTable {
  166. /**
  167. * primaryKey property
  168. *
  169. * @var string
  170. */
  171. public $primaryKey = 'pk';
  172. /**
  173. * schema method
  174. *
  175. * @return void
  176. */
  177. public function schema($field = false) {
  178. $this->_schema = parent::schema();
  179. $this->_schema['pk'] = $this->_schema['id'];
  180. unset($this->_schema['id']);
  181. return $this->_schema;
  182. }
  183. }
  184. /**
  185. * ContactTag class
  186. *
  187. */
  188. class ContactTagsTable extends Table {
  189. /**
  190. * schema definition
  191. *
  192. * @var array
  193. */
  194. protected $_schema = array(
  195. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  196. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  197. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  198. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  199. );
  200. }
  201. /**
  202. * UserForm class
  203. *
  204. */
  205. class UserFormsTable extends Table {
  206. /**
  207. * hasMany property
  208. *
  209. * @var array
  210. */
  211. public $hasMany = array(
  212. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  213. ));
  214. /**
  215. * schema definition
  216. *
  217. * @var array
  218. */
  219. protected $_schema = array(
  220. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  221. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  222. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  223. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  224. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  225. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  226. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  227. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  228. );
  229. }
  230. /**
  231. * OpenidUrl class
  232. *
  233. */
  234. class OpenidUrlsTable extends Table {
  235. /**
  236. * belongsTo property
  237. *
  238. * @var array
  239. */
  240. public $belongsTo = array('UserForm' => array(
  241. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  242. ));
  243. /**
  244. * validate property
  245. *
  246. * @var array
  247. */
  248. public $validate = array('openid_not_registered' => array());
  249. /**
  250. * schema method
  251. *
  252. * @var array
  253. */
  254. protected $_schema = array(
  255. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  256. 'user_form_id' => array(
  257. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  258. ),
  259. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  260. );
  261. /**
  262. * beforeValidate method
  263. *
  264. * @return void
  265. */
  266. public function beforeValidate($options = array()) {
  267. $this->invalidate('openid_not_registered');
  268. return true;
  269. }
  270. }
  271. /**
  272. * ValidateUser class
  273. *
  274. */
  275. class ValidateUsersTable extends Table {
  276. /**
  277. * hasOne property
  278. *
  279. * @var array
  280. */
  281. public $hasOne = array('ValidateProfile' => array(
  282. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  283. ));
  284. /**
  285. * schema method
  286. *
  287. * @var array
  288. */
  289. protected $_schema = array(
  290. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  291. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  292. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  293. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  294. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => '6,3'),
  295. 'ratio' => array('type' => 'decimal', 'null' => false, 'length' => '10,6'),
  296. 'population' => array('type' => 'decimal', 'null' => false, 'length' => '15,0'),
  297. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  298. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  299. );
  300. /**
  301. * beforeValidate method
  302. *
  303. * @param array $options
  304. * @return void
  305. */
  306. public function beforeValidate($options = array()) {
  307. $this->invalidate('email');
  308. return false;
  309. }
  310. }
  311. /**
  312. * ValidateProfile class
  313. *
  314. */
  315. class ValidateProfilesTable extends Table {
  316. /**
  317. * useTable property
  318. *
  319. * @var boolean
  320. */
  321. public $useTable = false;
  322. /**
  323. * schema property
  324. *
  325. * @var array
  326. */
  327. protected $_schema = array(
  328. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  329. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  330. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  331. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  332. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  333. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  334. );
  335. /**
  336. * hasOne property
  337. *
  338. * @var array
  339. */
  340. public $hasOne = array('ValidateItem' => array(
  341. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  342. ));
  343. /**
  344. * belongsTo property
  345. *
  346. * @var array
  347. */
  348. public $belongsTo = array('ValidateUser' => array(
  349. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  350. ));
  351. /**
  352. * beforeValidate method
  353. *
  354. * @return void
  355. */
  356. public function beforeValidate($options = array()) {
  357. $this->invalidate('full_name');
  358. $this->invalidate('city');
  359. return false;
  360. }
  361. }
  362. /**
  363. * ValidateItem class
  364. *
  365. */
  366. class ValidateItemsTable extends Table {
  367. /**
  368. * schema property
  369. *
  370. * @var array
  371. */
  372. protected $_schema = array(
  373. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  374. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  375. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  376. 'description' => array(
  377. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  378. ),
  379. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  380. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  381. );
  382. /**
  383. * belongsTo property
  384. *
  385. * @var array
  386. */
  387. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  388. /**
  389. * beforeValidate method
  390. *
  391. * @return void
  392. */
  393. public function beforeValidate($options = array()) {
  394. $this->invalidate('description');
  395. return false;
  396. }
  397. }
  398. /**
  399. * TestMail class
  400. *
  401. */
  402. class TestMailsTable extends Table {
  403. }
  404. /**
  405. * FormHelperTest class
  406. *
  407. * @property FormHelper $Form
  408. */
  409. class FormHelperTest extends TestCase {
  410. /**
  411. * Fixtures to be used
  412. *
  413. * @var array
  414. */
  415. public $fixtures = array('core.article');
  416. /**
  417. * Do not load the fixtures by default
  418. *
  419. * @var boolean
  420. */
  421. public $autoFixtures = false;
  422. /**
  423. * setUp method
  424. *
  425. * @return void
  426. */
  427. public function setUp() {
  428. parent::setUp();
  429. Configure::write('Config.language', 'eng');
  430. Configure::write('App.base', '');
  431. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  432. Configure::delete('Asset');
  433. $this->View = new View(null);
  434. $this->Form = new FormHelper($this->View);
  435. $this->Form->Html = new HtmlHelper($this->View);
  436. $this->Form->request = new Request('articles/add');
  437. $this->Form->request->here = '/articles/add';
  438. $this->Form->request['controller'] = 'articles';
  439. $this->Form->request['action'] = 'add';
  440. $this->Form->request->webroot = '';
  441. $this->Form->request->base = '';
  442. $this->dateRegex = array(
  443. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  444. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  445. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  446. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  447. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  448. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  449. );
  450. $this->article = [
  451. 'schema' => [
  452. 'id' => ['type' => 'integer'],
  453. 'author_id' => ['type' => 'integer', 'null' => true],
  454. 'title' => ['type' => 'string', 'null' => true],
  455. 'body' => 'text',
  456. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  457. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  458. ],
  459. 'required' => [
  460. 'author_id' => true,
  461. 'title' => true,
  462. ]
  463. ];
  464. Configure::write('Security.salt', 'foo!');
  465. Router::connect('/:controller', array('action' => 'index'));
  466. Router::connect('/:controller/:action/*');
  467. }
  468. /**
  469. * tearDown method
  470. *
  471. * @return void
  472. */
  473. public function tearDown() {
  474. parent::tearDown();
  475. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  476. }
  477. /**
  478. * Test registering a new widget class and rendering it.
  479. *
  480. * @return void
  481. */
  482. public function testAddWidgetAndRenderWidget() {
  483. $data = [
  484. 'val' => 1
  485. ];
  486. $mock = $this->getMock('Cake\View\Widget\WidgetInterface');
  487. $this->assertNull($this->Form->addWidget('test', $mock));
  488. $mock->expects($this->once())
  489. ->method('render')
  490. ->with($data)
  491. ->will($this->returnValue('HTML'));
  492. $result = $this->Form->widget('test', $data);
  493. $this->assertEquals('HTML', $result);
  494. }
  495. /**
  496. * Test registering an invalid widget class.
  497. *
  498. * @expectedException \RuntimeException
  499. * @return void
  500. */
  501. public function testAddWidgetInvalid() {
  502. $mock = new \StdClass();
  503. $this->Form->addWidget('test', $mock);
  504. $this->Form->widget('test');
  505. }
  506. /**
  507. * Test adding a new context class.
  508. *
  509. * @return void
  510. */
  511. public function testAddContextProvider() {
  512. $context = 'My data';
  513. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  514. $this->assertInstanceOf('Cake\Network\Request', $request);
  515. $this->assertEquals($context, $data['entity']);
  516. return $this->getMock('Cake\View\Form\ContextInterface');
  517. });
  518. $this->Form->create($context);
  519. }
  520. /**
  521. * Test adding an invalid context class.
  522. *
  523. * @expectedException RuntimeException
  524. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  525. * @return void
  526. */
  527. public function testAddContextProviderInvalid() {
  528. $context = 'My data';
  529. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  530. return new \StdClass();
  531. });
  532. $this->Form->create($context);
  533. }
  534. /**
  535. * Provides context options for create().
  536. *
  537. * @return array
  538. */
  539. public function contextSelectionProvider() {
  540. $entity = new Article();
  541. $collection = $this->getMock('Cake\Collection\Collection', ['extract'], [[$entity]]);
  542. $data = [
  543. 'schema' => [
  544. 'title' => ['type' => 'string']
  545. ]
  546. ];
  547. return [
  548. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  549. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  550. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  551. 'none' => [null, 'Cake\View\Form\NullContext'],
  552. 'false' => [false, 'Cake\View\Form\NullContext'],
  553. ];
  554. }
  555. /**
  556. * Test default context selection in create()
  557. *
  558. * @dataProvider contextSelectionProvider
  559. * @return void
  560. */
  561. public function testCreateContextSelectionBuiltIn($data, $class) {
  562. $this->loadFixtures('Article');
  563. $this->Form->create($data);
  564. $this->assertInstanceOf($class, $this->Form->context());
  565. }
  566. /**
  567. * Data provider for type option.
  568. *
  569. * @return array
  570. */
  571. public static function requestTypeProvider() {
  572. return [
  573. // type, method, override
  574. ['post', 'post', 'POST'],
  575. ['put', 'post', 'PUT'],
  576. ['patch', 'post', 'PATCH'],
  577. ['delete', 'post', 'DELETE'],
  578. ];
  579. }
  580. /**
  581. * Test creating file forms.
  582. *
  583. * @return void
  584. */
  585. public function testCreateFile() {
  586. $encoding = strtolower(Configure::read('App.encoding'));
  587. $result = $this->Form->create(false, array('type' => 'file'));
  588. $expected = array(
  589. 'form' => array(
  590. 'method' => 'post', 'action' => '/articles/add',
  591. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  592. ),
  593. 'div' => array('style' => 'display:none;'),
  594. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  595. '/div'
  596. );
  597. $this->assertTags($result, $expected);
  598. }
  599. /**
  600. * Test creating GET forms.
  601. *
  602. * @return void
  603. */
  604. public function testCreateGet() {
  605. $encoding = strtolower(Configure::read('App.encoding'));
  606. $result = $this->Form->create(false, array('type' => 'get'));
  607. $expected = array('form' => array(
  608. 'method' => 'get', 'action' => '/articles/add',
  609. 'accept-charset' => $encoding
  610. ));
  611. $this->assertTags($result, $expected);
  612. }
  613. /**
  614. * test the create() method
  615. *
  616. * @dataProvider requestTypeProvider
  617. * @return void
  618. */
  619. public function testCreateTypeOptions($type, $method, $override) {
  620. $encoding = strtolower(Configure::read('App.encoding'));
  621. $result = $this->Form->create(false, array('type' => $type));
  622. $expected = array(
  623. 'form' => array(
  624. 'method' => $method, 'action' => '/articles/add',
  625. 'accept-charset' => $encoding
  626. ),
  627. 'div' => array('style' => 'display:none;'),
  628. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => $override),
  629. '/div'
  630. );
  631. $this->assertTags($result, $expected);
  632. }
  633. /**
  634. * Test opening a form for an update operation.
  635. *
  636. * @return void
  637. */
  638. public function testCreateUpdateForm() {
  639. $encoding = strtolower(Configure::read('App.encoding'));
  640. $this->Form->request->here = '/articles/edit/1';
  641. $this->Form->request['action'] = 'edit';
  642. $this->article['defaults']['id'] = 1;
  643. $result = $this->Form->create($this->article);
  644. $expected = array(
  645. 'form' => array(
  646. 'method' => 'post', 'action' => '/articles/edit/1',
  647. 'accept-charset' => $encoding
  648. ),
  649. 'div' => array('style' => 'display:none;'),
  650. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  651. '/div'
  652. );
  653. $this->assertTags($result, $expected);
  654. }
  655. /**
  656. * Test the onsubmit option for create()
  657. *
  658. * @return void
  659. */
  660. public function testCreateOnSubmit() {
  661. $this->Form->request->data = [];
  662. $this->Form->request['controller'] = 'articles';
  663. $result = $this->Form->create($this->article, ['url' => ['action' => 'index', 'param'], 'default' => false]);
  664. $expected = array(
  665. 'form' => array(
  666. 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/articles/index/param',
  667. 'accept-charset' => 'utf-8'
  668. ),
  669. 'div' => array('style' => 'display:none;'),
  670. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  671. '/div'
  672. );
  673. $this->assertTags($result, $expected);
  674. $this->Form->request->data = [];
  675. $this->Form->request['controller'] = 'articles';
  676. $result = $this->Form->create($this->article, array(
  677. 'url' => array('action' => 'index', 'param'),
  678. 'default' => false,
  679. 'onsubmit' => 'someFunction();'
  680. ));
  681. $expected = array(
  682. 'form' => array(
  683. 'method' => 'post',
  684. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  685. 'action' => '/articles/index/param',
  686. 'accept-charset' => 'utf-8'
  687. ),
  688. 'div' => array('style' => 'display:none;'),
  689. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  690. '/div'
  691. );
  692. $this->assertTags($result, $expected);
  693. }
  694. /**
  695. * test create() with automatic url generation
  696. *
  697. * @return void
  698. */
  699. public function testCreateAutoUrl() {
  700. $encoding = strtolower(Configure::read('App.encoding'));
  701. $this->Form->request['action'] = 'delete';
  702. $this->Form->request->here = '/articles/delete/10';
  703. $this->Form->request->base = '';
  704. $result = $this->Form->create($this->article);
  705. $expected = array(
  706. 'form' => array(
  707. 'method' => 'post', 'action' => '/articles/delete/10',
  708. 'accept-charset' => $encoding
  709. ),
  710. 'div' => array('style' => 'display:none;'),
  711. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  712. '/div'
  713. );
  714. $this->assertTags($result, $expected);
  715. $this->article['defaults'] = ['id' => 1];
  716. $this->Form->request->here = '/articles/edit/1';
  717. $this->Form->request['action'] = 'delete';
  718. $result = $this->Form->create($this->article, ['action' => 'edit']);
  719. $expected = array(
  720. 'form' => array(
  721. 'method' => 'post',
  722. 'action' => '/articles/edit/1',
  723. 'accept-charset' => $encoding
  724. ),
  725. 'div' => array('style' => 'display:none;'),
  726. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  727. '/div'
  728. );
  729. $this->assertTags($result, $expected);
  730. $this->Form->request['action'] = 'add';
  731. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  732. $expected = array(
  733. 'form' => array(
  734. 'method' => 'post',
  735. 'action' => '/articles/publish/1',
  736. 'accept-charset' => $encoding
  737. ),
  738. 'div' => array('style' => 'display:none;'),
  739. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  740. '/div'
  741. );
  742. $this->assertTags($result, $expected);
  743. $result = $this->Form->create($this->article, array('url' => '/articles/publish'));
  744. $expected = array(
  745. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  746. 'div' => array('style' => 'display:none;'),
  747. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  748. '/div'
  749. );
  750. $this->assertTags($result, $expected);
  751. $this->Form->request['controller'] = 'pages';
  752. $result = $this->Form->create($this->article, array('action' => 'signup'));
  753. $expected = array(
  754. 'form' => array(
  755. 'method' => 'post', 'action' => '/pages/signup/1',
  756. 'accept-charset' => $encoding
  757. ),
  758. 'div' => array('style' => 'display:none;'),
  759. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  760. '/div'
  761. );
  762. $this->assertTags($result, $expected);
  763. }
  764. /**
  765. * test create() with a custom route
  766. *
  767. * @return void
  768. */
  769. public function testCreateCustomRoute() {
  770. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  771. $encoding = strtolower(Configure::read('App.encoding'));
  772. $this->Form->request['controller'] = 'users';
  773. $result = $this->Form->create(false, array('action' => 'login'));
  774. $expected = array(
  775. 'form' => array(
  776. 'method' => 'post', 'action' => '/login',
  777. 'accept-charset' => $encoding
  778. ),
  779. 'div' => array('style' => 'display:none;'),
  780. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  781. '/div'
  782. );
  783. $this->assertTags($result, $expected);
  784. }
  785. /**
  786. * test that inputDefaults are stored and used.
  787. *
  788. * @return void
  789. */
  790. public function testCreateWithInputDefaults() {
  791. $this->markTestIncomplete('Need to revisit once models work again.');
  792. $this->Form->create('User', array(
  793. 'inputDefaults' => array(
  794. 'div' => false,
  795. 'label' => false,
  796. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  797. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  798. )
  799. ));
  800. $result = $this->Form->input('username');
  801. $expected = array(
  802. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername')
  803. );
  804. $this->assertTags($result, $expected);
  805. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  806. $expected = array(
  807. 'div' => array('class' => 'input text'),
  808. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  809. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  810. '/div'
  811. );
  812. $this->assertTags($result, $expected);
  813. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  814. $expected = array(
  815. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  816. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  817. );
  818. $this->assertTags($result, $expected);
  819. $this->Form->create('User', array(
  820. 'inputDefaults' => array(
  821. 'div' => false,
  822. 'label' => array('class' => 'nice', 'for' => 'changed'),
  823. )
  824. ));
  825. $result = $this->Form->input('username', array('div' => true));
  826. $expected = array(
  827. 'div' => array('class' => 'input text'),
  828. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  829. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  830. '/div'
  831. );
  832. $this->assertTags($result, $expected);
  833. }
  834. /**
  835. * test automatic accept-charset overriding
  836. *
  837. * @return void
  838. */
  839. public function testCreateWithAcceptCharset() {
  840. $result = $this->Form->create($this->article, array(
  841. 'type' => 'post', 'action' => 'index', 'encoding' => 'iso-8859-1'
  842. )
  843. );
  844. $expected = array(
  845. 'form' => array(
  846. 'method' => 'post', 'action' => '/articles',
  847. 'accept-charset' => 'iso-8859-1'
  848. ),
  849. 'div' => array('style' => 'display:none;'),
  850. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  851. '/div'
  852. );
  853. $this->assertTags($result, $expected);
  854. }
  855. /**
  856. * Test base form URL when url param is passed with multiple parameters (&)
  857. *
  858. */
  859. public function testCreateQuerystringrequest() {
  860. $encoding = strtolower(Configure::read('App.encoding'));
  861. $result = $this->Form->create($this->article, array(
  862. 'type' => 'post',
  863. 'escape' => false,
  864. 'url' => array(
  865. 'controller' => 'controller',
  866. 'action' => 'action',
  867. '?' => array('param1' => 'value1', 'param2' => 'value2')
  868. )
  869. ));
  870. $expected = array(
  871. 'form' => array(
  872. 'method' => 'post',
  873. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  874. 'accept-charset' => $encoding
  875. ),
  876. 'div' => array('style' => 'display:none;'),
  877. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  878. '/div'
  879. );
  880. $this->assertTags($result, $expected);
  881. $result = $this->Form->create($this->article, array(
  882. 'type' => 'post',
  883. 'url' => array(
  884. 'controller' => 'controller',
  885. 'action' => 'action',
  886. '?' => array('param1' => 'value1', 'param2' => 'value2')
  887. )
  888. ));
  889. $this->assertTags($result, $expected);
  890. }
  891. /**
  892. * test that create() doesn't cause errors by multiple id's being in the primary key
  893. * as could happen with multiple select or checkboxes.
  894. *
  895. * @return void
  896. */
  897. public function testCreateWithMultipleIdInData() {
  898. $encoding = strtolower(Configure::read('App.encoding'));
  899. $this->Form->request->data['Article']['id'] = array(1, 2);
  900. $result = $this->Form->create($this->article);
  901. $expected = array(
  902. 'form' => array(
  903. 'method' => 'post',
  904. 'action' => '/articles/add',
  905. 'accept-charset' => $encoding
  906. ),
  907. 'div' => array('style' => 'display:none;'),
  908. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  909. '/div'
  910. );
  911. $this->assertTags($result, $expected);
  912. }
  913. /**
  914. * test that create() doesn't add in extra passed params.
  915. *
  916. * @return void
  917. */
  918. public function testCreatePassedArgs() {
  919. $encoding = strtolower(Configure::read('App.encoding'));
  920. $this->Form->request->data['Article']['id'] = 1;
  921. $result = $this->Form->create($this->article, array(
  922. 'type' => 'post',
  923. 'escape' => false,
  924. 'url' => array(
  925. 'action' => 'edit',
  926. 'myparam'
  927. )
  928. ));
  929. $expected = array(
  930. 'form' => array(
  931. 'method' => 'post',
  932. 'action' => '/articles/edit/myparam',
  933. 'accept-charset' => $encoding
  934. ),
  935. 'div' => array('style' => 'display:none;'),
  936. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  937. '/div'
  938. );
  939. $this->assertTags($result, $expected);
  940. }
  941. /**
  942. * test creating a get form, and get form inputs.
  943. *
  944. * @return void
  945. */
  946. public function testGetFormCreate() {
  947. $encoding = strtolower(Configure::read('App.encoding'));
  948. $result = $this->Form->create($this->article, array('type' => 'get'));
  949. $this->assertTags($result, array('form' => array(
  950. 'method' => 'get', 'action' => '/articles/add',
  951. 'accept-charset' => $encoding
  952. )));
  953. $result = $this->Form->text('title');
  954. $this->assertTags($result, array('input' => array(
  955. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  956. )));
  957. $result = $this->Form->password('password');
  958. $this->assertTags($result, array('input' => array(
  959. 'name' => 'password', 'type' => 'password'
  960. )));
  961. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  962. $result = $this->Form->text('user_form');
  963. $this->assertTags($result, array('input' => array(
  964. 'name' => 'user_form', 'type' => 'text'
  965. )));
  966. }
  967. /**
  968. * test get form, and inputs when the model param is false
  969. *
  970. * @return void
  971. */
  972. public function testGetFormWithFalseModel() {
  973. $encoding = strtolower(Configure::read('App.encoding'));
  974. $this->Form->request['controller'] = 'contact_test';
  975. $result = $this->Form->create(false, array(
  976. 'type' => 'get', 'url' => array('controller' => 'contact_test')
  977. ));
  978. $expected = array('form' => array(
  979. 'method' => 'get', 'action' => '/contact_test/add',
  980. 'accept-charset' => $encoding
  981. ));
  982. $this->assertTags($result, $expected);
  983. $result = $this->Form->text('reason');
  984. $expected = array(
  985. 'input' => array('type' => 'text', 'name' => 'reason')
  986. );
  987. $this->assertTags($result, $expected);
  988. }
  989. /**
  990. * testFormCreateWithSecurity method
  991. *
  992. * Test form->create() with security key.
  993. *
  994. * @return void
  995. */
  996. public function testCreateWithSecurity() {
  997. $this->Form->request->params['_csrfToken'] = 'testKey';
  998. $encoding = strtolower(Configure::read('App.encoding'));
  999. $result = $this->Form->create($this->article, [
  1000. 'url' => '/articles/publish',
  1001. ]);
  1002. $expected = array(
  1003. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  1004. 'div' => array('style' => 'display:none;'),
  1005. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1006. array('input' => array(
  1007. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  1008. )),
  1009. '/div'
  1010. );
  1011. $this->assertTags($result, $expected);
  1012. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1013. $expected['form']['id'] = 'MyForm';
  1014. $this->assertTags($result, $expected);
  1015. }
  1016. /**
  1017. * testFormCreateGetNoSecurity method
  1018. *
  1019. * Test form->create() with no security key as its a get form
  1020. *
  1021. * @return void
  1022. */
  1023. public function testCreateEndGetNoSecurity() {
  1024. $this->Form->request->params['_csrfToken'] = 'testKey';
  1025. $encoding = strtolower(Configure::read('App.encoding'));
  1026. $article = new Article();
  1027. $result = $this->Form->create($article, [
  1028. 'type' => 'get',
  1029. 'url' => '/contacts/add'
  1030. ]);
  1031. $this->assertNotContains('testKey', $result);
  1032. $result = $this->Form->end('Save');
  1033. $this->assertNotContains('testKey', $result);
  1034. }
  1035. /**
  1036. * test that create() clears the fields property so it starts fresh
  1037. *
  1038. * @return void
  1039. */
  1040. public function testCreateClearingFields() {
  1041. $this->Form->fields = array('model_id');
  1042. $this->Form->create($this->article);
  1043. $this->assertEquals(array(), $this->Form->fields);
  1044. }
  1045. /**
  1046. * Tests form hash generation with model-less data
  1047. *
  1048. * @return void
  1049. */
  1050. public function testValidateHashNoModel() {
  1051. $this->Form->request->params['_Token'] = 'foo';
  1052. $result = $this->Form->secure(array('anything'));
  1053. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1054. }
  1055. /**
  1056. * Tests that hidden fields generated for checkboxes don't get locked
  1057. *
  1058. * @return void
  1059. */
  1060. public function testNoCheckboxLocking() {
  1061. $this->Form->request->params['_Token'] = 'foo';
  1062. $this->assertSame([], $this->Form->fields);
  1063. $this->Form->checkbox('check', array('value' => '1'));
  1064. $this->assertSame($this->Form->fields, array('check'));
  1065. }
  1066. /**
  1067. * testFormSecurityFields method
  1068. *
  1069. * Test generation of secure form hash generation.
  1070. *
  1071. * @return void
  1072. */
  1073. public function testFormSecurityFields() {
  1074. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  1075. $this->Form->request->params['_Token'] = 'testKey';
  1076. $result = $this->Form->secure($fields);
  1077. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  1078. $hash .= ':' . 'Model.valid';
  1079. $hash = urlencode($hash);
  1080. $expected = array(
  1081. 'div' => array('style' => 'display:none;'),
  1082. array('input' => array(
  1083. 'type' => 'hidden',
  1084. 'name' => '_Token[fields]',
  1085. 'value' => $hash
  1086. )),
  1087. array('input' => array(
  1088. 'type' => 'hidden',
  1089. 'name' => '_Token[unlocked]',
  1090. 'value' => '',
  1091. )),
  1092. '/div'
  1093. );
  1094. $this->assertTags($result, $expected);
  1095. }
  1096. /**
  1097. * Tests correct generation of number fields for double and float fields
  1098. *
  1099. * @return void
  1100. */
  1101. public function testTextFieldGenerationForFloats() {
  1102. $this->markTestIncomplete('Need to revisit once models work again.');
  1103. $this->article['schema'] = [
  1104. 'foo' => [
  1105. 'type' => 'float',
  1106. 'null' => false,
  1107. 'default' => null,
  1108. 'length' => 10
  1109. ]
  1110. ];
  1111. $this->Form->create('Contact');
  1112. $result = $this->Form->input('foo');
  1113. $expected = array(
  1114. 'div' => array('class' => 'input number'),
  1115. 'label' => array('for' => 'ContactFoo'),
  1116. 'Foo',
  1117. '/label',
  1118. array('input' => array(
  1119. 'type' => 'number',
  1120. 'name' => 'Contact[foo]',
  1121. 'id' => 'ContactFoo',
  1122. 'step' => 'any'
  1123. )),
  1124. '/div'
  1125. );
  1126. $this->assertTags($result, $expected);
  1127. $result = $this->Form->input('foo', array('step' => 0.5));
  1128. $expected = array(
  1129. 'div' => array('class' => 'input number'),
  1130. 'label' => array('for' => 'ContactFoo'),
  1131. 'Foo',
  1132. '/label',
  1133. array('input' => array(
  1134. 'type' => 'number',
  1135. 'name' => 'Contact[foo]',
  1136. 'id' => 'ContactFoo',
  1137. 'step' => '0.5'
  1138. )),
  1139. '/div'
  1140. );
  1141. $this->assertTags($result, $expected);
  1142. }
  1143. /**
  1144. * Tests correct generation of number fields for integer fields
  1145. *
  1146. * @return void
  1147. */
  1148. public function testTextFieldTypeNumberGenerationForIntegers() {
  1149. $this->markTestIncomplete('Need to revisit once models work again.');
  1150. $model->setSchema(array('foo' => array(
  1151. 'type' => 'integer',
  1152. 'null' => false,
  1153. 'default' => null,
  1154. 'length' => null
  1155. )));
  1156. $this->Form->create('Contact');
  1157. $result = $this->Form->input('foo');
  1158. $expected = array(
  1159. 'div' => array('class' => 'input number'),
  1160. 'label' => array('for' => 'ContactFoo'),
  1161. 'Foo',
  1162. '/label',
  1163. array('input' => array(
  1164. 'type' => 'number', 'name' => 'Contact[foo]',
  1165. 'id' => 'ContactFoo'
  1166. )),
  1167. '/div'
  1168. );
  1169. $this->assertTags($result, $expected);
  1170. }
  1171. /**
  1172. * Tests correct generation of file upload fields for binary fields
  1173. *
  1174. * @return void
  1175. */
  1176. public function testFileUploadFieldTypeGenerationForBinaries() {
  1177. $this->markTestIncomplete('Need to revisit once models work again.');
  1178. $model->setSchema(array('foo' => array(
  1179. 'type' => 'binary',
  1180. 'null' => false,
  1181. 'default' => null,
  1182. 'length' => 1024
  1183. )));
  1184. $this->Form->create('Contact');
  1185. $result = $this->Form->input('foo');
  1186. $expected = array(
  1187. 'div' => array('class' => 'input file'),
  1188. 'label' => array('for' => 'ContactFoo'),
  1189. 'Foo',
  1190. '/label',
  1191. array('input' => array(
  1192. 'type' => 'file', 'name' => 'data[Contact][foo]',
  1193. 'id' => 'ContactFoo'
  1194. )),
  1195. '/div'
  1196. );
  1197. $this->assertTags($result, $expected);
  1198. }
  1199. /**
  1200. * testFormSecurityMultipleFields method
  1201. *
  1202. * Test secure() with multiple row form. Ensure hash is correct.
  1203. *
  1204. * @return void
  1205. */
  1206. public function testFormSecurityMultipleFields() {
  1207. $this->Form->request->params['_Token'] = 'foo';
  1208. $fields = array(
  1209. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1210. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1211. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1212. );
  1213. $result = $this->Form->secure($fields);
  1214. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1215. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1216. $expected = array(
  1217. 'div' => array('style' => 'display:none;'),
  1218. array('input' => array(
  1219. 'type' => 'hidden', 'name' => '_Token[fields]',
  1220. 'value' => $hash
  1221. )),
  1222. array('input' => array(
  1223. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1224. 'value' => ''
  1225. )),
  1226. '/div'
  1227. );
  1228. $this->assertTags($result, $expected);
  1229. }
  1230. /**
  1231. * testFormSecurityMultipleSubmitButtons
  1232. *
  1233. * test form submit generation and ensure that _Token is only created on end()
  1234. *
  1235. * @return void
  1236. */
  1237. public function testFormSecurityMultipleSubmitButtons() {
  1238. $this->Form->request->params['_Token'] = 'testKey';
  1239. $this->Form->create($this->article);
  1240. $this->Form->text('Address.title');
  1241. $this->Form->text('Address.first_name');
  1242. $result = $this->Form->submit('Save', array('name' => 'save'));
  1243. $expected = array(
  1244. 'div' => array('class' => 'submit'),
  1245. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  1246. '/div',
  1247. );
  1248. $this->assertTags($result, $expected);
  1249. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  1250. $expected = array(
  1251. 'div' => array('class' => 'submit'),
  1252. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  1253. '/div',
  1254. );
  1255. $this->assertTags($result, $expected);
  1256. $result = $this->Form->end();
  1257. $expected = array(
  1258. 'div' => array('style' => 'display:none;'),
  1259. array('input' => array(
  1260. 'type' => 'hidden',
  1261. 'name' => '_Token[fields]',
  1262. 'value'
  1263. )),
  1264. array('input' => array(
  1265. 'type' => 'hidden',
  1266. 'name' => '_Token[unlocked]',
  1267. 'value' => 'cancel%7Csave'
  1268. )),
  1269. '/div'
  1270. );
  1271. $this->assertTags($result, $expected);
  1272. }
  1273. /**
  1274. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1275. *
  1276. * @return void
  1277. */
  1278. public function testSecurityButtonNestedNamed() {
  1279. $key = 'testKey';
  1280. $this->Form->request->params['_csrfToken'] = $key;
  1281. $this->Form->create('Addresses');
  1282. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1283. $result = $this->Form->unlockField();
  1284. $this->assertEquals(array('Address.button'), $result);
  1285. }
  1286. /**
  1287. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1288. *
  1289. * @return void
  1290. */
  1291. public function testSecuritySubmitNestedNamed() {
  1292. $this->Form->request->params['_Token'] = 'testKey';
  1293. $this->Form->create($this->article);
  1294. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1295. $result = $this->Form->unlockField();
  1296. $this->assertEquals(array('Address.button'), $result);
  1297. }
  1298. /**
  1299. * Test that the correct fields are unlocked for image submits with no names.
  1300. *
  1301. * @return void
  1302. */
  1303. public function testSecuritySubmitImageNoName() {
  1304. $key = 'testKey';
  1305. $this->Form->request->params['_Token'] = 'testKey';
  1306. $this->Form->create(false);
  1307. $result = $this->Form->submit('save.png');
  1308. $expected = array(
  1309. 'div' => array('class' => 'submit'),
  1310. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  1311. '/div'
  1312. );
  1313. $this->assertTags($result, $expected);
  1314. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  1315. }
  1316. /**
  1317. * Test that the correct fields are unlocked for image submits with names.
  1318. *
  1319. * @return void
  1320. */
  1321. public function testSecuritySubmitImageName() {
  1322. $this->Form->request->params['_Token'] = 'testKey';
  1323. $this->Form->create(null);
  1324. $result = $this->Form->submit('save.png', array('name' => 'test'));
  1325. $expected = array(
  1326. 'div' => array('class' => 'submit'),
  1327. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  1328. '/div'
  1329. );
  1330. $this->assertTags($result, $expected);
  1331. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  1332. }
  1333. /**
  1334. * testFormSecurityMultipleInputFields method
  1335. *
  1336. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1337. *
  1338. * @return void
  1339. */
  1340. public function testFormSecurityMultipleInputFields() {
  1341. $this->markTestIncomplete('Need to revisit once models work again.');
  1342. $key = 'testKey';
  1343. $this->Form->request->params['_csrfToken'] = $key;
  1344. $this->Form->create('Addresses');
  1345. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1346. $this->Form->input('Addresses.0.title');
  1347. $this->Form->input('Addresses.0.first_name');
  1348. $this->Form->input('Addresses.0.last_name');
  1349. $this->Form->input('Addresses.0.address');
  1350. $this->Form->input('Addresses.0.city');
  1351. $this->Form->input('Addresses.0.phone');
  1352. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  1353. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1354. $this->Form->input('Addresses.1.title');
  1355. $this->Form->input('Addresses.1.first_name');
  1356. $this->Form->input('Addresses.1.last_name');
  1357. $this->Form->input('Addresses.1.address');
  1358. $this->Form->input('Addresses.1.city');
  1359. $this->Form->input('Addresses.1.phone');
  1360. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  1361. $result = $this->Form->secure($this->Form->fields);
  1362. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  1363. $expected = array(
  1364. 'div' => array('style' => 'display:none;'),
  1365. array('input' => array(
  1366. 'type' => 'hidden', 'name' => '_Token[fields]',
  1367. 'value' => $hash
  1368. )),
  1369. array('input' => array(
  1370. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1371. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1372. )),
  1373. '/div'
  1374. );
  1375. $this->assertTags($result, $expected);
  1376. }
  1377. /**
  1378. * Test form security with Model.field.0 style inputs
  1379. *
  1380. * @return void
  1381. */
  1382. public function testFormSecurityArrayFields() {
  1383. $this->Form->request->params['_Token'] = 'testKey';
  1384. $this->Form->create('Address');
  1385. $this->Form->text('Address.primary.1');
  1386. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1387. $this->Form->text('Address.secondary.1.0');
  1388. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1389. }
  1390. /**
  1391. * testFormSecurityMultipleInputDisabledFields method
  1392. *
  1393. * test secure form generation with multiple records and disabled fields.
  1394. *
  1395. * @return void
  1396. */
  1397. public function testFormSecurityMultipleInputDisabledFields() {
  1398. $this->Form->request->params['_Token'] = array(
  1399. 'unlockedFields' => array('first_name', 'address')
  1400. );
  1401. $this->Form->create();
  1402. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1403. $this->Form->text('Addresses.0.title');
  1404. $this->Form->text('Addresses.0.first_name');
  1405. $this->Form->text('Addresses.0.last_name');
  1406. $this->Form->text('Addresses.0.address');
  1407. $this->Form->text('Addresses.0.city');
  1408. $this->Form->text('Addresses.0.phone');
  1409. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1410. $this->Form->text('Addresses.1.title');
  1411. $this->Form->text('Addresses.1.first_name');
  1412. $this->Form->text('Addresses.1.last_name');
  1413. $this->Form->text('Addresses.1.address');
  1414. $this->Form->text('Addresses.1.city');
  1415. $this->Form->text('Addresses.1.phone');
  1416. $result = $this->Form->secure($this->Form->fields);
  1417. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1418. $expected = array(
  1419. 'div' => array('style' => 'display:none;'),
  1420. array('input' => array(
  1421. 'type' => 'hidden',
  1422. 'name' => '_Token[fields]',
  1423. 'value' => $hash
  1424. )),
  1425. array('input' => array(
  1426. 'type' => 'hidden',
  1427. 'name' => '_Token[unlocked]',
  1428. 'value' => 'address%7Cfirst_name',
  1429. )),
  1430. '/div'
  1431. );
  1432. $this->assertTags($result, $expected);
  1433. }
  1434. /**
  1435. * testFormSecurityInputDisabledFields method
  1436. *
  1437. * Test single record form with disabled fields.
  1438. *
  1439. * @return void
  1440. */
  1441. public function testFormSecurityInputUnlockedFields() {
  1442. $this->Form->request['_Token'] = array(
  1443. 'unlockedFields' => array('first_name', 'address')
  1444. );
  1445. $this->Form->create();
  1446. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1447. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1448. $this->Form->text('Addresses.title');
  1449. $this->Form->text('Addresses.first_name');
  1450. $this->Form->text('Addresses.last_name');
  1451. $this->Form->text('Addresses.address');
  1452. $this->Form->text('Addresses.city');
  1453. $this->Form->text('Addresses.phone');
  1454. $result = $this->Form->fields;
  1455. $expected = array(
  1456. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1457. 'Addresses.city', 'Addresses.phone'
  1458. );
  1459. $this->assertEquals($expected, $result);
  1460. $result = $this->Form->secure($expected);
  1461. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1462. $expected = array(
  1463. 'div' => array('style' => 'display:none;'),
  1464. array('input' => array(
  1465. 'type' => 'hidden',
  1466. 'name' => '_Token[fields]',
  1467. 'value' => $hash
  1468. )),
  1469. array('input' => array(
  1470. 'type' => 'hidden',
  1471. 'name' => '_Token[unlocked]',
  1472. 'value' => 'address%7Cfirst_name',
  1473. )),
  1474. '/div'
  1475. );
  1476. $this->assertTags($result, $expected);
  1477. }
  1478. /**
  1479. * test securing inputs with custom name attributes.
  1480. *
  1481. * @return void
  1482. */
  1483. public function testFormSecureWithCustomNameAttribute() {
  1484. $this->Form->request->params['_Token'] = 'testKey';
  1485. $this->Form->text('UserForm.published', array('name' => 'User[custom]'));
  1486. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1487. $this->Form->text('UserForm.published', array('name' => 'User[custom][another][value]'));
  1488. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1489. }
  1490. /**
  1491. * testFormSecuredInput method
  1492. *
  1493. * Test generation of entire secure form, assertions made on input() output.
  1494. *
  1495. * @return void
  1496. */
  1497. public function testFormSecuredInput() {
  1498. $this->markTestIncomplete('Need to revisit once models work again.');
  1499. $this->Form->request->params['_csrfToken'] = 'testKey';
  1500. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1501. $encoding = strtolower(Configure::read('App.encoding'));
  1502. $expected = array(
  1503. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1504. 'div' => array('style' => 'display:none;'),
  1505. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1506. array('input' => array(
  1507. 'type' => 'hidden', 'name' => '_csrfToken',
  1508. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1509. )),
  1510. '/div'
  1511. );
  1512. $this->assertTags($result, $expected);
  1513. $result = $this->Form->input('ValidateUser.ratio');
  1514. $expected = array(
  1515. 'div' => array('class'),
  1516. 'label' => array('for'),
  1517. 'Ratio',
  1518. '/label',
  1519. 'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
  1520. '/div',
  1521. );
  1522. $this->assertTags($result, $expected);
  1523. $result = $this->Form->input('ValidateUser.population');
  1524. $expected = array(
  1525. 'div' => array('class'),
  1526. 'label' => array('for'),
  1527. 'Population',
  1528. '/label',
  1529. 'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
  1530. '/div',
  1531. );
  1532. $this->assertTags($result, $expected);
  1533. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1534. $expected = array(
  1535. 'div' => array('class' => 'input text'),
  1536. 'label' => array('for' => 'UserFormPublished'),
  1537. 'Published',
  1538. '/label',
  1539. array('input' => array(
  1540. 'type' => 'text', 'name' => 'UserForm[published]',
  1541. 'id' => 'UserFormPublished'
  1542. )),
  1543. '/div'
  1544. );
  1545. $this->assertTags($result, $expected);
  1546. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1547. $expected = array(
  1548. 'div' => array('class' => 'input text'),
  1549. 'label' => array('for' => 'UserFormOther'),
  1550. 'Other',
  1551. '/label',
  1552. array('input' => array(
  1553. 'type' => 'text', 'name' => 'UserForm[other]',
  1554. 'id' => 'UserFormOther'
  1555. )),
  1556. '/div'
  1557. );
  1558. $this->assertTags($result, $expected);
  1559. $result = $this->Form->hidden('UserForm.stuff');
  1560. $expected = array(
  1561. 'input' => array(
  1562. 'type' => 'hidden', 'name' => 'UserForm[stuff]',
  1563. 'id' => 'UserFormStuff'
  1564. ));
  1565. $this->assertTags($result, $expected);
  1566. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1567. $expected = array('input' => array(
  1568. 'type' => 'hidden', 'name' => 'UserForm[hidden]',
  1569. 'value' => '0', 'id' => 'UserFormHidden'
  1570. ));
  1571. $this->assertTags($result, $expected);
  1572. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1573. $expected = array(
  1574. 'div' => array('class' => 'input checkbox'),
  1575. array('input' => array(
  1576. 'type' => 'hidden', 'name' => 'UserForm[something]',
  1577. 'value' => '0', 'id' => 'UserFormSomething_'
  1578. )),
  1579. array('input' => array(
  1580. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  1581. 'value' => '1', 'id' => 'UserFormSomething'
  1582. )),
  1583. 'label' => array('for' => 'UserFormSomething'),
  1584. 'Something',
  1585. '/label',
  1586. '/div'
  1587. );
  1588. $this->assertTags($result, $expected);
  1589. $result = $this->Form->fields;
  1590. $expected = array(
  1591. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1592. 'UserForm.hidden' => '0', 'UserForm.something'
  1593. );
  1594. $this->assertEquals($expected, $result);
  1595. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1596. $result = $this->Form->secure($this->Form->fields);
  1597. $expected = array(
  1598. 'div' => array('style' => 'display:none;'),
  1599. array('input' => array(
  1600. 'type' => 'hidden', 'name' => '_Token[fields]',
  1601. 'value' => $hash
  1602. )),
  1603. array('input' => array(
  1604. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1605. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1606. )),
  1607. '/div'
  1608. );
  1609. $this->assertTags($result, $expected);
  1610. }
  1611. /**
  1612. * Test secured inputs with custom names.
  1613. *
  1614. * @return void
  1615. */
  1616. public function testSecuredInputCustomName() {
  1617. $this->Form->request->params['_Token'] = 'testKey';
  1618. $this->assertEquals(array(), $this->Form->fields);
  1619. $this->Form->text('text_input', array(
  1620. 'name' => 'Option[General.default_role]',
  1621. ));
  1622. $expected = array('Option.General.default_role');
  1623. $this->assertEquals($expected, $this->Form->fields);
  1624. $this->Form->select('select_box', [1, 2], [
  1625. 'name' => 'Option[General.select_role]',
  1626. ]);
  1627. $expected = ['Option.General.default_role', 'Option.General.select_role'];
  1628. $this->assertEquals($expected, $this->Form->fields);
  1629. }
  1630. /**
  1631. * Tests that the correct keys are added to the field hash index
  1632. *
  1633. * @return void
  1634. */
  1635. public function testFormSecuredFileInput() {
  1636. $this->assertEquals(array(), $this->Form->fields);
  1637. $this->Form->file('Attachment.file');
  1638. $expected = array(
  1639. 'Attachment.file.name', 'Attachment.file.type',
  1640. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1641. 'Attachment.file.size'
  1642. );
  1643. $this->assertEquals($expected, $this->Form->fields);
  1644. }
  1645. /**
  1646. * test that multiple selects keys are added to field hash
  1647. *
  1648. * @return void
  1649. */
  1650. public function testFormSecuredMultipleSelect() {
  1651. $this->Form->request->params['_csrfToken'] = 'testKey';
  1652. $this->assertEquals(array(), $this->Form->fields);
  1653. $options = array('1' => 'one', '2' => 'two');
  1654. $this->Form->select('Model.select', $options);
  1655. $expected = array('Model.select');
  1656. $this->assertEquals($expected, $this->Form->fields);
  1657. $this->Form->fields = array();
  1658. $this->Form->select('Model.select', $options, array('multiple' => true));
  1659. $this->assertEquals($expected, $this->Form->fields);
  1660. }
  1661. /**
  1662. * testFormSecuredRadio method
  1663. *
  1664. * @return void
  1665. */
  1666. public function testFormSecuredRadio() {
  1667. $this->Form->request->params['_Token'] = 'testKey';
  1668. $this->assertEquals(array(), $this->Form->fields);
  1669. $options = array('1' => 'option1', '2' => 'option2');
  1670. $this->Form->radio('Test.test', $options);
  1671. $expected = array('Test.test');
  1672. $this->assertEquals($expected, $this->Form->fields);
  1673. }
  1674. /**
  1675. * Test that when disabled is in a list based attribute array it works.
  1676. *
  1677. * @return void
  1678. */
  1679. public function testFormSecuredAndDisabledNotAssoc() {
  1680. $this->Form->request->params['_Token'] = 'testKey';
  1681. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1682. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1683. $this->Form->text('Model.text', array('disabled'));
  1684. $this->Form->textarea('Model.textarea', array('disabled'));
  1685. $this->Form->password('Model.password', array('disabled'));
  1686. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1687. $expected = array(
  1688. 'Model.radio' => ''
  1689. );
  1690. $this->assertEquals($expected, $this->Form->fields);
  1691. }
  1692. /**
  1693. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1694. * hashing.
  1695. *
  1696. * @return void
  1697. */
  1698. public function testFormSecuredAndDisabled() {
  1699. $this->Form->request->params['_Token'] = 'testKey';
  1700. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1701. $this->Form->text('Model.text', array('disabled' => true));
  1702. $this->Form->password('Model.text', array('disabled' => true));
  1703. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1704. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1705. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1706. $this->Form->year('Model.year', array('disabled' => true));
  1707. $this->Form->month('Model.month', array('disabled' => true));
  1708. $this->Form->day('Model.day', array('disabled' => true));
  1709. $this->Form->hour('Model.hour', array('disabled' => true));
  1710. $this->Form->minute('Model.minute', array('disabled' => true));
  1711. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1712. $expected = array(
  1713. 'Model.radio' => ''
  1714. );
  1715. $this->assertEquals($expected, $this->Form->fields);
  1716. }
  1717. /**
  1718. * testDisableSecurityUsingForm method
  1719. *
  1720. * @return void
  1721. */
  1722. public function testDisableSecurityUsingForm() {
  1723. $this->Form->request['_Token'] = [
  1724. 'disabledFields' => []
  1725. ];
  1726. $this->Form->create();
  1727. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1728. $this->Form->text('Addresses.title');
  1729. $this->Form->text('Addresses.first_name', ['secure' => false]);
  1730. $this->Form->textarea('Addresses.city', ['secure' => false]);
  1731. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  1732. $result = $this->Form->fields;
  1733. $expected = [
  1734. 'Addresses.id' => '123456', 'Addresses.title',
  1735. ];
  1736. $this->assertEquals($expected, $result);
  1737. }
  1738. /**
  1739. * test disableField
  1740. *
  1741. * @return void
  1742. */
  1743. public function testUnlockFieldAddsToList() {
  1744. $this->Form->request['_Token'] = array(
  1745. 'unlockedFields' => array()
  1746. );
  1747. $this->Form->unlockField('Contact.name');
  1748. $this->Form->text('Contact.name');
  1749. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1750. $this->assertEquals(array(), $this->Form->fields);
  1751. }
  1752. /**
  1753. * test unlockField removing from fields array.
  1754. *
  1755. * @return void
  1756. */
  1757. public function testUnlockFieldRemovingFromFields() {
  1758. $this->Form->request['_Token'] = array(
  1759. 'unlockedFields' => array()
  1760. );
  1761. $this->Form->create($this->article);
  1762. $this->Form->hidden('Article.id', array('value' => 1));
  1763. $this->Form->text('Article.title');
  1764. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  1765. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  1766. $this->Form->unlockField('Article.title');
  1767. $this->Form->unlockField('Article.id');
  1768. $this->assertEquals(array(), $this->Form->fields);
  1769. }
  1770. /**
  1771. * Test validation errors.
  1772. *
  1773. * @return void
  1774. */
  1775. public function testPasswordValidation() {
  1776. $this->markTestIncomplete('Need to revisit once models work again.');
  1777. $Contact->validationErrors['password'] = array('Please provide a password');
  1778. $result = $this->Form->input('Contact.password');
  1779. $expected = array(
  1780. 'div' => array('class' => 'input password error'),
  1781. 'label' => array('for' => 'ContactPassword'),
  1782. 'Password',
  1783. '/label',
  1784. 'input' => array(
  1785. 'type' => 'password', 'name' => 'Contact[password]',
  1786. 'id' => 'ContactPassword', 'class' => 'form-error'
  1787. ),
  1788. array('div' => array('class' => 'error-message')),
  1789. 'Please provide a password',
  1790. '/div',
  1791. '/div'
  1792. );
  1793. $this->assertTags($result, $expected);
  1794. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1795. $expected = array(
  1796. 'div' => array('class' => 'input password error'),
  1797. 'label' => array('for' => 'ContactPassword'),
  1798. 'Password',
  1799. '/label',
  1800. 'input' => array(
  1801. 'type' => 'password', 'name' => 'Contact[password]',
  1802. 'id' => 'ContactPassword', 'class' => 'form-error'
  1803. ),
  1804. '/div'
  1805. );
  1806. $this->assertTags($result, $expected);
  1807. }
  1808. /**
  1809. * Test validation errors, when validation message is an empty string.
  1810. *
  1811. * @return void
  1812. */
  1813. public function testEmptyErrorValidation() {
  1814. $this->markTestIncomplete('Need to revisit once models work again.');
  1815. $this->Form->validationErrors['Contact']['password'] = '';
  1816. $result = $this->Form->input('Contact.password');
  1817. $expected = array(
  1818. 'div' => array('class' => 'input password error'),
  1819. 'label' => array('for' => 'ContactPassword'),
  1820. 'Password',
  1821. '/label',
  1822. 'input' => array(
  1823. 'type' => 'password', 'name' => 'Contact[password]',
  1824. 'id' => 'ContactPassword', 'class' => 'form-error'
  1825. ),
  1826. array('div' => array('class' => 'error-message')),
  1827. array(),
  1828. '/div',
  1829. '/div'
  1830. );
  1831. $this->assertTags($result, $expected);
  1832. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1833. $expected = array(
  1834. 'div' => array('class' => 'input password error'),
  1835. 'label' => array('for' => 'ContactPassword'),
  1836. 'Password',
  1837. '/label',
  1838. 'input' => array(
  1839. 'type' => 'password', 'name' => 'Contact[password]',
  1840. 'id' => 'ContactPassword', 'class' => 'form-error'
  1841. ),
  1842. '/div'
  1843. );
  1844. $this->assertTags($result, $expected);
  1845. }
  1846. /**
  1847. * Test validation errors, when calling input() overriding validation message by an empty string.
  1848. *
  1849. * @return void
  1850. */
  1851. public function testEmptyInputErrorValidation() {
  1852. $this->markTestIncomplete('Need to revisit once models work again.');
  1853. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1854. $result = $this->Form->input('Contact.password', array('error' => ''));
  1855. $expected = array(
  1856. 'div' => array('class' => 'input password error'),
  1857. 'label' => array('for' => 'ContactPassword'),
  1858. 'Password',
  1859. '/label',
  1860. 'input' => array(
  1861. 'type' => 'password', 'name' => 'Contact[password]',
  1862. 'id' => 'ContactPassword', 'class' => 'form-error'
  1863. ),
  1864. array('div' => array('class' => 'error-message')),
  1865. array(),
  1866. '/div',
  1867. '/div'
  1868. );
  1869. $this->assertTags($result, $expected);
  1870. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1871. $expected = array(
  1872. 'div' => array('class' => 'input password error'),
  1873. 'label' => array('for' => 'ContactPassword'),
  1874. 'Password',
  1875. '/label',
  1876. 'input' => array(
  1877. 'type' => 'password', 'name' => 'Contact[password]',
  1878. 'id' => 'ContactPassword', 'class' => 'form-error'
  1879. ),
  1880. '/div'
  1881. );
  1882. $this->assertTags($result, $expected);
  1883. }
  1884. /**
  1885. * testFormValidationAssociated method
  1886. *
  1887. * test display of form errors in conjunction with model::validates.
  1888. *
  1889. * @return void
  1890. */
  1891. public function testFormValidationAssociated() {
  1892. $this->markTestIncomplete('Need to revisit once models work again.');
  1893. $data = array(
  1894. 'UserForm' => array('name' => 'user'),
  1895. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1896. );
  1897. $result = $this->UserForm->OpenidUrl->create($data);
  1898. $this->assertFalse(empty($result));
  1899. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1900. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1901. $encoding = strtolower(Configure::read('App.encoding'));
  1902. $expected = array(
  1903. 'form' => array(
  1904. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1905. 'accept-charset' => $encoding
  1906. ),
  1907. 'div' => array('style' => 'display:none;'),
  1908. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1909. '/div'
  1910. );
  1911. $this->assertTags($result, $expected);
  1912. $result = $this->Form->error(
  1913. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1914. );
  1915. $this->assertEquals('Error, not registered', $result);
  1916. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1917. }
  1918. /**
  1919. * testFormValidationAssociatedFirstLevel method
  1920. *
  1921. * test form error display with associated model.
  1922. *
  1923. * @return void
  1924. */
  1925. public function testFormValidationAssociatedFirstLevel() {
  1926. $this->markTestIncomplete('Need to revisit once models work again.');
  1927. $data = array(
  1928. 'ValidateUser' => array('name' => 'mariano'),
  1929. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1930. );
  1931. $result = $this->ValidateUser->create($data);
  1932. $this->assertFalse(empty($result));
  1933. $this->assertFalse($this->ValidateUser->validates());
  1934. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1935. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1936. $encoding = strtolower(Configure::read('App.encoding'));
  1937. $expected = array(
  1938. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1939. 'div' => array('style' => 'display:none;'),
  1940. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1941. '/div'
  1942. );
  1943. $this->assertTags($result, $expected);
  1944. $result = $this->Form->error(
  1945. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1946. );
  1947. $this->assertEquals('Invalid email', $result);
  1948. $result = $this->Form->error(
  1949. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1950. );
  1951. $this->assertEquals('Invalid name', $result);
  1952. $result = $this->Form->error(
  1953. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1954. );
  1955. $this->assertEquals('Invalid city', $result);
  1956. unset($this->ValidateUser->ValidateProfile);
  1957. unset($this->ValidateUser);
  1958. }
  1959. /**
  1960. * testFormValidationAssociatedSecondLevel method
  1961. *
  1962. * test form error display with associated model.
  1963. *
  1964. * @return void
  1965. */
  1966. public function testFormValidationAssociatedSecondLevel() {
  1967. $this->markTestIncomplete('Need to revisit once models work again.');
  1968. $data = array(
  1969. 'ValidateUser' => array('name' => 'mariano'),
  1970. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1971. 'ValidateItem' => array('name' => 'Item')
  1972. );
  1973. $result = $this->ValidateUser->create($data);
  1974. $this->assertFalse(empty($result));
  1975. $this->assertFalse($this->ValidateUser->validates());
  1976. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1977. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1978. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1979. $encoding = strtolower(Configure::read('App.encoding'));
  1980. $expected = array(
  1981. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1982. 'div' => array('style' => 'display:none;'),
  1983. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1984. '/div'
  1985. );
  1986. $this->assertTags($result, $expected);
  1987. $result = $this->Form->error(
  1988. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1989. );
  1990. $this->assertEquals('Invalid email', $result);
  1991. $result = $this->Form->error(
  1992. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1993. );
  1994. $this->assertEquals('Invalid name', $result);
  1995. $result = $this->Form->error(
  1996. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1997. );
  1998. $result = $this->Form->error(
  1999. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  2000. );
  2001. $this->assertEquals('Invalid description', $result);
  2002. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  2003. unset($this->ValidateUser->ValidateProfile);
  2004. unset($this->ValidateUser);
  2005. }
  2006. /**
  2007. * testFormValidationMultiRecord method
  2008. *
  2009. * test form error display with multiple records.
  2010. *
  2011. * @return void
  2012. */
  2013. public function testFormValidationMultiRecord() {
  2014. $this->markTestIncomplete('Need to revisit once models work again.');
  2015. $Contact->validationErrors[2] = array(
  2016. 'name' => array('The provided value is invalid')
  2017. );
  2018. $result = $this->Form->input('Contact.2.name');
  2019. $expected = array(
  2020. 'div' => array('class' => 'input text error'),
  2021. 'label' => array('for' => 'Contact2Name'),
  2022. 'Name',
  2023. '/label',
  2024. 'input' => array(
  2025. 'type' => 'text', 'name' => 'Contact[2][name]', 'id' => 'Contact2Name',
  2026. 'class' => 'form-error', 'maxlength' => 255
  2027. ),
  2028. array('div' => array('class' => 'error-message')),
  2029. 'The provided value is invalid',
  2030. '/div',
  2031. '/div'
  2032. );
  2033. $this->assertTags($result, $expected);
  2034. }
  2035. /**
  2036. * testMultipleInputValidation method
  2037. *
  2038. * test multiple record form validation error display.
  2039. *
  2040. * @return void
  2041. */
  2042. public function testMultipleInputValidation() {
  2043. $this->markTestIncomplete('Need to revisit once models work again.');
  2044. $Address->validationErrors[0] = array(
  2045. 'title' => array('This field cannot be empty'),
  2046. 'first_name' => array('This field cannot be empty')
  2047. );
  2048. $Address->validationErrors[1] = array(
  2049. 'last_name' => array('You must have a last name')
  2050. );
  2051. $this->Form->create();
  2052. $result = $this->Form->input('Address.0.title');
  2053. $expected = array(
  2054. 'div' => array('class'),
  2055. 'label' => array('for'),
  2056. 'preg:/[^<]+/',
  2057. '/label',
  2058. 'input' => array(
  2059. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  2060. ),
  2061. array('div' => array('class' => 'error-message')),
  2062. 'This field cannot be empty',
  2063. '/div',
  2064. '/div'
  2065. );
  2066. $this->assertTags($result, $expected);
  2067. $result = $this->Form->input('Address.0.first_name');
  2068. $expected = array(
  2069. 'div' => array('class'),
  2070. 'label' => array('for'),
  2071. 'preg:/[^<]+/',
  2072. '/label',
  2073. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  2074. array('div' => array('class' => 'error-message')),
  2075. 'This field cannot be empty',
  2076. '/div',
  2077. '/div'
  2078. );
  2079. $this->assertTags($result, $expected);
  2080. $result = $this->Form->input('Address.0.last_name');
  2081. $expected = array(
  2082. 'div' => array('class'),
  2083. 'label' => array('for'),
  2084. 'preg:/[^<]+/',
  2085. '/label',
  2086. 'input' => array('type' => 'text', 'name', 'id'),
  2087. '/div'
  2088. );
  2089. $this->assertTags($result, $expected);
  2090. $result = $this->Form->input('Address.1.last_name');
  2091. $expected = array(
  2092. 'div' => array('class'),
  2093. 'label' => array('for'),
  2094. 'preg:/[^<]+/',
  2095. '/label',
  2096. 'input' => array(
  2097. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  2098. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  2099. ),
  2100. array('div' => array('class' => 'error-message')),
  2101. 'You must have a last name',
  2102. '/div',
  2103. '/div'
  2104. );
  2105. $this->assertTags($result, $expected);
  2106. }
  2107. /**
  2108. * testInput method
  2109. *
  2110. * Test various incarnations of input().
  2111. *
  2112. * @return void
  2113. */
  2114. public function testInput() {
  2115. $this->markTestIncomplete('Need to revisit once models work again.');
  2116. $result = $this->Form->input('ValidateUser.balance');
  2117. $expected = array(
  2118. 'div' => array('class'),
  2119. 'label' => array('for'),
  2120. 'Balance',
  2121. '/label',
  2122. 'input' => array('name', 'type' => 'number', 'id'),
  2123. '/div',
  2124. );
  2125. $this->assertTags($result, $expected);
  2126. $result = $this->Form->input('ValidateUser.cost_decimal');
  2127. $expected = array(
  2128. 'div' => array('class'),
  2129. 'label' => array('for'),
  2130. 'Cost Decimal',
  2131. '/label',
  2132. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  2133. '/div',
  2134. );
  2135. $this->assertTags($result, $expected);
  2136. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  2137. $expected = array(
  2138. 'div' => array('class' => 'input email'),
  2139. 'label' => array('for' => 'custom'),
  2140. 'Email',
  2141. '/label',
  2142. array('input' => array(
  2143. 'type' => 'email', 'name' => 'Contact[email]',
  2144. 'id' => 'custom', 'maxlength' => 255
  2145. )),
  2146. '/div'
  2147. );
  2148. $this->assertTags($result, $expected);
  2149. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  2150. $expected = array(
  2151. '<div',
  2152. 'label' => array('for' => 'ContactEmail'),
  2153. 'Email',
  2154. '/label',
  2155. array('input' => array(
  2156. 'type' => 'email', 'name' => 'Contact[email]',
  2157. 'id' => 'ContactEmail', 'maxlength' => 255
  2158. )),
  2159. '/div'
  2160. );
  2161. $this->assertTags($result, $expected);
  2162. $result = $this->Form->hidden('Contact.idontexist');
  2163. $expected = array('input' => array(
  2164. 'type' => 'hidden', 'name' => 'Contact[idontexist]',
  2165. 'id' => 'ContactIdontexist'
  2166. ));
  2167. $this->assertTags($result, $expected);
  2168. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  2169. $expected = array(
  2170. 'div' => array('class' => 'input text'),
  2171. 'label' => array('for' => 'ContactEmail'),
  2172. 'Email',
  2173. '/label',
  2174. array('input' => array(
  2175. 'type' => 'text', 'name' => 'Contact[email]',
  2176. 'id' => 'ContactEmail'
  2177. )),
  2178. '/div'
  2179. );
  2180. $this->assertTags($result, $expected);
  2181. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  2182. $expected = array(
  2183. 'div' => array('class' => 'input text'),
  2184. 'label' => array('for' => 'Contact5Email'),
  2185. 'Email',
  2186. '/label',
  2187. array('input' => array(
  2188. 'type' => 'text', 'name' => 'Contact[5][email]',
  2189. 'id' => 'Contact5Email'
  2190. )),
  2191. '/div'
  2192. );
  2193. $this->assertTags($result, $expected);
  2194. $result = $this->Form->input('Contact.password');
  2195. $expected = array(
  2196. 'div' => array('class' => 'input password'),
  2197. 'label' => array('for' => 'ContactPassword'),
  2198. 'Password',
  2199. '/label',
  2200. array('input' => array(
  2201. 'type' => 'password', 'name' => 'Contact[password]',
  2202. 'id' => 'ContactPassword'
  2203. )),
  2204. '/div'
  2205. );
  2206. $this->assertTags($result, $expected);
  2207. $result = $this->Form->input('Contact.email', array(
  2208. 'type' => 'file', 'class' => 'textbox'
  2209. ));
  2210. $expected = array(
  2211. 'div' => array('class' => 'input file'),
  2212. 'label' => array('for' => 'ContactEmail'),
  2213. 'Email',
  2214. '/label',
  2215. array('input' => array(
  2216. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2217. 'id' => 'ContactEmail'
  2218. )),
  2219. '/div'
  2220. );
  2221. $this->assertTags($result, $expected);
  2222. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  2223. $result = $this->Form->input('Contact.phone');
  2224. $expected = array(
  2225. 'div' => array('class' => 'input tel'),
  2226. 'label' => array('for' => 'ContactPhone'),
  2227. 'Phone',
  2228. '/label',
  2229. array('input' => array(
  2230. 'type' => 'tel', 'name' => 'Contact[phone]',
  2231. 'value' => 'Hello &amp; World &gt; weird chars',
  2232. 'id' => 'ContactPhone', 'maxlength' => 255
  2233. )),
  2234. '/div'
  2235. );
  2236. $this->assertTags($result, $expected);
  2237. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2238. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  2239. $expected = array(
  2240. 'div' => array('class' => 'input text'),
  2241. 'label' => array('for' => 'myId'),
  2242. 'Field',
  2243. '/label',
  2244. 'input' => array(
  2245. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2246. 'value' => 'My value', 'id' => 'myId'
  2247. ),
  2248. '/div'
  2249. );
  2250. $this->assertTags($result, $expected);
  2251. unset($this->Form->request->data);
  2252. $Contact->validationErrors['field'] = array('Badness!');
  2253. $result = $this->Form->input('Contact.field');
  2254. $expected = array(
  2255. 'div' => array('class' => 'input text error'),
  2256. 'label' => array('for' => 'ContactField'),
  2257. 'Field',
  2258. '/label',
  2259. 'input' => array(
  2260. 'type' => 'text', 'name' => 'Contact[field]',
  2261. 'id' => 'ContactField', 'class' => 'form-error'
  2262. ),
  2263. array('div' => array('class' => 'error-message')),
  2264. 'Badness!',
  2265. '/div',
  2266. '/div'
  2267. );
  2268. $this->assertTags($result, $expected);
  2269. $result = $this->Form->input('Contact.field', array(
  2270. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  2271. ));
  2272. $expected = array(
  2273. 'label' => array('for' => 'ContactField'),
  2274. 'Field',
  2275. '/label',
  2276. 'input' => array(
  2277. 'type' => 'text', 'name' => 'Contact[field]',
  2278. 'id' => 'ContactField', 'class' => 'form-error'
  2279. ),
  2280. array('span' => array('class' => 'error-message')),
  2281. 'Badness!',
  2282. '/span'
  2283. );
  2284. $this->assertTags($result, $expected);
  2285. $result = $this->Form->input('Contact.field', array(
  2286. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  2287. ));
  2288. $expected = array(
  2289. 'div' => array('class' => 'input text error'),
  2290. 'label' => array('for' => 'ContactField'),
  2291. 'Field',
  2292. '/label',
  2293. 'input' => array(
  2294. 'type' => 'text', 'name' => 'Contact[field]',
  2295. 'id' => 'ContactField', 'class' => 'form-error'
  2296. ),
  2297. array('div' => array('class' => 'error')),
  2298. 'Badness!',
  2299. '/div'
  2300. );
  2301. $this->assertTags($result, $expected);
  2302. $result = $this->Form->input('Contact.field', array(
  2303. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  2304. ));
  2305. $expected = array(
  2306. 'span' => 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. 'Badness!',
  2315. '/span'
  2316. );
  2317. $this->assertTags($result, $expected);
  2318. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  2319. $expected = array(
  2320. 'div' => array('class' => 'input text error'),
  2321. 'label' => array('for' => 'ContactField'),
  2322. 'Field',
  2323. '/label',
  2324. 'input' => array(
  2325. 'type' => 'text', 'name' => 'Contact[field]',
  2326. 'id' => 'ContactField', 'class' => 'form-error'
  2327. ),
  2328. 'A message to you, Rudy',
  2329. array('div' => array('class' => 'error-message')),
  2330. 'Badness!',
  2331. '/div',
  2332. '/div'
  2333. );
  2334. $this->assertTags($result, $expected);
  2335. $this->Form->setEntity(null);
  2336. $this->Form->setEntity('Contact.field');
  2337. $result = $this->Form->input('Contact.field', array(
  2338. 'after' => 'A message to you, Rudy', 'error' => false
  2339. ));
  2340. $expected = array(
  2341. 'div' => array('class' => 'input text'),
  2342. 'label' => array('for' => 'ContactField'),
  2343. 'Field',
  2344. '/label',
  2345. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  2346. 'A message to you, Rudy',
  2347. '/div'
  2348. );
  2349. $this->assertTags($result, $expected);
  2350. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  2351. $expected = array(
  2352. 'div' => array('class' => 'input text'),
  2353. 'label' => array('for' => 'ObjectField'),
  2354. 'Field',
  2355. '/label',
  2356. 'input' => array('type' => 'text', 'name' => 'Object[field]', 'id' => 'ObjectField'),
  2357. 'A message to you, Rudy',
  2358. '/div'
  2359. );
  2360. $this->assertTags($result, $expected);
  2361. $Contact->validationErrors['field'] = array('minLength');
  2362. $result = $this->Form->input('Contact.field', array(
  2363. 'error' => array(
  2364. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2365. 'maxLength' => 'login too large'
  2366. )
  2367. ));
  2368. $expected = array(
  2369. 'div' => array('class' => 'input text error'),
  2370. 'label' => array('for' => 'ContactField'),
  2371. 'Field',
  2372. '/label',
  2373. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  2374. array('div' => array('class' => 'error-message')),
  2375. 'Le login doit contenir au moins 2 caractères',
  2376. '/div',
  2377. '/div'
  2378. );
  2379. $this->assertTags($result, $expected);
  2380. $Contact->validationErrors['field'] = array('maxLength');
  2381. $result = $this->Form->input('Contact.field', array(
  2382. 'error' => array(
  2383. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  2384. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2385. 'maxLength' => 'login too large',
  2386. )
  2387. ));
  2388. $expected = array(
  2389. 'div' => array('class' => 'input text error'),
  2390. 'label' => array('for' => 'ContactField'),
  2391. 'Field',
  2392. '/label',
  2393. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  2394. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  2395. 'login too large',
  2396. '/span',
  2397. '/div'
  2398. );
  2399. $this->assertTags($result, $expected);
  2400. }
  2401. /**
  2402. * Test that inputs with 0 can be created.
  2403. *
  2404. * @return void
  2405. */
  2406. public function testInputZero() {
  2407. $this->markTestIncomplete('Need to revisit once models work again.');
  2408. $this->Form->create('User');
  2409. $result = $this->Form->input('0');
  2410. $expected = array(
  2411. 'div' => array('class' => 'input text'),
  2412. 'label' => array('for' => 'User0'), '/label',
  2413. 'input' => array('type' => 'text', 'name' => 'User[0]', 'id' => 'User0'),
  2414. '/div'
  2415. );
  2416. $this->assertTags($result, $expected);
  2417. }
  2418. /**
  2419. * test input() with checkbox creation
  2420. *
  2421. * @return void
  2422. */
  2423. public function testInputCheckbox() {
  2424. $this->markTestIncomplete('Need to revisit once models work again.');
  2425. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  2426. $expected = array(
  2427. 'div' => array('class' => 'input checkbox'),
  2428. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  2429. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2430. '/div'
  2431. );
  2432. $this->assertTags($result, $expected);
  2433. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2434. $expected = array(
  2435. 'div' => array('class' => 'input checkbox'),
  2436. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  2437. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2438. '/div'
  2439. );
  2440. $this->assertTags($result, $expected);
  2441. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2442. $expected = array(
  2443. 'div' => array('class' => 'input checkbox'),
  2444. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  2445. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2446. '/div'
  2447. );
  2448. $this->assertTags($result, $expected);
  2449. $result = $this->Form->input('User.disabled', array(
  2450. 'label' => 'Disabled',
  2451. 'type' => 'checkbox',
  2452. 'data-foo' => 'disabled'
  2453. ));
  2454. $expected = array(
  2455. 'div' => array('class' => 'input checkbox'),
  2456. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  2457. array('input' => array(
  2458. 'type' => 'checkbox',
  2459. 'name' => 'data[User][disabled]',
  2460. 'value' => '1',
  2461. 'id' => 'UserDisabled',
  2462. 'data-foo' => 'disabled'
  2463. )),
  2464. 'label' => array('for' => 'UserDisabled'),
  2465. 'Disabled',
  2466. '/label',
  2467. '/div'
  2468. );
  2469. $this->assertTags($result, $expected);
  2470. }
  2471. /**
  2472. * test form->input() with time types.
  2473. *
  2474. */
  2475. public function testInputTime() {
  2476. $this->markTestIncomplete('Need to revisit once models work again.');
  2477. extract($this->dateRegex);
  2478. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2479. $result = explode(':', $result);
  2480. $this->assertRegExp('/option value="23"/', $result[0]);
  2481. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2482. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2483. $result = explode(':', $result);
  2484. $this->assertRegExp('/option value="23"/', $result[0]);
  2485. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2486. $result = $this->Form->input('Model.field', array(
  2487. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2488. ));
  2489. $result = explode(':', $result);
  2490. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2491. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2492. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2493. $result = $this->Form->input('Model.field', array(
  2494. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2495. ));
  2496. $result = explode(':', $result);
  2497. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2498. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2499. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2500. $result = $this->Form->input('prueba', array(
  2501. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2502. 'maxYear' => date('Y') + 1, 'interval' => 15
  2503. ));
  2504. $result = explode(':', $result);
  2505. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2506. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2507. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2508. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2509. $result = $this->Form->input('Random.start_time', array(
  2510. 'type' => 'time',
  2511. 'selected' => '18:15'
  2512. ));
  2513. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2514. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2515. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2516. $result = $this->Form->input('published', array('type' => 'time'));
  2517. $now = strtotime('now');
  2518. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2519. $now = strtotime('2013-03-09 00:42:21');
  2520. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2521. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2522. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2523. }
  2524. /**
  2525. * Test interval + selected near the hour roll over.
  2526. *
  2527. * @return void
  2528. */
  2529. public function testTimeSelectedWithInterval() {
  2530. $this->markTestIncomplete('Need to revisit once models work again.');
  2531. $result = $this->Form->input('Model.start_time', array(
  2532. 'type' => 'time',
  2533. 'interval' => 15,
  2534. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2535. ));
  2536. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2537. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2538. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2539. $result = $this->Form->input('Model.start_time', array(
  2540. 'type' => 'time',
  2541. 'interval' => 15,
  2542. 'selected' => '2012-10-23 15:57:00'
  2543. ));
  2544. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2545. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2546. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2547. $result = $this->Form->input('Model.start_time', array(
  2548. 'timeFormat' => 24,
  2549. 'type' => 'time',
  2550. 'interval' => 15,
  2551. 'selected' => '15:57'
  2552. ));
  2553. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2554. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2555. $result = $this->Form->input('Model.start_time', array(
  2556. 'timeFormat' => 24,
  2557. 'type' => 'time',
  2558. 'interval' => 15,
  2559. 'selected' => '23:57'
  2560. ));
  2561. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2562. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2563. $result = $this->Form->input('Model.created', array(
  2564. 'timeFormat' => 24,
  2565. 'type' => 'datetime',
  2566. 'interval' => 15,
  2567. 'selected' => '2012-09-30 23:56'
  2568. ));
  2569. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2570. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2571. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2572. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2573. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2574. }
  2575. /**
  2576. * Test time with selected values around 12:xx:xx
  2577. *
  2578. * @return void
  2579. */
  2580. public function testTimeSelectedWithIntervalTwelve() {
  2581. $this->markTestIncomplete('Need to revisit once models work again.');
  2582. $result = $this->Form->input('Model.start_time', array(
  2583. 'type' => 'time',
  2584. 'timeFormat' => 12,
  2585. 'interval' => 15,
  2586. 'selected' => '00:00:00'
  2587. ));
  2588. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2589. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2590. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2591. $result = $this->Form->input('Model.start_time', array(
  2592. 'type' => 'time',
  2593. 'timeFormat' => 12,
  2594. 'interval' => 15,
  2595. 'selected' => '12:00:00'
  2596. ));
  2597. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2598. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2599. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2600. $result = $this->Form->input('Model.start_time', array(
  2601. 'type' => 'time',
  2602. 'timeFormat' => 12,
  2603. 'interval' => 15,
  2604. 'selected' => '12:15:00'
  2605. ));
  2606. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2607. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2608. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2609. }
  2610. /**
  2611. * Test interval & timeFormat = 12
  2612. *
  2613. * @return void
  2614. */
  2615. public function testInputTimeWithIntervalAnd12HourFormat() {
  2616. $this->markTestIncomplete('Need to revisit once models work again.');
  2617. $result = $this->Form->input('Model.start_time', array(
  2618. 'type' => 'time',
  2619. 'timeFormat' => 12,
  2620. 'interval' => 5,
  2621. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2622. ));
  2623. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2624. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2625. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2626. $result = $this->Form->input('Model.start_time', array(
  2627. 'type' => 'time',
  2628. 'timeFormat' => '12',
  2629. 'interval' => 5,
  2630. 'selected' => '2013-04-19 16:30:00'
  2631. ));
  2632. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2633. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2634. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2635. $result = $this->Form->input('Model.start_time', array(
  2636. 'type' => 'time',
  2637. 'timeFormat' => '12',
  2638. 'interval' => 10,
  2639. 'selected' => '2013-05-19 00:33:00'
  2640. ));
  2641. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2642. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2643. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2644. $result = $this->Form->input('Model.start_time', array(
  2645. 'type' => 'time',
  2646. 'timeFormat' => '12',
  2647. 'interval' => 10,
  2648. 'selected' => '2013-05-19 13:33:00'
  2649. ));
  2650. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2651. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2652. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2653. $result = $this->Form->input('Model.start_time', array(
  2654. 'type' => 'time',
  2655. 'timeFormat' => '12',
  2656. 'interval' => 10,
  2657. 'selected' => '2013-05-19 01:33:00'
  2658. ));
  2659. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2660. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2661. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2662. }
  2663. /**
  2664. * test form->input() with datetime, date and time types
  2665. *
  2666. * @return void
  2667. */
  2668. public function testInputDatetime() {
  2669. $this->markTestIncomplete('Need to revisit once models work again.');
  2670. extract($this->dateRegex);
  2671. $result = $this->Form->input('prueba', array(
  2672. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2673. 'maxYear' => date('Y') + 1, 'interval' => 15
  2674. ));
  2675. $result = explode(':', $result);
  2676. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2677. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2678. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2679. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2680. //related to ticket #5013
  2681. $result = $this->Form->input('Contact.date', array(
  2682. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2683. ));
  2684. $this->assertRegExp('/class="customClass"/', $result);
  2685. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2686. $result = $this->Form->input('Contact.date', array(
  2687. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2688. ));
  2689. $this->assertRegExp('/id="customIdDay"/', $result);
  2690. $this->assertRegExp('/id="customIdMonth"/', $result);
  2691. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2692. $result = $this->Form->input('Model.field', array(
  2693. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2694. ));
  2695. $this->assertRegExp('/id="customIDDay"/', $result);
  2696. $this->assertRegExp('/id="customIDHour"/', $result);
  2697. $result = explode('</select><select', $result);
  2698. $result = explode(':', $result[1]);
  2699. $this->assertRegExp('/option value="23"/', $result[0]);
  2700. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2701. $result = $this->Form->input('Model.field', array(
  2702. 'type' => 'datetime', 'timeFormat' => 12
  2703. ));
  2704. $result = explode('</select><select', $result);
  2705. $result = explode(':', $result[1]);
  2706. $this->assertRegExp('/option value="12"/', $result[0]);
  2707. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2708. $this->Form->request->data = array('Contact' => array('created' => null));
  2709. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2710. $expected = array(
  2711. 'div' => array('class' => 'input date'),
  2712. 'label' => array('for' => 'ContactCreatedMonth'),
  2713. 'Created',
  2714. '/label',
  2715. array('select' => array('name' => 'Contact[created][month]', 'id' => 'ContactCreatedMonth')),
  2716. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2717. $monthsRegex,
  2718. '/select', '-',
  2719. array('select' => array('name' => 'Contact[created][day]', 'id' => 'ContactCreatedDay')),
  2720. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2721. $daysRegex,
  2722. '/select', '-',
  2723. array('select' => array('name' => 'Contact[created][year]', 'id' => 'ContactCreatedYear')),
  2724. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2725. $yearsRegex,
  2726. '/select',
  2727. '/div'
  2728. );
  2729. $this->assertTags($result, $expected);
  2730. $this->Form->request->data = array('Contact' => array('created' => null));
  2731. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2732. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2733. $this->Form->request->data = array('Contact' => array('created' => null));
  2734. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2735. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2736. $result = $this->Form->input('Contact.created', array(
  2737. 'type' => 'date',
  2738. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2739. 'timeFormat' => 'NONE'
  2740. ));
  2741. $this->assertRegExp('/for\="created-month"/', $result);
  2742. }
  2743. /**
  2744. * Test generating checkboxes with disabled elements.
  2745. *
  2746. * @return void
  2747. */
  2748. public function testInputCheckboxWithDisabledElements() {
  2749. $this->markTestIncomplete('Need to revisit once models work again.');
  2750. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2751. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2752. $expected = array(
  2753. array('div' => array('class' => 'input select')),
  2754. array('label' => array('for' => "ContactMultiple")),
  2755. 'Multiple',
  2756. '/label',
  2757. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2758. array('div' => array('class' => 'checkbox')),
  2759. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2760. array('label' => array('for' => "ContactMultiple1")),
  2761. 'One',
  2762. '/label',
  2763. '/div',
  2764. array('div' => array('class' => 'checkbox')),
  2765. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2766. array('label' => array('for' => "ContactMultiple2")),
  2767. 'Two',
  2768. '/label',
  2769. '/div',
  2770. array('div' => array('class' => 'checkbox')),
  2771. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2772. array('label' => array('for' => "ContactMultiple3")),
  2773. 'Three',
  2774. '/label',
  2775. '/div',
  2776. '/div'
  2777. );
  2778. $this->assertTags($result, $expected);
  2779. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2780. $expected = array(
  2781. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  2782. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2783. 'label' => array('for' => 'ModelField12'),
  2784. 'half',
  2785. '/label'
  2786. );
  2787. $this->assertTags($result, $expected);
  2788. $disabled = array('2', 3);
  2789. $expected = array(
  2790. array('div' => array('class' => 'input select')),
  2791. array('label' => array('for' => "ContactMultiple")),
  2792. 'Multiple',
  2793. '/label',
  2794. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2795. array('div' => array('class' => 'checkbox')),
  2796. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2797. array('label' => array('for' => "ContactMultiple1")),
  2798. 'One',
  2799. '/label',
  2800. '/div',
  2801. array('div' => array('class' => 'checkbox')),
  2802. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2803. array('label' => array('for' => "ContactMultiple2")),
  2804. 'Two',
  2805. '/label',
  2806. '/div',
  2807. array('div' => array('class' => 'checkbox')),
  2808. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2809. array('label' => array('for' => "ContactMultiple3")),
  2810. 'Three',
  2811. '/label',
  2812. '/div',
  2813. '/div'
  2814. );
  2815. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2816. $this->assertTags($result, $expected);
  2817. // make sure 50 does only disable 50, and not 50f5c0cf
  2818. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2819. $disabled = array(50);
  2820. $expected = array(
  2821. array('div' => array('class' => 'input select')),
  2822. array('label' => array('for' => "ContactMultiple")),
  2823. 'Multiple',
  2824. '/label',
  2825. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2826. array('div' => array('class' => 'checkbox')),
  2827. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2828. array('label' => array('for' => "ContactMultiple50")),
  2829. 'Fifty',
  2830. '/label',
  2831. '/div',
  2832. array('div' => array('class' => 'checkbox')),
  2833. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2834. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2835. 'Stringy',
  2836. '/label',
  2837. '/div',
  2838. '/div'
  2839. );
  2840. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2841. $this->assertTags($result, $expected);
  2842. }
  2843. /**
  2844. * test input name with leading integer, ensure attributes are generated correctly.
  2845. *
  2846. * @return void
  2847. */
  2848. public function testInputWithLeadingInteger() {
  2849. $result = $this->Form->text('0.Node.title');
  2850. $expected = array(
  2851. 'input' => array('name' => '0[Node][title]', 'type' => 'text')
  2852. );
  2853. $this->assertTags($result, $expected);
  2854. }
  2855. /**
  2856. * test form->input() with select type inputs.
  2857. *
  2858. * @return void
  2859. */
  2860. public function testInputSelectType() {
  2861. $this->markTestIncomplete('Need to revisit once models work again.');
  2862. $result = $this->Form->input('email', array(
  2863. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2864. );
  2865. $expected = array(
  2866. 'div' => array('class' => 'input select'),
  2867. 'label' => array('for' => 'email'),
  2868. 'Email',
  2869. '/label',
  2870. array('select' => array('name' => 'email', 'id' => 'email')),
  2871. array('option' => array('value' => '')),
  2872. '/option',
  2873. array('option' => array('value' => 'è')),
  2874. 'Firést',
  2875. '/option',
  2876. array('option' => array('value' => 'é')),
  2877. 'Secoènd',
  2878. '/option',
  2879. '/select',
  2880. '/div'
  2881. );
  2882. $this->assertTags($result, $expected);
  2883. $result = $this->Form->input('email', array(
  2884. 'options' => array('First', 'Second'), 'empty' => true)
  2885. );
  2886. $expected = array(
  2887. 'div' => array('class' => 'input select'),
  2888. 'label' => array('for' => 'email'),
  2889. 'Email',
  2890. '/label',
  2891. array('select' => array('name' => 'email', 'id' => 'email')),
  2892. array('option' => array('value' => '')),
  2893. '/option',
  2894. array('option' => array('value' => '0')),
  2895. 'First',
  2896. '/option',
  2897. array('option' => array('value' => '1')),
  2898. 'Second',
  2899. '/option',
  2900. '/select',
  2901. '/div'
  2902. );
  2903. $this->assertTags($result, $expected);
  2904. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2905. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2906. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2907. $expected = array(
  2908. 'div' => array('class' => 'input select'),
  2909. 'label' => array('for' => 'ModelUserId'),
  2910. 'User',
  2911. '/label',
  2912. 'select' => array('name' => 'Model[user_id]', 'id' => 'ModelUserId'),
  2913. array('option' => array('value' => '')),
  2914. '/option',
  2915. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2916. 'good',
  2917. '/option',
  2918. array('option' => array('value' => 'other')),
  2919. 'bad',
  2920. '/option',
  2921. '/select',
  2922. '/div'
  2923. );
  2924. $this->assertTags($result, $expected);
  2925. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2926. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2927. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2928. $expected = array(
  2929. 'div' => array('class' => 'input select'),
  2930. 'label' => array('for' => 'ThingUserId'),
  2931. 'User',
  2932. '/label',
  2933. 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'),
  2934. array('option' => array('value' => '')),
  2935. 'Some Empty',
  2936. '/option',
  2937. array('option' => array('value' => 'value')),
  2938. 'good',
  2939. '/option',
  2940. array('option' => array('value' => 'other')),
  2941. 'bad',
  2942. '/option',
  2943. '/select',
  2944. '/div'
  2945. );
  2946. $this->assertTags($result, $expected);
  2947. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2948. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2949. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2950. $expected = array(
  2951. 'div' => array('class' => 'input select'),
  2952. 'label' => array('for' => 'ThingUserId'),
  2953. 'User',
  2954. '/label',
  2955. 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'),
  2956. array('option' => array('value' => '')),
  2957. 'Some Empty',
  2958. '/option',
  2959. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2960. 'good',
  2961. '/option',
  2962. array('option' => array('value' => 'other')),
  2963. 'bad',
  2964. '/option',
  2965. '/select',
  2966. '/div'
  2967. );
  2968. $this->assertTags($result, $expected);
  2969. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2970. $this->Form->request->data = array('User' => array('User' => array('value')));
  2971. $result = $this->Form->input('User.User', array('empty' => true));
  2972. $expected = array(
  2973. 'div' => array('class' => 'input select'),
  2974. 'label' => array('for' => 'UserUser'),
  2975. 'User',
  2976. '/label',
  2977. 'input' => array('type' => 'hidden', 'name' => 'User[User]', 'value' => '', 'id' => 'UserUser_'),
  2978. 'select' => array('name' => 'User[User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2979. array('option' => array('value' => '')),
  2980. '/option',
  2981. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2982. 'good',
  2983. '/option',
  2984. array('option' => array('value' => 'other')),
  2985. 'bad',
  2986. '/option',
  2987. '/select',
  2988. '/div'
  2989. );
  2990. $this->assertTags($result, $expected);
  2991. $this->Form->data = array();
  2992. $result = $this->Form->input('Publisher.id', array(
  2993. 'label' => 'Publisher',
  2994. 'type' => 'select',
  2995. 'multiple' => 'checkbox',
  2996. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2997. ));
  2998. $expected = array(
  2999. array('div' => array('class' => 'input select')),
  3000. array('label' => array('for' => 'PublisherId')),
  3001. 'Publisher',
  3002. '/label',
  3003. 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'PublisherId'),
  3004. array('div' => array('class' => 'checkbox')),
  3005. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  3006. array('label' => array('for' => 'PublisherIdValue1')),
  3007. 'Label 1',
  3008. '/label',
  3009. '/div',
  3010. array('div' => array('class' => 'checkbox')),
  3011. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  3012. array('label' => array('for' => 'PublisherIdValue2')),
  3013. 'Label 2',
  3014. '/label',
  3015. '/div',
  3016. '/div'
  3017. );
  3018. $this->assertTags($result, $expected);
  3019. }
  3020. /**
  3021. * test that input() and a non standard primary key makes a hidden input by default.
  3022. *
  3023. * @return void
  3024. */
  3025. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  3026. $this->markTestIncomplete('Need to revisit once models work again.');
  3027. $this->Form->create('User');
  3028. $this->Form->fieldset = array(
  3029. 'User' => array(
  3030. 'fields' => array(
  3031. 'model_id' => array('type' => 'integer')
  3032. ),
  3033. 'validates' => array(),
  3034. 'key' => 'model_id'
  3035. )
  3036. );
  3037. $result = $this->Form->input('model_id');
  3038. $expected = array(
  3039. 'input' => array('type' => 'hidden', 'name' => 'User[model_id]', 'id' => 'UserModelId'),
  3040. );
  3041. $this->assertTags($result, $expected);
  3042. }
  3043. /**
  3044. * test that overriding the magic select type widget is possible
  3045. *
  3046. * @return void
  3047. */
  3048. public function testInputOverridingMagicSelectType() {
  3049. $this->markTestIncomplete('Need to revisit once models work again.');
  3050. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3051. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  3052. $expected = array(
  3053. 'div' => array('class' => 'input text'),
  3054. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  3055. 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  3056. '/div'
  3057. );
  3058. $this->assertTags($result, $expected);
  3059. //Check that magic types still work for plural/singular vars
  3060. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  3061. $result = $this->Form->input('Model.type');
  3062. $expected = array(
  3063. 'div' => array('class' => 'input select'),
  3064. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  3065. 'select' => array('name' => 'Model[type]', 'id' => 'ModelType'),
  3066. array('option' => array('value' => 'value')), 'good', '/option',
  3067. array('option' => array('value' => 'other')), 'bad', '/option',
  3068. '/select',
  3069. '/div'
  3070. );
  3071. $this->assertTags($result, $expected);
  3072. }
  3073. /**
  3074. * Test that inferred types do not override developer input
  3075. *
  3076. * @return void
  3077. */
  3078. public function testInputMagicTypeDoesNotOverride() {
  3079. $this->markTestIncomplete('Need to revisit once models work again.');
  3080. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3081. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  3082. $expected = array(
  3083. 'div' => array('class' => 'input checkbox'),
  3084. array('input' => array(
  3085. 'type' => 'hidden',
  3086. 'name' => 'data[Model][user]',
  3087. 'id' => 'ModelUser_',
  3088. 'value' => 0,
  3089. )),
  3090. array('input' => array(
  3091. 'name' => 'data[Model][user]',
  3092. 'type' => 'checkbox',
  3093. 'id' => 'ModelUser',
  3094. 'value' => 1
  3095. )),
  3096. 'label' => array('for' => 'ModelUser'), 'User', '/label',
  3097. '/div'
  3098. );
  3099. $this->assertTags($result, $expected);
  3100. }
  3101. /**
  3102. * Test that magic input() selects are created for type=number
  3103. *
  3104. * @return void
  3105. */
  3106. public function testInputMagicSelectForTypeNumber() {
  3107. $this->markTestIncomplete('Need to revisit once models work again.');
  3108. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  3109. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  3110. $result = $this->Form->input('ValidateUser.balance');
  3111. $expected = array(
  3112. 'div' => array('class' => 'input select'),
  3113. 'label' => array('for' => 'ValidateUserBalance'),
  3114. 'Balance',
  3115. '/label',
  3116. 'select' => array('name' => 'ValidateUser[balance]', 'id' => 'ValidateUserBalance'),
  3117. array('option' => array('value' => '0')),
  3118. 'nothing',
  3119. '/option',
  3120. array('option' => array('value' => '1', 'selected' => 'selected')),
  3121. 'some',
  3122. '/option',
  3123. array('option' => array('value' => '100')),
  3124. 'a lot',
  3125. '/option',
  3126. '/select',
  3127. '/div'
  3128. );
  3129. $this->assertTags($result, $expected);
  3130. }
  3131. /**
  3132. * Test that magic input() selects can easily be converted into radio types without error.
  3133. *
  3134. * @return void
  3135. */
  3136. public function testInputMagicSelectChangeToRadio() {
  3137. $this->markTestIncomplete('Need to revisit once models work again.');
  3138. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3139. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  3140. $this->assertRegExp('/input type="radio"/', $result);
  3141. }
  3142. /**
  3143. * fields with the same name as the model should work.
  3144. *
  3145. * @return void
  3146. */
  3147. public function testInputWithMatchingFieldAndModelName() {
  3148. $this->markTestIncomplete('Need to revisit once models work again.');
  3149. $this->Form->create('User');
  3150. $this->Form->fieldset = array(
  3151. 'User' => array(
  3152. 'fields' => array(
  3153. 'User' => array('type' => 'text')
  3154. ),
  3155. 'validates' => array(),
  3156. 'key' => 'id'
  3157. )
  3158. );
  3159. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  3160. $result = $this->Form->input('User', array('type' => 'text'));
  3161. $expected = array(
  3162. 'div' => array('class' => 'input text'),
  3163. 'label' => array('for' => 'UserUser'), 'User', '/label',
  3164. 'input' => array('name' => 'User[User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  3165. '/div'
  3166. );
  3167. $this->assertTags($result, $expected);
  3168. }
  3169. /**
  3170. * testFormInputs method
  3171. *
  3172. * test correct results from form::inputs().
  3173. *
  3174. * @return void
  3175. */
  3176. public function testFormInputs() {
  3177. $this->markTestIncomplete('Need to revisit once models work again.');
  3178. $this->Form->create('Cake\Test\TestCase\View\Helper\Contact');
  3179. $result = $this->Form->inputs('The Legend');
  3180. $expected = array(
  3181. '<fieldset',
  3182. '<legend',
  3183. 'The Legend',
  3184. '/legend',
  3185. '*/fieldset',
  3186. );
  3187. $this->assertTags($result, $expected);
  3188. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  3189. $expected = array(
  3190. 'fieldset' => array('class' => 'classy-stuff'),
  3191. '<legend',
  3192. 'Field of Dreams',
  3193. '/legend',
  3194. '*/fieldset'
  3195. );
  3196. $this->assertTags($result, $expected);
  3197. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  3198. $this->assertTags($result, $expected);
  3199. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  3200. $this->assertTags($result, $expected);
  3201. $this->Form->create('Contact');
  3202. $this->Form->request['prefix'] = 'admin';
  3203. $this->Form->request['action'] = 'admin_edit';
  3204. $result = $this->Form->inputs();
  3205. $expected = array(
  3206. '<fieldset',
  3207. '<legend',
  3208. 'Edit Contact',
  3209. '/legend',
  3210. '*/fieldset',
  3211. );
  3212. $this->assertTags($result, $expected);
  3213. $this->Form->create('Contact');
  3214. $result = $this->Form->inputs(false);
  3215. $expected = array(
  3216. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3217. array('div' => array('class' => 'input text')),
  3218. '*/div',
  3219. array('div' => array('class' => 'input email')),
  3220. '*/div',
  3221. array('div' => array('class' => 'input tel')),
  3222. '*/div',
  3223. array('div' => array('class' => 'input password')),
  3224. '*/div',
  3225. array('div' => array('class' => 'input date')),
  3226. '*/div',
  3227. array('div' => array('class' => 'input date')),
  3228. '*/div',
  3229. array('div' => array('class' => 'input datetime')),
  3230. '*/div',
  3231. array('div' => array('class' => 'input number')),
  3232. '*/div',
  3233. array('div' => array('class' => 'input select')),
  3234. '*/div',
  3235. );
  3236. $this->assertTags($result, $expected);
  3237. $this->Form->create('Contact');
  3238. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  3239. $expected = array(
  3240. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3241. array('div' => array('class' => 'input text')),
  3242. '*/div',
  3243. array('div' => array('class' => 'input email')),
  3244. '*/div',
  3245. array('div' => array('class' => 'input tel')),
  3246. '*/div',
  3247. array('div' => array('class' => 'input password')),
  3248. '*/div',
  3249. array('div' => array('class' => 'input date')),
  3250. '*/div',
  3251. array('div' => array('class' => 'input date')),
  3252. '*/div',
  3253. array('div' => array('class' => 'input datetime')),
  3254. '*/div',
  3255. array('div' => array('class' => 'input number')),
  3256. '*/div',
  3257. array('div' => array('class' => 'input select')),
  3258. '*/div',
  3259. );
  3260. $this->assertTags($result, $expected);
  3261. $this->Form->create('Contact');
  3262. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  3263. $this->assertTags($result, $expected);
  3264. $this->Form->create('Contact');
  3265. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  3266. $expected = array(
  3267. 'fieldset' => array(),
  3268. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3269. array('div' => array('class' => 'input text')),
  3270. '*/div',
  3271. array('div' => array('class' => 'input email')),
  3272. '*/div',
  3273. array('div' => array('class' => 'input tel')),
  3274. '*/div',
  3275. array('div' => array('class' => 'input password')),
  3276. '*/div',
  3277. array('div' => array('class' => 'input date')),
  3278. '*/div',
  3279. array('div' => array('class' => 'input date')),
  3280. '*/div',
  3281. array('div' => array('class' => 'input datetime')),
  3282. '*/div',
  3283. array('div' => array('class' => 'input number')),
  3284. '*/div',
  3285. array('div' => array('class' => 'input select')),
  3286. '*/div',
  3287. '/fieldset'
  3288. );
  3289. $this->assertTags($result, $expected);
  3290. $this->Form->create('Contact');
  3291. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  3292. $expected = array(
  3293. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3294. array('div' => array('class' => 'input text')),
  3295. '*/div',
  3296. array('div' => array('class' => 'input email')),
  3297. '*/div',
  3298. array('div' => array('class' => 'input tel')),
  3299. '*/div',
  3300. array('div' => array('class' => 'input password')),
  3301. '*/div',
  3302. array('div' => array('class' => 'input date')),
  3303. '*/div',
  3304. array('div' => array('class' => 'input date')),
  3305. '*/div',
  3306. array('div' => array('class' => 'input datetime')),
  3307. '*/div',
  3308. array('div' => array('class' => 'input number')),
  3309. '*/div',
  3310. array('div' => array('class' => 'input select')),
  3311. '*/div',
  3312. );
  3313. $this->assertTags($result, $expected);
  3314. $this->Form->create('Contact');
  3315. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  3316. $this->assertTags($result, $expected);
  3317. $this->Form->create('Contact');
  3318. $result = $this->Form->inputs('Hello');
  3319. $expected = array(
  3320. 'fieldset' => array(),
  3321. 'legend' => array(),
  3322. 'Hello',
  3323. '/legend',
  3324. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3325. array('div' => array('class' => 'input text')),
  3326. '*/div',
  3327. array('div' => array('class' => 'input email')),
  3328. '*/div',
  3329. array('div' => array('class' => 'input tel')),
  3330. '*/div',
  3331. array('div' => array('class' => 'input password')),
  3332. '*/div',
  3333. array('div' => array('class' => 'input date')),
  3334. '*/div',
  3335. array('div' => array('class' => 'input date')),
  3336. '*/div',
  3337. array('div' => array('class' => 'input datetime')),
  3338. '*/div',
  3339. array('div' => array('class' => 'input number')),
  3340. '*/div',
  3341. array('div' => array('class' => 'input select')),
  3342. '*/div',
  3343. '/fieldset'
  3344. );
  3345. $this->assertTags($result, $expected);
  3346. $this->Form->create('Contact');
  3347. $result = $this->Form->inputs(array('legend' => 'Hello'));
  3348. $expected = array(
  3349. 'fieldset' => array(),
  3350. 'legend' => array(),
  3351. 'Hello',
  3352. '/legend',
  3353. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3354. array('div' => array('class' => 'input text')),
  3355. '*/div',
  3356. array('div' => array('class' => 'input email')),
  3357. '*/div',
  3358. array('div' => array('class' => 'input tel')),
  3359. '*/div',
  3360. array('div' => array('class' => 'input password')),
  3361. '*/div',
  3362. array('div' => array('class' => 'input date')),
  3363. '*/div',
  3364. array('div' => array('class' => 'input date')),
  3365. '*/div',
  3366. array('div' => array('class' => 'input datetime')),
  3367. '*/div',
  3368. array('div' => array('class' => 'input number')),
  3369. '*/div',
  3370. array('div' => array('class' => 'input select')),
  3371. '*/div',
  3372. '/fieldset'
  3373. );
  3374. $this->assertTags($result, $expected);
  3375. $this->Form->create('Contact');
  3376. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  3377. $this->assertTags($result, $expected);
  3378. $this->Form->end();
  3379. $this->Form->create(false);
  3380. $expected = array(
  3381. 'fieldset' => array(),
  3382. array('div' => array('class' => 'input text')),
  3383. 'label' => array('for' => 'foo'),
  3384. 'Foo',
  3385. '/label',
  3386. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  3387. '*/div',
  3388. '/fieldset'
  3389. );
  3390. $result = $this->Form->inputs(
  3391. array('foo' => array('type' => 'text')),
  3392. array(),
  3393. array('legend' => false)
  3394. );
  3395. $this->assertTags($result, $expected);
  3396. }
  3397. /**
  3398. * Tests inputs() works with plugin models
  3399. *
  3400. * @return void
  3401. */
  3402. public function testInputsPluginModel() {
  3403. $this->markTestIncomplete('Need to revisit once models work again.');
  3404. $this->loadFixtures('Post');
  3405. Plugin::load('TestPlugin');
  3406. $this->Form->request['models'] = array(
  3407. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  3408. );
  3409. $this->Form->create('TestPlugin.TestPluginPost');
  3410. $result = $this->Form->inputs();
  3411. $this->assertContains('TestPluginPost[id]', $result);
  3412. $this->assertContains('TestPluginPost[author_id]', $result);
  3413. $this->assertContains('TestPluginPost[title]', $result);
  3414. $this->assertEquals('TestPluginPost', $this->Form->model());
  3415. }
  3416. /**
  3417. * testSelectAsCheckbox method
  3418. *
  3419. * test multi-select widget with checkbox formatting.
  3420. *
  3421. * @return void
  3422. */
  3423. public function testSelectAsCheckbox() {
  3424. $result = $this->Form->select(
  3425. 'Model.multi_field',
  3426. array('first', 'second', 'third'),
  3427. array('multiple' => 'checkbox', 'value' => array(0, 1))
  3428. );
  3429. $expected = array(
  3430. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3431. array('div' => array('class' => 'checkbox')),
  3432. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0')),
  3433. array('label' => array('for' => 'model-multi-field-0', 'class' => 'selected')),
  3434. 'first',
  3435. '/label',
  3436. '/div',
  3437. array('div' => array('class' => 'checkbox')),
  3438. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1')),
  3439. array('label' => array('for' => 'model-multi-field-1', 'class' => 'selected')),
  3440. 'second',
  3441. '/label',
  3442. '/div',
  3443. array('div' => array('class' => 'checkbox')),
  3444. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  3445. array('label' => array('for' => 'model-multi-field-2')),
  3446. 'third',
  3447. '/label',
  3448. '/div',
  3449. );
  3450. $this->assertTags($result, $expected);
  3451. $result = $this->Form->select(
  3452. 'Model.multi_field',
  3453. array('1/2' => 'half'),
  3454. array('multiple' => 'checkbox')
  3455. );
  3456. $expected = array(
  3457. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3458. array('div' => array('class' => 'checkbox')),
  3459. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2')),
  3460. array('label' => array('for' => 'model-multi-field-1-2')),
  3461. 'half',
  3462. '/label',
  3463. '/div',
  3464. );
  3465. $this->assertTags($result, $expected);
  3466. }
  3467. /**
  3468. * testLabel method
  3469. *
  3470. * test label generation.
  3471. *
  3472. * @return void
  3473. */
  3474. public function testLabel() {
  3475. $result = $this->Form->label('Person.name');
  3476. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  3477. $result = $this->Form->label('Person.name');
  3478. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  3479. $result = $this->Form->label('Person.first_name');
  3480. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'First Name', '/label'));
  3481. $result = $this->Form->label('Person.first_name', 'Your first name');
  3482. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'Your first name', '/label'));
  3483. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  3484. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class'), 'Your first name', '/label'));
  3485. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  3486. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  3487. $result = $this->Form->label('Person.first_name', '');
  3488. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), '/label'));
  3489. $result = $this->Form->label('Person.2.name', '');
  3490. $this->assertTags($result, array('label' => array('for' => 'person-2-name'), '/label'));
  3491. }
  3492. /**
  3493. * testTextbox method
  3494. *
  3495. * test textbox element generation
  3496. *
  3497. * @return void
  3498. */
  3499. public function testTextbox() {
  3500. $result = $this->Form->text('Model.field');
  3501. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]')));
  3502. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3503. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]')));
  3504. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3505. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID')));
  3506. }
  3507. /**
  3508. * Test that text() hooks up with request data and error fields.
  3509. *
  3510. * @return void
  3511. */
  3512. public function testTextBoxDataAndError() {
  3513. $this->article['errors'] = [
  3514. 'Contact' => ['text' => 'wrong']
  3515. ];
  3516. $this->Form->create($this->article);
  3517. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3518. $result = $this->Form->text('Model.text');
  3519. $expected = [
  3520. 'input' => [
  3521. 'type' => 'text',
  3522. 'name' => 'Model[text]',
  3523. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3524. ]
  3525. ];
  3526. $this->assertTags($result, $expected);
  3527. $this->Form->request->data['Contact']['text'] = 'test';
  3528. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3529. $expected = [
  3530. 'input' => [
  3531. 'type' => 'text',
  3532. 'name' => 'Contact[text]',
  3533. 'value' => 'test',
  3534. 'id' => 'theID',
  3535. 'class' => 'form-error'
  3536. ]
  3537. ];
  3538. $this->assertTags($result, $expected);
  3539. }
  3540. /**
  3541. * testDefaultValue method
  3542. *
  3543. * Test default value setting
  3544. *
  3545. * @return void
  3546. */
  3547. public function testTextDefaultValue() {
  3548. $this->Form->request->data['Model']['field'] = 'test';
  3549. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3550. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3551. $this->assertTags($result, $expected);
  3552. unset($this->Form->request->data['Model']['field']);
  3553. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3554. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3555. $this->assertTags($result, $expected);
  3556. }
  3557. /**
  3558. * testError method
  3559. *
  3560. * Test field error generation
  3561. *
  3562. * @return void
  3563. */
  3564. public function testError() {
  3565. $this->article['errors'] = [
  3566. 'Article' => ['field' => 'email']
  3567. ];
  3568. $this->Form->create($this->article);
  3569. $result = $this->Form->error('Article.field');
  3570. $expected = [
  3571. ['div' => ['class' => 'error-message']],
  3572. 'email',
  3573. '/div',
  3574. ];
  3575. $this->assertTags($result, $expected);
  3576. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  3577. $expected = [
  3578. ['div' => ['class' => 'error-message']],
  3579. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3580. '/div',
  3581. ];
  3582. $this->assertTags($result, $expected);
  3583. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3584. $expected = [
  3585. ['div' => ['class' => 'error-message']],
  3586. '<strong', 'Badness!', '/strong',
  3587. '/div',
  3588. ];
  3589. $this->assertTags($result, $expected);
  3590. }
  3591. /**
  3592. * Test error with nested lists.
  3593. *
  3594. * @return void
  3595. */
  3596. public function testErrorMessages() {
  3597. $this->article['errors'] = [
  3598. 'Article' => ['field' => 'email']
  3599. ];
  3600. $this->Form->create($this->article);
  3601. $result = $this->Form->error('Article.field', array(
  3602. 'email' => 'No good!'
  3603. ));
  3604. $expected = array(
  3605. 'div' => array('class' => 'error-message'),
  3606. 'No good!',
  3607. '/div'
  3608. );
  3609. $this->assertTags($result, $expected);
  3610. }
  3611. /**
  3612. * test error() with multiple messages.
  3613. *
  3614. * @return void
  3615. */
  3616. public function testErrorMultipleMessages() {
  3617. $this->article['errors'] = [
  3618. 'field' => ['notEmpty', 'email', 'Something else']
  3619. ];
  3620. $this->Form->create($this->article);
  3621. $result = $this->Form->error('field', array(
  3622. 'notEmpty' => 'Cannot be empty',
  3623. 'email' => 'No good!'
  3624. ));
  3625. $expected = array(
  3626. 'div' => array('class' => 'error-message'),
  3627. 'ul' => array(),
  3628. '<li', 'Cannot be empty', '/li',
  3629. '<li', 'No good!', '/li',
  3630. '<li', 'Something else', '/li',
  3631. '/ul',
  3632. '/div'
  3633. );
  3634. $this->assertTags($result, $expected);
  3635. }
  3636. /**
  3637. * test error options when using form->input();
  3638. *
  3639. * @return void
  3640. */
  3641. public function testInputErrorEscape() {
  3642. $this->markTestIncomplete('Need to revisit once models work again.');
  3643. $this->Form->create('ValidateProfile');
  3644. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3645. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3646. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3647. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3648. $this->assertRegExp('/required<br>/', $result);
  3649. }
  3650. /**
  3651. * testPassword method
  3652. *
  3653. * Test password element generation
  3654. *
  3655. * @return void
  3656. */
  3657. public function testPassword() {
  3658. $this->article['errors'] = [
  3659. 'Contact' => [
  3660. 'passwd' => 1
  3661. ]
  3662. ];
  3663. $this->Form->create($this->article);
  3664. $result = $this->Form->password('Contact.field');
  3665. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]')));
  3666. $this->Form->request->data['Contact']['passwd'] = 'test';
  3667. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3668. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3669. }
  3670. /**
  3671. * testRadio method
  3672. *
  3673. * Test radio element set generation
  3674. *
  3675. * @return void
  3676. */
  3677. public function testRadio() {
  3678. $result = $this->Form->radio('Model.field', array('option A'));
  3679. $expected = array(
  3680. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3681. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3682. 'label' => array('for' => 'model-field-0'),
  3683. 'option A',
  3684. '/label'
  3685. );
  3686. $this->assertTags($result, $expected);
  3687. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3688. $expected = array(
  3689. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3690. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3691. array('label' => array('for' => 'model-field-0')),
  3692. 'option A',
  3693. '/label',
  3694. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1')),
  3695. array('label' => array('for' => 'model-field-1')),
  3696. 'option B',
  3697. '/label',
  3698. );
  3699. $this->assertTags($result, $expected);
  3700. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3701. $expected = array(
  3702. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''),
  3703. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male')),
  3704. array('label' => array('for' => 'employee-gender-male')),
  3705. 'Male',
  3706. '/label',
  3707. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female')),
  3708. array('label' => array('for' => 'employee-gender-female')),
  3709. 'Female',
  3710. '/label',
  3711. );
  3712. $this->assertTags($result, $expected);
  3713. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  3714. $expected = array(
  3715. array('input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '')),
  3716. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0')),
  3717. array('label' => array('for' => 'model-custom-0')),
  3718. 'option A',
  3719. '/label',
  3720. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1')),
  3721. array('label' => array('for' => 'model-custom-1')),
  3722. 'option B',
  3723. '/label',
  3724. );
  3725. $this->assertTags($result, $expected);
  3726. }
  3727. /**
  3728. * test generating radio input inside label ala twitter bootstrap
  3729. *
  3730. * @return void
  3731. */
  3732. public function testRadioInputInsideLabel() {
  3733. $this->Form->templates([
  3734. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3735. 'radioContainer' => '{{label}}'
  3736. ]);
  3737. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  3738. $expected = [
  3739. ['input' => [
  3740. 'type' => 'hidden',
  3741. 'name' => 'Model[field]',
  3742. 'value' => ''
  3743. ]],
  3744. ['label' => ['for' => 'model-field-0']],
  3745. ['input' => [
  3746. 'type' => 'radio',
  3747. 'name' => 'Model[field]',
  3748. 'value' => '0',
  3749. 'id' => 'model-field-0'
  3750. ]],
  3751. 'option A',
  3752. '/label',
  3753. ['label' => ['for' => 'model-field-1']],
  3754. ['input' => [
  3755. 'type' => 'radio',
  3756. 'name' => 'Model[field]',
  3757. 'value' => '1',
  3758. 'id' => 'model-field-1'
  3759. ]],
  3760. 'option B',
  3761. '/label',
  3762. ];
  3763. $this->assertTags($result, $expected);
  3764. }
  3765. /**
  3766. * test disabling the hidden input for radio buttons
  3767. *
  3768. * @return void
  3769. */
  3770. public function testRadioHiddenInputDisabling() {
  3771. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3772. $expected = array(
  3773. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'),
  3774. 'label' => array('for' => 'model-1-field-0'),
  3775. 'option A',
  3776. '/label'
  3777. );
  3778. $this->assertTags($result, $expected);
  3779. }
  3780. /**
  3781. * testDomIdSuffix method
  3782. *
  3783. * @return void
  3784. */
  3785. public function testDomIdSuffix() {
  3786. $this->markTestIncomplete('Need to revisit once models work again.');
  3787. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs');
  3788. $this->assertEquals('1StringWith1$-dollarSigns', $result);
  3789. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>');
  3790. $this->assertEquals('AbcX=FooY=Bar', $result);
  3791. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'xhtml');
  3792. $this->assertEquals('1StringWith1-dollarSigns', $result);
  3793. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>', 'xhtml');
  3794. $this->assertEquals('AbcXFooYBar', $result);
  3795. }
  3796. /**
  3797. * testDomIdSuffixCollisionResolvement()
  3798. *
  3799. * @return void
  3800. */
  3801. public function testDomIdSuffixCollisionResolvement() {
  3802. $this->markTestIncomplete('Need to revisit once models work again.');
  3803. $result = $this->Form->domIdSuffix('a>b');
  3804. $this->assertEquals('AB', $result);
  3805. $result = $this->Form->domIdSuffix('a<b');
  3806. $this->assertEquals('AB1', $result);
  3807. $result = $this->Form->domIdSuffix('a\'b');
  3808. $this->assertEquals('AB2', $result);
  3809. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3810. $this->assertEquals('1StringWith1-dollar', $result);
  3811. $result = $this->Form->domIdSuffix('1 string with 1€-dollar', 'xhtml');
  3812. $this->assertEquals('1StringWith1-dollar1', $result);
  3813. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3814. $this->assertEquals('1StringWith1-dollar2', $result);
  3815. }
  3816. /**
  3817. * testSelect method
  3818. *
  3819. * Test select element generation.
  3820. *
  3821. * @return void
  3822. */
  3823. public function testSelect() {
  3824. $result = $this->Form->select('Model.field', array());
  3825. $expected = array(
  3826. 'select' => array('name' => 'Model[field]'),
  3827. '/select'
  3828. );
  3829. $this->assertTags($result, $expected);
  3830. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3831. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3832. $expected = array(
  3833. 'select' => array('name' => 'Model[field]'),
  3834. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3835. 'good',
  3836. '/option',
  3837. array('option' => array('value' => 'other')),
  3838. 'bad',
  3839. '/option',
  3840. '/select'
  3841. );
  3842. $this->assertTags($result, $expected);
  3843. $this->Form->request->data = array();
  3844. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3845. $expected = array(
  3846. 'select' => array('name' => 'Model[field]'),
  3847. array('option' => array('value' => 'value')),
  3848. 'good',
  3849. '/option',
  3850. array('option' => array('value' => 'other')),
  3851. 'bad',
  3852. '/option',
  3853. '/select'
  3854. );
  3855. $this->assertTags($result, $expected);
  3856. $result = $this->Form->select(
  3857. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3858. array('empty' => false)
  3859. );
  3860. $expected = array(
  3861. 'select' => array('name' => 'Model[field]'),
  3862. array('option' => array('value' => 'first')),
  3863. 'first &quot;html&quot; &lt;chars&gt;',
  3864. '/option',
  3865. array('option' => array('value' => 'second')),
  3866. 'value',
  3867. '/option',
  3868. '/select'
  3869. );
  3870. $this->assertTags($result, $expected);
  3871. $result = $this->Form->select(
  3872. 'Model.field',
  3873. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3874. array('escape' => false, 'empty' => false)
  3875. );
  3876. $expected = array(
  3877. 'select' => array('name' => 'Model[field]'),
  3878. array('option' => array('value' => 'first')),
  3879. 'first "html" <chars>',
  3880. '/option',
  3881. array('option' => array('value' => 'second')),
  3882. 'value',
  3883. '/option',
  3884. '/select'
  3885. );
  3886. $this->assertTags($result, $expected);
  3887. $options = array(
  3888. array('value' => 'first', 'text' => 'First'),
  3889. array('value' => 'first', 'text' => 'Another First'),
  3890. );
  3891. $result = $this->Form->select(
  3892. 'Model.field',
  3893. $options,
  3894. array('escape' => false, 'empty' => false)
  3895. );
  3896. $expected = array(
  3897. 'select' => array('name' => 'Model[field]'),
  3898. array('option' => array('value' => 'first')),
  3899. 'First',
  3900. '/option',
  3901. array('option' => array('value' => 'first')),
  3902. 'Another First',
  3903. '/option',
  3904. '/select'
  3905. );
  3906. $this->assertTags($result, $expected);
  3907. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3908. $result = $this->Form->select(
  3909. 'Model.contact_id',
  3910. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3911. array('escape' => false, 'empty' => 'pick something')
  3912. );
  3913. $expected = array(
  3914. 'select' => array('name' => 'Model[contact_id]'),
  3915. array('option' => array('value' => '')), 'pick something', '/option',
  3916. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3917. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3918. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3919. '/select'
  3920. );
  3921. $this->assertTags($result, $expected);
  3922. $this->Form->request->data['Model']['field'] = 0;
  3923. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3924. $expected = array(
  3925. 'select' => array('name' => 'Model[field]'),
  3926. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3927. array('option' => array('value' => '1')), 'Yes', '/option',
  3928. '/select'
  3929. );
  3930. $this->assertTags($result, $expected);
  3931. }
  3932. /**
  3933. * test select() with required and disabled attributes.
  3934. *
  3935. * @return void
  3936. */
  3937. public function testSelectRequired() {
  3938. $this->article['required'] = [
  3939. 'user_id' => true
  3940. ];
  3941. $this->Form->create($this->article);
  3942. $result = $this->Form->select('user_id', array('option A'));
  3943. $expected = array(
  3944. 'select' => array(
  3945. 'name' => 'user_id',
  3946. 'required' => 'required'
  3947. ),
  3948. array('option' => array('value' => '0')), 'option A', '/option',
  3949. '/select'
  3950. );
  3951. $this->assertTags($result, $expected);
  3952. $result = $this->Form->select('user_id', array('option A'), array('disabled' => true));
  3953. $expected = array(
  3954. 'select' => array(
  3955. 'name' => 'user_id',
  3956. 'disabled' => 'disabled'
  3957. ),
  3958. array('option' => array('value' => '0')), 'option A', '/option',
  3959. '/select'
  3960. );
  3961. $this->assertTags($result, $expected);
  3962. }
  3963. /**
  3964. * testNestedSelect method
  3965. *
  3966. * test select element generation with optgroups
  3967. *
  3968. * @return void
  3969. */
  3970. public function testNestedSelect() {
  3971. $result = $this->Form->select(
  3972. 'Model.field',
  3973. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3974. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3975. )), array('empty' => false)
  3976. );
  3977. $expected = array(
  3978. 'select' => array('name' => 'Model[field]'),
  3979. array('option' => array('value' => 1)),
  3980. 'One',
  3981. '/option',
  3982. array('option' => array('value' => 2)),
  3983. 'Two',
  3984. '/option',
  3985. array('optgroup' => array('label' => 'Three')),
  3986. array('option' => array('value' => 3)),
  3987. 'Three',
  3988. '/option',
  3989. array('option' => array('value' => 4)),
  3990. 'Four',
  3991. '/option',
  3992. array('option' => array('value' => 5)),
  3993. 'Five',
  3994. '/option',
  3995. '/optgroup',
  3996. '/select'
  3997. );
  3998. $this->assertTags($result, $expected);
  3999. }
  4000. /**
  4001. * testSelectMultiple method
  4002. *
  4003. * test generation of multiple select elements
  4004. *
  4005. * @return void
  4006. */
  4007. public function testSelectMultiple() {
  4008. $options = array('first', 'second', 'third');
  4009. $result = $this->Form->select(
  4010. 'Model.multi_field',
  4011. $options,
  4012. array('multiple' => true)
  4013. );
  4014. $expected = array(
  4015. 'input' => array(
  4016. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4017. ),
  4018. 'select' => array(
  4019. 'name' => 'Model[multi_field][]',
  4020. 'multiple' => 'multiple'
  4021. ),
  4022. array('option' => array('value' => '0')),
  4023. 'first',
  4024. '/option',
  4025. array('option' => array('value' => '1')),
  4026. 'second',
  4027. '/option',
  4028. array('option' => array('value' => '2')),
  4029. 'third',
  4030. '/option',
  4031. '/select'
  4032. );
  4033. $this->assertTags($result, $expected);
  4034. $result = $this->Form->select(
  4035. 'Model.multi_field',
  4036. $options,
  4037. array('multiple' => 'multiple')
  4038. );
  4039. $this->assertTags($result, $expected);
  4040. }
  4041. /**
  4042. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4043. *
  4044. * @return void
  4045. */
  4046. public function testCheckboxZeroValue() {
  4047. $this->markTestIncomplete('Need to revisit once models work again.');
  4048. $result = $this->Form->input('User.get_spam', array(
  4049. 'type' => 'checkbox',
  4050. 'value' => '0',
  4051. 'hiddenField' => '1',
  4052. ));
  4053. $expected = array(
  4054. 'div' => array('class' => 'input checkbox'),
  4055. array('input' => array(
  4056. 'type' => 'hidden', 'name' => 'data[User][get_spam]',
  4057. 'value' => '1', 'id' => 'UserGetSpam_'
  4058. )),
  4059. array('input' => array(
  4060. 'type' => 'checkbox', 'name' => 'data[User][get_spam]',
  4061. 'value' => '0', 'id' => 'UserGetSpam'
  4062. )),
  4063. 'label' => array('for' => 'UserGetSpam'),
  4064. 'Get Spam',
  4065. '/label',
  4066. '/div'
  4067. );
  4068. $this->assertTags($result, $expected);
  4069. }
  4070. /**
  4071. * test generation of habtm select boxes.
  4072. *
  4073. * @return void
  4074. */
  4075. public function testHabtmSelectBox() {
  4076. $this->markTestIncomplete('Need to revisit once models work again.');
  4077. $this->View->viewVars['contactTags'] = array(
  4078. 1 => 'blue',
  4079. 2 => 'red',
  4080. 3 => 'green'
  4081. );
  4082. $this->Form->request->data = array(
  4083. 'Contact' => array(),
  4084. 'ContactTag' => array(
  4085. array(
  4086. 'id' => '1',
  4087. 'name' => 'blue'
  4088. ),
  4089. array(
  4090. 'id' => 3,
  4091. 'name' => 'green'
  4092. )
  4093. )
  4094. );
  4095. $this->Form->create('Contact');
  4096. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4097. $expected = array(
  4098. 'input' => array(
  4099. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4100. ),
  4101. 'select' => array(
  4102. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  4103. 'multiple' => 'multiple'
  4104. ),
  4105. array('option' => array('value' => '1', 'selected' => 'selected')),
  4106. 'blue',
  4107. '/option',
  4108. array('option' => array('value' => '2')),
  4109. 'red',
  4110. '/option',
  4111. array('option' => array('value' => '3', 'selected' => 'selected')),
  4112. 'green',
  4113. '/option',
  4114. '/select'
  4115. );
  4116. $this->assertTags($result, $expected);
  4117. // make sure only 50 is selected, and not 50f5c0cf
  4118. $this->View->viewVars['contactTags'] = array(
  4119. '1' => 'blue',
  4120. '50f5c0cf' => 'red',
  4121. '50' => 'green'
  4122. );
  4123. $this->Form->request->data = array(
  4124. 'Contact' => array(),
  4125. 'ContactTag' => array(
  4126. array(
  4127. 'id' => 1,
  4128. 'name' => 'blue'
  4129. ),
  4130. array(
  4131. 'id' => 50,
  4132. 'name' => 'green'
  4133. )
  4134. )
  4135. );
  4136. $this->Form->create('Contact');
  4137. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4138. $expected = array(
  4139. 'input' => array(
  4140. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4141. ),
  4142. 'select' => array(
  4143. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  4144. 'multiple' => 'multiple'
  4145. ),
  4146. array('option' => array('value' => '1', 'selected' => 'selected')),
  4147. 'blue',
  4148. '/option',
  4149. array('option' => array('value' => '50f5c0cf')),
  4150. 'red',
  4151. '/option',
  4152. array('option' => array('value' => '50', 'selected' => 'selected')),
  4153. 'green',
  4154. '/option',
  4155. '/select'
  4156. );
  4157. $this->assertTags($result, $expected);
  4158. }
  4159. /**
  4160. * test generation of multi select elements in checkbox format
  4161. *
  4162. * @return void
  4163. */
  4164. public function testSelectMultipleCheckboxes() {
  4165. $result = $this->Form->select(
  4166. 'Model.multi_field',
  4167. array('first', 'second', 'third'),
  4168. array('multiple' => 'checkbox')
  4169. );
  4170. $expected = array(
  4171. 'input' => array(
  4172. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4173. ),
  4174. array('div' => array('class' => 'checkbox')),
  4175. array('input' => array(
  4176. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4177. 'value' => '0', 'id' => 'model-multi-field-0'
  4178. )),
  4179. array('label' => array('for' => 'model-multi-field-0')),
  4180. 'first',
  4181. '/label',
  4182. '/div',
  4183. array('div' => array('class' => 'checkbox')),
  4184. array('input' => array(
  4185. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4186. 'value' => '1', 'id' => 'model-multi-field-1'
  4187. )),
  4188. array('label' => array('for' => 'model-multi-field-1')),
  4189. 'second',
  4190. '/label',
  4191. '/div',
  4192. array('div' => array('class' => 'checkbox')),
  4193. array('input' => array(
  4194. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4195. 'value' => '2', 'id' => 'model-multi-field-2'
  4196. )),
  4197. array('label' => array('for' => 'model-multi-field-2')),
  4198. 'third',
  4199. '/label',
  4200. '/div'
  4201. );
  4202. $this->assertTags($result, $expected);
  4203. $result = $this->Form->select(
  4204. 'Model.multi_field',
  4205. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  4206. array('multiple' => 'checkbox')
  4207. );
  4208. $expected = array(
  4209. 'input' => array(
  4210. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4211. ),
  4212. array('div' => array('class' => 'checkbox')),
  4213. array('input' => array(
  4214. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4215. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  4216. )),
  4217. array('label' => array('for' => 'model-multi-field-a+')),
  4218. 'first',
  4219. '/label',
  4220. '/div',
  4221. array('div' => array('class' => 'checkbox')),
  4222. array('input' => array(
  4223. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4224. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  4225. )),
  4226. array('label' => array('for' => 'model-multi-field-a++')),
  4227. 'second',
  4228. '/label',
  4229. '/div',
  4230. array('div' => array('class' => 'checkbox')),
  4231. array('input' => array(
  4232. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4233. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  4234. )),
  4235. array('label' => array('for' => 'model-multi-field-a+++')),
  4236. 'third',
  4237. '/label',
  4238. '/div'
  4239. );
  4240. $this->assertTags($result, $expected);
  4241. $result = $this->Form->select(
  4242. 'Model.multi_field',
  4243. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4244. array('multiple' => 'checkbox')
  4245. );
  4246. $expected = array(
  4247. 'input' => array(
  4248. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4249. ),
  4250. array('div' => array('class' => 'checkbox')),
  4251. array('input' => array(
  4252. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4253. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  4254. )),
  4255. array('label' => array('for' => 'model-multi-field-a-b')),
  4256. 'first',
  4257. '/label',
  4258. '/div',
  4259. array('div' => array('class' => 'checkbox')),
  4260. array('input' => array(
  4261. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4262. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  4263. )),
  4264. array('label' => array('for' => 'model-multi-field-a-b1')),
  4265. 'second',
  4266. '/label',
  4267. '/div',
  4268. array('div' => array('class' => 'checkbox')),
  4269. array('input' => array(
  4270. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4271. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  4272. )),
  4273. array('label' => array('for' => 'model-multi-field-a-b2')),
  4274. 'third',
  4275. '/label',
  4276. '/div'
  4277. );
  4278. $this->assertTags($result, $expected);
  4279. }
  4280. /**
  4281. * Ensure that multiCheckbox reads from the request data.
  4282. *
  4283. * @return void
  4284. */
  4285. public function testSelectMultipleCheckboxRequestData() {
  4286. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4287. $result = $this->Form->select(
  4288. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4289. );
  4290. $expected = array(
  4291. 'input' => array(
  4292. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  4293. ),
  4294. array('div' => array('class' => 'checkbox')),
  4295. array('input' => array(
  4296. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4297. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  4298. )),
  4299. array('label' => array('for' => 'model-tags-1', 'class' => 'selected')),
  4300. 'first',
  4301. '/label',
  4302. '/div',
  4303. array('div' => array('class' => 'checkbox')),
  4304. array('input' => array(
  4305. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4306. 'value' => 'Array', 'id' => 'model-tags-array'
  4307. )),
  4308. array('label' => array('for' => 'model-tags-array')),
  4309. 'Array',
  4310. '/label',
  4311. '/div'
  4312. );
  4313. $this->assertTags($result, $expected);
  4314. }
  4315. /**
  4316. * Checks the security hash array generated for multiple-input checkbox elements
  4317. *
  4318. * @return void
  4319. */
  4320. public function testSelectMultipleCheckboxSecurity() {
  4321. $this->Form->request->params['_Token'] = 'testKey';
  4322. $this->assertEquals(array(), $this->Form->fields);
  4323. $result = $this->Form->select(
  4324. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4325. array('multiple' => 'checkbox')
  4326. );
  4327. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4328. $result = $this->Form->secure($this->Form->fields);
  4329. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4330. $this->assertRegExp('/"' . $key . '"/', $result);
  4331. }
  4332. /**
  4333. * Multiple select elements should always be secured as they always participate
  4334. * in the POST data.
  4335. *
  4336. * @return void
  4337. */
  4338. public function testSelectMultipleSecureWithNoOptions() {
  4339. $this->assertEquals(array(), $this->Form->fields);
  4340. $this->Form->select(
  4341. 'Model.select',
  4342. array(),
  4343. array('multiple' => true)
  4344. );
  4345. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4346. }
  4347. /**
  4348. * When a select box has no options it should not be added to the fields list
  4349. * as it always fail post validation.
  4350. *
  4351. * @return void
  4352. */
  4353. public function testSelectNoSecureWithNoOptions() {
  4354. $this->Form->request->params['_Token'] = 'testkey';
  4355. $this->assertEquals([], $this->Form->fields);
  4356. $this->Form->select(
  4357. 'Model.select',
  4358. []
  4359. );
  4360. $this->assertEquals([], $this->Form->fields);
  4361. $this->Form->select(
  4362. 'Model.user_id',
  4363. [],
  4364. ['empty' => true]
  4365. );
  4366. $this->assertEquals(array('Model.user_id'), $this->Form->fields);
  4367. }
  4368. /**
  4369. * testInputMultipleCheckboxes method
  4370. *
  4371. * test input() resulting in multi select elements being generated.
  4372. *
  4373. * @return void
  4374. */
  4375. public function testInputMultipleCheckboxes() {
  4376. $this->markTestIncomplete('Need to revisit once models work again.');
  4377. $result = $this->Form->input('Model.multi_field', array(
  4378. 'options' => array('first', 'second', 'third'),
  4379. 'multiple' => 'checkbox'
  4380. ));
  4381. $expected = array(
  4382. array('div' => array('class' => 'input select')),
  4383. array('label' => array('for' => 'ModelMultiField')),
  4384. 'Multi Field',
  4385. '/label',
  4386. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4387. array('div' => array('class' => 'checkbox')),
  4388. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4389. array('label' => array('for' => 'ModelMultiField0')),
  4390. 'first',
  4391. '/label',
  4392. '/div',
  4393. array('div' => array('class' => 'checkbox')),
  4394. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4395. array('label' => array('for' => 'ModelMultiField1')),
  4396. 'second',
  4397. '/label',
  4398. '/div',
  4399. array('div' => array('class' => 'checkbox')),
  4400. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4401. array('label' => array('for' => 'ModelMultiField2')),
  4402. 'third',
  4403. '/label',
  4404. '/div',
  4405. '/div'
  4406. );
  4407. $this->assertTags($result, $expected);
  4408. $result = $this->Form->input('Model.multi_field', array(
  4409. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4410. 'multiple' => 'checkbox'
  4411. ));
  4412. $expected = array(
  4413. array('div' => array('class' => 'input select')),
  4414. array('label' => array('for' => 'ModelMultiField')),
  4415. 'Multi Field',
  4416. '/label',
  4417. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4418. array('div' => array('class' => 'checkbox')),
  4419. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4420. array('label' => array('for' => 'ModelMultiFieldA')),
  4421. 'first',
  4422. '/label',
  4423. '/div',
  4424. array('div' => array('class' => 'checkbox')),
  4425. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4426. array('label' => array('for' => 'ModelMultiFieldB')),
  4427. 'second',
  4428. '/label',
  4429. '/div',
  4430. array('div' => array('class' => 'checkbox')),
  4431. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4432. array('label' => array('for' => 'ModelMultiFieldC')),
  4433. 'third',
  4434. '/label',
  4435. '/div',
  4436. '/div'
  4437. );
  4438. $this->assertTags($result, $expected);
  4439. $result = $this->Form->input('Model.multi_field', array(
  4440. 'options' => array('1' => 'first'),
  4441. 'multiple' => 'checkbox',
  4442. 'label' => false,
  4443. 'div' => false
  4444. ));
  4445. $expected = array(
  4446. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4447. array('div' => array('class' => 'checkbox')),
  4448. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4449. array('label' => array('for' => 'ModelMultiField1')),
  4450. 'first',
  4451. '/label',
  4452. '/div'
  4453. );
  4454. $this->assertTags($result, $expected);
  4455. $result = $this->Form->input('Model.multi_field', array(
  4456. 'options' => array('2' => 'second'),
  4457. 'multiple' => 'checkbox',
  4458. 'label' => false,
  4459. 'div' => false
  4460. ));
  4461. $expected = array(
  4462. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4463. array('div' => array('class' => 'checkbox')),
  4464. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4465. array('label' => array('for' => 'ModelMultiField2')),
  4466. 'second',
  4467. '/label',
  4468. '/div'
  4469. );
  4470. $this->assertTags($result, $expected);
  4471. }
  4472. /**
  4473. * testSelectHiddenFieldOmission method
  4474. *
  4475. * test that select() with 'hiddenField' => false omits the hidden field
  4476. *
  4477. * @return void
  4478. */
  4479. public function testSelectHiddenFieldOmission() {
  4480. $result = $this->Form->select('Model.multi_field',
  4481. array('first', 'second'),
  4482. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4483. );
  4484. $this->assertNotContains('type="hidden"', $result);
  4485. }
  4486. /**
  4487. * test that select() with multiple = checkbox works with overriding name attribute.
  4488. *
  4489. * @return void
  4490. */
  4491. public function testSelectCheckboxMultipleOverrideName() {
  4492. $result = $this->Form->select('category', ['1', '2'], [
  4493. 'multiple' => 'checkbox',
  4494. 'name' => 'fish',
  4495. ]);
  4496. $expected = array(
  4497. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => ''),
  4498. array('div' => array('class' => 'checkbox')),
  4499. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0')),
  4500. array('label' => array('for' => 'fish-0')), '1', '/label',
  4501. '/div',
  4502. array('div' => array('class' => 'checkbox')),
  4503. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1')),
  4504. array('label' => array('for' => 'fish-1')), '2', '/label',
  4505. '/div'
  4506. );
  4507. $this->assertTags($result, $expected);
  4508. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  4509. 'name' => 'fish',
  4510. ]);
  4511. $this->assertTags($result, $expected);
  4512. }
  4513. /**
  4514. * testCheckbox method
  4515. *
  4516. * Test generation of checkboxes
  4517. *
  4518. * @return void
  4519. */
  4520. public function testCheckbox() {
  4521. $result = $this->Form->checkbox('Model.field');
  4522. $expected = array(
  4523. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  4524. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'))
  4525. );
  4526. $this->assertTags($result, $expected);
  4527. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4528. $expected = array(
  4529. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  4530. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => 'myvalue', 'id' => 'theID'))
  4531. );
  4532. $this->assertTags($result, $expected);
  4533. }
  4534. /**
  4535. * testCheckboxDefaultValue method
  4536. *
  4537. * Test default value setting on checkbox() method
  4538. *
  4539. * @return void
  4540. */
  4541. public function testCheckboxDefaultValue() {
  4542. $this->Form->request->data['Model']['field'] = false;
  4543. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  4544. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  4545. unset($this->Form->request->data['Model']['field']);
  4546. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  4547. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  4548. $this->Form->request->data['Model']['field'] = true;
  4549. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  4550. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  4551. unset($this->Form->request->data['Model']['field']);
  4552. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  4553. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  4554. }
  4555. /**
  4556. * Test checkbox being checked or having errors.
  4557. *
  4558. * @return void
  4559. */
  4560. public function testCheckboxCheckedAndError() {
  4561. $this->article['errors'] = [
  4562. 'published' => true
  4563. ];
  4564. $this->Form->request->data['published'] = 'myvalue';
  4565. $this->Form->create($this->article);
  4566. $result = $this->Form->checkbox('published', array('id' => 'theID', 'value' => 'myvalue'));
  4567. $expected = array(
  4568. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4569. array('input' => array(
  4570. 'type' => 'checkbox',
  4571. 'name' => 'published',
  4572. 'value' => 'myvalue',
  4573. 'id' => 'theID',
  4574. 'checked' => 'checked',
  4575. 'class' => 'form-error'
  4576. ))
  4577. );
  4578. $this->assertTags($result, $expected);
  4579. $this->Form->request->data['published'] = '';
  4580. $result = $this->Form->checkbox('published');
  4581. $expected = array(
  4582. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4583. array('input' => array('type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error'))
  4584. );
  4585. $this->assertTags($result, $expected);
  4586. }
  4587. /**
  4588. * test checkbox() with a custom name attribute
  4589. *
  4590. * @return void
  4591. */
  4592. public function testCheckboxCustomNameAttribute() {
  4593. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4594. $expected = array(
  4595. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0'),
  4596. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1'))
  4597. );
  4598. $this->assertTags($result, $expected);
  4599. }
  4600. /**
  4601. * Test that the hidden input for checkboxes can be omitted or set to a
  4602. * specific value.
  4603. *
  4604. * @return void
  4605. */
  4606. public function testCheckboxHiddenField() {
  4607. $result = $this->Form->checkbox('UserForm.something', array(
  4608. 'hiddenField' => false
  4609. ));
  4610. $expected = array(
  4611. 'input' => array(
  4612. 'type' => 'checkbox',
  4613. 'name' => 'UserForm[something]',
  4614. 'value' => '1'
  4615. ),
  4616. );
  4617. $this->assertTags($result, $expected);
  4618. $result = $this->Form->checkbox('UserForm.something', array(
  4619. 'value' => 'Y',
  4620. 'hiddenField' => 'N',
  4621. ));
  4622. $expected = array(
  4623. array('input' => array(
  4624. 'type' => 'hidden', 'name' => 'UserForm[something]',
  4625. 'value' => 'N'
  4626. )),
  4627. array('input' => array(
  4628. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  4629. 'value' => 'Y'
  4630. )),
  4631. );
  4632. $this->assertTags($result, $expected);
  4633. }
  4634. /**
  4635. * testDateTime method
  4636. *
  4637. * Test generation of date/time select elements
  4638. *
  4639. * @return void
  4640. */
  4641. public function testDateTime() {
  4642. extract($this->dateRegex);
  4643. $result = $this->Form->dateTime('Contact.date', array('empty' => false));
  4644. $now = strtotime('now');
  4645. $expected = array(
  4646. array('select' => array('name' => 'Contact[date][month]')),
  4647. $monthsRegex,
  4648. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4649. date('F', $now),
  4650. '/option',
  4651. '*/select',
  4652. array('select' => array('name' => 'Contact[date][day]')),
  4653. $daysRegex,
  4654. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4655. date('j', $now),
  4656. '/option',
  4657. '*/select',
  4658. array('select' => array('name' => 'Contact[date][year]')),
  4659. $yearsRegex,
  4660. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4661. date('Y', $now),
  4662. '/option',
  4663. '*/select',
  4664. array('select' => array('name' => 'Contact[date][hour]')),
  4665. $hoursRegex,
  4666. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4667. date('g', $now),
  4668. '/option',
  4669. '*/select',
  4670. array('select' => array('name' => 'Contact[date][minute]')),
  4671. $minutesRegex,
  4672. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4673. date('i', $now),
  4674. '/option',
  4675. '*/select',
  4676. array('select' => array('name' => 'Contact[date][meridian]')),
  4677. $meridianRegex,
  4678. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4679. date('a', $now),
  4680. '/option',
  4681. '*/select'
  4682. );
  4683. $this->assertTags($result, $expected);
  4684. }
  4685. /**
  4686. * Test empty defaulting to true for datetime.
  4687. *
  4688. * @return void
  4689. */
  4690. public function testDatetimeEmpty() {
  4691. extract($this->dateRegex);
  4692. $now = strtotime('now');
  4693. $result = $this->Form->dateTime('Contact.date', array(
  4694. 'empty' => true,
  4695. ));
  4696. $expected = array(
  4697. array('select' => array('name' => 'Contact[date][month]')),
  4698. $monthsRegex,
  4699. array('option' => array('value' => '')),
  4700. '/option',
  4701. '*/select',
  4702. array('select' => array('name' => 'Contact[date][day]')),
  4703. $daysRegex,
  4704. array('option' => array('value' => '')),
  4705. '/option',
  4706. '*/select',
  4707. array('select' => array('name' => 'Contact[date][year]')),
  4708. $yearsRegex,
  4709. array('option' => array('value' => '')),
  4710. '/option',
  4711. '*/select',
  4712. array('select' => array('name' => 'Contact[date][hour]')),
  4713. $hoursRegex,
  4714. array('option' => array('value' => '')),
  4715. '/option',
  4716. '*/select',
  4717. array('select' => array('name' => 'Contact[date][minute]')),
  4718. $minutesRegex,
  4719. array('option' => array('value' => '')),
  4720. '/option',
  4721. '*/select',
  4722. array('select' => array('name' => 'Contact[date][meridian]')),
  4723. $meridianRegex,
  4724. array('option' => array('value' => '')),
  4725. '/option',
  4726. '*/select'
  4727. );
  4728. $this->assertTags($result, $expected);
  4729. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4730. }
  4731. /**
  4732. * Test datetime with interval option.
  4733. *
  4734. * @return void
  4735. */
  4736. public function testDatetimeMinuteInterval() {
  4737. extract($this->dateRegex);
  4738. $now = strtotime('now');
  4739. $result = $this->Form->dateTime('Contact.date', array(
  4740. 'interval' => 5,
  4741. 'value' => ''
  4742. ));
  4743. $expected = array(
  4744. array('select' => array('name' => 'Contact[date][month]')),
  4745. $monthsRegex,
  4746. array('option' => array('selected' => 'selected', 'value' => '')),
  4747. '/option',
  4748. '*/select',
  4749. array('select' => array('name' => 'Contact[date][day]')),
  4750. $daysRegex,
  4751. array('option' => array('selected' => 'selected', 'value' => '')),
  4752. '/option',
  4753. '*/select',
  4754. array('select' => array('name' => 'Contact[date][year]')),
  4755. $yearsRegex,
  4756. array('option' => array('selected' => 'selected', 'value' => '')),
  4757. '/option',
  4758. '*/select',
  4759. array('select' => array('name' => 'Contact[date][hour]')),
  4760. $hoursRegex,
  4761. array('option' => array('selected' => 'selected', 'value' => '')),
  4762. '/option',
  4763. '*/select',
  4764. array('select' => array('name' => 'Contact[date][minute]')),
  4765. $minutesRegex,
  4766. array('option' => array('selected' => 'selected', 'value' => '')),
  4767. '/option',
  4768. array('option' => array('value' => '00')),
  4769. '00',
  4770. '/option',
  4771. array('option' => array('value' => '05')),
  4772. '05',
  4773. '/option',
  4774. array('option' => array('value' => '10')),
  4775. '10',
  4776. '/option',
  4777. '*/select',
  4778. );
  4779. $this->assertTags($result, $expected);
  4780. }
  4781. /**
  4782. * Test dateTime with rounding
  4783. *
  4784. * @return void
  4785. */
  4786. public function testDateTimeRounding() {
  4787. $this->Form->request->data['Contact'] = array(
  4788. 'date' => array(
  4789. 'day' => '13',
  4790. 'month' => '12',
  4791. 'year' => '2010',
  4792. 'hour' => '04',
  4793. 'minute' => '19',
  4794. 'meridian' => 'AM'
  4795. )
  4796. );
  4797. $result = $this->Form->dateTime('Contact.date', array('interval' => 15));
  4798. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4799. $result = $this->Form->dateTime('Contact.date', array('interval' => 15, 'round' => 'up'));
  4800. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  4801. $result = $this->Form->dateTime('Contact.date', array('interval' => 5, 'round' => 'down'));
  4802. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4803. }
  4804. /**
  4805. * test that datetime() and default values work.
  4806. *
  4807. * @return void
  4808. */
  4809. public function testDatetimeWithDefault() {
  4810. $this->markTestIncomplete('Need to revisit soon.');
  4811. $result = $this->Form->dateTime('Contact.updated', array('value' => '2009-06-01 11:15:30'));
  4812. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4813. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4814. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4815. $result = $this->Form->dateTime('Contact.updated', array(
  4816. 'default' => '2009-06-01 11:15:30'
  4817. ));
  4818. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4819. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4820. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4821. }
  4822. /**
  4823. * testDateTime all zeros
  4824. *
  4825. * @return void
  4826. */
  4827. public function testDateTimeAllZeros() {
  4828. $result = $this->Form->dateTime('Contact.date', array(
  4829. 'timeFormat' => false,
  4830. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  4831. 'value' => '0000-00-00'
  4832. ));
  4833. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  4834. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  4835. }
  4836. /**
  4837. * testDateTimeEmptyAsArray
  4838. *
  4839. * @return void
  4840. */
  4841. public function testDateTimeEmptyAsArray() {
  4842. $result = $this->Form->dateTime('Contact.date', array(
  4843. 'empty' => array(
  4844. 'day' => 'DAY',
  4845. 'month' => 'MONTH',
  4846. 'year' => 'YEAR',
  4847. 'hour' => 'HOUR',
  4848. 'minute' => 'MINUTE',
  4849. 'meridian' => false
  4850. )
  4851. ));
  4852. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4853. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4854. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4855. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4856. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4857. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4858. $result = $this->Form->dateTime('Contact.date', array(
  4859. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4860. ));
  4861. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4862. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4863. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4864. }
  4865. /**
  4866. * testFormDateTimeMulti method
  4867. *
  4868. * test multiple datetime element generation
  4869. *
  4870. * @return void
  4871. */
  4872. public function testFormDateTimeMulti() {
  4873. extract($this->dateRegex);
  4874. $result = $this->Form->dateTime('Contact.1.updated');
  4875. $this->assertContains('Contact[1][updated][month]', $result);
  4876. $this->assertContains('Contact[1][updated][day]', $result);
  4877. $this->assertContains('Contact[1][updated][year]', $result);
  4878. $this->assertContains('Contact[1][updated][hour]', $result);
  4879. $this->assertContains('Contact[1][updated][minute]', $result);
  4880. $this->assertContains('Contact[1][updated][meridian]', $result);
  4881. }
  4882. /**
  4883. * When changing the date format, the label should always focus the first select box when
  4884. * clicked.
  4885. *
  4886. * @return void
  4887. */
  4888. public function testDateTimeLabelIdMatchesFirstInput() {
  4889. $this->markTestIncomplete('Need to revisit once models work again.');
  4890. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4891. $this->assertContains('label for="ModelDateMonth"', $result);
  4892. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4893. $this->assertContains('label for="ModelDateDay"', $result);
  4894. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4895. $this->assertContains('label for="ModelDateYear"', $result);
  4896. }
  4897. /**
  4898. * testMonth method
  4899. *
  4900. * @return void
  4901. */
  4902. public function testMonth() {
  4903. $result = $this->Form->month('Model.field', ['value' => '']);
  4904. $expected = array(
  4905. array('select' => array('name' => 'Model[field][month]')),
  4906. array('option' => array('value' => '', 'selected' => 'selected')),
  4907. '/option',
  4908. array('option' => array('value' => '01')),
  4909. date('F', strtotime('2008-01-01 00:00:00')),
  4910. '/option',
  4911. array('option' => array('value' => '02')),
  4912. date('F', strtotime('2008-02-01 00:00:00')),
  4913. '/option',
  4914. '*/select',
  4915. );
  4916. $this->assertTags($result, $expected);
  4917. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  4918. $expected = array(
  4919. array('select' => array('name' => 'Model[field][month]')),
  4920. array('option' => array('selected' => 'selected', 'value' => '')),
  4921. '/option',
  4922. array('option' => array('value' => '01')),
  4923. date('F', strtotime('2008-01-01 00:00:00')),
  4924. '/option',
  4925. array('option' => array('value' => '02')),
  4926. date('F', strtotime('2008-02-01 00:00:00')),
  4927. '/option',
  4928. '*/select',
  4929. );
  4930. $this->assertTags($result, $expected);
  4931. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  4932. $expected = array(
  4933. array('select' => array('name' => 'Model[field][month]')),
  4934. array('option' => array('selected' => 'selected', 'value' => '')),
  4935. '/option',
  4936. array('option' => array('value' => '01')),
  4937. '1',
  4938. '/option',
  4939. array('option' => array('value' => '02')),
  4940. '2',
  4941. '/option',
  4942. '*/select',
  4943. );
  4944. $this->assertTags($result, $expected);
  4945. $monthNames = [
  4946. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4947. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  4948. ];
  4949. $result = $this->Form->month('Model.field', array('value' => '1', 'monthNames' => $monthNames));
  4950. $expected = array(
  4951. array('select' => array('name' => 'Model[field][month]')),
  4952. array('option' => array('value' => '')),
  4953. '/option',
  4954. array('option' => array('value' => '01', 'selected' => 'selected')),
  4955. 'Jan',
  4956. '/option',
  4957. array('option' => array('value' => '02')),
  4958. 'Feb',
  4959. '/option',
  4960. '*/select',
  4961. );
  4962. $this->assertTags($result, $expected);
  4963. $this->Form->request->data['Project']['release'] = '2050-02-10';
  4964. $result = $this->Form->month('Project.release');
  4965. $expected = array(
  4966. array('select' => array('name' => 'Project[release][month]')),
  4967. array('option' => array('value' => '')),
  4968. '/option',
  4969. array('option' => array('value' => '01')),
  4970. 'January',
  4971. '/option',
  4972. array('option' => array('value' => '02', 'selected' => 'selected')),
  4973. 'February',
  4974. '/option',
  4975. '*/select',
  4976. );
  4977. $this->assertTags($result, $expected);
  4978. }
  4979. /**
  4980. * testDay method
  4981. *
  4982. * @return void
  4983. */
  4984. public function testDay() {
  4985. extract($this->dateRegex);
  4986. $result = $this->Form->day('Model.field', array('value' => false));
  4987. $expected = array(
  4988. array('select' => array('name' => 'Model[field][day]')),
  4989. array('option' => array('selected' => 'selected', 'value' => '')),
  4990. '/option',
  4991. array('option' => array('value' => '01')),
  4992. '1',
  4993. '/option',
  4994. array('option' => array('value' => '02')),
  4995. '2',
  4996. '/option',
  4997. $daysRegex,
  4998. '/select',
  4999. );
  5000. $this->assertTags($result, $expected);
  5001. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5002. $result = $this->Form->day('Model.field');
  5003. $expected = array(
  5004. array('select' => array('name' => 'Model[field][day]')),
  5005. array('option' => array('value' => '')),
  5006. '/option',
  5007. array('option' => array('value' => '01')),
  5008. '1',
  5009. '/option',
  5010. array('option' => array('value' => '02')),
  5011. '2',
  5012. '/option',
  5013. $daysRegex,
  5014. array('option' => array('value' => '10', 'selected' => 'selected')),
  5015. '10',
  5016. '/option',
  5017. $daysRegex,
  5018. '/select',
  5019. );
  5020. $this->assertTags($result, $expected);
  5021. $this->Form->request->data['Model']['field'] = '';
  5022. $result = $this->Form->day('Model.field', array('value' => '10'));
  5023. $expected = array(
  5024. array('select' => array('name' => 'Model[field][day]')),
  5025. array('option' => array('value' => '')),
  5026. '/option',
  5027. array('option' => array('value' => '01')),
  5028. '1',
  5029. '/option',
  5030. array('option' => array('value' => '02')),
  5031. '2',
  5032. '/option',
  5033. $daysRegex,
  5034. array('option' => array('value' => '10', 'selected' => 'selected')),
  5035. '10',
  5036. '/option',
  5037. $daysRegex,
  5038. '/select',
  5039. );
  5040. $this->assertTags($result, $expected);
  5041. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5042. $result = $this->Form->day('Project.release');
  5043. $expected = array(
  5044. array('select' => array('name' => 'Project[release][day]')),
  5045. array('option' => array('value' => '')),
  5046. '/option',
  5047. array('option' => array('value' => '01')),
  5048. '1',
  5049. '/option',
  5050. array('option' => array('value' => '02')),
  5051. '2',
  5052. '/option',
  5053. $daysRegex,
  5054. array('option' => array('value' => '10', 'selected' => 'selected')),
  5055. '10',
  5056. '/option',
  5057. $daysRegex,
  5058. '/select',
  5059. );
  5060. $this->assertTags($result, $expected);
  5061. }
  5062. /**
  5063. * testMinute method
  5064. *
  5065. * @return void
  5066. */
  5067. public function testMinute() {
  5068. extract($this->dateRegex);
  5069. $result = $this->Form->minute('Model.field', ['value' => '']);
  5070. $expected = array(
  5071. array('select' => array('name' => 'Model[field][minute]')),
  5072. array('option' => array('selected' => 'selected', 'value' => '')),
  5073. '/option',
  5074. array('option' => array('value' => '00')),
  5075. '00',
  5076. '/option',
  5077. array('option' => array('value' => '01')),
  5078. '01',
  5079. '/option',
  5080. array('option' => array('value' => '02')),
  5081. '02',
  5082. '/option',
  5083. $minutesRegex,
  5084. '/select',
  5085. );
  5086. $this->assertTags($result, $expected);
  5087. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5088. $result = $this->Form->minute('Model.field');
  5089. $expected = array(
  5090. array('select' => array('name' => 'Model[field][minute]')),
  5091. array('option' => array('value' => '')),
  5092. '/option',
  5093. array('option' => array('value' => '00')),
  5094. '00',
  5095. '/option',
  5096. array('option' => array('value' => '01')),
  5097. '01',
  5098. '/option',
  5099. array('option' => array('value' => '02')),
  5100. '02',
  5101. '/option',
  5102. $minutesRegex,
  5103. array('option' => array('value' => '12', 'selected' => 'selected')),
  5104. '12',
  5105. '/option',
  5106. $minutesRegex,
  5107. '/select',
  5108. );
  5109. $this->assertTags($result, $expected);
  5110. $this->Form->request->data['Model']['field'] = '';
  5111. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5112. $expected = array(
  5113. array('select' => array('name' => 'Model[field][minute]')),
  5114. array('option' => array('selected' => 'selected', 'value' => '')),
  5115. '/option',
  5116. array('option' => array('value' => '00')),
  5117. '00',
  5118. '/option',
  5119. array('option' => array('value' => '05')),
  5120. '05',
  5121. '/option',
  5122. array('option' => array('value' => '10')),
  5123. '10',
  5124. '/option',
  5125. $minutesRegex,
  5126. '/select',
  5127. );
  5128. $this->assertTags($result, $expected);
  5129. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5130. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5131. $expected = array(
  5132. array('select' => array('name' => 'Model[field][minute]')),
  5133. array('option' => array('value' => '')),
  5134. '/option',
  5135. array('option' => array('value' => '00')),
  5136. '00',
  5137. '/option',
  5138. array('option' => array('value' => '05')),
  5139. '05',
  5140. '/option',
  5141. array('option' => array('value' => '10', 'selected' => 'selected')),
  5142. '10',
  5143. '/option',
  5144. $minutesRegex,
  5145. '/select',
  5146. );
  5147. $this->assertTags($result, $expected);
  5148. }
  5149. /**
  5150. * Test generating an input for the meridian.
  5151. *
  5152. * @return void
  5153. */
  5154. public function testMeridian() {
  5155. extract($this->dateRegex);
  5156. $now = time();
  5157. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  5158. $expected = [
  5159. array('select' => array('name' => 'Model[field][meridian]')),
  5160. array('option' => array('value' => '')),
  5161. '/option',
  5162. $meridianRegex,
  5163. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5164. date('a', $now),
  5165. '/option',
  5166. '*/select'
  5167. ];
  5168. $this->assertTags($result, $expected);
  5169. }
  5170. /**
  5171. * testHour method
  5172. *
  5173. * @return void
  5174. */
  5175. public function testHour() {
  5176. extract($this->dateRegex);
  5177. $result = $this->Form->hour('Model.field', ['value' => '']);
  5178. $expected = array(
  5179. array('select' => array('name' => 'Model[field][hour]')),
  5180. array('option' => array('selected' => 'selected', 'value' => '')),
  5181. '/option',
  5182. array('option' => array('value' => '01')),
  5183. '1',
  5184. '/option',
  5185. array('option' => array('value' => '02')),
  5186. '2',
  5187. '/option',
  5188. $hoursRegex,
  5189. '/select',
  5190. );
  5191. $this->assertTags($result, $expected);
  5192. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5193. $result = $this->Form->hour('Model.field', ['format' => 12]);
  5194. $expected = array(
  5195. array('select' => array('name' => 'Model[field][hour]')),
  5196. array('option' => array('value' => '')),
  5197. '/option',
  5198. array('option' => array('value' => '01')),
  5199. '1',
  5200. '/option',
  5201. array('option' => array('value' => '02')),
  5202. '2',
  5203. '/option',
  5204. $hoursRegex,
  5205. array('option' => array('value' => '12', 'selected' => 'selected')),
  5206. '12',
  5207. '/option',
  5208. '/select',
  5209. );
  5210. $this->assertTags($result, $expected);
  5211. $this->Form->request->data['Model']['field'] = '';
  5212. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => '23'));
  5213. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5214. $result = $this->Form->hour('Model.field', array('format' => 12, 'value' => '23'));
  5215. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5216. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5217. $result = $this->Form->hour('Model.field', ['format' => 24]);
  5218. $expected = array(
  5219. array('select' => array('name' => 'Model[field][hour]')),
  5220. array('option' => array('value' => '')),
  5221. '/option',
  5222. array('option' => array('value' => '00', 'selected' => 'selected')),
  5223. '0',
  5224. '/option',
  5225. array('option' => array('value' => '01')),
  5226. '1',
  5227. '/option',
  5228. array('option' => array('value' => '02')),
  5229. '2',
  5230. '/option',
  5231. $hoursRegex,
  5232. '/select',
  5233. );
  5234. $this->assertTags($result, $expected);
  5235. unset($this->Form->request->data['Model']['field']);
  5236. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => 'now'));
  5237. $thisHour = date('H');
  5238. $optValue = date('G');
  5239. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5240. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  5241. $result = $this->Form->hour('Model.field', ['format' => 24]);
  5242. $expected = array(
  5243. array('select' => array('name' => 'Model[field][hour]')),
  5244. array('option' => array('value' => '')),
  5245. '/option',
  5246. array('option' => array('value' => '00')),
  5247. '0',
  5248. '/option',
  5249. array('option' => array('value' => '01', 'selected' => 'selected')),
  5250. '1',
  5251. '/option',
  5252. array('option' => array('value' => '02')),
  5253. '2',
  5254. '/option',
  5255. $hoursRegex,
  5256. '/select',
  5257. );
  5258. $this->assertTags($result, $expected);
  5259. }
  5260. /**
  5261. * testYear method
  5262. *
  5263. * @return void
  5264. */
  5265. public function testYear() {
  5266. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  5267. $expected = array(
  5268. array('select' => array('name' => 'Model[field][year]')),
  5269. array('option' => array('selected' => 'selected', 'value' => '')),
  5270. '/option',
  5271. array('option' => array('value' => '2007')),
  5272. '2007',
  5273. '/option',
  5274. array('option' => array('value' => '2006')),
  5275. '2006',
  5276. '/option',
  5277. '/select',
  5278. );
  5279. $this->assertTags($result, $expected);
  5280. $result = $this->Form->year('Model.field', [
  5281. 'value' => '',
  5282. 'minYear' => 2006,
  5283. 'maxYear' => 2007,
  5284. 'orderYear' => 'asc'
  5285. ]);
  5286. $expected = array(
  5287. array('select' => array('name' => 'Model[field][year]')),
  5288. array('option' => array('selected' => 'selected', 'value' => '')),
  5289. '/option',
  5290. array('option' => array('value' => '2006')),
  5291. '2006',
  5292. '/option',
  5293. array('option' => array('value' => '2007')),
  5294. '2007',
  5295. '/option',
  5296. '/select',
  5297. );
  5298. $this->assertTags($result, $expected);
  5299. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5300. $result = $this->Form->year('Contact.published', [
  5301. 'empty' => false,
  5302. 'minYear' => 2006,
  5303. 'maxYear' => 2007,
  5304. ]);
  5305. $expected = array(
  5306. array('select' => array('name' => 'Contact[published][year]')),
  5307. array('option' => array('value' => '2007')),
  5308. '2007',
  5309. '/option',
  5310. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5311. '2006',
  5312. '/option',
  5313. '/select',
  5314. );
  5315. $this->assertTags($result, $expected);
  5316. }
  5317. /**
  5318. * testYearAutoExpandRange method
  5319. *
  5320. * @return void
  5321. */
  5322. public function testYearAutoExpandRange() {
  5323. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  5324. $result = $this->Form->year('User.birthday');
  5325. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5326. $result = $matches[1];
  5327. $expected = range(date('Y') + 5, 1930);
  5328. $this->assertEquals($expected, $result);
  5329. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5330. $result = $this->Form->year('Project.release');
  5331. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5332. $result = $matches[1];
  5333. $expected = range(2050, date('Y') - 5);
  5334. $this->assertEquals($expected, $result);
  5335. $this->Form->request->data['Project']['release'] = '1881-10-10';
  5336. $result = $this->Form->year('Project.release', [
  5337. 'minYear' => 1890,
  5338. 'maxYear' => 1900
  5339. ]);
  5340. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5341. $result = $matches[1];
  5342. $expected = range(1900, 1881);
  5343. $this->assertEquals($expected, $result);
  5344. }
  5345. /**
  5346. * testInputDate method
  5347. *
  5348. * Test various inputs with type date and different dateFormat values.
  5349. * Failing to provide a dateFormat key should not error.
  5350. * It should simply not pre-select any value then.
  5351. *
  5352. * @return void
  5353. */
  5354. public function testInputDate() {
  5355. $this->markTestIncomplete('Need to revisit once models work again.');
  5356. $this->Form->request->data = array(
  5357. 'User' => array(
  5358. 'month_year' => array('month' => date('m')),
  5359. 'just_year' => array('month' => date('m')),
  5360. 'just_month' => array('year' => date('Y')),
  5361. 'just_day' => array('month' => date('m')),
  5362. )
  5363. );
  5364. $this->Form->create('User');
  5365. $result = $this->Form->input('month_year',
  5366. array(
  5367. 'label' => false,
  5368. 'div' => false,
  5369. 'type' => 'date',
  5370. 'dateFormat' => 'MY',
  5371. 'minYear' => 2006,
  5372. 'maxYear' => 2008
  5373. )
  5374. );
  5375. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  5376. $this->assertNotContains('value="2008" selected="selected"', $result);
  5377. $result = $this->Form->input('just_year',
  5378. array(
  5379. 'type' => 'date',
  5380. 'label' => false,
  5381. 'dateFormat' => 'Y',
  5382. 'minYear' => date('Y'),
  5383. 'maxYear' => date('Y', strtotime('+20 years'))
  5384. )
  5385. );
  5386. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  5387. $result = $this->Form->input('just_month',
  5388. array(
  5389. 'type' => 'date',
  5390. 'label' => false,
  5391. 'dateFormat' => 'M',
  5392. 'empty' => false,
  5393. )
  5394. );
  5395. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  5396. $result = $this->Form->input('just_day',
  5397. array(
  5398. 'type' => 'date',
  5399. 'label' => false,
  5400. 'dateFormat' => 'D',
  5401. 'empty' => false,
  5402. )
  5403. );
  5404. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  5405. }
  5406. /**
  5407. * testInputDateMaxYear method
  5408. *
  5409. * Let's say we want to only allow users born from 2006 to 2008 to register
  5410. * This being the first singup page, we still don't have any data
  5411. *
  5412. * @return void
  5413. */
  5414. public function testInputDateMaxYear() {
  5415. $this->markTestIncomplete('Need to revisit once models work again.');
  5416. $this->Form->request->data = array();
  5417. $this->Form->create('User');
  5418. $result = $this->Form->input('birthday',
  5419. array(
  5420. 'label' => false,
  5421. 'div' => false,
  5422. 'type' => 'date',
  5423. 'dateFormat' => 'DMY',
  5424. 'minYear' => 2006,
  5425. 'maxYear' => 2008
  5426. )
  5427. );
  5428. $this->assertContains('value="2008" selected="selected"', $result);
  5429. }
  5430. /**
  5431. * testTextArea method
  5432. *
  5433. * @return void
  5434. */
  5435. public function testTextArea() {
  5436. $this->Form->request->data = array('field' => 'some test data');
  5437. $result = $this->Form->textarea('field');
  5438. $expected = array(
  5439. 'textarea' => array('name' => 'field'),
  5440. 'some test data',
  5441. '/textarea',
  5442. );
  5443. $this->assertTags($result, $expected);
  5444. $result = $this->Form->textarea('user.bio');
  5445. $expected = array(
  5446. 'textarea' => array('name' => 'user[bio]'),
  5447. '/textarea',
  5448. );
  5449. $this->assertTags($result, $expected);
  5450. $this->Form->request->data = array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  5451. $result = $this->Form->textarea('field');
  5452. $expected = array(
  5453. 'textarea' => array('name' => 'field'),
  5454. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5455. '/textarea',
  5456. );
  5457. $this->assertTags($result, $expected);
  5458. $this->Form->request->data = [
  5459. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  5460. ];
  5461. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5462. $expected = array(
  5463. 'textarea' => array('name' => 'Model[field]'),
  5464. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5465. '/textarea',
  5466. );
  5467. $this->assertTags($result, $expected);
  5468. $result = $this->Form->textarea('0.OtherModel.field');
  5469. $expected = array(
  5470. 'textarea' => array('name' => '0[OtherModel][field]'),
  5471. '/textarea'
  5472. );
  5473. $this->assertTags($result, $expected);
  5474. }
  5475. /**
  5476. * testTextAreaWithStupidCharacters method
  5477. *
  5478. * test text area with non-ascii characters
  5479. *
  5480. * @return void
  5481. */
  5482. public function testTextAreaWithStupidCharacters() {
  5483. $result = $this->Form->textarea('Post.content', [
  5484. 'value' => "GREAT®",
  5485. 'rows' => '15',
  5486. 'cols' => '75'
  5487. ]);
  5488. $expected = [
  5489. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5490. 'GREAT®',
  5491. '/textarea',
  5492. ];
  5493. $this->assertTags($result, $expected);
  5494. }
  5495. /**
  5496. * testHiddenField method
  5497. *
  5498. * @return void
  5499. */
  5500. public function testHiddenField() {
  5501. $this->article['errors'] = [
  5502. 'field' => true
  5503. ];
  5504. $this->Form->request->data['field'] = 'test';
  5505. $this->Form->create($this->article);
  5506. $result = $this->Form->hidden('field', array('id' => 'theID'));
  5507. $this->assertTags($result, array(
  5508. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test'))
  5509. );
  5510. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5511. $expected = [
  5512. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  5513. ];
  5514. $this->assertTags($result, $expected);
  5515. }
  5516. /**
  5517. * testFileUploadField method
  5518. *
  5519. * @return void
  5520. */
  5521. public function testFileUploadField() {
  5522. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5523. $result = $this->Form->file('Model.upload');
  5524. $this->assertTags($result, $expected);
  5525. $this->Form->request->data['Model']['upload'] = [
  5526. 'name' => '', 'type' => '', 'tmp_name' => '',
  5527. 'error' => 4, 'size' => 0
  5528. ];
  5529. $result = $this->Form->file('Model.upload');
  5530. $this->assertTags($result, $expected);
  5531. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  5532. $result = $this->Form->file('Model.upload');
  5533. $this->assertTags($result, $expected);
  5534. }
  5535. /**
  5536. * test File upload input on a model not used in create();
  5537. *
  5538. * @return void
  5539. */
  5540. public function testFileUploadOnOtherModel() {
  5541. $this->Form->create($this->article, array('type' => 'file'));
  5542. $result = $this->Form->file('ValidateProfile.city');
  5543. $expected = array(
  5544. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]')
  5545. );
  5546. $this->assertTags($result, $expected);
  5547. }
  5548. /**
  5549. * testButton method
  5550. *
  5551. * @return void
  5552. */
  5553. public function testButton() {
  5554. $result = $this->Form->button('Hi');
  5555. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5556. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5557. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5558. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5559. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5560. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5561. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5562. $result = $this->Form->button('No type', array('type' => false));
  5563. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5564. $result = $this->Form->button('Upload Text', array(
  5565. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  5566. 'escape' => false
  5567. ));
  5568. $this->assertNotRegExp('/\&039/', $result);
  5569. }
  5570. /**
  5571. * Test that button() makes unlocked fields by default.
  5572. *
  5573. * @return void
  5574. */
  5575. public function testButtonUnlockedByDefault() {
  5576. $this->Form->request->params['_csrfToken'] = 'secured';
  5577. $this->Form->button('Save', array('name' => 'save'));
  5578. $this->Form->button('Clear');
  5579. $result = $this->Form->unlockField();
  5580. $this->assertEquals(array('save'), $result);
  5581. }
  5582. /**
  5583. * testPostButton method
  5584. *
  5585. * @return void
  5586. */
  5587. public function testPostButton() {
  5588. $result = $this->Form->postButton('Hi', '/controller/action');
  5589. $this->assertTags($result, array(
  5590. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5591. 'div' => array('style' => 'display:none;'),
  5592. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5593. '/div',
  5594. 'button' => array('type' => 'submit'),
  5595. 'Hi',
  5596. '/button',
  5597. '/form'
  5598. ));
  5599. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5600. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  5601. }
  5602. /**
  5603. * Test using postButton with N dimensional data.
  5604. *
  5605. * @return void
  5606. */
  5607. public function testPostButtonNestedData() {
  5608. $data = array(
  5609. 'one' => array(
  5610. 'two' => array(
  5611. 3, 4, 5
  5612. )
  5613. )
  5614. );
  5615. $result = $this->Form->postButton('Send', '/', array('data' => $data));
  5616. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5617. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5618. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5619. }
  5620. /**
  5621. * Test that postButton adds _Token fields.
  5622. *
  5623. * @return void
  5624. */
  5625. public function testSecurePostButton() {
  5626. $this->Form->request->params['_csrfToken'] = 'testkey';
  5627. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5628. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5629. $expected = array(
  5630. 'form' => array(
  5631. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5632. ),
  5633. array('div' => array('style' => 'display:none;')),
  5634. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5635. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5636. '/div',
  5637. 'button' => array('type' => 'submit'),
  5638. 'Delete',
  5639. '/button',
  5640. array('div' => array('style' => 'display:none;')),
  5641. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5642. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5643. '/div',
  5644. '/form',
  5645. );
  5646. $this->assertTags($result, $expected);
  5647. }
  5648. /**
  5649. * testPostLink method
  5650. *
  5651. * @return void
  5652. */
  5653. public function testPostLink() {
  5654. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5655. $this->assertTags($result, array(
  5656. 'form' => array(
  5657. 'method' => 'post', 'action' => '/posts/delete/1',
  5658. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5659. ),
  5660. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5661. '/form',
  5662. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5663. 'Delete',
  5664. '/a'
  5665. ));
  5666. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5667. $this->assertTags($result, array(
  5668. 'form' => array(
  5669. 'method' => 'post', 'action' => '/posts/delete/1',
  5670. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5671. ),
  5672. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5673. '/form',
  5674. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5675. 'Delete',
  5676. '/a'
  5677. ));
  5678. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5679. $this->assertTags($result, array(
  5680. 'form' => array(
  5681. 'method' => 'post', 'action' => '/posts/delete/1',
  5682. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5683. ),
  5684. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5685. '/form',
  5686. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5687. 'Delete',
  5688. '/a'
  5689. ));
  5690. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  5691. $this->assertTags($result, array(
  5692. 'form' => array(
  5693. 'method' => 'post', 'action' => '/posts/delete/1',
  5694. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5695. ),
  5696. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5697. '/form',
  5698. '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;/'),
  5699. 'Delete',
  5700. '/a'
  5701. ));
  5702. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5703. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  5704. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  5705. $this->assertTags($result, array(
  5706. 'form' => array(
  5707. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  5708. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5709. ),
  5710. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5711. '/form',
  5712. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5713. 'Delete',
  5714. '/a'
  5715. ));
  5716. $result = $this->Form->postLink(
  5717. '',
  5718. array('controller' => 'items', 'action' => 'delete', 10),
  5719. array('class' => 'btn btn-danger', 'escape' => false),
  5720. 'Confirm thing'
  5721. );
  5722. $this->assertTags($result, array(
  5723. 'form' => array(
  5724. 'method' => 'post', 'action' => '/items/delete/10',
  5725. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5726. ),
  5727. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5728. '/form',
  5729. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5730. '/a'
  5731. ));
  5732. }
  5733. /**
  5734. * Test using postLink with N dimensional data.
  5735. *
  5736. * @return void
  5737. */
  5738. public function testPostLinkNestedData() {
  5739. $data = array(
  5740. 'one' => array(
  5741. 'two' => array(
  5742. 3, 4, 5
  5743. )
  5744. )
  5745. );
  5746. $result = $this->Form->postLink('Send', '/', array('data' => $data));
  5747. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5748. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5749. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5750. }
  5751. /**
  5752. * test creating postLinks after a GET form.
  5753. *
  5754. * @return void
  5755. */
  5756. public function testPostLinkAfterGetForm() {
  5757. $this->Form->request->params['_csrfToken'] = 'testkey';
  5758. $this->Form->request->params['_Token'] = 'val';
  5759. $this->Form->create($this->article, array('type' => 'get'));
  5760. $this->Form->end();
  5761. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5762. $this->assertTags($result, array(
  5763. 'form' => array(
  5764. 'method' => 'post', 'action' => '/posts/delete/1',
  5765. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5766. ),
  5767. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5768. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5769. 'div' => array('style' => 'display:none;'),
  5770. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5771. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5772. '/div',
  5773. '/form',
  5774. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5775. 'Delete',
  5776. '/a'
  5777. ));
  5778. }
  5779. /**
  5780. * Test that postLink adds form tags to view block
  5781. *
  5782. * @return void
  5783. */
  5784. public function testPostLinkFormBuffer() {
  5785. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
  5786. $this->assertTags($result, array(
  5787. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5788. 'Delete',
  5789. '/a'
  5790. ));
  5791. $result = $this->View->fetch('postLink');
  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. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5798. '/form'
  5799. ));
  5800. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  5801. array('inline' => false, 'method' => 'DELETE')
  5802. );
  5803. $this->assertTags($result, array(
  5804. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5805. 'Delete',
  5806. '/a'
  5807. ));
  5808. $result = $this->View->fetch('postLink');
  5809. $this->assertTags($result, array(
  5810. 'form' => array(
  5811. 'method' => 'post', 'action' => '/posts/delete/1',
  5812. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5813. ),
  5814. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5815. '/form',
  5816. array(
  5817. 'form' => array(
  5818. 'method' => 'post', 'action' => '/posts/delete/2',
  5819. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5820. ),
  5821. ),
  5822. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  5823. '/form'
  5824. ));
  5825. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  5826. $this->assertTags($result, array(
  5827. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5828. 'Delete',
  5829. '/a'
  5830. ));
  5831. $result = $this->View->fetch('foobar');
  5832. $this->assertTags($result, array(
  5833. 'form' => array(
  5834. 'method' => 'post', 'action' => '/posts/delete/1',
  5835. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5836. ),
  5837. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5838. '/form'
  5839. ));
  5840. }
  5841. /**
  5842. * testSubmitButton method
  5843. *
  5844. * @return void
  5845. */
  5846. public function testSubmitButton() {
  5847. $result = $this->Form->submit('');
  5848. $expected = array(
  5849. 'div' => array('class' => 'submit'),
  5850. 'input' => array('type' => 'submit', 'value' => ''),
  5851. '/div'
  5852. );
  5853. $this->assertTags($result, $expected);
  5854. $result = $this->Form->submit('Test Submit');
  5855. $expected = array(
  5856. 'div' => array('class' => 'submit'),
  5857. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5858. '/div'
  5859. );
  5860. $this->assertTags($result, $expected);
  5861. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5862. $expected = array(
  5863. 'span' => array('class' => 'submit'),
  5864. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5865. '/span'
  5866. );
  5867. $this->assertTags($result, $expected);
  5868. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5869. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5870. $this->assertTags($result, $expected);
  5871. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5872. $expected = array(
  5873. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5874. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5875. '/div'
  5876. );
  5877. $this->assertTags($result, $expected);
  5878. $result = $this->Form->submit('Next >');
  5879. $expected = array(
  5880. 'div' => array('class' => 'submit'),
  5881. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5882. '/div'
  5883. );
  5884. $this->assertTags($result, $expected);
  5885. $result = $this->Form->submit('Next >', array('escape' => false));
  5886. $expected = array(
  5887. 'div' => array('class' => 'submit'),
  5888. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5889. '/div'
  5890. );
  5891. $this->assertTags($result, $expected);
  5892. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5893. $expected = array(
  5894. 'div' => array('class' => 'submit'),
  5895. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5896. '/div'
  5897. );
  5898. $this->assertTags($result, $expected);
  5899. $before = '--before--';
  5900. $after = '--after--';
  5901. $result = $this->Form->submit('Test', array('before' => $before));
  5902. $expected = array(
  5903. 'div' => array('class' => 'submit'),
  5904. '--before--',
  5905. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5906. '/div'
  5907. );
  5908. $this->assertTags($result, $expected);
  5909. $result = $this->Form->submit('Test', array('after' => $after));
  5910. $expected = array(
  5911. 'div' => array('class' => 'submit'),
  5912. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5913. '--after--',
  5914. '/div'
  5915. );
  5916. $this->assertTags($result, $expected);
  5917. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5918. $expected = array(
  5919. 'div' => array('class' => 'submit'),
  5920. '--before--',
  5921. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5922. '--after--',
  5923. '/div'
  5924. );
  5925. $this->assertTags($result, $expected);
  5926. }
  5927. /**
  5928. * test image submit types.
  5929. *
  5930. * @return void
  5931. */
  5932. public function testSubmitImage() {
  5933. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5934. $expected = array(
  5935. 'div' => array('class' => 'submit'),
  5936. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5937. '/div'
  5938. );
  5939. $this->assertTags($result, $expected);
  5940. $result = $this->Form->submit('/relative/cake.power.gif');
  5941. $expected = array(
  5942. 'div' => array('class' => 'submit'),
  5943. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5944. '/div'
  5945. );
  5946. $this->assertTags($result, $expected);
  5947. $result = $this->Form->submit('cake.power.gif');
  5948. $expected = array(
  5949. 'div' => array('class' => 'submit'),
  5950. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5951. '/div'
  5952. );
  5953. $this->assertTags($result, $expected);
  5954. $result = $this->Form->submit('Not.an.image');
  5955. $expected = array(
  5956. 'div' => array('class' => 'submit'),
  5957. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5958. '/div'
  5959. );
  5960. $this->assertTags($result, $expected);
  5961. $after = '--after--';
  5962. $before = '--before--';
  5963. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5964. $expected = array(
  5965. 'div' => array('class' => 'submit'),
  5966. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5967. '--after--',
  5968. '/div'
  5969. );
  5970. $this->assertTags($result, $expected);
  5971. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5972. $expected = array(
  5973. 'div' => array('class' => 'submit'),
  5974. '--before--',
  5975. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5976. '/div'
  5977. );
  5978. $this->assertTags($result, $expected);
  5979. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5980. $expected = array(
  5981. 'div' => array('class' => 'submit'),
  5982. '--before--',
  5983. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5984. '--after--',
  5985. '/div'
  5986. );
  5987. $this->assertTags($result, $expected);
  5988. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5989. $expected = array(
  5990. 'div' => array('class' => 'submit'),
  5991. '--before--',
  5992. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5993. '--after--',
  5994. '/div'
  5995. );
  5996. $this->assertTags($result, $expected);
  5997. }
  5998. /**
  5999. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6000. * be submitted at a time.
  6001. *
  6002. * @return void
  6003. */
  6004. public function testSubmitUnlockedByDefault() {
  6005. $this->Form->request->params['_Token'] = 'secured';
  6006. $this->Form->submit('Go go');
  6007. $this->Form->submit('Save', array('name' => 'save'));
  6008. $result = $this->Form->unlockField();
  6009. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6010. }
  6011. /**
  6012. * Test submit image with timestamps.
  6013. *
  6014. * @return void
  6015. */
  6016. public function testSubmitImageTimestamp() {
  6017. Configure::write('Asset.timestamp', 'force');
  6018. $result = $this->Form->submit('cake.power.gif');
  6019. $expected = array(
  6020. 'div' => array('class' => 'submit'),
  6021. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6022. '/div'
  6023. );
  6024. $this->assertTags($result, $expected);
  6025. }
  6026. /**
  6027. * test that datetime() works with GET style forms.
  6028. *
  6029. * @return void
  6030. */
  6031. public function testDateTimeWithGetForms() {
  6032. $this->markTestIncomplete('Need to revisit once models work again.');
  6033. extract($this->dateRegex);
  6034. $this->Form->create('Contact', array('type' => 'get'));
  6035. $result = $this->Form->datetime('created');
  6036. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6037. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6038. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6039. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6040. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6041. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6042. }
  6043. /**
  6044. * testEditFormWithData method
  6045. *
  6046. * test auto populating form elements from submitted data.
  6047. *
  6048. * @return void
  6049. */
  6050. public function testEditFormWithData() {
  6051. $this->markTestIncomplete('Need to revisit once models work again.');
  6052. $this->Form->request->data = array('Person' => array(
  6053. 'id' => 1,
  6054. 'first_name' => 'Nate',
  6055. 'last_name' => 'Abele',
  6056. 'email' => 'nate@example.com'
  6057. ));
  6058. $this->Form->request->addParams(array(
  6059. 'models' => array('Person'),
  6060. 'controller' => 'people',
  6061. 'action' => 'add'
  6062. ));
  6063. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6064. $this->Form->create();
  6065. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6066. $expected = array(
  6067. 'input' => array('type' => 'hidden', 'name' => 'People[People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6068. 'select' => array(
  6069. 'name' => 'People[People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6070. ),
  6071. array('option' => array('value' => 1)), 'Nate', '/option',
  6072. array('option' => array('value' => 2)), 'Garrett', '/option',
  6073. array('option' => array('value' => 3)), 'Larry', '/option',
  6074. '/select'
  6075. );
  6076. $this->assertTags($result, $expected);
  6077. }
  6078. /**
  6079. * Test that required fields are created for various types of validation.
  6080. *
  6081. * @return void
  6082. */
  6083. public function testFormInputRequiredDetection() {
  6084. $this->markTestIncomplete('Need to revisit once models work again.');
  6085. $this->Form->create('Contact');
  6086. $result = $this->Form->input('Contact.non_existing');
  6087. $expected = array(
  6088. 'div' => array('class' => 'input text'),
  6089. 'label' => array('for' => 'ContactNonExisting'),
  6090. 'Non Existing',
  6091. '/label',
  6092. 'input' => array(
  6093. 'type' => 'text', 'name' => 'Contact[non_existing]',
  6094. 'id' => 'ContactNonExisting'
  6095. ),
  6096. '/div'
  6097. );
  6098. $this->assertTags($result, $expected);
  6099. $result = $this->Form->input('Contact.imrequired');
  6100. $expected = array(
  6101. 'div' => array('class' => 'input text required'),
  6102. 'label' => array('for' => 'ContactImrequired'),
  6103. 'Imrequired',
  6104. '/label',
  6105. 'input' => array(
  6106. 'type' => 'text', 'name' => 'Contact[imrequired]',
  6107. 'id' => 'ContactImrequired',
  6108. 'required' => 'required'
  6109. ),
  6110. '/div'
  6111. );
  6112. $this->assertTags($result, $expected);
  6113. $result = $this->Form->input('Contact.imalsorequired');
  6114. $expected = array(
  6115. 'div' => array('class' => 'input text required'),
  6116. 'label' => array('for' => 'ContactImalsorequired'),
  6117. 'Imalsorequired',
  6118. '/label',
  6119. 'input' => array(
  6120. 'type' => 'text', 'name' => 'Contact[imalsorequired]',
  6121. 'id' => 'ContactImalsorequired',
  6122. 'required' => 'required'
  6123. ),
  6124. '/div'
  6125. );
  6126. $this->assertTags($result, $expected);
  6127. $result = $this->Form->input('Contact.imrequiredtoo');
  6128. $expected = array(
  6129. 'div' => array('class' => 'input text required'),
  6130. 'label' => array('for' => 'ContactImrequiredtoo'),
  6131. 'Imrequiredtoo',
  6132. '/label',
  6133. 'input' => array(
  6134. 'type' => 'text', 'name' => 'Contact[imrequiredtoo]',
  6135. 'id' => 'ContactImrequiredtoo',
  6136. 'required' => 'required'
  6137. ),
  6138. '/div'
  6139. );
  6140. $this->assertTags($result, $expected);
  6141. $result = $this->Form->input('Contact.required_one');
  6142. $expected = array(
  6143. 'div' => array('class' => 'input text required'),
  6144. 'label' => array('for' => 'ContactRequiredOne'),
  6145. 'Required One',
  6146. '/label',
  6147. 'input' => array(
  6148. 'type' => 'text', 'name' => 'Contact[required_one]',
  6149. 'id' => 'ContactRequiredOne',
  6150. 'required' => 'required'
  6151. ),
  6152. '/div'
  6153. );
  6154. $this->assertTags($result, $expected);
  6155. $result = $this->Form->input('Contact.string_required');
  6156. $expected = array(
  6157. 'div' => array('class' => 'input text required'),
  6158. 'label' => array('for' => 'ContactStringRequired'),
  6159. 'String Required',
  6160. '/label',
  6161. 'input' => array(
  6162. 'type' => 'text', 'name' => 'Contact[string_required]',
  6163. 'id' => 'ContactStringRequired',
  6164. 'required' => 'required'
  6165. ),
  6166. '/div'
  6167. );
  6168. $this->assertTags($result, $expected);
  6169. $result = $this->Form->input('Contact.imnotrequired');
  6170. $expected = array(
  6171. 'div' => array('class' => 'input text'),
  6172. 'label' => array('for' => 'ContactImnotrequired'),
  6173. 'Imnotrequired',
  6174. '/label',
  6175. 'input' => array(
  6176. 'type' => 'text', 'name' => 'Contact[imnotrequired]',
  6177. 'id' => 'ContactImnotrequired'
  6178. ),
  6179. '/div'
  6180. );
  6181. $this->assertTags($result, $expected);
  6182. $result = $this->Form->input('Contact.imalsonotrequired');
  6183. $expected = array(
  6184. 'div' => array('class' => 'input text'),
  6185. 'label' => array('for' => 'ContactImalsonotrequired'),
  6186. 'Imalsonotrequired',
  6187. '/label',
  6188. 'input' => array(
  6189. 'type' => 'text', 'name' => 'Contact[imalsonotrequired]',
  6190. 'id' => 'ContactImalsonotrequired'
  6191. ),
  6192. '/div'
  6193. );
  6194. $this->assertTags($result, $expected);
  6195. $result = $this->Form->input('Contact.imalsonotrequired2');
  6196. $expected = array(
  6197. 'div' => array('class' => 'input text'),
  6198. 'label' => array('for' => 'ContactImalsonotrequired2'),
  6199. 'Imalsonotrequired2',
  6200. '/label',
  6201. 'input' => array(
  6202. 'type' => 'text', 'name' => 'Contact[imalsonotrequired2]',
  6203. 'id' => 'ContactImalsonotrequired2'
  6204. ),
  6205. '/div'
  6206. );
  6207. $this->assertTags($result, $expected);
  6208. $result = $this->Form->input('Contact.imnotrequiredeither');
  6209. $expected = array(
  6210. 'div' => array('class' => 'input text'),
  6211. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6212. 'Imnotrequiredeither',
  6213. '/label',
  6214. 'input' => array(
  6215. 'type' => 'text', 'name' => 'Contact[imnotrequiredeither]',
  6216. 'id' => 'ContactImnotrequiredeither'
  6217. ),
  6218. '/div'
  6219. );
  6220. $this->assertTags($result, $expected);
  6221. $result = $this->Form->input('Contact.iamrequiredalways');
  6222. $expected = array(
  6223. 'div' => array('class' => 'input text required'),
  6224. 'label' => array('for' => 'ContactIamrequiredalways'),
  6225. 'Iamrequiredalways',
  6226. '/label',
  6227. 'input' => array(
  6228. 'type' => 'text', 'name' => 'Contact[iamrequiredalways]',
  6229. 'id' => 'ContactIamrequiredalways',
  6230. 'required' => 'required'
  6231. ),
  6232. '/div'
  6233. );
  6234. $this->assertTags($result, $expected);
  6235. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  6236. $expected = array(
  6237. 'div' => array('class' => 'input checkbox required'),
  6238. array('input' => array(
  6239. 'type' => 'hidden',
  6240. 'name' => 'Contact[boolean_field]',
  6241. 'id' => 'ContactBooleanField_',
  6242. 'value' => '0'
  6243. )),
  6244. array('input' => array(
  6245. 'type' => 'checkbox',
  6246. 'name' => 'Contact[boolean_field]',
  6247. 'value' => '1',
  6248. 'id' => 'ContactBooleanField'
  6249. )),
  6250. 'label' => array('for' => 'ContactBooleanField'),
  6251. 'Boolean Field',
  6252. '/label',
  6253. '/div'
  6254. );
  6255. $this->assertTags($result, $expected);
  6256. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  6257. $expected = array(
  6258. 'div' => array('class' => 'input checkbox required'),
  6259. array('input' => array(
  6260. 'type' => 'hidden',
  6261. 'name' => 'Contact[boolean_field]',
  6262. 'id' => 'ContactBooleanField_',
  6263. 'value' => '0'
  6264. )),
  6265. array('input' => array(
  6266. 'type' => 'checkbox',
  6267. 'name' => 'Contact[boolean_field]',
  6268. 'value' => '1',
  6269. 'id' => 'ContactBooleanField',
  6270. 'required' => 'required'
  6271. )),
  6272. 'label' => array('for' => 'ContactBooleanField'),
  6273. 'Boolean Field',
  6274. '/label',
  6275. '/div'
  6276. );
  6277. $this->assertTags($result, $expected);
  6278. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  6279. $expected = array(
  6280. 'div' => array('class' => 'input file required'),
  6281. 'label' => array('for' => 'ContactIamrequiredalways'),
  6282. 'Iamrequiredalways',
  6283. '/label',
  6284. 'input' => array(
  6285. 'type' => 'file',
  6286. 'name' => 'data[Contact][iamrequiredalways]',
  6287. 'id' => 'ContactIamrequiredalways',
  6288. 'required' => 'required'
  6289. ),
  6290. '/div'
  6291. );
  6292. $this->assertTags($result, $expected);
  6293. }
  6294. /**
  6295. * Test that required fields are created when only using ModelValidator::add().
  6296. *
  6297. * @return void
  6298. */
  6299. public function testFormInputRequiredDetectionModelValidator() {
  6300. $this->markTestIncomplete('Need to revisit once models work again.');
  6301. $this->Form->create('ContactTag');
  6302. $result = $this->Form->input('ContactTag.iwillberequired');
  6303. $expected = array(
  6304. 'div' => array('class' => 'input text required'),
  6305. 'label' => array('for' => 'ContactTagIwillberequired'),
  6306. 'Iwillberequired',
  6307. '/label',
  6308. 'input' => array(
  6309. 'name' => 'data[ContactTag][iwillberequired]',
  6310. 'type' => 'text',
  6311. 'id' => 'ContactTagIwillberequired',
  6312. 'required' => 'required'
  6313. ),
  6314. '/div'
  6315. );
  6316. $this->assertTags($result, $expected);
  6317. }
  6318. /**
  6319. * testFormMagicInput method
  6320. *
  6321. * @return void
  6322. */
  6323. public function testFormMagicInput() {
  6324. $this->markTestIncomplete('Need to revisit once models work again.');
  6325. $encoding = strtolower(Configure::read('App.encoding'));
  6326. $result = $this->Form->create('Contact');
  6327. $expected = array(
  6328. 'form' => array(
  6329. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6330. 'accept-charset' => $encoding
  6331. ),
  6332. 'div' => array('style' => 'display:none;'),
  6333. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6334. '/div'
  6335. );
  6336. $this->assertTags($result, $expected);
  6337. $result = $this->Form->input('name');
  6338. $expected = array(
  6339. 'div' => array('class' => 'input text'),
  6340. 'label' => array('for' => 'ContactName'),
  6341. 'Name',
  6342. '/label',
  6343. 'input' => array(
  6344. 'type' => 'text', 'name' => 'Contact[name]',
  6345. 'id' => 'ContactName', 'maxlength' => '255'
  6346. ),
  6347. '/div'
  6348. );
  6349. $this->assertTags($result, $expected);
  6350. $result = $this->Form->input('non_existing_field_in_contact_model');
  6351. $expected = array(
  6352. 'div' => array('class' => 'input text'),
  6353. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6354. 'Non Existing Field In Contact Model',
  6355. '/label',
  6356. 'input' => array(
  6357. 'type' => 'text', 'name' => 'Contact[non_existing_field_in_contact_model]',
  6358. 'id' => 'ContactNonExistingFieldInContactModel'
  6359. ),
  6360. '/div'
  6361. );
  6362. $this->assertTags($result, $expected);
  6363. $result = $this->Form->input('Address.street');
  6364. $expected = array(
  6365. 'div' => array('class' => 'input text'),
  6366. 'label' => array('for' => 'AddressStreet'),
  6367. 'Street',
  6368. '/label',
  6369. 'input' => array(
  6370. 'type' => 'text', 'name' => 'Address[street]',
  6371. 'id' => 'AddressStreet'
  6372. ),
  6373. '/div'
  6374. );
  6375. $this->assertTags($result, $expected);
  6376. $result = $this->Form->input('Address.non_existing_field_in_model');
  6377. $expected = array(
  6378. 'div' => array('class' => 'input text'),
  6379. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6380. 'Non Existing Field In Model',
  6381. '/label',
  6382. 'input' => array(
  6383. 'type' => 'text', 'name' => 'Address[non_existing_field_in_model]',
  6384. 'id' => 'AddressNonExistingFieldInModel'
  6385. ),
  6386. '/div'
  6387. );
  6388. $this->assertTags($result, $expected);
  6389. $result = $this->Form->input('name', array('div' => false));
  6390. $expected = array(
  6391. 'label' => array('for' => 'ContactName'),
  6392. 'Name',
  6393. '/label',
  6394. 'input' => array(
  6395. 'type' => 'text', 'name' => 'Contact[name]',
  6396. 'id' => 'ContactName', 'maxlength' => '255'
  6397. )
  6398. );
  6399. $this->assertTags($result, $expected);
  6400. extract($this->dateRegex);
  6401. $now = strtotime('now');
  6402. $result = $this->Form->input('Contact.published', array('div' => false));
  6403. $expected = array(
  6404. 'label' => array('for' => 'ContactPublishedMonth'),
  6405. 'Published',
  6406. '/label',
  6407. array('select' => array(
  6408. 'name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth'
  6409. )),
  6410. $monthsRegex,
  6411. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6412. date('F', $now),
  6413. '/option',
  6414. '*/select',
  6415. '-',
  6416. array('select' => array(
  6417. 'name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay'
  6418. )),
  6419. $daysRegex,
  6420. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6421. date('j', $now),
  6422. '/option',
  6423. '*/select',
  6424. '-',
  6425. array('select' => array(
  6426. 'name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear'
  6427. )),
  6428. $yearsRegex,
  6429. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6430. date('Y', $now),
  6431. '*/select'
  6432. );
  6433. $this->assertTags($result, $expected);
  6434. $result = $this->Form->input('Contact.updated', array('div' => false));
  6435. $expected = array(
  6436. 'label' => array('for' => 'ContactUpdatedMonth'),
  6437. 'Updated',
  6438. '/label',
  6439. array('select' => array(
  6440. 'name' => 'Contact[updated][month]', 'id' => 'ContactUpdatedMonth'
  6441. )),
  6442. $monthsRegex,
  6443. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6444. date('F', $now),
  6445. '/option',
  6446. '*/select',
  6447. '-',
  6448. array('select' => array(
  6449. 'name' => 'Contact[updated][day]', 'id' => 'ContactUpdatedDay'
  6450. )),
  6451. $daysRegex,
  6452. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6453. date('j', $now),
  6454. '/option',
  6455. '*/select',
  6456. '-',
  6457. array('select' => array(
  6458. 'name' => 'Contact[updated][year]', 'id' => 'ContactUpdatedYear'
  6459. )),
  6460. $yearsRegex,
  6461. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6462. date('Y', $now),
  6463. '*/select'
  6464. );
  6465. $this->assertTags($result, $expected);
  6466. $result = $this->Form->input('UserForm.stuff');
  6467. $expected = array(
  6468. 'div' => array('class' => 'input text'),
  6469. 'label' => array('for' => 'UserFormStuff'),
  6470. 'Stuff',
  6471. '/label',
  6472. 'input' => array(
  6473. 'type' => 'text', 'name' => 'UserForm[stuff]',
  6474. 'id' => 'UserFormStuff', 'maxlength' => 10
  6475. ),
  6476. '/div'
  6477. );
  6478. $this->assertTags($result, $expected);
  6479. }
  6480. /**
  6481. * testForMagicInputNonExistingNorValidated method
  6482. *
  6483. * @return void
  6484. */
  6485. public function testForMagicInputNonExistingNorValidated() {
  6486. $this->markTestIncomplete('Need to revisit once models work again.');
  6487. $encoding = strtolower(Configure::read('App.encoding'));
  6488. $result = $this->Form->create('Contact');
  6489. $expected = array(
  6490. 'form' => array(
  6491. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6492. 'accept-charset' => $encoding
  6493. ),
  6494. 'div' => array('style' => 'display:none;'),
  6495. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6496. '/div'
  6497. );
  6498. $this->assertTags($result, $expected);
  6499. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6500. $expected = array(
  6501. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6502. 'Non Existing Nor Validated',
  6503. '/label',
  6504. 'input' => array(
  6505. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  6506. 'id' => 'ContactNonExistingNorValidated'
  6507. )
  6508. );
  6509. $this->assertTags($result, $expected);
  6510. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6511. 'div' => false, 'value' => 'my value'
  6512. ));
  6513. $expected = array(
  6514. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6515. 'Non Existing Nor Validated',
  6516. '/label',
  6517. 'input' => array(
  6518. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  6519. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6520. )
  6521. );
  6522. $this->assertTags($result, $expected);
  6523. $this->Form->request->data = array(
  6524. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6525. ));
  6526. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6527. $expected = array(
  6528. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6529. 'Non Existing Nor Validated',
  6530. '/label',
  6531. 'input' => array(
  6532. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  6533. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6534. )
  6535. );
  6536. $this->assertTags($result, $expected);
  6537. }
  6538. /**
  6539. * testFormMagicInputLabel method
  6540. *
  6541. * @return void
  6542. */
  6543. public function testFormMagicInputLabel() {
  6544. $this->markTestIncomplete('Need to revisit once models work again.');
  6545. $encoding = strtolower(Configure::read('App.encoding'));
  6546. $result = $this->Form->create('Contact');
  6547. $expected = array(
  6548. 'form' => array(
  6549. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6550. 'accept-charset' => $encoding
  6551. ),
  6552. 'div' => array('style' => 'display:none;'),
  6553. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6554. '/div'
  6555. );
  6556. $this->assertTags($result, $expected);
  6557. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6558. $this->assertTags($result, array('input' => array(
  6559. 'name' => 'Contact[name]', 'type' => 'text',
  6560. 'id' => 'ContactName', 'maxlength' => '255')
  6561. ));
  6562. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6563. $expected = array(
  6564. 'label' => array('for' => 'ContactName'),
  6565. 'My label',
  6566. '/label',
  6567. 'input' => array(
  6568. 'type' => 'text', 'name' => 'Contact[name]',
  6569. 'id' => 'ContactName', 'maxlength' => '255'
  6570. )
  6571. );
  6572. $this->assertTags($result, $expected);
  6573. $result = $this->Form->input('Contact.name', array(
  6574. 'div' => false, 'label' => array('class' => 'mandatory')
  6575. ));
  6576. $expected = array(
  6577. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6578. 'Name',
  6579. '/label',
  6580. 'input' => array(
  6581. 'type' => 'text', 'name' => 'Contact[name]',
  6582. 'id' => 'ContactName', 'maxlength' => '255'
  6583. )
  6584. );
  6585. $this->assertTags($result, $expected);
  6586. $result = $this->Form->input('Contact.name', array(
  6587. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6588. ));
  6589. $expected = array(
  6590. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6591. 'My label',
  6592. '/label',
  6593. 'input' => array(
  6594. 'type' => 'text', 'name' => 'Contact[name]',
  6595. 'id' => 'ContactName', 'maxlength' => '255'
  6596. )
  6597. );
  6598. $this->assertTags($result, $expected);
  6599. $result = $this->Form->input('Contact.name', array(
  6600. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6601. ));
  6602. $expected = array(
  6603. 'label' => array('for' => 'my_id'),
  6604. 'Name',
  6605. '/label',
  6606. 'input' => array(
  6607. 'type' => 'text', 'name' => 'Contact[name]',
  6608. 'id' => 'my_id', 'maxlength' => '255'
  6609. )
  6610. );
  6611. $this->assertTags($result, $expected);
  6612. $result = $this->Form->input('1.id');
  6613. $this->assertTags($result, array('input' => array(
  6614. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  6615. 'id' => 'Contact1Id'
  6616. )));
  6617. $result = $this->Form->input("1.name");
  6618. $expected = array(
  6619. 'div' => array('class' => 'input text'),
  6620. 'label' => array('for' => 'Contact1Name'),
  6621. 'Name',
  6622. '/label',
  6623. 'input' => array(
  6624. 'type' => 'text', 'name' => 'Contact[1][name]',
  6625. 'id' => 'Contact1Name', 'maxlength' => '255'
  6626. ),
  6627. '/div'
  6628. );
  6629. $this->assertTags($result, $expected);
  6630. $result = $this->Form->input('Contact.1.id');
  6631. $this->assertTags($result, array(
  6632. 'input' => array(
  6633. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  6634. 'id' => 'Contact1Id'
  6635. )
  6636. ));
  6637. $result = $this->Form->input("Model.1.name");
  6638. $expected = array(
  6639. 'div' => array('class' => 'input text'),
  6640. 'label' => array('for' => 'Model1Name'),
  6641. 'Name',
  6642. '/label',
  6643. 'input' => array(
  6644. 'type' => 'text', 'name' => 'Model[1][name]',
  6645. 'id' => 'Model1Name'
  6646. ),
  6647. '/div'
  6648. );
  6649. $this->assertTags($result, $expected);
  6650. }
  6651. /**
  6652. * testFormEnd method
  6653. *
  6654. * @return void
  6655. */
  6656. public function testFormEnd() {
  6657. $this->assertEquals('</form>', $this->Form->end());
  6658. }
  6659. /**
  6660. * testMultipleFormWithIdFields method
  6661. *
  6662. * @return void
  6663. */
  6664. public function testMultipleFormWithIdFields() {
  6665. $this->markTestIncomplete('Need to revisit once models work again.');
  6666. $this->Form->create('UserForm');
  6667. $result = $this->Form->input('id');
  6668. $this->assertTags($result, array('input' => array(
  6669. 'type' => 'hidden', 'name' => 'UserForm[id]', 'id' => 'UserFormId'
  6670. )));
  6671. $result = $this->Form->input('ValidateItem.id');
  6672. $this->assertTags($result, array('input' => array(
  6673. 'type' => 'hidden', 'name' => 'ValidateItem[id]',
  6674. 'id' => 'ValidateItemId'
  6675. )));
  6676. $result = $this->Form->input('ValidateUser.id');
  6677. $this->assertTags($result, array('input' => array(
  6678. 'type' => 'hidden', 'name' => 'ValidateUser[id]',
  6679. 'id' => 'ValidateUserId'
  6680. )));
  6681. }
  6682. /**
  6683. * testDbLessModel method
  6684. *
  6685. * @return void
  6686. */
  6687. public function testDbLessModel() {
  6688. $this->markTestIncomplete('Need to revisit once models work again.');
  6689. $this->Form->create('TestMail');
  6690. $result = $this->Form->input('name');
  6691. $expected = array(
  6692. 'div' => array('class' => 'input text'),
  6693. 'label' => array('for' => 'TestMailName'),
  6694. 'Name',
  6695. '/label',
  6696. 'input' => array(
  6697. 'name' => 'TestMail[name]', 'type' => 'text',
  6698. 'id' => 'TestMailName'
  6699. ),
  6700. '/div'
  6701. );
  6702. $this->assertTags($result, $expected);
  6703. $this->Form->create('TestMail');
  6704. $result = $this->Form->input('name');
  6705. $expected = array(
  6706. 'div' => array('class' => 'input text'),
  6707. 'label' => array('for' => 'TestMailName'),
  6708. 'Name',
  6709. '/label',
  6710. 'input' => array(
  6711. 'name' => 'TestMail[name]', 'type' => 'text',
  6712. 'id' => 'TestMailName'
  6713. ),
  6714. '/div'
  6715. );
  6716. $this->assertTags($result, $expected);
  6717. }
  6718. /**
  6719. * testBrokenness method
  6720. *
  6721. * @return void
  6722. */
  6723. public function testBrokenness() {
  6724. $this->markTestIncomplete('Need to revisit once models work again.');
  6725. /*
  6726. * #4 This test has two parents and four children. By default (as of r7117) both
  6727. * parents are show but the first parent is missing a child. This is the inconsistency
  6728. * in the default behaviour - one parent has all children, the other does not - dependent
  6729. * on the data values.
  6730. */
  6731. $result = $this->Form->select('Model.field', array(
  6732. 'Fred' => array(
  6733. 'freds_son_1' => 'Fred',
  6734. 'freds_son_2' => 'Freddie'
  6735. ),
  6736. 'Bert' => array(
  6737. 'berts_son_1' => 'Albert',
  6738. 'berts_son_2' => 'Bertie')
  6739. ),
  6740. array('showParents' => true, 'empty' => false)
  6741. );
  6742. $expected = array(
  6743. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  6744. array('optgroup' => array('label' => 'Fred')),
  6745. array('option' => array('value' => 'freds_son_1')),
  6746. 'Fred',
  6747. '/option',
  6748. array('option' => array('value' => 'freds_son_2')),
  6749. 'Freddie',
  6750. '/option',
  6751. '/optgroup',
  6752. array('optgroup' => array('label' => 'Bert')),
  6753. array('option' => array('value' => 'berts_son_1')),
  6754. 'Albert',
  6755. '/option',
  6756. array('option' => array('value' => 'berts_son_2')),
  6757. 'Bertie',
  6758. '/option',
  6759. '/optgroup',
  6760. '/select'
  6761. );
  6762. $this->assertTags($result, $expected);
  6763. /*
  6764. * #2 This is structurally identical to the test above (#1) - only the parent name has
  6765. * changed, so we should expect the same select list data, just with a different name
  6766. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  6767. * This is where data corruption can occur, because when a select value is missing from
  6768. * a list a form will substitute the first value in the list - without the user knowing.
  6769. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  6770. * affect the availability of 3 => 'Three' as a valid option.
  6771. */
  6772. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  6773. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  6774. ));
  6775. $result = $this->Form->select(
  6776. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  6777. );
  6778. $expected = array(
  6779. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  6780. array('option' => array('value' => 1)),
  6781. 'One',
  6782. '/option',
  6783. array('option' => array('value' => 2)),
  6784. 'Two',
  6785. '/option',
  6786. array('optgroup' => array('label' => 'Three')),
  6787. array('option' => array('value' => 3)),
  6788. 'Three',
  6789. '/option',
  6790. array('option' => array('value' => 4)),
  6791. 'Four',
  6792. '/option',
  6793. array('option' => array('value' => 5)),
  6794. 'Five',
  6795. '/option',
  6796. '/optgroup',
  6797. '/select'
  6798. );
  6799. $this->assertTags($result, $expected);
  6800. }
  6801. /**
  6802. * Test the generation of fields for a multi record form.
  6803. *
  6804. * @return void
  6805. */
  6806. public function testMultiRecordForm() {
  6807. $this->markTestIncomplete('Need to revisit once models work again.');
  6808. $this->Form->create('ValidateProfile');
  6809. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  6810. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  6811. $expected = array(
  6812. 'div' => array('class' => 'input textarea'),
  6813. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  6814. 'Name',
  6815. '/label',
  6816. 'textarea' => array(
  6817. 'id' => 'ValidateProfile1ValidateItem2Name',
  6818. 'name' => 'ValidateProfile[1][ValidateItem][2][name]',
  6819. 'cols' => 30,
  6820. 'rows' => 6
  6821. ),
  6822. 'Value',
  6823. '/textarea',
  6824. '/div'
  6825. );
  6826. $this->assertTags($result, $expected);
  6827. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  6828. $expected = array(
  6829. 'div' => array('class' => 'input date'),
  6830. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  6831. 'Created',
  6832. '/label',
  6833. array('select' => array(
  6834. 'name' => 'ValidateProfile[1][ValidateItem][2][created][month]',
  6835. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  6836. )
  6837. ),
  6838. array('option' => array('value' => '')), '/option',
  6839. $this->dateRegex['monthsRegex'],
  6840. '/select', '-',
  6841. array('select' => array(
  6842. 'name' => 'ValidateProfile[1][ValidateItem][2][created][day]',
  6843. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  6844. )
  6845. ),
  6846. array('option' => array('value' => '')), '/option',
  6847. $this->dateRegex['daysRegex'],
  6848. '/select', '-',
  6849. array('select' => array(
  6850. 'name' => 'ValidateProfile[1][ValidateItem][2][created][year]',
  6851. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  6852. )
  6853. ),
  6854. array('option' => array('value' => '')), '/option',
  6855. $this->dateRegex['yearsRegex'],
  6856. '/select',
  6857. '/div'
  6858. );
  6859. $this->assertTags($result, $expected);
  6860. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  6861. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  6862. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  6863. $expected = array(
  6864. 'div' => array('class' => 'input select error'),
  6865. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  6866. 'Profile',
  6867. '/label',
  6868. 'select' => array(
  6869. 'name' => 'ValidateProfile[1][ValidateItem][2][profile_id]',
  6870. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  6871. 'class' => 'form-error'
  6872. ),
  6873. '/select',
  6874. array('div' => array('class' => 'error-message')),
  6875. 'Error',
  6876. '/div',
  6877. '/div'
  6878. );
  6879. $this->assertTags($result, $expected);
  6880. }
  6881. /**
  6882. * test the correct display of multi-record form validation errors.
  6883. *
  6884. * @return void
  6885. */
  6886. public function testMultiRecordFormValidationErrors() {
  6887. $this->markTestIncomplete('Need to revisit once models work again.');
  6888. $this->Form->create('ValidateProfile');
  6889. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  6890. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  6891. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  6892. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  6893. $result = $this->Form->error('ValidateProfile.2.city');
  6894. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6895. $result = $this->Form->error('2.city');
  6896. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6897. }
  6898. /**
  6899. * test the correct display of multi-record form validation errors.
  6900. *
  6901. * @return void
  6902. */
  6903. public function testSaveManyRecordFormValidationErrors() {
  6904. $this->markTestIncomplete('Need to revisit once models work again.');
  6905. $this->Form->create('ValidateUser');
  6906. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  6907. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  6908. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  6909. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  6910. $result = $this->Form->error('ValidateUser.0.city');
  6911. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6912. }
  6913. /**
  6914. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  6915. *
  6916. * @return void
  6917. */
  6918. public function testInputTemplate() {
  6919. $this->markTestIncomplete('Need to revisit once models work again.');
  6920. $result = $this->Form->input('Contact.email', array(
  6921. 'type' => 'text', 'format' => array('input')
  6922. ));
  6923. $expected = array(
  6924. 'div' => array('class' => 'input text'),
  6925. 'input' => array(
  6926. 'type' => 'text', 'name' => 'Contact[email]',
  6927. 'id' => 'ContactEmail'
  6928. ),
  6929. '/div'
  6930. );
  6931. $this->assertTags($result, $expected);
  6932. $result = $this->Form->input('Contact.email', array(
  6933. 'type' => 'text', 'format' => array('input', 'label'),
  6934. 'label' => '<em>Email (required)</em>'
  6935. ));
  6936. $expected = array(
  6937. 'div' => array('class' => 'input text'),
  6938. array('input' => array(
  6939. 'type' => 'text', 'name' => 'Contact[email]',
  6940. 'id' => 'ContactEmail'
  6941. )),
  6942. 'label' => array('for' => 'ContactEmail'),
  6943. 'em' => array(),
  6944. 'Email (required)',
  6945. '/em',
  6946. '/label',
  6947. '/div'
  6948. );
  6949. $this->assertTags($result, $expected);
  6950. $result = $this->Form->input('Contact.email', array(
  6951. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  6952. 'between' => '<div>Something in the middle</div>',
  6953. 'after' => '<span>Some text at the end</span>'
  6954. ));
  6955. $expected = array(
  6956. 'div' => array('class' => 'input text'),
  6957. array('input' => array(
  6958. 'type' => 'text', 'name' => 'Contact[email]',
  6959. 'id' => 'ContactEmail'
  6960. )),
  6961. array('div' => array()),
  6962. 'Something in the middle',
  6963. '/div',
  6964. 'label' => array('for' => 'ContactEmail'),
  6965. 'Email',
  6966. '/label',
  6967. 'span' => array(),
  6968. 'Some text at the end',
  6969. '/span',
  6970. '/div'
  6971. );
  6972. $this->assertTags($result, $expected);
  6973. $result = $this->Form->input('Contact.method', array(
  6974. 'type' => 'radio',
  6975. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  6976. 'between' => 'I am between',
  6977. ));
  6978. $expected = array(
  6979. 'div' => array('class' => 'input radio'),
  6980. 'fieldset' => array(),
  6981. 'legend' => array(),
  6982. 'Method',
  6983. '/legend',
  6984. 'I am between',
  6985. 'input' => array(
  6986. 'type' => 'hidden', 'name' => 'Contact[method]',
  6987. 'value' => '', 'id' => 'ContactMethod_'
  6988. ),
  6989. array('input' => array(
  6990. 'type' => 'radio', 'name' => 'Contact[method]',
  6991. 'value' => 'email', 'id' => 'ContactMethodEmail'
  6992. )),
  6993. array('label' => array('for' => 'ContactMethodEmail')),
  6994. 'Email',
  6995. '/label',
  6996. array('input' => array(
  6997. 'type' => 'radio', 'name' => 'Contact[method]',
  6998. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  6999. )),
  7000. array('label' => array('for' => 'ContactMethodPigeon')),
  7001. 'Pigeon',
  7002. '/label',
  7003. '/fieldset',
  7004. '/div',
  7005. );
  7006. $this->assertTags($result, $expected);
  7007. }
  7008. /**
  7009. * test that some html5 inputs + FormHelper::__call() work
  7010. *
  7011. * @return void
  7012. */
  7013. public function testHtml5Inputs() {
  7014. $result = $this->Form->email('User.email');
  7015. $expected = array(
  7016. 'input' => array('type' => 'email', 'name' => 'User[email]')
  7017. );
  7018. $this->assertTags($result, $expected);
  7019. $result = $this->Form->search('User.query');
  7020. $expected = array(
  7021. 'input' => array('type' => 'search', 'name' => 'User[query]')
  7022. );
  7023. $this->assertTags($result, $expected);
  7024. $result = $this->Form->search('User.query', array('value' => 'test'));
  7025. $expected = array(
  7026. 'input' => array('type' => 'search', 'name' => 'User[query]', 'value' => 'test')
  7027. );
  7028. $this->assertTags($result, $expected);
  7029. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7030. $expected = array(
  7031. 'input' => array('type' => 'text', 'name' => 'User[query]', 'value' => 'test')
  7032. );
  7033. $this->assertTags($result, $expected);
  7034. }
  7035. /**
  7036. * Test accessing html5 inputs through input().
  7037. *
  7038. * @return void
  7039. */
  7040. public function testHtml5InputWithInput() {
  7041. $this->markTestIncomplete('Need to revisit once models work again.');
  7042. $result = $this->Form->input('User.website', array(
  7043. 'type' => 'url',
  7044. 'value' => 'http://domain.tld',
  7045. 'div' => false,
  7046. 'label' => false));
  7047. $expected = array(
  7048. 'input' => array('type' => 'url', 'name' => 'User[website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7049. );
  7050. $this->assertTags($result, $expected);
  7051. }
  7052. /**
  7053. * Test errors when field name is missing.
  7054. *
  7055. * @expectedException \Cake\Error\Exception
  7056. * @return void
  7057. */
  7058. public function testHtml5InputException() {
  7059. $this->Form->email();
  7060. }
  7061. /**
  7062. * Tests that a model can be loaded from the model names passed in the request object
  7063. *
  7064. * @return void
  7065. */
  7066. public function testIntrospectModelFromRequest() {
  7067. $this->markTestIncomplete('Need to revisit once models work again.');
  7068. $this->loadFixtures('Post');
  7069. App::build(array(
  7070. 'Plugin' => array(CAKE . 'Test/TestApp/Plugin/')
  7071. ));
  7072. Plugin::load('TestPlugin');
  7073. $this->Form->request['models'] = array(
  7074. 'TestPluginPost' => array(
  7075. 'plugin' => 'TestPlugin',
  7076. 'className' => 'TestPluginPost'
  7077. )
  7078. );
  7079. $this->Form->create('TestPluginPost');
  7080. Plugin::unload();
  7081. }
  7082. /**
  7083. * Tests that it is possible to set the validation errors directly in the helper for a field
  7084. *
  7085. * @return void
  7086. */
  7087. public function testCustomValidationErrors() {
  7088. $this->markTestIncomplete('Need to revisit once models work again.');
  7089. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7090. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7091. $this->assertEquals('Badness!', $result);
  7092. }
  7093. /**
  7094. * Tests that the 'on' key validates as expected on create
  7095. *
  7096. * @return void
  7097. */
  7098. public function testRequiredOnCreate() {
  7099. $this->markTestIncomplete('Need to revisit once models work again.');
  7100. $this->Form->create('Contact');
  7101. $result = $this->Form->input('Contact.imrequiredonupdate');
  7102. $expected = array(
  7103. 'div' => array('class' => 'input text'),
  7104. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7105. 'Imrequiredonupdate',
  7106. '/label',
  7107. 'input' => array(
  7108. 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
  7109. 'id' => 'ContactImrequiredonupdate'
  7110. ),
  7111. '/div'
  7112. );
  7113. $this->assertTags($result, $expected);
  7114. $result = $this->Form->input('Contact.imrequiredoncreate');
  7115. $expected = array(
  7116. 'div' => array('class' => 'input text required'),
  7117. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7118. 'Imrequiredoncreate',
  7119. '/label',
  7120. 'input' => array(
  7121. 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
  7122. 'id' => 'ContactImrequiredoncreate',
  7123. 'required' => 'required'
  7124. ),
  7125. '/div'
  7126. );
  7127. $this->assertTags($result, $expected);
  7128. $result = $this->Form->input('Contact.imrequiredonboth');
  7129. $expected = array(
  7130. 'div' => array('class' => 'input text required'),
  7131. 'label' => array('for' => 'ContactImrequiredonboth'),
  7132. 'Imrequiredonboth',
  7133. '/label',
  7134. 'input' => array(
  7135. 'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
  7136. 'id' => 'ContactImrequiredonboth',
  7137. 'required' => 'required'
  7138. ),
  7139. '/div'
  7140. );
  7141. $this->assertTags($result, $expected);
  7142. $this->Form->inputDefaults(array('required' => false));
  7143. $result = $this->Form->input('Contact.imrequired');
  7144. $expected = array(
  7145. 'div' => array('class' => 'input text'),
  7146. 'label' => array('for' => 'ContactImrequired'),
  7147. 'Imrequired',
  7148. '/label',
  7149. 'input' => array(
  7150. 'type' => 'text', 'name' => 'Contact[imrequired]',
  7151. 'id' => 'ContactImrequired'
  7152. ),
  7153. '/div'
  7154. );
  7155. $this->assertTags($result, $expected);
  7156. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  7157. $this->assertTags($result, $expected);
  7158. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  7159. $expected = array(
  7160. 'div' => array('class' => 'input text required'),
  7161. 'label' => array('for' => 'ContactImrequired'),
  7162. 'Imrequired',
  7163. '/label',
  7164. 'input' => array(
  7165. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7166. 'id' => 'ContactImrequired'
  7167. ),
  7168. '/div'
  7169. );
  7170. $this->assertTags($result, $expected);
  7171. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  7172. $this->assertTags($result, $expected);
  7173. }
  7174. /**
  7175. * Tests that the 'on' key validates as expected on update
  7176. *
  7177. * @return void
  7178. */
  7179. public function testRequiredOnUpdate() {
  7180. $this->markTestIncomplete('Need to revisit once models work again.');
  7181. $this->Form->request->data['Contact']['id'] = 1;
  7182. $this->Form->create('Contact');
  7183. $result = $this->Form->input('Contact.imrequiredonupdate');
  7184. $expected = array(
  7185. 'div' => array('class' => 'input text required'),
  7186. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7187. 'Imrequiredonupdate',
  7188. '/label',
  7189. 'input' => array(
  7190. 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
  7191. 'id' => 'ContactImrequiredonupdate',
  7192. 'required' => 'required'
  7193. ),
  7194. '/div'
  7195. );
  7196. $this->assertTags($result, $expected);
  7197. $result = $this->Form->input('Contact.imrequiredoncreate');
  7198. $expected = array(
  7199. 'div' => array('class' => 'input text'),
  7200. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7201. 'Imrequiredoncreate',
  7202. '/label',
  7203. 'input' => array(
  7204. 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
  7205. 'id' => 'ContactImrequiredoncreate'
  7206. ),
  7207. '/div'
  7208. );
  7209. $this->assertTags($result, $expected);
  7210. $result = $this->Form->input('Contact.imrequiredonboth');
  7211. $expected = array(
  7212. 'div' => array('class' => 'input text required'),
  7213. 'label' => array('for' => 'ContactImrequiredonboth'),
  7214. 'Imrequiredonboth',
  7215. '/label',
  7216. 'input' => array(
  7217. 'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
  7218. 'id' => 'ContactImrequiredonboth',
  7219. 'required' => 'required'
  7220. ),
  7221. '/div'
  7222. );
  7223. $this->assertTags($result, $expected);
  7224. $result = $this->Form->input('Contact.imrequired');
  7225. $expected = array(
  7226. 'div' => array('class' => 'input text required'),
  7227. 'label' => array('for' => 'ContactImrequired'),
  7228. 'Imrequired',
  7229. '/label',
  7230. 'input' => array(
  7231. 'type' => 'text', 'name' => 'Contact[imrequired]',
  7232. 'id' => 'ContactImrequired',
  7233. 'required' => 'required'
  7234. ),
  7235. '/div'
  7236. );
  7237. $this->assertTags($result, $expected);
  7238. }
  7239. /**
  7240. * Test inputDefaults setter and getter
  7241. *
  7242. * @return void
  7243. */
  7244. public function testInputDefaults() {
  7245. $this->markTestIncomplete('Need to revisit once models work again.');
  7246. $this->Form->create('Contact');
  7247. $this->Form->inputDefaults(array(
  7248. 'label' => false,
  7249. 'div' => array(
  7250. 'style' => 'color: #000;'
  7251. )
  7252. ));
  7253. $result = $this->Form->input('Contact.field1');
  7254. $expected = array(
  7255. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7256. 'input' => array(
  7257. 'type' => 'text', 'name' => 'Contact[field1]',
  7258. 'id' => 'ContactField1'
  7259. ),
  7260. '/div'
  7261. );
  7262. $this->assertTags($result, $expected);
  7263. $this->Form->inputDefaults(array(
  7264. 'div' => false,
  7265. 'label' => 'Label',
  7266. ));
  7267. $result = $this->Form->input('Contact.field1');
  7268. $expected = array(
  7269. 'label' => array('for' => 'ContactField1'),
  7270. 'Label',
  7271. '/label',
  7272. 'input' => array(
  7273. 'type' => 'text', 'name' => 'Contact[field1]',
  7274. 'id' => 'ContactField1'
  7275. ),
  7276. );
  7277. $this->assertTags($result, $expected);
  7278. $this->Form->inputDefaults(array(
  7279. 'label' => false,
  7280. ), true);
  7281. $result = $this->Form->input('Contact.field1');
  7282. $expected = array(
  7283. 'input' => array(
  7284. 'type' => 'text', 'name' => 'Contact[field1]',
  7285. 'id' => 'ContactField1'
  7286. ),
  7287. );
  7288. $this->assertTags($result, $expected);
  7289. $result = $this->Form->inputDefaults();
  7290. $expected = array(
  7291. 'div' => false,
  7292. 'label' => false,
  7293. );
  7294. $this->assertEquals($expected, $result);
  7295. }
  7296. }