FormHelperTest.php 306 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891
  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\Collection\Collection;
  19. use Cake\Core\Configure;
  20. use Cake\Core\Exception\CakeException;
  21. use Cake\Form\Form;
  22. use Cake\Http\ServerRequest;
  23. use Cake\I18n\Date;
  24. use Cake\I18n\DateTime;
  25. use Cake\ORM\Entity;
  26. use Cake\ORM\Table;
  27. use Cake\Routing\Router;
  28. use Cake\TestSuite\TestCase;
  29. use Cake\Utility\Security;
  30. use Cake\Validation\Validator;
  31. use Cake\View\Form\EntityContext;
  32. use Cake\View\Helper\FormHelper;
  33. use Cake\View\View;
  34. use Cake\View\Widget\WidgetLocator;
  35. use InvalidArgumentException;
  36. use ReflectionProperty;
  37. use TestApp\Model\Entity\Article;
  38. use TestApp\Model\Table\ContactsTable;
  39. use TestApp\Model\Table\ValidateUsersTable;
  40. /**
  41. * FormHelperTest class
  42. *
  43. * @property \Cake\View\Helper\FormHelper $Form
  44. * @property \Cake\View\View $View
  45. */
  46. class FormHelperTest extends TestCase
  47. {
  48. /**
  49. * Fixtures to be used
  50. *
  51. * @var array<string>
  52. */
  53. protected array $fixtures = ['core.Articles', 'core.Comments'];
  54. /**
  55. * @var array
  56. */
  57. protected $article = [];
  58. /**
  59. * @var string
  60. */
  61. protected $url;
  62. /**
  63. * @var \Cake\View\Helper\FormHelper
  64. */
  65. protected $Form;
  66. /**
  67. * @var \Cake\View\View
  68. */
  69. protected $View;
  70. /**
  71. * setUp method
  72. */
  73. public function setUp(): void
  74. {
  75. parent::setUp();
  76. Configure::write('Config.language', 'eng');
  77. Configure::write('App.base', '');
  78. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  79. $request = new ServerRequest([
  80. 'webroot' => '',
  81. 'base' => '',
  82. 'url' => '/articles/add',
  83. 'params' => [
  84. 'controller' => 'Articles',
  85. 'action' => 'add',
  86. 'plugin' => null,
  87. ],
  88. ]);
  89. $this->View = new View($request);
  90. Router::reload();
  91. Router::setRequest($request);
  92. $this->url = '/articles/add';
  93. $this->Form = new FormHelper($this->View);
  94. $this->article = [
  95. 'schema' => [
  96. 'id' => ['type' => 'integer'],
  97. 'author_id' => ['type' => 'integer', 'null' => true],
  98. 'title' => ['type' => 'string', 'null' => true],
  99. 'body' => 'text',
  100. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  101. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  102. ],
  103. 'required' => [
  104. 'author_id' => true,
  105. 'title' => true,
  106. ],
  107. ];
  108. Security::setSalt('foo!');
  109. $builder = Router::createRouteBuilder('/');
  110. $builder->connect('/{controller}', ['action' => 'index']);
  111. $builder->connect('/{controller}/{action}/*');
  112. }
  113. /**
  114. * tearDown method
  115. */
  116. public function tearDown(): void
  117. {
  118. parent::tearDown();
  119. unset($this->Form, $this->Controller, $this->View);
  120. }
  121. /**
  122. * Test construct() with the templates option.
  123. */
  124. public function testConstructTemplatesFile(): void
  125. {
  126. $helper = new FormHelper($this->View, [
  127. 'templates' => 'htmlhelper_tags',
  128. ]);
  129. $result = $helper->control('name');
  130. $this->assertStringContainsString('<input', $result);
  131. }
  132. /**
  133. * Test that when specifying custom widgets the config array for that widget
  134. * is overwritten instead of merged.
  135. */
  136. public function testConstructWithWidgets(): void
  137. {
  138. $config = [
  139. 'widgets' => [
  140. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  141. ],
  142. ];
  143. $helper = new FormHelper($this->View, $config);
  144. $locator = $helper->getWidgetLocator();
  145. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  146. }
  147. /**
  148. * Test that when specifying custom widgets config file and it should be
  149. * added to widgets array. WidgetLocator will load widgets in constructor.
  150. */
  151. public function testConstructWithWidgetsConfig(): void
  152. {
  153. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  154. $locator = $helper->getWidgetLocator();
  155. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  156. }
  157. /**
  158. * Test setting the widget locator
  159. */
  160. public function testSetAndGetWidgetLocator(): void
  161. {
  162. $helper = new FormHelper($this->View);
  163. $locator = new WidgetLocator($helper->templater(), $this->View);
  164. $helper->setWidgetLocator($locator);
  165. $this->assertSame($locator, $helper->getWidgetLocator());
  166. }
  167. /**
  168. * Test overridding grouped input types which controls generation of "for"
  169. * attribute of labels.
  170. */
  171. public function testConstructWithGroupedInputTypes(): void
  172. {
  173. $helper = new FormHelper($this->View, [
  174. 'groupedInputTypes' => ['radio'],
  175. ]);
  176. $result = $helper->control('when', ['type' => 'datetime-local']);
  177. $this->assertStringContainsString('<label for="when">When</label>', $result);
  178. }
  179. /**
  180. * Test registering a new widget class and rendering it.
  181. */
  182. public function testAddWidgetAndRenderWidget(): void
  183. {
  184. $data = [
  185. 'val' => 1,
  186. ];
  187. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  188. $this->Form->addWidget('test', $mock);
  189. $mock->expects($this->once())
  190. ->method('render')
  191. ->with($data)
  192. ->will($this->returnValue('HTML'));
  193. $result = $this->Form->widget('test', $data);
  194. $this->assertSame('HTML', $result);
  195. }
  196. /**
  197. * Test that secureFields() of widget is called after calling render(),
  198. * not before.
  199. */
  200. public function testOrderForRenderingWidgetAndFetchingSecureFields(): void
  201. {
  202. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  203. 'unlockedFields' => [],
  204. ]));
  205. $data = [
  206. 'val' => 1,
  207. 'name' => 'test',
  208. ];
  209. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  210. $this->Form->addWidget('test', $mock);
  211. $mock->expects($this->once())
  212. ->method('render')
  213. ->with($data)
  214. ->will($this->returnValue('HTML'));
  215. $mock->expects($this->once())
  216. ->method('secureFields')
  217. ->with($data)
  218. ->will($this->returnValue(['test']));
  219. $this->Form->create();
  220. $result = $this->Form->widget('test', $data + ['secure' => true]);
  221. $this->assertSame('HTML', $result);
  222. }
  223. /**
  224. * Test that empty string is not added to secure fields list when
  225. * rendering input widget without name.
  226. */
  227. public function testRenderingWidgetWithEmptyName(): void
  228. {
  229. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  230. $this->Form->create();
  231. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  232. $this->assertSame('<select name=""></select>', $result);
  233. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  234. $this->assertEquals([], $result);
  235. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  236. $this->assertSame('<select name="0"></select>', $result);
  237. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  238. $this->assertEquals(['0'], $result);
  239. }
  240. /**
  241. * Test adding a new context class.
  242. */
  243. public function testAddContextProvider(): void
  244. {
  245. $context = 'My data';
  246. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  247. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  248. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  249. $this->assertSame($context, $data['entity']);
  250. return $stub;
  251. });
  252. $this->Form->create($context);
  253. $result = $this->Form->context();
  254. $this->assertSame($stub, $result);
  255. }
  256. /**
  257. * Test replacing a context class.
  258. */
  259. public function testAddContextProviderReplace(): void
  260. {
  261. $entity = new Article();
  262. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  263. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  264. return $stub;
  265. });
  266. $this->Form->create($entity);
  267. $result = $this->Form->context();
  268. $this->assertSame($stub, $result);
  269. }
  270. /**
  271. * Test overriding a context class.
  272. */
  273. public function testAddContextProviderAdd(): void
  274. {
  275. $entity = new Article();
  276. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  277. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  278. if ($data['entity'] instanceof Entity) {
  279. return $stub;
  280. }
  281. });
  282. $this->Form->create($entity);
  283. $result = $this->Form->context();
  284. $this->assertSame($stub, $result);
  285. }
  286. /**
  287. * Provides context options for create().
  288. *
  289. * @return array
  290. */
  291. public function contextSelectionProvider(): array
  292. {
  293. $entity = new Article();
  294. $collection = new Collection([$entity]);
  295. $emptyCollection = new Collection([]);
  296. $arrayObject = new ArrayObject([]);
  297. $data = [
  298. 'schema' => [
  299. 'title' => ['type' => 'string'],
  300. ],
  301. ];
  302. $form = new Form();
  303. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  304. return [
  305. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  306. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  307. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  308. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  309. 'form' => [$form, 'Cake\View\Form\FormContext'],
  310. 'none' => [null, 'Cake\View\Form\NullContext'],
  311. 'custom' => [$custom, get_class($custom)],
  312. ];
  313. }
  314. /**
  315. * Test default context selection in create()
  316. *
  317. * @dataProvider contextSelectionProvider
  318. * @param mixed $data
  319. */
  320. public function testCreateContextSelectionBuiltIn($data, string $class): void
  321. {
  322. $this->Form->create($data);
  323. $this->assertInstanceOf($class, $this->Form->context());
  324. }
  325. /**
  326. * Data provider for type option.
  327. *
  328. * @return array
  329. */
  330. public static function requestTypeProvider(): array
  331. {
  332. return [
  333. // type, method, override
  334. ['post', 'post', 'POST'],
  335. ['put', 'post', 'PUT'],
  336. ['patch', 'post', 'PATCH'],
  337. ['delete', 'post', 'DELETE'],
  338. ];
  339. }
  340. /**
  341. * Test creating file forms.
  342. */
  343. public function testCreateFile(): void
  344. {
  345. $encoding = strtolower(Configure::read('App.encoding'));
  346. $result = $this->Form->create(null, ['type' => 'file']);
  347. $expected = [
  348. 'form' => [
  349. 'method' => 'post', 'action' => '/articles/add',
  350. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  351. ],
  352. 'div' => ['style' => 'display:none;'],
  353. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  354. '/div',
  355. ];
  356. $this->assertHtml($expected, $result);
  357. }
  358. /**
  359. * Test creating GET forms.
  360. */
  361. public function testCreateGet(): void
  362. {
  363. $encoding = strtolower(Configure::read('App.encoding'));
  364. $result = $this->Form->create(null, ['type' => 'get']);
  365. $expected = ['form' => [
  366. 'method' => 'get', 'action' => '/articles/add',
  367. 'accept-charset' => $encoding,
  368. ]];
  369. $this->assertHtml($expected, $result);
  370. }
  371. /**
  372. * Test explicit method/enctype options.
  373. *
  374. * Explicit method overwrites inferred method from 'type'
  375. */
  376. public function testCreateExplicitMethodEnctype(): void
  377. {
  378. $encoding = strtolower(Configure::read('App.encoding'));
  379. $result = $this->Form->create(null, [
  380. 'type' => 'get',
  381. 'method' => 'put',
  382. 'enctype' => 'multipart/form-data',
  383. ]);
  384. $expected = ['form' => [
  385. 'method' => 'put',
  386. 'action' => '/articles/add',
  387. 'enctype' => 'multipart/form-data',
  388. 'accept-charset' => $encoding,
  389. ]];
  390. $this->assertHtml($expected, $result);
  391. }
  392. /**
  393. * Test create() with the templates option.
  394. */
  395. public function testCreateTemplatesArray(): void
  396. {
  397. $result = $this->Form->create($this->article, [
  398. 'templates' => [
  399. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  400. ],
  401. ]);
  402. $expected = [
  403. 'form' => [
  404. 'class' => 'form-horizontal',
  405. 'method' => 'post',
  406. 'action' => '/articles/add',
  407. 'accept-charset' => 'utf-8',
  408. ],
  409. ];
  410. $this->assertHtml($expected, $result);
  411. }
  412. /**
  413. * Test create() with the templates option.
  414. */
  415. public function testCreateTemplatesFile(): void
  416. {
  417. $result = $this->Form->create($this->article, [
  418. 'templates' => 'htmlhelper_tags',
  419. ]);
  420. $expected = [
  421. 'start form',
  422. ];
  423. $this->assertHtml($expected, $result);
  424. }
  425. /**
  426. * Test that create() and end() restore templates.
  427. */
  428. public function testCreateEndRestoreTemplates(): void
  429. {
  430. $this->Form->create($this->article, [
  431. 'templates' => ['input' => 'custom input element'],
  432. ]);
  433. $this->Form->end();
  434. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  435. }
  436. /**
  437. * Test using template vars in various templates used by control() method.
  438. */
  439. public function testControlTemplateVars(): void
  440. {
  441. $result = $this->Form->control('text', [
  442. 'templates' => [
  443. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  444. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  445. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  446. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  447. ],
  448. 'templateVars' => [
  449. 'forinput' => 'in-input',
  450. 'forlabel' => 'in-label',
  451. 'forgroup' => 'in-group',
  452. 'forcontainer' => 'in-container',
  453. ],
  454. ]);
  455. $expected = [
  456. 'div' => ['class'],
  457. 'label' => ['for'],
  458. 'Text in-label',
  459. '/label',
  460. 'in-group',
  461. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  462. 'in-container',
  463. '/div',
  464. ];
  465. $this->assertHtml($expected, $result);
  466. }
  467. /**
  468. * Test ensuring template variables work in template files loaded
  469. * during control().
  470. */
  471. public function testControlTemplatesFromFile(): void
  472. {
  473. $result = $this->Form->control('title', [
  474. 'templates' => 'test_templates',
  475. 'templateVars' => [
  476. 'forcontainer' => 'container-data',
  477. ],
  478. ]);
  479. $expected = [
  480. 'div' => ['class'],
  481. 'label' => ['for'],
  482. 'Title',
  483. '/label',
  484. 'input' => ['name', 'type' => 'text', 'id'],
  485. 'container-data',
  486. '/div',
  487. ];
  488. $this->assertHtml($expected, $result);
  489. }
  490. /**
  491. * Test using template vars in inputSubmit and submitContainer template.
  492. */
  493. public function testSubmitTemplateVars(): void
  494. {
  495. $this->Form->setTemplates([
  496. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  497. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  498. ]);
  499. $result = $this->Form->submit('Submit', [
  500. 'templateVars' => [
  501. 'forinput' => 'in-input',
  502. 'forcontainer' => 'in-container',
  503. ],
  504. ]);
  505. $expected = [
  506. 'div' => ['class'],
  507. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  508. 'in-container',
  509. '/div',
  510. ];
  511. $this->assertHtml($expected, $result);
  512. }
  513. /**
  514. * test the create() method
  515. *
  516. * @dataProvider requestTypeProvider
  517. */
  518. public function testCreateTypeOptions(string $type, string $method, string $override): void
  519. {
  520. $encoding = strtolower(Configure::read('App.encoding'));
  521. $result = $this->Form->create(null, ['type' => $type]);
  522. $expected = [
  523. 'form' => [
  524. 'method' => $method, 'action' => '/articles/add',
  525. 'accept-charset' => $encoding,
  526. ],
  527. ];
  528. $extra = [
  529. 'div' => ['style' => 'display:none;'],
  530. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  531. '/div',
  532. ];
  533. if ($type !== 'post') {
  534. $expected = array_merge($expected, $extra);
  535. }
  536. $this->assertHtml($expected, $result);
  537. }
  538. /**
  539. * Test using template vars in Create (formStart template)
  540. */
  541. public function testCreateTemplateVars(): void
  542. {
  543. $result = $this->Form->create($this->article, [
  544. 'templates' => [
  545. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  546. ],
  547. 'templateVars' => ['header' => 'headertext'],
  548. ]);
  549. $expected = [
  550. 'h4' => ['class'],
  551. 'headertext',
  552. '/h4',
  553. 'form' => [
  554. 'method' => 'post',
  555. 'action' => '/articles/add',
  556. 'accept-charset' => 'utf-8',
  557. ],
  558. ];
  559. $this->assertHtml($expected, $result);
  560. }
  561. /**
  562. * Test opening a form for an update operation.
  563. */
  564. public function testCreateUpdateForm(): void
  565. {
  566. $encoding = strtolower(Configure::read('App.encoding'));
  567. $this->View->setRequest($this->View->getRequest()
  568. ->withRequestTarget('/articles/edit/1')
  569. ->withParam('action', 'edit'));
  570. $this->article['defaults']['id'] = 1;
  571. $result = $this->Form->create($this->article);
  572. $expected = [
  573. 'form' => [
  574. 'method' => 'post',
  575. 'action' => '/articles/edit/1',
  576. 'accept-charset' => $encoding,
  577. ],
  578. 'div' => ['style' => 'display:none;'],
  579. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  580. '/div',
  581. ];
  582. $this->assertHtml($expected, $result);
  583. }
  584. /**
  585. * test create() with automatic url generation
  586. */
  587. public function testCreateAutoUrl(): void
  588. {
  589. $encoding = strtolower(Configure::read('App.encoding'));
  590. $this->View->setRequest($this->View->getRequest()
  591. ->withRequestTarget('/articles/delete/10')
  592. ->withParam('action', 'delete'));
  593. $result = $this->Form->create($this->article);
  594. $expected = [
  595. 'form' => [
  596. 'method' => 'post', 'action' => '/articles/delete/10',
  597. 'accept-charset' => $encoding,
  598. ],
  599. ];
  600. $this->assertHtml($expected, $result);
  601. $this->article['defaults'] = ['id' => 1];
  602. $this->View->setRequest($this->View->getRequest()
  603. ->withRequestTarget('/Articles/edit/1')
  604. ->withParam('action', 'delete'));
  605. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  606. $expected = [
  607. 'form' => [
  608. 'method' => 'post',
  609. 'action' => '/Articles/edit/1',
  610. 'accept-charset' => $encoding,
  611. ],
  612. 'div' => ['style' => 'display:none;'],
  613. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  614. '/div',
  615. ];
  616. $this->assertHtml($expected, $result);
  617. $this->View->setRequest($this->View->getRequest()
  618. ->withParam('action', 'add'));
  619. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  620. $expected = [
  621. 'form' => [
  622. 'method' => 'post',
  623. 'action' => '/Articles/publish/1',
  624. 'accept-charset' => $encoding,
  625. ],
  626. 'div' => ['style' => 'display:none;'],
  627. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  628. '/div',
  629. ];
  630. $this->assertHtml($expected, $result);
  631. $result = $this->Form->create($this->article, ['url' => '/Articles/publish']);
  632. $expected = [
  633. 'form' => ['method' => 'post', 'action' => '/Articles/publish', 'accept-charset' => $encoding],
  634. 'div' => ['style' => 'display:none;'],
  635. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  636. '/div',
  637. ];
  638. $this->assertHtml($expected, $result);
  639. $this->View->setRequest($this->View->getRequest()
  640. ->withParam('controller', 'Pages'));
  641. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  642. $expected = [
  643. 'form' => [
  644. 'method' => 'post', 'action' => '/Pages/signup/1',
  645. 'accept-charset' => $encoding,
  646. ],
  647. 'div' => ['style' => 'display:none;'],
  648. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  649. '/div',
  650. ];
  651. $this->assertHtml($expected, $result);
  652. }
  653. /**
  654. * Test create() with no URL (no "action" attribute for <form> tag)
  655. */
  656. public function testCreateNoUrl(): void
  657. {
  658. $result = $this->Form->create(null, ['url' => false]);
  659. $expected = [
  660. 'form' => [
  661. 'method' => 'post',
  662. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  663. ],
  664. ];
  665. $this->assertHtml($expected, $result);
  666. }
  667. /**
  668. * test create() with a custom route
  669. */
  670. public function testCreateCustomRoute(): void
  671. {
  672. $builder = Router::createRouteBuilder('/');
  673. $builder->connect('/login', ['controller' => 'Users', 'action' => 'login']);
  674. $encoding = strtolower(Configure::read('App.encoding'));
  675. $this->View->setRequest($this->View->getRequest()
  676. ->withParam('controller', 'Users'));
  677. $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
  678. $expected = [
  679. 'form' => [
  680. 'method' => 'post', 'action' => '/login',
  681. 'accept-charset' => $encoding,
  682. ],
  683. ];
  684. $this->assertHtml($expected, $result);
  685. $builder->connect(
  686. '/new-article',
  687. ['controller' => 'Articles', 'action' => 'myAction'],
  688. ['_name' => 'my-route']
  689. );
  690. $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
  691. $expected = [
  692. 'form' => [
  693. 'method' => 'post', 'action' => '/new-article',
  694. 'accept-charset' => $encoding,
  695. ],
  696. ];
  697. $this->assertHtml($expected, $result);
  698. }
  699. /**
  700. * test automatic accept-charset overriding
  701. */
  702. public function testCreateWithAcceptCharset(): void
  703. {
  704. $result = $this->Form->create(
  705. $this->article,
  706. [
  707. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  708. ]
  709. );
  710. $expected = [
  711. 'form' => [
  712. 'method' => 'post', 'action' => '/Articles',
  713. 'accept-charset' => 'iso-8859-1',
  714. ],
  715. ];
  716. $this->assertHtml($expected, $result);
  717. }
  718. /**
  719. * Test base form URL when 'url' param is passed with multiple parameters (&)
  720. */
  721. public function testCreateQueryStringRequest(): void
  722. {
  723. $encoding = strtolower(Configure::read('App.encoding'));
  724. $result = $this->Form->create($this->article, [
  725. 'type' => 'post',
  726. 'escape' => false,
  727. 'url' => [
  728. 'controller' => 'Controller',
  729. 'action' => 'action',
  730. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  731. ],
  732. ]);
  733. $expected = [
  734. 'form' => [
  735. 'method' => 'post',
  736. 'action' => '/Controller/action?param1=value1&amp;param2=value2',
  737. 'accept-charset' => $encoding,
  738. ],
  739. ];
  740. $this->assertHtml($expected, $result);
  741. $result = $this->Form->create($this->article, [
  742. 'type' => 'post',
  743. 'url' => [
  744. 'controller' => 'Controller',
  745. 'action' => 'action',
  746. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  747. ],
  748. ]);
  749. $this->assertHtml($expected, $result);
  750. }
  751. /**
  752. * test that create() doesn't cause errors by multiple id's being in the primary key
  753. * as could happen with multiple select or checkboxes.
  754. */
  755. public function testCreateWithMultipleIdInData(): void
  756. {
  757. $encoding = strtolower(Configure::read('App.encoding'));
  758. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  759. $result = $this->Form->create($this->article);
  760. $expected = [
  761. 'form' => [
  762. 'method' => 'post',
  763. 'action' => '/articles/add',
  764. 'accept-charset' => $encoding,
  765. ],
  766. ];
  767. $this->assertHtml($expected, $result);
  768. }
  769. /**
  770. * test that create() doesn't add in extra passed params.
  771. */
  772. public function testCreatePassedArgs(): void
  773. {
  774. $encoding = strtolower(Configure::read('App.encoding'));
  775. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  776. $result = $this->Form->create($this->article, [
  777. 'type' => 'post',
  778. 'escape' => false,
  779. 'url' => [
  780. 'action' => 'edit',
  781. 'myparam',
  782. ],
  783. ]);
  784. $expected = [
  785. 'form' => [
  786. 'method' => 'post',
  787. 'action' => '/Articles/edit/myparam',
  788. 'accept-charset' => $encoding,
  789. ],
  790. ];
  791. $this->assertHtml($expected, $result);
  792. }
  793. /**
  794. * test creating a get form, and get form inputs.
  795. */
  796. public function testGetFormCreate(): void
  797. {
  798. $encoding = strtolower(Configure::read('App.encoding'));
  799. $result = $this->Form->create($this->article, ['type' => 'get']);
  800. $expected = ['form' => [
  801. 'method' => 'get', 'action' => '/articles/add',
  802. 'accept-charset' => $encoding,
  803. ]];
  804. $this->assertHtml($expected, $result);
  805. $result = $this->Form->text('title');
  806. $expected = ['input' => [
  807. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  808. ]];
  809. $this->assertHtml($expected, $result);
  810. $result = $this->Form->password('password');
  811. $expected = ['input' => [
  812. 'name' => 'password', 'type' => 'password',
  813. ]];
  814. $this->assertHtml($expected, $result);
  815. $this->assertDoesNotMatchRegularExpression('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  816. $result = $this->Form->text('user_form');
  817. $expected = ['input' => [
  818. 'name' => 'user_form', 'type' => 'text',
  819. ]];
  820. $this->assertHtml($expected, $result);
  821. }
  822. /**
  823. * test get form, and inputs when the model param is false
  824. */
  825. public function testGetFormWithFalseModel(): void
  826. {
  827. $encoding = strtolower(Configure::read('App.encoding'));
  828. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'ContactTest'));
  829. $result = $this->Form->create(null, [
  830. 'type' => 'get', 'url' => ['controller' => 'ContactTest'],
  831. ]);
  832. $expected = ['form' => [
  833. 'method' => 'get', 'action' => '/ContactTest/add',
  834. 'accept-charset' => $encoding,
  835. ]];
  836. $this->assertHtml($expected, $result);
  837. $result = $this->Form->text('reason');
  838. $expected = [
  839. 'input' => ['type' => 'text', 'name' => 'reason'],
  840. ];
  841. $this->assertHtml($expected, $result);
  842. }
  843. /**
  844. * testFormCreateWithSecurity method
  845. *
  846. * Test form->create() with security key.
  847. */
  848. public function testCreateWithSecurity(): void
  849. {
  850. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  851. $encoding = strtolower(Configure::read('App.encoding'));
  852. $result = $this->Form->create($this->article, [
  853. 'url' => '/articles/publish',
  854. ]);
  855. $expected = [
  856. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  857. 'div' => ['style' => 'display:none;'],
  858. ['input' => [
  859. 'type' => 'hidden',
  860. 'name' => '_csrfToken',
  861. 'value' => 'testKey',
  862. 'autocomplete' => 'off',
  863. ]],
  864. '/div',
  865. ];
  866. $this->assertHtml($expected, $result);
  867. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  868. $expected['form']['id'] = 'MyForm';
  869. $this->assertHtml($expected, $result);
  870. }
  871. /**
  872. * testFormCreateGetNoSecurity method
  873. *
  874. * Test form->create() with no security key as its a get form
  875. */
  876. public function testCreateEndGetNoSecurity(): void
  877. {
  878. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  879. $article = new Article();
  880. $result = $this->Form->create($article, [
  881. 'type' => 'get',
  882. 'url' => '/contacts/add',
  883. ]);
  884. $this->assertStringNotContainsString('testKey', $result);
  885. $result = $this->Form->end();
  886. $this->assertStringNotContainsString('testKey', $result);
  887. }
  888. /**
  889. * Tests form hash generation with model-less data
  890. */
  891. public function testValidateHashNoModel(): void
  892. {
  893. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  894. $fields = ['anything'];
  895. $this->Form->create();
  896. $result = $this->Form->secure($fields);
  897. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  898. $this->assertStringContainsString($hash, $result);
  899. }
  900. /**
  901. * Tests that hidden fields generated for checkboxes don't get locked
  902. */
  903. public function testNoCheckboxLocking(): void
  904. {
  905. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  906. $this->Form->create();
  907. $this->assertSame([], $this->Form->getFormProtector()->__debugInfo()['fields']);
  908. $this->Form->checkbox('check', ['value' => '1']);
  909. $this->assertSame(['check'], $this->Form->getFormProtector()->__debugInfo()['fields']);
  910. }
  911. /**
  912. * testFormSecurityFields method
  913. *
  914. * Test generation of secure form hash generation.
  915. */
  916. public function testFormSecurityFields(): void
  917. {
  918. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  919. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  920. $this->Form->create();
  921. $result = $this->Form->secure($fields);
  922. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  923. $hash .= ':' . 'Model.valid';
  924. $hash = urlencode($hash);
  925. $tokenDebug = urlencode(json_encode([
  926. $this->url,
  927. $fields,
  928. [],
  929. ]));
  930. $expected = [
  931. 'div' => ['style' => 'display:none;'],
  932. ['input' => [
  933. 'type' => 'hidden',
  934. 'name' => '_Token[fields]',
  935. 'value' => $hash,
  936. 'autocomplete' => 'off',
  937. ]],
  938. ['input' => [
  939. 'type' => 'hidden',
  940. 'name' => '_Token[unlocked]',
  941. 'value' => '',
  942. 'autocomplete' => 'off',
  943. ]],
  944. ['input' => [
  945. 'type' => 'hidden',
  946. 'name' => '_Token[debug]',
  947. 'value' => $tokenDebug,
  948. 'autocomplete' => 'off',
  949. ]],
  950. '/div',
  951. ];
  952. $this->assertHtml($expected, $result);
  953. }
  954. /**
  955. * testFormSecurityFields method
  956. *
  957. * Test debug token is not generated if debug is false
  958. */
  959. public function testFormSecurityFieldsNoDebugMode(): void
  960. {
  961. Configure::write('debug', false);
  962. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  963. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  964. $this->Form->create();
  965. $result = $this->Form->secure($fields);
  966. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  967. $hash .= ':' . 'Model.valid';
  968. $hash = urlencode($hash);
  969. $expected = [
  970. 'div' => ['style' => 'display:none;'],
  971. ['input' => [
  972. 'type' => 'hidden',
  973. 'name' => '_Token[fields]',
  974. 'autocomplete' => 'off',
  975. 'value' => $hash,
  976. ]],
  977. ['input' => [
  978. 'type' => 'hidden',
  979. 'name' => '_Token[unlocked]',
  980. 'autocomplete' => 'off',
  981. 'value' => '',
  982. ]],
  983. '/div',
  984. ];
  985. $this->assertHtml($expected, $result);
  986. }
  987. /**
  988. * Tests correct generation of number fields for smallint
  989. */
  990. public function testTextFieldGenerationForSmallint(): void
  991. {
  992. $this->article['schema'] = [
  993. 'foo' => [
  994. 'type' => 'smallinteger',
  995. 'null' => false,
  996. 'default' => null,
  997. 'length' => 10,
  998. ],
  999. ];
  1000. $this->Form->create($this->article);
  1001. $result = $this->Form->control('foo');
  1002. $this->assertStringContainsString('class="input number"', $result);
  1003. $this->assertStringContainsString('type="number"', $result);
  1004. }
  1005. /**
  1006. * Tests correct generation of number fields for tinyint
  1007. */
  1008. public function testTextFieldGenerationForTinyint(): void
  1009. {
  1010. $this->article['schema'] = [
  1011. 'foo' => [
  1012. 'type' => 'tinyinteger',
  1013. 'null' => false,
  1014. 'default' => null,
  1015. 'length' => 10,
  1016. ],
  1017. ];
  1018. $this->Form->create($this->article);
  1019. $result = $this->Form->control('foo');
  1020. $this->assertStringContainsString('class="input number"', $result);
  1021. $this->assertStringContainsString('type="number"', $result);
  1022. }
  1023. /**
  1024. * Tests correct generation of number fields for double and float fields
  1025. */
  1026. public function testTextFieldGenerationForFloats(): void
  1027. {
  1028. $this->article['schema'] = [
  1029. 'foo' => [
  1030. 'type' => 'float',
  1031. 'null' => false,
  1032. 'default' => null,
  1033. 'length' => 10,
  1034. ],
  1035. ];
  1036. $this->Form->create($this->article);
  1037. $result = $this->Form->control('foo');
  1038. $expected = [
  1039. 'div' => ['class' => 'input number'],
  1040. 'label' => ['for' => 'foo'],
  1041. 'Foo',
  1042. '/label',
  1043. ['input' => [
  1044. 'type' => 'number',
  1045. 'name' => 'foo',
  1046. 'id' => 'foo',
  1047. 'step' => 'any',
  1048. ]],
  1049. '/div',
  1050. ];
  1051. $this->assertHtml($expected, $result);
  1052. $result = $this->Form->control('foo', ['step' => 0.5]);
  1053. $expected = [
  1054. 'div' => ['class' => 'input number'],
  1055. 'label' => ['for' => 'foo'],
  1056. 'Foo',
  1057. '/label',
  1058. ['input' => [
  1059. 'type' => 'number',
  1060. 'name' => 'foo',
  1061. 'id' => 'foo',
  1062. 'step' => '0.5',
  1063. ]],
  1064. '/div',
  1065. ];
  1066. $this->assertHtml($expected, $result);
  1067. }
  1068. /**
  1069. * Tests correct generation of number fields for integer fields
  1070. */
  1071. public function testTextFieldTypeNumberGenerationForIntegers(): void
  1072. {
  1073. $this->getTableLocator()->get('Contacts', [
  1074. 'className' => ContactsTable::class,
  1075. ]);
  1076. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1077. $result = $this->Form->control('age');
  1078. $expected = [
  1079. 'div' => ['class' => 'input number'],
  1080. 'label' => ['for' => 'age'],
  1081. 'Age',
  1082. '/label',
  1083. ['input' => [
  1084. 'type' => 'number', 'name' => 'age',
  1085. 'id' => 'age',
  1086. ]],
  1087. '/div',
  1088. ];
  1089. $this->assertHtml($expected, $result);
  1090. }
  1091. /**
  1092. * Tests correct generation of file upload fields for binary fields
  1093. */
  1094. public function testFileUploadFieldTypeGenerationForBinaries(): void
  1095. {
  1096. $table = $this->getTableLocator()->get('Contacts', [
  1097. 'className' => ContactsTable::class,
  1098. ]);
  1099. $table->setSchema(['foo' => [
  1100. 'type' => 'binary',
  1101. 'null' => false,
  1102. 'default' => null,
  1103. 'length' => 1024,
  1104. ]]);
  1105. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1106. $result = $this->Form->control('foo');
  1107. $expected = [
  1108. 'div' => ['class' => 'input file'],
  1109. 'label' => ['for' => 'foo'],
  1110. 'Foo',
  1111. '/label',
  1112. ['input' => [
  1113. 'type' => 'file', 'name' => 'foo',
  1114. 'id' => 'foo',
  1115. ]],
  1116. '/div',
  1117. ];
  1118. $this->assertHtml($expected, $result);
  1119. }
  1120. /**
  1121. * testFormSecurityMultipleFields method
  1122. *
  1123. * Test secure() with multiple row form. Ensure hash is correct.
  1124. */
  1125. public function testFormSecurityMultipleFields(): void
  1126. {
  1127. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1128. $this->Form->create();
  1129. $fields = [
  1130. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1131. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1132. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1133. ];
  1134. $result = $this->Form->secure($fields);
  1135. $sortedFields = [
  1136. 'Model.0.password',
  1137. 'Model.0.username',
  1138. 'Model.1.password',
  1139. 'Model.1.username',
  1140. 'Model.0.hidden' => 'value',
  1141. 'Model.0.valid' => '0',
  1142. 'Model.1.hidden' => 'value',
  1143. 'Model.1.valid' => '0',
  1144. ];
  1145. $hash = hash_hmac('sha1', $this->url . serialize($sortedFields) . session_id(), Security::getSalt());
  1146. $hash .= ':Model.0.hidden|Model.0.valid|Model.1.hidden|Model.1.valid';
  1147. $hash = urlencode($hash);
  1148. $tokenDebug = urlencode(json_encode([
  1149. $this->url,
  1150. $fields,
  1151. [],
  1152. ]));
  1153. $expected = [
  1154. 'div' => ['style' => 'display:none;'],
  1155. ['input' => [
  1156. 'type' => 'hidden',
  1157. 'name' => '_Token[fields]',
  1158. 'value' => $hash,
  1159. 'autocomplete' => 'off',
  1160. ]],
  1161. ['input' => [
  1162. 'type' => 'hidden',
  1163. 'name' => '_Token[unlocked]',
  1164. 'autocomplete' => 'off',
  1165. 'value' => '',
  1166. ]],
  1167. ['input' => [
  1168. 'type' => 'hidden',
  1169. 'name' => '_Token[debug]',
  1170. 'value' => $tokenDebug,
  1171. 'autocomplete' => 'off',
  1172. ]],
  1173. '/div',
  1174. ];
  1175. $this->assertHtml($expected, $result);
  1176. }
  1177. /**
  1178. * testFormSecurityMultipleSubmitButtons
  1179. *
  1180. * test form submit generation and ensure that _Token is only created on end()
  1181. */
  1182. public function testFormSecurityMultipleSubmitButtons(): void
  1183. {
  1184. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1185. $this->Form->create($this->article);
  1186. $this->Form->text('Address.title');
  1187. $this->Form->text('Address.first_name');
  1188. $result = $this->Form->submit('Save', ['name' => 'save']);
  1189. $expected = [
  1190. 'div' => ['class' => 'submit'],
  1191. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1192. '/div',
  1193. ];
  1194. $this->assertHtml($expected, $result);
  1195. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1196. $expected = [
  1197. 'div' => ['class' => 'submit'],
  1198. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1199. '/div',
  1200. ];
  1201. $this->assertHtml($expected, $result);
  1202. $result = $this->Form->end();
  1203. $tokenDebug = urlencode(json_encode([
  1204. '/articles/add',
  1205. [
  1206. 'Address.title',
  1207. 'Address.first_name',
  1208. ],
  1209. ['save', 'cancel'],
  1210. ]));
  1211. $expected = [
  1212. 'div' => ['style' => 'display:none;'],
  1213. ['input' => [
  1214. 'type' => 'hidden',
  1215. 'name' => '_Token[fields]',
  1216. 'autocomplete',
  1217. 'value',
  1218. ]],
  1219. ['input' => [
  1220. 'type' => 'hidden',
  1221. 'name' => '_Token[unlocked]',
  1222. 'value' => 'cancel%7Csave',
  1223. 'autocomplete' => 'off',
  1224. ]],
  1225. ['input' => [
  1226. 'type' => 'hidden',
  1227. 'name' => '_Token[debug]',
  1228. 'value' => $tokenDebug,
  1229. 'autocomplete' => 'off',
  1230. ]],
  1231. '/div',
  1232. ];
  1233. $this->assertHtml($expected, $result);
  1234. }
  1235. /**
  1236. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1237. */
  1238. public function testSecurityButtonNestedNamed(): void
  1239. {
  1240. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1241. $this->Form->create();
  1242. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1243. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1244. $this->assertEquals(['Address.button'], $result);
  1245. }
  1246. /**
  1247. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1248. */
  1249. public function testSecuritySubmitNestedNamed(): void
  1250. {
  1251. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1252. $this->Form->create($this->article);
  1253. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1254. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1255. $this->assertEquals(['Address.button'], $result);
  1256. }
  1257. /**
  1258. * Test that the correct fields are unlocked for image submits with no names.
  1259. */
  1260. public function testSecuritySubmitImageNoName(): void
  1261. {
  1262. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1263. $this->Form->create();
  1264. $result = $this->Form->submit('save.png');
  1265. $expected = [
  1266. 'div' => ['class' => 'submit'],
  1267. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1268. '/div',
  1269. ];
  1270. $this->assertHtml($expected, $result);
  1271. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1272. $this->assertEquals(['x', 'y'], $result);
  1273. }
  1274. /**
  1275. * Test that the correct fields are unlocked for image submits with names.
  1276. */
  1277. public function testSecuritySubmitImageName(): void
  1278. {
  1279. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1280. $this->Form->create();
  1281. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1282. $expected = [
  1283. 'div' => ['class' => 'submit'],
  1284. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1285. '/div',
  1286. ];
  1287. $this->assertHtml($expected, $result);
  1288. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1289. $this->assertEquals(['test', 'test_x', 'test_y'], $result);
  1290. }
  1291. /**
  1292. * testFormSecurityMultipleControlFields method
  1293. *
  1294. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1295. */
  1296. public function testFormSecurityMultipleControlFields(): void
  1297. {
  1298. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1299. $this->Form->create();
  1300. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1301. $this->Form->control('Addresses.0.title');
  1302. $this->Form->control('Addresses.0.first_name');
  1303. $this->Form->control('Addresses.0.last_name');
  1304. $this->Form->control('Addresses.0.address');
  1305. $this->Form->control('Addresses.0.city');
  1306. $this->Form->control('Addresses.0.phone');
  1307. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1308. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1309. $this->Form->control('Addresses.1.title');
  1310. $this->Form->control('Addresses.1.first_name');
  1311. $this->Form->control('Addresses.1.last_name');
  1312. $this->Form->control('Addresses.1.address');
  1313. $this->Form->control('Addresses.1.city');
  1314. $this->Form->control('Addresses.1.phone');
  1315. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1316. $result = $this->Form->secure();
  1317. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1318. $tokenDebug = urlencode(json_encode([
  1319. '/articles/add',
  1320. [
  1321. 'Addresses.0.id' => '123456',
  1322. 'Addresses.0.title',
  1323. 'Addresses.0.first_name',
  1324. 'Addresses.0.last_name',
  1325. 'Addresses.0.address',
  1326. 'Addresses.0.city',
  1327. 'Addresses.0.phone',
  1328. 'Addresses.0.primary',
  1329. 'Addresses.1.id' => '654321',
  1330. 'Addresses.1.title',
  1331. 'Addresses.1.first_name',
  1332. 'Addresses.1.last_name',
  1333. 'Addresses.1.address',
  1334. 'Addresses.1.city',
  1335. 'Addresses.1.phone',
  1336. 'Addresses.1.primary',
  1337. ],
  1338. [],
  1339. ]));
  1340. $expected = [
  1341. 'div' => ['style' => 'display:none;'],
  1342. ['input' => [
  1343. 'type' => 'hidden',
  1344. 'name' => '_Token[fields]',
  1345. 'value' => $hash,
  1346. 'autocomplete' => 'off',
  1347. ]],
  1348. ['input' => [
  1349. 'type' => 'hidden',
  1350. 'name' => '_Token[unlocked]',
  1351. 'autocomplete' => 'off',
  1352. 'value' => '',
  1353. ]],
  1354. ['input' => [
  1355. 'type' => 'hidden',
  1356. 'name' => '_Token[debug]',
  1357. 'value' => $tokenDebug,
  1358. 'autocomplete' => 'off',
  1359. ]],
  1360. '/div',
  1361. ];
  1362. $this->assertHtml($expected, $result);
  1363. }
  1364. /**
  1365. * testFormSecurityArrayFields method
  1366. *
  1367. * Test form security with Model.field.0 style inputs.
  1368. */
  1369. public function testFormSecurityArrayFields(): void
  1370. {
  1371. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1372. $this->Form->create();
  1373. $this->Form->text('Address.primary.1');
  1374. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1375. $this->assertSame('Address.primary', $result[0]);
  1376. $this->Form->text('Address.secondary.1.0');
  1377. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1378. $this->assertSame('Address.secondary', $result[1]);
  1379. }
  1380. /**
  1381. * testFormSecurityMultipleControlDisabledFields method
  1382. *
  1383. * Test secure form generation with multiple records and disabled fields.
  1384. */
  1385. public function testFormSecurityMultipleControlDisabledFields(): void
  1386. {
  1387. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1388. 'unlockedFields' => ['first_name', 'address'],
  1389. ]));
  1390. $this->Form->create();
  1391. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1392. $this->Form->text('Addresses.0.title');
  1393. $this->Form->text('Addresses.0.first_name');
  1394. $this->Form->text('Addresses.0.last_name');
  1395. $this->Form->text('Addresses.0.address');
  1396. $this->Form->text('Addresses.0.city');
  1397. $this->Form->text('Addresses.0.phone');
  1398. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1399. $this->Form->text('Addresses.1.title');
  1400. $this->Form->text('Addresses.1.first_name');
  1401. $this->Form->text('Addresses.1.last_name');
  1402. $this->Form->text('Addresses.1.address');
  1403. $this->Form->text('Addresses.1.city');
  1404. $this->Form->text('Addresses.1.phone');
  1405. $result = $this->Form->secure();
  1406. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1407. $tokenDebug = urlencode(json_encode([
  1408. '/articles/add',
  1409. [
  1410. 'Addresses.0.id' => '123456',
  1411. 'Addresses.0.title',
  1412. 'Addresses.0.last_name',
  1413. 'Addresses.0.city',
  1414. 'Addresses.0.phone',
  1415. 'Addresses.1.id' => '654321',
  1416. 'Addresses.1.title',
  1417. 'Addresses.1.last_name',
  1418. 'Addresses.1.city',
  1419. 'Addresses.1.phone',
  1420. ],
  1421. [
  1422. 'first_name',
  1423. 'address',
  1424. ],
  1425. ]));
  1426. $expected = [
  1427. 'div' => ['style' => 'display:none;'],
  1428. ['input' => [
  1429. 'type' => 'hidden',
  1430. 'name' => '_Token[fields]',
  1431. 'autocomplete' => 'off',
  1432. 'value' => $hash,
  1433. ]],
  1434. ['input' => [
  1435. 'type' => 'hidden',
  1436. 'name' => '_Token[unlocked]',
  1437. 'autocomplete' => 'off',
  1438. 'value' => 'address%7Cfirst_name',
  1439. ]],
  1440. ['input' => [
  1441. 'type' => 'hidden',
  1442. 'name' => '_Token[debug]',
  1443. 'autocomplete' => 'off',
  1444. 'value' => $tokenDebug,
  1445. ]],
  1446. '/div',
  1447. ];
  1448. $this->assertHtml($expected, $result);
  1449. }
  1450. /**
  1451. * testFormSecurityControlDisabledFields method
  1452. *
  1453. * Test single record form with disabled fields.
  1454. */
  1455. public function testFormSecurityControlUnlockedFields(): void
  1456. {
  1457. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1458. 'unlockedFields' => ['first_name', 'address'],
  1459. ]));
  1460. $this->Form->create();
  1461. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1462. $this->assertEquals(
  1463. $this->View->getRequest()->getAttribute('formTokenData'),
  1464. ['unlockedFields' => $result]
  1465. );
  1466. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1467. $this->Form->text('Addresses.title');
  1468. $this->Form->text('Addresses.first_name');
  1469. $this->Form->text('Addresses.last_name');
  1470. $this->Form->text('Addresses.address');
  1471. $this->Form->text('Addresses.city');
  1472. $this->Form->text('Addresses.phone');
  1473. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1474. $expected = [
  1475. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1476. 'Addresses.city', 'Addresses.phone',
  1477. ];
  1478. $this->assertEquals($expected, $result);
  1479. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1480. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1481. $tokenDebug = urlencode(json_encode([
  1482. '/articles/add',
  1483. [
  1484. 'Addresses.id' => '123456',
  1485. 'Addresses.title',
  1486. 'Addresses.last_name',
  1487. 'Addresses.city',
  1488. 'Addresses.phone',
  1489. ],
  1490. [
  1491. 'first_name',
  1492. 'address',
  1493. ],
  1494. ]));
  1495. $expected = [
  1496. 'div' => ['style' => 'display:none;'],
  1497. ['input' => [
  1498. 'type' => 'hidden',
  1499. 'name' => '_Token[fields]',
  1500. 'value' => $hash,
  1501. 'autocomplete' => 'off',
  1502. 'data-foo' => 'bar',
  1503. ]],
  1504. ['input' => [
  1505. 'type' => 'hidden',
  1506. 'name' => '_Token[unlocked]',
  1507. 'value' => 'address%7Cfirst_name',
  1508. 'autocomplete' => 'off',
  1509. 'data-foo' => 'bar',
  1510. ]],
  1511. ['input' => [
  1512. 'type' => 'hidden', 'name' => '_Token[debug]',
  1513. 'value' => $tokenDebug,
  1514. 'autocomplete' => 'off',
  1515. 'data-foo' => 'bar',
  1516. ]],
  1517. '/div',
  1518. ];
  1519. $this->assertHtml($expected, $result);
  1520. }
  1521. /**
  1522. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1523. *
  1524. * Test single record form with debugSecurity param.
  1525. */
  1526. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue(): void
  1527. {
  1528. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1529. 'unlockedFields' => ['first_name', 'address'],
  1530. ]));
  1531. $this->Form->create();
  1532. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1533. $this->assertEquals(
  1534. $this->View->getRequest()->getAttribute('formTokenData'),
  1535. ['unlockedFields' => $result]
  1536. );
  1537. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1538. $this->Form->text('Addresses.title');
  1539. $this->Form->text('Addresses.first_name');
  1540. $this->Form->text('Addresses.last_name');
  1541. $this->Form->text('Addresses.address');
  1542. $this->Form->text('Addresses.city');
  1543. $this->Form->text('Addresses.phone');
  1544. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1545. $expected = [
  1546. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1547. 'Addresses.city', 'Addresses.phone',
  1548. ];
  1549. $this->assertEquals($expected, $result);
  1550. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1551. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1552. $tokenDebug = urlencode(json_encode([
  1553. '/articles/add',
  1554. [
  1555. 'Addresses.id' => '123456',
  1556. 'Addresses.title',
  1557. 'Addresses.last_name',
  1558. 'Addresses.city',
  1559. 'Addresses.phone',
  1560. ],
  1561. [
  1562. 'first_name',
  1563. 'address',
  1564. ],
  1565. ]));
  1566. $expected = [
  1567. 'div' => ['style' => 'display:none;'],
  1568. ['input' => [
  1569. 'type' => 'hidden',
  1570. 'name' => '_Token[fields]',
  1571. 'value' => $hash,
  1572. 'autocomplete' => 'off',
  1573. 'data-foo' => 'bar',
  1574. ]],
  1575. ['input' => [
  1576. 'type' => 'hidden',
  1577. 'name' => '_Token[unlocked]',
  1578. 'value' => 'address%7Cfirst_name',
  1579. 'autocomplete' => 'off',
  1580. 'data-foo' => 'bar',
  1581. ]],
  1582. ['input' => [
  1583. 'type' => 'hidden', 'name' => '_Token[debug]',
  1584. 'value' => $tokenDebug,
  1585. 'autocomplete' => 'off',
  1586. 'data-foo' => 'bar',
  1587. ]],
  1588. '/div',
  1589. ];
  1590. $this->assertHtml($expected, $result);
  1591. }
  1592. /**
  1593. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1594. *
  1595. * Debug is false, debugSecurity is true -> no debug
  1596. */
  1597. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse(): void
  1598. {
  1599. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1600. 'unlockedFields' => ['first_name', 'address'],
  1601. ]));
  1602. $this->Form->create();
  1603. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1604. $this->assertEquals(
  1605. $this->View->getRequest()->getAttribute('formTokenData'),
  1606. ['unlockedFields' => $result]
  1607. );
  1608. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1609. $this->Form->text('Addresses.title');
  1610. $this->Form->text('Addresses.first_name');
  1611. $this->Form->text('Addresses.last_name');
  1612. $this->Form->text('Addresses.address');
  1613. $this->Form->text('Addresses.city');
  1614. $this->Form->text('Addresses.phone');
  1615. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1616. $expected = [
  1617. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1618. 'Addresses.city', 'Addresses.phone',
  1619. ];
  1620. $this->assertEquals($expected, $result);
  1621. Configure::write('debug', false);
  1622. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1623. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1624. $expected = [
  1625. 'div' => ['style' => 'display:none;'],
  1626. ['input' => [
  1627. 'type' => 'hidden',
  1628. 'name' => '_Token[fields]',
  1629. 'value' => $hash,
  1630. 'autocomplete' => 'off',
  1631. 'data-foo' => 'bar',
  1632. ]],
  1633. ['input' => [
  1634. 'type' => 'hidden',
  1635. 'name' => '_Token[unlocked]',
  1636. 'value' => 'address%7Cfirst_name',
  1637. 'autocomplete' => 'off',
  1638. 'data-foo' => 'bar',
  1639. ]],
  1640. '/div',
  1641. ];
  1642. $this->assertHtml($expected, $result);
  1643. }
  1644. /**
  1645. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1646. *
  1647. * Test single record form with debugSecurity param.
  1648. */
  1649. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse(): void
  1650. {
  1651. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1652. 'unlockedFields' => ['first_name', 'address'],
  1653. ]));
  1654. $this->Form->create();
  1655. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1656. $this->assertEquals(
  1657. $this->View->getRequest()->getAttribute('formTokenData'),
  1658. ['unlockedFields' => $result]
  1659. );
  1660. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1661. $this->Form->text('Addresses.title');
  1662. $this->Form->text('Addresses.first_name');
  1663. $this->Form->text('Addresses.last_name');
  1664. $this->Form->text('Addresses.address');
  1665. $this->Form->text('Addresses.city');
  1666. $this->Form->text('Addresses.phone');
  1667. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1668. $expected = [
  1669. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1670. 'Addresses.city', 'Addresses.phone',
  1671. ];
  1672. $this->assertEquals($expected, $result);
  1673. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1674. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1675. $expected = [
  1676. 'div' => ['style' => 'display:none;'],
  1677. ['input' => [
  1678. 'type' => 'hidden',
  1679. 'name' => '_Token[fields]',
  1680. 'value' => $hash,
  1681. 'autocomplete' => 'off',
  1682. 'data-foo' => 'bar',
  1683. ]],
  1684. ['input' => [
  1685. 'type' => 'hidden',
  1686. 'name' => '_Token[unlocked]',
  1687. 'value' => 'address%7Cfirst_name',
  1688. 'autocomplete' => 'off',
  1689. 'data-foo' => 'bar',
  1690. ]],
  1691. '/div',
  1692. ];
  1693. $this->assertHtml($expected, $result);
  1694. }
  1695. /**
  1696. * testFormSecureWithCustomNameAttribute method
  1697. *
  1698. * Test securing inputs with custom name attributes.
  1699. */
  1700. public function testFormSecureWithCustomNameAttribute(): void
  1701. {
  1702. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1703. $this->Form->create();
  1704. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1705. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1706. $this->assertSame('User.custom', $result[0]);
  1707. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1708. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1709. $this->assertSame('User.custom.another.value', $result[1]);
  1710. }
  1711. /**
  1712. * testFormSecuredControl method
  1713. *
  1714. * Test generation of entire secure form, assertions made on control() output.
  1715. */
  1716. public function testFormSecuredControl(): void
  1717. {
  1718. $this->View->setRequest($this->View->getRequest()
  1719. ->withAttribute('formTokenData', [])
  1720. ->withAttribute('csrfToken', 'testKey'));
  1721. $this->article['schema'] = [
  1722. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1723. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1724. ];
  1725. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1726. $encoding = strtolower(Configure::read('App.encoding'));
  1727. $expected = [
  1728. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1729. 'div' => ['style' => 'display:none;'],
  1730. ['input' => [
  1731. 'type' => 'hidden',
  1732. 'name' => '_csrfToken',
  1733. 'value' => 'testKey',
  1734. 'autocomplete' => 'off',
  1735. ]],
  1736. '/div',
  1737. ];
  1738. $this->assertHtml($expected, $result);
  1739. $result = $this->Form->control('ratio');
  1740. $expected = [
  1741. 'div' => ['class'],
  1742. 'label' => ['for'],
  1743. 'Ratio',
  1744. '/label',
  1745. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1746. '/div',
  1747. ];
  1748. $this->assertHtml($expected, $result);
  1749. $result = $this->Form->control('population');
  1750. $expected = [
  1751. 'div' => ['class'],
  1752. 'label' => ['for'],
  1753. 'Population',
  1754. '/label',
  1755. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1756. '/div',
  1757. ];
  1758. $this->assertHtml($expected, $result);
  1759. $result = $this->Form->control('published', ['type' => 'text']);
  1760. $expected = [
  1761. 'div' => ['class' => 'input text'],
  1762. 'label' => ['for' => 'published'],
  1763. 'Published',
  1764. '/label',
  1765. ['input' => [
  1766. 'type' => 'text',
  1767. 'name' => 'published',
  1768. 'id' => 'published',
  1769. ]],
  1770. '/div',
  1771. ];
  1772. $this->assertHtml($expected, $result);
  1773. $result = $this->Form->control('other', ['type' => 'text']);
  1774. $expected = [
  1775. 'div' => ['class' => 'input text'],
  1776. 'label' => ['for' => 'other'],
  1777. 'Other',
  1778. '/label',
  1779. ['input' => [
  1780. 'type' => 'text',
  1781. 'name' => 'other',
  1782. 'id',
  1783. ]],
  1784. '/div',
  1785. ];
  1786. $this->assertHtml($expected, $result);
  1787. $result = $this->Form->hidden('stuff');
  1788. $expected = [
  1789. 'input' => [
  1790. 'type' => 'hidden',
  1791. 'name' => 'stuff',
  1792. ],
  1793. ];
  1794. $this->assertHtml($expected, $result);
  1795. $result = $this->Form->hidden('hidden', ['value' => false]);
  1796. $expected = ['input' => [
  1797. 'type' => 'hidden',
  1798. 'name' => 'hidden',
  1799. 'value' => '0',
  1800. ]];
  1801. $this->assertHtml($expected, $result);
  1802. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1803. $expected = [
  1804. 'div' => ['class' => 'input checkbox'],
  1805. ['input' => [
  1806. 'type' => 'hidden',
  1807. 'name' => 'something',
  1808. 'value' => '0',
  1809. ]],
  1810. 'label' => ['for' => 'something'],
  1811. ['input' => [
  1812. 'type' => 'checkbox',
  1813. 'name' => 'something',
  1814. 'value' => '1',
  1815. 'id' => 'something',
  1816. ]],
  1817. 'Something',
  1818. '/label',
  1819. '/div',
  1820. ];
  1821. $this->assertHtml($expected, $result);
  1822. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1823. $expectedFields = [
  1824. 'ratio',
  1825. 'population',
  1826. 'published',
  1827. 'other',
  1828. 'stuff' => '',
  1829. 'hidden' => '0',
  1830. 'something',
  1831. ];
  1832. $this->assertEquals($expectedFields, $result);
  1833. $result = $this->Form->secure();
  1834. $tokenDebug = urlencode(json_encode([
  1835. '/articles/add',
  1836. $expectedFields,
  1837. [],
  1838. ]));
  1839. $expected = [
  1840. 'div' => ['style' => 'display:none;'],
  1841. ['input' => [
  1842. 'type' => 'hidden',
  1843. 'name' => '_Token[fields]',
  1844. 'value',
  1845. 'autocomplete',
  1846. ]],
  1847. ['input' => [
  1848. 'type' => 'hidden',
  1849. 'name' => '_Token[unlocked]',
  1850. 'value' => '',
  1851. 'autocomplete' => 'off',
  1852. ]],
  1853. ['input' => [
  1854. 'type' => 'hidden', 'name' => '_Token[debug]',
  1855. 'value' => $tokenDebug,
  1856. 'autocomplete' => 'off',
  1857. ]],
  1858. '/div',
  1859. ];
  1860. $this->assertHtml($expected, $result);
  1861. $data = [
  1862. 'ratio' => '',
  1863. 'population' => '',
  1864. 'published' => '',
  1865. 'other' => '',
  1866. 'stuff' => '',
  1867. 'hidden' => '0',
  1868. 'something' => '',
  1869. '_Token' => $this->Form->getFormProtector()->buildTokenData(),
  1870. ];
  1871. $this->assertTrue($this->Form->getFormProtector()->validate($data, '', ''));
  1872. }
  1873. /**
  1874. * testSecuredControlCustomName method
  1875. *
  1876. * Test secured inputs with custom names.
  1877. */
  1878. public function testSecuredControlCustomName(): void
  1879. {
  1880. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1881. $this->Form->create();
  1882. $this->Form->text('text_input', [
  1883. 'name' => 'Option[General.default_role]',
  1884. ]);
  1885. $expected = ['Option.General.default_role'];
  1886. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1887. $this->assertEquals($expected, $result);
  1888. $this->Form->select('select_box', [1, 2], [
  1889. 'name' => 'Option[General.select_role]',
  1890. ]);
  1891. $expected[] = 'Option.General.select_role';
  1892. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1893. $this->assertEquals($expected, $result);
  1894. $this->Form->text('other.things[]');
  1895. $expected[] = 'other.things';
  1896. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1897. $this->assertEquals($expected, $result);
  1898. }
  1899. /**
  1900. * testSecuredControlDuplicate method
  1901. *
  1902. * Test that a hidden field followed by a visible field
  1903. * undoes the hidden field locking.
  1904. */
  1905. public function testSecuredControlDuplicate(): void
  1906. {
  1907. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1908. $this->Form->create();
  1909. $this->Form->control('text_val', [
  1910. 'type' => 'hidden',
  1911. 'value' => 'some text',
  1912. ]);
  1913. $expected = ['text_val' => 'some text'];
  1914. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1915. $this->assertEquals($expected, $result);
  1916. $this->Form->control('text_val', [
  1917. 'type' => 'text',
  1918. ]);
  1919. $expected = ['text_val'];
  1920. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1921. $this->assertEquals($expected, $result);
  1922. }
  1923. /**
  1924. * testFormSecuredFileControl method
  1925. *
  1926. * Tests that the correct keys are added to the field hash index.
  1927. */
  1928. public function testFormSecuredFileControl(): void
  1929. {
  1930. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1931. $this->Form->create();
  1932. $this->Form->file('Attachment.file');
  1933. $expected = ['Attachment.file'];
  1934. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1935. $this->assertEquals($expected, $result);
  1936. }
  1937. /**
  1938. * testFormSecuredMultipleSelect method
  1939. *
  1940. * Test that multiple selects keys are added to field hash.
  1941. */
  1942. public function testFormSecuredMultipleSelect(): void
  1943. {
  1944. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1945. $this->Form->create();
  1946. $options = ['1' => 'one', '2' => 'two'];
  1947. $this->Form->select('Model.select', $options);
  1948. $expected = ['Model.select'];
  1949. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1950. $this->assertEquals($expected, $result);
  1951. $this->Form->select('Model.select', $options, ['multiple' => true]);
  1952. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1953. $this->assertEquals($expected, $result);
  1954. }
  1955. /**
  1956. * testFormSecuredRadio method
  1957. */
  1958. public function testFormSecuredRadio(): void
  1959. {
  1960. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1961. $this->Form->create();
  1962. $options = ['1' => 'option1', '2' => 'option2'];
  1963. $this->Form->radio('Test.test', $options);
  1964. $expected = ['Test.test'];
  1965. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1966. $this->assertEquals($expected, $result);
  1967. $this->Form->radio('Test.all', $options, [
  1968. 'disabled' => ['option1', 'option2'],
  1969. ]);
  1970. $expected = ['Test.test', 'Test.all' => ''];
  1971. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1972. $this->assertEquals($expected, $result);
  1973. $this->Form->radio('Test.some', $options, [
  1974. 'disabled' => ['option1'],
  1975. ]);
  1976. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  1977. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1978. $this->assertEquals($expected, $result);
  1979. }
  1980. /**
  1981. * testFormSecuredAndDisabled method
  1982. *
  1983. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  1984. * hashing.
  1985. */
  1986. public function testFormSecuredAndDisabled(): void
  1987. {
  1988. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1989. $this->Form->create();
  1990. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  1991. $this->Form->text('Model.text', ['disabled' => true]);
  1992. $this->Form->password('Model.text', ['disabled' => true]);
  1993. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  1994. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  1995. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  1996. $this->Form->year('Model.year', ['disabled' => true]);
  1997. $this->Form->month('Model.month', ['disabled' => true]);
  1998. $this->Form->day('Model.day', ['disabled' => true]);
  1999. $this->Form->hour('Model.hour', ['disabled' => true]);
  2000. $this->Form->minute('Model.minute', ['disabled' => true]);
  2001. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2002. $expected = [
  2003. 'Model.radio' => '',
  2004. ];
  2005. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2006. $this->assertEquals($expected, $result);
  2007. }
  2008. /**
  2009. * testUnlockFieldAddsToList method
  2010. *
  2011. * Test disableField.
  2012. */
  2013. public function testUnlockFieldAddsToList(): void
  2014. {
  2015. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2016. 'unlockedFields' => [],
  2017. ]));
  2018. $this->Form->create();
  2019. $this->Form->unlockField('Contact.name');
  2020. $this->Form->text('Contact.name');
  2021. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2022. $this->assertEquals([], $result);
  2023. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  2024. $this->assertEquals(['Contact.name'], $result);
  2025. }
  2026. /**
  2027. * testUnlockFieldRemovingFromFields method
  2028. *
  2029. * Test unlockField removing from fields array.
  2030. */
  2031. public function testUnlockFieldRemovingFromFields(): void
  2032. {
  2033. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2034. 'unlockedFields' => [],
  2035. ]));
  2036. $this->Form->create($this->article);
  2037. $this->Form->hidden('Article.id', ['value' => 1]);
  2038. $this->Form->text('Article.title');
  2039. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2040. $this->assertSame('1', $result['Article.id'], 'Hidden input should be secured.');
  2041. $this->assertContains('Article.title', $result, 'Field should be secured.');
  2042. $this->Form->unlockField('Article.title');
  2043. $this->Form->unlockField('Article.id');
  2044. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2045. $this->assertEquals([], $result);
  2046. }
  2047. /**
  2048. * testResetUnlockFields method
  2049. *
  2050. * Test reset unlockFields, when create new form.
  2051. */
  2052. public function testResetUnlockFields(): void
  2053. {
  2054. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2055. 'key' => 'testKey',
  2056. 'unlockedFields' => [],
  2057. ]));
  2058. $this->Form->create();
  2059. $this->Form->unlockField('Contact.id');
  2060. $this->Form->hidden('Contact.id', ['value' => 1]);
  2061. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2062. $this->assertEmpty($result, 'Field should be unlocked');
  2063. $this->Form->end();
  2064. $this->Form->create();
  2065. $this->Form->hidden('Contact.id', ['value' => 1]);
  2066. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2067. $this->assertSame('1', $result['Contact.id'], 'Hidden input should be secured.');
  2068. }
  2069. /**
  2070. * testSecuredFormUrlIgnoresHost method
  2071. *
  2072. * Test that only the path + query elements of a form's URL show up in their hash.
  2073. */
  2074. public function testSecuredFormUrlIgnoresHost(): void
  2075. {
  2076. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'testKey']));
  2077. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2078. $this->Form->create($this->article, [
  2079. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2080. ]);
  2081. $result = $this->Form->secure();
  2082. $this->assertStringContainsString($expected, $result);
  2083. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2084. $result = $this->Form->secure();
  2085. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2086. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2087. $result = $this->Form->secure();
  2088. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2089. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2090. $result = $this->Form->secure();
  2091. $this->assertStringNotContainsString($expected, $result, 'URL is different');
  2092. }
  2093. /**
  2094. * testSecuredFormUrlHasHtmlAndIdentifier method
  2095. *
  2096. * Test that URL, HTML and identifier show up in their hashes.
  2097. */
  2098. public function testSecuredFormUrlHasHtmlAndIdentifier(): void
  2099. {
  2100. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2101. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2102. $this->Form->create($this->article, [
  2103. 'url' => [
  2104. 'controller' => 'articles',
  2105. 'action' => 'view',
  2106. '?' => [
  2107. 'page' => 1,
  2108. 'limit' => 10,
  2109. 'html' => '<>"',
  2110. ],
  2111. '#' => 'result',
  2112. ],
  2113. ]);
  2114. $result = $this->Form->secure();
  2115. $this->assertStringContainsString($expected, $result);
  2116. $this->Form->create($this->article, [
  2117. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2118. ]);
  2119. $result = $this->Form->secure();
  2120. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2121. $this->Form->create($this->article, [
  2122. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2123. ]);
  2124. $result = $this->Form->secure();
  2125. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2126. }
  2127. /**
  2128. * testErrorMessageDisplay method
  2129. *
  2130. * Test error message display.
  2131. */
  2132. public function testErrorMessageDisplay(): void
  2133. {
  2134. $this->article['errors'] = [
  2135. 'Article' => [
  2136. 'title' => 'error message',
  2137. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2138. ],
  2139. ];
  2140. $this->Form->create($this->article);
  2141. $result = $this->Form->control('Article.title');
  2142. $expected = [
  2143. 'div' => ['class' => 'input text error'],
  2144. 'label' => ['for' => 'article-title'],
  2145. 'Title',
  2146. '/label',
  2147. 'input' => [
  2148. 'type' => 'text',
  2149. 'name' => 'Article[title]',
  2150. 'id' => 'article-title',
  2151. 'class' => 'form-error',
  2152. 'aria-invalid' => 'true',
  2153. 'aria-describedby' => 'article-title-error',
  2154. ],
  2155. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2156. 'error message',
  2157. '/div',
  2158. '/div',
  2159. ];
  2160. $this->assertHtml($expected, $result);
  2161. $result = $this->Form->control('Article.title', [
  2162. 'templates' => [
  2163. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2164. ],
  2165. ]);
  2166. $expected = [
  2167. 'div' => ['class' => 'input text error'],
  2168. 'label' => ['for' => 'article-title'],
  2169. 'Title',
  2170. '/label',
  2171. 'input' => [
  2172. 'type' => 'text',
  2173. 'name' => 'Article[title]',
  2174. 'id' => 'article-title',
  2175. 'class' => 'form-error',
  2176. // No aria-describedby because error template is custom
  2177. 'aria-invalid' => 'true',
  2178. ],
  2179. '/div',
  2180. ];
  2181. $this->assertHtml($expected, $result);
  2182. $result = $this->Form->control('Article.content');
  2183. $expected = [
  2184. 'div' => ['class' => 'input text error'],
  2185. 'label' => ['for' => 'article-content'],
  2186. 'Content',
  2187. '/label',
  2188. 'input' => [
  2189. 'type' => 'text',
  2190. 'name' => 'Article[content]',
  2191. 'id' => 'article-content',
  2192. 'class' => 'form-error',
  2193. 'aria-invalid' => 'true',
  2194. 'aria-describedby' => 'article-content-error',
  2195. ],
  2196. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2197. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2198. '/div',
  2199. '/div',
  2200. ];
  2201. $this->assertHtml($expected, $result);
  2202. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2203. $expected = [
  2204. 'div' => ['class' => 'input text error'],
  2205. 'label' => ['for' => 'article-content'],
  2206. 'Content',
  2207. '/label',
  2208. 'input' => [
  2209. 'type' => 'text',
  2210. 'name' => 'Article[content]',
  2211. 'id' => 'article-content',
  2212. 'class' => 'form-error',
  2213. 'aria-invalid' => 'true',
  2214. 'aria-describedby' => 'article-content-error',
  2215. ],
  2216. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2217. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2218. '/div',
  2219. '/div',
  2220. ];
  2221. $this->assertHtml($expected, $result);
  2222. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2223. $expected = [
  2224. 'div' => ['class' => 'input text error'],
  2225. 'label' => ['for' => 'article-content'],
  2226. 'Content',
  2227. '/label',
  2228. 'input' => [
  2229. 'type' => 'text',
  2230. 'name' => 'Article[content]',
  2231. 'id' => 'article-content',
  2232. 'class' => 'form-error',
  2233. 'aria-invalid' => 'true',
  2234. 'aria-describedby' => 'article-content-error',
  2235. ],
  2236. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2237. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2238. '/div',
  2239. '/div',
  2240. ];
  2241. $this->assertHtml($expected, $result);
  2242. }
  2243. /**
  2244. * testEmptyErrorValidation method
  2245. *
  2246. * Test validation errors, when validation message is an empty string.
  2247. */
  2248. public function testEmptyErrorValidation(): void
  2249. {
  2250. $this->article['errors'] = [
  2251. 'Article' => ['title' => ''],
  2252. ];
  2253. $this->Form->create($this->article);
  2254. $result = $this->Form->control('Article.title');
  2255. $expected = [
  2256. 'div' => ['class' => 'input text error'],
  2257. 'label' => ['for' => 'article-title'],
  2258. 'Title',
  2259. '/label',
  2260. 'input' => [
  2261. 'type' => 'text',
  2262. 'name' => 'Article[title]',
  2263. 'id' => 'article-title',
  2264. 'class' => 'form-error',
  2265. 'aria-invalid' => 'true',
  2266. 'aria-describedby' => 'article-title-error',
  2267. ],
  2268. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2269. [],
  2270. '/div',
  2271. '/div',
  2272. ];
  2273. $this->assertHtml($expected, $result);
  2274. }
  2275. /**
  2276. * testEmptyControlErrorValidation method
  2277. *
  2278. * Test validation errors, when calling control() overriding validation message by an empty string.
  2279. */
  2280. public function testEmptyControlErrorValidation(): void
  2281. {
  2282. $this->article['errors'] = [
  2283. 'Article' => ['title' => 'error message'],
  2284. ];
  2285. $this->Form->create($this->article);
  2286. $result = $this->Form->control('Article.title', ['error' => '']);
  2287. $expected = [
  2288. 'div' => ['class' => 'input text error'],
  2289. 'label' => ['for' => 'article-title'],
  2290. 'Title',
  2291. '/label',
  2292. 'input' => [
  2293. 'aria-invalid' => 'true',
  2294. 'aria-describedby' => 'article-title-error',
  2295. 'type' => 'text',
  2296. 'name' => 'Article[title]',
  2297. 'id' => 'article-title',
  2298. 'class' => 'form-error',
  2299. ],
  2300. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2301. [],
  2302. '/div',
  2303. '/div',
  2304. ];
  2305. $this->assertHtml($expected, $result);
  2306. }
  2307. /**
  2308. * testControlErrorMessage method
  2309. *
  2310. * Test validation errors, when calling control() overriding validation messages.
  2311. */
  2312. public function testControlErrorMessage(): void
  2313. {
  2314. $this->article['errors'] = [
  2315. 'title' => ['error message'],
  2316. ];
  2317. $this->Form->create($this->article);
  2318. $result = $this->Form->control('title', [
  2319. 'error' => 'Custom error!',
  2320. ]);
  2321. $expected = [
  2322. 'div' => ['class' => 'input text required error'],
  2323. 'label' => ['for' => 'title'],
  2324. 'Title',
  2325. '/label',
  2326. 'input' => [
  2327. 'type' => 'text',
  2328. 'name' => 'title',
  2329. 'id' => 'title',
  2330. 'class' => 'form-error',
  2331. 'required' => 'required',
  2332. 'data-validity-message' => 'This field cannot be left empty',
  2333. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2334. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2335. 'aria-required' => 'true',
  2336. 'aria-invalid' => 'true',
  2337. 'aria-describedby' => 'title-error',
  2338. ],
  2339. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2340. 'Custom error!',
  2341. '/div',
  2342. '/div',
  2343. ];
  2344. $this->assertHtml($expected, $result);
  2345. $result = $this->Form->control('title', [
  2346. 'error' => ['error message' => 'Custom error!'],
  2347. ]);
  2348. $expected = [
  2349. 'div' => ['class' => 'input text required error'],
  2350. 'label' => ['for' => 'title'],
  2351. 'Title',
  2352. '/label',
  2353. 'input' => [
  2354. 'type' => 'text',
  2355. 'name' => 'title',
  2356. 'id' => 'title',
  2357. 'aria-required' => 'true',
  2358. 'aria-invalid' => 'true',
  2359. 'aria-describedby' => 'title-error',
  2360. 'class' => 'form-error',
  2361. 'required' => 'required',
  2362. 'data-validity-message' => 'This field cannot be left empty',
  2363. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2364. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2365. ],
  2366. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2367. 'Custom error!',
  2368. '/div',
  2369. '/div',
  2370. ];
  2371. $this->assertHtml($expected, $result);
  2372. }
  2373. /**
  2374. * testFormValidationAssociated method
  2375. *
  2376. * Tests displaying errors for nested entities.
  2377. */
  2378. public function testFormValidationAssociated(): void
  2379. {
  2380. $nested = new Entity(['foo' => 'bar']);
  2381. $nested->setError('foo', ['not a valid bar']);
  2382. $entity = new Entity(['nested' => $nested]);
  2383. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2384. $result = $this->Form->error('nested.foo');
  2385. $this->assertSame('<div class="error-message" id="nested-foo-error">not a valid bar</div>', $result);
  2386. }
  2387. /**
  2388. * testFormValidationAssociatedSecondLevel method
  2389. *
  2390. * Test form error display with associated model.
  2391. */
  2392. public function testFormValidationAssociatedSecondLevel(): void
  2393. {
  2394. $inner = new Entity(['bar' => 'baz']);
  2395. $nested = new Entity(['foo' => $inner]);
  2396. $entity = new Entity(['nested' => $nested]);
  2397. $inner->setError('bar', ['not a valid one']);
  2398. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2399. $result = $this->Form->error('nested.foo.bar');
  2400. $this->assertSame('<div class="error-message" id="nested-foo-bar-error">not a valid one</div>', $result);
  2401. }
  2402. /**
  2403. * testFormValidationMultiRecord method
  2404. *
  2405. * Test form error display with multiple records.
  2406. */
  2407. public function testFormValidationMultiRecord(): void
  2408. {
  2409. $one = new Entity();
  2410. $two = new Entity();
  2411. $this->getTableLocator()->get('Contacts', [
  2412. 'className' => ContactsTable::class,
  2413. ]);
  2414. $one->set('email', '');
  2415. $one->setError('email', ['invalid email']);
  2416. $two->set('name', '');
  2417. $two->setError('name', ['This is wrong']);
  2418. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2419. $result = $this->Form->control('0.email');
  2420. $expected = [
  2421. 'div' => ['class' => 'input email error'],
  2422. 'label' => ['for' => '0-email'],
  2423. 'Email',
  2424. '/label',
  2425. 'input' => [
  2426. 'type' => 'email',
  2427. 'name' => '0[email]',
  2428. 'id' => '0-email',
  2429. 'class' => 'form-error',
  2430. 'maxlength' => 255,
  2431. 'value' => '',
  2432. 'aria-invalid' => 'true',
  2433. 'aria-describedby' => '0-email-error',
  2434. ],
  2435. ['div' => ['class' => 'error-message', 'id' => '0-email-error']],
  2436. 'invalid email',
  2437. '/div',
  2438. '/div',
  2439. ];
  2440. $this->assertHtml($expected, $result);
  2441. $result = $this->Form->control('1.name');
  2442. $expected = [
  2443. 'div' => ['class' => 'input text error'],
  2444. 'label' => ['for' => '1-name'],
  2445. 'Name',
  2446. '/label',
  2447. 'input' => [
  2448. 'type' => 'text',
  2449. 'name' => '1[name]',
  2450. 'id' => '1-name',
  2451. 'class' => 'form-error',
  2452. 'maxlength' => 255,
  2453. 'value' => '',
  2454. 'aria-invalid' => 'true',
  2455. 'aria-describedby' => '1-name-error',
  2456. ],
  2457. ['div' => ['class' => 'error-message', 'id' => '1-name-error']],
  2458. 'This is wrong',
  2459. '/div',
  2460. '/div',
  2461. ];
  2462. $this->assertHtml($expected, $result);
  2463. }
  2464. /**
  2465. * testControl method
  2466. *
  2467. * Test various incarnations of control().
  2468. */
  2469. public function testControl(): void
  2470. {
  2471. $this->getTableLocator()->get('ValidateUsers', [
  2472. 'className' => ValidateUsersTable::class,
  2473. ]);
  2474. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2475. $result = $this->Form->control('ValidateUsers.balance');
  2476. $expected = [
  2477. 'div' => ['class'],
  2478. 'label' => ['for'],
  2479. 'Balance',
  2480. '/label',
  2481. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2482. '/div',
  2483. ];
  2484. $this->assertHtml($expected, $result);
  2485. $result = $this->Form->control('ValidateUser.cost_decimal');
  2486. $expected = [
  2487. 'div' => ['class'],
  2488. 'label' => ['for'],
  2489. 'Cost Decimal',
  2490. '/label',
  2491. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2492. '/div',
  2493. ];
  2494. $this->assertHtml($expected, $result);
  2495. $result = $this->Form->control('ValidateUser.null_decimal');
  2496. $expected = [
  2497. 'div' => ['class'],
  2498. 'label' => ['for'],
  2499. 'Null Decimal',
  2500. '/label',
  2501. 'input' => ['name', 'type' => 'number', 'id'],
  2502. '/div',
  2503. ];
  2504. $this->assertHtml($expected, $result);
  2505. }
  2506. /**
  2507. * testControlCustomization method
  2508. *
  2509. * Tests the input method and passing custom options.
  2510. */
  2511. public function testControlCustomization(): void
  2512. {
  2513. $this->getTableLocator()->get('Contacts', [
  2514. 'className' => ContactsTable::class,
  2515. ]);
  2516. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2517. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2518. $expected = [
  2519. 'div' => ['class' => 'input email'],
  2520. 'label' => ['for' => 'custom'],
  2521. 'Email',
  2522. '/label',
  2523. ['input' => [
  2524. 'type' => 'email', 'name' => 'Contact[email]',
  2525. 'id' => 'custom', 'maxlength' => 255,
  2526. ]],
  2527. '/div',
  2528. ];
  2529. $this->assertHtml($expected, $result);
  2530. $result = $this->Form->control('Contact.email', [
  2531. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2532. ]);
  2533. $expected = [
  2534. '<div',
  2535. 'label' => ['for' => 'contact-email'],
  2536. 'Email',
  2537. '/label',
  2538. ['input' => [
  2539. 'type' => 'email', 'name' => 'Contact[email]',
  2540. 'id' => 'contact-email', 'maxlength' => 255,
  2541. ]],
  2542. '/div',
  2543. ];
  2544. $this->assertHtml($expected, $result);
  2545. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2546. $expected = [
  2547. 'div' => ['class' => 'input text'],
  2548. 'label' => ['for' => 'contact-email'],
  2549. 'Email',
  2550. '/label',
  2551. ['input' => [
  2552. 'type' => 'text', 'name' => 'Contact[email]',
  2553. 'id' => 'contact-email', 'maxlength' => '255',
  2554. ]],
  2555. '/div',
  2556. ];
  2557. $this->assertHtml($expected, $result);
  2558. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2559. $expected = [
  2560. 'div' => ['class' => 'input text'],
  2561. 'label' => ['for' => 'contact-5-email'],
  2562. 'Email',
  2563. '/label',
  2564. ['input' => [
  2565. 'type' => 'text', 'name' => 'Contact[5][email]',
  2566. 'id' => 'contact-5-email', 'maxlength' => '255',
  2567. ]],
  2568. '/div',
  2569. ];
  2570. $this->assertHtml($expected, $result);
  2571. $result = $this->Form->control('Contact.password');
  2572. $expected = [
  2573. 'div' => ['class' => 'input password'],
  2574. 'label' => ['for' => 'contact-password'],
  2575. 'Password',
  2576. '/label',
  2577. ['input' => [
  2578. 'type' => 'password', 'name' => 'Contact[password]',
  2579. 'id' => 'contact-password',
  2580. ]],
  2581. '/div',
  2582. ];
  2583. $this->assertHtml($expected, $result);
  2584. $result = $this->Form->control('Contact.email', [
  2585. 'type' => 'file', 'class' => 'textbox',
  2586. ]);
  2587. $expected = [
  2588. 'div' => ['class' => 'input file'],
  2589. 'label' => ['for' => 'contact-email'],
  2590. 'Email',
  2591. '/label',
  2592. ['input' => [
  2593. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2594. 'id' => 'contact-email',
  2595. ]],
  2596. '/div',
  2597. ];
  2598. $this->assertHtml($expected, $result);
  2599. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2600. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2601. $result = $this->Form->control('phone');
  2602. $expected = [
  2603. 'div' => ['class' => 'input tel'],
  2604. 'label' => ['for' => 'phone'],
  2605. 'Phone',
  2606. '/label',
  2607. ['input' => [
  2608. 'type' => 'tel', 'name' => 'phone',
  2609. 'value' => 'Hello &amp; World &gt; weird chars',
  2610. 'id' => 'phone', 'maxlength' => 255,
  2611. ]],
  2612. '/div',
  2613. ];
  2614. $this->assertHtml($expected, $result);
  2615. $this->View->setRequest(
  2616. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2617. );
  2618. $this->Form->create();
  2619. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2620. $expected = [
  2621. 'div' => ['class' => 'input text'],
  2622. 'label' => ['for' => 'myId'],
  2623. 'Field',
  2624. '/label',
  2625. 'input' => [
  2626. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2627. 'value' => 'My value', 'id' => 'myId',
  2628. ],
  2629. '/div',
  2630. ];
  2631. $this->assertHtml($expected, $result);
  2632. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2633. $this->Form->create();
  2634. $entity->setError('field', 'Badness!');
  2635. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2636. $result = $this->Form->control('field');
  2637. $expected = [
  2638. 'div' => ['class' => 'input text error'],
  2639. 'label' => ['for' => 'field'],
  2640. 'Field',
  2641. '/label',
  2642. 'input' => [
  2643. 'type' => 'text',
  2644. 'name' => 'field',
  2645. 'id' => 'field',
  2646. 'class' => 'form-error',
  2647. 'aria-invalid' => 'true',
  2648. 'aria-describedby' => 'field-error',
  2649. ],
  2650. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2651. 'Badness!',
  2652. '/div',
  2653. '/div',
  2654. ];
  2655. $this->assertHtml($expected, $result);
  2656. $result = $this->Form->control('field', [
  2657. 'templates' => [
  2658. 'inputContainerError' => '{{content}}{{error}}',
  2659. 'error' => '<span class="error-message">{{content}}</span>',
  2660. ],
  2661. ]);
  2662. $expected = [
  2663. 'label' => ['for' => 'field'],
  2664. 'Field',
  2665. '/label',
  2666. 'input' => [
  2667. 'type' => 'text',
  2668. 'name' => 'field',
  2669. 'id' => 'field',
  2670. 'class' => 'form-error',
  2671. // No aria-describedby because error template is custom
  2672. 'aria-invalid' => 'true',
  2673. ],
  2674. ['span' => ['class' => 'error-message']],
  2675. 'Badness!',
  2676. '/span',
  2677. ];
  2678. $this->assertHtml($expected, $result);
  2679. $entity->setError('field', ['minLength'], true);
  2680. $result = $this->Form->control('field', [
  2681. 'error' => [
  2682. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2683. 'maxLength' => 'login too large',
  2684. ],
  2685. ]);
  2686. $expected = [
  2687. 'div' => ['class' => 'input text error'],
  2688. 'label' => ['for' => 'field'],
  2689. 'Field',
  2690. '/label',
  2691. 'input' => [
  2692. 'type' => 'text',
  2693. 'name' => 'field',
  2694. 'id' => 'field',
  2695. 'class' => 'form-error',
  2696. 'aria-invalid' => 'true',
  2697. 'aria-describedby' => 'field-error',
  2698. ],
  2699. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2700. 'Le login doit contenir au moins 2 caractères',
  2701. '/div',
  2702. '/div',
  2703. ];
  2704. $this->assertHtml($expected, $result);
  2705. $entity->setError('field', ['maxLength'], true);
  2706. $result = $this->Form->control('field', [
  2707. 'error' => [
  2708. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2709. 'maxLength' => 'login too large',
  2710. ],
  2711. ]);
  2712. $expected = [
  2713. 'div' => ['class' => 'input text error'],
  2714. 'label' => ['for' => 'field'],
  2715. 'Field',
  2716. '/label',
  2717. 'input' => [
  2718. 'type' => 'text',
  2719. 'name' => 'field',
  2720. 'id' => 'field',
  2721. 'class' => 'form-error',
  2722. 'aria-invalid' => 'true',
  2723. 'aria-describedby' => 'field-error',
  2724. ],
  2725. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2726. 'login too large',
  2727. '/div',
  2728. '/div',
  2729. ];
  2730. $this->assertHtml($expected, $result);
  2731. }
  2732. /**
  2733. * testControlWithTemplateFile method
  2734. *
  2735. * Test that control() accepts a template file.
  2736. */
  2737. public function testControlWithTemplateFile(): void
  2738. {
  2739. $result = $this->Form->control('field', [
  2740. 'templates' => 'htmlhelper_tags',
  2741. ]);
  2742. $expected = [
  2743. 'label' => ['for' => 'field'],
  2744. 'Field',
  2745. '/label',
  2746. 'input' => [
  2747. 'type' => 'text', 'name' => 'field',
  2748. 'id' => 'field',
  2749. ],
  2750. ];
  2751. $this->assertHtml($expected, $result);
  2752. }
  2753. /**
  2754. * testNestedControlsEndWithBrackets method
  2755. *
  2756. * Test that nested inputs end with brackets.
  2757. */
  2758. public function testNestedControlsEndWithBrackets(): void
  2759. {
  2760. $result = $this->Form->text('nested.text[]');
  2761. $expected = [
  2762. 'input' => [
  2763. 'type' => 'text', 'name' => 'nested[text][]',
  2764. ],
  2765. ];
  2766. $this->assertHtml($expected, $result);
  2767. $result = $this->Form->file('nested.file[]');
  2768. $expected = [
  2769. 'input' => [
  2770. 'type' => 'file', 'name' => 'nested[file][]',
  2771. ],
  2772. ];
  2773. $this->assertHtml($expected, $result);
  2774. }
  2775. /**
  2776. * testCreateIdPrefix method
  2777. *
  2778. * Test id prefix.
  2779. */
  2780. public function testCreateIdPrefix(): void
  2781. {
  2782. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2783. $result = $this->Form->control('field');
  2784. $expected = [
  2785. 'div' => ['class' => 'input text'],
  2786. 'label' => ['for' => 'prefix-field'],
  2787. 'Field',
  2788. '/label',
  2789. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2790. '/div',
  2791. ];
  2792. $this->assertHtml($expected, $result);
  2793. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2794. $expected = [
  2795. 'div' => ['class' => 'input text'],
  2796. 'label' => ['for' => 'custom-id'],
  2797. 'Field',
  2798. '/label',
  2799. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2800. '/div',
  2801. ];
  2802. $this->assertHtml($expected, $result);
  2803. $result = $this->Form->radio('Model.field', ['option A']);
  2804. $expected = [
  2805. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2806. 'label' => ['for' => 'prefix-model-field-0'],
  2807. ['input' => [
  2808. 'type' => 'radio',
  2809. 'name' => 'Model[field]',
  2810. 'value' => '0',
  2811. 'id' => 'prefix-model-field-0',
  2812. ]],
  2813. 'option A',
  2814. '/label',
  2815. ];
  2816. $this->assertHtml($expected, $result);
  2817. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2818. $expected = [
  2819. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2820. 'label' => ['for' => 'prefix-model-field-0'],
  2821. ['input' => [
  2822. 'type' => 'radio',
  2823. 'name' => 'Model[field]',
  2824. 'value' => '0',
  2825. 'id' => 'prefix-model-field-0',
  2826. ]],
  2827. 'option A',
  2828. '/label',
  2829. ];
  2830. $this->assertHtml($expected, $result);
  2831. $result = $this->Form->select(
  2832. 'Model.multi_field',
  2833. ['first'],
  2834. ['multiple' => 'checkbox']
  2835. );
  2836. $expected = [
  2837. 'input' => [
  2838. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'prefix-model-multi-field',
  2839. ],
  2840. ['div' => ['class' => 'checkbox']],
  2841. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2842. ['input' => [
  2843. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2844. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  2845. ]],
  2846. 'first',
  2847. '/label',
  2848. '/div',
  2849. ];
  2850. $this->assertHtml($expected, $result);
  2851. $this->Form->end();
  2852. $result = $this->Form->control('field');
  2853. $expected = [
  2854. 'div' => ['class' => 'input text'],
  2855. 'label' => ['for' => 'field'],
  2856. 'Field',
  2857. '/label',
  2858. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2859. '/div',
  2860. ];
  2861. $this->assertHtml($expected, $result);
  2862. }
  2863. /**
  2864. * testControlZero method
  2865. *
  2866. * Test that inputs with 0 can be created.
  2867. */
  2868. public function testControlZero(): void
  2869. {
  2870. $this->getTableLocator()->get('Contacts', [
  2871. 'className' => ContactsTable::class,
  2872. ]);
  2873. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2874. $result = $this->Form->control('0');
  2875. $expected = [
  2876. 'div' => ['class' => 'input text'],
  2877. 'label' => ['for' => '0'], '/label',
  2878. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2879. '/div',
  2880. ];
  2881. $this->assertHtml($expected, $result);
  2882. }
  2883. /**
  2884. * testControlCheckbox method
  2885. *
  2886. * Test control() with checkbox creation.
  2887. */
  2888. public function testControlCheckbox(): void
  2889. {
  2890. $articles = $this->getTableLocator()->get('Articles');
  2891. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  2892. $article = $articles->newEmptyEntity();
  2893. $this->Form->create($article);
  2894. $result = $this->Form->control('Articles.active');
  2895. $expected = [
  2896. 'div' => ['class' => 'input checkbox'],
  2897. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2898. 'label' => ['for' => 'articles-active'],
  2899. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  2900. 'Active',
  2901. '/label',
  2902. '/div',
  2903. ];
  2904. $this->assertHtml($expected, $result);
  2905. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  2906. $expected = [
  2907. 'div' => ['class' => 'input checkbox'],
  2908. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2909. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2910. '/div',
  2911. ];
  2912. $this->assertHtml($expected, $result);
  2913. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  2914. $expected = [
  2915. 'div' => ['class' => 'input checkbox'],
  2916. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2917. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2918. '/div',
  2919. ];
  2920. $this->assertHtml($expected, $result);
  2921. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  2922. $expected = [
  2923. 'div' => ['class' => 'input checkbox'],
  2924. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2925. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2926. '/div',
  2927. ];
  2928. $this->assertHtml($expected, $result);
  2929. $result = $this->Form->control('Articles.disabled', [
  2930. 'label' => 'Disabled',
  2931. 'type' => 'checkbox',
  2932. 'data-foo' => 'disabled',
  2933. ]);
  2934. $expected = [
  2935. 'div' => ['class' => 'input checkbox'],
  2936. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  2937. 'label' => ['for' => 'articles-disabled'],
  2938. ['input' => [
  2939. 'type' => 'checkbox',
  2940. 'name' => 'Articles[disabled]',
  2941. 'value' => '1',
  2942. 'id' => 'articles-disabled',
  2943. 'data-foo' => 'disabled',
  2944. ]],
  2945. 'Disabled',
  2946. '/label',
  2947. '/div',
  2948. ];
  2949. $this->assertHtml($expected, $result);
  2950. $result = $this->Form->control('Articles.confirm', [
  2951. 'label' => 'Confirm <b>me</b>!',
  2952. 'type' => 'checkbox',
  2953. 'escape' => false,
  2954. ]);
  2955. $expected = [
  2956. 'div' => ['class' => 'input checkbox'],
  2957. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  2958. 'label' => ['for' => 'articles-confirm'],
  2959. ['input' => [
  2960. 'type' => 'checkbox',
  2961. 'name' => 'Articles[confirm]',
  2962. 'value' => '1',
  2963. 'id' => 'articles-confirm',
  2964. ]],
  2965. 'Confirm <b>me</b>!',
  2966. '/label',
  2967. '/div',
  2968. ];
  2969. $this->assertHtml($expected, $result);
  2970. }
  2971. /**
  2972. * testControlHidden method
  2973. *
  2974. * Test that control() does not create wrapping div and label tag for hidden fields.
  2975. */
  2976. public function testControlHidden(): void
  2977. {
  2978. $this->getTableLocator()->get('ValidateUsers', [
  2979. 'className' => ValidateUsersTable::class,
  2980. ]);
  2981. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2982. $result = $this->Form->control('ValidateUser.id');
  2983. $expected = [
  2984. 'input' => ['name', 'type' => 'hidden', 'id'],
  2985. ];
  2986. $this->assertHtml($expected, $result);
  2987. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  2988. $expected = [
  2989. 'input' => ['name', 'type' => 'hidden', 'id'],
  2990. ];
  2991. $this->assertHtml($expected, $result);
  2992. }
  2993. /**
  2994. * testControlDatetime method
  2995. *
  2996. * Test form->control() with datetime.
  2997. */
  2998. public function testControlDatetime(): void
  2999. {
  3000. $result = $this->Form->control('prueba', [
  3001. 'type' => 'datetime',
  3002. 'value' => new DateTime('2019-09-27 02:52:43'),
  3003. ]);
  3004. $expected = [
  3005. 'div' => ['class' => 'input datetime'],
  3006. 'label' => ['for' => 'prueba'],
  3007. 'Prueba',
  3008. '/label',
  3009. 'input' => [
  3010. 'name' => 'prueba',
  3011. 'id' => 'prueba',
  3012. 'type' => 'datetime-local',
  3013. 'value' => '2019-09-27T02:52:43',
  3014. 'step' => '1',
  3015. ],
  3016. '/div',
  3017. ];
  3018. $this->assertHtml($expected, $result);
  3019. }
  3020. /**
  3021. * testControlDatetimeIdPrefix method
  3022. *
  3023. * Test form->control() with datetime with id prefix.
  3024. */
  3025. public function testControlDatetimeIdPrefix(): void
  3026. {
  3027. $this->Form->create(null, ['idPrefix' => 'prefix']);
  3028. $result = $this->Form->control('prueba', [
  3029. 'type' => 'datetime',
  3030. ]);
  3031. $expected = [
  3032. 'div' => ['class' => 'input datetime'],
  3033. 'label' => ['for' => 'prefix-prueba'],
  3034. 'Prueba',
  3035. '/label',
  3036. 'input' => [
  3037. 'name' => 'prueba',
  3038. 'id' => 'prefix-prueba',
  3039. 'type' => 'datetime-local',
  3040. 'value' => '',
  3041. 'step' => '1',
  3042. ],
  3043. '/div',
  3044. ];
  3045. $this->assertHtml($expected, $result);
  3046. }
  3047. /**
  3048. * testControlDatetimeStep method
  3049. *
  3050. * Test form->control() with datetime with custom step size.
  3051. */
  3052. public function testControlDatetimeStep(): void
  3053. {
  3054. $result = $this->Form->control('prueba', [
  3055. 'type' => 'datetime',
  3056. 'value' => new DateTime('2019-09-27 02:52:43'),
  3057. 'step' => '0.5',
  3058. ]);
  3059. $expected = [
  3060. 'div' => ['class' => 'input datetime'],
  3061. 'label' => ['for' => 'prueba'],
  3062. 'Prueba',
  3063. '/label',
  3064. 'input' => [
  3065. 'name' => 'prueba',
  3066. 'id' => 'prueba',
  3067. 'type' => 'datetime-local',
  3068. 'value' => '2019-09-27T02:52:43.000',
  3069. 'step' => '0.5',
  3070. ],
  3071. '/div',
  3072. ];
  3073. $this->assertHtml($expected, $result);
  3074. }
  3075. /**
  3076. * testControlCheckboxWithDisabledElements method
  3077. *
  3078. * Test generating checkboxes with disabled elements.
  3079. */
  3080. public function testControlCheckboxWithDisabledElements(): void
  3081. {
  3082. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3083. $result = $this->Form->control('Contact.multiple', [
  3084. 'multiple' => 'checkbox',
  3085. 'disabled' => 'disabled',
  3086. 'options' => $options,
  3087. ]);
  3088. $expected = [
  3089. ['div' => ['class' => 'input select']],
  3090. ['label' => ['for' => 'contact-multiple']],
  3091. 'Multiple',
  3092. '/label',
  3093. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '', 'id' => 'contact-multiple']],
  3094. ['div' => ['class' => 'checkbox']],
  3095. ['label' => ['for' => 'contact-multiple-1']],
  3096. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3097. 'One',
  3098. '/label',
  3099. '/div',
  3100. ['div' => ['class' => 'checkbox']],
  3101. ['label' => ['for' => 'contact-multiple-2']],
  3102. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3103. 'Two',
  3104. '/label',
  3105. '/div',
  3106. ['div' => ['class' => 'checkbox']],
  3107. ['label' => ['for' => 'contact-multiple-3']],
  3108. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3109. 'Three',
  3110. '/label',
  3111. '/div',
  3112. '/div',
  3113. ];
  3114. $this->assertHtml($expected, $result);
  3115. // make sure 50 does only disable 50, and not 50f5c0cf
  3116. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3117. $disabled = [50];
  3118. $expected = [
  3119. ['div' => ['class' => 'input select']],
  3120. ['label' => ['for' => 'contact-multiple']],
  3121. 'Multiple',
  3122. '/label',
  3123. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'contact-multiple']],
  3124. ['div' => ['class' => 'checkbox']],
  3125. ['label' => ['for' => 'contact-multiple-50']],
  3126. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3127. 'Fifty',
  3128. '/label',
  3129. '/div',
  3130. ['div' => ['class' => 'checkbox']],
  3131. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3132. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3133. 'Stringy',
  3134. '/label',
  3135. '/div',
  3136. '/div',
  3137. ];
  3138. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3139. $this->assertHtml($expected, $result);
  3140. }
  3141. /**
  3142. * testControlWithLeadingInteger method
  3143. *
  3144. * Test input name with leading integer, ensure attributes are generated correctly.
  3145. */
  3146. public function testControlWithLeadingInteger(): void
  3147. {
  3148. $result = $this->Form->text('0.Node.title');
  3149. $expected = [
  3150. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3151. ];
  3152. $this->assertHtml($expected, $result);
  3153. }
  3154. /**
  3155. * testControlSelectType method
  3156. *
  3157. * Test form->control() with select type inputs.
  3158. */
  3159. public function testControlSelectType(): void
  3160. {
  3161. $result = $this->Form->control(
  3162. 'email',
  3163. [
  3164. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3165. );
  3166. $expected = [
  3167. 'div' => ['class' => 'input select'],
  3168. 'label' => ['for' => 'email'],
  3169. 'Email',
  3170. '/label',
  3171. ['select' => ['name' => 'email', 'id' => 'email']],
  3172. ['option' => ['value' => '']],
  3173. '/option',
  3174. ['option' => ['value' => 'è']],
  3175. 'Firést',
  3176. '/option',
  3177. ['option' => ['value' => 'é']],
  3178. 'Secoènd',
  3179. '/option',
  3180. '/select',
  3181. '/div',
  3182. ];
  3183. $this->assertHtml($expected, $result);
  3184. $result = $this->Form->control(
  3185. 'email',
  3186. [
  3187. 'options' => ['First', 'Second'], 'empty' => true]
  3188. );
  3189. $expected = [
  3190. 'div' => ['class' => 'input select'],
  3191. 'label' => ['for' => 'email'],
  3192. 'Email',
  3193. '/label',
  3194. ['select' => ['name' => 'email', 'id' => 'email']],
  3195. ['option' => ['value' => '']],
  3196. '/option',
  3197. ['option' => ['value' => '0']],
  3198. 'First',
  3199. '/option',
  3200. ['option' => ['value' => '1']],
  3201. 'Second',
  3202. '/option',
  3203. '/select',
  3204. '/div',
  3205. ];
  3206. $this->assertHtml($expected, $result);
  3207. $result = $this->Form->control('email', [
  3208. 'type' => 'select',
  3209. 'options' => new ArrayObject(['First', 'Second']),
  3210. 'empty' => true,
  3211. ]);
  3212. $this->assertHtml($expected, $result);
  3213. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3214. $this->View->setRequest(
  3215. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3216. );
  3217. $this->Form->create();
  3218. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3219. $expected = [
  3220. 'div' => ['class' => 'input select'],
  3221. 'label' => ['for' => 'model-user-id'],
  3222. 'User',
  3223. '/label',
  3224. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3225. ['option' => ['value' => '']],
  3226. '/option',
  3227. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3228. 'good',
  3229. '/option',
  3230. ['option' => ['value' => 'other']],
  3231. 'bad',
  3232. '/option',
  3233. '/select',
  3234. '/div',
  3235. ];
  3236. $this->assertHtml($expected, $result);
  3237. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3238. $this->View->setRequest(
  3239. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3240. );
  3241. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3242. $expected = [
  3243. 'div' => ['class' => 'input select'],
  3244. 'label' => ['for' => 'thing-user-id'],
  3245. 'User',
  3246. '/label',
  3247. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3248. ['option' => ['value' => '']],
  3249. 'Some Empty',
  3250. '/option',
  3251. ['option' => ['value' => 'value']],
  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' => 'value'])
  3264. );
  3265. $this->Form->create();
  3266. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3267. $expected = [
  3268. 'div' => ['class' => 'input select'],
  3269. 'label' => ['for' => 'thing-user-id'],
  3270. 'User',
  3271. '/label',
  3272. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3273. ['option' => ['value' => '']],
  3274. 'Some Empty',
  3275. '/option',
  3276. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3277. 'good',
  3278. '/option',
  3279. ['option' => ['value' => 'other']],
  3280. 'bad',
  3281. '/option',
  3282. '/select',
  3283. '/div',
  3284. ];
  3285. $this->assertHtml($expected, $result);
  3286. $result = $this->Form->control('Publisher.id', [
  3287. 'label' => 'Publisher',
  3288. 'type' => 'select',
  3289. 'multiple' => 'checkbox',
  3290. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3291. ]);
  3292. $expected = [
  3293. ['div' => ['class' => 'input select']],
  3294. ['label' => ['for' => 'publisher-id']],
  3295. 'Publisher',
  3296. '/label',
  3297. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'publisher-id'],
  3298. ['div' => ['class' => 'checkbox']],
  3299. ['label' => ['for' => 'publisher-id-value-1']],
  3300. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3301. 'Label 1',
  3302. '/label',
  3303. '/div',
  3304. ['div' => ['class' => 'checkbox']],
  3305. ['label' => ['for' => 'publisher-id-value-2']],
  3306. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3307. 'Label 2',
  3308. '/label',
  3309. '/div',
  3310. '/div',
  3311. ];
  3312. $this->assertHtml($expected, $result);
  3313. }
  3314. /**
  3315. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3316. *
  3317. * Test that control() and a non standard primary key makes a hidden input by default.
  3318. */
  3319. public function testControlWithNonStandardPrimaryKeyMakesHidden(): void
  3320. {
  3321. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3322. $this->Form->create($this->article);
  3323. $result = $this->Form->control('title');
  3324. $expected = [
  3325. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3326. ];
  3327. $this->assertHtml($expected, $result);
  3328. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3329. $this->Form->create($this->article);
  3330. $result = $this->Form->control('title');
  3331. $expected = [
  3332. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3333. ];
  3334. $this->assertHtml($expected, $result);
  3335. $result = $this->Form->control('body');
  3336. $expected = [
  3337. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3338. ];
  3339. $this->assertHtml($expected, $result);
  3340. }
  3341. /**
  3342. * testControlOverridingMagicSelectType method
  3343. *
  3344. * Test that overriding the magic select type widget is possible.
  3345. */
  3346. public function testControlOverridingMagicSelectType(): void
  3347. {
  3348. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3349. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3350. $expected = [
  3351. 'div' => ['class' => 'input text'],
  3352. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3353. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3354. '/div',
  3355. ];
  3356. $this->assertHtml($expected, $result);
  3357. //Check that magic types still work for plural/singular vars
  3358. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3359. $result = $this->Form->control('Model.type');
  3360. $expected = [
  3361. 'div' => ['class' => 'input select'],
  3362. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3363. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3364. ['option' => ['value' => 'value']], 'good', '/option',
  3365. ['option' => ['value' => 'other']], 'bad', '/option',
  3366. '/select',
  3367. '/div',
  3368. ];
  3369. $this->assertHtml($expected, $result);
  3370. }
  3371. /**
  3372. * testControlMagicTypeDoesNotOverride method
  3373. *
  3374. * Test that inferred types do not override developer input.
  3375. */
  3376. public function testControlMagicTypeDoesNotOverride(): void
  3377. {
  3378. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3379. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3380. $expected = [
  3381. 'div' => ['class' => 'input checkbox'],
  3382. ['input' => [
  3383. 'type' => 'hidden',
  3384. 'name' => 'Model[user]',
  3385. 'value' => 0,
  3386. ]],
  3387. 'label' => ['for' => 'model-user'],
  3388. ['input' => [
  3389. 'name' => 'Model[user]',
  3390. 'type' => 'checkbox',
  3391. 'id' => 'model-user',
  3392. 'value' => 1,
  3393. ]],
  3394. 'User',
  3395. '/label',
  3396. '/div',
  3397. ];
  3398. $this->assertHtml($expected, $result);
  3399. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3400. $options = [
  3401. 1 => 'blue',
  3402. 2 => 'red',
  3403. ];
  3404. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3405. $expected = [
  3406. 'div' => ['class' => 'input select'],
  3407. 'label' => ['for' => 'tags-ids'],
  3408. 'Tags',
  3409. '/label',
  3410. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => '', 'id' => 'tags-ids'],
  3411. ['div' => ['class' => 'checkbox']],
  3412. ['label' => ['for' => 'tags-ids-1']],
  3413. ['input' => [
  3414. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3415. 'value' => '1', 'name' => 'tags[_ids][]',
  3416. ]],
  3417. 'blue',
  3418. '/label',
  3419. '/div',
  3420. ['div' => ['class' => 'checkbox']],
  3421. ['label' => ['for' => 'tags-ids-2']],
  3422. ['input' => [
  3423. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3424. 'value' => '2', 'name' => 'tags[_ids][]',
  3425. ]],
  3426. 'red',
  3427. '/label',
  3428. '/div',
  3429. '/div',
  3430. ];
  3431. $this->assertHtml($expected, $result);
  3432. }
  3433. /**
  3434. * testControlMagicSelectForTypeNumber method
  3435. *
  3436. * Test that magic control() selects are created for type=number.
  3437. */
  3438. public function testControlMagicSelectForTypeNumber(): void
  3439. {
  3440. $this->getTableLocator()->get('ValidateUsers', [
  3441. 'className' => ValidateUsersTable::class,
  3442. ]);
  3443. $entity = new Entity(['balance' => 1]);
  3444. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3445. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3446. $result = $this->Form->control('balance');
  3447. $expected = [
  3448. 'div' => ['class' => 'input select'],
  3449. 'label' => ['for' => 'balance'],
  3450. 'Balance',
  3451. '/label',
  3452. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3453. ['option' => ['value' => '0']],
  3454. 'nothing',
  3455. '/option',
  3456. ['option' => ['value' => '1', 'selected' => 'selected']],
  3457. 'some',
  3458. '/option',
  3459. ['option' => ['value' => '100']],
  3460. 'a lot',
  3461. '/option',
  3462. '/select',
  3463. '/div',
  3464. ];
  3465. $this->assertHtml($expected, $result);
  3466. }
  3467. /**
  3468. * testInvalidControlTypeOption method
  3469. *
  3470. * Test invalid 'input' type option to control() function.
  3471. */
  3472. public function testInvalidControlTypeOption(): void
  3473. {
  3474. $this->expectException(InvalidArgumentException::class);
  3475. $this->expectExceptionMessage('Invalid type `input` used for field `text`.');
  3476. $this->Form->control('text', ['type' => 'input']);
  3477. }
  3478. /**
  3479. * testControlMagicSelectChangeToRadio method
  3480. *
  3481. * Test that magic control() selects can easily be converted into radio types without error.
  3482. */
  3483. public function testControlMagicSelectChangeToRadio(): void
  3484. {
  3485. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3486. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3487. $this->assertStringContainsString('input type="radio"', $result);
  3488. }
  3489. /**
  3490. * testFormControlSubmit method
  3491. *
  3492. * Test correct results for form::control() and type submit.
  3493. */
  3494. public function testFormControlSubmit(): void
  3495. {
  3496. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3497. $expected = [
  3498. 'div' => ['class' => 'submit'],
  3499. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3500. '/div',
  3501. ];
  3502. $this->assertHtml($expected, $result);
  3503. }
  3504. /**
  3505. * testFormControls method
  3506. *
  3507. * Test correct results from Form::controls().
  3508. */
  3509. public function testFormControlsLegendFieldset(): void
  3510. {
  3511. $this->Form->create($this->article);
  3512. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3513. $expected = [
  3514. '<fieldset',
  3515. '<legend',
  3516. 'The Legend',
  3517. '/legend',
  3518. '*/fieldset',
  3519. ];
  3520. $this->assertHtml($expected, $result);
  3521. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3522. $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
  3523. $this->assertStringContainsString('<fieldset>', $result);
  3524. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3525. $this->assertStringNotContainsString('<legend>', $result);
  3526. $this->assertStringNotContainsString('<fieldset>', $result);
  3527. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3528. $this->assertStringNotContainsString('<legend>', $result);
  3529. $this->assertStringNotContainsString('<fieldset>', $result);
  3530. $this->Form->create($this->article);
  3531. $this->View->setRequest($this->View->getRequest()
  3532. ->withParam('prefix', 'admin')
  3533. ->withParam('action', 'admin_edit')
  3534. ->withParam('controller', 'articles'));
  3535. $result = $this->Form->allControls();
  3536. $expected = [
  3537. '<fieldset',
  3538. '<legend',
  3539. 'New Article',
  3540. '/legend',
  3541. '*/fieldset',
  3542. ];
  3543. $this->assertHtml($expected, $result);
  3544. $this->Form->create($this->article);
  3545. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3546. $expected = [
  3547. '<fieldset',
  3548. '<legend',
  3549. 'The Legend',
  3550. '/legend',
  3551. '*/fieldset',
  3552. ];
  3553. $this->assertHtml($expected, $result);
  3554. $this->Form->create($this->article);
  3555. $result = $this->Form->allControls([], [
  3556. 'fieldset' => [
  3557. 'class' => 'some-class some-other-class',
  3558. 'disabled' => true,
  3559. 'data-param' => 'a-param',
  3560. ],
  3561. 'legend' => 'The Legend',
  3562. ]);
  3563. $expected = [
  3564. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3565. '<legend',
  3566. 'The Legend',
  3567. '/legend',
  3568. '*/fieldset',
  3569. ];
  3570. $this->assertHtml($expected, $result);
  3571. }
  3572. /**
  3573. * testFormControls method
  3574. *
  3575. * Test the controls() method.
  3576. */
  3577. public function testFormControls(): void
  3578. {
  3579. $this->Form->create($this->article);
  3580. $result = $this->Form->allControls();
  3581. $expected = [
  3582. '<fieldset',
  3583. '<legend', 'New Article', '/legend',
  3584. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3585. ['div' => ['class' => 'input select required']],
  3586. '*/div',
  3587. ['div' => ['class' => 'input text required']],
  3588. '*/div',
  3589. ['div' => ['class' => 'input text']],
  3590. '*/div',
  3591. ['div' => ['class' => 'input text']],
  3592. '*/div',
  3593. '/fieldset',
  3594. ];
  3595. $this->assertHtml($expected, $result);
  3596. $result = $this->Form->allControls([
  3597. 'published' => ['type' => 'boolean'],
  3598. ]);
  3599. $expected = [
  3600. '<fieldset',
  3601. '<legend', 'New Article', '/legend',
  3602. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3603. ['div' => ['class' => 'input select required']],
  3604. '*/div',
  3605. ['div' => ['class' => 'input text required']],
  3606. '*/div',
  3607. ['div' => ['class' => 'input text']],
  3608. '*/div',
  3609. ['div' => ['class' => 'input boolean']],
  3610. '*/div',
  3611. '/fieldset',
  3612. ];
  3613. $this->assertHtml($expected, $result);
  3614. $this->Form->create($this->article);
  3615. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3616. $expected = [
  3617. 'fieldset' => [],
  3618. 'legend' => [],
  3619. 'Hello',
  3620. '/legend',
  3621. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3622. ['div' => ['class' => 'input select required']],
  3623. '*/div',
  3624. ['div' => ['class' => 'input text required']],
  3625. '*/div',
  3626. ['div' => ['class' => 'input text']],
  3627. '*/div',
  3628. ['div' => ['class' => 'input text']],
  3629. '*/div',
  3630. '/fieldset',
  3631. ];
  3632. $this->assertHtml($expected, $result);
  3633. $this->Form->create();
  3634. $expected = [
  3635. 'fieldset' => [],
  3636. ['div' => ['class' => 'input text']],
  3637. 'label' => ['for' => 'foo'],
  3638. 'Foo',
  3639. '/label',
  3640. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3641. '*/div',
  3642. '/fieldset',
  3643. ];
  3644. $result = $this->Form->allControls(
  3645. ['foo' => ['type' => 'text']],
  3646. ['legend' => false]
  3647. );
  3648. $this->assertHtml($expected, $result);
  3649. }
  3650. /**
  3651. * testFormControlsBlacklist method
  3652. */
  3653. public function testFormControlsBlacklist(): void
  3654. {
  3655. $this->Form->create($this->article);
  3656. $result = $this->Form->allControls([
  3657. 'id' => false,
  3658. ]);
  3659. $expected = [
  3660. '<fieldset',
  3661. '<legend', 'New Article', '/legend',
  3662. ['div' => ['class' => 'input select required']],
  3663. '*/div',
  3664. ['div' => ['class' => 'input text required']],
  3665. '*/div',
  3666. ['div' => ['class' => 'input text']],
  3667. '*/div',
  3668. ['div' => ['class' => 'input text']],
  3669. '*/div',
  3670. '/fieldset',
  3671. ];
  3672. $this->assertHtml($expected, $result);
  3673. $this->Form->create($this->article);
  3674. $result = $this->Form->allControls([
  3675. 'id' => [],
  3676. ]);
  3677. $expected = [
  3678. '<fieldset',
  3679. '<legend', 'New Article', '/legend',
  3680. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3681. ['div' => ['class' => 'input select required']],
  3682. '*/div',
  3683. ['div' => ['class' => 'input text required']],
  3684. '*/div',
  3685. ['div' => ['class' => 'input text']],
  3686. '*/div',
  3687. ['div' => ['class' => 'input text']],
  3688. '*/div',
  3689. '/fieldset',
  3690. ];
  3691. $this->assertHtml($expected, $result, true);
  3692. }
  3693. /**
  3694. * testSelectAsCheckbox method
  3695. *
  3696. * Test multi-select widget with checkbox formatting.
  3697. */
  3698. public function testSelectAsCheckbox(): void
  3699. {
  3700. $result = $this->Form->select(
  3701. 'Model.multi_field',
  3702. ['first', 'second', 'third'],
  3703. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3704. );
  3705. $expected = [
  3706. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3707. ['div' => ['class' => 'checkbox']],
  3708. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3709. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3710. 'first',
  3711. '/label',
  3712. '/div',
  3713. ['div' => ['class' => 'checkbox']],
  3714. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3715. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3716. 'second',
  3717. '/label',
  3718. '/div',
  3719. ['div' => ['class' => 'checkbox']],
  3720. ['label' => ['for' => 'model-multi-field-2']],
  3721. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3722. 'third',
  3723. '/label',
  3724. '/div',
  3725. ];
  3726. $this->assertHtml($expected, $result);
  3727. $result = $this->Form->select(
  3728. 'Model.multi_field',
  3729. ['1/2' => 'half'],
  3730. ['multiple' => 'checkbox']
  3731. );
  3732. $expected = [
  3733. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3734. ['div' => ['class' => 'checkbox']],
  3735. ['label' => ['for' => 'model-multi-field-1-2']],
  3736. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3737. 'half',
  3738. '/label',
  3739. '/div',
  3740. ];
  3741. $this->assertHtml($expected, $result);
  3742. }
  3743. /**
  3744. * testLabel method
  3745. *
  3746. * Test label generation.
  3747. */
  3748. public function testLabel(): void
  3749. {
  3750. $result = $this->Form->label('Person.name');
  3751. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3752. $this->assertHtml($expected, $result);
  3753. $result = $this->Form->label('Person.name');
  3754. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3755. $this->assertHtml($expected, $result);
  3756. $result = $this->Form->label('Person.first_name');
  3757. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3758. $this->assertHtml($expected, $result);
  3759. $result = $this->Form->label('Person.first_name', 'Your first name');
  3760. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3761. $this->assertHtml($expected, $result);
  3762. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3763. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3764. $this->assertHtml($expected, $result);
  3765. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3766. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3767. $this->assertHtml($expected, $result);
  3768. $result = $this->Form->label('Person.first_name', '');
  3769. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3770. $this->assertHtml($expected, $result);
  3771. $result = $this->Form->label('Person.2.name', '');
  3772. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3773. $this->assertHtml($expected, $result);
  3774. }
  3775. /**
  3776. * testLabelContainControl method
  3777. *
  3778. * Test that label() can accept an input with the correct template vars.
  3779. */
  3780. public function testLabelContainControl(): void
  3781. {
  3782. $this->Form->setTemplates([
  3783. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3784. ]);
  3785. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3786. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3787. ]);
  3788. $expected = [
  3789. 'label' => ['for' => 'person-accept-terms'],
  3790. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3791. 'Accept',
  3792. '/label',
  3793. ];
  3794. $this->assertHtml($expected, $result);
  3795. }
  3796. /**
  3797. * testTextbox method
  3798. *
  3799. * Test textbox element generation.
  3800. */
  3801. public function testTextbox(): void
  3802. {
  3803. $result = $this->Form->text('Model.field');
  3804. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3805. $this->assertHtml($expected, $result);
  3806. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3807. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3808. $this->assertHtml($expected, $result);
  3809. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3810. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3811. $this->assertHtml($expected, $result);
  3812. }
  3813. /**
  3814. * testTextBoxDataAndError method
  3815. *
  3816. * Test that text() hooks up with request data and error fields.
  3817. */
  3818. public function testTextBoxDataAndError(): void
  3819. {
  3820. $this->article['errors'] = [
  3821. 'Contact' => ['text' => 'wrong'],
  3822. ];
  3823. $this->View->setRequest($this->View->getRequest()
  3824. ->withData('Model.text', 'test <strong>HTML</strong> values')
  3825. ->withData('Contact.text', 'test'));
  3826. $this->Form->create($this->article);
  3827. $result = $this->Form->text('Model.text');
  3828. $expected = [
  3829. 'input' => [
  3830. 'type' => 'text',
  3831. 'name' => 'Model[text]',
  3832. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3833. ],
  3834. ];
  3835. $this->assertHtml($expected, $result);
  3836. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3837. $expected = [
  3838. 'input' => [
  3839. 'type' => 'text',
  3840. 'name' => 'Contact[text]',
  3841. 'value' => 'test',
  3842. 'id' => 'theID',
  3843. 'class' => 'form-error',
  3844. ],
  3845. ];
  3846. $this->assertHtml($expected, $result);
  3847. }
  3848. /**
  3849. * testDefaultValue method
  3850. *
  3851. * Test default value setting.
  3852. */
  3853. public function testTextDefaultValue(): void
  3854. {
  3855. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  3856. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3857. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3858. $this->assertHtml($expected, $result);
  3859. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  3860. $this->Form->create();
  3861. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3862. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3863. $this->assertHtml($expected, $result);
  3864. $Articles = $this->getTableLocator()->get('Articles');
  3865. $title = $Articles->getSchema()->getColumn('title');
  3866. $Articles->getSchema()->addColumn(
  3867. 'title',
  3868. ['default' => 'default title', 'length' => 255] + $title
  3869. );
  3870. $entity = $Articles->newEmptyEntity();
  3871. $this->Form->create($entity);
  3872. // Get default value from schema
  3873. $result = $this->Form->text('title');
  3874. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title', 'maxlength' => '255']];
  3875. $this->assertHtml($expected, $result);
  3876. // Don't get value from schema
  3877. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3878. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3879. $this->assertHtml($expected, $result);
  3880. // Custom default value overrides default value from schema
  3881. $result = $this->Form->text('title', ['default' => 'override default']);
  3882. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default', 'maxlength' => '255']];
  3883. $this->assertHtml($expected, $result);
  3884. // Default value from schema is used only for new entities.
  3885. $entity->setNew(false);
  3886. $result = $this->Form->text('title');
  3887. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3888. $this->assertHtml($expected, $result);
  3889. }
  3890. /**
  3891. * testError method
  3892. *
  3893. * Test field error generation.
  3894. */
  3895. public function testError(): void
  3896. {
  3897. $this->article['errors'] = [
  3898. 'Article' => ['field' => 'email'],
  3899. ];
  3900. $this->Form->create($this->article);
  3901. $result = $this->Form->error('Article.field');
  3902. $expected = [
  3903. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3904. 'email',
  3905. '/div',
  3906. ];
  3907. $this->assertHtml($expected, $result);
  3908. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  3909. $expected = [
  3910. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3911. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3912. '/div',
  3913. ];
  3914. $this->assertHtml($expected, $result);
  3915. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  3916. $expected = [
  3917. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3918. '<strong', 'Badness!', '/strong',
  3919. '/div',
  3920. ];
  3921. $this->assertHtml($expected, $result);
  3922. }
  3923. /**
  3924. * testErrorRuleName method
  3925. *
  3926. * Test error translation can use rule names for translating.
  3927. */
  3928. public function testErrorRuleName(): void
  3929. {
  3930. $this->article['errors'] = [
  3931. 'Article' => [
  3932. 'field' => ['email' => 'Your email was not good'],
  3933. ],
  3934. ];
  3935. $this->Form->create($this->article);
  3936. $result = $this->Form->error('Article.field');
  3937. $expected = [
  3938. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3939. 'Your email was not good',
  3940. '/div',
  3941. ];
  3942. $this->assertHtml($expected, $result);
  3943. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  3944. $expected = [
  3945. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3946. 'Email in use',
  3947. '/div',
  3948. ];
  3949. $this->assertHtml($expected, $result);
  3950. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  3951. $expected = [
  3952. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3953. 'Email in use',
  3954. '/div',
  3955. ];
  3956. $this->assertHtml($expected, $result);
  3957. $result = $this->Form->error('Article.field', [
  3958. 'email' => 'Key is preferred',
  3959. 'Your email was not good' => 'Email in use',
  3960. ]);
  3961. $expected = [
  3962. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3963. 'Key is preferred',
  3964. '/div',
  3965. ];
  3966. $this->assertHtml($expected, $result);
  3967. }
  3968. /**
  3969. * testErrorMessages method
  3970. *
  3971. * Test error with nested lists.
  3972. */
  3973. public function testErrorMessages(): void
  3974. {
  3975. $this->article['errors'] = [
  3976. 'Article' => ['field' => 'email'],
  3977. ];
  3978. $this->Form->create($this->article);
  3979. $result = $this->Form->error('Article.field', [
  3980. 'email' => 'No good!',
  3981. ]);
  3982. $expected = [
  3983. 'div' => ['class' => 'error-message', 'id' => 'article-field-error'],
  3984. 'No good!',
  3985. '/div',
  3986. ];
  3987. $this->assertHtml($expected, $result);
  3988. }
  3989. /**
  3990. * testErrorMultipleMessages method
  3991. *
  3992. * Test error() with multiple messages.
  3993. */
  3994. public function testErrorMultipleMessages(): void
  3995. {
  3996. $this->article['errors'] = [
  3997. 'field' => ['notBlank', 'email', 'Something else'],
  3998. ];
  3999. $this->Form->create($this->article);
  4000. $result = $this->Form->error('field', [
  4001. 'notBlank' => 'Cannot be empty',
  4002. 'email' => 'No good!',
  4003. ]);
  4004. $expected = [
  4005. 'div' => ['class' => 'error-message', 'id' => 'field-error'],
  4006. 'ul' => [],
  4007. '<li', 'Cannot be empty', '/li',
  4008. '<li', 'No good!', '/li',
  4009. '<li', 'Something else', '/li',
  4010. '/ul',
  4011. '/div',
  4012. ];
  4013. $this->assertHtml($expected, $result);
  4014. }
  4015. /**
  4016. * testPassword method
  4017. *
  4018. * Test password element generation.
  4019. */
  4020. public function testPassword(): void
  4021. {
  4022. $this->article['errors'] = [
  4023. 'Contact' => [
  4024. 'passwd' => 1,
  4025. ],
  4026. ];
  4027. $this->Form->create($this->article);
  4028. $result = $this->Form->password('Contact.field');
  4029. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4030. $this->assertHtml($expected, $result);
  4031. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4032. $this->Form->create($this->article);
  4033. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4034. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4035. $this->assertHtml($expected, $result);
  4036. }
  4037. /**
  4038. * testRadio method
  4039. *
  4040. * Test radio element set generation.
  4041. */
  4042. public function testRadio(): void
  4043. {
  4044. $result = $this->Form->radio('Model.field', ['option A']);
  4045. $expected = [
  4046. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4047. 'label' => ['for' => 'model-field-0'],
  4048. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4049. 'option A',
  4050. '/label',
  4051. ];
  4052. $this->assertHtml($expected, $result);
  4053. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4054. $this->assertHtml($expected, $result);
  4055. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4056. $expected = [
  4057. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4058. ['label' => ['for' => 'model-field-0']],
  4059. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4060. 'option A',
  4061. '/label',
  4062. ['label' => ['for' => 'model-field-1']],
  4063. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4064. 'option B',
  4065. '/label',
  4066. ];
  4067. $this->assertHtml($expected, $result);
  4068. $result = $this->Form->radio(
  4069. 'Employee.gender',
  4070. ['male' => 'Male', 'female' => 'Female'],
  4071. ['form' => 'my-form']
  4072. );
  4073. $expected = [
  4074. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form', 'id' => 'employee-gender'],
  4075. ['label' => ['for' => 'employee-gender-male']],
  4076. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4077. 'Male',
  4078. '/label',
  4079. ['label' => ['for' => 'employee-gender-female']],
  4080. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4081. 'Female',
  4082. '/label',
  4083. ];
  4084. $this->assertHtml($expected, $result);
  4085. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4086. $expected = [
  4087. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '', 'id' => 'model-field']],
  4088. ['label' => ['for' => 'model-custom-0']],
  4089. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4090. 'option A',
  4091. '/label',
  4092. ['label' => ['for' => 'model-custom-1']],
  4093. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4094. 'option B',
  4095. '/label',
  4096. ];
  4097. $this->assertHtml($expected, $result);
  4098. $result = $this->Form->radio(
  4099. 'Employee.gender',
  4100. [
  4101. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4102. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4103. ]
  4104. );
  4105. $expected = [
  4106. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'id' => 'employee-gender'],
  4107. ['label' => ['for' => 'employee-gender-male']],
  4108. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4109. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4110. 'Male',
  4111. '/label',
  4112. ['label' => ['for' => 'employee-gender-female']],
  4113. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4114. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4115. 'Female',
  4116. '/label',
  4117. ];
  4118. $this->assertHtml($expected, $result);
  4119. }
  4120. /**
  4121. * Test radio with complex options and empty disabled data.
  4122. */
  4123. public function testRadioComplexDisabled(): void
  4124. {
  4125. $options = [
  4126. ['value' => 'r', 'text' => 'red'],
  4127. ['value' => 'b', 'text' => 'blue'],
  4128. ];
  4129. $attrs = ['disabled' => []];
  4130. $result = $this->Form->radio('Model.field', $options, $attrs);
  4131. $expected = [
  4132. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4133. ['label' => ['for' => 'model-field-r']],
  4134. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4135. 'red',
  4136. '/label',
  4137. ['label' => ['for' => 'model-field-b']],
  4138. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4139. 'blue',
  4140. '/label',
  4141. ];
  4142. $this->assertHtml($expected, $result);
  4143. $attrs = ['disabled' => ['r']];
  4144. $result = $this->Form->radio('Model.field', $options, $attrs);
  4145. $this->assertStringContainsString('disabled="disabled"', $result);
  4146. }
  4147. /**
  4148. * testRadioDefaultValue method
  4149. *
  4150. * Test default value setting on radio() method.
  4151. */
  4152. public function testRadioDefaultValue(): void
  4153. {
  4154. $Articles = $this->getTableLocator()->get('Articles');
  4155. $title = $Articles->getSchema()->getColumn('title');
  4156. $Articles->getSchema()->addColumn(
  4157. 'title',
  4158. ['default' => '1'] + $title
  4159. );
  4160. $this->Form->create($Articles->newEmptyEntity());
  4161. $result = $this->Form->radio('title', ['option A', 'option B']);
  4162. $expected = [
  4163. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4164. ['label' => ['for' => 'title-0']],
  4165. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4166. 'option A',
  4167. '/label',
  4168. ['label' => ['for' => 'title-1']],
  4169. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4170. 'option B',
  4171. '/label',
  4172. ];
  4173. $this->assertHtml($expected, $result);
  4174. }
  4175. /**
  4176. * Test setting a hiddenField value on radio buttons.
  4177. */
  4178. public function testRadioHiddenFieldValue(): void
  4179. {
  4180. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4181. $expected = [
  4182. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N', 'id' => 'title']],
  4183. 'label' => ['for' => 'title-0'],
  4184. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4185. 'option A',
  4186. '/label',
  4187. ];
  4188. $this->assertHtml($expected, $result);
  4189. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => '']);
  4190. $expected = [
  4191. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4192. 'label' => ['for' => 'title-0'],
  4193. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4194. 'option A',
  4195. '/label',
  4196. ];
  4197. $this->assertHtml($expected, $result);
  4198. }
  4199. /**
  4200. * testControlRadio method
  4201. *
  4202. * Test that input works with radio types.
  4203. */
  4204. public function testControlRadio(): void
  4205. {
  4206. $result = $this->Form->control('test', [
  4207. 'type' => 'radio',
  4208. 'options' => ['A', 'B'],
  4209. ]);
  4210. $expected = [
  4211. ['div' => ['class' => 'input radio']],
  4212. '<label',
  4213. 'Test',
  4214. '/label',
  4215. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4216. ['label' => ['for' => 'test-0']],
  4217. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4218. 'A',
  4219. '/label',
  4220. ['label' => ['for' => 'test-1']],
  4221. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4222. 'B',
  4223. '/label',
  4224. '/div',
  4225. ];
  4226. $this->assertHtml($expected, $result);
  4227. $result = $this->Form->control('test', [
  4228. 'type' => 'radio',
  4229. 'options' => ['A', 'B'],
  4230. 'value' => '0',
  4231. ]);
  4232. $expected = [
  4233. ['div' => ['class' => 'input radio']],
  4234. '<label',
  4235. 'Test',
  4236. '/label',
  4237. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4238. ['label' => ['for' => 'test-0']],
  4239. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4240. 'A',
  4241. '/label',
  4242. ['label' => ['for' => 'test-1']],
  4243. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4244. 'B',
  4245. '/label',
  4246. '/div',
  4247. ];
  4248. $this->assertHtml($expected, $result);
  4249. $result = $this->Form->control('test', [
  4250. 'type' => 'radio',
  4251. 'options' => ['A', 'B'],
  4252. 'label' => false,
  4253. ]);
  4254. $expected = [
  4255. ['div' => ['class' => 'input radio']],
  4256. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4257. ['label' => ['for' => 'test-0']],
  4258. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4259. 'A',
  4260. '/label',
  4261. ['label' => ['for' => 'test-1']],
  4262. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4263. 'B',
  4264. '/label',
  4265. '/div',
  4266. ];
  4267. $this->assertHtml($expected, $result);
  4268. $result = $this->Form->control('accept', [
  4269. 'type' => 'radio',
  4270. 'options' => [
  4271. 1 => 'positive',
  4272. -1 => 'negative',
  4273. ],
  4274. 'label' => false,
  4275. ]);
  4276. $expected = [
  4277. ['div' => ['class' => 'input radio']],
  4278. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => '', 'id' => 'accept']],
  4279. ['label' => ['for' => 'accept-1']],
  4280. ['input' => [
  4281. 'type' => 'radio',
  4282. 'name' => 'accept',
  4283. 'value' => '1',
  4284. 'id' => 'accept-1',
  4285. ]],
  4286. 'positive',
  4287. '/label',
  4288. ['label' => ['for' => 'accept--1']],
  4289. ['input' => [
  4290. 'type' => 'radio',
  4291. 'name' => 'accept',
  4292. 'value' => '-1',
  4293. 'id' => 'accept--1',
  4294. ]],
  4295. 'negative',
  4296. '/label',
  4297. '/div',
  4298. ];
  4299. $this->assertHtml($expected, $result);
  4300. }
  4301. /**
  4302. * testRadioNoLabel method
  4303. *
  4304. * Test that radio() works with label = false.
  4305. */
  4306. public function testRadioNoLabel(): void
  4307. {
  4308. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4309. $expected = [
  4310. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4311. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4312. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4313. ];
  4314. $this->assertHtml($expected, $result);
  4315. }
  4316. /**
  4317. * testRadioControlInsideLabel method
  4318. *
  4319. * Test generating radio input inside label ala twitter bootstrap.
  4320. */
  4321. public function testRadioControlInsideLabel(): void
  4322. {
  4323. $this->Form->setTemplates([
  4324. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4325. 'radioWrapper' => '{{label}}',
  4326. ]);
  4327. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4328. // phpcs:disable
  4329. $expected = [
  4330. ['input' => [
  4331. 'type' => 'hidden',
  4332. 'name' => 'Model[field]',
  4333. 'value' => '',
  4334. 'id' => 'model-field'
  4335. ]],
  4336. ['label' => ['for' => 'model-field-0']],
  4337. ['input' => [
  4338. 'type' => 'radio',
  4339. 'name' => 'Model[field]',
  4340. 'value' => '0',
  4341. 'id' => 'model-field-0'
  4342. ]],
  4343. 'option A',
  4344. '/label',
  4345. ['label' => ['for' => 'model-field-1']],
  4346. ['input' => [
  4347. 'type' => 'radio',
  4348. 'name' => 'Model[field]',
  4349. 'value' => '1',
  4350. 'id' => 'model-field-1'
  4351. ]],
  4352. 'option B',
  4353. '/label',
  4354. ];
  4355. // phpcs:enable
  4356. $this->assertHtml($expected, $result);
  4357. }
  4358. /**
  4359. * testRadioHiddenControlDisabling method
  4360. *
  4361. * Test disabling the hidden input for radio buttons.
  4362. */
  4363. public function testRadioHiddenControlDisabling(): void
  4364. {
  4365. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4366. $expected = [
  4367. 'label' => ['for' => 'model-1-field-0'],
  4368. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4369. 'option A',
  4370. '/label',
  4371. ];
  4372. $this->assertHtml($expected, $result);
  4373. }
  4374. /**
  4375. * testRadioOutOfRange method
  4376. *
  4377. * Test radio element set generation.
  4378. */
  4379. public function testRadioOutOfRange(): void
  4380. {
  4381. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4382. $expected = [
  4383. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4384. 'label' => ['for' => 'model-field-v'],
  4385. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4386. 'value',
  4387. '/label',
  4388. ];
  4389. $this->assertHtml($expected, $result);
  4390. }
  4391. /**
  4392. * testSelect method
  4393. *
  4394. * Test select element generation.
  4395. */
  4396. public function testSelect(): void
  4397. {
  4398. $result = $this->Form->select('Model.field', []);
  4399. $expected = [
  4400. 'select' => ['name' => 'Model[field]'],
  4401. '/select',
  4402. ];
  4403. $this->assertHtml($expected, $result);
  4404. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4405. $this->Form->create();
  4406. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4407. $expected = [
  4408. 'select' => ['name' => 'Model[field]'],
  4409. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4410. 'good',
  4411. '/option',
  4412. ['option' => ['value' => 'other']],
  4413. 'bad',
  4414. '/option',
  4415. '/select',
  4416. ];
  4417. $this->assertHtml($expected, $result);
  4418. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4419. $this->assertHtml($expected, $result);
  4420. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4421. $this->Form->create();
  4422. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4423. $expected = [
  4424. 'select' => ['name' => 'Model[field]'],
  4425. ['option' => ['value' => 'value']],
  4426. 'good',
  4427. '/option',
  4428. ['option' => ['value' => 'other']],
  4429. 'bad',
  4430. '/option',
  4431. '/select',
  4432. ];
  4433. $this->assertHtml($expected, $result);
  4434. $options = [
  4435. ['value' => 'first', 'text' => 'First'],
  4436. ['value' => 'first', 'text' => 'Another First'],
  4437. ];
  4438. $result = $this->Form->select(
  4439. 'Model.field',
  4440. $options,
  4441. ['escape' => false, 'empty' => false]
  4442. );
  4443. $expected = [
  4444. 'select' => ['name' => 'Model[field]'],
  4445. ['option' => ['value' => 'first']],
  4446. 'First',
  4447. '/option',
  4448. ['option' => ['value' => 'first']],
  4449. 'Another First',
  4450. '/option',
  4451. '/select',
  4452. ];
  4453. $this->assertHtml($expected, $result);
  4454. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4455. $this->Form->create();
  4456. $result = $this->Form->select(
  4457. 'Model.contact_id',
  4458. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4459. ['escape' => false, 'empty' => 'pick something']
  4460. );
  4461. $expected = [
  4462. 'select' => ['name' => 'Model[contact_id]'],
  4463. ['option' => ['value' => '']], 'pick something', '/option',
  4464. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4465. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4466. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4467. '/select',
  4468. ];
  4469. $this->assertHtml($expected, $result);
  4470. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4471. $this->Form->create();
  4472. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4473. $expected = [
  4474. 'select' => ['name' => 'Model[field]'],
  4475. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4476. ['option' => ['value' => '1']], 'Yes', '/option',
  4477. '/select',
  4478. ];
  4479. $this->assertHtml($expected, $result);
  4480. }
  4481. /**
  4482. * testSelectEscapeHtml method
  4483. *
  4484. * Test that select() escapes HTML.
  4485. */
  4486. public function testSelectEscapeHtml(): void
  4487. {
  4488. $result = $this->Form->select(
  4489. 'Model.field',
  4490. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4491. ['empty' => false]
  4492. );
  4493. $expected = [
  4494. 'select' => ['name' => 'Model[field]'],
  4495. ['option' => ['value' => 'first']],
  4496. 'first &quot;html&quot; &lt;chars&gt;',
  4497. '/option',
  4498. ['option' => ['value' => 'second']],
  4499. 'value',
  4500. '/option',
  4501. '/select',
  4502. ];
  4503. $this->assertHtml($expected, $result);
  4504. $result = $this->Form->select(
  4505. 'Model.field',
  4506. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4507. ['escape' => false, 'empty' => false]
  4508. );
  4509. $expected = [
  4510. 'select' => ['name' => 'Model[field]'],
  4511. ['option' => ['value' => 'first']],
  4512. 'first "html" <chars>',
  4513. '/option',
  4514. ['option' => ['value' => 'second']],
  4515. 'value',
  4516. '/option',
  4517. '/select',
  4518. ];
  4519. $this->assertHtml($expected, $result);
  4520. }
  4521. /**
  4522. * testSelectRequired method
  4523. *
  4524. * Test select() with required and disabled attributes.
  4525. */
  4526. public function testSelectRequired(): void
  4527. {
  4528. $this->article['required'] = [
  4529. 'user_id' => true,
  4530. ];
  4531. $this->Form->create($this->article);
  4532. $result = $this->Form->select('user_id', ['option A']);
  4533. $expected = [
  4534. 'select' => [
  4535. 'name' => 'user_id',
  4536. 'required' => 'required',
  4537. ],
  4538. ['option' => ['value' => '0']], 'option A', '/option',
  4539. '/select',
  4540. ];
  4541. $this->assertHtml($expected, $result);
  4542. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4543. $expected = [
  4544. 'select' => [
  4545. 'name' => 'user_id',
  4546. 'disabled' => 'disabled',
  4547. ],
  4548. ['option' => ['value' => '0']], 'option A', '/option',
  4549. '/select',
  4550. ];
  4551. $this->assertHtml($expected, $result);
  4552. }
  4553. public function testSelectEmptyWithRequiredFalse(): void
  4554. {
  4555. $Articles = $this->getTableLocator()->get('Articles');
  4556. $validator = $Articles->getValidator('default');
  4557. $validator->allowEmptyString('user_id');
  4558. $Articles->setValidator('default', $validator);
  4559. $entity = $Articles->newEmptyEntity();
  4560. $this->Form->create($entity);
  4561. $result = $this->Form->select('user_id', ['option A']);
  4562. $expected = [
  4563. 'select' => [
  4564. 'name' => 'user_id',
  4565. ],
  4566. ['option' => ['value' => '']], '/option',
  4567. ['option' => ['value' => '0']], 'option A', '/option',
  4568. '/select',
  4569. ];
  4570. $this->assertHtml($expected, $result);
  4571. }
  4572. /**
  4573. * testNestedSelect method
  4574. *
  4575. * Test select element generation with optgroups.
  4576. */
  4577. public function testNestedSelect(): void
  4578. {
  4579. $result = $this->Form->select(
  4580. 'Model.field',
  4581. [1 => 'One', 2 => 'Two', 'Three' => [
  4582. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4583. ]],
  4584. ['empty' => false]
  4585. );
  4586. $expected = [
  4587. 'select' => ['name' => 'Model[field]'],
  4588. ['option' => ['value' => 1]],
  4589. 'One',
  4590. '/option',
  4591. ['option' => ['value' => 2]],
  4592. 'Two',
  4593. '/option',
  4594. ['optgroup' => ['label' => 'Three']],
  4595. ['option' => ['value' => 3]],
  4596. 'Three',
  4597. '/option',
  4598. ['option' => ['value' => 4]],
  4599. 'Four',
  4600. '/option',
  4601. ['option' => ['value' => 5]],
  4602. 'Five',
  4603. '/option',
  4604. '/optgroup',
  4605. '/select',
  4606. ];
  4607. $this->assertHtml($expected, $result);
  4608. }
  4609. /**
  4610. * testSelectMultiple method
  4611. *
  4612. * Test generation of multiple select elements.
  4613. */
  4614. public function testSelectMultiple(): void
  4615. {
  4616. $options = ['first', 'second', 'third'];
  4617. $result = $this->Form->select(
  4618. 'Model.multi_field',
  4619. $options,
  4620. ['form' => 'my-form', 'multiple' => true]
  4621. );
  4622. $expected = [
  4623. 'input' => [
  4624. 'type' => 'hidden',
  4625. 'name' => 'Model[multi_field]',
  4626. 'value' => '',
  4627. 'form' => 'my-form',
  4628. ],
  4629. 'select' => [
  4630. 'name' => 'Model[multi_field][]',
  4631. 'multiple' => 'multiple',
  4632. 'form' => 'my-form',
  4633. ],
  4634. ['option' => ['value' => '0']],
  4635. 'first',
  4636. '/option',
  4637. ['option' => ['value' => '1']],
  4638. 'second',
  4639. '/option',
  4640. ['option' => ['value' => '2']],
  4641. 'third',
  4642. '/option',
  4643. '/select',
  4644. ];
  4645. $this->assertHtml($expected, $result);
  4646. $result = $this->Form->select(
  4647. 'Model.multi_field',
  4648. $options,
  4649. ['multiple' => 'multiple', 'form' => 'my-form']
  4650. );
  4651. $this->assertHtml($expected, $result);
  4652. $result = $this->Form->select(
  4653. 'Model.multi_field',
  4654. $options,
  4655. ['form' => 'my-form', 'multiple' => false]
  4656. );
  4657. $this->assertStringNotContainsString('multiple', $result);
  4658. }
  4659. /**
  4660. * testCheckboxZeroValue method
  4661. *
  4662. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4663. */
  4664. public function testCheckboxZeroValue(): void
  4665. {
  4666. $result = $this->Form->control('User.get_spam', [
  4667. 'type' => 'checkbox',
  4668. 'value' => '0',
  4669. 'hiddenField' => '1',
  4670. ]);
  4671. $expected = [
  4672. 'div' => ['class' => 'input checkbox'],
  4673. 'label' => ['for' => 'user-get-spam'],
  4674. ['input' => [
  4675. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4676. 'value' => '1',
  4677. ]],
  4678. ['input' => [
  4679. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4680. 'value' => '0', 'id' => 'user-get-spam',
  4681. ]],
  4682. 'Get Spam',
  4683. '/label',
  4684. '/div',
  4685. ];
  4686. $this->assertHtml($expected, $result);
  4687. $result = $this->Form->control('User.get_spam', [
  4688. 'type' => 'checkbox',
  4689. 'value' => '0',
  4690. 'hiddenField' => '',
  4691. ]);
  4692. $expected = [
  4693. 'div' => ['class' => 'input checkbox'],
  4694. 'label' => ['for' => 'user-get-spam'],
  4695. ['input' => [
  4696. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4697. 'value' => '',
  4698. ]],
  4699. ['input' => [
  4700. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4701. 'value' => '0', 'id' => 'user-get-spam',
  4702. ]],
  4703. 'Get Spam',
  4704. '/label',
  4705. '/div',
  4706. ];
  4707. $this->assertHtml($expected, $result);
  4708. }
  4709. /**
  4710. * testHabtmSelectBox method
  4711. *
  4712. * Test generation of habtm select boxes.
  4713. */
  4714. public function testHabtmSelectBox(): void
  4715. {
  4716. $options = [
  4717. 1 => 'blue',
  4718. 2 => 'red',
  4719. 3 => 'green',
  4720. ];
  4721. $tags = [
  4722. new Entity(['id' => 1, 'name' => 'blue']),
  4723. new Entity(['id' => 3, 'name' => 'green']),
  4724. ];
  4725. $article = new Article(['tags' => $tags]);
  4726. $this->Form->create($article);
  4727. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4728. $expected = [
  4729. 'div' => ['class' => 'input select'],
  4730. 'label' => ['for' => 'tags-ids'],
  4731. 'Tags',
  4732. '/label',
  4733. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4734. 'select' => [
  4735. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4736. 'multiple' => 'multiple',
  4737. ],
  4738. ['option' => ['value' => '1', 'selected' => 'selected']],
  4739. 'blue',
  4740. '/option',
  4741. ['option' => ['value' => '2']],
  4742. 'red',
  4743. '/option',
  4744. ['option' => ['value' => '3', 'selected' => 'selected']],
  4745. 'green',
  4746. '/option',
  4747. '/select',
  4748. '/div',
  4749. ];
  4750. $this->assertHtml($expected, $result);
  4751. // make sure only 50 is selected, and not 50f5c0cf
  4752. $options = [
  4753. '1' => 'blue',
  4754. '50f5c0cf' => 'red',
  4755. '50' => 'green',
  4756. ];
  4757. $tags = [
  4758. new Entity(['id' => 1, 'name' => 'blue']),
  4759. new Entity(['id' => 50, 'name' => 'green']),
  4760. ];
  4761. $article = new Article(['tags' => $tags]);
  4762. $this->Form->create($article);
  4763. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4764. $expected = [
  4765. 'div' => ['class' => 'input select'],
  4766. 'label' => ['for' => 'tags-ids'],
  4767. 'Tags',
  4768. '/label',
  4769. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4770. 'select' => [
  4771. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4772. 'multiple' => 'multiple',
  4773. ],
  4774. ['option' => ['value' => '1', 'selected' => 'selected']],
  4775. 'blue',
  4776. '/option',
  4777. ['option' => ['value' => '50f5c0cf']],
  4778. 'red',
  4779. '/option',
  4780. ['option' => ['value' => '50', 'selected' => 'selected']],
  4781. 'green',
  4782. '/option',
  4783. '/select',
  4784. '/div',
  4785. ];
  4786. $this->assertHtml($expected, $result);
  4787. $spacecraft = [
  4788. 1 => 'Orion',
  4789. 2 => 'Helios',
  4790. ];
  4791. $this->View->set('spacecraft', $spacecraft);
  4792. $this->Form->create();
  4793. $result = $this->Form->control('spacecraft._ids');
  4794. $expected = [
  4795. 'div' => ['class' => 'input select'],
  4796. 'label' => ['for' => 'spacecraft-ids'],
  4797. 'Spacecraft',
  4798. '/label',
  4799. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4800. 'select' => [
  4801. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4802. 'multiple' => 'multiple',
  4803. ],
  4804. ['option' => ['value' => '1']],
  4805. 'Orion',
  4806. '/option',
  4807. ['option' => ['value' => '2']],
  4808. 'Helios',
  4809. '/option',
  4810. '/select',
  4811. '/div',
  4812. ];
  4813. $this->assertHtml($expected, $result);
  4814. }
  4815. /**
  4816. * testErrorsForBelongsToManySelect method
  4817. *
  4818. * Tests that errors for belongsToMany select fields are being
  4819. * picked up properly.
  4820. */
  4821. public function testErrorsForBelongsToManySelect(): void
  4822. {
  4823. $spacecraft = [
  4824. 1 => 'Orion',
  4825. 2 => 'Helios',
  4826. ];
  4827. $this->View->set('spacecraft', $spacecraft);
  4828. $article = new Article();
  4829. $article->setError('spacecraft', ['Invalid']);
  4830. $this->Form->create($article);
  4831. $result = $this->Form->control('spacecraft._ids');
  4832. $expected = [
  4833. ['div' => ['class' => 'input select error']],
  4834. 'label' => ['for' => 'spacecraft-ids'],
  4835. 'Spacecraft',
  4836. '/label',
  4837. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4838. 'select' => [
  4839. 'name' => 'spacecraft[_ids][]',
  4840. 'id' => 'spacecraft-ids',
  4841. 'multiple' => 'multiple',
  4842. ],
  4843. ['option' => ['value' => '1']],
  4844. 'Orion',
  4845. '/option',
  4846. ['option' => ['value' => '2']],
  4847. 'Helios',
  4848. '/option',
  4849. '/select',
  4850. ['div' => ['class' => 'error-message', 'id' => 'spacecraft-error']],
  4851. 'Invalid',
  4852. '/div',
  4853. '/div',
  4854. ];
  4855. $this->assertHtml($expected, $result);
  4856. }
  4857. /**
  4858. * testSelectMultipleCheckboxes method
  4859. *
  4860. * Test generation of multi select elements in checkbox format.
  4861. */
  4862. public function testSelectMultipleCheckboxes(): void
  4863. {
  4864. $result = $this->Form->select(
  4865. 'Model.multi_field',
  4866. ['first', 'second', 'third'],
  4867. ['multiple' => 'checkbox']
  4868. );
  4869. $expected = [
  4870. 'input' => [
  4871. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  4872. ],
  4873. ['div' => ['class' => 'checkbox']],
  4874. ['label' => ['for' => 'model-multi-field-0']],
  4875. ['input' => [
  4876. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4877. 'value' => '0', 'id' => 'model-multi-field-0',
  4878. ]],
  4879. 'first',
  4880. '/label',
  4881. '/div',
  4882. ['div' => ['class' => 'checkbox']],
  4883. ['label' => ['for' => 'model-multi-field-1']],
  4884. ['input' => [
  4885. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4886. 'value' => '1', 'id' => 'model-multi-field-1',
  4887. ]],
  4888. 'second',
  4889. '/label',
  4890. '/div',
  4891. ['div' => ['class' => 'checkbox']],
  4892. ['label' => ['for' => 'model-multi-field-2']],
  4893. ['input' => [
  4894. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4895. 'value' => '2', 'id' => 'model-multi-field-2',
  4896. ]],
  4897. 'third',
  4898. '/label',
  4899. '/div',
  4900. ];
  4901. $this->assertHtml($expected, $result);
  4902. $result = $this->Form->select(
  4903. 'Model.multi_field',
  4904. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4905. ['multiple' => 'checkbox']
  4906. );
  4907. $expected = [
  4908. 'input' => [
  4909. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  4910. ],
  4911. ['div' => ['class' => 'checkbox']],
  4912. ['label' => ['for' => 'model-multi-field-a+']],
  4913. ['input' => [
  4914. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4915. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  4916. ]],
  4917. 'first',
  4918. '/label',
  4919. '/div',
  4920. ['div' => ['class' => 'checkbox']],
  4921. ['label' => ['for' => 'model-multi-field-a++']],
  4922. ['input' => [
  4923. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4924. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  4925. ]],
  4926. 'second',
  4927. '/label',
  4928. '/div',
  4929. ['div' => ['class' => 'checkbox']],
  4930. ['label' => ['for' => 'model-multi-field-a+++']],
  4931. ['input' => [
  4932. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4933. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  4934. ]],
  4935. 'third',
  4936. '/label',
  4937. '/div',
  4938. ];
  4939. $this->assertHtml($expected, $result);
  4940. $result = $this->Form->select(
  4941. 'Model.multi_field',
  4942. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4943. ['multiple' => 'checkbox']
  4944. );
  4945. $expected = [
  4946. 'input' => [
  4947. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  4948. ],
  4949. ['div' => ['class' => 'checkbox']],
  4950. ['label' => ['for' => 'model-multi-field-a-b']],
  4951. ['input' => [
  4952. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4953. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  4954. ]],
  4955. 'first',
  4956. '/label',
  4957. '/div',
  4958. ['div' => ['class' => 'checkbox']],
  4959. ['label' => ['for' => 'model-multi-field-a-b1']],
  4960. ['input' => [
  4961. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4962. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  4963. ]],
  4964. 'second',
  4965. '/label',
  4966. '/div',
  4967. ['div' => ['class' => 'checkbox']],
  4968. ['label' => ['for' => 'model-multi-field-a-b2']],
  4969. ['input' => [
  4970. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4971. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  4972. ]],
  4973. 'third',
  4974. '/label',
  4975. '/div',
  4976. ];
  4977. $this->assertHtml($expected, $result);
  4978. }
  4979. /**
  4980. * testSelectMultipleCheckboxRequestData method
  4981. *
  4982. * Ensure that multiCheckbox reads from the request data.
  4983. */
  4984. public function testSelectMultipleCheckboxRequestData(): void
  4985. {
  4986. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  4987. $result = $this->Form->select(
  4988. 'Model.tags',
  4989. ['1' => 'first', 'Array' => 'Array'],
  4990. ['multiple' => 'checkbox']
  4991. );
  4992. $expected = [
  4993. 'input' => [
  4994. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'model-tags',
  4995. ],
  4996. ['div' => ['class' => 'checkbox']],
  4997. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  4998. ['input' => [
  4999. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5000. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5001. ]],
  5002. 'first',
  5003. '/label',
  5004. '/div',
  5005. ['div' => ['class' => 'checkbox']],
  5006. ['label' => ['for' => 'model-tags-array']],
  5007. ['input' => [
  5008. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5009. 'value' => 'Array', 'id' => 'model-tags-array',
  5010. ]],
  5011. 'Array',
  5012. '/label',
  5013. '/div',
  5014. ];
  5015. $this->assertHtml($expected, $result);
  5016. }
  5017. /**
  5018. * testSelectMultipleCheckboxSecurity method
  5019. *
  5020. * Checks the security hash array generated for multiple-input checkbox elements.
  5021. */
  5022. public function testSelectMultipleCheckboxSecurity(): void
  5023. {
  5024. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5025. $this->Form->create();
  5026. $this->Form->select(
  5027. 'Model.multi_field',
  5028. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5029. ['multiple' => 'checkbox']
  5030. );
  5031. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5032. $this->assertEquals(['Model.multi_field'], $fields);
  5033. $result = $this->Form->secure();
  5034. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  5035. $hash = urlencode($hash . ':');
  5036. $this->assertStringContainsString('"' . $hash . '"', $result);
  5037. }
  5038. /**
  5039. * testSelectMultipleSecureWithNoOptions method
  5040. *
  5041. * Multiple select elements should always be secured as they always participate
  5042. * in the POST data.
  5043. */
  5044. public function testSelectMultipleSecureWithNoOptions(): void
  5045. {
  5046. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5047. $this->Form->create();
  5048. $this->Form->select(
  5049. 'Model.select',
  5050. [],
  5051. ['multiple' => true]
  5052. );
  5053. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5054. $this->assertEquals(['Model.select'], $result);
  5055. }
  5056. /**
  5057. * testSelectNoSecureWithNoOptions method
  5058. *
  5059. * When a select box has no options it should not be added to the fields list
  5060. * as it always fail post validation.
  5061. */
  5062. public function testSelectNoSecureWithNoOptions(): void
  5063. {
  5064. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5065. $this->Form->create();
  5066. $this->Form->select(
  5067. 'Model.select',
  5068. []
  5069. );
  5070. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5071. $this->assertEquals([], $result);
  5072. $this->Form->select(
  5073. 'Model.user_id',
  5074. [],
  5075. ['empty' => true]
  5076. );
  5077. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5078. $this->assertEquals(['Model.user_id'], $result);
  5079. }
  5080. /**
  5081. * testControlMultipleCheckboxes method
  5082. *
  5083. * Test control() resulting in multi select elements being generated.
  5084. */
  5085. public function testControlMultipleCheckboxes(): void
  5086. {
  5087. $result = $this->Form->control('Model.multi_field', [
  5088. 'options' => ['first', 'second', 'third'],
  5089. 'multiple' => 'checkbox',
  5090. ]);
  5091. $expected = [
  5092. ['div' => ['class' => 'input select']],
  5093. ['label' => ['for' => 'model-multi-field']],
  5094. 'Multi Field',
  5095. '/label',
  5096. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5097. ['div' => ['class' => 'checkbox']],
  5098. ['label' => ['for' => 'model-multi-field-0']],
  5099. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5100. 'first',
  5101. '/label',
  5102. '/div',
  5103. ['div' => ['class' => 'checkbox']],
  5104. ['label' => ['for' => 'model-multi-field-1']],
  5105. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5106. 'second',
  5107. '/label',
  5108. '/div',
  5109. ['div' => ['class' => 'checkbox']],
  5110. ['label' => ['for' => 'model-multi-field-2']],
  5111. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5112. 'third',
  5113. '/label',
  5114. '/div',
  5115. '/div',
  5116. ];
  5117. $this->assertHtml($expected, $result);
  5118. $result = $this->Form->control('Model.multi_field', [
  5119. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5120. 'multiple' => 'checkbox',
  5121. ]);
  5122. $expected = [
  5123. ['div' => ['class' => 'input select']],
  5124. ['label' => ['for' => 'model-multi-field']],
  5125. 'Multi Field',
  5126. '/label',
  5127. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5128. ['div' => ['class' => 'checkbox']],
  5129. ['label' => ['for' => 'model-multi-field-a']],
  5130. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5131. 'first',
  5132. '/label',
  5133. '/div',
  5134. ['div' => ['class' => 'checkbox']],
  5135. ['label' => ['for' => 'model-multi-field-b']],
  5136. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5137. 'second',
  5138. '/label',
  5139. '/div',
  5140. ['div' => ['class' => 'checkbox']],
  5141. ['label' => ['for' => 'model-multi-field-c']],
  5142. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5143. 'third',
  5144. '/label',
  5145. '/div',
  5146. '/div',
  5147. ];
  5148. $this->assertHtml($expected, $result);
  5149. }
  5150. /**
  5151. * testSelectHiddenFieldOmission method
  5152. *
  5153. * Test that select() with 'hiddenField' => false omits the hidden field.
  5154. */
  5155. public function testSelectHiddenFieldOmission(): void
  5156. {
  5157. $result = $this->Form->select(
  5158. 'Model.multi_field',
  5159. ['first', 'second'],
  5160. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5161. );
  5162. $this->assertStringNotContainsString('type="hidden"', $result);
  5163. }
  5164. /**
  5165. * testSelectCheckboxMultipleOverrideName method
  5166. *
  5167. * Test that select() with multiple = checkbox works with overriding name attribute.
  5168. */
  5169. public function testSelectCheckboxMultipleOverrideName(): void
  5170. {
  5171. $result = $this->Form->select('category', ['1', '2'], [
  5172. 'multiple' => 'checkbox',
  5173. 'name' => 'fish',
  5174. ]);
  5175. $expected = [
  5176. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  5177. ['div' => ['class' => 'checkbox']],
  5178. ['label' => ['for' => 'fish-0']],
  5179. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5180. '1',
  5181. '/label',
  5182. '/div',
  5183. ['div' => ['class' => 'checkbox']],
  5184. ['label' => ['for' => 'fish-1']],
  5185. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5186. '2',
  5187. '/label',
  5188. '/div',
  5189. ];
  5190. $this->assertHtml($expected, $result);
  5191. $result = $this->Form->multiCheckbox(
  5192. 'category',
  5193. new Collection(['1', '2']),
  5194. ['name' => 'fish']
  5195. );
  5196. $this->assertHtml($expected, $result);
  5197. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5198. 'name' => 'fish',
  5199. ]);
  5200. $this->assertHtml($expected, $result);
  5201. }
  5202. /**
  5203. * testControlMultiCheckbox method
  5204. *
  5205. * Test that control() works with multicheckbox.
  5206. */
  5207. public function testControlMultiCheckbox(): void
  5208. {
  5209. $result = $this->Form->control('category', [
  5210. 'type' => 'multicheckbox',
  5211. 'options' => ['1', '2'],
  5212. ]);
  5213. $expected = [
  5214. ['div' => ['class' => 'input multicheckbox']],
  5215. '<label',
  5216. 'Category',
  5217. '/label',
  5218. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'category'],
  5219. ['div' => ['class' => 'checkbox']],
  5220. ['label' => ['for' => 'category-0']],
  5221. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5222. '1',
  5223. '/label',
  5224. '/div',
  5225. ['div' => ['class' => 'checkbox']],
  5226. ['label' => ['for' => 'category-1']],
  5227. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5228. '2',
  5229. '/label',
  5230. '/div',
  5231. '/div',
  5232. ];
  5233. $this->assertHtml($expected, $result);
  5234. }
  5235. /**
  5236. * testCheckbox method
  5237. *
  5238. * Test generation of checkboxes.
  5239. */
  5240. public function testCheckbox(): void
  5241. {
  5242. $result = $this->Form->checkbox('Model.field');
  5243. $expected = [
  5244. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5245. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5246. ];
  5247. $this->assertHtml($expected, $result);
  5248. $result = $this->Form->checkbox('Model.field', [
  5249. 'id' => 'theID',
  5250. 'value' => 'myvalue',
  5251. 'form' => 'my-form',
  5252. ]);
  5253. $expected = [
  5254. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5255. ['input' => [
  5256. 'type' => 'checkbox', 'name' => 'Model[field]',
  5257. 'value' => 'myvalue', 'id' => 'theID',
  5258. 'form' => 'my-form',
  5259. ]],
  5260. ];
  5261. $this->assertHtml($expected, $result);
  5262. }
  5263. /**
  5264. * testCheckboxDefaultValue method
  5265. *
  5266. * Test default value setting on checkbox() method.
  5267. */
  5268. public function testCheckboxDefaultValue(): void
  5269. {
  5270. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5271. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5272. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5273. $this->assertHtml($expected, $result);
  5274. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5275. $this->Form->create();
  5276. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5277. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5278. $this->assertHtml($expected, $result);
  5279. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5280. $this->Form->create();
  5281. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5282. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5283. $this->assertHtml($expected, $result);
  5284. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5285. $this->Form->create();
  5286. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5287. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5288. $this->assertHtml($expected, $result);
  5289. $Articles = $this->getTableLocator()->get('Articles');
  5290. $Articles->getSchema()->addColumn(
  5291. 'published',
  5292. ['type' => 'boolean', 'null' => false, 'default' => true]
  5293. );
  5294. $this->Form->create($Articles->newEmptyEntity());
  5295. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5296. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5297. $this->assertHtml($expected, $result);
  5298. }
  5299. /**
  5300. * testCheckboxCheckedAndError method
  5301. *
  5302. * Test checkbox being checked or having errors.
  5303. */
  5304. public function testCheckboxCheckedAndError(): void
  5305. {
  5306. $this->article['errors'] = [
  5307. 'published' => true,
  5308. ];
  5309. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5310. $this->Form->create($this->article);
  5311. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5312. $expected = [
  5313. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5314. ['input' => [
  5315. 'type' => 'checkbox',
  5316. 'name' => 'published',
  5317. 'value' => 'myvalue',
  5318. 'id' => 'theID',
  5319. 'checked' => 'checked',
  5320. 'class' => 'form-error',
  5321. ]],
  5322. ];
  5323. $this->assertHtml($expected, $result);
  5324. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5325. $this->Form->create($this->article);
  5326. $result = $this->Form->checkbox('published');
  5327. $expected = [
  5328. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5329. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5330. ];
  5331. $this->assertHtml($expected, $result);
  5332. }
  5333. /**
  5334. * testCheckboxCustomNameAttribute method
  5335. *
  5336. * Test checkbox() with a custom name attribute.
  5337. */
  5338. public function testCheckboxCustomNameAttribute(): void
  5339. {
  5340. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5341. $expected = [
  5342. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5343. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5344. ];
  5345. $this->assertHtml($expected, $result);
  5346. }
  5347. /**
  5348. * testCheckboxHiddenField method
  5349. *
  5350. * Test that the hidden input for checkboxes can be omitted or set to a
  5351. * specific value.
  5352. */
  5353. public function testCheckboxHiddenField(): void
  5354. {
  5355. $result = $this->Form->checkbox('UserForm.something', [
  5356. 'hiddenField' => false,
  5357. ]);
  5358. $expected = [
  5359. 'input' => [
  5360. 'type' => 'checkbox',
  5361. 'name' => 'UserForm[something]',
  5362. 'value' => '1',
  5363. ],
  5364. ];
  5365. $this->assertHtml($expected, $result);
  5366. $result = $this->Form->checkbox('UserForm.something', [
  5367. 'value' => 'Y',
  5368. 'hiddenField' => '',
  5369. ]);
  5370. $expected = [
  5371. ['input' => [
  5372. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5373. 'value' => '',
  5374. ]],
  5375. ['input' => [
  5376. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5377. 'value' => 'Y',
  5378. ]],
  5379. ];
  5380. $this->assertHtml($expected, $result);
  5381. $result = $this->Form->checkbox('UserForm.something', [
  5382. 'value' => 'Y',
  5383. 'hiddenField' => 'N',
  5384. ]);
  5385. $expected = [
  5386. ['input' => [
  5387. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5388. 'value' => 'N',
  5389. ]],
  5390. ['input' => [
  5391. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5392. 'value' => 'Y',
  5393. ]],
  5394. ];
  5395. $this->assertHtml($expected, $result);
  5396. }
  5397. /**
  5398. * testTime method
  5399. *
  5400. * Test the time type.
  5401. */
  5402. public function testTime(): void
  5403. {
  5404. $result = $this->Form->time('start_time', [
  5405. 'value' => '2014-03-08 16:30:00',
  5406. ]);
  5407. $expected = [
  5408. 'input' => [
  5409. 'type' => 'time',
  5410. 'name' => 'start_time',
  5411. 'value' => '16:30:00',
  5412. 'step' => '1',
  5413. ],
  5414. ];
  5415. $this->assertHtml($expected, $result);
  5416. }
  5417. /**
  5418. * testDate method
  5419. *
  5420. * Test the date type.
  5421. */
  5422. public function testDate(): void
  5423. {
  5424. $result = $this->Form->date('start_day', [
  5425. 'value' => '2014-03-08',
  5426. ]);
  5427. $expected = [
  5428. 'input' => [
  5429. 'type' => 'date',
  5430. 'name' => 'start_day',
  5431. 'value' => '2014-03-08',
  5432. ],
  5433. ];
  5434. $this->assertHtml($expected, $result);
  5435. $result = $this->Form->date('start_day', [
  5436. 'value' => new Date('2014-03-08'),
  5437. ]);
  5438. $this->assertHtml($expected, $result);
  5439. }
  5440. /**
  5441. * testDateTime method
  5442. */
  5443. public function testDateTime(): void
  5444. {
  5445. $result = $this->Form->dateTime('date', ['default' => true]);
  5446. $expected = [
  5447. 'input' => [
  5448. 'type' => 'datetime-local',
  5449. 'name' => 'date',
  5450. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  5451. 'step' => '1',
  5452. ],
  5453. ];
  5454. $this->assertHtml($expected, $result);
  5455. }
  5456. /**
  5457. * testDateTimeSecured method
  5458. *
  5459. * Test that datetime fields are added to protected fields list.
  5460. */
  5461. public function testDateTimeSecured(): void
  5462. {
  5463. $this->View->setRequest(
  5464. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5465. );
  5466. $this->Form->create();
  5467. $this->Form->dateTime('date');
  5468. $expected = ['date'];
  5469. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5470. $this->assertEquals($expected, $result);
  5471. $this->Form->date('published');
  5472. $expected = ['date', 'published'];
  5473. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5474. $this->assertEquals($expected, $result);
  5475. }
  5476. /**
  5477. * testDateTimeSecuredDisabled method
  5478. *
  5479. * Test that datetime fields are added to protected fields list.
  5480. */
  5481. public function testDateTimeSecuredDisabled(): void
  5482. {
  5483. $this->View->setRequest(
  5484. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5485. );
  5486. $this->Form->create();
  5487. $this->Form->dateTime('date', ['secure' => false]);
  5488. $expected = [];
  5489. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5490. $this->assertEquals($expected, $result);
  5491. $this->Form->date('published', ['secure' => false]);
  5492. $expected = [];
  5493. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5494. $this->assertEquals($expected, $result);
  5495. }
  5496. /**
  5497. * testDatetimeWithDefault method
  5498. *
  5499. * Test that datetime() and default values work.
  5500. */
  5501. public function testDatetimeWithDefault(): void
  5502. {
  5503. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5504. $expected = [
  5505. 'input' => [
  5506. 'type' => 'datetime-local',
  5507. 'name' => 'updated',
  5508. 'value' => '2009-06-01T11:15:30',
  5509. 'step' => '1',
  5510. ],
  5511. ];
  5512. $this->assertHtml($expected, $result);
  5513. $result = $this->Form->dateTime('updated', [
  5514. 'default' => '2009-06-01 11:15:30',
  5515. ]);
  5516. $expected = [
  5517. 'input' => [
  5518. 'type' => 'datetime-local',
  5519. 'name' => 'updated',
  5520. 'value' => '2009-06-01T11:15:30',
  5521. 'step' => '1',
  5522. ],
  5523. ];
  5524. $this->assertHtml($expected, $result);
  5525. }
  5526. /**
  5527. * testMonth method
  5528. *
  5529. * Test generation of a month input.
  5530. */
  5531. public function testMonth(): void
  5532. {
  5533. $result = $this->Form->month('field', ['value' => '']);
  5534. $expected = [
  5535. 'input' => [
  5536. 'type' => 'month',
  5537. 'name' => 'field',
  5538. 'value' => '',
  5539. ],
  5540. ];
  5541. $this->assertHtml($expected, $result);
  5542. $this->View->setRequest(
  5543. $this->View->getRequest()->withData('release', '2050-02-10')
  5544. );
  5545. $this->Form->create();
  5546. $result = $this->Form->month('release');
  5547. $expected = [
  5548. 'input' => [
  5549. 'type' => 'month',
  5550. 'name' => 'release',
  5551. 'value' => '2050-02',
  5552. ],
  5553. ];
  5554. $this->assertHtml($expected, $result);
  5555. $this->View->setRequest(
  5556. $this->View->getRequest()->withData('release', '2050-03')
  5557. );
  5558. $this->Form->create();
  5559. $result = $this->Form->month('release');
  5560. $expected = [
  5561. 'input' => [
  5562. 'type' => 'month',
  5563. 'name' => 'release',
  5564. 'value' => '2050-03',
  5565. ],
  5566. ];
  5567. $this->assertHtml($expected, $result);
  5568. }
  5569. /**
  5570. * testYear method
  5571. *
  5572. * Test generation of a year input.
  5573. */
  5574. public function testYear(): void
  5575. {
  5576. $this->View->setRequest(
  5577. $this->View->getRequest()->withData('published', '2006')
  5578. );
  5579. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5580. $expected = [
  5581. ['select' => ['name' => 'field']],
  5582. ['option' => ['selected' => 'selected', 'value' => '']],
  5583. '/option',
  5584. ['option' => ['value' => '2007']],
  5585. '2007',
  5586. '/option',
  5587. ['option' => ['value' => '2006']],
  5588. '2006',
  5589. '/option',
  5590. '/select',
  5591. ];
  5592. $this->assertHtml($expected, $result);
  5593. $result = $this->Form->year('field', [
  5594. 'value' => '',
  5595. 'min' => 2006,
  5596. 'max' => 2007,
  5597. 'order' => 'asc',
  5598. ]);
  5599. $expected = [
  5600. ['select' => ['name' => 'field']],
  5601. ['option' => ['selected' => 'selected', 'value' => '']],
  5602. '/option',
  5603. ['option' => ['value' => '2006']],
  5604. '2006',
  5605. '/option',
  5606. ['option' => ['value' => '2007']],
  5607. '2007',
  5608. '/option',
  5609. '/select',
  5610. ];
  5611. $this->assertHtml($expected, $result);
  5612. $result = $this->Form->year('published', [
  5613. 'empty' => false,
  5614. 'min' => 2006,
  5615. 'max' => 2007,
  5616. ]);
  5617. $expected = [
  5618. ['select' => ['name' => 'published']],
  5619. ['option' => ['value' => '2007']],
  5620. '2007',
  5621. '/option',
  5622. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5623. '2006',
  5624. '/option',
  5625. '/select',
  5626. ];
  5627. $this->assertHtml($expected, $result);
  5628. $result = $this->Form->year('published', [
  5629. 'empty' => false,
  5630. 'value' => new Date('2008-01-12'),
  5631. 'min' => 2007,
  5632. 'max' => 2009,
  5633. ]);
  5634. $expected = [
  5635. ['select' => ['name' => 'published']],
  5636. ['option' => ['value' => '2009']],
  5637. '2009',
  5638. '/option',
  5639. ['option' => ['value' => '2008', 'selected' => 'selected']],
  5640. '2008',
  5641. '/option',
  5642. ['option' => ['value' => '2007']],
  5643. '2007',
  5644. '/option',
  5645. '/select',
  5646. ];
  5647. $this->assertHtml($expected, $result);
  5648. $result = $this->Form->year('published', [
  5649. 'empty' => 'Published on',
  5650. ]);
  5651. $this->assertStringContainsString('Published on', $result);
  5652. }
  5653. /**
  5654. * testControlYearPreEpoch method
  5655. *
  5656. * Test minYear being prior to the unix epoch.
  5657. */
  5658. public function testControlYearPreEpoch(): void
  5659. {
  5660. $start = date('Y') - 80;
  5661. $end = date('Y') - 18;
  5662. $result = $this->Form->control('birth_year', [
  5663. 'type' => 'year',
  5664. 'label' => 'Birth Year',
  5665. 'min' => $start,
  5666. 'max' => $end,
  5667. ]);
  5668. $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
  5669. $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
  5670. $this->assertStringNotContainsString('value="00">00', $result);
  5671. }
  5672. /**
  5673. * test control() datetime & required attributes
  5674. */
  5675. public function testControlDatetimeRequired(): void
  5676. {
  5677. $result = $this->Form->control('birthday', [
  5678. 'type' => 'date',
  5679. 'required' => true,
  5680. ]);
  5681. $this->assertStringContainsString(
  5682. '<input type="date" name="birthday" required="required"',
  5683. $result
  5684. );
  5685. }
  5686. /**
  5687. * testYearAutoExpandRange method
  5688. */
  5689. public function testYearAutoExpandRange(): void
  5690. {
  5691. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5692. $result = $this->Form->year('birthday');
  5693. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5694. $result = $matches[1];
  5695. $expected = range(date('Y') + 5, 1930);
  5696. $this->assertEquals($expected, $result);
  5697. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5698. $this->Form->create();
  5699. $result = $this->Form->year('release');
  5700. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5701. $result = $matches[1];
  5702. $expected = range(2050, date('Y') - 5);
  5703. $this->assertEquals($expected, $result);
  5704. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5705. $this->Form->create();
  5706. $result = $this->Form->year('release', [
  5707. 'min' => 1890,
  5708. 'max' => 1900,
  5709. ]);
  5710. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5711. $result = $matches[1];
  5712. $expected = range(1900, 1881);
  5713. $this->assertEquals($expected, $result);
  5714. }
  5715. /**
  5716. * test control placeholder + label
  5717. */
  5718. public function testControlLabelAndPlaceholder(): void
  5719. {
  5720. $this->Form->create($this->article);
  5721. $result = $this->Form->control('title', ['label' => 'Title', 'placeholder' => 'Add title']);
  5722. $expected = [
  5723. 'div' => ['class' => 'input text required'],
  5724. 'label' => ['for' => 'title'],
  5725. 'Title',
  5726. '/label',
  5727. 'input' => [
  5728. 'aria-required' => 'true',
  5729. 'type' => 'text',
  5730. 'required' => 'required',
  5731. 'placeholder' => 'Add title',
  5732. 'id' => 'title',
  5733. 'name' => 'title',
  5734. 'data-validity-message' => 'This field cannot be left empty',
  5735. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5736. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5737. ],
  5738. '/div',
  5739. ];
  5740. $this->assertHtml($expected, $result);
  5741. }
  5742. /**
  5743. * testControlLabelFalse method
  5744. *
  5745. * Test the label option being set to false.
  5746. */
  5747. public function testControlLabelFalse(): void
  5748. {
  5749. $this->Form->create($this->article);
  5750. $result = $this->Form->control('title', ['label' => false]);
  5751. $expected = [
  5752. 'div' => ['class' => 'input text required'],
  5753. 'input' => [
  5754. 'aria-required' => 'true',
  5755. 'type' => 'text',
  5756. 'required' => 'required',
  5757. 'id' => 'title',
  5758. 'name' => 'title',
  5759. 'data-validity-message' => 'This field cannot be left empty',
  5760. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5761. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5762. ],
  5763. '/div',
  5764. ];
  5765. $this->assertHtml($expected, $result);
  5766. $this->Form->create($this->article);
  5767. $result = $this->Form->control('title', ['label' => false, 'placeholder' => 'Add title']);
  5768. $expected['input'] += [
  5769. 'placeholder' => 'Add title',
  5770. 'aria-label' => 'Add title',
  5771. ];
  5772. $this->assertHtml($expected, $result);
  5773. }
  5774. /**
  5775. * testTextArea method
  5776. *
  5777. * Test generation of a textarea input.
  5778. */
  5779. public function testTextArea(): void
  5780. {
  5781. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5782. $result = $this->Form->textarea('field');
  5783. $expected = [
  5784. 'textarea' => ['name' => 'field', 'rows' => 5],
  5785. 'some test data',
  5786. '/textarea',
  5787. ];
  5788. $this->assertHtml($expected, $result);
  5789. $result = $this->Form->textarea('user.bio');
  5790. $expected = [
  5791. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5792. '/textarea',
  5793. ];
  5794. $this->assertHtml($expected, $result);
  5795. $this->View->setRequest($this->View->getRequest()
  5796. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5797. $this->Form->create();
  5798. $result = $this->Form->textarea('field');
  5799. $expected = [
  5800. 'textarea' => ['name' => 'field', 'rows' => 5],
  5801. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5802. '/textarea',
  5803. ];
  5804. $this->assertHtml($expected, $result);
  5805. $this->View->setRequest($this->View->getRequest()->withData(
  5806. 'Model.field',
  5807. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  5808. ));
  5809. $this->Form->create();
  5810. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5811. $expected = [
  5812. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  5813. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5814. '/textarea',
  5815. ];
  5816. $this->assertHtml($expected, $result);
  5817. $result = $this->Form->textarea('0.OtherModel.field');
  5818. $expected = [
  5819. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  5820. '/textarea',
  5821. ];
  5822. $this->assertHtml($expected, $result);
  5823. }
  5824. /**
  5825. * testTextAreaWithStupidCharacters method
  5826. *
  5827. * Test text area with non-ascii characters.
  5828. */
  5829. public function testTextAreaWithStupidCharacters(): void
  5830. {
  5831. $result = $this->Form->textarea('Post.content', [
  5832. 'value' => 'GREAT®',
  5833. 'rows' => '15',
  5834. 'cols' => '75',
  5835. ]);
  5836. $expected = [
  5837. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5838. 'GREAT®',
  5839. '/textarea',
  5840. ];
  5841. $this->assertHtml($expected, $result);
  5842. }
  5843. /**
  5844. * testTextAreaMaxLength method
  5845. *
  5846. * Test textareas maxlength read from schema.
  5847. */
  5848. public function testTextAreaMaxLength(): void
  5849. {
  5850. $this->Form->create([
  5851. 'schema' => [
  5852. 'stuff' => ['type' => 'string', 'length' => 10],
  5853. ],
  5854. ]);
  5855. $result = $this->Form->control('other', ['type' => 'textarea']);
  5856. $expected = [
  5857. 'div' => ['class' => 'input textarea'],
  5858. 'label' => ['for' => 'other'],
  5859. 'Other',
  5860. '/label',
  5861. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  5862. '/textarea',
  5863. '/div',
  5864. ];
  5865. $this->assertHtml($expected, $result);
  5866. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  5867. $expected = [
  5868. 'div' => ['class' => 'input textarea'],
  5869. 'label' => ['for' => 'stuff'],
  5870. 'Stuff',
  5871. '/label',
  5872. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  5873. '/textarea',
  5874. '/div',
  5875. ];
  5876. $this->assertHtml($expected, $result);
  5877. }
  5878. /**
  5879. * testHiddenField method
  5880. *
  5881. * Test generation of a hidden input.
  5882. */
  5883. public function testHidden(): void
  5884. {
  5885. $this->article['errors'] = [
  5886. 'field' => true,
  5887. ];
  5888. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  5889. $this->Form->create($this->article);
  5890. $result = $this->Form->hidden('field', ['id' => 'theID']);
  5891. $expected = [
  5892. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  5893. $this->assertHtml($expected, $result);
  5894. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5895. $expected = [
  5896. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  5897. ];
  5898. $this->assertHtml($expected, $result);
  5899. }
  5900. /**
  5901. * Test hidden() with various boolean values.
  5902. */
  5903. public function testHiddenBooleanValues(): void
  5904. {
  5905. $this->Form->create($this->article);
  5906. $result = $this->Form->hidden('field', ['value' => null]);
  5907. $expected = [
  5908. 'input' => ['type' => 'hidden', 'name' => 'field'],
  5909. ];
  5910. $this->assertHtml($expected, $result);
  5911. $result = $this->Form->hidden('field', ['value' => true]);
  5912. $expected = [
  5913. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '1'],
  5914. ];
  5915. $this->assertHtml($expected, $result);
  5916. $result = $this->Form->hidden('field', ['value' => false]);
  5917. $expected = [
  5918. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '0'],
  5919. ];
  5920. $this->assertHtml($expected, $result);
  5921. }
  5922. /**
  5923. * testFileUploadField method
  5924. *
  5925. * Test generation of a file upload input.
  5926. */
  5927. public function testFileUploadField(): void
  5928. {
  5929. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5930. $result = $this->Form->file('Model.upload');
  5931. $this->assertHtml($expected, $result);
  5932. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  5933. 'name' => '', 'type' => '', 'tmp_name' => '',
  5934. 'error' => 4, 'size' => 0,
  5935. ]));
  5936. $result = $this->Form->file('Model.upload');
  5937. $this->assertHtml($expected, $result);
  5938. $this->View->setRequest(
  5939. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  5940. );
  5941. $result = $this->Form->file('Model.upload');
  5942. $this->assertHtml($expected, $result);
  5943. }
  5944. /**
  5945. * testFileUploadOnOtherModel method
  5946. *
  5947. * Test File upload input on a model not used in create().
  5948. */
  5949. public function testFileUploadOnOtherModel(): void
  5950. {
  5951. $this->Form->create($this->article, ['type' => 'file']);
  5952. $result = $this->Form->file('ValidateProfile.city');
  5953. $expected = [
  5954. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  5955. ];
  5956. $this->assertHtml($expected, $result);
  5957. }
  5958. /**
  5959. * testButton method
  5960. *
  5961. * Test generation of a form button.
  5962. */
  5963. public function testButton(): void
  5964. {
  5965. $result = $this->Form->button('Hi');
  5966. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  5967. $this->assertHtml($expected, $result);
  5968. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'escapeTitle' => false]);
  5969. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  5970. $this->assertHtml($expected, $result);
  5971. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm', 'escapeTitle' => false]);
  5972. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  5973. $this->assertHtml($expected, $result);
  5974. $result = $this->Form->button('<Clear Form>', ['type' => 'reset']);
  5975. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  5976. $this->assertHtml($expected, $result);
  5977. $result = $this->Form->button('No type', ['type' => false]);
  5978. $expected = ['button' => [], 'No type', '/button'];
  5979. $this->assertHtml($expected, $result);
  5980. $result = $this->Form->button('Upload Text', [
  5981. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  5982. 'escape' => false,
  5983. ]);
  5984. $this->assertDoesNotMatchRegularExpression('/\&039/', $result);
  5985. }
  5986. /**
  5987. * testButtonUnlockedByDefault method
  5988. *
  5989. * Test that button() makes unlocked fields by default.
  5990. */
  5991. public function testButtonUnlockedByDefault(): void
  5992. {
  5993. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5994. $this->Form->create();
  5995. $this->Form->button('Save', ['name' => 'save']);
  5996. $this->Form->button('Clear');
  5997. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  5998. $this->assertEquals(['save'], $result);
  5999. }
  6000. /**
  6001. * Test generation of a form button with confirm message.
  6002. */
  6003. public function testButtonWithConfirm(): void
  6004. {
  6005. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6006. $expected = ['button' => [
  6007. 'type' => 'submit',
  6008. 'data-confirm-message' => 'Confirm me!',
  6009. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6010. ], 'Hi', '/button'];
  6011. $this->assertHtml($expected, $result);
  6012. $result = $this->Form->button('Hi', ['escape' => false, 'confirm' => 'Confirm "me"!']);
  6013. $expected = ['button' => [
  6014. 'type' => 'submit',
  6015. 'data-confirm-message' => 'Confirm "me"!',
  6016. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6017. ], 'Hi', '/button'];
  6018. $this->assertHtml($expected, $result);
  6019. }
  6020. /**
  6021. * testPostButton method
  6022. */
  6023. public function testPostButton(): void
  6024. {
  6025. $result = $this->Form->postButton('Hi', '/controller/action');
  6026. $expected = [
  6027. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6028. 'button' => ['type' => 'submit'],
  6029. 'Hi',
  6030. '/button',
  6031. '/form',
  6032. ];
  6033. $this->assertHtml($expected, $result);
  6034. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6035. $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
  6036. }
  6037. /**
  6038. * testPostButtonMethodType method
  6039. */
  6040. public function testPostButtonMethodType(): void
  6041. {
  6042. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6043. $expected = [
  6044. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6045. 'div' => ['style' => 'display:none;'],
  6046. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6047. '/div',
  6048. 'button' => ['type' => 'submit'],
  6049. 'Hi',
  6050. '/button',
  6051. '/form',
  6052. ];
  6053. $this->assertHtml($expected, $result);
  6054. }
  6055. /**
  6056. * testPostButtonFormOptions method
  6057. */
  6058. public function testPostButtonFormOptions(): void
  6059. {
  6060. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6061. $expected = [
  6062. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6063. 'button' => ['type' => 'submit'],
  6064. 'Hi',
  6065. '/button',
  6066. '/form',
  6067. ];
  6068. $this->assertHtml($expected, $result);
  6069. }
  6070. /**
  6071. * testPostButtonNestedData method
  6072. *
  6073. * Test using postButton with N dimensional data.
  6074. */
  6075. public function testPostButtonNestedData(): void
  6076. {
  6077. $data = [
  6078. 'one' => [
  6079. 'two' => [
  6080. 3, 4, 5,
  6081. ],
  6082. ],
  6083. ];
  6084. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6085. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6086. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6087. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6088. }
  6089. /**
  6090. * testSecurePostButton method
  6091. *
  6092. * Test that postButton adds _Token fields.
  6093. */
  6094. public function testSecurePostButton(): void
  6095. {
  6096. $this->View->setRequest($this->View->getRequest()
  6097. ->withAttribute('csrfToken', 'testkey')
  6098. ->withAttribute('formTokenData', ['unlockedFields' => []]));
  6099. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6100. $tokenDebug = urlencode(json_encode([
  6101. '/posts/delete/1',
  6102. [],
  6103. [],
  6104. ]));
  6105. $expected = [
  6106. 'form' => [
  6107. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6108. ],
  6109. ['div' => ['style' => 'display:none;']],
  6110. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6111. '/div',
  6112. 'button' => ['type' => 'submit'],
  6113. 'Delete',
  6114. '/button',
  6115. ['div' => ['style' => 'display:none;']],
  6116. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6117. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6118. ['input' => [
  6119. 'type' => 'hidden',
  6120. 'name' => '_Token[debug]',
  6121. 'value' => $tokenDebug,
  6122. 'autocomplete' => 'off',
  6123. ]],
  6124. '/div',
  6125. '/form',
  6126. ];
  6127. $this->assertHtml($expected, $result);
  6128. }
  6129. /**
  6130. * testPostLink method
  6131. */
  6132. public function testPostLink(): void
  6133. {
  6134. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6135. $expected = [
  6136. 'form' => [
  6137. 'method' => 'post', 'action' => '/posts/delete/1',
  6138. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6139. ],
  6140. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6141. '/form',
  6142. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6143. 'Delete',
  6144. '/a',
  6145. ];
  6146. $this->assertHtml($expected, $result);
  6147. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6148. $expected = [
  6149. 'form' => [
  6150. 'method' => 'post', 'action' => '/posts/delete/1',
  6151. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6152. ],
  6153. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6154. '/form',
  6155. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6156. 'Delete',
  6157. '/a',
  6158. ];
  6159. $this->assertHtml($expected, $result);
  6160. $result = $this->Form->postLink(
  6161. 'Delete',
  6162. '/posts/delete/1',
  6163. ['target' => '_blank', 'class' => 'btn btn-danger']
  6164. );
  6165. $expected = [
  6166. 'form' => [
  6167. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6168. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6169. ],
  6170. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6171. '/form',
  6172. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6173. 'Delete',
  6174. '/a',
  6175. ];
  6176. $this->assertHtml($expected, $result);
  6177. }
  6178. /**
  6179. * testPostLinkWithConfirm method
  6180. *
  6181. * Test the confirm option for postLink().
  6182. */
  6183. public function testPostLinkWithConfirm(): void
  6184. {
  6185. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6186. $expected = [
  6187. 'form' => [
  6188. 'method' => 'post', 'action' => '/posts/delete/1',
  6189. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6190. ],
  6191. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6192. '/form',
  6193. 'a' => [
  6194. 'href' => '#',
  6195. 'data-confirm-message' => 'Confirm?',
  6196. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/',
  6197. ],
  6198. 'Delete',
  6199. '/a',
  6200. ];
  6201. $this->assertHtml($expected, $result);
  6202. $result = $this->Form->postLink(
  6203. 'Delete',
  6204. '/posts/delete/1',
  6205. ['confirm' => "'Confirm'\nthis \"deletion\"?"]
  6206. );
  6207. $expected = [
  6208. 'form' => [
  6209. 'method' => 'post', 'action' => '/posts/delete/1',
  6210. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6211. ],
  6212. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6213. '/form',
  6214. 'a' => [
  6215. 'href' => '#',
  6216. 'data-confirm-message' => "&#039;Confirm&#039;\nthis &quot;deletion&quot;?",
  6217. 'onclick' => "preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/",
  6218. ],
  6219. 'Delete',
  6220. '/a',
  6221. ];
  6222. $this->assertHtml($expected, $result);
  6223. $this->Form->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { $(\'form[name="{{formName}}"]\').submit();};']);
  6224. $result = $this->Form->postLink(
  6225. 'Delete',
  6226. '/posts/delete/1',
  6227. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  6228. );
  6229. $expected = [
  6230. 'form' => [
  6231. 'method' => 'post', 'action' => '/posts/delete/1',
  6232. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6233. ],
  6234. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6235. '/form',
  6236. 'a' => [
  6237. 'href' => '#',
  6238. 'data-confirm-message' => 'Confirm this deletion?',
  6239. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/',
  6240. ],
  6241. 'Delete',
  6242. '/a',
  6243. ];
  6244. $this->assertHtml($expected, $result);
  6245. }
  6246. /**
  6247. * testPostLinkWithQuery method
  6248. *
  6249. * Test postLink() with query string args.
  6250. */
  6251. public function testPostLinkWithQuery(): void
  6252. {
  6253. $result = $this->Form->postLink(
  6254. 'Delete',
  6255. ['controller' => 'Posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6256. );
  6257. $expected = [
  6258. 'form' => [
  6259. 'method' => 'post', 'action' => '/Posts/delete/1?a=b&amp;c=d',
  6260. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6261. ],
  6262. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6263. '/form',
  6264. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6265. 'Delete',
  6266. '/a',
  6267. ];
  6268. $this->assertHtml($expected, $result);
  6269. }
  6270. /**
  6271. * testPostLinkWithData method
  6272. *
  6273. * Test postLink with additional data.
  6274. */
  6275. public function testPostLinkWithData(): void
  6276. {
  6277. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6278. $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
  6279. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6280. $this->Form->create($entity);
  6281. $this->Form->end();
  6282. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6283. $this->assertStringContainsString(
  6284. '<input type="hidden" name="name" value="show"',
  6285. $result,
  6286. 'should not contain entity data.'
  6287. );
  6288. }
  6289. /**
  6290. * testPostLinkSecurityHash method
  6291. *
  6292. * Test that security hashes for postLink include the url.
  6293. */
  6294. public function testPostLinkSecurityHash(): void
  6295. {
  6296. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6297. $hash .= '%3Aid';
  6298. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6299. $result = $this->Form->postLink(
  6300. 'Delete',
  6301. '/posts/delete/1',
  6302. ['data' => ['id' => 1]]
  6303. );
  6304. $tokenDebug = urlencode(json_encode([
  6305. '/posts/delete/1',
  6306. [
  6307. 'id' => 1,
  6308. ],
  6309. [],
  6310. ]));
  6311. $expected = [
  6312. 'form' => [
  6313. 'method' => 'post', 'action' => '/posts/delete/1',
  6314. 'name', 'style' => 'display:none;',
  6315. ],
  6316. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6317. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6318. 'div' => ['style' => 'display:none;'],
  6319. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6320. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6321. ['input' => [
  6322. 'type' => 'hidden',
  6323. 'name' => '_Token[debug]',
  6324. 'value' => $tokenDebug,
  6325. 'autocomplete' => 'off',
  6326. ]],
  6327. '/div',
  6328. '/form',
  6329. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6330. 'Delete',
  6331. '/a',
  6332. ];
  6333. $this->assertHtml($expected, $result);
  6334. }
  6335. /**
  6336. * testPostLinkSecurityHashBlockMode method
  6337. *
  6338. * Test that postLink doesn't modify the fields in the containing form.
  6339. *
  6340. * postLink() calls inside open forms should not modify the field list
  6341. * for the form.
  6342. */
  6343. public function testPostLinkSecurityHashBlockMode(): void
  6344. {
  6345. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6346. $hash .= '%3A';
  6347. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6348. $this->Form->create(null, ['url' => ['action' => 'add']]);
  6349. $this->Form->control('title');
  6350. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6351. $result = $this->View->fetch('postLink');
  6352. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  6353. $this->assertEquals(['title'], $fields);
  6354. $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
  6355. $reflect = new ReflectionProperty($this->Form, '_lastAction');
  6356. $reflect->setAccessible(true);
  6357. $this->assertSame('/Articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
  6358. }
  6359. /**
  6360. * testPostLinkSecurityHashNoDebugMode method
  6361. *
  6362. * Test that security does not include debug token if debug is false.
  6363. */
  6364. public function testPostLinkSecurityHashNoDebugMode(): void
  6365. {
  6366. Configure::write('debug', false);
  6367. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6368. $hash .= '%3Aid';
  6369. $this->View->setRequest($this->View->getRequest()
  6370. ->withAttribute('formTokenData', ['key' => 'test']));
  6371. $result = $this->Form->postLink(
  6372. 'Delete',
  6373. '/posts/delete/1',
  6374. ['data' => ['id' => 1]]
  6375. );
  6376. $expected = [
  6377. 'form' => [
  6378. 'method' => 'post', 'action' => '/posts/delete/1',
  6379. 'name', 'style' => 'display:none;',
  6380. ],
  6381. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6382. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6383. 'div' => ['style' => 'display:none;'],
  6384. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6385. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6386. '/div',
  6387. '/form',
  6388. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6389. 'Delete',
  6390. '/a',
  6391. ];
  6392. $this->assertHtml($expected, $result);
  6393. }
  6394. /**
  6395. * testPostLinkNestedData method
  6396. *
  6397. * Test using postLink with N dimensional data.
  6398. */
  6399. public function testPostLinkNestedData(): void
  6400. {
  6401. $data = [
  6402. 'one' => [
  6403. 'two' => [
  6404. 3, 4, 5,
  6405. ],
  6406. ],
  6407. ];
  6408. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6409. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6410. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6411. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6412. }
  6413. /**
  6414. * testPostLinkAfterGetForm method
  6415. *
  6416. * Test creating postLinks after a GET form.
  6417. */
  6418. public function testPostLinkAfterGetForm(): void
  6419. {
  6420. $this->View->setRequest($this->View->getRequest()
  6421. ->withAttribute('csrfToken', 'testkey')
  6422. ->withAttribute('formTokenData', []));
  6423. $this->Form->create($this->article, ['type' => 'get']);
  6424. $this->Form->end();
  6425. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6426. $tokenDebug = urlencode(json_encode([
  6427. '/posts/delete/1',
  6428. [],
  6429. [],
  6430. ]));
  6431. $expected = [
  6432. 'form' => [
  6433. 'method' => 'post', 'action' => '/posts/delete/1',
  6434. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6435. ],
  6436. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6437. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6438. 'div' => ['style' => 'display:none;'],
  6439. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6440. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6441. ['input' => [
  6442. 'type' => 'hidden', 'name' => '_Token[debug]',
  6443. 'value' => $tokenDebug,
  6444. 'autocomplete' => 'off',
  6445. ]],
  6446. '/div',
  6447. '/form',
  6448. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6449. 'Delete',
  6450. '/a',
  6451. ];
  6452. $this->assertHtml($expected, $result);
  6453. }
  6454. /**
  6455. * testPostLinkFormBuffer method
  6456. *
  6457. * Test that postLink adds form tags to view block.
  6458. */
  6459. public function testPostLinkFormBuffer(): void
  6460. {
  6461. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6462. $expected = [
  6463. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6464. 'Delete',
  6465. '/a',
  6466. ];
  6467. $this->assertHtml($expected, $result);
  6468. $result = $this->View->fetch('postLink');
  6469. $expected = [
  6470. 'form' => [
  6471. 'method' => 'post', 'action' => '/posts/delete/1',
  6472. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6473. ],
  6474. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6475. '/form',
  6476. ];
  6477. $this->assertHtml($expected, $result);
  6478. $result = $this->Form->postLink(
  6479. 'Delete',
  6480. '/posts/delete/2',
  6481. ['block' => true, 'method' => 'DELETE']
  6482. );
  6483. $expected = [
  6484. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6485. 'Delete',
  6486. '/a',
  6487. ];
  6488. $this->assertHtml($expected, $result);
  6489. $result = $this->View->fetch('postLink');
  6490. $expected = [
  6491. 'form' => [
  6492. 'method' => 'post', 'action' => '/posts/delete/1',
  6493. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6494. ],
  6495. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6496. '/form',
  6497. [
  6498. 'form' => [
  6499. 'method' => 'post', 'action' => '/posts/delete/2',
  6500. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6501. ],
  6502. ],
  6503. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6504. '/form',
  6505. ];
  6506. $this->assertHtml($expected, $result);
  6507. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6508. $expected = [
  6509. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6510. 'Delete',
  6511. '/a',
  6512. ];
  6513. $this->assertHtml($expected, $result);
  6514. $result = $this->View->fetch('foobar');
  6515. $expected = [
  6516. 'form' => [
  6517. 'method' => 'post', 'action' => '/posts/delete/1',
  6518. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6519. ],
  6520. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6521. '/form',
  6522. ];
  6523. $this->assertHtml($expected, $result);
  6524. }
  6525. /**
  6526. * testSubmitButton method
  6527. */
  6528. public function testSubmitButton(): void
  6529. {
  6530. $result = $this->Form->submit('');
  6531. $expected = [
  6532. 'div' => ['class' => 'submit'],
  6533. 'input' => ['type' => 'submit', 'value' => ''],
  6534. '/div',
  6535. ];
  6536. $this->assertHtml($expected, $result);
  6537. $result = $this->Form->submit('Test Submit');
  6538. $expected = [
  6539. 'div' => ['class' => 'submit'],
  6540. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6541. '/div',
  6542. ];
  6543. $this->assertHtml($expected, $result);
  6544. $result = $this->Form->submit('Next >');
  6545. $expected = [
  6546. 'div' => ['class' => 'submit'],
  6547. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6548. '/div',
  6549. ];
  6550. $this->assertHtml($expected, $result);
  6551. $result = $this->Form->submit('Next >', ['escape' => false]);
  6552. $expected = [
  6553. 'div' => ['class' => 'submit'],
  6554. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6555. '/div',
  6556. ];
  6557. $this->assertHtml($expected, $result);
  6558. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6559. $expected = [
  6560. 'div' => ['class' => 'submit'],
  6561. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6562. '/div',
  6563. ];
  6564. $this->assertHtml($expected, $result);
  6565. }
  6566. /**
  6567. * testSubmitImage method
  6568. *
  6569. * Test image submit types.
  6570. */
  6571. public function testSubmitImage(): void
  6572. {
  6573. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6574. $expected = [
  6575. 'div' => ['class' => 'submit'],
  6576. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6577. '/div',
  6578. ];
  6579. $this->assertHtml($expected, $result);
  6580. $result = $this->Form->submit('/relative/cake.power.gif');
  6581. $expected = [
  6582. 'div' => ['class' => 'submit'],
  6583. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6584. '/div',
  6585. ];
  6586. $this->assertHtml($expected, $result);
  6587. $result = $this->Form->submit('cake.power.gif');
  6588. $expected = [
  6589. 'div' => ['class' => 'submit'],
  6590. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6591. '/div',
  6592. ];
  6593. $this->assertHtml($expected, $result);
  6594. $result = $this->Form->submit('Not.an.image');
  6595. $expected = [
  6596. 'div' => ['class' => 'submit'],
  6597. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6598. '/div',
  6599. ];
  6600. $this->assertHtml($expected, $result);
  6601. }
  6602. /**
  6603. * testSubmitUnlockedByDefault method
  6604. *
  6605. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6606. * be submitted at a time.
  6607. */
  6608. public function testSubmitUnlockedByDefault(): void
  6609. {
  6610. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6611. $this->Form->create();
  6612. $this->Form->submit('Go go');
  6613. $this->Form->submit('Save', ['name' => 'save']);
  6614. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6615. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6616. }
  6617. /**
  6618. * testSubmitImageTimestamp method
  6619. *
  6620. * Test submit image with timestamps.
  6621. */
  6622. public function testSubmitImageTimestamp(): void
  6623. {
  6624. Configure::write('Asset.timestamp', 'force');
  6625. $result = $this->Form->submit('cake.power.gif');
  6626. $expected = [
  6627. 'div' => ['class' => 'submit'],
  6628. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6629. '/div',
  6630. ];
  6631. $this->assertHtml($expected, $result);
  6632. }
  6633. /**
  6634. * testDateTimeWithGetForms method
  6635. *
  6636. * Test that datetime() works with GET style forms.
  6637. */
  6638. public function testDateTimeWithGetForms(): void
  6639. {
  6640. $this->Form->create($this->article, ['type' => 'get']);
  6641. $result = $this->Form->datetime('created');
  6642. $expected = [
  6643. 'input' => [
  6644. 'type' => 'datetime-local',
  6645. 'name' => 'created',
  6646. 'value' => '',
  6647. 'step' => '1',
  6648. ],
  6649. ];
  6650. $this->assertHtml($expected, $result);
  6651. $result = $this->Form->datetime('created', ['default' => true]);
  6652. $expected = [
  6653. 'input' => [
  6654. 'type' => 'datetime-local',
  6655. 'name' => 'created',
  6656. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  6657. 'step' => '1',
  6658. ],
  6659. ];
  6660. $this->assertHtml($expected, $result);
  6661. }
  6662. /**
  6663. * Provides fractional schema types
  6664. *
  6665. * @return array
  6666. */
  6667. public function fractionalTypeProvider(): array
  6668. {
  6669. return [
  6670. ['datetimefractional'],
  6671. ['timestampfractional'],
  6672. ['timestamptimezone'],
  6673. ];
  6674. }
  6675. /**
  6676. * testDateTimeWithFractional method
  6677. *
  6678. * Test that datetime() works with datetimefractional.
  6679. *
  6680. * @dataProvider fractionalTypeProvider
  6681. */
  6682. public function testDateTimeWithFractional(string $type): void
  6683. {
  6684. $this->Form->create([
  6685. 'schema' => [
  6686. 'created' => ['type' => $type],
  6687. ],
  6688. ]);
  6689. $result = $this->Form->datetime('created', [
  6690. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6691. ]);
  6692. $expected = [
  6693. 'input' => [
  6694. 'type' => 'datetime-local',
  6695. 'name' => 'created',
  6696. 'value' => '2019-09-27T02:52:43.123',
  6697. 'step' => '0.001',
  6698. ],
  6699. ];
  6700. $this->assertHtml($expected, $result);
  6701. }
  6702. /**
  6703. * testControlWithFractional method
  6704. *
  6705. * Test that control() works with datetimefractional.
  6706. *
  6707. * @dataProvider fractionalTypeProvider
  6708. */
  6709. public function testControlWithFractional(string $type): void
  6710. {
  6711. $this->Form->create([
  6712. 'schema' => [
  6713. 'created' => ['type' => $type],
  6714. ],
  6715. ]);
  6716. $result = $this->Form->control('created', [
  6717. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6718. ]);
  6719. $expected = [
  6720. 'div' => ['class' => 'input datetime'],
  6721. 'label' => ['for' => 'created'],
  6722. 'Created',
  6723. '/label',
  6724. 'input' => [
  6725. 'type' => 'datetime-local',
  6726. 'name' => 'created',
  6727. 'id' => 'created',
  6728. 'value' => '2019-09-27T02:52:43.123',
  6729. 'step' => '0.001',
  6730. ],
  6731. '/div',
  6732. ];
  6733. $this->assertHtml($expected, $result);
  6734. }
  6735. /**
  6736. * testForMagicControlNonExistentNotValidated method
  6737. */
  6738. public function testForMagicControlNonExistentNotValidated(): void
  6739. {
  6740. $this->Form->create($this->article);
  6741. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6742. $result = $this->Form->control('nonexistent_not_validated');
  6743. $expected = [
  6744. 'label' => ['for' => 'nonexistent-not-validated'],
  6745. 'Nonexistent Not Validated',
  6746. '/label',
  6747. 'input' => [
  6748. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6749. 'id' => 'nonexistent-not-validated',
  6750. ],
  6751. ];
  6752. $this->assertHtml($expected, $result);
  6753. $result = $this->Form->control('nonexistent_not_validated', [
  6754. 'val' => 'my value',
  6755. ]);
  6756. $expected = [
  6757. 'label' => ['for' => 'nonexistent-not-validated'],
  6758. 'Nonexistent Not Validated',
  6759. '/label',
  6760. 'input' => [
  6761. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6762. 'value' => 'my value', 'id' => 'nonexistent-not-validated',
  6763. ],
  6764. ];
  6765. $this->assertHtml($expected, $result);
  6766. $this->View->setRequest(
  6767. $this->View->getRequest()->withData('nonexistent_not_validated', 'CakePHP magic')
  6768. );
  6769. $this->Form->create($this->article);
  6770. $result = $this->Form->control('nonexistent_not_validated');
  6771. $expected = [
  6772. 'label' => ['for' => 'nonexistent-not-validated'],
  6773. 'Nonexistent Not Validated',
  6774. '/label',
  6775. 'input' => [
  6776. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6777. 'value' => 'CakePHP magic', 'id' => 'nonexistent-not-validated',
  6778. ],
  6779. ];
  6780. $this->assertHtml($expected, $result);
  6781. }
  6782. /**
  6783. * testFormMagicControlLabel method
  6784. */
  6785. public function testFormMagicControlLabel(): void
  6786. {
  6787. $this->getTableLocator()->get('Contacts', [
  6788. 'className' => ContactsTable::class,
  6789. ]);
  6790. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6791. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6792. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  6793. $expected = [
  6794. 'label' => ['for' => 'contacts-name'],
  6795. 'My label',
  6796. '/label',
  6797. 'input' => [
  6798. 'type' => 'text',
  6799. 'name' => 'Contacts[name]',
  6800. 'id' => 'contacts-name',
  6801. 'maxlength' => '255',
  6802. ],
  6803. ];
  6804. $this->assertHtml($expected, $result);
  6805. $result = $this->Form->control('name', [
  6806. 'label' => ['class' => 'mandatory'],
  6807. ]);
  6808. $expected = [
  6809. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6810. 'Name',
  6811. '/label',
  6812. 'input' => [
  6813. 'type' => 'text', 'name' => 'name',
  6814. 'id' => 'name', 'maxlength' => '255',
  6815. ],
  6816. ];
  6817. $this->assertHtml($expected, $result);
  6818. $result = $this->Form->control('name', [
  6819. 'div' => false,
  6820. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  6821. ]);
  6822. $expected = [
  6823. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6824. 'My label',
  6825. '/label',
  6826. 'input' => [
  6827. 'type' => 'text', 'name' => 'name',
  6828. 'id' => 'name', 'maxlength' => '255',
  6829. ],
  6830. ];
  6831. $this->assertHtml($expected, $result);
  6832. $result = $this->Form->control('Contact.name', [
  6833. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  6834. ]);
  6835. $expected = [
  6836. 'label' => ['for' => 'my_id'],
  6837. 'Name',
  6838. '/label',
  6839. 'input' => [
  6840. 'type' => 'text', 'name' => 'Contact[name]',
  6841. 'id' => 'my_id', 'maxlength' => '255',
  6842. ],
  6843. ];
  6844. $this->assertHtml($expected, $result);
  6845. $result = $this->Form->control('1.id');
  6846. $expected = ['input' => [
  6847. 'type' => 'hidden', 'name' => '1[id]',
  6848. 'id' => '1-id',
  6849. ]];
  6850. $this->assertHtml($expected, $result);
  6851. $result = $this->Form->control('1.name');
  6852. $expected = [
  6853. 'label' => ['for' => '1-name'],
  6854. 'Name',
  6855. '/label',
  6856. 'input' => [
  6857. 'type' => 'text', 'name' => '1[name]',
  6858. 'id' => '1-name', 'maxlength' => '255',
  6859. ],
  6860. ];
  6861. $this->assertHtml($expected, $result);
  6862. }
  6863. /**
  6864. * testFormEnd method
  6865. */
  6866. public function testFormEnd(): void
  6867. {
  6868. $this->assertSame('</form>', $this->Form->end());
  6869. }
  6870. /**
  6871. * testMultiRecordForm method
  6872. *
  6873. * Test the generation of fields for a multi record form.
  6874. */
  6875. public function testMultiRecordForm(): void
  6876. {
  6877. $articles = $this->getTableLocator()->get('Articles');
  6878. $articles->hasMany('Comments');
  6879. $comment = new Entity(['comment' => 'Value']);
  6880. $article = new Article(['comments' => [$comment]]);
  6881. $this->Form->create([$article]);
  6882. $result = $this->Form->control('0.comments.1.comment');
  6883. // phpcs:disable
  6884. $expected = [
  6885. 'div' => ['class' => 'input textarea'],
  6886. 'label' => ['for' => '0-comments-1-comment'],
  6887. 'Comment',
  6888. '/label',
  6889. 'textarea' => [
  6890. 'name',
  6891. 'id' => '0-comments-1-comment',
  6892. 'rows' => 5
  6893. ],
  6894. '/textarea',
  6895. '/div'
  6896. ];
  6897. // phpcs:enable
  6898. $this->assertHtml($expected, $result);
  6899. $result = $this->Form->control('0.comments.0.comment');
  6900. // phpcs:disable
  6901. $expected = [
  6902. 'div' => ['class' => 'input textarea'],
  6903. 'label' => ['for' => '0-comments-0-comment'],
  6904. 'Comment',
  6905. '/label',
  6906. 'textarea' => [
  6907. 'name',
  6908. 'id' => '0-comments-0-comment',
  6909. 'rows' => 5
  6910. ],
  6911. 'Value',
  6912. '/textarea',
  6913. '/div'
  6914. ];
  6915. // phpcs:enable
  6916. $this->assertHtml($expected, $result);
  6917. $comment->setError('comment', ['Not valid']);
  6918. $result = $this->Form->control('0.comments.0.comment');
  6919. // phpcs:disable
  6920. $expected = [
  6921. 'div' => ['class' => 'input textarea error'],
  6922. 'label' => ['for' => '0-comments-0-comment'],
  6923. 'Comment',
  6924. '/label',
  6925. 'textarea' => [
  6926. 'name',
  6927. 'class' => 'form-error',
  6928. 'id' => '0-comments-0-comment',
  6929. 'aria-invalid' => 'true',
  6930. 'aria-describedby' => '0-comments-0-comment-error',
  6931. 'rows' => 5
  6932. ],
  6933. 'Value',
  6934. '/textarea',
  6935. ['div' => ['class' => 'error-message', 'id' => '0-comments-0-comment-error']],
  6936. 'Not valid',
  6937. '/div',
  6938. '/div'
  6939. ];
  6940. // phpcs:enable
  6941. $this->assertHtml($expected, $result);
  6942. $this->getTableLocator()->get('Comments')
  6943. ->getValidator('default')
  6944. ->allowEmptyString('comment', null, false);
  6945. $result = $this->Form->control('0.comments.1.comment');
  6946. // phpcs:disable
  6947. $expected = [
  6948. 'div' => ['class' => 'input textarea required'],
  6949. 'label' => ['for' => '0-comments-1-comment'],
  6950. 'Comment',
  6951. '/label',
  6952. 'textarea' => [
  6953. 'name',
  6954. 'aria-required' => 'true',
  6955. 'required' => 'required',
  6956. 'id' => '0-comments-1-comment',
  6957. 'rows' => 5,
  6958. 'data-validity-message' => 'This field cannot be left empty',
  6959. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  6960. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  6961. ],
  6962. '/textarea',
  6963. '/div'
  6964. ];
  6965. // phpcs:enable
  6966. $this->assertHtml($expected, $result);
  6967. }
  6968. /**
  6969. * testHtml5Controls method
  6970. *
  6971. * Test that some html5 inputs + FormHelper::__call() work.
  6972. */
  6973. public function testHtml5Controls(): void
  6974. {
  6975. $result = $this->Form->email('User.email');
  6976. $expected = [
  6977. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  6978. ];
  6979. $this->assertHtml($expected, $result);
  6980. $result = $this->Form->search('User.query');
  6981. $expected = [
  6982. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  6983. ];
  6984. $this->assertHtml($expected, $result);
  6985. $result = $this->Form->search('User.query', ['value' => 'test']);
  6986. $expected = [
  6987. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  6988. ];
  6989. $this->assertHtml($expected, $result);
  6990. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  6991. $expected = [
  6992. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  6993. ];
  6994. $this->assertHtml($expected, $result);
  6995. }
  6996. /**
  6997. * testHtml5ControlWithControl method
  6998. *
  6999. * Test accessing html5 inputs through control().
  7000. */
  7001. public function testHtml5ControlWithControl(): void
  7002. {
  7003. $this->Form->create();
  7004. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7005. $result = $this->Form->control('website', [
  7006. 'type' => 'url',
  7007. 'val' => 'http://domain.tld',
  7008. 'label' => false,
  7009. ]);
  7010. $expected = [
  7011. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7012. ];
  7013. $this->assertHtml($expected, $result);
  7014. }
  7015. /**
  7016. * testHtml5ControlException method
  7017. *
  7018. * Test errors when field name is missing.
  7019. */
  7020. public function testHtml5ControlException(): void
  7021. {
  7022. $this->expectException(CakeException::class);
  7023. $this->Form->email();
  7024. }
  7025. /**
  7026. * tests fields that are required use custom validation messages
  7027. */
  7028. public function testHtml5ErrorMessage(): void
  7029. {
  7030. $this->Form->setConfig('autoSetCustomValidity', true);
  7031. $validator = (new Validator())
  7032. ->notEmptyString('email', 'Custom error message')
  7033. ->requirePresence('password')
  7034. ->alphaNumeric('password')
  7035. ->notBlank('phone');
  7036. $table = $this->getTableLocator()->get('Contacts', [
  7037. 'className' => ContactsTable::class,
  7038. ]);
  7039. $table->setValidator('default', $validator);
  7040. $contact = new Entity();
  7041. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7042. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7043. $result = $this->Form->control('password');
  7044. $expected = [
  7045. 'label' => ['for' => 'password'],
  7046. 'Password',
  7047. '/label',
  7048. 'input' => [
  7049. 'aria-required' => 'true',
  7050. 'id' => 'password',
  7051. 'name' => 'password',
  7052. 'type' => 'password',
  7053. 'value' => '',
  7054. 'required' => 'required',
  7055. 'data-validity-message' => 'This field cannot be left empty',
  7056. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7057. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7058. ],
  7059. ];
  7060. $this->assertHtml($expected, $result);
  7061. $result = $this->Form->control('phone');
  7062. $expected = [
  7063. 'label' => ['for' => 'phone'],
  7064. 'Phone',
  7065. '/label',
  7066. 'input' => [
  7067. 'aria-required' => 'true',
  7068. 'id' => 'phone',
  7069. 'name' => 'phone',
  7070. 'type' => 'tel',
  7071. 'value' => '',
  7072. 'maxlength' => 255,
  7073. 'required' => 'required',
  7074. 'data-validity-message' => 'This field cannot be left empty',
  7075. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7076. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7077. ],
  7078. ];
  7079. $this->assertHtml($expected, $result);
  7080. $result = $this->Form->control('email');
  7081. $expected = [
  7082. 'label' => ['for' => 'email'],
  7083. 'Email',
  7084. '/label',
  7085. 'input' => [
  7086. 'aria-required' => 'true',
  7087. 'id' => 'email',
  7088. 'name' => 'email',
  7089. 'type' => 'email',
  7090. 'value' => '',
  7091. 'maxlength' => 255,
  7092. 'required' => 'required',
  7093. 'data-validity-message' => 'Custom error message',
  7094. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7095. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7096. ],
  7097. ];
  7098. $this->assertHtml($expected, $result);
  7099. }
  7100. /**
  7101. * tests that custom validation messages are in templateVars
  7102. */
  7103. public function testHtml5ErrorMessageInTemplateVars(): void
  7104. {
  7105. $validator = (new Validator())
  7106. ->notEmptyString('email', 'Custom error "message" & entities')
  7107. ->requirePresence('password')
  7108. ->alphaNumeric('password')
  7109. ->notBlank('phone');
  7110. $table = $this->getTableLocator()->get('Contacts', [
  7111. 'className' => ContactsTable::class,
  7112. ]);
  7113. $table->setValidator('default', $validator);
  7114. $contact = new Entity();
  7115. $this->Form->setConfig('autoSetCustomValidity', false);
  7116. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7117. $this->Form->setTemplates([
  7118. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7119. 'inputContainer' => '{{content}}',
  7120. ]);
  7121. $result = $this->Form->control('password');
  7122. $expected = [
  7123. 'label' => ['for' => 'password'],
  7124. 'Password',
  7125. '/label',
  7126. 'input' => [
  7127. 'aria-required' => 'true',
  7128. 'id' => 'password',
  7129. 'name' => 'password',
  7130. 'type' => 'password',
  7131. 'value' => '',
  7132. 'required' => 'required',
  7133. 'data-message' => 'This field cannot be left empty',
  7134. ],
  7135. ];
  7136. $this->assertHtml($expected, $result);
  7137. $result = $this->Form->control('phone');
  7138. $expected = [
  7139. 'label' => ['for' => 'phone'],
  7140. 'Phone',
  7141. '/label',
  7142. 'input' => [
  7143. 'aria-required' => 'true',
  7144. 'id' => 'phone',
  7145. 'name' => 'phone',
  7146. 'type' => 'tel',
  7147. 'value' => '',
  7148. 'maxlength' => 255,
  7149. 'required' => 'required',
  7150. 'data-message' => 'This field cannot be left empty',
  7151. ],
  7152. ];
  7153. $this->assertHtml($expected, $result);
  7154. $result = $this->Form->control('email', [
  7155. 'templateVars' => [
  7156. 'custom' => 'data-custom="1"',
  7157. ],
  7158. ]);
  7159. $expected = [
  7160. 'label' => ['for' => 'email'],
  7161. 'Email',
  7162. '/label',
  7163. 'input' => [
  7164. 'aria-required' => 'true',
  7165. 'id' => 'email',
  7166. 'name' => 'email',
  7167. 'type' => 'email',
  7168. 'value' => '',
  7169. 'maxlength' => 255,
  7170. 'required' => 'required',
  7171. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7172. 'data-custom' => '1',
  7173. ],
  7174. ];
  7175. $this->assertHtml($expected, $result);
  7176. $this->Form->setConfig('autoSetCustomValidity', true);
  7177. }
  7178. /**
  7179. * testRequiredAttribute method
  7180. *
  7181. * Tests that formhelper sets required attributes.
  7182. */
  7183. public function testRequiredAttribute(): void
  7184. {
  7185. $this->article['required'] = [
  7186. 'title' => true,
  7187. 'body' => false,
  7188. ];
  7189. $this->Form->create($this->article);
  7190. $result = $this->Form->control('title');
  7191. $expected = [
  7192. 'div' => ['class' => 'input text required'],
  7193. 'label' => ['for' => 'title'],
  7194. 'Title',
  7195. '/label',
  7196. 'input' => [
  7197. 'type' => 'text',
  7198. 'name' => 'title',
  7199. 'id' => 'title',
  7200. 'aria-required' => 'true',
  7201. 'required' => 'required',
  7202. 'data-validity-message' => 'This field cannot be left empty',
  7203. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7204. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7205. ],
  7206. '/div',
  7207. ];
  7208. $this->assertHtml($expected, $result);
  7209. $result = $this->Form->control('title', ['required' => false]);
  7210. $this->assertStringNotContainsString('required', $result);
  7211. $result = $this->Form->control('body');
  7212. $expected = [
  7213. 'div' => ['class' => 'input text'],
  7214. 'label' => ['for' => 'body'],
  7215. 'Body',
  7216. '/label',
  7217. 'input' => [
  7218. 'type' => 'text',
  7219. 'name' => 'body',
  7220. 'id' => 'body',
  7221. ],
  7222. '/div',
  7223. ];
  7224. $this->assertHtml($expected, $result);
  7225. $result = $this->Form->control('body', ['required' => true]);
  7226. $this->assertStringContainsString('required', $result);
  7227. }
  7228. /**
  7229. * testControlsNotNested method
  7230. *
  7231. * Tests that it is possible to put inputs outside of the label.
  7232. */
  7233. public function testControlsNotNested(): void
  7234. {
  7235. $this->Form->setTemplates([
  7236. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7237. 'formGroup' => '{{input}}{{label}}',
  7238. ]);
  7239. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7240. $expected = [
  7241. 'div' => ['class' => 'input checkbox'],
  7242. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7243. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7244. 'label' => ['for' => 'foo'],
  7245. 'Foo',
  7246. '/label',
  7247. '/div',
  7248. ];
  7249. $this->assertHtml($expected, $result);
  7250. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7251. $expected = [
  7252. 'div' => ['class' => 'input checkbox'],
  7253. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7254. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7255. '/div',
  7256. ];
  7257. $this->assertHtml($expected, $result);
  7258. $result = $this->Form->control('confirm', [
  7259. 'type' => 'radio',
  7260. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7261. ]);
  7262. $expected = [
  7263. 'div' => ['class' => 'input radio'],
  7264. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '', 'id' => 'confirm']],
  7265. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7266. ['label' => ['for' => 'confirm-y']],
  7267. 'Yes',
  7268. '/label',
  7269. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7270. ['label' => ['for' => 'confirm-n']],
  7271. 'No',
  7272. '/label',
  7273. '<label',
  7274. 'Confirm',
  7275. '/label',
  7276. '/div',
  7277. ];
  7278. $this->assertHtml($expected, $result);
  7279. $result = $this->Form->select('category', ['1', '2'], [
  7280. 'multiple' => 'checkbox',
  7281. 'name' => 'fish',
  7282. ]);
  7283. $expected = [
  7284. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  7285. ['div' => ['class' => 'checkbox']],
  7286. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7287. ['label' => ['for' => 'fish-0']],
  7288. '1',
  7289. '/label',
  7290. '/div',
  7291. ['div' => ['class' => 'checkbox']],
  7292. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7293. ['label' => ['for' => 'fish-1']],
  7294. '2',
  7295. '/label',
  7296. '/div',
  7297. ];
  7298. $this->assertHtml($expected, $result);
  7299. }
  7300. /**
  7301. * testControlContainerTemplates method
  7302. *
  7303. * Test that *Container templates are used by input.
  7304. */
  7305. public function testControlContainerTemplates(): void
  7306. {
  7307. $this->Form->setTemplates([
  7308. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7309. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7310. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7311. ]);
  7312. $this->article['errors'] = [
  7313. 'Article' => ['published' => 'error message'],
  7314. ];
  7315. $this->Form->create($this->article);
  7316. $result = $this->Form->control('accept', [
  7317. 'type' => 'checkbox',
  7318. ]);
  7319. $expected = [
  7320. 'div' => ['class' => 'check'],
  7321. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7322. 'label' => ['for' => 'accept'],
  7323. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7324. 'Accept',
  7325. '/label',
  7326. '/div',
  7327. ];
  7328. $this->assertHtml($expected, $result);
  7329. $result = $this->Form->control('accept', [
  7330. 'type' => 'radio',
  7331. 'options' => ['Y', 'N'],
  7332. ]);
  7333. $this->assertStringContainsString('<div class="rad">', $result);
  7334. $result = $this->Form->control('Article.published', [
  7335. 'type' => 'radio',
  7336. 'options' => ['Y', 'N'],
  7337. ]);
  7338. $this->assertStringContainsString('<div class="rad err">', $result);
  7339. }
  7340. /**
  7341. * testFormGroupTemplates method
  7342. *
  7343. * Test that *Container templates are used by input.
  7344. */
  7345. public function testFormGroupTemplates(): void
  7346. {
  7347. $this->Form->setTemplates([
  7348. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7349. ]);
  7350. $this->Form->create($this->article);
  7351. $result = $this->Form->control('accept', [
  7352. 'type' => 'radio',
  7353. 'options' => ['Y', 'N'],
  7354. ]);
  7355. $this->assertStringContainsString('<div class="radio">', $result);
  7356. }
  7357. /**
  7358. * testResetTemplates method
  7359. *
  7360. * Test resetting templates.
  7361. */
  7362. public function testResetTemplates(): void
  7363. {
  7364. $this->Form->setTemplates(['input' => '<input/>']);
  7365. $this->assertSame('<input/>', $this->Form->templater()->get('input'));
  7366. $this->Form->resetTemplates();
  7367. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7368. }
  7369. /**
  7370. * testContext method
  7371. *
  7372. * Test the context method.
  7373. */
  7374. public function testContext(): void
  7375. {
  7376. $result = $this->Form->context();
  7377. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7378. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7379. $this->assertSame($mock, $this->Form->context($mock));
  7380. $this->assertSame($mock, $this->Form->context());
  7381. }
  7382. /**
  7383. * testAutoDomId method
  7384. */
  7385. public function testAutoDomId(): void
  7386. {
  7387. $result = $this->Form->text('field', ['id' => true]);
  7388. $expected = [
  7389. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7390. ];
  7391. $this->assertHtml($expected, $result);
  7392. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7393. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7394. $expected = [
  7395. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '', 'id' => 'field'],
  7396. ['label' => ['for' => 'field-0']],
  7397. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7398. 'option A',
  7399. '/label',
  7400. ['label' => ['for' => 'field-1']],
  7401. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7402. 'option B',
  7403. '/label',
  7404. ];
  7405. $this->assertHtml($expected, $result);
  7406. $result = $this->Form->select(
  7407. 'multi_field',
  7408. ['first', 'second'],
  7409. ['multiple' => 'checkbox', 'id' => true]
  7410. );
  7411. $expected = [
  7412. 'input' => [
  7413. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '', 'id' => 'multi-field',
  7414. ],
  7415. ['div' => ['class' => 'checkbox']],
  7416. ['label' => ['for' => 'multi-field-0']],
  7417. ['input' => [
  7418. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7419. 'value' => '0', 'id' => 'multi-field-0',
  7420. ]],
  7421. 'first',
  7422. '/label',
  7423. '/div',
  7424. ['div' => ['class' => 'checkbox']],
  7425. ['label' => ['for' => 'multi-field-1']],
  7426. ['input' => [
  7427. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7428. 'value' => '1', 'id' => 'multi-field-1',
  7429. ]],
  7430. 'second',
  7431. '/label',
  7432. '/div',
  7433. ];
  7434. $this->assertHtml($expected, $result);
  7435. }
  7436. /**
  7437. * Test the basic setters and getters for value sources
  7438. */
  7439. public function testFormValueSourcesSettersGetters(): void
  7440. {
  7441. $this->View->setRequest($this->View->getRequest()
  7442. ->withData('id', '1')
  7443. ->withQueryParams(['id' => '2']));
  7444. $expected = ['data', 'context'];
  7445. $result = $this->Form->getValueSources();
  7446. $this->assertEquals($expected, $result);
  7447. $this->Form->setValueSources(['context']);
  7448. $result = $this->Form->getSourceValue('id');
  7449. $this->assertNull($result);
  7450. $this->Form->setValueSources('query');
  7451. $expected = ['query'];
  7452. $result = $this->Form->getValueSources();
  7453. $this->assertEquals($expected, $result);
  7454. $expected = '2';
  7455. $result = $this->Form->getSourceValue('id');
  7456. $this->assertSame($expected, $result);
  7457. $this->Form->setValueSources(['data']);
  7458. $expected = '1';
  7459. $result = $this->Form->getSourceValue('id');
  7460. $this->assertSame($expected, $result);
  7461. $this->Form->setValueSources(['query', 'data']);
  7462. $expected = '2';
  7463. $result = $this->Form->getSourceValue('id');
  7464. $this->assertSame($expected, $result);
  7465. }
  7466. public function testValueSourcesValidation(): void
  7467. {
  7468. $this->expectException(InvalidArgumentException::class);
  7469. $this->expectExceptionMessage('Invalid value source(s): `invalid`, `foo`. Valid values are: `context`, `data`, `query`.');
  7470. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7471. }
  7472. /**
  7473. * Tests the different input rendering values based on sources values switching
  7474. */
  7475. public function testFormValueSourcesSingleSwitchRendering(): void
  7476. {
  7477. $articles = $this->getTableLocator()->get('Articles');
  7478. $article = new Article();
  7479. $articles->patchEntity($article, ['id' => '3']);
  7480. $this->Form->create($article);
  7481. $this->Form->setValueSources(['context']);
  7482. $result = $this->Form->control('id');
  7483. $expected = [
  7484. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7485. ];
  7486. $this->assertHtml($expected, $result);
  7487. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7488. $this->Form->setValueSources(['query']);
  7489. $result = $this->Form->control('id');
  7490. $expected = [
  7491. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7492. ];
  7493. $this->assertHtml($expected, $result);
  7494. $this->View->setRequest($this->View->getRequest()
  7495. ->withQueryParams(['id' => '5a'])
  7496. ->withData('id', '5b'));
  7497. $this->Form->setValueSources(['context']);
  7498. $this->Form->create($article);
  7499. $result = $this->Form->control('id');
  7500. $expected = [
  7501. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7502. ];
  7503. $this->assertHtml($expected, $result);
  7504. $this->Form->setValueSources(['data']);
  7505. $this->Form->create($article);
  7506. $result = $this->Form->control('id');
  7507. $expected = [
  7508. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7509. ];
  7510. $this->assertHtml($expected, $result);
  7511. $this->Form->setValueSources(['query']);
  7512. $result = $this->Form->control('id');
  7513. $expected = [
  7514. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7515. ];
  7516. $this->assertHtml($expected, $result);
  7517. }
  7518. /**
  7519. * Tests the different input rendering values based on sources values switching while supplying
  7520. * an entity (base context) and multiple sources (such as data, query)
  7521. */
  7522. public function testFormValueSourcesListSwitchRendering(): void
  7523. {
  7524. $articles = $this->getTableLocator()->get('Articles');
  7525. $article = new Article();
  7526. $articles->patchEntity($article, ['id' => '3']);
  7527. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7528. $this->Form->create($article);
  7529. $this->Form->setValueSources(['context', 'query']);
  7530. $result = $this->Form->control('id');
  7531. $expected = [
  7532. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7533. ];
  7534. $this->assertHtml($expected, $result);
  7535. $this->Form->setValueSources(['query', 'context']);
  7536. $result = $this->Form->control('id');
  7537. $expected = [
  7538. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7539. ];
  7540. $this->assertHtml($expected, $result);
  7541. $this->Form->setValueSources(['data', 'query', 'context']);
  7542. $result = $this->Form->control('id');
  7543. $expected = [
  7544. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7545. ];
  7546. $this->assertHtml($expected, $result);
  7547. $this->View->setRequest($this->View->getRequest()
  7548. ->withData('id', '8')
  7549. ->withQueryParams(['id' => '9']));
  7550. $this->Form->setValueSources(['data', 'query', 'context']);
  7551. $result = $this->Form->control('id');
  7552. $expected = [
  7553. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7554. ];
  7555. $this->assertHtml($expected, $result);
  7556. }
  7557. /**
  7558. * Test the different form input renderings based on values sources switchings through form options
  7559. */
  7560. public function testFormValueSourcesSwitchViaOptionsRendering(): void
  7561. {
  7562. $articles = $this->getTableLocator()->get('Articles');
  7563. $article = new Article();
  7564. $articles->patchEntity($article, ['id' => '3']);
  7565. $this->View->setRequest(
  7566. $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
  7567. );
  7568. $this->Form->create($article, ['valueSources' => 'query']);
  7569. $result = $this->Form->control('id');
  7570. $expected = [
  7571. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7572. ];
  7573. $this->assertHtml($expected, $result);
  7574. $result = $this->Form->getSourceValue('id');
  7575. $this->assertSame('5', $result);
  7576. $this->Form->setValueSources(['context']);
  7577. $this->Form->create($article, ['valueSources' => 'query']);
  7578. $result = $this->Form->control('id');
  7579. $expected = [
  7580. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7581. ];
  7582. $this->assertHtml($expected, $result);
  7583. $result = $this->Form->getSourceValue('id');
  7584. $this->assertSame('5', $result);
  7585. $this->Form->setValueSources(['query']);
  7586. $this->Form->create($article, ['valueSources' => 'data']);
  7587. $result = $this->Form->control('id');
  7588. $expected = [
  7589. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7590. ];
  7591. $this->assertHtml($expected, $result);
  7592. $result = $this->Form->getSourceValue('id');
  7593. $this->assertSame('4', $result);
  7594. $this->Form->setValueSources(['query']);
  7595. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7596. $result = $this->Form->control('id');
  7597. $expected = [
  7598. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7599. ];
  7600. $this->assertHtml($expected, $result);
  7601. $result = $this->Form->getSourceValue('id');
  7602. $this->assertSame(3, $result);
  7603. }
  7604. /**
  7605. * Test the different form input renderings based on values sources switchings through form options
  7606. */
  7607. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering(): void
  7608. {
  7609. $articles = $this->getTableLocator()->get('Articles');
  7610. $article = new Article();
  7611. $articles->patchEntity($article, ['id' => '3']);
  7612. $this->View->setRequest(
  7613. $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
  7614. );
  7615. $this->Form->setValueSources(['context'])
  7616. ->create($article, ['valueSources' => ['query', 'data']]);
  7617. $result = $this->Form->control('id');
  7618. $expected = [
  7619. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7620. ];
  7621. $this->assertHtml($expected, $result);
  7622. $result = $this->Form->getSourceValue('id');
  7623. $this->assertSame('11', $result);
  7624. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7625. $this->Form->setValueSources(['context'])
  7626. ->create($article, ['valueSources' => ['query', 'data']]);
  7627. $result = $this->Form->control('id');
  7628. $expected = [
  7629. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7630. ];
  7631. $this->assertHtml($expected, $result);
  7632. $result = $this->Form->getSourceValue('id');
  7633. $this->assertSame('10', $result);
  7634. }
  7635. /**
  7636. * Test the different form values sources resetting through From::end();
  7637. */
  7638. public function testFormValueSourcesResetViaEnd(): void
  7639. {
  7640. $expected = ['data', 'context'];
  7641. $result = $this->Form->getValueSources();
  7642. $this->assertEquals($expected, $result);
  7643. $expected = ['query', 'context', 'data'];
  7644. $this->Form->setValueSources(['query', 'context', 'data']);
  7645. $result = $this->Form->getValueSources();
  7646. $this->assertEquals($expected, $result);
  7647. $this->Form->create();
  7648. $result = $this->Form->getValueSources();
  7649. $this->assertEquals($expected, $result);
  7650. $this->Form->end();
  7651. $result = $this->Form->getValueSources();
  7652. $this->assertEquals(['data', 'context'], $result);
  7653. }
  7654. /**
  7655. * Test sources values defaults handling
  7656. */
  7657. public function testFormValueSourcesDefaults(): void
  7658. {
  7659. $this->View->setRequest(
  7660. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7661. );
  7662. $this->Form->create();
  7663. $result = $this->Form->password('password');
  7664. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7665. $this->assertHtml($expected, $result);
  7666. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7667. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7668. $this->assertHtml($expected, $result);
  7669. $this->Form->setValueSources('query');
  7670. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7671. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7672. $this->assertHtml($expected, $result);
  7673. $this->Form->setValueSources('data');
  7674. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7675. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7676. $this->assertHtml($expected, $result);
  7677. }
  7678. /**
  7679. * Test sources values schema defaults handling
  7680. */
  7681. public function testSourcesValueDoesntExistPassThrough(): void
  7682. {
  7683. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7684. $articles = $this->getTableLocator()->get('Articles');
  7685. $entity = $articles->newEmptyEntity();
  7686. $this->Form->create($entity);
  7687. $this->Form->setValueSources(['query', 'context']);
  7688. $result = $this->Form->getSourceValue('category');
  7689. $this->assertSame('sesame-cookies', $result);
  7690. $this->Form->setValueSources(['context', 'query']);
  7691. $result = $this->Form->getSourceValue('category');
  7692. $this->assertSame('sesame-cookies', $result);
  7693. }
  7694. /**
  7695. * testNestedLabelInput method
  7696. *
  7697. * Test the `nestedInput` parameter
  7698. */
  7699. public function testNestedLabelInput(): void
  7700. {
  7701. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7702. $expected = [
  7703. 'div' => ['class' => 'input text'],
  7704. 'label' => ['for' => 'foo'],
  7705. ['input' => [
  7706. 'type' => 'text',
  7707. 'name' => 'foo',
  7708. 'id' => 'foo',
  7709. ]],
  7710. 'Foo',
  7711. '/label',
  7712. '/div',
  7713. ];
  7714. $this->assertHtml($expected, $result);
  7715. }
  7716. /**
  7717. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7718. *
  7719. * This test makes sure `false` excludes the label from the render
  7720. */
  7721. public function testControlLabelManipulationDisableLabels(): void
  7722. {
  7723. $result = $this->Form->control('test', [
  7724. 'type' => 'radio',
  7725. 'options' => ['A', 'B'],
  7726. 'labelOptions' => false,
  7727. ]);
  7728. $expected = [
  7729. ['div' => ['class' => 'input radio']],
  7730. '<label',
  7731. 'Test',
  7732. '/label',
  7733. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7734. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7735. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7736. '/div',
  7737. ];
  7738. $this->assertHtml($expected, $result);
  7739. $result = $this->Form->control('checkbox1', [
  7740. 'label' => 'My checkboxes',
  7741. 'multiple' => 'checkbox',
  7742. 'type' => 'select',
  7743. 'options' => [
  7744. ['text' => 'First Checkbox', 'value' => 1],
  7745. ['text' => 'Second Checkbox', 'value' => 2],
  7746. ],
  7747. 'labelOptions' => false,
  7748. ]);
  7749. $expected = [
  7750. ['div' => ['class' => 'input select']],
  7751. ['label' => ['for' => 'checkbox1']],
  7752. 'My checkboxes',
  7753. '/label',
  7754. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7755. ['div' => ['class' => 'checkbox']],
  7756. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7757. '/div',
  7758. ['div' => ['class' => 'checkbox']],
  7759. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7760. '/div',
  7761. '/div',
  7762. ];
  7763. $this->assertHtml($expected, $result);
  7764. }
  7765. /**
  7766. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7767. *
  7768. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7769. * added to the class if checked.
  7770. *
  7771. * Also checks to make sure any custom attributes are rendered correctly
  7772. */
  7773. public function testControlLabelManipulationRadios(): void
  7774. {
  7775. $result = $this->Form->control('test', [
  7776. 'type' => 'radio',
  7777. 'options' => ['A', 'B'],
  7778. 'labelOptions' => ['class' => 'custom-class'],
  7779. ]);
  7780. $expected = [
  7781. ['div' => ['class' => 'input radio']],
  7782. '<label',
  7783. 'Test',
  7784. '/label',
  7785. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7786. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7787. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7788. 'A',
  7789. '/label',
  7790. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  7791. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7792. 'B',
  7793. '/label',
  7794. '/div',
  7795. ];
  7796. $this->assertHtml($expected, $result);
  7797. $result = $this->Form->control('test', [
  7798. 'type' => 'radio',
  7799. 'options' => ['A', 'B'],
  7800. 'value' => 1,
  7801. 'labelOptions' => ['class' => 'custom-class'],
  7802. ]);
  7803. $expected = [
  7804. ['div' => ['class' => 'input radio']],
  7805. '<label',
  7806. 'Test',
  7807. '/label',
  7808. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7809. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7810. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7811. 'A',
  7812. '/label',
  7813. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  7814. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7815. 'B',
  7816. '/label',
  7817. '/div',
  7818. ];
  7819. $this->assertHtml($expected, $result);
  7820. $result = $this->Form->control('test', [
  7821. 'type' => 'radio',
  7822. 'options' => ['A', 'B'],
  7823. 'value' => 1,
  7824. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  7825. ]);
  7826. $expected = [
  7827. ['div' => ['class' => 'input radio']],
  7828. '<label',
  7829. 'Test',
  7830. '/label',
  7831. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7832. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  7833. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7834. 'A',
  7835. '/label',
  7836. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  7837. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7838. 'B',
  7839. '/label',
  7840. '/div',
  7841. ];
  7842. $this->assertHtml($expected, $result);
  7843. $result = $this->Form->radio('test', ['A', 'B'], [
  7844. 'label' => [
  7845. 'class' => ['custom-class', 'another-class'],
  7846. 'data-name' => 'bob',
  7847. ],
  7848. 'value' => 1,
  7849. ]);
  7850. $expected = [
  7851. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test'],
  7852. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  7853. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7854. 'A',
  7855. '/label',
  7856. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  7857. ['input' => [
  7858. 'type' => 'radio',
  7859. 'name' => 'test',
  7860. 'value' => '1',
  7861. 'id' => 'test-1',
  7862. 'checked' => 'checked',
  7863. ]],
  7864. 'B',
  7865. '/label',
  7866. ];
  7867. $this->assertHtml($expected, $result);
  7868. }
  7869. /**
  7870. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  7871. *
  7872. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7873. * added to the class if checked.
  7874. *
  7875. * Also checks to make sure any custom attributes are rendered correctly
  7876. */
  7877. public function testControlLabelManipulationCheckboxes(): void
  7878. {
  7879. $result = $this->Form->control('checkbox1', [
  7880. 'label' => 'My checkboxes',
  7881. 'multiple' => 'checkbox',
  7882. 'type' => 'select',
  7883. 'options' => [
  7884. ['text' => 'First Checkbox', 'value' => 1],
  7885. ['text' => 'Second Checkbox', 'value' => 2],
  7886. ],
  7887. 'labelOptions' => ['class' => 'custom-class'],
  7888. 'value' => ['1'],
  7889. ]);
  7890. $expected = [
  7891. ['div' => ['class' => 'input select']],
  7892. ['label' => ['for' => 'checkbox1']],
  7893. 'My checkboxes',
  7894. '/label',
  7895. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7896. ['div' => ['class' => 'checkbox']],
  7897. ['label' => [
  7898. 'class' => 'custom-class selected',
  7899. 'for' => 'checkbox1-1',
  7900. ]],
  7901. ['input' => [
  7902. 'type' => 'checkbox',
  7903. 'name' => 'checkbox1[]',
  7904. 'value' => '1',
  7905. 'id' => 'checkbox1-1',
  7906. 'checked' => 'checked',
  7907. ]],
  7908. 'First Checkbox',
  7909. '/label',
  7910. '/div',
  7911. ['div' => ['class' => 'checkbox']],
  7912. ['label' => [
  7913. 'class' => 'custom-class',
  7914. 'for' => 'checkbox1-2',
  7915. ]],
  7916. ['input' => [
  7917. 'type' => 'checkbox',
  7918. 'name' => 'checkbox1[]',
  7919. 'value' => '2',
  7920. 'id' => 'checkbox1-2',
  7921. ]],
  7922. 'Second Checkbox',
  7923. '/label',
  7924. '/div',
  7925. '/div',
  7926. ];
  7927. $this->assertHtml($expected, $result);
  7928. $result = $this->Form->control('checkbox1', [
  7929. 'label' => 'My checkboxes',
  7930. 'multiple' => 'checkbox',
  7931. 'type' => 'select',
  7932. 'options' => [
  7933. ['text' => 'First Checkbox', 'value' => 1],
  7934. ['text' => 'Second Checkbox', 'value' => 2],
  7935. ],
  7936. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  7937. 'value' => ['1'],
  7938. ]);
  7939. $expected = [
  7940. ['div' => ['class' => 'input select']],
  7941. ['label' => ['for' => 'checkbox1']],
  7942. 'My checkboxes',
  7943. '/label',
  7944. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7945. ['div' => ['class' => 'checkbox']],
  7946. ['label' => [
  7947. 'class' => 'custom-class another-class selected',
  7948. 'data-name' => 'bob',
  7949. 'for' => 'checkbox1-1',
  7950. ]],
  7951. ['input' => [
  7952. 'type' => 'checkbox',
  7953. 'name' => 'checkbox1[]',
  7954. 'value' => '1',
  7955. 'id' => 'checkbox1-1',
  7956. 'checked' => 'checked',
  7957. ]],
  7958. 'First Checkbox',
  7959. '/label',
  7960. '/div',
  7961. ['div' => ['class' => 'checkbox']],
  7962. ['label' => [
  7963. 'class' => 'custom-class another-class',
  7964. 'data-name' => 'bob',
  7965. 'for' => 'checkbox1-2',
  7966. ]],
  7967. ['input' => [
  7968. 'type' => 'checkbox',
  7969. 'name' => 'checkbox1[]',
  7970. 'value' => '2',
  7971. 'id' => 'checkbox1-2',
  7972. ]],
  7973. 'Second Checkbox',
  7974. '/label',
  7975. '/div',
  7976. '/div',
  7977. ];
  7978. $this->assertHtml($expected, $result);
  7979. }
  7980. /**
  7981. * testControlMaxLengthArrayContext method
  7982. *
  7983. * Test control() with maxlength attribute in Array Context.
  7984. */
  7985. public function testControlMaxLengthArrayContext(): void
  7986. {
  7987. $this->article['schema'] = [
  7988. 'title' => ['length' => 10],
  7989. ];
  7990. $this->Form->create($this->article);
  7991. $result = $this->Form->control('title');
  7992. $expected = [
  7993. 'div' => ['class'],
  7994. 'label' => ['for'],
  7995. 'Title',
  7996. '/label',
  7997. 'input' => [
  7998. 'aria-required' => 'true',
  7999. 'id',
  8000. 'name' => 'title',
  8001. 'type' => 'text',
  8002. 'required' => 'required',
  8003. 'maxlength' => 10,
  8004. 'data-validity-message' => 'This field cannot be left empty',
  8005. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8006. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8007. ],
  8008. '/div',
  8009. ];
  8010. $this->assertHtml($expected, $result);
  8011. }
  8012. /**
  8013. * testControlMaxLengthEntityContext method
  8014. *
  8015. * Test control() with maxlength attribute in Entity Context.
  8016. */
  8017. public function testControlMaxLengthEntityContext(): void
  8018. {
  8019. $this->article['schema']['title']['length'] = 45;
  8020. $validator = new Validator();
  8021. $validator->maxLength('title', 10);
  8022. $article = new EntityContext(
  8023. [
  8024. 'entity' => new Entity($this->article),
  8025. 'table' => new Table([
  8026. 'alias' => 'Articles',
  8027. 'schema' => $this->article['schema'],
  8028. 'validator' => $validator,
  8029. ]),
  8030. ]
  8031. );
  8032. $this->Form->create($article);
  8033. $result = $this->Form->control('title');
  8034. $expected = [
  8035. 'div' => ['class'],
  8036. 'label' => ['for'],
  8037. 'Title',
  8038. '/label',
  8039. 'input' => [
  8040. 'aria-required' => 'true',
  8041. 'id',
  8042. 'name' => 'title',
  8043. 'type' => 'text',
  8044. 'required' => 'required',
  8045. 'maxlength' => 10,
  8046. 'data-validity-message' => 'This field cannot be left empty',
  8047. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8048. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8049. ],
  8050. '/div',
  8051. ];
  8052. $this->assertHtml($expected, $result);
  8053. $this->article['schema']['title']['length'] = 45;
  8054. $validator = new Validator();
  8055. $validator->maxLength('title', 55);
  8056. $article = new EntityContext(
  8057. [
  8058. 'entity' => new Entity($this->article),
  8059. 'table' => new Table([
  8060. 'schema' => $this->article['schema'],
  8061. 'validator' => $validator,
  8062. 'alias' => 'Articles',
  8063. ]),
  8064. ]
  8065. );
  8066. $this->Form->create($article);
  8067. $result = $this->Form->control('title');
  8068. $expected = [
  8069. 'div' => ['class'],
  8070. 'label' => ['for'],
  8071. 'Title',
  8072. '/label',
  8073. 'input' => [
  8074. 'aria-required' => 'true',
  8075. 'id',
  8076. 'name' => 'title',
  8077. 'type' => 'text',
  8078. 'required' => 'required',
  8079. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8080. 'data-validity-message' => 'This field cannot be left empty',
  8081. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8082. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8083. ],
  8084. '/div',
  8085. ];
  8086. $this->assertHtml($expected, $result);
  8087. $this->article['schema']['title']['length'] = 45;
  8088. $validator = new Validator();
  8089. $validator->maxLength('title', 55);
  8090. $article = new EntityContext(
  8091. [
  8092. 'entity' => new Entity($this->article),
  8093. 'table' => new Table([
  8094. 'schema' => $this->article['schema'],
  8095. 'validator' => $validator,
  8096. 'alias' => 'Articles',
  8097. ]),
  8098. ]
  8099. );
  8100. $this->Form->create($article);
  8101. $result = $this->Form->control('title', ['maxlength' => 10]);
  8102. $expected = [
  8103. 'div' => ['class'],
  8104. 'label' => ['for'],
  8105. 'Title',
  8106. '/label',
  8107. 'input' => [
  8108. 'aria-required' => 'true',
  8109. 'id',
  8110. 'name' => 'title',
  8111. 'type' => 'text',
  8112. 'required' => 'required',
  8113. 'maxlength' => 10, // Length set in options should take highest precedence.
  8114. 'data-validity-message' => 'This field cannot be left empty',
  8115. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8116. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8117. ],
  8118. '/div',
  8119. ];
  8120. $this->assertHtml($expected, $result);
  8121. }
  8122. /**
  8123. * testControlMinMaxLengthEntityContext method
  8124. *
  8125. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8126. */
  8127. public function testControlMinMaxLengthEntityContext(): void
  8128. {
  8129. $validator = new Validator();
  8130. $validator->maxLength('title', 10);
  8131. $article = new EntityContext(
  8132. [
  8133. 'entity' => new Entity($this->article),
  8134. 'table' => new Table([
  8135. 'alias' => 'Articles',
  8136. 'schema' => $this->article['schema'],
  8137. 'validator' => $validator,
  8138. ]),
  8139. ]
  8140. );
  8141. $this->Form->create($article);
  8142. $result = $this->Form->control('title');
  8143. $expected = [
  8144. 'div' => ['class'],
  8145. 'label' => ['for'],
  8146. 'Title',
  8147. '/label',
  8148. 'input' => [
  8149. 'aria-required' => 'true',
  8150. 'id',
  8151. 'name' => 'title',
  8152. 'type' => 'text',
  8153. 'required' => 'required',
  8154. 'maxlength' => 10,
  8155. 'data-validity-message' => 'This field cannot be left empty',
  8156. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8157. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8158. ],
  8159. '/div',
  8160. ];
  8161. $this->assertHtml($expected, $result);
  8162. }
  8163. /**
  8164. * testControlMaxLengthFormContext method
  8165. *
  8166. * Test control() with maxlength attribute in Form Context.
  8167. */
  8168. public function testControlMaxLengthFormContext(): void
  8169. {
  8170. $validator = new Validator();
  8171. $validator->maxLength('title', 10);
  8172. $form = new Form();
  8173. $form->setValidator('default', $validator);
  8174. $this->Form->create($form);
  8175. $result = $this->Form->control('title');
  8176. $expected = [
  8177. 'div' => ['class'],
  8178. 'label' => ['for'],
  8179. 'Title',
  8180. '/label',
  8181. 'input' => [
  8182. 'aria-required' => 'true',
  8183. 'id',
  8184. 'name' => 'title',
  8185. 'type' => 'text',
  8186. 'required' => 'required',
  8187. 'maxlength' => 10,
  8188. 'data-validity-message' => 'This field cannot be left empty',
  8189. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8190. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8191. ],
  8192. '/div',
  8193. ];
  8194. $this->assertHtml($expected, $result);
  8195. }
  8196. }