FormHelperTest.php 313 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104
  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\Enum\Priority;
  44. use TestApp\Model\Table\ContactsTable;
  45. use TestApp\Model\Table\ValidateUsersTable;
  46. use TestApp\View\Form\StubContext;
  47. /**
  48. * FormHelperTest class
  49. *
  50. * @property \Cake\View\Helper\FormHelper $Form
  51. * @property \Cake\View\View $View
  52. */
  53. class FormHelperTest extends TestCase
  54. {
  55. /**
  56. * Fixtures to be used
  57. *
  58. * @var array<string>
  59. */
  60. protected array $fixtures = ['core.Articles', 'core.Comments'];
  61. /**
  62. * @var array
  63. */
  64. protected $article = [];
  65. /**
  66. * @var string
  67. */
  68. protected $url;
  69. /**
  70. * @var \Cake\View\Helper\FormHelper
  71. */
  72. protected $Form;
  73. /**
  74. * @var \Cake\View\View
  75. */
  76. protected $View;
  77. /**
  78. * setUp method
  79. */
  80. public function setUp(): void
  81. {
  82. parent::setUp();
  83. Configure::write('Config.language', 'eng');
  84. Configure::write('App.base', '');
  85. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  86. $request = new ServerRequest([
  87. 'webroot' => '',
  88. 'base' => '',
  89. 'url' => '/articles/add',
  90. 'params' => [
  91. 'controller' => 'Articles',
  92. 'action' => 'add',
  93. 'plugin' => null,
  94. ],
  95. ]);
  96. $this->View = new View($request);
  97. Router::reload();
  98. Router::setRequest($request);
  99. $this->url = '/articles/add';
  100. $this->Form = new FormHelper($this->View);
  101. $this->article = [
  102. 'schema' => [
  103. 'id' => ['type' => 'integer'],
  104. 'author_id' => ['type' => 'integer', 'null' => true],
  105. 'title' => ['type' => 'string', 'null' => true],
  106. 'body' => 'text',
  107. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  108. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  109. ],
  110. 'required' => [
  111. 'author_id' => true,
  112. 'title' => true,
  113. ],
  114. ];
  115. Security::setSalt('foo!');
  116. $builder = Router::createRouteBuilder('/');
  117. $builder->connect('/{controller}', ['action' => 'index']);
  118. $builder->connect('/{controller}/{action}/*');
  119. }
  120. /**
  121. * tearDown method
  122. */
  123. public function tearDown(): void
  124. {
  125. parent::tearDown();
  126. unset($this->Form, $this->View);
  127. }
  128. /**
  129. * Test construct() with the templates option.
  130. */
  131. public function testConstructTemplatesFile(): void
  132. {
  133. $helper = new FormHelper($this->View, [
  134. 'templates' => 'htmlhelper_tags',
  135. ]);
  136. $result = $helper->control('name');
  137. $this->assertStringContainsString('<input', $result);
  138. }
  139. /**
  140. * Test that when specifying custom widgets the config array for that widget
  141. * is overwritten instead of merged.
  142. */
  143. public function testConstructWithWidgets(): void
  144. {
  145. $config = [
  146. 'widgets' => [
  147. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  148. ],
  149. ];
  150. $helper = new FormHelper($this->View, $config);
  151. $locator = $helper->getWidgetLocator();
  152. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  153. }
  154. /**
  155. * Test that when specifying custom widgets config file and it should be
  156. * added to widgets array. WidgetLocator will load widgets in constructor.
  157. */
  158. public function testConstructWithWidgetsConfig(): void
  159. {
  160. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  161. $locator = $helper->getWidgetLocator();
  162. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  163. }
  164. /**
  165. * Test setting the widget locator
  166. */
  167. public function testSetAndGetWidgetLocator(): void
  168. {
  169. $helper = new FormHelper($this->View);
  170. $locator = new WidgetLocator($helper->templater(), $this->View);
  171. $helper->setWidgetLocator($locator);
  172. $this->assertSame($locator, $helper->getWidgetLocator());
  173. }
  174. /**
  175. * Test overridding grouped input types which controls generation of "for"
  176. * attribute of labels.
  177. */
  178. public function testConstructWithGroupedInputTypes(): void
  179. {
  180. $helper = new FormHelper($this->View, [
  181. 'groupedInputTypes' => ['radio'],
  182. ]);
  183. $result = $helper->control('when', ['type' => 'datetime-local']);
  184. $this->assertStringContainsString('<label for="when">When</label>', $result);
  185. }
  186. /**
  187. * Test registering a new widget class and rendering it.
  188. */
  189. public function testAddWidgetAndRenderWidget(): void
  190. {
  191. $data = [
  192. 'val' => 1,
  193. ];
  194. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  195. $this->Form->addWidget('test', $mock);
  196. $mock->expects($this->once())
  197. ->method('render')
  198. ->with($data)
  199. ->willReturn('HTML');
  200. $result = $this->Form->widget('test', $data);
  201. $this->assertSame('HTML', $result);
  202. }
  203. /**
  204. * Test that secureFields() of widget is called after calling render(),
  205. * not before.
  206. */
  207. public function testOrderForRenderingWidgetAndFetchingSecureFields(): void
  208. {
  209. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  210. 'unlockedFields' => [],
  211. ]));
  212. $data = [
  213. 'val' => 1,
  214. 'name' => 'test',
  215. ];
  216. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  217. $this->Form->addWidget('test', $mock);
  218. $mock->expects($this->once())
  219. ->method('render')
  220. ->with($data)
  221. ->willReturn('HTML');
  222. $mock->expects($this->once())
  223. ->method('secureFields')
  224. ->with($data)
  225. ->willReturn(['test']);
  226. $this->Form->create();
  227. $result = $this->Form->widget('test', $data + ['secure' => true]);
  228. $this->assertSame('HTML', $result);
  229. }
  230. /**
  231. * Test that empty string is not added to secure fields list when
  232. * rendering input widget without name.
  233. */
  234. public function testRenderingWidgetWithEmptyName(): void
  235. {
  236. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  237. $this->Form->create();
  238. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  239. $this->assertSame('<select name=""></select>', $result);
  240. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  241. $this->assertEquals([], $result);
  242. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  243. $this->assertSame('<select name="0"></select>', $result);
  244. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  245. $this->assertEquals(['0'], $result);
  246. }
  247. /**
  248. * Test adding a new context class.
  249. */
  250. public function testAddContextProvider(): void
  251. {
  252. $context = 'My data';
  253. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  254. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  255. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  256. $this->assertSame($context, $data['entity']);
  257. return $stub;
  258. });
  259. $this->Form->create($context);
  260. $result = $this->Form->context();
  261. $this->assertSame($stub, $result);
  262. }
  263. /**
  264. * Test replacing a context class.
  265. */
  266. public function testAddContextProviderReplace(): void
  267. {
  268. $entity = new Article();
  269. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  270. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  271. return $stub;
  272. });
  273. $this->Form->create($entity);
  274. $result = $this->Form->context();
  275. $this->assertSame($stub, $result);
  276. }
  277. /**
  278. * Test overriding a context class.
  279. */
  280. public function testAddContextProviderAdd(): void
  281. {
  282. $entity = new Article();
  283. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  284. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  285. if ($data['entity'] instanceof Entity) {
  286. return $stub;
  287. }
  288. });
  289. $this->Form->create($entity);
  290. $result = $this->Form->context();
  291. $this->assertSame($stub, $result);
  292. }
  293. /**
  294. * Provides context options for create().
  295. *
  296. * @return array
  297. */
  298. public static function contextSelectionProvider(): array
  299. {
  300. $entity = new Article();
  301. $collection = new Collection([$entity]);
  302. $emptyCollection = new Collection([]);
  303. $data = [
  304. 'schema' => [
  305. 'title' => ['type' => 'string'],
  306. ],
  307. ];
  308. $form = new Form();
  309. $custom = new StubContext();
  310. return [
  311. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  312. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  313. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  314. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  315. 'form' => [$form, 'Cake\View\Form\FormContext'],
  316. 'none' => [null, 'Cake\View\Form\NullContext'],
  317. 'custom' => [$custom, $custom::class],
  318. ];
  319. }
  320. /**
  321. * Test default context selection in create()
  322. *
  323. * @dataProvider contextSelectionProvider
  324. * @param mixed $data
  325. */
  326. public function testCreateContextSelectionBuiltIn($data, string $class): void
  327. {
  328. $this->Form->create($data);
  329. $this->assertInstanceOf($class, $this->Form->context());
  330. }
  331. /**
  332. * Data provider for type option.
  333. *
  334. * @return array
  335. */
  336. public static function requestTypeProvider(): array
  337. {
  338. return [
  339. // type, method, override
  340. ['post', 'post', 'POST'],
  341. ['put', 'post', 'PUT'],
  342. ['patch', 'post', 'PATCH'],
  343. ['delete', 'post', 'DELETE'],
  344. ];
  345. }
  346. /**
  347. * Test creating file forms.
  348. */
  349. public function testCreateFile(): void
  350. {
  351. $encoding = strtolower(Configure::read('App.encoding'));
  352. $result = $this->Form->create(null, ['type' => 'file']);
  353. $expected = [
  354. 'form' => [
  355. 'method' => 'post', 'action' => '/articles/add',
  356. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  357. ],
  358. 'div' => ['style' => 'display:none;'],
  359. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  360. '/div',
  361. ];
  362. $this->assertHtml($expected, $result);
  363. }
  364. /**
  365. * Test creating GET forms.
  366. */
  367. public function testCreateGet(): void
  368. {
  369. $encoding = strtolower(Configure::read('App.encoding'));
  370. $result = $this->Form->create(null, ['type' => 'get']);
  371. $expected = ['form' => [
  372. 'method' => 'get', 'action' => '/articles/add',
  373. 'accept-charset' => $encoding,
  374. ]];
  375. $this->assertHtml($expected, $result);
  376. }
  377. /**
  378. * Test explicit method/enctype options.
  379. *
  380. * Explicit method overwrites inferred method from 'type'
  381. */
  382. public function testCreateExplicitMethodEnctype(): void
  383. {
  384. $encoding = strtolower(Configure::read('App.encoding'));
  385. $result = $this->Form->create(null, [
  386. 'type' => 'get',
  387. 'method' => 'put',
  388. 'enctype' => 'multipart/form-data',
  389. ]);
  390. $expected = ['form' => [
  391. 'method' => 'put',
  392. 'action' => '/articles/add',
  393. 'enctype' => 'multipart/form-data',
  394. 'accept-charset' => $encoding,
  395. ]];
  396. $this->assertHtml($expected, $result);
  397. }
  398. /**
  399. * Test create() with the templates option.
  400. */
  401. public function testCreateTemplatesArray(): void
  402. {
  403. $result = $this->Form->create($this->article, [
  404. 'templates' => [
  405. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  406. ],
  407. ]);
  408. $expected = [
  409. 'form' => [
  410. 'class' => 'form-horizontal',
  411. 'method' => 'post',
  412. 'action' => '/articles/add',
  413. 'accept-charset' => 'utf-8',
  414. ],
  415. ];
  416. $this->assertHtml($expected, $result);
  417. }
  418. /**
  419. * Test create() with the templates option.
  420. */
  421. public function testCreateTemplatesFile(): void
  422. {
  423. $result = $this->Form->create($this->article, [
  424. 'templates' => 'htmlhelper_tags',
  425. ]);
  426. $expected = [
  427. 'start form',
  428. ];
  429. $this->assertHtml($expected, $result);
  430. }
  431. /**
  432. * Test that create() and end() restore templates.
  433. */
  434. public function testCreateEndRestoreTemplates(): void
  435. {
  436. $this->Form->create($this->article, [
  437. 'templates' => ['input' => 'custom input element'],
  438. ]);
  439. $this->Form->end();
  440. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  441. }
  442. /**
  443. * Test create() with the templates option.
  444. */
  445. public function testCreateTemplatesRequiredClass(): void
  446. {
  447. $this->Form->create($this->article, [
  448. 'templates' => [
  449. 'requiredClass' => 'is-required',
  450. ],
  451. ]);
  452. $result = $this->Form->control('title');
  453. $expected = [
  454. 'div' => ['class' => 'input text is-required'],
  455. 'label' => ['for' => 'title'],
  456. 'Title',
  457. '/label',
  458. 'input' => [
  459. 'type' => 'text',
  460. 'name' => 'title',
  461. 'id' => 'title',
  462. 'required' => 'required',
  463. 'data-validity-message' => 'This field cannot be left empty',
  464. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  465. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  466. 'aria-required' => 'true',
  467. ],
  468. '/div',
  469. ];
  470. $this->assertHtml($expected, $result);
  471. }
  472. /**
  473. * Test using template vars in various templates used by control() method.
  474. */
  475. public function testControlTemplateVars(): void
  476. {
  477. $result = $this->Form->control('text', [
  478. 'templates' => [
  479. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  480. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  481. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  482. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  483. ],
  484. 'templateVars' => [
  485. 'forinput' => 'in-input',
  486. 'forlabel' => 'in-label',
  487. 'forgroup' => 'in-group',
  488. 'forcontainer' => 'in-container',
  489. ],
  490. ]);
  491. $expected = [
  492. 'div' => ['class'],
  493. 'label' => ['for'],
  494. 'Text in-label',
  495. '/label',
  496. 'in-group',
  497. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  498. 'in-container',
  499. '/div',
  500. ];
  501. $this->assertHtml($expected, $result);
  502. }
  503. /**
  504. * Test ensuring template variables work in template files loaded
  505. * during control().
  506. */
  507. public function testControlTemplatesFromFile(): void
  508. {
  509. $result = $this->Form->control('title', [
  510. 'templates' => 'test_templates',
  511. 'templateVars' => [
  512. 'forcontainer' => 'container-data',
  513. ],
  514. ]);
  515. $expected = [
  516. 'div' => ['class'],
  517. 'label' => ['for'],
  518. 'Title',
  519. '/label',
  520. 'input' => ['name', 'type' => 'text', 'id'],
  521. 'container-data',
  522. '/div',
  523. ];
  524. $this->assertHtml($expected, $result);
  525. }
  526. /**
  527. * Test using template vars in inputSubmit and submitContainer template.
  528. */
  529. public function testSubmitTemplateVars(): void
  530. {
  531. $this->Form->setTemplates([
  532. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  533. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  534. ]);
  535. $result = $this->Form->submit('Submit', [
  536. 'templateVars' => [
  537. 'forinput' => 'in-input',
  538. 'forcontainer' => 'in-container',
  539. ],
  540. ]);
  541. $expected = [
  542. 'div' => ['class'],
  543. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  544. 'in-container',
  545. '/div',
  546. ];
  547. $this->assertHtml($expected, $result);
  548. }
  549. /**
  550. * test the create() method
  551. *
  552. * @dataProvider requestTypeProvider
  553. */
  554. public function testCreateTypeOptions(string $type, string $method, string $override): void
  555. {
  556. $encoding = strtolower(Configure::read('App.encoding'));
  557. $result = $this->Form->create(null, ['type' => $type]);
  558. $expected = [
  559. 'form' => [
  560. 'method' => $method, 'action' => '/articles/add',
  561. 'accept-charset' => $encoding,
  562. ],
  563. ];
  564. $extra = [
  565. 'div' => ['style' => 'display:none;'],
  566. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  567. '/div',
  568. ];
  569. if ($type !== 'post') {
  570. $expected = array_merge($expected, $extra);
  571. }
  572. $this->assertHtml($expected, $result);
  573. }
  574. /**
  575. * Test using template vars in Create (formStart template)
  576. */
  577. public function testCreateTemplateVars(): void
  578. {
  579. $result = $this->Form->create($this->article, [
  580. 'templates' => [
  581. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  582. ],
  583. 'templateVars' => ['header' => 'headertext'],
  584. ]);
  585. $expected = [
  586. 'h4' => ['class'],
  587. 'headertext',
  588. '/h4',
  589. 'form' => [
  590. 'method' => 'post',
  591. 'action' => '/articles/add',
  592. 'accept-charset' => 'utf-8',
  593. ],
  594. ];
  595. $this->assertHtml($expected, $result);
  596. }
  597. /**
  598. * Test opening a form for an update operation.
  599. */
  600. public function testCreateUpdateForm(): void
  601. {
  602. $encoding = strtolower(Configure::read('App.encoding'));
  603. $this->View->setRequest($this->View->getRequest()
  604. ->withRequestTarget('/articles/edit/1')
  605. ->withParam('action', 'edit'));
  606. $this->article['defaults']['id'] = 1;
  607. $result = $this->Form->create($this->article);
  608. $expected = [
  609. 'form' => [
  610. 'method' => 'post',
  611. 'action' => '/articles/edit/1',
  612. 'accept-charset' => $encoding,
  613. ],
  614. 'div' => ['style' => 'display:none;'],
  615. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  616. '/div',
  617. ];
  618. $this->assertHtml($expected, $result);
  619. }
  620. /**
  621. * test create() with automatic url generation
  622. */
  623. public function testCreateAutoUrl(): void
  624. {
  625. $encoding = strtolower(Configure::read('App.encoding'));
  626. $this->View->setRequest($this->View->getRequest()
  627. ->withRequestTarget('/articles/delete/10')
  628. ->withParam('action', 'delete'));
  629. $result = $this->Form->create($this->article);
  630. $expected = [
  631. 'form' => [
  632. 'method' => 'post', 'action' => '/articles/delete/10',
  633. 'accept-charset' => $encoding,
  634. ],
  635. ];
  636. $this->assertHtml($expected, $result);
  637. $this->article['defaults'] = ['id' => 1];
  638. $this->View->setRequest($this->View->getRequest()
  639. ->withRequestTarget('/Articles/edit/1')
  640. ->withParam('action', 'delete'));
  641. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  642. $expected = [
  643. 'form' => [
  644. 'method' => 'post',
  645. 'action' => '/Articles/edit/1',
  646. 'accept-charset' => $encoding,
  647. ],
  648. 'div' => ['style' => 'display:none;'],
  649. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  650. '/div',
  651. ];
  652. $this->assertHtml($expected, $result);
  653. $this->View->setRequest($this->View->getRequest()
  654. ->withParam('action', 'add'));
  655. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  656. $expected = [
  657. 'form' => [
  658. 'method' => 'post',
  659. 'action' => '/Articles/publish/1',
  660. 'accept-charset' => $encoding,
  661. ],
  662. 'div' => ['style' => 'display:none;'],
  663. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  664. '/div',
  665. ];
  666. $this->assertHtml($expected, $result);
  667. $result = $this->Form->create($this->article, ['url' => '/Articles/publish']);
  668. $expected = [
  669. 'form' => ['method' => 'post', 'action' => '/Articles/publish', 'accept-charset' => $encoding],
  670. 'div' => ['style' => 'display:none;'],
  671. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  672. '/div',
  673. ];
  674. $this->assertHtml($expected, $result);
  675. $this->View->setRequest($this->View->getRequest()
  676. ->withParam('controller', 'Pages'));
  677. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  678. $expected = [
  679. 'form' => [
  680. 'method' => 'post', 'action' => '/Pages/signup/1',
  681. 'accept-charset' => $encoding,
  682. ],
  683. 'div' => ['style' => 'display:none;'],
  684. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  685. '/div',
  686. ];
  687. $this->assertHtml($expected, $result);
  688. }
  689. /**
  690. * Test create() with no URL (no "action" attribute for <form> tag)
  691. */
  692. public function testCreateNoUrl(): void
  693. {
  694. $result = $this->Form->create(null, ['url' => false]);
  695. $expected = [
  696. 'form' => [
  697. 'method' => 'post',
  698. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  699. ],
  700. ];
  701. $this->assertHtml($expected, $result);
  702. }
  703. /**
  704. * test create() with a custom route
  705. */
  706. public function testCreateCustomRoute(): void
  707. {
  708. $builder = Router::createRouteBuilder('/');
  709. $builder->connect('/login', ['controller' => 'Users', 'action' => 'login']);
  710. $encoding = strtolower(Configure::read('App.encoding'));
  711. $this->View->setRequest($this->View->getRequest()
  712. ->withParam('controller', 'Users'));
  713. $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
  714. $expected = [
  715. 'form' => [
  716. 'method' => 'post', 'action' => '/login',
  717. 'accept-charset' => $encoding,
  718. ],
  719. ];
  720. $this->assertHtml($expected, $result);
  721. $builder->connect(
  722. '/new-article',
  723. ['controller' => 'Articles', 'action' => 'myAction'],
  724. ['_name' => 'my-route']
  725. );
  726. $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
  727. $expected = [
  728. 'form' => [
  729. 'method' => 'post', 'action' => '/new-article',
  730. 'accept-charset' => $encoding,
  731. ],
  732. ];
  733. $this->assertHtml($expected, $result);
  734. }
  735. /**
  736. * test automatic accept-charset overriding
  737. */
  738. public function testCreateWithAcceptCharset(): void
  739. {
  740. $result = $this->Form->create(
  741. $this->article,
  742. [
  743. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  744. ]
  745. );
  746. $expected = [
  747. 'form' => [
  748. 'method' => 'post', 'action' => '/Articles',
  749. 'accept-charset' => 'iso-8859-1',
  750. ],
  751. ];
  752. $this->assertHtml($expected, $result);
  753. }
  754. /**
  755. * Test base form URL when 'url' param is passed with multiple parameters (&)
  756. */
  757. public function testCreateQueryStringRequest(): void
  758. {
  759. $encoding = strtolower(Configure::read('App.encoding'));
  760. $result = $this->Form->create($this->article, [
  761. 'type' => 'post',
  762. 'escape' => false,
  763. 'url' => [
  764. 'controller' => 'Controller',
  765. 'action' => 'action',
  766. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  767. ],
  768. ]);
  769. $expected = [
  770. 'form' => [
  771. 'method' => 'post',
  772. 'action' => '/Controller/action?param1=value1&amp;param2=value2',
  773. 'accept-charset' => $encoding,
  774. ],
  775. ];
  776. $this->assertHtml($expected, $result);
  777. $result = $this->Form->create($this->article, [
  778. 'type' => 'post',
  779. 'url' => [
  780. 'controller' => 'Controller',
  781. 'action' => 'action',
  782. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  783. ],
  784. ]);
  785. $this->assertHtml($expected, $result);
  786. }
  787. /**
  788. * test that create() doesn't cause errors by multiple id's being in the primary key
  789. * as could happen with multiple select or checkboxes.
  790. */
  791. public function testCreateWithMultipleIdInData(): void
  792. {
  793. $encoding = strtolower(Configure::read('App.encoding'));
  794. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  795. $result = $this->Form->create($this->article);
  796. $expected = [
  797. 'form' => [
  798. 'method' => 'post',
  799. 'action' => '/articles/add',
  800. 'accept-charset' => $encoding,
  801. ],
  802. ];
  803. $this->assertHtml($expected, $result);
  804. }
  805. /**
  806. * test that create() doesn't add in extra passed params.
  807. */
  808. public function testCreatePassedArgs(): void
  809. {
  810. $encoding = strtolower(Configure::read('App.encoding'));
  811. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  812. $result = $this->Form->create($this->article, [
  813. 'type' => 'post',
  814. 'escape' => false,
  815. 'url' => [
  816. 'action' => 'edit',
  817. 'myparam',
  818. ],
  819. ]);
  820. $expected = [
  821. 'form' => [
  822. 'method' => 'post',
  823. 'action' => '/Articles/edit/myparam',
  824. 'accept-charset' => $encoding,
  825. ],
  826. ];
  827. $this->assertHtml($expected, $result);
  828. }
  829. /**
  830. * test creating a get form, and get form inputs.
  831. */
  832. public function testGetFormCreate(): void
  833. {
  834. $encoding = strtolower(Configure::read('App.encoding'));
  835. $result = $this->Form->create($this->article, ['type' => 'get']);
  836. $expected = ['form' => [
  837. 'method' => 'get', 'action' => '/articles/add',
  838. 'accept-charset' => $encoding,
  839. ]];
  840. $this->assertHtml($expected, $result);
  841. $result = $this->Form->text('title');
  842. $expected = ['input' => [
  843. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  844. ]];
  845. $this->assertHtml($expected, $result);
  846. $result = $this->Form->password('password');
  847. $expected = ['input' => [
  848. 'name' => 'password', 'type' => 'password',
  849. ]];
  850. $this->assertHtml($expected, $result);
  851. $this->assertDoesNotMatchRegularExpression('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  852. $result = $this->Form->text('user_form');
  853. $expected = ['input' => [
  854. 'name' => 'user_form', 'type' => 'text',
  855. ]];
  856. $this->assertHtml($expected, $result);
  857. }
  858. /**
  859. * test get form, and inputs when the model param is false
  860. */
  861. public function testGetFormWithFalseModel(): void
  862. {
  863. $encoding = strtolower(Configure::read('App.encoding'));
  864. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'ContactTest'));
  865. $result = $this->Form->create(null, [
  866. 'type' => 'get', 'url' => ['controller' => 'ContactTest'],
  867. ]);
  868. $expected = ['form' => [
  869. 'method' => 'get', 'action' => '/ContactTest/add',
  870. 'accept-charset' => $encoding,
  871. ]];
  872. $this->assertHtml($expected, $result);
  873. $result = $this->Form->text('reason');
  874. $expected = [
  875. 'input' => ['type' => 'text', 'name' => 'reason'],
  876. ];
  877. $this->assertHtml($expected, $result);
  878. }
  879. /**
  880. * testFormCreateWithSecurity method
  881. *
  882. * Test form->create() with security key.
  883. */
  884. public function testCreateWithSecurity(): void
  885. {
  886. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  887. $encoding = strtolower(Configure::read('App.encoding'));
  888. $result = $this->Form->create($this->article, [
  889. 'url' => '/articles/publish',
  890. ]);
  891. $expected = [
  892. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  893. 'div' => ['style' => 'display:none;'],
  894. ['input' => [
  895. 'type' => 'hidden',
  896. 'name' => '_csrfToken',
  897. 'value' => 'testKey',
  898. ]],
  899. '/div',
  900. ];
  901. $this->assertHtml($expected, $result);
  902. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  903. $expected['form']['id'] = 'MyForm';
  904. $this->assertHtml($expected, $result);
  905. }
  906. /**
  907. * testFormCreateGetNoSecurity method
  908. *
  909. * Test form->create() with no security key as its a get form
  910. */
  911. public function testCreateEndGetNoSecurity(): void
  912. {
  913. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  914. $article = new Article();
  915. $result = $this->Form->create($article, [
  916. 'type' => 'get',
  917. 'url' => '/contacts/add',
  918. ]);
  919. $this->assertStringNotContainsString('testKey', $result);
  920. $result = $this->Form->end();
  921. $this->assertStringNotContainsString('testKey', $result);
  922. }
  923. /**
  924. * Tests form hash generation with model-less data
  925. */
  926. public function testValidateHashNoModel(): void
  927. {
  928. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  929. $fields = ['anything'];
  930. $this->Form->create();
  931. $result = $this->Form->secure($fields);
  932. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  933. $this->assertStringContainsString($hash, $result);
  934. }
  935. /**
  936. * Tests that hidden fields generated for checkboxes don't get locked
  937. */
  938. public function testNoCheckboxLocking(): void
  939. {
  940. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  941. $this->Form->create();
  942. $this->assertSame([], $this->Form->getFormProtector()->__debugInfo()['fields']);
  943. $this->Form->checkbox('check', ['value' => '1']);
  944. $this->assertSame(['check'], $this->Form->getFormProtector()->__debugInfo()['fields']);
  945. }
  946. /**
  947. * testFormSecurityFields method
  948. *
  949. * Test generation of secure form hash generation.
  950. */
  951. public function testFormSecurityFields(): void
  952. {
  953. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  954. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  955. $this->Form->create();
  956. $result = $this->Form->secure($fields);
  957. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  958. $hash .= ':' . 'Model.valid';
  959. $hash = urlencode($hash);
  960. $tokenDebug = urlencode(json_encode([
  961. $this->url,
  962. $fields,
  963. [],
  964. ]));
  965. $expected = [
  966. 'div' => ['style' => 'display:none;'],
  967. ['input' => [
  968. 'type' => 'hidden',
  969. 'name' => '_Token[fields]',
  970. 'value' => $hash,
  971. ]],
  972. ['input' => [
  973. 'type' => 'hidden',
  974. 'name' => '_Token[unlocked]',
  975. 'value' => '',
  976. ]],
  977. ['input' => [
  978. 'type' => 'hidden',
  979. 'name' => '_Token[debug]',
  980. 'value' => $tokenDebug,
  981. ]],
  982. '/div',
  983. ];
  984. $this->assertHtml($expected, $result);
  985. }
  986. /**
  987. * testFormSecurityFields method
  988. *
  989. * Test debug token is not generated if debug is false
  990. */
  991. public function testFormSecurityFieldsNoDebugMode(): void
  992. {
  993. Configure::write('debug', false);
  994. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  995. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  996. $this->Form->create();
  997. $result = $this->Form->secure($fields);
  998. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  999. $hash .= ':' . 'Model.valid';
  1000. $hash = urlencode($hash);
  1001. $expected = [
  1002. 'div' => ['style' => 'display:none;'],
  1003. ['input' => [
  1004. 'type' => 'hidden',
  1005. 'name' => '_Token[fields]',
  1006. 'value' => $hash,
  1007. ]],
  1008. ['input' => [
  1009. 'type' => 'hidden',
  1010. 'name' => '_Token[unlocked]',
  1011. 'value' => '',
  1012. ]],
  1013. '/div',
  1014. ];
  1015. $this->assertHtml($expected, $result);
  1016. }
  1017. /**
  1018. * Tests correct generation of number fields for smallint
  1019. */
  1020. public function testTextFieldGenerationForSmallint(): void
  1021. {
  1022. $this->article['schema'] = [
  1023. 'foo' => [
  1024. 'type' => 'smallinteger',
  1025. 'null' => false,
  1026. 'default' => null,
  1027. 'length' => 10,
  1028. ],
  1029. ];
  1030. $this->Form->create($this->article);
  1031. $result = $this->Form->control('foo');
  1032. $this->assertStringContainsString('class="input number"', $result);
  1033. $this->assertStringContainsString('type="number"', $result);
  1034. }
  1035. /**
  1036. * Tests correct generation of number fields for tinyint
  1037. */
  1038. public function testTextFieldGenerationForTinyint(): void
  1039. {
  1040. $this->article['schema'] = [
  1041. 'foo' => [
  1042. 'type' => 'tinyinteger',
  1043. 'null' => false,
  1044. 'default' => null,
  1045. 'length' => 10,
  1046. ],
  1047. ];
  1048. $this->Form->create($this->article);
  1049. $result = $this->Form->control('foo');
  1050. $this->assertStringContainsString('class="input number"', $result);
  1051. $this->assertStringContainsString('type="number"', $result);
  1052. }
  1053. /**
  1054. * Tests correct generation of number fields for double and float fields
  1055. */
  1056. public function testTextFieldGenerationForFloats(): void
  1057. {
  1058. $this->article['schema'] = [
  1059. 'foo' => [
  1060. 'type' => 'float',
  1061. 'null' => false,
  1062. 'default' => null,
  1063. 'length' => 10,
  1064. ],
  1065. ];
  1066. $this->Form->create($this->article);
  1067. $result = $this->Form->control('foo');
  1068. $expected = [
  1069. 'div' => ['class' => 'input number'],
  1070. 'label' => ['for' => 'foo'],
  1071. 'Foo',
  1072. '/label',
  1073. ['input' => [
  1074. 'type' => 'number',
  1075. 'name' => 'foo',
  1076. 'id' => 'foo',
  1077. 'step' => 'any',
  1078. ]],
  1079. '/div',
  1080. ];
  1081. $this->assertHtml($expected, $result);
  1082. $result = $this->Form->control('foo', ['step' => 0.5]);
  1083. $expected = [
  1084. 'div' => ['class' => 'input number'],
  1085. 'label' => ['for' => 'foo'],
  1086. 'Foo',
  1087. '/label',
  1088. ['input' => [
  1089. 'type' => 'number',
  1090. 'name' => 'foo',
  1091. 'id' => 'foo',
  1092. 'step' => '0.5',
  1093. ]],
  1094. '/div',
  1095. ];
  1096. $this->assertHtml($expected, $result);
  1097. }
  1098. /**
  1099. * Tests correct generation of number fields for integer fields
  1100. */
  1101. public function testTextFieldTypeNumberGenerationForIntegers(): void
  1102. {
  1103. $this->getTableLocator()->get('Contacts', [
  1104. 'className' => ContactsTable::class,
  1105. ]);
  1106. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1107. $result = $this->Form->control('age');
  1108. $expected = [
  1109. 'div' => ['class' => 'input number'],
  1110. 'label' => ['for' => 'age'],
  1111. 'Age',
  1112. '/label',
  1113. ['input' => [
  1114. 'type' => 'number', 'name' => 'age',
  1115. 'id' => 'age',
  1116. ]],
  1117. '/div',
  1118. ];
  1119. $this->assertHtml($expected, $result);
  1120. }
  1121. /**
  1122. * Tests correct generation of file upload fields for binary fields
  1123. */
  1124. public function testFileUploadFieldTypeGenerationForBinaries(): void
  1125. {
  1126. $table = $this->getTableLocator()->get('Contacts', [
  1127. 'className' => ContactsTable::class,
  1128. ]);
  1129. $table->setSchema(['foo' => [
  1130. 'type' => 'binary',
  1131. 'null' => false,
  1132. 'default' => null,
  1133. 'length' => 1024,
  1134. ]]);
  1135. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1136. $result = $this->Form->control('foo');
  1137. $expected = [
  1138. 'div' => ['class' => 'input file'],
  1139. 'label' => ['for' => 'foo'],
  1140. 'Foo',
  1141. '/label',
  1142. ['input' => [
  1143. 'type' => 'file', 'name' => 'foo',
  1144. 'id' => 'foo',
  1145. ]],
  1146. '/div',
  1147. ];
  1148. $this->assertHtml($expected, $result);
  1149. }
  1150. /**
  1151. * testFormSecurityMultipleFields method
  1152. *
  1153. * Test secure() with multiple row form. Ensure hash is correct.
  1154. */
  1155. public function testFormSecurityMultipleFields(): void
  1156. {
  1157. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1158. $this->Form->create();
  1159. $fields = [
  1160. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1161. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1162. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1163. ];
  1164. $result = $this->Form->secure($fields);
  1165. $sortedFields = [
  1166. 'Model.0.password',
  1167. 'Model.0.username',
  1168. 'Model.1.password',
  1169. 'Model.1.username',
  1170. 'Model.0.hidden' => 'value',
  1171. 'Model.0.valid' => '0',
  1172. 'Model.1.hidden' => 'value',
  1173. 'Model.1.valid' => '0',
  1174. ];
  1175. $hash = hash_hmac('sha1', $this->url . serialize($sortedFields) . session_id(), Security::getSalt());
  1176. $hash .= ':Model.0.hidden|Model.0.valid|Model.1.hidden|Model.1.valid';
  1177. $hash = urlencode($hash);
  1178. $tokenDebug = urlencode(json_encode([
  1179. $this->url,
  1180. $fields,
  1181. [],
  1182. ]));
  1183. $expected = [
  1184. 'div' => ['style' => 'display:none;'],
  1185. ['input' => [
  1186. 'type' => 'hidden',
  1187. 'name' => '_Token[fields]',
  1188. 'value' => $hash,
  1189. ]],
  1190. ['input' => [
  1191. 'type' => 'hidden',
  1192. 'name' => '_Token[unlocked]',
  1193. 'value' => '',
  1194. ]],
  1195. ['input' => [
  1196. 'type' => 'hidden',
  1197. 'name' => '_Token[debug]',
  1198. 'value' => $tokenDebug,
  1199. ]],
  1200. '/div',
  1201. ];
  1202. $this->assertHtml($expected, $result);
  1203. }
  1204. /**
  1205. * testFormSecurityMultipleSubmitButtons
  1206. *
  1207. * test form submit generation and ensure that _Token is only created on end()
  1208. */
  1209. public function testFormSecurityMultipleSubmitButtons(): void
  1210. {
  1211. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1212. $this->Form->create($this->article);
  1213. $this->Form->text('Address.title');
  1214. $this->Form->text('Address.first_name');
  1215. $result = $this->Form->submit('Save', ['name' => 'save']);
  1216. $expected = [
  1217. 'div' => ['class' => 'submit'],
  1218. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1219. '/div',
  1220. ];
  1221. $this->assertHtml($expected, $result);
  1222. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1223. $expected = [
  1224. 'div' => ['class' => 'submit'],
  1225. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1226. '/div',
  1227. ];
  1228. $this->assertHtml($expected, $result);
  1229. $result = $this->Form->end();
  1230. $tokenDebug = urlencode(json_encode([
  1231. '/articles/add',
  1232. [
  1233. 'Address.title',
  1234. 'Address.first_name',
  1235. ],
  1236. ['save', 'cancel'],
  1237. ]));
  1238. $expected = [
  1239. 'div' => ['style' => 'display:none;'],
  1240. ['input' => [
  1241. 'type' => 'hidden',
  1242. 'name' => '_Token[fields]',
  1243. 'value',
  1244. ]],
  1245. ['input' => [
  1246. 'type' => 'hidden',
  1247. 'name' => '_Token[unlocked]',
  1248. 'value' => 'cancel%7Csave',
  1249. ]],
  1250. ['input' => [
  1251. 'type' => 'hidden',
  1252. 'name' => '_Token[debug]',
  1253. 'value' => $tokenDebug,
  1254. ]],
  1255. '/div',
  1256. ];
  1257. $this->assertHtml($expected, $result);
  1258. }
  1259. /**
  1260. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1261. */
  1262. public function testSecurityButtonNestedNamed(): void
  1263. {
  1264. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1265. $this->Form->create();
  1266. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1267. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1268. $this->assertEquals(['Address.button'], $result);
  1269. }
  1270. /**
  1271. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1272. */
  1273. public function testSecuritySubmitNestedNamed(): void
  1274. {
  1275. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1276. $this->Form->create($this->article);
  1277. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1278. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1279. $this->assertEquals(['Address.button'], $result);
  1280. }
  1281. /**
  1282. * Test that the correct fields are unlocked for image submits with no names.
  1283. */
  1284. public function testSecuritySubmitImageNoName(): void
  1285. {
  1286. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1287. $this->Form->create();
  1288. $result = $this->Form->submit('save.png');
  1289. $expected = [
  1290. 'div' => ['class' => 'submit'],
  1291. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1292. '/div',
  1293. ];
  1294. $this->assertHtml($expected, $result);
  1295. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1296. $this->assertEquals(['x', 'y'], $result);
  1297. }
  1298. /**
  1299. * Test that the correct fields are unlocked for image submits with names.
  1300. */
  1301. public function testSecuritySubmitImageName(): void
  1302. {
  1303. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1304. $this->Form->create();
  1305. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1306. $expected = [
  1307. 'div' => ['class' => 'submit'],
  1308. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1309. '/div',
  1310. ];
  1311. $this->assertHtml($expected, $result);
  1312. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1313. $this->assertEquals(['test', 'test_x', 'test_y'], $result);
  1314. }
  1315. /**
  1316. * testFormSecurityMultipleControlFields method
  1317. *
  1318. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1319. */
  1320. public function testFormSecurityMultipleControlFields(): void
  1321. {
  1322. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1323. $this->Form->create();
  1324. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1325. $this->Form->control('Addresses.0.title');
  1326. $this->Form->control('Addresses.0.first_name');
  1327. $this->Form->control('Addresses.0.last_name');
  1328. $this->Form->control('Addresses.0.address');
  1329. $this->Form->control('Addresses.0.city');
  1330. $this->Form->control('Addresses.0.phone');
  1331. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1332. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1333. $this->Form->control('Addresses.1.title');
  1334. $this->Form->control('Addresses.1.first_name');
  1335. $this->Form->control('Addresses.1.last_name');
  1336. $this->Form->control('Addresses.1.address');
  1337. $this->Form->control('Addresses.1.city');
  1338. $this->Form->control('Addresses.1.phone');
  1339. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1340. $result = $this->Form->secure();
  1341. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1342. $tokenDebug = urlencode(json_encode([
  1343. '/articles/add',
  1344. [
  1345. 'Addresses.0.id' => '123456',
  1346. 'Addresses.0.title',
  1347. 'Addresses.0.first_name',
  1348. 'Addresses.0.last_name',
  1349. 'Addresses.0.address',
  1350. 'Addresses.0.city',
  1351. 'Addresses.0.phone',
  1352. 'Addresses.0.primary',
  1353. 'Addresses.1.id' => '654321',
  1354. 'Addresses.1.title',
  1355. 'Addresses.1.first_name',
  1356. 'Addresses.1.last_name',
  1357. 'Addresses.1.address',
  1358. 'Addresses.1.city',
  1359. 'Addresses.1.phone',
  1360. 'Addresses.1.primary',
  1361. ],
  1362. [],
  1363. ]));
  1364. $expected = [
  1365. 'div' => ['style' => 'display:none;'],
  1366. ['input' => [
  1367. 'type' => 'hidden',
  1368. 'name' => '_Token[fields]',
  1369. 'value' => $hash,
  1370. ]],
  1371. ['input' => [
  1372. 'type' => 'hidden',
  1373. 'name' => '_Token[unlocked]',
  1374. 'value' => '',
  1375. ]],
  1376. ['input' => [
  1377. 'type' => 'hidden',
  1378. 'name' => '_Token[debug]',
  1379. 'value' => $tokenDebug,
  1380. ]],
  1381. '/div',
  1382. ];
  1383. $this->assertHtml($expected, $result);
  1384. }
  1385. /**
  1386. * testFormSecurityArrayFields method
  1387. *
  1388. * Test form security with Model.field.0 style inputs.
  1389. */
  1390. public function testFormSecurityArrayFields(): void
  1391. {
  1392. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1393. $this->Form->create();
  1394. $this->Form->text('Address.primary.1');
  1395. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1396. $this->assertSame('Address.primary', $result[0]);
  1397. $this->Form->text('Address.secondary.1.0');
  1398. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1399. $this->assertSame('Address.secondary', $result[1]);
  1400. }
  1401. /**
  1402. * testFormSecurityMultipleControlDisabledFields method
  1403. *
  1404. * Test secure form generation with multiple records and disabled fields.
  1405. */
  1406. public function testFormSecurityMultipleControlDisabledFields(): void
  1407. {
  1408. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1409. 'unlockedFields' => ['first_name', 'address'],
  1410. ]));
  1411. $this->Form->create();
  1412. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1413. $this->Form->text('Addresses.0.title');
  1414. $this->Form->text('Addresses.0.first_name');
  1415. $this->Form->text('Addresses.0.last_name');
  1416. $this->Form->text('Addresses.0.address');
  1417. $this->Form->text('Addresses.0.city');
  1418. $this->Form->text('Addresses.0.phone');
  1419. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1420. $this->Form->text('Addresses.1.title');
  1421. $this->Form->text('Addresses.1.first_name');
  1422. $this->Form->text('Addresses.1.last_name');
  1423. $this->Form->text('Addresses.1.address');
  1424. $this->Form->text('Addresses.1.city');
  1425. $this->Form->text('Addresses.1.phone');
  1426. $result = $this->Form->secure();
  1427. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1428. $tokenDebug = urlencode(json_encode([
  1429. '/articles/add',
  1430. [
  1431. 'Addresses.0.id' => '123456',
  1432. 'Addresses.0.title',
  1433. 'Addresses.0.last_name',
  1434. 'Addresses.0.city',
  1435. 'Addresses.0.phone',
  1436. 'Addresses.1.id' => '654321',
  1437. 'Addresses.1.title',
  1438. 'Addresses.1.last_name',
  1439. 'Addresses.1.city',
  1440. 'Addresses.1.phone',
  1441. ],
  1442. [
  1443. 'first_name',
  1444. 'address',
  1445. ],
  1446. ]));
  1447. $expected = [
  1448. 'div' => ['style' => 'display:none;'],
  1449. ['input' => [
  1450. 'type' => 'hidden',
  1451. 'name' => '_Token[fields]',
  1452. 'value' => $hash,
  1453. ]],
  1454. ['input' => [
  1455. 'type' => 'hidden',
  1456. 'name' => '_Token[unlocked]',
  1457. 'value' => 'address%7Cfirst_name',
  1458. ]],
  1459. ['input' => [
  1460. 'type' => 'hidden',
  1461. 'name' => '_Token[debug]',
  1462. 'value' => $tokenDebug,
  1463. ]],
  1464. '/div',
  1465. ];
  1466. $this->assertHtml($expected, $result);
  1467. }
  1468. /**
  1469. * testFormSecurityControlDisabledFields method
  1470. *
  1471. * Test single record form with disabled fields.
  1472. */
  1473. public function testFormSecurityControlUnlockedFields(): void
  1474. {
  1475. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1476. 'unlockedFields' => ['first_name', 'address'],
  1477. ]));
  1478. $this->Form->create();
  1479. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1480. $this->assertEquals(
  1481. $this->View->getRequest()->getAttribute('formTokenData'),
  1482. ['unlockedFields' => $result]
  1483. );
  1484. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1485. $this->Form->text('Addresses.title');
  1486. $this->Form->text('Addresses.first_name');
  1487. $this->Form->text('Addresses.last_name');
  1488. $this->Form->text('Addresses.address');
  1489. $this->Form->text('Addresses.city');
  1490. $this->Form->text('Addresses.phone');
  1491. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1492. $expected = [
  1493. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1494. 'Addresses.city', 'Addresses.phone',
  1495. ];
  1496. $this->assertEquals($expected, $result);
  1497. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1498. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1499. $tokenDebug = urlencode(json_encode([
  1500. '/articles/add',
  1501. [
  1502. 'Addresses.id' => '123456',
  1503. 'Addresses.title',
  1504. 'Addresses.last_name',
  1505. 'Addresses.city',
  1506. 'Addresses.phone',
  1507. ],
  1508. [
  1509. 'first_name',
  1510. 'address',
  1511. ],
  1512. ]));
  1513. $expected = [
  1514. 'div' => ['style' => 'display:none;'],
  1515. ['input' => [
  1516. 'type' => 'hidden',
  1517. 'name' => '_Token[fields]',
  1518. 'value' => $hash,
  1519. 'data-foo' => 'bar',
  1520. ]],
  1521. ['input' => [
  1522. 'type' => 'hidden',
  1523. 'name' => '_Token[unlocked]',
  1524. 'value' => 'address%7Cfirst_name',
  1525. 'data-foo' => 'bar',
  1526. ]],
  1527. ['input' => [
  1528. 'type' => 'hidden', 'name' => '_Token[debug]',
  1529. 'value' => $tokenDebug,
  1530. 'data-foo' => 'bar',
  1531. ]],
  1532. '/div',
  1533. ];
  1534. $this->assertHtml($expected, $result);
  1535. }
  1536. /**
  1537. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1538. *
  1539. * Test single record form with debugSecurity param.
  1540. */
  1541. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue(): void
  1542. {
  1543. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1544. 'unlockedFields' => ['first_name', 'address'],
  1545. ]));
  1546. $this->Form->create();
  1547. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1548. $this->assertEquals(
  1549. $this->View->getRequest()->getAttribute('formTokenData'),
  1550. ['unlockedFields' => $result]
  1551. );
  1552. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1553. $this->Form->text('Addresses.title');
  1554. $this->Form->text('Addresses.first_name');
  1555. $this->Form->text('Addresses.last_name');
  1556. $this->Form->text('Addresses.address');
  1557. $this->Form->text('Addresses.city');
  1558. $this->Form->text('Addresses.phone');
  1559. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1560. $expected = [
  1561. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1562. 'Addresses.city', 'Addresses.phone',
  1563. ];
  1564. $this->assertEquals($expected, $result);
  1565. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1566. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1567. $tokenDebug = urlencode(json_encode([
  1568. '/articles/add',
  1569. [
  1570. 'Addresses.id' => '123456',
  1571. 'Addresses.title',
  1572. 'Addresses.last_name',
  1573. 'Addresses.city',
  1574. 'Addresses.phone',
  1575. ],
  1576. [
  1577. 'first_name',
  1578. 'address',
  1579. ],
  1580. ]));
  1581. $expected = [
  1582. 'div' => ['style' => 'display:none;'],
  1583. ['input' => [
  1584. 'type' => 'hidden',
  1585. 'name' => '_Token[fields]',
  1586. 'value' => $hash,
  1587. 'data-foo' => 'bar',
  1588. ]],
  1589. ['input' => [
  1590. 'type' => 'hidden',
  1591. 'name' => '_Token[unlocked]',
  1592. 'value' => 'address%7Cfirst_name',
  1593. 'data-foo' => 'bar',
  1594. ]],
  1595. ['input' => [
  1596. 'type' => 'hidden', 'name' => '_Token[debug]',
  1597. 'value' => $tokenDebug,
  1598. 'data-foo' => 'bar',
  1599. ]],
  1600. '/div',
  1601. ];
  1602. $this->assertHtml($expected, $result);
  1603. }
  1604. /**
  1605. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1606. *
  1607. * Debug is false, debugSecurity is true -> no debug
  1608. */
  1609. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse(): void
  1610. {
  1611. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1612. 'unlockedFields' => ['first_name', 'address'],
  1613. ]));
  1614. $this->Form->create();
  1615. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1616. $this->assertEquals(
  1617. $this->View->getRequest()->getAttribute('formTokenData'),
  1618. ['unlockedFields' => $result]
  1619. );
  1620. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1621. $this->Form->text('Addresses.title');
  1622. $this->Form->text('Addresses.first_name');
  1623. $this->Form->text('Addresses.last_name');
  1624. $this->Form->text('Addresses.address');
  1625. $this->Form->text('Addresses.city');
  1626. $this->Form->text('Addresses.phone');
  1627. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1628. $expected = [
  1629. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1630. 'Addresses.city', 'Addresses.phone',
  1631. ];
  1632. $this->assertEquals($expected, $result);
  1633. Configure::write('debug', false);
  1634. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1635. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1636. $expected = [
  1637. 'div' => ['style' => 'display:none;'],
  1638. ['input' => [
  1639. 'type' => 'hidden',
  1640. 'name' => '_Token[fields]',
  1641. 'value' => $hash,
  1642. 'data-foo' => 'bar',
  1643. ]],
  1644. ['input' => [
  1645. 'type' => 'hidden',
  1646. 'name' => '_Token[unlocked]',
  1647. 'value' => 'address%7Cfirst_name',
  1648. 'data-foo' => 'bar',
  1649. ]],
  1650. '/div',
  1651. ];
  1652. $this->assertHtml($expected, $result);
  1653. }
  1654. /**
  1655. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1656. *
  1657. * Test single record form with debugSecurity param.
  1658. */
  1659. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse(): void
  1660. {
  1661. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1662. 'unlockedFields' => ['first_name', 'address'],
  1663. ]));
  1664. $this->Form->create();
  1665. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1666. $this->assertEquals(
  1667. $this->View->getRequest()->getAttribute('formTokenData'),
  1668. ['unlockedFields' => $result]
  1669. );
  1670. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1671. $this->Form->text('Addresses.title');
  1672. $this->Form->text('Addresses.first_name');
  1673. $this->Form->text('Addresses.last_name');
  1674. $this->Form->text('Addresses.address');
  1675. $this->Form->text('Addresses.city');
  1676. $this->Form->text('Addresses.phone');
  1677. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1678. $expected = [
  1679. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1680. 'Addresses.city', 'Addresses.phone',
  1681. ];
  1682. $this->assertEquals($expected, $result);
  1683. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1684. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1685. $expected = [
  1686. 'div' => ['style' => 'display:none;'],
  1687. ['input' => [
  1688. 'type' => 'hidden',
  1689. 'name' => '_Token[fields]',
  1690. 'value' => $hash,
  1691. 'data-foo' => 'bar',
  1692. ]],
  1693. ['input' => [
  1694. 'type' => 'hidden',
  1695. 'name' => '_Token[unlocked]',
  1696. 'value' => 'address%7Cfirst_name',
  1697. 'data-foo' => 'bar',
  1698. ]],
  1699. '/div',
  1700. ];
  1701. $this->assertHtml($expected, $result);
  1702. }
  1703. /**
  1704. * testFormSecureWithCustomNameAttribute method
  1705. *
  1706. * Test securing inputs with custom name attributes.
  1707. */
  1708. public function testFormSecureWithCustomNameAttribute(): void
  1709. {
  1710. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1711. $this->Form->create();
  1712. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1713. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1714. $this->assertSame('User.custom', $result[0]);
  1715. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1716. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1717. $this->assertSame('User.custom.another.value', $result[1]);
  1718. }
  1719. /**
  1720. * testFormSecuredControl method
  1721. *
  1722. * Test generation of entire secure form, assertions made on control() output.
  1723. */
  1724. public function testFormSecuredControl(): void
  1725. {
  1726. $this->View->setRequest($this->View->getRequest()
  1727. ->withAttribute('formTokenData', [])
  1728. ->withAttribute('csrfToken', 'testKey'));
  1729. $this->article['schema'] = [
  1730. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1731. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1732. ];
  1733. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1734. $encoding = strtolower(Configure::read('App.encoding'));
  1735. $expected = [
  1736. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1737. 'div' => ['style' => 'display:none;'],
  1738. ['input' => [
  1739. 'type' => 'hidden',
  1740. 'name' => '_csrfToken',
  1741. 'value' => 'testKey',
  1742. ]],
  1743. '/div',
  1744. ];
  1745. $this->assertHtml($expected, $result);
  1746. $result = $this->Form->control('ratio');
  1747. $expected = [
  1748. 'div' => ['class'],
  1749. 'label' => ['for'],
  1750. 'Ratio',
  1751. '/label',
  1752. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1753. '/div',
  1754. ];
  1755. $this->assertHtml($expected, $result);
  1756. $result = $this->Form->control('population');
  1757. $expected = [
  1758. 'div' => ['class'],
  1759. 'label' => ['for'],
  1760. 'Population',
  1761. '/label',
  1762. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1763. '/div',
  1764. ];
  1765. $this->assertHtml($expected, $result);
  1766. $result = $this->Form->control('published', ['type' => 'text']);
  1767. $expected = [
  1768. 'div' => ['class' => 'input text'],
  1769. 'label' => ['for' => 'published'],
  1770. 'Published',
  1771. '/label',
  1772. ['input' => [
  1773. 'type' => 'text',
  1774. 'name' => 'published',
  1775. 'id' => 'published',
  1776. ]],
  1777. '/div',
  1778. ];
  1779. $this->assertHtml($expected, $result);
  1780. $result = $this->Form->control('other', ['type' => 'text']);
  1781. $expected = [
  1782. 'div' => ['class' => 'input text'],
  1783. 'label' => ['for' => 'other'],
  1784. 'Other',
  1785. '/label',
  1786. ['input' => [
  1787. 'type' => 'text',
  1788. 'name' => 'other',
  1789. 'id',
  1790. ]],
  1791. '/div',
  1792. ];
  1793. $this->assertHtml($expected, $result);
  1794. $result = $this->Form->hidden('stuff');
  1795. $expected = [
  1796. 'input' => [
  1797. 'type' => 'hidden',
  1798. 'name' => 'stuff',
  1799. ],
  1800. ];
  1801. $this->assertHtml($expected, $result);
  1802. $result = $this->Form->hidden('hidden', ['value' => false]);
  1803. $expected = ['input' => [
  1804. 'type' => 'hidden',
  1805. 'name' => 'hidden',
  1806. 'value' => '0',
  1807. ]];
  1808. $this->assertHtml($expected, $result);
  1809. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1810. $expected = [
  1811. 'div' => ['class' => 'input checkbox'],
  1812. ['input' => [
  1813. 'type' => 'hidden',
  1814. 'name' => 'something',
  1815. 'value' => '0',
  1816. ]],
  1817. 'label' => ['for' => 'something'],
  1818. ['input' => [
  1819. 'type' => 'checkbox',
  1820. 'name' => 'something',
  1821. 'value' => '1',
  1822. 'id' => 'something',
  1823. ]],
  1824. 'Something',
  1825. '/label',
  1826. '/div',
  1827. ];
  1828. $this->assertHtml($expected, $result);
  1829. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1830. $expectedFields = [
  1831. 'ratio',
  1832. 'population',
  1833. 'published',
  1834. 'other',
  1835. 'stuff' => '',
  1836. 'hidden' => '0',
  1837. 'something',
  1838. ];
  1839. $this->assertEquals($expectedFields, $result);
  1840. $result = $this->Form->secure();
  1841. $tokenDebug = urlencode(json_encode([
  1842. '/articles/add',
  1843. $expectedFields,
  1844. [],
  1845. ]));
  1846. $expected = [
  1847. 'div' => ['style' => 'display:none;'],
  1848. ['input' => [
  1849. 'type' => 'hidden',
  1850. 'name' => '_Token[fields]',
  1851. 'value',
  1852. ]],
  1853. ['input' => [
  1854. 'type' => 'hidden',
  1855. 'name' => '_Token[unlocked]',
  1856. 'value' => '',
  1857. ]],
  1858. ['input' => [
  1859. 'type' => 'hidden', 'name' => '_Token[debug]',
  1860. 'value' => $tokenDebug,
  1861. ]],
  1862. '/div',
  1863. ];
  1864. $this->assertHtml($expected, $result);
  1865. $data = [
  1866. 'ratio' => '',
  1867. 'population' => '',
  1868. 'published' => '',
  1869. 'other' => '',
  1870. 'stuff' => '',
  1871. 'hidden' => '0',
  1872. 'something' => '',
  1873. '_Token' => $this->Form->getFormProtector()->buildTokenData(),
  1874. ];
  1875. $this->assertTrue($this->Form->getFormProtector()->validate($data, '', ''));
  1876. }
  1877. /**
  1878. * testSecuredControlCustomName method
  1879. *
  1880. * Test secured inputs with custom names.
  1881. */
  1882. public function testSecuredControlCustomName(): void
  1883. {
  1884. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1885. $this->Form->create();
  1886. $this->Form->text('text_input', [
  1887. 'name' => 'Option[General.default_role]',
  1888. ]);
  1889. $expected = ['Option.General.default_role'];
  1890. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1891. $this->assertEquals($expected, $result);
  1892. $this->Form->select('select_box', [1, 2], [
  1893. 'name' => 'Option[General.select_role]',
  1894. ]);
  1895. $expected[] = 'Option.General.select_role';
  1896. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1897. $this->assertEquals($expected, $result);
  1898. $this->Form->text('other.things[]');
  1899. $expected[] = 'other.things';
  1900. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1901. $this->assertEquals($expected, $result);
  1902. }
  1903. /**
  1904. * testSecuredControlDuplicate method
  1905. *
  1906. * Test that a hidden field followed by a visible field
  1907. * undoes the hidden field locking.
  1908. */
  1909. public function testSecuredControlDuplicate(): void
  1910. {
  1911. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1912. $this->Form->create();
  1913. $this->Form->control('text_val', [
  1914. 'type' => 'hidden',
  1915. 'value' => 'some text',
  1916. ]);
  1917. $expected = ['text_val' => 'some text'];
  1918. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1919. $this->assertEquals($expected, $result);
  1920. $this->Form->control('text_val', [
  1921. 'type' => 'text',
  1922. ]);
  1923. $expected = ['text_val'];
  1924. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1925. $this->assertEquals($expected, $result);
  1926. }
  1927. /**
  1928. * testFormSecuredFileControl method
  1929. *
  1930. * Tests that the correct keys are added to the field hash index.
  1931. */
  1932. public function testFormSecuredFileControl(): void
  1933. {
  1934. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1935. $this->Form->create();
  1936. $this->Form->file('Attachment.file');
  1937. $expected = ['Attachment.file'];
  1938. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1939. $this->assertEquals($expected, $result);
  1940. }
  1941. /**
  1942. * testFormSecuredMultipleSelect method
  1943. *
  1944. * Test that multiple selects keys are added to field hash.
  1945. */
  1946. public function testFormSecuredMultipleSelect(): void
  1947. {
  1948. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1949. $this->Form->create();
  1950. $options = ['1' => 'one', '2' => 'two'];
  1951. $this->Form->select('Model.select', $options);
  1952. $expected = ['Model.select'];
  1953. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1954. $this->assertEquals($expected, $result);
  1955. $this->Form->select('Model.select', $options, ['multiple' => true]);
  1956. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1957. $this->assertEquals($expected, $result);
  1958. }
  1959. /**
  1960. * testFormSecuredRadio method
  1961. */
  1962. public function testFormSecuredRadio(): void
  1963. {
  1964. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1965. $this->Form->create();
  1966. $options = ['1' => 'option1', '2' => 'option2'];
  1967. $this->Form->radio('Test.test', $options);
  1968. $expected = ['Test.test'];
  1969. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1970. $this->assertEquals($expected, $result);
  1971. $this->Form->radio('Test.all', $options, [
  1972. 'disabled' => ['option1', 'option2'],
  1973. ]);
  1974. $expected = ['Test.test', 'Test.all' => ''];
  1975. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1976. $this->assertEquals($expected, $result);
  1977. $this->Form->radio('Test.some', $options, [
  1978. 'disabled' => ['option1'],
  1979. ]);
  1980. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  1981. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1982. $this->assertEquals($expected, $result);
  1983. }
  1984. /**
  1985. * testFormSecuredAndDisabled method
  1986. *
  1987. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  1988. * hashing.
  1989. */
  1990. public function testFormSecuredAndDisabled(): void
  1991. {
  1992. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1993. $this->Form->create();
  1994. $this->Form->checkbox('Model.checkbox', ['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->text('Model.text', ['disabled' => true]);
  2001. $this->Form->password('Model.text', ['disabled' => true]);
  2002. $this->Form->day('Model.day', ['disabled' => true]);
  2003. $this->Form->hour('Model.hour', ['disabled' => true]);
  2004. $this->Form->minute('Model.minute', ['disabled' => true]);
  2005. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2006. $expected = [
  2007. 'Model.radio' => '',
  2008. ];
  2009. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2010. $this->assertEquals($expected, $result);
  2011. }
  2012. /**
  2013. * testUnlockFieldAddsToList method
  2014. *
  2015. * Test disableField.
  2016. */
  2017. public function testUnlockFieldAddsToList(): void
  2018. {
  2019. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2020. 'unlockedFields' => [],
  2021. ]));
  2022. $this->Form->create();
  2023. $this->Form->unlockField('Contact.name');
  2024. $this->Form->text('Contact.name');
  2025. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2026. $this->assertEquals([], $result);
  2027. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  2028. $this->assertEquals(['Contact.name'], $result);
  2029. }
  2030. /**
  2031. * testUnlockFieldRemovingFromFields method
  2032. *
  2033. * Test unlockField removing from fields array.
  2034. */
  2035. public function testUnlockFieldRemovingFromFields(): void
  2036. {
  2037. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2038. 'unlockedFields' => [],
  2039. ]));
  2040. $this->Form->create($this->article);
  2041. $this->Form->hidden('Article.id', ['value' => 1]);
  2042. $this->Form->text('Article.title');
  2043. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2044. $this->assertSame('1', $result['Article.id'], 'Hidden input should be secured.');
  2045. $this->assertContains('Article.title', $result, 'Field should be secured.');
  2046. $this->Form->unlockField('Article.title');
  2047. $this->Form->unlockField('Article.id');
  2048. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2049. $this->assertEquals([], $result);
  2050. }
  2051. /**
  2052. * testResetUnlockFields method
  2053. *
  2054. * Test reset unlockFields, when create new form.
  2055. */
  2056. public function testResetUnlockFields(): void
  2057. {
  2058. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2059. 'key' => 'testKey',
  2060. 'unlockedFields' => [],
  2061. ]));
  2062. $this->Form->create();
  2063. $this->Form->unlockField('Contact.id');
  2064. $this->Form->hidden('Contact.id', ['value' => 1]);
  2065. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2066. $this->assertEmpty($result, 'Field should be unlocked');
  2067. $this->Form->end();
  2068. $this->Form->create();
  2069. $this->Form->hidden('Contact.id', ['value' => 1]);
  2070. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2071. $this->assertSame('1', $result['Contact.id'], 'Hidden input should be secured.');
  2072. }
  2073. /**
  2074. * testSecuredFormUrlIgnoresHost method
  2075. *
  2076. * Test that only the path + query elements of a form's URL show up in their hash.
  2077. */
  2078. public function testSecuredFormUrlIgnoresHost(): void
  2079. {
  2080. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'testKey']));
  2081. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2082. $this->Form->create($this->article, [
  2083. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2084. ]);
  2085. $result = $this->Form->secure();
  2086. $this->assertStringContainsString($expected, $result);
  2087. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2088. $result = $this->Form->secure();
  2089. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2090. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2091. $result = $this->Form->secure();
  2092. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2093. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2094. $result = $this->Form->secure();
  2095. $this->assertStringNotContainsString($expected, $result, 'URL is different');
  2096. }
  2097. /**
  2098. * testSecuredFormUrlHasHtmlAndIdentifier method
  2099. *
  2100. * Test that URL, HTML and identifier show up in their hashes.
  2101. */
  2102. public function testSecuredFormUrlHasHtmlAndIdentifier(): void
  2103. {
  2104. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2105. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2106. $this->Form->create($this->article, [
  2107. 'url' => [
  2108. 'controller' => 'articles',
  2109. 'action' => 'view',
  2110. '?' => [
  2111. 'page' => 1,
  2112. 'limit' => 10,
  2113. 'html' => '<>"',
  2114. ],
  2115. '#' => 'result',
  2116. ],
  2117. ]);
  2118. $result = $this->Form->secure();
  2119. $this->assertStringContainsString($expected, $result);
  2120. $this->Form->create($this->article, [
  2121. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2122. ]);
  2123. $result = $this->Form->secure();
  2124. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2125. $this->Form->create($this->article, [
  2126. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2127. ]);
  2128. $result = $this->Form->secure();
  2129. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2130. }
  2131. /**
  2132. * testErrorMessageDisplay method
  2133. *
  2134. * Test error message display.
  2135. */
  2136. public function testErrorMessageDisplay(): void
  2137. {
  2138. $this->article['errors'] = [
  2139. 'Article' => [
  2140. 'title' => 'error message',
  2141. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2142. ],
  2143. ];
  2144. $this->Form->create($this->article);
  2145. $result = $this->Form->control('Article.title');
  2146. $expected = [
  2147. 'div' => ['class' => 'input text error'],
  2148. 'label' => ['for' => 'article-title'],
  2149. 'Title',
  2150. '/label',
  2151. 'input' => [
  2152. 'type' => 'text',
  2153. 'name' => 'Article[title]',
  2154. 'id' => 'article-title',
  2155. 'class' => 'form-error',
  2156. 'aria-invalid' => 'true',
  2157. 'aria-describedby' => 'article-title-error',
  2158. ],
  2159. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2160. 'error message',
  2161. '/div',
  2162. '/div',
  2163. ];
  2164. $this->assertHtml($expected, $result);
  2165. $result = $this->Form->control('Article.title', [
  2166. 'templates' => [
  2167. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2168. ],
  2169. ]);
  2170. $expected = [
  2171. 'div' => ['class' => 'input text error'],
  2172. 'label' => ['for' => 'article-title'],
  2173. 'Title',
  2174. '/label',
  2175. 'input' => [
  2176. 'type' => 'text',
  2177. 'name' => 'Article[title]',
  2178. 'id' => 'article-title',
  2179. 'class' => 'form-error',
  2180. // No aria-describedby because error template is custom
  2181. 'aria-invalid' => 'true',
  2182. ],
  2183. '/div',
  2184. ];
  2185. $this->assertHtml($expected, $result);
  2186. $result = $this->Form->control('Article.content');
  2187. $expected = [
  2188. 'div' => ['class' => 'input text error'],
  2189. 'label' => ['for' => 'article-content'],
  2190. 'Content',
  2191. '/label',
  2192. 'input' => [
  2193. 'type' => 'text',
  2194. 'name' => 'Article[content]',
  2195. 'id' => 'article-content',
  2196. 'class' => 'form-error',
  2197. 'aria-invalid' => 'true',
  2198. 'aria-describedby' => 'article-content-error',
  2199. ],
  2200. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2201. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2202. '/div',
  2203. '/div',
  2204. ];
  2205. $this->assertHtml($expected, $result);
  2206. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2207. $expected = [
  2208. 'div' => ['class' => 'input text error'],
  2209. 'label' => ['for' => 'article-content'],
  2210. 'Content',
  2211. '/label',
  2212. 'input' => [
  2213. 'type' => 'text',
  2214. 'name' => 'Article[content]',
  2215. 'id' => 'article-content',
  2216. 'class' => 'form-error',
  2217. 'aria-invalid' => 'true',
  2218. 'aria-describedby' => 'article-content-error',
  2219. ],
  2220. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2221. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2222. '/div',
  2223. '/div',
  2224. ];
  2225. $this->assertHtml($expected, $result);
  2226. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2227. $expected = [
  2228. 'div' => ['class' => 'input text error'],
  2229. 'label' => ['for' => 'article-content'],
  2230. 'Content',
  2231. '/label',
  2232. 'input' => [
  2233. 'type' => 'text',
  2234. 'name' => 'Article[content]',
  2235. 'id' => 'article-content',
  2236. 'class' => 'form-error',
  2237. 'aria-invalid' => 'true',
  2238. 'aria-describedby' => 'article-content-error',
  2239. ],
  2240. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2241. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2242. '/div',
  2243. '/div',
  2244. ];
  2245. $this->assertHtml($expected, $result);
  2246. }
  2247. /**
  2248. * testEmptyErrorValidation method
  2249. *
  2250. * Test validation errors, when validation message is an empty string.
  2251. */
  2252. public function testEmptyErrorValidation(): void
  2253. {
  2254. $this->article['errors'] = [
  2255. 'Article' => ['title' => ''],
  2256. ];
  2257. $this->Form->create($this->article);
  2258. $result = $this->Form->control('Article.title');
  2259. $expected = [
  2260. 'div' => ['class' => 'input text error'],
  2261. 'label' => ['for' => 'article-title'],
  2262. 'Title',
  2263. '/label',
  2264. 'input' => [
  2265. 'type' => 'text',
  2266. 'name' => 'Article[title]',
  2267. 'id' => 'article-title',
  2268. 'class' => 'form-error',
  2269. 'aria-invalid' => 'true',
  2270. 'aria-describedby' => 'article-title-error',
  2271. ],
  2272. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2273. [],
  2274. '/div',
  2275. '/div',
  2276. ];
  2277. $this->assertHtml($expected, $result);
  2278. }
  2279. /**
  2280. * testEmptyControlErrorValidation method
  2281. *
  2282. * Test validation errors, when calling control() overriding validation message by an empty string.
  2283. */
  2284. public function testEmptyControlErrorValidation(): void
  2285. {
  2286. $this->article['errors'] = [
  2287. 'Article' => ['title' => 'error message'],
  2288. ];
  2289. $this->Form->create($this->article);
  2290. $result = $this->Form->control('Article.title', ['error' => '']);
  2291. $expected = [
  2292. 'div' => ['class' => 'input text error'],
  2293. 'label' => ['for' => 'article-title'],
  2294. 'Title',
  2295. '/label',
  2296. 'input' => [
  2297. 'aria-invalid' => 'true',
  2298. 'aria-describedby' => 'article-title-error',
  2299. 'type' => 'text',
  2300. 'name' => 'Article[title]',
  2301. 'id' => 'article-title',
  2302. 'class' => 'form-error',
  2303. ],
  2304. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2305. [],
  2306. '/div',
  2307. '/div',
  2308. ];
  2309. $this->assertHtml($expected, $result);
  2310. }
  2311. /**
  2312. * testControlErrorMessage method
  2313. *
  2314. * Test validation errors, when calling control() overriding validation messages.
  2315. */
  2316. public function testControlErrorMessage(): void
  2317. {
  2318. $this->article['errors'] = [
  2319. 'title' => ['error message'],
  2320. ];
  2321. $this->Form->create($this->article);
  2322. $result = $this->Form->control('title', [
  2323. 'error' => 'Custom error!',
  2324. ]);
  2325. $expected = [
  2326. 'div' => ['class' => 'input text required error'],
  2327. 'label' => ['for' => 'title'],
  2328. 'Title',
  2329. '/label',
  2330. 'input' => [
  2331. 'type' => 'text',
  2332. 'name' => 'title',
  2333. 'id' => 'title',
  2334. 'class' => 'form-error',
  2335. 'required' => 'required',
  2336. 'data-validity-message' => 'This field cannot be left empty',
  2337. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2338. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2339. 'aria-required' => 'true',
  2340. 'aria-invalid' => 'true',
  2341. 'aria-describedby' => 'title-error',
  2342. ],
  2343. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2344. 'Custom error!',
  2345. '/div',
  2346. '/div',
  2347. ];
  2348. $this->assertHtml($expected, $result);
  2349. $result = $this->Form->control('title', [
  2350. 'error' => ['error message' => 'Custom error!'],
  2351. ]);
  2352. $expected = [
  2353. 'div' => ['class' => 'input text required error'],
  2354. 'label' => ['for' => 'title'],
  2355. 'Title',
  2356. '/label',
  2357. 'input' => [
  2358. 'type' => 'text',
  2359. 'name' => 'title',
  2360. 'id' => 'title',
  2361. 'aria-required' => 'true',
  2362. 'aria-invalid' => 'true',
  2363. 'aria-describedby' => 'title-error',
  2364. 'class' => 'form-error',
  2365. 'required' => 'required',
  2366. 'data-validity-message' => 'This field cannot be left empty',
  2367. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2368. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2369. ],
  2370. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2371. 'Custom error!',
  2372. '/div',
  2373. '/div',
  2374. ];
  2375. $this->assertHtml($expected, $result);
  2376. }
  2377. /**
  2378. * testFormValidationAssociated method
  2379. *
  2380. * Tests displaying errors for nested entities.
  2381. */
  2382. public function testFormValidationAssociated(): void
  2383. {
  2384. $nested = new Entity(['foo' => 'bar']);
  2385. $nested->setError('foo', ['not a valid bar']);
  2386. $entity = new Entity(['nested' => $nested]);
  2387. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2388. $result = $this->Form->error('nested.foo');
  2389. $this->assertSame('<div class="error-message" id="nested-foo-error">not a valid bar</div>', $result);
  2390. }
  2391. /**
  2392. * testFormValidationAssociatedSecondLevel method
  2393. *
  2394. * Test form error display with associated model.
  2395. */
  2396. public function testFormValidationAssociatedSecondLevel(): void
  2397. {
  2398. $inner = new Entity(['bar' => 'baz']);
  2399. $nested = new Entity(['foo' => $inner]);
  2400. $entity = new Entity(['nested' => $nested]);
  2401. $inner->setError('bar', ['not a valid one']);
  2402. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2403. $result = $this->Form->error('nested.foo.bar');
  2404. $this->assertSame('<div class="error-message" id="nested-foo-bar-error">not a valid one</div>', $result);
  2405. }
  2406. /**
  2407. * testFormValidationMultiRecord method
  2408. *
  2409. * Test form error display with multiple records.
  2410. */
  2411. public function testFormValidationMultiRecord(): void
  2412. {
  2413. $one = new Entity();
  2414. $two = new Entity();
  2415. $this->getTableLocator()->get('Contacts', [
  2416. 'className' => ContactsTable::class,
  2417. ]);
  2418. $one->set('email', '');
  2419. $one->setError('email', ['invalid email']);
  2420. $two->set('name', '');
  2421. $two->setError('name', ['This is wrong']);
  2422. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2423. $result = $this->Form->control('0.email');
  2424. $expected = [
  2425. 'div' => ['class' => 'input email error'],
  2426. 'label' => ['for' => '0-email'],
  2427. 'Email',
  2428. '/label',
  2429. 'input' => [
  2430. 'type' => 'email',
  2431. 'name' => '0[email]',
  2432. 'id' => '0-email',
  2433. 'class' => 'form-error',
  2434. 'maxlength' => 255,
  2435. 'value' => '',
  2436. 'aria-invalid' => 'true',
  2437. 'aria-describedby' => '0-email-error',
  2438. ],
  2439. ['div' => ['class' => 'error-message', 'id' => '0-email-error']],
  2440. 'invalid email',
  2441. '/div',
  2442. '/div',
  2443. ];
  2444. $this->assertHtml($expected, $result);
  2445. $result = $this->Form->control('1.name');
  2446. $expected = [
  2447. 'div' => ['class' => 'input text error'],
  2448. 'label' => ['for' => '1-name'],
  2449. 'Name',
  2450. '/label',
  2451. 'input' => [
  2452. 'type' => 'text',
  2453. 'name' => '1[name]',
  2454. 'id' => '1-name',
  2455. 'class' => 'form-error',
  2456. 'maxlength' => 255,
  2457. 'value' => '',
  2458. 'aria-invalid' => 'true',
  2459. 'aria-describedby' => '1-name-error',
  2460. ],
  2461. ['div' => ['class' => 'error-message', 'id' => '1-name-error']],
  2462. 'This is wrong',
  2463. '/div',
  2464. '/div',
  2465. ];
  2466. $this->assertHtml($expected, $result);
  2467. }
  2468. /**
  2469. * testControl method
  2470. *
  2471. * Test various incarnations of control().
  2472. */
  2473. public function testControl(): void
  2474. {
  2475. $this->getTableLocator()->get('ValidateUsers', [
  2476. 'className' => ValidateUsersTable::class,
  2477. ]);
  2478. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2479. $result = $this->Form->control('ValidateUsers.balance');
  2480. $expected = [
  2481. 'div' => ['class'],
  2482. 'label' => ['for'],
  2483. 'Balance',
  2484. '/label',
  2485. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2486. '/div',
  2487. ];
  2488. $this->assertHtml($expected, $result);
  2489. $result = $this->Form->control('ValidateUser.cost_decimal');
  2490. $expected = [
  2491. 'div' => ['class'],
  2492. 'label' => ['for'],
  2493. 'Cost Decimal',
  2494. '/label',
  2495. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2496. '/div',
  2497. ];
  2498. $this->assertHtml($expected, $result);
  2499. $result = $this->Form->control('ValidateUser.null_decimal');
  2500. $expected = [
  2501. 'div' => ['class'],
  2502. 'label' => ['for'],
  2503. 'Null Decimal',
  2504. '/label',
  2505. 'input' => ['name', 'type' => 'number', 'id'],
  2506. '/div',
  2507. ];
  2508. $this->assertHtml($expected, $result);
  2509. }
  2510. /**
  2511. * testControlCustomization method
  2512. *
  2513. * Tests the input method and passing custom options.
  2514. */
  2515. public function testControlCustomization(): void
  2516. {
  2517. $this->getTableLocator()->get('Contacts', [
  2518. 'className' => ContactsTable::class,
  2519. ]);
  2520. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2521. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2522. $expected = [
  2523. 'div' => ['class' => 'input email'],
  2524. 'label' => ['for' => 'custom'],
  2525. 'Email',
  2526. '/label',
  2527. ['input' => [
  2528. 'type' => 'email', 'name' => 'Contact[email]',
  2529. 'id' => 'custom', 'maxlength' => 255,
  2530. ]],
  2531. '/div',
  2532. ];
  2533. $this->assertHtml($expected, $result);
  2534. $result = $this->Form->control('Contact.email', [
  2535. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2536. ]);
  2537. $expected = [
  2538. '<div',
  2539. 'label' => ['for' => 'contact-email'],
  2540. 'Email',
  2541. '/label',
  2542. ['input' => [
  2543. 'type' => 'email', 'name' => 'Contact[email]',
  2544. 'id' => 'contact-email', 'maxlength' => 255,
  2545. ]],
  2546. '/div',
  2547. ];
  2548. $this->assertHtml($expected, $result);
  2549. $result = $this->Form->control('Contact.email', [
  2550. 'templates' => [
  2551. 'formGroup' => '{{input}}',
  2552. 'inputContainer' => '<div><div>{{label}}</div>{{content}}</div>',
  2553. ],
  2554. ]);
  2555. $expected = [
  2556. '<div',
  2557. '<div',
  2558. 'label' => ['for' => 'contact-email'],
  2559. 'Email',
  2560. '/label',
  2561. '/div',
  2562. ['input' => [
  2563. 'type' => 'email', 'name' => 'Contact[email]',
  2564. 'id' => 'contact-email', 'maxlength' => 255,
  2565. ]],
  2566. '/div',
  2567. ];
  2568. $this->assertHtml($expected, $result);
  2569. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2570. $expected = [
  2571. 'div' => ['class' => 'input text'],
  2572. 'label' => ['for' => 'contact-email'],
  2573. 'Email',
  2574. '/label',
  2575. ['input' => [
  2576. 'type' => 'text', 'name' => 'Contact[email]',
  2577. 'id' => 'contact-email', 'maxlength' => '255',
  2578. ]],
  2579. '/div',
  2580. ];
  2581. $this->assertHtml($expected, $result);
  2582. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2583. $expected = [
  2584. 'div' => ['class' => 'input text'],
  2585. 'label' => ['for' => 'contact-5-email'],
  2586. 'Email',
  2587. '/label',
  2588. ['input' => [
  2589. 'type' => 'text', 'name' => 'Contact[5][email]',
  2590. 'id' => 'contact-5-email', 'maxlength' => '255',
  2591. ]],
  2592. '/div',
  2593. ];
  2594. $this->assertHtml($expected, $result);
  2595. $result = $this->Form->control('Contact.password');
  2596. $expected = [
  2597. 'div' => ['class' => 'input password'],
  2598. 'label' => ['for' => 'contact-password'],
  2599. 'Password',
  2600. '/label',
  2601. ['input' => [
  2602. 'type' => 'password', 'name' => 'Contact[password]',
  2603. 'id' => 'contact-password',
  2604. ]],
  2605. '/div',
  2606. ];
  2607. $this->assertHtml($expected, $result);
  2608. $result = $this->Form->control('Contact.email', [
  2609. 'type' => 'file', 'class' => 'textbox',
  2610. ]);
  2611. $expected = [
  2612. 'div' => ['class' => 'input file'],
  2613. 'label' => ['for' => 'contact-email'],
  2614. 'Email',
  2615. '/label',
  2616. ['input' => [
  2617. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2618. 'id' => 'contact-email',
  2619. ]],
  2620. '/div',
  2621. ];
  2622. $this->assertHtml($expected, $result);
  2623. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2624. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2625. $result = $this->Form->control('phone');
  2626. $expected = [
  2627. 'div' => ['class' => 'input tel'],
  2628. 'label' => ['for' => 'phone'],
  2629. 'Phone',
  2630. '/label',
  2631. ['input' => [
  2632. 'type' => 'tel', 'name' => 'phone',
  2633. 'value' => 'Hello &amp; World &gt; weird chars',
  2634. 'id' => 'phone', 'maxlength' => 255,
  2635. ]],
  2636. '/div',
  2637. ];
  2638. $this->assertHtml($expected, $result);
  2639. $this->View->setRequest(
  2640. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2641. );
  2642. $this->Form->create();
  2643. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2644. $expected = [
  2645. 'div' => ['class' => 'input text'],
  2646. 'label' => ['for' => 'myId'],
  2647. 'Field',
  2648. '/label',
  2649. 'input' => [
  2650. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2651. 'value' => 'My value', 'id' => 'myId',
  2652. ],
  2653. '/div',
  2654. ];
  2655. $this->assertHtml($expected, $result);
  2656. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2657. $this->Form->create();
  2658. $entity->setError('field', 'Badness!');
  2659. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2660. $result = $this->Form->control('field');
  2661. $expected = [
  2662. 'div' => ['class' => 'input text error'],
  2663. 'label' => ['for' => 'field'],
  2664. 'Field',
  2665. '/label',
  2666. 'input' => [
  2667. 'type' => 'text',
  2668. 'name' => 'field',
  2669. 'id' => 'field',
  2670. 'class' => 'form-error',
  2671. 'aria-invalid' => 'true',
  2672. 'aria-describedby' => 'field-error',
  2673. ],
  2674. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2675. 'Badness!',
  2676. '/div',
  2677. '/div',
  2678. ];
  2679. $this->assertHtml($expected, $result);
  2680. $result = $this->Form->control('field', [
  2681. 'templates' => [
  2682. 'inputContainerError' => '{{content}}{{error}}',
  2683. 'error' => '<span class="error-message">{{content}}</span>',
  2684. ],
  2685. ]);
  2686. $expected = [
  2687. 'label' => ['for' => 'field'],
  2688. 'Field',
  2689. '/label',
  2690. 'input' => [
  2691. 'type' => 'text',
  2692. 'name' => 'field',
  2693. 'id' => 'field',
  2694. 'class' => 'form-error',
  2695. // No aria-describedby because error template is custom
  2696. 'aria-invalid' => 'true',
  2697. ],
  2698. ['span' => ['class' => 'error-message']],
  2699. 'Badness!',
  2700. '/span',
  2701. ];
  2702. $this->assertHtml($expected, $result);
  2703. $entity->setError('field', ['minLength'], true);
  2704. $result = $this->Form->control('field', [
  2705. 'error' => [
  2706. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2707. 'maxLength' => 'login too large',
  2708. ],
  2709. ]);
  2710. $expected = [
  2711. 'div' => ['class' => 'input text error'],
  2712. 'label' => ['for' => 'field'],
  2713. 'Field',
  2714. '/label',
  2715. 'input' => [
  2716. 'type' => 'text',
  2717. 'name' => 'field',
  2718. 'id' => 'field',
  2719. 'class' => 'form-error',
  2720. 'aria-invalid' => 'true',
  2721. 'aria-describedby' => 'field-error',
  2722. ],
  2723. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2724. 'Le login doit contenir au moins 2 caractères',
  2725. '/div',
  2726. '/div',
  2727. ];
  2728. $this->assertHtml($expected, $result);
  2729. $entity->setError('field', ['maxLength'], true);
  2730. $result = $this->Form->control('field', [
  2731. 'error' => [
  2732. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2733. 'maxLength' => 'login too large',
  2734. ],
  2735. ]);
  2736. $expected = [
  2737. 'div' => ['class' => 'input text error'],
  2738. 'label' => ['for' => 'field'],
  2739. 'Field',
  2740. '/label',
  2741. 'input' => [
  2742. 'type' => 'text',
  2743. 'name' => 'field',
  2744. 'id' => 'field',
  2745. 'class' => 'form-error',
  2746. 'aria-invalid' => 'true',
  2747. 'aria-describedby' => 'field-error',
  2748. ],
  2749. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2750. 'login too large',
  2751. '/div',
  2752. '/div',
  2753. ];
  2754. $this->assertHtml($expected, $result);
  2755. }
  2756. /**
  2757. * testControlWithTemplateFile method
  2758. *
  2759. * Test that control() accepts a template file.
  2760. */
  2761. public function testControlWithTemplateFile(): void
  2762. {
  2763. $result = $this->Form->control('field', [
  2764. 'templates' => 'htmlhelper_tags',
  2765. ]);
  2766. $expected = [
  2767. 'label' => ['for' => 'field'],
  2768. 'Field',
  2769. '/label',
  2770. 'input' => [
  2771. 'type' => 'text', 'name' => 'field',
  2772. 'id' => 'field',
  2773. ],
  2774. ];
  2775. $this->assertHtml($expected, $result);
  2776. }
  2777. /**
  2778. * testNestedControlsEndWithBrackets method
  2779. *
  2780. * Test that nested inputs end with brackets.
  2781. */
  2782. public function testNestedControlsEndWithBrackets(): void
  2783. {
  2784. $result = $this->Form->text('nested.text[]');
  2785. $expected = [
  2786. 'input' => [
  2787. 'type' => 'text', 'name' => 'nested[text][]',
  2788. ],
  2789. ];
  2790. $this->assertHtml($expected, $result);
  2791. $result = $this->Form->file('nested.file[]');
  2792. $expected = [
  2793. 'input' => [
  2794. 'type' => 'file', 'name' => 'nested[file][]',
  2795. ],
  2796. ];
  2797. $this->assertHtml($expected, $result);
  2798. }
  2799. /**
  2800. * testCreateIdPrefix method
  2801. *
  2802. * Test id prefix.
  2803. */
  2804. public function testCreateIdPrefix(): void
  2805. {
  2806. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2807. $result = $this->Form->control('field');
  2808. $expected = [
  2809. 'div' => ['class' => 'input text'],
  2810. 'label' => ['for' => 'prefix-field'],
  2811. 'Field',
  2812. '/label',
  2813. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2814. '/div',
  2815. ];
  2816. $this->assertHtml($expected, $result);
  2817. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2818. $expected = [
  2819. 'div' => ['class' => 'input text'],
  2820. 'label' => ['for' => 'custom-id'],
  2821. 'Field',
  2822. '/label',
  2823. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2824. '/div',
  2825. ];
  2826. $this->assertHtml($expected, $result);
  2827. $result = $this->Form->radio('Model.field', ['option A']);
  2828. $expected = [
  2829. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2830. 'label' => ['for' => 'prefix-model-field-0'],
  2831. ['input' => [
  2832. 'type' => 'radio',
  2833. 'name' => 'Model[field]',
  2834. 'value' => '0',
  2835. 'id' => 'prefix-model-field-0',
  2836. ]],
  2837. 'option A',
  2838. '/label',
  2839. ];
  2840. $this->assertHtml($expected, $result);
  2841. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2842. $expected = [
  2843. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2844. 'label' => ['for' => 'prefix-model-field-0'],
  2845. ['input' => [
  2846. 'type' => 'radio',
  2847. 'name' => 'Model[field]',
  2848. 'value' => '0',
  2849. 'id' => 'prefix-model-field-0',
  2850. ]],
  2851. 'option A',
  2852. '/label',
  2853. ];
  2854. $this->assertHtml($expected, $result);
  2855. $result = $this->Form->select(
  2856. 'Model.multi_field',
  2857. ['first'],
  2858. ['multiple' => 'checkbox']
  2859. );
  2860. $expected = [
  2861. 'input' => [
  2862. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'prefix-model-multi-field',
  2863. ],
  2864. ['div' => ['class' => 'checkbox']],
  2865. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2866. ['input' => [
  2867. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2868. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  2869. ]],
  2870. 'first',
  2871. '/label',
  2872. '/div',
  2873. ];
  2874. $this->assertHtml($expected, $result);
  2875. $this->Form->end();
  2876. $result = $this->Form->control('field');
  2877. $expected = [
  2878. 'div' => ['class' => 'input text'],
  2879. 'label' => ['for' => 'field'],
  2880. 'Field',
  2881. '/label',
  2882. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2883. '/div',
  2884. ];
  2885. $this->assertHtml($expected, $result);
  2886. }
  2887. /**
  2888. * testControlZero method
  2889. *
  2890. * Test that inputs with 0 can be created.
  2891. */
  2892. public function testControlZero(): void
  2893. {
  2894. $this->getTableLocator()->get('Contacts', [
  2895. 'className' => ContactsTable::class,
  2896. ]);
  2897. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2898. $result = $this->Form->control('0');
  2899. $expected = [
  2900. 'div' => ['class' => 'input text'],
  2901. 'label' => ['for' => '0'], '/label',
  2902. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2903. '/div',
  2904. ];
  2905. $this->assertHtml($expected, $result);
  2906. }
  2907. /**
  2908. * testControlCheckbox method
  2909. *
  2910. * Test control() with checkbox creation.
  2911. */
  2912. public function testControlCheckbox(): void
  2913. {
  2914. $articles = $this->getTableLocator()->get('Articles');
  2915. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  2916. $article = $articles->newEmptyEntity();
  2917. $this->Form->create($article);
  2918. $result = $this->Form->control('Articles.active');
  2919. $expected = [
  2920. 'div' => ['class' => 'input checkbox'],
  2921. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2922. 'label' => ['for' => 'articles-active'],
  2923. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  2924. 'Active',
  2925. '/label',
  2926. '/div',
  2927. ];
  2928. $this->assertHtml($expected, $result);
  2929. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  2930. $expected = [
  2931. 'div' => ['class' => 'input checkbox'],
  2932. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2933. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2934. '/div',
  2935. ];
  2936. $this->assertHtml($expected, $result);
  2937. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  2938. $expected = [
  2939. 'div' => ['class' => 'input checkbox'],
  2940. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2941. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2942. '/div',
  2943. ];
  2944. $this->assertHtml($expected, $result);
  2945. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  2946. $expected = [
  2947. 'div' => ['class' => 'input checkbox'],
  2948. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2949. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2950. '/div',
  2951. ];
  2952. $this->assertHtml($expected, $result);
  2953. $result = $this->Form->control('Articles.disabled', [
  2954. 'label' => 'Disabled',
  2955. 'type' => 'checkbox',
  2956. 'data-foo' => 'disabled',
  2957. ]);
  2958. $expected = [
  2959. 'div' => ['class' => 'input checkbox'],
  2960. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  2961. 'label' => ['for' => 'articles-disabled'],
  2962. ['input' => [
  2963. 'type' => 'checkbox',
  2964. 'name' => 'Articles[disabled]',
  2965. 'value' => '1',
  2966. 'id' => 'articles-disabled',
  2967. 'data-foo' => 'disabled',
  2968. ]],
  2969. 'Disabled',
  2970. '/label',
  2971. '/div',
  2972. ];
  2973. $this->assertHtml($expected, $result);
  2974. $result = $this->Form->control('Articles.confirm', [
  2975. 'label' => 'Confirm <b>me</b>!',
  2976. 'type' => 'checkbox',
  2977. 'escape' => false,
  2978. ]);
  2979. $expected = [
  2980. 'div' => ['class' => 'input checkbox'],
  2981. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  2982. 'label' => ['for' => 'articles-confirm'],
  2983. ['input' => [
  2984. 'type' => 'checkbox',
  2985. 'name' => 'Articles[confirm]',
  2986. 'value' => '1',
  2987. 'id' => 'articles-confirm',
  2988. ]],
  2989. 'Confirm <b>me</b>!',
  2990. '/label',
  2991. '/div',
  2992. ];
  2993. $this->assertHtml($expected, $result);
  2994. }
  2995. /**
  2996. * testControlHidden method
  2997. *
  2998. * Test that control() does not create wrapping div and label tag for hidden fields.
  2999. */
  3000. public function testControlHidden(): void
  3001. {
  3002. $this->getTableLocator()->get('ValidateUsers', [
  3003. 'className' => ValidateUsersTable::class,
  3004. ]);
  3005. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3006. $result = $this->Form->control('ValidateUser.id');
  3007. $expected = [
  3008. 'input' => ['name', 'type' => 'hidden', 'id'],
  3009. ];
  3010. $this->assertHtml($expected, $result);
  3011. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3012. $expected = [
  3013. 'input' => ['name', 'type' => 'hidden', 'id'],
  3014. ];
  3015. $this->assertHtml($expected, $result);
  3016. }
  3017. /**
  3018. * testControlDatetime method
  3019. *
  3020. * Test form->control() with datetime.
  3021. */
  3022. public function testControlDatetime(): void
  3023. {
  3024. $result = $this->Form->control('prueba', [
  3025. 'type' => 'datetime',
  3026. 'value' => new DateTime('2019-09-27 02:52:43'),
  3027. ]);
  3028. $expected = [
  3029. 'div' => ['class' => 'input datetime'],
  3030. 'label' => ['for' => 'prueba'],
  3031. 'Prueba',
  3032. '/label',
  3033. 'input' => [
  3034. 'name' => 'prueba',
  3035. 'id' => 'prueba',
  3036. 'type' => 'datetime-local',
  3037. 'value' => '2019-09-27T02:52:43',
  3038. 'step' => '1',
  3039. ],
  3040. '/div',
  3041. ];
  3042. $this->assertHtml($expected, $result);
  3043. }
  3044. /**
  3045. * testControlDatetimeIdPrefix method
  3046. *
  3047. * Test form->control() with datetime with id prefix.
  3048. */
  3049. public function testControlDatetimeIdPrefix(): void
  3050. {
  3051. $this->Form->create(null, ['idPrefix' => 'prefix']);
  3052. $result = $this->Form->control('prueba', [
  3053. 'type' => 'datetime',
  3054. ]);
  3055. $expected = [
  3056. 'div' => ['class' => 'input datetime'],
  3057. 'label' => ['for' => 'prefix-prueba'],
  3058. 'Prueba',
  3059. '/label',
  3060. 'input' => [
  3061. 'name' => 'prueba',
  3062. 'id' => 'prefix-prueba',
  3063. 'type' => 'datetime-local',
  3064. 'value' => '',
  3065. 'step' => '1',
  3066. ],
  3067. '/div',
  3068. ];
  3069. $this->assertHtml($expected, $result);
  3070. }
  3071. /**
  3072. * testControlDatetimeStep method
  3073. *
  3074. * Test form->control() with datetime with custom step size.
  3075. */
  3076. public function testControlDatetimeStep(): void
  3077. {
  3078. $result = $this->Form->control('prueba', [
  3079. 'type' => 'datetime',
  3080. 'value' => new DateTime('2019-09-27 02:52:43'),
  3081. 'step' => '0.5',
  3082. ]);
  3083. $expected = [
  3084. 'div' => ['class' => 'input datetime'],
  3085. 'label' => ['for' => 'prueba'],
  3086. 'Prueba',
  3087. '/label',
  3088. 'input' => [
  3089. 'name' => 'prueba',
  3090. 'id' => 'prueba',
  3091. 'type' => 'datetime-local',
  3092. 'value' => '2019-09-27T02:52:43.000',
  3093. 'step' => '0.5',
  3094. ],
  3095. '/div',
  3096. ];
  3097. $this->assertHtml($expected, $result);
  3098. }
  3099. /**
  3100. * testControlCheckboxWithDisabledElements method
  3101. *
  3102. * Test generating checkboxes with disabled elements.
  3103. */
  3104. public function testControlCheckboxWithDisabledElements(): void
  3105. {
  3106. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3107. $result = $this->Form->control('Contact.multiple', [
  3108. 'multiple' => 'checkbox',
  3109. 'disabled' => 'disabled',
  3110. 'options' => $options,
  3111. ]);
  3112. $expected = [
  3113. ['div' => ['class' => 'input select']],
  3114. ['label' => ['for' => 'contact-multiple']],
  3115. 'Multiple',
  3116. '/label',
  3117. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '', 'id' => 'contact-multiple']],
  3118. ['div' => ['class' => 'checkbox']],
  3119. ['label' => ['for' => 'contact-multiple-1']],
  3120. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3121. 'One',
  3122. '/label',
  3123. '/div',
  3124. ['div' => ['class' => 'checkbox']],
  3125. ['label' => ['for' => 'contact-multiple-2']],
  3126. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3127. 'Two',
  3128. '/label',
  3129. '/div',
  3130. ['div' => ['class' => 'checkbox']],
  3131. ['label' => ['for' => 'contact-multiple-3']],
  3132. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3133. 'Three',
  3134. '/label',
  3135. '/div',
  3136. '/div',
  3137. ];
  3138. $this->assertHtml($expected, $result);
  3139. // make sure 50 does only disable 50, and not 50f5c0cf
  3140. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3141. $disabled = [50];
  3142. $expected = [
  3143. ['div' => ['class' => 'input select']],
  3144. ['label' => ['for' => 'contact-multiple']],
  3145. 'Multiple',
  3146. '/label',
  3147. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'contact-multiple']],
  3148. ['div' => ['class' => 'checkbox']],
  3149. ['label' => ['for' => 'contact-multiple-50']],
  3150. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3151. 'Fifty',
  3152. '/label',
  3153. '/div',
  3154. ['div' => ['class' => 'checkbox']],
  3155. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3156. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3157. 'Stringy',
  3158. '/label',
  3159. '/div',
  3160. '/div',
  3161. ];
  3162. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3163. $this->assertHtml($expected, $result);
  3164. }
  3165. /**
  3166. * testControlWithLeadingInteger method
  3167. *
  3168. * Test input name with leading integer, ensure attributes are generated correctly.
  3169. */
  3170. public function testControlWithLeadingInteger(): void
  3171. {
  3172. $result = $this->Form->text('0.Node.title');
  3173. $expected = [
  3174. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3175. ];
  3176. $this->assertHtml($expected, $result);
  3177. }
  3178. /**
  3179. * testControlSelectType method
  3180. *
  3181. * Test form->control() with select type inputs.
  3182. */
  3183. public function testControlSelectType(): void
  3184. {
  3185. $result = $this->Form->control(
  3186. 'email',
  3187. [
  3188. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3189. );
  3190. $expected = [
  3191. 'div' => ['class' => 'input select'],
  3192. 'label' => ['for' => 'email'],
  3193. 'Email',
  3194. '/label',
  3195. ['select' => ['name' => 'email', 'id' => 'email']],
  3196. ['option' => ['value' => '']],
  3197. '/option',
  3198. ['option' => ['value' => 'è']],
  3199. 'Firést',
  3200. '/option',
  3201. ['option' => ['value' => 'é']],
  3202. 'Secoènd',
  3203. '/option',
  3204. '/select',
  3205. '/div',
  3206. ];
  3207. $this->assertHtml($expected, $result);
  3208. $result = $this->Form->control(
  3209. 'email',
  3210. [
  3211. 'options' => ['First', 'Second'], 'empty' => true]
  3212. );
  3213. $expected = [
  3214. 'div' => ['class' => 'input select'],
  3215. 'label' => ['for' => 'email'],
  3216. 'Email',
  3217. '/label',
  3218. ['select' => ['name' => 'email', 'id' => 'email']],
  3219. ['option' => ['value' => '']],
  3220. '/option',
  3221. ['option' => ['value' => '0']],
  3222. 'First',
  3223. '/option',
  3224. ['option' => ['value' => '1']],
  3225. 'Second',
  3226. '/option',
  3227. '/select',
  3228. '/div',
  3229. ];
  3230. $this->assertHtml($expected, $result);
  3231. $result = $this->Form->control('email', [
  3232. 'type' => 'select',
  3233. 'options' => new ArrayObject(['First', 'Second']),
  3234. 'empty' => true,
  3235. ]);
  3236. $this->assertHtml($expected, $result);
  3237. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3238. $this->View->setRequest(
  3239. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3240. );
  3241. $this->Form->create();
  3242. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3243. $expected = [
  3244. 'div' => ['class' => 'input select'],
  3245. 'label' => ['for' => 'model-user-id'],
  3246. 'User',
  3247. '/label',
  3248. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3249. ['option' => ['value' => '']],
  3250. '/option',
  3251. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3252. 'good',
  3253. '/option',
  3254. ['option' => ['value' => 'other']],
  3255. 'bad',
  3256. '/option',
  3257. '/select',
  3258. '/div',
  3259. ];
  3260. $this->assertHtml($expected, $result);
  3261. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3262. $this->View->setRequest(
  3263. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3264. );
  3265. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3266. $expected = [
  3267. 'div' => ['class' => 'input select'],
  3268. 'label' => ['for' => 'thing-user-id'],
  3269. 'User',
  3270. '/label',
  3271. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3272. ['option' => ['value' => '']],
  3273. 'Some Empty',
  3274. '/option',
  3275. ['option' => ['value' => 'value']],
  3276. 'good',
  3277. '/option',
  3278. ['option' => ['value' => 'other']],
  3279. 'bad',
  3280. '/option',
  3281. '/select',
  3282. '/div',
  3283. ];
  3284. $this->assertHtml($expected, $result);
  3285. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3286. $this->View->setRequest(
  3287. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3288. );
  3289. $this->Form->create();
  3290. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3291. $expected = [
  3292. 'div' => ['class' => 'input select'],
  3293. 'label' => ['for' => 'thing-user-id'],
  3294. 'User',
  3295. '/label',
  3296. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3297. ['option' => ['value' => '']],
  3298. 'Some Empty',
  3299. '/option',
  3300. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3301. 'good',
  3302. '/option',
  3303. ['option' => ['value' => 'other']],
  3304. 'bad',
  3305. '/option',
  3306. '/select',
  3307. '/div',
  3308. ];
  3309. $this->assertHtml($expected, $result);
  3310. $result = $this->Form->control('Publisher.id', [
  3311. 'label' => 'Publisher',
  3312. 'type' => 'select',
  3313. 'multiple' => 'checkbox',
  3314. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3315. ]);
  3316. $expected = [
  3317. ['div' => ['class' => 'input select']],
  3318. ['label' => ['for' => 'publisher-id']],
  3319. 'Publisher',
  3320. '/label',
  3321. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'publisher-id'],
  3322. ['div' => ['class' => 'checkbox']],
  3323. ['label' => ['for' => 'publisher-id-value-1']],
  3324. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3325. 'Label 1',
  3326. '/label',
  3327. '/div',
  3328. ['div' => ['class' => 'checkbox']],
  3329. ['label' => ['for' => 'publisher-id-value-2']],
  3330. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3331. 'Label 2',
  3332. '/label',
  3333. '/div',
  3334. '/div',
  3335. ];
  3336. $this->assertHtml($expected, $result);
  3337. $articlesTable = $this->getTableLocator()->get('Articles');
  3338. $articlesTable->getSchema()->setColumnType(
  3339. 'published',
  3340. EnumType::from(ArticleStatus::class)
  3341. );
  3342. $this->Form->create($articlesTable->newEmptyEntity());
  3343. $result = $this->Form->control('published');
  3344. $expected = [
  3345. 'div' => ['class' => 'input select'],
  3346. 'label' => ['for' => 'published'],
  3347. 'Published',
  3348. '/label',
  3349. 'select' => ['name' => 'published', 'id' => 'published'],
  3350. ['option' => ['value' => 'Y']],
  3351. 'Published',
  3352. '/option',
  3353. ['option' => ['value' => 'N', 'selected' => 'selected']],
  3354. 'Unpublished',
  3355. '/option',
  3356. '/select',
  3357. '/div',
  3358. ];
  3359. $this->assertHtml($expected, $result);
  3360. $articlesTable->getSchema()->setColumnType(
  3361. 'published',
  3362. EnumType::from(ArticleStatusLabel::class)
  3363. );
  3364. $this->Form->create($articlesTable->newEmptyEntity());
  3365. $result = $this->Form->control('published');
  3366. $expected = [
  3367. 'div' => ['class' => 'input select'],
  3368. 'label' => ['for' => 'published'],
  3369. 'Published',
  3370. '/label',
  3371. 'select' => ['name' => 'published', 'id' => 'published'],
  3372. ['option' => ['value' => 'Y']],
  3373. 'Is Published',
  3374. '/option',
  3375. ['option' => ['value' => 'N', 'selected' => 'selected']],
  3376. 'Is Unpublished',
  3377. '/option',
  3378. '/select',
  3379. '/div',
  3380. ];
  3381. $this->assertHtml($expected, $result);
  3382. $articlesTable->getSchema()->setColumnType(
  3383. 'published',
  3384. EnumType::from(ArticleStatusLabelInterface::class)
  3385. );
  3386. $this->Form->create($articlesTable->newEmptyEntity());
  3387. $result = $this->Form->control('published');
  3388. $this->assertHtml($expected, $result);
  3389. $articlePriosTable = $this->getTableLocator()->get('ArticlePrios');
  3390. $articlePriosTable->getSchema()->setColumnType(
  3391. 'priority',
  3392. EnumType::from(Priority::class)
  3393. );
  3394. $this->Form->create($articlePriosTable->newEmptyEntity());
  3395. // Select empty by default
  3396. $result = $this->Form->control('priority', ['empty' => ['' => ' - please select - '], 'default' => '']);
  3397. $expected = [
  3398. 'div' => ['class' => 'input select'],
  3399. 'label' => ['for' => 'priority'],
  3400. 'Priority',
  3401. '/label',
  3402. 'select' => ['name' => 'priority', 'id' => 'priority'],
  3403. ['option' => ['value' => '', 'selected' => 'selected']],
  3404. ' - please select - ',
  3405. '/option',
  3406. ['option' => ['value' => '1']],
  3407. 'Is Low',
  3408. '/option',
  3409. ['option' => ['value' => '2']],
  3410. 'Is Medium',
  3411. '/option',
  3412. ['option' => ['value' => '3']],
  3413. 'Is High',
  3414. '/option',
  3415. '/select',
  3416. '/div',
  3417. ];
  3418. $this->assertHtml($expected, $result);
  3419. }
  3420. /**
  3421. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3422. *
  3423. * Test that control() and a non standard primary key makes a hidden input by default.
  3424. */
  3425. public function testControlWithNonStandardPrimaryKeyMakesHidden(): void
  3426. {
  3427. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3428. $this->Form->create($this->article);
  3429. $result = $this->Form->control('title');
  3430. $expected = [
  3431. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3432. ];
  3433. $this->assertHtml($expected, $result);
  3434. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3435. $this->Form->create($this->article);
  3436. $result = $this->Form->control('title');
  3437. $expected = [
  3438. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3439. ];
  3440. $this->assertHtml($expected, $result);
  3441. $result = $this->Form->control('body');
  3442. $expected = [
  3443. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3444. ];
  3445. $this->assertHtml($expected, $result);
  3446. }
  3447. /**
  3448. * testControlOverridingMagicSelectType method
  3449. *
  3450. * Test that overriding the magic select type widget is possible.
  3451. */
  3452. public function testControlOverridingMagicSelectType(): void
  3453. {
  3454. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3455. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3456. $expected = [
  3457. 'div' => ['class' => 'input text'],
  3458. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3459. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3460. '/div',
  3461. ];
  3462. $this->assertHtml($expected, $result);
  3463. //Check that magic types still work for plural/singular vars
  3464. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3465. $result = $this->Form->control('Model.type');
  3466. $expected = [
  3467. 'div' => ['class' => 'input select'],
  3468. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3469. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3470. ['option' => ['value' => 'value']], 'good', '/option',
  3471. ['option' => ['value' => 'other']], 'bad', '/option',
  3472. '/select',
  3473. '/div',
  3474. ];
  3475. $this->assertHtml($expected, $result);
  3476. }
  3477. /**
  3478. * testControlMagicTypeDoesNotOverride method
  3479. *
  3480. * Test that inferred types do not override developer input.
  3481. */
  3482. public function testControlMagicTypeDoesNotOverride(): void
  3483. {
  3484. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3485. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3486. $expected = [
  3487. 'div' => ['class' => 'input checkbox'],
  3488. ['input' => [
  3489. 'type' => 'hidden',
  3490. 'name' => 'Model[user]',
  3491. 'value' => 0,
  3492. ]],
  3493. 'label' => ['for' => 'model-user'],
  3494. ['input' => [
  3495. 'name' => 'Model[user]',
  3496. 'type' => 'checkbox',
  3497. 'id' => 'model-user',
  3498. 'value' => 1,
  3499. ]],
  3500. 'User',
  3501. '/label',
  3502. '/div',
  3503. ];
  3504. $this->assertHtml($expected, $result);
  3505. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3506. $options = [
  3507. 1 => 'blue',
  3508. 2 => 'red',
  3509. ];
  3510. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3511. $expected = [
  3512. 'div' => ['class' => 'input select'],
  3513. 'label' => ['for' => 'tags-ids'],
  3514. 'Tags',
  3515. '/label',
  3516. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => '', 'id' => 'tags-ids'],
  3517. ['div' => ['class' => 'checkbox']],
  3518. ['label' => ['for' => 'tags-ids-1']],
  3519. ['input' => [
  3520. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3521. 'value' => '1', 'name' => 'tags[_ids][]',
  3522. ]],
  3523. 'blue',
  3524. '/label',
  3525. '/div',
  3526. ['div' => ['class' => 'checkbox']],
  3527. ['label' => ['for' => 'tags-ids-2']],
  3528. ['input' => [
  3529. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3530. 'value' => '2', 'name' => 'tags[_ids][]',
  3531. ]],
  3532. 'red',
  3533. '/label',
  3534. '/div',
  3535. '/div',
  3536. ];
  3537. $this->assertHtml($expected, $result);
  3538. }
  3539. /**
  3540. * testControlMagicSelectForTypeNumber method
  3541. *
  3542. * Test that magic control() selects are created for type=number.
  3543. */
  3544. public function testControlMagicSelectForTypeNumber(): void
  3545. {
  3546. $this->getTableLocator()->get('ValidateUsers', [
  3547. 'className' => ValidateUsersTable::class,
  3548. ]);
  3549. $entity = new Entity(['balance' => 1]);
  3550. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3551. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3552. $result = $this->Form->control('balance');
  3553. $expected = [
  3554. 'div' => ['class' => 'input select'],
  3555. 'label' => ['for' => 'balance'],
  3556. 'Balance',
  3557. '/label',
  3558. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3559. ['option' => ['value' => '0']],
  3560. 'nothing',
  3561. '/option',
  3562. ['option' => ['value' => '1', 'selected' => 'selected']],
  3563. 'some',
  3564. '/option',
  3565. ['option' => ['value' => '100']],
  3566. 'a lot',
  3567. '/option',
  3568. '/select',
  3569. '/div',
  3570. ];
  3571. $this->assertHtml($expected, $result);
  3572. }
  3573. /**
  3574. * testInvalidControlTypeOption method
  3575. *
  3576. * Test invalid 'input' type option to control() function.
  3577. */
  3578. public function testInvalidControlTypeOption(): void
  3579. {
  3580. $this->expectException(InvalidArgumentException::class);
  3581. $this->expectExceptionMessage('Invalid type `input` used for field `text`.');
  3582. $this->Form->control('text', ['type' => 'input']);
  3583. }
  3584. /**
  3585. * testControlMagicSelectChangeToRadio method
  3586. *
  3587. * Test that magic control() selects can easily be converted into radio types without error.
  3588. */
  3589. public function testControlMagicSelectChangeToRadio(): void
  3590. {
  3591. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3592. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3593. $this->assertStringContainsString('input type="radio"', $result);
  3594. }
  3595. /**
  3596. * testFormControlSubmit method
  3597. *
  3598. * Test correct results for form::control() and type submit.
  3599. */
  3600. public function testFormControlSubmit(): void
  3601. {
  3602. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3603. $expected = [
  3604. 'div' => ['class' => 'submit'],
  3605. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3606. '/div',
  3607. ];
  3608. $this->assertHtml($expected, $result);
  3609. }
  3610. /**
  3611. * testFormControls method
  3612. *
  3613. * Test correct results from Form::controls().
  3614. */
  3615. public function testFormControlsLegendFieldset(): void
  3616. {
  3617. $this->Form->create($this->article);
  3618. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3619. $expected = [
  3620. '<fieldset',
  3621. '<legend',
  3622. 'The Legend',
  3623. '/legend',
  3624. '*/fieldset',
  3625. ];
  3626. $this->assertHtml($expected, $result);
  3627. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3628. $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
  3629. $this->assertStringContainsString('<fieldset>', $result);
  3630. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3631. $this->assertStringNotContainsString('<legend>', $result);
  3632. $this->assertStringNotContainsString('<fieldset>', $result);
  3633. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3634. $this->assertStringNotContainsString('<legend>', $result);
  3635. $this->assertStringNotContainsString('<fieldset>', $result);
  3636. $this->Form->create($this->article);
  3637. $this->View->setRequest($this->View->getRequest()
  3638. ->withParam('prefix', 'admin')
  3639. ->withParam('action', 'admin_edit')
  3640. ->withParam('controller', 'articles'));
  3641. $result = $this->Form->allControls();
  3642. $expected = [
  3643. '<fieldset',
  3644. '<legend',
  3645. 'New Article',
  3646. '/legend',
  3647. '*/fieldset',
  3648. ];
  3649. $this->assertHtml($expected, $result);
  3650. $this->Form->create($this->article);
  3651. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3652. $expected = [
  3653. '<fieldset',
  3654. '<legend',
  3655. 'The Legend',
  3656. '/legend',
  3657. '*/fieldset',
  3658. ];
  3659. $this->assertHtml($expected, $result);
  3660. $this->Form->create($this->article);
  3661. $result = $this->Form->allControls([], [
  3662. 'fieldset' => [
  3663. 'class' => 'some-class some-other-class',
  3664. 'disabled' => true,
  3665. 'data-param' => 'a-param',
  3666. ],
  3667. 'legend' => 'The Legend',
  3668. ]);
  3669. $expected = [
  3670. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3671. '<legend',
  3672. 'The Legend',
  3673. '/legend',
  3674. '*/fieldset',
  3675. ];
  3676. $this->assertHtml($expected, $result);
  3677. }
  3678. /**
  3679. * testFormControls method
  3680. *
  3681. * Test the controls() method.
  3682. */
  3683. public function testFormControls(): void
  3684. {
  3685. $this->Form->create($this->article);
  3686. $result = $this->Form->allControls();
  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 text']],
  3698. '*/div',
  3699. '/fieldset',
  3700. ];
  3701. $this->assertHtml($expected, $result);
  3702. $result = $this->Form->allControls([
  3703. 'published' => ['type' => 'boolean'],
  3704. ]);
  3705. $expected = [
  3706. '<fieldset',
  3707. '<legend', 'New Article', '/legend',
  3708. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3709. ['div' => ['class' => 'input select required']],
  3710. '*/div',
  3711. ['div' => ['class' => 'input text required']],
  3712. '*/div',
  3713. ['div' => ['class' => 'input text']],
  3714. '*/div',
  3715. ['div' => ['class' => 'input boolean']],
  3716. '*/div',
  3717. '/fieldset',
  3718. ];
  3719. $this->assertHtml($expected, $result);
  3720. $this->Form->create($this->article);
  3721. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3722. $expected = [
  3723. 'fieldset' => [],
  3724. 'legend' => [],
  3725. 'Hello',
  3726. '/legend',
  3727. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3728. ['div' => ['class' => 'input select required']],
  3729. '*/div',
  3730. ['div' => ['class' => 'input text required']],
  3731. '*/div',
  3732. ['div' => ['class' => 'input text']],
  3733. '*/div',
  3734. ['div' => ['class' => 'input text']],
  3735. '*/div',
  3736. '/fieldset',
  3737. ];
  3738. $this->assertHtml($expected, $result);
  3739. $this->Form->create();
  3740. $expected = [
  3741. 'fieldset' => [],
  3742. ['div' => ['class' => 'input text']],
  3743. 'label' => ['for' => 'foo'],
  3744. 'Foo',
  3745. '/label',
  3746. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3747. '*/div',
  3748. '/fieldset',
  3749. ];
  3750. $result = $this->Form->allControls(
  3751. ['foo' => ['type' => 'text']],
  3752. ['legend' => false]
  3753. );
  3754. $this->assertHtml($expected, $result);
  3755. }
  3756. /**
  3757. * testFormControlsBlacklist method
  3758. */
  3759. public function testFormControlsBlacklist(): void
  3760. {
  3761. $this->Form->create($this->article);
  3762. $result = $this->Form->allControls([
  3763. 'id' => false,
  3764. ]);
  3765. $expected = [
  3766. '<fieldset',
  3767. '<legend', 'New Article', '/legend',
  3768. ['div' => ['class' => 'input select required']],
  3769. '*/div',
  3770. ['div' => ['class' => 'input text required']],
  3771. '*/div',
  3772. ['div' => ['class' => 'input text']],
  3773. '*/div',
  3774. ['div' => ['class' => 'input text']],
  3775. '*/div',
  3776. '/fieldset',
  3777. ];
  3778. $this->assertHtml($expected, $result);
  3779. $this->Form->create($this->article);
  3780. $result = $this->Form->allControls([
  3781. 'id' => [],
  3782. ]);
  3783. $expected = [
  3784. '<fieldset',
  3785. '<legend', 'New Article', '/legend',
  3786. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3787. ['div' => ['class' => 'input select required']],
  3788. '*/div',
  3789. ['div' => ['class' => 'input text required']],
  3790. '*/div',
  3791. ['div' => ['class' => 'input text']],
  3792. '*/div',
  3793. ['div' => ['class' => 'input text']],
  3794. '*/div',
  3795. '/fieldset',
  3796. ];
  3797. $this->assertHtml($expected, $result, true);
  3798. }
  3799. /**
  3800. * testSelectAsCheckbox method
  3801. *
  3802. * Test multi-select widget with checkbox formatting.
  3803. */
  3804. public function testSelectAsCheckbox(): void
  3805. {
  3806. $result = $this->Form->select(
  3807. 'Model.multi_field',
  3808. ['first', 'second', 'third'],
  3809. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3810. );
  3811. $expected = [
  3812. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3813. ['div' => ['class' => 'checkbox']],
  3814. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3815. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3816. 'first',
  3817. '/label',
  3818. '/div',
  3819. ['div' => ['class' => 'checkbox']],
  3820. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3821. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3822. 'second',
  3823. '/label',
  3824. '/div',
  3825. ['div' => ['class' => 'checkbox']],
  3826. ['label' => ['for' => 'model-multi-field-2']],
  3827. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3828. 'third',
  3829. '/label',
  3830. '/div',
  3831. ];
  3832. $this->assertHtml($expected, $result);
  3833. $result = $this->Form->select(
  3834. 'Model.multi_field',
  3835. ['1/2' => 'half'],
  3836. ['multiple' => 'checkbox']
  3837. );
  3838. $expected = [
  3839. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3840. ['div' => ['class' => 'checkbox']],
  3841. ['label' => ['for' => 'model-multi-field-1-2']],
  3842. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3843. 'half',
  3844. '/label',
  3845. '/div',
  3846. ];
  3847. $this->assertHtml($expected, $result);
  3848. }
  3849. /**
  3850. * testLabel method
  3851. *
  3852. * Test label generation.
  3853. */
  3854. public function testLabel(): void
  3855. {
  3856. $result = $this->Form->label('Person.name');
  3857. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3858. $this->assertHtml($expected, $result);
  3859. $result = $this->Form->label('Person.name');
  3860. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3861. $this->assertHtml($expected, $result);
  3862. $result = $this->Form->label('Person.first_name');
  3863. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3864. $this->assertHtml($expected, $result);
  3865. $result = $this->Form->label('Person.first_name', 'Your first name');
  3866. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3867. $this->assertHtml($expected, $result);
  3868. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3869. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3870. $this->assertHtml($expected, $result);
  3871. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3872. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3873. $this->assertHtml($expected, $result);
  3874. $result = $this->Form->label('Person.first_name', '');
  3875. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3876. $this->assertHtml($expected, $result);
  3877. $result = $this->Form->label('Person.2.name', '');
  3878. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3879. $this->assertHtml($expected, $result);
  3880. }
  3881. /**
  3882. * testLabelContainControl method
  3883. *
  3884. * Test that label() can accept an input with the correct template vars.
  3885. */
  3886. public function testLabelContainControl(): void
  3887. {
  3888. $this->Form->setTemplates([
  3889. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3890. ]);
  3891. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3892. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3893. ]);
  3894. $expected = [
  3895. 'label' => ['for' => 'person-accept-terms'],
  3896. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3897. 'Accept',
  3898. '/label',
  3899. ];
  3900. $this->assertHtml($expected, $result);
  3901. }
  3902. /**
  3903. * testTextbox method
  3904. *
  3905. * Test textbox element generation.
  3906. */
  3907. public function testTextbox(): void
  3908. {
  3909. $result = $this->Form->text('Model.field');
  3910. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3911. $this->assertHtml($expected, $result);
  3912. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3913. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3914. $this->assertHtml($expected, $result);
  3915. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3916. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3917. $this->assertHtml($expected, $result);
  3918. }
  3919. /**
  3920. * testTextBoxDataAndError method
  3921. *
  3922. * Test that text() hooks up with request data and error fields.
  3923. */
  3924. public function testTextBoxDataAndError(): void
  3925. {
  3926. $this->article['errors'] = [
  3927. 'Contact' => ['text' => 'wrong'],
  3928. ];
  3929. $this->View->setRequest($this->View->getRequest()
  3930. ->withData('Model.text', 'test <strong>HTML</strong> values')
  3931. ->withData('Contact.text', 'test'));
  3932. $this->Form->create($this->article);
  3933. $result = $this->Form->text('Model.text');
  3934. $expected = [
  3935. 'input' => [
  3936. 'type' => 'text',
  3937. 'name' => 'Model[text]',
  3938. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3939. ],
  3940. ];
  3941. $this->assertHtml($expected, $result);
  3942. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3943. $expected = [
  3944. 'input' => [
  3945. 'type' => 'text',
  3946. 'name' => 'Contact[text]',
  3947. 'value' => 'test',
  3948. 'id' => 'theID',
  3949. 'class' => 'form-error',
  3950. ],
  3951. ];
  3952. $this->assertHtml($expected, $result);
  3953. }
  3954. /**
  3955. * testDefaultValue method
  3956. *
  3957. * Test default value setting.
  3958. */
  3959. public function testTextDefaultValue(): void
  3960. {
  3961. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  3962. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3963. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3964. $this->assertHtml($expected, $result);
  3965. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  3966. $this->Form->create();
  3967. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3968. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3969. $this->assertHtml($expected, $result);
  3970. $Articles = $this->getTableLocator()->get('Articles');
  3971. $title = $Articles->getSchema()->getColumn('title');
  3972. $Articles->getSchema()->addColumn(
  3973. 'title',
  3974. ['default' => 'default title', 'length' => 255] + $title
  3975. );
  3976. $entity = $Articles->newEmptyEntity();
  3977. $this->Form->create($entity);
  3978. // Get default value from schema
  3979. $result = $this->Form->text('title');
  3980. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title', 'maxlength' => '255']];
  3981. $this->assertHtml($expected, $result);
  3982. // Don't get value from schema
  3983. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3984. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3985. $this->assertHtml($expected, $result);
  3986. // Custom default value overrides default value from schema
  3987. $result = $this->Form->text('title', ['default' => 'override default']);
  3988. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default', 'maxlength' => '255']];
  3989. $this->assertHtml($expected, $result);
  3990. // Default value from schema is used only for new entities.
  3991. $entity->setNew(false);
  3992. $result = $this->Form->text('title');
  3993. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3994. $this->assertHtml($expected, $result);
  3995. }
  3996. /**
  3997. * testError method
  3998. *
  3999. * Test field error generation.
  4000. */
  4001. public function testError(): void
  4002. {
  4003. $this->article['errors'] = [
  4004. 'Article' => ['field' => 'email'],
  4005. ];
  4006. $this->Form->create($this->article);
  4007. $result = $this->Form->error('Article.field');
  4008. $expected = [
  4009. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4010. 'email',
  4011. '/div',
  4012. ];
  4013. $this->assertHtml($expected, $result);
  4014. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4015. $expected = [
  4016. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4017. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4018. '/div',
  4019. ];
  4020. $this->assertHtml($expected, $result);
  4021. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4022. $expected = [
  4023. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4024. '<strong', 'Badness!', '/strong',
  4025. '/div',
  4026. ];
  4027. $this->assertHtml($expected, $result);
  4028. }
  4029. /**
  4030. * testErrorRuleName method
  4031. *
  4032. * Test error translation can use rule names for translating.
  4033. */
  4034. public function testErrorRuleName(): void
  4035. {
  4036. $this->article['errors'] = [
  4037. 'Article' => [
  4038. 'field' => ['email' => 'Your email was not good'],
  4039. ],
  4040. ];
  4041. $this->Form->create($this->article);
  4042. $result = $this->Form->error('Article.field');
  4043. $expected = [
  4044. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4045. 'Your email was not good',
  4046. '/div',
  4047. ];
  4048. $this->assertHtml($expected, $result);
  4049. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4050. $expected = [
  4051. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4052. 'Email in use',
  4053. '/div',
  4054. ];
  4055. $this->assertHtml($expected, $result);
  4056. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4057. $expected = [
  4058. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4059. 'Email in use',
  4060. '/div',
  4061. ];
  4062. $this->assertHtml($expected, $result);
  4063. $result = $this->Form->error('Article.field', [
  4064. 'email' => 'Key is preferred',
  4065. 'Your email was not good' => 'Email in use',
  4066. ]);
  4067. $expected = [
  4068. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4069. 'Key is preferred',
  4070. '/div',
  4071. ];
  4072. $this->assertHtml($expected, $result);
  4073. }
  4074. /**
  4075. * testErrorMessages method
  4076. *
  4077. * Test error with nested lists.
  4078. */
  4079. public function testErrorMessages(): void
  4080. {
  4081. $this->article['errors'] = [
  4082. 'Article' => ['field' => 'email'],
  4083. ];
  4084. $this->Form->create($this->article);
  4085. $result = $this->Form->error('Article.field', [
  4086. 'email' => 'No good!',
  4087. ]);
  4088. $expected = [
  4089. 'div' => ['class' => 'error-message', 'id' => 'article-field-error'],
  4090. 'No good!',
  4091. '/div',
  4092. ];
  4093. $this->assertHtml($expected, $result);
  4094. }
  4095. /**
  4096. * testErrorMultipleMessages method
  4097. *
  4098. * Test error() with multiple messages.
  4099. */
  4100. public function testErrorMultipleMessages(): void
  4101. {
  4102. $this->article['errors'] = [
  4103. 'field' => ['notBlank', 'email', 'Something else'],
  4104. ];
  4105. $this->Form->create($this->article);
  4106. $result = $this->Form->error('field', [
  4107. 'notBlank' => 'Cannot be empty',
  4108. 'email' => 'No good!',
  4109. ]);
  4110. $expected = [
  4111. 'div' => ['class' => 'error-message', 'id' => 'field-error'],
  4112. 'ul' => [],
  4113. '<li', 'Cannot be empty', '/li',
  4114. '<li', 'No good!', '/li',
  4115. '<li', 'Something else', '/li',
  4116. '/ul',
  4117. '/div',
  4118. ];
  4119. $this->assertHtml($expected, $result);
  4120. }
  4121. /**
  4122. * testPassword method
  4123. *
  4124. * Test password element generation.
  4125. */
  4126. public function testPassword(): void
  4127. {
  4128. $this->article['errors'] = [
  4129. 'Contact' => [
  4130. 'passwd' => 1,
  4131. ],
  4132. ];
  4133. $this->Form->create($this->article);
  4134. $result = $this->Form->password('Contact.field');
  4135. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4136. $this->assertHtml($expected, $result);
  4137. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4138. $this->Form->create($this->article);
  4139. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4140. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4141. $this->assertHtml($expected, $result);
  4142. }
  4143. /**
  4144. * testRadio method
  4145. *
  4146. * Test radio element set generation.
  4147. */
  4148. public function testRadio(): void
  4149. {
  4150. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4151. $expected = [
  4152. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4153. ['label' => ['for' => 'model-field-0']],
  4154. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4155. 'option A',
  4156. '/label',
  4157. ['label' => ['for' => 'model-field-1']],
  4158. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4159. 'option B',
  4160. '/label',
  4161. ];
  4162. $this->assertHtml($expected, $result);
  4163. $result = $this->Form->radio('Model.field', new Collection(['option A', 'option B']));
  4164. $this->assertHtml($expected, $result);
  4165. $result = $this->Form->radio(
  4166. 'Employee.vegetarian',
  4167. ['yes' => 'Yes', 'no' => 'No'],
  4168. ['form' => 'my-form', 'id' => 'id-veg']
  4169. );
  4170. $expected = [
  4171. 'input' => ['type' => 'hidden', 'name' => 'Employee[vegetarian]', 'value' => '', 'form' => 'my-form', 'id' => 'id-veg'],
  4172. ['label' => ['for' => 'id-veg-yes']],
  4173. ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'yes', 'id' => 'id-veg-yes', 'form' => 'my-form']],
  4174. 'Yes',
  4175. '/label',
  4176. ['label' => ['for' => 'id-veg-no']],
  4177. ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'no', 'id' => 'id-veg-no', 'form' => 'my-form']],
  4178. 'No',
  4179. '/label',
  4180. ];
  4181. $this->assertHtml($expected, $result);
  4182. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4183. $expected = [
  4184. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '', 'id' => 'model-field']],
  4185. ['label' => ['for' => 'model-custom-0']],
  4186. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4187. 'option A',
  4188. '/label',
  4189. ['label' => ['for' => 'model-custom-1']],
  4190. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4191. 'option B',
  4192. '/label',
  4193. ];
  4194. $this->assertHtml($expected, $result);
  4195. $result = $this->Form->radio(
  4196. 'Employee.gender',
  4197. [
  4198. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4199. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4200. ]
  4201. );
  4202. $expected = [
  4203. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'id' => 'employee-gender'],
  4204. ['label' => ['for' => 'employee-gender-male']],
  4205. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4206. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4207. 'Male',
  4208. '/label',
  4209. ['label' => ['for' => 'employee-gender-female']],
  4210. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4211. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4212. 'Female',
  4213. '/label',
  4214. ];
  4215. $this->assertHtml($expected, $result);
  4216. $article = new Article([
  4217. 'status' => ArticleStatus::Unpublished,
  4218. ]);
  4219. $this->Form->create($article);
  4220. $result = $this->Form->radio('status', ['Y' => 'Published', 'N' => 'Unpublished']);
  4221. $expected = [
  4222. 'input' => ['type' => 'hidden', 'name' => 'status', 'value' => '', 'id' => 'status'],
  4223. ['label' => ['for' => 'status-y']],
  4224. ['input' => ['type' => 'radio', 'name' => 'status', 'value' => 'Y', 'id' => 'status-y']],
  4225. 'Published',
  4226. '/label',
  4227. ['label' => ['for' => 'status-n']],
  4228. ['input' => ['type' => 'radio', 'name' => 'status', 'value' => 'N', 'id' => 'status-n', 'checked' => 'checked']],
  4229. 'Unpublished',
  4230. '/label',
  4231. ];
  4232. $this->assertHtml($expected, $result);
  4233. }
  4234. /**
  4235. * Test radio with complex options and empty disabled data.
  4236. */
  4237. public function testRadioComplexDisabled(): void
  4238. {
  4239. $options = [
  4240. ['value' => 'r', 'text' => 'red'],
  4241. ['value' => 'b', 'text' => 'blue'],
  4242. ];
  4243. $attrs = ['disabled' => []];
  4244. $result = $this->Form->radio('Model.field', $options, $attrs);
  4245. $expected = [
  4246. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4247. ['label' => ['for' => 'model-field-r']],
  4248. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4249. 'red',
  4250. '/label',
  4251. ['label' => ['for' => 'model-field-b']],
  4252. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4253. 'blue',
  4254. '/label',
  4255. ];
  4256. $this->assertHtml($expected, $result);
  4257. $attrs = ['disabled' => ['r']];
  4258. $result = $this->Form->radio('Model.field', $options, $attrs);
  4259. $this->assertStringContainsString('disabled="disabled"', $result);
  4260. }
  4261. /**
  4262. * testRadioDefaultValue method
  4263. *
  4264. * Test default value setting on radio() method.
  4265. */
  4266. public function testRadioDefaultValue(): void
  4267. {
  4268. $Articles = $this->getTableLocator()->get('Articles');
  4269. $title = $Articles->getSchema()->getColumn('title');
  4270. $Articles->getSchema()->addColumn(
  4271. 'title',
  4272. ['default' => '1'] + $title
  4273. );
  4274. $this->Form->create($Articles->newEmptyEntity());
  4275. $result = $this->Form->radio('title', ['option A', 'option B']);
  4276. $expected = [
  4277. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4278. ['label' => ['for' => 'title-0']],
  4279. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4280. 'option A',
  4281. '/label',
  4282. ['label' => ['for' => 'title-1']],
  4283. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4284. 'option B',
  4285. '/label',
  4286. ];
  4287. $this->assertHtml($expected, $result);
  4288. }
  4289. /**
  4290. * Test setting a hiddenField value on radio buttons.
  4291. */
  4292. public function testRadioHiddenFieldValue(): void
  4293. {
  4294. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4295. $expected = [
  4296. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N', 'id' => 'title']],
  4297. 'label' => ['for' => 'title-0'],
  4298. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4299. 'option A',
  4300. '/label',
  4301. ];
  4302. $this->assertHtml($expected, $result);
  4303. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => '']);
  4304. $expected = [
  4305. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4306. 'label' => ['for' => 'title-0'],
  4307. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4308. 'option A',
  4309. '/label',
  4310. ];
  4311. $this->assertHtml($expected, $result);
  4312. }
  4313. /**
  4314. * testControlRadio method
  4315. *
  4316. * Test that input works with radio types.
  4317. */
  4318. public function testControlRadio(): void
  4319. {
  4320. $result = $this->Form->control('test', [
  4321. 'type' => 'radio',
  4322. 'options' => ['A', 'B'],
  4323. ]);
  4324. $expected = [
  4325. ['div' => ['class' => 'input radio']],
  4326. '<label',
  4327. 'Test',
  4328. '/label',
  4329. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4330. ['label' => ['for' => 'test-0']],
  4331. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4332. 'A',
  4333. '/label',
  4334. ['label' => ['for' => 'test-1']],
  4335. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4336. 'B',
  4337. '/label',
  4338. '/div',
  4339. ];
  4340. $this->assertHtml($expected, $result);
  4341. $result = $this->Form->control('test', [
  4342. 'type' => 'radio',
  4343. 'options' => ['A', 'B'],
  4344. 'value' => '0',
  4345. ]);
  4346. $expected = [
  4347. ['div' => ['class' => 'input radio']],
  4348. '<label',
  4349. 'Test',
  4350. '/label',
  4351. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4352. ['label' => ['for' => 'test-0']],
  4353. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4354. 'A',
  4355. '/label',
  4356. ['label' => ['for' => 'test-1']],
  4357. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4358. 'B',
  4359. '/label',
  4360. '/div',
  4361. ];
  4362. $this->assertHtml($expected, $result);
  4363. $result = $this->Form->control('test', [
  4364. 'type' => 'radio',
  4365. 'options' => ['A', 'B'],
  4366. 'label' => false,
  4367. ]);
  4368. $expected = [
  4369. ['div' => ['class' => 'input radio']],
  4370. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4371. ['label' => ['for' => 'test-0']],
  4372. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4373. 'A',
  4374. '/label',
  4375. ['label' => ['for' => 'test-1']],
  4376. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4377. 'B',
  4378. '/label',
  4379. '/div',
  4380. ];
  4381. $this->assertHtml($expected, $result);
  4382. $result = $this->Form->control('accept', [
  4383. 'type' => 'radio',
  4384. 'options' => [
  4385. 1 => 'positive',
  4386. -1 => 'negative',
  4387. ],
  4388. 'label' => false,
  4389. ]);
  4390. $expected = [
  4391. ['div' => ['class' => 'input radio']],
  4392. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => '', 'id' => 'accept']],
  4393. ['label' => ['for' => 'accept-1']],
  4394. ['input' => [
  4395. 'type' => 'radio',
  4396. 'name' => 'accept',
  4397. 'value' => '1',
  4398. 'id' => 'accept-1',
  4399. ]],
  4400. 'positive',
  4401. '/label',
  4402. ['label' => ['for' => 'accept--1']],
  4403. ['input' => [
  4404. 'type' => 'radio',
  4405. 'name' => 'accept',
  4406. 'value' => '-1',
  4407. 'id' => 'accept--1',
  4408. ]],
  4409. 'negative',
  4410. '/label',
  4411. '/div',
  4412. ];
  4413. $this->assertHtml($expected, $result);
  4414. $articlesTable = $this->getTableLocator()->get('Articles');
  4415. $articlesTable->getSchema()->setColumnType(
  4416. 'published',
  4417. EnumType::from(ArticleStatus::class)
  4418. );
  4419. $this->Form->create($articlesTable->newEmptyEntity());
  4420. $result = $this->Form->control('published', ['type' => 'radio', 'label' => false,]);
  4421. $expected = [
  4422. ['div' => ['class' => 'input radio']],
  4423. 'input' => ['type' => 'hidden', 'name' => 'published', 'value' => '', 'id' => 'published'],
  4424. ['label' => ['for' => 'published-y']],
  4425. ['input' => ['type' => 'radio', 'name' => 'published', 'value' => 'Y', 'id' => 'published-y']],
  4426. 'Published',
  4427. '/label',
  4428. ['label' => ['for' => 'published-n']],
  4429. ['input' => ['type' => 'radio', 'name' => 'published', 'value' => 'N', 'id' => 'published-n', 'checked' => 'checked']],
  4430. 'Unpublished',
  4431. '/label',
  4432. '/div',
  4433. ];
  4434. $this->assertHtml($expected, $result);
  4435. }
  4436. /**
  4437. * testRadioNoLabel method
  4438. *
  4439. * Test that radio() works with label = false.
  4440. */
  4441. public function testRadioNoLabel(): void
  4442. {
  4443. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4444. $expected = [
  4445. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4446. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4447. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4448. ];
  4449. $this->assertHtml($expected, $result);
  4450. }
  4451. /**
  4452. * testRadioControlInsideLabel method
  4453. *
  4454. * Test generating radio input inside label ala twitter bootstrap.
  4455. */
  4456. public function testRadioControlInsideLabel(): void
  4457. {
  4458. $this->Form->setTemplates([
  4459. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4460. 'radioWrapper' => '{{label}}',
  4461. ]);
  4462. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4463. // phpcs:disable
  4464. $expected = [
  4465. ['input' => [
  4466. 'type' => 'hidden',
  4467. 'name' => 'Model[field]',
  4468. 'value' => '',
  4469. 'id' => 'model-field'
  4470. ]],
  4471. ['label' => ['for' => 'model-field-0']],
  4472. ['input' => [
  4473. 'type' => 'radio',
  4474. 'name' => 'Model[field]',
  4475. 'value' => '0',
  4476. 'id' => 'model-field-0'
  4477. ]],
  4478. 'option A',
  4479. '/label',
  4480. ['label' => ['for' => 'model-field-1']],
  4481. ['input' => [
  4482. 'type' => 'radio',
  4483. 'name' => 'Model[field]',
  4484. 'value' => '1',
  4485. 'id' => 'model-field-1'
  4486. ]],
  4487. 'option B',
  4488. '/label',
  4489. ];
  4490. // phpcs:enable
  4491. $this->assertHtml($expected, $result);
  4492. }
  4493. /**
  4494. * testRadioHiddenControlDisabling method
  4495. *
  4496. * Test disabling the hidden input for radio buttons.
  4497. */
  4498. public function testRadioHiddenControlDisabling(): void
  4499. {
  4500. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4501. $expected = [
  4502. 'label' => ['for' => 'model-1-field-0'],
  4503. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4504. 'option A',
  4505. '/label',
  4506. ];
  4507. $this->assertHtml($expected, $result);
  4508. }
  4509. /**
  4510. * testRadioOutOfRange method
  4511. *
  4512. * Test radio element set generation.
  4513. */
  4514. public function testRadioOutOfRange(): void
  4515. {
  4516. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4517. $expected = [
  4518. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4519. 'label' => ['for' => 'model-field-v'],
  4520. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4521. 'value',
  4522. '/label',
  4523. ];
  4524. $this->assertHtml($expected, $result);
  4525. }
  4526. /**
  4527. * testSelect method
  4528. *
  4529. * Test select element generation.
  4530. */
  4531. public function testSelect(): void
  4532. {
  4533. $result = $this->Form->select('Model.field', []);
  4534. $expected = [
  4535. 'select' => ['name' => 'Model[field]'],
  4536. '/select',
  4537. ];
  4538. $this->assertHtml($expected, $result);
  4539. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4540. $this->Form->create();
  4541. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4542. $expected = [
  4543. 'select' => ['name' => 'Model[field]'],
  4544. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4545. 'good',
  4546. '/option',
  4547. ['option' => ['value' => 'other']],
  4548. 'bad',
  4549. '/option',
  4550. '/select',
  4551. ];
  4552. $this->assertHtml($expected, $result);
  4553. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4554. $this->assertHtml($expected, $result);
  4555. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4556. $this->Form->create();
  4557. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4558. $expected = [
  4559. 'select' => ['name' => 'Model[field]'],
  4560. ['option' => ['value' => 'value']],
  4561. 'good',
  4562. '/option',
  4563. ['option' => ['value' => 'other']],
  4564. 'bad',
  4565. '/option',
  4566. '/select',
  4567. ];
  4568. $this->assertHtml($expected, $result);
  4569. $options = [
  4570. ['value' => 'first', 'text' => 'First'],
  4571. ['value' => 'first', 'text' => 'Another First'],
  4572. ];
  4573. $result = $this->Form->select(
  4574. 'Model.field',
  4575. $options,
  4576. ['escape' => false, 'empty' => false]
  4577. );
  4578. $expected = [
  4579. 'select' => ['name' => 'Model[field]'],
  4580. ['option' => ['value' => 'first']],
  4581. 'First',
  4582. '/option',
  4583. ['option' => ['value' => 'first']],
  4584. 'Another First',
  4585. '/option',
  4586. '/select',
  4587. ];
  4588. $this->assertHtml($expected, $result);
  4589. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4590. $this->Form->create();
  4591. $result = $this->Form->select(
  4592. 'Model.contact_id',
  4593. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4594. ['escape' => false, 'empty' => 'pick something']
  4595. );
  4596. $expected = [
  4597. 'select' => ['name' => 'Model[contact_id]'],
  4598. ['option' => ['value' => '']], 'pick something', '/option',
  4599. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4600. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4601. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4602. '/select',
  4603. ];
  4604. $this->assertHtml($expected, $result);
  4605. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4606. $this->Form->create();
  4607. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4608. $expected = [
  4609. 'select' => ['name' => 'Model[field]'],
  4610. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4611. ['option' => ['value' => '1']], 'Yes', '/option',
  4612. '/select',
  4613. ];
  4614. $this->assertHtml($expected, $result);
  4615. $article = new Article([
  4616. 'status' => ArticleStatus::Unpublished,
  4617. ]);
  4618. $this->Form->create($article);
  4619. $result = $this->Form->select('status', ['Y' => 'Published', 'N' => 'Unpublished']);
  4620. $expected = [
  4621. 'select' => ['name' => 'status'],
  4622. ['option' => ['value' => 'Y']], 'Published', '/option',
  4623. ['option' => ['value' => 'N', 'selected' => 'selected']], 'Unpublished', '/option',
  4624. '/select',
  4625. ];
  4626. $this->assertHtml($expected, $result);
  4627. }
  4628. /**
  4629. * testSelectEscapeHtml method
  4630. *
  4631. * Test that select() escapes HTML.
  4632. */
  4633. public function testSelectEscapeHtml(): void
  4634. {
  4635. $result = $this->Form->select(
  4636. 'Model.field',
  4637. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4638. ['empty' => false]
  4639. );
  4640. $expected = [
  4641. 'select' => ['name' => 'Model[field]'],
  4642. ['option' => ['value' => 'first']],
  4643. 'first &quot;html&quot; &lt;chars&gt;',
  4644. '/option',
  4645. ['option' => ['value' => 'second']],
  4646. 'value',
  4647. '/option',
  4648. '/select',
  4649. ];
  4650. $this->assertHtml($expected, $result);
  4651. $result = $this->Form->select(
  4652. 'Model.field',
  4653. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4654. ['escape' => false, 'empty' => false]
  4655. );
  4656. $expected = [
  4657. 'select' => ['name' => 'Model[field]'],
  4658. ['option' => ['value' => 'first']],
  4659. 'first "html" <chars>',
  4660. '/option',
  4661. ['option' => ['value' => 'second']],
  4662. 'value',
  4663. '/option',
  4664. '/select',
  4665. ];
  4666. $this->assertHtml($expected, $result);
  4667. }
  4668. /**
  4669. * testSelectRequired method
  4670. *
  4671. * Test select() with required and disabled attributes.
  4672. */
  4673. public function testSelectRequired(): void
  4674. {
  4675. $this->article['required'] = [
  4676. 'user_id' => true,
  4677. ];
  4678. $this->Form->create($this->article);
  4679. $result = $this->Form->select('user_id', ['option A']);
  4680. $expected = [
  4681. 'select' => [
  4682. 'name' => 'user_id',
  4683. 'required' => 'required',
  4684. ],
  4685. ['option' => ['value' => '0']], 'option A', '/option',
  4686. '/select',
  4687. ];
  4688. $this->assertHtml($expected, $result);
  4689. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4690. $expected = [
  4691. 'select' => [
  4692. 'name' => 'user_id',
  4693. 'disabled' => 'disabled',
  4694. ],
  4695. ['option' => ['value' => '0']], 'option A', '/option',
  4696. '/select',
  4697. ];
  4698. $this->assertHtml($expected, $result);
  4699. }
  4700. public function testSelectEmptyWithRequiredFalse(): void
  4701. {
  4702. $Articles = $this->getTableLocator()->get('Articles');
  4703. $validator = $Articles->getValidator('default');
  4704. $validator->allowEmptyString('user_id');
  4705. $Articles->setValidator('default', $validator);
  4706. $entity = $Articles->newEmptyEntity();
  4707. $this->Form->create($entity);
  4708. $result = $this->Form->select('user_id', ['option A']);
  4709. $expected = [
  4710. 'select' => [
  4711. 'name' => 'user_id',
  4712. ],
  4713. ['option' => ['value' => '']], '/option',
  4714. ['option' => ['value' => '0']], 'option A', '/option',
  4715. '/select',
  4716. ];
  4717. $this->assertHtml($expected, $result);
  4718. }
  4719. /**
  4720. * testNestedSelect method
  4721. *
  4722. * Test select element generation with optgroups.
  4723. */
  4724. public function testNestedSelect(): void
  4725. {
  4726. $result = $this->Form->select(
  4727. 'Model.field',
  4728. [1 => 'One', 2 => 'Two', 'Three' => [
  4729. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4730. ]],
  4731. ['empty' => false]
  4732. );
  4733. $expected = [
  4734. 'select' => ['name' => 'Model[field]'],
  4735. ['option' => ['value' => 1]],
  4736. 'One',
  4737. '/option',
  4738. ['option' => ['value' => 2]],
  4739. 'Two',
  4740. '/option',
  4741. ['optgroup' => ['label' => 'Three']],
  4742. ['option' => ['value' => 3]],
  4743. 'Three',
  4744. '/option',
  4745. ['option' => ['value' => 4]],
  4746. 'Four',
  4747. '/option',
  4748. ['option' => ['value' => 5]],
  4749. 'Five',
  4750. '/option',
  4751. '/optgroup',
  4752. '/select',
  4753. ];
  4754. $this->assertHtml($expected, $result);
  4755. }
  4756. /**
  4757. * testSelectMultiple method
  4758. *
  4759. * Test generation of multiple select elements.
  4760. */
  4761. public function testSelectMultiple(): void
  4762. {
  4763. $options = ['first', 'second', 'third'];
  4764. $result = $this->Form->select(
  4765. 'Model.multi_field',
  4766. $options,
  4767. ['form' => 'my-form', 'multiple' => true]
  4768. );
  4769. $expected = [
  4770. 'input' => [
  4771. 'type' => 'hidden',
  4772. 'name' => 'Model[multi_field]',
  4773. 'value' => '',
  4774. 'form' => 'my-form',
  4775. ],
  4776. 'select' => [
  4777. 'name' => 'Model[multi_field][]',
  4778. 'multiple' => 'multiple',
  4779. 'form' => 'my-form',
  4780. ],
  4781. ['option' => ['value' => '0']],
  4782. 'first',
  4783. '/option',
  4784. ['option' => ['value' => '1']],
  4785. 'second',
  4786. '/option',
  4787. ['option' => ['value' => '2']],
  4788. 'third',
  4789. '/option',
  4790. '/select',
  4791. ];
  4792. $this->assertHtml($expected, $result);
  4793. $result = $this->Form->select(
  4794. 'Model.multi_field',
  4795. $options,
  4796. ['multiple' => 'multiple', 'form' => 'my-form']
  4797. );
  4798. $this->assertHtml($expected, $result);
  4799. $result = $this->Form->select(
  4800. 'Model.multi_field',
  4801. $options,
  4802. ['form' => 'my-form', 'multiple' => false]
  4803. );
  4804. $this->assertStringNotContainsString('multiple', $result);
  4805. }
  4806. /**
  4807. * testCheckboxZeroValue method
  4808. *
  4809. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4810. */
  4811. public function testCheckboxZeroValue(): void
  4812. {
  4813. $result = $this->Form->control('User.get_spam', [
  4814. 'type' => 'checkbox',
  4815. 'value' => '0',
  4816. 'hiddenField' => '1',
  4817. ]);
  4818. $expected = [
  4819. 'div' => ['class' => 'input checkbox'],
  4820. 'label' => ['for' => 'user-get-spam'],
  4821. ['input' => [
  4822. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4823. 'value' => '1',
  4824. ]],
  4825. ['input' => [
  4826. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4827. 'value' => '0', 'id' => 'user-get-spam',
  4828. ]],
  4829. 'Get Spam',
  4830. '/label',
  4831. '/div',
  4832. ];
  4833. $this->assertHtml($expected, $result);
  4834. $result = $this->Form->control('User.get_spam', [
  4835. 'type' => 'checkbox',
  4836. 'value' => '0',
  4837. 'hiddenField' => '',
  4838. ]);
  4839. $expected = [
  4840. 'div' => ['class' => 'input checkbox'],
  4841. 'label' => ['for' => 'user-get-spam'],
  4842. ['input' => [
  4843. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4844. 'value' => '',
  4845. ]],
  4846. ['input' => [
  4847. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4848. 'value' => '0', 'id' => 'user-get-spam',
  4849. ]],
  4850. 'Get Spam',
  4851. '/label',
  4852. '/div',
  4853. ];
  4854. $this->assertHtml($expected, $result);
  4855. }
  4856. /**
  4857. * testHabtmSelectBox method
  4858. *
  4859. * Test generation of habtm select boxes.
  4860. */
  4861. public function testHabtmSelectBox(): void
  4862. {
  4863. $options = [
  4864. 1 => 'blue',
  4865. 2 => 'red',
  4866. 3 => 'green',
  4867. ];
  4868. $tags = [
  4869. new Entity(['id' => 1, 'name' => 'blue']),
  4870. new Entity(['id' => 3, 'name' => 'green']),
  4871. ];
  4872. $article = new Article(['tags' => $tags]);
  4873. $this->Form->create($article);
  4874. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4875. $expected = [
  4876. 'div' => ['class' => 'input select'],
  4877. 'label' => ['for' => 'tags-ids'],
  4878. 'Tags',
  4879. '/label',
  4880. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4881. 'select' => [
  4882. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4883. 'multiple' => 'multiple',
  4884. ],
  4885. ['option' => ['value' => '1', 'selected' => 'selected']],
  4886. 'blue',
  4887. '/option',
  4888. ['option' => ['value' => '2']],
  4889. 'red',
  4890. '/option',
  4891. ['option' => ['value' => '3', 'selected' => 'selected']],
  4892. 'green',
  4893. '/option',
  4894. '/select',
  4895. '/div',
  4896. ];
  4897. $this->assertHtml($expected, $result);
  4898. // make sure only 50 is selected, and not 50f5c0cf
  4899. $options = [
  4900. '1' => 'blue',
  4901. '50f5c0cf' => 'red',
  4902. '50' => 'green',
  4903. ];
  4904. $tags = [
  4905. new Entity(['id' => 1, 'name' => 'blue']),
  4906. new Entity(['id' => 50, 'name' => 'green']),
  4907. ];
  4908. $article = new Article(['tags' => $tags]);
  4909. $this->Form->create($article);
  4910. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4911. $expected = [
  4912. 'div' => ['class' => 'input select'],
  4913. 'label' => ['for' => 'tags-ids'],
  4914. 'Tags',
  4915. '/label',
  4916. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4917. 'select' => [
  4918. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4919. 'multiple' => 'multiple',
  4920. ],
  4921. ['option' => ['value' => '1', 'selected' => 'selected']],
  4922. 'blue',
  4923. '/option',
  4924. ['option' => ['value' => '50f5c0cf']],
  4925. 'red',
  4926. '/option',
  4927. ['option' => ['value' => '50', 'selected' => 'selected']],
  4928. 'green',
  4929. '/option',
  4930. '/select',
  4931. '/div',
  4932. ];
  4933. $this->assertHtml($expected, $result);
  4934. $spacecraft = [
  4935. 1 => 'Orion',
  4936. 2 => 'Helios',
  4937. ];
  4938. $this->View->set('spacecraft', $spacecraft);
  4939. $this->Form->create();
  4940. $result = $this->Form->control('spacecraft._ids');
  4941. $expected = [
  4942. 'div' => ['class' => 'input select'],
  4943. 'label' => ['for' => 'spacecraft-ids'],
  4944. 'Spacecraft',
  4945. '/label',
  4946. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4947. 'select' => [
  4948. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4949. 'multiple' => 'multiple',
  4950. ],
  4951. ['option' => ['value' => '1']],
  4952. 'Orion',
  4953. '/option',
  4954. ['option' => ['value' => '2']],
  4955. 'Helios',
  4956. '/option',
  4957. '/select',
  4958. '/div',
  4959. ];
  4960. $this->assertHtml($expected, $result);
  4961. }
  4962. /**
  4963. * testErrorsForBelongsToManySelect method
  4964. *
  4965. * Tests that errors for belongsToMany select fields are being
  4966. * picked up properly.
  4967. */
  4968. public function testErrorsForBelongsToManySelect(): void
  4969. {
  4970. $spacecraft = [
  4971. 1 => 'Orion',
  4972. 2 => 'Helios',
  4973. ];
  4974. $this->View->set('spacecraft', $spacecraft);
  4975. $article = new Article();
  4976. $article->setError('spacecraft', ['Invalid']);
  4977. $this->Form->create($article);
  4978. $result = $this->Form->control('spacecraft._ids');
  4979. $expected = [
  4980. ['div' => ['class' => 'input select error']],
  4981. 'label' => ['for' => 'spacecraft-ids'],
  4982. 'Spacecraft',
  4983. '/label',
  4984. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4985. 'select' => [
  4986. 'name' => 'spacecraft[_ids][]',
  4987. 'id' => 'spacecraft-ids',
  4988. 'multiple' => 'multiple',
  4989. ],
  4990. ['option' => ['value' => '1']],
  4991. 'Orion',
  4992. '/option',
  4993. ['option' => ['value' => '2']],
  4994. 'Helios',
  4995. '/option',
  4996. '/select',
  4997. ['div' => ['class' => 'error-message', 'id' => 'spacecraft-error']],
  4998. 'Invalid',
  4999. '/div',
  5000. '/div',
  5001. ];
  5002. $this->assertHtml($expected, $result);
  5003. }
  5004. /**
  5005. * testSelectMultipleCheckboxes method
  5006. *
  5007. * Test generation of multi select elements in checkbox format.
  5008. */
  5009. public function testSelectMultipleCheckboxes(): void
  5010. {
  5011. $result = $this->Form->select(
  5012. 'Model.multi_field',
  5013. ['first', 'second', 'third'],
  5014. ['multiple' => 'checkbox']
  5015. );
  5016. $expected = [
  5017. 'input' => [
  5018. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  5019. ],
  5020. ['div' => ['class' => 'checkbox']],
  5021. ['label' => ['for' => 'model-multi-field-0']],
  5022. ['input' => [
  5023. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5024. 'value' => '0', 'id' => 'model-multi-field-0',
  5025. ]],
  5026. 'first',
  5027. '/label',
  5028. '/div',
  5029. ['div' => ['class' => 'checkbox']],
  5030. ['label' => ['for' => 'model-multi-field-1']],
  5031. ['input' => [
  5032. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5033. 'value' => '1', 'id' => 'model-multi-field-1',
  5034. ]],
  5035. 'second',
  5036. '/label',
  5037. '/div',
  5038. ['div' => ['class' => 'checkbox']],
  5039. ['label' => ['for' => 'model-multi-field-2']],
  5040. ['input' => [
  5041. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5042. 'value' => '2', 'id' => 'model-multi-field-2',
  5043. ]],
  5044. 'third',
  5045. '/label',
  5046. '/div',
  5047. ];
  5048. $this->assertHtml($expected, $result);
  5049. $result = $this->Form->select(
  5050. 'Model.multi_field',
  5051. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5052. ['multiple' => 'checkbox']
  5053. );
  5054. $expected = [
  5055. 'input' => [
  5056. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  5057. ],
  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. 'first',
  5065. '/label',
  5066. '/div',
  5067. ['div' => ['class' => 'checkbox']],
  5068. ['label' => ['for' => 'model-multi-field-a++']],
  5069. ['input' => [
  5070. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5071. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  5072. ]],
  5073. 'second',
  5074. '/label',
  5075. '/div',
  5076. ['div' => ['class' => 'checkbox']],
  5077. ['label' => ['for' => 'model-multi-field-a+++']],
  5078. ['input' => [
  5079. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5080. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  5081. ]],
  5082. 'third',
  5083. '/label',
  5084. '/div',
  5085. ];
  5086. $this->assertHtml($expected, $result);
  5087. $result = $this->Form->select(
  5088. 'Model.multi_field',
  5089. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5090. ['multiple' => 'checkbox']
  5091. );
  5092. $expected = [
  5093. 'input' => [
  5094. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  5095. ],
  5096. ['div' => ['class' => 'checkbox']],
  5097. ['label' => ['for' => 'model-multi-field-a-b']],
  5098. ['input' => [
  5099. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5100. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  5101. ]],
  5102. 'first',
  5103. '/label',
  5104. '/div',
  5105. ['div' => ['class' => 'checkbox']],
  5106. ['label' => ['for' => 'model-multi-field-a-b1']],
  5107. ['input' => [
  5108. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5109. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  5110. ]],
  5111. 'second',
  5112. '/label',
  5113. '/div',
  5114. ['div' => ['class' => 'checkbox']],
  5115. ['label' => ['for' => 'model-multi-field-a-b2']],
  5116. ['input' => [
  5117. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5118. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  5119. ]],
  5120. 'third',
  5121. '/label',
  5122. '/div',
  5123. ];
  5124. $this->assertHtml($expected, $result);
  5125. }
  5126. /**
  5127. * testSelectMultipleCheckboxRequestData method
  5128. *
  5129. * Ensure that multiCheckbox reads from the request data.
  5130. */
  5131. public function testSelectMultipleCheckboxRequestData(): void
  5132. {
  5133. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5134. $result = $this->Form->select(
  5135. 'Model.tags',
  5136. ['1' => 'first', 'Array' => 'Array'],
  5137. ['multiple' => 'checkbox']
  5138. );
  5139. $expected = [
  5140. 'input' => [
  5141. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'model-tags',
  5142. ],
  5143. ['div' => ['class' => 'checkbox']],
  5144. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5145. ['input' => [
  5146. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5147. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5148. ]],
  5149. 'first',
  5150. '/label',
  5151. '/div',
  5152. ['div' => ['class' => 'checkbox']],
  5153. ['label' => ['for' => 'model-tags-array']],
  5154. ['input' => [
  5155. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5156. 'value' => 'Array', 'id' => 'model-tags-array',
  5157. ]],
  5158. 'Array',
  5159. '/label',
  5160. '/div',
  5161. ];
  5162. $this->assertHtml($expected, $result);
  5163. }
  5164. /**
  5165. * testSelectMultipleCheckboxSecurity method
  5166. *
  5167. * Checks the security hash array generated for multiple-input checkbox elements.
  5168. */
  5169. public function testSelectMultipleCheckboxSecurity(): void
  5170. {
  5171. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5172. $this->Form->create();
  5173. $this->Form->select(
  5174. 'Model.multi_field',
  5175. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5176. ['multiple' => 'checkbox']
  5177. );
  5178. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5179. $this->assertEquals(['Model.multi_field'], $fields);
  5180. $result = $this->Form->secure();
  5181. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  5182. $hash = urlencode($hash . ':');
  5183. $this->assertStringContainsString('"' . $hash . '"', $result);
  5184. }
  5185. /**
  5186. * testSelectMultipleSecureWithNoOptions method
  5187. *
  5188. * Multiple select elements should always be secured as they always participate
  5189. * in the POST data.
  5190. */
  5191. public function testSelectMultipleSecureWithNoOptions(): void
  5192. {
  5193. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5194. $this->Form->create();
  5195. $this->Form->select(
  5196. 'Model.select',
  5197. [],
  5198. ['multiple' => true]
  5199. );
  5200. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5201. $this->assertEquals(['Model.select'], $result);
  5202. }
  5203. /**
  5204. * testSelectNoSecureWithNoOptions method
  5205. *
  5206. * When a select box has no options it should not be added to the fields list
  5207. * as it always fail post validation.
  5208. */
  5209. public function testSelectNoSecureWithNoOptions(): void
  5210. {
  5211. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5212. $this->Form->create();
  5213. $this->Form->select(
  5214. 'Model.select',
  5215. []
  5216. );
  5217. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5218. $this->assertEquals([], $result);
  5219. $this->Form->select(
  5220. 'Model.user_id',
  5221. [],
  5222. ['empty' => true]
  5223. );
  5224. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5225. $this->assertEquals(['Model.user_id'], $result);
  5226. }
  5227. /**
  5228. * testControlMultipleCheckboxes method
  5229. *
  5230. * Test control() resulting in multi select elements being generated.
  5231. */
  5232. public function testControlMultipleCheckboxes(): void
  5233. {
  5234. $result = $this->Form->control('Model.multi_field', [
  5235. 'options' => ['first', 'second', 'third'],
  5236. 'multiple' => 'checkbox',
  5237. ]);
  5238. $expected = [
  5239. ['div' => ['class' => 'input select']],
  5240. ['label' => ['for' => 'model-multi-field']],
  5241. 'Multi Field',
  5242. '/label',
  5243. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5244. ['div' => ['class' => 'checkbox']],
  5245. ['label' => ['for' => 'model-multi-field-0']],
  5246. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5247. 'first',
  5248. '/label',
  5249. '/div',
  5250. ['div' => ['class' => 'checkbox']],
  5251. ['label' => ['for' => 'model-multi-field-1']],
  5252. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5253. 'second',
  5254. '/label',
  5255. '/div',
  5256. ['div' => ['class' => 'checkbox']],
  5257. ['label' => ['for' => 'model-multi-field-2']],
  5258. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5259. 'third',
  5260. '/label',
  5261. '/div',
  5262. '/div',
  5263. ];
  5264. $this->assertHtml($expected, $result);
  5265. $result = $this->Form->control('Model.multi_field', [
  5266. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5267. 'multiple' => 'checkbox',
  5268. ]);
  5269. $expected = [
  5270. ['div' => ['class' => 'input select']],
  5271. ['label' => ['for' => 'model-multi-field']],
  5272. 'Multi Field',
  5273. '/label',
  5274. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5275. ['div' => ['class' => 'checkbox']],
  5276. ['label' => ['for' => 'model-multi-field-a']],
  5277. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5278. 'first',
  5279. '/label',
  5280. '/div',
  5281. ['div' => ['class' => 'checkbox']],
  5282. ['label' => ['for' => 'model-multi-field-b']],
  5283. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5284. 'second',
  5285. '/label',
  5286. '/div',
  5287. ['div' => ['class' => 'checkbox']],
  5288. ['label' => ['for' => 'model-multi-field-c']],
  5289. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5290. 'third',
  5291. '/label',
  5292. '/div',
  5293. '/div',
  5294. ];
  5295. $this->assertHtml($expected, $result);
  5296. }
  5297. /**
  5298. * testSelectHiddenFieldOmission method
  5299. *
  5300. * Test that select() with 'hiddenField' => false omits the hidden field.
  5301. */
  5302. public function testSelectHiddenFieldOmission(): void
  5303. {
  5304. $result = $this->Form->select(
  5305. 'Model.multi_field',
  5306. ['first', 'second'],
  5307. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5308. );
  5309. $this->assertStringNotContainsString('type="hidden"', $result);
  5310. }
  5311. /**
  5312. * testSelectCheckboxMultipleOverrideName method
  5313. *
  5314. * Test that select() with multiple = checkbox works with overriding name attribute.
  5315. */
  5316. public function testSelectCheckboxMultipleOverrideName(): void
  5317. {
  5318. $result = $this->Form->select('category', ['1', '2'], [
  5319. 'multiple' => 'checkbox',
  5320. 'name' => 'fish',
  5321. ]);
  5322. $expected = [
  5323. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  5324. ['div' => ['class' => 'checkbox']],
  5325. ['label' => ['for' => 'fish-0']],
  5326. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5327. '1',
  5328. '/label',
  5329. '/div',
  5330. ['div' => ['class' => 'checkbox']],
  5331. ['label' => ['for' => 'fish-1']],
  5332. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5333. '2',
  5334. '/label',
  5335. '/div',
  5336. ];
  5337. $this->assertHtml($expected, $result);
  5338. $result = $this->Form->multiCheckbox(
  5339. 'category',
  5340. new Collection(['1', '2']),
  5341. ['name' => 'fish']
  5342. );
  5343. $this->assertHtml($expected, $result);
  5344. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5345. 'name' => 'fish',
  5346. ]);
  5347. $this->assertHtml($expected, $result);
  5348. }
  5349. /**
  5350. * testSelectCheckboxMultipleOverrideName method
  5351. *
  5352. * Test that select() with multiple = checkbox works with overriding name attribute.
  5353. */
  5354. public function testSelectCheckboxMultipleCustomId(): void
  5355. {
  5356. $result = $this->Form->select('category', ['1', '2'], [
  5357. 'multiple' => 'checkbox',
  5358. 'id' => 'cat',
  5359. ]);
  5360. $expected = [
  5361. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'cat'],
  5362. ['div' => ['class' => 'checkbox']],
  5363. ['label' => ['for' => 'cat-0']],
  5364. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'cat-0']],
  5365. '1',
  5366. '/label',
  5367. '/div',
  5368. ['div' => ['class' => 'checkbox']],
  5369. ['label' => ['for' => 'cat-1']],
  5370. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'cat-1']],
  5371. '2',
  5372. '/label',
  5373. '/div',
  5374. ];
  5375. $this->assertHtml($expected, $result);
  5376. $result = $this->Form->multiCheckbox(
  5377. 'category',
  5378. ['1', '2'],
  5379. ['id' => 'cat']
  5380. );
  5381. $this->assertHtml($expected, $result);
  5382. }
  5383. /**
  5384. * testControlMultiCheckbox method
  5385. *
  5386. * Test that control() works with multicheckbox.
  5387. */
  5388. public function testControlMultiCheckbox(): void
  5389. {
  5390. $result = $this->Form->control('category', [
  5391. 'type' => 'multicheckbox',
  5392. 'options' => ['1', '2'],
  5393. ]);
  5394. $expected = [
  5395. ['div' => ['class' => 'input multicheckbox']],
  5396. '<label',
  5397. 'Category',
  5398. '/label',
  5399. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'category'],
  5400. ['div' => ['class' => 'checkbox']],
  5401. ['label' => ['for' => 'category-0']],
  5402. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5403. '1',
  5404. '/label',
  5405. '/div',
  5406. ['div' => ['class' => 'checkbox']],
  5407. ['label' => ['for' => 'category-1']],
  5408. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5409. '2',
  5410. '/label',
  5411. '/div',
  5412. '/div',
  5413. ];
  5414. $this->assertHtml($expected, $result);
  5415. }
  5416. /**
  5417. * testCheckbox method
  5418. *
  5419. * Test generation of checkboxes.
  5420. */
  5421. public function testCheckbox(): void
  5422. {
  5423. $result = $this->Form->checkbox('Model.field');
  5424. $expected = [
  5425. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5426. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5427. ];
  5428. $this->assertHtml($expected, $result);
  5429. $result = $this->Form->checkbox('Model.field', [
  5430. 'id' => 'theID',
  5431. 'value' => 'myvalue',
  5432. 'form' => 'my-form',
  5433. ]);
  5434. $expected = [
  5435. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5436. ['input' => [
  5437. 'type' => 'checkbox', 'name' => 'Model[field]',
  5438. 'value' => 'myvalue', 'id' => 'theID',
  5439. 'form' => 'my-form',
  5440. ]],
  5441. ];
  5442. $this->assertHtml($expected, $result);
  5443. }
  5444. /**
  5445. * testCheckboxDefaultValue method
  5446. *
  5447. * Test default value setting on checkbox() method.
  5448. */
  5449. public function testCheckboxDefaultValue(): void
  5450. {
  5451. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5452. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5453. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5454. $this->assertHtml($expected, $result);
  5455. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5456. $this->Form->create();
  5457. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5458. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5459. $this->assertHtml($expected, $result);
  5460. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5461. $this->Form->create();
  5462. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5463. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5464. $this->assertHtml($expected, $result);
  5465. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5466. $this->Form->create();
  5467. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5468. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5469. $this->assertHtml($expected, $result);
  5470. $Articles = $this->getTableLocator()->get('Articles');
  5471. $Articles->getSchema()->addColumn(
  5472. 'published',
  5473. ['type' => 'boolean', 'null' => false, 'default' => true]
  5474. );
  5475. $this->Form->create($Articles->newEmptyEntity());
  5476. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5477. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5478. $this->assertHtml($expected, $result);
  5479. }
  5480. /**
  5481. * testCheckboxCheckedAndError method
  5482. *
  5483. * Test checkbox being checked or having errors.
  5484. */
  5485. public function testCheckboxCheckedAndError(): void
  5486. {
  5487. $this->article['errors'] = [
  5488. 'published' => true,
  5489. ];
  5490. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5491. $this->Form->create($this->article);
  5492. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5493. $expected = [
  5494. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5495. ['input' => [
  5496. 'type' => 'checkbox',
  5497. 'name' => 'published',
  5498. 'value' => 'myvalue',
  5499. 'id' => 'theID',
  5500. 'checked' => 'checked',
  5501. 'class' => 'form-error',
  5502. ]],
  5503. ];
  5504. $this->assertHtml($expected, $result);
  5505. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5506. $this->Form->create($this->article);
  5507. $result = $this->Form->checkbox('published');
  5508. $expected = [
  5509. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5510. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5511. ];
  5512. $this->assertHtml($expected, $result);
  5513. }
  5514. /**
  5515. * testCheckboxCustomNameAttribute method
  5516. *
  5517. * Test checkbox() with a custom name attribute.
  5518. */
  5519. public function testCheckboxCustomNameAttribute(): void
  5520. {
  5521. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5522. $expected = [
  5523. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5524. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5525. ];
  5526. $this->assertHtml($expected, $result);
  5527. }
  5528. /**
  5529. * testCheckboxHiddenField method
  5530. *
  5531. * Test that the hidden input for checkboxes can be omitted or set to a
  5532. * specific value.
  5533. */
  5534. public function testCheckboxHiddenField(): void
  5535. {
  5536. $result = $this->Form->checkbox('UserForm.something', [
  5537. 'hiddenField' => false,
  5538. ]);
  5539. $expected = [
  5540. 'input' => [
  5541. 'type' => 'checkbox',
  5542. 'name' => 'UserForm[something]',
  5543. 'value' => '1',
  5544. ],
  5545. ];
  5546. $this->assertHtml($expected, $result);
  5547. $result = $this->Form->checkbox('UserForm.something', [
  5548. 'value' => 'Y',
  5549. 'hiddenField' => '',
  5550. ]);
  5551. $expected = [
  5552. ['input' => [
  5553. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5554. 'value' => '',
  5555. ]],
  5556. ['input' => [
  5557. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5558. 'value' => 'Y',
  5559. ]],
  5560. ];
  5561. $this->assertHtml($expected, $result);
  5562. $result = $this->Form->checkbox('UserForm.something', [
  5563. 'value' => 'Y',
  5564. 'hiddenField' => 'N',
  5565. ]);
  5566. $expected = [
  5567. ['input' => [
  5568. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5569. 'value' => 'N',
  5570. ]],
  5571. ['input' => [
  5572. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5573. 'value' => 'Y',
  5574. ]],
  5575. ];
  5576. $this->assertHtml($expected, $result);
  5577. }
  5578. /**
  5579. * testTime method
  5580. *
  5581. * Test the time type.
  5582. */
  5583. public function testTime(): void
  5584. {
  5585. $result = $this->Form->time('start_time', [
  5586. 'value' => '2014-03-08 16:30:00',
  5587. ]);
  5588. $expected = [
  5589. 'input' => [
  5590. 'type' => 'time',
  5591. 'name' => 'start_time',
  5592. 'value' => '16:30:00',
  5593. 'step' => '1',
  5594. ],
  5595. ];
  5596. $this->assertHtml($expected, $result);
  5597. $result = $this->Form->time('start_time', [
  5598. 'value' => new ChronosTime('16:30:00'),
  5599. ]);
  5600. $expected = [
  5601. 'input' => [
  5602. 'type' => 'time',
  5603. 'name' => 'start_time',
  5604. 'value' => '16:30:00',
  5605. 'step' => '1',
  5606. ],
  5607. ];
  5608. $this->assertHtml($expected, $result);
  5609. }
  5610. /**
  5611. * testDate method
  5612. *
  5613. * Test the date type.
  5614. */
  5615. public function testDate(): void
  5616. {
  5617. $result = $this->Form->date('start_day', [
  5618. 'value' => '2014-03-08',
  5619. ]);
  5620. $expected = [
  5621. 'input' => [
  5622. 'type' => 'date',
  5623. 'name' => 'start_day',
  5624. 'value' => '2014-03-08',
  5625. ],
  5626. ];
  5627. $this->assertHtml($expected, $result);
  5628. $result = $this->Form->date('start_day', [
  5629. 'value' => new Date('2014-03-08'),
  5630. ]);
  5631. $this->assertHtml($expected, $result);
  5632. }
  5633. /**
  5634. * testDateTime method
  5635. */
  5636. public function testDateTime(): void
  5637. {
  5638. $result = $this->Form->dateTime('date', ['default' => true]);
  5639. $expected = [
  5640. 'input' => [
  5641. 'type' => 'datetime-local',
  5642. 'name' => 'date',
  5643. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  5644. 'step' => '1',
  5645. ],
  5646. ];
  5647. $this->assertHtml($expected, $result);
  5648. }
  5649. /**
  5650. * testDateTimeSecured method
  5651. *
  5652. * Test that datetime fields are added to protected fields list.
  5653. */
  5654. public function testDateTimeSecured(): void
  5655. {
  5656. $this->View->setRequest(
  5657. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5658. );
  5659. $this->Form->create();
  5660. $this->Form->dateTime('date');
  5661. $expected = ['date'];
  5662. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5663. $this->assertEquals($expected, $result);
  5664. $this->Form->date('published');
  5665. $expected = ['date', 'published'];
  5666. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5667. $this->assertEquals($expected, $result);
  5668. }
  5669. /**
  5670. * testDateTimeSecuredDisabled method
  5671. *
  5672. * Test that datetime fields are added to protected fields list.
  5673. */
  5674. public function testDateTimeSecuredDisabled(): void
  5675. {
  5676. $this->View->setRequest(
  5677. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5678. );
  5679. $this->Form->create();
  5680. $this->Form->dateTime('date', ['secure' => false]);
  5681. $expected = [];
  5682. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5683. $this->assertEquals($expected, $result);
  5684. $this->Form->date('published', ['secure' => false]);
  5685. $expected = [];
  5686. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5687. $this->assertEquals($expected, $result);
  5688. }
  5689. /**
  5690. * testDatetimeWithDefault method
  5691. *
  5692. * Test that datetime() and default values work.
  5693. */
  5694. public function testDatetimeWithDefault(): void
  5695. {
  5696. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5697. $expected = [
  5698. 'input' => [
  5699. 'type' => 'datetime-local',
  5700. 'name' => 'updated',
  5701. 'value' => '2009-06-01T11:15:30',
  5702. 'step' => '1',
  5703. ],
  5704. ];
  5705. $this->assertHtml($expected, $result);
  5706. $result = $this->Form->dateTime('updated', [
  5707. 'default' => '2009-06-01 11:15:30',
  5708. ]);
  5709. $expected = [
  5710. 'input' => [
  5711. 'type' => 'datetime-local',
  5712. 'name' => 'updated',
  5713. 'value' => '2009-06-01T11:15:30',
  5714. 'step' => '1',
  5715. ],
  5716. ];
  5717. $this->assertHtml($expected, $result);
  5718. }
  5719. /**
  5720. * testMonth method
  5721. *
  5722. * Test generation of a month input.
  5723. */
  5724. public function testMonth(): void
  5725. {
  5726. $result = $this->Form->month('field', ['value' => '']);
  5727. $expected = [
  5728. 'input' => [
  5729. 'type' => 'month',
  5730. 'name' => 'field',
  5731. 'value' => '',
  5732. ],
  5733. ];
  5734. $this->assertHtml($expected, $result);
  5735. $this->View->setRequest(
  5736. $this->View->getRequest()->withData('release', '2050-02-10')
  5737. );
  5738. $this->Form->create();
  5739. $result = $this->Form->month('release');
  5740. $expected = [
  5741. 'input' => [
  5742. 'type' => 'month',
  5743. 'name' => 'release',
  5744. 'value' => '2050-02',
  5745. ],
  5746. ];
  5747. $this->assertHtml($expected, $result);
  5748. $this->View->setRequest(
  5749. $this->View->getRequest()->withData('release', '2050-03')
  5750. );
  5751. $this->Form->create();
  5752. $result = $this->Form->month('release');
  5753. $expected = [
  5754. 'input' => [
  5755. 'type' => 'month',
  5756. 'name' => 'release',
  5757. 'value' => '2050-03',
  5758. ],
  5759. ];
  5760. $this->assertHtml($expected, $result);
  5761. }
  5762. /**
  5763. * testYear method
  5764. *
  5765. * Test generation of a year input.
  5766. */
  5767. public function testYear(): void
  5768. {
  5769. $this->View->setRequest(
  5770. $this->View->getRequest()->withData('published', '2006')
  5771. );
  5772. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5773. $expected = [
  5774. ['select' => ['name' => 'field']],
  5775. ['option' => ['selected' => 'selected', 'value' => '']],
  5776. '/option',
  5777. ['option' => ['value' => '2007']],
  5778. '2007',
  5779. '/option',
  5780. ['option' => ['value' => '2006']],
  5781. '2006',
  5782. '/option',
  5783. '/select',
  5784. ];
  5785. $this->assertHtml($expected, $result);
  5786. $result = $this->Form->year('field', [
  5787. 'value' => '',
  5788. 'min' => 2006,
  5789. 'max' => 2007,
  5790. 'order' => 'asc',
  5791. ]);
  5792. $expected = [
  5793. ['select' => ['name' => 'field']],
  5794. ['option' => ['selected' => 'selected', 'value' => '']],
  5795. '/option',
  5796. ['option' => ['value' => '2006']],
  5797. '2006',
  5798. '/option',
  5799. ['option' => ['value' => '2007']],
  5800. '2007',
  5801. '/option',
  5802. '/select',
  5803. ];
  5804. $this->assertHtml($expected, $result);
  5805. $result = $this->Form->year('published', [
  5806. 'empty' => false,
  5807. 'min' => 2006,
  5808. 'max' => 2007,
  5809. ]);
  5810. $expected = [
  5811. ['select' => ['name' => 'published']],
  5812. ['option' => ['value' => '2007']],
  5813. '2007',
  5814. '/option',
  5815. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5816. '2006',
  5817. '/option',
  5818. '/select',
  5819. ];
  5820. $this->assertHtml($expected, $result);
  5821. $result = $this->Form->year('published', [
  5822. 'empty' => false,
  5823. 'value' => new Date('2008-01-12'),
  5824. 'min' => 2007,
  5825. 'max' => 2009,
  5826. ]);
  5827. $expected = [
  5828. ['select' => ['name' => 'published']],
  5829. ['option' => ['value' => '2009']],
  5830. '2009',
  5831. '/option',
  5832. ['option' => ['value' => '2008', 'selected' => 'selected']],
  5833. '2008',
  5834. '/option',
  5835. ['option' => ['value' => '2007']],
  5836. '2007',
  5837. '/option',
  5838. '/select',
  5839. ];
  5840. $this->assertHtml($expected, $result);
  5841. $result = $this->Form->year('published', [
  5842. 'empty' => 'Published on',
  5843. ]);
  5844. $this->assertStringContainsString('Published on', $result);
  5845. }
  5846. /**
  5847. * testControlYearPreEpoch method
  5848. *
  5849. * Test minYear being prior to the unix epoch.
  5850. */
  5851. public function testControlYearPreEpoch(): void
  5852. {
  5853. $start = date('Y') - 80;
  5854. $end = date('Y') - 18;
  5855. $result = $this->Form->control('birth_year', [
  5856. 'type' => 'year',
  5857. 'label' => 'Birth Year',
  5858. 'min' => $start,
  5859. 'max' => $end,
  5860. ]);
  5861. $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
  5862. $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
  5863. $this->assertStringNotContainsString('value="00">00', $result);
  5864. }
  5865. /**
  5866. * test control() datetime & required attributes
  5867. */
  5868. public function testControlDatetimeRequired(): void
  5869. {
  5870. $result = $this->Form->control('birthday', [
  5871. 'type' => 'date',
  5872. 'required' => true,
  5873. ]);
  5874. $this->assertStringContainsString(
  5875. '<input type="date" name="birthday" required="required"',
  5876. $result
  5877. );
  5878. }
  5879. /**
  5880. * testYearAutoExpandRange method
  5881. */
  5882. public function testYearAutoExpandRange(): void
  5883. {
  5884. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5885. $result = $this->Form->year('birthday');
  5886. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5887. $result = $matches[1];
  5888. $expected = range(date('Y') + 5, 1930);
  5889. $this->assertEquals($expected, $result);
  5890. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5891. $this->Form->create();
  5892. $result = $this->Form->year('release');
  5893. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5894. $result = $matches[1];
  5895. $expected = range(2050, date('Y') - 5);
  5896. $this->assertEquals($expected, $result);
  5897. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5898. $this->Form->create();
  5899. $result = $this->Form->year('release', [
  5900. 'min' => 1890,
  5901. 'max' => 1900,
  5902. ]);
  5903. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5904. $result = $matches[1];
  5905. $expected = range(1900, 1881);
  5906. $this->assertEquals($expected, $result);
  5907. }
  5908. /**
  5909. * test control placeholder + label
  5910. */
  5911. public function testControlLabelAndPlaceholder(): void
  5912. {
  5913. $this->Form->create($this->article);
  5914. $result = $this->Form->control('title', ['label' => 'Title', 'placeholder' => 'Add title']);
  5915. $expected = [
  5916. 'div' => ['class' => 'input text required'],
  5917. 'label' => ['for' => 'title'],
  5918. 'Title',
  5919. '/label',
  5920. 'input' => [
  5921. 'aria-required' => 'true',
  5922. 'type' => 'text',
  5923. 'required' => 'required',
  5924. 'placeholder' => 'Add title',
  5925. 'id' => 'title',
  5926. 'name' => 'title',
  5927. 'data-validity-message' => 'This field cannot be left empty',
  5928. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5929. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5930. ],
  5931. '/div',
  5932. ];
  5933. $this->assertHtml($expected, $result);
  5934. }
  5935. /**
  5936. * testControlLabelFalse method
  5937. *
  5938. * Test the label option being set to false.
  5939. */
  5940. public function testControlLabelFalse(): void
  5941. {
  5942. $this->Form->create($this->article);
  5943. $result = $this->Form->control('title', ['label' => false]);
  5944. $expected = [
  5945. 'div' => ['class' => 'input text required'],
  5946. 'input' => [
  5947. 'aria-required' => 'true',
  5948. 'type' => 'text',
  5949. 'required' => 'required',
  5950. 'id' => 'title',
  5951. 'name' => 'title',
  5952. 'data-validity-message' => 'This field cannot be left empty',
  5953. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5954. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5955. ],
  5956. '/div',
  5957. ];
  5958. $this->assertHtml($expected, $result);
  5959. $this->Form->create($this->article);
  5960. $result = $this->Form->control('title', ['label' => false, 'placeholder' => 'Add title']);
  5961. $expected['input'] += [
  5962. 'placeholder' => 'Add title',
  5963. 'aria-label' => 'Add title',
  5964. ];
  5965. $this->assertHtml($expected, $result);
  5966. }
  5967. /**
  5968. * testTextArea method
  5969. *
  5970. * Test generation of a textarea input.
  5971. */
  5972. public function testTextArea(): void
  5973. {
  5974. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5975. $result = $this->Form->textarea('field');
  5976. $expected = [
  5977. 'textarea' => ['name' => 'field', 'rows' => 5],
  5978. 'some test data',
  5979. '/textarea',
  5980. ];
  5981. $this->assertHtml($expected, $result);
  5982. $result = $this->Form->textarea('user.bio');
  5983. $expected = [
  5984. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5985. '/textarea',
  5986. ];
  5987. $this->assertHtml($expected, $result);
  5988. $this->View->setRequest($this->View->getRequest()
  5989. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5990. $this->Form->create();
  5991. $result = $this->Form->textarea('field');
  5992. $expected = [
  5993. 'textarea' => ['name' => 'field', 'rows' => 5],
  5994. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5995. '/textarea',
  5996. ];
  5997. $this->assertHtml($expected, $result);
  5998. $this->View->setRequest($this->View->getRequest()->withData(
  5999. 'Model.field',
  6000. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  6001. ));
  6002. $this->Form->create();
  6003. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6004. $expected = [
  6005. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6006. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6007. '/textarea',
  6008. ];
  6009. $this->assertHtml($expected, $result);
  6010. $result = $this->Form->textarea('0.OtherModel.field');
  6011. $expected = [
  6012. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6013. '/textarea',
  6014. ];
  6015. $this->assertHtml($expected, $result);
  6016. }
  6017. /**
  6018. * testTextAreaWithStupidCharacters method
  6019. *
  6020. * Test text area with non-ascii characters.
  6021. */
  6022. public function testTextAreaWithStupidCharacters(): void
  6023. {
  6024. $result = $this->Form->textarea('Post.content', [
  6025. 'value' => 'GREAT®',
  6026. 'rows' => '15',
  6027. 'cols' => '75',
  6028. ]);
  6029. $expected = [
  6030. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6031. 'GREAT®',
  6032. '/textarea',
  6033. ];
  6034. $this->assertHtml($expected, $result);
  6035. }
  6036. /**
  6037. * testTextAreaMaxLength method
  6038. *
  6039. * Test textareas maxlength read from schema.
  6040. */
  6041. public function testTextAreaMaxLength(): void
  6042. {
  6043. $this->Form->create([
  6044. 'schema' => [
  6045. 'stuff' => ['type' => 'string', 'length' => 10],
  6046. ],
  6047. ]);
  6048. $result = $this->Form->control('other', ['type' => 'textarea']);
  6049. $expected = [
  6050. 'div' => ['class' => 'input textarea'],
  6051. 'label' => ['for' => 'other'],
  6052. 'Other',
  6053. '/label',
  6054. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6055. '/textarea',
  6056. '/div',
  6057. ];
  6058. $this->assertHtml($expected, $result);
  6059. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6060. $expected = [
  6061. 'div' => ['class' => 'input textarea'],
  6062. 'label' => ['for' => 'stuff'],
  6063. 'Stuff',
  6064. '/label',
  6065. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6066. '/textarea',
  6067. '/div',
  6068. ];
  6069. $this->assertHtml($expected, $result);
  6070. }
  6071. /**
  6072. * testHiddenField method
  6073. *
  6074. * Test generation of a hidden input.
  6075. */
  6076. public function testHidden(): void
  6077. {
  6078. $this->article['errors'] = [
  6079. 'field' => true,
  6080. ];
  6081. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  6082. $this->Form->create($this->article);
  6083. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6084. $expected = [
  6085. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6086. $this->assertHtml($expected, $result);
  6087. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6088. $expected = [
  6089. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  6090. ];
  6091. $this->assertHtml($expected, $result);
  6092. $article = new Article([
  6093. 'status' => ArticleStatus::Unpublished,
  6094. ]);
  6095. $this->Form->create($article);
  6096. $result = $this->Form->hidden('status');
  6097. $expected = [
  6098. 'input' => ['type' => 'hidden', 'name' => 'status', 'value' => 'N'],
  6099. ];
  6100. $this->assertHtml($expected, $result);
  6101. }
  6102. /**
  6103. * Test hidden() with various boolean values.
  6104. */
  6105. public function testHiddenBooleanValues(): void
  6106. {
  6107. $this->Form->create($this->article);
  6108. $result = $this->Form->hidden('field', ['value' => null]);
  6109. $expected = [
  6110. 'input' => ['type' => 'hidden', 'name' => 'field'],
  6111. ];
  6112. $this->assertHtml($expected, $result);
  6113. $result = $this->Form->hidden('field', ['value' => true]);
  6114. $expected = [
  6115. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '1'],
  6116. ];
  6117. $this->assertHtml($expected, $result);
  6118. $result = $this->Form->hidden('field', ['value' => false]);
  6119. $expected = [
  6120. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '0'],
  6121. ];
  6122. $this->assertHtml($expected, $result);
  6123. }
  6124. /**
  6125. * testFileUploadField method
  6126. *
  6127. * Test generation of a file upload input.
  6128. */
  6129. public function testFileUploadField(): void
  6130. {
  6131. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6132. $result = $this->Form->file('Model.upload');
  6133. $this->assertHtml($expected, $result);
  6134. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  6135. 'name' => '', 'type' => '', 'tmp_name' => '',
  6136. 'error' => 4, 'size' => 0,
  6137. ]));
  6138. $result = $this->Form->file('Model.upload');
  6139. $this->assertHtml($expected, $result);
  6140. $this->View->setRequest(
  6141. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6142. );
  6143. $result = $this->Form->file('Model.upload');
  6144. $this->assertHtml($expected, $result);
  6145. }
  6146. /**
  6147. * testFileUploadOnOtherModel method
  6148. *
  6149. * Test File upload input on a model not used in create().
  6150. */
  6151. public function testFileUploadOnOtherModel(): void
  6152. {
  6153. $this->Form->create($this->article, ['type' => 'file']);
  6154. $result = $this->Form->file('ValidateProfile.city');
  6155. $expected = [
  6156. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  6157. ];
  6158. $this->assertHtml($expected, $result);
  6159. }
  6160. /**
  6161. * testButton method
  6162. *
  6163. * Test generation of a form button.
  6164. */
  6165. public function testButton(): void
  6166. {
  6167. $result = $this->Form->button('Hi');
  6168. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6169. $this->assertHtml($expected, $result);
  6170. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'escapeTitle' => false]);
  6171. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6172. $this->assertHtml($expected, $result);
  6173. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm', 'escapeTitle' => false]);
  6174. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6175. $this->assertHtml($expected, $result);
  6176. $result = $this->Form->button('<Clear Form>', ['type' => 'reset']);
  6177. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6178. $this->assertHtml($expected, $result);
  6179. $result = $this->Form->button('No type', ['type' => false]);
  6180. $expected = ['button' => [], 'No type', '/button'];
  6181. $this->assertHtml($expected, $result);
  6182. $result = $this->Form->button('Upload Text', [
  6183. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6184. 'escape' => false,
  6185. ]);
  6186. $this->assertDoesNotMatchRegularExpression('/\&039/', $result);
  6187. }
  6188. /**
  6189. * testButtonUnlockedByDefault method
  6190. *
  6191. * Test that button() makes unlocked fields by default.
  6192. */
  6193. public function testButtonUnlockedByDefault(): void
  6194. {
  6195. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6196. $this->Form->create();
  6197. $this->Form->button('Save', ['name' => 'save']);
  6198. $this->Form->button('Clear');
  6199. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6200. $this->assertEquals(['save'], $result);
  6201. }
  6202. /**
  6203. * Test generation of a form button with confirm message.
  6204. */
  6205. public function testButtonWithConfirm(): void
  6206. {
  6207. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6208. $expected = ['button' => [
  6209. 'type' => 'submit',
  6210. 'data-confirm-message' => 'Confirm me!',
  6211. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6212. ], 'Hi', '/button'];
  6213. $this->assertHtml($expected, $result);
  6214. $result = $this->Form->button('Hi', ['escape' => false, 'confirm' => 'Confirm "me"!']);
  6215. $expected = ['button' => [
  6216. 'type' => 'submit',
  6217. 'data-confirm-message' => 'Confirm "me"!',
  6218. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6219. ], 'Hi', '/button'];
  6220. $this->assertHtml($expected, $result);
  6221. }
  6222. /**
  6223. * testPostButton method
  6224. */
  6225. public function testPostButton(): void
  6226. {
  6227. $result = $this->Form->postButton('Hi', '/controller/action');
  6228. $expected = [
  6229. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6230. 'button' => ['type' => 'submit'],
  6231. 'Hi',
  6232. '/button',
  6233. '/form',
  6234. ];
  6235. $this->assertHtml($expected, $result);
  6236. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6237. $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
  6238. }
  6239. /**
  6240. * testPostButtonMethodType method
  6241. */
  6242. public function testPostButtonMethodType(): void
  6243. {
  6244. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6245. $expected = [
  6246. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6247. 'div' => ['style' => 'display:none;'],
  6248. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6249. '/div',
  6250. 'button' => ['type' => 'submit'],
  6251. 'Hi',
  6252. '/button',
  6253. '/form',
  6254. ];
  6255. $this->assertHtml($expected, $result);
  6256. }
  6257. /**
  6258. * testPostButtonFormOptions method
  6259. */
  6260. public function testPostButtonFormOptions(): void
  6261. {
  6262. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6263. $expected = [
  6264. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6265. 'button' => ['type' => 'submit'],
  6266. 'Hi',
  6267. '/button',
  6268. '/form',
  6269. ];
  6270. $this->assertHtml($expected, $result);
  6271. }
  6272. /**
  6273. * testPostButtonNestedData method
  6274. *
  6275. * Test using postButton with N dimensional data.
  6276. */
  6277. public function testPostButtonNestedData(): void
  6278. {
  6279. $data = [
  6280. 'one' => [
  6281. 'two' => [
  6282. 3, 4, 5,
  6283. ],
  6284. ],
  6285. ];
  6286. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6287. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6288. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6289. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6290. }
  6291. /**
  6292. * testSecurePostButton method
  6293. *
  6294. * Test that postButton adds _Token fields.
  6295. */
  6296. public function testSecurePostButton(): void
  6297. {
  6298. $this->View->setRequest($this->View->getRequest()
  6299. ->withAttribute('csrfToken', 'testkey')
  6300. ->withAttribute('formTokenData', ['unlockedFields' => []]));
  6301. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6302. $tokenDebug = urlencode(json_encode([
  6303. '/posts/delete/1',
  6304. [],
  6305. [],
  6306. ]));
  6307. $expected = [
  6308. 'form' => [
  6309. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6310. ],
  6311. ['div' => ['style' => 'display:none;']],
  6312. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6313. '/div',
  6314. 'button' => ['type' => 'submit'],
  6315. 'Delete',
  6316. '/button',
  6317. ['div' => ['style' => 'display:none;']],
  6318. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6319. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6320. ['input' => [
  6321. 'type' => 'hidden',
  6322. 'name' => '_Token[debug]',
  6323. 'value' => $tokenDebug,
  6324. ]],
  6325. '/div',
  6326. '/form',
  6327. ];
  6328. $this->assertHtml($expected, $result);
  6329. }
  6330. /**
  6331. * testPostLink method
  6332. */
  6333. public function testPostLink(): void
  6334. {
  6335. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6336. $expected = [
  6337. 'form' => [
  6338. 'method' => 'post', 'action' => '/posts/delete/1',
  6339. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6340. ],
  6341. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6342. '/form',
  6343. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6344. 'Delete',
  6345. '/a',
  6346. ];
  6347. $this->assertHtml($expected, $result);
  6348. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6349. $expected = [
  6350. 'form' => [
  6351. 'method' => 'post', 'action' => '/posts/delete/1',
  6352. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6353. ],
  6354. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6355. '/form',
  6356. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6357. 'Delete',
  6358. '/a',
  6359. ];
  6360. $this->assertHtml($expected, $result);
  6361. $result = $this->Form->postLink(
  6362. 'Delete',
  6363. '/posts/delete/1',
  6364. ['target' => '_blank', 'class' => 'btn btn-danger']
  6365. );
  6366. $expected = [
  6367. 'form' => [
  6368. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6369. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6370. ],
  6371. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6372. '/form',
  6373. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6374. 'Delete',
  6375. '/a',
  6376. ];
  6377. $this->assertHtml($expected, $result);
  6378. }
  6379. /**
  6380. * testPostLinkWithConfirm method
  6381. *
  6382. * Test the confirm option for postLink().
  6383. */
  6384. public function testPostLinkWithConfirm(): void
  6385. {
  6386. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6387. $expected = [
  6388. 'form' => [
  6389. 'method' => 'post', 'action' => '/posts/delete/1',
  6390. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6391. ],
  6392. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6393. '/form',
  6394. 'a' => [
  6395. 'href' => '#',
  6396. 'data-confirm-message' => 'Confirm?',
  6397. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/',
  6398. ],
  6399. 'Delete',
  6400. '/a',
  6401. ];
  6402. $this->assertHtml($expected, $result);
  6403. $result = $this->Form->postLink(
  6404. 'Delete',
  6405. '/posts/delete/1',
  6406. ['confirm' => "'Confirm'\nthis \"deletion\"?"]
  6407. );
  6408. $expected = [
  6409. 'form' => [
  6410. 'method' => 'post', 'action' => '/posts/delete/1',
  6411. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6412. ],
  6413. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6414. '/form',
  6415. 'a' => [
  6416. 'href' => '#',
  6417. 'data-confirm-message' => "&#039;Confirm&#039;\nthis &quot;deletion&quot;?",
  6418. 'onclick' => "preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/",
  6419. ],
  6420. 'Delete',
  6421. '/a',
  6422. ];
  6423. $this->assertHtml($expected, $result);
  6424. $this->Form->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { $(\'form[name="{{formName}}"]\').submit();};']);
  6425. $result = $this->Form->postLink(
  6426. 'Delete',
  6427. '/posts/delete/1',
  6428. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  6429. );
  6430. $expected = [
  6431. 'form' => [
  6432. 'method' => 'post', 'action' => '/posts/delete/1',
  6433. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6434. ],
  6435. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6436. '/form',
  6437. 'a' => [
  6438. 'href' => '#',
  6439. 'data-confirm-message' => 'Confirm this deletion?',
  6440. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/',
  6441. ],
  6442. 'Delete',
  6443. '/a',
  6444. ];
  6445. $this->assertHtml($expected, $result);
  6446. }
  6447. /**
  6448. * testPostLinkWithQuery method
  6449. *
  6450. * Test postLink() with query string args.
  6451. */
  6452. public function testPostLinkWithQuery(): void
  6453. {
  6454. $result = $this->Form->postLink(
  6455. 'Delete',
  6456. ['controller' => 'Posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6457. );
  6458. $expected = [
  6459. 'form' => [
  6460. 'method' => 'post', 'action' => '/Posts/delete/1?a=b&amp;c=d',
  6461. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6462. ],
  6463. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6464. '/form',
  6465. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6466. 'Delete',
  6467. '/a',
  6468. ];
  6469. $this->assertHtml($expected, $result);
  6470. }
  6471. /**
  6472. * testPostLinkWithData method
  6473. *
  6474. * Test postLink with additional data.
  6475. */
  6476. public function testPostLinkWithData(): void
  6477. {
  6478. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6479. $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
  6480. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6481. $this->Form->create($entity);
  6482. $this->Form->end();
  6483. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6484. $this->assertStringContainsString(
  6485. '<input type="hidden" name="name" value="show"',
  6486. $result,
  6487. 'should not contain entity data.'
  6488. );
  6489. }
  6490. /**
  6491. * testPostLinkSecurityHash method
  6492. *
  6493. * Test that security hashes for postLink include the url.
  6494. */
  6495. public function testPostLinkSecurityHash(): void
  6496. {
  6497. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6498. $hash .= '%3Aid';
  6499. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6500. $result = $this->Form->postLink(
  6501. 'Delete',
  6502. '/posts/delete/1',
  6503. ['data' => ['id' => 1]]
  6504. );
  6505. $tokenDebug = urlencode(json_encode([
  6506. '/posts/delete/1',
  6507. [
  6508. 'id' => 1,
  6509. ],
  6510. [],
  6511. ]));
  6512. $expected = [
  6513. 'form' => [
  6514. 'method' => 'post', 'action' => '/posts/delete/1',
  6515. 'name', 'style' => 'display:none;',
  6516. ],
  6517. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6518. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6519. 'div' => ['style' => 'display:none;'],
  6520. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6521. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6522. ['input' => [
  6523. 'type' => 'hidden',
  6524. 'name' => '_Token[debug]',
  6525. 'value' => $tokenDebug,
  6526. ]],
  6527. '/div',
  6528. '/form',
  6529. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6530. 'Delete',
  6531. '/a',
  6532. ];
  6533. $this->assertHtml($expected, $result);
  6534. }
  6535. /**
  6536. * testPostLinkSecurityHashBlockMode method
  6537. *
  6538. * Test that postLink doesn't modify the fields in the containing form.
  6539. *
  6540. * postLink() calls inside open forms should not modify the field list
  6541. * for the form.
  6542. */
  6543. public function testPostLinkSecurityHashBlockMode(): void
  6544. {
  6545. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6546. $hash .= '%3A';
  6547. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6548. $this->Form->create(null, ['url' => ['action' => 'add']]);
  6549. $this->Form->control('title');
  6550. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6551. $result = $this->View->fetch('postLink');
  6552. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  6553. $this->assertEquals(['title'], $fields);
  6554. $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
  6555. $reflect = new ReflectionProperty($this->Form, '_lastAction');
  6556. $this->assertSame('/Articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
  6557. }
  6558. /**
  6559. * testPostLinkSecurityHashNoDebugMode method
  6560. *
  6561. * Test that security does not include debug token if debug is false.
  6562. */
  6563. public function testPostLinkSecurityHashNoDebugMode(): void
  6564. {
  6565. Configure::write('debug', false);
  6566. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6567. $hash .= '%3Aid';
  6568. $this->View->setRequest($this->View->getRequest()
  6569. ->withAttribute('formTokenData', ['key' => 'test']));
  6570. $result = $this->Form->postLink(
  6571. 'Delete',
  6572. '/posts/delete/1',
  6573. ['data' => ['id' => 1]]
  6574. );
  6575. $expected = [
  6576. 'form' => [
  6577. 'method' => 'post', 'action' => '/posts/delete/1',
  6578. 'name', 'style' => 'display:none;',
  6579. ],
  6580. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6581. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6582. 'div' => ['style' => 'display:none;'],
  6583. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6584. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6585. '/div',
  6586. '/form',
  6587. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6588. 'Delete',
  6589. '/a',
  6590. ];
  6591. $this->assertHtml($expected, $result);
  6592. }
  6593. /**
  6594. * testPostLinkNestedData method
  6595. *
  6596. * Test using postLink with N dimensional data.
  6597. */
  6598. public function testPostLinkNestedData(): void
  6599. {
  6600. $data = [
  6601. 'one' => [
  6602. 'two' => [
  6603. 3, 4, 5,
  6604. ],
  6605. ],
  6606. ];
  6607. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6608. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6609. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6610. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6611. }
  6612. /**
  6613. * testPostLinkAfterGetForm method
  6614. *
  6615. * Test creating postLinks after a GET form.
  6616. */
  6617. public function testPostLinkAfterGetForm(): void
  6618. {
  6619. $this->View->setRequest($this->View->getRequest()
  6620. ->withAttribute('csrfToken', 'testkey')
  6621. ->withAttribute('formTokenData', []));
  6622. $this->Form->create($this->article, ['type' => 'get']);
  6623. $this->Form->end();
  6624. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6625. $tokenDebug = urlencode(json_encode([
  6626. '/posts/delete/1',
  6627. [],
  6628. [],
  6629. ]));
  6630. $expected = [
  6631. 'form' => [
  6632. 'method' => 'post', 'action' => '/posts/delete/1',
  6633. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6634. ],
  6635. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6636. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6637. 'div' => ['style' => 'display:none;'],
  6638. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6639. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6640. ['input' => [
  6641. 'type' => 'hidden', 'name' => '_Token[debug]',
  6642. 'value' => $tokenDebug,
  6643. ]],
  6644. '/div',
  6645. '/form',
  6646. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6647. 'Delete',
  6648. '/a',
  6649. ];
  6650. $this->assertHtml($expected, $result);
  6651. }
  6652. /**
  6653. * testPostLinkFormBuffer method
  6654. *
  6655. * Test that postLink adds form tags to view block.
  6656. */
  6657. public function testPostLinkFormBuffer(): void
  6658. {
  6659. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6660. $expected = [
  6661. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6662. 'Delete',
  6663. '/a',
  6664. ];
  6665. $this->assertHtml($expected, $result);
  6666. $result = $this->View->fetch('postLink');
  6667. $expected = [
  6668. 'form' => [
  6669. 'method' => 'post', 'action' => '/posts/delete/1',
  6670. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6671. ],
  6672. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6673. '/form',
  6674. ];
  6675. $this->assertHtml($expected, $result);
  6676. $result = $this->Form->postLink(
  6677. 'Delete',
  6678. '/posts/delete/2',
  6679. ['block' => true, 'method' => 'DELETE']
  6680. );
  6681. $expected = [
  6682. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6683. 'Delete',
  6684. '/a',
  6685. ];
  6686. $this->assertHtml($expected, $result);
  6687. $result = $this->View->fetch('postLink');
  6688. $expected = [
  6689. 'form' => [
  6690. 'method' => 'post', 'action' => '/posts/delete/1',
  6691. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6692. ],
  6693. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6694. '/form',
  6695. [
  6696. 'form' => [
  6697. 'method' => 'post', 'action' => '/posts/delete/2',
  6698. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6699. ],
  6700. ],
  6701. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6702. '/form',
  6703. ];
  6704. $this->assertHtml($expected, $result);
  6705. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6706. $expected = [
  6707. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6708. 'Delete',
  6709. '/a',
  6710. ];
  6711. $this->assertHtml($expected, $result);
  6712. $result = $this->View->fetch('foobar');
  6713. $expected = [
  6714. 'form' => [
  6715. 'method' => 'post', 'action' => '/posts/delete/1',
  6716. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6717. ],
  6718. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6719. '/form',
  6720. ];
  6721. $this->assertHtml($expected, $result);
  6722. }
  6723. /**
  6724. * testSubmitButton method
  6725. */
  6726. public function testSubmitButton(): void
  6727. {
  6728. $result = $this->Form->submit('');
  6729. $expected = [
  6730. 'div' => ['class' => 'submit'],
  6731. 'input' => ['type' => 'submit', 'value' => ''],
  6732. '/div',
  6733. ];
  6734. $this->assertHtml($expected, $result);
  6735. $result = $this->Form->submit('Test Submit');
  6736. $expected = [
  6737. 'div' => ['class' => 'submit'],
  6738. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6739. '/div',
  6740. ];
  6741. $this->assertHtml($expected, $result);
  6742. $result = $this->Form->submit('Next >');
  6743. $expected = [
  6744. 'div' => ['class' => 'submit'],
  6745. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6746. '/div',
  6747. ];
  6748. $this->assertHtml($expected, $result);
  6749. $result = $this->Form->submit('Next >', ['escape' => false]);
  6750. $expected = [
  6751. 'div' => ['class' => 'submit'],
  6752. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6753. '/div',
  6754. ];
  6755. $this->assertHtml($expected, $result);
  6756. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6757. $expected = [
  6758. 'div' => ['class' => 'submit'],
  6759. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6760. '/div',
  6761. ];
  6762. $this->assertHtml($expected, $result);
  6763. }
  6764. /**
  6765. * testSubmitImage method
  6766. *
  6767. * Test image submit types.
  6768. */
  6769. public function testSubmitImage(): void
  6770. {
  6771. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6772. $expected = [
  6773. 'div' => ['class' => 'submit'],
  6774. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6775. '/div',
  6776. ];
  6777. $this->assertHtml($expected, $result);
  6778. $result = $this->Form->submit('/relative/cake.power.gif');
  6779. $expected = [
  6780. 'div' => ['class' => 'submit'],
  6781. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6782. '/div',
  6783. ];
  6784. $this->assertHtml($expected, $result);
  6785. $result = $this->Form->submit('cake.power.gif');
  6786. $expected = [
  6787. 'div' => ['class' => 'submit'],
  6788. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6789. '/div',
  6790. ];
  6791. $this->assertHtml($expected, $result);
  6792. $result = $this->Form->submit('Not.an.image');
  6793. $expected = [
  6794. 'div' => ['class' => 'submit'],
  6795. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6796. '/div',
  6797. ];
  6798. $this->assertHtml($expected, $result);
  6799. }
  6800. /**
  6801. * testSubmitUnlockedByDefault method
  6802. *
  6803. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6804. * be submitted at a time.
  6805. */
  6806. public function testSubmitUnlockedByDefault(): void
  6807. {
  6808. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6809. $this->Form->create();
  6810. $this->Form->submit('Go go');
  6811. $this->Form->submit('Save', ['name' => 'save']);
  6812. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6813. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6814. }
  6815. /**
  6816. * testSubmitImageTimestamp method
  6817. *
  6818. * Test submit image with timestamps.
  6819. */
  6820. public function testSubmitImageTimestamp(): void
  6821. {
  6822. Configure::write('Asset.timestamp', 'force');
  6823. $result = $this->Form->submit('cake.power.gif');
  6824. $expected = [
  6825. 'div' => ['class' => 'submit'],
  6826. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6827. '/div',
  6828. ];
  6829. $this->assertHtml($expected, $result);
  6830. }
  6831. /**
  6832. * testDateTimeWithGetForms method
  6833. *
  6834. * Test that datetime() works with GET style forms.
  6835. */
  6836. public function testDateTimeWithGetForms(): void
  6837. {
  6838. $this->Form->create($this->article, ['type' => 'get']);
  6839. $result = $this->Form->datetime('created');
  6840. $expected = [
  6841. 'input' => [
  6842. 'type' => 'datetime-local',
  6843. 'name' => 'created',
  6844. 'value' => '',
  6845. 'step' => '1',
  6846. ],
  6847. ];
  6848. $this->assertHtml($expected, $result);
  6849. $result = $this->Form->datetime('created', ['default' => true]);
  6850. $expected = [
  6851. 'input' => [
  6852. 'type' => 'datetime-local',
  6853. 'name' => 'created',
  6854. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  6855. 'step' => '1',
  6856. ],
  6857. ];
  6858. $this->assertHtml($expected, $result);
  6859. }
  6860. /**
  6861. * Provides fractional schema types
  6862. *
  6863. * @return array
  6864. */
  6865. public static function fractionalTypeProvider(): array
  6866. {
  6867. return [
  6868. ['datetimefractional'],
  6869. ['timestampfractional'],
  6870. ['timestamptimezone'],
  6871. ];
  6872. }
  6873. /**
  6874. * testDateTimeWithFractional method
  6875. *
  6876. * Test that datetime() works with datetimefractional.
  6877. *
  6878. * @dataProvider fractionalTypeProvider
  6879. */
  6880. public function testDateTimeWithFractional(string $type): void
  6881. {
  6882. $this->Form->create([
  6883. 'schema' => [
  6884. 'created' => ['type' => $type],
  6885. ],
  6886. ]);
  6887. $result = $this->Form->datetime('created', [
  6888. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6889. ]);
  6890. $expected = [
  6891. 'input' => [
  6892. 'type' => 'datetime-local',
  6893. 'name' => 'created',
  6894. 'value' => '2019-09-27T02:52:43.123',
  6895. 'step' => '0.001',
  6896. ],
  6897. ];
  6898. $this->assertHtml($expected, $result);
  6899. }
  6900. /**
  6901. * testControlWithFractional method
  6902. *
  6903. * Test that control() works with datetimefractional.
  6904. *
  6905. * @dataProvider fractionalTypeProvider
  6906. */
  6907. public function testControlWithFractional(string $type): void
  6908. {
  6909. $this->Form->create([
  6910. 'schema' => [
  6911. 'created' => ['type' => $type],
  6912. ],
  6913. ]);
  6914. $result = $this->Form->control('created', [
  6915. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6916. ]);
  6917. $expected = [
  6918. 'div' => ['class' => 'input datetime'],
  6919. 'label' => ['for' => 'created'],
  6920. 'Created',
  6921. '/label',
  6922. 'input' => [
  6923. 'type' => 'datetime-local',
  6924. 'name' => 'created',
  6925. 'id' => 'created',
  6926. 'value' => '2019-09-27T02:52:43.123',
  6927. 'step' => '0.001',
  6928. ],
  6929. '/div',
  6930. ];
  6931. $this->assertHtml($expected, $result);
  6932. }
  6933. /**
  6934. * testForMagicControlNonExistentNotValidated method
  6935. */
  6936. public function testForMagicControlNonExistentNotValidated(): void
  6937. {
  6938. $this->Form->create($this->article);
  6939. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6940. $result = $this->Form->control('nonexistent_not_validated');
  6941. $expected = [
  6942. 'label' => ['for' => 'nonexistent-not-validated'],
  6943. 'Nonexistent Not Validated',
  6944. '/label',
  6945. 'input' => [
  6946. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6947. 'id' => 'nonexistent-not-validated',
  6948. ],
  6949. ];
  6950. $this->assertHtml($expected, $result);
  6951. $result = $this->Form->control('nonexistent_not_validated', [
  6952. 'val' => 'my value',
  6953. ]);
  6954. $expected = [
  6955. 'label' => ['for' => 'nonexistent-not-validated'],
  6956. 'Nonexistent Not Validated',
  6957. '/label',
  6958. 'input' => [
  6959. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6960. 'value' => 'my value', 'id' => 'nonexistent-not-validated',
  6961. ],
  6962. ];
  6963. $this->assertHtml($expected, $result);
  6964. $this->View->setRequest(
  6965. $this->View->getRequest()->withData('nonexistent_not_validated', 'CakePHP magic')
  6966. );
  6967. $this->Form->create($this->article);
  6968. $result = $this->Form->control('nonexistent_not_validated');
  6969. $expected = [
  6970. 'label' => ['for' => 'nonexistent-not-validated'],
  6971. 'Nonexistent Not Validated',
  6972. '/label',
  6973. 'input' => [
  6974. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6975. 'value' => 'CakePHP magic', 'id' => 'nonexistent-not-validated',
  6976. ],
  6977. ];
  6978. $this->assertHtml($expected, $result);
  6979. }
  6980. /**
  6981. * testFormMagicControlLabel method
  6982. */
  6983. public function testFormMagicControlLabel(): void
  6984. {
  6985. $this->getTableLocator()->get('Contacts', [
  6986. 'className' => ContactsTable::class,
  6987. ]);
  6988. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6989. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6990. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  6991. $expected = [
  6992. 'label' => ['for' => 'contacts-name'],
  6993. 'My label',
  6994. '/label',
  6995. 'input' => [
  6996. 'type' => 'text',
  6997. 'name' => 'Contacts[name]',
  6998. 'id' => 'contacts-name',
  6999. 'maxlength' => '255',
  7000. ],
  7001. ];
  7002. $this->assertHtml($expected, $result);
  7003. $result = $this->Form->control('name', [
  7004. 'label' => ['class' => 'mandatory'],
  7005. ]);
  7006. $expected = [
  7007. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7008. 'Name',
  7009. '/label',
  7010. 'input' => [
  7011. 'type' => 'text', 'name' => 'name',
  7012. 'id' => 'name', 'maxlength' => '255',
  7013. ],
  7014. ];
  7015. $this->assertHtml($expected, $result);
  7016. $result = $this->Form->control('name', [
  7017. 'div' => false,
  7018. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  7019. ]);
  7020. $expected = [
  7021. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7022. 'My label',
  7023. '/label',
  7024. 'input' => [
  7025. 'type' => 'text', 'name' => 'name',
  7026. 'id' => 'name', 'maxlength' => '255',
  7027. ],
  7028. ];
  7029. $this->assertHtml($expected, $result);
  7030. $result = $this->Form->control('Contact.name', [
  7031. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  7032. ]);
  7033. $expected = [
  7034. 'label' => ['for' => 'my_id'],
  7035. 'Name',
  7036. '/label',
  7037. 'input' => [
  7038. 'type' => 'text', 'name' => 'Contact[name]',
  7039. 'id' => 'my_id', 'maxlength' => '255',
  7040. ],
  7041. ];
  7042. $this->assertHtml($expected, $result);
  7043. $result = $this->Form->control('1.id');
  7044. $expected = ['input' => [
  7045. 'type' => 'hidden', 'name' => '1[id]',
  7046. 'id' => '1-id',
  7047. ]];
  7048. $this->assertHtml($expected, $result);
  7049. $result = $this->Form->control('1.name');
  7050. $expected = [
  7051. 'label' => ['for' => '1-name'],
  7052. 'Name',
  7053. '/label',
  7054. 'input' => [
  7055. 'type' => 'text', 'name' => '1[name]',
  7056. 'id' => '1-name', 'maxlength' => '255',
  7057. ],
  7058. ];
  7059. $this->assertHtml($expected, $result);
  7060. }
  7061. /**
  7062. * testFormEnd method
  7063. */
  7064. public function testFormEnd(): void
  7065. {
  7066. $this->assertSame('</form>', $this->Form->end());
  7067. }
  7068. /**
  7069. * testMultiRecordForm method
  7070. *
  7071. * Test the generation of fields for a multi record form.
  7072. */
  7073. public function testMultiRecordForm(): void
  7074. {
  7075. $articles = $this->getTableLocator()->get('Articles');
  7076. $articles->hasMany('Comments');
  7077. $comment = new Entity(['comment' => 'Value']);
  7078. $article = new Article(['comments' => [$comment]]);
  7079. $this->Form->create([$article]);
  7080. $result = $this->Form->control('0.comments.1.comment');
  7081. // phpcs:disable
  7082. $expected = [
  7083. 'div' => ['class' => 'input textarea'],
  7084. 'label' => ['for' => '0-comments-1-comment'],
  7085. 'Comment',
  7086. '/label',
  7087. 'textarea' => [
  7088. 'name',
  7089. 'id' => '0-comments-1-comment',
  7090. 'rows' => 5
  7091. ],
  7092. '/textarea',
  7093. '/div'
  7094. ];
  7095. // phpcs:enable
  7096. $this->assertHtml($expected, $result);
  7097. $result = $this->Form->control('0.comments.0.comment');
  7098. // phpcs:disable
  7099. $expected = [
  7100. 'div' => ['class' => 'input textarea'],
  7101. 'label' => ['for' => '0-comments-0-comment'],
  7102. 'Comment',
  7103. '/label',
  7104. 'textarea' => [
  7105. 'name',
  7106. 'id' => '0-comments-0-comment',
  7107. 'rows' => 5
  7108. ],
  7109. 'Value',
  7110. '/textarea',
  7111. '/div'
  7112. ];
  7113. // phpcs:enable
  7114. $this->assertHtml($expected, $result);
  7115. $comment->setError('comment', ['Not valid']);
  7116. $result = $this->Form->control('0.comments.0.comment');
  7117. // phpcs:disable
  7118. $expected = [
  7119. 'div' => ['class' => 'input textarea error'],
  7120. 'label' => ['for' => '0-comments-0-comment'],
  7121. 'Comment',
  7122. '/label',
  7123. 'textarea' => [
  7124. 'name',
  7125. 'class' => 'form-error',
  7126. 'id' => '0-comments-0-comment',
  7127. 'aria-invalid' => 'true',
  7128. 'aria-describedby' => '0-comments-0-comment-error',
  7129. 'rows' => 5
  7130. ],
  7131. 'Value',
  7132. '/textarea',
  7133. ['div' => ['class' => 'error-message', 'id' => '0-comments-0-comment-error']],
  7134. 'Not valid',
  7135. '/div',
  7136. '/div'
  7137. ];
  7138. // phpcs:enable
  7139. $this->assertHtml($expected, $result);
  7140. $this->getTableLocator()->get('Comments')
  7141. ->getValidator('default')
  7142. ->allowEmptyString('comment', null, false);
  7143. $result = $this->Form->control('0.comments.1.comment');
  7144. // phpcs:disable
  7145. $expected = [
  7146. 'div' => ['class' => 'input textarea required'],
  7147. 'label' => ['for' => '0-comments-1-comment'],
  7148. 'Comment',
  7149. '/label',
  7150. 'textarea' => [
  7151. 'name',
  7152. 'aria-required' => 'true',
  7153. 'required' => 'required',
  7154. 'id' => '0-comments-1-comment',
  7155. 'rows' => 5,
  7156. 'data-validity-message' => 'This field cannot be left empty',
  7157. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7158. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7159. ],
  7160. '/textarea',
  7161. '/div'
  7162. ];
  7163. // phpcs:enable
  7164. $this->assertHtml($expected, $result);
  7165. }
  7166. /**
  7167. * testHtml5Controls method
  7168. *
  7169. * Test that some html5 inputs + FormHelper::__call() work.
  7170. */
  7171. public function testHtml5Controls(): void
  7172. {
  7173. $result = $this->Form->email('User.email');
  7174. $expected = [
  7175. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  7176. ];
  7177. $this->assertHtml($expected, $result);
  7178. $result = $this->Form->search('User.query');
  7179. $expected = [
  7180. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  7181. ];
  7182. $this->assertHtml($expected, $result);
  7183. $result = $this->Form->search('User.query', ['value' => 'test']);
  7184. $expected = [
  7185. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  7186. ];
  7187. $this->assertHtml($expected, $result);
  7188. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7189. $expected = [
  7190. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  7191. ];
  7192. $this->assertHtml($expected, $result);
  7193. }
  7194. /**
  7195. * testHtml5ControlWithControl method
  7196. *
  7197. * Test accessing html5 inputs through control().
  7198. */
  7199. public function testHtml5ControlWithControl(): void
  7200. {
  7201. $this->Form->create();
  7202. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7203. $result = $this->Form->control('website', [
  7204. 'type' => 'url',
  7205. 'val' => 'http://domain.tld',
  7206. 'label' => false,
  7207. ]);
  7208. $expected = [
  7209. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7210. ];
  7211. $this->assertHtml($expected, $result);
  7212. }
  7213. /**
  7214. * testHtml5ControlException method
  7215. *
  7216. * Test errors when field name is missing.
  7217. */
  7218. public function testHtml5ControlException(): void
  7219. {
  7220. $this->expectException(CakeException::class);
  7221. $this->Form->email();
  7222. }
  7223. /**
  7224. * tests fields that are required use custom validation messages
  7225. */
  7226. public function testHtml5ErrorMessage(): void
  7227. {
  7228. $this->Form->setConfig('autoSetCustomValidity', true);
  7229. $validator = (new Validator())
  7230. ->notEmptyString('email', 'Custom error message')
  7231. ->requirePresence('password')
  7232. ->alphaNumeric('password')
  7233. ->notBlank('phone');
  7234. $table = $this->getTableLocator()->get('Contacts', [
  7235. 'className' => ContactsTable::class,
  7236. ]);
  7237. $table->setValidator('default', $validator);
  7238. $contact = new Entity();
  7239. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7240. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7241. $result = $this->Form->control('password');
  7242. $expected = [
  7243. 'label' => ['for' => 'password'],
  7244. 'Password',
  7245. '/label',
  7246. 'input' => [
  7247. 'aria-required' => 'true',
  7248. 'id' => 'password',
  7249. 'name' => 'password',
  7250. 'type' => 'password',
  7251. 'value' => '',
  7252. 'required' => 'required',
  7253. 'data-validity-message' => 'This field cannot be left empty',
  7254. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7255. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7256. ],
  7257. ];
  7258. $this->assertHtml($expected, $result);
  7259. $result = $this->Form->control('phone');
  7260. $expected = [
  7261. 'label' => ['for' => 'phone'],
  7262. 'Phone',
  7263. '/label',
  7264. 'input' => [
  7265. 'aria-required' => 'true',
  7266. 'id' => 'phone',
  7267. 'name' => 'phone',
  7268. 'type' => 'tel',
  7269. 'value' => '',
  7270. 'maxlength' => 255,
  7271. 'required' => 'required',
  7272. 'data-validity-message' => 'This field cannot be left empty',
  7273. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7274. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7275. ],
  7276. ];
  7277. $this->assertHtml($expected, $result);
  7278. $result = $this->Form->control('email');
  7279. $expected = [
  7280. 'label' => ['for' => 'email'],
  7281. 'Email',
  7282. '/label',
  7283. 'input' => [
  7284. 'aria-required' => 'true',
  7285. 'id' => 'email',
  7286. 'name' => 'email',
  7287. 'type' => 'email',
  7288. 'value' => '',
  7289. 'maxlength' => 255,
  7290. 'required' => 'required',
  7291. 'data-validity-message' => 'Custom error message',
  7292. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7293. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7294. ],
  7295. ];
  7296. $this->assertHtml($expected, $result);
  7297. }
  7298. /**
  7299. * tests that custom validation messages are in templateVars
  7300. */
  7301. public function testHtml5ErrorMessageInTemplateVars(): void
  7302. {
  7303. $validator = (new Validator())
  7304. ->notEmptyString('email', 'Custom error "message" & entities')
  7305. ->requirePresence('password')
  7306. ->alphaNumeric('password')
  7307. ->notBlank('phone');
  7308. $table = $this->getTableLocator()->get('Contacts', [
  7309. 'className' => ContactsTable::class,
  7310. ]);
  7311. $table->setValidator('default', $validator);
  7312. $contact = new Entity();
  7313. $this->Form->setConfig('autoSetCustomValidity', false);
  7314. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7315. $this->Form->setTemplates([
  7316. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7317. 'inputContainer' => '{{content}}',
  7318. ]);
  7319. $result = $this->Form->control('password');
  7320. $expected = [
  7321. 'label' => ['for' => 'password'],
  7322. 'Password',
  7323. '/label',
  7324. 'input' => [
  7325. 'aria-required' => 'true',
  7326. 'id' => 'password',
  7327. 'name' => 'password',
  7328. 'type' => 'password',
  7329. 'value' => '',
  7330. 'required' => 'required',
  7331. 'data-message' => 'This field cannot be left empty',
  7332. ],
  7333. ];
  7334. $this->assertHtml($expected, $result);
  7335. $result = $this->Form->control('phone');
  7336. $expected = [
  7337. 'label' => ['for' => 'phone'],
  7338. 'Phone',
  7339. '/label',
  7340. 'input' => [
  7341. 'aria-required' => 'true',
  7342. 'id' => 'phone',
  7343. 'name' => 'phone',
  7344. 'type' => 'tel',
  7345. 'value' => '',
  7346. 'maxlength' => 255,
  7347. 'required' => 'required',
  7348. 'data-message' => 'This field cannot be left empty',
  7349. ],
  7350. ];
  7351. $this->assertHtml($expected, $result);
  7352. $result = $this->Form->control('email', [
  7353. 'templateVars' => [
  7354. 'custom' => 'data-custom="1"',
  7355. ],
  7356. ]);
  7357. $expected = [
  7358. 'label' => ['for' => 'email'],
  7359. 'Email',
  7360. '/label',
  7361. 'input' => [
  7362. 'aria-required' => 'true',
  7363. 'id' => 'email',
  7364. 'name' => 'email',
  7365. 'type' => 'email',
  7366. 'value' => '',
  7367. 'maxlength' => 255,
  7368. 'required' => 'required',
  7369. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7370. 'data-custom' => '1',
  7371. ],
  7372. ];
  7373. $this->assertHtml($expected, $result);
  7374. $this->Form->setConfig('autoSetCustomValidity', true);
  7375. }
  7376. /**
  7377. * testRequiredAttribute method
  7378. *
  7379. * Tests that formhelper sets required attributes.
  7380. */
  7381. public function testRequiredAttribute(): void
  7382. {
  7383. $this->article['required'] = [
  7384. 'title' => true,
  7385. 'body' => false,
  7386. ];
  7387. $this->Form->create($this->article);
  7388. $result = $this->Form->control('title');
  7389. $expected = [
  7390. 'div' => ['class' => 'input text required'],
  7391. 'label' => ['for' => 'title'],
  7392. 'Title',
  7393. '/label',
  7394. 'input' => [
  7395. 'type' => 'text',
  7396. 'name' => 'title',
  7397. 'id' => 'title',
  7398. 'aria-required' => 'true',
  7399. 'required' => 'required',
  7400. 'data-validity-message' => 'This field cannot be left empty',
  7401. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7402. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7403. ],
  7404. '/div',
  7405. ];
  7406. $this->assertHtml($expected, $result);
  7407. $result = $this->Form->control('title', ['required' => false]);
  7408. $this->assertStringNotContainsString('required', $result);
  7409. $result = $this->Form->control('body');
  7410. $expected = [
  7411. 'div' => ['class' => 'input text'],
  7412. 'label' => ['for' => 'body'],
  7413. 'Body',
  7414. '/label',
  7415. 'input' => [
  7416. 'type' => 'text',
  7417. 'name' => 'body',
  7418. 'id' => 'body',
  7419. ],
  7420. '/div',
  7421. ];
  7422. $this->assertHtml($expected, $result);
  7423. $result = $this->Form->control('body', ['required' => true]);
  7424. $this->assertStringContainsString('required', $result);
  7425. }
  7426. /**
  7427. * testControlsNotNested method
  7428. *
  7429. * Tests that it is possible to put inputs outside of the label.
  7430. */
  7431. public function testControlsNotNested(): void
  7432. {
  7433. $this->Form->setTemplates([
  7434. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7435. 'formGroup' => '{{input}}{{label}}',
  7436. ]);
  7437. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7438. $expected = [
  7439. 'div' => ['class' => 'input checkbox'],
  7440. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7441. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7442. 'label' => ['for' => 'foo'],
  7443. 'Foo',
  7444. '/label',
  7445. '/div',
  7446. ];
  7447. $this->assertHtml($expected, $result);
  7448. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7449. $expected = [
  7450. 'div' => ['class' => 'input checkbox'],
  7451. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7452. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7453. '/div',
  7454. ];
  7455. $this->assertHtml($expected, $result);
  7456. $result = $this->Form->control('confirm', [
  7457. 'type' => 'radio',
  7458. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7459. ]);
  7460. $expected = [
  7461. 'div' => ['class' => 'input radio'],
  7462. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '', 'id' => 'confirm']],
  7463. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7464. ['label' => ['for' => 'confirm-y']],
  7465. 'Yes',
  7466. '/label',
  7467. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7468. ['label' => ['for' => 'confirm-n']],
  7469. 'No',
  7470. '/label',
  7471. '<label',
  7472. 'Confirm',
  7473. '/label',
  7474. '/div',
  7475. ];
  7476. $this->assertHtml($expected, $result);
  7477. $result = $this->Form->select('category', ['1', '2'], [
  7478. 'multiple' => 'checkbox',
  7479. 'name' => 'fish',
  7480. ]);
  7481. $expected = [
  7482. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  7483. ['div' => ['class' => 'checkbox']],
  7484. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7485. ['label' => ['for' => 'fish-0']],
  7486. '1',
  7487. '/label',
  7488. '/div',
  7489. ['div' => ['class' => 'checkbox']],
  7490. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7491. ['label' => ['for' => 'fish-1']],
  7492. '2',
  7493. '/label',
  7494. '/div',
  7495. ];
  7496. $this->assertHtml($expected, $result);
  7497. }
  7498. /**
  7499. * testControlContainerTemplates method
  7500. *
  7501. * Test that *Container templates are used by input.
  7502. */
  7503. public function testControlContainerTemplates(): void
  7504. {
  7505. $this->Form->setTemplates([
  7506. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7507. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7508. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7509. ]);
  7510. $this->article['errors'] = [
  7511. 'Article' => ['published' => 'error message'],
  7512. ];
  7513. $this->Form->create($this->article);
  7514. $result = $this->Form->control('accept', [
  7515. 'type' => 'checkbox',
  7516. ]);
  7517. $expected = [
  7518. 'div' => ['class' => 'check'],
  7519. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7520. 'label' => ['for' => 'accept'],
  7521. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7522. 'Accept',
  7523. '/label',
  7524. '/div',
  7525. ];
  7526. $this->assertHtml($expected, $result);
  7527. $result = $this->Form->control('accept', [
  7528. 'type' => 'radio',
  7529. 'options' => ['Y', 'N'],
  7530. ]);
  7531. $this->assertStringContainsString('<div class="rad">', $result);
  7532. $result = $this->Form->control('Article.published', [
  7533. 'type' => 'radio',
  7534. 'options' => ['Y', 'N'],
  7535. ]);
  7536. $this->assertStringContainsString('<div class="rad err">', $result);
  7537. }
  7538. /**
  7539. * testFormGroupTemplates method
  7540. *
  7541. * Test that *Container templates are used by input.
  7542. */
  7543. public function testFormGroupTemplates(): void
  7544. {
  7545. $this->Form->setTemplates([
  7546. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7547. ]);
  7548. $this->Form->create($this->article);
  7549. $result = $this->Form->control('accept', [
  7550. 'type' => 'radio',
  7551. 'options' => ['Y', 'N'],
  7552. ]);
  7553. $this->assertStringContainsString('<div class="radio">', $result);
  7554. }
  7555. /**
  7556. * testResetTemplates method
  7557. *
  7558. * Test resetting templates.
  7559. */
  7560. public function testResetTemplates(): void
  7561. {
  7562. $this->Form->setTemplates(['input' => '<input/>']);
  7563. $this->assertSame('<input/>', $this->Form->templater()->get('input'));
  7564. $this->Form->resetTemplates();
  7565. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7566. }
  7567. /**
  7568. * testContext method
  7569. *
  7570. * Test the context method.
  7571. */
  7572. public function testContext(): void
  7573. {
  7574. $result = $this->Form->context();
  7575. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7576. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7577. $this->assertSame($mock, $this->Form->context($mock));
  7578. $this->assertSame($mock, $this->Form->context());
  7579. }
  7580. /**
  7581. * testAutoDomId method
  7582. */
  7583. public function testAutoDomId(): void
  7584. {
  7585. $result = $this->Form->text('field', ['id' => true]);
  7586. $expected = [
  7587. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7588. ];
  7589. $this->assertHtml($expected, $result);
  7590. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7591. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7592. $expected = [
  7593. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '', 'id' => 'field'],
  7594. ['label' => ['for' => 'field-0']],
  7595. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7596. 'option A',
  7597. '/label',
  7598. ['label' => ['for' => 'field-1']],
  7599. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7600. 'option B',
  7601. '/label',
  7602. ];
  7603. $this->assertHtml($expected, $result);
  7604. $result = $this->Form->select(
  7605. 'multi_field',
  7606. ['first', 'second'],
  7607. ['multiple' => 'checkbox', 'id' => true]
  7608. );
  7609. $expected = [
  7610. 'input' => [
  7611. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '', 'id' => 'multi-field',
  7612. ],
  7613. ['div' => ['class' => 'checkbox']],
  7614. ['label' => ['for' => 'multi-field-0']],
  7615. ['input' => [
  7616. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7617. 'value' => '0', 'id' => 'multi-field-0',
  7618. ]],
  7619. 'first',
  7620. '/label',
  7621. '/div',
  7622. ['div' => ['class' => 'checkbox']],
  7623. ['label' => ['for' => 'multi-field-1']],
  7624. ['input' => [
  7625. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7626. 'value' => '1', 'id' => 'multi-field-1',
  7627. ]],
  7628. 'second',
  7629. '/label',
  7630. '/div',
  7631. ];
  7632. $this->assertHtml($expected, $result);
  7633. }
  7634. /**
  7635. * Test the basic setters and getters for value sources
  7636. */
  7637. public function testFormValueSourcesSettersGetters(): void
  7638. {
  7639. $this->View->setRequest($this->View->getRequest()
  7640. ->withData('id', '1')
  7641. ->withQueryParams(['id' => '2']));
  7642. $expected = ['data', 'context'];
  7643. $result = $this->Form->getValueSources();
  7644. $this->assertEquals($expected, $result);
  7645. $this->Form->setValueSources(['context']);
  7646. $result = $this->Form->getSourceValue('id');
  7647. $this->assertNull($result);
  7648. $this->Form->setValueSources('query');
  7649. $expected = ['query'];
  7650. $result = $this->Form->getValueSources();
  7651. $this->assertEquals($expected, $result);
  7652. $expected = '2';
  7653. $result = $this->Form->getSourceValue('id');
  7654. $this->assertSame($expected, $result);
  7655. $this->Form->setValueSources(['data']);
  7656. $expected = '1';
  7657. $result = $this->Form->getSourceValue('id');
  7658. $this->assertSame($expected, $result);
  7659. $this->Form->setValueSources(['query', 'data']);
  7660. $expected = '2';
  7661. $result = $this->Form->getSourceValue('id');
  7662. $this->assertSame($expected, $result);
  7663. }
  7664. public function testValueSourcesValidation(): void
  7665. {
  7666. $this->expectException(InvalidArgumentException::class);
  7667. $this->expectExceptionMessage('Invalid value source(s): `invalid`, `foo`. Valid values are: `context`, `data`, `query`.');
  7668. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7669. }
  7670. /**
  7671. * Tests the different input rendering values based on sources values switching
  7672. */
  7673. public function testFormValueSourcesSingleSwitchRendering(): void
  7674. {
  7675. $articles = $this->getTableLocator()->get('Articles');
  7676. $article = new Article();
  7677. $articles->patchEntity($article, ['id' => '3']);
  7678. $this->Form->create($article);
  7679. $this->Form->setValueSources(['context']);
  7680. $result = $this->Form->control('id');
  7681. $expected = [
  7682. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7683. ];
  7684. $this->assertHtml($expected, $result);
  7685. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7686. $this->Form->setValueSources(['query']);
  7687. $result = $this->Form->control('id');
  7688. $expected = [
  7689. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7690. ];
  7691. $this->assertHtml($expected, $result);
  7692. $this->View->setRequest($this->View->getRequest()
  7693. ->withQueryParams(['id' => '5a'])
  7694. ->withData('id', '5b'));
  7695. $this->Form->setValueSources(['context']);
  7696. $this->Form->create($article);
  7697. $result = $this->Form->control('id');
  7698. $expected = [
  7699. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7700. ];
  7701. $this->assertHtml($expected, $result);
  7702. $this->Form->setValueSources(['data']);
  7703. $this->Form->create($article);
  7704. $result = $this->Form->control('id');
  7705. $expected = [
  7706. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7707. ];
  7708. $this->assertHtml($expected, $result);
  7709. $this->Form->setValueSources(['query']);
  7710. $result = $this->Form->control('id');
  7711. $expected = [
  7712. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7713. ];
  7714. $this->assertHtml($expected, $result);
  7715. }
  7716. /**
  7717. * Tests the different input rendering values based on sources values switching while supplying
  7718. * an entity (base context) and multiple sources (such as data, query)
  7719. */
  7720. public function testFormValueSourcesListSwitchRendering(): void
  7721. {
  7722. $articles = $this->getTableLocator()->get('Articles');
  7723. $article = new Article();
  7724. $articles->patchEntity($article, ['id' => '3']);
  7725. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7726. $this->Form->create($article);
  7727. $this->Form->setValueSources(['context', 'query']);
  7728. $result = $this->Form->control('id');
  7729. $expected = [
  7730. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7731. ];
  7732. $this->assertHtml($expected, $result);
  7733. $this->Form->setValueSources(['query', 'context']);
  7734. $result = $this->Form->control('id');
  7735. $expected = [
  7736. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7737. ];
  7738. $this->assertHtml($expected, $result);
  7739. $this->Form->setValueSources(['data', 'query', 'context']);
  7740. $result = $this->Form->control('id');
  7741. $expected = [
  7742. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7743. ];
  7744. $this->assertHtml($expected, $result);
  7745. $this->View->setRequest($this->View->getRequest()
  7746. ->withData('id', '8')
  7747. ->withQueryParams(['id' => '9']));
  7748. $this->Form->setValueSources(['data', 'query', 'context']);
  7749. $result = $this->Form->control('id');
  7750. $expected = [
  7751. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7752. ];
  7753. $this->assertHtml($expected, $result);
  7754. }
  7755. /**
  7756. * Test the different form input renderings based on values sources switchings through form options
  7757. */
  7758. public function testFormValueSourcesSwitchViaOptionsRendering(): void
  7759. {
  7760. $articles = $this->getTableLocator()->get('Articles');
  7761. $article = new Article();
  7762. $articles->patchEntity($article, ['id' => '3']);
  7763. $this->View->setRequest(
  7764. $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
  7765. );
  7766. $this->Form->create($article, ['valueSources' => 'query']);
  7767. $result = $this->Form->control('id');
  7768. $expected = [
  7769. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7770. ];
  7771. $this->assertHtml($expected, $result);
  7772. $result = $this->Form->getSourceValue('id');
  7773. $this->assertSame('5', $result);
  7774. $this->Form->setValueSources(['context']);
  7775. $this->Form->create($article, ['valueSources' => 'query']);
  7776. $result = $this->Form->control('id');
  7777. $expected = [
  7778. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7779. ];
  7780. $this->assertHtml($expected, $result);
  7781. $result = $this->Form->getSourceValue('id');
  7782. $this->assertSame('5', $result);
  7783. $this->Form->setValueSources(['query']);
  7784. $this->Form->create($article, ['valueSources' => 'data']);
  7785. $result = $this->Form->control('id');
  7786. $expected = [
  7787. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7788. ];
  7789. $this->assertHtml($expected, $result);
  7790. $result = $this->Form->getSourceValue('id');
  7791. $this->assertSame('4', $result);
  7792. $this->Form->setValueSources(['query']);
  7793. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7794. $result = $this->Form->control('id');
  7795. $expected = [
  7796. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7797. ];
  7798. $this->assertHtml($expected, $result);
  7799. $result = $this->Form->getSourceValue('id');
  7800. $this->assertSame(3, $result);
  7801. }
  7802. /**
  7803. * Test the different form input renderings based on values sources switchings through form options
  7804. */
  7805. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering(): void
  7806. {
  7807. $articles = $this->getTableLocator()->get('Articles');
  7808. $article = new Article();
  7809. $articles->patchEntity($article, ['id' => '3']);
  7810. $this->View->setRequest(
  7811. $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
  7812. );
  7813. $this->Form->setValueSources(['context'])
  7814. ->create($article, ['valueSources' => ['query', 'data']]);
  7815. $result = $this->Form->control('id');
  7816. $expected = [
  7817. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7818. ];
  7819. $this->assertHtml($expected, $result);
  7820. $result = $this->Form->getSourceValue('id');
  7821. $this->assertSame('11', $result);
  7822. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7823. $this->Form->setValueSources(['context'])
  7824. ->create($article, ['valueSources' => ['query', 'data']]);
  7825. $result = $this->Form->control('id');
  7826. $expected = [
  7827. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7828. ];
  7829. $this->assertHtml($expected, $result);
  7830. $result = $this->Form->getSourceValue('id');
  7831. $this->assertSame('10', $result);
  7832. }
  7833. /**
  7834. * Test the different form values sources resetting through From::end();
  7835. */
  7836. public function testFormValueSourcesResetViaEnd(): void
  7837. {
  7838. $expected = ['data', 'context'];
  7839. $result = $this->Form->getValueSources();
  7840. $this->assertEquals($expected, $result);
  7841. $expected = ['query', 'context', 'data'];
  7842. $this->Form->setValueSources(['query', 'context', 'data']);
  7843. $result = $this->Form->getValueSources();
  7844. $this->assertEquals($expected, $result);
  7845. $this->Form->create();
  7846. $result = $this->Form->getValueSources();
  7847. $this->assertEquals($expected, $result);
  7848. $this->Form->end();
  7849. $result = $this->Form->getValueSources();
  7850. $this->assertEquals(['data', 'context'], $result);
  7851. }
  7852. /**
  7853. * Test sources values defaults handling
  7854. */
  7855. public function testFormValueSourcesDefaults(): void
  7856. {
  7857. $this->View->setRequest(
  7858. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7859. );
  7860. $this->Form->create();
  7861. $result = $this->Form->password('password');
  7862. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7863. $this->assertHtml($expected, $result);
  7864. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7865. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7866. $this->assertHtml($expected, $result);
  7867. $this->Form->setValueSources('query');
  7868. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7869. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7870. $this->assertHtml($expected, $result);
  7871. $this->Form->setValueSources('data');
  7872. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7873. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7874. $this->assertHtml($expected, $result);
  7875. }
  7876. /**
  7877. * Test sources values schema defaults handling
  7878. */
  7879. public function testSourcesValueDoesntExistPassThrough(): void
  7880. {
  7881. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7882. $articles = $this->getTableLocator()->get('Articles');
  7883. $entity = $articles->newEmptyEntity();
  7884. $this->Form->create($entity);
  7885. $this->Form->setValueSources(['query', 'context']);
  7886. $result = $this->Form->getSourceValue('category');
  7887. $this->assertSame('sesame-cookies', $result);
  7888. $this->Form->setValueSources(['context', 'query']);
  7889. $result = $this->Form->getSourceValue('category');
  7890. $this->assertSame('sesame-cookies', $result);
  7891. }
  7892. /**
  7893. * testNestedLabelInput method
  7894. *
  7895. * Test the `nestedInput` parameter
  7896. */
  7897. public function testNestedLabelInput(): void
  7898. {
  7899. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7900. $expected = [
  7901. 'div' => ['class' => 'input text'],
  7902. 'label' => ['for' => 'foo'],
  7903. ['input' => [
  7904. 'type' => 'text',
  7905. 'name' => 'foo',
  7906. 'id' => 'foo',
  7907. ]],
  7908. 'Foo',
  7909. '/label',
  7910. '/div',
  7911. ];
  7912. $this->assertHtml($expected, $result);
  7913. }
  7914. /**
  7915. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7916. *
  7917. * This test makes sure `false` excludes the label from the render
  7918. */
  7919. public function testControlLabelManipulationDisableLabels(): void
  7920. {
  7921. $result = $this->Form->control('test', [
  7922. 'type' => 'radio',
  7923. 'options' => ['A', 'B'],
  7924. 'labelOptions' => false,
  7925. ]);
  7926. $expected = [
  7927. ['div' => ['class' => 'input radio']],
  7928. '<label',
  7929. 'Test',
  7930. '/label',
  7931. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7932. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7933. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7934. '/div',
  7935. ];
  7936. $this->assertHtml($expected, $result);
  7937. $result = $this->Form->control('checkbox1', [
  7938. 'label' => 'My checkboxes',
  7939. 'multiple' => 'checkbox',
  7940. 'type' => 'select',
  7941. 'options' => [
  7942. ['text' => 'First Checkbox', 'value' => 1],
  7943. ['text' => 'Second Checkbox', 'value' => 2],
  7944. ],
  7945. 'labelOptions' => false,
  7946. ]);
  7947. $expected = [
  7948. ['div' => ['class' => 'input select']],
  7949. ['label' => ['for' => 'checkbox1']],
  7950. 'My checkboxes',
  7951. '/label',
  7952. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7953. ['div' => ['class' => 'checkbox']],
  7954. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7955. '/div',
  7956. ['div' => ['class' => 'checkbox']],
  7957. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7958. '/div',
  7959. '/div',
  7960. ];
  7961. $this->assertHtml($expected, $result);
  7962. }
  7963. /**
  7964. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7965. *
  7966. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7967. * added to the class if checked.
  7968. *
  7969. * Also checks to make sure any custom attributes are rendered correctly
  7970. */
  7971. public function testControlLabelManipulationRadios(): void
  7972. {
  7973. $result = $this->Form->control('test', [
  7974. 'type' => 'radio',
  7975. 'options' => ['A', 'B'],
  7976. 'labelOptions' => ['class' => 'custom-class'],
  7977. ]);
  7978. $expected = [
  7979. ['div' => ['class' => 'input radio']],
  7980. '<label',
  7981. 'Test',
  7982. '/label',
  7983. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7984. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7985. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7986. 'A',
  7987. '/label',
  7988. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  7989. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7990. 'B',
  7991. '/label',
  7992. '/div',
  7993. ];
  7994. $this->assertHtml($expected, $result);
  7995. $result = $this->Form->control('test', [
  7996. 'type' => 'radio',
  7997. 'options' => ['A', 'B'],
  7998. 'value' => 1,
  7999. 'labelOptions' => ['class' => 'custom-class'],
  8000. ]);
  8001. $expected = [
  8002. ['div' => ['class' => 'input radio']],
  8003. '<label',
  8004. 'Test',
  8005. '/label',
  8006. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  8007. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8008. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8009. 'A',
  8010. '/label',
  8011. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8012. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8013. 'B',
  8014. '/label',
  8015. '/div',
  8016. ];
  8017. $this->assertHtml($expected, $result);
  8018. $result = $this->Form->control('test', [
  8019. 'type' => 'radio',
  8020. 'options' => ['A', 'B'],
  8021. 'value' => 1,
  8022. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  8023. ]);
  8024. $expected = [
  8025. ['div' => ['class' => 'input radio']],
  8026. '<label',
  8027. 'Test',
  8028. '/label',
  8029. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  8030. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8031. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8032. 'A',
  8033. '/label',
  8034. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8035. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8036. 'B',
  8037. '/label',
  8038. '/div',
  8039. ];
  8040. $this->assertHtml($expected, $result);
  8041. $result = $this->Form->radio('test', ['A', 'B'], [
  8042. 'label' => [
  8043. 'class' => ['custom-class', 'another-class'],
  8044. 'data-name' => 'bob',
  8045. ],
  8046. 'value' => 1,
  8047. ]);
  8048. $expected = [
  8049. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test'],
  8050. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8051. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8052. 'A',
  8053. '/label',
  8054. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8055. ['input' => [
  8056. 'type' => 'radio',
  8057. 'name' => 'test',
  8058. 'value' => '1',
  8059. 'id' => 'test-1',
  8060. 'checked' => 'checked',
  8061. ]],
  8062. 'B',
  8063. '/label',
  8064. ];
  8065. $this->assertHtml($expected, $result);
  8066. }
  8067. /**
  8068. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8069. *
  8070. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8071. * added to the class if checked.
  8072. *
  8073. * Also checks to make sure any custom attributes are rendered correctly
  8074. */
  8075. public function testControlLabelManipulationCheckboxes(): void
  8076. {
  8077. $result = $this->Form->control('checkbox1', [
  8078. 'label' => 'My checkboxes',
  8079. 'multiple' => 'checkbox',
  8080. 'type' => 'select',
  8081. 'options' => [
  8082. ['text' => 'First Checkbox', 'value' => 1],
  8083. ['text' => 'Second Checkbox', 'value' => 2],
  8084. ],
  8085. 'labelOptions' => ['class' => 'custom-class'],
  8086. 'value' => ['1'],
  8087. ]);
  8088. $expected = [
  8089. ['div' => ['class' => 'input select']],
  8090. ['label' => ['for' => 'checkbox1']],
  8091. 'My checkboxes',
  8092. '/label',
  8093. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  8094. ['div' => ['class' => 'checkbox']],
  8095. ['label' => [
  8096. 'class' => 'custom-class selected',
  8097. 'for' => 'checkbox1-1',
  8098. ]],
  8099. ['input' => [
  8100. 'type' => 'checkbox',
  8101. 'name' => 'checkbox1[]',
  8102. 'value' => '1',
  8103. 'id' => 'checkbox1-1',
  8104. 'checked' => 'checked',
  8105. ]],
  8106. 'First Checkbox',
  8107. '/label',
  8108. '/div',
  8109. ['div' => ['class' => 'checkbox']],
  8110. ['label' => [
  8111. 'class' => 'custom-class',
  8112. 'for' => 'checkbox1-2',
  8113. ]],
  8114. ['input' => [
  8115. 'type' => 'checkbox',
  8116. 'name' => 'checkbox1[]',
  8117. 'value' => '2',
  8118. 'id' => 'checkbox1-2',
  8119. ]],
  8120. 'Second Checkbox',
  8121. '/label',
  8122. '/div',
  8123. '/div',
  8124. ];
  8125. $this->assertHtml($expected, $result);
  8126. $result = $this->Form->control('checkbox1', [
  8127. 'label' => 'My checkboxes',
  8128. 'multiple' => 'checkbox',
  8129. 'type' => 'select',
  8130. 'options' => [
  8131. ['text' => 'First Checkbox', 'value' => 1],
  8132. ['text' => 'Second Checkbox', 'value' => 2],
  8133. ],
  8134. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8135. 'value' => ['1'],
  8136. ]);
  8137. $expected = [
  8138. ['div' => ['class' => 'input select']],
  8139. ['label' => ['for' => 'checkbox1']],
  8140. 'My checkboxes',
  8141. '/label',
  8142. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  8143. ['div' => ['class' => 'checkbox']],
  8144. ['label' => [
  8145. 'class' => 'custom-class another-class selected',
  8146. 'data-name' => 'bob',
  8147. 'for' => 'checkbox1-1',
  8148. ]],
  8149. ['input' => [
  8150. 'type' => 'checkbox',
  8151. 'name' => 'checkbox1[]',
  8152. 'value' => '1',
  8153. 'id' => 'checkbox1-1',
  8154. 'checked' => 'checked',
  8155. ]],
  8156. 'First Checkbox',
  8157. '/label',
  8158. '/div',
  8159. ['div' => ['class' => 'checkbox']],
  8160. ['label' => [
  8161. 'class' => 'custom-class another-class',
  8162. 'data-name' => 'bob',
  8163. 'for' => 'checkbox1-2',
  8164. ]],
  8165. ['input' => [
  8166. 'type' => 'checkbox',
  8167. 'name' => 'checkbox1[]',
  8168. 'value' => '2',
  8169. 'id' => 'checkbox1-2',
  8170. ]],
  8171. 'Second Checkbox',
  8172. '/label',
  8173. '/div',
  8174. '/div',
  8175. ];
  8176. $this->assertHtml($expected, $result);
  8177. }
  8178. /**
  8179. * testControlMaxLengthArrayContext method
  8180. *
  8181. * Test control() with maxlength attribute in Array Context.
  8182. */
  8183. public function testControlMaxLengthArrayContext(): void
  8184. {
  8185. $this->article['schema'] = [
  8186. 'title' => ['length' => 10],
  8187. ];
  8188. $this->Form->create($this->article);
  8189. $result = $this->Form->control('title');
  8190. $expected = [
  8191. 'div' => ['class'],
  8192. 'label' => ['for'],
  8193. 'Title',
  8194. '/label',
  8195. 'input' => [
  8196. 'aria-required' => 'true',
  8197. 'id',
  8198. 'name' => 'title',
  8199. 'type' => 'text',
  8200. 'required' => 'required',
  8201. 'maxlength' => 10,
  8202. 'data-validity-message' => 'This field cannot be left empty',
  8203. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8204. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8205. ],
  8206. '/div',
  8207. ];
  8208. $this->assertHtml($expected, $result);
  8209. }
  8210. /**
  8211. * testControlMaxLengthEntityContext method
  8212. *
  8213. * Test control() with maxlength attribute in Entity Context.
  8214. */
  8215. public function testControlMaxLengthEntityContext(): void
  8216. {
  8217. $this->article['schema']['title']['length'] = 45;
  8218. $validator = new Validator();
  8219. $validator->maxLength('title', 10);
  8220. $article = new EntityContext(
  8221. [
  8222. 'entity' => new Entity($this->article),
  8223. 'table' => new Table([
  8224. 'alias' => 'Articles',
  8225. 'schema' => $this->article['schema'],
  8226. 'validator' => $validator,
  8227. ]),
  8228. ]
  8229. );
  8230. $this->Form->create($article);
  8231. $result = $this->Form->control('title');
  8232. $expected = [
  8233. 'div' => ['class'],
  8234. 'label' => ['for'],
  8235. 'Title',
  8236. '/label',
  8237. 'input' => [
  8238. 'aria-required' => 'true',
  8239. 'id',
  8240. 'name' => 'title',
  8241. 'type' => 'text',
  8242. 'required' => 'required',
  8243. 'maxlength' => 10,
  8244. 'data-validity-message' => 'This field cannot be left empty',
  8245. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8246. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8247. ],
  8248. '/div',
  8249. ];
  8250. $this->assertHtml($expected, $result);
  8251. $this->article['schema']['title']['length'] = 45;
  8252. $validator = new Validator();
  8253. $validator->maxLength('title', 55);
  8254. $article = new EntityContext(
  8255. [
  8256. 'entity' => new Entity($this->article),
  8257. 'table' => new Table([
  8258. 'schema' => $this->article['schema'],
  8259. 'validator' => $validator,
  8260. 'alias' => 'Articles',
  8261. ]),
  8262. ]
  8263. );
  8264. $this->Form->create($article);
  8265. $result = $this->Form->control('title');
  8266. $expected = [
  8267. 'div' => ['class'],
  8268. 'label' => ['for'],
  8269. 'Title',
  8270. '/label',
  8271. 'input' => [
  8272. 'aria-required' => 'true',
  8273. 'id',
  8274. 'name' => 'title',
  8275. 'type' => 'text',
  8276. 'required' => 'required',
  8277. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8278. 'data-validity-message' => 'This field cannot be left empty',
  8279. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8280. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8281. ],
  8282. '/div',
  8283. ];
  8284. $this->assertHtml($expected, $result);
  8285. $this->article['schema']['title']['length'] = 45;
  8286. $validator = new Validator();
  8287. $validator->maxLength('title', 55);
  8288. $article = new EntityContext(
  8289. [
  8290. 'entity' => new Entity($this->article),
  8291. 'table' => new Table([
  8292. 'schema' => $this->article['schema'],
  8293. 'validator' => $validator,
  8294. 'alias' => 'Articles',
  8295. ]),
  8296. ]
  8297. );
  8298. $this->Form->create($article);
  8299. $result = $this->Form->control('title', ['maxlength' => 10]);
  8300. $expected = [
  8301. 'div' => ['class'],
  8302. 'label' => ['for'],
  8303. 'Title',
  8304. '/label',
  8305. 'input' => [
  8306. 'aria-required' => 'true',
  8307. 'id',
  8308. 'name' => 'title',
  8309. 'type' => 'text',
  8310. 'required' => 'required',
  8311. 'maxlength' => 10, // Length set in options should take highest precedence.
  8312. 'data-validity-message' => 'This field cannot be left empty',
  8313. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8314. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8315. ],
  8316. '/div',
  8317. ];
  8318. $this->assertHtml($expected, $result);
  8319. }
  8320. /**
  8321. * testControlMinMaxLengthEntityContext method
  8322. *
  8323. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8324. */
  8325. public function testControlMinMaxLengthEntityContext(): void
  8326. {
  8327. $validator = new Validator();
  8328. $validator->maxLength('title', 10);
  8329. $article = new EntityContext(
  8330. [
  8331. 'entity' => new Entity($this->article),
  8332. 'table' => new Table([
  8333. 'alias' => 'Articles',
  8334. 'schema' => $this->article['schema'],
  8335. 'validator' => $validator,
  8336. ]),
  8337. ]
  8338. );
  8339. $this->Form->create($article);
  8340. $result = $this->Form->control('title');
  8341. $expected = [
  8342. 'div' => ['class'],
  8343. 'label' => ['for'],
  8344. 'Title',
  8345. '/label',
  8346. 'input' => [
  8347. 'aria-required' => 'true',
  8348. 'id',
  8349. 'name' => 'title',
  8350. 'type' => 'text',
  8351. 'required' => 'required',
  8352. 'maxlength' => 10,
  8353. 'data-validity-message' => 'This field cannot be left empty',
  8354. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8355. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8356. ],
  8357. '/div',
  8358. ];
  8359. $this->assertHtml($expected, $result);
  8360. }
  8361. /**
  8362. * testControlMaxLengthFormContext method
  8363. *
  8364. * Test control() with maxlength attribute in Form Context.
  8365. */
  8366. public function testControlMaxLengthFormContext(): void
  8367. {
  8368. $validator = new Validator();
  8369. $validator->maxLength('title', 10);
  8370. $form = new Form();
  8371. $form->setValidator('default', $validator);
  8372. $this->Form->create($form);
  8373. $result = $this->Form->control('title');
  8374. $expected = [
  8375. 'div' => ['class'],
  8376. 'label' => ['for'],
  8377. 'Title',
  8378. '/label',
  8379. 'input' => [
  8380. 'aria-required' => 'true',
  8381. 'id',
  8382. 'name' => 'title',
  8383. 'type' => 'text',
  8384. 'required' => 'required',
  8385. 'maxlength' => 10,
  8386. 'data-validity-message' => 'This field cannot be left empty',
  8387. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8388. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8389. ],
  8390. '/div',
  8391. ];
  8392. $this->assertHtml($expected, $result);
  8393. }
  8394. }