FormHelperTest.php 195 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692
  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. $result = $this->Form->input('email', array(
  2509. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2510. );
  2511. $expected = array(
  2512. 'div' => array('class' => 'input select'),
  2513. 'label' => array('for' => 'email'),
  2514. 'Email',
  2515. '/label',
  2516. array('select' => array('name' => 'email', 'id' => 'email')),
  2517. array('option' => array('value' => '')),
  2518. '/option',
  2519. array('option' => array('value' => 'è')),
  2520. 'Firést',
  2521. '/option',
  2522. array('option' => array('value' => 'é')),
  2523. 'Secoènd',
  2524. '/option',
  2525. '/select',
  2526. '/div'
  2527. );
  2528. $this->assertTags($result, $expected);
  2529. $result = $this->Form->input('email', array(
  2530. 'options' => array('First', 'Second'), 'empty' => true)
  2531. );
  2532. $expected = array(
  2533. 'div' => array('class' => 'input select'),
  2534. 'label' => array('for' => 'email'),
  2535. 'Email',
  2536. '/label',
  2537. array('select' => array('name' => 'email', 'id' => 'email')),
  2538. array('option' => array('value' => '')),
  2539. '/option',
  2540. array('option' => array('value' => '0')),
  2541. 'First',
  2542. '/option',
  2543. array('option' => array('value' => '1')),
  2544. 'Second',
  2545. '/option',
  2546. '/select',
  2547. '/div'
  2548. );
  2549. $this->assertTags($result, $expected);
  2550. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2551. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2552. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2553. $expected = array(
  2554. 'div' => array('class' => 'input select'),
  2555. 'label' => array('for' => 'model-user-id'),
  2556. 'User',
  2557. '/label',
  2558. 'select' => array('name' => 'Model[user_id]', 'id' => 'model-user-id'),
  2559. array('option' => array('value' => '')),
  2560. '/option',
  2561. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2562. 'good',
  2563. '/option',
  2564. array('option' => array('value' => 'other')),
  2565. 'bad',
  2566. '/option',
  2567. '/select',
  2568. '/div'
  2569. );
  2570. $this->assertTags($result, $expected);
  2571. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2572. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2573. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2574. $expected = array(
  2575. 'div' => array('class' => 'input select'),
  2576. 'label' => array('for' => 'thing-user-id'),
  2577. 'User',
  2578. '/label',
  2579. 'select' => array('name' => 'Thing[user_id]', 'id' => 'thing-user-id'),
  2580. array('option' => array('value' => '')),
  2581. 'Some Empty',
  2582. '/option',
  2583. array('option' => array('value' => 'value')),
  2584. 'good',
  2585. '/option',
  2586. array('option' => array('value' => 'other')),
  2587. 'bad',
  2588. '/option',
  2589. '/select',
  2590. '/div'
  2591. );
  2592. $this->assertTags($result, $expected);
  2593. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2594. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2595. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2596. $expected = array(
  2597. 'div' => array('class' => 'input select'),
  2598. 'label' => array('for' => 'thing-user-id'),
  2599. 'User',
  2600. '/label',
  2601. 'select' => array('name' => 'Thing[user_id]', 'id' => 'thing-user-id'),
  2602. array('option' => array('value' => '')),
  2603. 'Some Empty',
  2604. '/option',
  2605. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2606. 'good',
  2607. '/option',
  2608. array('option' => array('value' => 'other')),
  2609. 'bad',
  2610. '/option',
  2611. '/select',
  2612. '/div'
  2613. );
  2614. $this->assertTags($result, $expected);
  2615. $this->Form->data = array();
  2616. $result = $this->Form->input('Publisher.id', array(
  2617. 'label' => 'Publisher',
  2618. 'type' => 'select',
  2619. 'multiple' => 'checkbox',
  2620. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2621. ));
  2622. $expected = array(
  2623. array('div' => array('class' => 'input select')),
  2624. array('label' => array('for' => 'publisher-id')),
  2625. 'Publisher',
  2626. '/label',
  2627. 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''),
  2628. array('div' => array('class' => 'checkbox')),
  2629. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1')),
  2630. array('label' => array('for' => 'publisher-id-value-1')),
  2631. 'Label 1',
  2632. '/label',
  2633. '/div',
  2634. array('div' => array('class' => 'checkbox')),
  2635. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2')),
  2636. array('label' => array('for' => 'publisher-id-value-2')),
  2637. 'Label 2',
  2638. '/label',
  2639. '/div',
  2640. '/div'
  2641. );
  2642. $this->assertTags($result, $expected);
  2643. }
  2644. /**
  2645. * test that input() and a non standard primary key makes a hidden input by default.
  2646. *
  2647. * @return void
  2648. */
  2649. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2650. $this->markTestIncomplete('Need to revisit once models work again.');
  2651. $this->Form->create('User');
  2652. $this->Form->fieldset = array(
  2653. 'User' => array(
  2654. 'fields' => array(
  2655. 'model_id' => array('type' => 'integer')
  2656. ),
  2657. 'validates' => array(),
  2658. 'key' => 'model_id'
  2659. )
  2660. );
  2661. $result = $this->Form->input('model_id');
  2662. $expected = array(
  2663. 'input' => array('type' => 'hidden', 'name' => 'User[model_id]', 'id' => 'UserModelId'),
  2664. );
  2665. $this->assertTags($result, $expected);
  2666. }
  2667. /**
  2668. * test that overriding the magic select type widget is possible
  2669. *
  2670. * @return void
  2671. */
  2672. public function testInputOverridingMagicSelectType() {
  2673. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2674. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2675. $expected = array(
  2676. 'div' => array('class' => 'input text'),
  2677. 'label' => array('for' => 'model-user-id'), 'User', '/label',
  2678. 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'),
  2679. '/div'
  2680. );
  2681. $this->assertTags($result, $expected);
  2682. //Check that magic types still work for plural/singular vars
  2683. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2684. $result = $this->Form->input('Model.type');
  2685. $expected = array(
  2686. 'div' => array('class' => 'input select'),
  2687. 'label' => array('for' => 'model-type'), 'Type', '/label',
  2688. 'select' => array('name' => 'Model[type]', 'id' => 'model-type'),
  2689. array('option' => array('value' => 'value')), 'good', '/option',
  2690. array('option' => array('value' => 'other')), 'bad', '/option',
  2691. '/select',
  2692. '/div'
  2693. );
  2694. $this->assertTags($result, $expected);
  2695. }
  2696. /**
  2697. * Test that inferred types do not override developer input
  2698. *
  2699. * @return void
  2700. */
  2701. public function testInputMagicTypeDoesNotOverride() {
  2702. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2703. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2704. $expected = array(
  2705. 'div' => array('class' => 'input checkbox'),
  2706. array('input' => array(
  2707. 'type' => 'hidden',
  2708. 'name' => 'Model[user]',
  2709. 'value' => 0,
  2710. )),
  2711. array('input' => array(
  2712. 'name' => 'Model[user]',
  2713. 'type' => 'checkbox',
  2714. 'id' => 'model-user',
  2715. 'value' => 1
  2716. )),
  2717. 'label' => array('for' => 'model-user'), 'User', '/label',
  2718. '/div'
  2719. );
  2720. $this->assertTags($result, $expected);
  2721. }
  2722. /**
  2723. * Test that magic input() selects are created for type=number
  2724. *
  2725. * @return void
  2726. */
  2727. public function testInputMagicSelectForTypeNumber() {
  2728. TableRegistry::get('ValidateUsers', [
  2729. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2730. ]);
  2731. $entity = new Entity(['balance' => 1]);
  2732. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  2733. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2734. $result = $this->Form->input('ValidateUser.balance');
  2735. $expected = array(
  2736. 'div' => array('class' => 'input select'),
  2737. 'label' => array('for' => 'validateuser-balance'),
  2738. 'Balance',
  2739. '/label',
  2740. 'select' => array('name' => 'ValidateUser[balance]', 'id' => 'validateuser-balance'),
  2741. array('option' => array('value' => '0')),
  2742. 'nothing',
  2743. '/option',
  2744. array('option' => array('value' => '1', 'selected' => 'selected')),
  2745. 'some',
  2746. '/option',
  2747. array('option' => array('value' => '100')),
  2748. 'a lot',
  2749. '/option',
  2750. '/select',
  2751. '/div'
  2752. );
  2753. $this->assertTags($result, $expected);
  2754. }
  2755. /**
  2756. * Test that magic input() selects can easily be converted into radio types without error.
  2757. *
  2758. * @return void
  2759. */
  2760. public function testInputMagicSelectChangeToRadio() {
  2761. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2762. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2763. $this->assertRegExp('/input type="radio"/', $result);
  2764. }
  2765. /**
  2766. * fields with the same name as the model should work.
  2767. *
  2768. * @return void
  2769. */
  2770. public function testInputWithMatchingFieldAndModelName() {
  2771. $this->markTestIncomplete('Need to revisit once models work again.');
  2772. $this->Form->create('User');
  2773. $this->Form->fieldset = array(
  2774. 'User' => array(
  2775. 'fields' => array(
  2776. 'User' => array('type' => 'text')
  2777. ),
  2778. 'validates' => array(),
  2779. 'key' => 'id'
  2780. )
  2781. );
  2782. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2783. $result = $this->Form->input('User', array('type' => 'text'));
  2784. $expected = array(
  2785. 'div' => array('class' => 'input text'),
  2786. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2787. 'input' => array('name' => 'User[User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2788. '/div'
  2789. );
  2790. $this->assertTags($result, $expected);
  2791. }
  2792. /**
  2793. * testFormInputs method
  2794. *
  2795. * test correct results from form::inputs().
  2796. *
  2797. * @return void
  2798. */
  2799. public function testFormInputs() {
  2800. $this->markTestIncomplete('Need to revisit once models work again.');
  2801. $this->Form->create('Cake\Test\TestCase\View\Helper\Contact');
  2802. $result = $this->Form->inputs('The Legend');
  2803. $expected = array(
  2804. '<fieldset',
  2805. '<legend',
  2806. 'The Legend',
  2807. '/legend',
  2808. '*/fieldset',
  2809. );
  2810. $this->assertTags($result, $expected);
  2811. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2812. $expected = array(
  2813. 'fieldset' => array('class' => 'classy-stuff'),
  2814. '<legend',
  2815. 'Field of Dreams',
  2816. '/legend',
  2817. '*/fieldset'
  2818. );
  2819. $this->assertTags($result, $expected);
  2820. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2821. $this->assertTags($result, $expected);
  2822. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2823. $this->assertTags($result, $expected);
  2824. $this->Form->create('Contact');
  2825. $this->Form->request['prefix'] = 'admin';
  2826. $this->Form->request['action'] = 'admin_edit';
  2827. $result = $this->Form->inputs();
  2828. $expected = array(
  2829. '<fieldset',
  2830. '<legend',
  2831. 'Edit Contact',
  2832. '/legend',
  2833. '*/fieldset',
  2834. );
  2835. $this->assertTags($result, $expected);
  2836. $this->Form->create('Contact');
  2837. $result = $this->Form->inputs(false);
  2838. $expected = array(
  2839. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2840. array('div' => array('class' => 'input text')),
  2841. '*/div',
  2842. array('div' => array('class' => 'input email')),
  2843. '*/div',
  2844. array('div' => array('class' => 'input tel')),
  2845. '*/div',
  2846. array('div' => array('class' => 'input password')),
  2847. '*/div',
  2848. array('div' => array('class' => 'input date')),
  2849. '*/div',
  2850. array('div' => array('class' => 'input date')),
  2851. '*/div',
  2852. array('div' => array('class' => 'input datetime')),
  2853. '*/div',
  2854. array('div' => array('class' => 'input number')),
  2855. '*/div',
  2856. array('div' => array('class' => 'input select')),
  2857. '*/div',
  2858. );
  2859. $this->assertTags($result, $expected);
  2860. $this->Form->create('Contact');
  2861. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2862. $expected = array(
  2863. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2864. array('div' => array('class' => 'input text')),
  2865. '*/div',
  2866. array('div' => array('class' => 'input email')),
  2867. '*/div',
  2868. array('div' => array('class' => 'input tel')),
  2869. '*/div',
  2870. array('div' => array('class' => 'input password')),
  2871. '*/div',
  2872. array('div' => array('class' => 'input date')),
  2873. '*/div',
  2874. array('div' => array('class' => 'input date')),
  2875. '*/div',
  2876. array('div' => array('class' => 'input datetime')),
  2877. '*/div',
  2878. array('div' => array('class' => 'input number')),
  2879. '*/div',
  2880. array('div' => array('class' => 'input select')),
  2881. '*/div',
  2882. );
  2883. $this->assertTags($result, $expected);
  2884. $this->Form->create('Contact');
  2885. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2886. $this->assertTags($result, $expected);
  2887. $this->Form->create('Contact');
  2888. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2889. $expected = array(
  2890. 'fieldset' => array(),
  2891. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2892. array('div' => array('class' => 'input text')),
  2893. '*/div',
  2894. array('div' => array('class' => 'input email')),
  2895. '*/div',
  2896. array('div' => array('class' => 'input tel')),
  2897. '*/div',
  2898. array('div' => array('class' => 'input password')),
  2899. '*/div',
  2900. array('div' => array('class' => 'input date')),
  2901. '*/div',
  2902. array('div' => array('class' => 'input date')),
  2903. '*/div',
  2904. array('div' => array('class' => 'input datetime')),
  2905. '*/div',
  2906. array('div' => array('class' => 'input number')),
  2907. '*/div',
  2908. array('div' => array('class' => 'input select')),
  2909. '*/div',
  2910. '/fieldset'
  2911. );
  2912. $this->assertTags($result, $expected);
  2913. $this->Form->create('Contact');
  2914. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2915. $expected = array(
  2916. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2917. array('div' => array('class' => 'input text')),
  2918. '*/div',
  2919. array('div' => array('class' => 'input email')),
  2920. '*/div',
  2921. array('div' => array('class' => 'input tel')),
  2922. '*/div',
  2923. array('div' => array('class' => 'input password')),
  2924. '*/div',
  2925. array('div' => array('class' => 'input date')),
  2926. '*/div',
  2927. array('div' => array('class' => 'input date')),
  2928. '*/div',
  2929. array('div' => array('class' => 'input datetime')),
  2930. '*/div',
  2931. array('div' => array('class' => 'input number')),
  2932. '*/div',
  2933. array('div' => array('class' => 'input select')),
  2934. '*/div',
  2935. );
  2936. $this->assertTags($result, $expected);
  2937. $this->Form->create('Contact');
  2938. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2939. $this->assertTags($result, $expected);
  2940. $this->Form->create('Contact');
  2941. $result = $this->Form->inputs('Hello');
  2942. $expected = array(
  2943. 'fieldset' => array(),
  2944. 'legend' => array(),
  2945. 'Hello',
  2946. '/legend',
  2947. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2948. array('div' => array('class' => 'input text')),
  2949. '*/div',
  2950. array('div' => array('class' => 'input email')),
  2951. '*/div',
  2952. array('div' => array('class' => 'input tel')),
  2953. '*/div',
  2954. array('div' => array('class' => 'input password')),
  2955. '*/div',
  2956. array('div' => array('class' => 'input date')),
  2957. '*/div',
  2958. array('div' => array('class' => 'input date')),
  2959. '*/div',
  2960. array('div' => array('class' => 'input datetime')),
  2961. '*/div',
  2962. array('div' => array('class' => 'input number')),
  2963. '*/div',
  2964. array('div' => array('class' => 'input select')),
  2965. '*/div',
  2966. '/fieldset'
  2967. );
  2968. $this->assertTags($result, $expected);
  2969. $this->Form->create('Contact');
  2970. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2971. $expected = array(
  2972. 'fieldset' => array(),
  2973. 'legend' => array(),
  2974. 'Hello',
  2975. '/legend',
  2976. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2977. array('div' => array('class' => 'input text')),
  2978. '*/div',
  2979. array('div' => array('class' => 'input email')),
  2980. '*/div',
  2981. array('div' => array('class' => 'input tel')),
  2982. '*/div',
  2983. array('div' => array('class' => 'input password')),
  2984. '*/div',
  2985. array('div' => array('class' => 'input date')),
  2986. '*/div',
  2987. array('div' => array('class' => 'input date')),
  2988. '*/div',
  2989. array('div' => array('class' => 'input datetime')),
  2990. '*/div',
  2991. array('div' => array('class' => 'input number')),
  2992. '*/div',
  2993. array('div' => array('class' => 'input select')),
  2994. '*/div',
  2995. '/fieldset'
  2996. );
  2997. $this->assertTags($result, $expected);
  2998. $this->Form->create('Contact');
  2999. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  3000. $this->assertTags($result, $expected);
  3001. $this->Form->end();
  3002. $this->Form->create(false);
  3003. $expected = array(
  3004. 'fieldset' => array(),
  3005. array('div' => array('class' => 'input text')),
  3006. 'label' => array('for' => 'foo'),
  3007. 'Foo',
  3008. '/label',
  3009. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  3010. '*/div',
  3011. '/fieldset'
  3012. );
  3013. $result = $this->Form->inputs(
  3014. array('foo' => array('type' => 'text')),
  3015. array(),
  3016. array('legend' => false)
  3017. );
  3018. $this->assertTags($result, $expected);
  3019. }
  3020. /**
  3021. * Tests inputs() works with plugin models
  3022. *
  3023. * @return void
  3024. */
  3025. public function testInputsPluginModel() {
  3026. $this->markTestIncomplete('Need to revisit once models work again.');
  3027. $this->loadFixtures('Post');
  3028. Plugin::load('TestPlugin');
  3029. $this->Form->request['models'] = array(
  3030. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  3031. );
  3032. $this->Form->create('TestPlugin.TestPluginPost');
  3033. $result = $this->Form->inputs();
  3034. $this->assertContains('TestPluginPost[id]', $result);
  3035. $this->assertContains('TestPluginPost[author_id]', $result);
  3036. $this->assertContains('TestPluginPost[title]', $result);
  3037. $this->assertEquals('TestPluginPost', $this->Form->model());
  3038. }
  3039. /**
  3040. * testSelectAsCheckbox method
  3041. *
  3042. * test multi-select widget with checkbox formatting.
  3043. *
  3044. * @return void
  3045. */
  3046. public function testSelectAsCheckbox() {
  3047. $result = $this->Form->select(
  3048. 'Model.multi_field',
  3049. array('first', 'second', 'third'),
  3050. array('multiple' => 'checkbox', 'value' => array(0, 1))
  3051. );
  3052. $expected = array(
  3053. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3054. array('div' => array('class' => 'checkbox')),
  3055. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0')),
  3056. array('label' => array('for' => 'model-multi-field-0', 'class' => 'selected')),
  3057. 'first',
  3058. '/label',
  3059. '/div',
  3060. array('div' => array('class' => 'checkbox')),
  3061. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1')),
  3062. array('label' => array('for' => 'model-multi-field-1', 'class' => 'selected')),
  3063. 'second',
  3064. '/label',
  3065. '/div',
  3066. array('div' => array('class' => 'checkbox')),
  3067. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  3068. array('label' => array('for' => 'model-multi-field-2')),
  3069. 'third',
  3070. '/label',
  3071. '/div',
  3072. );
  3073. $this->assertTags($result, $expected);
  3074. $result = $this->Form->select(
  3075. 'Model.multi_field',
  3076. array('1/2' => 'half'),
  3077. array('multiple' => 'checkbox')
  3078. );
  3079. $expected = array(
  3080. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3081. array('div' => array('class' => 'checkbox')),
  3082. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2')),
  3083. array('label' => array('for' => 'model-multi-field-1-2')),
  3084. 'half',
  3085. '/label',
  3086. '/div',
  3087. );
  3088. $this->assertTags($result, $expected);
  3089. }
  3090. /**
  3091. * testLabel method
  3092. *
  3093. * test label generation.
  3094. *
  3095. * @return void
  3096. */
  3097. public function testLabel() {
  3098. $result = $this->Form->label('Person.name');
  3099. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  3100. $result = $this->Form->label('Person.name');
  3101. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  3102. $result = $this->Form->label('Person.first_name');
  3103. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'First Name', '/label'));
  3104. $result = $this->Form->label('Person.first_name', 'Your first name');
  3105. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'Your first name', '/label'));
  3106. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  3107. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class'), 'Your first name', '/label'));
  3108. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  3109. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  3110. $result = $this->Form->label('Person.first_name', '');
  3111. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), '/label'));
  3112. $result = $this->Form->label('Person.2.name', '');
  3113. $this->assertTags($result, array('label' => array('for' => 'person-2-name'), '/label'));
  3114. }
  3115. /**
  3116. * testTextbox method
  3117. *
  3118. * test textbox element generation
  3119. *
  3120. * @return void
  3121. */
  3122. public function testTextbox() {
  3123. $result = $this->Form->text('Model.field');
  3124. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]')));
  3125. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3126. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]')));
  3127. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3128. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID')));
  3129. }
  3130. /**
  3131. * Test that text() hooks up with request data and error fields.
  3132. *
  3133. * @return void
  3134. */
  3135. public function testTextBoxDataAndError() {
  3136. $this->article['errors'] = [
  3137. 'Contact' => ['text' => 'wrong']
  3138. ];
  3139. $this->Form->create($this->article);
  3140. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3141. $result = $this->Form->text('Model.text');
  3142. $expected = [
  3143. 'input' => [
  3144. 'type' => 'text',
  3145. 'name' => 'Model[text]',
  3146. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3147. ]
  3148. ];
  3149. $this->assertTags($result, $expected);
  3150. $this->Form->request->data['Contact']['text'] = 'test';
  3151. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3152. $expected = [
  3153. 'input' => [
  3154. 'type' => 'text',
  3155. 'name' => 'Contact[text]',
  3156. 'value' => 'test',
  3157. 'id' => 'theID',
  3158. 'class' => 'form-error'
  3159. ]
  3160. ];
  3161. $this->assertTags($result, $expected);
  3162. }
  3163. /**
  3164. * testDefaultValue method
  3165. *
  3166. * Test default value setting
  3167. *
  3168. * @return void
  3169. */
  3170. public function testTextDefaultValue() {
  3171. $this->Form->request->data['Model']['field'] = 'test';
  3172. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3173. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3174. $this->assertTags($result, $expected);
  3175. unset($this->Form->request->data['Model']['field']);
  3176. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3177. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3178. $this->assertTags($result, $expected);
  3179. }
  3180. /**
  3181. * testError method
  3182. *
  3183. * Test field error generation
  3184. *
  3185. * @return void
  3186. */
  3187. public function testError() {
  3188. $this->article['errors'] = [
  3189. 'Article' => ['field' => 'email']
  3190. ];
  3191. $this->Form->create($this->article);
  3192. $result = $this->Form->error('Article.field');
  3193. $expected = [
  3194. ['div' => ['class' => 'error-message']],
  3195. 'email',
  3196. '/div',
  3197. ];
  3198. $this->assertTags($result, $expected);
  3199. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  3200. $expected = [
  3201. ['div' => ['class' => 'error-message']],
  3202. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3203. '/div',
  3204. ];
  3205. $this->assertTags($result, $expected);
  3206. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3207. $expected = [
  3208. ['div' => ['class' => 'error-message']],
  3209. '<strong', 'Badness!', '/strong',
  3210. '/div',
  3211. ];
  3212. $this->assertTags($result, $expected);
  3213. }
  3214. /**
  3215. * Test error with nested lists.
  3216. *
  3217. * @return void
  3218. */
  3219. public function testErrorMessages() {
  3220. $this->article['errors'] = [
  3221. 'Article' => ['field' => 'email']
  3222. ];
  3223. $this->Form->create($this->article);
  3224. $result = $this->Form->error('Article.field', array(
  3225. 'email' => 'No good!'
  3226. ));
  3227. $expected = array(
  3228. 'div' => array('class' => 'error-message'),
  3229. 'No good!',
  3230. '/div'
  3231. );
  3232. $this->assertTags($result, $expected);
  3233. }
  3234. /**
  3235. * test error() with multiple messages.
  3236. *
  3237. * @return void
  3238. */
  3239. public function testErrorMultipleMessages() {
  3240. $this->article['errors'] = [
  3241. 'field' => ['notEmpty', 'email', 'Something else']
  3242. ];
  3243. $this->Form->create($this->article);
  3244. $result = $this->Form->error('field', array(
  3245. 'notEmpty' => 'Cannot be empty',
  3246. 'email' => 'No good!'
  3247. ));
  3248. $expected = array(
  3249. 'div' => array('class' => 'error-message'),
  3250. 'ul' => array(),
  3251. '<li', 'Cannot be empty', '/li',
  3252. '<li', 'No good!', '/li',
  3253. '<li', 'Something else', '/li',
  3254. '/ul',
  3255. '/div'
  3256. );
  3257. $this->assertTags($result, $expected);
  3258. }
  3259. /**
  3260. * test error options when using form->input();
  3261. *
  3262. * @return void
  3263. */
  3264. public function testInputErrorEscape() {
  3265. $this->markTestIncomplete('Need to revisit once models work again.');
  3266. $this->Form->create('ValidateProfile');
  3267. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3268. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3269. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3270. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3271. $this->assertRegExp('/required<br>/', $result);
  3272. }
  3273. /**
  3274. * testPassword method
  3275. *
  3276. * Test password element generation
  3277. *
  3278. * @return void
  3279. */
  3280. public function testPassword() {
  3281. $this->article['errors'] = [
  3282. 'Contact' => [
  3283. 'passwd' => 1
  3284. ]
  3285. ];
  3286. $this->Form->create($this->article);
  3287. $result = $this->Form->password('Contact.field');
  3288. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]')));
  3289. $this->Form->request->data['Contact']['passwd'] = 'test';
  3290. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3291. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3292. }
  3293. /**
  3294. * testRadio method
  3295. *
  3296. * Test radio element set generation
  3297. *
  3298. * @return void
  3299. */
  3300. public function testRadio() {
  3301. $result = $this->Form->radio('Model.field', array('option A'));
  3302. $expected = array(
  3303. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3304. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3305. 'label' => array('for' => 'model-field-0'),
  3306. 'option A',
  3307. '/label'
  3308. );
  3309. $this->assertTags($result, $expected);
  3310. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3311. $expected = array(
  3312. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3313. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3314. array('label' => array('for' => 'model-field-0')),
  3315. 'option A',
  3316. '/label',
  3317. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1')),
  3318. array('label' => array('for' => 'model-field-1')),
  3319. 'option B',
  3320. '/label',
  3321. );
  3322. $this->assertTags($result, $expected);
  3323. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3324. $expected = array(
  3325. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''),
  3326. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male')),
  3327. array('label' => array('for' => 'employee-gender-male')),
  3328. 'Male',
  3329. '/label',
  3330. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female')),
  3331. array('label' => array('for' => 'employee-gender-female')),
  3332. 'Female',
  3333. '/label',
  3334. );
  3335. $this->assertTags($result, $expected);
  3336. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  3337. $expected = array(
  3338. array('input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '')),
  3339. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0')),
  3340. array('label' => array('for' => 'model-custom-0')),
  3341. 'option A',
  3342. '/label',
  3343. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1')),
  3344. array('label' => array('for' => 'model-custom-1')),
  3345. 'option B',
  3346. '/label',
  3347. );
  3348. $this->assertTags($result, $expected);
  3349. }
  3350. /**
  3351. * test generating radio input inside label ala twitter bootstrap
  3352. *
  3353. * @return void
  3354. */
  3355. public function testRadioInputInsideLabel() {
  3356. $this->Form->templates([
  3357. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3358. 'radioContainer' => '{{label}}'
  3359. ]);
  3360. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  3361. $expected = [
  3362. ['input' => [
  3363. 'type' => 'hidden',
  3364. 'name' => 'Model[field]',
  3365. 'value' => ''
  3366. ]],
  3367. ['label' => ['for' => 'model-field-0']],
  3368. ['input' => [
  3369. 'type' => 'radio',
  3370. 'name' => 'Model[field]',
  3371. 'value' => '0',
  3372. 'id' => 'model-field-0'
  3373. ]],
  3374. 'option A',
  3375. '/label',
  3376. ['label' => ['for' => 'model-field-1']],
  3377. ['input' => [
  3378. 'type' => 'radio',
  3379. 'name' => 'Model[field]',
  3380. 'value' => '1',
  3381. 'id' => 'model-field-1'
  3382. ]],
  3383. 'option B',
  3384. '/label',
  3385. ];
  3386. $this->assertTags($result, $expected);
  3387. }
  3388. /**
  3389. * test disabling the hidden input for radio buttons
  3390. *
  3391. * @return void
  3392. */
  3393. public function testRadioHiddenInputDisabling() {
  3394. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3395. $expected = array(
  3396. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'),
  3397. 'label' => array('for' => 'model-1-field-0'),
  3398. 'option A',
  3399. '/label'
  3400. );
  3401. $this->assertTags($result, $expected);
  3402. }
  3403. /**
  3404. * testSelect method
  3405. *
  3406. * Test select element generation.
  3407. *
  3408. * @return void
  3409. */
  3410. public function testSelect() {
  3411. $result = $this->Form->select('Model.field', array());
  3412. $expected = array(
  3413. 'select' => array('name' => 'Model[field]'),
  3414. '/select'
  3415. );
  3416. $this->assertTags($result, $expected);
  3417. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3418. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3419. $expected = array(
  3420. 'select' => array('name' => 'Model[field]'),
  3421. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3422. 'good',
  3423. '/option',
  3424. array('option' => array('value' => 'other')),
  3425. 'bad',
  3426. '/option',
  3427. '/select'
  3428. );
  3429. $this->assertTags($result, $expected);
  3430. $this->Form->request->data = array();
  3431. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3432. $expected = array(
  3433. 'select' => array('name' => 'Model[field]'),
  3434. array('option' => array('value' => 'value')),
  3435. 'good',
  3436. '/option',
  3437. array('option' => array('value' => 'other')),
  3438. 'bad',
  3439. '/option',
  3440. '/select'
  3441. );
  3442. $this->assertTags($result, $expected);
  3443. $result = $this->Form->select(
  3444. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3445. array('empty' => false)
  3446. );
  3447. $expected = array(
  3448. 'select' => array('name' => 'Model[field]'),
  3449. array('option' => array('value' => 'first')),
  3450. 'first &quot;html&quot; &lt;chars&gt;',
  3451. '/option',
  3452. array('option' => array('value' => 'second')),
  3453. 'value',
  3454. '/option',
  3455. '/select'
  3456. );
  3457. $this->assertTags($result, $expected);
  3458. $result = $this->Form->select(
  3459. 'Model.field',
  3460. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3461. array('escape' => false, 'empty' => false)
  3462. );
  3463. $expected = array(
  3464. 'select' => array('name' => 'Model[field]'),
  3465. array('option' => array('value' => 'first')),
  3466. 'first "html" <chars>',
  3467. '/option',
  3468. array('option' => array('value' => 'second')),
  3469. 'value',
  3470. '/option',
  3471. '/select'
  3472. );
  3473. $this->assertTags($result, $expected);
  3474. $options = array(
  3475. array('value' => 'first', 'text' => 'First'),
  3476. array('value' => 'first', 'text' => 'Another First'),
  3477. );
  3478. $result = $this->Form->select(
  3479. 'Model.field',
  3480. $options,
  3481. array('escape' => false, 'empty' => false)
  3482. );
  3483. $expected = array(
  3484. 'select' => array('name' => 'Model[field]'),
  3485. array('option' => array('value' => 'first')),
  3486. 'First',
  3487. '/option',
  3488. array('option' => array('value' => 'first')),
  3489. 'Another First',
  3490. '/option',
  3491. '/select'
  3492. );
  3493. $this->assertTags($result, $expected);
  3494. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3495. $result = $this->Form->select(
  3496. 'Model.contact_id',
  3497. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3498. array('escape' => false, 'empty' => 'pick something')
  3499. );
  3500. $expected = array(
  3501. 'select' => array('name' => 'Model[contact_id]'),
  3502. array('option' => array('value' => '')), 'pick something', '/option',
  3503. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3504. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3505. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3506. '/select'
  3507. );
  3508. $this->assertTags($result, $expected);
  3509. $this->Form->request->data['Model']['field'] = 0;
  3510. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3511. $expected = array(
  3512. 'select' => array('name' => 'Model[field]'),
  3513. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3514. array('option' => array('value' => '1')), 'Yes', '/option',
  3515. '/select'
  3516. );
  3517. $this->assertTags($result, $expected);
  3518. }
  3519. /**
  3520. * test select() with required and disabled attributes.
  3521. *
  3522. * @return void
  3523. */
  3524. public function testSelectRequired() {
  3525. $this->article['required'] = [
  3526. 'user_id' => true
  3527. ];
  3528. $this->Form->create($this->article);
  3529. $result = $this->Form->select('user_id', array('option A'));
  3530. $expected = array(
  3531. 'select' => array(
  3532. 'name' => 'user_id',
  3533. 'required' => 'required'
  3534. ),
  3535. array('option' => array('value' => '0')), 'option A', '/option',
  3536. '/select'
  3537. );
  3538. $this->assertTags($result, $expected);
  3539. $result = $this->Form->select('user_id', array('option A'), array('disabled' => true));
  3540. $expected = array(
  3541. 'select' => array(
  3542. 'name' => 'user_id',
  3543. 'disabled' => 'disabled'
  3544. ),
  3545. array('option' => array('value' => '0')), 'option A', '/option',
  3546. '/select'
  3547. );
  3548. $this->assertTags($result, $expected);
  3549. }
  3550. /**
  3551. * testNestedSelect method
  3552. *
  3553. * test select element generation with optgroups
  3554. *
  3555. * @return void
  3556. */
  3557. public function testNestedSelect() {
  3558. $result = $this->Form->select(
  3559. 'Model.field',
  3560. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3561. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3562. )), array('empty' => false)
  3563. );
  3564. $expected = array(
  3565. 'select' => array('name' => 'Model[field]'),
  3566. array('option' => array('value' => 1)),
  3567. 'One',
  3568. '/option',
  3569. array('option' => array('value' => 2)),
  3570. 'Two',
  3571. '/option',
  3572. array('optgroup' => array('label' => 'Three')),
  3573. array('option' => array('value' => 3)),
  3574. 'Three',
  3575. '/option',
  3576. array('option' => array('value' => 4)),
  3577. 'Four',
  3578. '/option',
  3579. array('option' => array('value' => 5)),
  3580. 'Five',
  3581. '/option',
  3582. '/optgroup',
  3583. '/select'
  3584. );
  3585. $this->assertTags($result, $expected);
  3586. }
  3587. /**
  3588. * testSelectMultiple method
  3589. *
  3590. * test generation of multiple select elements
  3591. *
  3592. * @return void
  3593. */
  3594. public function testSelectMultiple() {
  3595. $options = array('first', 'second', 'third');
  3596. $result = $this->Form->select(
  3597. 'Model.multi_field',
  3598. $options,
  3599. array('multiple' => true)
  3600. );
  3601. $expected = array(
  3602. 'input' => array(
  3603. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3604. ),
  3605. 'select' => array(
  3606. 'name' => 'Model[multi_field][]',
  3607. 'multiple' => 'multiple'
  3608. ),
  3609. array('option' => array('value' => '0')),
  3610. 'first',
  3611. '/option',
  3612. array('option' => array('value' => '1')),
  3613. 'second',
  3614. '/option',
  3615. array('option' => array('value' => '2')),
  3616. 'third',
  3617. '/option',
  3618. '/select'
  3619. );
  3620. $this->assertTags($result, $expected);
  3621. $result = $this->Form->select(
  3622. 'Model.multi_field',
  3623. $options,
  3624. array('multiple' => 'multiple')
  3625. );
  3626. $this->assertTags($result, $expected);
  3627. }
  3628. /**
  3629. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  3630. *
  3631. * @return void
  3632. */
  3633. public function testCheckboxZeroValue() {
  3634. $this->markTestIncomplete('Need to revisit once models work again.');
  3635. $result = $this->Form->input('User.get_spam', array(
  3636. 'type' => 'checkbox',
  3637. 'value' => '0',
  3638. 'hiddenField' => '1',
  3639. ));
  3640. $expected = array(
  3641. 'div' => array('class' => 'input checkbox'),
  3642. array('input' => array(
  3643. 'type' => 'hidden', 'name' => 'data[User][get_spam]',
  3644. 'value' => '1', 'id' => 'UserGetSpam_'
  3645. )),
  3646. array('input' => array(
  3647. 'type' => 'checkbox', 'name' => 'data[User][get_spam]',
  3648. 'value' => '0', 'id' => 'UserGetSpam'
  3649. )),
  3650. 'label' => array('for' => 'UserGetSpam'),
  3651. 'Get Spam',
  3652. '/label',
  3653. '/div'
  3654. );
  3655. $this->assertTags($result, $expected);
  3656. }
  3657. /**
  3658. * test generation of habtm select boxes.
  3659. *
  3660. * @return void
  3661. */
  3662. public function testHabtmSelectBox() {
  3663. $this->markTestIncomplete('Need to revisit once models work again.');
  3664. $this->View->viewVars['contactTags'] = array(
  3665. 1 => 'blue',
  3666. 2 => 'red',
  3667. 3 => 'green'
  3668. );
  3669. $this->Form->request->data = array(
  3670. 'Contact' => array(),
  3671. 'ContactTag' => array(
  3672. array(
  3673. 'id' => '1',
  3674. 'name' => 'blue'
  3675. ),
  3676. array(
  3677. 'id' => 3,
  3678. 'name' => 'green'
  3679. )
  3680. )
  3681. );
  3682. $this->Form->create('Contact');
  3683. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3684. $expected = array(
  3685. 'input' => array(
  3686. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3687. ),
  3688. 'select' => array(
  3689. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  3690. 'multiple' => 'multiple'
  3691. ),
  3692. array('option' => array('value' => '1', 'selected' => 'selected')),
  3693. 'blue',
  3694. '/option',
  3695. array('option' => array('value' => '2')),
  3696. 'red',
  3697. '/option',
  3698. array('option' => array('value' => '3', 'selected' => 'selected')),
  3699. 'green',
  3700. '/option',
  3701. '/select'
  3702. );
  3703. $this->assertTags($result, $expected);
  3704. // make sure only 50 is selected, and not 50f5c0cf
  3705. $this->View->viewVars['contactTags'] = array(
  3706. '1' => 'blue',
  3707. '50f5c0cf' => 'red',
  3708. '50' => 'green'
  3709. );
  3710. $this->Form->request->data = array(
  3711. 'Contact' => array(),
  3712. 'ContactTag' => array(
  3713. array(
  3714. 'id' => 1,
  3715. 'name' => 'blue'
  3716. ),
  3717. array(
  3718. 'id' => 50,
  3719. 'name' => 'green'
  3720. )
  3721. )
  3722. );
  3723. $this->Form->create('Contact');
  3724. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3725. $expected = array(
  3726. 'input' => array(
  3727. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3728. ),
  3729. 'select' => array(
  3730. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  3731. 'multiple' => 'multiple'
  3732. ),
  3733. array('option' => array('value' => '1', 'selected' => 'selected')),
  3734. 'blue',
  3735. '/option',
  3736. array('option' => array('value' => '50f5c0cf')),
  3737. 'red',
  3738. '/option',
  3739. array('option' => array('value' => '50', 'selected' => 'selected')),
  3740. 'green',
  3741. '/option',
  3742. '/select'
  3743. );
  3744. $this->assertTags($result, $expected);
  3745. }
  3746. /**
  3747. * test generation of multi select elements in checkbox format
  3748. *
  3749. * @return void
  3750. */
  3751. public function testSelectMultipleCheckboxes() {
  3752. $result = $this->Form->select(
  3753. 'Model.multi_field',
  3754. array('first', 'second', 'third'),
  3755. array('multiple' => 'checkbox')
  3756. );
  3757. $expected = array(
  3758. 'input' => array(
  3759. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3760. ),
  3761. array('div' => array('class' => 'checkbox')),
  3762. array('input' => array(
  3763. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3764. 'value' => '0', 'id' => 'model-multi-field-0'
  3765. )),
  3766. array('label' => array('for' => 'model-multi-field-0')),
  3767. 'first',
  3768. '/label',
  3769. '/div',
  3770. array('div' => array('class' => 'checkbox')),
  3771. array('input' => array(
  3772. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3773. 'value' => '1', 'id' => 'model-multi-field-1'
  3774. )),
  3775. array('label' => array('for' => 'model-multi-field-1')),
  3776. 'second',
  3777. '/label',
  3778. '/div',
  3779. array('div' => array('class' => 'checkbox')),
  3780. array('input' => array(
  3781. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3782. 'value' => '2', 'id' => 'model-multi-field-2'
  3783. )),
  3784. array('label' => array('for' => 'model-multi-field-2')),
  3785. 'third',
  3786. '/label',
  3787. '/div'
  3788. );
  3789. $this->assertTags($result, $expected);
  3790. $result = $this->Form->select(
  3791. 'Model.multi_field',
  3792. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  3793. array('multiple' => 'checkbox')
  3794. );
  3795. $expected = array(
  3796. 'input' => array(
  3797. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3798. ),
  3799. array('div' => array('class' => 'checkbox')),
  3800. array('input' => array(
  3801. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3802. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  3803. )),
  3804. array('label' => array('for' => 'model-multi-field-a+')),
  3805. 'first',
  3806. '/label',
  3807. '/div',
  3808. array('div' => array('class' => 'checkbox')),
  3809. array('input' => array(
  3810. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3811. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  3812. )),
  3813. array('label' => array('for' => 'model-multi-field-a++')),
  3814. 'second',
  3815. '/label',
  3816. '/div',
  3817. array('div' => array('class' => 'checkbox')),
  3818. array('input' => array(
  3819. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3820. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  3821. )),
  3822. array('label' => array('for' => 'model-multi-field-a+++')),
  3823. 'third',
  3824. '/label',
  3825. '/div'
  3826. );
  3827. $this->assertTags($result, $expected);
  3828. $result = $this->Form->select(
  3829. 'Model.multi_field',
  3830. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  3831. array('multiple' => 'checkbox')
  3832. );
  3833. $expected = array(
  3834. 'input' => array(
  3835. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3836. ),
  3837. array('div' => array('class' => 'checkbox')),
  3838. array('input' => array(
  3839. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3840. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  3841. )),
  3842. array('label' => array('for' => 'model-multi-field-a-b')),
  3843. 'first',
  3844. '/label',
  3845. '/div',
  3846. array('div' => array('class' => 'checkbox')),
  3847. array('input' => array(
  3848. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3849. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  3850. )),
  3851. array('label' => array('for' => 'model-multi-field-a-b1')),
  3852. 'second',
  3853. '/label',
  3854. '/div',
  3855. array('div' => array('class' => 'checkbox')),
  3856. array('input' => array(
  3857. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3858. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  3859. )),
  3860. array('label' => array('for' => 'model-multi-field-a-b2')),
  3861. 'third',
  3862. '/label',
  3863. '/div'
  3864. );
  3865. $this->assertTags($result, $expected);
  3866. }
  3867. /**
  3868. * Ensure that multiCheckbox reads from the request data.
  3869. *
  3870. * @return void
  3871. */
  3872. public function testSelectMultipleCheckboxRequestData() {
  3873. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3874. $result = $this->Form->select(
  3875. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3876. );
  3877. $expected = array(
  3878. 'input' => array(
  3879. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  3880. ),
  3881. array('div' => array('class' => 'checkbox')),
  3882. array('input' => array(
  3883. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3884. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  3885. )),
  3886. array('label' => array('for' => 'model-tags-1', 'class' => 'selected')),
  3887. 'first',
  3888. '/label',
  3889. '/div',
  3890. array('div' => array('class' => 'checkbox')),
  3891. array('input' => array(
  3892. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3893. 'value' => 'Array', 'id' => 'model-tags-array'
  3894. )),
  3895. array('label' => array('for' => 'model-tags-array')),
  3896. 'Array',
  3897. '/label',
  3898. '/div'
  3899. );
  3900. $this->assertTags($result, $expected);
  3901. }
  3902. /**
  3903. * Checks the security hash array generated for multiple-input checkbox elements
  3904. *
  3905. * @return void
  3906. */
  3907. public function testSelectMultipleCheckboxSecurity() {
  3908. $this->Form->request->params['_Token'] = 'testKey';
  3909. $this->assertEquals(array(), $this->Form->fields);
  3910. $result = $this->Form->select(
  3911. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3912. array('multiple' => 'checkbox')
  3913. );
  3914. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3915. $result = $this->Form->secure($this->Form->fields);
  3916. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3917. $this->assertRegExp('/"' . $key . '"/', $result);
  3918. }
  3919. /**
  3920. * Multiple select elements should always be secured as they always participate
  3921. * in the POST data.
  3922. *
  3923. * @return void
  3924. */
  3925. public function testSelectMultipleSecureWithNoOptions() {
  3926. $this->assertEquals(array(), $this->Form->fields);
  3927. $this->Form->select(
  3928. 'Model.select',
  3929. array(),
  3930. array('multiple' => true)
  3931. );
  3932. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3933. }
  3934. /**
  3935. * When a select box has no options it should not be added to the fields list
  3936. * as it always fail post validation.
  3937. *
  3938. * @return void
  3939. */
  3940. public function testSelectNoSecureWithNoOptions() {
  3941. $this->Form->request->params['_Token'] = 'testkey';
  3942. $this->assertEquals([], $this->Form->fields);
  3943. $this->Form->select(
  3944. 'Model.select',
  3945. []
  3946. );
  3947. $this->assertEquals([], $this->Form->fields);
  3948. $this->Form->select(
  3949. 'Model.user_id',
  3950. [],
  3951. ['empty' => true]
  3952. );
  3953. $this->assertEquals(array('Model.user_id'), $this->Form->fields);
  3954. }
  3955. /**
  3956. * testInputMultipleCheckboxes method
  3957. *
  3958. * test input() resulting in multi select elements being generated.
  3959. *
  3960. * @return void
  3961. */
  3962. public function testInputMultipleCheckboxes() {
  3963. $this->markTestIncomplete('Need to revisit once models work again.');
  3964. $result = $this->Form->input('Model.multi_field', array(
  3965. 'options' => array('first', 'second', 'third'),
  3966. 'multiple' => 'checkbox'
  3967. ));
  3968. $expected = array(
  3969. array('div' => array('class' => 'input select')),
  3970. array('label' => array('for' => 'ModelMultiField')),
  3971. 'Multi Field',
  3972. '/label',
  3973. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3974. array('div' => array('class' => 'checkbox')),
  3975. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3976. array('label' => array('for' => 'ModelMultiField0')),
  3977. 'first',
  3978. '/label',
  3979. '/div',
  3980. array('div' => array('class' => 'checkbox')),
  3981. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3982. array('label' => array('for' => 'ModelMultiField1')),
  3983. 'second',
  3984. '/label',
  3985. '/div',
  3986. array('div' => array('class' => 'checkbox')),
  3987. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3988. array('label' => array('for' => 'ModelMultiField2')),
  3989. 'third',
  3990. '/label',
  3991. '/div',
  3992. '/div'
  3993. );
  3994. $this->assertTags($result, $expected);
  3995. $result = $this->Form->input('Model.multi_field', array(
  3996. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3997. 'multiple' => 'checkbox'
  3998. ));
  3999. $expected = array(
  4000. array('div' => array('class' => 'input select')),
  4001. array('label' => array('for' => 'ModelMultiField')),
  4002. 'Multi Field',
  4003. '/label',
  4004. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4005. array('div' => array('class' => 'checkbox')),
  4006. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4007. array('label' => array('for' => 'ModelMultiFieldA')),
  4008. 'first',
  4009. '/label',
  4010. '/div',
  4011. array('div' => array('class' => 'checkbox')),
  4012. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4013. array('label' => array('for' => 'ModelMultiFieldB')),
  4014. 'second',
  4015. '/label',
  4016. '/div',
  4017. array('div' => array('class' => 'checkbox')),
  4018. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4019. array('label' => array('for' => 'ModelMultiFieldC')),
  4020. 'third',
  4021. '/label',
  4022. '/div',
  4023. '/div'
  4024. );
  4025. $this->assertTags($result, $expected);
  4026. $result = $this->Form->input('Model.multi_field', array(
  4027. 'options' => array('1' => 'first'),
  4028. 'multiple' => 'checkbox',
  4029. 'label' => false,
  4030. 'div' => false
  4031. ));
  4032. $expected = array(
  4033. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4034. array('div' => array('class' => 'checkbox')),
  4035. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4036. array('label' => array('for' => 'ModelMultiField1')),
  4037. 'first',
  4038. '/label',
  4039. '/div'
  4040. );
  4041. $this->assertTags($result, $expected);
  4042. $result = $this->Form->input('Model.multi_field', array(
  4043. 'options' => array('2' => 'second'),
  4044. 'multiple' => 'checkbox',
  4045. 'label' => false,
  4046. 'div' => false
  4047. ));
  4048. $expected = array(
  4049. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4050. array('div' => array('class' => 'checkbox')),
  4051. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4052. array('label' => array('for' => 'ModelMultiField2')),
  4053. 'second',
  4054. '/label',
  4055. '/div'
  4056. );
  4057. $this->assertTags($result, $expected);
  4058. }
  4059. /**
  4060. * testSelectHiddenFieldOmission method
  4061. *
  4062. * test that select() with 'hiddenField' => false omits the hidden field
  4063. *
  4064. * @return void
  4065. */
  4066. public function testSelectHiddenFieldOmission() {
  4067. $result = $this->Form->select('Model.multi_field',
  4068. array('first', 'second'),
  4069. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4070. );
  4071. $this->assertNotContains('type="hidden"', $result);
  4072. }
  4073. /**
  4074. * test that select() with multiple = checkbox works with overriding name attribute.
  4075. *
  4076. * @return void
  4077. */
  4078. public function testSelectCheckboxMultipleOverrideName() {
  4079. $result = $this->Form->select('category', ['1', '2'], [
  4080. 'multiple' => 'checkbox',
  4081. 'name' => 'fish',
  4082. ]);
  4083. $expected = array(
  4084. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => ''),
  4085. array('div' => array('class' => 'checkbox')),
  4086. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0')),
  4087. array('label' => array('for' => 'fish-0')), '1', '/label',
  4088. '/div',
  4089. array('div' => array('class' => 'checkbox')),
  4090. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1')),
  4091. array('label' => array('for' => 'fish-1')), '2', '/label',
  4092. '/div'
  4093. );
  4094. $this->assertTags($result, $expected);
  4095. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  4096. 'name' => 'fish',
  4097. ]);
  4098. $this->assertTags($result, $expected);
  4099. }
  4100. /**
  4101. * testCheckbox method
  4102. *
  4103. * Test generation of checkboxes
  4104. *
  4105. * @return void
  4106. */
  4107. public function testCheckbox() {
  4108. $result = $this->Form->checkbox('Model.field');
  4109. $expected = array(
  4110. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  4111. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'))
  4112. );
  4113. $this->assertTags($result, $expected);
  4114. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4115. $expected = array(
  4116. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  4117. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => 'myvalue', 'id' => 'theID'))
  4118. );
  4119. $this->assertTags($result, $expected);
  4120. }
  4121. /**
  4122. * testCheckboxDefaultValue method
  4123. *
  4124. * Test default value setting on checkbox() method
  4125. *
  4126. * @return void
  4127. */
  4128. public function testCheckboxDefaultValue() {
  4129. $this->Form->request->data['Model']['field'] = false;
  4130. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  4131. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  4132. unset($this->Form->request->data['Model']['field']);
  4133. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  4134. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  4135. $this->Form->request->data['Model']['field'] = true;
  4136. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  4137. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  4138. unset($this->Form->request->data['Model']['field']);
  4139. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  4140. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  4141. }
  4142. /**
  4143. * Test checkbox being checked or having errors.
  4144. *
  4145. * @return void
  4146. */
  4147. public function testCheckboxCheckedAndError() {
  4148. $this->article['errors'] = [
  4149. 'published' => true
  4150. ];
  4151. $this->Form->request->data['published'] = 'myvalue';
  4152. $this->Form->create($this->article);
  4153. $result = $this->Form->checkbox('published', array('id' => 'theID', 'value' => 'myvalue'));
  4154. $expected = array(
  4155. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4156. array('input' => array(
  4157. 'type' => 'checkbox',
  4158. 'name' => 'published',
  4159. 'value' => 'myvalue',
  4160. 'id' => 'theID',
  4161. 'checked' => 'checked',
  4162. 'class' => 'form-error'
  4163. ))
  4164. );
  4165. $this->assertTags($result, $expected);
  4166. $this->Form->request->data['published'] = '';
  4167. $result = $this->Form->checkbox('published');
  4168. $expected = array(
  4169. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4170. array('input' => array('type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error'))
  4171. );
  4172. $this->assertTags($result, $expected);
  4173. }
  4174. /**
  4175. * test checkbox() with a custom name attribute
  4176. *
  4177. * @return void
  4178. */
  4179. public function testCheckboxCustomNameAttribute() {
  4180. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4181. $expected = array(
  4182. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0'),
  4183. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1'))
  4184. );
  4185. $this->assertTags($result, $expected);
  4186. }
  4187. /**
  4188. * Test that the hidden input for checkboxes can be omitted or set to a
  4189. * specific value.
  4190. *
  4191. * @return void
  4192. */
  4193. public function testCheckboxHiddenField() {
  4194. $result = $this->Form->checkbox('UserForm.something', array(
  4195. 'hiddenField' => false
  4196. ));
  4197. $expected = array(
  4198. 'input' => array(
  4199. 'type' => 'checkbox',
  4200. 'name' => 'UserForm[something]',
  4201. 'value' => '1'
  4202. ),
  4203. );
  4204. $this->assertTags($result, $expected);
  4205. $result = $this->Form->checkbox('UserForm.something', array(
  4206. 'value' => 'Y',
  4207. 'hiddenField' => 'N',
  4208. ));
  4209. $expected = array(
  4210. array('input' => array(
  4211. 'type' => 'hidden', 'name' => 'UserForm[something]',
  4212. 'value' => 'N'
  4213. )),
  4214. array('input' => array(
  4215. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  4216. 'value' => 'Y'
  4217. )),
  4218. );
  4219. $this->assertTags($result, $expected);
  4220. }
  4221. /**
  4222. * testDateTime method
  4223. *
  4224. * Test generation of date/time select elements
  4225. *
  4226. * @return void
  4227. */
  4228. public function testDateTime() {
  4229. extract($this->dateRegex);
  4230. $result = $this->Form->dateTime('Contact.date', array('empty' => false));
  4231. $now = strtotime('now');
  4232. $expected = array(
  4233. array('select' => array('name' => 'Contact[date][month]')),
  4234. $monthsRegex,
  4235. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4236. date('F', $now),
  4237. '/option',
  4238. '*/select',
  4239. array('select' => array('name' => 'Contact[date][day]')),
  4240. $daysRegex,
  4241. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4242. date('j', $now),
  4243. '/option',
  4244. '*/select',
  4245. array('select' => array('name' => 'Contact[date][year]')),
  4246. $yearsRegex,
  4247. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4248. date('Y', $now),
  4249. '/option',
  4250. '*/select',
  4251. array('select' => array('name' => 'Contact[date][hour]')),
  4252. $hoursRegex,
  4253. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4254. date('g', $now),
  4255. '/option',
  4256. '*/select',
  4257. array('select' => array('name' => 'Contact[date][minute]')),
  4258. $minutesRegex,
  4259. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4260. date('i', $now),
  4261. '/option',
  4262. '*/select',
  4263. array('select' => array('name' => 'Contact[date][meridian]')),
  4264. $meridianRegex,
  4265. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4266. date('a', $now),
  4267. '/option',
  4268. '*/select'
  4269. );
  4270. $this->assertTags($result, $expected);
  4271. }
  4272. /**
  4273. * Test empty defaulting to true for datetime.
  4274. *
  4275. * @return void
  4276. */
  4277. public function testDatetimeEmpty() {
  4278. extract($this->dateRegex);
  4279. $now = strtotime('now');
  4280. $result = $this->Form->dateTime('Contact.date', array(
  4281. 'empty' => true,
  4282. ));
  4283. $expected = array(
  4284. array('select' => array('name' => 'Contact[date][month]')),
  4285. $monthsRegex,
  4286. array('option' => array('value' => '')),
  4287. '/option',
  4288. '*/select',
  4289. array('select' => array('name' => 'Contact[date][day]')),
  4290. $daysRegex,
  4291. array('option' => array('value' => '')),
  4292. '/option',
  4293. '*/select',
  4294. array('select' => array('name' => 'Contact[date][year]')),
  4295. $yearsRegex,
  4296. array('option' => array('value' => '')),
  4297. '/option',
  4298. '*/select',
  4299. array('select' => array('name' => 'Contact[date][hour]')),
  4300. $hoursRegex,
  4301. array('option' => array('value' => '')),
  4302. '/option',
  4303. '*/select',
  4304. array('select' => array('name' => 'Contact[date][minute]')),
  4305. $minutesRegex,
  4306. array('option' => array('value' => '')),
  4307. '/option',
  4308. '*/select',
  4309. array('select' => array('name' => 'Contact[date][meridian]')),
  4310. $meridianRegex,
  4311. array('option' => array('value' => '')),
  4312. '/option',
  4313. '*/select'
  4314. );
  4315. $this->assertTags($result, $expected);
  4316. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4317. }
  4318. /**
  4319. * Test datetime with interval option.
  4320. *
  4321. * @return void
  4322. */
  4323. public function testDatetimeMinuteInterval() {
  4324. extract($this->dateRegex);
  4325. $now = strtotime('now');
  4326. $result = $this->Form->dateTime('Contact.date', array(
  4327. 'interval' => 5,
  4328. 'value' => ''
  4329. ));
  4330. $expected = array(
  4331. array('select' => array('name' => 'Contact[date][month]')),
  4332. $monthsRegex,
  4333. array('option' => array('selected' => 'selected', 'value' => '')),
  4334. '/option',
  4335. '*/select',
  4336. array('select' => array('name' => 'Contact[date][day]')),
  4337. $daysRegex,
  4338. array('option' => array('selected' => 'selected', 'value' => '')),
  4339. '/option',
  4340. '*/select',
  4341. array('select' => array('name' => 'Contact[date][year]')),
  4342. $yearsRegex,
  4343. array('option' => array('selected' => 'selected', 'value' => '')),
  4344. '/option',
  4345. '*/select',
  4346. array('select' => array('name' => 'Contact[date][hour]')),
  4347. $hoursRegex,
  4348. array('option' => array('selected' => 'selected', 'value' => '')),
  4349. '/option',
  4350. '*/select',
  4351. array('select' => array('name' => 'Contact[date][minute]')),
  4352. $minutesRegex,
  4353. array('option' => array('selected' => 'selected', 'value' => '')),
  4354. '/option',
  4355. array('option' => array('value' => '00')),
  4356. '00',
  4357. '/option',
  4358. array('option' => array('value' => '05')),
  4359. '05',
  4360. '/option',
  4361. array('option' => array('value' => '10')),
  4362. '10',
  4363. '/option',
  4364. '*/select',
  4365. );
  4366. $this->assertTags($result, $expected);
  4367. }
  4368. /**
  4369. * Test dateTime with rounding
  4370. *
  4371. * @return void
  4372. */
  4373. public function testDateTimeRounding() {
  4374. $this->Form->request->data['Contact'] = array(
  4375. 'date' => array(
  4376. 'day' => '13',
  4377. 'month' => '12',
  4378. 'year' => '2010',
  4379. 'hour' => '04',
  4380. 'minute' => '19',
  4381. 'meridian' => 'AM'
  4382. )
  4383. );
  4384. $result = $this->Form->dateTime('Contact.date', array('interval' => 15));
  4385. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4386. $result = $this->Form->dateTime('Contact.date', array('interval' => 15, 'round' => 'up'));
  4387. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  4388. $result = $this->Form->dateTime('Contact.date', array('interval' => 5, 'round' => 'down'));
  4389. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4390. }
  4391. /**
  4392. * test that datetime() and default values work.
  4393. *
  4394. * @return void
  4395. */
  4396. public function testDatetimeWithDefault() {
  4397. $result = $this->Form->dateTime('Contact.updated', array('value' => '2009-06-01 11:15:30'));
  4398. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4399. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4400. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4401. $result = $this->Form->dateTime('Contact.updated', array(
  4402. 'default' => '2009-06-01 11:15:30'
  4403. ));
  4404. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4405. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4406. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4407. }
  4408. /**
  4409. * testDateTime all zeros
  4410. *
  4411. * @return void
  4412. */
  4413. public function testDateTimeAllZeros() {
  4414. $result = $this->Form->dateTime('Contact.date', array(
  4415. 'timeFormat' => false,
  4416. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  4417. 'value' => '0000-00-00'
  4418. ));
  4419. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  4420. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  4421. }
  4422. /**
  4423. * testDateTimeEmptyAsArray
  4424. *
  4425. * @return void
  4426. */
  4427. public function testDateTimeEmptyAsArray() {
  4428. $result = $this->Form->dateTime('Contact.date', array(
  4429. 'empty' => array(
  4430. 'day' => 'DAY',
  4431. 'month' => 'MONTH',
  4432. 'year' => 'YEAR',
  4433. 'hour' => 'HOUR',
  4434. 'minute' => 'MINUTE',
  4435. 'meridian' => false
  4436. )
  4437. ));
  4438. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4439. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4440. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4441. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4442. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4443. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4444. $result = $this->Form->dateTime('Contact.date', array(
  4445. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4446. ));
  4447. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4448. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4449. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4450. }
  4451. /**
  4452. * testFormDateTimeMulti method
  4453. *
  4454. * test multiple datetime element generation
  4455. *
  4456. * @return void
  4457. */
  4458. public function testFormDateTimeMulti() {
  4459. extract($this->dateRegex);
  4460. $result = $this->Form->dateTime('Contact.1.updated');
  4461. $this->assertContains('Contact[1][updated][month]', $result);
  4462. $this->assertContains('Contact[1][updated][day]', $result);
  4463. $this->assertContains('Contact[1][updated][year]', $result);
  4464. $this->assertContains('Contact[1][updated][hour]', $result);
  4465. $this->assertContains('Contact[1][updated][minute]', $result);
  4466. $this->assertContains('Contact[1][updated][meridian]', $result);
  4467. }
  4468. /**
  4469. * When changing the date format, the label should always focus the first select box when
  4470. * clicked.
  4471. *
  4472. * @return void
  4473. */
  4474. public function testDateTimeLabelIdMatchesFirstInput() {
  4475. $this->markTestIncomplete('Need to revisit once models work again.');
  4476. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4477. $this->assertContains('label for="ModelDateMonth"', $result);
  4478. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4479. $this->assertContains('label for="ModelDateDay"', $result);
  4480. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4481. $this->assertContains('label for="ModelDateYear"', $result);
  4482. }
  4483. /**
  4484. * testMonth method
  4485. *
  4486. * @return void
  4487. */
  4488. public function testMonth() {
  4489. $result = $this->Form->month('Model.field', ['value' => '']);
  4490. $expected = array(
  4491. array('select' => array('name' => 'Model[field][month]')),
  4492. array('option' => array('value' => '', 'selected' => 'selected')),
  4493. '/option',
  4494. array('option' => array('value' => '01')),
  4495. date('F', strtotime('2008-01-01 00:00:00')),
  4496. '/option',
  4497. array('option' => array('value' => '02')),
  4498. date('F', strtotime('2008-02-01 00:00:00')),
  4499. '/option',
  4500. '*/select',
  4501. );
  4502. $this->assertTags($result, $expected);
  4503. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  4504. $expected = array(
  4505. array('select' => array('name' => 'Model[field][month]')),
  4506. array('option' => array('selected' => 'selected', 'value' => '')),
  4507. '/option',
  4508. array('option' => array('value' => '01')),
  4509. date('F', strtotime('2008-01-01 00:00:00')),
  4510. '/option',
  4511. array('option' => array('value' => '02')),
  4512. date('F', strtotime('2008-02-01 00:00:00')),
  4513. '/option',
  4514. '*/select',
  4515. );
  4516. $this->assertTags($result, $expected);
  4517. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  4518. $expected = array(
  4519. array('select' => array('name' => 'Model[field][month]')),
  4520. array('option' => array('selected' => 'selected', 'value' => '')),
  4521. '/option',
  4522. array('option' => array('value' => '01')),
  4523. '1',
  4524. '/option',
  4525. array('option' => array('value' => '02')),
  4526. '2',
  4527. '/option',
  4528. '*/select',
  4529. );
  4530. $this->assertTags($result, $expected);
  4531. $monthNames = [
  4532. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4533. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  4534. ];
  4535. $result = $this->Form->month('Model.field', array('value' => '1', 'monthNames' => $monthNames));
  4536. $expected = array(
  4537. array('select' => array('name' => 'Model[field][month]')),
  4538. array('option' => array('value' => '')),
  4539. '/option',
  4540. array('option' => array('value' => '01', 'selected' => 'selected')),
  4541. 'Jan',
  4542. '/option',
  4543. array('option' => array('value' => '02')),
  4544. 'Feb',
  4545. '/option',
  4546. '*/select',
  4547. );
  4548. $this->assertTags($result, $expected);
  4549. $this->Form->request->data['Project']['release'] = '2050-02-10';
  4550. $result = $this->Form->month('Project.release');
  4551. $expected = array(
  4552. array('select' => array('name' => 'Project[release][month]')),
  4553. array('option' => array('value' => '')),
  4554. '/option',
  4555. array('option' => array('value' => '01')),
  4556. 'January',
  4557. '/option',
  4558. array('option' => array('value' => '02', 'selected' => 'selected')),
  4559. 'February',
  4560. '/option',
  4561. '*/select',
  4562. );
  4563. $this->assertTags($result, $expected);
  4564. }
  4565. /**
  4566. * testDay method
  4567. *
  4568. * @return void
  4569. */
  4570. public function testDay() {
  4571. extract($this->dateRegex);
  4572. $result = $this->Form->day('Model.field', array('value' => false));
  4573. $expected = array(
  4574. array('select' => array('name' => 'Model[field][day]')),
  4575. array('option' => array('selected' => 'selected', 'value' => '')),
  4576. '/option',
  4577. array('option' => array('value' => '01')),
  4578. '1',
  4579. '/option',
  4580. array('option' => array('value' => '02')),
  4581. '2',
  4582. '/option',
  4583. $daysRegex,
  4584. '/select',
  4585. );
  4586. $this->assertTags($result, $expected);
  4587. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4588. $result = $this->Form->day('Model.field');
  4589. $expected = array(
  4590. array('select' => array('name' => 'Model[field][day]')),
  4591. array('option' => array('value' => '')),
  4592. '/option',
  4593. array('option' => array('value' => '01')),
  4594. '1',
  4595. '/option',
  4596. array('option' => array('value' => '02')),
  4597. '2',
  4598. '/option',
  4599. $daysRegex,
  4600. array('option' => array('value' => '10', 'selected' => 'selected')),
  4601. '10',
  4602. '/option',
  4603. $daysRegex,
  4604. '/select',
  4605. );
  4606. $this->assertTags($result, $expected);
  4607. $this->Form->request->data['Model']['field'] = '';
  4608. $result = $this->Form->day('Model.field', array('value' => '10'));
  4609. $expected = array(
  4610. array('select' => array('name' => 'Model[field][day]')),
  4611. array('option' => array('value' => '')),
  4612. '/option',
  4613. array('option' => array('value' => '01')),
  4614. '1',
  4615. '/option',
  4616. array('option' => array('value' => '02')),
  4617. '2',
  4618. '/option',
  4619. $daysRegex,
  4620. array('option' => array('value' => '10', 'selected' => 'selected')),
  4621. '10',
  4622. '/option',
  4623. $daysRegex,
  4624. '/select',
  4625. );
  4626. $this->assertTags($result, $expected);
  4627. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4628. $result = $this->Form->day('Project.release');
  4629. $expected = array(
  4630. array('select' => array('name' => 'Project[release][day]')),
  4631. array('option' => array('value' => '')),
  4632. '/option',
  4633. array('option' => array('value' => '01')),
  4634. '1',
  4635. '/option',
  4636. array('option' => array('value' => '02')),
  4637. '2',
  4638. '/option',
  4639. $daysRegex,
  4640. array('option' => array('value' => '10', 'selected' => 'selected')),
  4641. '10',
  4642. '/option',
  4643. $daysRegex,
  4644. '/select',
  4645. );
  4646. $this->assertTags($result, $expected);
  4647. }
  4648. /**
  4649. * testMinute method
  4650. *
  4651. * @return void
  4652. */
  4653. public function testMinute() {
  4654. extract($this->dateRegex);
  4655. $result = $this->Form->minute('Model.field', ['value' => '']);
  4656. $expected = array(
  4657. array('select' => array('name' => 'Model[field][minute]')),
  4658. array('option' => array('selected' => 'selected', 'value' => '')),
  4659. '/option',
  4660. array('option' => array('value' => '00')),
  4661. '00',
  4662. '/option',
  4663. array('option' => array('value' => '01')),
  4664. '01',
  4665. '/option',
  4666. array('option' => array('value' => '02')),
  4667. '02',
  4668. '/option',
  4669. $minutesRegex,
  4670. '/select',
  4671. );
  4672. $this->assertTags($result, $expected);
  4673. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4674. $result = $this->Form->minute('Model.field');
  4675. $expected = array(
  4676. array('select' => array('name' => 'Model[field][minute]')),
  4677. array('option' => array('value' => '')),
  4678. '/option',
  4679. array('option' => array('value' => '00')),
  4680. '00',
  4681. '/option',
  4682. array('option' => array('value' => '01')),
  4683. '01',
  4684. '/option',
  4685. array('option' => array('value' => '02')),
  4686. '02',
  4687. '/option',
  4688. $minutesRegex,
  4689. array('option' => array('value' => '12', 'selected' => 'selected')),
  4690. '12',
  4691. '/option',
  4692. $minutesRegex,
  4693. '/select',
  4694. );
  4695. $this->assertTags($result, $expected);
  4696. $this->Form->request->data['Model']['field'] = '';
  4697. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4698. $expected = array(
  4699. array('select' => array('name' => 'Model[field][minute]')),
  4700. array('option' => array('selected' => 'selected', 'value' => '')),
  4701. '/option',
  4702. array('option' => array('value' => '00')),
  4703. '00',
  4704. '/option',
  4705. array('option' => array('value' => '05')),
  4706. '05',
  4707. '/option',
  4708. array('option' => array('value' => '10')),
  4709. '10',
  4710. '/option',
  4711. $minutesRegex,
  4712. '/select',
  4713. );
  4714. $this->assertTags($result, $expected);
  4715. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  4716. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4717. $expected = array(
  4718. array('select' => array('name' => 'Model[field][minute]')),
  4719. array('option' => array('value' => '')),
  4720. '/option',
  4721. array('option' => array('value' => '00')),
  4722. '00',
  4723. '/option',
  4724. array('option' => array('value' => '05')),
  4725. '05',
  4726. '/option',
  4727. array('option' => array('value' => '10', 'selected' => 'selected')),
  4728. '10',
  4729. '/option',
  4730. $minutesRegex,
  4731. '/select',
  4732. );
  4733. $this->assertTags($result, $expected);
  4734. }
  4735. /**
  4736. * Test generating an input for the meridian.
  4737. *
  4738. * @return void
  4739. */
  4740. public function testMeridian() {
  4741. extract($this->dateRegex);
  4742. $now = time();
  4743. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  4744. $expected = [
  4745. array('select' => array('name' => 'Model[field][meridian]')),
  4746. array('option' => array('value' => '')),
  4747. '/option',
  4748. $meridianRegex,
  4749. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4750. date('a', $now),
  4751. '/option',
  4752. '*/select'
  4753. ];
  4754. $this->assertTags($result, $expected);
  4755. }
  4756. /**
  4757. * testHour method
  4758. *
  4759. * @return void
  4760. */
  4761. public function testHour() {
  4762. extract($this->dateRegex);
  4763. $result = $this->Form->hour('Model.field', ['value' => '']);
  4764. $expected = array(
  4765. array('select' => array('name' => 'Model[field][hour]')),
  4766. array('option' => array('selected' => 'selected', 'value' => '')),
  4767. '/option',
  4768. array('option' => array('value' => '01')),
  4769. '1',
  4770. '/option',
  4771. array('option' => array('value' => '02')),
  4772. '2',
  4773. '/option',
  4774. $hoursRegex,
  4775. '/select',
  4776. );
  4777. $this->assertTags($result, $expected);
  4778. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4779. $result = $this->Form->hour('Model.field', ['format' => 12]);
  4780. $expected = array(
  4781. array('select' => array('name' => 'Model[field][hour]')),
  4782. array('option' => array('value' => '')),
  4783. '/option',
  4784. array('option' => array('value' => '01')),
  4785. '1',
  4786. '/option',
  4787. array('option' => array('value' => '02')),
  4788. '2',
  4789. '/option',
  4790. $hoursRegex,
  4791. array('option' => array('value' => '12', 'selected' => 'selected')),
  4792. '12',
  4793. '/option',
  4794. '/select',
  4795. );
  4796. $this->assertTags($result, $expected);
  4797. $this->Form->request->data['Model']['field'] = '';
  4798. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => '23'));
  4799. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  4800. $result = $this->Form->hour('Model.field', array('format' => 12, 'value' => '23'));
  4801. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  4802. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4803. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4804. $expected = array(
  4805. array('select' => array('name' => 'Model[field][hour]')),
  4806. array('option' => array('value' => '')),
  4807. '/option',
  4808. array('option' => array('value' => '00', 'selected' => 'selected')),
  4809. '0',
  4810. '/option',
  4811. array('option' => array('value' => '01')),
  4812. '1',
  4813. '/option',
  4814. array('option' => array('value' => '02')),
  4815. '2',
  4816. '/option',
  4817. $hoursRegex,
  4818. '/select',
  4819. );
  4820. $this->assertTags($result, $expected);
  4821. unset($this->Form->request->data['Model']['field']);
  4822. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => 'now'));
  4823. $thisHour = date('H');
  4824. $optValue = date('G');
  4825. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  4826. $this->Form->request->data['Model']['field'] = '2050-10-10 01: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')),
  4833. '0',
  4834. '/option',
  4835. array('option' => array('value' => '01', 'selected' => 'selected')),
  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. }
  4846. /**
  4847. * testYear method
  4848. *
  4849. * @return void
  4850. */
  4851. public function testYear() {
  4852. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  4853. $expected = array(
  4854. array('select' => array('name' => 'Model[field][year]')),
  4855. array('option' => array('selected' => 'selected', 'value' => '')),
  4856. '/option',
  4857. array('option' => array('value' => '2007')),
  4858. '2007',
  4859. '/option',
  4860. array('option' => array('value' => '2006')),
  4861. '2006',
  4862. '/option',
  4863. '/select',
  4864. );
  4865. $this->assertTags($result, $expected);
  4866. $result = $this->Form->year('Model.field', [
  4867. 'value' => '',
  4868. 'minYear' => 2006,
  4869. 'maxYear' => 2007,
  4870. 'orderYear' => 'asc'
  4871. ]);
  4872. $expected = array(
  4873. array('select' => array('name' => 'Model[field][year]')),
  4874. array('option' => array('selected' => 'selected', 'value' => '')),
  4875. '/option',
  4876. array('option' => array('value' => '2006')),
  4877. '2006',
  4878. '/option',
  4879. array('option' => array('value' => '2007')),
  4880. '2007',
  4881. '/option',
  4882. '/select',
  4883. );
  4884. $this->assertTags($result, $expected);
  4885. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  4886. $result = $this->Form->year('Contact.published', [
  4887. 'empty' => false,
  4888. 'minYear' => 2006,
  4889. 'maxYear' => 2007,
  4890. ]);
  4891. $expected = array(
  4892. array('select' => array('name' => 'Contact[published][year]')),
  4893. array('option' => array('value' => '2007')),
  4894. '2007',
  4895. '/option',
  4896. array('option' => array('value' => '2006', 'selected' => 'selected')),
  4897. '2006',
  4898. '/option',
  4899. '/select',
  4900. );
  4901. $this->assertTags($result, $expected);
  4902. }
  4903. /**
  4904. * testYearAutoExpandRange method
  4905. *
  4906. * @return void
  4907. */
  4908. public function testYearAutoExpandRange() {
  4909. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  4910. $result = $this->Form->year('User.birthday');
  4911. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4912. $result = $matches[1];
  4913. $expected = range(date('Y') + 5, 1930);
  4914. $this->assertEquals($expected, $result);
  4915. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4916. $result = $this->Form->year('Project.release');
  4917. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4918. $result = $matches[1];
  4919. $expected = range(2050, date('Y') - 5);
  4920. $this->assertEquals($expected, $result);
  4921. $this->Form->request->data['Project']['release'] = '1881-10-10';
  4922. $result = $this->Form->year('Project.release', [
  4923. 'minYear' => 1890,
  4924. 'maxYear' => 1900
  4925. ]);
  4926. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4927. $result = $matches[1];
  4928. $expected = range(1900, 1881);
  4929. $this->assertEquals($expected, $result);
  4930. }
  4931. /**
  4932. * Test that input() accepts the type of date and passes options in.
  4933. *
  4934. * @return void
  4935. */
  4936. public function testInputDate() {
  4937. $this->Form->request->data = array(
  4938. 'month_year' => array('month' => date('m')),
  4939. );
  4940. $this->Form->create($this->article);
  4941. $result = $this->Form->input('month_year', array(
  4942. 'label' => false,
  4943. 'type' => 'date',
  4944. 'minYear' => 2006,
  4945. 'maxYear' => 2008
  4946. ));
  4947. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  4948. $this->assertNotContains('value="2008" selected="selected"', $result);
  4949. }
  4950. /**
  4951. * testInputDateMaxYear method
  4952. *
  4953. * Let's say we want to only allow users born from 2006 to 2008 to register
  4954. * This being the first singup page, we still don't have any data
  4955. *
  4956. * @return void
  4957. */
  4958. public function testInputDateMaxYear() {
  4959. $this->Form->request->data = [];
  4960. $this->Form->create($this->article);
  4961. $result = $this->Form->input('birthday', array(
  4962. 'label' => false,
  4963. 'type' => 'date',
  4964. 'minYear' => 2006,
  4965. 'maxYear' => 2008
  4966. ));
  4967. $this->assertContains('value="2008" selected="selected"', $result);
  4968. }
  4969. /**
  4970. * testTextArea method
  4971. *
  4972. * @return void
  4973. */
  4974. public function testTextArea() {
  4975. $this->Form->request->data = array('field' => 'some test data');
  4976. $result = $this->Form->textarea('field');
  4977. $expected = array(
  4978. 'textarea' => array('name' => 'field'),
  4979. 'some test data',
  4980. '/textarea',
  4981. );
  4982. $this->assertTags($result, $expected);
  4983. $result = $this->Form->textarea('user.bio');
  4984. $expected = array(
  4985. 'textarea' => array('name' => 'user[bio]'),
  4986. '/textarea',
  4987. );
  4988. $this->assertTags($result, $expected);
  4989. $this->Form->request->data = array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  4990. $result = $this->Form->textarea('field');
  4991. $expected = array(
  4992. 'textarea' => array('name' => 'field'),
  4993. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  4994. '/textarea',
  4995. );
  4996. $this->assertTags($result, $expected);
  4997. $this->Form->request->data = [
  4998. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  4999. ];
  5000. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5001. $expected = array(
  5002. 'textarea' => array('name' => 'Model[field]'),
  5003. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5004. '/textarea',
  5005. );
  5006. $this->assertTags($result, $expected);
  5007. $result = $this->Form->textarea('0.OtherModel.field');
  5008. $expected = array(
  5009. 'textarea' => array('name' => '0[OtherModel][field]'),
  5010. '/textarea'
  5011. );
  5012. $this->assertTags($result, $expected);
  5013. }
  5014. /**
  5015. * testTextAreaWithStupidCharacters method
  5016. *
  5017. * test text area with non-ascii characters
  5018. *
  5019. * @return void
  5020. */
  5021. public function testTextAreaWithStupidCharacters() {
  5022. $result = $this->Form->textarea('Post.content', [
  5023. 'value' => "GREAT®",
  5024. 'rows' => '15',
  5025. 'cols' => '75'
  5026. ]);
  5027. $expected = [
  5028. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5029. 'GREAT®',
  5030. '/textarea',
  5031. ];
  5032. $this->assertTags($result, $expected);
  5033. }
  5034. /**
  5035. * testHiddenField method
  5036. *
  5037. * @return void
  5038. */
  5039. public function testHiddenField() {
  5040. $this->article['errors'] = [
  5041. 'field' => true
  5042. ];
  5043. $this->Form->request->data['field'] = 'test';
  5044. $this->Form->create($this->article);
  5045. $result = $this->Form->hidden('field', array('id' => 'theID'));
  5046. $this->assertTags($result, array(
  5047. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test'))
  5048. );
  5049. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5050. $expected = [
  5051. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  5052. ];
  5053. $this->assertTags($result, $expected);
  5054. }
  5055. /**
  5056. * testFileUploadField method
  5057. *
  5058. * @return void
  5059. */
  5060. public function testFileUploadField() {
  5061. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5062. $result = $this->Form->file('Model.upload');
  5063. $this->assertTags($result, $expected);
  5064. $this->Form->request->data['Model']['upload'] = [
  5065. 'name' => '', 'type' => '', 'tmp_name' => '',
  5066. 'error' => 4, 'size' => 0
  5067. ];
  5068. $result = $this->Form->file('Model.upload');
  5069. $this->assertTags($result, $expected);
  5070. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  5071. $result = $this->Form->file('Model.upload');
  5072. $this->assertTags($result, $expected);
  5073. }
  5074. /**
  5075. * test File upload input on a model not used in create();
  5076. *
  5077. * @return void
  5078. */
  5079. public function testFileUploadOnOtherModel() {
  5080. $this->Form->create($this->article, array('type' => 'file'));
  5081. $result = $this->Form->file('ValidateProfile.city');
  5082. $expected = array(
  5083. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]')
  5084. );
  5085. $this->assertTags($result, $expected);
  5086. }
  5087. /**
  5088. * testButton method
  5089. *
  5090. * @return void
  5091. */
  5092. public function testButton() {
  5093. $result = $this->Form->button('Hi');
  5094. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5095. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5096. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5097. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5098. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5099. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5100. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5101. $result = $this->Form->button('No type', array('type' => false));
  5102. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5103. $result = $this->Form->button('Upload Text', array(
  5104. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  5105. 'escape' => false
  5106. ));
  5107. $this->assertNotRegExp('/\&039/', $result);
  5108. }
  5109. /**
  5110. * Test that button() makes unlocked fields by default.
  5111. *
  5112. * @return void
  5113. */
  5114. public function testButtonUnlockedByDefault() {
  5115. $this->Form->request->params['_csrfToken'] = 'secured';
  5116. $this->Form->button('Save', array('name' => 'save'));
  5117. $this->Form->button('Clear');
  5118. $result = $this->Form->unlockField();
  5119. $this->assertEquals(array('save'), $result);
  5120. }
  5121. /**
  5122. * testPostButton method
  5123. *
  5124. * @return void
  5125. */
  5126. public function testPostButton() {
  5127. $result = $this->Form->postButton('Hi', '/controller/action');
  5128. $this->assertTags($result, array(
  5129. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5130. 'div' => array('style' => 'display:none;'),
  5131. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5132. '/div',
  5133. 'button' => array('type' => 'submit'),
  5134. 'Hi',
  5135. '/button',
  5136. '/form'
  5137. ));
  5138. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5139. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  5140. }
  5141. /**
  5142. * Test using postButton with N dimensional data.
  5143. *
  5144. * @return void
  5145. */
  5146. public function testPostButtonNestedData() {
  5147. $data = array(
  5148. 'one' => array(
  5149. 'two' => array(
  5150. 3, 4, 5
  5151. )
  5152. )
  5153. );
  5154. $result = $this->Form->postButton('Send', '/', array('data' => $data));
  5155. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5156. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5157. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5158. }
  5159. /**
  5160. * Test that postButton adds _Token fields.
  5161. *
  5162. * @return void
  5163. */
  5164. public function testSecurePostButton() {
  5165. $this->Form->request->params['_csrfToken'] = 'testkey';
  5166. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5167. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5168. $expected = array(
  5169. 'form' => array(
  5170. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5171. ),
  5172. array('div' => array('style' => 'display:none;')),
  5173. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5174. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5175. '/div',
  5176. 'button' => array('type' => 'submit'),
  5177. 'Delete',
  5178. '/button',
  5179. array('div' => array('style' => 'display:none;')),
  5180. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5181. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5182. '/div',
  5183. '/form',
  5184. );
  5185. $this->assertTags($result, $expected);
  5186. }
  5187. /**
  5188. * testPostLink method
  5189. *
  5190. * @return void
  5191. */
  5192. public function testPostLink() {
  5193. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5194. $this->assertTags($result, array(
  5195. 'form' => array(
  5196. 'method' => 'post', 'action' => '/posts/delete/1',
  5197. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5198. ),
  5199. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5200. '/form',
  5201. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5202. 'Delete',
  5203. '/a'
  5204. ));
  5205. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5206. $this->assertTags($result, array(
  5207. 'form' => array(
  5208. 'method' => 'post', 'action' => '/posts/delete/1',
  5209. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5210. ),
  5211. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5212. '/form',
  5213. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5214. 'Delete',
  5215. '/a'
  5216. ));
  5217. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5218. $this->assertTags($result, array(
  5219. 'form' => array(
  5220. 'method' => 'post', 'action' => '/posts/delete/1',
  5221. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5222. ),
  5223. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5224. '/form',
  5225. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5226. 'Delete',
  5227. '/a'
  5228. ));
  5229. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  5230. $this->assertTags($result, array(
  5231. 'form' => array(
  5232. 'method' => 'post', 'action' => '/posts/delete/1',
  5233. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5234. ),
  5235. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5236. '/form',
  5237. '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;/'),
  5238. 'Delete',
  5239. '/a'
  5240. ));
  5241. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5242. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  5243. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  5244. $this->assertTags($result, array(
  5245. 'form' => array(
  5246. 'method' => 'post', 'target' => '_blank', '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:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5252. 'Delete',
  5253. '/a'
  5254. ));
  5255. $result = $this->Form->postLink(
  5256. '',
  5257. array('controller' => 'items', 'action' => 'delete', 10),
  5258. array('class' => 'btn btn-danger', 'escape' => false),
  5259. 'Confirm thing'
  5260. );
  5261. $this->assertTags($result, array(
  5262. 'form' => array(
  5263. 'method' => 'post', 'action' => '/items/delete/10',
  5264. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5265. ),
  5266. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5267. '/form',
  5268. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5269. '/a'
  5270. ));
  5271. }
  5272. /**
  5273. * Test using postLink with N dimensional data.
  5274. *
  5275. * @return void
  5276. */
  5277. public function testPostLinkNestedData() {
  5278. $data = array(
  5279. 'one' => array(
  5280. 'two' => array(
  5281. 3, 4, 5
  5282. )
  5283. )
  5284. );
  5285. $result = $this->Form->postLink('Send', '/', array('data' => $data));
  5286. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5287. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5288. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5289. }
  5290. /**
  5291. * test creating postLinks after a GET form.
  5292. *
  5293. * @return void
  5294. */
  5295. public function testPostLinkAfterGetForm() {
  5296. $this->Form->request->params['_csrfToken'] = 'testkey';
  5297. $this->Form->request->params['_Token'] = 'val';
  5298. $this->Form->create($this->article, array('type' => 'get'));
  5299. $this->Form->end();
  5300. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5301. $this->assertTags($result, array(
  5302. 'form' => array(
  5303. 'method' => 'post', 'action' => '/posts/delete/1',
  5304. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5305. ),
  5306. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5307. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5308. 'div' => array('style' => 'display:none;'),
  5309. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5310. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5311. '/div',
  5312. '/form',
  5313. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5314. 'Delete',
  5315. '/a'
  5316. ));
  5317. }
  5318. /**
  5319. * Test that postLink adds form tags to view block
  5320. *
  5321. * @return void
  5322. */
  5323. public function testPostLinkFormBuffer() {
  5324. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
  5325. $this->assertTags($result, array(
  5326. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5327. 'Delete',
  5328. '/a'
  5329. ));
  5330. $result = $this->View->fetch('postLink');
  5331. $this->assertTags($result, array(
  5332. 'form' => array(
  5333. 'method' => 'post', 'action' => '/posts/delete/1',
  5334. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5335. ),
  5336. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5337. '/form'
  5338. ));
  5339. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  5340. array('inline' => false, 'method' => 'DELETE')
  5341. );
  5342. $this->assertTags($result, array(
  5343. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5344. 'Delete',
  5345. '/a'
  5346. ));
  5347. $result = $this->View->fetch('postLink');
  5348. $this->assertTags($result, array(
  5349. 'form' => array(
  5350. 'method' => 'post', 'action' => '/posts/delete/1',
  5351. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5352. ),
  5353. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5354. '/form',
  5355. array(
  5356. 'form' => array(
  5357. 'method' => 'post', 'action' => '/posts/delete/2',
  5358. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5359. ),
  5360. ),
  5361. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  5362. '/form'
  5363. ));
  5364. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  5365. $this->assertTags($result, array(
  5366. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5367. 'Delete',
  5368. '/a'
  5369. ));
  5370. $result = $this->View->fetch('foobar');
  5371. $this->assertTags($result, array(
  5372. 'form' => array(
  5373. 'method' => 'post', 'action' => '/posts/delete/1',
  5374. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5375. ),
  5376. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5377. '/form'
  5378. ));
  5379. }
  5380. /**
  5381. * testSubmitButton method
  5382. *
  5383. * @return void
  5384. */
  5385. public function testSubmitButton() {
  5386. $result = $this->Form->submit('');
  5387. $expected = array(
  5388. 'div' => array('class' => 'submit'),
  5389. 'input' => array('type' => 'submit', 'value' => ''),
  5390. '/div'
  5391. );
  5392. $this->assertTags($result, $expected);
  5393. $result = $this->Form->submit('Test Submit');
  5394. $expected = array(
  5395. 'div' => array('class' => 'submit'),
  5396. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5397. '/div'
  5398. );
  5399. $this->assertTags($result, $expected);
  5400. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5401. $expected = array(
  5402. 'span' => array('class' => 'submit'),
  5403. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5404. '/span'
  5405. );
  5406. $this->assertTags($result, $expected);
  5407. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5408. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5409. $this->assertTags($result, $expected);
  5410. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5411. $expected = array(
  5412. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5413. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5414. '/div'
  5415. );
  5416. $this->assertTags($result, $expected);
  5417. $result = $this->Form->submit('Next >');
  5418. $expected = array(
  5419. 'div' => array('class' => 'submit'),
  5420. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5421. '/div'
  5422. );
  5423. $this->assertTags($result, $expected);
  5424. $result = $this->Form->submit('Next >', array('escape' => false));
  5425. $expected = array(
  5426. 'div' => array('class' => 'submit'),
  5427. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5428. '/div'
  5429. );
  5430. $this->assertTags($result, $expected);
  5431. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5432. $expected = array(
  5433. 'div' => array('class' => 'submit'),
  5434. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5435. '/div'
  5436. );
  5437. $this->assertTags($result, $expected);
  5438. $before = '--before--';
  5439. $after = '--after--';
  5440. $result = $this->Form->submit('Test', array('before' => $before));
  5441. $expected = array(
  5442. 'div' => array('class' => 'submit'),
  5443. '--before--',
  5444. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5445. '/div'
  5446. );
  5447. $this->assertTags($result, $expected);
  5448. $result = $this->Form->submit('Test', array('after' => $after));
  5449. $expected = array(
  5450. 'div' => array('class' => 'submit'),
  5451. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5452. '--after--',
  5453. '/div'
  5454. );
  5455. $this->assertTags($result, $expected);
  5456. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5457. $expected = array(
  5458. 'div' => array('class' => 'submit'),
  5459. '--before--',
  5460. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5461. '--after--',
  5462. '/div'
  5463. );
  5464. $this->assertTags($result, $expected);
  5465. }
  5466. /**
  5467. * test image submit types.
  5468. *
  5469. * @return void
  5470. */
  5471. public function testSubmitImage() {
  5472. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5473. $expected = array(
  5474. 'div' => array('class' => 'submit'),
  5475. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5476. '/div'
  5477. );
  5478. $this->assertTags($result, $expected);
  5479. $result = $this->Form->submit('/relative/cake.power.gif');
  5480. $expected = array(
  5481. 'div' => array('class' => 'submit'),
  5482. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5483. '/div'
  5484. );
  5485. $this->assertTags($result, $expected);
  5486. $result = $this->Form->submit('cake.power.gif');
  5487. $expected = array(
  5488. 'div' => array('class' => 'submit'),
  5489. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5490. '/div'
  5491. );
  5492. $this->assertTags($result, $expected);
  5493. $result = $this->Form->submit('Not.an.image');
  5494. $expected = array(
  5495. 'div' => array('class' => 'submit'),
  5496. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5497. '/div'
  5498. );
  5499. $this->assertTags($result, $expected);
  5500. $after = '--after--';
  5501. $before = '--before--';
  5502. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5503. $expected = array(
  5504. 'div' => array('class' => 'submit'),
  5505. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5506. '--after--',
  5507. '/div'
  5508. );
  5509. $this->assertTags($result, $expected);
  5510. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5511. $expected = array(
  5512. 'div' => array('class' => 'submit'),
  5513. '--before--',
  5514. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5515. '/div'
  5516. );
  5517. $this->assertTags($result, $expected);
  5518. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5519. $expected = array(
  5520. 'div' => array('class' => 'submit'),
  5521. '--before--',
  5522. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5523. '--after--',
  5524. '/div'
  5525. );
  5526. $this->assertTags($result, $expected);
  5527. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5528. $expected = array(
  5529. 'div' => array('class' => 'submit'),
  5530. '--before--',
  5531. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5532. '--after--',
  5533. '/div'
  5534. );
  5535. $this->assertTags($result, $expected);
  5536. }
  5537. /**
  5538. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5539. * be submitted at a time.
  5540. *
  5541. * @return void
  5542. */
  5543. public function testSubmitUnlockedByDefault() {
  5544. $this->Form->request->params['_Token'] = 'secured';
  5545. $this->Form->submit('Go go');
  5546. $this->Form->submit('Save', array('name' => 'save'));
  5547. $result = $this->Form->unlockField();
  5548. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5549. }
  5550. /**
  5551. * Test submit image with timestamps.
  5552. *
  5553. * @return void
  5554. */
  5555. public function testSubmitImageTimestamp() {
  5556. Configure::write('Asset.timestamp', 'force');
  5557. $result = $this->Form->submit('cake.power.gif');
  5558. $expected = array(
  5559. 'div' => array('class' => 'submit'),
  5560. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5561. '/div'
  5562. );
  5563. $this->assertTags($result, $expected);
  5564. }
  5565. /**
  5566. * test that datetime() works with GET style forms.
  5567. *
  5568. * @return void
  5569. */
  5570. public function testDateTimeWithGetForms() {
  5571. extract($this->dateRegex);
  5572. $this->Form->create($this->article, array('type' => 'get'));
  5573. $result = $this->Form->datetime('created');
  5574. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  5575. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  5576. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  5577. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  5578. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  5579. $this->assertContains('name="created[meridian]"', $result, 'meridian name attribute is wrong.');
  5580. }
  5581. /**
  5582. * testFormMagicInput method
  5583. *
  5584. * @return void
  5585. */
  5586. public function testFormMagicInput() {
  5587. $this->markTestIncomplete('Need to revisit once models work again.');
  5588. $encoding = strtolower(Configure::read('App.encoding'));
  5589. TableRegistry::get('Contacts', [
  5590. 'className' => __NAMESPACE__ . '\ContactsTable'
  5591. ]);
  5592. $result = $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  5593. $expected = array(
  5594. 'form' => array(
  5595. 'method' => 'post', 'action' => '/articles/add',
  5596. 'accept-charset' => $encoding
  5597. ),
  5598. 'div' => array('style' => 'display:none;'),
  5599. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5600. '/div'
  5601. );
  5602. $this->assertTags($result, $expected);
  5603. $result = $this->Form->input('name');
  5604. $expected = array(
  5605. 'div' => array('class' => 'input text'),
  5606. 'label' => array('for' => 'name'),
  5607. 'Name',
  5608. '/label',
  5609. 'input' => array(
  5610. 'type' => 'text', 'name' => 'name',
  5611. 'id' => 'name', 'maxlength' => '255'
  5612. ),
  5613. '/div'
  5614. );
  5615. $this->assertTags($result, $expected);
  5616. $result = $this->Form->input('non_existing_field_in_contact_model');
  5617. $expected = array(
  5618. 'div' => array('class' => 'input text'),
  5619. 'label' => array('for' => 'non-existing-field-in-contact-model'),
  5620. 'Non Existing Field In Contact Model',
  5621. '/label',
  5622. 'input' => array(
  5623. 'type' => 'text', 'name' => 'non_existing_field_in_contact_model',
  5624. 'id' => 'non-existing-field-in-contact-model'
  5625. ),
  5626. '/div'
  5627. );
  5628. $this->assertTags($result, $expected);
  5629. extract($this->dateRegex);
  5630. $now = strtotime('now');
  5631. $result = $this->Form->input('Contact.published', array(
  5632. 'templates' => ['groupContainer' => '{{label}}{{input}}']
  5633. ));
  5634. $expected = array(
  5635. 'label' => array('for' => 'ContactPublishedMonth'),
  5636. 'Published',
  5637. '/label',
  5638. array('select' => array(
  5639. 'name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth'
  5640. )),
  5641. $monthsRegex,
  5642. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5643. date('F', $now),
  5644. '/option',
  5645. '*/select',
  5646. '-',
  5647. array('select' => array(
  5648. 'name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay'
  5649. )),
  5650. $daysRegex,
  5651. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5652. date('j', $now),
  5653. '/option',
  5654. '*/select',
  5655. '-',
  5656. array('select' => array(
  5657. 'name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear'
  5658. )),
  5659. $yearsRegex,
  5660. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5661. date('Y', $now),
  5662. '*/select'
  5663. );
  5664. $this->assertTags($result, $expected);
  5665. $result = $this->Form->input('Contact.updated', array('div' => false));
  5666. $expected = array(
  5667. 'label' => array('for' => 'ContactUpdatedMonth'),
  5668. 'Updated',
  5669. '/label',
  5670. array('select' => array(
  5671. 'name' => 'Contact[updated][month]', 'id' => 'ContactUpdatedMonth'
  5672. )),
  5673. $monthsRegex,
  5674. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5675. date('F', $now),
  5676. '/option',
  5677. '*/select',
  5678. '-',
  5679. array('select' => array(
  5680. 'name' => 'Contact[updated][day]', 'id' => 'ContactUpdatedDay'
  5681. )),
  5682. $daysRegex,
  5683. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5684. date('j', $now),
  5685. '/option',
  5686. '*/select',
  5687. '-',
  5688. array('select' => array(
  5689. 'name' => 'Contact[updated][year]', 'id' => 'ContactUpdatedYear'
  5690. )),
  5691. $yearsRegex,
  5692. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5693. date('Y', $now),
  5694. '*/select'
  5695. );
  5696. $this->assertTags($result, $expected);
  5697. $result = $this->Form->input('UserForm.stuff');
  5698. $expected = array(
  5699. 'div' => array('class' => 'input text'),
  5700. 'label' => array('for' => 'UserFormStuff'),
  5701. 'Stuff',
  5702. '/label',
  5703. 'input' => array(
  5704. 'type' => 'text', 'name' => 'UserForm[stuff]',
  5705. 'id' => 'UserFormStuff', 'maxlength' => 10
  5706. ),
  5707. '/div'
  5708. );
  5709. $this->assertTags($result, $expected);
  5710. }
  5711. /**
  5712. * testForMagicInputNonExistingNorValidated method
  5713. *
  5714. * @return void
  5715. */
  5716. public function testForMagicInputNonExistingNorValidated() {
  5717. $this->markTestIncomplete('Need to revisit once models work again.');
  5718. $encoding = strtolower(Configure::read('App.encoding'));
  5719. $result = $this->Form->create('Contact');
  5720. $expected = array(
  5721. 'form' => array(
  5722. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5723. 'accept-charset' => $encoding
  5724. ),
  5725. 'div' => array('style' => 'display:none;'),
  5726. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5727. '/div'
  5728. );
  5729. $this->assertTags($result, $expected);
  5730. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  5731. $expected = array(
  5732. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  5733. 'Non Existing Nor Validated',
  5734. '/label',
  5735. 'input' => array(
  5736. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  5737. 'id' => 'ContactNonExistingNorValidated'
  5738. )
  5739. );
  5740. $this->assertTags($result, $expected);
  5741. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  5742. 'div' => false, 'value' => 'my value'
  5743. ));
  5744. $expected = array(
  5745. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  5746. 'Non Existing Nor Validated',
  5747. '/label',
  5748. 'input' => array(
  5749. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  5750. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  5751. )
  5752. );
  5753. $this->assertTags($result, $expected);
  5754. $this->Form->request->data = array(
  5755. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  5756. ));
  5757. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  5758. $expected = array(
  5759. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  5760. 'Non Existing Nor Validated',
  5761. '/label',
  5762. 'input' => array(
  5763. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  5764. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  5765. )
  5766. );
  5767. $this->assertTags($result, $expected);
  5768. }
  5769. /**
  5770. * testFormMagicInputLabel method
  5771. *
  5772. * @return void
  5773. */
  5774. public function testFormMagicInputLabel() {
  5775. $this->markTestIncomplete('Need to revisit once models work again.');
  5776. TableRegistry::get('Contacts', [
  5777. 'className' => __NAMESPACE__ . '\ContactsTable'
  5778. ]);
  5779. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  5780. $this->Form->templates(['groupContainer' => '{{content}}']);
  5781. $result = $this->Form->input('Contacts.name', array('label' => 'My label'));
  5782. $expected = array(
  5783. 'label' => array('for' => 'contacts-name'),
  5784. 'My label',
  5785. '/label',
  5786. 'input' => array(
  5787. 'type' => 'text', 'name' => 'Contacts[name]',
  5788. 'id' => 'contacts-name', 'maxlength' => '255'
  5789. )
  5790. );
  5791. $this->assertTags($result, $expected);
  5792. $result = $this->Form->input('name', array(
  5793. 'label' => array('class' => 'mandatory')
  5794. ));
  5795. $expected = array(
  5796. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  5797. 'Name',
  5798. '/label',
  5799. 'input' => array(
  5800. 'type' => 'text', 'name' => 'name',
  5801. 'id' => 'name', 'maxlength' => '255'
  5802. )
  5803. );
  5804. $this->assertTags($result, $expected);
  5805. $result = $this->Form->input('name', array(
  5806. 'div' => false,
  5807. 'label' => array('class' => 'mandatory', 'text' => 'My label')
  5808. ));
  5809. $expected = array(
  5810. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  5811. 'My label',
  5812. '/label',
  5813. 'input' => array(
  5814. 'type' => 'text', 'name' => 'name',
  5815. 'id' => 'name', 'maxlength' => '255'
  5816. )
  5817. );
  5818. $this->assertTags($result, $expected);
  5819. $result = $this->Form->input('Contact.name', array(
  5820. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  5821. ));
  5822. $expected = array(
  5823. 'label' => array('for' => 'my_id'),
  5824. 'Name',
  5825. '/label',
  5826. 'input' => array(
  5827. 'type' => 'text', 'name' => 'Contact[name]',
  5828. 'id' => 'my_id', 'maxlength' => '255'
  5829. )
  5830. );
  5831. $this->assertTags($result, $expected);
  5832. $result = $this->Form->input('1.id');
  5833. $this->assertTags($result, array('input' => array(
  5834. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  5835. 'id' => 'Contact1Id'
  5836. )));
  5837. $result = $this->Form->input("1.name");
  5838. $expected = array(
  5839. 'div' => array('class' => 'input text'),
  5840. 'label' => array('for' => 'Contact1Name'),
  5841. 'Name',
  5842. '/label',
  5843. 'input' => array(
  5844. 'type' => 'text', 'name' => 'Contact[1][name]',
  5845. 'id' => 'Contact1Name', 'maxlength' => '255'
  5846. ),
  5847. '/div'
  5848. );
  5849. $this->assertTags($result, $expected);
  5850. $result = $this->Form->input('Contact.1.id');
  5851. $this->assertTags($result, array(
  5852. 'input' => array(
  5853. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  5854. 'id' => 'Contact1Id'
  5855. )
  5856. ));
  5857. $result = $this->Form->input("Model.1.name");
  5858. $expected = array(
  5859. 'div' => array('class' => 'input text'),
  5860. 'label' => array('for' => 'Model1Name'),
  5861. 'Name',
  5862. '/label',
  5863. 'input' => array(
  5864. 'type' => 'text', 'name' => 'Model[1][name]',
  5865. 'id' => 'Model1Name'
  5866. ),
  5867. '/div'
  5868. );
  5869. $this->assertTags($result, $expected);
  5870. }
  5871. /**
  5872. * testFormEnd method
  5873. *
  5874. * @return void
  5875. */
  5876. public function testFormEnd() {
  5877. $this->assertEquals('</form>', $this->Form->end());
  5878. }
  5879. /**
  5880. * testDbLessModel method
  5881. *
  5882. * @return void
  5883. */
  5884. public function testDbLessModel() {
  5885. $this->markTestIncomplete('Need to revisit once models work again.');
  5886. $this->Form->create('TestMail');
  5887. $result = $this->Form->input('name');
  5888. $expected = array(
  5889. 'div' => array('class' => 'input text'),
  5890. 'label' => array('for' => 'TestMailName'),
  5891. 'Name',
  5892. '/label',
  5893. 'input' => array(
  5894. 'name' => 'TestMail[name]', 'type' => 'text',
  5895. 'id' => 'TestMailName'
  5896. ),
  5897. '/div'
  5898. );
  5899. $this->assertTags($result, $expected);
  5900. $this->Form->create('TestMail');
  5901. $result = $this->Form->input('name');
  5902. $expected = array(
  5903. 'div' => array('class' => 'input text'),
  5904. 'label' => array('for' => 'TestMailName'),
  5905. 'Name',
  5906. '/label',
  5907. 'input' => array(
  5908. 'name' => 'TestMail[name]', 'type' => 'text',
  5909. 'id' => 'TestMailName'
  5910. ),
  5911. '/div'
  5912. );
  5913. $this->assertTags($result, $expected);
  5914. }
  5915. /**
  5916. * Test the generation of fields for a multi record form.
  5917. *
  5918. * @return void
  5919. */
  5920. public function testMultiRecordForm() {
  5921. $this->markTestIncomplete('Need to revisit once models work again.');
  5922. $this->Form->create('ValidateProfile');
  5923. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  5924. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  5925. $expected = array(
  5926. 'div' => array('class' => 'input textarea'),
  5927. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  5928. 'Name',
  5929. '/label',
  5930. 'textarea' => array(
  5931. 'id' => 'ValidateProfile1ValidateItem2Name',
  5932. 'name' => 'ValidateProfile[1][ValidateItem][2][name]',
  5933. 'cols' => 30,
  5934. 'rows' => 6
  5935. ),
  5936. 'Value',
  5937. '/textarea',
  5938. '/div'
  5939. );
  5940. $this->assertTags($result, $expected);
  5941. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  5942. $expected = array(
  5943. 'div' => array('class' => 'input date'),
  5944. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  5945. 'Created',
  5946. '/label',
  5947. array('select' => array(
  5948. 'name' => 'ValidateProfile[1][ValidateItem][2][created][month]',
  5949. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  5950. )
  5951. ),
  5952. array('option' => array('value' => '')), '/option',
  5953. $this->dateRegex['monthsRegex'],
  5954. '/select', '-',
  5955. array('select' => array(
  5956. 'name' => 'ValidateProfile[1][ValidateItem][2][created][day]',
  5957. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  5958. )
  5959. ),
  5960. array('option' => array('value' => '')), '/option',
  5961. $this->dateRegex['daysRegex'],
  5962. '/select', '-',
  5963. array('select' => array(
  5964. 'name' => 'ValidateProfile[1][ValidateItem][2][created][year]',
  5965. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  5966. )
  5967. ),
  5968. array('option' => array('value' => '')), '/option',
  5969. $this->dateRegex['yearsRegex'],
  5970. '/select',
  5971. '/div'
  5972. );
  5973. $this->assertTags($result, $expected);
  5974. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  5975. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  5976. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  5977. $expected = array(
  5978. 'div' => array('class' => 'input select error'),
  5979. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  5980. 'Profile',
  5981. '/label',
  5982. 'select' => array(
  5983. 'name' => 'ValidateProfile[1][ValidateItem][2][profile_id]',
  5984. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  5985. 'class' => 'form-error'
  5986. ),
  5987. '/select',
  5988. array('div' => array('class' => 'error-message')),
  5989. 'Error',
  5990. '/div',
  5991. '/div'
  5992. );
  5993. $this->assertTags($result, $expected);
  5994. }
  5995. /**
  5996. * test the correct display of multi-record form validation errors.
  5997. *
  5998. * @return void
  5999. */
  6000. public function testMultiRecordFormValidationErrors() {
  6001. $this->markTestIncomplete('Need to revisit once models work again.');
  6002. $this->Form->create('ValidateProfile');
  6003. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  6004. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  6005. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  6006. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  6007. $result = $this->Form->error('ValidateProfile.2.city');
  6008. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6009. $result = $this->Form->error('2.city');
  6010. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6011. }
  6012. /**
  6013. * test the correct display of multi-record form validation errors.
  6014. *
  6015. * @return void
  6016. */
  6017. public function testSaveManyRecordFormValidationErrors() {
  6018. $this->markTestIncomplete('Need to revisit once models work again.');
  6019. $this->Form->create('ValidateUser');
  6020. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  6021. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  6022. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  6023. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  6024. $result = $this->Form->error('ValidateUser.0.city');
  6025. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6026. }
  6027. /**
  6028. * test that some html5 inputs + FormHelper::__call() work
  6029. *
  6030. * @return void
  6031. */
  6032. public function testHtml5Inputs() {
  6033. $result = $this->Form->email('User.email');
  6034. $expected = array(
  6035. 'input' => array('type' => 'email', 'name' => 'User[email]')
  6036. );
  6037. $this->assertTags($result, $expected);
  6038. $result = $this->Form->search('User.query');
  6039. $expected = array(
  6040. 'input' => array('type' => 'search', 'name' => 'User[query]')
  6041. );
  6042. $this->assertTags($result, $expected);
  6043. $result = $this->Form->search('User.query', array('value' => 'test'));
  6044. $expected = array(
  6045. 'input' => array('type' => 'search', 'name' => 'User[query]', 'value' => 'test')
  6046. );
  6047. $this->assertTags($result, $expected);
  6048. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  6049. $expected = array(
  6050. 'input' => array('type' => 'text', 'name' => 'User[query]', 'value' => 'test')
  6051. );
  6052. $this->assertTags($result, $expected);
  6053. }
  6054. /**
  6055. * Test accessing html5 inputs through input().
  6056. *
  6057. * @return void
  6058. */
  6059. public function testHtml5InputWithInput() {
  6060. $this->Form->create();
  6061. $this->Form->templates(['groupContainer' => '{{content}}']);
  6062. $result = $this->Form->input('website', array(
  6063. 'type' => 'url',
  6064. 'val' => 'http://domain.tld',
  6065. 'label' => false
  6066. ));
  6067. $expected = array(
  6068. 'input' => array('type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld')
  6069. );
  6070. $this->assertTags($result, $expected);
  6071. }
  6072. /**
  6073. * Test errors when field name is missing.
  6074. *
  6075. * @expectedException \Cake\Error\Exception
  6076. * @return void
  6077. */
  6078. public function testHtml5InputException() {
  6079. $this->Form->email();
  6080. }
  6081. /**
  6082. * Tests that formhelper sets required attributes.
  6083. *
  6084. * @return void
  6085. */
  6086. public function testRequiredAttribute() {
  6087. $this->article['required'] = [
  6088. 'title' => true,
  6089. 'body' => false,
  6090. ];
  6091. $this->Form->create($this->article);
  6092. $result = $this->Form->input('title');
  6093. $expected = array(
  6094. 'div' => array('class' => 'input text required'),
  6095. 'label' => array('for' => 'title'),
  6096. 'Title',
  6097. '/label',
  6098. 'input' => array(
  6099. 'type' => 'text',
  6100. 'name' => 'title',
  6101. 'id' => 'title',
  6102. 'required' => 'required',
  6103. ),
  6104. '/div'
  6105. );
  6106. $this->assertTags($result, $expected);
  6107. $result = $this->Form->input('title', ['required' => false]);
  6108. $this->assertNotContains('required', $result);
  6109. $result = $this->Form->input('body');
  6110. $expected = array(
  6111. 'div' => array('class' => 'input text'),
  6112. 'label' => array('for' => 'body'),
  6113. 'Body',
  6114. '/label',
  6115. 'input' => array(
  6116. 'type' => 'text',
  6117. 'name' => 'body',
  6118. 'id' => 'body',
  6119. ),
  6120. '/div'
  6121. );
  6122. $this->assertTags($result, $expected);
  6123. $result = $this->Form->input('body', ['required' => true]);
  6124. $this->assertContains('required', $result);
  6125. }
  6126. }