FormHelperTest.php 311 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\View\Helper;
  17. use ArrayObject;
  18. use Cake\Chronos\ChronosTime;
  19. use Cake\Collection\Collection;
  20. use Cake\Core\Configure;
  21. use Cake\Core\Exception\CakeException;
  22. use Cake\Database\Type\EnumType;
  23. use Cake\Form\Form;
  24. use Cake\Http\ServerRequest;
  25. use Cake\I18n\Date;
  26. use Cake\I18n\DateTime;
  27. use Cake\ORM\Entity;
  28. use Cake\ORM\Table;
  29. use Cake\Routing\Router;
  30. use Cake\TestSuite\TestCase;
  31. use Cake\Utility\Security;
  32. use Cake\Validation\Validator;
  33. use Cake\View\Form\EntityContext;
  34. use Cake\View\Helper\FormHelper;
  35. use Cake\View\View;
  36. use Cake\View\Widget\WidgetLocator;
  37. use InvalidArgumentException;
  38. use ReflectionProperty;
  39. use TestApp\Model\Entity\Article;
  40. use TestApp\Model\Enum\ArticleStatus;
  41. use TestApp\Model\Enum\ArticleStatusLabel;
  42. use TestApp\Model\Enum\ArticleStatusLabelInterface;
  43. use TestApp\Model\Table\ContactsTable;
  44. use TestApp\Model\Table\ValidateUsersTable;
  45. use TestApp\View\Form\StubContext;
  46. /**
  47. * FormHelperTest class
  48. *
  49. * @property \Cake\View\Helper\FormHelper $Form
  50. * @property \Cake\View\View $View
  51. */
  52. class FormHelperTest extends TestCase
  53. {
  54. /**
  55. * Fixtures to be used
  56. *
  57. * @var array<string>
  58. */
  59. protected array $fixtures = ['core.Articles', 'core.Comments'];
  60. /**
  61. * @var array
  62. */
  63. protected $article = [];
  64. /**
  65. * @var string
  66. */
  67. protected $url;
  68. /**
  69. * @var \Cake\View\Helper\FormHelper
  70. */
  71. protected $Form;
  72. /**
  73. * @var \Cake\View\View
  74. */
  75. protected $View;
  76. /**
  77. * setUp method
  78. */
  79. public function setUp(): void
  80. {
  81. parent::setUp();
  82. Configure::write('Config.language', 'eng');
  83. Configure::write('App.base', '');
  84. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  85. $request = new ServerRequest([
  86. 'webroot' => '',
  87. 'base' => '',
  88. 'url' => '/articles/add',
  89. 'params' => [
  90. 'controller' => 'Articles',
  91. 'action' => 'add',
  92. 'plugin' => null,
  93. ],
  94. ]);
  95. $this->View = new View($request);
  96. Router::reload();
  97. Router::setRequest($request);
  98. $this->url = '/articles/add';
  99. $this->Form = new FormHelper($this->View);
  100. $this->article = [
  101. 'schema' => [
  102. 'id' => ['type' => 'integer'],
  103. 'author_id' => ['type' => 'integer', 'null' => true],
  104. 'title' => ['type' => 'string', 'null' => true],
  105. 'body' => 'text',
  106. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  107. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  108. ],
  109. 'required' => [
  110. 'author_id' => true,
  111. 'title' => true,
  112. ],
  113. ];
  114. Security::setSalt('foo!');
  115. $builder = Router::createRouteBuilder('/');
  116. $builder->connect('/{controller}', ['action' => 'index']);
  117. $builder->connect('/{controller}/{action}/*');
  118. }
  119. /**
  120. * tearDown method
  121. */
  122. public function tearDown(): void
  123. {
  124. parent::tearDown();
  125. unset($this->Form, $this->View);
  126. }
  127. /**
  128. * Test construct() with the templates option.
  129. */
  130. public function testConstructTemplatesFile(): void
  131. {
  132. $helper = new FormHelper($this->View, [
  133. 'templates' => 'htmlhelper_tags',
  134. ]);
  135. $result = $helper->control('name');
  136. $this->assertStringContainsString('<input', $result);
  137. }
  138. /**
  139. * Test that when specifying custom widgets the config array for that widget
  140. * is overwritten instead of merged.
  141. */
  142. public function testConstructWithWidgets(): void
  143. {
  144. $config = [
  145. 'widgets' => [
  146. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  147. ],
  148. ];
  149. $helper = new FormHelper($this->View, $config);
  150. $locator = $helper->getWidgetLocator();
  151. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  152. }
  153. /**
  154. * Test that when specifying custom widgets config file and it should be
  155. * added to widgets array. WidgetLocator will load widgets in constructor.
  156. */
  157. public function testConstructWithWidgetsConfig(): void
  158. {
  159. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  160. $locator = $helper->getWidgetLocator();
  161. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  162. }
  163. /**
  164. * Test setting the widget locator
  165. */
  166. public function testSetAndGetWidgetLocator(): void
  167. {
  168. $helper = new FormHelper($this->View);
  169. $locator = new WidgetLocator($helper->templater(), $this->View);
  170. $helper->setWidgetLocator($locator);
  171. $this->assertSame($locator, $helper->getWidgetLocator());
  172. }
  173. /**
  174. * Test overridding grouped input types which controls generation of "for"
  175. * attribute of labels.
  176. */
  177. public function testConstructWithGroupedInputTypes(): void
  178. {
  179. $helper = new FormHelper($this->View, [
  180. 'groupedInputTypes' => ['radio'],
  181. ]);
  182. $result = $helper->control('when', ['type' => 'datetime-local']);
  183. $this->assertStringContainsString('<label for="when">When</label>', $result);
  184. }
  185. /**
  186. * Test registering a new widget class and rendering it.
  187. */
  188. public function testAddWidgetAndRenderWidget(): void
  189. {
  190. $data = [
  191. 'val' => 1,
  192. ];
  193. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  194. $this->Form->addWidget('test', $mock);
  195. $mock->expects($this->once())
  196. ->method('render')
  197. ->with($data)
  198. ->willReturn('HTML');
  199. $result = $this->Form->widget('test', $data);
  200. $this->assertSame('HTML', $result);
  201. }
  202. /**
  203. * Test that secureFields() of widget is called after calling render(),
  204. * not before.
  205. */
  206. public function testOrderForRenderingWidgetAndFetchingSecureFields(): void
  207. {
  208. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  209. 'unlockedFields' => [],
  210. ]));
  211. $data = [
  212. 'val' => 1,
  213. 'name' => 'test',
  214. ];
  215. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  216. $this->Form->addWidget('test', $mock);
  217. $mock->expects($this->once())
  218. ->method('render')
  219. ->with($data)
  220. ->willReturn('HTML');
  221. $mock->expects($this->once())
  222. ->method('secureFields')
  223. ->with($data)
  224. ->willReturn(['test']);
  225. $this->Form->create();
  226. $result = $this->Form->widget('test', $data + ['secure' => true]);
  227. $this->assertSame('HTML', $result);
  228. }
  229. /**
  230. * Test that empty string is not added to secure fields list when
  231. * rendering input widget without name.
  232. */
  233. public function testRenderingWidgetWithEmptyName(): void
  234. {
  235. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  236. $this->Form->create();
  237. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  238. $this->assertSame('<select name=""></select>', $result);
  239. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  240. $this->assertEquals([], $result);
  241. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  242. $this->assertSame('<select name="0"></select>', $result);
  243. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  244. $this->assertEquals(['0'], $result);
  245. }
  246. /**
  247. * Test adding a new context class.
  248. */
  249. public function testAddContextProvider(): void
  250. {
  251. $context = 'My data';
  252. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  253. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  254. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  255. $this->assertSame($context, $data['entity']);
  256. return $stub;
  257. });
  258. $this->Form->create($context);
  259. $result = $this->Form->context();
  260. $this->assertSame($stub, $result);
  261. }
  262. /**
  263. * Test replacing a context class.
  264. */
  265. public function testAddContextProviderReplace(): void
  266. {
  267. $entity = new Article();
  268. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  269. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  270. return $stub;
  271. });
  272. $this->Form->create($entity);
  273. $result = $this->Form->context();
  274. $this->assertSame($stub, $result);
  275. }
  276. /**
  277. * Test overriding a context class.
  278. */
  279. public function testAddContextProviderAdd(): void
  280. {
  281. $entity = new Article();
  282. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  283. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  284. if ($data['entity'] instanceof Entity) {
  285. return $stub;
  286. }
  287. });
  288. $this->Form->create($entity);
  289. $result = $this->Form->context();
  290. $this->assertSame($stub, $result);
  291. }
  292. /**
  293. * Provides context options for create().
  294. *
  295. * @return array
  296. */
  297. public static function contextSelectionProvider(): array
  298. {
  299. $entity = new Article();
  300. $collection = new Collection([$entity]);
  301. $emptyCollection = new Collection([]);
  302. $data = [
  303. 'schema' => [
  304. 'title' => ['type' => 'string'],
  305. ],
  306. ];
  307. $form = new Form();
  308. $custom = new StubContext();
  309. return [
  310. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  311. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  312. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  313. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  314. 'form' => [$form, 'Cake\View\Form\FormContext'],
  315. 'none' => [null, 'Cake\View\Form\NullContext'],
  316. 'custom' => [$custom, $custom::class],
  317. ];
  318. }
  319. /**
  320. * Test default context selection in create()
  321. *
  322. * @dataProvider contextSelectionProvider
  323. * @param mixed $data
  324. */
  325. public function testCreateContextSelectionBuiltIn($data, string $class): void
  326. {
  327. $this->Form->create($data);
  328. $this->assertInstanceOf($class, $this->Form->context());
  329. }
  330. /**
  331. * Data provider for type option.
  332. *
  333. * @return array
  334. */
  335. public static function requestTypeProvider(): array
  336. {
  337. return [
  338. // type, method, override
  339. ['post', 'post', 'POST'],
  340. ['put', 'post', 'PUT'],
  341. ['patch', 'post', 'PATCH'],
  342. ['delete', 'post', 'DELETE'],
  343. ];
  344. }
  345. /**
  346. * Test creating file forms.
  347. */
  348. public function testCreateFile(): void
  349. {
  350. $encoding = strtolower(Configure::read('App.encoding'));
  351. $result = $this->Form->create(null, ['type' => 'file']);
  352. $expected = [
  353. 'form' => [
  354. 'method' => 'post', 'action' => '/articles/add',
  355. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  356. ],
  357. 'div' => ['style' => 'display:none;'],
  358. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  359. '/div',
  360. ];
  361. $this->assertHtml($expected, $result);
  362. }
  363. /**
  364. * Test creating GET forms.
  365. */
  366. public function testCreateGet(): void
  367. {
  368. $encoding = strtolower(Configure::read('App.encoding'));
  369. $result = $this->Form->create(null, ['type' => 'get']);
  370. $expected = ['form' => [
  371. 'method' => 'get', 'action' => '/articles/add',
  372. 'accept-charset' => $encoding,
  373. ]];
  374. $this->assertHtml($expected, $result);
  375. }
  376. /**
  377. * Test explicit method/enctype options.
  378. *
  379. * Explicit method overwrites inferred method from 'type'
  380. */
  381. public function testCreateExplicitMethodEnctype(): void
  382. {
  383. $encoding = strtolower(Configure::read('App.encoding'));
  384. $result = $this->Form->create(null, [
  385. 'type' => 'get',
  386. 'method' => 'put',
  387. 'enctype' => 'multipart/form-data',
  388. ]);
  389. $expected = ['form' => [
  390. 'method' => 'put',
  391. 'action' => '/articles/add',
  392. 'enctype' => 'multipart/form-data',
  393. 'accept-charset' => $encoding,
  394. ]];
  395. $this->assertHtml($expected, $result);
  396. }
  397. /**
  398. * Test create() with the templates option.
  399. */
  400. public function testCreateTemplatesArray(): void
  401. {
  402. $result = $this->Form->create($this->article, [
  403. 'templates' => [
  404. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  405. ],
  406. ]);
  407. $expected = [
  408. 'form' => [
  409. 'class' => 'form-horizontal',
  410. 'method' => 'post',
  411. 'action' => '/articles/add',
  412. 'accept-charset' => 'utf-8',
  413. ],
  414. ];
  415. $this->assertHtml($expected, $result);
  416. }
  417. /**
  418. * Test create() with the templates option.
  419. */
  420. public function testCreateTemplatesFile(): void
  421. {
  422. $result = $this->Form->create($this->article, [
  423. 'templates' => 'htmlhelper_tags',
  424. ]);
  425. $expected = [
  426. 'start form',
  427. ];
  428. $this->assertHtml($expected, $result);
  429. }
  430. /**
  431. * Test that create() and end() restore templates.
  432. */
  433. public function testCreateEndRestoreTemplates(): void
  434. {
  435. $this->Form->create($this->article, [
  436. 'templates' => ['input' => 'custom input element'],
  437. ]);
  438. $this->Form->end();
  439. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  440. }
  441. /**
  442. * Test create() with the templates option.
  443. */
  444. public function testCreateTemplatesRequiredClass(): void
  445. {
  446. $this->Form->create($this->article, [
  447. 'templates' => [
  448. 'requiredClass' => 'is-required',
  449. ],
  450. ]);
  451. $result = $this->Form->control('title');
  452. $expected = [
  453. 'div' => ['class' => 'input text is-required'],
  454. 'label' => ['for' => 'title'],
  455. 'Title',
  456. '/label',
  457. 'input' => [
  458. 'type' => 'text',
  459. 'name' => 'title',
  460. 'id' => 'title',
  461. 'required' => 'required',
  462. 'data-validity-message' => 'This field cannot be left empty',
  463. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  464. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  465. ],
  466. '/div',
  467. ];
  468. $this->assertHtml($expected, $result);
  469. }
  470. /**
  471. * Test using template vars in various templates used by control() method.
  472. */
  473. public function testControlTemplateVars(): void
  474. {
  475. $result = $this->Form->control('text', [
  476. 'templates' => [
  477. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  478. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  479. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  480. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  481. ],
  482. 'templateVars' => [
  483. 'forinput' => 'in-input',
  484. 'forlabel' => 'in-label',
  485. 'forgroup' => 'in-group',
  486. 'forcontainer' => 'in-container',
  487. ],
  488. ]);
  489. $expected = [
  490. 'div' => ['class'],
  491. 'label' => ['for'],
  492. 'Text in-label',
  493. '/label',
  494. 'in-group',
  495. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  496. 'in-container',
  497. '/div',
  498. ];
  499. $this->assertHtml($expected, $result);
  500. }
  501. /**
  502. * Test ensuring template variables work in template files loaded
  503. * during control().
  504. */
  505. public function testControlTemplatesFromFile(): void
  506. {
  507. $result = $this->Form->control('title', [
  508. 'templates' => 'test_templates',
  509. 'templateVars' => [
  510. 'forcontainer' => 'container-data',
  511. ],
  512. ]);
  513. $expected = [
  514. 'div' => ['class'],
  515. 'label' => ['for'],
  516. 'Title',
  517. '/label',
  518. 'input' => ['name', 'type' => 'text', 'id'],
  519. 'container-data',
  520. '/div',
  521. ];
  522. $this->assertHtml($expected, $result);
  523. }
  524. /**
  525. * Test using template vars in inputSubmit and submitContainer template.
  526. */
  527. public function testSubmitTemplateVars(): void
  528. {
  529. $this->Form->setTemplates([
  530. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  531. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  532. ]);
  533. $result = $this->Form->submit('Submit', [
  534. 'templateVars' => [
  535. 'forinput' => 'in-input',
  536. 'forcontainer' => 'in-container',
  537. ],
  538. ]);
  539. $expected = [
  540. 'div' => ['class'],
  541. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  542. 'in-container',
  543. '/div',
  544. ];
  545. $this->assertHtml($expected, $result);
  546. }
  547. /**
  548. * test the create() method
  549. *
  550. * @dataProvider requestTypeProvider
  551. */
  552. public function testCreateTypeOptions(string $type, string $method, string $override): void
  553. {
  554. $encoding = strtolower(Configure::read('App.encoding'));
  555. $result = $this->Form->create(null, ['type' => $type]);
  556. $expected = [
  557. 'form' => [
  558. 'method' => $method, 'action' => '/articles/add',
  559. 'accept-charset' => $encoding,
  560. ],
  561. ];
  562. $extra = [
  563. 'div' => ['style' => 'display:none;'],
  564. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  565. '/div',
  566. ];
  567. if ($type !== 'post') {
  568. $expected = array_merge($expected, $extra);
  569. }
  570. $this->assertHtml($expected, $result);
  571. }
  572. /**
  573. * Test using template vars in Create (formStart template)
  574. */
  575. public function testCreateTemplateVars(): void
  576. {
  577. $result = $this->Form->create($this->article, [
  578. 'templates' => [
  579. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  580. ],
  581. 'templateVars' => ['header' => 'headertext'],
  582. ]);
  583. $expected = [
  584. 'h4' => ['class'],
  585. 'headertext',
  586. '/h4',
  587. 'form' => [
  588. 'method' => 'post',
  589. 'action' => '/articles/add',
  590. 'accept-charset' => 'utf-8',
  591. ],
  592. ];
  593. $this->assertHtml($expected, $result);
  594. }
  595. /**
  596. * Test opening a form for an update operation.
  597. */
  598. public function testCreateUpdateForm(): void
  599. {
  600. $encoding = strtolower(Configure::read('App.encoding'));
  601. $this->View->setRequest($this->View->getRequest()
  602. ->withRequestTarget('/articles/edit/1')
  603. ->withParam('action', 'edit'));
  604. $this->article['defaults']['id'] = 1;
  605. $result = $this->Form->create($this->article);
  606. $expected = [
  607. 'form' => [
  608. 'method' => 'post',
  609. 'action' => '/articles/edit/1',
  610. 'accept-charset' => $encoding,
  611. ],
  612. 'div' => ['style' => 'display:none;'],
  613. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  614. '/div',
  615. ];
  616. $this->assertHtml($expected, $result);
  617. }
  618. /**
  619. * test create() with automatic url generation
  620. */
  621. public function testCreateAutoUrl(): void
  622. {
  623. $encoding = strtolower(Configure::read('App.encoding'));
  624. $this->View->setRequest($this->View->getRequest()
  625. ->withRequestTarget('/articles/delete/10')
  626. ->withParam('action', 'delete'));
  627. $result = $this->Form->create($this->article);
  628. $expected = [
  629. 'form' => [
  630. 'method' => 'post', 'action' => '/articles/delete/10',
  631. 'accept-charset' => $encoding,
  632. ],
  633. ];
  634. $this->assertHtml($expected, $result);
  635. $this->article['defaults'] = ['id' => 1];
  636. $this->View->setRequest($this->View->getRequest()
  637. ->withRequestTarget('/Articles/edit/1')
  638. ->withParam('action', 'delete'));
  639. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  640. $expected = [
  641. 'form' => [
  642. 'method' => 'post',
  643. 'action' => '/Articles/edit/1',
  644. 'accept-charset' => $encoding,
  645. ],
  646. 'div' => ['style' => 'display:none;'],
  647. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  648. '/div',
  649. ];
  650. $this->assertHtml($expected, $result);
  651. $this->View->setRequest($this->View->getRequest()
  652. ->withParam('action', 'add'));
  653. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  654. $expected = [
  655. 'form' => [
  656. 'method' => 'post',
  657. 'action' => '/Articles/publish/1',
  658. 'accept-charset' => $encoding,
  659. ],
  660. 'div' => ['style' => 'display:none;'],
  661. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  662. '/div',
  663. ];
  664. $this->assertHtml($expected, $result);
  665. $result = $this->Form->create($this->article, ['url' => '/Articles/publish']);
  666. $expected = [
  667. 'form' => ['method' => 'post', 'action' => '/Articles/publish', 'accept-charset' => $encoding],
  668. 'div' => ['style' => 'display:none;'],
  669. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  670. '/div',
  671. ];
  672. $this->assertHtml($expected, $result);
  673. $this->View->setRequest($this->View->getRequest()
  674. ->withParam('controller', 'Pages'));
  675. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  676. $expected = [
  677. 'form' => [
  678. 'method' => 'post', 'action' => '/Pages/signup/1',
  679. 'accept-charset' => $encoding,
  680. ],
  681. 'div' => ['style' => 'display:none;'],
  682. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  683. '/div',
  684. ];
  685. $this->assertHtml($expected, $result);
  686. }
  687. /**
  688. * Test create() with no URL (no "action" attribute for <form> tag)
  689. */
  690. public function testCreateNoUrl(): void
  691. {
  692. $result = $this->Form->create(null, ['url' => false]);
  693. $expected = [
  694. 'form' => [
  695. 'method' => 'post',
  696. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  697. ],
  698. ];
  699. $this->assertHtml($expected, $result);
  700. }
  701. /**
  702. * test create() with a custom route
  703. */
  704. public function testCreateCustomRoute(): void
  705. {
  706. $builder = Router::createRouteBuilder('/');
  707. $builder->connect('/login', ['controller' => 'Users', 'action' => 'login']);
  708. $encoding = strtolower(Configure::read('App.encoding'));
  709. $this->View->setRequest($this->View->getRequest()
  710. ->withParam('controller', 'Users'));
  711. $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
  712. $expected = [
  713. 'form' => [
  714. 'method' => 'post', 'action' => '/login',
  715. 'accept-charset' => $encoding,
  716. ],
  717. ];
  718. $this->assertHtml($expected, $result);
  719. $builder->connect(
  720. '/new-article',
  721. ['controller' => 'Articles', 'action' => 'myAction'],
  722. ['_name' => 'my-route']
  723. );
  724. $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
  725. $expected = [
  726. 'form' => [
  727. 'method' => 'post', 'action' => '/new-article',
  728. 'accept-charset' => $encoding,
  729. ],
  730. ];
  731. $this->assertHtml($expected, $result);
  732. }
  733. /**
  734. * test automatic accept-charset overriding
  735. */
  736. public function testCreateWithAcceptCharset(): void
  737. {
  738. $result = $this->Form->create(
  739. $this->article,
  740. [
  741. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  742. ]
  743. );
  744. $expected = [
  745. 'form' => [
  746. 'method' => 'post', 'action' => '/Articles',
  747. 'accept-charset' => 'iso-8859-1',
  748. ],
  749. ];
  750. $this->assertHtml($expected, $result);
  751. }
  752. /**
  753. * Test base form URL when 'url' param is passed with multiple parameters (&)
  754. */
  755. public function testCreateQueryStringRequest(): void
  756. {
  757. $encoding = strtolower(Configure::read('App.encoding'));
  758. $result = $this->Form->create($this->article, [
  759. 'type' => 'post',
  760. 'escape' => false,
  761. 'url' => [
  762. 'controller' => 'Controller',
  763. 'action' => 'action',
  764. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  765. ],
  766. ]);
  767. $expected = [
  768. 'form' => [
  769. 'method' => 'post',
  770. 'action' => '/Controller/action?param1=value1&amp;param2=value2',
  771. 'accept-charset' => $encoding,
  772. ],
  773. ];
  774. $this->assertHtml($expected, $result);
  775. $result = $this->Form->create($this->article, [
  776. 'type' => 'post',
  777. 'url' => [
  778. 'controller' => 'Controller',
  779. 'action' => 'action',
  780. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  781. ],
  782. ]);
  783. $this->assertHtml($expected, $result);
  784. }
  785. /**
  786. * test that create() doesn't cause errors by multiple id's being in the primary key
  787. * as could happen with multiple select or checkboxes.
  788. */
  789. public function testCreateWithMultipleIdInData(): void
  790. {
  791. $encoding = strtolower(Configure::read('App.encoding'));
  792. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  793. $result = $this->Form->create($this->article);
  794. $expected = [
  795. 'form' => [
  796. 'method' => 'post',
  797. 'action' => '/articles/add',
  798. 'accept-charset' => $encoding,
  799. ],
  800. ];
  801. $this->assertHtml($expected, $result);
  802. }
  803. /**
  804. * test that create() doesn't add in extra passed params.
  805. */
  806. public function testCreatePassedArgs(): void
  807. {
  808. $encoding = strtolower(Configure::read('App.encoding'));
  809. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  810. $result = $this->Form->create($this->article, [
  811. 'type' => 'post',
  812. 'escape' => false,
  813. 'url' => [
  814. 'action' => 'edit',
  815. 'myparam',
  816. ],
  817. ]);
  818. $expected = [
  819. 'form' => [
  820. 'method' => 'post',
  821. 'action' => '/Articles/edit/myparam',
  822. 'accept-charset' => $encoding,
  823. ],
  824. ];
  825. $this->assertHtml($expected, $result);
  826. }
  827. /**
  828. * test creating a get form, and get form inputs.
  829. */
  830. public function testGetFormCreate(): void
  831. {
  832. $encoding = strtolower(Configure::read('App.encoding'));
  833. $result = $this->Form->create($this->article, ['type' => 'get']);
  834. $expected = ['form' => [
  835. 'method' => 'get', 'action' => '/articles/add',
  836. 'accept-charset' => $encoding,
  837. ]];
  838. $this->assertHtml($expected, $result);
  839. $result = $this->Form->text('title');
  840. $expected = ['input' => [
  841. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  842. ]];
  843. $this->assertHtml($expected, $result);
  844. $result = $this->Form->password('password');
  845. $expected = ['input' => [
  846. 'name' => 'password', 'type' => 'password',
  847. ]];
  848. $this->assertHtml($expected, $result);
  849. $this->assertDoesNotMatchRegularExpression('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  850. $result = $this->Form->text('user_form');
  851. $expected = ['input' => [
  852. 'name' => 'user_form', 'type' => 'text',
  853. ]];
  854. $this->assertHtml($expected, $result);
  855. }
  856. /**
  857. * test get form, and inputs when the model param is false
  858. */
  859. public function testGetFormWithFalseModel(): void
  860. {
  861. $encoding = strtolower(Configure::read('App.encoding'));
  862. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'ContactTest'));
  863. $result = $this->Form->create(null, [
  864. 'type' => 'get', 'url' => ['controller' => 'ContactTest'],
  865. ]);
  866. $expected = ['form' => [
  867. 'method' => 'get', 'action' => '/ContactTest/add',
  868. 'accept-charset' => $encoding,
  869. ]];
  870. $this->assertHtml($expected, $result);
  871. $result = $this->Form->text('reason');
  872. $expected = [
  873. 'input' => ['type' => 'text', 'name' => 'reason'],
  874. ];
  875. $this->assertHtml($expected, $result);
  876. }
  877. /**
  878. * testFormCreateWithSecurity method
  879. *
  880. * Test form->create() with security key.
  881. */
  882. public function testCreateWithSecurity(): void
  883. {
  884. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  885. $encoding = strtolower(Configure::read('App.encoding'));
  886. $result = $this->Form->create($this->article, [
  887. 'url' => '/articles/publish',
  888. ]);
  889. $expected = [
  890. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  891. 'div' => ['style' => 'display:none;'],
  892. ['input' => [
  893. 'type' => 'hidden',
  894. 'name' => '_csrfToken',
  895. 'value' => 'testKey',
  896. ]],
  897. '/div',
  898. ];
  899. $this->assertHtml($expected, $result);
  900. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  901. $expected['form']['id'] = 'MyForm';
  902. $this->assertHtml($expected, $result);
  903. }
  904. /**
  905. * testFormCreateGetNoSecurity method
  906. *
  907. * Test form->create() with no security key as its a get form
  908. */
  909. public function testCreateEndGetNoSecurity(): void
  910. {
  911. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  912. $article = new Article();
  913. $result = $this->Form->create($article, [
  914. 'type' => 'get',
  915. 'url' => '/contacts/add',
  916. ]);
  917. $this->assertStringNotContainsString('testKey', $result);
  918. $result = $this->Form->end();
  919. $this->assertStringNotContainsString('testKey', $result);
  920. }
  921. /**
  922. * Tests form hash generation with model-less data
  923. */
  924. public function testValidateHashNoModel(): void
  925. {
  926. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  927. $fields = ['anything'];
  928. $this->Form->create();
  929. $result = $this->Form->secure($fields);
  930. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  931. $this->assertStringContainsString($hash, $result);
  932. }
  933. /**
  934. * Tests that hidden fields generated for checkboxes don't get locked
  935. */
  936. public function testNoCheckboxLocking(): void
  937. {
  938. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  939. $this->Form->create();
  940. $this->assertSame([], $this->Form->getFormProtector()->__debugInfo()['fields']);
  941. $this->Form->checkbox('check', ['value' => '1']);
  942. $this->assertSame(['check'], $this->Form->getFormProtector()->__debugInfo()['fields']);
  943. }
  944. /**
  945. * testFormSecurityFields method
  946. *
  947. * Test generation of secure form hash generation.
  948. */
  949. public function testFormSecurityFields(): void
  950. {
  951. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  952. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  953. $this->Form->create();
  954. $result = $this->Form->secure($fields);
  955. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  956. $hash .= ':' . 'Model.valid';
  957. $hash = urlencode($hash);
  958. $tokenDebug = urlencode(json_encode([
  959. $this->url,
  960. $fields,
  961. [],
  962. ]));
  963. $expected = [
  964. 'div' => ['style' => 'display:none;'],
  965. ['input' => [
  966. 'type' => 'hidden',
  967. 'name' => '_Token[fields]',
  968. 'value' => $hash,
  969. ]],
  970. ['input' => [
  971. 'type' => 'hidden',
  972. 'name' => '_Token[unlocked]',
  973. 'value' => '',
  974. ]],
  975. ['input' => [
  976. 'type' => 'hidden',
  977. 'name' => '_Token[debug]',
  978. 'value' => $tokenDebug,
  979. ]],
  980. '/div',
  981. ];
  982. $this->assertHtml($expected, $result);
  983. }
  984. /**
  985. * testFormSecurityFields method
  986. *
  987. * Test debug token is not generated if debug is false
  988. */
  989. public function testFormSecurityFieldsNoDebugMode(): void
  990. {
  991. Configure::write('debug', false);
  992. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  993. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  994. $this->Form->create();
  995. $result = $this->Form->secure($fields);
  996. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  997. $hash .= ':' . 'Model.valid';
  998. $hash = urlencode($hash);
  999. $expected = [
  1000. 'div' => ['style' => 'display:none;'],
  1001. ['input' => [
  1002. 'type' => 'hidden',
  1003. 'name' => '_Token[fields]',
  1004. 'value' => $hash,
  1005. ]],
  1006. ['input' => [
  1007. 'type' => 'hidden',
  1008. 'name' => '_Token[unlocked]',
  1009. 'value' => '',
  1010. ]],
  1011. '/div',
  1012. ];
  1013. $this->assertHtml($expected, $result);
  1014. }
  1015. /**
  1016. * Tests correct generation of number fields for smallint
  1017. */
  1018. public function testTextFieldGenerationForSmallint(): void
  1019. {
  1020. $this->article['schema'] = [
  1021. 'foo' => [
  1022. 'type' => 'smallinteger',
  1023. 'null' => false,
  1024. 'default' => null,
  1025. 'length' => 10,
  1026. ],
  1027. ];
  1028. $this->Form->create($this->article);
  1029. $result = $this->Form->control('foo');
  1030. $this->assertStringContainsString('class="input number"', $result);
  1031. $this->assertStringContainsString('type="number"', $result);
  1032. }
  1033. /**
  1034. * Tests correct generation of number fields for tinyint
  1035. */
  1036. public function testTextFieldGenerationForTinyint(): void
  1037. {
  1038. $this->article['schema'] = [
  1039. 'foo' => [
  1040. 'type' => 'tinyinteger',
  1041. 'null' => false,
  1042. 'default' => null,
  1043. 'length' => 10,
  1044. ],
  1045. ];
  1046. $this->Form->create($this->article);
  1047. $result = $this->Form->control('foo');
  1048. $this->assertStringContainsString('class="input number"', $result);
  1049. $this->assertStringContainsString('type="number"', $result);
  1050. }
  1051. /**
  1052. * Tests correct generation of number fields for double and float fields
  1053. */
  1054. public function testTextFieldGenerationForFloats(): void
  1055. {
  1056. $this->article['schema'] = [
  1057. 'foo' => [
  1058. 'type' => 'float',
  1059. 'null' => false,
  1060. 'default' => null,
  1061. 'length' => 10,
  1062. ],
  1063. ];
  1064. $this->Form->create($this->article);
  1065. $result = $this->Form->control('foo');
  1066. $expected = [
  1067. 'div' => ['class' => 'input number'],
  1068. 'label' => ['for' => 'foo'],
  1069. 'Foo',
  1070. '/label',
  1071. ['input' => [
  1072. 'type' => 'number',
  1073. 'name' => 'foo',
  1074. 'id' => 'foo',
  1075. 'step' => 'any',
  1076. ]],
  1077. '/div',
  1078. ];
  1079. $this->assertHtml($expected, $result);
  1080. $result = $this->Form->control('foo', ['step' => 0.5]);
  1081. $expected = [
  1082. 'div' => ['class' => 'input number'],
  1083. 'label' => ['for' => 'foo'],
  1084. 'Foo',
  1085. '/label',
  1086. ['input' => [
  1087. 'type' => 'number',
  1088. 'name' => 'foo',
  1089. 'id' => 'foo',
  1090. 'step' => '0.5',
  1091. ]],
  1092. '/div',
  1093. ];
  1094. $this->assertHtml($expected, $result);
  1095. }
  1096. /**
  1097. * Tests correct generation of number fields for integer fields
  1098. */
  1099. public function testTextFieldTypeNumberGenerationForIntegers(): void
  1100. {
  1101. $this->getTableLocator()->get('Contacts', [
  1102. 'className' => ContactsTable::class,
  1103. ]);
  1104. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1105. $result = $this->Form->control('age');
  1106. $expected = [
  1107. 'div' => ['class' => 'input number'],
  1108. 'label' => ['for' => 'age'],
  1109. 'Age',
  1110. '/label',
  1111. ['input' => [
  1112. 'type' => 'number', 'name' => 'age',
  1113. 'id' => 'age',
  1114. ]],
  1115. '/div',
  1116. ];
  1117. $this->assertHtml($expected, $result);
  1118. }
  1119. /**
  1120. * Tests correct generation of file upload fields for binary fields
  1121. */
  1122. public function testFileUploadFieldTypeGenerationForBinaries(): void
  1123. {
  1124. $table = $this->getTableLocator()->get('Contacts', [
  1125. 'className' => ContactsTable::class,
  1126. ]);
  1127. $table->setSchema(['foo' => [
  1128. 'type' => 'binary',
  1129. 'null' => false,
  1130. 'default' => null,
  1131. 'length' => 1024,
  1132. ]]);
  1133. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1134. $result = $this->Form->control('foo');
  1135. $expected = [
  1136. 'div' => ['class' => 'input file'],
  1137. 'label' => ['for' => 'foo'],
  1138. 'Foo',
  1139. '/label',
  1140. ['input' => [
  1141. 'type' => 'file', 'name' => 'foo',
  1142. 'id' => 'foo',
  1143. ]],
  1144. '/div',
  1145. ];
  1146. $this->assertHtml($expected, $result);
  1147. }
  1148. /**
  1149. * testFormSecurityMultipleFields method
  1150. *
  1151. * Test secure() with multiple row form. Ensure hash is correct.
  1152. */
  1153. public function testFormSecurityMultipleFields(): void
  1154. {
  1155. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1156. $this->Form->create();
  1157. $fields = [
  1158. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1159. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1160. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1161. ];
  1162. $result = $this->Form->secure($fields);
  1163. $sortedFields = [
  1164. 'Model.0.password',
  1165. 'Model.0.username',
  1166. 'Model.1.password',
  1167. 'Model.1.username',
  1168. 'Model.0.hidden' => 'value',
  1169. 'Model.0.valid' => '0',
  1170. 'Model.1.hidden' => 'value',
  1171. 'Model.1.valid' => '0',
  1172. ];
  1173. $hash = hash_hmac('sha1', $this->url . serialize($sortedFields) . session_id(), Security::getSalt());
  1174. $hash .= ':Model.0.hidden|Model.0.valid|Model.1.hidden|Model.1.valid';
  1175. $hash = urlencode($hash);
  1176. $tokenDebug = urlencode(json_encode([
  1177. $this->url,
  1178. $fields,
  1179. [],
  1180. ]));
  1181. $expected = [
  1182. 'div' => ['style' => 'display:none;'],
  1183. ['input' => [
  1184. 'type' => 'hidden',
  1185. 'name' => '_Token[fields]',
  1186. 'value' => $hash,
  1187. ]],
  1188. ['input' => [
  1189. 'type' => 'hidden',
  1190. 'name' => '_Token[unlocked]',
  1191. 'value' => '',
  1192. ]],
  1193. ['input' => [
  1194. 'type' => 'hidden',
  1195. 'name' => '_Token[debug]',
  1196. 'value' => $tokenDebug,
  1197. ]],
  1198. '/div',
  1199. ];
  1200. $this->assertHtml($expected, $result);
  1201. }
  1202. /**
  1203. * testFormSecurityMultipleSubmitButtons
  1204. *
  1205. * test form submit generation and ensure that _Token is only created on end()
  1206. */
  1207. public function testFormSecurityMultipleSubmitButtons(): void
  1208. {
  1209. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1210. $this->Form->create($this->article);
  1211. $this->Form->text('Address.title');
  1212. $this->Form->text('Address.first_name');
  1213. $result = $this->Form->submit('Save', ['name' => 'save']);
  1214. $expected = [
  1215. 'div' => ['class' => 'submit'],
  1216. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1217. '/div',
  1218. ];
  1219. $this->assertHtml($expected, $result);
  1220. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1221. $expected = [
  1222. 'div' => ['class' => 'submit'],
  1223. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1224. '/div',
  1225. ];
  1226. $this->assertHtml($expected, $result);
  1227. $result = $this->Form->end();
  1228. $tokenDebug = urlencode(json_encode([
  1229. '/articles/add',
  1230. [
  1231. 'Address.title',
  1232. 'Address.first_name',
  1233. ],
  1234. ['save', 'cancel'],
  1235. ]));
  1236. $expected = [
  1237. 'div' => ['style' => 'display:none;'],
  1238. ['input' => [
  1239. 'type' => 'hidden',
  1240. 'name' => '_Token[fields]',
  1241. 'value',
  1242. ]],
  1243. ['input' => [
  1244. 'type' => 'hidden',
  1245. 'name' => '_Token[unlocked]',
  1246. 'value' => 'cancel%7Csave',
  1247. ]],
  1248. ['input' => [
  1249. 'type' => 'hidden',
  1250. 'name' => '_Token[debug]',
  1251. 'value' => $tokenDebug,
  1252. ]],
  1253. '/div',
  1254. ];
  1255. $this->assertHtml($expected, $result);
  1256. }
  1257. /**
  1258. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1259. */
  1260. public function testSecurityButtonNestedNamed(): void
  1261. {
  1262. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1263. $this->Form->create();
  1264. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1265. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1266. $this->assertEquals(['Address.button'], $result);
  1267. }
  1268. /**
  1269. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1270. */
  1271. public function testSecuritySubmitNestedNamed(): void
  1272. {
  1273. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1274. $this->Form->create($this->article);
  1275. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1276. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1277. $this->assertEquals(['Address.button'], $result);
  1278. }
  1279. /**
  1280. * Test that the correct fields are unlocked for image submits with no names.
  1281. */
  1282. public function testSecuritySubmitImageNoName(): void
  1283. {
  1284. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1285. $this->Form->create();
  1286. $result = $this->Form->submit('save.png');
  1287. $expected = [
  1288. 'div' => ['class' => 'submit'],
  1289. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1290. '/div',
  1291. ];
  1292. $this->assertHtml($expected, $result);
  1293. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1294. $this->assertEquals(['x', 'y'], $result);
  1295. }
  1296. /**
  1297. * Test that the correct fields are unlocked for image submits with names.
  1298. */
  1299. public function testSecuritySubmitImageName(): void
  1300. {
  1301. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1302. $this->Form->create();
  1303. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1304. $expected = [
  1305. 'div' => ['class' => 'submit'],
  1306. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1307. '/div',
  1308. ];
  1309. $this->assertHtml($expected, $result);
  1310. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1311. $this->assertEquals(['test', 'test_x', 'test_y'], $result);
  1312. }
  1313. /**
  1314. * testFormSecurityMultipleControlFields method
  1315. *
  1316. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1317. */
  1318. public function testFormSecurityMultipleControlFields(): void
  1319. {
  1320. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1321. $this->Form->create();
  1322. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1323. $this->Form->control('Addresses.0.title');
  1324. $this->Form->control('Addresses.0.first_name');
  1325. $this->Form->control('Addresses.0.last_name');
  1326. $this->Form->control('Addresses.0.address');
  1327. $this->Form->control('Addresses.0.city');
  1328. $this->Form->control('Addresses.0.phone');
  1329. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1330. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1331. $this->Form->control('Addresses.1.title');
  1332. $this->Form->control('Addresses.1.first_name');
  1333. $this->Form->control('Addresses.1.last_name');
  1334. $this->Form->control('Addresses.1.address');
  1335. $this->Form->control('Addresses.1.city');
  1336. $this->Form->control('Addresses.1.phone');
  1337. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1338. $result = $this->Form->secure();
  1339. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1340. $tokenDebug = urlencode(json_encode([
  1341. '/articles/add',
  1342. [
  1343. 'Addresses.0.id' => '123456',
  1344. 'Addresses.0.title',
  1345. 'Addresses.0.first_name',
  1346. 'Addresses.0.last_name',
  1347. 'Addresses.0.address',
  1348. 'Addresses.0.city',
  1349. 'Addresses.0.phone',
  1350. 'Addresses.0.primary',
  1351. 'Addresses.1.id' => '654321',
  1352. 'Addresses.1.title',
  1353. 'Addresses.1.first_name',
  1354. 'Addresses.1.last_name',
  1355. 'Addresses.1.address',
  1356. 'Addresses.1.city',
  1357. 'Addresses.1.phone',
  1358. 'Addresses.1.primary',
  1359. ],
  1360. [],
  1361. ]));
  1362. $expected = [
  1363. 'div' => ['style' => 'display:none;'],
  1364. ['input' => [
  1365. 'type' => 'hidden',
  1366. 'name' => '_Token[fields]',
  1367. 'value' => $hash,
  1368. ]],
  1369. ['input' => [
  1370. 'type' => 'hidden',
  1371. 'name' => '_Token[unlocked]',
  1372. 'value' => '',
  1373. ]],
  1374. ['input' => [
  1375. 'type' => 'hidden',
  1376. 'name' => '_Token[debug]',
  1377. 'value' => $tokenDebug,
  1378. ]],
  1379. '/div',
  1380. ];
  1381. $this->assertHtml($expected, $result);
  1382. }
  1383. /**
  1384. * testFormSecurityArrayFields method
  1385. *
  1386. * Test form security with Model.field.0 style inputs.
  1387. */
  1388. public function testFormSecurityArrayFields(): void
  1389. {
  1390. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1391. $this->Form->create();
  1392. $this->Form->text('Address.primary.1');
  1393. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1394. $this->assertSame('Address.primary', $result[0]);
  1395. $this->Form->text('Address.secondary.1.0');
  1396. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1397. $this->assertSame('Address.secondary', $result[1]);
  1398. }
  1399. /**
  1400. * testFormSecurityMultipleControlDisabledFields method
  1401. *
  1402. * Test secure form generation with multiple records and disabled fields.
  1403. */
  1404. public function testFormSecurityMultipleControlDisabledFields(): void
  1405. {
  1406. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1407. 'unlockedFields' => ['first_name', 'address'],
  1408. ]));
  1409. $this->Form->create();
  1410. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1411. $this->Form->text('Addresses.0.title');
  1412. $this->Form->text('Addresses.0.first_name');
  1413. $this->Form->text('Addresses.0.last_name');
  1414. $this->Form->text('Addresses.0.address');
  1415. $this->Form->text('Addresses.0.city');
  1416. $this->Form->text('Addresses.0.phone');
  1417. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1418. $this->Form->text('Addresses.1.title');
  1419. $this->Form->text('Addresses.1.first_name');
  1420. $this->Form->text('Addresses.1.last_name');
  1421. $this->Form->text('Addresses.1.address');
  1422. $this->Form->text('Addresses.1.city');
  1423. $this->Form->text('Addresses.1.phone');
  1424. $result = $this->Form->secure();
  1425. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1426. $tokenDebug = urlencode(json_encode([
  1427. '/articles/add',
  1428. [
  1429. 'Addresses.0.id' => '123456',
  1430. 'Addresses.0.title',
  1431. 'Addresses.0.last_name',
  1432. 'Addresses.0.city',
  1433. 'Addresses.0.phone',
  1434. 'Addresses.1.id' => '654321',
  1435. 'Addresses.1.title',
  1436. 'Addresses.1.last_name',
  1437. 'Addresses.1.city',
  1438. 'Addresses.1.phone',
  1439. ],
  1440. [
  1441. 'first_name',
  1442. 'address',
  1443. ],
  1444. ]));
  1445. $expected = [
  1446. 'div' => ['style' => 'display:none;'],
  1447. ['input' => [
  1448. 'type' => 'hidden',
  1449. 'name' => '_Token[fields]',
  1450. 'value' => $hash,
  1451. ]],
  1452. ['input' => [
  1453. 'type' => 'hidden',
  1454. 'name' => '_Token[unlocked]',
  1455. 'value' => 'address%7Cfirst_name',
  1456. ]],
  1457. ['input' => [
  1458. 'type' => 'hidden',
  1459. 'name' => '_Token[debug]',
  1460. 'value' => $tokenDebug,
  1461. ]],
  1462. '/div',
  1463. ];
  1464. $this->assertHtml($expected, $result);
  1465. }
  1466. /**
  1467. * testFormSecurityControlDisabledFields method
  1468. *
  1469. * Test single record form with disabled fields.
  1470. */
  1471. public function testFormSecurityControlUnlockedFields(): void
  1472. {
  1473. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1474. 'unlockedFields' => ['first_name', 'address'],
  1475. ]));
  1476. $this->Form->create();
  1477. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1478. $this->assertEquals(
  1479. $this->View->getRequest()->getAttribute('formTokenData'),
  1480. ['unlockedFields' => $result]
  1481. );
  1482. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1483. $this->Form->text('Addresses.title');
  1484. $this->Form->text('Addresses.first_name');
  1485. $this->Form->text('Addresses.last_name');
  1486. $this->Form->text('Addresses.address');
  1487. $this->Form->text('Addresses.city');
  1488. $this->Form->text('Addresses.phone');
  1489. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1490. $expected = [
  1491. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1492. 'Addresses.city', 'Addresses.phone',
  1493. ];
  1494. $this->assertEquals($expected, $result);
  1495. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1496. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1497. $tokenDebug = urlencode(json_encode([
  1498. '/articles/add',
  1499. [
  1500. 'Addresses.id' => '123456',
  1501. 'Addresses.title',
  1502. 'Addresses.last_name',
  1503. 'Addresses.city',
  1504. 'Addresses.phone',
  1505. ],
  1506. [
  1507. 'first_name',
  1508. 'address',
  1509. ],
  1510. ]));
  1511. $expected = [
  1512. 'div' => ['style' => 'display:none;'],
  1513. ['input' => [
  1514. 'type' => 'hidden',
  1515. 'name' => '_Token[fields]',
  1516. 'value' => $hash,
  1517. 'data-foo' => 'bar',
  1518. ]],
  1519. ['input' => [
  1520. 'type' => 'hidden',
  1521. 'name' => '_Token[unlocked]',
  1522. 'value' => 'address%7Cfirst_name',
  1523. 'data-foo' => 'bar',
  1524. ]],
  1525. ['input' => [
  1526. 'type' => 'hidden', 'name' => '_Token[debug]',
  1527. 'value' => $tokenDebug,
  1528. 'data-foo' => 'bar',
  1529. ]],
  1530. '/div',
  1531. ];
  1532. $this->assertHtml($expected, $result);
  1533. }
  1534. /**
  1535. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1536. *
  1537. * Test single record form with debugSecurity param.
  1538. */
  1539. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue(): void
  1540. {
  1541. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1542. 'unlockedFields' => ['first_name', 'address'],
  1543. ]));
  1544. $this->Form->create();
  1545. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1546. $this->assertEquals(
  1547. $this->View->getRequest()->getAttribute('formTokenData'),
  1548. ['unlockedFields' => $result]
  1549. );
  1550. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1551. $this->Form->text('Addresses.title');
  1552. $this->Form->text('Addresses.first_name');
  1553. $this->Form->text('Addresses.last_name');
  1554. $this->Form->text('Addresses.address');
  1555. $this->Form->text('Addresses.city');
  1556. $this->Form->text('Addresses.phone');
  1557. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1558. $expected = [
  1559. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1560. 'Addresses.city', 'Addresses.phone',
  1561. ];
  1562. $this->assertEquals($expected, $result);
  1563. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1564. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1565. $tokenDebug = urlencode(json_encode([
  1566. '/articles/add',
  1567. [
  1568. 'Addresses.id' => '123456',
  1569. 'Addresses.title',
  1570. 'Addresses.last_name',
  1571. 'Addresses.city',
  1572. 'Addresses.phone',
  1573. ],
  1574. [
  1575. 'first_name',
  1576. 'address',
  1577. ],
  1578. ]));
  1579. $expected = [
  1580. 'div' => ['style' => 'display:none;'],
  1581. ['input' => [
  1582. 'type' => 'hidden',
  1583. 'name' => '_Token[fields]',
  1584. 'value' => $hash,
  1585. 'data-foo' => 'bar',
  1586. ]],
  1587. ['input' => [
  1588. 'type' => 'hidden',
  1589. 'name' => '_Token[unlocked]',
  1590. 'value' => 'address%7Cfirst_name',
  1591. 'data-foo' => 'bar',
  1592. ]],
  1593. ['input' => [
  1594. 'type' => 'hidden', 'name' => '_Token[debug]',
  1595. 'value' => $tokenDebug,
  1596. 'data-foo' => 'bar',
  1597. ]],
  1598. '/div',
  1599. ];
  1600. $this->assertHtml($expected, $result);
  1601. }
  1602. /**
  1603. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1604. *
  1605. * Debug is false, debugSecurity is true -> no debug
  1606. */
  1607. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse(): void
  1608. {
  1609. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1610. 'unlockedFields' => ['first_name', 'address'],
  1611. ]));
  1612. $this->Form->create();
  1613. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1614. $this->assertEquals(
  1615. $this->View->getRequest()->getAttribute('formTokenData'),
  1616. ['unlockedFields' => $result]
  1617. );
  1618. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1619. $this->Form->text('Addresses.title');
  1620. $this->Form->text('Addresses.first_name');
  1621. $this->Form->text('Addresses.last_name');
  1622. $this->Form->text('Addresses.address');
  1623. $this->Form->text('Addresses.city');
  1624. $this->Form->text('Addresses.phone');
  1625. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1626. $expected = [
  1627. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1628. 'Addresses.city', 'Addresses.phone',
  1629. ];
  1630. $this->assertEquals($expected, $result);
  1631. Configure::write('debug', false);
  1632. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1633. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1634. $expected = [
  1635. 'div' => ['style' => 'display:none;'],
  1636. ['input' => [
  1637. 'type' => 'hidden',
  1638. 'name' => '_Token[fields]',
  1639. 'value' => $hash,
  1640. 'data-foo' => 'bar',
  1641. ]],
  1642. ['input' => [
  1643. 'type' => 'hidden',
  1644. 'name' => '_Token[unlocked]',
  1645. 'value' => 'address%7Cfirst_name',
  1646. 'data-foo' => 'bar',
  1647. ]],
  1648. '/div',
  1649. ];
  1650. $this->assertHtml($expected, $result);
  1651. }
  1652. /**
  1653. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1654. *
  1655. * Test single record form with debugSecurity param.
  1656. */
  1657. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse(): void
  1658. {
  1659. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1660. 'unlockedFields' => ['first_name', 'address'],
  1661. ]));
  1662. $this->Form->create();
  1663. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1664. $this->assertEquals(
  1665. $this->View->getRequest()->getAttribute('formTokenData'),
  1666. ['unlockedFields' => $result]
  1667. );
  1668. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1669. $this->Form->text('Addresses.title');
  1670. $this->Form->text('Addresses.first_name');
  1671. $this->Form->text('Addresses.last_name');
  1672. $this->Form->text('Addresses.address');
  1673. $this->Form->text('Addresses.city');
  1674. $this->Form->text('Addresses.phone');
  1675. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1676. $expected = [
  1677. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1678. 'Addresses.city', 'Addresses.phone',
  1679. ];
  1680. $this->assertEquals($expected, $result);
  1681. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1682. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1683. $expected = [
  1684. 'div' => ['style' => 'display:none;'],
  1685. ['input' => [
  1686. 'type' => 'hidden',
  1687. 'name' => '_Token[fields]',
  1688. 'value' => $hash,
  1689. 'data-foo' => 'bar',
  1690. ]],
  1691. ['input' => [
  1692. 'type' => 'hidden',
  1693. 'name' => '_Token[unlocked]',
  1694. 'value' => 'address%7Cfirst_name',
  1695. 'data-foo' => 'bar',
  1696. ]],
  1697. '/div',
  1698. ];
  1699. $this->assertHtml($expected, $result);
  1700. }
  1701. /**
  1702. * testFormSecureWithCustomNameAttribute method
  1703. *
  1704. * Test securing inputs with custom name attributes.
  1705. */
  1706. public function testFormSecureWithCustomNameAttribute(): void
  1707. {
  1708. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1709. $this->Form->create();
  1710. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1711. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1712. $this->assertSame('User.custom', $result[0]);
  1713. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1714. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1715. $this->assertSame('User.custom.another.value', $result[1]);
  1716. }
  1717. /**
  1718. * testFormSecuredControl method
  1719. *
  1720. * Test generation of entire secure form, assertions made on control() output.
  1721. */
  1722. public function testFormSecuredControl(): void
  1723. {
  1724. $this->View->setRequest($this->View->getRequest()
  1725. ->withAttribute('formTokenData', [])
  1726. ->withAttribute('csrfToken', 'testKey'));
  1727. $this->article['schema'] = [
  1728. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1729. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1730. ];
  1731. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1732. $encoding = strtolower(Configure::read('App.encoding'));
  1733. $expected = [
  1734. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1735. 'div' => ['style' => 'display:none;'],
  1736. ['input' => [
  1737. 'type' => 'hidden',
  1738. 'name' => '_csrfToken',
  1739. 'value' => 'testKey',
  1740. ]],
  1741. '/div',
  1742. ];
  1743. $this->assertHtml($expected, $result);
  1744. $result = $this->Form->control('ratio');
  1745. $expected = [
  1746. 'div' => ['class'],
  1747. 'label' => ['for'],
  1748. 'Ratio',
  1749. '/label',
  1750. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1751. '/div',
  1752. ];
  1753. $this->assertHtml($expected, $result);
  1754. $result = $this->Form->control('population');
  1755. $expected = [
  1756. 'div' => ['class'],
  1757. 'label' => ['for'],
  1758. 'Population',
  1759. '/label',
  1760. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1761. '/div',
  1762. ];
  1763. $this->assertHtml($expected, $result);
  1764. $result = $this->Form->control('published', ['type' => 'text']);
  1765. $expected = [
  1766. 'div' => ['class' => 'input text'],
  1767. 'label' => ['for' => 'published'],
  1768. 'Published',
  1769. '/label',
  1770. ['input' => [
  1771. 'type' => 'text',
  1772. 'name' => 'published',
  1773. 'id' => 'published',
  1774. ]],
  1775. '/div',
  1776. ];
  1777. $this->assertHtml($expected, $result);
  1778. $result = $this->Form->control('other', ['type' => 'text']);
  1779. $expected = [
  1780. 'div' => ['class' => 'input text'],
  1781. 'label' => ['for' => 'other'],
  1782. 'Other',
  1783. '/label',
  1784. ['input' => [
  1785. 'type' => 'text',
  1786. 'name' => 'other',
  1787. 'id',
  1788. ]],
  1789. '/div',
  1790. ];
  1791. $this->assertHtml($expected, $result);
  1792. $result = $this->Form->hidden('stuff');
  1793. $expected = [
  1794. 'input' => [
  1795. 'type' => 'hidden',
  1796. 'name' => 'stuff',
  1797. ],
  1798. ];
  1799. $this->assertHtml($expected, $result);
  1800. $result = $this->Form->hidden('hidden', ['value' => false]);
  1801. $expected = ['input' => [
  1802. 'type' => 'hidden',
  1803. 'name' => 'hidden',
  1804. 'value' => '0',
  1805. ]];
  1806. $this->assertHtml($expected, $result);
  1807. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1808. $expected = [
  1809. 'div' => ['class' => 'input checkbox'],
  1810. ['input' => [
  1811. 'type' => 'hidden',
  1812. 'name' => 'something',
  1813. 'value' => '0',
  1814. ]],
  1815. 'label' => ['for' => 'something'],
  1816. ['input' => [
  1817. 'type' => 'checkbox',
  1818. 'name' => 'something',
  1819. 'value' => '1',
  1820. 'id' => 'something',
  1821. ]],
  1822. 'Something',
  1823. '/label',
  1824. '/div',
  1825. ];
  1826. $this->assertHtml($expected, $result);
  1827. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1828. $expectedFields = [
  1829. 'ratio',
  1830. 'population',
  1831. 'published',
  1832. 'other',
  1833. 'stuff' => '',
  1834. 'hidden' => '0',
  1835. 'something',
  1836. ];
  1837. $this->assertEquals($expectedFields, $result);
  1838. $result = $this->Form->secure();
  1839. $tokenDebug = urlencode(json_encode([
  1840. '/articles/add',
  1841. $expectedFields,
  1842. [],
  1843. ]));
  1844. $expected = [
  1845. 'div' => ['style' => 'display:none;'],
  1846. ['input' => [
  1847. 'type' => 'hidden',
  1848. 'name' => '_Token[fields]',
  1849. 'value',
  1850. ]],
  1851. ['input' => [
  1852. 'type' => 'hidden',
  1853. 'name' => '_Token[unlocked]',
  1854. 'value' => '',
  1855. ]],
  1856. ['input' => [
  1857. 'type' => 'hidden', 'name' => '_Token[debug]',
  1858. 'value' => $tokenDebug,
  1859. ]],
  1860. '/div',
  1861. ];
  1862. $this->assertHtml($expected, $result);
  1863. $data = [
  1864. 'ratio' => '',
  1865. 'population' => '',
  1866. 'published' => '',
  1867. 'other' => '',
  1868. 'stuff' => '',
  1869. 'hidden' => '0',
  1870. 'something' => '',
  1871. '_Token' => $this->Form->getFormProtector()->buildTokenData(),
  1872. ];
  1873. $this->assertTrue($this->Form->getFormProtector()->validate($data, '', ''));
  1874. }
  1875. /**
  1876. * testSecuredControlCustomName method
  1877. *
  1878. * Test secured inputs with custom names.
  1879. */
  1880. public function testSecuredControlCustomName(): void
  1881. {
  1882. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1883. $this->Form->create();
  1884. $this->Form->text('text_input', [
  1885. 'name' => 'Option[General.default_role]',
  1886. ]);
  1887. $expected = ['Option.General.default_role'];
  1888. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1889. $this->assertEquals($expected, $result);
  1890. $this->Form->select('select_box', [1, 2], [
  1891. 'name' => 'Option[General.select_role]',
  1892. ]);
  1893. $expected[] = 'Option.General.select_role';
  1894. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1895. $this->assertEquals($expected, $result);
  1896. $this->Form->text('other.things[]');
  1897. $expected[] = 'other.things';
  1898. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1899. $this->assertEquals($expected, $result);
  1900. }
  1901. /**
  1902. * testSecuredControlDuplicate method
  1903. *
  1904. * Test that a hidden field followed by a visible field
  1905. * undoes the hidden field locking.
  1906. */
  1907. public function testSecuredControlDuplicate(): void
  1908. {
  1909. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1910. $this->Form->create();
  1911. $this->Form->control('text_val', [
  1912. 'type' => 'hidden',
  1913. 'value' => 'some text',
  1914. ]);
  1915. $expected = ['text_val' => 'some text'];
  1916. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1917. $this->assertEquals($expected, $result);
  1918. $this->Form->control('text_val', [
  1919. 'type' => 'text',
  1920. ]);
  1921. $expected = ['text_val'];
  1922. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1923. $this->assertEquals($expected, $result);
  1924. }
  1925. /**
  1926. * testFormSecuredFileControl method
  1927. *
  1928. * Tests that the correct keys are added to the field hash index.
  1929. */
  1930. public function testFormSecuredFileControl(): void
  1931. {
  1932. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1933. $this->Form->create();
  1934. $this->Form->file('Attachment.file');
  1935. $expected = ['Attachment.file'];
  1936. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1937. $this->assertEquals($expected, $result);
  1938. }
  1939. /**
  1940. * testFormSecuredMultipleSelect method
  1941. *
  1942. * Test that multiple selects keys are added to field hash.
  1943. */
  1944. public function testFormSecuredMultipleSelect(): void
  1945. {
  1946. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1947. $this->Form->create();
  1948. $options = ['1' => 'one', '2' => 'two'];
  1949. $this->Form->select('Model.select', $options);
  1950. $expected = ['Model.select'];
  1951. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1952. $this->assertEquals($expected, $result);
  1953. $this->Form->select('Model.select', $options, ['multiple' => true]);
  1954. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1955. $this->assertEquals($expected, $result);
  1956. }
  1957. /**
  1958. * testFormSecuredRadio method
  1959. */
  1960. public function testFormSecuredRadio(): void
  1961. {
  1962. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1963. $this->Form->create();
  1964. $options = ['1' => 'option1', '2' => 'option2'];
  1965. $this->Form->radio('Test.test', $options);
  1966. $expected = ['Test.test'];
  1967. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1968. $this->assertEquals($expected, $result);
  1969. $this->Form->radio('Test.all', $options, [
  1970. 'disabled' => ['option1', 'option2'],
  1971. ]);
  1972. $expected = ['Test.test', 'Test.all' => ''];
  1973. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1974. $this->assertEquals($expected, $result);
  1975. $this->Form->radio('Test.some', $options, [
  1976. 'disabled' => ['option1'],
  1977. ]);
  1978. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  1979. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1980. $this->assertEquals($expected, $result);
  1981. }
  1982. /**
  1983. * testFormSecuredAndDisabled method
  1984. *
  1985. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  1986. * hashing.
  1987. */
  1988. public function testFormSecuredAndDisabled(): void
  1989. {
  1990. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1991. $this->Form->create();
  1992. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  1993. $this->Form->text('Model.text', ['disabled' => true]);
  1994. $this->Form->password('Model.text', ['disabled' => true]);
  1995. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  1996. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  1997. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  1998. $this->Form->year('Model.year', ['disabled' => true]);
  1999. $this->Form->month('Model.month', ['disabled' => true]);
  2000. $this->Form->day('Model.day', ['disabled' => true]);
  2001. $this->Form->hour('Model.hour', ['disabled' => true]);
  2002. $this->Form->minute('Model.minute', ['disabled' => true]);
  2003. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2004. $expected = [
  2005. 'Model.radio' => '',
  2006. ];
  2007. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2008. $this->assertEquals($expected, $result);
  2009. }
  2010. /**
  2011. * testUnlockFieldAddsToList method
  2012. *
  2013. * Test disableField.
  2014. */
  2015. public function testUnlockFieldAddsToList(): void
  2016. {
  2017. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2018. 'unlockedFields' => [],
  2019. ]));
  2020. $this->Form->create();
  2021. $this->Form->unlockField('Contact.name');
  2022. $this->Form->text('Contact.name');
  2023. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2024. $this->assertEquals([], $result);
  2025. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  2026. $this->assertEquals(['Contact.name'], $result);
  2027. }
  2028. /**
  2029. * testUnlockFieldRemovingFromFields method
  2030. *
  2031. * Test unlockField removing from fields array.
  2032. */
  2033. public function testUnlockFieldRemovingFromFields(): void
  2034. {
  2035. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2036. 'unlockedFields' => [],
  2037. ]));
  2038. $this->Form->create($this->article);
  2039. $this->Form->hidden('Article.id', ['value' => 1]);
  2040. $this->Form->text('Article.title');
  2041. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2042. $this->assertSame('1', $result['Article.id'], 'Hidden input should be secured.');
  2043. $this->assertContains('Article.title', $result, 'Field should be secured.');
  2044. $this->Form->unlockField('Article.title');
  2045. $this->Form->unlockField('Article.id');
  2046. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2047. $this->assertEquals([], $result);
  2048. }
  2049. /**
  2050. * testResetUnlockFields method
  2051. *
  2052. * Test reset unlockFields, when create new form.
  2053. */
  2054. public function testResetUnlockFields(): void
  2055. {
  2056. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2057. 'key' => 'testKey',
  2058. 'unlockedFields' => [],
  2059. ]));
  2060. $this->Form->create();
  2061. $this->Form->unlockField('Contact.id');
  2062. $this->Form->hidden('Contact.id', ['value' => 1]);
  2063. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2064. $this->assertEmpty($result, 'Field should be unlocked');
  2065. $this->Form->end();
  2066. $this->Form->create();
  2067. $this->Form->hidden('Contact.id', ['value' => 1]);
  2068. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2069. $this->assertSame('1', $result['Contact.id'], 'Hidden input should be secured.');
  2070. }
  2071. /**
  2072. * testSecuredFormUrlIgnoresHost method
  2073. *
  2074. * Test that only the path + query elements of a form's URL show up in their hash.
  2075. */
  2076. public function testSecuredFormUrlIgnoresHost(): void
  2077. {
  2078. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'testKey']));
  2079. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2080. $this->Form->create($this->article, [
  2081. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2082. ]);
  2083. $result = $this->Form->secure();
  2084. $this->assertStringContainsString($expected, $result);
  2085. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2086. $result = $this->Form->secure();
  2087. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2088. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2089. $result = $this->Form->secure();
  2090. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2091. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2092. $result = $this->Form->secure();
  2093. $this->assertStringNotContainsString($expected, $result, 'URL is different');
  2094. }
  2095. /**
  2096. * testSecuredFormUrlHasHtmlAndIdentifier method
  2097. *
  2098. * Test that URL, HTML and identifier show up in their hashes.
  2099. */
  2100. public function testSecuredFormUrlHasHtmlAndIdentifier(): void
  2101. {
  2102. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2103. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2104. $this->Form->create($this->article, [
  2105. 'url' => [
  2106. 'controller' => 'articles',
  2107. 'action' => 'view',
  2108. '?' => [
  2109. 'page' => 1,
  2110. 'limit' => 10,
  2111. 'html' => '<>"',
  2112. ],
  2113. '#' => 'result',
  2114. ],
  2115. ]);
  2116. $result = $this->Form->secure();
  2117. $this->assertStringContainsString($expected, $result);
  2118. $this->Form->create($this->article, [
  2119. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2120. ]);
  2121. $result = $this->Form->secure();
  2122. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2123. $this->Form->create($this->article, [
  2124. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2125. ]);
  2126. $result = $this->Form->secure();
  2127. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2128. }
  2129. /**
  2130. * testErrorMessageDisplay method
  2131. *
  2132. * Test error message display.
  2133. */
  2134. public function testErrorMessageDisplay(): void
  2135. {
  2136. $this->article['errors'] = [
  2137. 'Article' => [
  2138. 'title' => 'error message',
  2139. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2140. ],
  2141. ];
  2142. $this->Form->create($this->article);
  2143. $result = $this->Form->control('Article.title');
  2144. $expected = [
  2145. 'div' => ['class' => 'input text error'],
  2146. 'label' => ['for' => 'article-title'],
  2147. 'Title',
  2148. '/label',
  2149. 'input' => [
  2150. 'type' => 'text',
  2151. 'name' => 'Article[title]',
  2152. 'id' => 'article-title',
  2153. 'class' => 'form-error',
  2154. 'aria-invalid' => 'true',
  2155. 'aria-describedby' => 'article-title-error',
  2156. ],
  2157. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2158. 'error message',
  2159. '/div',
  2160. '/div',
  2161. ];
  2162. $this->assertHtml($expected, $result);
  2163. $result = $this->Form->control('Article.title', [
  2164. 'templates' => [
  2165. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2166. ],
  2167. ]);
  2168. $expected = [
  2169. 'div' => ['class' => 'input text error'],
  2170. 'label' => ['for' => 'article-title'],
  2171. 'Title',
  2172. '/label',
  2173. 'input' => [
  2174. 'type' => 'text',
  2175. 'name' => 'Article[title]',
  2176. 'id' => 'article-title',
  2177. 'class' => 'form-error',
  2178. // No aria-describedby because error template is custom
  2179. 'aria-invalid' => 'true',
  2180. ],
  2181. '/div',
  2182. ];
  2183. $this->assertHtml($expected, $result);
  2184. $result = $this->Form->control('Article.content');
  2185. $expected = [
  2186. 'div' => ['class' => 'input text error'],
  2187. 'label' => ['for' => 'article-content'],
  2188. 'Content',
  2189. '/label',
  2190. 'input' => [
  2191. 'type' => 'text',
  2192. 'name' => 'Article[content]',
  2193. 'id' => 'article-content',
  2194. 'class' => 'form-error',
  2195. 'aria-invalid' => 'true',
  2196. 'aria-describedby' => 'article-content-error',
  2197. ],
  2198. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2199. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2200. '/div',
  2201. '/div',
  2202. ];
  2203. $this->assertHtml($expected, $result);
  2204. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2205. $expected = [
  2206. 'div' => ['class' => 'input text error'],
  2207. 'label' => ['for' => 'article-content'],
  2208. 'Content',
  2209. '/label',
  2210. 'input' => [
  2211. 'type' => 'text',
  2212. 'name' => 'Article[content]',
  2213. 'id' => 'article-content',
  2214. 'class' => 'form-error',
  2215. 'aria-invalid' => 'true',
  2216. 'aria-describedby' => 'article-content-error',
  2217. ],
  2218. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2219. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2220. '/div',
  2221. '/div',
  2222. ];
  2223. $this->assertHtml($expected, $result);
  2224. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2225. $expected = [
  2226. 'div' => ['class' => 'input text error'],
  2227. 'label' => ['for' => 'article-content'],
  2228. 'Content',
  2229. '/label',
  2230. 'input' => [
  2231. 'type' => 'text',
  2232. 'name' => 'Article[content]',
  2233. 'id' => 'article-content',
  2234. 'class' => 'form-error',
  2235. 'aria-invalid' => 'true',
  2236. 'aria-describedby' => 'article-content-error',
  2237. ],
  2238. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2239. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2240. '/div',
  2241. '/div',
  2242. ];
  2243. $this->assertHtml($expected, $result);
  2244. }
  2245. /**
  2246. * testEmptyErrorValidation method
  2247. *
  2248. * Test validation errors, when validation message is an empty string.
  2249. */
  2250. public function testEmptyErrorValidation(): void
  2251. {
  2252. $this->article['errors'] = [
  2253. 'Article' => ['title' => ''],
  2254. ];
  2255. $this->Form->create($this->article);
  2256. $result = $this->Form->control('Article.title');
  2257. $expected = [
  2258. 'div' => ['class' => 'input text error'],
  2259. 'label' => ['for' => 'article-title'],
  2260. 'Title',
  2261. '/label',
  2262. 'input' => [
  2263. 'type' => 'text',
  2264. 'name' => 'Article[title]',
  2265. 'id' => 'article-title',
  2266. 'class' => 'form-error',
  2267. 'aria-invalid' => 'true',
  2268. 'aria-describedby' => 'article-title-error',
  2269. ],
  2270. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2271. [],
  2272. '/div',
  2273. '/div',
  2274. ];
  2275. $this->assertHtml($expected, $result);
  2276. }
  2277. /**
  2278. * testEmptyControlErrorValidation method
  2279. *
  2280. * Test validation errors, when calling control() overriding validation message by an empty string.
  2281. */
  2282. public function testEmptyControlErrorValidation(): void
  2283. {
  2284. $this->article['errors'] = [
  2285. 'Article' => ['title' => 'error message'],
  2286. ];
  2287. $this->Form->create($this->article);
  2288. $result = $this->Form->control('Article.title', ['error' => '']);
  2289. $expected = [
  2290. 'div' => ['class' => 'input text error'],
  2291. 'label' => ['for' => 'article-title'],
  2292. 'Title',
  2293. '/label',
  2294. 'input' => [
  2295. 'aria-invalid' => 'true',
  2296. 'aria-describedby' => 'article-title-error',
  2297. 'type' => 'text',
  2298. 'name' => 'Article[title]',
  2299. 'id' => 'article-title',
  2300. 'class' => 'form-error',
  2301. ],
  2302. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2303. [],
  2304. '/div',
  2305. '/div',
  2306. ];
  2307. $this->assertHtml($expected, $result);
  2308. }
  2309. /**
  2310. * testControlErrorMessage method
  2311. *
  2312. * Test validation errors, when calling control() overriding validation messages.
  2313. */
  2314. public function testControlErrorMessage(): void
  2315. {
  2316. $this->article['errors'] = [
  2317. 'title' => ['error message'],
  2318. ];
  2319. $this->Form->create($this->article);
  2320. $result = $this->Form->control('title', [
  2321. 'error' => 'Custom error!',
  2322. ]);
  2323. $expected = [
  2324. 'div' => ['class' => 'input text required error'],
  2325. 'label' => ['for' => 'title'],
  2326. 'Title',
  2327. '/label',
  2328. 'input' => [
  2329. 'type' => 'text',
  2330. 'name' => 'title',
  2331. 'id' => 'title',
  2332. 'class' => 'form-error',
  2333. 'required' => 'required',
  2334. 'data-validity-message' => 'This field cannot be left empty',
  2335. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2336. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2337. 'aria-invalid' => 'true',
  2338. 'aria-describedby' => 'title-error',
  2339. ],
  2340. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2341. 'Custom error!',
  2342. '/div',
  2343. '/div',
  2344. ];
  2345. $this->assertHtml($expected, $result);
  2346. $result = $this->Form->control('title', [
  2347. 'error' => ['error message' => 'Custom error!'],
  2348. ]);
  2349. $expected = [
  2350. 'div' => ['class' => 'input text required error'],
  2351. 'label' => ['for' => 'title'],
  2352. 'Title',
  2353. '/label',
  2354. 'input' => [
  2355. 'type' => 'text',
  2356. 'name' => 'title',
  2357. 'id' => 'title',
  2358. 'aria-invalid' => 'true',
  2359. 'aria-describedby' => 'title-error',
  2360. 'class' => 'form-error',
  2361. 'required' => 'required',
  2362. 'data-validity-message' => 'This field cannot be left empty',
  2363. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2364. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2365. ],
  2366. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2367. 'Custom error!',
  2368. '/div',
  2369. '/div',
  2370. ];
  2371. $this->assertHtml($expected, $result);
  2372. }
  2373. /**
  2374. * testFormValidationAssociated method
  2375. *
  2376. * Tests displaying errors for nested entities.
  2377. */
  2378. public function testFormValidationAssociated(): void
  2379. {
  2380. $nested = new Entity(['foo' => 'bar']);
  2381. $nested->setError('foo', ['not a valid bar']);
  2382. $entity = new Entity(['nested' => $nested]);
  2383. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2384. $result = $this->Form->error('nested.foo');
  2385. $this->assertSame('<div class="error-message" id="nested-foo-error">not a valid bar</div>', $result);
  2386. }
  2387. /**
  2388. * testFormValidationAssociatedSecondLevel method
  2389. *
  2390. * Test form error display with associated model.
  2391. */
  2392. public function testFormValidationAssociatedSecondLevel(): void
  2393. {
  2394. $inner = new Entity(['bar' => 'baz']);
  2395. $nested = new Entity(['foo' => $inner]);
  2396. $entity = new Entity(['nested' => $nested]);
  2397. $inner->setError('bar', ['not a valid one']);
  2398. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2399. $result = $this->Form->error('nested.foo.bar');
  2400. $this->assertSame('<div class="error-message" id="nested-foo-bar-error">not a valid one</div>', $result);
  2401. }
  2402. /**
  2403. * testFormValidationMultiRecord method
  2404. *
  2405. * Test form error display with multiple records.
  2406. */
  2407. public function testFormValidationMultiRecord(): void
  2408. {
  2409. $one = new Entity();
  2410. $two = new Entity();
  2411. $this->getTableLocator()->get('Contacts', [
  2412. 'className' => ContactsTable::class,
  2413. ]);
  2414. $one->set('email', '');
  2415. $one->setError('email', ['invalid email']);
  2416. $two->set('name', '');
  2417. $two->setError('name', ['This is wrong']);
  2418. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2419. $result = $this->Form->control('0.email');
  2420. $expected = [
  2421. 'div' => ['class' => 'input email error'],
  2422. 'label' => ['for' => '0-email'],
  2423. 'Email',
  2424. '/label',
  2425. 'input' => [
  2426. 'type' => 'email',
  2427. 'name' => '0[email]',
  2428. 'id' => '0-email',
  2429. 'class' => 'form-error',
  2430. 'maxlength' => 255,
  2431. 'value' => '',
  2432. 'aria-invalid' => 'true',
  2433. 'aria-describedby' => '0-email-error',
  2434. ],
  2435. ['div' => ['class' => 'error-message', 'id' => '0-email-error']],
  2436. 'invalid email',
  2437. '/div',
  2438. '/div',
  2439. ];
  2440. $this->assertHtml($expected, $result);
  2441. $result = $this->Form->control('1.name');
  2442. $expected = [
  2443. 'div' => ['class' => 'input text error'],
  2444. 'label' => ['for' => '1-name'],
  2445. 'Name',
  2446. '/label',
  2447. 'input' => [
  2448. 'type' => 'text',
  2449. 'name' => '1[name]',
  2450. 'id' => '1-name',
  2451. 'class' => 'form-error',
  2452. 'maxlength' => 255,
  2453. 'value' => '',
  2454. 'aria-invalid' => 'true',
  2455. 'aria-describedby' => '1-name-error',
  2456. ],
  2457. ['div' => ['class' => 'error-message', 'id' => '1-name-error']],
  2458. 'This is wrong',
  2459. '/div',
  2460. '/div',
  2461. ];
  2462. $this->assertHtml($expected, $result);
  2463. }
  2464. /**
  2465. * testControl method
  2466. *
  2467. * Test various incarnations of control().
  2468. */
  2469. public function testControl(): void
  2470. {
  2471. $this->getTableLocator()->get('ValidateUsers', [
  2472. 'className' => ValidateUsersTable::class,
  2473. ]);
  2474. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2475. $result = $this->Form->control('ValidateUsers.balance');
  2476. $expected = [
  2477. 'div' => ['class'],
  2478. 'label' => ['for'],
  2479. 'Balance',
  2480. '/label',
  2481. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2482. '/div',
  2483. ];
  2484. $this->assertHtml($expected, $result);
  2485. $result = $this->Form->control('ValidateUser.cost_decimal');
  2486. $expected = [
  2487. 'div' => ['class'],
  2488. 'label' => ['for'],
  2489. 'Cost Decimal',
  2490. '/label',
  2491. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2492. '/div',
  2493. ];
  2494. $this->assertHtml($expected, $result);
  2495. $result = $this->Form->control('ValidateUser.null_decimal');
  2496. $expected = [
  2497. 'div' => ['class'],
  2498. 'label' => ['for'],
  2499. 'Null Decimal',
  2500. '/label',
  2501. 'input' => ['name', 'type' => 'number', 'id'],
  2502. '/div',
  2503. ];
  2504. $this->assertHtml($expected, $result);
  2505. }
  2506. /**
  2507. * testControlCustomization method
  2508. *
  2509. * Tests the input method and passing custom options.
  2510. */
  2511. public function testControlCustomization(): void
  2512. {
  2513. $this->getTableLocator()->get('Contacts', [
  2514. 'className' => ContactsTable::class,
  2515. ]);
  2516. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2517. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2518. $expected = [
  2519. 'div' => ['class' => 'input email'],
  2520. 'label' => ['for' => 'custom'],
  2521. 'Email',
  2522. '/label',
  2523. ['input' => [
  2524. 'type' => 'email', 'name' => 'Contact[email]',
  2525. 'id' => 'custom', 'maxlength' => 255,
  2526. ]],
  2527. '/div',
  2528. ];
  2529. $this->assertHtml($expected, $result);
  2530. $result = $this->Form->control('Contact.email', [
  2531. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2532. ]);
  2533. $expected = [
  2534. '<div',
  2535. 'label' => ['for' => 'contact-email'],
  2536. 'Email',
  2537. '/label',
  2538. ['input' => [
  2539. 'type' => 'email', 'name' => 'Contact[email]',
  2540. 'id' => 'contact-email', 'maxlength' => 255,
  2541. ]],
  2542. '/div',
  2543. ];
  2544. $this->assertHtml($expected, $result);
  2545. $result = $this->Form->control('Contact.email', [
  2546. 'templates' => [
  2547. 'formGroup' => '{{input}}',
  2548. 'inputContainer' => '<div><div>{{label}}</div>{{content}}</div>',
  2549. ],
  2550. ]);
  2551. $expected = [
  2552. '<div',
  2553. '<div',
  2554. 'label' => ['for' => 'contact-email'],
  2555. 'Email',
  2556. '/label',
  2557. '/div',
  2558. ['input' => [
  2559. 'type' => 'email', 'name' => 'Contact[email]',
  2560. 'id' => 'contact-email', 'maxlength' => 255,
  2561. ]],
  2562. '/div',
  2563. ];
  2564. $this->assertHtml($expected, $result);
  2565. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2566. $expected = [
  2567. 'div' => ['class' => 'input text'],
  2568. 'label' => ['for' => 'contact-email'],
  2569. 'Email',
  2570. '/label',
  2571. ['input' => [
  2572. 'type' => 'text', 'name' => 'Contact[email]',
  2573. 'id' => 'contact-email', 'maxlength' => '255',
  2574. ]],
  2575. '/div',
  2576. ];
  2577. $this->assertHtml($expected, $result);
  2578. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2579. $expected = [
  2580. 'div' => ['class' => 'input text'],
  2581. 'label' => ['for' => 'contact-5-email'],
  2582. 'Email',
  2583. '/label',
  2584. ['input' => [
  2585. 'type' => 'text', 'name' => 'Contact[5][email]',
  2586. 'id' => 'contact-5-email', 'maxlength' => '255',
  2587. ]],
  2588. '/div',
  2589. ];
  2590. $this->assertHtml($expected, $result);
  2591. $result = $this->Form->control('Contact.password');
  2592. $expected = [
  2593. 'div' => ['class' => 'input password'],
  2594. 'label' => ['for' => 'contact-password'],
  2595. 'Password',
  2596. '/label',
  2597. ['input' => [
  2598. 'type' => 'password', 'name' => 'Contact[password]',
  2599. 'id' => 'contact-password',
  2600. ]],
  2601. '/div',
  2602. ];
  2603. $this->assertHtml($expected, $result);
  2604. $result = $this->Form->control('Contact.email', [
  2605. 'type' => 'file', 'class' => 'textbox',
  2606. ]);
  2607. $expected = [
  2608. 'div' => ['class' => 'input file'],
  2609. 'label' => ['for' => 'contact-email'],
  2610. 'Email',
  2611. '/label',
  2612. ['input' => [
  2613. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2614. 'id' => 'contact-email',
  2615. ]],
  2616. '/div',
  2617. ];
  2618. $this->assertHtml($expected, $result);
  2619. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2620. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2621. $result = $this->Form->control('phone');
  2622. $expected = [
  2623. 'div' => ['class' => 'input tel'],
  2624. 'label' => ['for' => 'phone'],
  2625. 'Phone',
  2626. '/label',
  2627. ['input' => [
  2628. 'type' => 'tel', 'name' => 'phone',
  2629. 'value' => 'Hello &amp; World &gt; weird chars',
  2630. 'id' => 'phone', 'maxlength' => 255,
  2631. ]],
  2632. '/div',
  2633. ];
  2634. $this->assertHtml($expected, $result);
  2635. $this->View->setRequest(
  2636. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2637. );
  2638. $this->Form->create();
  2639. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2640. $expected = [
  2641. 'div' => ['class' => 'input text'],
  2642. 'label' => ['for' => 'myId'],
  2643. 'Field',
  2644. '/label',
  2645. 'input' => [
  2646. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2647. 'value' => 'My value', 'id' => 'myId',
  2648. ],
  2649. '/div',
  2650. ];
  2651. $this->assertHtml($expected, $result);
  2652. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2653. $this->Form->create();
  2654. $entity->setError('field', 'Badness!');
  2655. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2656. $result = $this->Form->control('field');
  2657. $expected = [
  2658. 'div' => ['class' => 'input text error'],
  2659. 'label' => ['for' => 'field'],
  2660. 'Field',
  2661. '/label',
  2662. 'input' => [
  2663. 'type' => 'text',
  2664. 'name' => 'field',
  2665. 'id' => 'field',
  2666. 'class' => 'form-error',
  2667. 'aria-invalid' => 'true',
  2668. 'aria-describedby' => 'field-error',
  2669. ],
  2670. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2671. 'Badness!',
  2672. '/div',
  2673. '/div',
  2674. ];
  2675. $this->assertHtml($expected, $result);
  2676. $result = $this->Form->control('field', [
  2677. 'templates' => [
  2678. 'inputContainerError' => '{{content}}{{error}}',
  2679. 'error' => '<span class="error-message">{{content}}</span>',
  2680. ],
  2681. ]);
  2682. $expected = [
  2683. 'label' => ['for' => 'field'],
  2684. 'Field',
  2685. '/label',
  2686. 'input' => [
  2687. 'type' => 'text',
  2688. 'name' => 'field',
  2689. 'id' => 'field',
  2690. 'class' => 'form-error',
  2691. // No aria-describedby because error template is custom
  2692. 'aria-invalid' => 'true',
  2693. ],
  2694. ['span' => ['class' => 'error-message']],
  2695. 'Badness!',
  2696. '/span',
  2697. ];
  2698. $this->assertHtml($expected, $result);
  2699. $entity->setError('field', ['minLength'], true);
  2700. $result = $this->Form->control('field', [
  2701. 'error' => [
  2702. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2703. 'maxLength' => 'login too large',
  2704. ],
  2705. ]);
  2706. $expected = [
  2707. 'div' => ['class' => 'input text error'],
  2708. 'label' => ['for' => 'field'],
  2709. 'Field',
  2710. '/label',
  2711. 'input' => [
  2712. 'type' => 'text',
  2713. 'name' => 'field',
  2714. 'id' => 'field',
  2715. 'class' => 'form-error',
  2716. 'aria-invalid' => 'true',
  2717. 'aria-describedby' => 'field-error',
  2718. ],
  2719. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2720. 'Le login doit contenir au moins 2 caractères',
  2721. '/div',
  2722. '/div',
  2723. ];
  2724. $this->assertHtml($expected, $result);
  2725. $entity->setError('field', ['maxLength'], true);
  2726. $result = $this->Form->control('field', [
  2727. 'error' => [
  2728. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2729. 'maxLength' => 'login too large',
  2730. ],
  2731. ]);
  2732. $expected = [
  2733. 'div' => ['class' => 'input text error'],
  2734. 'label' => ['for' => 'field'],
  2735. 'Field',
  2736. '/label',
  2737. 'input' => [
  2738. 'type' => 'text',
  2739. 'name' => 'field',
  2740. 'id' => 'field',
  2741. 'class' => 'form-error',
  2742. 'aria-invalid' => 'true',
  2743. 'aria-describedby' => 'field-error',
  2744. ],
  2745. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2746. 'login too large',
  2747. '/div',
  2748. '/div',
  2749. ];
  2750. $this->assertHtml($expected, $result);
  2751. }
  2752. /**
  2753. * testControlWithTemplateFile method
  2754. *
  2755. * Test that control() accepts a template file.
  2756. */
  2757. public function testControlWithTemplateFile(): void
  2758. {
  2759. $result = $this->Form->control('field', [
  2760. 'templates' => 'htmlhelper_tags',
  2761. ]);
  2762. $expected = [
  2763. 'label' => ['for' => 'field'],
  2764. 'Field',
  2765. '/label',
  2766. 'input' => [
  2767. 'type' => 'text', 'name' => 'field',
  2768. 'id' => 'field',
  2769. ],
  2770. ];
  2771. $this->assertHtml($expected, $result);
  2772. }
  2773. /**
  2774. * testNestedControlsEndWithBrackets method
  2775. *
  2776. * Test that nested inputs end with brackets.
  2777. */
  2778. public function testNestedControlsEndWithBrackets(): void
  2779. {
  2780. $result = $this->Form->text('nested.text[]');
  2781. $expected = [
  2782. 'input' => [
  2783. 'type' => 'text', 'name' => 'nested[text][]',
  2784. ],
  2785. ];
  2786. $this->assertHtml($expected, $result);
  2787. $result = $this->Form->file('nested.file[]');
  2788. $expected = [
  2789. 'input' => [
  2790. 'type' => 'file', 'name' => 'nested[file][]',
  2791. ],
  2792. ];
  2793. $this->assertHtml($expected, $result);
  2794. }
  2795. /**
  2796. * testCreateIdPrefix method
  2797. *
  2798. * Test id prefix.
  2799. */
  2800. public function testCreateIdPrefix(): void
  2801. {
  2802. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2803. $result = $this->Form->control('field');
  2804. $expected = [
  2805. 'div' => ['class' => 'input text'],
  2806. 'label' => ['for' => 'prefix-field'],
  2807. 'Field',
  2808. '/label',
  2809. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2810. '/div',
  2811. ];
  2812. $this->assertHtml($expected, $result);
  2813. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2814. $expected = [
  2815. 'div' => ['class' => 'input text'],
  2816. 'label' => ['for' => 'custom-id'],
  2817. 'Field',
  2818. '/label',
  2819. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2820. '/div',
  2821. ];
  2822. $this->assertHtml($expected, $result);
  2823. $result = $this->Form->radio('Model.field', ['option A']);
  2824. $expected = [
  2825. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2826. 'label' => ['for' => 'prefix-model-field-0'],
  2827. ['input' => [
  2828. 'type' => 'radio',
  2829. 'name' => 'Model[field]',
  2830. 'value' => '0',
  2831. 'id' => 'prefix-model-field-0',
  2832. ]],
  2833. 'option A',
  2834. '/label',
  2835. ];
  2836. $this->assertHtml($expected, $result);
  2837. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2838. $expected = [
  2839. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2840. 'label' => ['for' => 'prefix-model-field-0'],
  2841. ['input' => [
  2842. 'type' => 'radio',
  2843. 'name' => 'Model[field]',
  2844. 'value' => '0',
  2845. 'id' => 'prefix-model-field-0',
  2846. ]],
  2847. 'option A',
  2848. '/label',
  2849. ];
  2850. $this->assertHtml($expected, $result);
  2851. $result = $this->Form->select(
  2852. 'Model.multi_field',
  2853. ['first'],
  2854. ['multiple' => 'checkbox']
  2855. );
  2856. $expected = [
  2857. 'input' => [
  2858. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'prefix-model-multi-field',
  2859. ],
  2860. ['div' => ['class' => 'checkbox']],
  2861. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2862. ['input' => [
  2863. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2864. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  2865. ]],
  2866. 'first',
  2867. '/label',
  2868. '/div',
  2869. ];
  2870. $this->assertHtml($expected, $result);
  2871. $this->Form->end();
  2872. $result = $this->Form->control('field');
  2873. $expected = [
  2874. 'div' => ['class' => 'input text'],
  2875. 'label' => ['for' => 'field'],
  2876. 'Field',
  2877. '/label',
  2878. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2879. '/div',
  2880. ];
  2881. $this->assertHtml($expected, $result);
  2882. }
  2883. /**
  2884. * testControlZero method
  2885. *
  2886. * Test that inputs with 0 can be created.
  2887. */
  2888. public function testControlZero(): void
  2889. {
  2890. $this->getTableLocator()->get('Contacts', [
  2891. 'className' => ContactsTable::class,
  2892. ]);
  2893. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2894. $result = $this->Form->control('0');
  2895. $expected = [
  2896. 'div' => ['class' => 'input text'],
  2897. 'label' => ['for' => '0'], '/label',
  2898. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2899. '/div',
  2900. ];
  2901. $this->assertHtml($expected, $result);
  2902. }
  2903. /**
  2904. * testControlCheckbox method
  2905. *
  2906. * Test control() with checkbox creation.
  2907. */
  2908. public function testControlCheckbox(): void
  2909. {
  2910. $articles = $this->getTableLocator()->get('Articles');
  2911. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  2912. $article = $articles->newEmptyEntity();
  2913. $this->Form->create($article);
  2914. $result = $this->Form->control('Articles.active');
  2915. $expected = [
  2916. 'div' => ['class' => 'input checkbox'],
  2917. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2918. 'label' => ['for' => 'articles-active'],
  2919. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  2920. 'Active',
  2921. '/label',
  2922. '/div',
  2923. ];
  2924. $this->assertHtml($expected, $result);
  2925. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  2926. $expected = [
  2927. 'div' => ['class' => 'input checkbox'],
  2928. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2929. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2930. '/div',
  2931. ];
  2932. $this->assertHtml($expected, $result);
  2933. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  2934. $expected = [
  2935. 'div' => ['class' => 'input checkbox'],
  2936. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2937. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2938. '/div',
  2939. ];
  2940. $this->assertHtml($expected, $result);
  2941. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  2942. $expected = [
  2943. 'div' => ['class' => 'input checkbox'],
  2944. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2945. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2946. '/div',
  2947. ];
  2948. $this->assertHtml($expected, $result);
  2949. $result = $this->Form->control('Articles.disabled', [
  2950. 'label' => 'Disabled',
  2951. 'type' => 'checkbox',
  2952. 'data-foo' => 'disabled',
  2953. ]);
  2954. $expected = [
  2955. 'div' => ['class' => 'input checkbox'],
  2956. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  2957. 'label' => ['for' => 'articles-disabled'],
  2958. ['input' => [
  2959. 'type' => 'checkbox',
  2960. 'name' => 'Articles[disabled]',
  2961. 'value' => '1',
  2962. 'id' => 'articles-disabled',
  2963. 'data-foo' => 'disabled',
  2964. ]],
  2965. 'Disabled',
  2966. '/label',
  2967. '/div',
  2968. ];
  2969. $this->assertHtml($expected, $result);
  2970. $result = $this->Form->control('Articles.confirm', [
  2971. 'label' => 'Confirm <b>me</b>!',
  2972. 'type' => 'checkbox',
  2973. 'escape' => false,
  2974. ]);
  2975. $expected = [
  2976. 'div' => ['class' => 'input checkbox'],
  2977. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  2978. 'label' => ['for' => 'articles-confirm'],
  2979. ['input' => [
  2980. 'type' => 'checkbox',
  2981. 'name' => 'Articles[confirm]',
  2982. 'value' => '1',
  2983. 'id' => 'articles-confirm',
  2984. ]],
  2985. 'Confirm <b>me</b>!',
  2986. '/label',
  2987. '/div',
  2988. ];
  2989. $this->assertHtml($expected, $result);
  2990. }
  2991. /**
  2992. * testControlHidden method
  2993. *
  2994. * Test that control() does not create wrapping div and label tag for hidden fields.
  2995. */
  2996. public function testControlHidden(): void
  2997. {
  2998. $this->getTableLocator()->get('ValidateUsers', [
  2999. 'className' => ValidateUsersTable::class,
  3000. ]);
  3001. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3002. $result = $this->Form->control('ValidateUser.id');
  3003. $expected = [
  3004. 'input' => ['name', 'type' => 'hidden', 'id'],
  3005. ];
  3006. $this->assertHtml($expected, $result);
  3007. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3008. $expected = [
  3009. 'input' => ['name', 'type' => 'hidden', 'id'],
  3010. ];
  3011. $this->assertHtml($expected, $result);
  3012. }
  3013. /**
  3014. * testControlDatetime method
  3015. *
  3016. * Test form->control() with datetime.
  3017. */
  3018. public function testControlDatetime(): void
  3019. {
  3020. $result = $this->Form->control('prueba', [
  3021. 'type' => 'datetime',
  3022. 'value' => new DateTime('2019-09-27 02:52:43'),
  3023. ]);
  3024. $expected = [
  3025. 'div' => ['class' => 'input datetime'],
  3026. 'label' => ['for' => 'prueba'],
  3027. 'Prueba',
  3028. '/label',
  3029. 'input' => [
  3030. 'name' => 'prueba',
  3031. 'id' => 'prueba',
  3032. 'type' => 'datetime-local',
  3033. 'value' => '2019-09-27T02:52:43',
  3034. 'step' => '1',
  3035. ],
  3036. '/div',
  3037. ];
  3038. $this->assertHtml($expected, $result);
  3039. }
  3040. /**
  3041. * testControlDatetimeIdPrefix method
  3042. *
  3043. * Test form->control() with datetime with id prefix.
  3044. */
  3045. public function testControlDatetimeIdPrefix(): void
  3046. {
  3047. $this->Form->create(null, ['idPrefix' => 'prefix']);
  3048. $result = $this->Form->control('prueba', [
  3049. 'type' => 'datetime',
  3050. ]);
  3051. $expected = [
  3052. 'div' => ['class' => 'input datetime'],
  3053. 'label' => ['for' => 'prefix-prueba'],
  3054. 'Prueba',
  3055. '/label',
  3056. 'input' => [
  3057. 'name' => 'prueba',
  3058. 'id' => 'prefix-prueba',
  3059. 'type' => 'datetime-local',
  3060. 'value' => '',
  3061. 'step' => '1',
  3062. ],
  3063. '/div',
  3064. ];
  3065. $this->assertHtml($expected, $result);
  3066. }
  3067. /**
  3068. * testControlDatetimeStep method
  3069. *
  3070. * Test form->control() with datetime with custom step size.
  3071. */
  3072. public function testControlDatetimeStep(): void
  3073. {
  3074. $result = $this->Form->control('prueba', [
  3075. 'type' => 'datetime',
  3076. 'value' => new DateTime('2019-09-27 02:52:43'),
  3077. 'step' => '0.5',
  3078. ]);
  3079. $expected = [
  3080. 'div' => ['class' => 'input datetime'],
  3081. 'label' => ['for' => 'prueba'],
  3082. 'Prueba',
  3083. '/label',
  3084. 'input' => [
  3085. 'name' => 'prueba',
  3086. 'id' => 'prueba',
  3087. 'type' => 'datetime-local',
  3088. 'value' => '2019-09-27T02:52:43.000',
  3089. 'step' => '0.5',
  3090. ],
  3091. '/div',
  3092. ];
  3093. $this->assertHtml($expected, $result);
  3094. }
  3095. /**
  3096. * testControlCheckboxWithDisabledElements method
  3097. *
  3098. * Test generating checkboxes with disabled elements.
  3099. */
  3100. public function testControlCheckboxWithDisabledElements(): void
  3101. {
  3102. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3103. $result = $this->Form->control('Contact.multiple', [
  3104. 'multiple' => 'checkbox',
  3105. 'disabled' => 'disabled',
  3106. 'options' => $options,
  3107. ]);
  3108. $expected = [
  3109. ['div' => ['class' => 'input select']],
  3110. ['label' => ['for' => 'contact-multiple']],
  3111. 'Multiple',
  3112. '/label',
  3113. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '', 'id' => 'contact-multiple']],
  3114. ['div' => ['class' => 'checkbox']],
  3115. ['label' => ['for' => 'contact-multiple-1']],
  3116. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3117. 'One',
  3118. '/label',
  3119. '/div',
  3120. ['div' => ['class' => 'checkbox']],
  3121. ['label' => ['for' => 'contact-multiple-2']],
  3122. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3123. 'Two',
  3124. '/label',
  3125. '/div',
  3126. ['div' => ['class' => 'checkbox']],
  3127. ['label' => ['for' => 'contact-multiple-3']],
  3128. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3129. 'Three',
  3130. '/label',
  3131. '/div',
  3132. '/div',
  3133. ];
  3134. $this->assertHtml($expected, $result);
  3135. // make sure 50 does only disable 50, and not 50f5c0cf
  3136. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3137. $disabled = [50];
  3138. $expected = [
  3139. ['div' => ['class' => 'input select']],
  3140. ['label' => ['for' => 'contact-multiple']],
  3141. 'Multiple',
  3142. '/label',
  3143. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'contact-multiple']],
  3144. ['div' => ['class' => 'checkbox']],
  3145. ['label' => ['for' => 'contact-multiple-50']],
  3146. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3147. 'Fifty',
  3148. '/label',
  3149. '/div',
  3150. ['div' => ['class' => 'checkbox']],
  3151. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3152. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3153. 'Stringy',
  3154. '/label',
  3155. '/div',
  3156. '/div',
  3157. ];
  3158. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3159. $this->assertHtml($expected, $result);
  3160. }
  3161. /**
  3162. * testControlWithLeadingInteger method
  3163. *
  3164. * Test input name with leading integer, ensure attributes are generated correctly.
  3165. */
  3166. public function testControlWithLeadingInteger(): void
  3167. {
  3168. $result = $this->Form->text('0.Node.title');
  3169. $expected = [
  3170. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3171. ];
  3172. $this->assertHtml($expected, $result);
  3173. }
  3174. /**
  3175. * testControlSelectType method
  3176. *
  3177. * Test form->control() with select type inputs.
  3178. */
  3179. public function testControlSelectType(): void
  3180. {
  3181. $result = $this->Form->control(
  3182. 'email',
  3183. [
  3184. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3185. );
  3186. $expected = [
  3187. 'div' => ['class' => 'input select'],
  3188. 'label' => ['for' => 'email'],
  3189. 'Email',
  3190. '/label',
  3191. ['select' => ['name' => 'email', 'id' => 'email']],
  3192. ['option' => ['value' => '']],
  3193. '/option',
  3194. ['option' => ['value' => 'è']],
  3195. 'Firést',
  3196. '/option',
  3197. ['option' => ['value' => 'é']],
  3198. 'Secoènd',
  3199. '/option',
  3200. '/select',
  3201. '/div',
  3202. ];
  3203. $this->assertHtml($expected, $result);
  3204. $result = $this->Form->control(
  3205. 'email',
  3206. [
  3207. 'options' => ['First', 'Second'], 'empty' => true]
  3208. );
  3209. $expected = [
  3210. 'div' => ['class' => 'input select'],
  3211. 'label' => ['for' => 'email'],
  3212. 'Email',
  3213. '/label',
  3214. ['select' => ['name' => 'email', 'id' => 'email']],
  3215. ['option' => ['value' => '']],
  3216. '/option',
  3217. ['option' => ['value' => '0']],
  3218. 'First',
  3219. '/option',
  3220. ['option' => ['value' => '1']],
  3221. 'Second',
  3222. '/option',
  3223. '/select',
  3224. '/div',
  3225. ];
  3226. $this->assertHtml($expected, $result);
  3227. $result = $this->Form->control('email', [
  3228. 'type' => 'select',
  3229. 'options' => new ArrayObject(['First', 'Second']),
  3230. 'empty' => true,
  3231. ]);
  3232. $this->assertHtml($expected, $result);
  3233. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3234. $this->View->setRequest(
  3235. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3236. );
  3237. $this->Form->create();
  3238. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3239. $expected = [
  3240. 'div' => ['class' => 'input select'],
  3241. 'label' => ['for' => 'model-user-id'],
  3242. 'User',
  3243. '/label',
  3244. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3245. ['option' => ['value' => '']],
  3246. '/option',
  3247. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3248. 'good',
  3249. '/option',
  3250. ['option' => ['value' => 'other']],
  3251. 'bad',
  3252. '/option',
  3253. '/select',
  3254. '/div',
  3255. ];
  3256. $this->assertHtml($expected, $result);
  3257. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3258. $this->View->setRequest(
  3259. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3260. );
  3261. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3262. $expected = [
  3263. 'div' => ['class' => 'input select'],
  3264. 'label' => ['for' => 'thing-user-id'],
  3265. 'User',
  3266. '/label',
  3267. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3268. ['option' => ['value' => '']],
  3269. 'Some Empty',
  3270. '/option',
  3271. ['option' => ['value' => 'value']],
  3272. 'good',
  3273. '/option',
  3274. ['option' => ['value' => 'other']],
  3275. 'bad',
  3276. '/option',
  3277. '/select',
  3278. '/div',
  3279. ];
  3280. $this->assertHtml($expected, $result);
  3281. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3282. $this->View->setRequest(
  3283. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3284. );
  3285. $this->Form->create();
  3286. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3287. $expected = [
  3288. 'div' => ['class' => 'input select'],
  3289. 'label' => ['for' => 'thing-user-id'],
  3290. 'User',
  3291. '/label',
  3292. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3293. ['option' => ['value' => '']],
  3294. 'Some Empty',
  3295. '/option',
  3296. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3297. 'good',
  3298. '/option',
  3299. ['option' => ['value' => 'other']],
  3300. 'bad',
  3301. '/option',
  3302. '/select',
  3303. '/div',
  3304. ];
  3305. $this->assertHtml($expected, $result);
  3306. $result = $this->Form->control('Publisher.id', [
  3307. 'label' => 'Publisher',
  3308. 'type' => 'select',
  3309. 'multiple' => 'checkbox',
  3310. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3311. ]);
  3312. $expected = [
  3313. ['div' => ['class' => 'input select']],
  3314. ['label' => ['for' => 'publisher-id']],
  3315. 'Publisher',
  3316. '/label',
  3317. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'publisher-id'],
  3318. ['div' => ['class' => 'checkbox']],
  3319. ['label' => ['for' => 'publisher-id-value-1']],
  3320. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3321. 'Label 1',
  3322. '/label',
  3323. '/div',
  3324. ['div' => ['class' => 'checkbox']],
  3325. ['label' => ['for' => 'publisher-id-value-2']],
  3326. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3327. 'Label 2',
  3328. '/label',
  3329. '/div',
  3330. '/div',
  3331. ];
  3332. $this->assertHtml($expected, $result);
  3333. $articlesTable = $this->getTableLocator()->get('Articles');
  3334. $articlesTable->getSchema()->setColumnType(
  3335. 'published',
  3336. EnumType::from(ArticleStatus::class)
  3337. );
  3338. $this->Form->create($articlesTable->newEmptyEntity());
  3339. $result = $this->Form->control('published');
  3340. $expected = [
  3341. 'div' => ['class' => 'input select'],
  3342. 'label' => ['for' => 'published'],
  3343. 'Published',
  3344. '/label',
  3345. 'select' => ['name' => 'published', 'id' => 'published'],
  3346. ['option' => ['value' => 'Y']],
  3347. 'PUBLISHED',
  3348. '/option',
  3349. ['option' => ['value' => 'N', 'selected' => 'selected']],
  3350. 'UNPUBLISHED',
  3351. '/option',
  3352. '/select',
  3353. '/div',
  3354. ];
  3355. $this->assertHtml($expected, $result);
  3356. $articlesTable->getSchema()->setColumnType(
  3357. 'published',
  3358. EnumType::from(ArticleStatusLabel::class)
  3359. );
  3360. $this->Form->create($articlesTable->newEmptyEntity());
  3361. $result = $this->Form->control('published');
  3362. $expected = [
  3363. 'div' => ['class' => 'input select'],
  3364. 'label' => ['for' => 'published'],
  3365. 'Published',
  3366. '/label',
  3367. 'select' => ['name' => 'published', 'id' => 'published'],
  3368. ['option' => ['value' => 'Y']],
  3369. 'Is published',
  3370. '/option',
  3371. ['option' => ['value' => 'N', 'selected' => 'selected']],
  3372. 'Is unpublished',
  3373. '/option',
  3374. '/select',
  3375. '/div',
  3376. ];
  3377. $this->assertHtml($expected, $result);
  3378. $articlesTable->getSchema()->setColumnType(
  3379. 'published',
  3380. EnumType::from(ArticleStatusLabelInterface::class)
  3381. );
  3382. $article = $articlesTable->newEmptyEntity();
  3383. $this->Form->create($article);
  3384. $result = $this->Form->control('published');
  3385. $expected = [
  3386. 'div' => ['class' => 'input select'],
  3387. 'label' => ['for' => 'published'],
  3388. 'Published',
  3389. '/label',
  3390. 'select' => ['name' => 'published', 'id' => 'published'],
  3391. ['option' => ['value' => 'Y']],
  3392. 'Is published',
  3393. '/option',
  3394. ['option' => ['value' => 'N', 'selected' => 'selected']],
  3395. 'Is unpublished',
  3396. '/option',
  3397. '/select',
  3398. '/div',
  3399. ];
  3400. $this->assertHtml($expected, $result);
  3401. }
  3402. /**
  3403. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3404. *
  3405. * Test that control() and a non standard primary key makes a hidden input by default.
  3406. */
  3407. public function testControlWithNonStandardPrimaryKeyMakesHidden(): void
  3408. {
  3409. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3410. $this->Form->create($this->article);
  3411. $result = $this->Form->control('title');
  3412. $expected = [
  3413. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3414. ];
  3415. $this->assertHtml($expected, $result);
  3416. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3417. $this->Form->create($this->article);
  3418. $result = $this->Form->control('title');
  3419. $expected = [
  3420. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3421. ];
  3422. $this->assertHtml($expected, $result);
  3423. $result = $this->Form->control('body');
  3424. $expected = [
  3425. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3426. ];
  3427. $this->assertHtml($expected, $result);
  3428. }
  3429. /**
  3430. * testControlOverridingMagicSelectType method
  3431. *
  3432. * Test that overriding the magic select type widget is possible.
  3433. */
  3434. public function testControlOverridingMagicSelectType(): void
  3435. {
  3436. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3437. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3438. $expected = [
  3439. 'div' => ['class' => 'input text'],
  3440. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3441. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3442. '/div',
  3443. ];
  3444. $this->assertHtml($expected, $result);
  3445. //Check that magic types still work for plural/singular vars
  3446. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3447. $result = $this->Form->control('Model.type');
  3448. $expected = [
  3449. 'div' => ['class' => 'input select'],
  3450. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3451. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3452. ['option' => ['value' => 'value']], 'good', '/option',
  3453. ['option' => ['value' => 'other']], 'bad', '/option',
  3454. '/select',
  3455. '/div',
  3456. ];
  3457. $this->assertHtml($expected, $result);
  3458. }
  3459. /**
  3460. * testControlMagicTypeDoesNotOverride method
  3461. *
  3462. * Test that inferred types do not override developer input.
  3463. */
  3464. public function testControlMagicTypeDoesNotOverride(): void
  3465. {
  3466. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3467. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3468. $expected = [
  3469. 'div' => ['class' => 'input checkbox'],
  3470. ['input' => [
  3471. 'type' => 'hidden',
  3472. 'name' => 'Model[user]',
  3473. 'value' => 0,
  3474. ]],
  3475. 'label' => ['for' => 'model-user'],
  3476. ['input' => [
  3477. 'name' => 'Model[user]',
  3478. 'type' => 'checkbox',
  3479. 'id' => 'model-user',
  3480. 'value' => 1,
  3481. ]],
  3482. 'User',
  3483. '/label',
  3484. '/div',
  3485. ];
  3486. $this->assertHtml($expected, $result);
  3487. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3488. $options = [
  3489. 1 => 'blue',
  3490. 2 => 'red',
  3491. ];
  3492. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3493. $expected = [
  3494. 'div' => ['class' => 'input select'],
  3495. 'label' => ['for' => 'tags-ids'],
  3496. 'Tags',
  3497. '/label',
  3498. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => '', 'id' => 'tags-ids'],
  3499. ['div' => ['class' => 'checkbox']],
  3500. ['label' => ['for' => 'tags-ids-1']],
  3501. ['input' => [
  3502. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3503. 'value' => '1', 'name' => 'tags[_ids][]',
  3504. ]],
  3505. 'blue',
  3506. '/label',
  3507. '/div',
  3508. ['div' => ['class' => 'checkbox']],
  3509. ['label' => ['for' => 'tags-ids-2']],
  3510. ['input' => [
  3511. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3512. 'value' => '2', 'name' => 'tags[_ids][]',
  3513. ]],
  3514. 'red',
  3515. '/label',
  3516. '/div',
  3517. '/div',
  3518. ];
  3519. $this->assertHtml($expected, $result);
  3520. }
  3521. /**
  3522. * testControlMagicSelectForTypeNumber method
  3523. *
  3524. * Test that magic control() selects are created for type=number.
  3525. */
  3526. public function testControlMagicSelectForTypeNumber(): void
  3527. {
  3528. $this->getTableLocator()->get('ValidateUsers', [
  3529. 'className' => ValidateUsersTable::class,
  3530. ]);
  3531. $entity = new Entity(['balance' => 1]);
  3532. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3533. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3534. $result = $this->Form->control('balance');
  3535. $expected = [
  3536. 'div' => ['class' => 'input select'],
  3537. 'label' => ['for' => 'balance'],
  3538. 'Balance',
  3539. '/label',
  3540. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3541. ['option' => ['value' => '0']],
  3542. 'nothing',
  3543. '/option',
  3544. ['option' => ['value' => '1', 'selected' => 'selected']],
  3545. 'some',
  3546. '/option',
  3547. ['option' => ['value' => '100']],
  3548. 'a lot',
  3549. '/option',
  3550. '/select',
  3551. '/div',
  3552. ];
  3553. $this->assertHtml($expected, $result);
  3554. }
  3555. /**
  3556. * testInvalidControlTypeOption method
  3557. *
  3558. * Test invalid 'input' type option to control() function.
  3559. */
  3560. public function testInvalidControlTypeOption(): void
  3561. {
  3562. $this->expectException(InvalidArgumentException::class);
  3563. $this->expectExceptionMessage('Invalid type `input` used for field `text`.');
  3564. $this->Form->control('text', ['type' => 'input']);
  3565. }
  3566. /**
  3567. * testControlMagicSelectChangeToRadio method
  3568. *
  3569. * Test that magic control() selects can easily be converted into radio types without error.
  3570. */
  3571. public function testControlMagicSelectChangeToRadio(): void
  3572. {
  3573. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3574. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3575. $this->assertStringContainsString('input type="radio"', $result);
  3576. }
  3577. /**
  3578. * testFormControlSubmit method
  3579. *
  3580. * Test correct results for form::control() and type submit.
  3581. */
  3582. public function testFormControlSubmit(): void
  3583. {
  3584. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3585. $expected = [
  3586. 'div' => ['class' => 'submit'],
  3587. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3588. '/div',
  3589. ];
  3590. $this->assertHtml($expected, $result);
  3591. }
  3592. /**
  3593. * testFormControls method
  3594. *
  3595. * Test correct results from Form::controls().
  3596. */
  3597. public function testFormControlsLegendFieldset(): void
  3598. {
  3599. $this->Form->create($this->article);
  3600. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3601. $expected = [
  3602. '<fieldset',
  3603. '<legend',
  3604. 'The Legend',
  3605. '/legend',
  3606. '*/fieldset',
  3607. ];
  3608. $this->assertHtml($expected, $result);
  3609. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3610. $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
  3611. $this->assertStringContainsString('<fieldset>', $result);
  3612. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3613. $this->assertStringNotContainsString('<legend>', $result);
  3614. $this->assertStringNotContainsString('<fieldset>', $result);
  3615. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3616. $this->assertStringNotContainsString('<legend>', $result);
  3617. $this->assertStringNotContainsString('<fieldset>', $result);
  3618. $this->Form->create($this->article);
  3619. $this->View->setRequest($this->View->getRequest()
  3620. ->withParam('prefix', 'admin')
  3621. ->withParam('action', 'admin_edit')
  3622. ->withParam('controller', 'articles'));
  3623. $result = $this->Form->allControls();
  3624. $expected = [
  3625. '<fieldset',
  3626. '<legend',
  3627. 'New Article',
  3628. '/legend',
  3629. '*/fieldset',
  3630. ];
  3631. $this->assertHtml($expected, $result);
  3632. $this->Form->create($this->article);
  3633. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3634. $expected = [
  3635. '<fieldset',
  3636. '<legend',
  3637. 'The Legend',
  3638. '/legend',
  3639. '*/fieldset',
  3640. ];
  3641. $this->assertHtml($expected, $result);
  3642. $this->Form->create($this->article);
  3643. $result = $this->Form->allControls([], [
  3644. 'fieldset' => [
  3645. 'class' => 'some-class some-other-class',
  3646. 'disabled' => true,
  3647. 'data-param' => 'a-param',
  3648. ],
  3649. 'legend' => 'The Legend',
  3650. ]);
  3651. $expected = [
  3652. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3653. '<legend',
  3654. 'The Legend',
  3655. '/legend',
  3656. '*/fieldset',
  3657. ];
  3658. $this->assertHtml($expected, $result);
  3659. }
  3660. /**
  3661. * testFormControls method
  3662. *
  3663. * Test the controls() method.
  3664. */
  3665. public function testFormControls(): void
  3666. {
  3667. $this->Form->create($this->article);
  3668. $result = $this->Form->allControls();
  3669. $expected = [
  3670. '<fieldset',
  3671. '<legend', 'New Article', '/legend',
  3672. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3673. ['div' => ['class' => 'input select required']],
  3674. '*/div',
  3675. ['div' => ['class' => 'input text required']],
  3676. '*/div',
  3677. ['div' => ['class' => 'input text']],
  3678. '*/div',
  3679. ['div' => ['class' => 'input text']],
  3680. '*/div',
  3681. '/fieldset',
  3682. ];
  3683. $this->assertHtml($expected, $result);
  3684. $result = $this->Form->allControls([
  3685. 'published' => ['type' => 'boolean'],
  3686. ]);
  3687. $expected = [
  3688. '<fieldset',
  3689. '<legend', 'New Article', '/legend',
  3690. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3691. ['div' => ['class' => 'input select required']],
  3692. '*/div',
  3693. ['div' => ['class' => 'input text required']],
  3694. '*/div',
  3695. ['div' => ['class' => 'input text']],
  3696. '*/div',
  3697. ['div' => ['class' => 'input boolean']],
  3698. '*/div',
  3699. '/fieldset',
  3700. ];
  3701. $this->assertHtml($expected, $result);
  3702. $this->Form->create($this->article);
  3703. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3704. $expected = [
  3705. 'fieldset' => [],
  3706. 'legend' => [],
  3707. 'Hello',
  3708. '/legend',
  3709. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3710. ['div' => ['class' => 'input select required']],
  3711. '*/div',
  3712. ['div' => ['class' => 'input text required']],
  3713. '*/div',
  3714. ['div' => ['class' => 'input text']],
  3715. '*/div',
  3716. ['div' => ['class' => 'input text']],
  3717. '*/div',
  3718. '/fieldset',
  3719. ];
  3720. $this->assertHtml($expected, $result);
  3721. $this->Form->create();
  3722. $expected = [
  3723. 'fieldset' => [],
  3724. ['div' => ['class' => 'input text']],
  3725. 'label' => ['for' => 'foo'],
  3726. 'Foo',
  3727. '/label',
  3728. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3729. '*/div',
  3730. '/fieldset',
  3731. ];
  3732. $result = $this->Form->allControls(
  3733. ['foo' => ['type' => 'text']],
  3734. ['legend' => false]
  3735. );
  3736. $this->assertHtml($expected, $result);
  3737. }
  3738. /**
  3739. * testFormControlsBlacklist method
  3740. */
  3741. public function testFormControlsBlacklist(): void
  3742. {
  3743. $this->Form->create($this->article);
  3744. $result = $this->Form->allControls([
  3745. 'id' => false,
  3746. ]);
  3747. $expected = [
  3748. '<fieldset',
  3749. '<legend', 'New Article', '/legend',
  3750. ['div' => ['class' => 'input select required']],
  3751. '*/div',
  3752. ['div' => ['class' => 'input text required']],
  3753. '*/div',
  3754. ['div' => ['class' => 'input text']],
  3755. '*/div',
  3756. ['div' => ['class' => 'input text']],
  3757. '*/div',
  3758. '/fieldset',
  3759. ];
  3760. $this->assertHtml($expected, $result);
  3761. $this->Form->create($this->article);
  3762. $result = $this->Form->allControls([
  3763. 'id' => [],
  3764. ]);
  3765. $expected = [
  3766. '<fieldset',
  3767. '<legend', 'New Article', '/legend',
  3768. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3769. ['div' => ['class' => 'input select required']],
  3770. '*/div',
  3771. ['div' => ['class' => 'input text required']],
  3772. '*/div',
  3773. ['div' => ['class' => 'input text']],
  3774. '*/div',
  3775. ['div' => ['class' => 'input text']],
  3776. '*/div',
  3777. '/fieldset',
  3778. ];
  3779. $this->assertHtml($expected, $result, true);
  3780. }
  3781. /**
  3782. * testSelectAsCheckbox method
  3783. *
  3784. * Test multi-select widget with checkbox formatting.
  3785. */
  3786. public function testSelectAsCheckbox(): void
  3787. {
  3788. $result = $this->Form->select(
  3789. 'Model.multi_field',
  3790. ['first', 'second', 'third'],
  3791. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3792. );
  3793. $expected = [
  3794. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3795. ['div' => ['class' => 'checkbox']],
  3796. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3797. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3798. 'first',
  3799. '/label',
  3800. '/div',
  3801. ['div' => ['class' => 'checkbox']],
  3802. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3803. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3804. 'second',
  3805. '/label',
  3806. '/div',
  3807. ['div' => ['class' => 'checkbox']],
  3808. ['label' => ['for' => 'model-multi-field-2']],
  3809. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3810. 'third',
  3811. '/label',
  3812. '/div',
  3813. ];
  3814. $this->assertHtml($expected, $result);
  3815. $result = $this->Form->select(
  3816. 'Model.multi_field',
  3817. ['1/2' => 'half'],
  3818. ['multiple' => 'checkbox']
  3819. );
  3820. $expected = [
  3821. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3822. ['div' => ['class' => 'checkbox']],
  3823. ['label' => ['for' => 'model-multi-field-1-2']],
  3824. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3825. 'half',
  3826. '/label',
  3827. '/div',
  3828. ];
  3829. $this->assertHtml($expected, $result);
  3830. }
  3831. /**
  3832. * testLabel method
  3833. *
  3834. * Test label generation.
  3835. */
  3836. public function testLabel(): void
  3837. {
  3838. $result = $this->Form->label('Person.name');
  3839. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3840. $this->assertHtml($expected, $result);
  3841. $result = $this->Form->label('Person.name');
  3842. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3843. $this->assertHtml($expected, $result);
  3844. $result = $this->Form->label('Person.first_name');
  3845. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3846. $this->assertHtml($expected, $result);
  3847. $result = $this->Form->label('Person.first_name', 'Your first name');
  3848. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3849. $this->assertHtml($expected, $result);
  3850. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3851. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3852. $this->assertHtml($expected, $result);
  3853. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3854. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3855. $this->assertHtml($expected, $result);
  3856. $result = $this->Form->label('Person.first_name', '');
  3857. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3858. $this->assertHtml($expected, $result);
  3859. $result = $this->Form->label('Person.2.name', '');
  3860. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3861. $this->assertHtml($expected, $result);
  3862. }
  3863. /**
  3864. * testLabelContainControl method
  3865. *
  3866. * Test that label() can accept an input with the correct template vars.
  3867. */
  3868. public function testLabelContainControl(): void
  3869. {
  3870. $this->Form->setTemplates([
  3871. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3872. ]);
  3873. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3874. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3875. ]);
  3876. $expected = [
  3877. 'label' => ['for' => 'person-accept-terms'],
  3878. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3879. 'Accept',
  3880. '/label',
  3881. ];
  3882. $this->assertHtml($expected, $result);
  3883. }
  3884. /**
  3885. * testTextbox method
  3886. *
  3887. * Test textbox element generation.
  3888. */
  3889. public function testTextbox(): void
  3890. {
  3891. $result = $this->Form->text('Model.field');
  3892. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3893. $this->assertHtml($expected, $result);
  3894. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3895. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3896. $this->assertHtml($expected, $result);
  3897. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3898. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3899. $this->assertHtml($expected, $result);
  3900. }
  3901. /**
  3902. * testTextBoxDataAndError method
  3903. *
  3904. * Test that text() hooks up with request data and error fields.
  3905. */
  3906. public function testTextBoxDataAndError(): void
  3907. {
  3908. $this->article['errors'] = [
  3909. 'Contact' => ['text' => 'wrong'],
  3910. ];
  3911. $this->View->setRequest($this->View->getRequest()
  3912. ->withData('Model.text', 'test <strong>HTML</strong> values')
  3913. ->withData('Contact.text', 'test'));
  3914. $this->Form->create($this->article);
  3915. $result = $this->Form->text('Model.text');
  3916. $expected = [
  3917. 'input' => [
  3918. 'type' => 'text',
  3919. 'name' => 'Model[text]',
  3920. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3921. ],
  3922. ];
  3923. $this->assertHtml($expected, $result);
  3924. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3925. $expected = [
  3926. 'input' => [
  3927. 'type' => 'text',
  3928. 'name' => 'Contact[text]',
  3929. 'value' => 'test',
  3930. 'id' => 'theID',
  3931. 'class' => 'form-error',
  3932. ],
  3933. ];
  3934. $this->assertHtml($expected, $result);
  3935. }
  3936. /**
  3937. * testDefaultValue method
  3938. *
  3939. * Test default value setting.
  3940. */
  3941. public function testTextDefaultValue(): void
  3942. {
  3943. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  3944. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3945. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3946. $this->assertHtml($expected, $result);
  3947. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  3948. $this->Form->create();
  3949. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3950. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3951. $this->assertHtml($expected, $result);
  3952. $Articles = $this->getTableLocator()->get('Articles');
  3953. $title = $Articles->getSchema()->getColumn('title');
  3954. $Articles->getSchema()->addColumn(
  3955. 'title',
  3956. ['default' => 'default title', 'length' => 255] + $title
  3957. );
  3958. $entity = $Articles->newEmptyEntity();
  3959. $this->Form->create($entity);
  3960. // Get default value from schema
  3961. $result = $this->Form->text('title');
  3962. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title', 'maxlength' => '255']];
  3963. $this->assertHtml($expected, $result);
  3964. // Don't get value from schema
  3965. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3966. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3967. $this->assertHtml($expected, $result);
  3968. // Custom default value overrides default value from schema
  3969. $result = $this->Form->text('title', ['default' => 'override default']);
  3970. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default', 'maxlength' => '255']];
  3971. $this->assertHtml($expected, $result);
  3972. // Default value from schema is used only for new entities.
  3973. $entity->setNew(false);
  3974. $result = $this->Form->text('title');
  3975. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3976. $this->assertHtml($expected, $result);
  3977. }
  3978. /**
  3979. * testError method
  3980. *
  3981. * Test field error generation.
  3982. */
  3983. public function testError(): void
  3984. {
  3985. $this->article['errors'] = [
  3986. 'Article' => ['field' => 'email'],
  3987. ];
  3988. $this->Form->create($this->article);
  3989. $result = $this->Form->error('Article.field');
  3990. $expected = [
  3991. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3992. 'email',
  3993. '/div',
  3994. ];
  3995. $this->assertHtml($expected, $result);
  3996. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  3997. $expected = [
  3998. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3999. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4000. '/div',
  4001. ];
  4002. $this->assertHtml($expected, $result);
  4003. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4004. $expected = [
  4005. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4006. '<strong', 'Badness!', '/strong',
  4007. '/div',
  4008. ];
  4009. $this->assertHtml($expected, $result);
  4010. }
  4011. /**
  4012. * testErrorRuleName method
  4013. *
  4014. * Test error translation can use rule names for translating.
  4015. */
  4016. public function testErrorRuleName(): void
  4017. {
  4018. $this->article['errors'] = [
  4019. 'Article' => [
  4020. 'field' => ['email' => 'Your email was not good'],
  4021. ],
  4022. ];
  4023. $this->Form->create($this->article);
  4024. $result = $this->Form->error('Article.field');
  4025. $expected = [
  4026. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4027. 'Your email was not good',
  4028. '/div',
  4029. ];
  4030. $this->assertHtml($expected, $result);
  4031. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4032. $expected = [
  4033. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4034. 'Email in use',
  4035. '/div',
  4036. ];
  4037. $this->assertHtml($expected, $result);
  4038. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4039. $expected = [
  4040. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4041. 'Email in use',
  4042. '/div',
  4043. ];
  4044. $this->assertHtml($expected, $result);
  4045. $result = $this->Form->error('Article.field', [
  4046. 'email' => 'Key is preferred',
  4047. 'Your email was not good' => 'Email in use',
  4048. ]);
  4049. $expected = [
  4050. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4051. 'Key is preferred',
  4052. '/div',
  4053. ];
  4054. $this->assertHtml($expected, $result);
  4055. }
  4056. /**
  4057. * testErrorMessages method
  4058. *
  4059. * Test error with nested lists.
  4060. */
  4061. public function testErrorMessages(): void
  4062. {
  4063. $this->article['errors'] = [
  4064. 'Article' => ['field' => 'email'],
  4065. ];
  4066. $this->Form->create($this->article);
  4067. $result = $this->Form->error('Article.field', [
  4068. 'email' => 'No good!',
  4069. ]);
  4070. $expected = [
  4071. 'div' => ['class' => 'error-message', 'id' => 'article-field-error'],
  4072. 'No good!',
  4073. '/div',
  4074. ];
  4075. $this->assertHtml($expected, $result);
  4076. }
  4077. /**
  4078. * testErrorMultipleMessages method
  4079. *
  4080. * Test error() with multiple messages.
  4081. */
  4082. public function testErrorMultipleMessages(): void
  4083. {
  4084. $this->article['errors'] = [
  4085. 'field' => ['notBlank', 'email', 'Something else'],
  4086. ];
  4087. $this->Form->create($this->article);
  4088. $result = $this->Form->error('field', [
  4089. 'notBlank' => 'Cannot be empty',
  4090. 'email' => 'No good!',
  4091. ]);
  4092. $expected = [
  4093. 'div' => ['class' => 'error-message', 'id' => 'field-error'],
  4094. 'ul' => [],
  4095. '<li', 'Cannot be empty', '/li',
  4096. '<li', 'No good!', '/li',
  4097. '<li', 'Something else', '/li',
  4098. '/ul',
  4099. '/div',
  4100. ];
  4101. $this->assertHtml($expected, $result);
  4102. }
  4103. /**
  4104. * testPassword method
  4105. *
  4106. * Test password element generation.
  4107. */
  4108. public function testPassword(): void
  4109. {
  4110. $this->article['errors'] = [
  4111. 'Contact' => [
  4112. 'passwd' => 1,
  4113. ],
  4114. ];
  4115. $this->Form->create($this->article);
  4116. $result = $this->Form->password('Contact.field');
  4117. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4118. $this->assertHtml($expected, $result);
  4119. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4120. $this->Form->create($this->article);
  4121. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4122. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4123. $this->assertHtml($expected, $result);
  4124. }
  4125. /**
  4126. * testRadio method
  4127. *
  4128. * Test radio element set generation.
  4129. */
  4130. public function testRadio(): void
  4131. {
  4132. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4133. $expected = [
  4134. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4135. ['label' => ['for' => 'model-field-0']],
  4136. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4137. 'option A',
  4138. '/label',
  4139. ['label' => ['for' => 'model-field-1']],
  4140. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4141. 'option B',
  4142. '/label',
  4143. ];
  4144. $this->assertHtml($expected, $result);
  4145. $result = $this->Form->radio('Model.field', new Collection(['option A', 'option B']));
  4146. $this->assertHtml($expected, $result);
  4147. $result = $this->Form->radio(
  4148. 'Employee.vegetarian',
  4149. ['yes' => 'Yes', 'no' => 'No'],
  4150. ['form' => 'my-form', 'id' => 'id-veg']
  4151. );
  4152. $expected = [
  4153. 'input' => ['type' => 'hidden', 'name' => 'Employee[vegetarian]', 'value' => '', 'form' => 'my-form', 'id' => 'id-veg'],
  4154. ['label' => ['for' => 'id-veg-yes']],
  4155. ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'yes', 'id' => 'id-veg-yes', 'form' => 'my-form']],
  4156. 'Yes',
  4157. '/label',
  4158. ['label' => ['for' => 'id-veg-no']],
  4159. ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'no', 'id' => 'id-veg-no', 'form' => 'my-form']],
  4160. 'No',
  4161. '/label',
  4162. ];
  4163. $this->assertHtml($expected, $result);
  4164. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4165. $expected = [
  4166. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '', 'id' => 'model-field']],
  4167. ['label' => ['for' => 'model-custom-0']],
  4168. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4169. 'option A',
  4170. '/label',
  4171. ['label' => ['for' => 'model-custom-1']],
  4172. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4173. 'option B',
  4174. '/label',
  4175. ];
  4176. $this->assertHtml($expected, $result);
  4177. $result = $this->Form->radio(
  4178. 'Employee.gender',
  4179. [
  4180. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4181. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4182. ]
  4183. );
  4184. $expected = [
  4185. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'id' => 'employee-gender'],
  4186. ['label' => ['for' => 'employee-gender-male']],
  4187. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4188. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4189. 'Male',
  4190. '/label',
  4191. ['label' => ['for' => 'employee-gender-female']],
  4192. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4193. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4194. 'Female',
  4195. '/label',
  4196. ];
  4197. $this->assertHtml($expected, $result);
  4198. $article = new Article([
  4199. 'status' => ArticleStatus::UNPUBLISHED,
  4200. ]);
  4201. $this->Form->create($article);
  4202. $result = $this->Form->radio('status', ['Y' => 'Published', 'N' => 'Unpublished']);
  4203. $expected = [
  4204. 'input' => ['type' => 'hidden', 'name' => 'status', 'value' => '', 'id' => 'status'],
  4205. ['label' => ['for' => 'status-y']],
  4206. ['input' => ['type' => 'radio', 'name' => 'status', 'value' => 'Y', 'id' => 'status-y']],
  4207. 'Published',
  4208. '/label',
  4209. ['label' => ['for' => 'status-n']],
  4210. ['input' => ['type' => 'radio', 'name' => 'status', 'value' => 'N', 'id' => 'status-n', 'checked' => 'checked']],
  4211. 'Unpublished',
  4212. '/label',
  4213. ];
  4214. $this->assertHtml($expected, $result);
  4215. }
  4216. /**
  4217. * Test radio with complex options and empty disabled data.
  4218. */
  4219. public function testRadioComplexDisabled(): void
  4220. {
  4221. $options = [
  4222. ['value' => 'r', 'text' => 'red'],
  4223. ['value' => 'b', 'text' => 'blue'],
  4224. ];
  4225. $attrs = ['disabled' => []];
  4226. $result = $this->Form->radio('Model.field', $options, $attrs);
  4227. $expected = [
  4228. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4229. ['label' => ['for' => 'model-field-r']],
  4230. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4231. 'red',
  4232. '/label',
  4233. ['label' => ['for' => 'model-field-b']],
  4234. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4235. 'blue',
  4236. '/label',
  4237. ];
  4238. $this->assertHtml($expected, $result);
  4239. $attrs = ['disabled' => ['r']];
  4240. $result = $this->Form->radio('Model.field', $options, $attrs);
  4241. $this->assertStringContainsString('disabled="disabled"', $result);
  4242. }
  4243. /**
  4244. * testRadioDefaultValue method
  4245. *
  4246. * Test default value setting on radio() method.
  4247. */
  4248. public function testRadioDefaultValue(): void
  4249. {
  4250. $Articles = $this->getTableLocator()->get('Articles');
  4251. $title = $Articles->getSchema()->getColumn('title');
  4252. $Articles->getSchema()->addColumn(
  4253. 'title',
  4254. ['default' => '1'] + $title
  4255. );
  4256. $this->Form->create($Articles->newEmptyEntity());
  4257. $result = $this->Form->radio('title', ['option A', 'option B']);
  4258. $expected = [
  4259. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4260. ['label' => ['for' => 'title-0']],
  4261. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4262. 'option A',
  4263. '/label',
  4264. ['label' => ['for' => 'title-1']],
  4265. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4266. 'option B',
  4267. '/label',
  4268. ];
  4269. $this->assertHtml($expected, $result);
  4270. }
  4271. /**
  4272. * Test setting a hiddenField value on radio buttons.
  4273. */
  4274. public function testRadioHiddenFieldValue(): void
  4275. {
  4276. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4277. $expected = [
  4278. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N', 'id' => 'title']],
  4279. 'label' => ['for' => 'title-0'],
  4280. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4281. 'option A',
  4282. '/label',
  4283. ];
  4284. $this->assertHtml($expected, $result);
  4285. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => '']);
  4286. $expected = [
  4287. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4288. 'label' => ['for' => 'title-0'],
  4289. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4290. 'option A',
  4291. '/label',
  4292. ];
  4293. $this->assertHtml($expected, $result);
  4294. }
  4295. /**
  4296. * testControlRadio method
  4297. *
  4298. * Test that input works with radio types.
  4299. */
  4300. public function testControlRadio(): void
  4301. {
  4302. $result = $this->Form->control('test', [
  4303. 'type' => 'radio',
  4304. 'options' => ['A', 'B'],
  4305. ]);
  4306. $expected = [
  4307. ['div' => ['class' => 'input radio']],
  4308. '<label',
  4309. 'Test',
  4310. '/label',
  4311. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4312. ['label' => ['for' => 'test-0']],
  4313. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4314. 'A',
  4315. '/label',
  4316. ['label' => ['for' => 'test-1']],
  4317. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4318. 'B',
  4319. '/label',
  4320. '/div',
  4321. ];
  4322. $this->assertHtml($expected, $result);
  4323. $result = $this->Form->control('test', [
  4324. 'type' => 'radio',
  4325. 'options' => ['A', 'B'],
  4326. 'value' => '0',
  4327. ]);
  4328. $expected = [
  4329. ['div' => ['class' => 'input radio']],
  4330. '<label',
  4331. 'Test',
  4332. '/label',
  4333. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4334. ['label' => ['for' => 'test-0']],
  4335. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4336. 'A',
  4337. '/label',
  4338. ['label' => ['for' => 'test-1']],
  4339. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4340. 'B',
  4341. '/label',
  4342. '/div',
  4343. ];
  4344. $this->assertHtml($expected, $result);
  4345. $result = $this->Form->control('test', [
  4346. 'type' => 'radio',
  4347. 'options' => ['A', 'B'],
  4348. 'label' => false,
  4349. ]);
  4350. $expected = [
  4351. ['div' => ['class' => 'input radio']],
  4352. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4353. ['label' => ['for' => 'test-0']],
  4354. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4355. 'A',
  4356. '/label',
  4357. ['label' => ['for' => 'test-1']],
  4358. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4359. 'B',
  4360. '/label',
  4361. '/div',
  4362. ];
  4363. $this->assertHtml($expected, $result);
  4364. $result = $this->Form->control('accept', [
  4365. 'type' => 'radio',
  4366. 'options' => [
  4367. 1 => 'positive',
  4368. -1 => 'negative',
  4369. ],
  4370. 'label' => false,
  4371. ]);
  4372. $expected = [
  4373. ['div' => ['class' => 'input radio']],
  4374. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => '', 'id' => 'accept']],
  4375. ['label' => ['for' => 'accept-1']],
  4376. ['input' => [
  4377. 'type' => 'radio',
  4378. 'name' => 'accept',
  4379. 'value' => '1',
  4380. 'id' => 'accept-1',
  4381. ]],
  4382. 'positive',
  4383. '/label',
  4384. ['label' => ['for' => 'accept--1']],
  4385. ['input' => [
  4386. 'type' => 'radio',
  4387. 'name' => 'accept',
  4388. 'value' => '-1',
  4389. 'id' => 'accept--1',
  4390. ]],
  4391. 'negative',
  4392. '/label',
  4393. '/div',
  4394. ];
  4395. $this->assertHtml($expected, $result);
  4396. $articlesTable = $this->getTableLocator()->get('Articles');
  4397. $articlesTable->getSchema()->setColumnType(
  4398. 'published',
  4399. EnumType::from(ArticleStatus::class)
  4400. );
  4401. $this->Form->create($articlesTable->newEmptyEntity());
  4402. $result = $this->Form->control('published', ['type' => 'radio', 'label' => false,]);
  4403. $expected = [
  4404. ['div' => ['class' => 'input radio']],
  4405. 'input' => ['type' => 'hidden', 'name' => 'published', 'value' => '', 'id' => 'published'],
  4406. ['label' => ['for' => 'published-y']],
  4407. ['input' => ['type' => 'radio', 'name' => 'published', 'value' => 'Y', 'id' => 'published-y']],
  4408. 'PUBLISHED',
  4409. '/label',
  4410. ['label' => ['for' => 'published-n']],
  4411. ['input' => ['type' => 'radio', 'name' => 'published', 'value' => 'N', 'id' => 'published-n', 'checked' => 'checked']],
  4412. 'UNPUBLISHED',
  4413. '/label',
  4414. '/div',
  4415. ];
  4416. $this->assertHtml($expected, $result);
  4417. }
  4418. /**
  4419. * testRadioNoLabel method
  4420. *
  4421. * Test that radio() works with label = false.
  4422. */
  4423. public function testRadioNoLabel(): void
  4424. {
  4425. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4426. $expected = [
  4427. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4428. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4429. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4430. ];
  4431. $this->assertHtml($expected, $result);
  4432. }
  4433. /**
  4434. * testRadioControlInsideLabel method
  4435. *
  4436. * Test generating radio input inside label ala twitter bootstrap.
  4437. */
  4438. public function testRadioControlInsideLabel(): void
  4439. {
  4440. $this->Form->setTemplates([
  4441. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4442. 'radioWrapper' => '{{label}}',
  4443. ]);
  4444. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4445. // phpcs:disable
  4446. $expected = [
  4447. ['input' => [
  4448. 'type' => 'hidden',
  4449. 'name' => 'Model[field]',
  4450. 'value' => '',
  4451. 'id' => 'model-field'
  4452. ]],
  4453. ['label' => ['for' => 'model-field-0']],
  4454. ['input' => [
  4455. 'type' => 'radio',
  4456. 'name' => 'Model[field]',
  4457. 'value' => '0',
  4458. 'id' => 'model-field-0'
  4459. ]],
  4460. 'option A',
  4461. '/label',
  4462. ['label' => ['for' => 'model-field-1']],
  4463. ['input' => [
  4464. 'type' => 'radio',
  4465. 'name' => 'Model[field]',
  4466. 'value' => '1',
  4467. 'id' => 'model-field-1'
  4468. ]],
  4469. 'option B',
  4470. '/label',
  4471. ];
  4472. // phpcs:enable
  4473. $this->assertHtml($expected, $result);
  4474. }
  4475. /**
  4476. * testRadioHiddenControlDisabling method
  4477. *
  4478. * Test disabling the hidden input for radio buttons.
  4479. */
  4480. public function testRadioHiddenControlDisabling(): void
  4481. {
  4482. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4483. $expected = [
  4484. 'label' => ['for' => 'model-1-field-0'],
  4485. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4486. 'option A',
  4487. '/label',
  4488. ];
  4489. $this->assertHtml($expected, $result);
  4490. }
  4491. /**
  4492. * testRadioOutOfRange method
  4493. *
  4494. * Test radio element set generation.
  4495. */
  4496. public function testRadioOutOfRange(): void
  4497. {
  4498. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4499. $expected = [
  4500. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4501. 'label' => ['for' => 'model-field-v'],
  4502. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4503. 'value',
  4504. '/label',
  4505. ];
  4506. $this->assertHtml($expected, $result);
  4507. }
  4508. /**
  4509. * testSelect method
  4510. *
  4511. * Test select element generation.
  4512. */
  4513. public function testSelect(): void
  4514. {
  4515. $result = $this->Form->select('Model.field', []);
  4516. $expected = [
  4517. 'select' => ['name' => 'Model[field]'],
  4518. '/select',
  4519. ];
  4520. $this->assertHtml($expected, $result);
  4521. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4522. $this->Form->create();
  4523. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4524. $expected = [
  4525. 'select' => ['name' => 'Model[field]'],
  4526. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4527. 'good',
  4528. '/option',
  4529. ['option' => ['value' => 'other']],
  4530. 'bad',
  4531. '/option',
  4532. '/select',
  4533. ];
  4534. $this->assertHtml($expected, $result);
  4535. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4536. $this->assertHtml($expected, $result);
  4537. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4538. $this->Form->create();
  4539. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4540. $expected = [
  4541. 'select' => ['name' => 'Model[field]'],
  4542. ['option' => ['value' => 'value']],
  4543. 'good',
  4544. '/option',
  4545. ['option' => ['value' => 'other']],
  4546. 'bad',
  4547. '/option',
  4548. '/select',
  4549. ];
  4550. $this->assertHtml($expected, $result);
  4551. $options = [
  4552. ['value' => 'first', 'text' => 'First'],
  4553. ['value' => 'first', 'text' => 'Another First'],
  4554. ];
  4555. $result = $this->Form->select(
  4556. 'Model.field',
  4557. $options,
  4558. ['escape' => false, 'empty' => false]
  4559. );
  4560. $expected = [
  4561. 'select' => ['name' => 'Model[field]'],
  4562. ['option' => ['value' => 'first']],
  4563. 'First',
  4564. '/option',
  4565. ['option' => ['value' => 'first']],
  4566. 'Another First',
  4567. '/option',
  4568. '/select',
  4569. ];
  4570. $this->assertHtml($expected, $result);
  4571. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4572. $this->Form->create();
  4573. $result = $this->Form->select(
  4574. 'Model.contact_id',
  4575. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4576. ['escape' => false, 'empty' => 'pick something']
  4577. );
  4578. $expected = [
  4579. 'select' => ['name' => 'Model[contact_id]'],
  4580. ['option' => ['value' => '']], 'pick something', '/option',
  4581. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4582. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4583. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4584. '/select',
  4585. ];
  4586. $this->assertHtml($expected, $result);
  4587. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4588. $this->Form->create();
  4589. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4590. $expected = [
  4591. 'select' => ['name' => 'Model[field]'],
  4592. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4593. ['option' => ['value' => '1']], 'Yes', '/option',
  4594. '/select',
  4595. ];
  4596. $this->assertHtml($expected, $result);
  4597. $article = new Article([
  4598. 'status' => ArticleStatus::UNPUBLISHED,
  4599. ]);
  4600. $this->Form->create($article);
  4601. $result = $this->Form->select('status', ['Y' => 'Published', 'N' => 'Unpublished']);
  4602. $expected = [
  4603. 'select' => ['name' => 'status'],
  4604. ['option' => ['value' => 'Y']], 'Published', '/option',
  4605. ['option' => ['value' => 'N', 'selected' => 'selected']], 'Unpublished', '/option',
  4606. '/select',
  4607. ];
  4608. $this->assertHtml($expected, $result);
  4609. }
  4610. /**
  4611. * testSelectEscapeHtml method
  4612. *
  4613. * Test that select() escapes HTML.
  4614. */
  4615. public function testSelectEscapeHtml(): void
  4616. {
  4617. $result = $this->Form->select(
  4618. 'Model.field',
  4619. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4620. ['empty' => false]
  4621. );
  4622. $expected = [
  4623. 'select' => ['name' => 'Model[field]'],
  4624. ['option' => ['value' => 'first']],
  4625. 'first &quot;html&quot; &lt;chars&gt;',
  4626. '/option',
  4627. ['option' => ['value' => 'second']],
  4628. 'value',
  4629. '/option',
  4630. '/select',
  4631. ];
  4632. $this->assertHtml($expected, $result);
  4633. $result = $this->Form->select(
  4634. 'Model.field',
  4635. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4636. ['escape' => false, 'empty' => false]
  4637. );
  4638. $expected = [
  4639. 'select' => ['name' => 'Model[field]'],
  4640. ['option' => ['value' => 'first']],
  4641. 'first "html" <chars>',
  4642. '/option',
  4643. ['option' => ['value' => 'second']],
  4644. 'value',
  4645. '/option',
  4646. '/select',
  4647. ];
  4648. $this->assertHtml($expected, $result);
  4649. }
  4650. /**
  4651. * testSelectRequired method
  4652. *
  4653. * Test select() with required and disabled attributes.
  4654. */
  4655. public function testSelectRequired(): void
  4656. {
  4657. $this->article['required'] = [
  4658. 'user_id' => true,
  4659. ];
  4660. $this->Form->create($this->article);
  4661. $result = $this->Form->select('user_id', ['option A']);
  4662. $expected = [
  4663. 'select' => [
  4664. 'name' => 'user_id',
  4665. 'required' => 'required',
  4666. ],
  4667. ['option' => ['value' => '0']], 'option A', '/option',
  4668. '/select',
  4669. ];
  4670. $this->assertHtml($expected, $result);
  4671. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4672. $expected = [
  4673. 'select' => [
  4674. 'name' => 'user_id',
  4675. 'disabled' => 'disabled',
  4676. ],
  4677. ['option' => ['value' => '0']], 'option A', '/option',
  4678. '/select',
  4679. ];
  4680. $this->assertHtml($expected, $result);
  4681. }
  4682. public function testSelectEmptyWithRequiredFalse(): void
  4683. {
  4684. $Articles = $this->getTableLocator()->get('Articles');
  4685. $validator = $Articles->getValidator('default');
  4686. $validator->allowEmptyString('user_id');
  4687. $Articles->setValidator('default', $validator);
  4688. $entity = $Articles->newEmptyEntity();
  4689. $this->Form->create($entity);
  4690. $result = $this->Form->select('user_id', ['option A']);
  4691. $expected = [
  4692. 'select' => [
  4693. 'name' => 'user_id',
  4694. ],
  4695. ['option' => ['value' => '']], '/option',
  4696. ['option' => ['value' => '0']], 'option A', '/option',
  4697. '/select',
  4698. ];
  4699. $this->assertHtml($expected, $result);
  4700. }
  4701. /**
  4702. * testNestedSelect method
  4703. *
  4704. * Test select element generation with optgroups.
  4705. */
  4706. public function testNestedSelect(): void
  4707. {
  4708. $result = $this->Form->select(
  4709. 'Model.field',
  4710. [1 => 'One', 2 => 'Two', 'Three' => [
  4711. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4712. ]],
  4713. ['empty' => false]
  4714. );
  4715. $expected = [
  4716. 'select' => ['name' => 'Model[field]'],
  4717. ['option' => ['value' => 1]],
  4718. 'One',
  4719. '/option',
  4720. ['option' => ['value' => 2]],
  4721. 'Two',
  4722. '/option',
  4723. ['optgroup' => ['label' => 'Three']],
  4724. ['option' => ['value' => 3]],
  4725. 'Three',
  4726. '/option',
  4727. ['option' => ['value' => 4]],
  4728. 'Four',
  4729. '/option',
  4730. ['option' => ['value' => 5]],
  4731. 'Five',
  4732. '/option',
  4733. '/optgroup',
  4734. '/select',
  4735. ];
  4736. $this->assertHtml($expected, $result);
  4737. }
  4738. /**
  4739. * testSelectMultiple method
  4740. *
  4741. * Test generation of multiple select elements.
  4742. */
  4743. public function testSelectMultiple(): void
  4744. {
  4745. $options = ['first', 'second', 'third'];
  4746. $result = $this->Form->select(
  4747. 'Model.multi_field',
  4748. $options,
  4749. ['form' => 'my-form', 'multiple' => true]
  4750. );
  4751. $expected = [
  4752. 'input' => [
  4753. 'type' => 'hidden',
  4754. 'name' => 'Model[multi_field]',
  4755. 'value' => '',
  4756. 'form' => 'my-form',
  4757. ],
  4758. 'select' => [
  4759. 'name' => 'Model[multi_field][]',
  4760. 'multiple' => 'multiple',
  4761. 'form' => 'my-form',
  4762. ],
  4763. ['option' => ['value' => '0']],
  4764. 'first',
  4765. '/option',
  4766. ['option' => ['value' => '1']],
  4767. 'second',
  4768. '/option',
  4769. ['option' => ['value' => '2']],
  4770. 'third',
  4771. '/option',
  4772. '/select',
  4773. ];
  4774. $this->assertHtml($expected, $result);
  4775. $result = $this->Form->select(
  4776. 'Model.multi_field',
  4777. $options,
  4778. ['multiple' => 'multiple', 'form' => 'my-form']
  4779. );
  4780. $this->assertHtml($expected, $result);
  4781. $result = $this->Form->select(
  4782. 'Model.multi_field',
  4783. $options,
  4784. ['form' => 'my-form', 'multiple' => false]
  4785. );
  4786. $this->assertStringNotContainsString('multiple', $result);
  4787. }
  4788. /**
  4789. * testCheckboxZeroValue method
  4790. *
  4791. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4792. */
  4793. public function testCheckboxZeroValue(): void
  4794. {
  4795. $result = $this->Form->control('User.get_spam', [
  4796. 'type' => 'checkbox',
  4797. 'value' => '0',
  4798. 'hiddenField' => '1',
  4799. ]);
  4800. $expected = [
  4801. 'div' => ['class' => 'input checkbox'],
  4802. 'label' => ['for' => 'user-get-spam'],
  4803. ['input' => [
  4804. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4805. 'value' => '1',
  4806. ]],
  4807. ['input' => [
  4808. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4809. 'value' => '0', 'id' => 'user-get-spam',
  4810. ]],
  4811. 'Get Spam',
  4812. '/label',
  4813. '/div',
  4814. ];
  4815. $this->assertHtml($expected, $result);
  4816. $result = $this->Form->control('User.get_spam', [
  4817. 'type' => 'checkbox',
  4818. 'value' => '0',
  4819. 'hiddenField' => '',
  4820. ]);
  4821. $expected = [
  4822. 'div' => ['class' => 'input checkbox'],
  4823. 'label' => ['for' => 'user-get-spam'],
  4824. ['input' => [
  4825. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4826. 'value' => '',
  4827. ]],
  4828. ['input' => [
  4829. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4830. 'value' => '0', 'id' => 'user-get-spam',
  4831. ]],
  4832. 'Get Spam',
  4833. '/label',
  4834. '/div',
  4835. ];
  4836. $this->assertHtml($expected, $result);
  4837. }
  4838. /**
  4839. * testHabtmSelectBox method
  4840. *
  4841. * Test generation of habtm select boxes.
  4842. */
  4843. public function testHabtmSelectBox(): void
  4844. {
  4845. $options = [
  4846. 1 => 'blue',
  4847. 2 => 'red',
  4848. 3 => 'green',
  4849. ];
  4850. $tags = [
  4851. new Entity(['id' => 1, 'name' => 'blue']),
  4852. new Entity(['id' => 3, 'name' => 'green']),
  4853. ];
  4854. $article = new Article(['tags' => $tags]);
  4855. $this->Form->create($article);
  4856. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4857. $expected = [
  4858. 'div' => ['class' => 'input select'],
  4859. 'label' => ['for' => 'tags-ids'],
  4860. 'Tags',
  4861. '/label',
  4862. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4863. 'select' => [
  4864. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4865. 'multiple' => 'multiple',
  4866. ],
  4867. ['option' => ['value' => '1', 'selected' => 'selected']],
  4868. 'blue',
  4869. '/option',
  4870. ['option' => ['value' => '2']],
  4871. 'red',
  4872. '/option',
  4873. ['option' => ['value' => '3', 'selected' => 'selected']],
  4874. 'green',
  4875. '/option',
  4876. '/select',
  4877. '/div',
  4878. ];
  4879. $this->assertHtml($expected, $result);
  4880. // make sure only 50 is selected, and not 50f5c0cf
  4881. $options = [
  4882. '1' => 'blue',
  4883. '50f5c0cf' => 'red',
  4884. '50' => 'green',
  4885. ];
  4886. $tags = [
  4887. new Entity(['id' => 1, 'name' => 'blue']),
  4888. new Entity(['id' => 50, 'name' => 'green']),
  4889. ];
  4890. $article = new Article(['tags' => $tags]);
  4891. $this->Form->create($article);
  4892. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4893. $expected = [
  4894. 'div' => ['class' => 'input select'],
  4895. 'label' => ['for' => 'tags-ids'],
  4896. 'Tags',
  4897. '/label',
  4898. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4899. 'select' => [
  4900. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4901. 'multiple' => 'multiple',
  4902. ],
  4903. ['option' => ['value' => '1', 'selected' => 'selected']],
  4904. 'blue',
  4905. '/option',
  4906. ['option' => ['value' => '50f5c0cf']],
  4907. 'red',
  4908. '/option',
  4909. ['option' => ['value' => '50', 'selected' => 'selected']],
  4910. 'green',
  4911. '/option',
  4912. '/select',
  4913. '/div',
  4914. ];
  4915. $this->assertHtml($expected, $result);
  4916. $spacecraft = [
  4917. 1 => 'Orion',
  4918. 2 => 'Helios',
  4919. ];
  4920. $this->View->set('spacecraft', $spacecraft);
  4921. $this->Form->create();
  4922. $result = $this->Form->control('spacecraft._ids');
  4923. $expected = [
  4924. 'div' => ['class' => 'input select'],
  4925. 'label' => ['for' => 'spacecraft-ids'],
  4926. 'Spacecraft',
  4927. '/label',
  4928. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4929. 'select' => [
  4930. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4931. 'multiple' => 'multiple',
  4932. ],
  4933. ['option' => ['value' => '1']],
  4934. 'Orion',
  4935. '/option',
  4936. ['option' => ['value' => '2']],
  4937. 'Helios',
  4938. '/option',
  4939. '/select',
  4940. '/div',
  4941. ];
  4942. $this->assertHtml($expected, $result);
  4943. }
  4944. /**
  4945. * testErrorsForBelongsToManySelect method
  4946. *
  4947. * Tests that errors for belongsToMany select fields are being
  4948. * picked up properly.
  4949. */
  4950. public function testErrorsForBelongsToManySelect(): void
  4951. {
  4952. $spacecraft = [
  4953. 1 => 'Orion',
  4954. 2 => 'Helios',
  4955. ];
  4956. $this->View->set('spacecraft', $spacecraft);
  4957. $article = new Article();
  4958. $article->setError('spacecraft', ['Invalid']);
  4959. $this->Form->create($article);
  4960. $result = $this->Form->control('spacecraft._ids');
  4961. $expected = [
  4962. ['div' => ['class' => 'input select error']],
  4963. 'label' => ['for' => 'spacecraft-ids'],
  4964. 'Spacecraft',
  4965. '/label',
  4966. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4967. 'select' => [
  4968. 'name' => 'spacecraft[_ids][]',
  4969. 'id' => 'spacecraft-ids',
  4970. 'multiple' => 'multiple',
  4971. ],
  4972. ['option' => ['value' => '1']],
  4973. 'Orion',
  4974. '/option',
  4975. ['option' => ['value' => '2']],
  4976. 'Helios',
  4977. '/option',
  4978. '/select',
  4979. ['div' => ['class' => 'error-message', 'id' => 'spacecraft-error']],
  4980. 'Invalid',
  4981. '/div',
  4982. '/div',
  4983. ];
  4984. $this->assertHtml($expected, $result);
  4985. }
  4986. /**
  4987. * testSelectMultipleCheckboxes method
  4988. *
  4989. * Test generation of multi select elements in checkbox format.
  4990. */
  4991. public function testSelectMultipleCheckboxes(): void
  4992. {
  4993. $result = $this->Form->select(
  4994. 'Model.multi_field',
  4995. ['first', 'second', 'third'],
  4996. ['multiple' => 'checkbox']
  4997. );
  4998. $expected = [
  4999. 'input' => [
  5000. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  5001. ],
  5002. ['div' => ['class' => 'checkbox']],
  5003. ['label' => ['for' => 'model-multi-field-0']],
  5004. ['input' => [
  5005. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5006. 'value' => '0', 'id' => 'model-multi-field-0',
  5007. ]],
  5008. 'first',
  5009. '/label',
  5010. '/div',
  5011. ['div' => ['class' => 'checkbox']],
  5012. ['label' => ['for' => 'model-multi-field-1']],
  5013. ['input' => [
  5014. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5015. 'value' => '1', 'id' => 'model-multi-field-1',
  5016. ]],
  5017. 'second',
  5018. '/label',
  5019. '/div',
  5020. ['div' => ['class' => 'checkbox']],
  5021. ['label' => ['for' => 'model-multi-field-2']],
  5022. ['input' => [
  5023. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5024. 'value' => '2', 'id' => 'model-multi-field-2',
  5025. ]],
  5026. 'third',
  5027. '/label',
  5028. '/div',
  5029. ];
  5030. $this->assertHtml($expected, $result);
  5031. $result = $this->Form->select(
  5032. 'Model.multi_field',
  5033. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5034. ['multiple' => 'checkbox']
  5035. );
  5036. $expected = [
  5037. 'input' => [
  5038. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  5039. ],
  5040. ['div' => ['class' => 'checkbox']],
  5041. ['label' => ['for' => 'model-multi-field-a+']],
  5042. ['input' => [
  5043. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5044. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  5045. ]],
  5046. 'first',
  5047. '/label',
  5048. '/div',
  5049. ['div' => ['class' => 'checkbox']],
  5050. ['label' => ['for' => 'model-multi-field-a++']],
  5051. ['input' => [
  5052. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5053. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  5054. ]],
  5055. 'second',
  5056. '/label',
  5057. '/div',
  5058. ['div' => ['class' => 'checkbox']],
  5059. ['label' => ['for' => 'model-multi-field-a+++']],
  5060. ['input' => [
  5061. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5062. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  5063. ]],
  5064. 'third',
  5065. '/label',
  5066. '/div',
  5067. ];
  5068. $this->assertHtml($expected, $result);
  5069. $result = $this->Form->select(
  5070. 'Model.multi_field',
  5071. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5072. ['multiple' => 'checkbox']
  5073. );
  5074. $expected = [
  5075. 'input' => [
  5076. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  5077. ],
  5078. ['div' => ['class' => 'checkbox']],
  5079. ['label' => ['for' => 'model-multi-field-a-b']],
  5080. ['input' => [
  5081. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5082. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  5083. ]],
  5084. 'first',
  5085. '/label',
  5086. '/div',
  5087. ['div' => ['class' => 'checkbox']],
  5088. ['label' => ['for' => 'model-multi-field-a-b1']],
  5089. ['input' => [
  5090. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5091. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  5092. ]],
  5093. 'second',
  5094. '/label',
  5095. '/div',
  5096. ['div' => ['class' => 'checkbox']],
  5097. ['label' => ['for' => 'model-multi-field-a-b2']],
  5098. ['input' => [
  5099. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5100. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  5101. ]],
  5102. 'third',
  5103. '/label',
  5104. '/div',
  5105. ];
  5106. $this->assertHtml($expected, $result);
  5107. }
  5108. /**
  5109. * testSelectMultipleCheckboxRequestData method
  5110. *
  5111. * Ensure that multiCheckbox reads from the request data.
  5112. */
  5113. public function testSelectMultipleCheckboxRequestData(): void
  5114. {
  5115. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5116. $result = $this->Form->select(
  5117. 'Model.tags',
  5118. ['1' => 'first', 'Array' => 'Array'],
  5119. ['multiple' => 'checkbox']
  5120. );
  5121. $expected = [
  5122. 'input' => [
  5123. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'model-tags',
  5124. ],
  5125. ['div' => ['class' => 'checkbox']],
  5126. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5127. ['input' => [
  5128. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5129. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5130. ]],
  5131. 'first',
  5132. '/label',
  5133. '/div',
  5134. ['div' => ['class' => 'checkbox']],
  5135. ['label' => ['for' => 'model-tags-array']],
  5136. ['input' => [
  5137. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5138. 'value' => 'Array', 'id' => 'model-tags-array',
  5139. ]],
  5140. 'Array',
  5141. '/label',
  5142. '/div',
  5143. ];
  5144. $this->assertHtml($expected, $result);
  5145. }
  5146. /**
  5147. * testSelectMultipleCheckboxSecurity method
  5148. *
  5149. * Checks the security hash array generated for multiple-input checkbox elements.
  5150. */
  5151. public function testSelectMultipleCheckboxSecurity(): void
  5152. {
  5153. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5154. $this->Form->create();
  5155. $this->Form->select(
  5156. 'Model.multi_field',
  5157. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5158. ['multiple' => 'checkbox']
  5159. );
  5160. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5161. $this->assertEquals(['Model.multi_field'], $fields);
  5162. $result = $this->Form->secure();
  5163. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  5164. $hash = urlencode($hash . ':');
  5165. $this->assertStringContainsString('"' . $hash . '"', $result);
  5166. }
  5167. /**
  5168. * testSelectMultipleSecureWithNoOptions method
  5169. *
  5170. * Multiple select elements should always be secured as they always participate
  5171. * in the POST data.
  5172. */
  5173. public function testSelectMultipleSecureWithNoOptions(): void
  5174. {
  5175. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5176. $this->Form->create();
  5177. $this->Form->select(
  5178. 'Model.select',
  5179. [],
  5180. ['multiple' => true]
  5181. );
  5182. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5183. $this->assertEquals(['Model.select'], $result);
  5184. }
  5185. /**
  5186. * testSelectNoSecureWithNoOptions method
  5187. *
  5188. * When a select box has no options it should not be added to the fields list
  5189. * as it always fail post validation.
  5190. */
  5191. public function testSelectNoSecureWithNoOptions(): void
  5192. {
  5193. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5194. $this->Form->create();
  5195. $this->Form->select(
  5196. 'Model.select',
  5197. []
  5198. );
  5199. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5200. $this->assertEquals([], $result);
  5201. $this->Form->select(
  5202. 'Model.user_id',
  5203. [],
  5204. ['empty' => true]
  5205. );
  5206. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5207. $this->assertEquals(['Model.user_id'], $result);
  5208. }
  5209. /**
  5210. * testControlMultipleCheckboxes method
  5211. *
  5212. * Test control() resulting in multi select elements being generated.
  5213. */
  5214. public function testControlMultipleCheckboxes(): void
  5215. {
  5216. $result = $this->Form->control('Model.multi_field', [
  5217. 'options' => ['first', 'second', 'third'],
  5218. 'multiple' => 'checkbox',
  5219. ]);
  5220. $expected = [
  5221. ['div' => ['class' => 'input select']],
  5222. ['label' => ['for' => 'model-multi-field']],
  5223. 'Multi Field',
  5224. '/label',
  5225. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5226. ['div' => ['class' => 'checkbox']],
  5227. ['label' => ['for' => 'model-multi-field-0']],
  5228. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5229. 'first',
  5230. '/label',
  5231. '/div',
  5232. ['div' => ['class' => 'checkbox']],
  5233. ['label' => ['for' => 'model-multi-field-1']],
  5234. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5235. 'second',
  5236. '/label',
  5237. '/div',
  5238. ['div' => ['class' => 'checkbox']],
  5239. ['label' => ['for' => 'model-multi-field-2']],
  5240. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5241. 'third',
  5242. '/label',
  5243. '/div',
  5244. '/div',
  5245. ];
  5246. $this->assertHtml($expected, $result);
  5247. $result = $this->Form->control('Model.multi_field', [
  5248. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5249. 'multiple' => 'checkbox',
  5250. ]);
  5251. $expected = [
  5252. ['div' => ['class' => 'input select']],
  5253. ['label' => ['for' => 'model-multi-field']],
  5254. 'Multi Field',
  5255. '/label',
  5256. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5257. ['div' => ['class' => 'checkbox']],
  5258. ['label' => ['for' => 'model-multi-field-a']],
  5259. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5260. 'first',
  5261. '/label',
  5262. '/div',
  5263. ['div' => ['class' => 'checkbox']],
  5264. ['label' => ['for' => 'model-multi-field-b']],
  5265. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5266. 'second',
  5267. '/label',
  5268. '/div',
  5269. ['div' => ['class' => 'checkbox']],
  5270. ['label' => ['for' => 'model-multi-field-c']],
  5271. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5272. 'third',
  5273. '/label',
  5274. '/div',
  5275. '/div',
  5276. ];
  5277. $this->assertHtml($expected, $result);
  5278. }
  5279. /**
  5280. * testSelectHiddenFieldOmission method
  5281. *
  5282. * Test that select() with 'hiddenField' => false omits the hidden field.
  5283. */
  5284. public function testSelectHiddenFieldOmission(): void
  5285. {
  5286. $result = $this->Form->select(
  5287. 'Model.multi_field',
  5288. ['first', 'second'],
  5289. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5290. );
  5291. $this->assertStringNotContainsString('type="hidden"', $result);
  5292. }
  5293. /**
  5294. * testSelectCheckboxMultipleOverrideName method
  5295. *
  5296. * Test that select() with multiple = checkbox works with overriding name attribute.
  5297. */
  5298. public function testSelectCheckboxMultipleOverrideName(): void
  5299. {
  5300. $result = $this->Form->select('category', ['1', '2'], [
  5301. 'multiple' => 'checkbox',
  5302. 'name' => 'fish',
  5303. ]);
  5304. $expected = [
  5305. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  5306. ['div' => ['class' => 'checkbox']],
  5307. ['label' => ['for' => 'fish-0']],
  5308. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5309. '1',
  5310. '/label',
  5311. '/div',
  5312. ['div' => ['class' => 'checkbox']],
  5313. ['label' => ['for' => 'fish-1']],
  5314. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5315. '2',
  5316. '/label',
  5317. '/div',
  5318. ];
  5319. $this->assertHtml($expected, $result);
  5320. $result = $this->Form->multiCheckbox(
  5321. 'category',
  5322. new Collection(['1', '2']),
  5323. ['name' => 'fish']
  5324. );
  5325. $this->assertHtml($expected, $result);
  5326. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5327. 'name' => 'fish',
  5328. ]);
  5329. $this->assertHtml($expected, $result);
  5330. }
  5331. /**
  5332. * testSelectCheckboxMultipleOverrideName method
  5333. *
  5334. * Test that select() with multiple = checkbox works with overriding name attribute.
  5335. */
  5336. public function testSelectCheckboxMultipleCustomId(): void
  5337. {
  5338. $result = $this->Form->select('category', ['1', '2'], [
  5339. 'multiple' => 'checkbox',
  5340. 'id' => 'cat',
  5341. ]);
  5342. $expected = [
  5343. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'cat'],
  5344. ['div' => ['class' => 'checkbox']],
  5345. ['label' => ['for' => 'cat-0']],
  5346. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'cat-0']],
  5347. '1',
  5348. '/label',
  5349. '/div',
  5350. ['div' => ['class' => 'checkbox']],
  5351. ['label' => ['for' => 'cat-1']],
  5352. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'cat-1']],
  5353. '2',
  5354. '/label',
  5355. '/div',
  5356. ];
  5357. $this->assertHtml($expected, $result);
  5358. $result = $this->Form->multiCheckbox(
  5359. 'category',
  5360. ['1', '2'],
  5361. ['id' => 'cat']
  5362. );
  5363. $this->assertHtml($expected, $result);
  5364. }
  5365. /**
  5366. * testControlMultiCheckbox method
  5367. *
  5368. * Test that control() works with multicheckbox.
  5369. */
  5370. public function testControlMultiCheckbox(): void
  5371. {
  5372. $result = $this->Form->control('category', [
  5373. 'type' => 'multicheckbox',
  5374. 'options' => ['1', '2'],
  5375. ]);
  5376. $expected = [
  5377. ['div' => ['class' => 'input multicheckbox']],
  5378. '<label',
  5379. 'Category',
  5380. '/label',
  5381. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'category'],
  5382. ['div' => ['class' => 'checkbox']],
  5383. ['label' => ['for' => 'category-0']],
  5384. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5385. '1',
  5386. '/label',
  5387. '/div',
  5388. ['div' => ['class' => 'checkbox']],
  5389. ['label' => ['for' => 'category-1']],
  5390. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5391. '2',
  5392. '/label',
  5393. '/div',
  5394. '/div',
  5395. ];
  5396. $this->assertHtml($expected, $result);
  5397. }
  5398. /**
  5399. * testCheckbox method
  5400. *
  5401. * Test generation of checkboxes.
  5402. */
  5403. public function testCheckbox(): void
  5404. {
  5405. $result = $this->Form->checkbox('Model.field');
  5406. $expected = [
  5407. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5408. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5409. ];
  5410. $this->assertHtml($expected, $result);
  5411. $result = $this->Form->checkbox('Model.field', [
  5412. 'id' => 'theID',
  5413. 'value' => 'myvalue',
  5414. 'form' => 'my-form',
  5415. ]);
  5416. $expected = [
  5417. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5418. ['input' => [
  5419. 'type' => 'checkbox', 'name' => 'Model[field]',
  5420. 'value' => 'myvalue', 'id' => 'theID',
  5421. 'form' => 'my-form',
  5422. ]],
  5423. ];
  5424. $this->assertHtml($expected, $result);
  5425. }
  5426. /**
  5427. * testCheckboxDefaultValue method
  5428. *
  5429. * Test default value setting on checkbox() method.
  5430. */
  5431. public function testCheckboxDefaultValue(): void
  5432. {
  5433. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5434. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5435. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5436. $this->assertHtml($expected, $result);
  5437. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5438. $this->Form->create();
  5439. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5440. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5441. $this->assertHtml($expected, $result);
  5442. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5443. $this->Form->create();
  5444. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5445. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5446. $this->assertHtml($expected, $result);
  5447. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5448. $this->Form->create();
  5449. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5450. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5451. $this->assertHtml($expected, $result);
  5452. $Articles = $this->getTableLocator()->get('Articles');
  5453. $Articles->getSchema()->addColumn(
  5454. 'published',
  5455. ['type' => 'boolean', 'null' => false, 'default' => true]
  5456. );
  5457. $this->Form->create($Articles->newEmptyEntity());
  5458. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5459. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5460. $this->assertHtml($expected, $result);
  5461. }
  5462. /**
  5463. * testCheckboxCheckedAndError method
  5464. *
  5465. * Test checkbox being checked or having errors.
  5466. */
  5467. public function testCheckboxCheckedAndError(): void
  5468. {
  5469. $this->article['errors'] = [
  5470. 'published' => true,
  5471. ];
  5472. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5473. $this->Form->create($this->article);
  5474. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5475. $expected = [
  5476. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5477. ['input' => [
  5478. 'type' => 'checkbox',
  5479. 'name' => 'published',
  5480. 'value' => 'myvalue',
  5481. 'id' => 'theID',
  5482. 'checked' => 'checked',
  5483. 'class' => 'form-error',
  5484. ]],
  5485. ];
  5486. $this->assertHtml($expected, $result);
  5487. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5488. $this->Form->create($this->article);
  5489. $result = $this->Form->checkbox('published');
  5490. $expected = [
  5491. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5492. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5493. ];
  5494. $this->assertHtml($expected, $result);
  5495. }
  5496. /**
  5497. * testCheckboxCustomNameAttribute method
  5498. *
  5499. * Test checkbox() with a custom name attribute.
  5500. */
  5501. public function testCheckboxCustomNameAttribute(): void
  5502. {
  5503. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5504. $expected = [
  5505. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5506. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5507. ];
  5508. $this->assertHtml($expected, $result);
  5509. }
  5510. /**
  5511. * testCheckboxHiddenField method
  5512. *
  5513. * Test that the hidden input for checkboxes can be omitted or set to a
  5514. * specific value.
  5515. */
  5516. public function testCheckboxHiddenField(): void
  5517. {
  5518. $result = $this->Form->checkbox('UserForm.something', [
  5519. 'hiddenField' => false,
  5520. ]);
  5521. $expected = [
  5522. 'input' => [
  5523. 'type' => 'checkbox',
  5524. 'name' => 'UserForm[something]',
  5525. 'value' => '1',
  5526. ],
  5527. ];
  5528. $this->assertHtml($expected, $result);
  5529. $result = $this->Form->checkbox('UserForm.something', [
  5530. 'value' => 'Y',
  5531. 'hiddenField' => '',
  5532. ]);
  5533. $expected = [
  5534. ['input' => [
  5535. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5536. 'value' => '',
  5537. ]],
  5538. ['input' => [
  5539. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5540. 'value' => 'Y',
  5541. ]],
  5542. ];
  5543. $this->assertHtml($expected, $result);
  5544. $result = $this->Form->checkbox('UserForm.something', [
  5545. 'value' => 'Y',
  5546. 'hiddenField' => 'N',
  5547. ]);
  5548. $expected = [
  5549. ['input' => [
  5550. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5551. 'value' => 'N',
  5552. ]],
  5553. ['input' => [
  5554. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5555. 'value' => 'Y',
  5556. ]],
  5557. ];
  5558. $this->assertHtml($expected, $result);
  5559. }
  5560. /**
  5561. * testTime method
  5562. *
  5563. * Test the time type.
  5564. */
  5565. public function testTime(): void
  5566. {
  5567. $result = $this->Form->time('start_time', [
  5568. 'value' => '2014-03-08 16:30:00',
  5569. ]);
  5570. $expected = [
  5571. 'input' => [
  5572. 'type' => 'time',
  5573. 'name' => 'start_time',
  5574. 'value' => '16:30:00',
  5575. 'step' => '1',
  5576. ],
  5577. ];
  5578. $this->assertHtml($expected, $result);
  5579. $result = $this->Form->time('start_time', [
  5580. 'value' => new ChronosTime('16:30:00'),
  5581. ]);
  5582. $expected = [
  5583. 'input' => [
  5584. 'type' => 'time',
  5585. 'name' => 'start_time',
  5586. 'value' => '16:30:00',
  5587. 'step' => '1',
  5588. ],
  5589. ];
  5590. $this->assertHtml($expected, $result);
  5591. }
  5592. /**
  5593. * testDate method
  5594. *
  5595. * Test the date type.
  5596. */
  5597. public function testDate(): void
  5598. {
  5599. $result = $this->Form->date('start_day', [
  5600. 'value' => '2014-03-08',
  5601. ]);
  5602. $expected = [
  5603. 'input' => [
  5604. 'type' => 'date',
  5605. 'name' => 'start_day',
  5606. 'value' => '2014-03-08',
  5607. ],
  5608. ];
  5609. $this->assertHtml($expected, $result);
  5610. $result = $this->Form->date('start_day', [
  5611. 'value' => new Date('2014-03-08'),
  5612. ]);
  5613. $this->assertHtml($expected, $result);
  5614. }
  5615. /**
  5616. * testDateTime method
  5617. */
  5618. public function testDateTime(): void
  5619. {
  5620. $result = $this->Form->dateTime('date', ['default' => true]);
  5621. $expected = [
  5622. 'input' => [
  5623. 'type' => 'datetime-local',
  5624. 'name' => 'date',
  5625. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  5626. 'step' => '1',
  5627. ],
  5628. ];
  5629. $this->assertHtml($expected, $result);
  5630. }
  5631. /**
  5632. * testDateTimeSecured method
  5633. *
  5634. * Test that datetime fields are added to protected fields list.
  5635. */
  5636. public function testDateTimeSecured(): void
  5637. {
  5638. $this->View->setRequest(
  5639. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5640. );
  5641. $this->Form->create();
  5642. $this->Form->dateTime('date');
  5643. $expected = ['date'];
  5644. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5645. $this->assertEquals($expected, $result);
  5646. $this->Form->date('published');
  5647. $expected = ['date', 'published'];
  5648. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5649. $this->assertEquals($expected, $result);
  5650. }
  5651. /**
  5652. * testDateTimeSecuredDisabled method
  5653. *
  5654. * Test that datetime fields are added to protected fields list.
  5655. */
  5656. public function testDateTimeSecuredDisabled(): void
  5657. {
  5658. $this->View->setRequest(
  5659. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5660. );
  5661. $this->Form->create();
  5662. $this->Form->dateTime('date', ['secure' => false]);
  5663. $expected = [];
  5664. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5665. $this->assertEquals($expected, $result);
  5666. $this->Form->date('published', ['secure' => false]);
  5667. $expected = [];
  5668. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5669. $this->assertEquals($expected, $result);
  5670. }
  5671. /**
  5672. * testDatetimeWithDefault method
  5673. *
  5674. * Test that datetime() and default values work.
  5675. */
  5676. public function testDatetimeWithDefault(): void
  5677. {
  5678. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5679. $expected = [
  5680. 'input' => [
  5681. 'type' => 'datetime-local',
  5682. 'name' => 'updated',
  5683. 'value' => '2009-06-01T11:15:30',
  5684. 'step' => '1',
  5685. ],
  5686. ];
  5687. $this->assertHtml($expected, $result);
  5688. $result = $this->Form->dateTime('updated', [
  5689. 'default' => '2009-06-01 11:15:30',
  5690. ]);
  5691. $expected = [
  5692. 'input' => [
  5693. 'type' => 'datetime-local',
  5694. 'name' => 'updated',
  5695. 'value' => '2009-06-01T11:15:30',
  5696. 'step' => '1',
  5697. ],
  5698. ];
  5699. $this->assertHtml($expected, $result);
  5700. }
  5701. /**
  5702. * testMonth method
  5703. *
  5704. * Test generation of a month input.
  5705. */
  5706. public function testMonth(): void
  5707. {
  5708. $result = $this->Form->month('field', ['value' => '']);
  5709. $expected = [
  5710. 'input' => [
  5711. 'type' => 'month',
  5712. 'name' => 'field',
  5713. 'value' => '',
  5714. ],
  5715. ];
  5716. $this->assertHtml($expected, $result);
  5717. $this->View->setRequest(
  5718. $this->View->getRequest()->withData('release', '2050-02-10')
  5719. );
  5720. $this->Form->create();
  5721. $result = $this->Form->month('release');
  5722. $expected = [
  5723. 'input' => [
  5724. 'type' => 'month',
  5725. 'name' => 'release',
  5726. 'value' => '2050-02',
  5727. ],
  5728. ];
  5729. $this->assertHtml($expected, $result);
  5730. $this->View->setRequest(
  5731. $this->View->getRequest()->withData('release', '2050-03')
  5732. );
  5733. $this->Form->create();
  5734. $result = $this->Form->month('release');
  5735. $expected = [
  5736. 'input' => [
  5737. 'type' => 'month',
  5738. 'name' => 'release',
  5739. 'value' => '2050-03',
  5740. ],
  5741. ];
  5742. $this->assertHtml($expected, $result);
  5743. }
  5744. /**
  5745. * testYear method
  5746. *
  5747. * Test generation of a year input.
  5748. */
  5749. public function testYear(): void
  5750. {
  5751. $this->View->setRequest(
  5752. $this->View->getRequest()->withData('published', '2006')
  5753. );
  5754. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5755. $expected = [
  5756. ['select' => ['name' => 'field']],
  5757. ['option' => ['selected' => 'selected', 'value' => '']],
  5758. '/option',
  5759. ['option' => ['value' => '2007']],
  5760. '2007',
  5761. '/option',
  5762. ['option' => ['value' => '2006']],
  5763. '2006',
  5764. '/option',
  5765. '/select',
  5766. ];
  5767. $this->assertHtml($expected, $result);
  5768. $result = $this->Form->year('field', [
  5769. 'value' => '',
  5770. 'min' => 2006,
  5771. 'max' => 2007,
  5772. 'order' => 'asc',
  5773. ]);
  5774. $expected = [
  5775. ['select' => ['name' => 'field']],
  5776. ['option' => ['selected' => 'selected', 'value' => '']],
  5777. '/option',
  5778. ['option' => ['value' => '2006']],
  5779. '2006',
  5780. '/option',
  5781. ['option' => ['value' => '2007']],
  5782. '2007',
  5783. '/option',
  5784. '/select',
  5785. ];
  5786. $this->assertHtml($expected, $result);
  5787. $result = $this->Form->year('published', [
  5788. 'empty' => false,
  5789. 'min' => 2006,
  5790. 'max' => 2007,
  5791. ]);
  5792. $expected = [
  5793. ['select' => ['name' => 'published']],
  5794. ['option' => ['value' => '2007']],
  5795. '2007',
  5796. '/option',
  5797. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5798. '2006',
  5799. '/option',
  5800. '/select',
  5801. ];
  5802. $this->assertHtml($expected, $result);
  5803. $result = $this->Form->year('published', [
  5804. 'empty' => false,
  5805. 'value' => new Date('2008-01-12'),
  5806. 'min' => 2007,
  5807. 'max' => 2009,
  5808. ]);
  5809. $expected = [
  5810. ['select' => ['name' => 'published']],
  5811. ['option' => ['value' => '2009']],
  5812. '2009',
  5813. '/option',
  5814. ['option' => ['value' => '2008', 'selected' => 'selected']],
  5815. '2008',
  5816. '/option',
  5817. ['option' => ['value' => '2007']],
  5818. '2007',
  5819. '/option',
  5820. '/select',
  5821. ];
  5822. $this->assertHtml($expected, $result);
  5823. $result = $this->Form->year('published', [
  5824. 'empty' => 'Published on',
  5825. ]);
  5826. $this->assertStringContainsString('Published on', $result);
  5827. }
  5828. /**
  5829. * testControlYearPreEpoch method
  5830. *
  5831. * Test minYear being prior to the unix epoch.
  5832. */
  5833. public function testControlYearPreEpoch(): void
  5834. {
  5835. $start = date('Y') - 80;
  5836. $end = date('Y') - 18;
  5837. $result = $this->Form->control('birth_year', [
  5838. 'type' => 'year',
  5839. 'label' => 'Birth Year',
  5840. 'min' => $start,
  5841. 'max' => $end,
  5842. ]);
  5843. $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
  5844. $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
  5845. $this->assertStringNotContainsString('value="00">00', $result);
  5846. }
  5847. /**
  5848. * test control() datetime & required attributes
  5849. */
  5850. public function testControlDatetimeRequired(): void
  5851. {
  5852. $result = $this->Form->control('birthday', [
  5853. 'type' => 'date',
  5854. 'required' => true,
  5855. ]);
  5856. $this->assertStringContainsString(
  5857. '<input type="date" name="birthday" required="required"',
  5858. $result
  5859. );
  5860. }
  5861. /**
  5862. * testYearAutoExpandRange method
  5863. */
  5864. public function testYearAutoExpandRange(): void
  5865. {
  5866. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5867. $result = $this->Form->year('birthday');
  5868. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5869. $result = $matches[1];
  5870. $expected = range(date('Y') + 5, 1930);
  5871. $this->assertEquals($expected, $result);
  5872. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5873. $this->Form->create();
  5874. $result = $this->Form->year('release');
  5875. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5876. $result = $matches[1];
  5877. $expected = range(2050, date('Y') - 5);
  5878. $this->assertEquals($expected, $result);
  5879. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5880. $this->Form->create();
  5881. $result = $this->Form->year('release', [
  5882. 'min' => 1890,
  5883. 'max' => 1900,
  5884. ]);
  5885. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5886. $result = $matches[1];
  5887. $expected = range(1900, 1881);
  5888. $this->assertEquals($expected, $result);
  5889. }
  5890. /**
  5891. * test control placeholder + label
  5892. */
  5893. public function testControlLabelAndPlaceholder(): void
  5894. {
  5895. $this->Form->create($this->article);
  5896. $result = $this->Form->control('title', ['label' => 'Title', 'placeholder' => 'Add title']);
  5897. $expected = [
  5898. 'div' => ['class' => 'input text required'],
  5899. 'label' => ['for' => 'title'],
  5900. 'Title',
  5901. '/label',
  5902. 'input' => [
  5903. 'type' => 'text',
  5904. 'required' => 'required',
  5905. 'placeholder' => 'Add title',
  5906. 'id' => 'title',
  5907. 'name' => 'title',
  5908. 'data-validity-message' => 'This field cannot be left empty',
  5909. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5910. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5911. ],
  5912. '/div',
  5913. ];
  5914. $this->assertHtml($expected, $result);
  5915. }
  5916. /**
  5917. * testControlLabelFalse method
  5918. *
  5919. * Test the label option being set to false.
  5920. */
  5921. public function testControlLabelFalse(): void
  5922. {
  5923. $this->Form->create($this->article);
  5924. $result = $this->Form->control('title', ['label' => false]);
  5925. $expected = [
  5926. 'div' => ['class' => 'input text required'],
  5927. 'input' => [
  5928. 'type' => 'text',
  5929. 'required' => 'required',
  5930. 'id' => 'title',
  5931. 'name' => 'title',
  5932. 'data-validity-message' => 'This field cannot be left empty',
  5933. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5934. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5935. ],
  5936. '/div',
  5937. ];
  5938. $this->assertHtml($expected, $result);
  5939. $this->Form->create($this->article);
  5940. $result = $this->Form->control('title', ['label' => false, 'placeholder' => 'Add title']);
  5941. $expected['input'] += [
  5942. 'placeholder' => 'Add title',
  5943. 'aria-label' => 'Add title',
  5944. ];
  5945. $this->assertHtml($expected, $result);
  5946. }
  5947. /**
  5948. * testTextArea method
  5949. *
  5950. * Test generation of a textarea input.
  5951. */
  5952. public function testTextArea(): void
  5953. {
  5954. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5955. $result = $this->Form->textarea('field');
  5956. $expected = [
  5957. 'textarea' => ['name' => 'field', 'rows' => 5],
  5958. 'some test data',
  5959. '/textarea',
  5960. ];
  5961. $this->assertHtml($expected, $result);
  5962. $result = $this->Form->textarea('user.bio');
  5963. $expected = [
  5964. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5965. '/textarea',
  5966. ];
  5967. $this->assertHtml($expected, $result);
  5968. $this->View->setRequest($this->View->getRequest()
  5969. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5970. $this->Form->create();
  5971. $result = $this->Form->textarea('field');
  5972. $expected = [
  5973. 'textarea' => ['name' => 'field', 'rows' => 5],
  5974. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5975. '/textarea',
  5976. ];
  5977. $this->assertHtml($expected, $result);
  5978. $this->View->setRequest($this->View->getRequest()->withData(
  5979. 'Model.field',
  5980. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  5981. ));
  5982. $this->Form->create();
  5983. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5984. $expected = [
  5985. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  5986. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5987. '/textarea',
  5988. ];
  5989. $this->assertHtml($expected, $result);
  5990. $result = $this->Form->textarea('0.OtherModel.field');
  5991. $expected = [
  5992. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  5993. '/textarea',
  5994. ];
  5995. $this->assertHtml($expected, $result);
  5996. }
  5997. /**
  5998. * testTextAreaWithStupidCharacters method
  5999. *
  6000. * Test text area with non-ascii characters.
  6001. */
  6002. public function testTextAreaWithStupidCharacters(): void
  6003. {
  6004. $result = $this->Form->textarea('Post.content', [
  6005. 'value' => 'GREAT®',
  6006. 'rows' => '15',
  6007. 'cols' => '75',
  6008. ]);
  6009. $expected = [
  6010. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6011. 'GREAT®',
  6012. '/textarea',
  6013. ];
  6014. $this->assertHtml($expected, $result);
  6015. }
  6016. /**
  6017. * testTextAreaMaxLength method
  6018. *
  6019. * Test textareas maxlength read from schema.
  6020. */
  6021. public function testTextAreaMaxLength(): void
  6022. {
  6023. $this->Form->create([
  6024. 'schema' => [
  6025. 'stuff' => ['type' => 'string', 'length' => 10],
  6026. ],
  6027. ]);
  6028. $result = $this->Form->control('other', ['type' => 'textarea']);
  6029. $expected = [
  6030. 'div' => ['class' => 'input textarea'],
  6031. 'label' => ['for' => 'other'],
  6032. 'Other',
  6033. '/label',
  6034. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6035. '/textarea',
  6036. '/div',
  6037. ];
  6038. $this->assertHtml($expected, $result);
  6039. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6040. $expected = [
  6041. 'div' => ['class' => 'input textarea'],
  6042. 'label' => ['for' => 'stuff'],
  6043. 'Stuff',
  6044. '/label',
  6045. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6046. '/textarea',
  6047. '/div',
  6048. ];
  6049. $this->assertHtml($expected, $result);
  6050. }
  6051. /**
  6052. * testHiddenField method
  6053. *
  6054. * Test generation of a hidden input.
  6055. */
  6056. public function testHidden(): void
  6057. {
  6058. $this->article['errors'] = [
  6059. 'field' => true,
  6060. ];
  6061. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  6062. $this->Form->create($this->article);
  6063. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6064. $expected = [
  6065. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6066. $this->assertHtml($expected, $result);
  6067. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6068. $expected = [
  6069. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  6070. ];
  6071. $this->assertHtml($expected, $result);
  6072. $article = new Article([
  6073. 'status' => ArticleStatus::UNPUBLISHED,
  6074. ]);
  6075. $this->Form->create($article);
  6076. $result = $this->Form->hidden('status');
  6077. $expected = [
  6078. 'input' => ['type' => 'hidden', 'name' => 'status', 'value' => 'N'],
  6079. ];
  6080. $this->assertHtml($expected, $result);
  6081. }
  6082. /**
  6083. * Test hidden() with various boolean values.
  6084. */
  6085. public function testHiddenBooleanValues(): void
  6086. {
  6087. $this->Form->create($this->article);
  6088. $result = $this->Form->hidden('field', ['value' => null]);
  6089. $expected = [
  6090. 'input' => ['type' => 'hidden', 'name' => 'field'],
  6091. ];
  6092. $this->assertHtml($expected, $result);
  6093. $result = $this->Form->hidden('field', ['value' => true]);
  6094. $expected = [
  6095. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '1'],
  6096. ];
  6097. $this->assertHtml($expected, $result);
  6098. $result = $this->Form->hidden('field', ['value' => false]);
  6099. $expected = [
  6100. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '0'],
  6101. ];
  6102. $this->assertHtml($expected, $result);
  6103. }
  6104. /**
  6105. * testFileUploadField method
  6106. *
  6107. * Test generation of a file upload input.
  6108. */
  6109. public function testFileUploadField(): void
  6110. {
  6111. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6112. $result = $this->Form->file('Model.upload');
  6113. $this->assertHtml($expected, $result);
  6114. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  6115. 'name' => '', 'type' => '', 'tmp_name' => '',
  6116. 'error' => 4, 'size' => 0,
  6117. ]));
  6118. $result = $this->Form->file('Model.upload');
  6119. $this->assertHtml($expected, $result);
  6120. $this->View->setRequest(
  6121. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6122. );
  6123. $result = $this->Form->file('Model.upload');
  6124. $this->assertHtml($expected, $result);
  6125. }
  6126. /**
  6127. * testFileUploadOnOtherModel method
  6128. *
  6129. * Test File upload input on a model not used in create().
  6130. */
  6131. public function testFileUploadOnOtherModel(): void
  6132. {
  6133. $this->Form->create($this->article, ['type' => 'file']);
  6134. $result = $this->Form->file('ValidateProfile.city');
  6135. $expected = [
  6136. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  6137. ];
  6138. $this->assertHtml($expected, $result);
  6139. }
  6140. /**
  6141. * testButton method
  6142. *
  6143. * Test generation of a form button.
  6144. */
  6145. public function testButton(): void
  6146. {
  6147. $result = $this->Form->button('Hi');
  6148. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6149. $this->assertHtml($expected, $result);
  6150. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'escapeTitle' => false]);
  6151. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6152. $this->assertHtml($expected, $result);
  6153. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm', 'escapeTitle' => false]);
  6154. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6155. $this->assertHtml($expected, $result);
  6156. $result = $this->Form->button('<Clear Form>', ['type' => 'reset']);
  6157. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6158. $this->assertHtml($expected, $result);
  6159. $result = $this->Form->button('No type', ['type' => false]);
  6160. $expected = ['button' => [], 'No type', '/button'];
  6161. $this->assertHtml($expected, $result);
  6162. $result = $this->Form->button('Upload Text', [
  6163. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6164. 'escape' => false,
  6165. ]);
  6166. $this->assertDoesNotMatchRegularExpression('/\&039/', $result);
  6167. }
  6168. /**
  6169. * testButtonUnlockedByDefault method
  6170. *
  6171. * Test that button() makes unlocked fields by default.
  6172. */
  6173. public function testButtonUnlockedByDefault(): void
  6174. {
  6175. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6176. $this->Form->create();
  6177. $this->Form->button('Save', ['name' => 'save']);
  6178. $this->Form->button('Clear');
  6179. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6180. $this->assertEquals(['save'], $result);
  6181. }
  6182. /**
  6183. * Test generation of a form button with confirm message.
  6184. */
  6185. public function testButtonWithConfirm(): void
  6186. {
  6187. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6188. $expected = ['button' => [
  6189. 'type' => 'submit',
  6190. 'data-confirm-message' => 'Confirm me!',
  6191. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6192. ], 'Hi', '/button'];
  6193. $this->assertHtml($expected, $result);
  6194. $result = $this->Form->button('Hi', ['escape' => false, 'confirm' => 'Confirm "me"!']);
  6195. $expected = ['button' => [
  6196. 'type' => 'submit',
  6197. 'data-confirm-message' => 'Confirm "me"!',
  6198. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6199. ], 'Hi', '/button'];
  6200. $this->assertHtml($expected, $result);
  6201. }
  6202. /**
  6203. * testPostButton method
  6204. */
  6205. public function testPostButton(): void
  6206. {
  6207. $result = $this->Form->postButton('Hi', '/controller/action');
  6208. $expected = [
  6209. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6210. 'button' => ['type' => 'submit'],
  6211. 'Hi',
  6212. '/button',
  6213. '/form',
  6214. ];
  6215. $this->assertHtml($expected, $result);
  6216. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6217. $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
  6218. }
  6219. /**
  6220. * testPostButtonMethodType method
  6221. */
  6222. public function testPostButtonMethodType(): void
  6223. {
  6224. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6225. $expected = [
  6226. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6227. 'div' => ['style' => 'display:none;'],
  6228. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6229. '/div',
  6230. 'button' => ['type' => 'submit'],
  6231. 'Hi',
  6232. '/button',
  6233. '/form',
  6234. ];
  6235. $this->assertHtml($expected, $result);
  6236. }
  6237. /**
  6238. * testPostButtonFormOptions method
  6239. */
  6240. public function testPostButtonFormOptions(): void
  6241. {
  6242. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6243. $expected = [
  6244. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6245. 'button' => ['type' => 'submit'],
  6246. 'Hi',
  6247. '/button',
  6248. '/form',
  6249. ];
  6250. $this->assertHtml($expected, $result);
  6251. }
  6252. /**
  6253. * testPostButtonNestedData method
  6254. *
  6255. * Test using postButton with N dimensional data.
  6256. */
  6257. public function testPostButtonNestedData(): void
  6258. {
  6259. $data = [
  6260. 'one' => [
  6261. 'two' => [
  6262. 3, 4, 5,
  6263. ],
  6264. ],
  6265. ];
  6266. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6267. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6268. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6269. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6270. }
  6271. /**
  6272. * testSecurePostButton method
  6273. *
  6274. * Test that postButton adds _Token fields.
  6275. */
  6276. public function testSecurePostButton(): void
  6277. {
  6278. $this->View->setRequest($this->View->getRequest()
  6279. ->withAttribute('csrfToken', 'testkey')
  6280. ->withAttribute('formTokenData', ['unlockedFields' => []]));
  6281. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6282. $tokenDebug = urlencode(json_encode([
  6283. '/posts/delete/1',
  6284. [],
  6285. [],
  6286. ]));
  6287. $expected = [
  6288. 'form' => [
  6289. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6290. ],
  6291. ['div' => ['style' => 'display:none;']],
  6292. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6293. '/div',
  6294. 'button' => ['type' => 'submit'],
  6295. 'Delete',
  6296. '/button',
  6297. ['div' => ['style' => 'display:none;']],
  6298. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6299. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6300. ['input' => [
  6301. 'type' => 'hidden',
  6302. 'name' => '_Token[debug]',
  6303. 'value' => $tokenDebug,
  6304. ]],
  6305. '/div',
  6306. '/form',
  6307. ];
  6308. $this->assertHtml($expected, $result);
  6309. }
  6310. /**
  6311. * testPostLink method
  6312. */
  6313. public function testPostLink(): void
  6314. {
  6315. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6316. $expected = [
  6317. 'form' => [
  6318. 'method' => 'post', 'action' => '/posts/delete/1',
  6319. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6320. ],
  6321. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6322. '/form',
  6323. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6324. 'Delete',
  6325. '/a',
  6326. ];
  6327. $this->assertHtml($expected, $result);
  6328. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6329. $expected = [
  6330. 'form' => [
  6331. 'method' => 'post', 'action' => '/posts/delete/1',
  6332. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6333. ],
  6334. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6335. '/form',
  6336. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6337. 'Delete',
  6338. '/a',
  6339. ];
  6340. $this->assertHtml($expected, $result);
  6341. $result = $this->Form->postLink(
  6342. 'Delete',
  6343. '/posts/delete/1',
  6344. ['target' => '_blank', 'class' => 'btn btn-danger']
  6345. );
  6346. $expected = [
  6347. 'form' => [
  6348. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6349. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6350. ],
  6351. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6352. '/form',
  6353. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6354. 'Delete',
  6355. '/a',
  6356. ];
  6357. $this->assertHtml($expected, $result);
  6358. }
  6359. /**
  6360. * testPostLinkWithConfirm method
  6361. *
  6362. * Test the confirm option for postLink().
  6363. */
  6364. public function testPostLinkWithConfirm(): void
  6365. {
  6366. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6367. $expected = [
  6368. 'form' => [
  6369. 'method' => 'post', 'action' => '/posts/delete/1',
  6370. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6371. ],
  6372. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6373. '/form',
  6374. 'a' => [
  6375. 'href' => '#',
  6376. 'data-confirm-message' => 'Confirm?',
  6377. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/',
  6378. ],
  6379. 'Delete',
  6380. '/a',
  6381. ];
  6382. $this->assertHtml($expected, $result);
  6383. $result = $this->Form->postLink(
  6384. 'Delete',
  6385. '/posts/delete/1',
  6386. ['confirm' => "'Confirm'\nthis \"deletion\"?"]
  6387. );
  6388. $expected = [
  6389. 'form' => [
  6390. 'method' => 'post', 'action' => '/posts/delete/1',
  6391. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6392. ],
  6393. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6394. '/form',
  6395. 'a' => [
  6396. 'href' => '#',
  6397. 'data-confirm-message' => "&#039;Confirm&#039;\nthis &quot;deletion&quot;?",
  6398. 'onclick' => "preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/",
  6399. ],
  6400. 'Delete',
  6401. '/a',
  6402. ];
  6403. $this->assertHtml($expected, $result);
  6404. $this->Form->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { $(\'form[name="{{formName}}"]\').submit();};']);
  6405. $result = $this->Form->postLink(
  6406. 'Delete',
  6407. '/posts/delete/1',
  6408. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  6409. );
  6410. $expected = [
  6411. 'form' => [
  6412. 'method' => 'post', 'action' => '/posts/delete/1',
  6413. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6414. ],
  6415. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6416. '/form',
  6417. 'a' => [
  6418. 'href' => '#',
  6419. 'data-confirm-message' => 'Confirm this deletion?',
  6420. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/',
  6421. ],
  6422. 'Delete',
  6423. '/a',
  6424. ];
  6425. $this->assertHtml($expected, $result);
  6426. }
  6427. /**
  6428. * testPostLinkWithQuery method
  6429. *
  6430. * Test postLink() with query string args.
  6431. */
  6432. public function testPostLinkWithQuery(): void
  6433. {
  6434. $result = $this->Form->postLink(
  6435. 'Delete',
  6436. ['controller' => 'Posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6437. );
  6438. $expected = [
  6439. 'form' => [
  6440. 'method' => 'post', 'action' => '/Posts/delete/1?a=b&amp;c=d',
  6441. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6442. ],
  6443. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6444. '/form',
  6445. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6446. 'Delete',
  6447. '/a',
  6448. ];
  6449. $this->assertHtml($expected, $result);
  6450. }
  6451. /**
  6452. * testPostLinkWithData method
  6453. *
  6454. * Test postLink with additional data.
  6455. */
  6456. public function testPostLinkWithData(): void
  6457. {
  6458. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6459. $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
  6460. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6461. $this->Form->create($entity);
  6462. $this->Form->end();
  6463. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6464. $this->assertStringContainsString(
  6465. '<input type="hidden" name="name" value="show"',
  6466. $result,
  6467. 'should not contain entity data.'
  6468. );
  6469. }
  6470. /**
  6471. * testPostLinkSecurityHash method
  6472. *
  6473. * Test that security hashes for postLink include the url.
  6474. */
  6475. public function testPostLinkSecurityHash(): void
  6476. {
  6477. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6478. $hash .= '%3Aid';
  6479. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6480. $result = $this->Form->postLink(
  6481. 'Delete',
  6482. '/posts/delete/1',
  6483. ['data' => ['id' => 1]]
  6484. );
  6485. $tokenDebug = urlencode(json_encode([
  6486. '/posts/delete/1',
  6487. [
  6488. 'id' => 1,
  6489. ],
  6490. [],
  6491. ]));
  6492. $expected = [
  6493. 'form' => [
  6494. 'method' => 'post', 'action' => '/posts/delete/1',
  6495. 'name', 'style' => 'display:none;',
  6496. ],
  6497. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6498. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6499. 'div' => ['style' => 'display:none;'],
  6500. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6501. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6502. ['input' => [
  6503. 'type' => 'hidden',
  6504. 'name' => '_Token[debug]',
  6505. 'value' => $tokenDebug,
  6506. ]],
  6507. '/div',
  6508. '/form',
  6509. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6510. 'Delete',
  6511. '/a',
  6512. ];
  6513. $this->assertHtml($expected, $result);
  6514. }
  6515. /**
  6516. * testPostLinkSecurityHashBlockMode method
  6517. *
  6518. * Test that postLink doesn't modify the fields in the containing form.
  6519. *
  6520. * postLink() calls inside open forms should not modify the field list
  6521. * for the form.
  6522. */
  6523. public function testPostLinkSecurityHashBlockMode(): void
  6524. {
  6525. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6526. $hash .= '%3A';
  6527. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6528. $this->Form->create(null, ['url' => ['action' => 'add']]);
  6529. $this->Form->control('title');
  6530. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6531. $result = $this->View->fetch('postLink');
  6532. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  6533. $this->assertEquals(['title'], $fields);
  6534. $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
  6535. $reflect = new ReflectionProperty($this->Form, '_lastAction');
  6536. $this->assertSame('/Articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
  6537. }
  6538. /**
  6539. * testPostLinkSecurityHashNoDebugMode method
  6540. *
  6541. * Test that security does not include debug token if debug is false.
  6542. */
  6543. public function testPostLinkSecurityHashNoDebugMode(): void
  6544. {
  6545. Configure::write('debug', false);
  6546. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6547. $hash .= '%3Aid';
  6548. $this->View->setRequest($this->View->getRequest()
  6549. ->withAttribute('formTokenData', ['key' => 'test']));
  6550. $result = $this->Form->postLink(
  6551. 'Delete',
  6552. '/posts/delete/1',
  6553. ['data' => ['id' => 1]]
  6554. );
  6555. $expected = [
  6556. 'form' => [
  6557. 'method' => 'post', 'action' => '/posts/delete/1',
  6558. 'name', 'style' => 'display:none;',
  6559. ],
  6560. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6561. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6562. 'div' => ['style' => 'display:none;'],
  6563. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6564. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6565. '/div',
  6566. '/form',
  6567. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6568. 'Delete',
  6569. '/a',
  6570. ];
  6571. $this->assertHtml($expected, $result);
  6572. }
  6573. /**
  6574. * testPostLinkNestedData method
  6575. *
  6576. * Test using postLink with N dimensional data.
  6577. */
  6578. public function testPostLinkNestedData(): void
  6579. {
  6580. $data = [
  6581. 'one' => [
  6582. 'two' => [
  6583. 3, 4, 5,
  6584. ],
  6585. ],
  6586. ];
  6587. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6588. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6589. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6590. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6591. }
  6592. /**
  6593. * testPostLinkAfterGetForm method
  6594. *
  6595. * Test creating postLinks after a GET form.
  6596. */
  6597. public function testPostLinkAfterGetForm(): void
  6598. {
  6599. $this->View->setRequest($this->View->getRequest()
  6600. ->withAttribute('csrfToken', 'testkey')
  6601. ->withAttribute('formTokenData', []));
  6602. $this->Form->create($this->article, ['type' => 'get']);
  6603. $this->Form->end();
  6604. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6605. $tokenDebug = urlencode(json_encode([
  6606. '/posts/delete/1',
  6607. [],
  6608. [],
  6609. ]));
  6610. $expected = [
  6611. 'form' => [
  6612. 'method' => 'post', 'action' => '/posts/delete/1',
  6613. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6614. ],
  6615. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6616. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6617. 'div' => ['style' => 'display:none;'],
  6618. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6619. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6620. ['input' => [
  6621. 'type' => 'hidden', 'name' => '_Token[debug]',
  6622. 'value' => $tokenDebug,
  6623. ]],
  6624. '/div',
  6625. '/form',
  6626. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6627. 'Delete',
  6628. '/a',
  6629. ];
  6630. $this->assertHtml($expected, $result);
  6631. }
  6632. /**
  6633. * testPostLinkFormBuffer method
  6634. *
  6635. * Test that postLink adds form tags to view block.
  6636. */
  6637. public function testPostLinkFormBuffer(): void
  6638. {
  6639. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6640. $expected = [
  6641. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6642. 'Delete',
  6643. '/a',
  6644. ];
  6645. $this->assertHtml($expected, $result);
  6646. $result = $this->View->fetch('postLink');
  6647. $expected = [
  6648. 'form' => [
  6649. 'method' => 'post', 'action' => '/posts/delete/1',
  6650. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6651. ],
  6652. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6653. '/form',
  6654. ];
  6655. $this->assertHtml($expected, $result);
  6656. $result = $this->Form->postLink(
  6657. 'Delete',
  6658. '/posts/delete/2',
  6659. ['block' => true, 'method' => 'DELETE']
  6660. );
  6661. $expected = [
  6662. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6663. 'Delete',
  6664. '/a',
  6665. ];
  6666. $this->assertHtml($expected, $result);
  6667. $result = $this->View->fetch('postLink');
  6668. $expected = [
  6669. 'form' => [
  6670. 'method' => 'post', 'action' => '/posts/delete/1',
  6671. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6672. ],
  6673. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6674. '/form',
  6675. [
  6676. 'form' => [
  6677. 'method' => 'post', 'action' => '/posts/delete/2',
  6678. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6679. ],
  6680. ],
  6681. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6682. '/form',
  6683. ];
  6684. $this->assertHtml($expected, $result);
  6685. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6686. $expected = [
  6687. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6688. 'Delete',
  6689. '/a',
  6690. ];
  6691. $this->assertHtml($expected, $result);
  6692. $result = $this->View->fetch('foobar');
  6693. $expected = [
  6694. 'form' => [
  6695. 'method' => 'post', 'action' => '/posts/delete/1',
  6696. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6697. ],
  6698. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6699. '/form',
  6700. ];
  6701. $this->assertHtml($expected, $result);
  6702. }
  6703. /**
  6704. * testSubmitButton method
  6705. */
  6706. public function testSubmitButton(): void
  6707. {
  6708. $result = $this->Form->submit('');
  6709. $expected = [
  6710. 'div' => ['class' => 'submit'],
  6711. 'input' => ['type' => 'submit', 'value' => ''],
  6712. '/div',
  6713. ];
  6714. $this->assertHtml($expected, $result);
  6715. $result = $this->Form->submit('Test Submit');
  6716. $expected = [
  6717. 'div' => ['class' => 'submit'],
  6718. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6719. '/div',
  6720. ];
  6721. $this->assertHtml($expected, $result);
  6722. $result = $this->Form->submit('Next >');
  6723. $expected = [
  6724. 'div' => ['class' => 'submit'],
  6725. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6726. '/div',
  6727. ];
  6728. $this->assertHtml($expected, $result);
  6729. $result = $this->Form->submit('Next >', ['escape' => false]);
  6730. $expected = [
  6731. 'div' => ['class' => 'submit'],
  6732. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6733. '/div',
  6734. ];
  6735. $this->assertHtml($expected, $result);
  6736. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6737. $expected = [
  6738. 'div' => ['class' => 'submit'],
  6739. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6740. '/div',
  6741. ];
  6742. $this->assertHtml($expected, $result);
  6743. }
  6744. /**
  6745. * testSubmitImage method
  6746. *
  6747. * Test image submit types.
  6748. */
  6749. public function testSubmitImage(): void
  6750. {
  6751. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6752. $expected = [
  6753. 'div' => ['class' => 'submit'],
  6754. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6755. '/div',
  6756. ];
  6757. $this->assertHtml($expected, $result);
  6758. $result = $this->Form->submit('/relative/cake.power.gif');
  6759. $expected = [
  6760. 'div' => ['class' => 'submit'],
  6761. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6762. '/div',
  6763. ];
  6764. $this->assertHtml($expected, $result);
  6765. $result = $this->Form->submit('cake.power.gif');
  6766. $expected = [
  6767. 'div' => ['class' => 'submit'],
  6768. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6769. '/div',
  6770. ];
  6771. $this->assertHtml($expected, $result);
  6772. $result = $this->Form->submit('Not.an.image');
  6773. $expected = [
  6774. 'div' => ['class' => 'submit'],
  6775. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6776. '/div',
  6777. ];
  6778. $this->assertHtml($expected, $result);
  6779. }
  6780. /**
  6781. * testSubmitUnlockedByDefault method
  6782. *
  6783. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6784. * be submitted at a time.
  6785. */
  6786. public function testSubmitUnlockedByDefault(): void
  6787. {
  6788. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6789. $this->Form->create();
  6790. $this->Form->submit('Go go');
  6791. $this->Form->submit('Save', ['name' => 'save']);
  6792. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6793. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6794. }
  6795. /**
  6796. * testSubmitImageTimestamp method
  6797. *
  6798. * Test submit image with timestamps.
  6799. */
  6800. public function testSubmitImageTimestamp(): void
  6801. {
  6802. Configure::write('Asset.timestamp', 'force');
  6803. $result = $this->Form->submit('cake.power.gif');
  6804. $expected = [
  6805. 'div' => ['class' => 'submit'],
  6806. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6807. '/div',
  6808. ];
  6809. $this->assertHtml($expected, $result);
  6810. }
  6811. /**
  6812. * testDateTimeWithGetForms method
  6813. *
  6814. * Test that datetime() works with GET style forms.
  6815. */
  6816. public function testDateTimeWithGetForms(): void
  6817. {
  6818. $this->Form->create($this->article, ['type' => 'get']);
  6819. $result = $this->Form->datetime('created');
  6820. $expected = [
  6821. 'input' => [
  6822. 'type' => 'datetime-local',
  6823. 'name' => 'created',
  6824. 'value' => '',
  6825. 'step' => '1',
  6826. ],
  6827. ];
  6828. $this->assertHtml($expected, $result);
  6829. $result = $this->Form->datetime('created', ['default' => true]);
  6830. $expected = [
  6831. 'input' => [
  6832. 'type' => 'datetime-local',
  6833. 'name' => 'created',
  6834. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  6835. 'step' => '1',
  6836. ],
  6837. ];
  6838. $this->assertHtml($expected, $result);
  6839. }
  6840. /**
  6841. * Provides fractional schema types
  6842. *
  6843. * @return array
  6844. */
  6845. public static function fractionalTypeProvider(): array
  6846. {
  6847. return [
  6848. ['datetimefractional'],
  6849. ['timestampfractional'],
  6850. ['timestamptimezone'],
  6851. ];
  6852. }
  6853. /**
  6854. * testDateTimeWithFractional method
  6855. *
  6856. * Test that datetime() works with datetimefractional.
  6857. *
  6858. * @dataProvider fractionalTypeProvider
  6859. */
  6860. public function testDateTimeWithFractional(string $type): void
  6861. {
  6862. $this->Form->create([
  6863. 'schema' => [
  6864. 'created' => ['type' => $type],
  6865. ],
  6866. ]);
  6867. $result = $this->Form->datetime('created', [
  6868. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6869. ]);
  6870. $expected = [
  6871. 'input' => [
  6872. 'type' => 'datetime-local',
  6873. 'name' => 'created',
  6874. 'value' => '2019-09-27T02:52:43.123',
  6875. 'step' => '0.001',
  6876. ],
  6877. ];
  6878. $this->assertHtml($expected, $result);
  6879. }
  6880. /**
  6881. * testControlWithFractional method
  6882. *
  6883. * Test that control() works with datetimefractional.
  6884. *
  6885. * @dataProvider fractionalTypeProvider
  6886. */
  6887. public function testControlWithFractional(string $type): void
  6888. {
  6889. $this->Form->create([
  6890. 'schema' => [
  6891. 'created' => ['type' => $type],
  6892. ],
  6893. ]);
  6894. $result = $this->Form->control('created', [
  6895. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6896. ]);
  6897. $expected = [
  6898. 'div' => ['class' => 'input datetime'],
  6899. 'label' => ['for' => 'created'],
  6900. 'Created',
  6901. '/label',
  6902. 'input' => [
  6903. 'type' => 'datetime-local',
  6904. 'name' => 'created',
  6905. 'id' => 'created',
  6906. 'value' => '2019-09-27T02:52:43.123',
  6907. 'step' => '0.001',
  6908. ],
  6909. '/div',
  6910. ];
  6911. $this->assertHtml($expected, $result);
  6912. }
  6913. /**
  6914. * testForMagicControlNonExistentNotValidated method
  6915. */
  6916. public function testForMagicControlNonExistentNotValidated(): void
  6917. {
  6918. $this->Form->create($this->article);
  6919. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6920. $result = $this->Form->control('nonexistent_not_validated');
  6921. $expected = [
  6922. 'label' => ['for' => 'nonexistent-not-validated'],
  6923. 'Nonexistent Not Validated',
  6924. '/label',
  6925. 'input' => [
  6926. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6927. 'id' => 'nonexistent-not-validated',
  6928. ],
  6929. ];
  6930. $this->assertHtml($expected, $result);
  6931. $result = $this->Form->control('nonexistent_not_validated', [
  6932. 'val' => 'my value',
  6933. ]);
  6934. $expected = [
  6935. 'label' => ['for' => 'nonexistent-not-validated'],
  6936. 'Nonexistent Not Validated',
  6937. '/label',
  6938. 'input' => [
  6939. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6940. 'value' => 'my value', 'id' => 'nonexistent-not-validated',
  6941. ],
  6942. ];
  6943. $this->assertHtml($expected, $result);
  6944. $this->View->setRequest(
  6945. $this->View->getRequest()->withData('nonexistent_not_validated', 'CakePHP magic')
  6946. );
  6947. $this->Form->create($this->article);
  6948. $result = $this->Form->control('nonexistent_not_validated');
  6949. $expected = [
  6950. 'label' => ['for' => 'nonexistent-not-validated'],
  6951. 'Nonexistent Not Validated',
  6952. '/label',
  6953. 'input' => [
  6954. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6955. 'value' => 'CakePHP magic', 'id' => 'nonexistent-not-validated',
  6956. ],
  6957. ];
  6958. $this->assertHtml($expected, $result);
  6959. }
  6960. /**
  6961. * testFormMagicControlLabel method
  6962. */
  6963. public function testFormMagicControlLabel(): void
  6964. {
  6965. $this->getTableLocator()->get('Contacts', [
  6966. 'className' => ContactsTable::class,
  6967. ]);
  6968. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6969. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6970. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  6971. $expected = [
  6972. 'label' => ['for' => 'contacts-name'],
  6973. 'My label',
  6974. '/label',
  6975. 'input' => [
  6976. 'type' => 'text',
  6977. 'name' => 'Contacts[name]',
  6978. 'id' => 'contacts-name',
  6979. 'maxlength' => '255',
  6980. ],
  6981. ];
  6982. $this->assertHtml($expected, $result);
  6983. $result = $this->Form->control('name', [
  6984. 'label' => ['class' => 'mandatory'],
  6985. ]);
  6986. $expected = [
  6987. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6988. 'Name',
  6989. '/label',
  6990. 'input' => [
  6991. 'type' => 'text', 'name' => 'name',
  6992. 'id' => 'name', 'maxlength' => '255',
  6993. ],
  6994. ];
  6995. $this->assertHtml($expected, $result);
  6996. $result = $this->Form->control('name', [
  6997. 'div' => false,
  6998. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  6999. ]);
  7000. $expected = [
  7001. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7002. 'My label',
  7003. '/label',
  7004. 'input' => [
  7005. 'type' => 'text', 'name' => 'name',
  7006. 'id' => 'name', 'maxlength' => '255',
  7007. ],
  7008. ];
  7009. $this->assertHtml($expected, $result);
  7010. $result = $this->Form->control('Contact.name', [
  7011. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  7012. ]);
  7013. $expected = [
  7014. 'label' => ['for' => 'my_id'],
  7015. 'Name',
  7016. '/label',
  7017. 'input' => [
  7018. 'type' => 'text', 'name' => 'Contact[name]',
  7019. 'id' => 'my_id', 'maxlength' => '255',
  7020. ],
  7021. ];
  7022. $this->assertHtml($expected, $result);
  7023. $result = $this->Form->control('1.id');
  7024. $expected = ['input' => [
  7025. 'type' => 'hidden', 'name' => '1[id]',
  7026. 'id' => '1-id',
  7027. ]];
  7028. $this->assertHtml($expected, $result);
  7029. $result = $this->Form->control('1.name');
  7030. $expected = [
  7031. 'label' => ['for' => '1-name'],
  7032. 'Name',
  7033. '/label',
  7034. 'input' => [
  7035. 'type' => 'text', 'name' => '1[name]',
  7036. 'id' => '1-name', 'maxlength' => '255',
  7037. ],
  7038. ];
  7039. $this->assertHtml($expected, $result);
  7040. }
  7041. /**
  7042. * testFormEnd method
  7043. */
  7044. public function testFormEnd(): void
  7045. {
  7046. $this->assertSame('</form>', $this->Form->end());
  7047. }
  7048. /**
  7049. * testMultiRecordForm method
  7050. *
  7051. * Test the generation of fields for a multi record form.
  7052. */
  7053. public function testMultiRecordForm(): void
  7054. {
  7055. $articles = $this->getTableLocator()->get('Articles');
  7056. $articles->hasMany('Comments');
  7057. $comment = new Entity(['comment' => 'Value']);
  7058. $article = new Article(['comments' => [$comment]]);
  7059. $this->Form->create([$article]);
  7060. $result = $this->Form->control('0.comments.1.comment');
  7061. // phpcs:disable
  7062. $expected = [
  7063. 'div' => ['class' => 'input textarea'],
  7064. 'label' => ['for' => '0-comments-1-comment'],
  7065. 'Comment',
  7066. '/label',
  7067. 'textarea' => [
  7068. 'name',
  7069. 'id' => '0-comments-1-comment',
  7070. 'rows' => 5
  7071. ],
  7072. '/textarea',
  7073. '/div'
  7074. ];
  7075. // phpcs:enable
  7076. $this->assertHtml($expected, $result);
  7077. $result = $this->Form->control('0.comments.0.comment');
  7078. // phpcs:disable
  7079. $expected = [
  7080. 'div' => ['class' => 'input textarea'],
  7081. 'label' => ['for' => '0-comments-0-comment'],
  7082. 'Comment',
  7083. '/label',
  7084. 'textarea' => [
  7085. 'name',
  7086. 'id' => '0-comments-0-comment',
  7087. 'rows' => 5
  7088. ],
  7089. 'Value',
  7090. '/textarea',
  7091. '/div'
  7092. ];
  7093. // phpcs:enable
  7094. $this->assertHtml($expected, $result);
  7095. $comment->setError('comment', ['Not valid']);
  7096. $result = $this->Form->control('0.comments.0.comment');
  7097. // phpcs:disable
  7098. $expected = [
  7099. 'div' => ['class' => 'input textarea error'],
  7100. 'label' => ['for' => '0-comments-0-comment'],
  7101. 'Comment',
  7102. '/label',
  7103. 'textarea' => [
  7104. 'name',
  7105. 'class' => 'form-error',
  7106. 'id' => '0-comments-0-comment',
  7107. 'aria-invalid' => 'true',
  7108. 'aria-describedby' => '0-comments-0-comment-error',
  7109. 'rows' => 5
  7110. ],
  7111. 'Value',
  7112. '/textarea',
  7113. ['div' => ['class' => 'error-message', 'id' => '0-comments-0-comment-error']],
  7114. 'Not valid',
  7115. '/div',
  7116. '/div'
  7117. ];
  7118. // phpcs:enable
  7119. $this->assertHtml($expected, $result);
  7120. $this->getTableLocator()->get('Comments')
  7121. ->getValidator('default')
  7122. ->allowEmptyString('comment', null, false);
  7123. $result = $this->Form->control('0.comments.1.comment');
  7124. // phpcs:disable
  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. 'data-validity-message' => 'This field cannot be left empty',
  7136. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7137. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7138. ],
  7139. '/textarea',
  7140. '/div'
  7141. ];
  7142. // phpcs:enable
  7143. $this->assertHtml($expected, $result);
  7144. }
  7145. /**
  7146. * testHtml5Controls method
  7147. *
  7148. * Test that some html5 inputs + FormHelper::__call() work.
  7149. */
  7150. public function testHtml5Controls(): void
  7151. {
  7152. $result = $this->Form->email('User.email');
  7153. $expected = [
  7154. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  7155. ];
  7156. $this->assertHtml($expected, $result);
  7157. $result = $this->Form->search('User.query');
  7158. $expected = [
  7159. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  7160. ];
  7161. $this->assertHtml($expected, $result);
  7162. $result = $this->Form->search('User.query', ['value' => 'test']);
  7163. $expected = [
  7164. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  7165. ];
  7166. $this->assertHtml($expected, $result);
  7167. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7168. $expected = [
  7169. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  7170. ];
  7171. $this->assertHtml($expected, $result);
  7172. }
  7173. /**
  7174. * testHtml5ControlWithControl method
  7175. *
  7176. * Test accessing html5 inputs through control().
  7177. */
  7178. public function testHtml5ControlWithControl(): void
  7179. {
  7180. $this->Form->create();
  7181. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7182. $result = $this->Form->control('website', [
  7183. 'type' => 'url',
  7184. 'val' => 'http://domain.tld',
  7185. 'label' => false,
  7186. ]);
  7187. $expected = [
  7188. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7189. ];
  7190. $this->assertHtml($expected, $result);
  7191. }
  7192. /**
  7193. * testHtml5ControlException method
  7194. *
  7195. * Test errors when field name is missing.
  7196. */
  7197. public function testHtml5ControlException(): void
  7198. {
  7199. $this->expectException(CakeException::class);
  7200. $this->Form->email();
  7201. }
  7202. /**
  7203. * tests fields that are required use custom validation messages
  7204. */
  7205. public function testHtml5ErrorMessage(): void
  7206. {
  7207. $this->Form->setConfig('autoSetCustomValidity', true);
  7208. $validator = (new Validator())
  7209. ->notEmptyString('email', 'Custom error message')
  7210. ->requirePresence('password')
  7211. ->alphaNumeric('password')
  7212. ->notBlank('phone');
  7213. $table = $this->getTableLocator()->get('Contacts', [
  7214. 'className' => ContactsTable::class,
  7215. ]);
  7216. $table->setValidator('default', $validator);
  7217. $contact = new Entity();
  7218. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7219. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7220. $result = $this->Form->control('password');
  7221. $expected = [
  7222. 'label' => ['for' => 'password'],
  7223. 'Password',
  7224. '/label',
  7225. 'input' => [
  7226. 'id' => 'password',
  7227. 'name' => 'password',
  7228. 'type' => 'password',
  7229. 'value' => '',
  7230. 'required' => 'required',
  7231. 'data-validity-message' => 'This field cannot be left empty',
  7232. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7233. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7234. ],
  7235. ];
  7236. $this->assertHtml($expected, $result);
  7237. $result = $this->Form->control('phone');
  7238. $expected = [
  7239. 'label' => ['for' => 'phone'],
  7240. 'Phone',
  7241. '/label',
  7242. 'input' => [
  7243. 'id' => 'phone',
  7244. 'name' => 'phone',
  7245. 'type' => 'tel',
  7246. 'value' => '',
  7247. 'maxlength' => 255,
  7248. 'required' => 'required',
  7249. 'data-validity-message' => 'This field cannot be left empty',
  7250. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7251. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7252. ],
  7253. ];
  7254. $this->assertHtml($expected, $result);
  7255. $result = $this->Form->control('email');
  7256. $expected = [
  7257. 'label' => ['for' => 'email'],
  7258. 'Email',
  7259. '/label',
  7260. 'input' => [
  7261. 'id' => 'email',
  7262. 'name' => 'email',
  7263. 'type' => 'email',
  7264. 'value' => '',
  7265. 'maxlength' => 255,
  7266. 'required' => 'required',
  7267. 'data-validity-message' => 'Custom error message',
  7268. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7269. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7270. ],
  7271. ];
  7272. $this->assertHtml($expected, $result);
  7273. }
  7274. /**
  7275. * tests that custom validation messages are in templateVars
  7276. */
  7277. public function testHtml5ErrorMessageInTemplateVars(): void
  7278. {
  7279. $validator = (new Validator())
  7280. ->notEmptyString('email', 'Custom error "message" & entities')
  7281. ->requirePresence('password')
  7282. ->alphaNumeric('password')
  7283. ->notBlank('phone');
  7284. $table = $this->getTableLocator()->get('Contacts', [
  7285. 'className' => ContactsTable::class,
  7286. ]);
  7287. $table->setValidator('default', $validator);
  7288. $contact = new Entity();
  7289. $this->Form->setConfig('autoSetCustomValidity', false);
  7290. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7291. $this->Form->setTemplates([
  7292. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7293. 'inputContainer' => '{{content}}',
  7294. ]);
  7295. $result = $this->Form->control('password');
  7296. $expected = [
  7297. 'label' => ['for' => 'password'],
  7298. 'Password',
  7299. '/label',
  7300. 'input' => [
  7301. 'id' => 'password',
  7302. 'name' => 'password',
  7303. 'type' => 'password',
  7304. 'value' => '',
  7305. 'required' => 'required',
  7306. 'data-message' => 'This field cannot be left empty',
  7307. ],
  7308. ];
  7309. $this->assertHtml($expected, $result);
  7310. $result = $this->Form->control('phone');
  7311. $expected = [
  7312. 'label' => ['for' => 'phone'],
  7313. 'Phone',
  7314. '/label',
  7315. 'input' => [
  7316. 'id' => 'phone',
  7317. 'name' => 'phone',
  7318. 'type' => 'tel',
  7319. 'value' => '',
  7320. 'maxlength' => 255,
  7321. 'required' => 'required',
  7322. 'data-message' => 'This field cannot be left empty',
  7323. ],
  7324. ];
  7325. $this->assertHtml($expected, $result);
  7326. $result = $this->Form->control('email', [
  7327. 'templateVars' => [
  7328. 'custom' => 'data-custom="1"',
  7329. ],
  7330. ]);
  7331. $expected = [
  7332. 'label' => ['for' => 'email'],
  7333. 'Email',
  7334. '/label',
  7335. 'input' => [
  7336. 'id' => 'email',
  7337. 'name' => 'email',
  7338. 'type' => 'email',
  7339. 'value' => '',
  7340. 'maxlength' => 255,
  7341. 'required' => 'required',
  7342. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7343. 'data-custom' => '1',
  7344. ],
  7345. ];
  7346. $this->assertHtml($expected, $result);
  7347. $this->Form->setConfig('autoSetCustomValidity', true);
  7348. }
  7349. /**
  7350. * testRequiredAttribute method
  7351. *
  7352. * Tests that formhelper sets required attributes.
  7353. */
  7354. public function testRequiredAttribute(): void
  7355. {
  7356. $this->article['required'] = [
  7357. 'title' => true,
  7358. 'body' => false,
  7359. ];
  7360. $this->Form->create($this->article);
  7361. $result = $this->Form->control('title');
  7362. $expected = [
  7363. 'div' => ['class' => 'input text required'],
  7364. 'label' => ['for' => 'title'],
  7365. 'Title',
  7366. '/label',
  7367. 'input' => [
  7368. 'type' => 'text',
  7369. 'name' => 'title',
  7370. 'id' => 'title',
  7371. 'required' => 'required',
  7372. 'data-validity-message' => 'This field cannot be left empty',
  7373. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7374. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7375. ],
  7376. '/div',
  7377. ];
  7378. $this->assertHtml($expected, $result);
  7379. $result = $this->Form->control('title', ['required' => false]);
  7380. $this->assertStringNotContainsString('required', $result);
  7381. $result = $this->Form->control('body');
  7382. $expected = [
  7383. 'div' => ['class' => 'input text'],
  7384. 'label' => ['for' => 'body'],
  7385. 'Body',
  7386. '/label',
  7387. 'input' => [
  7388. 'type' => 'text',
  7389. 'name' => 'body',
  7390. 'id' => 'body',
  7391. ],
  7392. '/div',
  7393. ];
  7394. $this->assertHtml($expected, $result);
  7395. $result = $this->Form->control('body', ['required' => true]);
  7396. $this->assertStringContainsString('required', $result);
  7397. }
  7398. /**
  7399. * testControlsNotNested method
  7400. *
  7401. * Tests that it is possible to put inputs outside of the label.
  7402. */
  7403. public function testControlsNotNested(): void
  7404. {
  7405. $this->Form->setTemplates([
  7406. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7407. 'formGroup' => '{{input}}{{label}}',
  7408. ]);
  7409. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7410. $expected = [
  7411. 'div' => ['class' => 'input checkbox'],
  7412. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7413. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7414. 'label' => ['for' => 'foo'],
  7415. 'Foo',
  7416. '/label',
  7417. '/div',
  7418. ];
  7419. $this->assertHtml($expected, $result);
  7420. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7421. $expected = [
  7422. 'div' => ['class' => 'input checkbox'],
  7423. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7424. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7425. '/div',
  7426. ];
  7427. $this->assertHtml($expected, $result);
  7428. $result = $this->Form->control('confirm', [
  7429. 'type' => 'radio',
  7430. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7431. ]);
  7432. $expected = [
  7433. 'div' => ['class' => 'input radio'],
  7434. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '', 'id' => 'confirm']],
  7435. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7436. ['label' => ['for' => 'confirm-y']],
  7437. 'Yes',
  7438. '/label',
  7439. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7440. ['label' => ['for' => 'confirm-n']],
  7441. 'No',
  7442. '/label',
  7443. '<label',
  7444. 'Confirm',
  7445. '/label',
  7446. '/div',
  7447. ];
  7448. $this->assertHtml($expected, $result);
  7449. $result = $this->Form->select('category', ['1', '2'], [
  7450. 'multiple' => 'checkbox',
  7451. 'name' => 'fish',
  7452. ]);
  7453. $expected = [
  7454. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  7455. ['div' => ['class' => 'checkbox']],
  7456. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7457. ['label' => ['for' => 'fish-0']],
  7458. '1',
  7459. '/label',
  7460. '/div',
  7461. ['div' => ['class' => 'checkbox']],
  7462. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7463. ['label' => ['for' => 'fish-1']],
  7464. '2',
  7465. '/label',
  7466. '/div',
  7467. ];
  7468. $this->assertHtml($expected, $result);
  7469. }
  7470. /**
  7471. * testControlContainerTemplates method
  7472. *
  7473. * Test that *Container templates are used by input.
  7474. */
  7475. public function testControlContainerTemplates(): void
  7476. {
  7477. $this->Form->setTemplates([
  7478. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7479. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7480. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7481. ]);
  7482. $this->article['errors'] = [
  7483. 'Article' => ['published' => 'error message'],
  7484. ];
  7485. $this->Form->create($this->article);
  7486. $result = $this->Form->control('accept', [
  7487. 'type' => 'checkbox',
  7488. ]);
  7489. $expected = [
  7490. 'div' => ['class' => 'check'],
  7491. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7492. 'label' => ['for' => 'accept'],
  7493. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7494. 'Accept',
  7495. '/label',
  7496. '/div',
  7497. ];
  7498. $this->assertHtml($expected, $result);
  7499. $result = $this->Form->control('accept', [
  7500. 'type' => 'radio',
  7501. 'options' => ['Y', 'N'],
  7502. ]);
  7503. $this->assertStringContainsString('<div class="rad">', $result);
  7504. $result = $this->Form->control('Article.published', [
  7505. 'type' => 'radio',
  7506. 'options' => ['Y', 'N'],
  7507. ]);
  7508. $this->assertStringContainsString('<div class="rad err">', $result);
  7509. }
  7510. /**
  7511. * testFormGroupTemplates method
  7512. *
  7513. * Test that *Container templates are used by input.
  7514. */
  7515. public function testFormGroupTemplates(): void
  7516. {
  7517. $this->Form->setTemplates([
  7518. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7519. ]);
  7520. $this->Form->create($this->article);
  7521. $result = $this->Form->control('accept', [
  7522. 'type' => 'radio',
  7523. 'options' => ['Y', 'N'],
  7524. ]);
  7525. $this->assertStringContainsString('<div class="radio">', $result);
  7526. }
  7527. /**
  7528. * testResetTemplates method
  7529. *
  7530. * Test resetting templates.
  7531. */
  7532. public function testResetTemplates(): void
  7533. {
  7534. $this->Form->setTemplates(['input' => '<input/>']);
  7535. $this->assertSame('<input/>', $this->Form->templater()->get('input'));
  7536. $this->Form->resetTemplates();
  7537. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7538. }
  7539. /**
  7540. * testContext method
  7541. *
  7542. * Test the context method.
  7543. */
  7544. public function testContext(): void
  7545. {
  7546. $result = $this->Form->context();
  7547. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7548. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7549. $this->assertSame($mock, $this->Form->context($mock));
  7550. $this->assertSame($mock, $this->Form->context());
  7551. }
  7552. /**
  7553. * testAutoDomId method
  7554. */
  7555. public function testAutoDomId(): void
  7556. {
  7557. $result = $this->Form->text('field', ['id' => true]);
  7558. $expected = [
  7559. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7560. ];
  7561. $this->assertHtml($expected, $result);
  7562. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7563. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7564. $expected = [
  7565. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '', 'id' => 'field'],
  7566. ['label' => ['for' => 'field-0']],
  7567. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7568. 'option A',
  7569. '/label',
  7570. ['label' => ['for' => 'field-1']],
  7571. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7572. 'option B',
  7573. '/label',
  7574. ];
  7575. $this->assertHtml($expected, $result);
  7576. $result = $this->Form->select(
  7577. 'multi_field',
  7578. ['first', 'second'],
  7579. ['multiple' => 'checkbox', 'id' => true]
  7580. );
  7581. $expected = [
  7582. 'input' => [
  7583. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '', 'id' => 'multi-field',
  7584. ],
  7585. ['div' => ['class' => 'checkbox']],
  7586. ['label' => ['for' => 'multi-field-0']],
  7587. ['input' => [
  7588. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7589. 'value' => '0', 'id' => 'multi-field-0',
  7590. ]],
  7591. 'first',
  7592. '/label',
  7593. '/div',
  7594. ['div' => ['class' => 'checkbox']],
  7595. ['label' => ['for' => 'multi-field-1']],
  7596. ['input' => [
  7597. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7598. 'value' => '1', 'id' => 'multi-field-1',
  7599. ]],
  7600. 'second',
  7601. '/label',
  7602. '/div',
  7603. ];
  7604. $this->assertHtml($expected, $result);
  7605. }
  7606. /**
  7607. * Test the basic setters and getters for value sources
  7608. */
  7609. public function testFormValueSourcesSettersGetters(): void
  7610. {
  7611. $this->View->setRequest($this->View->getRequest()
  7612. ->withData('id', '1')
  7613. ->withQueryParams(['id' => '2']));
  7614. $expected = ['data', 'context'];
  7615. $result = $this->Form->getValueSources();
  7616. $this->assertEquals($expected, $result);
  7617. $this->Form->setValueSources(['context']);
  7618. $result = $this->Form->getSourceValue('id');
  7619. $this->assertNull($result);
  7620. $this->Form->setValueSources('query');
  7621. $expected = ['query'];
  7622. $result = $this->Form->getValueSources();
  7623. $this->assertEquals($expected, $result);
  7624. $expected = '2';
  7625. $result = $this->Form->getSourceValue('id');
  7626. $this->assertSame($expected, $result);
  7627. $this->Form->setValueSources(['data']);
  7628. $expected = '1';
  7629. $result = $this->Form->getSourceValue('id');
  7630. $this->assertSame($expected, $result);
  7631. $this->Form->setValueSources(['query', 'data']);
  7632. $expected = '2';
  7633. $result = $this->Form->getSourceValue('id');
  7634. $this->assertSame($expected, $result);
  7635. }
  7636. public function testValueSourcesValidation(): void
  7637. {
  7638. $this->expectException(InvalidArgumentException::class);
  7639. $this->expectExceptionMessage('Invalid value source(s): `invalid`, `foo`. Valid values are: `context`, `data`, `query`.');
  7640. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7641. }
  7642. /**
  7643. * Tests the different input rendering values based on sources values switching
  7644. */
  7645. public function testFormValueSourcesSingleSwitchRendering(): void
  7646. {
  7647. $articles = $this->getTableLocator()->get('Articles');
  7648. $article = new Article();
  7649. $articles->patchEntity($article, ['id' => '3']);
  7650. $this->Form->create($article);
  7651. $this->Form->setValueSources(['context']);
  7652. $result = $this->Form->control('id');
  7653. $expected = [
  7654. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7655. ];
  7656. $this->assertHtml($expected, $result);
  7657. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7658. $this->Form->setValueSources(['query']);
  7659. $result = $this->Form->control('id');
  7660. $expected = [
  7661. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7662. ];
  7663. $this->assertHtml($expected, $result);
  7664. $this->View->setRequest($this->View->getRequest()
  7665. ->withQueryParams(['id' => '5a'])
  7666. ->withData('id', '5b'));
  7667. $this->Form->setValueSources(['context']);
  7668. $this->Form->create($article);
  7669. $result = $this->Form->control('id');
  7670. $expected = [
  7671. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7672. ];
  7673. $this->assertHtml($expected, $result);
  7674. $this->Form->setValueSources(['data']);
  7675. $this->Form->create($article);
  7676. $result = $this->Form->control('id');
  7677. $expected = [
  7678. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7679. ];
  7680. $this->assertHtml($expected, $result);
  7681. $this->Form->setValueSources(['query']);
  7682. $result = $this->Form->control('id');
  7683. $expected = [
  7684. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7685. ];
  7686. $this->assertHtml($expected, $result);
  7687. }
  7688. /**
  7689. * Tests the different input rendering values based on sources values switching while supplying
  7690. * an entity (base context) and multiple sources (such as data, query)
  7691. */
  7692. public function testFormValueSourcesListSwitchRendering(): void
  7693. {
  7694. $articles = $this->getTableLocator()->get('Articles');
  7695. $article = new Article();
  7696. $articles->patchEntity($article, ['id' => '3']);
  7697. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7698. $this->Form->create($article);
  7699. $this->Form->setValueSources(['context', 'query']);
  7700. $result = $this->Form->control('id');
  7701. $expected = [
  7702. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7703. ];
  7704. $this->assertHtml($expected, $result);
  7705. $this->Form->setValueSources(['query', 'context']);
  7706. $result = $this->Form->control('id');
  7707. $expected = [
  7708. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7709. ];
  7710. $this->assertHtml($expected, $result);
  7711. $this->Form->setValueSources(['data', 'query', 'context']);
  7712. $result = $this->Form->control('id');
  7713. $expected = [
  7714. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7715. ];
  7716. $this->assertHtml($expected, $result);
  7717. $this->View->setRequest($this->View->getRequest()
  7718. ->withData('id', '8')
  7719. ->withQueryParams(['id' => '9']));
  7720. $this->Form->setValueSources(['data', 'query', 'context']);
  7721. $result = $this->Form->control('id');
  7722. $expected = [
  7723. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7724. ];
  7725. $this->assertHtml($expected, $result);
  7726. }
  7727. /**
  7728. * Test the different form input renderings based on values sources switchings through form options
  7729. */
  7730. public function testFormValueSourcesSwitchViaOptionsRendering(): void
  7731. {
  7732. $articles = $this->getTableLocator()->get('Articles');
  7733. $article = new Article();
  7734. $articles->patchEntity($article, ['id' => '3']);
  7735. $this->View->setRequest(
  7736. $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
  7737. );
  7738. $this->Form->create($article, ['valueSources' => 'query']);
  7739. $result = $this->Form->control('id');
  7740. $expected = [
  7741. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7742. ];
  7743. $this->assertHtml($expected, $result);
  7744. $result = $this->Form->getSourceValue('id');
  7745. $this->assertSame('5', $result);
  7746. $this->Form->setValueSources(['context']);
  7747. $this->Form->create($article, ['valueSources' => 'query']);
  7748. $result = $this->Form->control('id');
  7749. $expected = [
  7750. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7751. ];
  7752. $this->assertHtml($expected, $result);
  7753. $result = $this->Form->getSourceValue('id');
  7754. $this->assertSame('5', $result);
  7755. $this->Form->setValueSources(['query']);
  7756. $this->Form->create($article, ['valueSources' => 'data']);
  7757. $result = $this->Form->control('id');
  7758. $expected = [
  7759. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7760. ];
  7761. $this->assertHtml($expected, $result);
  7762. $result = $this->Form->getSourceValue('id');
  7763. $this->assertSame('4', $result);
  7764. $this->Form->setValueSources(['query']);
  7765. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7766. $result = $this->Form->control('id');
  7767. $expected = [
  7768. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7769. ];
  7770. $this->assertHtml($expected, $result);
  7771. $result = $this->Form->getSourceValue('id');
  7772. $this->assertSame(3, $result);
  7773. }
  7774. /**
  7775. * Test the different form input renderings based on values sources switchings through form options
  7776. */
  7777. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering(): void
  7778. {
  7779. $articles = $this->getTableLocator()->get('Articles');
  7780. $article = new Article();
  7781. $articles->patchEntity($article, ['id' => '3']);
  7782. $this->View->setRequest(
  7783. $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
  7784. );
  7785. $this->Form->setValueSources(['context'])
  7786. ->create($article, ['valueSources' => ['query', 'data']]);
  7787. $result = $this->Form->control('id');
  7788. $expected = [
  7789. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7790. ];
  7791. $this->assertHtml($expected, $result);
  7792. $result = $this->Form->getSourceValue('id');
  7793. $this->assertSame('11', $result);
  7794. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7795. $this->Form->setValueSources(['context'])
  7796. ->create($article, ['valueSources' => ['query', 'data']]);
  7797. $result = $this->Form->control('id');
  7798. $expected = [
  7799. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7800. ];
  7801. $this->assertHtml($expected, $result);
  7802. $result = $this->Form->getSourceValue('id');
  7803. $this->assertSame('10', $result);
  7804. }
  7805. /**
  7806. * Test the different form values sources resetting through From::end();
  7807. */
  7808. public function testFormValueSourcesResetViaEnd(): void
  7809. {
  7810. $expected = ['data', 'context'];
  7811. $result = $this->Form->getValueSources();
  7812. $this->assertEquals($expected, $result);
  7813. $expected = ['query', 'context', 'data'];
  7814. $this->Form->setValueSources(['query', 'context', 'data']);
  7815. $result = $this->Form->getValueSources();
  7816. $this->assertEquals($expected, $result);
  7817. $this->Form->create();
  7818. $result = $this->Form->getValueSources();
  7819. $this->assertEquals($expected, $result);
  7820. $this->Form->end();
  7821. $result = $this->Form->getValueSources();
  7822. $this->assertEquals(['data', 'context'], $result);
  7823. }
  7824. /**
  7825. * Test sources values defaults handling
  7826. */
  7827. public function testFormValueSourcesDefaults(): void
  7828. {
  7829. $this->View->setRequest(
  7830. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7831. );
  7832. $this->Form->create();
  7833. $result = $this->Form->password('password');
  7834. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7835. $this->assertHtml($expected, $result);
  7836. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7837. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7838. $this->assertHtml($expected, $result);
  7839. $this->Form->setValueSources('query');
  7840. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7841. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7842. $this->assertHtml($expected, $result);
  7843. $this->Form->setValueSources('data');
  7844. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7845. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7846. $this->assertHtml($expected, $result);
  7847. }
  7848. /**
  7849. * Test sources values schema defaults handling
  7850. */
  7851. public function testSourcesValueDoesntExistPassThrough(): void
  7852. {
  7853. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7854. $articles = $this->getTableLocator()->get('Articles');
  7855. $entity = $articles->newEmptyEntity();
  7856. $this->Form->create($entity);
  7857. $this->Form->setValueSources(['query', 'context']);
  7858. $result = $this->Form->getSourceValue('category');
  7859. $this->assertSame('sesame-cookies', $result);
  7860. $this->Form->setValueSources(['context', 'query']);
  7861. $result = $this->Form->getSourceValue('category');
  7862. $this->assertSame('sesame-cookies', $result);
  7863. }
  7864. /**
  7865. * testNestedLabelInput method
  7866. *
  7867. * Test the `nestedInput` parameter
  7868. */
  7869. public function testNestedLabelInput(): void
  7870. {
  7871. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7872. $expected = [
  7873. 'div' => ['class' => 'input text'],
  7874. 'label' => ['for' => 'foo'],
  7875. ['input' => [
  7876. 'type' => 'text',
  7877. 'name' => 'foo',
  7878. 'id' => 'foo',
  7879. ]],
  7880. 'Foo',
  7881. '/label',
  7882. '/div',
  7883. ];
  7884. $this->assertHtml($expected, $result);
  7885. }
  7886. /**
  7887. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7888. *
  7889. * This test makes sure `false` excludes the label from the render
  7890. */
  7891. public function testControlLabelManipulationDisableLabels(): void
  7892. {
  7893. $result = $this->Form->control('test', [
  7894. 'type' => 'radio',
  7895. 'options' => ['A', 'B'],
  7896. 'labelOptions' => false,
  7897. ]);
  7898. $expected = [
  7899. ['div' => ['class' => 'input radio']],
  7900. '<label',
  7901. 'Test',
  7902. '/label',
  7903. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7904. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7905. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7906. '/div',
  7907. ];
  7908. $this->assertHtml($expected, $result);
  7909. $result = $this->Form->control('checkbox1', [
  7910. 'label' => 'My checkboxes',
  7911. 'multiple' => 'checkbox',
  7912. 'type' => 'select',
  7913. 'options' => [
  7914. ['text' => 'First Checkbox', 'value' => 1],
  7915. ['text' => 'Second Checkbox', 'value' => 2],
  7916. ],
  7917. 'labelOptions' => false,
  7918. ]);
  7919. $expected = [
  7920. ['div' => ['class' => 'input select']],
  7921. ['label' => ['for' => 'checkbox1']],
  7922. 'My checkboxes',
  7923. '/label',
  7924. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7925. ['div' => ['class' => 'checkbox']],
  7926. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7927. '/div',
  7928. ['div' => ['class' => 'checkbox']],
  7929. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7930. '/div',
  7931. '/div',
  7932. ];
  7933. $this->assertHtml($expected, $result);
  7934. }
  7935. /**
  7936. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7937. *
  7938. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7939. * added to the class if checked.
  7940. *
  7941. * Also checks to make sure any custom attributes are rendered correctly
  7942. */
  7943. public function testControlLabelManipulationRadios(): void
  7944. {
  7945. $result = $this->Form->control('test', [
  7946. 'type' => 'radio',
  7947. 'options' => ['A', 'B'],
  7948. 'labelOptions' => ['class' => 'custom-class'],
  7949. ]);
  7950. $expected = [
  7951. ['div' => ['class' => 'input radio']],
  7952. '<label',
  7953. 'Test',
  7954. '/label',
  7955. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7956. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7957. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7958. 'A',
  7959. '/label',
  7960. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  7961. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7962. 'B',
  7963. '/label',
  7964. '/div',
  7965. ];
  7966. $this->assertHtml($expected, $result);
  7967. $result = $this->Form->control('test', [
  7968. 'type' => 'radio',
  7969. 'options' => ['A', 'B'],
  7970. 'value' => 1,
  7971. 'labelOptions' => ['class' => 'custom-class'],
  7972. ]);
  7973. $expected = [
  7974. ['div' => ['class' => 'input radio']],
  7975. '<label',
  7976. 'Test',
  7977. '/label',
  7978. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7979. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7980. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7981. 'A',
  7982. '/label',
  7983. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  7984. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7985. 'B',
  7986. '/label',
  7987. '/div',
  7988. ];
  7989. $this->assertHtml($expected, $result);
  7990. $result = $this->Form->control('test', [
  7991. 'type' => 'radio',
  7992. 'options' => ['A', 'B'],
  7993. 'value' => 1,
  7994. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  7995. ]);
  7996. $expected = [
  7997. ['div' => ['class' => 'input radio']],
  7998. '<label',
  7999. 'Test',
  8000. '/label',
  8001. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  8002. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8003. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8004. 'A',
  8005. '/label',
  8006. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8007. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8008. 'B',
  8009. '/label',
  8010. '/div',
  8011. ];
  8012. $this->assertHtml($expected, $result);
  8013. $result = $this->Form->radio('test', ['A', 'B'], [
  8014. 'label' => [
  8015. 'class' => ['custom-class', 'another-class'],
  8016. 'data-name' => 'bob',
  8017. ],
  8018. 'value' => 1,
  8019. ]);
  8020. $expected = [
  8021. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test'],
  8022. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8023. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8024. 'A',
  8025. '/label',
  8026. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8027. ['input' => [
  8028. 'type' => 'radio',
  8029. 'name' => 'test',
  8030. 'value' => '1',
  8031. 'id' => 'test-1',
  8032. 'checked' => 'checked',
  8033. ]],
  8034. 'B',
  8035. '/label',
  8036. ];
  8037. $this->assertHtml($expected, $result);
  8038. }
  8039. /**
  8040. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8041. *
  8042. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8043. * added to the class if checked.
  8044. *
  8045. * Also checks to make sure any custom attributes are rendered correctly
  8046. */
  8047. public function testControlLabelManipulationCheckboxes(): void
  8048. {
  8049. $result = $this->Form->control('checkbox1', [
  8050. 'label' => 'My checkboxes',
  8051. 'multiple' => 'checkbox',
  8052. 'type' => 'select',
  8053. 'options' => [
  8054. ['text' => 'First Checkbox', 'value' => 1],
  8055. ['text' => 'Second Checkbox', 'value' => 2],
  8056. ],
  8057. 'labelOptions' => ['class' => 'custom-class'],
  8058. 'value' => ['1'],
  8059. ]);
  8060. $expected = [
  8061. ['div' => ['class' => 'input select']],
  8062. ['label' => ['for' => 'checkbox1']],
  8063. 'My checkboxes',
  8064. '/label',
  8065. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  8066. ['div' => ['class' => 'checkbox']],
  8067. ['label' => [
  8068. 'class' => 'custom-class selected',
  8069. 'for' => 'checkbox1-1',
  8070. ]],
  8071. ['input' => [
  8072. 'type' => 'checkbox',
  8073. 'name' => 'checkbox1[]',
  8074. 'value' => '1',
  8075. 'id' => 'checkbox1-1',
  8076. 'checked' => 'checked',
  8077. ]],
  8078. 'First Checkbox',
  8079. '/label',
  8080. '/div',
  8081. ['div' => ['class' => 'checkbox']],
  8082. ['label' => [
  8083. 'class' => 'custom-class',
  8084. 'for' => 'checkbox1-2',
  8085. ]],
  8086. ['input' => [
  8087. 'type' => 'checkbox',
  8088. 'name' => 'checkbox1[]',
  8089. 'value' => '2',
  8090. 'id' => 'checkbox1-2',
  8091. ]],
  8092. 'Second Checkbox',
  8093. '/label',
  8094. '/div',
  8095. '/div',
  8096. ];
  8097. $this->assertHtml($expected, $result);
  8098. $result = $this->Form->control('checkbox1', [
  8099. 'label' => 'My checkboxes',
  8100. 'multiple' => 'checkbox',
  8101. 'type' => 'select',
  8102. 'options' => [
  8103. ['text' => 'First Checkbox', 'value' => 1],
  8104. ['text' => 'Second Checkbox', 'value' => 2],
  8105. ],
  8106. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8107. 'value' => ['1'],
  8108. ]);
  8109. $expected = [
  8110. ['div' => ['class' => 'input select']],
  8111. ['label' => ['for' => 'checkbox1']],
  8112. 'My checkboxes',
  8113. '/label',
  8114. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  8115. ['div' => ['class' => 'checkbox']],
  8116. ['label' => [
  8117. 'class' => 'custom-class another-class selected',
  8118. 'data-name' => 'bob',
  8119. 'for' => 'checkbox1-1',
  8120. ]],
  8121. ['input' => [
  8122. 'type' => 'checkbox',
  8123. 'name' => 'checkbox1[]',
  8124. 'value' => '1',
  8125. 'id' => 'checkbox1-1',
  8126. 'checked' => 'checked',
  8127. ]],
  8128. 'First Checkbox',
  8129. '/label',
  8130. '/div',
  8131. ['div' => ['class' => 'checkbox']],
  8132. ['label' => [
  8133. 'class' => 'custom-class another-class',
  8134. 'data-name' => 'bob',
  8135. 'for' => 'checkbox1-2',
  8136. ]],
  8137. ['input' => [
  8138. 'type' => 'checkbox',
  8139. 'name' => 'checkbox1[]',
  8140. 'value' => '2',
  8141. 'id' => 'checkbox1-2',
  8142. ]],
  8143. 'Second Checkbox',
  8144. '/label',
  8145. '/div',
  8146. '/div',
  8147. ];
  8148. $this->assertHtml($expected, $result);
  8149. }
  8150. /**
  8151. * testControlMaxLengthArrayContext method
  8152. *
  8153. * Test control() with maxlength attribute in Array Context.
  8154. */
  8155. public function testControlMaxLengthArrayContext(): void
  8156. {
  8157. $this->article['schema'] = [
  8158. 'title' => ['length' => 10],
  8159. ];
  8160. $this->Form->create($this->article);
  8161. $result = $this->Form->control('title');
  8162. $expected = [
  8163. 'div' => ['class'],
  8164. 'label' => ['for'],
  8165. 'Title',
  8166. '/label',
  8167. 'input' => [
  8168. 'id',
  8169. 'name' => 'title',
  8170. 'type' => 'text',
  8171. 'required' => 'required',
  8172. 'maxlength' => 10,
  8173. 'data-validity-message' => 'This field cannot be left empty',
  8174. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8175. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8176. ],
  8177. '/div',
  8178. ];
  8179. $this->assertHtml($expected, $result);
  8180. }
  8181. /**
  8182. * testControlMaxLengthEntityContext method
  8183. *
  8184. * Test control() with maxlength attribute in Entity Context.
  8185. */
  8186. public function testControlMaxLengthEntityContext(): void
  8187. {
  8188. $this->article['schema']['title']['length'] = 45;
  8189. $validator = new Validator();
  8190. $validator->maxLength('title', 10);
  8191. $article = new EntityContext(
  8192. [
  8193. 'entity' => new Entity($this->article),
  8194. 'table' => new Table([
  8195. 'alias' => 'Articles',
  8196. 'schema' => $this->article['schema'],
  8197. 'validator' => $validator,
  8198. ]),
  8199. ]
  8200. );
  8201. $this->Form->create($article);
  8202. $result = $this->Form->control('title');
  8203. $expected = [
  8204. 'div' => ['class'],
  8205. 'label' => ['for'],
  8206. 'Title',
  8207. '/label',
  8208. 'input' => [
  8209. 'id',
  8210. 'name' => 'title',
  8211. 'type' => 'text',
  8212. 'required' => 'required',
  8213. 'maxlength' => 10,
  8214. 'data-validity-message' => 'This field cannot be left empty',
  8215. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8216. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8217. ],
  8218. '/div',
  8219. ];
  8220. $this->assertHtml($expected, $result);
  8221. $this->article['schema']['title']['length'] = 45;
  8222. $validator = new Validator();
  8223. $validator->maxLength('title', 55);
  8224. $article = new EntityContext(
  8225. [
  8226. 'entity' => new Entity($this->article),
  8227. 'table' => new Table([
  8228. 'schema' => $this->article['schema'],
  8229. 'validator' => $validator,
  8230. 'alias' => 'Articles',
  8231. ]),
  8232. ]
  8233. );
  8234. $this->Form->create($article);
  8235. $result = $this->Form->control('title');
  8236. $expected = [
  8237. 'div' => ['class'],
  8238. 'label' => ['for'],
  8239. 'Title',
  8240. '/label',
  8241. 'input' => [
  8242. 'id',
  8243. 'name' => 'title',
  8244. 'type' => 'text',
  8245. 'required' => 'required',
  8246. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8247. 'data-validity-message' => 'This field cannot be left empty',
  8248. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8249. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8250. ],
  8251. '/div',
  8252. ];
  8253. $this->assertHtml($expected, $result);
  8254. $this->article['schema']['title']['length'] = 45;
  8255. $validator = new Validator();
  8256. $validator->maxLength('title', 55);
  8257. $article = new EntityContext(
  8258. [
  8259. 'entity' => new Entity($this->article),
  8260. 'table' => new Table([
  8261. 'schema' => $this->article['schema'],
  8262. 'validator' => $validator,
  8263. 'alias' => 'Articles',
  8264. ]),
  8265. ]
  8266. );
  8267. $this->Form->create($article);
  8268. $result = $this->Form->control('title', ['maxlength' => 10]);
  8269. $expected = [
  8270. 'div' => ['class'],
  8271. 'label' => ['for'],
  8272. 'Title',
  8273. '/label',
  8274. 'input' => [
  8275. 'id',
  8276. 'name' => 'title',
  8277. 'type' => 'text',
  8278. 'required' => 'required',
  8279. 'maxlength' => 10, // Length set in options should take highest precedence.
  8280. 'data-validity-message' => 'This field cannot be left empty',
  8281. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8282. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8283. ],
  8284. '/div',
  8285. ];
  8286. $this->assertHtml($expected, $result);
  8287. }
  8288. /**
  8289. * testControlMinMaxLengthEntityContext method
  8290. *
  8291. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8292. */
  8293. public function testControlMinMaxLengthEntityContext(): void
  8294. {
  8295. $validator = new Validator();
  8296. $validator->maxLength('title', 10);
  8297. $article = new EntityContext(
  8298. [
  8299. 'entity' => new Entity($this->article),
  8300. 'table' => new Table([
  8301. 'alias' => 'Articles',
  8302. 'schema' => $this->article['schema'],
  8303. 'validator' => $validator,
  8304. ]),
  8305. ]
  8306. );
  8307. $this->Form->create($article);
  8308. $result = $this->Form->control('title');
  8309. $expected = [
  8310. 'div' => ['class'],
  8311. 'label' => ['for'],
  8312. 'Title',
  8313. '/label',
  8314. 'input' => [
  8315. 'id',
  8316. 'name' => 'title',
  8317. 'type' => 'text',
  8318. 'required' => 'required',
  8319. 'maxlength' => 10,
  8320. 'data-validity-message' => 'This field cannot be left empty',
  8321. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8322. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8323. ],
  8324. '/div',
  8325. ];
  8326. $this->assertHtml($expected, $result);
  8327. }
  8328. /**
  8329. * testControlMaxLengthFormContext method
  8330. *
  8331. * Test control() with maxlength attribute in Form Context.
  8332. */
  8333. public function testControlMaxLengthFormContext(): void
  8334. {
  8335. $validator = new Validator();
  8336. $validator->maxLength('title', 10);
  8337. $form = new Form();
  8338. $form->setValidator('default', $validator);
  8339. $this->Form->create($form);
  8340. $result = $this->Form->control('title');
  8341. $expected = [
  8342. 'div' => ['class'],
  8343. 'label' => ['for'],
  8344. 'Title',
  8345. '/label',
  8346. 'input' => [
  8347. 'id',
  8348. 'name' => 'title',
  8349. 'type' => 'text',
  8350. 'required' => 'required',
  8351. 'maxlength' => 10,
  8352. 'data-validity-message' => 'This field cannot be left empty',
  8353. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8354. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8355. ],
  8356. '/div',
  8357. ];
  8358. $this->assertHtml($expected, $result);
  8359. }
  8360. }