FormHelperTest.php 306 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912
  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. use TestApp\View\Form\StubContext;
  41. /**
  42. * FormHelperTest class
  43. *
  44. * @property \Cake\View\Helper\FormHelper $Form
  45. * @property \Cake\View\View $View
  46. */
  47. class FormHelperTest extends TestCase
  48. {
  49. /**
  50. * Fixtures to be used
  51. *
  52. * @var array<string>
  53. */
  54. protected array $fixtures = ['core.Articles', 'core.Comments'];
  55. /**
  56. * @var array
  57. */
  58. protected $article = [];
  59. /**
  60. * @var string
  61. */
  62. protected $url;
  63. /**
  64. * @var \Cake\View\Helper\FormHelper
  65. */
  66. protected $Form;
  67. /**
  68. * @var \Cake\View\View
  69. */
  70. protected $View;
  71. /**
  72. * setUp method
  73. */
  74. public function setUp(): void
  75. {
  76. parent::setUp();
  77. Configure::write('Config.language', 'eng');
  78. Configure::write('App.base', '');
  79. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  80. $request = new ServerRequest([
  81. 'webroot' => '',
  82. 'base' => '',
  83. 'url' => '/articles/add',
  84. 'params' => [
  85. 'controller' => 'Articles',
  86. 'action' => 'add',
  87. 'plugin' => null,
  88. ],
  89. ]);
  90. $this->View = new View($request);
  91. Router::reload();
  92. Router::setRequest($request);
  93. $this->url = '/articles/add';
  94. $this->Form = new FormHelper($this->View);
  95. $this->article = [
  96. 'schema' => [
  97. 'id' => ['type' => 'integer'],
  98. 'author_id' => ['type' => 'integer', 'null' => true],
  99. 'title' => ['type' => 'string', 'null' => true],
  100. 'body' => 'text',
  101. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  102. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  103. ],
  104. 'required' => [
  105. 'author_id' => true,
  106. 'title' => true,
  107. ],
  108. ];
  109. Security::setSalt('foo!');
  110. $builder = Router::createRouteBuilder('/');
  111. $builder->connect('/{controller}', ['action' => 'index']);
  112. $builder->connect('/{controller}/{action}/*');
  113. }
  114. /**
  115. * tearDown method
  116. */
  117. public function tearDown(): void
  118. {
  119. parent::tearDown();
  120. unset($this->Form, $this->View);
  121. }
  122. /**
  123. * Test construct() with the templates option.
  124. */
  125. public function testConstructTemplatesFile(): void
  126. {
  127. $helper = new FormHelper($this->View, [
  128. 'templates' => 'htmlhelper_tags',
  129. ]);
  130. $result = $helper->control('name');
  131. $this->assertStringContainsString('<input', $result);
  132. }
  133. /**
  134. * Test that when specifying custom widgets the config array for that widget
  135. * is overwritten instead of merged.
  136. */
  137. public function testConstructWithWidgets(): void
  138. {
  139. $config = [
  140. 'widgets' => [
  141. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  142. ],
  143. ];
  144. $helper = new FormHelper($this->View, $config);
  145. $locator = $helper->getWidgetLocator();
  146. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  147. }
  148. /**
  149. * Test that when specifying custom widgets config file and it should be
  150. * added to widgets array. WidgetLocator will load widgets in constructor.
  151. */
  152. public function testConstructWithWidgetsConfig(): void
  153. {
  154. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  155. $locator = $helper->getWidgetLocator();
  156. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  157. }
  158. /**
  159. * Test setting the widget locator
  160. */
  161. public function testSetAndGetWidgetLocator(): void
  162. {
  163. $helper = new FormHelper($this->View);
  164. $locator = new WidgetLocator($helper->templater(), $this->View);
  165. $helper->setWidgetLocator($locator);
  166. $this->assertSame($locator, $helper->getWidgetLocator());
  167. }
  168. /**
  169. * Test overridding grouped input types which controls generation of "for"
  170. * attribute of labels.
  171. */
  172. public function testConstructWithGroupedInputTypes(): void
  173. {
  174. $helper = new FormHelper($this->View, [
  175. 'groupedInputTypes' => ['radio'],
  176. ]);
  177. $result = $helper->control('when', ['type' => 'datetime-local']);
  178. $this->assertStringContainsString('<label for="when">When</label>', $result);
  179. }
  180. /**
  181. * Test registering a new widget class and rendering it.
  182. */
  183. public function testAddWidgetAndRenderWidget(): void
  184. {
  185. $data = [
  186. 'val' => 1,
  187. ];
  188. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  189. $this->Form->addWidget('test', $mock);
  190. $mock->expects($this->once())
  191. ->method('render')
  192. ->with($data)
  193. ->will($this->returnValue('HTML'));
  194. $result = $this->Form->widget('test', $data);
  195. $this->assertSame('HTML', $result);
  196. }
  197. /**
  198. * Test that secureFields() of widget is called after calling render(),
  199. * not before.
  200. */
  201. public function testOrderForRenderingWidgetAndFetchingSecureFields(): void
  202. {
  203. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  204. 'unlockedFields' => [],
  205. ]));
  206. $data = [
  207. 'val' => 1,
  208. 'name' => 'test',
  209. ];
  210. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  211. $this->Form->addWidget('test', $mock);
  212. $mock->expects($this->once())
  213. ->method('render')
  214. ->with($data)
  215. ->will($this->returnValue('HTML'));
  216. $mock->expects($this->once())
  217. ->method('secureFields')
  218. ->with($data)
  219. ->will($this->returnValue(['test']));
  220. $this->Form->create();
  221. $result = $this->Form->widget('test', $data + ['secure' => true]);
  222. $this->assertSame('HTML', $result);
  223. }
  224. /**
  225. * Test that empty string is not added to secure fields list when
  226. * rendering input widget without name.
  227. */
  228. public function testRenderingWidgetWithEmptyName(): void
  229. {
  230. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  231. $this->Form->create();
  232. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  233. $this->assertSame('<select name=""></select>', $result);
  234. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  235. $this->assertEquals([], $result);
  236. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  237. $this->assertSame('<select name="0"></select>', $result);
  238. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  239. $this->assertEquals(['0'], $result);
  240. }
  241. /**
  242. * Test adding a new context class.
  243. */
  244. public function testAddContextProvider(): void
  245. {
  246. $context = 'My data';
  247. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  248. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  249. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  250. $this->assertSame($context, $data['entity']);
  251. return $stub;
  252. });
  253. $this->Form->create($context);
  254. $result = $this->Form->context();
  255. $this->assertSame($stub, $result);
  256. }
  257. /**
  258. * Test replacing a context class.
  259. */
  260. public function testAddContextProviderReplace(): void
  261. {
  262. $entity = new Article();
  263. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  264. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  265. return $stub;
  266. });
  267. $this->Form->create($entity);
  268. $result = $this->Form->context();
  269. $this->assertSame($stub, $result);
  270. }
  271. /**
  272. * Test overriding a context class.
  273. */
  274. public function testAddContextProviderAdd(): void
  275. {
  276. $entity = new Article();
  277. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  278. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  279. if ($data['entity'] instanceof Entity) {
  280. return $stub;
  281. }
  282. });
  283. $this->Form->create($entity);
  284. $result = $this->Form->context();
  285. $this->assertSame($stub, $result);
  286. }
  287. /**
  288. * Provides context options for create().
  289. *
  290. * @return array
  291. */
  292. public static function contextSelectionProvider(): array
  293. {
  294. $entity = new Article();
  295. $collection = new Collection([$entity]);
  296. $emptyCollection = new Collection([]);
  297. $data = [
  298. 'schema' => [
  299. 'title' => ['type' => 'string'],
  300. ],
  301. ];
  302. $form = new Form();
  303. $custom = new StubContext();
  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, $custom::class],
  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 create() with the templates option.
  438. */
  439. public function testCreateTemplatesRequiredClass(): void
  440. {
  441. $this->Form->create($this->article, [
  442. 'templates' => [
  443. 'requiredClass' => 'is-required',
  444. ],
  445. ]);
  446. $result = $this->Form->control('title');
  447. $expected = [
  448. 'div' => ['class' => 'input text is-required'],
  449. 'label' => ['for' => 'title'],
  450. 'Title',
  451. '/label',
  452. 'input' => [
  453. 'type' => 'text',
  454. 'name' => 'title',
  455. 'id' => 'title',
  456. 'required' => 'required',
  457. 'data-validity-message' => 'This field cannot be left empty',
  458. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  459. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  460. 'aria-required' => 'true',
  461. ],
  462. '/div',
  463. ];
  464. $this->assertHtml($expected, $result);
  465. }
  466. /**
  467. * Test using template vars in various templates used by control() method.
  468. */
  469. public function testControlTemplateVars(): void
  470. {
  471. $result = $this->Form->control('text', [
  472. 'templates' => [
  473. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  474. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  475. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  476. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  477. ],
  478. 'templateVars' => [
  479. 'forinput' => 'in-input',
  480. 'forlabel' => 'in-label',
  481. 'forgroup' => 'in-group',
  482. 'forcontainer' => 'in-container',
  483. ],
  484. ]);
  485. $expected = [
  486. 'div' => ['class'],
  487. 'label' => ['for'],
  488. 'Text in-label',
  489. '/label',
  490. 'in-group',
  491. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  492. 'in-container',
  493. '/div',
  494. ];
  495. $this->assertHtml($expected, $result);
  496. }
  497. /**
  498. * Test ensuring template variables work in template files loaded
  499. * during control().
  500. */
  501. public function testControlTemplatesFromFile(): void
  502. {
  503. $result = $this->Form->control('title', [
  504. 'templates' => 'test_templates',
  505. 'templateVars' => [
  506. 'forcontainer' => 'container-data',
  507. ],
  508. ]);
  509. $expected = [
  510. 'div' => ['class'],
  511. 'label' => ['for'],
  512. 'Title',
  513. '/label',
  514. 'input' => ['name', 'type' => 'text', 'id'],
  515. 'container-data',
  516. '/div',
  517. ];
  518. $this->assertHtml($expected, $result);
  519. }
  520. /**
  521. * Test using template vars in inputSubmit and submitContainer template.
  522. */
  523. public function testSubmitTemplateVars(): void
  524. {
  525. $this->Form->setTemplates([
  526. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  527. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  528. ]);
  529. $result = $this->Form->submit('Submit', [
  530. 'templateVars' => [
  531. 'forinput' => 'in-input',
  532. 'forcontainer' => 'in-container',
  533. ],
  534. ]);
  535. $expected = [
  536. 'div' => ['class'],
  537. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  538. 'in-container',
  539. '/div',
  540. ];
  541. $this->assertHtml($expected, $result);
  542. }
  543. /**
  544. * test the create() method
  545. *
  546. * @dataProvider requestTypeProvider
  547. */
  548. public function testCreateTypeOptions(string $type, string $method, string $override): void
  549. {
  550. $encoding = strtolower(Configure::read('App.encoding'));
  551. $result = $this->Form->create(null, ['type' => $type]);
  552. $expected = [
  553. 'form' => [
  554. 'method' => $method, 'action' => '/articles/add',
  555. 'accept-charset' => $encoding,
  556. ],
  557. ];
  558. $extra = [
  559. 'div' => ['style' => 'display:none;'],
  560. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  561. '/div',
  562. ];
  563. if ($type !== 'post') {
  564. $expected = array_merge($expected, $extra);
  565. }
  566. $this->assertHtml($expected, $result);
  567. }
  568. /**
  569. * Test using template vars in Create (formStart template)
  570. */
  571. public function testCreateTemplateVars(): void
  572. {
  573. $result = $this->Form->create($this->article, [
  574. 'templates' => [
  575. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  576. ],
  577. 'templateVars' => ['header' => 'headertext'],
  578. ]);
  579. $expected = [
  580. 'h4' => ['class'],
  581. 'headertext',
  582. '/h4',
  583. 'form' => [
  584. 'method' => 'post',
  585. 'action' => '/articles/add',
  586. 'accept-charset' => 'utf-8',
  587. ],
  588. ];
  589. $this->assertHtml($expected, $result);
  590. }
  591. /**
  592. * Test opening a form for an update operation.
  593. */
  594. public function testCreateUpdateForm(): void
  595. {
  596. $encoding = strtolower(Configure::read('App.encoding'));
  597. $this->View->setRequest($this->View->getRequest()
  598. ->withRequestTarget('/articles/edit/1')
  599. ->withParam('action', 'edit'));
  600. $this->article['defaults']['id'] = 1;
  601. $result = $this->Form->create($this->article);
  602. $expected = [
  603. 'form' => [
  604. 'method' => 'post',
  605. 'action' => '/articles/edit/1',
  606. 'accept-charset' => $encoding,
  607. ],
  608. 'div' => ['style' => 'display:none;'],
  609. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  610. '/div',
  611. ];
  612. $this->assertHtml($expected, $result);
  613. }
  614. /**
  615. * test create() with automatic url generation
  616. */
  617. public function testCreateAutoUrl(): void
  618. {
  619. $encoding = strtolower(Configure::read('App.encoding'));
  620. $this->View->setRequest($this->View->getRequest()
  621. ->withRequestTarget('/articles/delete/10')
  622. ->withParam('action', 'delete'));
  623. $result = $this->Form->create($this->article);
  624. $expected = [
  625. 'form' => [
  626. 'method' => 'post', 'action' => '/articles/delete/10',
  627. 'accept-charset' => $encoding,
  628. ],
  629. ];
  630. $this->assertHtml($expected, $result);
  631. $this->article['defaults'] = ['id' => 1];
  632. $this->View->setRequest($this->View->getRequest()
  633. ->withRequestTarget('/Articles/edit/1')
  634. ->withParam('action', 'delete'));
  635. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  636. $expected = [
  637. 'form' => [
  638. 'method' => 'post',
  639. 'action' => '/Articles/edit/1',
  640. 'accept-charset' => $encoding,
  641. ],
  642. 'div' => ['style' => 'display:none;'],
  643. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  644. '/div',
  645. ];
  646. $this->assertHtml($expected, $result);
  647. $this->View->setRequest($this->View->getRequest()
  648. ->withParam('action', 'add'));
  649. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  650. $expected = [
  651. 'form' => [
  652. 'method' => 'post',
  653. 'action' => '/Articles/publish/1',
  654. 'accept-charset' => $encoding,
  655. ],
  656. 'div' => ['style' => 'display:none;'],
  657. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  658. '/div',
  659. ];
  660. $this->assertHtml($expected, $result);
  661. $result = $this->Form->create($this->article, ['url' => '/Articles/publish']);
  662. $expected = [
  663. 'form' => ['method' => 'post', 'action' => '/Articles/publish', 'accept-charset' => $encoding],
  664. 'div' => ['style' => 'display:none;'],
  665. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  666. '/div',
  667. ];
  668. $this->assertHtml($expected, $result);
  669. $this->View->setRequest($this->View->getRequest()
  670. ->withParam('controller', 'Pages'));
  671. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  672. $expected = [
  673. 'form' => [
  674. 'method' => 'post', 'action' => '/Pages/signup/1',
  675. 'accept-charset' => $encoding,
  676. ],
  677. 'div' => ['style' => 'display:none;'],
  678. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  679. '/div',
  680. ];
  681. $this->assertHtml($expected, $result);
  682. }
  683. /**
  684. * Test create() with no URL (no "action" attribute for <form> tag)
  685. */
  686. public function testCreateNoUrl(): void
  687. {
  688. $result = $this->Form->create(null, ['url' => false]);
  689. $expected = [
  690. 'form' => [
  691. 'method' => 'post',
  692. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  693. ],
  694. ];
  695. $this->assertHtml($expected, $result);
  696. }
  697. /**
  698. * test create() with a custom route
  699. */
  700. public function testCreateCustomRoute(): void
  701. {
  702. $builder = Router::createRouteBuilder('/');
  703. $builder->connect('/login', ['controller' => 'Users', 'action' => 'login']);
  704. $encoding = strtolower(Configure::read('App.encoding'));
  705. $this->View->setRequest($this->View->getRequest()
  706. ->withParam('controller', 'Users'));
  707. $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
  708. $expected = [
  709. 'form' => [
  710. 'method' => 'post', 'action' => '/login',
  711. 'accept-charset' => $encoding,
  712. ],
  713. ];
  714. $this->assertHtml($expected, $result);
  715. $builder->connect(
  716. '/new-article',
  717. ['controller' => 'Articles', 'action' => 'myAction'],
  718. ['_name' => 'my-route']
  719. );
  720. $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
  721. $expected = [
  722. 'form' => [
  723. 'method' => 'post', 'action' => '/new-article',
  724. 'accept-charset' => $encoding,
  725. ],
  726. ];
  727. $this->assertHtml($expected, $result);
  728. }
  729. /**
  730. * test automatic accept-charset overriding
  731. */
  732. public function testCreateWithAcceptCharset(): void
  733. {
  734. $result = $this->Form->create(
  735. $this->article,
  736. [
  737. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  738. ]
  739. );
  740. $expected = [
  741. 'form' => [
  742. 'method' => 'post', 'action' => '/Articles',
  743. 'accept-charset' => 'iso-8859-1',
  744. ],
  745. ];
  746. $this->assertHtml($expected, $result);
  747. }
  748. /**
  749. * Test base form URL when 'url' param is passed with multiple parameters (&)
  750. */
  751. public function testCreateQueryStringRequest(): void
  752. {
  753. $encoding = strtolower(Configure::read('App.encoding'));
  754. $result = $this->Form->create($this->article, [
  755. 'type' => 'post',
  756. 'escape' => false,
  757. 'url' => [
  758. 'controller' => 'Controller',
  759. 'action' => 'action',
  760. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  761. ],
  762. ]);
  763. $expected = [
  764. 'form' => [
  765. 'method' => 'post',
  766. 'action' => '/Controller/action?param1=value1&amp;param2=value2',
  767. 'accept-charset' => $encoding,
  768. ],
  769. ];
  770. $this->assertHtml($expected, $result);
  771. $result = $this->Form->create($this->article, [
  772. 'type' => 'post',
  773. 'url' => [
  774. 'controller' => 'Controller',
  775. 'action' => 'action',
  776. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  777. ],
  778. ]);
  779. $this->assertHtml($expected, $result);
  780. }
  781. /**
  782. * test that create() doesn't cause errors by multiple id's being in the primary key
  783. * as could happen with multiple select or checkboxes.
  784. */
  785. public function testCreateWithMultipleIdInData(): void
  786. {
  787. $encoding = strtolower(Configure::read('App.encoding'));
  788. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  789. $result = $this->Form->create($this->article);
  790. $expected = [
  791. 'form' => [
  792. 'method' => 'post',
  793. 'action' => '/articles/add',
  794. 'accept-charset' => $encoding,
  795. ],
  796. ];
  797. $this->assertHtml($expected, $result);
  798. }
  799. /**
  800. * test that create() doesn't add in extra passed params.
  801. */
  802. public function testCreatePassedArgs(): void
  803. {
  804. $encoding = strtolower(Configure::read('App.encoding'));
  805. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  806. $result = $this->Form->create($this->article, [
  807. 'type' => 'post',
  808. 'escape' => false,
  809. 'url' => [
  810. 'action' => 'edit',
  811. 'myparam',
  812. ],
  813. ]);
  814. $expected = [
  815. 'form' => [
  816. 'method' => 'post',
  817. 'action' => '/Articles/edit/myparam',
  818. 'accept-charset' => $encoding,
  819. ],
  820. ];
  821. $this->assertHtml($expected, $result);
  822. }
  823. /**
  824. * test creating a get form, and get form inputs.
  825. */
  826. public function testGetFormCreate(): void
  827. {
  828. $encoding = strtolower(Configure::read('App.encoding'));
  829. $result = $this->Form->create($this->article, ['type' => 'get']);
  830. $expected = ['form' => [
  831. 'method' => 'get', 'action' => '/articles/add',
  832. 'accept-charset' => $encoding,
  833. ]];
  834. $this->assertHtml($expected, $result);
  835. $result = $this->Form->text('title');
  836. $expected = ['input' => [
  837. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  838. ]];
  839. $this->assertHtml($expected, $result);
  840. $result = $this->Form->password('password');
  841. $expected = ['input' => [
  842. 'name' => 'password', 'type' => 'password',
  843. ]];
  844. $this->assertHtml($expected, $result);
  845. $this->assertDoesNotMatchRegularExpression('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  846. $result = $this->Form->text('user_form');
  847. $expected = ['input' => [
  848. 'name' => 'user_form', 'type' => 'text',
  849. ]];
  850. $this->assertHtml($expected, $result);
  851. }
  852. /**
  853. * test get form, and inputs when the model param is false
  854. */
  855. public function testGetFormWithFalseModel(): void
  856. {
  857. $encoding = strtolower(Configure::read('App.encoding'));
  858. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'ContactTest'));
  859. $result = $this->Form->create(null, [
  860. 'type' => 'get', 'url' => ['controller' => 'ContactTest'],
  861. ]);
  862. $expected = ['form' => [
  863. 'method' => 'get', 'action' => '/ContactTest/add',
  864. 'accept-charset' => $encoding,
  865. ]];
  866. $this->assertHtml($expected, $result);
  867. $result = $this->Form->text('reason');
  868. $expected = [
  869. 'input' => ['type' => 'text', 'name' => 'reason'],
  870. ];
  871. $this->assertHtml($expected, $result);
  872. }
  873. /**
  874. * testFormCreateWithSecurity method
  875. *
  876. * Test form->create() with security key.
  877. */
  878. public function testCreateWithSecurity(): void
  879. {
  880. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  881. $encoding = strtolower(Configure::read('App.encoding'));
  882. $result = $this->Form->create($this->article, [
  883. 'url' => '/articles/publish',
  884. ]);
  885. $expected = [
  886. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  887. 'div' => ['style' => 'display:none;'],
  888. ['input' => [
  889. 'type' => 'hidden',
  890. 'name' => '_csrfToken',
  891. 'value' => 'testKey',
  892. ]],
  893. '/div',
  894. ];
  895. $this->assertHtml($expected, $result);
  896. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  897. $expected['form']['id'] = 'MyForm';
  898. $this->assertHtml($expected, $result);
  899. }
  900. /**
  901. * testFormCreateGetNoSecurity method
  902. *
  903. * Test form->create() with no security key as its a get form
  904. */
  905. public function testCreateEndGetNoSecurity(): void
  906. {
  907. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  908. $article = new Article();
  909. $result = $this->Form->create($article, [
  910. 'type' => 'get',
  911. 'url' => '/contacts/add',
  912. ]);
  913. $this->assertStringNotContainsString('testKey', $result);
  914. $result = $this->Form->end();
  915. $this->assertStringNotContainsString('testKey', $result);
  916. }
  917. /**
  918. * Tests form hash generation with model-less data
  919. */
  920. public function testValidateHashNoModel(): void
  921. {
  922. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  923. $fields = ['anything'];
  924. $this->Form->create();
  925. $result = $this->Form->secure($fields);
  926. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  927. $this->assertStringContainsString($hash, $result);
  928. }
  929. /**
  930. * Tests that hidden fields generated for checkboxes don't get locked
  931. */
  932. public function testNoCheckboxLocking(): void
  933. {
  934. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  935. $this->Form->create();
  936. $this->assertSame([], $this->Form->getFormProtector()->__debugInfo()['fields']);
  937. $this->Form->checkbox('check', ['value' => '1']);
  938. $this->assertSame(['check'], $this->Form->getFormProtector()->__debugInfo()['fields']);
  939. }
  940. /**
  941. * testFormSecurityFields method
  942. *
  943. * Test generation of secure form hash generation.
  944. */
  945. public function testFormSecurityFields(): void
  946. {
  947. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  948. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  949. $this->Form->create();
  950. $result = $this->Form->secure($fields);
  951. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  952. $hash .= ':' . 'Model.valid';
  953. $hash = urlencode($hash);
  954. $tokenDebug = urlencode(json_encode([
  955. $this->url,
  956. $fields,
  957. [],
  958. ]));
  959. $expected = [
  960. 'div' => ['style' => 'display:none;'],
  961. ['input' => [
  962. 'type' => 'hidden',
  963. 'name' => '_Token[fields]',
  964. 'value' => $hash,
  965. ]],
  966. ['input' => [
  967. 'type' => 'hidden',
  968. 'name' => '_Token[unlocked]',
  969. 'value' => '',
  970. ]],
  971. ['input' => [
  972. 'type' => 'hidden',
  973. 'name' => '_Token[debug]',
  974. 'value' => $tokenDebug,
  975. ]],
  976. '/div',
  977. ];
  978. $this->assertHtml($expected, $result);
  979. }
  980. /**
  981. * testFormSecurityFields method
  982. *
  983. * Test debug token is not generated if debug is false
  984. */
  985. public function testFormSecurityFieldsNoDebugMode(): void
  986. {
  987. Configure::write('debug', false);
  988. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  989. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  990. $this->Form->create();
  991. $result = $this->Form->secure($fields);
  992. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  993. $hash .= ':' . 'Model.valid';
  994. $hash = urlencode($hash);
  995. $expected = [
  996. 'div' => ['style' => 'display:none;'],
  997. ['input' => [
  998. 'type' => 'hidden',
  999. 'name' => '_Token[fields]',
  1000. 'value' => $hash,
  1001. ]],
  1002. ['input' => [
  1003. 'type' => 'hidden',
  1004. 'name' => '_Token[unlocked]',
  1005. 'value' => '',
  1006. ]],
  1007. '/div',
  1008. ];
  1009. $this->assertHtml($expected, $result);
  1010. }
  1011. /**
  1012. * Tests correct generation of number fields for smallint
  1013. */
  1014. public function testTextFieldGenerationForSmallint(): void
  1015. {
  1016. $this->article['schema'] = [
  1017. 'foo' => [
  1018. 'type' => 'smallinteger',
  1019. 'null' => false,
  1020. 'default' => null,
  1021. 'length' => 10,
  1022. ],
  1023. ];
  1024. $this->Form->create($this->article);
  1025. $result = $this->Form->control('foo');
  1026. $this->assertStringContainsString('class="input number"', $result);
  1027. $this->assertStringContainsString('type="number"', $result);
  1028. }
  1029. /**
  1030. * Tests correct generation of number fields for tinyint
  1031. */
  1032. public function testTextFieldGenerationForTinyint(): void
  1033. {
  1034. $this->article['schema'] = [
  1035. 'foo' => [
  1036. 'type' => 'tinyinteger',
  1037. 'null' => false,
  1038. 'default' => null,
  1039. 'length' => 10,
  1040. ],
  1041. ];
  1042. $this->Form->create($this->article);
  1043. $result = $this->Form->control('foo');
  1044. $this->assertStringContainsString('class="input number"', $result);
  1045. $this->assertStringContainsString('type="number"', $result);
  1046. }
  1047. /**
  1048. * Tests correct generation of number fields for double and float fields
  1049. */
  1050. public function testTextFieldGenerationForFloats(): void
  1051. {
  1052. $this->article['schema'] = [
  1053. 'foo' => [
  1054. 'type' => 'float',
  1055. 'null' => false,
  1056. 'default' => null,
  1057. 'length' => 10,
  1058. ],
  1059. ];
  1060. $this->Form->create($this->article);
  1061. $result = $this->Form->control('foo');
  1062. $expected = [
  1063. 'div' => ['class' => 'input number'],
  1064. 'label' => ['for' => 'foo'],
  1065. 'Foo',
  1066. '/label',
  1067. ['input' => [
  1068. 'type' => 'number',
  1069. 'name' => 'foo',
  1070. 'id' => 'foo',
  1071. 'step' => 'any',
  1072. ]],
  1073. '/div',
  1074. ];
  1075. $this->assertHtml($expected, $result);
  1076. $result = $this->Form->control('foo', ['step' => 0.5]);
  1077. $expected = [
  1078. 'div' => ['class' => 'input number'],
  1079. 'label' => ['for' => 'foo'],
  1080. 'Foo',
  1081. '/label',
  1082. ['input' => [
  1083. 'type' => 'number',
  1084. 'name' => 'foo',
  1085. 'id' => 'foo',
  1086. 'step' => '0.5',
  1087. ]],
  1088. '/div',
  1089. ];
  1090. $this->assertHtml($expected, $result);
  1091. }
  1092. /**
  1093. * Tests correct generation of number fields for integer fields
  1094. */
  1095. public function testTextFieldTypeNumberGenerationForIntegers(): void
  1096. {
  1097. $this->getTableLocator()->get('Contacts', [
  1098. 'className' => ContactsTable::class,
  1099. ]);
  1100. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1101. $result = $this->Form->control('age');
  1102. $expected = [
  1103. 'div' => ['class' => 'input number'],
  1104. 'label' => ['for' => 'age'],
  1105. 'Age',
  1106. '/label',
  1107. ['input' => [
  1108. 'type' => 'number', 'name' => 'age',
  1109. 'id' => 'age',
  1110. ]],
  1111. '/div',
  1112. ];
  1113. $this->assertHtml($expected, $result);
  1114. }
  1115. /**
  1116. * Tests correct generation of file upload fields for binary fields
  1117. */
  1118. public function testFileUploadFieldTypeGenerationForBinaries(): void
  1119. {
  1120. $table = $this->getTableLocator()->get('Contacts', [
  1121. 'className' => ContactsTable::class,
  1122. ]);
  1123. $table->setSchema(['foo' => [
  1124. 'type' => 'binary',
  1125. 'null' => false,
  1126. 'default' => null,
  1127. 'length' => 1024,
  1128. ]]);
  1129. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1130. $result = $this->Form->control('foo');
  1131. $expected = [
  1132. 'div' => ['class' => 'input file'],
  1133. 'label' => ['for' => 'foo'],
  1134. 'Foo',
  1135. '/label',
  1136. ['input' => [
  1137. 'type' => 'file', 'name' => 'foo',
  1138. 'id' => 'foo',
  1139. ]],
  1140. '/div',
  1141. ];
  1142. $this->assertHtml($expected, $result);
  1143. }
  1144. /**
  1145. * testFormSecurityMultipleFields method
  1146. *
  1147. * Test secure() with multiple row form. Ensure hash is correct.
  1148. */
  1149. public function testFormSecurityMultipleFields(): void
  1150. {
  1151. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1152. $this->Form->create();
  1153. $fields = [
  1154. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1155. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1156. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1157. ];
  1158. $result = $this->Form->secure($fields);
  1159. $sortedFields = [
  1160. 'Model.0.password',
  1161. 'Model.0.username',
  1162. 'Model.1.password',
  1163. 'Model.1.username',
  1164. 'Model.0.hidden' => 'value',
  1165. 'Model.0.valid' => '0',
  1166. 'Model.1.hidden' => 'value',
  1167. 'Model.1.valid' => '0',
  1168. ];
  1169. $hash = hash_hmac('sha1', $this->url . serialize($sortedFields) . session_id(), Security::getSalt());
  1170. $hash .= ':Model.0.hidden|Model.0.valid|Model.1.hidden|Model.1.valid';
  1171. $hash = urlencode($hash);
  1172. $tokenDebug = urlencode(json_encode([
  1173. $this->url,
  1174. $fields,
  1175. [],
  1176. ]));
  1177. $expected = [
  1178. 'div' => ['style' => 'display:none;'],
  1179. ['input' => [
  1180. 'type' => 'hidden',
  1181. 'name' => '_Token[fields]',
  1182. 'value' => $hash,
  1183. ]],
  1184. ['input' => [
  1185. 'type' => 'hidden',
  1186. 'name' => '_Token[unlocked]',
  1187. 'value' => '',
  1188. ]],
  1189. ['input' => [
  1190. 'type' => 'hidden',
  1191. 'name' => '_Token[debug]',
  1192. 'value' => $tokenDebug,
  1193. ]],
  1194. '/div',
  1195. ];
  1196. $this->assertHtml($expected, $result);
  1197. }
  1198. /**
  1199. * testFormSecurityMultipleSubmitButtons
  1200. *
  1201. * test form submit generation and ensure that _Token is only created on end()
  1202. */
  1203. public function testFormSecurityMultipleSubmitButtons(): void
  1204. {
  1205. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1206. $this->Form->create($this->article);
  1207. $this->Form->text('Address.title');
  1208. $this->Form->text('Address.first_name');
  1209. $result = $this->Form->submit('Save', ['name' => 'save']);
  1210. $expected = [
  1211. 'div' => ['class' => 'submit'],
  1212. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1213. '/div',
  1214. ];
  1215. $this->assertHtml($expected, $result);
  1216. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1217. $expected = [
  1218. 'div' => ['class' => 'submit'],
  1219. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1220. '/div',
  1221. ];
  1222. $this->assertHtml($expected, $result);
  1223. $result = $this->Form->end();
  1224. $tokenDebug = urlencode(json_encode([
  1225. '/articles/add',
  1226. [
  1227. 'Address.title',
  1228. 'Address.first_name',
  1229. ],
  1230. ['save', 'cancel'],
  1231. ]));
  1232. $expected = [
  1233. 'div' => ['style' => 'display:none;'],
  1234. ['input' => [
  1235. 'type' => 'hidden',
  1236. 'name' => '_Token[fields]',
  1237. 'value',
  1238. ]],
  1239. ['input' => [
  1240. 'type' => 'hidden',
  1241. 'name' => '_Token[unlocked]',
  1242. 'value' => 'cancel%7Csave',
  1243. ]],
  1244. ['input' => [
  1245. 'type' => 'hidden',
  1246. 'name' => '_Token[debug]',
  1247. 'value' => $tokenDebug,
  1248. ]],
  1249. '/div',
  1250. ];
  1251. $this->assertHtml($expected, $result);
  1252. }
  1253. /**
  1254. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1255. */
  1256. public function testSecurityButtonNestedNamed(): void
  1257. {
  1258. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1259. $this->Form->create();
  1260. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1261. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1262. $this->assertEquals(['Address.button'], $result);
  1263. }
  1264. /**
  1265. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1266. */
  1267. public function testSecuritySubmitNestedNamed(): void
  1268. {
  1269. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1270. $this->Form->create($this->article);
  1271. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1272. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1273. $this->assertEquals(['Address.button'], $result);
  1274. }
  1275. /**
  1276. * Test that the correct fields are unlocked for image submits with no names.
  1277. */
  1278. public function testSecuritySubmitImageNoName(): void
  1279. {
  1280. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1281. $this->Form->create();
  1282. $result = $this->Form->submit('save.png');
  1283. $expected = [
  1284. 'div' => ['class' => 'submit'],
  1285. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1286. '/div',
  1287. ];
  1288. $this->assertHtml($expected, $result);
  1289. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1290. $this->assertEquals(['x', 'y'], $result);
  1291. }
  1292. /**
  1293. * Test that the correct fields are unlocked for image submits with names.
  1294. */
  1295. public function testSecuritySubmitImageName(): void
  1296. {
  1297. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1298. $this->Form->create();
  1299. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1300. $expected = [
  1301. 'div' => ['class' => 'submit'],
  1302. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1303. '/div',
  1304. ];
  1305. $this->assertHtml($expected, $result);
  1306. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1307. $this->assertEquals(['test', 'test_x', 'test_y'], $result);
  1308. }
  1309. /**
  1310. * testFormSecurityMultipleControlFields method
  1311. *
  1312. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1313. */
  1314. public function testFormSecurityMultipleControlFields(): void
  1315. {
  1316. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1317. $this->Form->create();
  1318. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1319. $this->Form->control('Addresses.0.title');
  1320. $this->Form->control('Addresses.0.first_name');
  1321. $this->Form->control('Addresses.0.last_name');
  1322. $this->Form->control('Addresses.0.address');
  1323. $this->Form->control('Addresses.0.city');
  1324. $this->Form->control('Addresses.0.phone');
  1325. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1326. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1327. $this->Form->control('Addresses.1.title');
  1328. $this->Form->control('Addresses.1.first_name');
  1329. $this->Form->control('Addresses.1.last_name');
  1330. $this->Form->control('Addresses.1.address');
  1331. $this->Form->control('Addresses.1.city');
  1332. $this->Form->control('Addresses.1.phone');
  1333. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1334. $result = $this->Form->secure();
  1335. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1336. $tokenDebug = urlencode(json_encode([
  1337. '/articles/add',
  1338. [
  1339. 'Addresses.0.id' => '123456',
  1340. 'Addresses.0.title',
  1341. 'Addresses.0.first_name',
  1342. 'Addresses.0.last_name',
  1343. 'Addresses.0.address',
  1344. 'Addresses.0.city',
  1345. 'Addresses.0.phone',
  1346. 'Addresses.0.primary',
  1347. 'Addresses.1.id' => '654321',
  1348. 'Addresses.1.title',
  1349. 'Addresses.1.first_name',
  1350. 'Addresses.1.last_name',
  1351. 'Addresses.1.address',
  1352. 'Addresses.1.city',
  1353. 'Addresses.1.phone',
  1354. 'Addresses.1.primary',
  1355. ],
  1356. [],
  1357. ]));
  1358. $expected = [
  1359. 'div' => ['style' => 'display:none;'],
  1360. ['input' => [
  1361. 'type' => 'hidden',
  1362. 'name' => '_Token[fields]',
  1363. 'value' => $hash,
  1364. ]],
  1365. ['input' => [
  1366. 'type' => 'hidden',
  1367. 'name' => '_Token[unlocked]',
  1368. 'value' => '',
  1369. ]],
  1370. ['input' => [
  1371. 'type' => 'hidden',
  1372. 'name' => '_Token[debug]',
  1373. 'value' => $tokenDebug,
  1374. ]],
  1375. '/div',
  1376. ];
  1377. $this->assertHtml($expected, $result);
  1378. }
  1379. /**
  1380. * testFormSecurityArrayFields method
  1381. *
  1382. * Test form security with Model.field.0 style inputs.
  1383. */
  1384. public function testFormSecurityArrayFields(): void
  1385. {
  1386. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1387. $this->Form->create();
  1388. $this->Form->text('Address.primary.1');
  1389. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1390. $this->assertSame('Address.primary', $result[0]);
  1391. $this->Form->text('Address.secondary.1.0');
  1392. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1393. $this->assertSame('Address.secondary', $result[1]);
  1394. }
  1395. /**
  1396. * testFormSecurityMultipleControlDisabledFields method
  1397. *
  1398. * Test secure form generation with multiple records and disabled fields.
  1399. */
  1400. public function testFormSecurityMultipleControlDisabledFields(): void
  1401. {
  1402. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1403. 'unlockedFields' => ['first_name', 'address'],
  1404. ]));
  1405. $this->Form->create();
  1406. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1407. $this->Form->text('Addresses.0.title');
  1408. $this->Form->text('Addresses.0.first_name');
  1409. $this->Form->text('Addresses.0.last_name');
  1410. $this->Form->text('Addresses.0.address');
  1411. $this->Form->text('Addresses.0.city');
  1412. $this->Form->text('Addresses.0.phone');
  1413. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1414. $this->Form->text('Addresses.1.title');
  1415. $this->Form->text('Addresses.1.first_name');
  1416. $this->Form->text('Addresses.1.last_name');
  1417. $this->Form->text('Addresses.1.address');
  1418. $this->Form->text('Addresses.1.city');
  1419. $this->Form->text('Addresses.1.phone');
  1420. $result = $this->Form->secure();
  1421. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1422. $tokenDebug = urlencode(json_encode([
  1423. '/articles/add',
  1424. [
  1425. 'Addresses.0.id' => '123456',
  1426. 'Addresses.0.title',
  1427. 'Addresses.0.last_name',
  1428. 'Addresses.0.city',
  1429. 'Addresses.0.phone',
  1430. 'Addresses.1.id' => '654321',
  1431. 'Addresses.1.title',
  1432. 'Addresses.1.last_name',
  1433. 'Addresses.1.city',
  1434. 'Addresses.1.phone',
  1435. ],
  1436. [
  1437. 'first_name',
  1438. 'address',
  1439. ],
  1440. ]));
  1441. $expected = [
  1442. 'div' => ['style' => 'display:none;'],
  1443. ['input' => [
  1444. 'type' => 'hidden',
  1445. 'name' => '_Token[fields]',
  1446. 'value' => $hash,
  1447. ]],
  1448. ['input' => [
  1449. 'type' => 'hidden',
  1450. 'name' => '_Token[unlocked]',
  1451. 'value' => 'address%7Cfirst_name',
  1452. ]],
  1453. ['input' => [
  1454. 'type' => 'hidden',
  1455. 'name' => '_Token[debug]',
  1456. 'value' => $tokenDebug,
  1457. ]],
  1458. '/div',
  1459. ];
  1460. $this->assertHtml($expected, $result);
  1461. }
  1462. /**
  1463. * testFormSecurityControlDisabledFields method
  1464. *
  1465. * Test single record form with disabled fields.
  1466. */
  1467. public function testFormSecurityControlUnlockedFields(): void
  1468. {
  1469. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1470. 'unlockedFields' => ['first_name', 'address'],
  1471. ]));
  1472. $this->Form->create();
  1473. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1474. $this->assertEquals(
  1475. $this->View->getRequest()->getAttribute('formTokenData'),
  1476. ['unlockedFields' => $result]
  1477. );
  1478. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1479. $this->Form->text('Addresses.title');
  1480. $this->Form->text('Addresses.first_name');
  1481. $this->Form->text('Addresses.last_name');
  1482. $this->Form->text('Addresses.address');
  1483. $this->Form->text('Addresses.city');
  1484. $this->Form->text('Addresses.phone');
  1485. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1486. $expected = [
  1487. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1488. 'Addresses.city', 'Addresses.phone',
  1489. ];
  1490. $this->assertEquals($expected, $result);
  1491. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1492. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1493. $tokenDebug = urlencode(json_encode([
  1494. '/articles/add',
  1495. [
  1496. 'Addresses.id' => '123456',
  1497. 'Addresses.title',
  1498. 'Addresses.last_name',
  1499. 'Addresses.city',
  1500. 'Addresses.phone',
  1501. ],
  1502. [
  1503. 'first_name',
  1504. 'address',
  1505. ],
  1506. ]));
  1507. $expected = [
  1508. 'div' => ['style' => 'display:none;'],
  1509. ['input' => [
  1510. 'type' => 'hidden',
  1511. 'name' => '_Token[fields]',
  1512. 'value' => $hash,
  1513. 'data-foo' => 'bar',
  1514. ]],
  1515. ['input' => [
  1516. 'type' => 'hidden',
  1517. 'name' => '_Token[unlocked]',
  1518. 'value' => 'address%7Cfirst_name',
  1519. 'data-foo' => 'bar',
  1520. ]],
  1521. ['input' => [
  1522. 'type' => 'hidden', 'name' => '_Token[debug]',
  1523. 'value' => $tokenDebug,
  1524. 'data-foo' => 'bar',
  1525. ]],
  1526. '/div',
  1527. ];
  1528. $this->assertHtml($expected, $result);
  1529. }
  1530. /**
  1531. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1532. *
  1533. * Test single record form with debugSecurity param.
  1534. */
  1535. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue(): void
  1536. {
  1537. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1538. 'unlockedFields' => ['first_name', 'address'],
  1539. ]));
  1540. $this->Form->create();
  1541. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1542. $this->assertEquals(
  1543. $this->View->getRequest()->getAttribute('formTokenData'),
  1544. ['unlockedFields' => $result]
  1545. );
  1546. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1547. $this->Form->text('Addresses.title');
  1548. $this->Form->text('Addresses.first_name');
  1549. $this->Form->text('Addresses.last_name');
  1550. $this->Form->text('Addresses.address');
  1551. $this->Form->text('Addresses.city');
  1552. $this->Form->text('Addresses.phone');
  1553. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1554. $expected = [
  1555. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1556. 'Addresses.city', 'Addresses.phone',
  1557. ];
  1558. $this->assertEquals($expected, $result);
  1559. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1560. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1561. $tokenDebug = urlencode(json_encode([
  1562. '/articles/add',
  1563. [
  1564. 'Addresses.id' => '123456',
  1565. 'Addresses.title',
  1566. 'Addresses.last_name',
  1567. 'Addresses.city',
  1568. 'Addresses.phone',
  1569. ],
  1570. [
  1571. 'first_name',
  1572. 'address',
  1573. ],
  1574. ]));
  1575. $expected = [
  1576. 'div' => ['style' => 'display:none;'],
  1577. ['input' => [
  1578. 'type' => 'hidden',
  1579. 'name' => '_Token[fields]',
  1580. 'value' => $hash,
  1581. 'data-foo' => 'bar',
  1582. ]],
  1583. ['input' => [
  1584. 'type' => 'hidden',
  1585. 'name' => '_Token[unlocked]',
  1586. 'value' => 'address%7Cfirst_name',
  1587. 'data-foo' => 'bar',
  1588. ]],
  1589. ['input' => [
  1590. 'type' => 'hidden', 'name' => '_Token[debug]',
  1591. 'value' => $tokenDebug,
  1592. 'data-foo' => 'bar',
  1593. ]],
  1594. '/div',
  1595. ];
  1596. $this->assertHtml($expected, $result);
  1597. }
  1598. /**
  1599. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1600. *
  1601. * Debug is false, debugSecurity is true -> no debug
  1602. */
  1603. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse(): void
  1604. {
  1605. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1606. 'unlockedFields' => ['first_name', 'address'],
  1607. ]));
  1608. $this->Form->create();
  1609. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1610. $this->assertEquals(
  1611. $this->View->getRequest()->getAttribute('formTokenData'),
  1612. ['unlockedFields' => $result]
  1613. );
  1614. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1615. $this->Form->text('Addresses.title');
  1616. $this->Form->text('Addresses.first_name');
  1617. $this->Form->text('Addresses.last_name');
  1618. $this->Form->text('Addresses.address');
  1619. $this->Form->text('Addresses.city');
  1620. $this->Form->text('Addresses.phone');
  1621. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1622. $expected = [
  1623. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1624. 'Addresses.city', 'Addresses.phone',
  1625. ];
  1626. $this->assertEquals($expected, $result);
  1627. Configure::write('debug', false);
  1628. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1629. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1630. $expected = [
  1631. 'div' => ['style' => 'display:none;'],
  1632. ['input' => [
  1633. 'type' => 'hidden',
  1634. 'name' => '_Token[fields]',
  1635. 'value' => $hash,
  1636. 'data-foo' => 'bar',
  1637. ]],
  1638. ['input' => [
  1639. 'type' => 'hidden',
  1640. 'name' => '_Token[unlocked]',
  1641. 'value' => 'address%7Cfirst_name',
  1642. 'data-foo' => 'bar',
  1643. ]],
  1644. '/div',
  1645. ];
  1646. $this->assertHtml($expected, $result);
  1647. }
  1648. /**
  1649. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1650. *
  1651. * Test single record form with debugSecurity param.
  1652. */
  1653. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse(): void
  1654. {
  1655. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1656. 'unlockedFields' => ['first_name', 'address'],
  1657. ]));
  1658. $this->Form->create();
  1659. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1660. $this->assertEquals(
  1661. $this->View->getRequest()->getAttribute('formTokenData'),
  1662. ['unlockedFields' => $result]
  1663. );
  1664. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1665. $this->Form->text('Addresses.title');
  1666. $this->Form->text('Addresses.first_name');
  1667. $this->Form->text('Addresses.last_name');
  1668. $this->Form->text('Addresses.address');
  1669. $this->Form->text('Addresses.city');
  1670. $this->Form->text('Addresses.phone');
  1671. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1672. $expected = [
  1673. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1674. 'Addresses.city', 'Addresses.phone',
  1675. ];
  1676. $this->assertEquals($expected, $result);
  1677. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1678. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1679. $expected = [
  1680. 'div' => ['style' => 'display:none;'],
  1681. ['input' => [
  1682. 'type' => 'hidden',
  1683. 'name' => '_Token[fields]',
  1684. 'value' => $hash,
  1685. 'data-foo' => 'bar',
  1686. ]],
  1687. ['input' => [
  1688. 'type' => 'hidden',
  1689. 'name' => '_Token[unlocked]',
  1690. 'value' => 'address%7Cfirst_name',
  1691. 'data-foo' => 'bar',
  1692. ]],
  1693. '/div',
  1694. ];
  1695. $this->assertHtml($expected, $result);
  1696. }
  1697. /**
  1698. * testFormSecureWithCustomNameAttribute method
  1699. *
  1700. * Test securing inputs with custom name attributes.
  1701. */
  1702. public function testFormSecureWithCustomNameAttribute(): void
  1703. {
  1704. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1705. $this->Form->create();
  1706. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1707. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1708. $this->assertSame('User.custom', $result[0]);
  1709. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1710. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1711. $this->assertSame('User.custom.another.value', $result[1]);
  1712. }
  1713. /**
  1714. * testFormSecuredControl method
  1715. *
  1716. * Test generation of entire secure form, assertions made on control() output.
  1717. */
  1718. public function testFormSecuredControl(): void
  1719. {
  1720. $this->View->setRequest($this->View->getRequest()
  1721. ->withAttribute('formTokenData', [])
  1722. ->withAttribute('csrfToken', 'testKey'));
  1723. $this->article['schema'] = [
  1724. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1725. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1726. ];
  1727. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1728. $encoding = strtolower(Configure::read('App.encoding'));
  1729. $expected = [
  1730. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1731. 'div' => ['style' => 'display:none;'],
  1732. ['input' => [
  1733. 'type' => 'hidden',
  1734. 'name' => '_csrfToken',
  1735. 'value' => 'testKey',
  1736. ]],
  1737. '/div',
  1738. ];
  1739. $this->assertHtml($expected, $result);
  1740. $result = $this->Form->control('ratio');
  1741. $expected = [
  1742. 'div' => ['class'],
  1743. 'label' => ['for'],
  1744. 'Ratio',
  1745. '/label',
  1746. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1747. '/div',
  1748. ];
  1749. $this->assertHtml($expected, $result);
  1750. $result = $this->Form->control('population');
  1751. $expected = [
  1752. 'div' => ['class'],
  1753. 'label' => ['for'],
  1754. 'Population',
  1755. '/label',
  1756. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1757. '/div',
  1758. ];
  1759. $this->assertHtml($expected, $result);
  1760. $result = $this->Form->control('published', ['type' => 'text']);
  1761. $expected = [
  1762. 'div' => ['class' => 'input text'],
  1763. 'label' => ['for' => 'published'],
  1764. 'Published',
  1765. '/label',
  1766. ['input' => [
  1767. 'type' => 'text',
  1768. 'name' => 'published',
  1769. 'id' => 'published',
  1770. ]],
  1771. '/div',
  1772. ];
  1773. $this->assertHtml($expected, $result);
  1774. $result = $this->Form->control('other', ['type' => 'text']);
  1775. $expected = [
  1776. 'div' => ['class' => 'input text'],
  1777. 'label' => ['for' => 'other'],
  1778. 'Other',
  1779. '/label',
  1780. ['input' => [
  1781. 'type' => 'text',
  1782. 'name' => 'other',
  1783. 'id',
  1784. ]],
  1785. '/div',
  1786. ];
  1787. $this->assertHtml($expected, $result);
  1788. $result = $this->Form->hidden('stuff');
  1789. $expected = [
  1790. 'input' => [
  1791. 'type' => 'hidden',
  1792. 'name' => 'stuff',
  1793. ],
  1794. ];
  1795. $this->assertHtml($expected, $result);
  1796. $result = $this->Form->hidden('hidden', ['value' => false]);
  1797. $expected = ['input' => [
  1798. 'type' => 'hidden',
  1799. 'name' => 'hidden',
  1800. 'value' => '0',
  1801. ]];
  1802. $this->assertHtml($expected, $result);
  1803. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1804. $expected = [
  1805. 'div' => ['class' => 'input checkbox'],
  1806. ['input' => [
  1807. 'type' => 'hidden',
  1808. 'name' => 'something',
  1809. 'value' => '0',
  1810. ]],
  1811. 'label' => ['for' => 'something'],
  1812. ['input' => [
  1813. 'type' => 'checkbox',
  1814. 'name' => 'something',
  1815. 'value' => '1',
  1816. 'id' => 'something',
  1817. ]],
  1818. 'Something',
  1819. '/label',
  1820. '/div',
  1821. ];
  1822. $this->assertHtml($expected, $result);
  1823. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1824. $expectedFields = [
  1825. 'ratio',
  1826. 'population',
  1827. 'published',
  1828. 'other',
  1829. 'stuff' => '',
  1830. 'hidden' => '0',
  1831. 'something',
  1832. ];
  1833. $this->assertEquals($expectedFields, $result);
  1834. $result = $this->Form->secure();
  1835. $tokenDebug = urlencode(json_encode([
  1836. '/articles/add',
  1837. $expectedFields,
  1838. [],
  1839. ]));
  1840. $expected = [
  1841. 'div' => ['style' => 'display:none;'],
  1842. ['input' => [
  1843. 'type' => 'hidden',
  1844. 'name' => '_Token[fields]',
  1845. 'value',
  1846. ]],
  1847. ['input' => [
  1848. 'type' => 'hidden',
  1849. 'name' => '_Token[unlocked]',
  1850. 'value' => '',
  1851. ]],
  1852. ['input' => [
  1853. 'type' => 'hidden', 'name' => '_Token[debug]',
  1854. 'value' => $tokenDebug,
  1855. ]],
  1856. '/div',
  1857. ];
  1858. $this->assertHtml($expected, $result);
  1859. $data = [
  1860. 'ratio' => '',
  1861. 'population' => '',
  1862. 'published' => '',
  1863. 'other' => '',
  1864. 'stuff' => '',
  1865. 'hidden' => '0',
  1866. 'something' => '',
  1867. '_Token' => $this->Form->getFormProtector()->buildTokenData(),
  1868. ];
  1869. $this->assertTrue($this->Form->getFormProtector()->validate($data, '', ''));
  1870. }
  1871. /**
  1872. * testSecuredControlCustomName method
  1873. *
  1874. * Test secured inputs with custom names.
  1875. */
  1876. public function testSecuredControlCustomName(): void
  1877. {
  1878. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1879. $this->Form->create();
  1880. $this->Form->text('text_input', [
  1881. 'name' => 'Option[General.default_role]',
  1882. ]);
  1883. $expected = ['Option.General.default_role'];
  1884. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1885. $this->assertEquals($expected, $result);
  1886. $this->Form->select('select_box', [1, 2], [
  1887. 'name' => 'Option[General.select_role]',
  1888. ]);
  1889. $expected[] = 'Option.General.select_role';
  1890. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1891. $this->assertEquals($expected, $result);
  1892. $this->Form->text('other.things[]');
  1893. $expected[] = 'other.things';
  1894. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1895. $this->assertEquals($expected, $result);
  1896. }
  1897. /**
  1898. * testSecuredControlDuplicate method
  1899. *
  1900. * Test that a hidden field followed by a visible field
  1901. * undoes the hidden field locking.
  1902. */
  1903. public function testSecuredControlDuplicate(): void
  1904. {
  1905. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1906. $this->Form->create();
  1907. $this->Form->control('text_val', [
  1908. 'type' => 'hidden',
  1909. 'value' => 'some text',
  1910. ]);
  1911. $expected = ['text_val' => 'some text'];
  1912. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1913. $this->assertEquals($expected, $result);
  1914. $this->Form->control('text_val', [
  1915. 'type' => 'text',
  1916. ]);
  1917. $expected = ['text_val'];
  1918. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1919. $this->assertEquals($expected, $result);
  1920. }
  1921. /**
  1922. * testFormSecuredFileControl method
  1923. *
  1924. * Tests that the correct keys are added to the field hash index.
  1925. */
  1926. public function testFormSecuredFileControl(): void
  1927. {
  1928. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1929. $this->Form->create();
  1930. $this->Form->file('Attachment.file');
  1931. $expected = ['Attachment.file'];
  1932. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1933. $this->assertEquals($expected, $result);
  1934. }
  1935. /**
  1936. * testFormSecuredMultipleSelect method
  1937. *
  1938. * Test that multiple selects keys are added to field hash.
  1939. */
  1940. public function testFormSecuredMultipleSelect(): void
  1941. {
  1942. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1943. $this->Form->create();
  1944. $options = ['1' => 'one', '2' => 'two'];
  1945. $this->Form->select('Model.select', $options);
  1946. $expected = ['Model.select'];
  1947. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1948. $this->assertEquals($expected, $result);
  1949. $this->Form->select('Model.select', $options, ['multiple' => true]);
  1950. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1951. $this->assertEquals($expected, $result);
  1952. }
  1953. /**
  1954. * testFormSecuredRadio method
  1955. */
  1956. public function testFormSecuredRadio(): void
  1957. {
  1958. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1959. $this->Form->create();
  1960. $options = ['1' => 'option1', '2' => 'option2'];
  1961. $this->Form->radio('Test.test', $options);
  1962. $expected = ['Test.test'];
  1963. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1964. $this->assertEquals($expected, $result);
  1965. $this->Form->radio('Test.all', $options, [
  1966. 'disabled' => ['option1', 'option2'],
  1967. ]);
  1968. $expected = ['Test.test', 'Test.all' => ''];
  1969. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1970. $this->assertEquals($expected, $result);
  1971. $this->Form->radio('Test.some', $options, [
  1972. 'disabled' => ['option1'],
  1973. ]);
  1974. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  1975. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1976. $this->assertEquals($expected, $result);
  1977. }
  1978. /**
  1979. * testFormSecuredAndDisabled method
  1980. *
  1981. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  1982. * hashing.
  1983. */
  1984. public function testFormSecuredAndDisabled(): void
  1985. {
  1986. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1987. $this->Form->create();
  1988. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  1989. $this->Form->text('Model.text', ['disabled' => true]);
  1990. $this->Form->password('Model.text', ['disabled' => true]);
  1991. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  1992. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  1993. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  1994. $this->Form->year('Model.year', ['disabled' => true]);
  1995. $this->Form->month('Model.month', ['disabled' => true]);
  1996. $this->Form->day('Model.day', ['disabled' => true]);
  1997. $this->Form->hour('Model.hour', ['disabled' => true]);
  1998. $this->Form->minute('Model.minute', ['disabled' => true]);
  1999. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2000. $expected = [
  2001. 'Model.radio' => '',
  2002. ];
  2003. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2004. $this->assertEquals($expected, $result);
  2005. }
  2006. /**
  2007. * testUnlockFieldAddsToList method
  2008. *
  2009. * Test disableField.
  2010. */
  2011. public function testUnlockFieldAddsToList(): void
  2012. {
  2013. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2014. 'unlockedFields' => [],
  2015. ]));
  2016. $this->Form->create();
  2017. $this->Form->unlockField('Contact.name');
  2018. $this->Form->text('Contact.name');
  2019. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2020. $this->assertEquals([], $result);
  2021. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  2022. $this->assertEquals(['Contact.name'], $result);
  2023. }
  2024. /**
  2025. * testUnlockFieldRemovingFromFields method
  2026. *
  2027. * Test unlockField removing from fields array.
  2028. */
  2029. public function testUnlockFieldRemovingFromFields(): void
  2030. {
  2031. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2032. 'unlockedFields' => [],
  2033. ]));
  2034. $this->Form->create($this->article);
  2035. $this->Form->hidden('Article.id', ['value' => 1]);
  2036. $this->Form->text('Article.title');
  2037. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2038. $this->assertSame('1', $result['Article.id'], 'Hidden input should be secured.');
  2039. $this->assertContains('Article.title', $result, 'Field should be secured.');
  2040. $this->Form->unlockField('Article.title');
  2041. $this->Form->unlockField('Article.id');
  2042. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2043. $this->assertEquals([], $result);
  2044. }
  2045. /**
  2046. * testResetUnlockFields method
  2047. *
  2048. * Test reset unlockFields, when create new form.
  2049. */
  2050. public function testResetUnlockFields(): void
  2051. {
  2052. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2053. 'key' => 'testKey',
  2054. 'unlockedFields' => [],
  2055. ]));
  2056. $this->Form->create();
  2057. $this->Form->unlockField('Contact.id');
  2058. $this->Form->hidden('Contact.id', ['value' => 1]);
  2059. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2060. $this->assertEmpty($result, 'Field should be unlocked');
  2061. $this->Form->end();
  2062. $this->Form->create();
  2063. $this->Form->hidden('Contact.id', ['value' => 1]);
  2064. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2065. $this->assertSame('1', $result['Contact.id'], 'Hidden input should be secured.');
  2066. }
  2067. /**
  2068. * testSecuredFormUrlIgnoresHost method
  2069. *
  2070. * Test that only the path + query elements of a form's URL show up in their hash.
  2071. */
  2072. public function testSecuredFormUrlIgnoresHost(): void
  2073. {
  2074. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'testKey']));
  2075. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2076. $this->Form->create($this->article, [
  2077. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2078. ]);
  2079. $result = $this->Form->secure();
  2080. $this->assertStringContainsString($expected, $result);
  2081. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2082. $result = $this->Form->secure();
  2083. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2084. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2085. $result = $this->Form->secure();
  2086. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2087. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2088. $result = $this->Form->secure();
  2089. $this->assertStringNotContainsString($expected, $result, 'URL is different');
  2090. }
  2091. /**
  2092. * testSecuredFormUrlHasHtmlAndIdentifier method
  2093. *
  2094. * Test that URL, HTML and identifier show up in their hashes.
  2095. */
  2096. public function testSecuredFormUrlHasHtmlAndIdentifier(): void
  2097. {
  2098. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2099. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2100. $this->Form->create($this->article, [
  2101. 'url' => [
  2102. 'controller' => 'articles',
  2103. 'action' => 'view',
  2104. '?' => [
  2105. 'page' => 1,
  2106. 'limit' => 10,
  2107. 'html' => '<>"',
  2108. ],
  2109. '#' => 'result',
  2110. ],
  2111. ]);
  2112. $result = $this->Form->secure();
  2113. $this->assertStringContainsString($expected, $result);
  2114. $this->Form->create($this->article, [
  2115. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2116. ]);
  2117. $result = $this->Form->secure();
  2118. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2119. $this->Form->create($this->article, [
  2120. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2121. ]);
  2122. $result = $this->Form->secure();
  2123. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2124. }
  2125. /**
  2126. * testErrorMessageDisplay method
  2127. *
  2128. * Test error message display.
  2129. */
  2130. public function testErrorMessageDisplay(): void
  2131. {
  2132. $this->article['errors'] = [
  2133. 'Article' => [
  2134. 'title' => 'error message',
  2135. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2136. ],
  2137. ];
  2138. $this->Form->create($this->article);
  2139. $result = $this->Form->control('Article.title');
  2140. $expected = [
  2141. 'div' => ['class' => 'input text error'],
  2142. 'label' => ['for' => 'article-title'],
  2143. 'Title',
  2144. '/label',
  2145. 'input' => [
  2146. 'type' => 'text',
  2147. 'name' => 'Article[title]',
  2148. 'id' => 'article-title',
  2149. 'class' => 'form-error',
  2150. 'aria-invalid' => 'true',
  2151. 'aria-describedby' => 'article-title-error',
  2152. ],
  2153. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2154. 'error message',
  2155. '/div',
  2156. '/div',
  2157. ];
  2158. $this->assertHtml($expected, $result);
  2159. $result = $this->Form->control('Article.title', [
  2160. 'templates' => [
  2161. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2162. ],
  2163. ]);
  2164. $expected = [
  2165. 'div' => ['class' => 'input text error'],
  2166. 'label' => ['for' => 'article-title'],
  2167. 'Title',
  2168. '/label',
  2169. 'input' => [
  2170. 'type' => 'text',
  2171. 'name' => 'Article[title]',
  2172. 'id' => 'article-title',
  2173. 'class' => 'form-error',
  2174. // No aria-describedby because error template is custom
  2175. 'aria-invalid' => 'true',
  2176. ],
  2177. '/div',
  2178. ];
  2179. $this->assertHtml($expected, $result);
  2180. $result = $this->Form->control('Article.content');
  2181. $expected = [
  2182. 'div' => ['class' => 'input text error'],
  2183. 'label' => ['for' => 'article-content'],
  2184. 'Content',
  2185. '/label',
  2186. 'input' => [
  2187. 'type' => 'text',
  2188. 'name' => 'Article[content]',
  2189. 'id' => 'article-content',
  2190. 'class' => 'form-error',
  2191. 'aria-invalid' => 'true',
  2192. 'aria-describedby' => 'article-content-error',
  2193. ],
  2194. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2195. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2196. '/div',
  2197. '/div',
  2198. ];
  2199. $this->assertHtml($expected, $result);
  2200. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2201. $expected = [
  2202. 'div' => ['class' => 'input text error'],
  2203. 'label' => ['for' => 'article-content'],
  2204. 'Content',
  2205. '/label',
  2206. 'input' => [
  2207. 'type' => 'text',
  2208. 'name' => 'Article[content]',
  2209. 'id' => 'article-content',
  2210. 'class' => 'form-error',
  2211. 'aria-invalid' => 'true',
  2212. 'aria-describedby' => 'article-content-error',
  2213. ],
  2214. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2215. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2216. '/div',
  2217. '/div',
  2218. ];
  2219. $this->assertHtml($expected, $result);
  2220. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2221. $expected = [
  2222. 'div' => ['class' => 'input text error'],
  2223. 'label' => ['for' => 'article-content'],
  2224. 'Content',
  2225. '/label',
  2226. 'input' => [
  2227. 'type' => 'text',
  2228. 'name' => 'Article[content]',
  2229. 'id' => 'article-content',
  2230. 'class' => 'form-error',
  2231. 'aria-invalid' => 'true',
  2232. 'aria-describedby' => 'article-content-error',
  2233. ],
  2234. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2235. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2236. '/div',
  2237. '/div',
  2238. ];
  2239. $this->assertHtml($expected, $result);
  2240. }
  2241. /**
  2242. * testEmptyErrorValidation method
  2243. *
  2244. * Test validation errors, when validation message is an empty string.
  2245. */
  2246. public function testEmptyErrorValidation(): void
  2247. {
  2248. $this->article['errors'] = [
  2249. 'Article' => ['title' => ''],
  2250. ];
  2251. $this->Form->create($this->article);
  2252. $result = $this->Form->control('Article.title');
  2253. $expected = [
  2254. 'div' => ['class' => 'input text error'],
  2255. 'label' => ['for' => 'article-title'],
  2256. 'Title',
  2257. '/label',
  2258. 'input' => [
  2259. 'type' => 'text',
  2260. 'name' => 'Article[title]',
  2261. 'id' => 'article-title',
  2262. 'class' => 'form-error',
  2263. 'aria-invalid' => 'true',
  2264. 'aria-describedby' => 'article-title-error',
  2265. ],
  2266. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2267. [],
  2268. '/div',
  2269. '/div',
  2270. ];
  2271. $this->assertHtml($expected, $result);
  2272. }
  2273. /**
  2274. * testEmptyControlErrorValidation method
  2275. *
  2276. * Test validation errors, when calling control() overriding validation message by an empty string.
  2277. */
  2278. public function testEmptyControlErrorValidation(): void
  2279. {
  2280. $this->article['errors'] = [
  2281. 'Article' => ['title' => 'error message'],
  2282. ];
  2283. $this->Form->create($this->article);
  2284. $result = $this->Form->control('Article.title', ['error' => '']);
  2285. $expected = [
  2286. 'div' => ['class' => 'input text error'],
  2287. 'label' => ['for' => 'article-title'],
  2288. 'Title',
  2289. '/label',
  2290. 'input' => [
  2291. 'aria-invalid' => 'true',
  2292. 'aria-describedby' => 'article-title-error',
  2293. 'type' => 'text',
  2294. 'name' => 'Article[title]',
  2295. 'id' => 'article-title',
  2296. 'class' => 'form-error',
  2297. ],
  2298. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2299. [],
  2300. '/div',
  2301. '/div',
  2302. ];
  2303. $this->assertHtml($expected, $result);
  2304. }
  2305. /**
  2306. * testControlErrorMessage method
  2307. *
  2308. * Test validation errors, when calling control() overriding validation messages.
  2309. */
  2310. public function testControlErrorMessage(): void
  2311. {
  2312. $this->article['errors'] = [
  2313. 'title' => ['error message'],
  2314. ];
  2315. $this->Form->create($this->article);
  2316. $result = $this->Form->control('title', [
  2317. 'error' => 'Custom error!',
  2318. ]);
  2319. $expected = [
  2320. 'div' => ['class' => 'input text required error'],
  2321. 'label' => ['for' => 'title'],
  2322. 'Title',
  2323. '/label',
  2324. 'input' => [
  2325. 'type' => 'text',
  2326. 'name' => 'title',
  2327. 'id' => 'title',
  2328. 'class' => 'form-error',
  2329. 'required' => 'required',
  2330. 'data-validity-message' => 'This field cannot be left empty',
  2331. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2332. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2333. 'aria-required' => 'true',
  2334. 'aria-invalid' => 'true',
  2335. 'aria-describedby' => 'title-error',
  2336. ],
  2337. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2338. 'Custom error!',
  2339. '/div',
  2340. '/div',
  2341. ];
  2342. $this->assertHtml($expected, $result);
  2343. $result = $this->Form->control('title', [
  2344. 'error' => ['error message' => 'Custom error!'],
  2345. ]);
  2346. $expected = [
  2347. 'div' => ['class' => 'input text required error'],
  2348. 'label' => ['for' => 'title'],
  2349. 'Title',
  2350. '/label',
  2351. 'input' => [
  2352. 'type' => 'text',
  2353. 'name' => 'title',
  2354. 'id' => 'title',
  2355. 'aria-required' => 'true',
  2356. 'aria-invalid' => 'true',
  2357. 'aria-describedby' => 'title-error',
  2358. 'class' => 'form-error',
  2359. 'required' => 'required',
  2360. 'data-validity-message' => 'This field cannot be left empty',
  2361. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2362. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2363. ],
  2364. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2365. 'Custom error!',
  2366. '/div',
  2367. '/div',
  2368. ];
  2369. $this->assertHtml($expected, $result);
  2370. }
  2371. /**
  2372. * testFormValidationAssociated method
  2373. *
  2374. * Tests displaying errors for nested entities.
  2375. */
  2376. public function testFormValidationAssociated(): void
  2377. {
  2378. $nested = new Entity(['foo' => 'bar']);
  2379. $nested->setError('foo', ['not a valid bar']);
  2380. $entity = new Entity(['nested' => $nested]);
  2381. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2382. $result = $this->Form->error('nested.foo');
  2383. $this->assertSame('<div class="error-message" id="nested-foo-error">not a valid bar</div>', $result);
  2384. }
  2385. /**
  2386. * testFormValidationAssociatedSecondLevel method
  2387. *
  2388. * Test form error display with associated model.
  2389. */
  2390. public function testFormValidationAssociatedSecondLevel(): void
  2391. {
  2392. $inner = new Entity(['bar' => 'baz']);
  2393. $nested = new Entity(['foo' => $inner]);
  2394. $entity = new Entity(['nested' => $nested]);
  2395. $inner->setError('bar', ['not a valid one']);
  2396. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2397. $result = $this->Form->error('nested.foo.bar');
  2398. $this->assertSame('<div class="error-message" id="nested-foo-bar-error">not a valid one</div>', $result);
  2399. }
  2400. /**
  2401. * testFormValidationMultiRecord method
  2402. *
  2403. * Test form error display with multiple records.
  2404. */
  2405. public function testFormValidationMultiRecord(): void
  2406. {
  2407. $one = new Entity();
  2408. $two = new Entity();
  2409. $this->getTableLocator()->get('Contacts', [
  2410. 'className' => ContactsTable::class,
  2411. ]);
  2412. $one->set('email', '');
  2413. $one->setError('email', ['invalid email']);
  2414. $two->set('name', '');
  2415. $two->setError('name', ['This is wrong']);
  2416. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2417. $result = $this->Form->control('0.email');
  2418. $expected = [
  2419. 'div' => ['class' => 'input email error'],
  2420. 'label' => ['for' => '0-email'],
  2421. 'Email',
  2422. '/label',
  2423. 'input' => [
  2424. 'type' => 'email',
  2425. 'name' => '0[email]',
  2426. 'id' => '0-email',
  2427. 'class' => 'form-error',
  2428. 'maxlength' => 255,
  2429. 'value' => '',
  2430. 'aria-invalid' => 'true',
  2431. 'aria-describedby' => '0-email-error',
  2432. ],
  2433. ['div' => ['class' => 'error-message', 'id' => '0-email-error']],
  2434. 'invalid email',
  2435. '/div',
  2436. '/div',
  2437. ];
  2438. $this->assertHtml($expected, $result);
  2439. $result = $this->Form->control('1.name');
  2440. $expected = [
  2441. 'div' => ['class' => 'input text error'],
  2442. 'label' => ['for' => '1-name'],
  2443. 'Name',
  2444. '/label',
  2445. 'input' => [
  2446. 'type' => 'text',
  2447. 'name' => '1[name]',
  2448. 'id' => '1-name',
  2449. 'class' => 'form-error',
  2450. 'maxlength' => 255,
  2451. 'value' => '',
  2452. 'aria-invalid' => 'true',
  2453. 'aria-describedby' => '1-name-error',
  2454. ],
  2455. ['div' => ['class' => 'error-message', 'id' => '1-name-error']],
  2456. 'This is wrong',
  2457. '/div',
  2458. '/div',
  2459. ];
  2460. $this->assertHtml($expected, $result);
  2461. }
  2462. /**
  2463. * testControl method
  2464. *
  2465. * Test various incarnations of control().
  2466. */
  2467. public function testControl(): void
  2468. {
  2469. $this->getTableLocator()->get('ValidateUsers', [
  2470. 'className' => ValidateUsersTable::class,
  2471. ]);
  2472. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2473. $result = $this->Form->control('ValidateUsers.balance');
  2474. $expected = [
  2475. 'div' => ['class'],
  2476. 'label' => ['for'],
  2477. 'Balance',
  2478. '/label',
  2479. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2480. '/div',
  2481. ];
  2482. $this->assertHtml($expected, $result);
  2483. $result = $this->Form->control('ValidateUser.cost_decimal');
  2484. $expected = [
  2485. 'div' => ['class'],
  2486. 'label' => ['for'],
  2487. 'Cost Decimal',
  2488. '/label',
  2489. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2490. '/div',
  2491. ];
  2492. $this->assertHtml($expected, $result);
  2493. $result = $this->Form->control('ValidateUser.null_decimal');
  2494. $expected = [
  2495. 'div' => ['class'],
  2496. 'label' => ['for'],
  2497. 'Null Decimal',
  2498. '/label',
  2499. 'input' => ['name', 'type' => 'number', 'id'],
  2500. '/div',
  2501. ];
  2502. $this->assertHtml($expected, $result);
  2503. }
  2504. /**
  2505. * testControlCustomization method
  2506. *
  2507. * Tests the input method and passing custom options.
  2508. */
  2509. public function testControlCustomization(): void
  2510. {
  2511. $this->getTableLocator()->get('Contacts', [
  2512. 'className' => ContactsTable::class,
  2513. ]);
  2514. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2515. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2516. $expected = [
  2517. 'div' => ['class' => 'input email'],
  2518. 'label' => ['for' => 'custom'],
  2519. 'Email',
  2520. '/label',
  2521. ['input' => [
  2522. 'type' => 'email', 'name' => 'Contact[email]',
  2523. 'id' => 'custom', 'maxlength' => 255,
  2524. ]],
  2525. '/div',
  2526. ];
  2527. $this->assertHtml($expected, $result);
  2528. $result = $this->Form->control('Contact.email', [
  2529. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2530. ]);
  2531. $expected = [
  2532. '<div',
  2533. 'label' => ['for' => 'contact-email'],
  2534. 'Email',
  2535. '/label',
  2536. ['input' => [
  2537. 'type' => 'email', 'name' => 'Contact[email]',
  2538. 'id' => 'contact-email', 'maxlength' => 255,
  2539. ]],
  2540. '/div',
  2541. ];
  2542. $this->assertHtml($expected, $result);
  2543. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2544. $expected = [
  2545. 'div' => ['class' => 'input text'],
  2546. 'label' => ['for' => 'contact-email'],
  2547. 'Email',
  2548. '/label',
  2549. ['input' => [
  2550. 'type' => 'text', 'name' => 'Contact[email]',
  2551. 'id' => 'contact-email', 'maxlength' => '255',
  2552. ]],
  2553. '/div',
  2554. ];
  2555. $this->assertHtml($expected, $result);
  2556. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2557. $expected = [
  2558. 'div' => ['class' => 'input text'],
  2559. 'label' => ['for' => 'contact-5-email'],
  2560. 'Email',
  2561. '/label',
  2562. ['input' => [
  2563. 'type' => 'text', 'name' => 'Contact[5][email]',
  2564. 'id' => 'contact-5-email', 'maxlength' => '255',
  2565. ]],
  2566. '/div',
  2567. ];
  2568. $this->assertHtml($expected, $result);
  2569. $result = $this->Form->control('Contact.password');
  2570. $expected = [
  2571. 'div' => ['class' => 'input password'],
  2572. 'label' => ['for' => 'contact-password'],
  2573. 'Password',
  2574. '/label',
  2575. ['input' => [
  2576. 'type' => 'password', 'name' => 'Contact[password]',
  2577. 'id' => 'contact-password',
  2578. ]],
  2579. '/div',
  2580. ];
  2581. $this->assertHtml($expected, $result);
  2582. $result = $this->Form->control('Contact.email', [
  2583. 'type' => 'file', 'class' => 'textbox',
  2584. ]);
  2585. $expected = [
  2586. 'div' => ['class' => 'input file'],
  2587. 'label' => ['for' => 'contact-email'],
  2588. 'Email',
  2589. '/label',
  2590. ['input' => [
  2591. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2592. 'id' => 'contact-email',
  2593. ]],
  2594. '/div',
  2595. ];
  2596. $this->assertHtml($expected, $result);
  2597. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2598. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2599. $result = $this->Form->control('phone');
  2600. $expected = [
  2601. 'div' => ['class' => 'input tel'],
  2602. 'label' => ['for' => 'phone'],
  2603. 'Phone',
  2604. '/label',
  2605. ['input' => [
  2606. 'type' => 'tel', 'name' => 'phone',
  2607. 'value' => 'Hello &amp; World &gt; weird chars',
  2608. 'id' => 'phone', 'maxlength' => 255,
  2609. ]],
  2610. '/div',
  2611. ];
  2612. $this->assertHtml($expected, $result);
  2613. $this->View->setRequest(
  2614. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2615. );
  2616. $this->Form->create();
  2617. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2618. $expected = [
  2619. 'div' => ['class' => 'input text'],
  2620. 'label' => ['for' => 'myId'],
  2621. 'Field',
  2622. '/label',
  2623. 'input' => [
  2624. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2625. 'value' => 'My value', 'id' => 'myId',
  2626. ],
  2627. '/div',
  2628. ];
  2629. $this->assertHtml($expected, $result);
  2630. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2631. $this->Form->create();
  2632. $entity->setError('field', 'Badness!');
  2633. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2634. $result = $this->Form->control('field');
  2635. $expected = [
  2636. 'div' => ['class' => 'input text error'],
  2637. 'label' => ['for' => 'field'],
  2638. 'Field',
  2639. '/label',
  2640. 'input' => [
  2641. 'type' => 'text',
  2642. 'name' => 'field',
  2643. 'id' => 'field',
  2644. 'class' => 'form-error',
  2645. 'aria-invalid' => 'true',
  2646. 'aria-describedby' => 'field-error',
  2647. ],
  2648. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2649. 'Badness!',
  2650. '/div',
  2651. '/div',
  2652. ];
  2653. $this->assertHtml($expected, $result);
  2654. $result = $this->Form->control('field', [
  2655. 'templates' => [
  2656. 'inputContainerError' => '{{content}}{{error}}',
  2657. 'error' => '<span class="error-message">{{content}}</span>',
  2658. ],
  2659. ]);
  2660. $expected = [
  2661. 'label' => ['for' => 'field'],
  2662. 'Field',
  2663. '/label',
  2664. 'input' => [
  2665. 'type' => 'text',
  2666. 'name' => 'field',
  2667. 'id' => 'field',
  2668. 'class' => 'form-error',
  2669. // No aria-describedby because error template is custom
  2670. 'aria-invalid' => 'true',
  2671. ],
  2672. ['span' => ['class' => 'error-message']],
  2673. 'Badness!',
  2674. '/span',
  2675. ];
  2676. $this->assertHtml($expected, $result);
  2677. $entity->setError('field', ['minLength'], true);
  2678. $result = $this->Form->control('field', [
  2679. 'error' => [
  2680. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2681. 'maxLength' => 'login too large',
  2682. ],
  2683. ]);
  2684. $expected = [
  2685. 'div' => ['class' => 'input text error'],
  2686. 'label' => ['for' => 'field'],
  2687. 'Field',
  2688. '/label',
  2689. 'input' => [
  2690. 'type' => 'text',
  2691. 'name' => 'field',
  2692. 'id' => 'field',
  2693. 'class' => 'form-error',
  2694. 'aria-invalid' => 'true',
  2695. 'aria-describedby' => 'field-error',
  2696. ],
  2697. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2698. 'Le login doit contenir au moins 2 caractères',
  2699. '/div',
  2700. '/div',
  2701. ];
  2702. $this->assertHtml($expected, $result);
  2703. $entity->setError('field', ['maxLength'], true);
  2704. $result = $this->Form->control('field', [
  2705. 'error' => [
  2706. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2707. 'maxLength' => 'login too large',
  2708. ],
  2709. ]);
  2710. $expected = [
  2711. 'div' => ['class' => 'input text error'],
  2712. 'label' => ['for' => 'field'],
  2713. 'Field',
  2714. '/label',
  2715. 'input' => [
  2716. 'type' => 'text',
  2717. 'name' => 'field',
  2718. 'id' => 'field',
  2719. 'class' => 'form-error',
  2720. 'aria-invalid' => 'true',
  2721. 'aria-describedby' => 'field-error',
  2722. ],
  2723. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2724. 'login too large',
  2725. '/div',
  2726. '/div',
  2727. ];
  2728. $this->assertHtml($expected, $result);
  2729. }
  2730. /**
  2731. * testControlWithTemplateFile method
  2732. *
  2733. * Test that control() accepts a template file.
  2734. */
  2735. public function testControlWithTemplateFile(): void
  2736. {
  2737. $result = $this->Form->control('field', [
  2738. 'templates' => 'htmlhelper_tags',
  2739. ]);
  2740. $expected = [
  2741. 'label' => ['for' => 'field'],
  2742. 'Field',
  2743. '/label',
  2744. 'input' => [
  2745. 'type' => 'text', 'name' => 'field',
  2746. 'id' => 'field',
  2747. ],
  2748. ];
  2749. $this->assertHtml($expected, $result);
  2750. }
  2751. /**
  2752. * testNestedControlsEndWithBrackets method
  2753. *
  2754. * Test that nested inputs end with brackets.
  2755. */
  2756. public function testNestedControlsEndWithBrackets(): void
  2757. {
  2758. $result = $this->Form->text('nested.text[]');
  2759. $expected = [
  2760. 'input' => [
  2761. 'type' => 'text', 'name' => 'nested[text][]',
  2762. ],
  2763. ];
  2764. $this->assertHtml($expected, $result);
  2765. $result = $this->Form->file('nested.file[]');
  2766. $expected = [
  2767. 'input' => [
  2768. 'type' => 'file', 'name' => 'nested[file][]',
  2769. ],
  2770. ];
  2771. $this->assertHtml($expected, $result);
  2772. }
  2773. /**
  2774. * testCreateIdPrefix method
  2775. *
  2776. * Test id prefix.
  2777. */
  2778. public function testCreateIdPrefix(): void
  2779. {
  2780. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2781. $result = $this->Form->control('field');
  2782. $expected = [
  2783. 'div' => ['class' => 'input text'],
  2784. 'label' => ['for' => 'prefix-field'],
  2785. 'Field',
  2786. '/label',
  2787. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2788. '/div',
  2789. ];
  2790. $this->assertHtml($expected, $result);
  2791. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2792. $expected = [
  2793. 'div' => ['class' => 'input text'],
  2794. 'label' => ['for' => 'custom-id'],
  2795. 'Field',
  2796. '/label',
  2797. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2798. '/div',
  2799. ];
  2800. $this->assertHtml($expected, $result);
  2801. $result = $this->Form->radio('Model.field', ['option A']);
  2802. $expected = [
  2803. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2804. 'label' => ['for' => 'prefix-model-field-0'],
  2805. ['input' => [
  2806. 'type' => 'radio',
  2807. 'name' => 'Model[field]',
  2808. 'value' => '0',
  2809. 'id' => 'prefix-model-field-0',
  2810. ]],
  2811. 'option A',
  2812. '/label',
  2813. ];
  2814. $this->assertHtml($expected, $result);
  2815. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2816. $expected = [
  2817. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2818. 'label' => ['for' => 'prefix-model-field-0'],
  2819. ['input' => [
  2820. 'type' => 'radio',
  2821. 'name' => 'Model[field]',
  2822. 'value' => '0',
  2823. 'id' => 'prefix-model-field-0',
  2824. ]],
  2825. 'option A',
  2826. '/label',
  2827. ];
  2828. $this->assertHtml($expected, $result);
  2829. $result = $this->Form->select(
  2830. 'Model.multi_field',
  2831. ['first'],
  2832. ['multiple' => 'checkbox']
  2833. );
  2834. $expected = [
  2835. 'input' => [
  2836. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'prefix-model-multi-field',
  2837. ],
  2838. ['div' => ['class' => 'checkbox']],
  2839. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2840. ['input' => [
  2841. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2842. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  2843. ]],
  2844. 'first',
  2845. '/label',
  2846. '/div',
  2847. ];
  2848. $this->assertHtml($expected, $result);
  2849. $this->Form->end();
  2850. $result = $this->Form->control('field');
  2851. $expected = [
  2852. 'div' => ['class' => 'input text'],
  2853. 'label' => ['for' => 'field'],
  2854. 'Field',
  2855. '/label',
  2856. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2857. '/div',
  2858. ];
  2859. $this->assertHtml($expected, $result);
  2860. }
  2861. /**
  2862. * testControlZero method
  2863. *
  2864. * Test that inputs with 0 can be created.
  2865. */
  2866. public function testControlZero(): void
  2867. {
  2868. $this->getTableLocator()->get('Contacts', [
  2869. 'className' => ContactsTable::class,
  2870. ]);
  2871. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2872. $result = $this->Form->control('0');
  2873. $expected = [
  2874. 'div' => ['class' => 'input text'],
  2875. 'label' => ['for' => '0'], '/label',
  2876. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2877. '/div',
  2878. ];
  2879. $this->assertHtml($expected, $result);
  2880. }
  2881. /**
  2882. * testControlCheckbox method
  2883. *
  2884. * Test control() with checkbox creation.
  2885. */
  2886. public function testControlCheckbox(): void
  2887. {
  2888. $articles = $this->getTableLocator()->get('Articles');
  2889. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  2890. $article = $articles->newEmptyEntity();
  2891. $this->Form->create($article);
  2892. $result = $this->Form->control('Articles.active');
  2893. $expected = [
  2894. 'div' => ['class' => 'input checkbox'],
  2895. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2896. 'label' => ['for' => 'articles-active'],
  2897. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  2898. 'Active',
  2899. '/label',
  2900. '/div',
  2901. ];
  2902. $this->assertHtml($expected, $result);
  2903. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  2904. $expected = [
  2905. 'div' => ['class' => 'input checkbox'],
  2906. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2907. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2908. '/div',
  2909. ];
  2910. $this->assertHtml($expected, $result);
  2911. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  2912. $expected = [
  2913. 'div' => ['class' => 'input checkbox'],
  2914. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2915. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2916. '/div',
  2917. ];
  2918. $this->assertHtml($expected, $result);
  2919. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  2920. $expected = [
  2921. 'div' => ['class' => 'input checkbox'],
  2922. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2923. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2924. '/div',
  2925. ];
  2926. $this->assertHtml($expected, $result);
  2927. $result = $this->Form->control('Articles.disabled', [
  2928. 'label' => 'Disabled',
  2929. 'type' => 'checkbox',
  2930. 'data-foo' => 'disabled',
  2931. ]);
  2932. $expected = [
  2933. 'div' => ['class' => 'input checkbox'],
  2934. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  2935. 'label' => ['for' => 'articles-disabled'],
  2936. ['input' => [
  2937. 'type' => 'checkbox',
  2938. 'name' => 'Articles[disabled]',
  2939. 'value' => '1',
  2940. 'id' => 'articles-disabled',
  2941. 'data-foo' => 'disabled',
  2942. ]],
  2943. 'Disabled',
  2944. '/label',
  2945. '/div',
  2946. ];
  2947. $this->assertHtml($expected, $result);
  2948. $result = $this->Form->control('Articles.confirm', [
  2949. 'label' => 'Confirm <b>me</b>!',
  2950. 'type' => 'checkbox',
  2951. 'escape' => false,
  2952. ]);
  2953. $expected = [
  2954. 'div' => ['class' => 'input checkbox'],
  2955. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  2956. 'label' => ['for' => 'articles-confirm'],
  2957. ['input' => [
  2958. 'type' => 'checkbox',
  2959. 'name' => 'Articles[confirm]',
  2960. 'value' => '1',
  2961. 'id' => 'articles-confirm',
  2962. ]],
  2963. 'Confirm <b>me</b>!',
  2964. '/label',
  2965. '/div',
  2966. ];
  2967. $this->assertHtml($expected, $result);
  2968. }
  2969. /**
  2970. * testControlHidden method
  2971. *
  2972. * Test that control() does not create wrapping div and label tag for hidden fields.
  2973. */
  2974. public function testControlHidden(): void
  2975. {
  2976. $this->getTableLocator()->get('ValidateUsers', [
  2977. 'className' => ValidateUsersTable::class,
  2978. ]);
  2979. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2980. $result = $this->Form->control('ValidateUser.id');
  2981. $expected = [
  2982. 'input' => ['name', 'type' => 'hidden', 'id'],
  2983. ];
  2984. $this->assertHtml($expected, $result);
  2985. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  2986. $expected = [
  2987. 'input' => ['name', 'type' => 'hidden', 'id'],
  2988. ];
  2989. $this->assertHtml($expected, $result);
  2990. }
  2991. /**
  2992. * testControlDatetime method
  2993. *
  2994. * Test form->control() with datetime.
  2995. */
  2996. public function testControlDatetime(): void
  2997. {
  2998. $result = $this->Form->control('prueba', [
  2999. 'type' => 'datetime',
  3000. 'value' => new DateTime('2019-09-27 02:52:43'),
  3001. ]);
  3002. $expected = [
  3003. 'div' => ['class' => 'input datetime'],
  3004. 'label' => ['for' => 'prueba'],
  3005. 'Prueba',
  3006. '/label',
  3007. 'input' => [
  3008. 'name' => 'prueba',
  3009. 'id' => 'prueba',
  3010. 'type' => 'datetime-local',
  3011. 'value' => '2019-09-27T02:52:43',
  3012. 'step' => '1',
  3013. ],
  3014. '/div',
  3015. ];
  3016. $this->assertHtml($expected, $result);
  3017. }
  3018. /**
  3019. * testControlDatetimeIdPrefix method
  3020. *
  3021. * Test form->control() with datetime with id prefix.
  3022. */
  3023. public function testControlDatetimeIdPrefix(): void
  3024. {
  3025. $this->Form->create(null, ['idPrefix' => 'prefix']);
  3026. $result = $this->Form->control('prueba', [
  3027. 'type' => 'datetime',
  3028. ]);
  3029. $expected = [
  3030. 'div' => ['class' => 'input datetime'],
  3031. 'label' => ['for' => 'prefix-prueba'],
  3032. 'Prueba',
  3033. '/label',
  3034. 'input' => [
  3035. 'name' => 'prueba',
  3036. 'id' => 'prefix-prueba',
  3037. 'type' => 'datetime-local',
  3038. 'value' => '',
  3039. 'step' => '1',
  3040. ],
  3041. '/div',
  3042. ];
  3043. $this->assertHtml($expected, $result);
  3044. }
  3045. /**
  3046. * testControlDatetimeStep method
  3047. *
  3048. * Test form->control() with datetime with custom step size.
  3049. */
  3050. public function testControlDatetimeStep(): void
  3051. {
  3052. $result = $this->Form->control('prueba', [
  3053. 'type' => 'datetime',
  3054. 'value' => new DateTime('2019-09-27 02:52:43'),
  3055. 'step' => '0.5',
  3056. ]);
  3057. $expected = [
  3058. 'div' => ['class' => 'input datetime'],
  3059. 'label' => ['for' => 'prueba'],
  3060. 'Prueba',
  3061. '/label',
  3062. 'input' => [
  3063. 'name' => 'prueba',
  3064. 'id' => 'prueba',
  3065. 'type' => 'datetime-local',
  3066. 'value' => '2019-09-27T02:52:43.000',
  3067. 'step' => '0.5',
  3068. ],
  3069. '/div',
  3070. ];
  3071. $this->assertHtml($expected, $result);
  3072. }
  3073. /**
  3074. * testControlCheckboxWithDisabledElements method
  3075. *
  3076. * Test generating checkboxes with disabled elements.
  3077. */
  3078. public function testControlCheckboxWithDisabledElements(): void
  3079. {
  3080. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3081. $result = $this->Form->control('Contact.multiple', [
  3082. 'multiple' => 'checkbox',
  3083. 'disabled' => 'disabled',
  3084. 'options' => $options,
  3085. ]);
  3086. $expected = [
  3087. ['div' => ['class' => 'input select']],
  3088. ['label' => ['for' => 'contact-multiple']],
  3089. 'Multiple',
  3090. '/label',
  3091. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '', 'id' => 'contact-multiple']],
  3092. ['div' => ['class' => 'checkbox']],
  3093. ['label' => ['for' => 'contact-multiple-1']],
  3094. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3095. 'One',
  3096. '/label',
  3097. '/div',
  3098. ['div' => ['class' => 'checkbox']],
  3099. ['label' => ['for' => 'contact-multiple-2']],
  3100. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3101. 'Two',
  3102. '/label',
  3103. '/div',
  3104. ['div' => ['class' => 'checkbox']],
  3105. ['label' => ['for' => 'contact-multiple-3']],
  3106. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3107. 'Three',
  3108. '/label',
  3109. '/div',
  3110. '/div',
  3111. ];
  3112. $this->assertHtml($expected, $result);
  3113. // make sure 50 does only disable 50, and not 50f5c0cf
  3114. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3115. $disabled = [50];
  3116. $expected = [
  3117. ['div' => ['class' => 'input select']],
  3118. ['label' => ['for' => 'contact-multiple']],
  3119. 'Multiple',
  3120. '/label',
  3121. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'contact-multiple']],
  3122. ['div' => ['class' => 'checkbox']],
  3123. ['label' => ['for' => 'contact-multiple-50']],
  3124. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3125. 'Fifty',
  3126. '/label',
  3127. '/div',
  3128. ['div' => ['class' => 'checkbox']],
  3129. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3130. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3131. 'Stringy',
  3132. '/label',
  3133. '/div',
  3134. '/div',
  3135. ];
  3136. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3137. $this->assertHtml($expected, $result);
  3138. }
  3139. /**
  3140. * testControlWithLeadingInteger method
  3141. *
  3142. * Test input name with leading integer, ensure attributes are generated correctly.
  3143. */
  3144. public function testControlWithLeadingInteger(): void
  3145. {
  3146. $result = $this->Form->text('0.Node.title');
  3147. $expected = [
  3148. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3149. ];
  3150. $this->assertHtml($expected, $result);
  3151. }
  3152. /**
  3153. * testControlSelectType method
  3154. *
  3155. * Test form->control() with select type inputs.
  3156. */
  3157. public function testControlSelectType(): void
  3158. {
  3159. $result = $this->Form->control(
  3160. 'email',
  3161. [
  3162. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3163. );
  3164. $expected = [
  3165. 'div' => ['class' => 'input select'],
  3166. 'label' => ['for' => 'email'],
  3167. 'Email',
  3168. '/label',
  3169. ['select' => ['name' => 'email', 'id' => 'email']],
  3170. ['option' => ['value' => '']],
  3171. '/option',
  3172. ['option' => ['value' => 'è']],
  3173. 'Firést',
  3174. '/option',
  3175. ['option' => ['value' => 'é']],
  3176. 'Secoènd',
  3177. '/option',
  3178. '/select',
  3179. '/div',
  3180. ];
  3181. $this->assertHtml($expected, $result);
  3182. $result = $this->Form->control(
  3183. 'email',
  3184. [
  3185. 'options' => ['First', 'Second'], 'empty' => true]
  3186. );
  3187. $expected = [
  3188. 'div' => ['class' => 'input select'],
  3189. 'label' => ['for' => 'email'],
  3190. 'Email',
  3191. '/label',
  3192. ['select' => ['name' => 'email', 'id' => 'email']],
  3193. ['option' => ['value' => '']],
  3194. '/option',
  3195. ['option' => ['value' => '0']],
  3196. 'First',
  3197. '/option',
  3198. ['option' => ['value' => '1']],
  3199. 'Second',
  3200. '/option',
  3201. '/select',
  3202. '/div',
  3203. ];
  3204. $this->assertHtml($expected, $result);
  3205. $result = $this->Form->control('email', [
  3206. 'type' => 'select',
  3207. 'options' => new ArrayObject(['First', 'Second']),
  3208. 'empty' => true,
  3209. ]);
  3210. $this->assertHtml($expected, $result);
  3211. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3212. $this->View->setRequest(
  3213. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3214. );
  3215. $this->Form->create();
  3216. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3217. $expected = [
  3218. 'div' => ['class' => 'input select'],
  3219. 'label' => ['for' => 'model-user-id'],
  3220. 'User',
  3221. '/label',
  3222. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3223. ['option' => ['value' => '']],
  3224. '/option',
  3225. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3226. 'good',
  3227. '/option',
  3228. ['option' => ['value' => 'other']],
  3229. 'bad',
  3230. '/option',
  3231. '/select',
  3232. '/div',
  3233. ];
  3234. $this->assertHtml($expected, $result);
  3235. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3236. $this->View->setRequest(
  3237. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3238. );
  3239. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3240. $expected = [
  3241. 'div' => ['class' => 'input select'],
  3242. 'label' => ['for' => 'thing-user-id'],
  3243. 'User',
  3244. '/label',
  3245. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3246. ['option' => ['value' => '']],
  3247. 'Some Empty',
  3248. '/option',
  3249. ['option' => ['value' => 'value']],
  3250. 'good',
  3251. '/option',
  3252. ['option' => ['value' => 'other']],
  3253. 'bad',
  3254. '/option',
  3255. '/select',
  3256. '/div',
  3257. ];
  3258. $this->assertHtml($expected, $result);
  3259. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3260. $this->View->setRequest(
  3261. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3262. );
  3263. $this->Form->create();
  3264. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3265. $expected = [
  3266. 'div' => ['class' => 'input select'],
  3267. 'label' => ['for' => 'thing-user-id'],
  3268. 'User',
  3269. '/label',
  3270. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3271. ['option' => ['value' => '']],
  3272. 'Some Empty',
  3273. '/option',
  3274. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3275. 'good',
  3276. '/option',
  3277. ['option' => ['value' => 'other']],
  3278. 'bad',
  3279. '/option',
  3280. '/select',
  3281. '/div',
  3282. ];
  3283. $this->assertHtml($expected, $result);
  3284. $result = $this->Form->control('Publisher.id', [
  3285. 'label' => 'Publisher',
  3286. 'type' => 'select',
  3287. 'multiple' => 'checkbox',
  3288. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3289. ]);
  3290. $expected = [
  3291. ['div' => ['class' => 'input select']],
  3292. ['label' => ['for' => 'publisher-id']],
  3293. 'Publisher',
  3294. '/label',
  3295. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'publisher-id'],
  3296. ['div' => ['class' => 'checkbox']],
  3297. ['label' => ['for' => 'publisher-id-value-1']],
  3298. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3299. 'Label 1',
  3300. '/label',
  3301. '/div',
  3302. ['div' => ['class' => 'checkbox']],
  3303. ['label' => ['for' => 'publisher-id-value-2']],
  3304. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3305. 'Label 2',
  3306. '/label',
  3307. '/div',
  3308. '/div',
  3309. ];
  3310. $this->assertHtml($expected, $result);
  3311. }
  3312. /**
  3313. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3314. *
  3315. * Test that control() and a non standard primary key makes a hidden input by default.
  3316. */
  3317. public function testControlWithNonStandardPrimaryKeyMakesHidden(): void
  3318. {
  3319. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3320. $this->Form->create($this->article);
  3321. $result = $this->Form->control('title');
  3322. $expected = [
  3323. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3324. ];
  3325. $this->assertHtml($expected, $result);
  3326. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3327. $this->Form->create($this->article);
  3328. $result = $this->Form->control('title');
  3329. $expected = [
  3330. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3331. ];
  3332. $this->assertHtml($expected, $result);
  3333. $result = $this->Form->control('body');
  3334. $expected = [
  3335. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3336. ];
  3337. $this->assertHtml($expected, $result);
  3338. }
  3339. /**
  3340. * testControlOverridingMagicSelectType method
  3341. *
  3342. * Test that overriding the magic select type widget is possible.
  3343. */
  3344. public function testControlOverridingMagicSelectType(): void
  3345. {
  3346. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3347. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3348. $expected = [
  3349. 'div' => ['class' => 'input text'],
  3350. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3351. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3352. '/div',
  3353. ];
  3354. $this->assertHtml($expected, $result);
  3355. //Check that magic types still work for plural/singular vars
  3356. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3357. $result = $this->Form->control('Model.type');
  3358. $expected = [
  3359. 'div' => ['class' => 'input select'],
  3360. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3361. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3362. ['option' => ['value' => 'value']], 'good', '/option',
  3363. ['option' => ['value' => 'other']], 'bad', '/option',
  3364. '/select',
  3365. '/div',
  3366. ];
  3367. $this->assertHtml($expected, $result);
  3368. }
  3369. /**
  3370. * testControlMagicTypeDoesNotOverride method
  3371. *
  3372. * Test that inferred types do not override developer input.
  3373. */
  3374. public function testControlMagicTypeDoesNotOverride(): void
  3375. {
  3376. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3377. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3378. $expected = [
  3379. 'div' => ['class' => 'input checkbox'],
  3380. ['input' => [
  3381. 'type' => 'hidden',
  3382. 'name' => 'Model[user]',
  3383. 'value' => 0,
  3384. ]],
  3385. 'label' => ['for' => 'model-user'],
  3386. ['input' => [
  3387. 'name' => 'Model[user]',
  3388. 'type' => 'checkbox',
  3389. 'id' => 'model-user',
  3390. 'value' => 1,
  3391. ]],
  3392. 'User',
  3393. '/label',
  3394. '/div',
  3395. ];
  3396. $this->assertHtml($expected, $result);
  3397. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3398. $options = [
  3399. 1 => 'blue',
  3400. 2 => 'red',
  3401. ];
  3402. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3403. $expected = [
  3404. 'div' => ['class' => 'input select'],
  3405. 'label' => ['for' => 'tags-ids'],
  3406. 'Tags',
  3407. '/label',
  3408. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => '', 'id' => 'tags-ids'],
  3409. ['div' => ['class' => 'checkbox']],
  3410. ['label' => ['for' => 'tags-ids-1']],
  3411. ['input' => [
  3412. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3413. 'value' => '1', 'name' => 'tags[_ids][]',
  3414. ]],
  3415. 'blue',
  3416. '/label',
  3417. '/div',
  3418. ['div' => ['class' => 'checkbox']],
  3419. ['label' => ['for' => 'tags-ids-2']],
  3420. ['input' => [
  3421. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3422. 'value' => '2', 'name' => 'tags[_ids][]',
  3423. ]],
  3424. 'red',
  3425. '/label',
  3426. '/div',
  3427. '/div',
  3428. ];
  3429. $this->assertHtml($expected, $result);
  3430. }
  3431. /**
  3432. * testControlMagicSelectForTypeNumber method
  3433. *
  3434. * Test that magic control() selects are created for type=number.
  3435. */
  3436. public function testControlMagicSelectForTypeNumber(): void
  3437. {
  3438. $this->getTableLocator()->get('ValidateUsers', [
  3439. 'className' => ValidateUsersTable::class,
  3440. ]);
  3441. $entity = new Entity(['balance' => 1]);
  3442. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3443. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3444. $result = $this->Form->control('balance');
  3445. $expected = [
  3446. 'div' => ['class' => 'input select'],
  3447. 'label' => ['for' => 'balance'],
  3448. 'Balance',
  3449. '/label',
  3450. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3451. ['option' => ['value' => '0']],
  3452. 'nothing',
  3453. '/option',
  3454. ['option' => ['value' => '1', 'selected' => 'selected']],
  3455. 'some',
  3456. '/option',
  3457. ['option' => ['value' => '100']],
  3458. 'a lot',
  3459. '/option',
  3460. '/select',
  3461. '/div',
  3462. ];
  3463. $this->assertHtml($expected, $result);
  3464. }
  3465. /**
  3466. * testInvalidControlTypeOption method
  3467. *
  3468. * Test invalid 'input' type option to control() function.
  3469. */
  3470. public function testInvalidControlTypeOption(): void
  3471. {
  3472. $this->expectException(InvalidArgumentException::class);
  3473. $this->expectExceptionMessage('Invalid type `input` used for field `text`.');
  3474. $this->Form->control('text', ['type' => 'input']);
  3475. }
  3476. /**
  3477. * testControlMagicSelectChangeToRadio method
  3478. *
  3479. * Test that magic control() selects can easily be converted into radio types without error.
  3480. */
  3481. public function testControlMagicSelectChangeToRadio(): void
  3482. {
  3483. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3484. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3485. $this->assertStringContainsString('input type="radio"', $result);
  3486. }
  3487. /**
  3488. * testFormControlSubmit method
  3489. *
  3490. * Test correct results for form::control() and type submit.
  3491. */
  3492. public function testFormControlSubmit(): void
  3493. {
  3494. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3495. $expected = [
  3496. 'div' => ['class' => 'submit'],
  3497. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3498. '/div',
  3499. ];
  3500. $this->assertHtml($expected, $result);
  3501. }
  3502. /**
  3503. * testFormControls method
  3504. *
  3505. * Test correct results from Form::controls().
  3506. */
  3507. public function testFormControlsLegendFieldset(): void
  3508. {
  3509. $this->Form->create($this->article);
  3510. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3511. $expected = [
  3512. '<fieldset',
  3513. '<legend',
  3514. 'The Legend',
  3515. '/legend',
  3516. '*/fieldset',
  3517. ];
  3518. $this->assertHtml($expected, $result);
  3519. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3520. $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
  3521. $this->assertStringContainsString('<fieldset>', $result);
  3522. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3523. $this->assertStringNotContainsString('<legend>', $result);
  3524. $this->assertStringNotContainsString('<fieldset>', $result);
  3525. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3526. $this->assertStringNotContainsString('<legend>', $result);
  3527. $this->assertStringNotContainsString('<fieldset>', $result);
  3528. $this->Form->create($this->article);
  3529. $this->View->setRequest($this->View->getRequest()
  3530. ->withParam('prefix', 'admin')
  3531. ->withParam('action', 'admin_edit')
  3532. ->withParam('controller', 'articles'));
  3533. $result = $this->Form->allControls();
  3534. $expected = [
  3535. '<fieldset',
  3536. '<legend',
  3537. 'New Article',
  3538. '/legend',
  3539. '*/fieldset',
  3540. ];
  3541. $this->assertHtml($expected, $result);
  3542. $this->Form->create($this->article);
  3543. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3544. $expected = [
  3545. '<fieldset',
  3546. '<legend',
  3547. 'The Legend',
  3548. '/legend',
  3549. '*/fieldset',
  3550. ];
  3551. $this->assertHtml($expected, $result);
  3552. $this->Form->create($this->article);
  3553. $result = $this->Form->allControls([], [
  3554. 'fieldset' => [
  3555. 'class' => 'some-class some-other-class',
  3556. 'disabled' => true,
  3557. 'data-param' => 'a-param',
  3558. ],
  3559. 'legend' => 'The Legend',
  3560. ]);
  3561. $expected = [
  3562. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3563. '<legend',
  3564. 'The Legend',
  3565. '/legend',
  3566. '*/fieldset',
  3567. ];
  3568. $this->assertHtml($expected, $result);
  3569. }
  3570. /**
  3571. * testFormControls method
  3572. *
  3573. * Test the controls() method.
  3574. */
  3575. public function testFormControls(): void
  3576. {
  3577. $this->Form->create($this->article);
  3578. $result = $this->Form->allControls();
  3579. $expected = [
  3580. '<fieldset',
  3581. '<legend', 'New Article', '/legend',
  3582. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3583. ['div' => ['class' => 'input select required']],
  3584. '*/div',
  3585. ['div' => ['class' => 'input text required']],
  3586. '*/div',
  3587. ['div' => ['class' => 'input text']],
  3588. '*/div',
  3589. ['div' => ['class' => 'input text']],
  3590. '*/div',
  3591. '/fieldset',
  3592. ];
  3593. $this->assertHtml($expected, $result);
  3594. $result = $this->Form->allControls([
  3595. 'published' => ['type' => 'boolean'],
  3596. ]);
  3597. $expected = [
  3598. '<fieldset',
  3599. '<legend', 'New Article', '/legend',
  3600. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3601. ['div' => ['class' => 'input select required']],
  3602. '*/div',
  3603. ['div' => ['class' => 'input text required']],
  3604. '*/div',
  3605. ['div' => ['class' => 'input text']],
  3606. '*/div',
  3607. ['div' => ['class' => 'input boolean']],
  3608. '*/div',
  3609. '/fieldset',
  3610. ];
  3611. $this->assertHtml($expected, $result);
  3612. $this->Form->create($this->article);
  3613. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3614. $expected = [
  3615. 'fieldset' => [],
  3616. 'legend' => [],
  3617. 'Hello',
  3618. '/legend',
  3619. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3620. ['div' => ['class' => 'input select required']],
  3621. '*/div',
  3622. ['div' => ['class' => 'input text required']],
  3623. '*/div',
  3624. ['div' => ['class' => 'input text']],
  3625. '*/div',
  3626. ['div' => ['class' => 'input text']],
  3627. '*/div',
  3628. '/fieldset',
  3629. ];
  3630. $this->assertHtml($expected, $result);
  3631. $this->Form->create();
  3632. $expected = [
  3633. 'fieldset' => [],
  3634. ['div' => ['class' => 'input text']],
  3635. 'label' => ['for' => 'foo'],
  3636. 'Foo',
  3637. '/label',
  3638. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3639. '*/div',
  3640. '/fieldset',
  3641. ];
  3642. $result = $this->Form->allControls(
  3643. ['foo' => ['type' => 'text']],
  3644. ['legend' => false]
  3645. );
  3646. $this->assertHtml($expected, $result);
  3647. }
  3648. /**
  3649. * testFormControlsBlacklist method
  3650. */
  3651. public function testFormControlsBlacklist(): void
  3652. {
  3653. $this->Form->create($this->article);
  3654. $result = $this->Form->allControls([
  3655. 'id' => false,
  3656. ]);
  3657. $expected = [
  3658. '<fieldset',
  3659. '<legend', 'New Article', '/legend',
  3660. ['div' => ['class' => 'input select required']],
  3661. '*/div',
  3662. ['div' => ['class' => 'input text required']],
  3663. '*/div',
  3664. ['div' => ['class' => 'input text']],
  3665. '*/div',
  3666. ['div' => ['class' => 'input text']],
  3667. '*/div',
  3668. '/fieldset',
  3669. ];
  3670. $this->assertHtml($expected, $result);
  3671. $this->Form->create($this->article);
  3672. $result = $this->Form->allControls([
  3673. 'id' => [],
  3674. ]);
  3675. $expected = [
  3676. '<fieldset',
  3677. '<legend', 'New Article', '/legend',
  3678. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3679. ['div' => ['class' => 'input select required']],
  3680. '*/div',
  3681. ['div' => ['class' => 'input text required']],
  3682. '*/div',
  3683. ['div' => ['class' => 'input text']],
  3684. '*/div',
  3685. ['div' => ['class' => 'input text']],
  3686. '*/div',
  3687. '/fieldset',
  3688. ];
  3689. $this->assertHtml($expected, $result, true);
  3690. }
  3691. /**
  3692. * testSelectAsCheckbox method
  3693. *
  3694. * Test multi-select widget with checkbox formatting.
  3695. */
  3696. public function testSelectAsCheckbox(): void
  3697. {
  3698. $result = $this->Form->select(
  3699. 'Model.multi_field',
  3700. ['first', 'second', 'third'],
  3701. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3702. );
  3703. $expected = [
  3704. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3705. ['div' => ['class' => 'checkbox']],
  3706. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3707. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3708. 'first',
  3709. '/label',
  3710. '/div',
  3711. ['div' => ['class' => 'checkbox']],
  3712. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3713. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3714. 'second',
  3715. '/label',
  3716. '/div',
  3717. ['div' => ['class' => 'checkbox']],
  3718. ['label' => ['for' => 'model-multi-field-2']],
  3719. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3720. 'third',
  3721. '/label',
  3722. '/div',
  3723. ];
  3724. $this->assertHtml($expected, $result);
  3725. $result = $this->Form->select(
  3726. 'Model.multi_field',
  3727. ['1/2' => 'half'],
  3728. ['multiple' => 'checkbox']
  3729. );
  3730. $expected = [
  3731. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3732. ['div' => ['class' => 'checkbox']],
  3733. ['label' => ['for' => 'model-multi-field-1-2']],
  3734. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3735. 'half',
  3736. '/label',
  3737. '/div',
  3738. ];
  3739. $this->assertHtml($expected, $result);
  3740. }
  3741. /**
  3742. * testLabel method
  3743. *
  3744. * Test label generation.
  3745. */
  3746. public function testLabel(): void
  3747. {
  3748. $result = $this->Form->label('Person.name');
  3749. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3750. $this->assertHtml($expected, $result);
  3751. $result = $this->Form->label('Person.name');
  3752. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3753. $this->assertHtml($expected, $result);
  3754. $result = $this->Form->label('Person.first_name');
  3755. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3756. $this->assertHtml($expected, $result);
  3757. $result = $this->Form->label('Person.first_name', 'Your first name');
  3758. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3759. $this->assertHtml($expected, $result);
  3760. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3761. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3762. $this->assertHtml($expected, $result);
  3763. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3764. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3765. $this->assertHtml($expected, $result);
  3766. $result = $this->Form->label('Person.first_name', '');
  3767. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3768. $this->assertHtml($expected, $result);
  3769. $result = $this->Form->label('Person.2.name', '');
  3770. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3771. $this->assertHtml($expected, $result);
  3772. }
  3773. /**
  3774. * testLabelContainControl method
  3775. *
  3776. * Test that label() can accept an input with the correct template vars.
  3777. */
  3778. public function testLabelContainControl(): void
  3779. {
  3780. $this->Form->setTemplates([
  3781. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3782. ]);
  3783. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3784. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3785. ]);
  3786. $expected = [
  3787. 'label' => ['for' => 'person-accept-terms'],
  3788. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3789. 'Accept',
  3790. '/label',
  3791. ];
  3792. $this->assertHtml($expected, $result);
  3793. }
  3794. /**
  3795. * testTextbox method
  3796. *
  3797. * Test textbox element generation.
  3798. */
  3799. public function testTextbox(): void
  3800. {
  3801. $result = $this->Form->text('Model.field');
  3802. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3803. $this->assertHtml($expected, $result);
  3804. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3805. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3806. $this->assertHtml($expected, $result);
  3807. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3808. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3809. $this->assertHtml($expected, $result);
  3810. }
  3811. /**
  3812. * testTextBoxDataAndError method
  3813. *
  3814. * Test that text() hooks up with request data and error fields.
  3815. */
  3816. public function testTextBoxDataAndError(): void
  3817. {
  3818. $this->article['errors'] = [
  3819. 'Contact' => ['text' => 'wrong'],
  3820. ];
  3821. $this->View->setRequest($this->View->getRequest()
  3822. ->withData('Model.text', 'test <strong>HTML</strong> values')
  3823. ->withData('Contact.text', 'test'));
  3824. $this->Form->create($this->article);
  3825. $result = $this->Form->text('Model.text');
  3826. $expected = [
  3827. 'input' => [
  3828. 'type' => 'text',
  3829. 'name' => 'Model[text]',
  3830. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3831. ],
  3832. ];
  3833. $this->assertHtml($expected, $result);
  3834. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3835. $expected = [
  3836. 'input' => [
  3837. 'type' => 'text',
  3838. 'name' => 'Contact[text]',
  3839. 'value' => 'test',
  3840. 'id' => 'theID',
  3841. 'class' => 'form-error',
  3842. ],
  3843. ];
  3844. $this->assertHtml($expected, $result);
  3845. }
  3846. /**
  3847. * testDefaultValue method
  3848. *
  3849. * Test default value setting.
  3850. */
  3851. public function testTextDefaultValue(): void
  3852. {
  3853. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  3854. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3855. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3856. $this->assertHtml($expected, $result);
  3857. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  3858. $this->Form->create();
  3859. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3860. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3861. $this->assertHtml($expected, $result);
  3862. $Articles = $this->getTableLocator()->get('Articles');
  3863. $title = $Articles->getSchema()->getColumn('title');
  3864. $Articles->getSchema()->addColumn(
  3865. 'title',
  3866. ['default' => 'default title', 'length' => 255] + $title
  3867. );
  3868. $entity = $Articles->newEmptyEntity();
  3869. $this->Form->create($entity);
  3870. // Get default value from schema
  3871. $result = $this->Form->text('title');
  3872. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title', 'maxlength' => '255']];
  3873. $this->assertHtml($expected, $result);
  3874. // Don't get value from schema
  3875. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3876. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3877. $this->assertHtml($expected, $result);
  3878. // Custom default value overrides default value from schema
  3879. $result = $this->Form->text('title', ['default' => 'override default']);
  3880. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default', 'maxlength' => '255']];
  3881. $this->assertHtml($expected, $result);
  3882. // Default value from schema is used only for new entities.
  3883. $entity->setNew(false);
  3884. $result = $this->Form->text('title');
  3885. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3886. $this->assertHtml($expected, $result);
  3887. }
  3888. /**
  3889. * testError method
  3890. *
  3891. * Test field error generation.
  3892. */
  3893. public function testError(): void
  3894. {
  3895. $this->article['errors'] = [
  3896. 'Article' => ['field' => 'email'],
  3897. ];
  3898. $this->Form->create($this->article);
  3899. $result = $this->Form->error('Article.field');
  3900. $expected = [
  3901. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3902. 'email',
  3903. '/div',
  3904. ];
  3905. $this->assertHtml($expected, $result);
  3906. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  3907. $expected = [
  3908. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3909. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3910. '/div',
  3911. ];
  3912. $this->assertHtml($expected, $result);
  3913. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  3914. $expected = [
  3915. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3916. '<strong', 'Badness!', '/strong',
  3917. '/div',
  3918. ];
  3919. $this->assertHtml($expected, $result);
  3920. }
  3921. /**
  3922. * testErrorRuleName method
  3923. *
  3924. * Test error translation can use rule names for translating.
  3925. */
  3926. public function testErrorRuleName(): void
  3927. {
  3928. $this->article['errors'] = [
  3929. 'Article' => [
  3930. 'field' => ['email' => 'Your email was not good'],
  3931. ],
  3932. ];
  3933. $this->Form->create($this->article);
  3934. $result = $this->Form->error('Article.field');
  3935. $expected = [
  3936. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3937. 'Your email was not good',
  3938. '/div',
  3939. ];
  3940. $this->assertHtml($expected, $result);
  3941. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  3942. $expected = [
  3943. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3944. 'Email in use',
  3945. '/div',
  3946. ];
  3947. $this->assertHtml($expected, $result);
  3948. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  3949. $expected = [
  3950. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3951. 'Email in use',
  3952. '/div',
  3953. ];
  3954. $this->assertHtml($expected, $result);
  3955. $result = $this->Form->error('Article.field', [
  3956. 'email' => 'Key is preferred',
  3957. 'Your email was not good' => 'Email in use',
  3958. ]);
  3959. $expected = [
  3960. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3961. 'Key is preferred',
  3962. '/div',
  3963. ];
  3964. $this->assertHtml($expected, $result);
  3965. }
  3966. /**
  3967. * testErrorMessages method
  3968. *
  3969. * Test error with nested lists.
  3970. */
  3971. public function testErrorMessages(): void
  3972. {
  3973. $this->article['errors'] = [
  3974. 'Article' => ['field' => 'email'],
  3975. ];
  3976. $this->Form->create($this->article);
  3977. $result = $this->Form->error('Article.field', [
  3978. 'email' => 'No good!',
  3979. ]);
  3980. $expected = [
  3981. 'div' => ['class' => 'error-message', 'id' => 'article-field-error'],
  3982. 'No good!',
  3983. '/div',
  3984. ];
  3985. $this->assertHtml($expected, $result);
  3986. }
  3987. /**
  3988. * testErrorMultipleMessages method
  3989. *
  3990. * Test error() with multiple messages.
  3991. */
  3992. public function testErrorMultipleMessages(): void
  3993. {
  3994. $this->article['errors'] = [
  3995. 'field' => ['notBlank', 'email', 'Something else'],
  3996. ];
  3997. $this->Form->create($this->article);
  3998. $result = $this->Form->error('field', [
  3999. 'notBlank' => 'Cannot be empty',
  4000. 'email' => 'No good!',
  4001. ]);
  4002. $expected = [
  4003. 'div' => ['class' => 'error-message', 'id' => 'field-error'],
  4004. 'ul' => [],
  4005. '<li', 'Cannot be empty', '/li',
  4006. '<li', 'No good!', '/li',
  4007. '<li', 'Something else', '/li',
  4008. '/ul',
  4009. '/div',
  4010. ];
  4011. $this->assertHtml($expected, $result);
  4012. }
  4013. /**
  4014. * testPassword method
  4015. *
  4016. * Test password element generation.
  4017. */
  4018. public function testPassword(): void
  4019. {
  4020. $this->article['errors'] = [
  4021. 'Contact' => [
  4022. 'passwd' => 1,
  4023. ],
  4024. ];
  4025. $this->Form->create($this->article);
  4026. $result = $this->Form->password('Contact.field');
  4027. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4028. $this->assertHtml($expected, $result);
  4029. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4030. $this->Form->create($this->article);
  4031. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4032. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4033. $this->assertHtml($expected, $result);
  4034. }
  4035. /**
  4036. * testRadio method
  4037. *
  4038. * Test radio element set generation.
  4039. */
  4040. public function testRadio(): void
  4041. {
  4042. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4043. $expected = [
  4044. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4045. ['label' => ['for' => 'model-field-0']],
  4046. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4047. 'option A',
  4048. '/label',
  4049. ['label' => ['for' => 'model-field-1']],
  4050. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4051. 'option B',
  4052. '/label',
  4053. ];
  4054. $this->assertHtml($expected, $result);
  4055. $result = $this->Form->radio('Model.field', new Collection(['option A', 'option B']));
  4056. $this->assertHtml($expected, $result);
  4057. $result = $this->Form->radio(
  4058. 'Employee.vegetarian',
  4059. ['yes' => 'Yes', 'no' => 'No'],
  4060. ['form' => 'my-form', 'id' => 'id-veg']
  4061. );
  4062. $expected = [
  4063. 'input' => ['type' => 'hidden', 'name' => 'Employee[vegetarian]', 'value' => '', 'form' => 'my-form', 'id' => 'id-veg'],
  4064. ['label' => ['for' => 'id-veg-yes']],
  4065. ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'yes', 'id' => 'id-veg-yes', 'form' => 'my-form']],
  4066. 'Yes',
  4067. '/label',
  4068. ['label' => ['for' => 'id-veg-no']],
  4069. ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'no', 'id' => 'id-veg-no', 'form' => 'my-form']],
  4070. 'No',
  4071. '/label',
  4072. ];
  4073. $this->assertHtml($expected, $result);
  4074. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4075. $expected = [
  4076. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '', 'id' => 'model-field']],
  4077. ['label' => ['for' => 'model-custom-0']],
  4078. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4079. 'option A',
  4080. '/label',
  4081. ['label' => ['for' => 'model-custom-1']],
  4082. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4083. 'option B',
  4084. '/label',
  4085. ];
  4086. $this->assertHtml($expected, $result);
  4087. $result = $this->Form->radio(
  4088. 'Employee.gender',
  4089. [
  4090. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4091. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4092. ]
  4093. );
  4094. $expected = [
  4095. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'id' => 'employee-gender'],
  4096. ['label' => ['for' => 'employee-gender-male']],
  4097. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4098. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4099. 'Male',
  4100. '/label',
  4101. ['label' => ['for' => 'employee-gender-female']],
  4102. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4103. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4104. 'Female',
  4105. '/label',
  4106. ];
  4107. $this->assertHtml($expected, $result);
  4108. }
  4109. /**
  4110. * Test radio with complex options and empty disabled data.
  4111. */
  4112. public function testRadioComplexDisabled(): void
  4113. {
  4114. $options = [
  4115. ['value' => 'r', 'text' => 'red'],
  4116. ['value' => 'b', 'text' => 'blue'],
  4117. ];
  4118. $attrs = ['disabled' => []];
  4119. $result = $this->Form->radio('Model.field', $options, $attrs);
  4120. $expected = [
  4121. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4122. ['label' => ['for' => 'model-field-r']],
  4123. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4124. 'red',
  4125. '/label',
  4126. ['label' => ['for' => 'model-field-b']],
  4127. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4128. 'blue',
  4129. '/label',
  4130. ];
  4131. $this->assertHtml($expected, $result);
  4132. $attrs = ['disabled' => ['r']];
  4133. $result = $this->Form->radio('Model.field', $options, $attrs);
  4134. $this->assertStringContainsString('disabled="disabled"', $result);
  4135. }
  4136. /**
  4137. * testRadioDefaultValue method
  4138. *
  4139. * Test default value setting on radio() method.
  4140. */
  4141. public function testRadioDefaultValue(): void
  4142. {
  4143. $Articles = $this->getTableLocator()->get('Articles');
  4144. $title = $Articles->getSchema()->getColumn('title');
  4145. $Articles->getSchema()->addColumn(
  4146. 'title',
  4147. ['default' => '1'] + $title
  4148. );
  4149. $this->Form->create($Articles->newEmptyEntity());
  4150. $result = $this->Form->radio('title', ['option A', 'option B']);
  4151. $expected = [
  4152. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4153. ['label' => ['for' => 'title-0']],
  4154. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4155. 'option A',
  4156. '/label',
  4157. ['label' => ['for' => 'title-1']],
  4158. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4159. 'option B',
  4160. '/label',
  4161. ];
  4162. $this->assertHtml($expected, $result);
  4163. }
  4164. /**
  4165. * Test setting a hiddenField value on radio buttons.
  4166. */
  4167. public function testRadioHiddenFieldValue(): void
  4168. {
  4169. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4170. $expected = [
  4171. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N', 'id' => 'title']],
  4172. 'label' => ['for' => 'title-0'],
  4173. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4174. 'option A',
  4175. '/label',
  4176. ];
  4177. $this->assertHtml($expected, $result);
  4178. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => '']);
  4179. $expected = [
  4180. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4181. 'label' => ['for' => 'title-0'],
  4182. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4183. 'option A',
  4184. '/label',
  4185. ];
  4186. $this->assertHtml($expected, $result);
  4187. }
  4188. /**
  4189. * testControlRadio method
  4190. *
  4191. * Test that input works with radio types.
  4192. */
  4193. public function testControlRadio(): void
  4194. {
  4195. $result = $this->Form->control('test', [
  4196. 'type' => 'radio',
  4197. 'options' => ['A', 'B'],
  4198. ]);
  4199. $expected = [
  4200. ['div' => ['class' => 'input radio']],
  4201. '<label',
  4202. 'Test',
  4203. '/label',
  4204. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4205. ['label' => ['for' => 'test-0']],
  4206. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4207. 'A',
  4208. '/label',
  4209. ['label' => ['for' => 'test-1']],
  4210. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4211. 'B',
  4212. '/label',
  4213. '/div',
  4214. ];
  4215. $this->assertHtml($expected, $result);
  4216. $result = $this->Form->control('test', [
  4217. 'type' => 'radio',
  4218. 'options' => ['A', 'B'],
  4219. 'value' => '0',
  4220. ]);
  4221. $expected = [
  4222. ['div' => ['class' => 'input radio']],
  4223. '<label',
  4224. 'Test',
  4225. '/label',
  4226. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4227. ['label' => ['for' => 'test-0']],
  4228. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4229. 'A',
  4230. '/label',
  4231. ['label' => ['for' => 'test-1']],
  4232. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4233. 'B',
  4234. '/label',
  4235. '/div',
  4236. ];
  4237. $this->assertHtml($expected, $result);
  4238. $result = $this->Form->control('test', [
  4239. 'type' => 'radio',
  4240. 'options' => ['A', 'B'],
  4241. 'label' => false,
  4242. ]);
  4243. $expected = [
  4244. ['div' => ['class' => 'input radio']],
  4245. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4246. ['label' => ['for' => 'test-0']],
  4247. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4248. 'A',
  4249. '/label',
  4250. ['label' => ['for' => 'test-1']],
  4251. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4252. 'B',
  4253. '/label',
  4254. '/div',
  4255. ];
  4256. $this->assertHtml($expected, $result);
  4257. $result = $this->Form->control('accept', [
  4258. 'type' => 'radio',
  4259. 'options' => [
  4260. 1 => 'positive',
  4261. -1 => 'negative',
  4262. ],
  4263. 'label' => false,
  4264. ]);
  4265. $expected = [
  4266. ['div' => ['class' => 'input radio']],
  4267. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => '', 'id' => 'accept']],
  4268. ['label' => ['for' => 'accept-1']],
  4269. ['input' => [
  4270. 'type' => 'radio',
  4271. 'name' => 'accept',
  4272. 'value' => '1',
  4273. 'id' => 'accept-1',
  4274. ]],
  4275. 'positive',
  4276. '/label',
  4277. ['label' => ['for' => 'accept--1']],
  4278. ['input' => [
  4279. 'type' => 'radio',
  4280. 'name' => 'accept',
  4281. 'value' => '-1',
  4282. 'id' => 'accept--1',
  4283. ]],
  4284. 'negative',
  4285. '/label',
  4286. '/div',
  4287. ];
  4288. $this->assertHtml($expected, $result);
  4289. }
  4290. /**
  4291. * testRadioNoLabel method
  4292. *
  4293. * Test that radio() works with label = false.
  4294. */
  4295. public function testRadioNoLabel(): void
  4296. {
  4297. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4298. $expected = [
  4299. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4300. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4301. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4302. ];
  4303. $this->assertHtml($expected, $result);
  4304. }
  4305. /**
  4306. * testRadioControlInsideLabel method
  4307. *
  4308. * Test generating radio input inside label ala twitter bootstrap.
  4309. */
  4310. public function testRadioControlInsideLabel(): void
  4311. {
  4312. $this->Form->setTemplates([
  4313. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4314. 'radioWrapper' => '{{label}}',
  4315. ]);
  4316. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4317. // phpcs:disable
  4318. $expected = [
  4319. ['input' => [
  4320. 'type' => 'hidden',
  4321. 'name' => 'Model[field]',
  4322. 'value' => '',
  4323. 'id' => 'model-field'
  4324. ]],
  4325. ['label' => ['for' => 'model-field-0']],
  4326. ['input' => [
  4327. 'type' => 'radio',
  4328. 'name' => 'Model[field]',
  4329. 'value' => '0',
  4330. 'id' => 'model-field-0'
  4331. ]],
  4332. 'option A',
  4333. '/label',
  4334. ['label' => ['for' => 'model-field-1']],
  4335. ['input' => [
  4336. 'type' => 'radio',
  4337. 'name' => 'Model[field]',
  4338. 'value' => '1',
  4339. 'id' => 'model-field-1'
  4340. ]],
  4341. 'option B',
  4342. '/label',
  4343. ];
  4344. // phpcs:enable
  4345. $this->assertHtml($expected, $result);
  4346. }
  4347. /**
  4348. * testRadioHiddenControlDisabling method
  4349. *
  4350. * Test disabling the hidden input for radio buttons.
  4351. */
  4352. public function testRadioHiddenControlDisabling(): void
  4353. {
  4354. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4355. $expected = [
  4356. 'label' => ['for' => 'model-1-field-0'],
  4357. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4358. 'option A',
  4359. '/label',
  4360. ];
  4361. $this->assertHtml($expected, $result);
  4362. }
  4363. /**
  4364. * testRadioOutOfRange method
  4365. *
  4366. * Test radio element set generation.
  4367. */
  4368. public function testRadioOutOfRange(): void
  4369. {
  4370. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4371. $expected = [
  4372. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4373. 'label' => ['for' => 'model-field-v'],
  4374. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4375. 'value',
  4376. '/label',
  4377. ];
  4378. $this->assertHtml($expected, $result);
  4379. }
  4380. /**
  4381. * testSelect method
  4382. *
  4383. * Test select element generation.
  4384. */
  4385. public function testSelect(): void
  4386. {
  4387. $result = $this->Form->select('Model.field', []);
  4388. $expected = [
  4389. 'select' => ['name' => 'Model[field]'],
  4390. '/select',
  4391. ];
  4392. $this->assertHtml($expected, $result);
  4393. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4394. $this->Form->create();
  4395. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4396. $expected = [
  4397. 'select' => ['name' => 'Model[field]'],
  4398. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4399. 'good',
  4400. '/option',
  4401. ['option' => ['value' => 'other']],
  4402. 'bad',
  4403. '/option',
  4404. '/select',
  4405. ];
  4406. $this->assertHtml($expected, $result);
  4407. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4408. $this->assertHtml($expected, $result);
  4409. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4410. $this->Form->create();
  4411. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4412. $expected = [
  4413. 'select' => ['name' => 'Model[field]'],
  4414. ['option' => ['value' => 'value']],
  4415. 'good',
  4416. '/option',
  4417. ['option' => ['value' => 'other']],
  4418. 'bad',
  4419. '/option',
  4420. '/select',
  4421. ];
  4422. $this->assertHtml($expected, $result);
  4423. $options = [
  4424. ['value' => 'first', 'text' => 'First'],
  4425. ['value' => 'first', 'text' => 'Another First'],
  4426. ];
  4427. $result = $this->Form->select(
  4428. 'Model.field',
  4429. $options,
  4430. ['escape' => false, 'empty' => false]
  4431. );
  4432. $expected = [
  4433. 'select' => ['name' => 'Model[field]'],
  4434. ['option' => ['value' => 'first']],
  4435. 'First',
  4436. '/option',
  4437. ['option' => ['value' => 'first']],
  4438. 'Another First',
  4439. '/option',
  4440. '/select',
  4441. ];
  4442. $this->assertHtml($expected, $result);
  4443. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4444. $this->Form->create();
  4445. $result = $this->Form->select(
  4446. 'Model.contact_id',
  4447. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4448. ['escape' => false, 'empty' => 'pick something']
  4449. );
  4450. $expected = [
  4451. 'select' => ['name' => 'Model[contact_id]'],
  4452. ['option' => ['value' => '']], 'pick something', '/option',
  4453. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4454. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4455. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4456. '/select',
  4457. ];
  4458. $this->assertHtml($expected, $result);
  4459. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4460. $this->Form->create();
  4461. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4462. $expected = [
  4463. 'select' => ['name' => 'Model[field]'],
  4464. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4465. ['option' => ['value' => '1']], 'Yes', '/option',
  4466. '/select',
  4467. ];
  4468. $this->assertHtml($expected, $result);
  4469. }
  4470. /**
  4471. * testSelectEscapeHtml method
  4472. *
  4473. * Test that select() escapes HTML.
  4474. */
  4475. public function testSelectEscapeHtml(): void
  4476. {
  4477. $result = $this->Form->select(
  4478. 'Model.field',
  4479. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4480. ['empty' => false]
  4481. );
  4482. $expected = [
  4483. 'select' => ['name' => 'Model[field]'],
  4484. ['option' => ['value' => 'first']],
  4485. 'first &quot;html&quot; &lt;chars&gt;',
  4486. '/option',
  4487. ['option' => ['value' => 'second']],
  4488. 'value',
  4489. '/option',
  4490. '/select',
  4491. ];
  4492. $this->assertHtml($expected, $result);
  4493. $result = $this->Form->select(
  4494. 'Model.field',
  4495. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4496. ['escape' => false, 'empty' => false]
  4497. );
  4498. $expected = [
  4499. 'select' => ['name' => 'Model[field]'],
  4500. ['option' => ['value' => 'first']],
  4501. 'first "html" <chars>',
  4502. '/option',
  4503. ['option' => ['value' => 'second']],
  4504. 'value',
  4505. '/option',
  4506. '/select',
  4507. ];
  4508. $this->assertHtml($expected, $result);
  4509. }
  4510. /**
  4511. * testSelectRequired method
  4512. *
  4513. * Test select() with required and disabled attributes.
  4514. */
  4515. public function testSelectRequired(): void
  4516. {
  4517. $this->article['required'] = [
  4518. 'user_id' => true,
  4519. ];
  4520. $this->Form->create($this->article);
  4521. $result = $this->Form->select('user_id', ['option A']);
  4522. $expected = [
  4523. 'select' => [
  4524. 'name' => 'user_id',
  4525. 'required' => 'required',
  4526. ],
  4527. ['option' => ['value' => '0']], 'option A', '/option',
  4528. '/select',
  4529. ];
  4530. $this->assertHtml($expected, $result);
  4531. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4532. $expected = [
  4533. 'select' => [
  4534. 'name' => 'user_id',
  4535. 'disabled' => 'disabled',
  4536. ],
  4537. ['option' => ['value' => '0']], 'option A', '/option',
  4538. '/select',
  4539. ];
  4540. $this->assertHtml($expected, $result);
  4541. }
  4542. public function testSelectEmptyWithRequiredFalse(): void
  4543. {
  4544. $Articles = $this->getTableLocator()->get('Articles');
  4545. $validator = $Articles->getValidator('default');
  4546. $validator->allowEmptyString('user_id');
  4547. $Articles->setValidator('default', $validator);
  4548. $entity = $Articles->newEmptyEntity();
  4549. $this->Form->create($entity);
  4550. $result = $this->Form->select('user_id', ['option A']);
  4551. $expected = [
  4552. 'select' => [
  4553. 'name' => 'user_id',
  4554. ],
  4555. ['option' => ['value' => '']], '/option',
  4556. ['option' => ['value' => '0']], 'option A', '/option',
  4557. '/select',
  4558. ];
  4559. $this->assertHtml($expected, $result);
  4560. }
  4561. /**
  4562. * testNestedSelect method
  4563. *
  4564. * Test select element generation with optgroups.
  4565. */
  4566. public function testNestedSelect(): void
  4567. {
  4568. $result = $this->Form->select(
  4569. 'Model.field',
  4570. [1 => 'One', 2 => 'Two', 'Three' => [
  4571. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4572. ]],
  4573. ['empty' => false]
  4574. );
  4575. $expected = [
  4576. 'select' => ['name' => 'Model[field]'],
  4577. ['option' => ['value' => 1]],
  4578. 'One',
  4579. '/option',
  4580. ['option' => ['value' => 2]],
  4581. 'Two',
  4582. '/option',
  4583. ['optgroup' => ['label' => 'Three']],
  4584. ['option' => ['value' => 3]],
  4585. 'Three',
  4586. '/option',
  4587. ['option' => ['value' => 4]],
  4588. 'Four',
  4589. '/option',
  4590. ['option' => ['value' => 5]],
  4591. 'Five',
  4592. '/option',
  4593. '/optgroup',
  4594. '/select',
  4595. ];
  4596. $this->assertHtml($expected, $result);
  4597. }
  4598. /**
  4599. * testSelectMultiple method
  4600. *
  4601. * Test generation of multiple select elements.
  4602. */
  4603. public function testSelectMultiple(): void
  4604. {
  4605. $options = ['first', 'second', 'third'];
  4606. $result = $this->Form->select(
  4607. 'Model.multi_field',
  4608. $options,
  4609. ['form' => 'my-form', 'multiple' => true]
  4610. );
  4611. $expected = [
  4612. 'input' => [
  4613. 'type' => 'hidden',
  4614. 'name' => 'Model[multi_field]',
  4615. 'value' => '',
  4616. 'form' => 'my-form',
  4617. ],
  4618. 'select' => [
  4619. 'name' => 'Model[multi_field][]',
  4620. 'multiple' => 'multiple',
  4621. 'form' => 'my-form',
  4622. ],
  4623. ['option' => ['value' => '0']],
  4624. 'first',
  4625. '/option',
  4626. ['option' => ['value' => '1']],
  4627. 'second',
  4628. '/option',
  4629. ['option' => ['value' => '2']],
  4630. 'third',
  4631. '/option',
  4632. '/select',
  4633. ];
  4634. $this->assertHtml($expected, $result);
  4635. $result = $this->Form->select(
  4636. 'Model.multi_field',
  4637. $options,
  4638. ['multiple' => 'multiple', 'form' => 'my-form']
  4639. );
  4640. $this->assertHtml($expected, $result);
  4641. $result = $this->Form->select(
  4642. 'Model.multi_field',
  4643. $options,
  4644. ['form' => 'my-form', 'multiple' => false]
  4645. );
  4646. $this->assertStringNotContainsString('multiple', $result);
  4647. }
  4648. /**
  4649. * testCheckboxZeroValue method
  4650. *
  4651. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4652. */
  4653. public function testCheckboxZeroValue(): void
  4654. {
  4655. $result = $this->Form->control('User.get_spam', [
  4656. 'type' => 'checkbox',
  4657. 'value' => '0',
  4658. 'hiddenField' => '1',
  4659. ]);
  4660. $expected = [
  4661. 'div' => ['class' => 'input checkbox'],
  4662. 'label' => ['for' => 'user-get-spam'],
  4663. ['input' => [
  4664. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4665. 'value' => '1',
  4666. ]],
  4667. ['input' => [
  4668. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4669. 'value' => '0', 'id' => 'user-get-spam',
  4670. ]],
  4671. 'Get Spam',
  4672. '/label',
  4673. '/div',
  4674. ];
  4675. $this->assertHtml($expected, $result);
  4676. $result = $this->Form->control('User.get_spam', [
  4677. 'type' => 'checkbox',
  4678. 'value' => '0',
  4679. 'hiddenField' => '',
  4680. ]);
  4681. $expected = [
  4682. 'div' => ['class' => 'input checkbox'],
  4683. 'label' => ['for' => 'user-get-spam'],
  4684. ['input' => [
  4685. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4686. 'value' => '',
  4687. ]],
  4688. ['input' => [
  4689. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4690. 'value' => '0', 'id' => 'user-get-spam',
  4691. ]],
  4692. 'Get Spam',
  4693. '/label',
  4694. '/div',
  4695. ];
  4696. $this->assertHtml($expected, $result);
  4697. }
  4698. /**
  4699. * testHabtmSelectBox method
  4700. *
  4701. * Test generation of habtm select boxes.
  4702. */
  4703. public function testHabtmSelectBox(): void
  4704. {
  4705. $options = [
  4706. 1 => 'blue',
  4707. 2 => 'red',
  4708. 3 => 'green',
  4709. ];
  4710. $tags = [
  4711. new Entity(['id' => 1, 'name' => 'blue']),
  4712. new Entity(['id' => 3, 'name' => 'green']),
  4713. ];
  4714. $article = new Article(['tags' => $tags]);
  4715. $this->Form->create($article);
  4716. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4717. $expected = [
  4718. 'div' => ['class' => 'input select'],
  4719. 'label' => ['for' => 'tags-ids'],
  4720. 'Tags',
  4721. '/label',
  4722. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4723. 'select' => [
  4724. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4725. 'multiple' => 'multiple',
  4726. ],
  4727. ['option' => ['value' => '1', 'selected' => 'selected']],
  4728. 'blue',
  4729. '/option',
  4730. ['option' => ['value' => '2']],
  4731. 'red',
  4732. '/option',
  4733. ['option' => ['value' => '3', 'selected' => 'selected']],
  4734. 'green',
  4735. '/option',
  4736. '/select',
  4737. '/div',
  4738. ];
  4739. $this->assertHtml($expected, $result);
  4740. // make sure only 50 is selected, and not 50f5c0cf
  4741. $options = [
  4742. '1' => 'blue',
  4743. '50f5c0cf' => 'red',
  4744. '50' => 'green',
  4745. ];
  4746. $tags = [
  4747. new Entity(['id' => 1, 'name' => 'blue']),
  4748. new Entity(['id' => 50, 'name' => 'green']),
  4749. ];
  4750. $article = new Article(['tags' => $tags]);
  4751. $this->Form->create($article);
  4752. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4753. $expected = [
  4754. 'div' => ['class' => 'input select'],
  4755. 'label' => ['for' => 'tags-ids'],
  4756. 'Tags',
  4757. '/label',
  4758. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4759. 'select' => [
  4760. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4761. 'multiple' => 'multiple',
  4762. ],
  4763. ['option' => ['value' => '1', 'selected' => 'selected']],
  4764. 'blue',
  4765. '/option',
  4766. ['option' => ['value' => '50f5c0cf']],
  4767. 'red',
  4768. '/option',
  4769. ['option' => ['value' => '50', 'selected' => 'selected']],
  4770. 'green',
  4771. '/option',
  4772. '/select',
  4773. '/div',
  4774. ];
  4775. $this->assertHtml($expected, $result);
  4776. $spacecraft = [
  4777. 1 => 'Orion',
  4778. 2 => 'Helios',
  4779. ];
  4780. $this->View->set('spacecraft', $spacecraft);
  4781. $this->Form->create();
  4782. $result = $this->Form->control('spacecraft._ids');
  4783. $expected = [
  4784. 'div' => ['class' => 'input select'],
  4785. 'label' => ['for' => 'spacecraft-ids'],
  4786. 'Spacecraft',
  4787. '/label',
  4788. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4789. 'select' => [
  4790. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4791. 'multiple' => 'multiple',
  4792. ],
  4793. ['option' => ['value' => '1']],
  4794. 'Orion',
  4795. '/option',
  4796. ['option' => ['value' => '2']],
  4797. 'Helios',
  4798. '/option',
  4799. '/select',
  4800. '/div',
  4801. ];
  4802. $this->assertHtml($expected, $result);
  4803. }
  4804. /**
  4805. * testErrorsForBelongsToManySelect method
  4806. *
  4807. * Tests that errors for belongsToMany select fields are being
  4808. * picked up properly.
  4809. */
  4810. public function testErrorsForBelongsToManySelect(): void
  4811. {
  4812. $spacecraft = [
  4813. 1 => 'Orion',
  4814. 2 => 'Helios',
  4815. ];
  4816. $this->View->set('spacecraft', $spacecraft);
  4817. $article = new Article();
  4818. $article->setError('spacecraft', ['Invalid']);
  4819. $this->Form->create($article);
  4820. $result = $this->Form->control('spacecraft._ids');
  4821. $expected = [
  4822. ['div' => ['class' => 'input select error']],
  4823. 'label' => ['for' => 'spacecraft-ids'],
  4824. 'Spacecraft',
  4825. '/label',
  4826. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4827. 'select' => [
  4828. 'name' => 'spacecraft[_ids][]',
  4829. 'id' => 'spacecraft-ids',
  4830. 'multiple' => 'multiple',
  4831. ],
  4832. ['option' => ['value' => '1']],
  4833. 'Orion',
  4834. '/option',
  4835. ['option' => ['value' => '2']],
  4836. 'Helios',
  4837. '/option',
  4838. '/select',
  4839. ['div' => ['class' => 'error-message', 'id' => 'spacecraft-error']],
  4840. 'Invalid',
  4841. '/div',
  4842. '/div',
  4843. ];
  4844. $this->assertHtml($expected, $result);
  4845. }
  4846. /**
  4847. * testSelectMultipleCheckboxes method
  4848. *
  4849. * Test generation of multi select elements in checkbox format.
  4850. */
  4851. public function testSelectMultipleCheckboxes(): void
  4852. {
  4853. $result = $this->Form->select(
  4854. 'Model.multi_field',
  4855. ['first', 'second', 'third'],
  4856. ['multiple' => 'checkbox']
  4857. );
  4858. $expected = [
  4859. 'input' => [
  4860. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  4861. ],
  4862. ['div' => ['class' => 'checkbox']],
  4863. ['label' => ['for' => 'model-multi-field-0']],
  4864. ['input' => [
  4865. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4866. 'value' => '0', 'id' => 'model-multi-field-0',
  4867. ]],
  4868. 'first',
  4869. '/label',
  4870. '/div',
  4871. ['div' => ['class' => 'checkbox']],
  4872. ['label' => ['for' => 'model-multi-field-1']],
  4873. ['input' => [
  4874. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4875. 'value' => '1', 'id' => 'model-multi-field-1',
  4876. ]],
  4877. 'second',
  4878. '/label',
  4879. '/div',
  4880. ['div' => ['class' => 'checkbox']],
  4881. ['label' => ['for' => 'model-multi-field-2']],
  4882. ['input' => [
  4883. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4884. 'value' => '2', 'id' => 'model-multi-field-2',
  4885. ]],
  4886. 'third',
  4887. '/label',
  4888. '/div',
  4889. ];
  4890. $this->assertHtml($expected, $result);
  4891. $result = $this->Form->select(
  4892. 'Model.multi_field',
  4893. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4894. ['multiple' => 'checkbox']
  4895. );
  4896. $expected = [
  4897. 'input' => [
  4898. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  4899. ],
  4900. ['div' => ['class' => 'checkbox']],
  4901. ['label' => ['for' => 'model-multi-field-a+']],
  4902. ['input' => [
  4903. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4904. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  4905. ]],
  4906. 'first',
  4907. '/label',
  4908. '/div',
  4909. ['div' => ['class' => 'checkbox']],
  4910. ['label' => ['for' => 'model-multi-field-a++']],
  4911. ['input' => [
  4912. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4913. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  4914. ]],
  4915. 'second',
  4916. '/label',
  4917. '/div',
  4918. ['div' => ['class' => 'checkbox']],
  4919. ['label' => ['for' => 'model-multi-field-a+++']],
  4920. ['input' => [
  4921. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4922. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  4923. ]],
  4924. 'third',
  4925. '/label',
  4926. '/div',
  4927. ];
  4928. $this->assertHtml($expected, $result);
  4929. $result = $this->Form->select(
  4930. 'Model.multi_field',
  4931. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4932. ['multiple' => 'checkbox']
  4933. );
  4934. $expected = [
  4935. 'input' => [
  4936. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  4937. ],
  4938. ['div' => ['class' => 'checkbox']],
  4939. ['label' => ['for' => 'model-multi-field-a-b']],
  4940. ['input' => [
  4941. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4942. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  4943. ]],
  4944. 'first',
  4945. '/label',
  4946. '/div',
  4947. ['div' => ['class' => 'checkbox']],
  4948. ['label' => ['for' => 'model-multi-field-a-b1']],
  4949. ['input' => [
  4950. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4951. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  4952. ]],
  4953. 'second',
  4954. '/label',
  4955. '/div',
  4956. ['div' => ['class' => 'checkbox']],
  4957. ['label' => ['for' => 'model-multi-field-a-b2']],
  4958. ['input' => [
  4959. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4960. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  4961. ]],
  4962. 'third',
  4963. '/label',
  4964. '/div',
  4965. ];
  4966. $this->assertHtml($expected, $result);
  4967. }
  4968. /**
  4969. * testSelectMultipleCheckboxRequestData method
  4970. *
  4971. * Ensure that multiCheckbox reads from the request data.
  4972. */
  4973. public function testSelectMultipleCheckboxRequestData(): void
  4974. {
  4975. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  4976. $result = $this->Form->select(
  4977. 'Model.tags',
  4978. ['1' => 'first', 'Array' => 'Array'],
  4979. ['multiple' => 'checkbox']
  4980. );
  4981. $expected = [
  4982. 'input' => [
  4983. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'model-tags',
  4984. ],
  4985. ['div' => ['class' => 'checkbox']],
  4986. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  4987. ['input' => [
  4988. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4989. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  4990. ]],
  4991. 'first',
  4992. '/label',
  4993. '/div',
  4994. ['div' => ['class' => 'checkbox']],
  4995. ['label' => ['for' => 'model-tags-array']],
  4996. ['input' => [
  4997. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4998. 'value' => 'Array', 'id' => 'model-tags-array',
  4999. ]],
  5000. 'Array',
  5001. '/label',
  5002. '/div',
  5003. ];
  5004. $this->assertHtml($expected, $result);
  5005. }
  5006. /**
  5007. * testSelectMultipleCheckboxSecurity method
  5008. *
  5009. * Checks the security hash array generated for multiple-input checkbox elements.
  5010. */
  5011. public function testSelectMultipleCheckboxSecurity(): void
  5012. {
  5013. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5014. $this->Form->create();
  5015. $this->Form->select(
  5016. 'Model.multi_field',
  5017. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5018. ['multiple' => 'checkbox']
  5019. );
  5020. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5021. $this->assertEquals(['Model.multi_field'], $fields);
  5022. $result = $this->Form->secure();
  5023. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  5024. $hash = urlencode($hash . ':');
  5025. $this->assertStringContainsString('"' . $hash . '"', $result);
  5026. }
  5027. /**
  5028. * testSelectMultipleSecureWithNoOptions method
  5029. *
  5030. * Multiple select elements should always be secured as they always participate
  5031. * in the POST data.
  5032. */
  5033. public function testSelectMultipleSecureWithNoOptions(): void
  5034. {
  5035. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5036. $this->Form->create();
  5037. $this->Form->select(
  5038. 'Model.select',
  5039. [],
  5040. ['multiple' => true]
  5041. );
  5042. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5043. $this->assertEquals(['Model.select'], $result);
  5044. }
  5045. /**
  5046. * testSelectNoSecureWithNoOptions method
  5047. *
  5048. * When a select box has no options it should not be added to the fields list
  5049. * as it always fail post validation.
  5050. */
  5051. public function testSelectNoSecureWithNoOptions(): void
  5052. {
  5053. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5054. $this->Form->create();
  5055. $this->Form->select(
  5056. 'Model.select',
  5057. []
  5058. );
  5059. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5060. $this->assertEquals([], $result);
  5061. $this->Form->select(
  5062. 'Model.user_id',
  5063. [],
  5064. ['empty' => true]
  5065. );
  5066. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5067. $this->assertEquals(['Model.user_id'], $result);
  5068. }
  5069. /**
  5070. * testControlMultipleCheckboxes method
  5071. *
  5072. * Test control() resulting in multi select elements being generated.
  5073. */
  5074. public function testControlMultipleCheckboxes(): void
  5075. {
  5076. $result = $this->Form->control('Model.multi_field', [
  5077. 'options' => ['first', 'second', 'third'],
  5078. 'multiple' => 'checkbox',
  5079. ]);
  5080. $expected = [
  5081. ['div' => ['class' => 'input select']],
  5082. ['label' => ['for' => 'model-multi-field']],
  5083. 'Multi Field',
  5084. '/label',
  5085. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5086. ['div' => ['class' => 'checkbox']],
  5087. ['label' => ['for' => 'model-multi-field-0']],
  5088. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5089. 'first',
  5090. '/label',
  5091. '/div',
  5092. ['div' => ['class' => 'checkbox']],
  5093. ['label' => ['for' => 'model-multi-field-1']],
  5094. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5095. 'second',
  5096. '/label',
  5097. '/div',
  5098. ['div' => ['class' => 'checkbox']],
  5099. ['label' => ['for' => 'model-multi-field-2']],
  5100. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5101. 'third',
  5102. '/label',
  5103. '/div',
  5104. '/div',
  5105. ];
  5106. $this->assertHtml($expected, $result);
  5107. $result = $this->Form->control('Model.multi_field', [
  5108. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5109. 'multiple' => 'checkbox',
  5110. ]);
  5111. $expected = [
  5112. ['div' => ['class' => 'input select']],
  5113. ['label' => ['for' => 'model-multi-field']],
  5114. 'Multi Field',
  5115. '/label',
  5116. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5117. ['div' => ['class' => 'checkbox']],
  5118. ['label' => ['for' => 'model-multi-field-a']],
  5119. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5120. 'first',
  5121. '/label',
  5122. '/div',
  5123. ['div' => ['class' => 'checkbox']],
  5124. ['label' => ['for' => 'model-multi-field-b']],
  5125. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5126. 'second',
  5127. '/label',
  5128. '/div',
  5129. ['div' => ['class' => 'checkbox']],
  5130. ['label' => ['for' => 'model-multi-field-c']],
  5131. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5132. 'third',
  5133. '/label',
  5134. '/div',
  5135. '/div',
  5136. ];
  5137. $this->assertHtml($expected, $result);
  5138. }
  5139. /**
  5140. * testSelectHiddenFieldOmission method
  5141. *
  5142. * Test that select() with 'hiddenField' => false omits the hidden field.
  5143. */
  5144. public function testSelectHiddenFieldOmission(): void
  5145. {
  5146. $result = $this->Form->select(
  5147. 'Model.multi_field',
  5148. ['first', 'second'],
  5149. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5150. );
  5151. $this->assertStringNotContainsString('type="hidden"', $result);
  5152. }
  5153. /**
  5154. * testSelectCheckboxMultipleOverrideName method
  5155. *
  5156. * Test that select() with multiple = checkbox works with overriding name attribute.
  5157. */
  5158. public function testSelectCheckboxMultipleOverrideName(): void
  5159. {
  5160. $result = $this->Form->select('category', ['1', '2'], [
  5161. 'multiple' => 'checkbox',
  5162. 'name' => 'fish',
  5163. ]);
  5164. $expected = [
  5165. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  5166. ['div' => ['class' => 'checkbox']],
  5167. ['label' => ['for' => 'fish-0']],
  5168. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5169. '1',
  5170. '/label',
  5171. '/div',
  5172. ['div' => ['class' => 'checkbox']],
  5173. ['label' => ['for' => 'fish-1']],
  5174. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5175. '2',
  5176. '/label',
  5177. '/div',
  5178. ];
  5179. $this->assertHtml($expected, $result);
  5180. $result = $this->Form->multiCheckbox(
  5181. 'category',
  5182. new Collection(['1', '2']),
  5183. ['name' => 'fish']
  5184. );
  5185. $this->assertHtml($expected, $result);
  5186. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5187. 'name' => 'fish',
  5188. ]);
  5189. $this->assertHtml($expected, $result);
  5190. }
  5191. /**
  5192. * testSelectCheckboxMultipleOverrideName method
  5193. *
  5194. * Test that select() with multiple = checkbox works with overriding name attribute.
  5195. */
  5196. public function testSelectCheckboxMultipleCustomId(): void
  5197. {
  5198. $result = $this->Form->select('category', ['1', '2'], [
  5199. 'multiple' => 'checkbox',
  5200. 'id' => 'cat',
  5201. ]);
  5202. $expected = [
  5203. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'cat'],
  5204. ['div' => ['class' => 'checkbox']],
  5205. ['label' => ['for' => 'cat-0']],
  5206. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'cat-0']],
  5207. '1',
  5208. '/label',
  5209. '/div',
  5210. ['div' => ['class' => 'checkbox']],
  5211. ['label' => ['for' => 'cat-1']],
  5212. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'cat-1']],
  5213. '2',
  5214. '/label',
  5215. '/div',
  5216. ];
  5217. $this->assertHtml($expected, $result);
  5218. $result = $this->Form->multiCheckbox(
  5219. 'category',
  5220. ['1', '2'],
  5221. ['id' => 'cat']
  5222. );
  5223. $this->assertHtml($expected, $result);
  5224. }
  5225. /**
  5226. * testControlMultiCheckbox method
  5227. *
  5228. * Test that control() works with multicheckbox.
  5229. */
  5230. public function testControlMultiCheckbox(): void
  5231. {
  5232. $result = $this->Form->control('category', [
  5233. 'type' => 'multicheckbox',
  5234. 'options' => ['1', '2'],
  5235. ]);
  5236. $expected = [
  5237. ['div' => ['class' => 'input multicheckbox']],
  5238. '<label',
  5239. 'Category',
  5240. '/label',
  5241. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'category'],
  5242. ['div' => ['class' => 'checkbox']],
  5243. ['label' => ['for' => 'category-0']],
  5244. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5245. '1',
  5246. '/label',
  5247. '/div',
  5248. ['div' => ['class' => 'checkbox']],
  5249. ['label' => ['for' => 'category-1']],
  5250. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5251. '2',
  5252. '/label',
  5253. '/div',
  5254. '/div',
  5255. ];
  5256. $this->assertHtml($expected, $result);
  5257. }
  5258. /**
  5259. * testCheckbox method
  5260. *
  5261. * Test generation of checkboxes.
  5262. */
  5263. public function testCheckbox(): void
  5264. {
  5265. $result = $this->Form->checkbox('Model.field');
  5266. $expected = [
  5267. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5268. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5269. ];
  5270. $this->assertHtml($expected, $result);
  5271. $result = $this->Form->checkbox('Model.field', [
  5272. 'id' => 'theID',
  5273. 'value' => 'myvalue',
  5274. 'form' => 'my-form',
  5275. ]);
  5276. $expected = [
  5277. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5278. ['input' => [
  5279. 'type' => 'checkbox', 'name' => 'Model[field]',
  5280. 'value' => 'myvalue', 'id' => 'theID',
  5281. 'form' => 'my-form',
  5282. ]],
  5283. ];
  5284. $this->assertHtml($expected, $result);
  5285. }
  5286. /**
  5287. * testCheckboxDefaultValue method
  5288. *
  5289. * Test default value setting on checkbox() method.
  5290. */
  5291. public function testCheckboxDefaultValue(): void
  5292. {
  5293. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5294. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5295. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5296. $this->assertHtml($expected, $result);
  5297. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5298. $this->Form->create();
  5299. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5300. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5301. $this->assertHtml($expected, $result);
  5302. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5303. $this->Form->create();
  5304. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5305. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5306. $this->assertHtml($expected, $result);
  5307. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5308. $this->Form->create();
  5309. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5310. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5311. $this->assertHtml($expected, $result);
  5312. $Articles = $this->getTableLocator()->get('Articles');
  5313. $Articles->getSchema()->addColumn(
  5314. 'published',
  5315. ['type' => 'boolean', 'null' => false, 'default' => true]
  5316. );
  5317. $this->Form->create($Articles->newEmptyEntity());
  5318. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5319. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5320. $this->assertHtml($expected, $result);
  5321. }
  5322. /**
  5323. * testCheckboxCheckedAndError method
  5324. *
  5325. * Test checkbox being checked or having errors.
  5326. */
  5327. public function testCheckboxCheckedAndError(): void
  5328. {
  5329. $this->article['errors'] = [
  5330. 'published' => true,
  5331. ];
  5332. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5333. $this->Form->create($this->article);
  5334. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5335. $expected = [
  5336. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5337. ['input' => [
  5338. 'type' => 'checkbox',
  5339. 'name' => 'published',
  5340. 'value' => 'myvalue',
  5341. 'id' => 'theID',
  5342. 'checked' => 'checked',
  5343. 'class' => 'form-error',
  5344. ]],
  5345. ];
  5346. $this->assertHtml($expected, $result);
  5347. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5348. $this->Form->create($this->article);
  5349. $result = $this->Form->checkbox('published');
  5350. $expected = [
  5351. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5352. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5353. ];
  5354. $this->assertHtml($expected, $result);
  5355. }
  5356. /**
  5357. * testCheckboxCustomNameAttribute method
  5358. *
  5359. * Test checkbox() with a custom name attribute.
  5360. */
  5361. public function testCheckboxCustomNameAttribute(): void
  5362. {
  5363. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5364. $expected = [
  5365. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5366. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5367. ];
  5368. $this->assertHtml($expected, $result);
  5369. }
  5370. /**
  5371. * testCheckboxHiddenField method
  5372. *
  5373. * Test that the hidden input for checkboxes can be omitted or set to a
  5374. * specific value.
  5375. */
  5376. public function testCheckboxHiddenField(): void
  5377. {
  5378. $result = $this->Form->checkbox('UserForm.something', [
  5379. 'hiddenField' => false,
  5380. ]);
  5381. $expected = [
  5382. 'input' => [
  5383. 'type' => 'checkbox',
  5384. 'name' => 'UserForm[something]',
  5385. 'value' => '1',
  5386. ],
  5387. ];
  5388. $this->assertHtml($expected, $result);
  5389. $result = $this->Form->checkbox('UserForm.something', [
  5390. 'value' => 'Y',
  5391. 'hiddenField' => '',
  5392. ]);
  5393. $expected = [
  5394. ['input' => [
  5395. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5396. 'value' => '',
  5397. ]],
  5398. ['input' => [
  5399. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5400. 'value' => 'Y',
  5401. ]],
  5402. ];
  5403. $this->assertHtml($expected, $result);
  5404. $result = $this->Form->checkbox('UserForm.something', [
  5405. 'value' => 'Y',
  5406. 'hiddenField' => 'N',
  5407. ]);
  5408. $expected = [
  5409. ['input' => [
  5410. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5411. 'value' => 'N',
  5412. ]],
  5413. ['input' => [
  5414. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5415. 'value' => 'Y',
  5416. ]],
  5417. ];
  5418. $this->assertHtml($expected, $result);
  5419. }
  5420. /**
  5421. * testTime method
  5422. *
  5423. * Test the time type.
  5424. */
  5425. public function testTime(): void
  5426. {
  5427. $result = $this->Form->time('start_time', [
  5428. 'value' => '2014-03-08 16:30:00',
  5429. ]);
  5430. $expected = [
  5431. 'input' => [
  5432. 'type' => 'time',
  5433. 'name' => 'start_time',
  5434. 'value' => '16:30:00',
  5435. 'step' => '1',
  5436. ],
  5437. ];
  5438. $this->assertHtml($expected, $result);
  5439. }
  5440. /**
  5441. * testDate method
  5442. *
  5443. * Test the date type.
  5444. */
  5445. public function testDate(): void
  5446. {
  5447. $result = $this->Form->date('start_day', [
  5448. 'value' => '2014-03-08',
  5449. ]);
  5450. $expected = [
  5451. 'input' => [
  5452. 'type' => 'date',
  5453. 'name' => 'start_day',
  5454. 'value' => '2014-03-08',
  5455. ],
  5456. ];
  5457. $this->assertHtml($expected, $result);
  5458. $result = $this->Form->date('start_day', [
  5459. 'value' => new Date('2014-03-08'),
  5460. ]);
  5461. $this->assertHtml($expected, $result);
  5462. }
  5463. /**
  5464. * testDateTime method
  5465. */
  5466. public function testDateTime(): void
  5467. {
  5468. $result = $this->Form->dateTime('date', ['default' => true]);
  5469. $expected = [
  5470. 'input' => [
  5471. 'type' => 'datetime-local',
  5472. 'name' => 'date',
  5473. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  5474. 'step' => '1',
  5475. ],
  5476. ];
  5477. $this->assertHtml($expected, $result);
  5478. }
  5479. /**
  5480. * testDateTimeSecured method
  5481. *
  5482. * Test that datetime fields are added to protected fields list.
  5483. */
  5484. public function testDateTimeSecured(): void
  5485. {
  5486. $this->View->setRequest(
  5487. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5488. );
  5489. $this->Form->create();
  5490. $this->Form->dateTime('date');
  5491. $expected = ['date'];
  5492. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5493. $this->assertEquals($expected, $result);
  5494. $this->Form->date('published');
  5495. $expected = ['date', 'published'];
  5496. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5497. $this->assertEquals($expected, $result);
  5498. }
  5499. /**
  5500. * testDateTimeSecuredDisabled method
  5501. *
  5502. * Test that datetime fields are added to protected fields list.
  5503. */
  5504. public function testDateTimeSecuredDisabled(): void
  5505. {
  5506. $this->View->setRequest(
  5507. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5508. );
  5509. $this->Form->create();
  5510. $this->Form->dateTime('date', ['secure' => false]);
  5511. $expected = [];
  5512. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5513. $this->assertEquals($expected, $result);
  5514. $this->Form->date('published', ['secure' => false]);
  5515. $expected = [];
  5516. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5517. $this->assertEquals($expected, $result);
  5518. }
  5519. /**
  5520. * testDatetimeWithDefault method
  5521. *
  5522. * Test that datetime() and default values work.
  5523. */
  5524. public function testDatetimeWithDefault(): void
  5525. {
  5526. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5527. $expected = [
  5528. 'input' => [
  5529. 'type' => 'datetime-local',
  5530. 'name' => 'updated',
  5531. 'value' => '2009-06-01T11:15:30',
  5532. 'step' => '1',
  5533. ],
  5534. ];
  5535. $this->assertHtml($expected, $result);
  5536. $result = $this->Form->dateTime('updated', [
  5537. 'default' => '2009-06-01 11:15:30',
  5538. ]);
  5539. $expected = [
  5540. 'input' => [
  5541. 'type' => 'datetime-local',
  5542. 'name' => 'updated',
  5543. 'value' => '2009-06-01T11:15:30',
  5544. 'step' => '1',
  5545. ],
  5546. ];
  5547. $this->assertHtml($expected, $result);
  5548. }
  5549. /**
  5550. * testMonth method
  5551. *
  5552. * Test generation of a month input.
  5553. */
  5554. public function testMonth(): void
  5555. {
  5556. $result = $this->Form->month('field', ['value' => '']);
  5557. $expected = [
  5558. 'input' => [
  5559. 'type' => 'month',
  5560. 'name' => 'field',
  5561. 'value' => '',
  5562. ],
  5563. ];
  5564. $this->assertHtml($expected, $result);
  5565. $this->View->setRequest(
  5566. $this->View->getRequest()->withData('release', '2050-02-10')
  5567. );
  5568. $this->Form->create();
  5569. $result = $this->Form->month('release');
  5570. $expected = [
  5571. 'input' => [
  5572. 'type' => 'month',
  5573. 'name' => 'release',
  5574. 'value' => '2050-02',
  5575. ],
  5576. ];
  5577. $this->assertHtml($expected, $result);
  5578. $this->View->setRequest(
  5579. $this->View->getRequest()->withData('release', '2050-03')
  5580. );
  5581. $this->Form->create();
  5582. $result = $this->Form->month('release');
  5583. $expected = [
  5584. 'input' => [
  5585. 'type' => 'month',
  5586. 'name' => 'release',
  5587. 'value' => '2050-03',
  5588. ],
  5589. ];
  5590. $this->assertHtml($expected, $result);
  5591. }
  5592. /**
  5593. * testYear method
  5594. *
  5595. * Test generation of a year input.
  5596. */
  5597. public function testYear(): void
  5598. {
  5599. $this->View->setRequest(
  5600. $this->View->getRequest()->withData('published', '2006')
  5601. );
  5602. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5603. $expected = [
  5604. ['select' => ['name' => 'field']],
  5605. ['option' => ['selected' => 'selected', 'value' => '']],
  5606. '/option',
  5607. ['option' => ['value' => '2007']],
  5608. '2007',
  5609. '/option',
  5610. ['option' => ['value' => '2006']],
  5611. '2006',
  5612. '/option',
  5613. '/select',
  5614. ];
  5615. $this->assertHtml($expected, $result);
  5616. $result = $this->Form->year('field', [
  5617. 'value' => '',
  5618. 'min' => 2006,
  5619. 'max' => 2007,
  5620. 'order' => 'asc',
  5621. ]);
  5622. $expected = [
  5623. ['select' => ['name' => 'field']],
  5624. ['option' => ['selected' => 'selected', 'value' => '']],
  5625. '/option',
  5626. ['option' => ['value' => '2006']],
  5627. '2006',
  5628. '/option',
  5629. ['option' => ['value' => '2007']],
  5630. '2007',
  5631. '/option',
  5632. '/select',
  5633. ];
  5634. $this->assertHtml($expected, $result);
  5635. $result = $this->Form->year('published', [
  5636. 'empty' => false,
  5637. 'min' => 2006,
  5638. 'max' => 2007,
  5639. ]);
  5640. $expected = [
  5641. ['select' => ['name' => 'published']],
  5642. ['option' => ['value' => '2007']],
  5643. '2007',
  5644. '/option',
  5645. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5646. '2006',
  5647. '/option',
  5648. '/select',
  5649. ];
  5650. $this->assertHtml($expected, $result);
  5651. $result = $this->Form->year('published', [
  5652. 'empty' => false,
  5653. 'value' => new Date('2008-01-12'),
  5654. 'min' => 2007,
  5655. 'max' => 2009,
  5656. ]);
  5657. $expected = [
  5658. ['select' => ['name' => 'published']],
  5659. ['option' => ['value' => '2009']],
  5660. '2009',
  5661. '/option',
  5662. ['option' => ['value' => '2008', 'selected' => 'selected']],
  5663. '2008',
  5664. '/option',
  5665. ['option' => ['value' => '2007']],
  5666. '2007',
  5667. '/option',
  5668. '/select',
  5669. ];
  5670. $this->assertHtml($expected, $result);
  5671. $result = $this->Form->year('published', [
  5672. 'empty' => 'Published on',
  5673. ]);
  5674. $this->assertStringContainsString('Published on', $result);
  5675. }
  5676. /**
  5677. * testControlYearPreEpoch method
  5678. *
  5679. * Test minYear being prior to the unix epoch.
  5680. */
  5681. public function testControlYearPreEpoch(): void
  5682. {
  5683. $start = date('Y') - 80;
  5684. $end = date('Y') - 18;
  5685. $result = $this->Form->control('birth_year', [
  5686. 'type' => 'year',
  5687. 'label' => 'Birth Year',
  5688. 'min' => $start,
  5689. 'max' => $end,
  5690. ]);
  5691. $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
  5692. $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
  5693. $this->assertStringNotContainsString('value="00">00', $result);
  5694. }
  5695. /**
  5696. * test control() datetime & required attributes
  5697. */
  5698. public function testControlDatetimeRequired(): void
  5699. {
  5700. $result = $this->Form->control('birthday', [
  5701. 'type' => 'date',
  5702. 'required' => true,
  5703. ]);
  5704. $this->assertStringContainsString(
  5705. '<input type="date" name="birthday" required="required"',
  5706. $result
  5707. );
  5708. }
  5709. /**
  5710. * testYearAutoExpandRange method
  5711. */
  5712. public function testYearAutoExpandRange(): void
  5713. {
  5714. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5715. $result = $this->Form->year('birthday');
  5716. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5717. $result = $matches[1];
  5718. $expected = range(date('Y') + 5, 1930);
  5719. $this->assertEquals($expected, $result);
  5720. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5721. $this->Form->create();
  5722. $result = $this->Form->year('release');
  5723. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5724. $result = $matches[1];
  5725. $expected = range(2050, date('Y') - 5);
  5726. $this->assertEquals($expected, $result);
  5727. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5728. $this->Form->create();
  5729. $result = $this->Form->year('release', [
  5730. 'min' => 1890,
  5731. 'max' => 1900,
  5732. ]);
  5733. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5734. $result = $matches[1];
  5735. $expected = range(1900, 1881);
  5736. $this->assertEquals($expected, $result);
  5737. }
  5738. /**
  5739. * test control placeholder + label
  5740. */
  5741. public function testControlLabelAndPlaceholder(): void
  5742. {
  5743. $this->Form->create($this->article);
  5744. $result = $this->Form->control('title', ['label' => 'Title', 'placeholder' => 'Add title']);
  5745. $expected = [
  5746. 'div' => ['class' => 'input text required'],
  5747. 'label' => ['for' => 'title'],
  5748. 'Title',
  5749. '/label',
  5750. 'input' => [
  5751. 'aria-required' => 'true',
  5752. 'type' => 'text',
  5753. 'required' => 'required',
  5754. 'placeholder' => 'Add title',
  5755. 'id' => 'title',
  5756. 'name' => 'title',
  5757. 'data-validity-message' => 'This field cannot be left empty',
  5758. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5759. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5760. ],
  5761. '/div',
  5762. ];
  5763. $this->assertHtml($expected, $result);
  5764. }
  5765. /**
  5766. * testControlLabelFalse method
  5767. *
  5768. * Test the label option being set to false.
  5769. */
  5770. public function testControlLabelFalse(): void
  5771. {
  5772. $this->Form->create($this->article);
  5773. $result = $this->Form->control('title', ['label' => false]);
  5774. $expected = [
  5775. 'div' => ['class' => 'input text required'],
  5776. 'input' => [
  5777. 'aria-required' => 'true',
  5778. 'type' => 'text',
  5779. 'required' => 'required',
  5780. 'id' => 'title',
  5781. 'name' => 'title',
  5782. 'data-validity-message' => 'This field cannot be left empty',
  5783. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5784. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5785. ],
  5786. '/div',
  5787. ];
  5788. $this->assertHtml($expected, $result);
  5789. $this->Form->create($this->article);
  5790. $result = $this->Form->control('title', ['label' => false, 'placeholder' => 'Add title']);
  5791. $expected['input'] += [
  5792. 'placeholder' => 'Add title',
  5793. 'aria-label' => 'Add title',
  5794. ];
  5795. $this->assertHtml($expected, $result);
  5796. }
  5797. /**
  5798. * testTextArea method
  5799. *
  5800. * Test generation of a textarea input.
  5801. */
  5802. public function testTextArea(): void
  5803. {
  5804. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5805. $result = $this->Form->textarea('field');
  5806. $expected = [
  5807. 'textarea' => ['name' => 'field', 'rows' => 5],
  5808. 'some test data',
  5809. '/textarea',
  5810. ];
  5811. $this->assertHtml($expected, $result);
  5812. $result = $this->Form->textarea('user.bio');
  5813. $expected = [
  5814. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5815. '/textarea',
  5816. ];
  5817. $this->assertHtml($expected, $result);
  5818. $this->View->setRequest($this->View->getRequest()
  5819. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5820. $this->Form->create();
  5821. $result = $this->Form->textarea('field');
  5822. $expected = [
  5823. 'textarea' => ['name' => 'field', 'rows' => 5],
  5824. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5825. '/textarea',
  5826. ];
  5827. $this->assertHtml($expected, $result);
  5828. $this->View->setRequest($this->View->getRequest()->withData(
  5829. 'Model.field',
  5830. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  5831. ));
  5832. $this->Form->create();
  5833. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5834. $expected = [
  5835. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  5836. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5837. '/textarea',
  5838. ];
  5839. $this->assertHtml($expected, $result);
  5840. $result = $this->Form->textarea('0.OtherModel.field');
  5841. $expected = [
  5842. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  5843. '/textarea',
  5844. ];
  5845. $this->assertHtml($expected, $result);
  5846. }
  5847. /**
  5848. * testTextAreaWithStupidCharacters method
  5849. *
  5850. * Test text area with non-ascii characters.
  5851. */
  5852. public function testTextAreaWithStupidCharacters(): void
  5853. {
  5854. $result = $this->Form->textarea('Post.content', [
  5855. 'value' => 'GREAT®',
  5856. 'rows' => '15',
  5857. 'cols' => '75',
  5858. ]);
  5859. $expected = [
  5860. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5861. 'GREAT®',
  5862. '/textarea',
  5863. ];
  5864. $this->assertHtml($expected, $result);
  5865. }
  5866. /**
  5867. * testTextAreaMaxLength method
  5868. *
  5869. * Test textareas maxlength read from schema.
  5870. */
  5871. public function testTextAreaMaxLength(): void
  5872. {
  5873. $this->Form->create([
  5874. 'schema' => [
  5875. 'stuff' => ['type' => 'string', 'length' => 10],
  5876. ],
  5877. ]);
  5878. $result = $this->Form->control('other', ['type' => 'textarea']);
  5879. $expected = [
  5880. 'div' => ['class' => 'input textarea'],
  5881. 'label' => ['for' => 'other'],
  5882. 'Other',
  5883. '/label',
  5884. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  5885. '/textarea',
  5886. '/div',
  5887. ];
  5888. $this->assertHtml($expected, $result);
  5889. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  5890. $expected = [
  5891. 'div' => ['class' => 'input textarea'],
  5892. 'label' => ['for' => 'stuff'],
  5893. 'Stuff',
  5894. '/label',
  5895. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  5896. '/textarea',
  5897. '/div',
  5898. ];
  5899. $this->assertHtml($expected, $result);
  5900. }
  5901. /**
  5902. * testHiddenField method
  5903. *
  5904. * Test generation of a hidden input.
  5905. */
  5906. public function testHidden(): void
  5907. {
  5908. $this->article['errors'] = [
  5909. 'field' => true,
  5910. ];
  5911. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  5912. $this->Form->create($this->article);
  5913. $result = $this->Form->hidden('field', ['id' => 'theID']);
  5914. $expected = [
  5915. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  5916. $this->assertHtml($expected, $result);
  5917. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5918. $expected = [
  5919. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  5920. ];
  5921. $this->assertHtml($expected, $result);
  5922. }
  5923. /**
  5924. * Test hidden() with various boolean values.
  5925. */
  5926. public function testHiddenBooleanValues(): void
  5927. {
  5928. $this->Form->create($this->article);
  5929. $result = $this->Form->hidden('field', ['value' => null]);
  5930. $expected = [
  5931. 'input' => ['type' => 'hidden', 'name' => 'field'],
  5932. ];
  5933. $this->assertHtml($expected, $result);
  5934. $result = $this->Form->hidden('field', ['value' => true]);
  5935. $expected = [
  5936. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '1'],
  5937. ];
  5938. $this->assertHtml($expected, $result);
  5939. $result = $this->Form->hidden('field', ['value' => false]);
  5940. $expected = [
  5941. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '0'],
  5942. ];
  5943. $this->assertHtml($expected, $result);
  5944. }
  5945. /**
  5946. * testFileUploadField method
  5947. *
  5948. * Test generation of a file upload input.
  5949. */
  5950. public function testFileUploadField(): void
  5951. {
  5952. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5953. $result = $this->Form->file('Model.upload');
  5954. $this->assertHtml($expected, $result);
  5955. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  5956. 'name' => '', 'type' => '', 'tmp_name' => '',
  5957. 'error' => 4, 'size' => 0,
  5958. ]));
  5959. $result = $this->Form->file('Model.upload');
  5960. $this->assertHtml($expected, $result);
  5961. $this->View->setRequest(
  5962. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  5963. );
  5964. $result = $this->Form->file('Model.upload');
  5965. $this->assertHtml($expected, $result);
  5966. }
  5967. /**
  5968. * testFileUploadOnOtherModel method
  5969. *
  5970. * Test File upload input on a model not used in create().
  5971. */
  5972. public function testFileUploadOnOtherModel(): void
  5973. {
  5974. $this->Form->create($this->article, ['type' => 'file']);
  5975. $result = $this->Form->file('ValidateProfile.city');
  5976. $expected = [
  5977. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  5978. ];
  5979. $this->assertHtml($expected, $result);
  5980. }
  5981. /**
  5982. * testButton method
  5983. *
  5984. * Test generation of a form button.
  5985. */
  5986. public function testButton(): void
  5987. {
  5988. $result = $this->Form->button('Hi');
  5989. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  5990. $this->assertHtml($expected, $result);
  5991. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'escapeTitle' => false]);
  5992. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  5993. $this->assertHtml($expected, $result);
  5994. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm', 'escapeTitle' => false]);
  5995. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  5996. $this->assertHtml($expected, $result);
  5997. $result = $this->Form->button('<Clear Form>', ['type' => 'reset']);
  5998. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  5999. $this->assertHtml($expected, $result);
  6000. $result = $this->Form->button('No type', ['type' => false]);
  6001. $expected = ['button' => [], 'No type', '/button'];
  6002. $this->assertHtml($expected, $result);
  6003. $result = $this->Form->button('Upload Text', [
  6004. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6005. 'escape' => false,
  6006. ]);
  6007. $this->assertDoesNotMatchRegularExpression('/\&039/', $result);
  6008. }
  6009. /**
  6010. * testButtonUnlockedByDefault method
  6011. *
  6012. * Test that button() makes unlocked fields by default.
  6013. */
  6014. public function testButtonUnlockedByDefault(): void
  6015. {
  6016. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6017. $this->Form->create();
  6018. $this->Form->button('Save', ['name' => 'save']);
  6019. $this->Form->button('Clear');
  6020. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6021. $this->assertEquals(['save'], $result);
  6022. }
  6023. /**
  6024. * Test generation of a form button with confirm message.
  6025. */
  6026. public function testButtonWithConfirm(): void
  6027. {
  6028. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6029. $expected = ['button' => [
  6030. 'type' => 'submit',
  6031. 'data-confirm-message' => 'Confirm me!',
  6032. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6033. ], 'Hi', '/button'];
  6034. $this->assertHtml($expected, $result);
  6035. $result = $this->Form->button('Hi', ['escape' => false, 'confirm' => 'Confirm "me"!']);
  6036. $expected = ['button' => [
  6037. 'type' => 'submit',
  6038. 'data-confirm-message' => 'Confirm "me"!',
  6039. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6040. ], 'Hi', '/button'];
  6041. $this->assertHtml($expected, $result);
  6042. }
  6043. /**
  6044. * testPostButton method
  6045. */
  6046. public function testPostButton(): void
  6047. {
  6048. $result = $this->Form->postButton('Hi', '/controller/action');
  6049. $expected = [
  6050. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6051. 'button' => ['type' => 'submit'],
  6052. 'Hi',
  6053. '/button',
  6054. '/form',
  6055. ];
  6056. $this->assertHtml($expected, $result);
  6057. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6058. $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
  6059. }
  6060. /**
  6061. * testPostButtonMethodType method
  6062. */
  6063. public function testPostButtonMethodType(): void
  6064. {
  6065. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6066. $expected = [
  6067. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6068. 'div' => ['style' => 'display:none;'],
  6069. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6070. '/div',
  6071. 'button' => ['type' => 'submit'],
  6072. 'Hi',
  6073. '/button',
  6074. '/form',
  6075. ];
  6076. $this->assertHtml($expected, $result);
  6077. }
  6078. /**
  6079. * testPostButtonFormOptions method
  6080. */
  6081. public function testPostButtonFormOptions(): void
  6082. {
  6083. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6084. $expected = [
  6085. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6086. 'button' => ['type' => 'submit'],
  6087. 'Hi',
  6088. '/button',
  6089. '/form',
  6090. ];
  6091. $this->assertHtml($expected, $result);
  6092. }
  6093. /**
  6094. * testPostButtonNestedData method
  6095. *
  6096. * Test using postButton with N dimensional data.
  6097. */
  6098. public function testPostButtonNestedData(): void
  6099. {
  6100. $data = [
  6101. 'one' => [
  6102. 'two' => [
  6103. 3, 4, 5,
  6104. ],
  6105. ],
  6106. ];
  6107. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6108. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6109. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6110. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6111. }
  6112. /**
  6113. * testSecurePostButton method
  6114. *
  6115. * Test that postButton adds _Token fields.
  6116. */
  6117. public function testSecurePostButton(): void
  6118. {
  6119. $this->View->setRequest($this->View->getRequest()
  6120. ->withAttribute('csrfToken', 'testkey')
  6121. ->withAttribute('formTokenData', ['unlockedFields' => []]));
  6122. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6123. $tokenDebug = urlencode(json_encode([
  6124. '/posts/delete/1',
  6125. [],
  6126. [],
  6127. ]));
  6128. $expected = [
  6129. 'form' => [
  6130. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6131. ],
  6132. ['div' => ['style' => 'display:none;']],
  6133. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6134. '/div',
  6135. 'button' => ['type' => 'submit'],
  6136. 'Delete',
  6137. '/button',
  6138. ['div' => ['style' => 'display:none;']],
  6139. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6140. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6141. ['input' => [
  6142. 'type' => 'hidden',
  6143. 'name' => '_Token[debug]',
  6144. 'value' => $tokenDebug,
  6145. ]],
  6146. '/div',
  6147. '/form',
  6148. ];
  6149. $this->assertHtml($expected, $result);
  6150. }
  6151. /**
  6152. * testPostLink method
  6153. */
  6154. public function testPostLink(): void
  6155. {
  6156. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6157. $expected = [
  6158. 'form' => [
  6159. 'method' => 'post', 'action' => '/posts/delete/1',
  6160. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6161. ],
  6162. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6163. '/form',
  6164. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6165. 'Delete',
  6166. '/a',
  6167. ];
  6168. $this->assertHtml($expected, $result);
  6169. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6170. $expected = [
  6171. 'form' => [
  6172. 'method' => 'post', 'action' => '/posts/delete/1',
  6173. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6174. ],
  6175. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6176. '/form',
  6177. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6178. 'Delete',
  6179. '/a',
  6180. ];
  6181. $this->assertHtml($expected, $result);
  6182. $result = $this->Form->postLink(
  6183. 'Delete',
  6184. '/posts/delete/1',
  6185. ['target' => '_blank', 'class' => 'btn btn-danger']
  6186. );
  6187. $expected = [
  6188. 'form' => [
  6189. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6190. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6191. ],
  6192. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6193. '/form',
  6194. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6195. 'Delete',
  6196. '/a',
  6197. ];
  6198. $this->assertHtml($expected, $result);
  6199. }
  6200. /**
  6201. * testPostLinkWithConfirm method
  6202. *
  6203. * Test the confirm option for postLink().
  6204. */
  6205. public function testPostLinkWithConfirm(): void
  6206. {
  6207. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6208. $expected = [
  6209. 'form' => [
  6210. 'method' => 'post', 'action' => '/posts/delete/1',
  6211. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6212. ],
  6213. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6214. '/form',
  6215. 'a' => [
  6216. 'href' => '#',
  6217. 'data-confirm-message' => 'Confirm?',
  6218. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/',
  6219. ],
  6220. 'Delete',
  6221. '/a',
  6222. ];
  6223. $this->assertHtml($expected, $result);
  6224. $result = $this->Form->postLink(
  6225. 'Delete',
  6226. '/posts/delete/1',
  6227. ['confirm' => "'Confirm'\nthis \"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' => "&#039;Confirm&#039;\nthis &quot;deletion&quot;?",
  6239. 'onclick' => "preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/",
  6240. ],
  6241. 'Delete',
  6242. '/a',
  6243. ];
  6244. $this->assertHtml($expected, $result);
  6245. $this->Form->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { $(\'form[name="{{formName}}"]\').submit();};']);
  6246. $result = $this->Form->postLink(
  6247. 'Delete',
  6248. '/posts/delete/1',
  6249. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  6250. );
  6251. $expected = [
  6252. 'form' => [
  6253. 'method' => 'post', 'action' => '/posts/delete/1',
  6254. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6255. ],
  6256. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6257. '/form',
  6258. 'a' => [
  6259. 'href' => '#',
  6260. 'data-confirm-message' => 'Confirm this deletion?',
  6261. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/',
  6262. ],
  6263. 'Delete',
  6264. '/a',
  6265. ];
  6266. $this->assertHtml($expected, $result);
  6267. }
  6268. /**
  6269. * testPostLinkWithQuery method
  6270. *
  6271. * Test postLink() with query string args.
  6272. */
  6273. public function testPostLinkWithQuery(): void
  6274. {
  6275. $result = $this->Form->postLink(
  6276. 'Delete',
  6277. ['controller' => 'Posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6278. );
  6279. $expected = [
  6280. 'form' => [
  6281. 'method' => 'post', 'action' => '/Posts/delete/1?a=b&amp;c=d',
  6282. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6283. ],
  6284. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6285. '/form',
  6286. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6287. 'Delete',
  6288. '/a',
  6289. ];
  6290. $this->assertHtml($expected, $result);
  6291. }
  6292. /**
  6293. * testPostLinkWithData method
  6294. *
  6295. * Test postLink with additional data.
  6296. */
  6297. public function testPostLinkWithData(): void
  6298. {
  6299. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6300. $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
  6301. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6302. $this->Form->create($entity);
  6303. $this->Form->end();
  6304. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6305. $this->assertStringContainsString(
  6306. '<input type="hidden" name="name" value="show"',
  6307. $result,
  6308. 'should not contain entity data.'
  6309. );
  6310. }
  6311. /**
  6312. * testPostLinkSecurityHash method
  6313. *
  6314. * Test that security hashes for postLink include the url.
  6315. */
  6316. public function testPostLinkSecurityHash(): void
  6317. {
  6318. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6319. $hash .= '%3Aid';
  6320. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6321. $result = $this->Form->postLink(
  6322. 'Delete',
  6323. '/posts/delete/1',
  6324. ['data' => ['id' => 1]]
  6325. );
  6326. $tokenDebug = urlencode(json_encode([
  6327. '/posts/delete/1',
  6328. [
  6329. 'id' => 1,
  6330. ],
  6331. [],
  6332. ]));
  6333. $expected = [
  6334. 'form' => [
  6335. 'method' => 'post', 'action' => '/posts/delete/1',
  6336. 'name', 'style' => 'display:none;',
  6337. ],
  6338. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6339. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6340. 'div' => ['style' => 'display:none;'],
  6341. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6342. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6343. ['input' => [
  6344. 'type' => 'hidden',
  6345. 'name' => '_Token[debug]',
  6346. 'value' => $tokenDebug,
  6347. ]],
  6348. '/div',
  6349. '/form',
  6350. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6351. 'Delete',
  6352. '/a',
  6353. ];
  6354. $this->assertHtml($expected, $result);
  6355. }
  6356. /**
  6357. * testPostLinkSecurityHashBlockMode method
  6358. *
  6359. * Test that postLink doesn't modify the fields in the containing form.
  6360. *
  6361. * postLink() calls inside open forms should not modify the field list
  6362. * for the form.
  6363. */
  6364. public function testPostLinkSecurityHashBlockMode(): void
  6365. {
  6366. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6367. $hash .= '%3A';
  6368. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6369. $this->Form->create(null, ['url' => ['action' => 'add']]);
  6370. $this->Form->control('title');
  6371. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6372. $result = $this->View->fetch('postLink');
  6373. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  6374. $this->assertEquals(['title'], $fields);
  6375. $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
  6376. $reflect = new ReflectionProperty($this->Form, '_lastAction');
  6377. $this->assertSame('/Articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
  6378. }
  6379. /**
  6380. * testPostLinkSecurityHashNoDebugMode method
  6381. *
  6382. * Test that security does not include debug token if debug is false.
  6383. */
  6384. public function testPostLinkSecurityHashNoDebugMode(): void
  6385. {
  6386. Configure::write('debug', false);
  6387. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6388. $hash .= '%3Aid';
  6389. $this->View->setRequest($this->View->getRequest()
  6390. ->withAttribute('formTokenData', ['key' => 'test']));
  6391. $result = $this->Form->postLink(
  6392. 'Delete',
  6393. '/posts/delete/1',
  6394. ['data' => ['id' => 1]]
  6395. );
  6396. $expected = [
  6397. 'form' => [
  6398. 'method' => 'post', 'action' => '/posts/delete/1',
  6399. 'name', 'style' => 'display:none;',
  6400. ],
  6401. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6402. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6403. 'div' => ['style' => 'display:none;'],
  6404. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6405. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6406. '/div',
  6407. '/form',
  6408. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6409. 'Delete',
  6410. '/a',
  6411. ];
  6412. $this->assertHtml($expected, $result);
  6413. }
  6414. /**
  6415. * testPostLinkNestedData method
  6416. *
  6417. * Test using postLink with N dimensional data.
  6418. */
  6419. public function testPostLinkNestedData(): void
  6420. {
  6421. $data = [
  6422. 'one' => [
  6423. 'two' => [
  6424. 3, 4, 5,
  6425. ],
  6426. ],
  6427. ];
  6428. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6429. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6430. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6431. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6432. }
  6433. /**
  6434. * testPostLinkAfterGetForm method
  6435. *
  6436. * Test creating postLinks after a GET form.
  6437. */
  6438. public function testPostLinkAfterGetForm(): void
  6439. {
  6440. $this->View->setRequest($this->View->getRequest()
  6441. ->withAttribute('csrfToken', 'testkey')
  6442. ->withAttribute('formTokenData', []));
  6443. $this->Form->create($this->article, ['type' => 'get']);
  6444. $this->Form->end();
  6445. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6446. $tokenDebug = urlencode(json_encode([
  6447. '/posts/delete/1',
  6448. [],
  6449. [],
  6450. ]));
  6451. $expected = [
  6452. 'form' => [
  6453. 'method' => 'post', 'action' => '/posts/delete/1',
  6454. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6455. ],
  6456. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6457. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6458. 'div' => ['style' => 'display:none;'],
  6459. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6460. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6461. ['input' => [
  6462. 'type' => 'hidden', 'name' => '_Token[debug]',
  6463. 'value' => $tokenDebug,
  6464. ]],
  6465. '/div',
  6466. '/form',
  6467. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6468. 'Delete',
  6469. '/a',
  6470. ];
  6471. $this->assertHtml($expected, $result);
  6472. }
  6473. /**
  6474. * testPostLinkFormBuffer method
  6475. *
  6476. * Test that postLink adds form tags to view block.
  6477. */
  6478. public function testPostLinkFormBuffer(): void
  6479. {
  6480. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6481. $expected = [
  6482. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6483. 'Delete',
  6484. '/a',
  6485. ];
  6486. $this->assertHtml($expected, $result);
  6487. $result = $this->View->fetch('postLink');
  6488. $expected = [
  6489. 'form' => [
  6490. 'method' => 'post', 'action' => '/posts/delete/1',
  6491. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6492. ],
  6493. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6494. '/form',
  6495. ];
  6496. $this->assertHtml($expected, $result);
  6497. $result = $this->Form->postLink(
  6498. 'Delete',
  6499. '/posts/delete/2',
  6500. ['block' => true, 'method' => 'DELETE']
  6501. );
  6502. $expected = [
  6503. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6504. 'Delete',
  6505. '/a',
  6506. ];
  6507. $this->assertHtml($expected, $result);
  6508. $result = $this->View->fetch('postLink');
  6509. $expected = [
  6510. 'form' => [
  6511. 'method' => 'post', 'action' => '/posts/delete/1',
  6512. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6513. ],
  6514. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6515. '/form',
  6516. [
  6517. 'form' => [
  6518. 'method' => 'post', 'action' => '/posts/delete/2',
  6519. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6520. ],
  6521. ],
  6522. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6523. '/form',
  6524. ];
  6525. $this->assertHtml($expected, $result);
  6526. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6527. $expected = [
  6528. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6529. 'Delete',
  6530. '/a',
  6531. ];
  6532. $this->assertHtml($expected, $result);
  6533. $result = $this->View->fetch('foobar');
  6534. $expected = [
  6535. 'form' => [
  6536. 'method' => 'post', 'action' => '/posts/delete/1',
  6537. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6538. ],
  6539. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6540. '/form',
  6541. ];
  6542. $this->assertHtml($expected, $result);
  6543. }
  6544. /**
  6545. * testSubmitButton method
  6546. */
  6547. public function testSubmitButton(): void
  6548. {
  6549. $result = $this->Form->submit('');
  6550. $expected = [
  6551. 'div' => ['class' => 'submit'],
  6552. 'input' => ['type' => 'submit', 'value' => ''],
  6553. '/div',
  6554. ];
  6555. $this->assertHtml($expected, $result);
  6556. $result = $this->Form->submit('Test Submit');
  6557. $expected = [
  6558. 'div' => ['class' => 'submit'],
  6559. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6560. '/div',
  6561. ];
  6562. $this->assertHtml($expected, $result);
  6563. $result = $this->Form->submit('Next >');
  6564. $expected = [
  6565. 'div' => ['class' => 'submit'],
  6566. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6567. '/div',
  6568. ];
  6569. $this->assertHtml($expected, $result);
  6570. $result = $this->Form->submit('Next >', ['escape' => false]);
  6571. $expected = [
  6572. 'div' => ['class' => 'submit'],
  6573. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6574. '/div',
  6575. ];
  6576. $this->assertHtml($expected, $result);
  6577. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6578. $expected = [
  6579. 'div' => ['class' => 'submit'],
  6580. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6581. '/div',
  6582. ];
  6583. $this->assertHtml($expected, $result);
  6584. }
  6585. /**
  6586. * testSubmitImage method
  6587. *
  6588. * Test image submit types.
  6589. */
  6590. public function testSubmitImage(): void
  6591. {
  6592. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6593. $expected = [
  6594. 'div' => ['class' => 'submit'],
  6595. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6596. '/div',
  6597. ];
  6598. $this->assertHtml($expected, $result);
  6599. $result = $this->Form->submit('/relative/cake.power.gif');
  6600. $expected = [
  6601. 'div' => ['class' => 'submit'],
  6602. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6603. '/div',
  6604. ];
  6605. $this->assertHtml($expected, $result);
  6606. $result = $this->Form->submit('cake.power.gif');
  6607. $expected = [
  6608. 'div' => ['class' => 'submit'],
  6609. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6610. '/div',
  6611. ];
  6612. $this->assertHtml($expected, $result);
  6613. $result = $this->Form->submit('Not.an.image');
  6614. $expected = [
  6615. 'div' => ['class' => 'submit'],
  6616. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6617. '/div',
  6618. ];
  6619. $this->assertHtml($expected, $result);
  6620. }
  6621. /**
  6622. * testSubmitUnlockedByDefault method
  6623. *
  6624. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6625. * be submitted at a time.
  6626. */
  6627. public function testSubmitUnlockedByDefault(): void
  6628. {
  6629. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6630. $this->Form->create();
  6631. $this->Form->submit('Go go');
  6632. $this->Form->submit('Save', ['name' => 'save']);
  6633. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6634. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6635. }
  6636. /**
  6637. * testSubmitImageTimestamp method
  6638. *
  6639. * Test submit image with timestamps.
  6640. */
  6641. public function testSubmitImageTimestamp(): void
  6642. {
  6643. Configure::write('Asset.timestamp', 'force');
  6644. $result = $this->Form->submit('cake.power.gif');
  6645. $expected = [
  6646. 'div' => ['class' => 'submit'],
  6647. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6648. '/div',
  6649. ];
  6650. $this->assertHtml($expected, $result);
  6651. }
  6652. /**
  6653. * testDateTimeWithGetForms method
  6654. *
  6655. * Test that datetime() works with GET style forms.
  6656. */
  6657. public function testDateTimeWithGetForms(): void
  6658. {
  6659. $this->Form->create($this->article, ['type' => 'get']);
  6660. $result = $this->Form->datetime('created');
  6661. $expected = [
  6662. 'input' => [
  6663. 'type' => 'datetime-local',
  6664. 'name' => 'created',
  6665. 'value' => '',
  6666. 'step' => '1',
  6667. ],
  6668. ];
  6669. $this->assertHtml($expected, $result);
  6670. $result = $this->Form->datetime('created', ['default' => true]);
  6671. $expected = [
  6672. 'input' => [
  6673. 'type' => 'datetime-local',
  6674. 'name' => 'created',
  6675. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  6676. 'step' => '1',
  6677. ],
  6678. ];
  6679. $this->assertHtml($expected, $result);
  6680. }
  6681. /**
  6682. * Provides fractional schema types
  6683. *
  6684. * @return array
  6685. */
  6686. public static function fractionalTypeProvider(): array
  6687. {
  6688. return [
  6689. ['datetimefractional'],
  6690. ['timestampfractional'],
  6691. ['timestamptimezone'],
  6692. ];
  6693. }
  6694. /**
  6695. * testDateTimeWithFractional method
  6696. *
  6697. * Test that datetime() works with datetimefractional.
  6698. *
  6699. * @dataProvider fractionalTypeProvider
  6700. */
  6701. public function testDateTimeWithFractional(string $type): void
  6702. {
  6703. $this->Form->create([
  6704. 'schema' => [
  6705. 'created' => ['type' => $type],
  6706. ],
  6707. ]);
  6708. $result = $this->Form->datetime('created', [
  6709. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6710. ]);
  6711. $expected = [
  6712. 'input' => [
  6713. 'type' => 'datetime-local',
  6714. 'name' => 'created',
  6715. 'value' => '2019-09-27T02:52:43.123',
  6716. 'step' => '0.001',
  6717. ],
  6718. ];
  6719. $this->assertHtml($expected, $result);
  6720. }
  6721. /**
  6722. * testControlWithFractional method
  6723. *
  6724. * Test that control() works with datetimefractional.
  6725. *
  6726. * @dataProvider fractionalTypeProvider
  6727. */
  6728. public function testControlWithFractional(string $type): void
  6729. {
  6730. $this->Form->create([
  6731. 'schema' => [
  6732. 'created' => ['type' => $type],
  6733. ],
  6734. ]);
  6735. $result = $this->Form->control('created', [
  6736. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6737. ]);
  6738. $expected = [
  6739. 'div' => ['class' => 'input datetime'],
  6740. 'label' => ['for' => 'created'],
  6741. 'Created',
  6742. '/label',
  6743. 'input' => [
  6744. 'type' => 'datetime-local',
  6745. 'name' => 'created',
  6746. 'id' => 'created',
  6747. 'value' => '2019-09-27T02:52:43.123',
  6748. 'step' => '0.001',
  6749. ],
  6750. '/div',
  6751. ];
  6752. $this->assertHtml($expected, $result);
  6753. }
  6754. /**
  6755. * testForMagicControlNonExistentNotValidated method
  6756. */
  6757. public function testForMagicControlNonExistentNotValidated(): void
  6758. {
  6759. $this->Form->create($this->article);
  6760. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6761. $result = $this->Form->control('nonexistent_not_validated');
  6762. $expected = [
  6763. 'label' => ['for' => 'nonexistent-not-validated'],
  6764. 'Nonexistent Not Validated',
  6765. '/label',
  6766. 'input' => [
  6767. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6768. 'id' => 'nonexistent-not-validated',
  6769. ],
  6770. ];
  6771. $this->assertHtml($expected, $result);
  6772. $result = $this->Form->control('nonexistent_not_validated', [
  6773. 'val' => 'my value',
  6774. ]);
  6775. $expected = [
  6776. 'label' => ['for' => 'nonexistent-not-validated'],
  6777. 'Nonexistent Not Validated',
  6778. '/label',
  6779. 'input' => [
  6780. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6781. 'value' => 'my value', 'id' => 'nonexistent-not-validated',
  6782. ],
  6783. ];
  6784. $this->assertHtml($expected, $result);
  6785. $this->View->setRequest(
  6786. $this->View->getRequest()->withData('nonexistent_not_validated', 'CakePHP magic')
  6787. );
  6788. $this->Form->create($this->article);
  6789. $result = $this->Form->control('nonexistent_not_validated');
  6790. $expected = [
  6791. 'label' => ['for' => 'nonexistent-not-validated'],
  6792. 'Nonexistent Not Validated',
  6793. '/label',
  6794. 'input' => [
  6795. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6796. 'value' => 'CakePHP magic', 'id' => 'nonexistent-not-validated',
  6797. ],
  6798. ];
  6799. $this->assertHtml($expected, $result);
  6800. }
  6801. /**
  6802. * testFormMagicControlLabel method
  6803. */
  6804. public function testFormMagicControlLabel(): void
  6805. {
  6806. $this->getTableLocator()->get('Contacts', [
  6807. 'className' => ContactsTable::class,
  6808. ]);
  6809. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6810. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6811. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  6812. $expected = [
  6813. 'label' => ['for' => 'contacts-name'],
  6814. 'My label',
  6815. '/label',
  6816. 'input' => [
  6817. 'type' => 'text',
  6818. 'name' => 'Contacts[name]',
  6819. 'id' => 'contacts-name',
  6820. 'maxlength' => '255',
  6821. ],
  6822. ];
  6823. $this->assertHtml($expected, $result);
  6824. $result = $this->Form->control('name', [
  6825. 'label' => ['class' => 'mandatory'],
  6826. ]);
  6827. $expected = [
  6828. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6829. 'Name',
  6830. '/label',
  6831. 'input' => [
  6832. 'type' => 'text', 'name' => 'name',
  6833. 'id' => 'name', 'maxlength' => '255',
  6834. ],
  6835. ];
  6836. $this->assertHtml($expected, $result);
  6837. $result = $this->Form->control('name', [
  6838. 'div' => false,
  6839. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  6840. ]);
  6841. $expected = [
  6842. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6843. 'My label',
  6844. '/label',
  6845. 'input' => [
  6846. 'type' => 'text', 'name' => 'name',
  6847. 'id' => 'name', 'maxlength' => '255',
  6848. ],
  6849. ];
  6850. $this->assertHtml($expected, $result);
  6851. $result = $this->Form->control('Contact.name', [
  6852. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  6853. ]);
  6854. $expected = [
  6855. 'label' => ['for' => 'my_id'],
  6856. 'Name',
  6857. '/label',
  6858. 'input' => [
  6859. 'type' => 'text', 'name' => 'Contact[name]',
  6860. 'id' => 'my_id', 'maxlength' => '255',
  6861. ],
  6862. ];
  6863. $this->assertHtml($expected, $result);
  6864. $result = $this->Form->control('1.id');
  6865. $expected = ['input' => [
  6866. 'type' => 'hidden', 'name' => '1[id]',
  6867. 'id' => '1-id',
  6868. ]];
  6869. $this->assertHtml($expected, $result);
  6870. $result = $this->Form->control('1.name');
  6871. $expected = [
  6872. 'label' => ['for' => '1-name'],
  6873. 'Name',
  6874. '/label',
  6875. 'input' => [
  6876. 'type' => 'text', 'name' => '1[name]',
  6877. 'id' => '1-name', 'maxlength' => '255',
  6878. ],
  6879. ];
  6880. $this->assertHtml($expected, $result);
  6881. }
  6882. /**
  6883. * testFormEnd method
  6884. */
  6885. public function testFormEnd(): void
  6886. {
  6887. $this->assertSame('</form>', $this->Form->end());
  6888. }
  6889. /**
  6890. * testMultiRecordForm method
  6891. *
  6892. * Test the generation of fields for a multi record form.
  6893. */
  6894. public function testMultiRecordForm(): void
  6895. {
  6896. $articles = $this->getTableLocator()->get('Articles');
  6897. $articles->hasMany('Comments');
  6898. $comment = new Entity(['comment' => 'Value']);
  6899. $article = new Article(['comments' => [$comment]]);
  6900. $this->Form->create([$article]);
  6901. $result = $this->Form->control('0.comments.1.comment');
  6902. // phpcs:disable
  6903. $expected = [
  6904. 'div' => ['class' => 'input textarea'],
  6905. 'label' => ['for' => '0-comments-1-comment'],
  6906. 'Comment',
  6907. '/label',
  6908. 'textarea' => [
  6909. 'name',
  6910. 'id' => '0-comments-1-comment',
  6911. 'rows' => 5
  6912. ],
  6913. '/textarea',
  6914. '/div'
  6915. ];
  6916. // phpcs:enable
  6917. $this->assertHtml($expected, $result);
  6918. $result = $this->Form->control('0.comments.0.comment');
  6919. // phpcs:disable
  6920. $expected = [
  6921. 'div' => ['class' => 'input textarea'],
  6922. 'label' => ['for' => '0-comments-0-comment'],
  6923. 'Comment',
  6924. '/label',
  6925. 'textarea' => [
  6926. 'name',
  6927. 'id' => '0-comments-0-comment',
  6928. 'rows' => 5
  6929. ],
  6930. 'Value',
  6931. '/textarea',
  6932. '/div'
  6933. ];
  6934. // phpcs:enable
  6935. $this->assertHtml($expected, $result);
  6936. $comment->setError('comment', ['Not valid']);
  6937. $result = $this->Form->control('0.comments.0.comment');
  6938. // phpcs:disable
  6939. $expected = [
  6940. 'div' => ['class' => 'input textarea error'],
  6941. 'label' => ['for' => '0-comments-0-comment'],
  6942. 'Comment',
  6943. '/label',
  6944. 'textarea' => [
  6945. 'name',
  6946. 'class' => 'form-error',
  6947. 'id' => '0-comments-0-comment',
  6948. 'aria-invalid' => 'true',
  6949. 'aria-describedby' => '0-comments-0-comment-error',
  6950. 'rows' => 5
  6951. ],
  6952. 'Value',
  6953. '/textarea',
  6954. ['div' => ['class' => 'error-message', 'id' => '0-comments-0-comment-error']],
  6955. 'Not valid',
  6956. '/div',
  6957. '/div'
  6958. ];
  6959. // phpcs:enable
  6960. $this->assertHtml($expected, $result);
  6961. $this->getTableLocator()->get('Comments')
  6962. ->getValidator('default')
  6963. ->allowEmptyString('comment', null, false);
  6964. $result = $this->Form->control('0.comments.1.comment');
  6965. // phpcs:disable
  6966. $expected = [
  6967. 'div' => ['class' => 'input textarea required'],
  6968. 'label' => ['for' => '0-comments-1-comment'],
  6969. 'Comment',
  6970. '/label',
  6971. 'textarea' => [
  6972. 'name',
  6973. 'aria-required' => 'true',
  6974. 'required' => 'required',
  6975. 'id' => '0-comments-1-comment',
  6976. 'rows' => 5,
  6977. 'data-validity-message' => 'This field cannot be left empty',
  6978. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  6979. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  6980. ],
  6981. '/textarea',
  6982. '/div'
  6983. ];
  6984. // phpcs:enable
  6985. $this->assertHtml($expected, $result);
  6986. }
  6987. /**
  6988. * testHtml5Controls method
  6989. *
  6990. * Test that some html5 inputs + FormHelper::__call() work.
  6991. */
  6992. public function testHtml5Controls(): void
  6993. {
  6994. $result = $this->Form->email('User.email');
  6995. $expected = [
  6996. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  6997. ];
  6998. $this->assertHtml($expected, $result);
  6999. $result = $this->Form->search('User.query');
  7000. $expected = [
  7001. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  7002. ];
  7003. $this->assertHtml($expected, $result);
  7004. $result = $this->Form->search('User.query', ['value' => 'test']);
  7005. $expected = [
  7006. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  7007. ];
  7008. $this->assertHtml($expected, $result);
  7009. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7010. $expected = [
  7011. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  7012. ];
  7013. $this->assertHtml($expected, $result);
  7014. }
  7015. /**
  7016. * testHtml5ControlWithControl method
  7017. *
  7018. * Test accessing html5 inputs through control().
  7019. */
  7020. public function testHtml5ControlWithControl(): void
  7021. {
  7022. $this->Form->create();
  7023. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7024. $result = $this->Form->control('website', [
  7025. 'type' => 'url',
  7026. 'val' => 'http://domain.tld',
  7027. 'label' => false,
  7028. ]);
  7029. $expected = [
  7030. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7031. ];
  7032. $this->assertHtml($expected, $result);
  7033. }
  7034. /**
  7035. * testHtml5ControlException method
  7036. *
  7037. * Test errors when field name is missing.
  7038. */
  7039. public function testHtml5ControlException(): void
  7040. {
  7041. $this->expectException(CakeException::class);
  7042. $this->Form->email();
  7043. }
  7044. /**
  7045. * tests fields that are required use custom validation messages
  7046. */
  7047. public function testHtml5ErrorMessage(): void
  7048. {
  7049. $this->Form->setConfig('autoSetCustomValidity', true);
  7050. $validator = (new Validator())
  7051. ->notEmptyString('email', 'Custom error message')
  7052. ->requirePresence('password')
  7053. ->alphaNumeric('password')
  7054. ->notBlank('phone');
  7055. $table = $this->getTableLocator()->get('Contacts', [
  7056. 'className' => ContactsTable::class,
  7057. ]);
  7058. $table->setValidator('default', $validator);
  7059. $contact = new Entity();
  7060. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7061. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7062. $result = $this->Form->control('password');
  7063. $expected = [
  7064. 'label' => ['for' => 'password'],
  7065. 'Password',
  7066. '/label',
  7067. 'input' => [
  7068. 'aria-required' => 'true',
  7069. 'id' => 'password',
  7070. 'name' => 'password',
  7071. 'type' => 'password',
  7072. 'value' => '',
  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('phone');
  7081. $expected = [
  7082. 'label' => ['for' => 'phone'],
  7083. 'Phone',
  7084. '/label',
  7085. 'input' => [
  7086. 'aria-required' => 'true',
  7087. 'id' => 'phone',
  7088. 'name' => 'phone',
  7089. 'type' => 'tel',
  7090. 'value' => '',
  7091. 'maxlength' => 255,
  7092. 'required' => 'required',
  7093. 'data-validity-message' => 'This field cannot be left empty',
  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. $result = $this->Form->control('email');
  7100. $expected = [
  7101. 'label' => ['for' => 'email'],
  7102. 'Email',
  7103. '/label',
  7104. 'input' => [
  7105. 'aria-required' => 'true',
  7106. 'id' => 'email',
  7107. 'name' => 'email',
  7108. 'type' => 'email',
  7109. 'value' => '',
  7110. 'maxlength' => 255,
  7111. 'required' => 'required',
  7112. 'data-validity-message' => 'Custom error message',
  7113. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7114. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7115. ],
  7116. ];
  7117. $this->assertHtml($expected, $result);
  7118. }
  7119. /**
  7120. * tests that custom validation messages are in templateVars
  7121. */
  7122. public function testHtml5ErrorMessageInTemplateVars(): void
  7123. {
  7124. $validator = (new Validator())
  7125. ->notEmptyString('email', 'Custom error "message" & entities')
  7126. ->requirePresence('password')
  7127. ->alphaNumeric('password')
  7128. ->notBlank('phone');
  7129. $table = $this->getTableLocator()->get('Contacts', [
  7130. 'className' => ContactsTable::class,
  7131. ]);
  7132. $table->setValidator('default', $validator);
  7133. $contact = new Entity();
  7134. $this->Form->setConfig('autoSetCustomValidity', false);
  7135. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7136. $this->Form->setTemplates([
  7137. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7138. 'inputContainer' => '{{content}}',
  7139. ]);
  7140. $result = $this->Form->control('password');
  7141. $expected = [
  7142. 'label' => ['for' => 'password'],
  7143. 'Password',
  7144. '/label',
  7145. 'input' => [
  7146. 'aria-required' => 'true',
  7147. 'id' => 'password',
  7148. 'name' => 'password',
  7149. 'type' => 'password',
  7150. 'value' => '',
  7151. 'required' => 'required',
  7152. 'data-message' => 'This field cannot be left empty',
  7153. ],
  7154. ];
  7155. $this->assertHtml($expected, $result);
  7156. $result = $this->Form->control('phone');
  7157. $expected = [
  7158. 'label' => ['for' => 'phone'],
  7159. 'Phone',
  7160. '/label',
  7161. 'input' => [
  7162. 'aria-required' => 'true',
  7163. 'id' => 'phone',
  7164. 'name' => 'phone',
  7165. 'type' => 'tel',
  7166. 'value' => '',
  7167. 'maxlength' => 255,
  7168. 'required' => 'required',
  7169. 'data-message' => 'This field cannot be left empty',
  7170. ],
  7171. ];
  7172. $this->assertHtml($expected, $result);
  7173. $result = $this->Form->control('email', [
  7174. 'templateVars' => [
  7175. 'custom' => 'data-custom="1"',
  7176. ],
  7177. ]);
  7178. $expected = [
  7179. 'label' => ['for' => 'email'],
  7180. 'Email',
  7181. '/label',
  7182. 'input' => [
  7183. 'aria-required' => 'true',
  7184. 'id' => 'email',
  7185. 'name' => 'email',
  7186. 'type' => 'email',
  7187. 'value' => '',
  7188. 'maxlength' => 255,
  7189. 'required' => 'required',
  7190. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7191. 'data-custom' => '1',
  7192. ],
  7193. ];
  7194. $this->assertHtml($expected, $result);
  7195. $this->Form->setConfig('autoSetCustomValidity', true);
  7196. }
  7197. /**
  7198. * testRequiredAttribute method
  7199. *
  7200. * Tests that formhelper sets required attributes.
  7201. */
  7202. public function testRequiredAttribute(): void
  7203. {
  7204. $this->article['required'] = [
  7205. 'title' => true,
  7206. 'body' => false,
  7207. ];
  7208. $this->Form->create($this->article);
  7209. $result = $this->Form->control('title');
  7210. $expected = [
  7211. 'div' => ['class' => 'input text required'],
  7212. 'label' => ['for' => 'title'],
  7213. 'Title',
  7214. '/label',
  7215. 'input' => [
  7216. 'type' => 'text',
  7217. 'name' => 'title',
  7218. 'id' => 'title',
  7219. 'aria-required' => 'true',
  7220. 'required' => 'required',
  7221. 'data-validity-message' => 'This field cannot be left empty',
  7222. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7223. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7224. ],
  7225. '/div',
  7226. ];
  7227. $this->assertHtml($expected, $result);
  7228. $result = $this->Form->control('title', ['required' => false]);
  7229. $this->assertStringNotContainsString('required', $result);
  7230. $result = $this->Form->control('body');
  7231. $expected = [
  7232. 'div' => ['class' => 'input text'],
  7233. 'label' => ['for' => 'body'],
  7234. 'Body',
  7235. '/label',
  7236. 'input' => [
  7237. 'type' => 'text',
  7238. 'name' => 'body',
  7239. 'id' => 'body',
  7240. ],
  7241. '/div',
  7242. ];
  7243. $this->assertHtml($expected, $result);
  7244. $result = $this->Form->control('body', ['required' => true]);
  7245. $this->assertStringContainsString('required', $result);
  7246. }
  7247. /**
  7248. * testControlsNotNested method
  7249. *
  7250. * Tests that it is possible to put inputs outside of the label.
  7251. */
  7252. public function testControlsNotNested(): void
  7253. {
  7254. $this->Form->setTemplates([
  7255. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7256. 'formGroup' => '{{input}}{{label}}',
  7257. ]);
  7258. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7259. $expected = [
  7260. 'div' => ['class' => 'input checkbox'],
  7261. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7262. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7263. 'label' => ['for' => 'foo'],
  7264. 'Foo',
  7265. '/label',
  7266. '/div',
  7267. ];
  7268. $this->assertHtml($expected, $result);
  7269. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7270. $expected = [
  7271. 'div' => ['class' => 'input checkbox'],
  7272. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7273. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7274. '/div',
  7275. ];
  7276. $this->assertHtml($expected, $result);
  7277. $result = $this->Form->control('confirm', [
  7278. 'type' => 'radio',
  7279. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7280. ]);
  7281. $expected = [
  7282. 'div' => ['class' => 'input radio'],
  7283. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '', 'id' => 'confirm']],
  7284. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7285. ['label' => ['for' => 'confirm-y']],
  7286. 'Yes',
  7287. '/label',
  7288. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7289. ['label' => ['for' => 'confirm-n']],
  7290. 'No',
  7291. '/label',
  7292. '<label',
  7293. 'Confirm',
  7294. '/label',
  7295. '/div',
  7296. ];
  7297. $this->assertHtml($expected, $result);
  7298. $result = $this->Form->select('category', ['1', '2'], [
  7299. 'multiple' => 'checkbox',
  7300. 'name' => 'fish',
  7301. ]);
  7302. $expected = [
  7303. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  7304. ['div' => ['class' => 'checkbox']],
  7305. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7306. ['label' => ['for' => 'fish-0']],
  7307. '1',
  7308. '/label',
  7309. '/div',
  7310. ['div' => ['class' => 'checkbox']],
  7311. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7312. ['label' => ['for' => 'fish-1']],
  7313. '2',
  7314. '/label',
  7315. '/div',
  7316. ];
  7317. $this->assertHtml($expected, $result);
  7318. }
  7319. /**
  7320. * testControlContainerTemplates method
  7321. *
  7322. * Test that *Container templates are used by input.
  7323. */
  7324. public function testControlContainerTemplates(): void
  7325. {
  7326. $this->Form->setTemplates([
  7327. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7328. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7329. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7330. ]);
  7331. $this->article['errors'] = [
  7332. 'Article' => ['published' => 'error message'],
  7333. ];
  7334. $this->Form->create($this->article);
  7335. $result = $this->Form->control('accept', [
  7336. 'type' => 'checkbox',
  7337. ]);
  7338. $expected = [
  7339. 'div' => ['class' => 'check'],
  7340. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7341. 'label' => ['for' => 'accept'],
  7342. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7343. 'Accept',
  7344. '/label',
  7345. '/div',
  7346. ];
  7347. $this->assertHtml($expected, $result);
  7348. $result = $this->Form->control('accept', [
  7349. 'type' => 'radio',
  7350. 'options' => ['Y', 'N'],
  7351. ]);
  7352. $this->assertStringContainsString('<div class="rad">', $result);
  7353. $result = $this->Form->control('Article.published', [
  7354. 'type' => 'radio',
  7355. 'options' => ['Y', 'N'],
  7356. ]);
  7357. $this->assertStringContainsString('<div class="rad err">', $result);
  7358. }
  7359. /**
  7360. * testFormGroupTemplates method
  7361. *
  7362. * Test that *Container templates are used by input.
  7363. */
  7364. public function testFormGroupTemplates(): void
  7365. {
  7366. $this->Form->setTemplates([
  7367. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7368. ]);
  7369. $this->Form->create($this->article);
  7370. $result = $this->Form->control('accept', [
  7371. 'type' => 'radio',
  7372. 'options' => ['Y', 'N'],
  7373. ]);
  7374. $this->assertStringContainsString('<div class="radio">', $result);
  7375. }
  7376. /**
  7377. * testResetTemplates method
  7378. *
  7379. * Test resetting templates.
  7380. */
  7381. public function testResetTemplates(): void
  7382. {
  7383. $this->Form->setTemplates(['input' => '<input/>']);
  7384. $this->assertSame('<input/>', $this->Form->templater()->get('input'));
  7385. $this->Form->resetTemplates();
  7386. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7387. }
  7388. /**
  7389. * testContext method
  7390. *
  7391. * Test the context method.
  7392. */
  7393. public function testContext(): void
  7394. {
  7395. $result = $this->Form->context();
  7396. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7397. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7398. $this->assertSame($mock, $this->Form->context($mock));
  7399. $this->assertSame($mock, $this->Form->context());
  7400. }
  7401. /**
  7402. * testAutoDomId method
  7403. */
  7404. public function testAutoDomId(): void
  7405. {
  7406. $result = $this->Form->text('field', ['id' => true]);
  7407. $expected = [
  7408. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7409. ];
  7410. $this->assertHtml($expected, $result);
  7411. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7412. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7413. $expected = [
  7414. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '', 'id' => 'field'],
  7415. ['label' => ['for' => 'field-0']],
  7416. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7417. 'option A',
  7418. '/label',
  7419. ['label' => ['for' => 'field-1']],
  7420. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7421. 'option B',
  7422. '/label',
  7423. ];
  7424. $this->assertHtml($expected, $result);
  7425. $result = $this->Form->select(
  7426. 'multi_field',
  7427. ['first', 'second'],
  7428. ['multiple' => 'checkbox', 'id' => true]
  7429. );
  7430. $expected = [
  7431. 'input' => [
  7432. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '', 'id' => 'multi-field',
  7433. ],
  7434. ['div' => ['class' => 'checkbox']],
  7435. ['label' => ['for' => 'multi-field-0']],
  7436. ['input' => [
  7437. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7438. 'value' => '0', 'id' => 'multi-field-0',
  7439. ]],
  7440. 'first',
  7441. '/label',
  7442. '/div',
  7443. ['div' => ['class' => 'checkbox']],
  7444. ['label' => ['for' => 'multi-field-1']],
  7445. ['input' => [
  7446. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7447. 'value' => '1', 'id' => 'multi-field-1',
  7448. ]],
  7449. 'second',
  7450. '/label',
  7451. '/div',
  7452. ];
  7453. $this->assertHtml($expected, $result);
  7454. }
  7455. /**
  7456. * Test the basic setters and getters for value sources
  7457. */
  7458. public function testFormValueSourcesSettersGetters(): void
  7459. {
  7460. $this->View->setRequest($this->View->getRequest()
  7461. ->withData('id', '1')
  7462. ->withQueryParams(['id' => '2']));
  7463. $expected = ['data', 'context'];
  7464. $result = $this->Form->getValueSources();
  7465. $this->assertEquals($expected, $result);
  7466. $this->Form->setValueSources(['context']);
  7467. $result = $this->Form->getSourceValue('id');
  7468. $this->assertNull($result);
  7469. $this->Form->setValueSources('query');
  7470. $expected = ['query'];
  7471. $result = $this->Form->getValueSources();
  7472. $this->assertEquals($expected, $result);
  7473. $expected = '2';
  7474. $result = $this->Form->getSourceValue('id');
  7475. $this->assertSame($expected, $result);
  7476. $this->Form->setValueSources(['data']);
  7477. $expected = '1';
  7478. $result = $this->Form->getSourceValue('id');
  7479. $this->assertSame($expected, $result);
  7480. $this->Form->setValueSources(['query', 'data']);
  7481. $expected = '2';
  7482. $result = $this->Form->getSourceValue('id');
  7483. $this->assertSame($expected, $result);
  7484. }
  7485. public function testValueSourcesValidation(): void
  7486. {
  7487. $this->expectException(InvalidArgumentException::class);
  7488. $this->expectExceptionMessage('Invalid value source(s): `invalid`, `foo`. Valid values are: `context`, `data`, `query`.');
  7489. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7490. }
  7491. /**
  7492. * Tests the different input rendering values based on sources values switching
  7493. */
  7494. public function testFormValueSourcesSingleSwitchRendering(): void
  7495. {
  7496. $articles = $this->getTableLocator()->get('Articles');
  7497. $article = new Article();
  7498. $articles->patchEntity($article, ['id' => '3']);
  7499. $this->Form->create($article);
  7500. $this->Form->setValueSources(['context']);
  7501. $result = $this->Form->control('id');
  7502. $expected = [
  7503. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7504. ];
  7505. $this->assertHtml($expected, $result);
  7506. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7507. $this->Form->setValueSources(['query']);
  7508. $result = $this->Form->control('id');
  7509. $expected = [
  7510. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7511. ];
  7512. $this->assertHtml($expected, $result);
  7513. $this->View->setRequest($this->View->getRequest()
  7514. ->withQueryParams(['id' => '5a'])
  7515. ->withData('id', '5b'));
  7516. $this->Form->setValueSources(['context']);
  7517. $this->Form->create($article);
  7518. $result = $this->Form->control('id');
  7519. $expected = [
  7520. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7521. ];
  7522. $this->assertHtml($expected, $result);
  7523. $this->Form->setValueSources(['data']);
  7524. $this->Form->create($article);
  7525. $result = $this->Form->control('id');
  7526. $expected = [
  7527. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7528. ];
  7529. $this->assertHtml($expected, $result);
  7530. $this->Form->setValueSources(['query']);
  7531. $result = $this->Form->control('id');
  7532. $expected = [
  7533. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7534. ];
  7535. $this->assertHtml($expected, $result);
  7536. }
  7537. /**
  7538. * Tests the different input rendering values based on sources values switching while supplying
  7539. * an entity (base context) and multiple sources (such as data, query)
  7540. */
  7541. public function testFormValueSourcesListSwitchRendering(): void
  7542. {
  7543. $articles = $this->getTableLocator()->get('Articles');
  7544. $article = new Article();
  7545. $articles->patchEntity($article, ['id' => '3']);
  7546. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7547. $this->Form->create($article);
  7548. $this->Form->setValueSources(['context', 'query']);
  7549. $result = $this->Form->control('id');
  7550. $expected = [
  7551. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7552. ];
  7553. $this->assertHtml($expected, $result);
  7554. $this->Form->setValueSources(['query', 'context']);
  7555. $result = $this->Form->control('id');
  7556. $expected = [
  7557. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7558. ];
  7559. $this->assertHtml($expected, $result);
  7560. $this->Form->setValueSources(['data', 'query', 'context']);
  7561. $result = $this->Form->control('id');
  7562. $expected = [
  7563. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7564. ];
  7565. $this->assertHtml($expected, $result);
  7566. $this->View->setRequest($this->View->getRequest()
  7567. ->withData('id', '8')
  7568. ->withQueryParams(['id' => '9']));
  7569. $this->Form->setValueSources(['data', 'query', 'context']);
  7570. $result = $this->Form->control('id');
  7571. $expected = [
  7572. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7573. ];
  7574. $this->assertHtml($expected, $result);
  7575. }
  7576. /**
  7577. * Test the different form input renderings based on values sources switchings through form options
  7578. */
  7579. public function testFormValueSourcesSwitchViaOptionsRendering(): void
  7580. {
  7581. $articles = $this->getTableLocator()->get('Articles');
  7582. $article = new Article();
  7583. $articles->patchEntity($article, ['id' => '3']);
  7584. $this->View->setRequest(
  7585. $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
  7586. );
  7587. $this->Form->create($article, ['valueSources' => 'query']);
  7588. $result = $this->Form->control('id');
  7589. $expected = [
  7590. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7591. ];
  7592. $this->assertHtml($expected, $result);
  7593. $result = $this->Form->getSourceValue('id');
  7594. $this->assertSame('5', $result);
  7595. $this->Form->setValueSources(['context']);
  7596. $this->Form->create($article, ['valueSources' => 'query']);
  7597. $result = $this->Form->control('id');
  7598. $expected = [
  7599. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7600. ];
  7601. $this->assertHtml($expected, $result);
  7602. $result = $this->Form->getSourceValue('id');
  7603. $this->assertSame('5', $result);
  7604. $this->Form->setValueSources(['query']);
  7605. $this->Form->create($article, ['valueSources' => 'data']);
  7606. $result = $this->Form->control('id');
  7607. $expected = [
  7608. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7609. ];
  7610. $this->assertHtml($expected, $result);
  7611. $result = $this->Form->getSourceValue('id');
  7612. $this->assertSame('4', $result);
  7613. $this->Form->setValueSources(['query']);
  7614. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7615. $result = $this->Form->control('id');
  7616. $expected = [
  7617. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7618. ];
  7619. $this->assertHtml($expected, $result);
  7620. $result = $this->Form->getSourceValue('id');
  7621. $this->assertSame(3, $result);
  7622. }
  7623. /**
  7624. * Test the different form input renderings based on values sources switchings through form options
  7625. */
  7626. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering(): void
  7627. {
  7628. $articles = $this->getTableLocator()->get('Articles');
  7629. $article = new Article();
  7630. $articles->patchEntity($article, ['id' => '3']);
  7631. $this->View->setRequest(
  7632. $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
  7633. );
  7634. $this->Form->setValueSources(['context'])
  7635. ->create($article, ['valueSources' => ['query', 'data']]);
  7636. $result = $this->Form->control('id');
  7637. $expected = [
  7638. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7639. ];
  7640. $this->assertHtml($expected, $result);
  7641. $result = $this->Form->getSourceValue('id');
  7642. $this->assertSame('11', $result);
  7643. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7644. $this->Form->setValueSources(['context'])
  7645. ->create($article, ['valueSources' => ['query', 'data']]);
  7646. $result = $this->Form->control('id');
  7647. $expected = [
  7648. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7649. ];
  7650. $this->assertHtml($expected, $result);
  7651. $result = $this->Form->getSourceValue('id');
  7652. $this->assertSame('10', $result);
  7653. }
  7654. /**
  7655. * Test the different form values sources resetting through From::end();
  7656. */
  7657. public function testFormValueSourcesResetViaEnd(): void
  7658. {
  7659. $expected = ['data', 'context'];
  7660. $result = $this->Form->getValueSources();
  7661. $this->assertEquals($expected, $result);
  7662. $expected = ['query', 'context', 'data'];
  7663. $this->Form->setValueSources(['query', 'context', 'data']);
  7664. $result = $this->Form->getValueSources();
  7665. $this->assertEquals($expected, $result);
  7666. $this->Form->create();
  7667. $result = $this->Form->getValueSources();
  7668. $this->assertEquals($expected, $result);
  7669. $this->Form->end();
  7670. $result = $this->Form->getValueSources();
  7671. $this->assertEquals(['data', 'context'], $result);
  7672. }
  7673. /**
  7674. * Test sources values defaults handling
  7675. */
  7676. public function testFormValueSourcesDefaults(): void
  7677. {
  7678. $this->View->setRequest(
  7679. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7680. );
  7681. $this->Form->create();
  7682. $result = $this->Form->password('password');
  7683. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7684. $this->assertHtml($expected, $result);
  7685. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7686. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7687. $this->assertHtml($expected, $result);
  7688. $this->Form->setValueSources('query');
  7689. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7690. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7691. $this->assertHtml($expected, $result);
  7692. $this->Form->setValueSources('data');
  7693. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7694. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7695. $this->assertHtml($expected, $result);
  7696. }
  7697. /**
  7698. * Test sources values schema defaults handling
  7699. */
  7700. public function testSourcesValueDoesntExistPassThrough(): void
  7701. {
  7702. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7703. $articles = $this->getTableLocator()->get('Articles');
  7704. $entity = $articles->newEmptyEntity();
  7705. $this->Form->create($entity);
  7706. $this->Form->setValueSources(['query', 'context']);
  7707. $result = $this->Form->getSourceValue('category');
  7708. $this->assertSame('sesame-cookies', $result);
  7709. $this->Form->setValueSources(['context', 'query']);
  7710. $result = $this->Form->getSourceValue('category');
  7711. $this->assertSame('sesame-cookies', $result);
  7712. }
  7713. /**
  7714. * testNestedLabelInput method
  7715. *
  7716. * Test the `nestedInput` parameter
  7717. */
  7718. public function testNestedLabelInput(): void
  7719. {
  7720. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7721. $expected = [
  7722. 'div' => ['class' => 'input text'],
  7723. 'label' => ['for' => 'foo'],
  7724. ['input' => [
  7725. 'type' => 'text',
  7726. 'name' => 'foo',
  7727. 'id' => 'foo',
  7728. ]],
  7729. 'Foo',
  7730. '/label',
  7731. '/div',
  7732. ];
  7733. $this->assertHtml($expected, $result);
  7734. }
  7735. /**
  7736. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7737. *
  7738. * This test makes sure `false` excludes the label from the render
  7739. */
  7740. public function testControlLabelManipulationDisableLabels(): void
  7741. {
  7742. $result = $this->Form->control('test', [
  7743. 'type' => 'radio',
  7744. 'options' => ['A', 'B'],
  7745. 'labelOptions' => false,
  7746. ]);
  7747. $expected = [
  7748. ['div' => ['class' => 'input radio']],
  7749. '<label',
  7750. 'Test',
  7751. '/label',
  7752. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7753. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7754. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7755. '/div',
  7756. ];
  7757. $this->assertHtml($expected, $result);
  7758. $result = $this->Form->control('checkbox1', [
  7759. 'label' => 'My checkboxes',
  7760. 'multiple' => 'checkbox',
  7761. 'type' => 'select',
  7762. 'options' => [
  7763. ['text' => 'First Checkbox', 'value' => 1],
  7764. ['text' => 'Second Checkbox', 'value' => 2],
  7765. ],
  7766. 'labelOptions' => false,
  7767. ]);
  7768. $expected = [
  7769. ['div' => ['class' => 'input select']],
  7770. ['label' => ['for' => 'checkbox1']],
  7771. 'My checkboxes',
  7772. '/label',
  7773. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7774. ['div' => ['class' => 'checkbox']],
  7775. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7776. '/div',
  7777. ['div' => ['class' => 'checkbox']],
  7778. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7779. '/div',
  7780. '/div',
  7781. ];
  7782. $this->assertHtml($expected, $result);
  7783. }
  7784. /**
  7785. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7786. *
  7787. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7788. * added to the class if checked.
  7789. *
  7790. * Also checks to make sure any custom attributes are rendered correctly
  7791. */
  7792. public function testControlLabelManipulationRadios(): void
  7793. {
  7794. $result = $this->Form->control('test', [
  7795. 'type' => 'radio',
  7796. 'options' => ['A', 'B'],
  7797. 'labelOptions' => ['class' => 'custom-class'],
  7798. ]);
  7799. $expected = [
  7800. ['div' => ['class' => 'input radio']],
  7801. '<label',
  7802. 'Test',
  7803. '/label',
  7804. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7805. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7806. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7807. 'A',
  7808. '/label',
  7809. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  7810. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7811. 'B',
  7812. '/label',
  7813. '/div',
  7814. ];
  7815. $this->assertHtml($expected, $result);
  7816. $result = $this->Form->control('test', [
  7817. 'type' => 'radio',
  7818. 'options' => ['A', 'B'],
  7819. 'value' => 1,
  7820. 'labelOptions' => ['class' => 'custom-class'],
  7821. ]);
  7822. $expected = [
  7823. ['div' => ['class' => 'input radio']],
  7824. '<label',
  7825. 'Test',
  7826. '/label',
  7827. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7828. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7829. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7830. 'A',
  7831. '/label',
  7832. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  7833. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7834. 'B',
  7835. '/label',
  7836. '/div',
  7837. ];
  7838. $this->assertHtml($expected, $result);
  7839. $result = $this->Form->control('test', [
  7840. 'type' => 'radio',
  7841. 'options' => ['A', 'B'],
  7842. 'value' => 1,
  7843. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  7844. ]);
  7845. $expected = [
  7846. ['div' => ['class' => 'input radio']],
  7847. '<label',
  7848. 'Test',
  7849. '/label',
  7850. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7851. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  7852. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7853. 'A',
  7854. '/label',
  7855. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  7856. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7857. 'B',
  7858. '/label',
  7859. '/div',
  7860. ];
  7861. $this->assertHtml($expected, $result);
  7862. $result = $this->Form->radio('test', ['A', 'B'], [
  7863. 'label' => [
  7864. 'class' => ['custom-class', 'another-class'],
  7865. 'data-name' => 'bob',
  7866. ],
  7867. 'value' => 1,
  7868. ]);
  7869. $expected = [
  7870. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test'],
  7871. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  7872. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7873. 'A',
  7874. '/label',
  7875. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  7876. ['input' => [
  7877. 'type' => 'radio',
  7878. 'name' => 'test',
  7879. 'value' => '1',
  7880. 'id' => 'test-1',
  7881. 'checked' => 'checked',
  7882. ]],
  7883. 'B',
  7884. '/label',
  7885. ];
  7886. $this->assertHtml($expected, $result);
  7887. }
  7888. /**
  7889. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  7890. *
  7891. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7892. * added to the class if checked.
  7893. *
  7894. * Also checks to make sure any custom attributes are rendered correctly
  7895. */
  7896. public function testControlLabelManipulationCheckboxes(): void
  7897. {
  7898. $result = $this->Form->control('checkbox1', [
  7899. 'label' => 'My checkboxes',
  7900. 'multiple' => 'checkbox',
  7901. 'type' => 'select',
  7902. 'options' => [
  7903. ['text' => 'First Checkbox', 'value' => 1],
  7904. ['text' => 'Second Checkbox', 'value' => 2],
  7905. ],
  7906. 'labelOptions' => ['class' => 'custom-class'],
  7907. 'value' => ['1'],
  7908. ]);
  7909. $expected = [
  7910. ['div' => ['class' => 'input select']],
  7911. ['label' => ['for' => 'checkbox1']],
  7912. 'My checkboxes',
  7913. '/label',
  7914. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7915. ['div' => ['class' => 'checkbox']],
  7916. ['label' => [
  7917. 'class' => 'custom-class selected',
  7918. 'for' => 'checkbox1-1',
  7919. ]],
  7920. ['input' => [
  7921. 'type' => 'checkbox',
  7922. 'name' => 'checkbox1[]',
  7923. 'value' => '1',
  7924. 'id' => 'checkbox1-1',
  7925. 'checked' => 'checked',
  7926. ]],
  7927. 'First Checkbox',
  7928. '/label',
  7929. '/div',
  7930. ['div' => ['class' => 'checkbox']],
  7931. ['label' => [
  7932. 'class' => 'custom-class',
  7933. 'for' => 'checkbox1-2',
  7934. ]],
  7935. ['input' => [
  7936. 'type' => 'checkbox',
  7937. 'name' => 'checkbox1[]',
  7938. 'value' => '2',
  7939. 'id' => 'checkbox1-2',
  7940. ]],
  7941. 'Second Checkbox',
  7942. '/label',
  7943. '/div',
  7944. '/div',
  7945. ];
  7946. $this->assertHtml($expected, $result);
  7947. $result = $this->Form->control('checkbox1', [
  7948. 'label' => 'My checkboxes',
  7949. 'multiple' => 'checkbox',
  7950. 'type' => 'select',
  7951. 'options' => [
  7952. ['text' => 'First Checkbox', 'value' => 1],
  7953. ['text' => 'Second Checkbox', 'value' => 2],
  7954. ],
  7955. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  7956. 'value' => ['1'],
  7957. ]);
  7958. $expected = [
  7959. ['div' => ['class' => 'input select']],
  7960. ['label' => ['for' => 'checkbox1']],
  7961. 'My checkboxes',
  7962. '/label',
  7963. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7964. ['div' => ['class' => 'checkbox']],
  7965. ['label' => [
  7966. 'class' => 'custom-class another-class selected',
  7967. 'data-name' => 'bob',
  7968. 'for' => 'checkbox1-1',
  7969. ]],
  7970. ['input' => [
  7971. 'type' => 'checkbox',
  7972. 'name' => 'checkbox1[]',
  7973. 'value' => '1',
  7974. 'id' => 'checkbox1-1',
  7975. 'checked' => 'checked',
  7976. ]],
  7977. 'First Checkbox',
  7978. '/label',
  7979. '/div',
  7980. ['div' => ['class' => 'checkbox']],
  7981. ['label' => [
  7982. 'class' => 'custom-class another-class',
  7983. 'data-name' => 'bob',
  7984. 'for' => 'checkbox1-2',
  7985. ]],
  7986. ['input' => [
  7987. 'type' => 'checkbox',
  7988. 'name' => 'checkbox1[]',
  7989. 'value' => '2',
  7990. 'id' => 'checkbox1-2',
  7991. ]],
  7992. 'Second Checkbox',
  7993. '/label',
  7994. '/div',
  7995. '/div',
  7996. ];
  7997. $this->assertHtml($expected, $result);
  7998. }
  7999. /**
  8000. * testControlMaxLengthArrayContext method
  8001. *
  8002. * Test control() with maxlength attribute in Array Context.
  8003. */
  8004. public function testControlMaxLengthArrayContext(): void
  8005. {
  8006. $this->article['schema'] = [
  8007. 'title' => ['length' => 10],
  8008. ];
  8009. $this->Form->create($this->article);
  8010. $result = $this->Form->control('title');
  8011. $expected = [
  8012. 'div' => ['class'],
  8013. 'label' => ['for'],
  8014. 'Title',
  8015. '/label',
  8016. 'input' => [
  8017. 'aria-required' => 'true',
  8018. 'id',
  8019. 'name' => 'title',
  8020. 'type' => 'text',
  8021. 'required' => 'required',
  8022. 'maxlength' => 10,
  8023. 'data-validity-message' => 'This field cannot be left empty',
  8024. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8025. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8026. ],
  8027. '/div',
  8028. ];
  8029. $this->assertHtml($expected, $result);
  8030. }
  8031. /**
  8032. * testControlMaxLengthEntityContext method
  8033. *
  8034. * Test control() with maxlength attribute in Entity Context.
  8035. */
  8036. public function testControlMaxLengthEntityContext(): void
  8037. {
  8038. $this->article['schema']['title']['length'] = 45;
  8039. $validator = new Validator();
  8040. $validator->maxLength('title', 10);
  8041. $article = new EntityContext(
  8042. [
  8043. 'entity' => new Entity($this->article),
  8044. 'table' => new Table([
  8045. 'alias' => 'Articles',
  8046. 'schema' => $this->article['schema'],
  8047. 'validator' => $validator,
  8048. ]),
  8049. ]
  8050. );
  8051. $this->Form->create($article);
  8052. $result = $this->Form->control('title');
  8053. $expected = [
  8054. 'div' => ['class'],
  8055. 'label' => ['for'],
  8056. 'Title',
  8057. '/label',
  8058. 'input' => [
  8059. 'aria-required' => 'true',
  8060. 'id',
  8061. 'name' => 'title',
  8062. 'type' => 'text',
  8063. 'required' => 'required',
  8064. 'maxlength' => 10,
  8065. 'data-validity-message' => 'This field cannot be left empty',
  8066. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8067. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8068. ],
  8069. '/div',
  8070. ];
  8071. $this->assertHtml($expected, $result);
  8072. $this->article['schema']['title']['length'] = 45;
  8073. $validator = new Validator();
  8074. $validator->maxLength('title', 55);
  8075. $article = new EntityContext(
  8076. [
  8077. 'entity' => new Entity($this->article),
  8078. 'table' => new Table([
  8079. 'schema' => $this->article['schema'],
  8080. 'validator' => $validator,
  8081. 'alias' => 'Articles',
  8082. ]),
  8083. ]
  8084. );
  8085. $this->Form->create($article);
  8086. $result = $this->Form->control('title');
  8087. $expected = [
  8088. 'div' => ['class'],
  8089. 'label' => ['for'],
  8090. 'Title',
  8091. '/label',
  8092. 'input' => [
  8093. 'aria-required' => 'true',
  8094. 'id',
  8095. 'name' => 'title',
  8096. 'type' => 'text',
  8097. 'required' => 'required',
  8098. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8099. 'data-validity-message' => 'This field cannot be left empty',
  8100. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8101. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8102. ],
  8103. '/div',
  8104. ];
  8105. $this->assertHtml($expected, $result);
  8106. $this->article['schema']['title']['length'] = 45;
  8107. $validator = new Validator();
  8108. $validator->maxLength('title', 55);
  8109. $article = new EntityContext(
  8110. [
  8111. 'entity' => new Entity($this->article),
  8112. 'table' => new Table([
  8113. 'schema' => $this->article['schema'],
  8114. 'validator' => $validator,
  8115. 'alias' => 'Articles',
  8116. ]),
  8117. ]
  8118. );
  8119. $this->Form->create($article);
  8120. $result = $this->Form->control('title', ['maxlength' => 10]);
  8121. $expected = [
  8122. 'div' => ['class'],
  8123. 'label' => ['for'],
  8124. 'Title',
  8125. '/label',
  8126. 'input' => [
  8127. 'aria-required' => 'true',
  8128. 'id',
  8129. 'name' => 'title',
  8130. 'type' => 'text',
  8131. 'required' => 'required',
  8132. 'maxlength' => 10, // Length set in options should take highest precedence.
  8133. 'data-validity-message' => 'This field cannot be left empty',
  8134. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8135. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8136. ],
  8137. '/div',
  8138. ];
  8139. $this->assertHtml($expected, $result);
  8140. }
  8141. /**
  8142. * testControlMinMaxLengthEntityContext method
  8143. *
  8144. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8145. */
  8146. public function testControlMinMaxLengthEntityContext(): void
  8147. {
  8148. $validator = new Validator();
  8149. $validator->maxLength('title', 10);
  8150. $article = new EntityContext(
  8151. [
  8152. 'entity' => new Entity($this->article),
  8153. 'table' => new Table([
  8154. 'alias' => 'Articles',
  8155. 'schema' => $this->article['schema'],
  8156. 'validator' => $validator,
  8157. ]),
  8158. ]
  8159. );
  8160. $this->Form->create($article);
  8161. $result = $this->Form->control('title');
  8162. $expected = [
  8163. 'div' => ['class'],
  8164. 'label' => ['for'],
  8165. 'Title',
  8166. '/label',
  8167. 'input' => [
  8168. 'aria-required' => 'true',
  8169. 'id',
  8170. 'name' => 'title',
  8171. 'type' => 'text',
  8172. 'required' => 'required',
  8173. 'maxlength' => 10,
  8174. 'data-validity-message' => 'This field cannot be left empty',
  8175. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8176. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8177. ],
  8178. '/div',
  8179. ];
  8180. $this->assertHtml($expected, $result);
  8181. }
  8182. /**
  8183. * testControlMaxLengthFormContext method
  8184. *
  8185. * Test control() with maxlength attribute in Form Context.
  8186. */
  8187. public function testControlMaxLengthFormContext(): void
  8188. {
  8189. $validator = new Validator();
  8190. $validator->maxLength('title', 10);
  8191. $form = new Form();
  8192. $form->setValidator('default', $validator);
  8193. $this->Form->create($form);
  8194. $result = $this->Form->control('title');
  8195. $expected = [
  8196. 'div' => ['class'],
  8197. 'label' => ['for'],
  8198. 'Title',
  8199. '/label',
  8200. 'input' => [
  8201. 'aria-required' => 'true',
  8202. 'id',
  8203. 'name' => 'title',
  8204. 'type' => 'text',
  8205. 'required' => 'required',
  8206. 'maxlength' => 10,
  8207. 'data-validity-message' => 'This field cannot be left empty',
  8208. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8209. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8210. ],
  8211. '/div',
  8212. ];
  8213. $this->assertHtml($expected, $result);
  8214. }
  8215. }