FormHelperTest.php 265 KB

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