FormHelperTest.php 196 KB

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