FormHelperTest.php 268 KB

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