FormHelperTest.php 305 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894
  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 RuntimeException;
  38. use TestApp\Model\Entity\Article;
  39. use TestApp\Model\Table\ContactsTable;
  40. use TestApp\Model\Table\ValidateUsersTable;
  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->Controller, $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 function contextSelectionProvider(): array
  293. {
  294. $entity = new Article();
  295. $collection = new Collection([$entity]);
  296. $emptyCollection = new Collection([]);
  297. $arrayObject = new ArrayObject([]);
  298. $data = [
  299. 'schema' => [
  300. 'title' => ['type' => 'string'],
  301. ],
  302. ];
  303. $form = new Form();
  304. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  305. return [
  306. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  307. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  308. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  309. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  310. 'form' => [$form, 'Cake\View\Form\FormContext'],
  311. 'none' => [null, 'Cake\View\Form\NullContext'],
  312. 'custom' => [$custom, get_class($custom)],
  313. ];
  314. }
  315. /**
  316. * Test default context selection in create()
  317. *
  318. * @dataProvider contextSelectionProvider
  319. * @param mixed $data
  320. */
  321. public function testCreateContextSelectionBuiltIn($data, string $class): void
  322. {
  323. $this->Form->create($data);
  324. $this->assertInstanceOf($class, $this->Form->context());
  325. }
  326. /**
  327. * Data provider for type option.
  328. *
  329. * @return array
  330. */
  331. public static function requestTypeProvider(): array
  332. {
  333. return [
  334. // type, method, override
  335. ['post', 'post', 'POST'],
  336. ['put', 'post', 'PUT'],
  337. ['patch', 'post', 'PATCH'],
  338. ['delete', 'post', 'DELETE'],
  339. ];
  340. }
  341. /**
  342. * Test creating file forms.
  343. */
  344. public function testCreateFile(): void
  345. {
  346. $encoding = strtolower(Configure::read('App.encoding'));
  347. $result = $this->Form->create(null, ['type' => 'file']);
  348. $expected = [
  349. 'form' => [
  350. 'method' => 'post', 'action' => '/articles/add',
  351. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  352. ],
  353. 'div' => ['style' => 'display:none;'],
  354. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  355. '/div',
  356. ];
  357. $this->assertHtml($expected, $result);
  358. }
  359. /**
  360. * Test creating GET forms.
  361. */
  362. public function testCreateGet(): void
  363. {
  364. $encoding = strtolower(Configure::read('App.encoding'));
  365. $result = $this->Form->create(null, ['type' => 'get']);
  366. $expected = ['form' => [
  367. 'method' => 'get', 'action' => '/articles/add',
  368. 'accept-charset' => $encoding,
  369. ]];
  370. $this->assertHtml($expected, $result);
  371. }
  372. /**
  373. * Test explicit method/enctype options.
  374. *
  375. * Explicit method overwrites inferred method from 'type'
  376. */
  377. public function testCreateExplicitMethodEnctype(): void
  378. {
  379. $encoding = strtolower(Configure::read('App.encoding'));
  380. $result = $this->Form->create(null, [
  381. 'type' => 'get',
  382. 'method' => 'put',
  383. 'enctype' => 'multipart/form-data',
  384. ]);
  385. $expected = ['form' => [
  386. 'method' => 'put',
  387. 'action' => '/articles/add',
  388. 'enctype' => 'multipart/form-data',
  389. 'accept-charset' => $encoding,
  390. ]];
  391. $this->assertHtml($expected, $result);
  392. }
  393. /**
  394. * Test create() with the templates option.
  395. */
  396. public function testCreateTemplatesArray(): void
  397. {
  398. $result = $this->Form->create($this->article, [
  399. 'templates' => [
  400. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  401. ],
  402. ]);
  403. $expected = [
  404. 'form' => [
  405. 'class' => 'form-horizontal',
  406. 'method' => 'post',
  407. 'action' => '/articles/add',
  408. 'accept-charset' => 'utf-8',
  409. ],
  410. ];
  411. $this->assertHtml($expected, $result);
  412. }
  413. /**
  414. * Test create() with the templates option.
  415. */
  416. public function testCreateTemplatesFile(): void
  417. {
  418. $result = $this->Form->create($this->article, [
  419. 'templates' => 'htmlhelper_tags',
  420. ]);
  421. $expected = [
  422. 'start form',
  423. ];
  424. $this->assertHtml($expected, $result);
  425. }
  426. /**
  427. * Test that create() and end() restore templates.
  428. */
  429. public function testCreateEndRestoreTemplates(): void
  430. {
  431. $this->Form->create($this->article, [
  432. 'templates' => ['input' => 'custom input element'],
  433. ]);
  434. $this->Form->end();
  435. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  436. }
  437. /**
  438. * Test using template vars in various templates used by control() method.
  439. */
  440. public function testControlTemplateVars(): void
  441. {
  442. $result = $this->Form->control('text', [
  443. 'templates' => [
  444. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  445. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  446. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  447. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  448. ],
  449. 'templateVars' => [
  450. 'forinput' => 'in-input',
  451. 'forlabel' => 'in-label',
  452. 'forgroup' => 'in-group',
  453. 'forcontainer' => 'in-container',
  454. ],
  455. ]);
  456. $expected = [
  457. 'div' => ['class'],
  458. 'label' => ['for'],
  459. 'Text in-label',
  460. '/label',
  461. 'in-group',
  462. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  463. 'in-container',
  464. '/div',
  465. ];
  466. $this->assertHtml($expected, $result);
  467. }
  468. /**
  469. * Test ensuring template variables work in template files loaded
  470. * during control().
  471. */
  472. public function testControlTemplatesFromFile(): void
  473. {
  474. $result = $this->Form->control('title', [
  475. 'templates' => 'test_templates',
  476. 'templateVars' => [
  477. 'forcontainer' => 'container-data',
  478. ],
  479. ]);
  480. $expected = [
  481. 'div' => ['class'],
  482. 'label' => ['for'],
  483. 'Title',
  484. '/label',
  485. 'input' => ['name', 'type' => 'text', 'id'],
  486. 'container-data',
  487. '/div',
  488. ];
  489. $this->assertHtml($expected, $result);
  490. }
  491. /**
  492. * Test using template vars in inputSubmit and submitContainer template.
  493. */
  494. public function testSubmitTemplateVars(): void
  495. {
  496. $this->Form->setTemplates([
  497. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  498. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  499. ]);
  500. $result = $this->Form->submit('Submit', [
  501. 'templateVars' => [
  502. 'forinput' => 'in-input',
  503. 'forcontainer' => 'in-container',
  504. ],
  505. ]);
  506. $expected = [
  507. 'div' => ['class'],
  508. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  509. 'in-container',
  510. '/div',
  511. ];
  512. $this->assertHtml($expected, $result);
  513. }
  514. /**
  515. * test the create() method
  516. *
  517. * @dataProvider requestTypeProvider
  518. */
  519. public function testCreateTypeOptions(string $type, string $method, string $override): void
  520. {
  521. $encoding = strtolower(Configure::read('App.encoding'));
  522. $result = $this->Form->create(null, ['type' => $type]);
  523. $expected = [
  524. 'form' => [
  525. 'method' => $method, 'action' => '/articles/add',
  526. 'accept-charset' => $encoding,
  527. ],
  528. ];
  529. $extra = [
  530. 'div' => ['style' => 'display:none;'],
  531. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  532. '/div',
  533. ];
  534. if ($type !== 'post') {
  535. $expected = array_merge($expected, $extra);
  536. }
  537. $this->assertHtml($expected, $result);
  538. }
  539. /**
  540. * Test using template vars in Create (formStart template)
  541. */
  542. public function testCreateTemplateVars(): void
  543. {
  544. $result = $this->Form->create($this->article, [
  545. 'templates' => [
  546. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  547. ],
  548. 'templateVars' => ['header' => 'headertext'],
  549. ]);
  550. $expected = [
  551. 'h4' => ['class'],
  552. 'headertext',
  553. '/h4',
  554. 'form' => [
  555. 'method' => 'post',
  556. 'action' => '/articles/add',
  557. 'accept-charset' => 'utf-8',
  558. ],
  559. ];
  560. $this->assertHtml($expected, $result);
  561. }
  562. /**
  563. * Test opening a form for an update operation.
  564. */
  565. public function testCreateUpdateForm(): void
  566. {
  567. $encoding = strtolower(Configure::read('App.encoding'));
  568. $this->View->setRequest($this->View->getRequest()
  569. ->withRequestTarget('/articles/edit/1')
  570. ->withParam('action', 'edit'));
  571. $this->article['defaults']['id'] = 1;
  572. $result = $this->Form->create($this->article);
  573. $expected = [
  574. 'form' => [
  575. 'method' => 'post',
  576. 'action' => '/articles/edit/1',
  577. 'accept-charset' => $encoding,
  578. ],
  579. 'div' => ['style' => 'display:none;'],
  580. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  581. '/div',
  582. ];
  583. $this->assertHtml($expected, $result);
  584. }
  585. /**
  586. * test create() with automatic url generation
  587. */
  588. public function testCreateAutoUrl(): void
  589. {
  590. $encoding = strtolower(Configure::read('App.encoding'));
  591. $this->View->setRequest($this->View->getRequest()
  592. ->withRequestTarget('/articles/delete/10')
  593. ->withParam('action', 'delete'));
  594. $result = $this->Form->create($this->article);
  595. $expected = [
  596. 'form' => [
  597. 'method' => 'post', 'action' => '/articles/delete/10',
  598. 'accept-charset' => $encoding,
  599. ],
  600. ];
  601. $this->assertHtml($expected, $result);
  602. $this->article['defaults'] = ['id' => 1];
  603. $this->View->setRequest($this->View->getRequest()
  604. ->withRequestTarget('/Articles/edit/1')
  605. ->withParam('action', 'delete'));
  606. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  607. $expected = [
  608. 'form' => [
  609. 'method' => 'post',
  610. 'action' => '/Articles/edit/1',
  611. 'accept-charset' => $encoding,
  612. ],
  613. 'div' => ['style' => 'display:none;'],
  614. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  615. '/div',
  616. ];
  617. $this->assertHtml($expected, $result);
  618. $this->View->setRequest($this->View->getRequest()
  619. ->withParam('action', 'add'));
  620. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  621. $expected = [
  622. 'form' => [
  623. 'method' => 'post',
  624. 'action' => '/Articles/publish/1',
  625. 'accept-charset' => $encoding,
  626. ],
  627. 'div' => ['style' => 'display:none;'],
  628. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  629. '/div',
  630. ];
  631. $this->assertHtml($expected, $result);
  632. $result = $this->Form->create($this->article, ['url' => '/Articles/publish']);
  633. $expected = [
  634. 'form' => ['method' => 'post', 'action' => '/Articles/publish', 'accept-charset' => $encoding],
  635. 'div' => ['style' => 'display:none;'],
  636. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  637. '/div',
  638. ];
  639. $this->assertHtml($expected, $result);
  640. $this->View->setRequest($this->View->getRequest()
  641. ->withParam('controller', 'Pages'));
  642. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  643. $expected = [
  644. 'form' => [
  645. 'method' => 'post', 'action' => '/Pages/signup/1',
  646. 'accept-charset' => $encoding,
  647. ],
  648. 'div' => ['style' => 'display:none;'],
  649. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  650. '/div',
  651. ];
  652. $this->assertHtml($expected, $result);
  653. }
  654. /**
  655. * Test create() with no URL (no "action" attribute for <form> tag)
  656. */
  657. public function testCreateNoUrl(): void
  658. {
  659. $result = $this->Form->create(null, ['url' => false]);
  660. $expected = [
  661. 'form' => [
  662. 'method' => 'post',
  663. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  664. ],
  665. ];
  666. $this->assertHtml($expected, $result);
  667. }
  668. /**
  669. * test create() with a custom route
  670. */
  671. public function testCreateCustomRoute(): void
  672. {
  673. $builder = Router::createRouteBuilder('/');
  674. $builder->connect('/login', ['controller' => 'Users', 'action' => 'login']);
  675. $encoding = strtolower(Configure::read('App.encoding'));
  676. $this->View->setRequest($this->View->getRequest()
  677. ->withParam('controller', 'Users'));
  678. $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
  679. $expected = [
  680. 'form' => [
  681. 'method' => 'post', 'action' => '/login',
  682. 'accept-charset' => $encoding,
  683. ],
  684. ];
  685. $this->assertHtml($expected, $result);
  686. $builder->connect(
  687. '/new-article',
  688. ['controller' => 'Articles', 'action' => 'myAction'],
  689. ['_name' => 'my-route']
  690. );
  691. $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
  692. $expected = [
  693. 'form' => [
  694. 'method' => 'post', 'action' => '/new-article',
  695. 'accept-charset' => $encoding,
  696. ],
  697. ];
  698. $this->assertHtml($expected, $result);
  699. }
  700. /**
  701. * test automatic accept-charset overriding
  702. */
  703. public function testCreateWithAcceptCharset(): void
  704. {
  705. $result = $this->Form->create(
  706. $this->article,
  707. [
  708. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  709. ]
  710. );
  711. $expected = [
  712. 'form' => [
  713. 'method' => 'post', 'action' => '/Articles',
  714. 'accept-charset' => 'iso-8859-1',
  715. ],
  716. ];
  717. $this->assertHtml($expected, $result);
  718. }
  719. /**
  720. * Test base form URL when 'url' param is passed with multiple parameters (&)
  721. */
  722. public function testCreateQueryStringRequest(): void
  723. {
  724. $encoding = strtolower(Configure::read('App.encoding'));
  725. $result = $this->Form->create($this->article, [
  726. 'type' => 'post',
  727. 'escape' => false,
  728. 'url' => [
  729. 'controller' => 'Controller',
  730. 'action' => 'action',
  731. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  732. ],
  733. ]);
  734. $expected = [
  735. 'form' => [
  736. 'method' => 'post',
  737. 'action' => '/Controller/action?param1=value1&amp;param2=value2',
  738. 'accept-charset' => $encoding,
  739. ],
  740. ];
  741. $this->assertHtml($expected, $result);
  742. $result = $this->Form->create($this->article, [
  743. 'type' => 'post',
  744. 'url' => [
  745. 'controller' => 'Controller',
  746. 'action' => 'action',
  747. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  748. ],
  749. ]);
  750. $this->assertHtml($expected, $result);
  751. }
  752. /**
  753. * test that create() doesn't cause errors by multiple id's being in the primary key
  754. * as could happen with multiple select or checkboxes.
  755. */
  756. public function testCreateWithMultipleIdInData(): void
  757. {
  758. $encoding = strtolower(Configure::read('App.encoding'));
  759. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  760. $result = $this->Form->create($this->article);
  761. $expected = [
  762. 'form' => [
  763. 'method' => 'post',
  764. 'action' => '/articles/add',
  765. 'accept-charset' => $encoding,
  766. ],
  767. ];
  768. $this->assertHtml($expected, $result);
  769. }
  770. /**
  771. * test that create() doesn't add in extra passed params.
  772. */
  773. public function testCreatePassedArgs(): void
  774. {
  775. $encoding = strtolower(Configure::read('App.encoding'));
  776. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  777. $result = $this->Form->create($this->article, [
  778. 'type' => 'post',
  779. 'escape' => false,
  780. 'url' => [
  781. 'action' => 'edit',
  782. 'myparam',
  783. ],
  784. ]);
  785. $expected = [
  786. 'form' => [
  787. 'method' => 'post',
  788. 'action' => '/Articles/edit/myparam',
  789. 'accept-charset' => $encoding,
  790. ],
  791. ];
  792. $this->assertHtml($expected, $result);
  793. }
  794. /**
  795. * test creating a get form, and get form inputs.
  796. */
  797. public function testGetFormCreate(): void
  798. {
  799. $encoding = strtolower(Configure::read('App.encoding'));
  800. $result = $this->Form->create($this->article, ['type' => 'get']);
  801. $expected = ['form' => [
  802. 'method' => 'get', 'action' => '/articles/add',
  803. 'accept-charset' => $encoding,
  804. ]];
  805. $this->assertHtml($expected, $result);
  806. $result = $this->Form->text('title');
  807. $expected = ['input' => [
  808. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  809. ]];
  810. $this->assertHtml($expected, $result);
  811. $result = $this->Form->password('password');
  812. $expected = ['input' => [
  813. 'name' => 'password', 'type' => 'password',
  814. ]];
  815. $this->assertHtml($expected, $result);
  816. $this->assertDoesNotMatchRegularExpression('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  817. $result = $this->Form->text('user_form');
  818. $expected = ['input' => [
  819. 'name' => 'user_form', 'type' => 'text',
  820. ]];
  821. $this->assertHtml($expected, $result);
  822. }
  823. /**
  824. * test get form, and inputs when the model param is false
  825. */
  826. public function testGetFormWithFalseModel(): void
  827. {
  828. $encoding = strtolower(Configure::read('App.encoding'));
  829. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'ContactTest'));
  830. $result = $this->Form->create(null, [
  831. 'type' => 'get', 'url' => ['controller' => 'ContactTest'],
  832. ]);
  833. $expected = ['form' => [
  834. 'method' => 'get', 'action' => '/ContactTest/add',
  835. 'accept-charset' => $encoding,
  836. ]];
  837. $this->assertHtml($expected, $result);
  838. $result = $this->Form->text('reason');
  839. $expected = [
  840. 'input' => ['type' => 'text', 'name' => 'reason'],
  841. ];
  842. $this->assertHtml($expected, $result);
  843. }
  844. /**
  845. * testFormCreateWithSecurity method
  846. *
  847. * Test form->create() with security key.
  848. */
  849. public function testCreateWithSecurity(): void
  850. {
  851. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  852. $encoding = strtolower(Configure::read('App.encoding'));
  853. $result = $this->Form->create($this->article, [
  854. 'url' => '/articles/publish',
  855. ]);
  856. $expected = [
  857. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  858. 'div' => ['style' => 'display:none;'],
  859. ['input' => [
  860. 'type' => 'hidden',
  861. 'name' => '_csrfToken',
  862. 'value' => 'testKey',
  863. 'autocomplete' => 'off',
  864. ]],
  865. '/div',
  866. ];
  867. $this->assertHtml($expected, $result);
  868. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  869. $expected['form']['id'] = 'MyForm';
  870. $this->assertHtml($expected, $result);
  871. }
  872. /**
  873. * testFormCreateGetNoSecurity method
  874. *
  875. * Test form->create() with no security key as its a get form
  876. */
  877. public function testCreateEndGetNoSecurity(): void
  878. {
  879. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  880. $article = new Article();
  881. $result = $this->Form->create($article, [
  882. 'type' => 'get',
  883. 'url' => '/contacts/add',
  884. ]);
  885. $this->assertStringNotContainsString('testKey', $result);
  886. $result = $this->Form->end();
  887. $this->assertStringNotContainsString('testKey', $result);
  888. }
  889. /**
  890. * Tests form hash generation with model-less data
  891. */
  892. public function testValidateHashNoModel(): void
  893. {
  894. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  895. $fields = ['anything'];
  896. $this->Form->create();
  897. $result = $this->Form->secure($fields);
  898. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  899. $this->assertStringContainsString($hash, $result);
  900. }
  901. /**
  902. * Tests that hidden fields generated for checkboxes don't get locked
  903. */
  904. public function testNoCheckboxLocking(): void
  905. {
  906. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  907. $this->Form->create();
  908. $this->assertSame([], $this->Form->getFormProtector()->__debugInfo()['fields']);
  909. $this->Form->checkbox('check', ['value' => '1']);
  910. $this->assertSame(['check'], $this->Form->getFormProtector()->__debugInfo()['fields']);
  911. }
  912. /**
  913. * testFormSecurityFields method
  914. *
  915. * Test generation of secure form hash generation.
  916. */
  917. public function testFormSecurityFields(): void
  918. {
  919. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  920. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  921. $this->Form->create();
  922. $result = $this->Form->secure($fields);
  923. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  924. $hash .= ':' . 'Model.valid';
  925. $hash = urlencode($hash);
  926. $tokenDebug = urlencode(json_encode([
  927. $this->url,
  928. $fields,
  929. [],
  930. ]));
  931. $expected = [
  932. 'div' => ['style' => 'display:none;'],
  933. ['input' => [
  934. 'type' => 'hidden',
  935. 'name' => '_Token[fields]',
  936. 'value' => $hash,
  937. 'autocomplete' => 'off',
  938. ]],
  939. ['input' => [
  940. 'type' => 'hidden',
  941. 'name' => '_Token[unlocked]',
  942. 'value' => '',
  943. 'autocomplete' => 'off',
  944. ]],
  945. ['input' => [
  946. 'type' => 'hidden',
  947. 'name' => '_Token[debug]',
  948. 'value' => $tokenDebug,
  949. 'autocomplete' => 'off',
  950. ]],
  951. '/div',
  952. ];
  953. $this->assertHtml($expected, $result);
  954. }
  955. /**
  956. * testFormSecurityFields method
  957. *
  958. * Test debug token is not generated if debug is false
  959. */
  960. public function testFormSecurityFieldsNoDebugMode(): void
  961. {
  962. Configure::write('debug', false);
  963. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  964. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  965. $this->Form->create();
  966. $result = $this->Form->secure($fields);
  967. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  968. $hash .= ':' . 'Model.valid';
  969. $hash = urlencode($hash);
  970. $expected = [
  971. 'div' => ['style' => 'display:none;'],
  972. ['input' => [
  973. 'type' => 'hidden',
  974. 'name' => '_Token[fields]',
  975. 'autocomplete' => 'off',
  976. 'value' => $hash,
  977. ]],
  978. ['input' => [
  979. 'type' => 'hidden',
  980. 'name' => '_Token[unlocked]',
  981. 'autocomplete' => 'off',
  982. 'value' => '',
  983. ]],
  984. '/div',
  985. ];
  986. $this->assertHtml($expected, $result);
  987. }
  988. /**
  989. * Tests correct generation of number fields for smallint
  990. */
  991. public function testTextFieldGenerationForSmallint(): void
  992. {
  993. $this->article['schema'] = [
  994. 'foo' => [
  995. 'type' => 'smallinteger',
  996. 'null' => false,
  997. 'default' => null,
  998. 'length' => 10,
  999. ],
  1000. ];
  1001. $this->Form->create($this->article);
  1002. $result = $this->Form->control('foo');
  1003. $this->assertStringContainsString('class="input number"', $result);
  1004. $this->assertStringContainsString('type="number"', $result);
  1005. }
  1006. /**
  1007. * Tests correct generation of number fields for tinyint
  1008. */
  1009. public function testTextFieldGenerationForTinyint(): void
  1010. {
  1011. $this->article['schema'] = [
  1012. 'foo' => [
  1013. 'type' => 'tinyinteger',
  1014. 'null' => false,
  1015. 'default' => null,
  1016. 'length' => 10,
  1017. ],
  1018. ];
  1019. $this->Form->create($this->article);
  1020. $result = $this->Form->control('foo');
  1021. $this->assertStringContainsString('class="input number"', $result);
  1022. $this->assertStringContainsString('type="number"', $result);
  1023. }
  1024. /**
  1025. * Tests correct generation of number fields for double and float fields
  1026. */
  1027. public function testTextFieldGenerationForFloats(): void
  1028. {
  1029. $this->article['schema'] = [
  1030. 'foo' => [
  1031. 'type' => 'float',
  1032. 'null' => false,
  1033. 'default' => null,
  1034. 'length' => 10,
  1035. ],
  1036. ];
  1037. $this->Form->create($this->article);
  1038. $result = $this->Form->control('foo');
  1039. $expected = [
  1040. 'div' => ['class' => 'input number'],
  1041. 'label' => ['for' => 'foo'],
  1042. 'Foo',
  1043. '/label',
  1044. ['input' => [
  1045. 'type' => 'number',
  1046. 'name' => 'foo',
  1047. 'id' => 'foo',
  1048. 'step' => 'any',
  1049. ]],
  1050. '/div',
  1051. ];
  1052. $this->assertHtml($expected, $result);
  1053. $result = $this->Form->control('foo', ['step' => 0.5]);
  1054. $expected = [
  1055. 'div' => ['class' => 'input number'],
  1056. 'label' => ['for' => 'foo'],
  1057. 'Foo',
  1058. '/label',
  1059. ['input' => [
  1060. 'type' => 'number',
  1061. 'name' => 'foo',
  1062. 'id' => 'foo',
  1063. 'step' => '0.5',
  1064. ]],
  1065. '/div',
  1066. ];
  1067. $this->assertHtml($expected, $result);
  1068. }
  1069. /**
  1070. * Tests correct generation of number fields for integer fields
  1071. */
  1072. public function testTextFieldTypeNumberGenerationForIntegers(): void
  1073. {
  1074. $this->getTableLocator()->get('Contacts', [
  1075. 'className' => ContactsTable::class,
  1076. ]);
  1077. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1078. $result = $this->Form->control('age');
  1079. $expected = [
  1080. 'div' => ['class' => 'input number'],
  1081. 'label' => ['for' => 'age'],
  1082. 'Age',
  1083. '/label',
  1084. ['input' => [
  1085. 'type' => 'number', 'name' => 'age',
  1086. 'id' => 'age',
  1087. ]],
  1088. '/div',
  1089. ];
  1090. $this->assertHtml($expected, $result);
  1091. }
  1092. /**
  1093. * Tests correct generation of file upload fields for binary fields
  1094. */
  1095. public function testFileUploadFieldTypeGenerationForBinaries(): void
  1096. {
  1097. $table = $this->getTableLocator()->get('Contacts', [
  1098. 'className' => ContactsTable::class,
  1099. ]);
  1100. $table->setSchema(['foo' => [
  1101. 'type' => 'binary',
  1102. 'null' => false,
  1103. 'default' => null,
  1104. 'length' => 1024,
  1105. ]]);
  1106. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1107. $result = $this->Form->control('foo');
  1108. $expected = [
  1109. 'div' => ['class' => 'input file'],
  1110. 'label' => ['for' => 'foo'],
  1111. 'Foo',
  1112. '/label',
  1113. ['input' => [
  1114. 'type' => 'file', 'name' => 'foo',
  1115. 'id' => 'foo',
  1116. ]],
  1117. '/div',
  1118. ];
  1119. $this->assertHtml($expected, $result);
  1120. }
  1121. /**
  1122. * testFormSecurityMultipleFields method
  1123. *
  1124. * Test secure() with multiple row form. Ensure hash is correct.
  1125. */
  1126. public function testFormSecurityMultipleFields(): void
  1127. {
  1128. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1129. $this->Form->create();
  1130. $fields = [
  1131. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1132. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1133. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1134. ];
  1135. $result = $this->Form->secure($fields);
  1136. $sortedFields = [
  1137. 'Model.0.password',
  1138. 'Model.0.username',
  1139. 'Model.1.password',
  1140. 'Model.1.username',
  1141. 'Model.0.hidden' => 'value',
  1142. 'Model.0.valid' => '0',
  1143. 'Model.1.hidden' => 'value',
  1144. 'Model.1.valid' => '0',
  1145. ];
  1146. $hash = hash_hmac('sha1', $this->url . serialize($sortedFields) . session_id(), Security::getSalt());
  1147. $hash .= ':Model.0.hidden|Model.0.valid|Model.1.hidden|Model.1.valid';
  1148. $hash = urlencode($hash);
  1149. $tokenDebug = urlencode(json_encode([
  1150. $this->url,
  1151. $fields,
  1152. [],
  1153. ]));
  1154. $expected = [
  1155. 'div' => ['style' => 'display:none;'],
  1156. ['input' => [
  1157. 'type' => 'hidden',
  1158. 'name' => '_Token[fields]',
  1159. 'value' => $hash,
  1160. 'autocomplete' => 'off',
  1161. ]],
  1162. ['input' => [
  1163. 'type' => 'hidden',
  1164. 'name' => '_Token[unlocked]',
  1165. 'autocomplete' => 'off',
  1166. 'value' => '',
  1167. ]],
  1168. ['input' => [
  1169. 'type' => 'hidden',
  1170. 'name' => '_Token[debug]',
  1171. 'value' => $tokenDebug,
  1172. 'autocomplete' => 'off',
  1173. ]],
  1174. '/div',
  1175. ];
  1176. $this->assertHtml($expected, $result);
  1177. }
  1178. /**
  1179. * testFormSecurityMultipleSubmitButtons
  1180. *
  1181. * test form submit generation and ensure that _Token is only created on end()
  1182. */
  1183. public function testFormSecurityMultipleSubmitButtons(): void
  1184. {
  1185. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1186. $this->Form->create($this->article);
  1187. $this->Form->text('Address.title');
  1188. $this->Form->text('Address.first_name');
  1189. $result = $this->Form->submit('Save', ['name' => 'save']);
  1190. $expected = [
  1191. 'div' => ['class' => 'submit'],
  1192. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1193. '/div',
  1194. ];
  1195. $this->assertHtml($expected, $result);
  1196. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1197. $expected = [
  1198. 'div' => ['class' => 'submit'],
  1199. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1200. '/div',
  1201. ];
  1202. $this->assertHtml($expected, $result);
  1203. $result = $this->Form->end();
  1204. $tokenDebug = urlencode(json_encode([
  1205. '/articles/add',
  1206. [
  1207. 'Address.title',
  1208. 'Address.first_name',
  1209. ],
  1210. ['save', 'cancel'],
  1211. ]));
  1212. $expected = [
  1213. 'div' => ['style' => 'display:none;'],
  1214. ['input' => [
  1215. 'type' => 'hidden',
  1216. 'name' => '_Token[fields]',
  1217. 'autocomplete',
  1218. 'value',
  1219. ]],
  1220. ['input' => [
  1221. 'type' => 'hidden',
  1222. 'name' => '_Token[unlocked]',
  1223. 'value' => 'cancel%7Csave',
  1224. 'autocomplete' => 'off',
  1225. ]],
  1226. ['input' => [
  1227. 'type' => 'hidden',
  1228. 'name' => '_Token[debug]',
  1229. 'value' => $tokenDebug,
  1230. 'autocomplete' => 'off',
  1231. ]],
  1232. '/div',
  1233. ];
  1234. $this->assertHtml($expected, $result);
  1235. }
  1236. /**
  1237. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1238. */
  1239. public function testSecurityButtonNestedNamed(): void
  1240. {
  1241. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1242. $this->Form->create();
  1243. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1244. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1245. $this->assertEquals(['Address.button'], $result);
  1246. }
  1247. /**
  1248. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1249. */
  1250. public function testSecuritySubmitNestedNamed(): void
  1251. {
  1252. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1253. $this->Form->create($this->article);
  1254. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1255. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1256. $this->assertEquals(['Address.button'], $result);
  1257. }
  1258. /**
  1259. * Test that the correct fields are unlocked for image submits with no names.
  1260. */
  1261. public function testSecuritySubmitImageNoName(): void
  1262. {
  1263. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1264. $this->Form->create();
  1265. $result = $this->Form->submit('save.png');
  1266. $expected = [
  1267. 'div' => ['class' => 'submit'],
  1268. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1269. '/div',
  1270. ];
  1271. $this->assertHtml($expected, $result);
  1272. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1273. $this->assertEquals(['x', 'y'], $result);
  1274. }
  1275. /**
  1276. * Test that the correct fields are unlocked for image submits with names.
  1277. */
  1278. public function testSecuritySubmitImageName(): void
  1279. {
  1280. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1281. $this->Form->create();
  1282. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1283. $expected = [
  1284. 'div' => ['class' => 'submit'],
  1285. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1286. '/div',
  1287. ];
  1288. $this->assertHtml($expected, $result);
  1289. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1290. $this->assertEquals(['test', 'test_x', 'test_y'], $result);
  1291. }
  1292. /**
  1293. * testFormSecurityMultipleControlFields method
  1294. *
  1295. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1296. */
  1297. public function testFormSecurityMultipleControlFields(): void
  1298. {
  1299. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1300. $this->Form->create();
  1301. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1302. $this->Form->control('Addresses.0.title');
  1303. $this->Form->control('Addresses.0.first_name');
  1304. $this->Form->control('Addresses.0.last_name');
  1305. $this->Form->control('Addresses.0.address');
  1306. $this->Form->control('Addresses.0.city');
  1307. $this->Form->control('Addresses.0.phone');
  1308. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1309. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1310. $this->Form->control('Addresses.1.title');
  1311. $this->Form->control('Addresses.1.first_name');
  1312. $this->Form->control('Addresses.1.last_name');
  1313. $this->Form->control('Addresses.1.address');
  1314. $this->Form->control('Addresses.1.city');
  1315. $this->Form->control('Addresses.1.phone');
  1316. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1317. $result = $this->Form->secure();
  1318. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1319. $tokenDebug = urlencode(json_encode([
  1320. '/articles/add',
  1321. [
  1322. 'Addresses.0.id' => '123456',
  1323. 'Addresses.0.title',
  1324. 'Addresses.0.first_name',
  1325. 'Addresses.0.last_name',
  1326. 'Addresses.0.address',
  1327. 'Addresses.0.city',
  1328. 'Addresses.0.phone',
  1329. 'Addresses.0.primary',
  1330. 'Addresses.1.id' => '654321',
  1331. 'Addresses.1.title',
  1332. 'Addresses.1.first_name',
  1333. 'Addresses.1.last_name',
  1334. 'Addresses.1.address',
  1335. 'Addresses.1.city',
  1336. 'Addresses.1.phone',
  1337. 'Addresses.1.primary',
  1338. ],
  1339. [],
  1340. ]));
  1341. $expected = [
  1342. 'div' => ['style' => 'display:none;'],
  1343. ['input' => [
  1344. 'type' => 'hidden',
  1345. 'name' => '_Token[fields]',
  1346. 'value' => $hash,
  1347. 'autocomplete' => 'off',
  1348. ]],
  1349. ['input' => [
  1350. 'type' => 'hidden',
  1351. 'name' => '_Token[unlocked]',
  1352. 'autocomplete' => 'off',
  1353. 'value' => '',
  1354. ]],
  1355. ['input' => [
  1356. 'type' => 'hidden',
  1357. 'name' => '_Token[debug]',
  1358. 'value' => $tokenDebug,
  1359. 'autocomplete' => 'off',
  1360. ]],
  1361. '/div',
  1362. ];
  1363. $this->assertHtml($expected, $result);
  1364. }
  1365. /**
  1366. * testFormSecurityArrayFields method
  1367. *
  1368. * Test form security with Model.field.0 style inputs.
  1369. */
  1370. public function testFormSecurityArrayFields(): void
  1371. {
  1372. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1373. $this->Form->create();
  1374. $this->Form->text('Address.primary.1');
  1375. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1376. $this->assertSame('Address.primary', $result[0]);
  1377. $this->Form->text('Address.secondary.1.0');
  1378. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1379. $this->assertSame('Address.secondary', $result[1]);
  1380. }
  1381. /**
  1382. * testFormSecurityMultipleControlDisabledFields method
  1383. *
  1384. * Test secure form generation with multiple records and disabled fields.
  1385. */
  1386. public function testFormSecurityMultipleControlDisabledFields(): void
  1387. {
  1388. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1389. 'unlockedFields' => ['first_name', 'address'],
  1390. ]));
  1391. $this->Form->create();
  1392. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1393. $this->Form->text('Addresses.0.title');
  1394. $this->Form->text('Addresses.0.first_name');
  1395. $this->Form->text('Addresses.0.last_name');
  1396. $this->Form->text('Addresses.0.address');
  1397. $this->Form->text('Addresses.0.city');
  1398. $this->Form->text('Addresses.0.phone');
  1399. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1400. $this->Form->text('Addresses.1.title');
  1401. $this->Form->text('Addresses.1.first_name');
  1402. $this->Form->text('Addresses.1.last_name');
  1403. $this->Form->text('Addresses.1.address');
  1404. $this->Form->text('Addresses.1.city');
  1405. $this->Form->text('Addresses.1.phone');
  1406. $result = $this->Form->secure();
  1407. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1408. $tokenDebug = urlencode(json_encode([
  1409. '/articles/add',
  1410. [
  1411. 'Addresses.0.id' => '123456',
  1412. 'Addresses.0.title',
  1413. 'Addresses.0.last_name',
  1414. 'Addresses.0.city',
  1415. 'Addresses.0.phone',
  1416. 'Addresses.1.id' => '654321',
  1417. 'Addresses.1.title',
  1418. 'Addresses.1.last_name',
  1419. 'Addresses.1.city',
  1420. 'Addresses.1.phone',
  1421. ],
  1422. [
  1423. 'first_name',
  1424. 'address',
  1425. ],
  1426. ]));
  1427. $expected = [
  1428. 'div' => ['style' => 'display:none;'],
  1429. ['input' => [
  1430. 'type' => 'hidden',
  1431. 'name' => '_Token[fields]',
  1432. 'autocomplete' => 'off',
  1433. 'value' => $hash,
  1434. ]],
  1435. ['input' => [
  1436. 'type' => 'hidden',
  1437. 'name' => '_Token[unlocked]',
  1438. 'autocomplete' => 'off',
  1439. 'value' => 'address%7Cfirst_name',
  1440. ]],
  1441. ['input' => [
  1442. 'type' => 'hidden',
  1443. 'name' => '_Token[debug]',
  1444. 'autocomplete' => 'off',
  1445. 'value' => $tokenDebug,
  1446. ]],
  1447. '/div',
  1448. ];
  1449. $this->assertHtml($expected, $result);
  1450. }
  1451. /**
  1452. * testFormSecurityControlDisabledFields method
  1453. *
  1454. * Test single record form with disabled fields.
  1455. */
  1456. public function testFormSecurityControlUnlockedFields(): void
  1457. {
  1458. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1459. 'unlockedFields' => ['first_name', 'address'],
  1460. ]));
  1461. $this->Form->create();
  1462. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1463. $this->assertEquals(
  1464. $this->View->getRequest()->getAttribute('formTokenData'),
  1465. ['unlockedFields' => $result]
  1466. );
  1467. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1468. $this->Form->text('Addresses.title');
  1469. $this->Form->text('Addresses.first_name');
  1470. $this->Form->text('Addresses.last_name');
  1471. $this->Form->text('Addresses.address');
  1472. $this->Form->text('Addresses.city');
  1473. $this->Form->text('Addresses.phone');
  1474. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1475. $expected = [
  1476. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1477. 'Addresses.city', 'Addresses.phone',
  1478. ];
  1479. $this->assertEquals($expected, $result);
  1480. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1481. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1482. $tokenDebug = urlencode(json_encode([
  1483. '/articles/add',
  1484. [
  1485. 'Addresses.id' => '123456',
  1486. 'Addresses.title',
  1487. 'Addresses.last_name',
  1488. 'Addresses.city',
  1489. 'Addresses.phone',
  1490. ],
  1491. [
  1492. 'first_name',
  1493. 'address',
  1494. ],
  1495. ]));
  1496. $expected = [
  1497. 'div' => ['style' => 'display:none;'],
  1498. ['input' => [
  1499. 'type' => 'hidden',
  1500. 'name' => '_Token[fields]',
  1501. 'value' => $hash,
  1502. 'autocomplete' => 'off',
  1503. 'data-foo' => 'bar',
  1504. ]],
  1505. ['input' => [
  1506. 'type' => 'hidden',
  1507. 'name' => '_Token[unlocked]',
  1508. 'value' => 'address%7Cfirst_name',
  1509. 'autocomplete' => 'off',
  1510. 'data-foo' => 'bar',
  1511. ]],
  1512. ['input' => [
  1513. 'type' => 'hidden', 'name' => '_Token[debug]',
  1514. 'value' => $tokenDebug,
  1515. 'autocomplete' => 'off',
  1516. 'data-foo' => 'bar',
  1517. ]],
  1518. '/div',
  1519. ];
  1520. $this->assertHtml($expected, $result);
  1521. }
  1522. /**
  1523. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1524. *
  1525. * Test single record form with debugSecurity param.
  1526. */
  1527. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue(): void
  1528. {
  1529. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1530. 'unlockedFields' => ['first_name', 'address'],
  1531. ]));
  1532. $this->Form->create();
  1533. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1534. $this->assertEquals(
  1535. $this->View->getRequest()->getAttribute('formTokenData'),
  1536. ['unlockedFields' => $result]
  1537. );
  1538. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1539. $this->Form->text('Addresses.title');
  1540. $this->Form->text('Addresses.first_name');
  1541. $this->Form->text('Addresses.last_name');
  1542. $this->Form->text('Addresses.address');
  1543. $this->Form->text('Addresses.city');
  1544. $this->Form->text('Addresses.phone');
  1545. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1546. $expected = [
  1547. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1548. 'Addresses.city', 'Addresses.phone',
  1549. ];
  1550. $this->assertEquals($expected, $result);
  1551. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1552. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1553. $tokenDebug = urlencode(json_encode([
  1554. '/articles/add',
  1555. [
  1556. 'Addresses.id' => '123456',
  1557. 'Addresses.title',
  1558. 'Addresses.last_name',
  1559. 'Addresses.city',
  1560. 'Addresses.phone',
  1561. ],
  1562. [
  1563. 'first_name',
  1564. 'address',
  1565. ],
  1566. ]));
  1567. $expected = [
  1568. 'div' => ['style' => 'display:none;'],
  1569. ['input' => [
  1570. 'type' => 'hidden',
  1571. 'name' => '_Token[fields]',
  1572. 'value' => $hash,
  1573. 'autocomplete' => 'off',
  1574. 'data-foo' => 'bar',
  1575. ]],
  1576. ['input' => [
  1577. 'type' => 'hidden',
  1578. 'name' => '_Token[unlocked]',
  1579. 'value' => 'address%7Cfirst_name',
  1580. 'autocomplete' => 'off',
  1581. 'data-foo' => 'bar',
  1582. ]],
  1583. ['input' => [
  1584. 'type' => 'hidden', 'name' => '_Token[debug]',
  1585. 'value' => $tokenDebug,
  1586. 'autocomplete' => 'off',
  1587. 'data-foo' => 'bar',
  1588. ]],
  1589. '/div',
  1590. ];
  1591. $this->assertHtml($expected, $result);
  1592. }
  1593. /**
  1594. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1595. *
  1596. * Debug is false, debugSecurity is true -> no debug
  1597. */
  1598. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse(): void
  1599. {
  1600. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1601. 'unlockedFields' => ['first_name', 'address'],
  1602. ]));
  1603. $this->Form->create();
  1604. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1605. $this->assertEquals(
  1606. $this->View->getRequest()->getAttribute('formTokenData'),
  1607. ['unlockedFields' => $result]
  1608. );
  1609. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1610. $this->Form->text('Addresses.title');
  1611. $this->Form->text('Addresses.first_name');
  1612. $this->Form->text('Addresses.last_name');
  1613. $this->Form->text('Addresses.address');
  1614. $this->Form->text('Addresses.city');
  1615. $this->Form->text('Addresses.phone');
  1616. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1617. $expected = [
  1618. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1619. 'Addresses.city', 'Addresses.phone',
  1620. ];
  1621. $this->assertEquals($expected, $result);
  1622. Configure::write('debug', false);
  1623. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1624. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1625. $expected = [
  1626. 'div' => ['style' => 'display:none;'],
  1627. ['input' => [
  1628. 'type' => 'hidden',
  1629. 'name' => '_Token[fields]',
  1630. 'value' => $hash,
  1631. 'autocomplete' => 'off',
  1632. 'data-foo' => 'bar',
  1633. ]],
  1634. ['input' => [
  1635. 'type' => 'hidden',
  1636. 'name' => '_Token[unlocked]',
  1637. 'value' => 'address%7Cfirst_name',
  1638. 'autocomplete' => 'off',
  1639. 'data-foo' => 'bar',
  1640. ]],
  1641. '/div',
  1642. ];
  1643. $this->assertHtml($expected, $result);
  1644. }
  1645. /**
  1646. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1647. *
  1648. * Test single record form with debugSecurity param.
  1649. */
  1650. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse(): void
  1651. {
  1652. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1653. 'unlockedFields' => ['first_name', 'address'],
  1654. ]));
  1655. $this->Form->create();
  1656. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1657. $this->assertEquals(
  1658. $this->View->getRequest()->getAttribute('formTokenData'),
  1659. ['unlockedFields' => $result]
  1660. );
  1661. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1662. $this->Form->text('Addresses.title');
  1663. $this->Form->text('Addresses.first_name');
  1664. $this->Form->text('Addresses.last_name');
  1665. $this->Form->text('Addresses.address');
  1666. $this->Form->text('Addresses.city');
  1667. $this->Form->text('Addresses.phone');
  1668. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1669. $expected = [
  1670. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1671. 'Addresses.city', 'Addresses.phone',
  1672. ];
  1673. $this->assertEquals($expected, $result);
  1674. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1675. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1676. $expected = [
  1677. 'div' => ['style' => 'display:none;'],
  1678. ['input' => [
  1679. 'type' => 'hidden',
  1680. 'name' => '_Token[fields]',
  1681. 'value' => $hash,
  1682. 'autocomplete' => 'off',
  1683. 'data-foo' => 'bar',
  1684. ]],
  1685. ['input' => [
  1686. 'type' => 'hidden',
  1687. 'name' => '_Token[unlocked]',
  1688. 'value' => 'address%7Cfirst_name',
  1689. 'autocomplete' => 'off',
  1690. 'data-foo' => 'bar',
  1691. ]],
  1692. '/div',
  1693. ];
  1694. $this->assertHtml($expected, $result);
  1695. }
  1696. /**
  1697. * testFormSecureWithCustomNameAttribute method
  1698. *
  1699. * Test securing inputs with custom name attributes.
  1700. */
  1701. public function testFormSecureWithCustomNameAttribute(): void
  1702. {
  1703. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1704. $this->Form->create();
  1705. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1706. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1707. $this->assertSame('User.custom', $result[0]);
  1708. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1709. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1710. $this->assertSame('User.custom.another.value', $result[1]);
  1711. }
  1712. /**
  1713. * testFormSecuredControl method
  1714. *
  1715. * Test generation of entire secure form, assertions made on control() output.
  1716. */
  1717. public function testFormSecuredControl(): void
  1718. {
  1719. $this->View->setRequest($this->View->getRequest()
  1720. ->withAttribute('formTokenData', [])
  1721. ->withAttribute('csrfToken', 'testKey'));
  1722. $this->article['schema'] = [
  1723. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1724. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1725. ];
  1726. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1727. $encoding = strtolower(Configure::read('App.encoding'));
  1728. $expected = [
  1729. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1730. 'div' => ['style' => 'display:none;'],
  1731. ['input' => [
  1732. 'type' => 'hidden',
  1733. 'name' => '_csrfToken',
  1734. 'value' => 'testKey',
  1735. 'autocomplete' => 'off',
  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. 'autocomplete',
  1847. ]],
  1848. ['input' => [
  1849. 'type' => 'hidden',
  1850. 'name' => '_Token[unlocked]',
  1851. 'value' => '',
  1852. 'autocomplete' => 'off',
  1853. ]],
  1854. ['input' => [
  1855. 'type' => 'hidden', 'name' => '_Token[debug]',
  1856. 'value' => $tokenDebug,
  1857. 'autocomplete' => 'off',
  1858. ]],
  1859. '/div',
  1860. ];
  1861. $this->assertHtml($expected, $result);
  1862. $data = [
  1863. 'ratio' => '',
  1864. 'population' => '',
  1865. 'published' => '',
  1866. 'other' => '',
  1867. 'stuff' => '',
  1868. 'hidden' => '0',
  1869. 'something' => '',
  1870. '_Token' => $this->Form->getFormProtector()->buildTokenData(),
  1871. ];
  1872. $this->assertTrue($this->Form->getFormProtector()->validate($data, '', ''));
  1873. }
  1874. /**
  1875. * testSecuredControlCustomName method
  1876. *
  1877. * Test secured inputs with custom names.
  1878. */
  1879. public function testSecuredControlCustomName(): void
  1880. {
  1881. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1882. $this->Form->create();
  1883. $this->Form->text('text_input', [
  1884. 'name' => 'Option[General.default_role]',
  1885. ]);
  1886. $expected = ['Option.General.default_role'];
  1887. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1888. $this->assertEquals($expected, $result);
  1889. $this->Form->select('select_box', [1, 2], [
  1890. 'name' => 'Option[General.select_role]',
  1891. ]);
  1892. $expected[] = 'Option.General.select_role';
  1893. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1894. $this->assertEquals($expected, $result);
  1895. $this->Form->text('other.things[]');
  1896. $expected[] = 'other.things';
  1897. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1898. $this->assertEquals($expected, $result);
  1899. }
  1900. /**
  1901. * testSecuredControlDuplicate method
  1902. *
  1903. * Test that a hidden field followed by a visible field
  1904. * undoes the hidden field locking.
  1905. */
  1906. public function testSecuredControlDuplicate(): void
  1907. {
  1908. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1909. $this->Form->create();
  1910. $this->Form->control('text_val', [
  1911. 'type' => 'hidden',
  1912. 'value' => 'some text',
  1913. ]);
  1914. $expected = ['text_val' => 'some text'];
  1915. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1916. $this->assertEquals($expected, $result);
  1917. $this->Form->control('text_val', [
  1918. 'type' => 'text',
  1919. ]);
  1920. $expected = ['text_val'];
  1921. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1922. $this->assertEquals($expected, $result);
  1923. }
  1924. /**
  1925. * testFormSecuredFileControl method
  1926. *
  1927. * Tests that the correct keys are added to the field hash index.
  1928. */
  1929. public function testFormSecuredFileControl(): void
  1930. {
  1931. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1932. $this->Form->create();
  1933. $this->Form->file('Attachment.file');
  1934. $expected = ['Attachment.file'];
  1935. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1936. $this->assertEquals($expected, $result);
  1937. }
  1938. /**
  1939. * testFormSecuredMultipleSelect method
  1940. *
  1941. * Test that multiple selects keys are added to field hash.
  1942. */
  1943. public function testFormSecuredMultipleSelect(): void
  1944. {
  1945. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1946. $this->Form->create();
  1947. $options = ['1' => 'one', '2' => 'two'];
  1948. $this->Form->select('Model.select', $options);
  1949. $expected = ['Model.select'];
  1950. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1951. $this->assertEquals($expected, $result);
  1952. $this->Form->fields = [];
  1953. $this->Form->select('Model.select', $options, ['multiple' => true]);
  1954. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1955. $this->assertEquals($expected, $result);
  1956. }
  1957. /**
  1958. * testFormSecuredRadio method
  1959. */
  1960. public function testFormSecuredRadio(): void
  1961. {
  1962. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1963. $this->Form->create();
  1964. $options = ['1' => 'option1', '2' => 'option2'];
  1965. $this->Form->radio('Test.test', $options);
  1966. $expected = ['Test.test'];
  1967. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1968. $this->assertEquals($expected, $result);
  1969. $this->Form->radio('Test.all', $options, [
  1970. 'disabled' => ['option1', 'option2'],
  1971. ]);
  1972. $expected = ['Test.test', 'Test.all' => ''];
  1973. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1974. $this->assertEquals($expected, $result);
  1975. $this->Form->radio('Test.some', $options, [
  1976. 'disabled' => ['option1'],
  1977. ]);
  1978. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  1979. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1980. $this->assertEquals($expected, $result);
  1981. }
  1982. /**
  1983. * testFormSecuredAndDisabled method
  1984. *
  1985. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  1986. * hashing.
  1987. */
  1988. public function testFormSecuredAndDisabled(): void
  1989. {
  1990. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1991. $this->Form->create();
  1992. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  1993. $this->Form->text('Model.text', ['disabled' => true]);
  1994. $this->Form->password('Model.text', ['disabled' => true]);
  1995. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  1996. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  1997. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  1998. $this->Form->year('Model.year', ['disabled' => true]);
  1999. $this->Form->month('Model.month', ['disabled' => true]);
  2000. $this->Form->day('Model.day', ['disabled' => true]);
  2001. $this->Form->hour('Model.hour', ['disabled' => true]);
  2002. $this->Form->minute('Model.minute', ['disabled' => true]);
  2003. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2004. $expected = [
  2005. 'Model.radio' => '',
  2006. ];
  2007. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2008. $this->assertEquals($expected, $result);
  2009. }
  2010. /**
  2011. * testUnlockFieldAddsToList method
  2012. *
  2013. * Test disableField.
  2014. */
  2015. public function testUnlockFieldAddsToList(): void
  2016. {
  2017. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2018. 'unlockedFields' => [],
  2019. ]));
  2020. $this->Form->create();
  2021. $this->Form->unlockField('Contact.name');
  2022. $this->Form->text('Contact.name');
  2023. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2024. $this->assertEquals([], $result);
  2025. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  2026. $this->assertEquals(['Contact.name'], $result);
  2027. }
  2028. /**
  2029. * testUnlockFieldRemovingFromFields method
  2030. *
  2031. * Test unlockField removing from fields array.
  2032. */
  2033. public function testUnlockFieldRemovingFromFields(): void
  2034. {
  2035. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2036. 'unlockedFields' => [],
  2037. ]));
  2038. $this->Form->create($this->article);
  2039. $this->Form->hidden('Article.id', ['value' => 1]);
  2040. $this->Form->text('Article.title');
  2041. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2042. $this->assertSame('1', $result['Article.id'], 'Hidden input should be secured.');
  2043. $this->assertContains('Article.title', $result, 'Field should be secured.');
  2044. $this->Form->unlockField('Article.title');
  2045. $this->Form->unlockField('Article.id');
  2046. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2047. $this->assertEquals([], $result);
  2048. }
  2049. /**
  2050. * testResetUnlockFields method
  2051. *
  2052. * Test reset unlockFields, when create new form.
  2053. */
  2054. public function testResetUnlockFields(): void
  2055. {
  2056. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2057. 'key' => 'testKey',
  2058. 'unlockedFields' => [],
  2059. ]));
  2060. $this->Form->create();
  2061. $this->Form->unlockField('Contact.id');
  2062. $this->Form->hidden('Contact.id', ['value' => 1]);
  2063. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2064. $this->assertEmpty($result, 'Field should be unlocked');
  2065. $this->Form->end();
  2066. $this->Form->create();
  2067. $this->Form->hidden('Contact.id', ['value' => 1]);
  2068. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2069. $this->assertSame('1', $result['Contact.id'], 'Hidden input should be secured.');
  2070. }
  2071. /**
  2072. * testSecuredFormUrlIgnoresHost method
  2073. *
  2074. * Test that only the path + query elements of a form's URL show up in their hash.
  2075. */
  2076. public function testSecuredFormUrlIgnoresHost(): void
  2077. {
  2078. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'testKey']));
  2079. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2080. $this->Form->create($this->article, [
  2081. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2082. ]);
  2083. $result = $this->Form->secure();
  2084. $this->assertStringContainsString($expected, $result);
  2085. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2086. $result = $this->Form->secure();
  2087. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2088. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2089. $result = $this->Form->secure();
  2090. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2091. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2092. $result = $this->Form->secure();
  2093. $this->assertStringNotContainsString($expected, $result, 'URL is different');
  2094. }
  2095. /**
  2096. * testSecuredFormUrlHasHtmlAndIdentifier method
  2097. *
  2098. * Test that URL, HTML and identifier show up in their hashes.
  2099. */
  2100. public function testSecuredFormUrlHasHtmlAndIdentifier(): void
  2101. {
  2102. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2103. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2104. $this->Form->create($this->article, [
  2105. 'url' => [
  2106. 'controller' => 'articles',
  2107. 'action' => 'view',
  2108. '?' => [
  2109. 'page' => 1,
  2110. 'limit' => 10,
  2111. 'html' => '<>"',
  2112. ],
  2113. '#' => 'result',
  2114. ],
  2115. ]);
  2116. $result = $this->Form->secure();
  2117. $this->assertStringContainsString($expected, $result);
  2118. $this->Form->create($this->article, [
  2119. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2120. ]);
  2121. $result = $this->Form->secure();
  2122. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2123. $this->Form->create($this->article, [
  2124. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2125. ]);
  2126. $result = $this->Form->secure();
  2127. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2128. }
  2129. /**
  2130. * testErrorMessageDisplay method
  2131. *
  2132. * Test error message display.
  2133. */
  2134. public function testErrorMessageDisplay(): void
  2135. {
  2136. $this->article['errors'] = [
  2137. 'Article' => [
  2138. 'title' => 'error message',
  2139. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2140. ],
  2141. ];
  2142. $this->Form->create($this->article);
  2143. $result = $this->Form->control('Article.title');
  2144. $expected = [
  2145. 'div' => ['class' => 'input text error'],
  2146. 'label' => ['for' => 'article-title'],
  2147. 'Title',
  2148. '/label',
  2149. 'input' => [
  2150. 'type' => 'text',
  2151. 'name' => 'Article[title]',
  2152. 'id' => 'article-title',
  2153. 'class' => 'form-error',
  2154. 'aria-invalid' => 'true',
  2155. 'aria-describedby' => 'article-title-error',
  2156. ],
  2157. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2158. 'error message',
  2159. '/div',
  2160. '/div',
  2161. ];
  2162. $this->assertHtml($expected, $result);
  2163. $result = $this->Form->control('Article.title', [
  2164. 'templates' => [
  2165. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2166. ],
  2167. ]);
  2168. $expected = [
  2169. 'div' => ['class' => 'input text error'],
  2170. 'label' => ['for' => 'article-title'],
  2171. 'Title',
  2172. '/label',
  2173. 'input' => [
  2174. 'type' => 'text',
  2175. 'name' => 'Article[title]',
  2176. 'id' => 'article-title',
  2177. 'class' => 'form-error',
  2178. // No aria-describedby because error template is custom
  2179. 'aria-invalid' => 'true',
  2180. ],
  2181. '/div',
  2182. ];
  2183. $this->assertHtml($expected, $result);
  2184. $result = $this->Form->control('Article.content');
  2185. $expected = [
  2186. 'div' => ['class' => 'input text error'],
  2187. 'label' => ['for' => 'article-content'],
  2188. 'Content',
  2189. '/label',
  2190. 'input' => [
  2191. 'type' => 'text',
  2192. 'name' => 'Article[content]',
  2193. 'id' => 'article-content',
  2194. 'class' => 'form-error',
  2195. 'aria-invalid' => 'true',
  2196. 'aria-describedby' => 'article-content-error',
  2197. ],
  2198. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2199. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2200. '/div',
  2201. '/div',
  2202. ];
  2203. $this->assertHtml($expected, $result);
  2204. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2205. $expected = [
  2206. 'div' => ['class' => 'input text error'],
  2207. 'label' => ['for' => 'article-content'],
  2208. 'Content',
  2209. '/label',
  2210. 'input' => [
  2211. 'type' => 'text',
  2212. 'name' => 'Article[content]',
  2213. 'id' => 'article-content',
  2214. 'class' => 'form-error',
  2215. 'aria-invalid' => 'true',
  2216. 'aria-describedby' => 'article-content-error',
  2217. ],
  2218. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2219. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2220. '/div',
  2221. '/div',
  2222. ];
  2223. $this->assertHtml($expected, $result);
  2224. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2225. $expected = [
  2226. 'div' => ['class' => 'input text error'],
  2227. 'label' => ['for' => 'article-content'],
  2228. 'Content',
  2229. '/label',
  2230. 'input' => [
  2231. 'type' => 'text',
  2232. 'name' => 'Article[content]',
  2233. 'id' => 'article-content',
  2234. 'class' => 'form-error',
  2235. 'aria-invalid' => 'true',
  2236. 'aria-describedby' => 'article-content-error',
  2237. ],
  2238. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2239. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2240. '/div',
  2241. '/div',
  2242. ];
  2243. $this->assertHtml($expected, $result);
  2244. }
  2245. /**
  2246. * testEmptyErrorValidation method
  2247. *
  2248. * Test validation errors, when validation message is an empty string.
  2249. */
  2250. public function testEmptyErrorValidation(): void
  2251. {
  2252. $this->article['errors'] = [
  2253. 'Article' => ['title' => ''],
  2254. ];
  2255. $this->Form->create($this->article);
  2256. $result = $this->Form->control('Article.title');
  2257. $expected = [
  2258. 'div' => ['class' => 'input text error'],
  2259. 'label' => ['for' => 'article-title'],
  2260. 'Title',
  2261. '/label',
  2262. 'input' => [
  2263. 'type' => 'text',
  2264. 'name' => 'Article[title]',
  2265. 'id' => 'article-title',
  2266. 'class' => 'form-error',
  2267. 'aria-invalid' => 'true',
  2268. 'aria-describedby' => 'article-title-error',
  2269. ],
  2270. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2271. [],
  2272. '/div',
  2273. '/div',
  2274. ];
  2275. $this->assertHtml($expected, $result);
  2276. }
  2277. /**
  2278. * testEmptyControlErrorValidation method
  2279. *
  2280. * Test validation errors, when calling control() overriding validation message by an empty string.
  2281. */
  2282. public function testEmptyControlErrorValidation(): void
  2283. {
  2284. $this->article['errors'] = [
  2285. 'Article' => ['title' => 'error message'],
  2286. ];
  2287. $this->Form->create($this->article);
  2288. $result = $this->Form->control('Article.title', ['error' => '']);
  2289. $expected = [
  2290. 'div' => ['class' => 'input text error'],
  2291. 'label' => ['for' => 'article-title'],
  2292. 'Title',
  2293. '/label',
  2294. 'input' => [
  2295. 'aria-invalid' => 'true',
  2296. 'aria-describedby' => 'article-title-error',
  2297. 'type' => 'text',
  2298. 'name' => 'Article[title]',
  2299. 'id' => 'article-title',
  2300. 'class' => 'form-error',
  2301. ],
  2302. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2303. [],
  2304. '/div',
  2305. '/div',
  2306. ];
  2307. $this->assertHtml($expected, $result);
  2308. }
  2309. /**
  2310. * testControlErrorMessage method
  2311. *
  2312. * Test validation errors, when calling control() overriding validation messages.
  2313. */
  2314. public function testControlErrorMessage(): void
  2315. {
  2316. $this->article['errors'] = [
  2317. 'title' => ['error message'],
  2318. ];
  2319. $this->Form->create($this->article);
  2320. $result = $this->Form->control('title', [
  2321. 'error' => 'Custom error!',
  2322. ]);
  2323. $expected = [
  2324. 'div' => ['class' => 'input text required error'],
  2325. 'label' => ['for' => 'title'],
  2326. 'Title',
  2327. '/label',
  2328. 'input' => [
  2329. 'type' => 'text',
  2330. 'name' => 'title',
  2331. 'id' => 'title',
  2332. 'class' => 'form-error',
  2333. 'required' => 'required',
  2334. 'data-validity-message' => 'This field cannot be left empty',
  2335. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2336. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2337. 'aria-required' => 'true',
  2338. 'aria-invalid' => 'true',
  2339. 'aria-describedby' => 'title-error',
  2340. ],
  2341. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2342. 'Custom error!',
  2343. '/div',
  2344. '/div',
  2345. ];
  2346. $this->assertHtml($expected, $result);
  2347. $result = $this->Form->control('title', [
  2348. 'error' => ['error message' => 'Custom error!'],
  2349. ]);
  2350. $expected = [
  2351. 'div' => ['class' => 'input text required error'],
  2352. 'label' => ['for' => 'title'],
  2353. 'Title',
  2354. '/label',
  2355. 'input' => [
  2356. 'type' => 'text',
  2357. 'name' => 'title',
  2358. 'id' => 'title',
  2359. 'aria-required' => 'true',
  2360. 'aria-invalid' => 'true',
  2361. 'aria-describedby' => 'title-error',
  2362. 'class' => 'form-error',
  2363. 'required' => 'required',
  2364. 'data-validity-message' => 'This field cannot be left empty',
  2365. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2366. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2367. ],
  2368. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2369. 'Custom error!',
  2370. '/div',
  2371. '/div',
  2372. ];
  2373. $this->assertHtml($expected, $result);
  2374. }
  2375. /**
  2376. * testFormValidationAssociated method
  2377. *
  2378. * Tests displaying errors for nested entities.
  2379. */
  2380. public function testFormValidationAssociated(): void
  2381. {
  2382. $nested = new Entity(['foo' => 'bar']);
  2383. $nested->setError('foo', ['not a valid bar']);
  2384. $entity = new Entity(['nested' => $nested]);
  2385. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2386. $result = $this->Form->error('nested.foo');
  2387. $this->assertSame('<div class="error-message" id="nested-foo-error">not a valid bar</div>', $result);
  2388. }
  2389. /**
  2390. * testFormValidationAssociatedSecondLevel method
  2391. *
  2392. * Test form error display with associated model.
  2393. */
  2394. public function testFormValidationAssociatedSecondLevel(): void
  2395. {
  2396. $inner = new Entity(['bar' => 'baz']);
  2397. $nested = new Entity(['foo' => $inner]);
  2398. $entity = new Entity(['nested' => $nested]);
  2399. $inner->setError('bar', ['not a valid one']);
  2400. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2401. $result = $this->Form->error('nested.foo.bar');
  2402. $this->assertSame('<div class="error-message" id="nested-foo-bar-error">not a valid one</div>', $result);
  2403. }
  2404. /**
  2405. * testFormValidationMultiRecord method
  2406. *
  2407. * Test form error display with multiple records.
  2408. */
  2409. public function testFormValidationMultiRecord(): void
  2410. {
  2411. $one = new Entity();
  2412. $two = new Entity();
  2413. $this->getTableLocator()->get('Contacts', [
  2414. 'className' => ContactsTable::class,
  2415. ]);
  2416. $one->set('email', '');
  2417. $one->setError('email', ['invalid email']);
  2418. $two->set('name', '');
  2419. $two->setError('name', ['This is wrong']);
  2420. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2421. $result = $this->Form->control('0.email');
  2422. $expected = [
  2423. 'div' => ['class' => 'input email error'],
  2424. 'label' => ['for' => '0-email'],
  2425. 'Email',
  2426. '/label',
  2427. 'input' => [
  2428. 'type' => 'email',
  2429. 'name' => '0[email]',
  2430. 'id' => '0-email',
  2431. 'class' => 'form-error',
  2432. 'maxlength' => 255,
  2433. 'value' => '',
  2434. 'aria-invalid' => 'true',
  2435. 'aria-describedby' => '0-email-error',
  2436. ],
  2437. ['div' => ['class' => 'error-message', 'id' => '0-email-error']],
  2438. 'invalid email',
  2439. '/div',
  2440. '/div',
  2441. ];
  2442. $this->assertHtml($expected, $result);
  2443. $result = $this->Form->control('1.name');
  2444. $expected = [
  2445. 'div' => ['class' => 'input text error'],
  2446. 'label' => ['for' => '1-name'],
  2447. 'Name',
  2448. '/label',
  2449. 'input' => [
  2450. 'type' => 'text',
  2451. 'name' => '1[name]',
  2452. 'id' => '1-name',
  2453. 'class' => 'form-error',
  2454. 'maxlength' => 255,
  2455. 'value' => '',
  2456. 'aria-invalid' => 'true',
  2457. 'aria-describedby' => '1-name-error',
  2458. ],
  2459. ['div' => ['class' => 'error-message', 'id' => '1-name-error']],
  2460. 'This is wrong',
  2461. '/div',
  2462. '/div',
  2463. ];
  2464. $this->assertHtml($expected, $result);
  2465. }
  2466. /**
  2467. * testControl method
  2468. *
  2469. * Test various incarnations of control().
  2470. */
  2471. public function testControl(): void
  2472. {
  2473. $this->getTableLocator()->get('ValidateUsers', [
  2474. 'className' => ValidateUsersTable::class,
  2475. ]);
  2476. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2477. $result = $this->Form->control('ValidateUsers.balance');
  2478. $expected = [
  2479. 'div' => ['class'],
  2480. 'label' => ['for'],
  2481. 'Balance',
  2482. '/label',
  2483. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2484. '/div',
  2485. ];
  2486. $this->assertHtml($expected, $result);
  2487. $result = $this->Form->control('ValidateUser.cost_decimal');
  2488. $expected = [
  2489. 'div' => ['class'],
  2490. 'label' => ['for'],
  2491. 'Cost Decimal',
  2492. '/label',
  2493. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2494. '/div',
  2495. ];
  2496. $this->assertHtml($expected, $result);
  2497. $result = $this->Form->control('ValidateUser.null_decimal');
  2498. $expected = [
  2499. 'div' => ['class'],
  2500. 'label' => ['for'],
  2501. 'Null Decimal',
  2502. '/label',
  2503. 'input' => ['name', 'type' => 'number', 'id'],
  2504. '/div',
  2505. ];
  2506. $this->assertHtml($expected, $result);
  2507. }
  2508. /**
  2509. * testControlCustomization method
  2510. *
  2511. * Tests the input method and passing custom options.
  2512. */
  2513. public function testControlCustomization(): void
  2514. {
  2515. $this->getTableLocator()->get('Contacts', [
  2516. 'className' => ContactsTable::class,
  2517. ]);
  2518. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2519. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2520. $expected = [
  2521. 'div' => ['class' => 'input email'],
  2522. 'label' => ['for' => 'custom'],
  2523. 'Email',
  2524. '/label',
  2525. ['input' => [
  2526. 'type' => 'email', 'name' => 'Contact[email]',
  2527. 'id' => 'custom', 'maxlength' => 255,
  2528. ]],
  2529. '/div',
  2530. ];
  2531. $this->assertHtml($expected, $result);
  2532. $result = $this->Form->control('Contact.email', [
  2533. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2534. ]);
  2535. $expected = [
  2536. '<div',
  2537. 'label' => ['for' => 'contact-email'],
  2538. 'Email',
  2539. '/label',
  2540. ['input' => [
  2541. 'type' => 'email', 'name' => 'Contact[email]',
  2542. 'id' => 'contact-email', 'maxlength' => 255,
  2543. ]],
  2544. '/div',
  2545. ];
  2546. $this->assertHtml($expected, $result);
  2547. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2548. $expected = [
  2549. 'div' => ['class' => 'input text'],
  2550. 'label' => ['for' => 'contact-email'],
  2551. 'Email',
  2552. '/label',
  2553. ['input' => [
  2554. 'type' => 'text', 'name' => 'Contact[email]',
  2555. 'id' => 'contact-email', 'maxlength' => '255',
  2556. ]],
  2557. '/div',
  2558. ];
  2559. $this->assertHtml($expected, $result);
  2560. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2561. $expected = [
  2562. 'div' => ['class' => 'input text'],
  2563. 'label' => ['for' => 'contact-5-email'],
  2564. 'Email',
  2565. '/label',
  2566. ['input' => [
  2567. 'type' => 'text', 'name' => 'Contact[5][email]',
  2568. 'id' => 'contact-5-email', 'maxlength' => '255',
  2569. ]],
  2570. '/div',
  2571. ];
  2572. $this->assertHtml($expected, $result);
  2573. $result = $this->Form->control('Contact.password');
  2574. $expected = [
  2575. 'div' => ['class' => 'input password'],
  2576. 'label' => ['for' => 'contact-password'],
  2577. 'Password',
  2578. '/label',
  2579. ['input' => [
  2580. 'type' => 'password', 'name' => 'Contact[password]',
  2581. 'id' => 'contact-password',
  2582. ]],
  2583. '/div',
  2584. ];
  2585. $this->assertHtml($expected, $result);
  2586. $result = $this->Form->control('Contact.email', [
  2587. 'type' => 'file', 'class' => 'textbox',
  2588. ]);
  2589. $expected = [
  2590. 'div' => ['class' => 'input file'],
  2591. 'label' => ['for' => 'contact-email'],
  2592. 'Email',
  2593. '/label',
  2594. ['input' => [
  2595. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2596. 'id' => 'contact-email',
  2597. ]],
  2598. '/div',
  2599. ];
  2600. $this->assertHtml($expected, $result);
  2601. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2602. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2603. $result = $this->Form->control('phone');
  2604. $expected = [
  2605. 'div' => ['class' => 'input tel'],
  2606. 'label' => ['for' => 'phone'],
  2607. 'Phone',
  2608. '/label',
  2609. ['input' => [
  2610. 'type' => 'tel', 'name' => 'phone',
  2611. 'value' => 'Hello &amp; World &gt; weird chars',
  2612. 'id' => 'phone', 'maxlength' => 255,
  2613. ]],
  2614. '/div',
  2615. ];
  2616. $this->assertHtml($expected, $result);
  2617. $this->View->setRequest(
  2618. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2619. );
  2620. $this->Form->create();
  2621. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2622. $expected = [
  2623. 'div' => ['class' => 'input text'],
  2624. 'label' => ['for' => 'myId'],
  2625. 'Field',
  2626. '/label',
  2627. 'input' => [
  2628. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2629. 'value' => 'My value', 'id' => 'myId',
  2630. ],
  2631. '/div',
  2632. ];
  2633. $this->assertHtml($expected, $result);
  2634. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2635. $this->Form->create();
  2636. $entity->setError('field', 'Badness!');
  2637. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2638. $result = $this->Form->control('field');
  2639. $expected = [
  2640. 'div' => ['class' => 'input text error'],
  2641. 'label' => ['for' => 'field'],
  2642. 'Field',
  2643. '/label',
  2644. 'input' => [
  2645. 'type' => 'text',
  2646. 'name' => 'field',
  2647. 'id' => 'field',
  2648. 'class' => 'form-error',
  2649. 'aria-invalid' => 'true',
  2650. 'aria-describedby' => 'field-error',
  2651. ],
  2652. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2653. 'Badness!',
  2654. '/div',
  2655. '/div',
  2656. ];
  2657. $this->assertHtml($expected, $result);
  2658. $result = $this->Form->control('field', [
  2659. 'templates' => [
  2660. 'inputContainerError' => '{{content}}{{error}}',
  2661. 'error' => '<span class="error-message">{{content}}</span>',
  2662. ],
  2663. ]);
  2664. $expected = [
  2665. 'label' => ['for' => 'field'],
  2666. 'Field',
  2667. '/label',
  2668. 'input' => [
  2669. 'type' => 'text',
  2670. 'name' => 'field',
  2671. 'id' => 'field',
  2672. 'class' => 'form-error',
  2673. // No aria-describedby because error template is custom
  2674. 'aria-invalid' => 'true',
  2675. ],
  2676. ['span' => ['class' => 'error-message']],
  2677. 'Badness!',
  2678. '/span',
  2679. ];
  2680. $this->assertHtml($expected, $result);
  2681. $entity->setError('field', ['minLength'], true);
  2682. $result = $this->Form->control('field', [
  2683. 'error' => [
  2684. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2685. 'maxLength' => 'login too large',
  2686. ],
  2687. ]);
  2688. $expected = [
  2689. 'div' => ['class' => 'input text error'],
  2690. 'label' => ['for' => 'field'],
  2691. 'Field',
  2692. '/label',
  2693. 'input' => [
  2694. 'type' => 'text',
  2695. 'name' => 'field',
  2696. 'id' => 'field',
  2697. 'class' => 'form-error',
  2698. 'aria-invalid' => 'true',
  2699. 'aria-describedby' => 'field-error',
  2700. ],
  2701. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2702. 'Le login doit contenir au moins 2 caractères',
  2703. '/div',
  2704. '/div',
  2705. ];
  2706. $this->assertHtml($expected, $result);
  2707. $entity->setError('field', ['maxLength'], true);
  2708. $result = $this->Form->control('field', [
  2709. 'error' => [
  2710. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2711. 'maxLength' => 'login too large',
  2712. ],
  2713. ]);
  2714. $expected = [
  2715. 'div' => ['class' => 'input text error'],
  2716. 'label' => ['for' => 'field'],
  2717. 'Field',
  2718. '/label',
  2719. 'input' => [
  2720. 'type' => 'text',
  2721. 'name' => 'field',
  2722. 'id' => 'field',
  2723. 'class' => 'form-error',
  2724. 'aria-invalid' => 'true',
  2725. 'aria-describedby' => 'field-error',
  2726. ],
  2727. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2728. 'login too large',
  2729. '/div',
  2730. '/div',
  2731. ];
  2732. $this->assertHtml($expected, $result);
  2733. }
  2734. /**
  2735. * testControlWithTemplateFile method
  2736. *
  2737. * Test that control() accepts a template file.
  2738. */
  2739. public function testControlWithTemplateFile(): void
  2740. {
  2741. $result = $this->Form->control('field', [
  2742. 'templates' => 'htmlhelper_tags',
  2743. ]);
  2744. $expected = [
  2745. 'label' => ['for' => 'field'],
  2746. 'Field',
  2747. '/label',
  2748. 'input' => [
  2749. 'type' => 'text', 'name' => 'field',
  2750. 'id' => 'field',
  2751. ],
  2752. ];
  2753. $this->assertHtml($expected, $result);
  2754. }
  2755. /**
  2756. * testNestedControlsEndWithBrackets method
  2757. *
  2758. * Test that nested inputs end with brackets.
  2759. */
  2760. public function testNestedControlsEndWithBrackets(): void
  2761. {
  2762. $result = $this->Form->text('nested.text[]');
  2763. $expected = [
  2764. 'input' => [
  2765. 'type' => 'text', 'name' => 'nested[text][]',
  2766. ],
  2767. ];
  2768. $this->assertHtml($expected, $result);
  2769. $result = $this->Form->file('nested.file[]');
  2770. $expected = [
  2771. 'input' => [
  2772. 'type' => 'file', 'name' => 'nested[file][]',
  2773. ],
  2774. ];
  2775. $this->assertHtml($expected, $result);
  2776. }
  2777. /**
  2778. * testCreateIdPrefix method
  2779. *
  2780. * Test id prefix.
  2781. */
  2782. public function testCreateIdPrefix(): void
  2783. {
  2784. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2785. $result = $this->Form->control('field');
  2786. $expected = [
  2787. 'div' => ['class' => 'input text'],
  2788. 'label' => ['for' => 'prefix-field'],
  2789. 'Field',
  2790. '/label',
  2791. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2792. '/div',
  2793. ];
  2794. $this->assertHtml($expected, $result);
  2795. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2796. $expected = [
  2797. 'div' => ['class' => 'input text'],
  2798. 'label' => ['for' => 'custom-id'],
  2799. 'Field',
  2800. '/label',
  2801. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2802. '/div',
  2803. ];
  2804. $this->assertHtml($expected, $result);
  2805. $result = $this->Form->radio('Model.field', ['option A']);
  2806. $expected = [
  2807. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2808. 'label' => ['for' => 'prefix-model-field-0'],
  2809. ['input' => [
  2810. 'type' => 'radio',
  2811. 'name' => 'Model[field]',
  2812. 'value' => '0',
  2813. 'id' => 'prefix-model-field-0',
  2814. ]],
  2815. 'option A',
  2816. '/label',
  2817. ];
  2818. $this->assertHtml($expected, $result);
  2819. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2820. $expected = [
  2821. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2822. 'label' => ['for' => 'prefix-model-field-0'],
  2823. ['input' => [
  2824. 'type' => 'radio',
  2825. 'name' => 'Model[field]',
  2826. 'value' => '0',
  2827. 'id' => 'prefix-model-field-0',
  2828. ]],
  2829. 'option A',
  2830. '/label',
  2831. ];
  2832. $this->assertHtml($expected, $result);
  2833. $result = $this->Form->select(
  2834. 'Model.multi_field',
  2835. ['first'],
  2836. ['multiple' => 'checkbox']
  2837. );
  2838. $expected = [
  2839. 'input' => [
  2840. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  2841. ],
  2842. ['div' => ['class' => 'checkbox']],
  2843. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2844. ['input' => [
  2845. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2846. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  2847. ]],
  2848. 'first',
  2849. '/label',
  2850. '/div',
  2851. ];
  2852. $this->assertHtml($expected, $result);
  2853. $this->Form->end();
  2854. $result = $this->Form->control('field');
  2855. $expected = [
  2856. 'div' => ['class' => 'input text'],
  2857. 'label' => ['for' => 'field'],
  2858. 'Field',
  2859. '/label',
  2860. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2861. '/div',
  2862. ];
  2863. $this->assertHtml($expected, $result);
  2864. }
  2865. /**
  2866. * testControlZero method
  2867. *
  2868. * Test that inputs with 0 can be created.
  2869. */
  2870. public function testControlZero(): void
  2871. {
  2872. $this->getTableLocator()->get('Contacts', [
  2873. 'className' => ContactsTable::class,
  2874. ]);
  2875. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2876. $result = $this->Form->control('0');
  2877. $expected = [
  2878. 'div' => ['class' => 'input text'],
  2879. 'label' => ['for' => '0'], '/label',
  2880. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2881. '/div',
  2882. ];
  2883. $this->assertHtml($expected, $result);
  2884. }
  2885. /**
  2886. * testControlCheckbox method
  2887. *
  2888. * Test control() with checkbox creation.
  2889. */
  2890. public function testControlCheckbox(): void
  2891. {
  2892. $articles = $this->getTableLocator()->get('Articles');
  2893. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  2894. $article = $articles->newEmptyEntity();
  2895. $this->Form->create($article);
  2896. $result = $this->Form->control('Articles.active');
  2897. $expected = [
  2898. 'div' => ['class' => 'input checkbox'],
  2899. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2900. 'label' => ['for' => 'articles-active'],
  2901. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  2902. 'Active',
  2903. '/label',
  2904. '/div',
  2905. ];
  2906. $this->assertHtml($expected, $result);
  2907. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  2908. $expected = [
  2909. 'div' => ['class' => 'input checkbox'],
  2910. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2911. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2912. '/div',
  2913. ];
  2914. $this->assertHtml($expected, $result);
  2915. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  2916. $expected = [
  2917. 'div' => ['class' => 'input checkbox'],
  2918. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2919. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2920. '/div',
  2921. ];
  2922. $this->assertHtml($expected, $result);
  2923. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  2924. $expected = [
  2925. 'div' => ['class' => 'input checkbox'],
  2926. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2927. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2928. '/div',
  2929. ];
  2930. $this->assertHtml($expected, $result);
  2931. $result = $this->Form->control('Articles.disabled', [
  2932. 'label' => 'Disabled',
  2933. 'type' => 'checkbox',
  2934. 'data-foo' => 'disabled',
  2935. ]);
  2936. $expected = [
  2937. 'div' => ['class' => 'input checkbox'],
  2938. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  2939. 'label' => ['for' => 'articles-disabled'],
  2940. ['input' => [
  2941. 'type' => 'checkbox',
  2942. 'name' => 'Articles[disabled]',
  2943. 'value' => '1',
  2944. 'id' => 'articles-disabled',
  2945. 'data-foo' => 'disabled',
  2946. ]],
  2947. 'Disabled',
  2948. '/label',
  2949. '/div',
  2950. ];
  2951. $this->assertHtml($expected, $result);
  2952. $result = $this->Form->control('Articles.confirm', [
  2953. 'label' => 'Confirm <b>me</b>!',
  2954. 'type' => 'checkbox',
  2955. 'escape' => false,
  2956. ]);
  2957. $expected = [
  2958. 'div' => ['class' => 'input checkbox'],
  2959. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  2960. 'label' => ['for' => 'articles-confirm'],
  2961. ['input' => [
  2962. 'type' => 'checkbox',
  2963. 'name' => 'Articles[confirm]',
  2964. 'value' => '1',
  2965. 'id' => 'articles-confirm',
  2966. ]],
  2967. 'Confirm <b>me</b>!',
  2968. '/label',
  2969. '/div',
  2970. ];
  2971. $this->assertHtml($expected, $result);
  2972. }
  2973. /**
  2974. * testControlHidden method
  2975. *
  2976. * Test that control() does not create wrapping div and label tag for hidden fields.
  2977. */
  2978. public function testControlHidden(): void
  2979. {
  2980. $this->getTableLocator()->get('ValidateUsers', [
  2981. 'className' => ValidateUsersTable::class,
  2982. ]);
  2983. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2984. $result = $this->Form->control('ValidateUser.id');
  2985. $expected = [
  2986. 'input' => ['name', 'type' => 'hidden', 'id'],
  2987. ];
  2988. $this->assertHtml($expected, $result);
  2989. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  2990. $expected = [
  2991. 'input' => ['name', 'type' => 'hidden', 'id'],
  2992. ];
  2993. $this->assertHtml($expected, $result);
  2994. }
  2995. /**
  2996. * testControlDatetime method
  2997. *
  2998. * Test form->control() with datetime.
  2999. */
  3000. public function testControlDatetime(): void
  3001. {
  3002. $result = $this->Form->control('prueba', [
  3003. 'type' => 'datetime',
  3004. 'value' => new DateTime('2019-09-27 02:52:43'),
  3005. ]);
  3006. $expected = [
  3007. 'div' => ['class' => 'input datetime'],
  3008. 'label' => ['for' => 'prueba'],
  3009. 'Prueba',
  3010. '/label',
  3011. 'input' => [
  3012. 'name' => 'prueba',
  3013. 'id' => 'prueba',
  3014. 'type' => 'datetime-local',
  3015. 'value' => '2019-09-27T02:52:43',
  3016. 'step' => '1',
  3017. ],
  3018. '/div',
  3019. ];
  3020. $this->assertHtml($expected, $result);
  3021. }
  3022. /**
  3023. * testControlDatetimeIdPrefix method
  3024. *
  3025. * Test form->control() with datetime with id prefix.
  3026. */
  3027. public function testControlDatetimeIdPrefix(): void
  3028. {
  3029. $this->Form->create(null, ['idPrefix' => 'prefix']);
  3030. $result = $this->Form->control('prueba', [
  3031. 'type' => 'datetime',
  3032. ]);
  3033. $expected = [
  3034. 'div' => ['class' => 'input datetime'],
  3035. 'label' => ['for' => 'prefix-prueba'],
  3036. 'Prueba',
  3037. '/label',
  3038. 'input' => [
  3039. 'name' => 'prueba',
  3040. 'id' => 'prefix-prueba',
  3041. 'type' => 'datetime-local',
  3042. 'value' => '',
  3043. 'step' => '1',
  3044. ],
  3045. '/div',
  3046. ];
  3047. $this->assertHtml($expected, $result);
  3048. }
  3049. /**
  3050. * testControlDatetimeStep method
  3051. *
  3052. * Test form->control() with datetime with custom step size.
  3053. */
  3054. public function testControlDatetimeStep(): void
  3055. {
  3056. $result = $this->Form->control('prueba', [
  3057. 'type' => 'datetime',
  3058. 'value' => new DateTime('2019-09-27 02:52:43'),
  3059. 'step' => '0.5',
  3060. ]);
  3061. $expected = [
  3062. 'div' => ['class' => 'input datetime'],
  3063. 'label' => ['for' => 'prueba'],
  3064. 'Prueba',
  3065. '/label',
  3066. 'input' => [
  3067. 'name' => 'prueba',
  3068. 'id' => 'prueba',
  3069. 'type' => 'datetime-local',
  3070. 'value' => '2019-09-27T02:52:43.000',
  3071. 'step' => '0.5',
  3072. ],
  3073. '/div',
  3074. ];
  3075. $this->assertHtml($expected, $result);
  3076. }
  3077. /**
  3078. * testControlCheckboxWithDisabledElements method
  3079. *
  3080. * Test generating checkboxes with disabled elements.
  3081. */
  3082. public function testControlCheckboxWithDisabledElements(): void
  3083. {
  3084. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3085. $result = $this->Form->control('Contact.multiple', [
  3086. 'multiple' => 'checkbox',
  3087. 'disabled' => 'disabled',
  3088. 'options' => $options,
  3089. ]);
  3090. $expected = [
  3091. ['div' => ['class' => 'input select']],
  3092. ['label' => ['for' => 'contact-multiple']],
  3093. 'Multiple',
  3094. '/label',
  3095. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3096. ['div' => ['class' => 'checkbox']],
  3097. ['label' => ['for' => 'contact-multiple-1']],
  3098. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3099. 'One',
  3100. '/label',
  3101. '/div',
  3102. ['div' => ['class' => 'checkbox']],
  3103. ['label' => ['for' => 'contact-multiple-2']],
  3104. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3105. 'Two',
  3106. '/label',
  3107. '/div',
  3108. ['div' => ['class' => 'checkbox']],
  3109. ['label' => ['for' => 'contact-multiple-3']],
  3110. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3111. 'Three',
  3112. '/label',
  3113. '/div',
  3114. '/div',
  3115. ];
  3116. $this->assertHtml($expected, $result);
  3117. // make sure 50 does only disable 50, and not 50f5c0cf
  3118. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3119. $disabled = [50];
  3120. $expected = [
  3121. ['div' => ['class' => 'input select']],
  3122. ['label' => ['for' => 'contact-multiple']],
  3123. 'Multiple',
  3124. '/label',
  3125. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3126. ['div' => ['class' => 'checkbox']],
  3127. ['label' => ['for' => 'contact-multiple-50']],
  3128. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3129. 'Fifty',
  3130. '/label',
  3131. '/div',
  3132. ['div' => ['class' => 'checkbox']],
  3133. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3134. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3135. 'Stringy',
  3136. '/label',
  3137. '/div',
  3138. '/div',
  3139. ];
  3140. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3141. $this->assertHtml($expected, $result);
  3142. }
  3143. /**
  3144. * testControlWithLeadingInteger method
  3145. *
  3146. * Test input name with leading integer, ensure attributes are generated correctly.
  3147. */
  3148. public function testControlWithLeadingInteger(): void
  3149. {
  3150. $result = $this->Form->text('0.Node.title');
  3151. $expected = [
  3152. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3153. ];
  3154. $this->assertHtml($expected, $result);
  3155. }
  3156. /**
  3157. * testControlSelectType method
  3158. *
  3159. * Test form->control() with select type inputs.
  3160. */
  3161. public function testControlSelectType(): void
  3162. {
  3163. $result = $this->Form->control(
  3164. 'email',
  3165. [
  3166. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3167. );
  3168. $expected = [
  3169. 'div' => ['class' => 'input select'],
  3170. 'label' => ['for' => 'email'],
  3171. 'Email',
  3172. '/label',
  3173. ['select' => ['name' => 'email', 'id' => 'email']],
  3174. ['option' => ['value' => '']],
  3175. '/option',
  3176. ['option' => ['value' => 'è']],
  3177. 'Firést',
  3178. '/option',
  3179. ['option' => ['value' => 'é']],
  3180. 'Secoènd',
  3181. '/option',
  3182. '/select',
  3183. '/div',
  3184. ];
  3185. $this->assertHtml($expected, $result);
  3186. $result = $this->Form->control(
  3187. 'email',
  3188. [
  3189. 'options' => ['First', 'Second'], 'empty' => true]
  3190. );
  3191. $expected = [
  3192. 'div' => ['class' => 'input select'],
  3193. 'label' => ['for' => 'email'],
  3194. 'Email',
  3195. '/label',
  3196. ['select' => ['name' => 'email', 'id' => 'email']],
  3197. ['option' => ['value' => '']],
  3198. '/option',
  3199. ['option' => ['value' => '0']],
  3200. 'First',
  3201. '/option',
  3202. ['option' => ['value' => '1']],
  3203. 'Second',
  3204. '/option',
  3205. '/select',
  3206. '/div',
  3207. ];
  3208. $this->assertHtml($expected, $result);
  3209. $result = $this->Form->control('email', [
  3210. 'type' => 'select',
  3211. 'options' => new ArrayObject(['First', 'Second']),
  3212. 'empty' => true,
  3213. ]);
  3214. $this->assertHtml($expected, $result);
  3215. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3216. $this->View->setRequest(
  3217. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3218. );
  3219. $this->Form->create();
  3220. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3221. $expected = [
  3222. 'div' => ['class' => 'input select'],
  3223. 'label' => ['for' => 'model-user-id'],
  3224. 'User',
  3225. '/label',
  3226. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3227. ['option' => ['value' => '']],
  3228. '/option',
  3229. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3230. 'good',
  3231. '/option',
  3232. ['option' => ['value' => 'other']],
  3233. 'bad',
  3234. '/option',
  3235. '/select',
  3236. '/div',
  3237. ];
  3238. $this->assertHtml($expected, $result);
  3239. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3240. $this->View->setRequest(
  3241. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3242. );
  3243. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3244. $expected = [
  3245. 'div' => ['class' => 'input select'],
  3246. 'label' => ['for' => 'thing-user-id'],
  3247. 'User',
  3248. '/label',
  3249. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3250. ['option' => ['value' => '']],
  3251. 'Some Empty',
  3252. '/option',
  3253. ['option' => ['value' => 'value']],
  3254. 'good',
  3255. '/option',
  3256. ['option' => ['value' => 'other']],
  3257. 'bad',
  3258. '/option',
  3259. '/select',
  3260. '/div',
  3261. ];
  3262. $this->assertHtml($expected, $result);
  3263. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3264. $this->View->setRequest(
  3265. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3266. );
  3267. $this->Form->create();
  3268. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3269. $expected = [
  3270. 'div' => ['class' => 'input select'],
  3271. 'label' => ['for' => 'thing-user-id'],
  3272. 'User',
  3273. '/label',
  3274. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3275. ['option' => ['value' => '']],
  3276. 'Some Empty',
  3277. '/option',
  3278. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3279. 'good',
  3280. '/option',
  3281. ['option' => ['value' => 'other']],
  3282. 'bad',
  3283. '/option',
  3284. '/select',
  3285. '/div',
  3286. ];
  3287. $this->assertHtml($expected, $result);
  3288. $result = $this->Form->control('Publisher.id', [
  3289. 'label' => 'Publisher',
  3290. 'type' => 'select',
  3291. 'multiple' => 'checkbox',
  3292. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3293. ]);
  3294. $expected = [
  3295. ['div' => ['class' => 'input select']],
  3296. ['label' => ['for' => 'publisher-id']],
  3297. 'Publisher',
  3298. '/label',
  3299. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3300. ['div' => ['class' => 'checkbox']],
  3301. ['label' => ['for' => 'publisher-id-value-1']],
  3302. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3303. 'Label 1',
  3304. '/label',
  3305. '/div',
  3306. ['div' => ['class' => 'checkbox']],
  3307. ['label' => ['for' => 'publisher-id-value-2']],
  3308. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3309. 'Label 2',
  3310. '/label',
  3311. '/div',
  3312. '/div',
  3313. ];
  3314. $this->assertHtml($expected, $result);
  3315. }
  3316. /**
  3317. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3318. *
  3319. * Test that control() and a non standard primary key makes a hidden input by default.
  3320. */
  3321. public function testControlWithNonStandardPrimaryKeyMakesHidden(): void
  3322. {
  3323. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3324. $this->Form->create($this->article);
  3325. $result = $this->Form->control('title');
  3326. $expected = [
  3327. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3328. ];
  3329. $this->assertHtml($expected, $result);
  3330. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3331. $this->Form->create($this->article);
  3332. $result = $this->Form->control('title');
  3333. $expected = [
  3334. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3335. ];
  3336. $this->assertHtml($expected, $result);
  3337. $result = $this->Form->control('body');
  3338. $expected = [
  3339. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3340. ];
  3341. $this->assertHtml($expected, $result);
  3342. }
  3343. /**
  3344. * testControlOverridingMagicSelectType method
  3345. *
  3346. * Test that overriding the magic select type widget is possible.
  3347. */
  3348. public function testControlOverridingMagicSelectType(): void
  3349. {
  3350. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3351. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3352. $expected = [
  3353. 'div' => ['class' => 'input text'],
  3354. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3355. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3356. '/div',
  3357. ];
  3358. $this->assertHtml($expected, $result);
  3359. //Check that magic types still work for plural/singular vars
  3360. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3361. $result = $this->Form->control('Model.type');
  3362. $expected = [
  3363. 'div' => ['class' => 'input select'],
  3364. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3365. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3366. ['option' => ['value' => 'value']], 'good', '/option',
  3367. ['option' => ['value' => 'other']], 'bad', '/option',
  3368. '/select',
  3369. '/div',
  3370. ];
  3371. $this->assertHtml($expected, $result);
  3372. }
  3373. /**
  3374. * testControlMagicTypeDoesNotOverride method
  3375. *
  3376. * Test that inferred types do not override developer input.
  3377. */
  3378. public function testControlMagicTypeDoesNotOverride(): void
  3379. {
  3380. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3381. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3382. $expected = [
  3383. 'div' => ['class' => 'input checkbox'],
  3384. ['input' => [
  3385. 'type' => 'hidden',
  3386. 'name' => 'Model[user]',
  3387. 'value' => 0,
  3388. ]],
  3389. 'label' => ['for' => 'model-user'],
  3390. ['input' => [
  3391. 'name' => 'Model[user]',
  3392. 'type' => 'checkbox',
  3393. 'id' => 'model-user',
  3394. 'value' => 1,
  3395. ]],
  3396. 'User',
  3397. '/label',
  3398. '/div',
  3399. ];
  3400. $this->assertHtml($expected, $result);
  3401. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3402. $options = [
  3403. 1 => 'blue',
  3404. 2 => 'red',
  3405. ];
  3406. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3407. $expected = [
  3408. 'div' => ['class' => 'input select'],
  3409. 'label' => ['for' => 'tags-ids'],
  3410. 'Tags',
  3411. '/label',
  3412. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3413. ['div' => ['class' => 'checkbox']],
  3414. ['label' => ['for' => 'tags-ids-1']],
  3415. ['input' => [
  3416. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3417. 'value' => '1', 'name' => 'tags[_ids][]',
  3418. ]],
  3419. 'blue',
  3420. '/label',
  3421. '/div',
  3422. ['div' => ['class' => 'checkbox']],
  3423. ['label' => ['for' => 'tags-ids-2']],
  3424. ['input' => [
  3425. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3426. 'value' => '2', 'name' => 'tags[_ids][]',
  3427. ]],
  3428. 'red',
  3429. '/label',
  3430. '/div',
  3431. '/div',
  3432. ];
  3433. $this->assertHtml($expected, $result);
  3434. }
  3435. /**
  3436. * testControlMagicSelectForTypeNumber method
  3437. *
  3438. * Test that magic control() selects are created for type=number.
  3439. */
  3440. public function testControlMagicSelectForTypeNumber(): void
  3441. {
  3442. $this->getTableLocator()->get('ValidateUsers', [
  3443. 'className' => ValidateUsersTable::class,
  3444. ]);
  3445. $entity = new Entity(['balance' => 1]);
  3446. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3447. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3448. $result = $this->Form->control('balance');
  3449. $expected = [
  3450. 'div' => ['class' => 'input select'],
  3451. 'label' => ['for' => 'balance'],
  3452. 'Balance',
  3453. '/label',
  3454. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3455. ['option' => ['value' => '0']],
  3456. 'nothing',
  3457. '/option',
  3458. ['option' => ['value' => '1', 'selected' => 'selected']],
  3459. 'some',
  3460. '/option',
  3461. ['option' => ['value' => '100']],
  3462. 'a lot',
  3463. '/option',
  3464. '/select',
  3465. '/div',
  3466. ];
  3467. $this->assertHtml($expected, $result);
  3468. }
  3469. /**
  3470. * testInvalidControlTypeOption method
  3471. *
  3472. * Test invalid 'input' type option to control() function.
  3473. */
  3474. public function testInvalidControlTypeOption(): void
  3475. {
  3476. $this->expectException(RuntimeException::class);
  3477. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3478. $this->Form->control('text', ['type' => 'input']);
  3479. }
  3480. /**
  3481. * testControlMagicSelectChangeToRadio method
  3482. *
  3483. * Test that magic control() selects can easily be converted into radio types without error.
  3484. */
  3485. public function testControlMagicSelectChangeToRadio(): void
  3486. {
  3487. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3488. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3489. $this->assertStringContainsString('input type="radio"', $result);
  3490. }
  3491. /**
  3492. * testFormControlSubmit method
  3493. *
  3494. * Test correct results for form::control() and type submit.
  3495. */
  3496. public function testFormControlSubmit(): void
  3497. {
  3498. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3499. $expected = [
  3500. 'div' => ['class' => 'submit'],
  3501. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3502. '/div',
  3503. ];
  3504. $this->assertHtml($expected, $result);
  3505. }
  3506. /**
  3507. * testFormControls method
  3508. *
  3509. * Test correct results from Form::controls().
  3510. */
  3511. public function testFormControlsLegendFieldset(): void
  3512. {
  3513. $this->Form->create($this->article);
  3514. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3515. $expected = [
  3516. '<fieldset',
  3517. '<legend',
  3518. 'The Legend',
  3519. '/legend',
  3520. '*/fieldset',
  3521. ];
  3522. $this->assertHtml($expected, $result);
  3523. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3524. $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
  3525. $this->assertStringContainsString('<fieldset>', $result);
  3526. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3527. $this->assertStringNotContainsString('<legend>', $result);
  3528. $this->assertStringNotContainsString('<fieldset>', $result);
  3529. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3530. $this->assertStringNotContainsString('<legend>', $result);
  3531. $this->assertStringNotContainsString('<fieldset>', $result);
  3532. $this->Form->create($this->article);
  3533. $this->View->setRequest($this->View->getRequest()
  3534. ->withParam('prefix', 'admin')
  3535. ->withParam('action', 'admin_edit')
  3536. ->withParam('controller', 'articles'));
  3537. $result = $this->Form->allControls();
  3538. $expected = [
  3539. '<fieldset',
  3540. '<legend',
  3541. 'New Article',
  3542. '/legend',
  3543. '*/fieldset',
  3544. ];
  3545. $this->assertHtml($expected, $result);
  3546. $this->Form->create($this->article);
  3547. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3548. $expected = [
  3549. '<fieldset',
  3550. '<legend',
  3551. 'The Legend',
  3552. '/legend',
  3553. '*/fieldset',
  3554. ];
  3555. $this->assertHtml($expected, $result);
  3556. $this->Form->create($this->article);
  3557. $result = $this->Form->allControls([], [
  3558. 'fieldset' => [
  3559. 'class' => 'some-class some-other-class',
  3560. 'disabled' => true,
  3561. 'data-param' => 'a-param',
  3562. ],
  3563. 'legend' => 'The Legend',
  3564. ]);
  3565. $expected = [
  3566. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3567. '<legend',
  3568. 'The Legend',
  3569. '/legend',
  3570. '*/fieldset',
  3571. ];
  3572. $this->assertHtml($expected, $result);
  3573. }
  3574. /**
  3575. * testFormControls method
  3576. *
  3577. * Test the controls() method.
  3578. */
  3579. public function testFormControls(): void
  3580. {
  3581. $this->Form->create($this->article);
  3582. $result = $this->Form->allControls();
  3583. $expected = [
  3584. '<fieldset',
  3585. '<legend', 'New Article', '/legend',
  3586. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3587. ['div' => ['class' => 'input select required']],
  3588. '*/div',
  3589. ['div' => ['class' => 'input text required']],
  3590. '*/div',
  3591. ['div' => ['class' => 'input text']],
  3592. '*/div',
  3593. ['div' => ['class' => 'input text']],
  3594. '*/div',
  3595. '/fieldset',
  3596. ];
  3597. $this->assertHtml($expected, $result);
  3598. $result = $this->Form->allControls([
  3599. 'published' => ['type' => 'boolean'],
  3600. ]);
  3601. $expected = [
  3602. '<fieldset',
  3603. '<legend', 'New Article', '/legend',
  3604. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3605. ['div' => ['class' => 'input select required']],
  3606. '*/div',
  3607. ['div' => ['class' => 'input text required']],
  3608. '*/div',
  3609. ['div' => ['class' => 'input text']],
  3610. '*/div',
  3611. ['div' => ['class' => 'input boolean']],
  3612. '*/div',
  3613. '/fieldset',
  3614. ];
  3615. $this->assertHtml($expected, $result);
  3616. $this->Form->create($this->article);
  3617. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3618. $expected = [
  3619. 'fieldset' => [],
  3620. 'legend' => [],
  3621. 'Hello',
  3622. '/legend',
  3623. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3624. ['div' => ['class' => 'input select required']],
  3625. '*/div',
  3626. ['div' => ['class' => 'input text required']],
  3627. '*/div',
  3628. ['div' => ['class' => 'input text']],
  3629. '*/div',
  3630. ['div' => ['class' => 'input text']],
  3631. '*/div',
  3632. '/fieldset',
  3633. ];
  3634. $this->assertHtml($expected, $result);
  3635. $this->Form->create();
  3636. $expected = [
  3637. 'fieldset' => [],
  3638. ['div' => ['class' => 'input text']],
  3639. 'label' => ['for' => 'foo'],
  3640. 'Foo',
  3641. '/label',
  3642. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3643. '*/div',
  3644. '/fieldset',
  3645. ];
  3646. $result = $this->Form->allControls(
  3647. ['foo' => ['type' => 'text']],
  3648. ['legend' => false]
  3649. );
  3650. $this->assertHtml($expected, $result);
  3651. }
  3652. /**
  3653. * testFormControlsBlacklist method
  3654. */
  3655. public function testFormControlsBlacklist(): void
  3656. {
  3657. $this->Form->create($this->article);
  3658. $result = $this->Form->allControls([
  3659. 'id' => false,
  3660. ]);
  3661. $expected = [
  3662. '<fieldset',
  3663. '<legend', 'New Article', '/legend',
  3664. ['div' => ['class' => 'input select required']],
  3665. '*/div',
  3666. ['div' => ['class' => 'input text required']],
  3667. '*/div',
  3668. ['div' => ['class' => 'input text']],
  3669. '*/div',
  3670. ['div' => ['class' => 'input text']],
  3671. '*/div',
  3672. '/fieldset',
  3673. ];
  3674. $this->assertHtml($expected, $result);
  3675. $this->Form->create($this->article);
  3676. $result = $this->Form->allControls([
  3677. 'id' => [],
  3678. ]);
  3679. $expected = [
  3680. '<fieldset',
  3681. '<legend', 'New Article', '/legend',
  3682. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3683. ['div' => ['class' => 'input select required']],
  3684. '*/div',
  3685. ['div' => ['class' => 'input text required']],
  3686. '*/div',
  3687. ['div' => ['class' => 'input text']],
  3688. '*/div',
  3689. ['div' => ['class' => 'input text']],
  3690. '*/div',
  3691. '/fieldset',
  3692. ];
  3693. $this->assertHtml($expected, $result, true);
  3694. }
  3695. /**
  3696. * testSelectAsCheckbox method
  3697. *
  3698. * Test multi-select widget with checkbox formatting.
  3699. */
  3700. public function testSelectAsCheckbox(): void
  3701. {
  3702. $result = $this->Form->select(
  3703. 'Model.multi_field',
  3704. ['first', 'second', 'third'],
  3705. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3706. );
  3707. $expected = [
  3708. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3709. ['div' => ['class' => 'checkbox']],
  3710. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3711. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3712. 'first',
  3713. '/label',
  3714. '/div',
  3715. ['div' => ['class' => 'checkbox']],
  3716. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3717. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3718. 'second',
  3719. '/label',
  3720. '/div',
  3721. ['div' => ['class' => 'checkbox']],
  3722. ['label' => ['for' => 'model-multi-field-2']],
  3723. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3724. 'third',
  3725. '/label',
  3726. '/div',
  3727. ];
  3728. $this->assertHtml($expected, $result);
  3729. $result = $this->Form->select(
  3730. 'Model.multi_field',
  3731. ['1/2' => 'half'],
  3732. ['multiple' => 'checkbox']
  3733. );
  3734. $expected = [
  3735. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3736. ['div' => ['class' => 'checkbox']],
  3737. ['label' => ['for' => 'model-multi-field-1-2']],
  3738. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3739. 'half',
  3740. '/label',
  3741. '/div',
  3742. ];
  3743. $this->assertHtml($expected, $result);
  3744. }
  3745. /**
  3746. * testLabel method
  3747. *
  3748. * Test label generation.
  3749. */
  3750. public function testLabel(): void
  3751. {
  3752. $result = $this->Form->label('Person.name');
  3753. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3754. $this->assertHtml($expected, $result);
  3755. $result = $this->Form->label('Person.name');
  3756. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3757. $this->assertHtml($expected, $result);
  3758. $result = $this->Form->label('Person.first_name');
  3759. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3760. $this->assertHtml($expected, $result);
  3761. $result = $this->Form->label('Person.first_name', 'Your first name');
  3762. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3763. $this->assertHtml($expected, $result);
  3764. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3765. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3766. $this->assertHtml($expected, $result);
  3767. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3768. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3769. $this->assertHtml($expected, $result);
  3770. $result = $this->Form->label('Person.first_name', '');
  3771. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3772. $this->assertHtml($expected, $result);
  3773. $result = $this->Form->label('Person.2.name', '');
  3774. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3775. $this->assertHtml($expected, $result);
  3776. }
  3777. /**
  3778. * testLabelContainControl method
  3779. *
  3780. * Test that label() can accept an input with the correct template vars.
  3781. */
  3782. public function testLabelContainControl(): void
  3783. {
  3784. $this->Form->setTemplates([
  3785. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3786. ]);
  3787. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3788. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3789. ]);
  3790. $expected = [
  3791. 'label' => ['for' => 'person-accept-terms'],
  3792. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3793. 'Accept',
  3794. '/label',
  3795. ];
  3796. $this->assertHtml($expected, $result);
  3797. }
  3798. /**
  3799. * testTextbox method
  3800. *
  3801. * Test textbox element generation.
  3802. */
  3803. public function testTextbox(): void
  3804. {
  3805. $result = $this->Form->text('Model.field');
  3806. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3807. $this->assertHtml($expected, $result);
  3808. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3809. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3810. $this->assertHtml($expected, $result);
  3811. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3812. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3813. $this->assertHtml($expected, $result);
  3814. }
  3815. /**
  3816. * testTextBoxDataAndError method
  3817. *
  3818. * Test that text() hooks up with request data and error fields.
  3819. */
  3820. public function testTextBoxDataAndError(): void
  3821. {
  3822. $this->article['errors'] = [
  3823. 'Contact' => ['text' => 'wrong'],
  3824. ];
  3825. $this->View->setRequest($this->View->getRequest()
  3826. ->withData('Model.text', 'test <strong>HTML</strong> values')
  3827. ->withData('Contact.text', 'test'));
  3828. $this->Form->create($this->article);
  3829. $result = $this->Form->text('Model.text');
  3830. $expected = [
  3831. 'input' => [
  3832. 'type' => 'text',
  3833. 'name' => 'Model[text]',
  3834. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3835. ],
  3836. ];
  3837. $this->assertHtml($expected, $result);
  3838. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3839. $expected = [
  3840. 'input' => [
  3841. 'type' => 'text',
  3842. 'name' => 'Contact[text]',
  3843. 'value' => 'test',
  3844. 'id' => 'theID',
  3845. 'class' => 'form-error',
  3846. ],
  3847. ];
  3848. $this->assertHtml($expected, $result);
  3849. }
  3850. /**
  3851. * testDefaultValue method
  3852. *
  3853. * Test default value setting.
  3854. */
  3855. public function testTextDefaultValue(): void
  3856. {
  3857. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  3858. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3859. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3860. $this->assertHtml($expected, $result);
  3861. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  3862. $this->Form->create();
  3863. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3864. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3865. $this->assertHtml($expected, $result);
  3866. $Articles = $this->getTableLocator()->get('Articles');
  3867. $title = $Articles->getSchema()->getColumn('title');
  3868. $Articles->getSchema()->addColumn(
  3869. 'title',
  3870. ['default' => 'default title', 'length' => 255] + $title
  3871. );
  3872. $entity = $Articles->newEmptyEntity();
  3873. $this->Form->create($entity);
  3874. // Get default value from schema
  3875. $result = $this->Form->text('title');
  3876. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title', 'maxlength' => '255']];
  3877. $this->assertHtml($expected, $result);
  3878. // Don't get value from schema
  3879. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3880. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3881. $this->assertHtml($expected, $result);
  3882. // Custom default value overrides default value from schema
  3883. $result = $this->Form->text('title', ['default' => 'override default']);
  3884. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default', 'maxlength' => '255']];
  3885. $this->assertHtml($expected, $result);
  3886. // Default value from schema is used only for new entities.
  3887. $entity->setNew(false);
  3888. $result = $this->Form->text('title');
  3889. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3890. $this->assertHtml($expected, $result);
  3891. }
  3892. /**
  3893. * testError method
  3894. *
  3895. * Test field error generation.
  3896. */
  3897. public function testError(): void
  3898. {
  3899. $this->article['errors'] = [
  3900. 'Article' => ['field' => 'email'],
  3901. ];
  3902. $this->Form->create($this->article);
  3903. $result = $this->Form->error('Article.field');
  3904. $expected = [
  3905. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3906. 'email',
  3907. '/div',
  3908. ];
  3909. $this->assertHtml($expected, $result);
  3910. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  3911. $expected = [
  3912. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3913. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3914. '/div',
  3915. ];
  3916. $this->assertHtml($expected, $result);
  3917. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  3918. $expected = [
  3919. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3920. '<strong', 'Badness!', '/strong',
  3921. '/div',
  3922. ];
  3923. $this->assertHtml($expected, $result);
  3924. }
  3925. /**
  3926. * testErrorRuleName method
  3927. *
  3928. * Test error translation can use rule names for translating.
  3929. */
  3930. public function testErrorRuleName(): void
  3931. {
  3932. $this->article['errors'] = [
  3933. 'Article' => [
  3934. 'field' => ['email' => 'Your email was not good'],
  3935. ],
  3936. ];
  3937. $this->Form->create($this->article);
  3938. $result = $this->Form->error('Article.field');
  3939. $expected = [
  3940. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3941. 'Your email was not good',
  3942. '/div',
  3943. ];
  3944. $this->assertHtml($expected, $result);
  3945. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  3946. $expected = [
  3947. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3948. 'Email in use',
  3949. '/div',
  3950. ];
  3951. $this->assertHtml($expected, $result);
  3952. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  3953. $expected = [
  3954. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3955. 'Email in use',
  3956. '/div',
  3957. ];
  3958. $this->assertHtml($expected, $result);
  3959. $result = $this->Form->error('Article.field', [
  3960. 'email' => 'Key is preferred',
  3961. 'Your email was not good' => 'Email in use',
  3962. ]);
  3963. $expected = [
  3964. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3965. 'Key is preferred',
  3966. '/div',
  3967. ];
  3968. $this->assertHtml($expected, $result);
  3969. }
  3970. /**
  3971. * testErrorMessages method
  3972. *
  3973. * Test error with nested lists.
  3974. */
  3975. public function testErrorMessages(): void
  3976. {
  3977. $this->article['errors'] = [
  3978. 'Article' => ['field' => 'email'],
  3979. ];
  3980. $this->Form->create($this->article);
  3981. $result = $this->Form->error('Article.field', [
  3982. 'email' => 'No good!',
  3983. ]);
  3984. $expected = [
  3985. 'div' => ['class' => 'error-message', 'id' => 'article-field-error'],
  3986. 'No good!',
  3987. '/div',
  3988. ];
  3989. $this->assertHtml($expected, $result);
  3990. }
  3991. /**
  3992. * testErrorMultipleMessages method
  3993. *
  3994. * Test error() with multiple messages.
  3995. */
  3996. public function testErrorMultipleMessages(): void
  3997. {
  3998. $this->article['errors'] = [
  3999. 'field' => ['notBlank', 'email', 'Something else'],
  4000. ];
  4001. $this->Form->create($this->article);
  4002. $result = $this->Form->error('field', [
  4003. 'notBlank' => 'Cannot be empty',
  4004. 'email' => 'No good!',
  4005. ]);
  4006. $expected = [
  4007. 'div' => ['class' => 'error-message', 'id' => 'field-error'],
  4008. 'ul' => [],
  4009. '<li', 'Cannot be empty', '/li',
  4010. '<li', 'No good!', '/li',
  4011. '<li', 'Something else', '/li',
  4012. '/ul',
  4013. '/div',
  4014. ];
  4015. $this->assertHtml($expected, $result);
  4016. }
  4017. /**
  4018. * testPassword method
  4019. *
  4020. * Test password element generation.
  4021. */
  4022. public function testPassword(): void
  4023. {
  4024. $this->article['errors'] = [
  4025. 'Contact' => [
  4026. 'passwd' => 1,
  4027. ],
  4028. ];
  4029. $this->Form->create($this->article);
  4030. $result = $this->Form->password('Contact.field');
  4031. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4032. $this->assertHtml($expected, $result);
  4033. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4034. $this->Form->create($this->article);
  4035. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4036. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4037. $this->assertHtml($expected, $result);
  4038. }
  4039. /**
  4040. * testRadio method
  4041. *
  4042. * Test radio element set generation.
  4043. */
  4044. public function testRadio(): void
  4045. {
  4046. $result = $this->Form->radio('Model.field', ['option A']);
  4047. $expected = [
  4048. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4049. 'label' => ['for' => 'model-field-0'],
  4050. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4051. 'option A',
  4052. '/label',
  4053. ];
  4054. $this->assertHtml($expected, $result);
  4055. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4056. $this->assertHtml($expected, $result);
  4057. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4058. $expected = [
  4059. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4060. ['label' => ['for' => 'model-field-0']],
  4061. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4062. 'option A',
  4063. '/label',
  4064. ['label' => ['for' => 'model-field-1']],
  4065. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4066. 'option B',
  4067. '/label',
  4068. ];
  4069. $this->assertHtml($expected, $result);
  4070. $result = $this->Form->radio(
  4071. 'Employee.gender',
  4072. ['male' => 'Male', 'female' => 'Female'],
  4073. ['form' => 'my-form']
  4074. );
  4075. $expected = [
  4076. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4077. ['label' => ['for' => 'employee-gender-male']],
  4078. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4079. 'Male',
  4080. '/label',
  4081. ['label' => ['for' => 'employee-gender-female']],
  4082. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4083. 'Female',
  4084. '/label',
  4085. ];
  4086. $this->assertHtml($expected, $result);
  4087. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4088. $expected = [
  4089. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4090. ['label' => ['for' => 'model-custom-0']],
  4091. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4092. 'option A',
  4093. '/label',
  4094. ['label' => ['for' => 'model-custom-1']],
  4095. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4096. 'option B',
  4097. '/label',
  4098. ];
  4099. $this->assertHtml($expected, $result);
  4100. $result = $this->Form->radio(
  4101. 'Employee.gender',
  4102. [
  4103. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4104. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4105. ]
  4106. );
  4107. $expected = [
  4108. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4109. ['label' => ['for' => 'employee-gender-male']],
  4110. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4111. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4112. 'Male',
  4113. '/label',
  4114. ['label' => ['for' => 'employee-gender-female']],
  4115. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4116. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4117. 'Female',
  4118. '/label',
  4119. ];
  4120. $this->assertHtml($expected, $result);
  4121. }
  4122. /**
  4123. * Test radio with complex options and empty disabled data.
  4124. */
  4125. public function testRadioComplexDisabled(): void
  4126. {
  4127. $options = [
  4128. ['value' => 'r', 'text' => 'red'],
  4129. ['value' => 'b', 'text' => 'blue'],
  4130. ];
  4131. $attrs = ['disabled' => []];
  4132. $result = $this->Form->radio('Model.field', $options, $attrs);
  4133. $expected = [
  4134. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4135. ['label' => ['for' => 'model-field-r']],
  4136. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4137. 'red',
  4138. '/label',
  4139. ['label' => ['for' => 'model-field-b']],
  4140. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4141. 'blue',
  4142. '/label',
  4143. ];
  4144. $this->assertHtml($expected, $result);
  4145. $attrs = ['disabled' => ['r']];
  4146. $result = $this->Form->radio('Model.field', $options, $attrs);
  4147. $this->assertStringContainsString('disabled="disabled"', $result);
  4148. }
  4149. /**
  4150. * testRadioDefaultValue method
  4151. *
  4152. * Test default value setting on radio() method.
  4153. */
  4154. public function testRadioDefaultValue(): void
  4155. {
  4156. $Articles = $this->getTableLocator()->get('Articles');
  4157. $title = $Articles->getSchema()->getColumn('title');
  4158. $Articles->getSchema()->addColumn(
  4159. 'title',
  4160. ['default' => '1'] + $title
  4161. );
  4162. $this->Form->create($Articles->newEmptyEntity());
  4163. $result = $this->Form->radio('title', ['option A', 'option B']);
  4164. $expected = [
  4165. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4166. ['label' => ['for' => 'title-0']],
  4167. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4168. 'option A',
  4169. '/label',
  4170. ['label' => ['for' => 'title-1']],
  4171. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4172. 'option B',
  4173. '/label',
  4174. ];
  4175. $this->assertHtml($expected, $result);
  4176. }
  4177. /**
  4178. * Test setting a hiddenField value on radio buttons.
  4179. */
  4180. public function testRadioHiddenFieldValue(): void
  4181. {
  4182. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4183. $expected = [
  4184. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4185. 'label' => ['for' => 'title-0'],
  4186. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4187. 'option A',
  4188. '/label',
  4189. ];
  4190. $this->assertHtml($expected, $result);
  4191. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => '']);
  4192. $expected = [
  4193. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4194. 'label' => ['for' => 'title-0'],
  4195. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4196. 'option A',
  4197. '/label',
  4198. ];
  4199. $this->assertHtml($expected, $result);
  4200. }
  4201. /**
  4202. * testControlRadio method
  4203. *
  4204. * Test that input works with radio types.
  4205. */
  4206. public function testControlRadio(): void
  4207. {
  4208. $result = $this->Form->control('test', [
  4209. 'type' => 'radio',
  4210. 'options' => ['A', 'B'],
  4211. ]);
  4212. $expected = [
  4213. ['div' => ['class' => 'input radio']],
  4214. '<label',
  4215. 'Test',
  4216. '/label',
  4217. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4218. ['label' => ['for' => 'test-0']],
  4219. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4220. 'A',
  4221. '/label',
  4222. ['label' => ['for' => 'test-1']],
  4223. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4224. 'B',
  4225. '/label',
  4226. '/div',
  4227. ];
  4228. $this->assertHtml($expected, $result);
  4229. $result = $this->Form->control('test', [
  4230. 'type' => 'radio',
  4231. 'options' => ['A', 'B'],
  4232. 'value' => '0',
  4233. ]);
  4234. $expected = [
  4235. ['div' => ['class' => 'input radio']],
  4236. '<label',
  4237. 'Test',
  4238. '/label',
  4239. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4240. ['label' => ['for' => 'test-0']],
  4241. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4242. 'A',
  4243. '/label',
  4244. ['label' => ['for' => 'test-1']],
  4245. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4246. 'B',
  4247. '/label',
  4248. '/div',
  4249. ];
  4250. $this->assertHtml($expected, $result);
  4251. $result = $this->Form->control('test', [
  4252. 'type' => 'radio',
  4253. 'options' => ['A', 'B'],
  4254. 'label' => false,
  4255. ]);
  4256. $expected = [
  4257. ['div' => ['class' => 'input radio']],
  4258. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4259. ['label' => ['for' => 'test-0']],
  4260. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4261. 'A',
  4262. '/label',
  4263. ['label' => ['for' => 'test-1']],
  4264. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4265. 'B',
  4266. '/label',
  4267. '/div',
  4268. ];
  4269. $this->assertHtml($expected, $result);
  4270. $result = $this->Form->control('accept', [
  4271. 'type' => 'radio',
  4272. 'options' => [
  4273. 1 => 'positive',
  4274. -1 => 'negative',
  4275. ],
  4276. 'label' => false,
  4277. ]);
  4278. $expected = [
  4279. ['div' => ['class' => 'input radio']],
  4280. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => '']],
  4281. ['label' => ['for' => 'accept-1']],
  4282. ['input' => [
  4283. 'type' => 'radio',
  4284. 'name' => 'accept',
  4285. 'value' => '1',
  4286. 'id' => 'accept-1',
  4287. ]],
  4288. 'positive',
  4289. '/label',
  4290. ['label' => ['for' => 'accept--1']],
  4291. ['input' => [
  4292. 'type' => 'radio',
  4293. 'name' => 'accept',
  4294. 'value' => '-1',
  4295. 'id' => 'accept--1',
  4296. ]],
  4297. 'negative',
  4298. '/label',
  4299. '/div',
  4300. ];
  4301. $this->assertHtml($expected, $result);
  4302. }
  4303. /**
  4304. * testRadioNoLabel method
  4305. *
  4306. * Test that radio() works with label = false.
  4307. */
  4308. public function testRadioNoLabel(): void
  4309. {
  4310. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4311. $expected = [
  4312. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4313. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4314. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4315. ];
  4316. $this->assertHtml($expected, $result);
  4317. }
  4318. /**
  4319. * testRadioControlInsideLabel method
  4320. *
  4321. * Test generating radio input inside label ala twitter bootstrap.
  4322. */
  4323. public function testRadioControlInsideLabel(): void
  4324. {
  4325. $this->Form->setTemplates([
  4326. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4327. 'radioWrapper' => '{{label}}',
  4328. ]);
  4329. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4330. // phpcs:disable
  4331. $expected = [
  4332. ['input' => [
  4333. 'type' => 'hidden',
  4334. 'name' => 'Model[field]',
  4335. 'value' => ''
  4336. ]],
  4337. ['label' => ['for' => 'model-field-0']],
  4338. ['input' => [
  4339. 'type' => 'radio',
  4340. 'name' => 'Model[field]',
  4341. 'value' => '0',
  4342. 'id' => 'model-field-0'
  4343. ]],
  4344. 'option A',
  4345. '/label',
  4346. ['label' => ['for' => 'model-field-1']],
  4347. ['input' => [
  4348. 'type' => 'radio',
  4349. 'name' => 'Model[field]',
  4350. 'value' => '1',
  4351. 'id' => 'model-field-1'
  4352. ]],
  4353. 'option B',
  4354. '/label',
  4355. ];
  4356. // phpcs:enable
  4357. $this->assertHtml($expected, $result);
  4358. }
  4359. /**
  4360. * testRadioHiddenControlDisabling method
  4361. *
  4362. * Test disabling the hidden input for radio buttons.
  4363. */
  4364. public function testRadioHiddenControlDisabling(): void
  4365. {
  4366. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4367. $expected = [
  4368. 'label' => ['for' => 'model-1-field-0'],
  4369. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4370. 'option A',
  4371. '/label',
  4372. ];
  4373. $this->assertHtml($expected, $result);
  4374. }
  4375. /**
  4376. * testRadioOutOfRange method
  4377. *
  4378. * Test radio element set generation.
  4379. */
  4380. public function testRadioOutOfRange(): void
  4381. {
  4382. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4383. $expected = [
  4384. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4385. 'label' => ['for' => 'model-field-v'],
  4386. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4387. 'value',
  4388. '/label',
  4389. ];
  4390. $this->assertHtml($expected, $result);
  4391. }
  4392. /**
  4393. * testSelect method
  4394. *
  4395. * Test select element generation.
  4396. */
  4397. public function testSelect(): void
  4398. {
  4399. $result = $this->Form->select('Model.field', []);
  4400. $expected = [
  4401. 'select' => ['name' => 'Model[field]'],
  4402. '/select',
  4403. ];
  4404. $this->assertHtml($expected, $result);
  4405. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4406. $this->Form->create();
  4407. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4408. $expected = [
  4409. 'select' => ['name' => 'Model[field]'],
  4410. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4411. 'good',
  4412. '/option',
  4413. ['option' => ['value' => 'other']],
  4414. 'bad',
  4415. '/option',
  4416. '/select',
  4417. ];
  4418. $this->assertHtml($expected, $result);
  4419. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4420. $this->assertHtml($expected, $result);
  4421. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4422. $this->Form->create();
  4423. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4424. $expected = [
  4425. 'select' => ['name' => 'Model[field]'],
  4426. ['option' => ['value' => 'value']],
  4427. 'good',
  4428. '/option',
  4429. ['option' => ['value' => 'other']],
  4430. 'bad',
  4431. '/option',
  4432. '/select',
  4433. ];
  4434. $this->assertHtml($expected, $result);
  4435. $options = [
  4436. ['value' => 'first', 'text' => 'First'],
  4437. ['value' => 'first', 'text' => 'Another First'],
  4438. ];
  4439. $result = $this->Form->select(
  4440. 'Model.field',
  4441. $options,
  4442. ['escape' => false, 'empty' => false]
  4443. );
  4444. $expected = [
  4445. 'select' => ['name' => 'Model[field]'],
  4446. ['option' => ['value' => 'first']],
  4447. 'First',
  4448. '/option',
  4449. ['option' => ['value' => 'first']],
  4450. 'Another First',
  4451. '/option',
  4452. '/select',
  4453. ];
  4454. $this->assertHtml($expected, $result);
  4455. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4456. $this->Form->create();
  4457. $result = $this->Form->select(
  4458. 'Model.contact_id',
  4459. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4460. ['escape' => false, 'empty' => 'pick something']
  4461. );
  4462. $expected = [
  4463. 'select' => ['name' => 'Model[contact_id]'],
  4464. ['option' => ['value' => '']], 'pick something', '/option',
  4465. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4466. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4467. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4468. '/select',
  4469. ];
  4470. $this->assertHtml($expected, $result);
  4471. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4472. $this->Form->create();
  4473. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4474. $expected = [
  4475. 'select' => ['name' => 'Model[field]'],
  4476. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4477. ['option' => ['value' => '1']], 'Yes', '/option',
  4478. '/select',
  4479. ];
  4480. $this->assertHtml($expected, $result);
  4481. }
  4482. /**
  4483. * testSelectEscapeHtml method
  4484. *
  4485. * Test that select() escapes HTML.
  4486. */
  4487. public function testSelectEscapeHtml(): void
  4488. {
  4489. $result = $this->Form->select(
  4490. 'Model.field',
  4491. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4492. ['empty' => false]
  4493. );
  4494. $expected = [
  4495. 'select' => ['name' => 'Model[field]'],
  4496. ['option' => ['value' => 'first']],
  4497. 'first &quot;html&quot; &lt;chars&gt;',
  4498. '/option',
  4499. ['option' => ['value' => 'second']],
  4500. 'value',
  4501. '/option',
  4502. '/select',
  4503. ];
  4504. $this->assertHtml($expected, $result);
  4505. $result = $this->Form->select(
  4506. 'Model.field',
  4507. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4508. ['escape' => false, 'empty' => false]
  4509. );
  4510. $expected = [
  4511. 'select' => ['name' => 'Model[field]'],
  4512. ['option' => ['value' => 'first']],
  4513. 'first "html" <chars>',
  4514. '/option',
  4515. ['option' => ['value' => 'second']],
  4516. 'value',
  4517. '/option',
  4518. '/select',
  4519. ];
  4520. $this->assertHtml($expected, $result);
  4521. }
  4522. /**
  4523. * testSelectRequired method
  4524. *
  4525. * Test select() with required and disabled attributes.
  4526. */
  4527. public function testSelectRequired(): void
  4528. {
  4529. $this->article['required'] = [
  4530. 'user_id' => true,
  4531. ];
  4532. $this->Form->create($this->article);
  4533. $result = $this->Form->select('user_id', ['option A']);
  4534. $expected = [
  4535. 'select' => [
  4536. 'name' => 'user_id',
  4537. 'required' => 'required',
  4538. ],
  4539. ['option' => ['value' => '0']], 'option A', '/option',
  4540. '/select',
  4541. ];
  4542. $this->assertHtml($expected, $result);
  4543. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4544. $expected = [
  4545. 'select' => [
  4546. 'name' => 'user_id',
  4547. 'disabled' => 'disabled',
  4548. ],
  4549. ['option' => ['value' => '0']], 'option A', '/option',
  4550. '/select',
  4551. ];
  4552. $this->assertHtml($expected, $result);
  4553. }
  4554. public function testSelectEmptyWithRequiredFalse(): void
  4555. {
  4556. $Articles = $this->getTableLocator()->get('Articles');
  4557. $validator = $Articles->getValidator('default');
  4558. $validator->allowEmptyString('user_id');
  4559. $Articles->setValidator('default', $validator);
  4560. $entity = $Articles->newEmptyEntity();
  4561. $this->Form->create($entity);
  4562. $result = $this->Form->select('user_id', ['option A']);
  4563. $expected = [
  4564. 'select' => [
  4565. 'name' => 'user_id',
  4566. ],
  4567. ['option' => ['value' => '']], '/option',
  4568. ['option' => ['value' => '0']], 'option A', '/option',
  4569. '/select',
  4570. ];
  4571. $this->assertHtml($expected, $result);
  4572. }
  4573. /**
  4574. * testNestedSelect method
  4575. *
  4576. * Test select element generation with optgroups.
  4577. */
  4578. public function testNestedSelect(): void
  4579. {
  4580. $result = $this->Form->select(
  4581. 'Model.field',
  4582. [1 => 'One', 2 => 'Two', 'Three' => [
  4583. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4584. ]],
  4585. ['empty' => false]
  4586. );
  4587. $expected = [
  4588. 'select' => ['name' => 'Model[field]'],
  4589. ['option' => ['value' => 1]],
  4590. 'One',
  4591. '/option',
  4592. ['option' => ['value' => 2]],
  4593. 'Two',
  4594. '/option',
  4595. ['optgroup' => ['label' => 'Three']],
  4596. ['option' => ['value' => 3]],
  4597. 'Three',
  4598. '/option',
  4599. ['option' => ['value' => 4]],
  4600. 'Four',
  4601. '/option',
  4602. ['option' => ['value' => 5]],
  4603. 'Five',
  4604. '/option',
  4605. '/optgroup',
  4606. '/select',
  4607. ];
  4608. $this->assertHtml($expected, $result);
  4609. }
  4610. /**
  4611. * testSelectMultiple method
  4612. *
  4613. * Test generation of multiple select elements.
  4614. */
  4615. public function testSelectMultiple(): void
  4616. {
  4617. $options = ['first', 'second', 'third'];
  4618. $result = $this->Form->select(
  4619. 'Model.multi_field',
  4620. $options,
  4621. ['form' => 'my-form', 'multiple' => true]
  4622. );
  4623. $expected = [
  4624. 'input' => [
  4625. 'type' => 'hidden',
  4626. 'name' => 'Model[multi_field]',
  4627. 'value' => '',
  4628. 'form' => 'my-form',
  4629. ],
  4630. 'select' => [
  4631. 'name' => 'Model[multi_field][]',
  4632. 'multiple' => 'multiple',
  4633. 'form' => 'my-form',
  4634. ],
  4635. ['option' => ['value' => '0']],
  4636. 'first',
  4637. '/option',
  4638. ['option' => ['value' => '1']],
  4639. 'second',
  4640. '/option',
  4641. ['option' => ['value' => '2']],
  4642. 'third',
  4643. '/option',
  4644. '/select',
  4645. ];
  4646. $this->assertHtml($expected, $result);
  4647. $result = $this->Form->select(
  4648. 'Model.multi_field',
  4649. $options,
  4650. ['multiple' => 'multiple', 'form' => 'my-form']
  4651. );
  4652. $this->assertHtml($expected, $result);
  4653. $result = $this->Form->select(
  4654. 'Model.multi_field',
  4655. $options,
  4656. ['form' => 'my-form', 'multiple' => false]
  4657. );
  4658. $this->assertStringNotContainsString('multiple', $result);
  4659. }
  4660. /**
  4661. * testCheckboxZeroValue method
  4662. *
  4663. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4664. */
  4665. public function testCheckboxZeroValue(): void
  4666. {
  4667. $result = $this->Form->control('User.get_spam', [
  4668. 'type' => 'checkbox',
  4669. 'value' => '0',
  4670. 'hiddenField' => '1',
  4671. ]);
  4672. $expected = [
  4673. 'div' => ['class' => 'input checkbox'],
  4674. 'label' => ['for' => 'user-get-spam'],
  4675. ['input' => [
  4676. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4677. 'value' => '1',
  4678. ]],
  4679. ['input' => [
  4680. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4681. 'value' => '0', 'id' => 'user-get-spam',
  4682. ]],
  4683. 'Get Spam',
  4684. '/label',
  4685. '/div',
  4686. ];
  4687. $this->assertHtml($expected, $result);
  4688. $result = $this->Form->control('User.get_spam', [
  4689. 'type' => 'checkbox',
  4690. 'value' => '0',
  4691. 'hiddenField' => '',
  4692. ]);
  4693. $expected = [
  4694. 'div' => ['class' => 'input checkbox'],
  4695. 'label' => ['for' => 'user-get-spam'],
  4696. ['input' => [
  4697. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4698. 'value' => '',
  4699. ]],
  4700. ['input' => [
  4701. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4702. 'value' => '0', 'id' => 'user-get-spam',
  4703. ]],
  4704. 'Get Spam',
  4705. '/label',
  4706. '/div',
  4707. ];
  4708. $this->assertHtml($expected, $result);
  4709. }
  4710. /**
  4711. * testHabtmSelectBox method
  4712. *
  4713. * Test generation of habtm select boxes.
  4714. */
  4715. public function testHabtmSelectBox(): void
  4716. {
  4717. $options = [
  4718. 1 => 'blue',
  4719. 2 => 'red',
  4720. 3 => 'green',
  4721. ];
  4722. $tags = [
  4723. new Entity(['id' => 1, 'name' => 'blue']),
  4724. new Entity(['id' => 3, 'name' => 'green']),
  4725. ];
  4726. $article = new Article(['tags' => $tags]);
  4727. $this->Form->create($article);
  4728. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4729. $expected = [
  4730. 'div' => ['class' => 'input select'],
  4731. 'label' => ['for' => 'tags-ids'],
  4732. 'Tags',
  4733. '/label',
  4734. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4735. 'select' => [
  4736. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4737. 'multiple' => 'multiple',
  4738. ],
  4739. ['option' => ['value' => '1', 'selected' => 'selected']],
  4740. 'blue',
  4741. '/option',
  4742. ['option' => ['value' => '2']],
  4743. 'red',
  4744. '/option',
  4745. ['option' => ['value' => '3', 'selected' => 'selected']],
  4746. 'green',
  4747. '/option',
  4748. '/select',
  4749. '/div',
  4750. ];
  4751. $this->assertHtml($expected, $result);
  4752. // make sure only 50 is selected, and not 50f5c0cf
  4753. $options = [
  4754. '1' => 'blue',
  4755. '50f5c0cf' => 'red',
  4756. '50' => 'green',
  4757. ];
  4758. $tags = [
  4759. new Entity(['id' => 1, 'name' => 'blue']),
  4760. new Entity(['id' => 50, 'name' => 'green']),
  4761. ];
  4762. $article = new Article(['tags' => $tags]);
  4763. $this->Form->create($article);
  4764. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4765. $expected = [
  4766. 'div' => ['class' => 'input select'],
  4767. 'label' => ['for' => 'tags-ids'],
  4768. 'Tags',
  4769. '/label',
  4770. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4771. 'select' => [
  4772. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4773. 'multiple' => 'multiple',
  4774. ],
  4775. ['option' => ['value' => '1', 'selected' => 'selected']],
  4776. 'blue',
  4777. '/option',
  4778. ['option' => ['value' => '50f5c0cf']],
  4779. 'red',
  4780. '/option',
  4781. ['option' => ['value' => '50', 'selected' => 'selected']],
  4782. 'green',
  4783. '/option',
  4784. '/select',
  4785. '/div',
  4786. ];
  4787. $this->assertHtml($expected, $result);
  4788. $spacecraft = [
  4789. 1 => 'Orion',
  4790. 2 => 'Helios',
  4791. ];
  4792. $this->View->set('spacecraft', $spacecraft);
  4793. $this->Form->create();
  4794. $result = $this->Form->control('spacecraft._ids');
  4795. $expected = [
  4796. 'div' => ['class' => 'input select'],
  4797. 'label' => ['for' => 'spacecraft-ids'],
  4798. 'Spacecraft',
  4799. '/label',
  4800. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4801. 'select' => [
  4802. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4803. 'multiple' => 'multiple',
  4804. ],
  4805. ['option' => ['value' => '1']],
  4806. 'Orion',
  4807. '/option',
  4808. ['option' => ['value' => '2']],
  4809. 'Helios',
  4810. '/option',
  4811. '/select',
  4812. '/div',
  4813. ];
  4814. $this->assertHtml($expected, $result);
  4815. }
  4816. /**
  4817. * testErrorsForBelongsToManySelect method
  4818. *
  4819. * Tests that errors for belongsToMany select fields are being
  4820. * picked up properly.
  4821. */
  4822. public function testErrorsForBelongsToManySelect(): void
  4823. {
  4824. $spacecraft = [
  4825. 1 => 'Orion',
  4826. 2 => 'Helios',
  4827. ];
  4828. $this->View->set('spacecraft', $spacecraft);
  4829. $article = new Article();
  4830. $article->setError('spacecraft', ['Invalid']);
  4831. $this->Form->create($article);
  4832. $result = $this->Form->control('spacecraft._ids');
  4833. $expected = [
  4834. ['div' => ['class' => 'input select error']],
  4835. 'label' => ['for' => 'spacecraft-ids'],
  4836. 'Spacecraft',
  4837. '/label',
  4838. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4839. 'select' => [
  4840. 'name' => 'spacecraft[_ids][]',
  4841. 'id' => 'spacecraft-ids',
  4842. 'multiple' => 'multiple',
  4843. ],
  4844. ['option' => ['value' => '1']],
  4845. 'Orion',
  4846. '/option',
  4847. ['option' => ['value' => '2']],
  4848. 'Helios',
  4849. '/option',
  4850. '/select',
  4851. ['div' => ['class' => 'error-message', 'id' => 'spacecraft-error']],
  4852. 'Invalid',
  4853. '/div',
  4854. '/div',
  4855. ];
  4856. $this->assertHtml($expected, $result);
  4857. }
  4858. /**
  4859. * testSelectMultipleCheckboxes method
  4860. *
  4861. * Test generation of multi select elements in checkbox format.
  4862. */
  4863. public function testSelectMultipleCheckboxes(): void
  4864. {
  4865. $result = $this->Form->select(
  4866. 'Model.multi_field',
  4867. ['first', 'second', 'third'],
  4868. ['multiple' => 'checkbox']
  4869. );
  4870. $expected = [
  4871. 'input' => [
  4872. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  4873. ],
  4874. ['div' => ['class' => 'checkbox']],
  4875. ['label' => ['for' => 'model-multi-field-0']],
  4876. ['input' => [
  4877. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4878. 'value' => '0', 'id' => 'model-multi-field-0',
  4879. ]],
  4880. 'first',
  4881. '/label',
  4882. '/div',
  4883. ['div' => ['class' => 'checkbox']],
  4884. ['label' => ['for' => 'model-multi-field-1']],
  4885. ['input' => [
  4886. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4887. 'value' => '1', 'id' => 'model-multi-field-1',
  4888. ]],
  4889. 'second',
  4890. '/label',
  4891. '/div',
  4892. ['div' => ['class' => 'checkbox']],
  4893. ['label' => ['for' => 'model-multi-field-2']],
  4894. ['input' => [
  4895. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4896. 'value' => '2', 'id' => 'model-multi-field-2',
  4897. ]],
  4898. 'third',
  4899. '/label',
  4900. '/div',
  4901. ];
  4902. $this->assertHtml($expected, $result);
  4903. $result = $this->Form->select(
  4904. 'Model.multi_field',
  4905. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4906. ['multiple' => 'checkbox']
  4907. );
  4908. $expected = [
  4909. 'input' => [
  4910. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  4911. ],
  4912. ['div' => ['class' => 'checkbox']],
  4913. ['label' => ['for' => 'model-multi-field-a+']],
  4914. ['input' => [
  4915. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4916. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  4917. ]],
  4918. 'first',
  4919. '/label',
  4920. '/div',
  4921. ['div' => ['class' => 'checkbox']],
  4922. ['label' => ['for' => 'model-multi-field-a++']],
  4923. ['input' => [
  4924. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4925. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  4926. ]],
  4927. 'second',
  4928. '/label',
  4929. '/div',
  4930. ['div' => ['class' => 'checkbox']],
  4931. ['label' => ['for' => 'model-multi-field-a+++']],
  4932. ['input' => [
  4933. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4934. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  4935. ]],
  4936. 'third',
  4937. '/label',
  4938. '/div',
  4939. ];
  4940. $this->assertHtml($expected, $result);
  4941. $result = $this->Form->select(
  4942. 'Model.multi_field',
  4943. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4944. ['multiple' => 'checkbox']
  4945. );
  4946. $expected = [
  4947. 'input' => [
  4948. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  4949. ],
  4950. ['div' => ['class' => 'checkbox']],
  4951. ['label' => ['for' => 'model-multi-field-a-b']],
  4952. ['input' => [
  4953. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4954. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  4955. ]],
  4956. 'first',
  4957. '/label',
  4958. '/div',
  4959. ['div' => ['class' => 'checkbox']],
  4960. ['label' => ['for' => 'model-multi-field-a-b1']],
  4961. ['input' => [
  4962. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4963. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  4964. ]],
  4965. 'second',
  4966. '/label',
  4967. '/div',
  4968. ['div' => ['class' => 'checkbox']],
  4969. ['label' => ['for' => 'model-multi-field-a-b2']],
  4970. ['input' => [
  4971. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4972. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  4973. ]],
  4974. 'third',
  4975. '/label',
  4976. '/div',
  4977. ];
  4978. $this->assertHtml($expected, $result);
  4979. }
  4980. /**
  4981. * testSelectMultipleCheckboxRequestData method
  4982. *
  4983. * Ensure that multiCheckbox reads from the request data.
  4984. */
  4985. public function testSelectMultipleCheckboxRequestData(): void
  4986. {
  4987. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  4988. $result = $this->Form->select(
  4989. 'Model.tags',
  4990. ['1' => 'first', 'Array' => 'Array'],
  4991. ['multiple' => 'checkbox']
  4992. );
  4993. $expected = [
  4994. 'input' => [
  4995. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '',
  4996. ],
  4997. ['div' => ['class' => 'checkbox']],
  4998. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  4999. ['input' => [
  5000. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5001. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5002. ]],
  5003. 'first',
  5004. '/label',
  5005. '/div',
  5006. ['div' => ['class' => 'checkbox']],
  5007. ['label' => ['for' => 'model-tags-array']],
  5008. ['input' => [
  5009. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5010. 'value' => 'Array', 'id' => 'model-tags-array',
  5011. ]],
  5012. 'Array',
  5013. '/label',
  5014. '/div',
  5015. ];
  5016. $this->assertHtml($expected, $result);
  5017. }
  5018. /**
  5019. * testSelectMultipleCheckboxSecurity method
  5020. *
  5021. * Checks the security hash array generated for multiple-input checkbox elements.
  5022. */
  5023. public function testSelectMultipleCheckboxSecurity(): void
  5024. {
  5025. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5026. $this->Form->create();
  5027. $this->Form->select(
  5028. 'Model.multi_field',
  5029. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5030. ['multiple' => 'checkbox']
  5031. );
  5032. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5033. $this->assertEquals(['Model.multi_field'], $fields);
  5034. $result = $this->Form->secure();
  5035. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  5036. $hash = urlencode($hash . ':');
  5037. $this->assertStringContainsString('"' . $hash . '"', $result);
  5038. }
  5039. /**
  5040. * testSelectMultipleSecureWithNoOptions method
  5041. *
  5042. * Multiple select elements should always be secured as they always participate
  5043. * in the POST data.
  5044. */
  5045. public function testSelectMultipleSecureWithNoOptions(): void
  5046. {
  5047. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5048. $this->Form->create();
  5049. $this->Form->select(
  5050. 'Model.select',
  5051. [],
  5052. ['multiple' => true]
  5053. );
  5054. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5055. $this->assertEquals(['Model.select'], $result);
  5056. }
  5057. /**
  5058. * testSelectNoSecureWithNoOptions method
  5059. *
  5060. * When a select box has no options it should not be added to the fields list
  5061. * as it always fail post validation.
  5062. */
  5063. public function testSelectNoSecureWithNoOptions(): void
  5064. {
  5065. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5066. $this->Form->create();
  5067. $this->Form->select(
  5068. 'Model.select',
  5069. []
  5070. );
  5071. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5072. $this->assertEquals([], $result);
  5073. $this->Form->select(
  5074. 'Model.user_id',
  5075. [],
  5076. ['empty' => true]
  5077. );
  5078. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5079. $this->assertEquals(['Model.user_id'], $result);
  5080. }
  5081. /**
  5082. * testControlMultipleCheckboxes method
  5083. *
  5084. * Test control() resulting in multi select elements being generated.
  5085. */
  5086. public function testControlMultipleCheckboxes(): void
  5087. {
  5088. $result = $this->Form->control('Model.multi_field', [
  5089. 'options' => ['first', 'second', 'third'],
  5090. 'multiple' => 'checkbox',
  5091. ]);
  5092. $expected = [
  5093. ['div' => ['class' => 'input select']],
  5094. ['label' => ['for' => 'model-multi-field']],
  5095. 'Multi Field',
  5096. '/label',
  5097. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5098. ['div' => ['class' => 'checkbox']],
  5099. ['label' => ['for' => 'model-multi-field-0']],
  5100. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5101. 'first',
  5102. '/label',
  5103. '/div',
  5104. ['div' => ['class' => 'checkbox']],
  5105. ['label' => ['for' => 'model-multi-field-1']],
  5106. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5107. 'second',
  5108. '/label',
  5109. '/div',
  5110. ['div' => ['class' => 'checkbox']],
  5111. ['label' => ['for' => 'model-multi-field-2']],
  5112. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5113. 'third',
  5114. '/label',
  5115. '/div',
  5116. '/div',
  5117. ];
  5118. $this->assertHtml($expected, $result);
  5119. $result = $this->Form->control('Model.multi_field', [
  5120. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5121. 'multiple' => 'checkbox',
  5122. ]);
  5123. $expected = [
  5124. ['div' => ['class' => 'input select']],
  5125. ['label' => ['for' => 'model-multi-field']],
  5126. 'Multi Field',
  5127. '/label',
  5128. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5129. ['div' => ['class' => 'checkbox']],
  5130. ['label' => ['for' => 'model-multi-field-a']],
  5131. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5132. 'first',
  5133. '/label',
  5134. '/div',
  5135. ['div' => ['class' => 'checkbox']],
  5136. ['label' => ['for' => 'model-multi-field-b']],
  5137. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5138. 'second',
  5139. '/label',
  5140. '/div',
  5141. ['div' => ['class' => 'checkbox']],
  5142. ['label' => ['for' => 'model-multi-field-c']],
  5143. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5144. 'third',
  5145. '/label',
  5146. '/div',
  5147. '/div',
  5148. ];
  5149. $this->assertHtml($expected, $result);
  5150. }
  5151. /**
  5152. * testSelectHiddenFieldOmission method
  5153. *
  5154. * Test that select() with 'hiddenField' => false omits the hidden field.
  5155. */
  5156. public function testSelectHiddenFieldOmission(): void
  5157. {
  5158. $result = $this->Form->select(
  5159. 'Model.multi_field',
  5160. ['first', 'second'],
  5161. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5162. );
  5163. $this->assertStringNotContainsString('type="hidden"', $result);
  5164. }
  5165. /**
  5166. * testSelectCheckboxMultipleOverrideName method
  5167. *
  5168. * Test that select() with multiple = checkbox works with overriding name attribute.
  5169. */
  5170. public function testSelectCheckboxMultipleOverrideName(): void
  5171. {
  5172. $result = $this->Form->select('category', ['1', '2'], [
  5173. 'multiple' => 'checkbox',
  5174. 'name' => 'fish',
  5175. ]);
  5176. $expected = [
  5177. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5178. ['div' => ['class' => 'checkbox']],
  5179. ['label' => ['for' => 'fish-0']],
  5180. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5181. '1',
  5182. '/label',
  5183. '/div',
  5184. ['div' => ['class' => 'checkbox']],
  5185. ['label' => ['for' => 'fish-1']],
  5186. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5187. '2',
  5188. '/label',
  5189. '/div',
  5190. ];
  5191. $this->assertHtml($expected, $result);
  5192. $result = $this->Form->multiCheckbox(
  5193. 'category',
  5194. new Collection(['1', '2']),
  5195. ['name' => 'fish']
  5196. );
  5197. $this->assertHtml($expected, $result);
  5198. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5199. 'name' => 'fish',
  5200. ]);
  5201. $this->assertHtml($expected, $result);
  5202. }
  5203. /**
  5204. * testControlMultiCheckbox method
  5205. *
  5206. * Test that control() works with multicheckbox.
  5207. */
  5208. public function testControlMultiCheckbox(): void
  5209. {
  5210. $result = $this->Form->control('category', [
  5211. 'type' => 'multicheckbox',
  5212. 'options' => ['1', '2'],
  5213. ]);
  5214. $expected = [
  5215. ['div' => ['class' => 'input multicheckbox']],
  5216. '<label',
  5217. 'Category',
  5218. '/label',
  5219. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5220. ['div' => ['class' => 'checkbox']],
  5221. ['label' => ['for' => 'category-0']],
  5222. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5223. '1',
  5224. '/label',
  5225. '/div',
  5226. ['div' => ['class' => 'checkbox']],
  5227. ['label' => ['for' => 'category-1']],
  5228. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5229. '2',
  5230. '/label',
  5231. '/div',
  5232. '/div',
  5233. ];
  5234. $this->assertHtml($expected, $result);
  5235. }
  5236. /**
  5237. * testCheckbox method
  5238. *
  5239. * Test generation of checkboxes.
  5240. */
  5241. public function testCheckbox(): void
  5242. {
  5243. $result = $this->Form->checkbox('Model.field');
  5244. $expected = [
  5245. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5246. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5247. ];
  5248. $this->assertHtml($expected, $result);
  5249. $result = $this->Form->checkbox('Model.field', [
  5250. 'id' => 'theID',
  5251. 'value' => 'myvalue',
  5252. 'form' => 'my-form',
  5253. ]);
  5254. $expected = [
  5255. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5256. ['input' => [
  5257. 'type' => 'checkbox', 'name' => 'Model[field]',
  5258. 'value' => 'myvalue', 'id' => 'theID',
  5259. 'form' => 'my-form',
  5260. ]],
  5261. ];
  5262. $this->assertHtml($expected, $result);
  5263. }
  5264. /**
  5265. * testCheckboxDefaultValue method
  5266. *
  5267. * Test default value setting on checkbox() method.
  5268. */
  5269. public function testCheckboxDefaultValue(): void
  5270. {
  5271. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5272. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5273. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5274. $this->assertHtml($expected, $result);
  5275. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5276. $this->Form->create();
  5277. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5278. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5279. $this->assertHtml($expected, $result);
  5280. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5281. $this->Form->create();
  5282. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5283. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5284. $this->assertHtml($expected, $result);
  5285. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5286. $this->Form->create();
  5287. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5288. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5289. $this->assertHtml($expected, $result);
  5290. $Articles = $this->getTableLocator()->get('Articles');
  5291. $Articles->getSchema()->addColumn(
  5292. 'published',
  5293. ['type' => 'boolean', 'null' => false, 'default' => true]
  5294. );
  5295. $this->Form->create($Articles->newEmptyEntity());
  5296. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5297. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5298. $this->assertHtml($expected, $result);
  5299. }
  5300. /**
  5301. * testCheckboxCheckedAndError method
  5302. *
  5303. * Test checkbox being checked or having errors.
  5304. */
  5305. public function testCheckboxCheckedAndError(): void
  5306. {
  5307. $this->article['errors'] = [
  5308. 'published' => true,
  5309. ];
  5310. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5311. $this->Form->create($this->article);
  5312. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5313. $expected = [
  5314. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5315. ['input' => [
  5316. 'type' => 'checkbox',
  5317. 'name' => 'published',
  5318. 'value' => 'myvalue',
  5319. 'id' => 'theID',
  5320. 'checked' => 'checked',
  5321. 'class' => 'form-error',
  5322. ]],
  5323. ];
  5324. $this->assertHtml($expected, $result);
  5325. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5326. $this->Form->create($this->article);
  5327. $result = $this->Form->checkbox('published');
  5328. $expected = [
  5329. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5330. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5331. ];
  5332. $this->assertHtml($expected, $result);
  5333. }
  5334. /**
  5335. * testCheckboxCustomNameAttribute method
  5336. *
  5337. * Test checkbox() with a custom name attribute.
  5338. */
  5339. public function testCheckboxCustomNameAttribute(): void
  5340. {
  5341. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5342. $expected = [
  5343. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5344. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5345. ];
  5346. $this->assertHtml($expected, $result);
  5347. }
  5348. /**
  5349. * testCheckboxHiddenField method
  5350. *
  5351. * Test that the hidden input for checkboxes can be omitted or set to a
  5352. * specific value.
  5353. */
  5354. public function testCheckboxHiddenField(): void
  5355. {
  5356. $result = $this->Form->checkbox('UserForm.something', [
  5357. 'hiddenField' => false,
  5358. ]);
  5359. $expected = [
  5360. 'input' => [
  5361. 'type' => 'checkbox',
  5362. 'name' => 'UserForm[something]',
  5363. 'value' => '1',
  5364. ],
  5365. ];
  5366. $this->assertHtml($expected, $result);
  5367. $result = $this->Form->checkbox('UserForm.something', [
  5368. 'value' => 'Y',
  5369. 'hiddenField' => '',
  5370. ]);
  5371. $expected = [
  5372. ['input' => [
  5373. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5374. 'value' => '',
  5375. ]],
  5376. ['input' => [
  5377. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5378. 'value' => 'Y',
  5379. ]],
  5380. ];
  5381. $this->assertHtml($expected, $result);
  5382. $result = $this->Form->checkbox('UserForm.something', [
  5383. 'value' => 'Y',
  5384. 'hiddenField' => 'N',
  5385. ]);
  5386. $expected = [
  5387. ['input' => [
  5388. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5389. 'value' => 'N',
  5390. ]],
  5391. ['input' => [
  5392. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5393. 'value' => 'Y',
  5394. ]],
  5395. ];
  5396. $this->assertHtml($expected, $result);
  5397. }
  5398. /**
  5399. * testTime method
  5400. *
  5401. * Test the time type.
  5402. */
  5403. public function testTime(): void
  5404. {
  5405. $result = $this->Form->time('start_time', [
  5406. 'value' => '2014-03-08 16:30:00',
  5407. ]);
  5408. $expected = [
  5409. 'input' => [
  5410. 'type' => 'time',
  5411. 'name' => 'start_time',
  5412. 'value' => '16:30:00',
  5413. 'step' => '1',
  5414. ],
  5415. ];
  5416. $this->assertHtml($expected, $result);
  5417. }
  5418. /**
  5419. * testDate method
  5420. *
  5421. * Test the date type.
  5422. */
  5423. public function testDate(): void
  5424. {
  5425. $result = $this->Form->date('start_day', [
  5426. 'value' => '2014-03-08',
  5427. ]);
  5428. $expected = [
  5429. 'input' => [
  5430. 'type' => 'date',
  5431. 'name' => 'start_day',
  5432. 'value' => '2014-03-08',
  5433. ],
  5434. ];
  5435. $this->assertHtml($expected, $result);
  5436. $result = $this->Form->date('start_day', [
  5437. 'value' => new Date('2014-03-08'),
  5438. ]);
  5439. $this->assertHtml($expected, $result);
  5440. }
  5441. /**
  5442. * testDateTime method
  5443. */
  5444. public function testDateTime(): void
  5445. {
  5446. $result = $this->Form->dateTime('date', ['default' => true]);
  5447. $expected = [
  5448. 'input' => [
  5449. 'type' => 'datetime-local',
  5450. 'name' => 'date',
  5451. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  5452. 'step' => '1',
  5453. ],
  5454. ];
  5455. $this->assertHtml($expected, $result);
  5456. }
  5457. /**
  5458. * testDateTimeSecured method
  5459. *
  5460. * Test that datetime fields are added to protected fields list.
  5461. */
  5462. public function testDateTimeSecured(): void
  5463. {
  5464. $this->View->setRequest(
  5465. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5466. );
  5467. $this->Form->create();
  5468. $this->Form->dateTime('date');
  5469. $expected = ['date'];
  5470. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5471. $this->assertEquals($expected, $result);
  5472. $this->Form->fields = [];
  5473. $this->Form->date('published');
  5474. $expected = ['date', 'published'];
  5475. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5476. $this->assertEquals($expected, $result);
  5477. }
  5478. /**
  5479. * testDateTimeSecuredDisabled method
  5480. *
  5481. * Test that datetime fields are added to protected fields list.
  5482. */
  5483. public function testDateTimeSecuredDisabled(): void
  5484. {
  5485. $this->View->setRequest(
  5486. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5487. );
  5488. $this->Form->create();
  5489. $this->Form->dateTime('date', ['secure' => false]);
  5490. $expected = [];
  5491. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5492. $this->assertEquals($expected, $result);
  5493. $this->Form->fields = [];
  5494. $this->Form->date('published', ['secure' => false]);
  5495. $expected = [];
  5496. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5497. $this->assertEquals($expected, $result);
  5498. }
  5499. /**
  5500. * testDatetimeWithDefault method
  5501. *
  5502. * Test that datetime() and default values work.
  5503. */
  5504. public function testDatetimeWithDefault(): void
  5505. {
  5506. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5507. $expected = [
  5508. 'input' => [
  5509. 'type' => 'datetime-local',
  5510. 'name' => 'updated',
  5511. 'value' => '2009-06-01T11:15:30',
  5512. 'step' => '1',
  5513. ],
  5514. ];
  5515. $this->assertHtml($expected, $result);
  5516. $result = $this->Form->dateTime('updated', [
  5517. 'default' => '2009-06-01 11:15:30',
  5518. ]);
  5519. $expected = [
  5520. 'input' => [
  5521. 'type' => 'datetime-local',
  5522. 'name' => 'updated',
  5523. 'value' => '2009-06-01T11:15:30',
  5524. 'step' => '1',
  5525. ],
  5526. ];
  5527. $this->assertHtml($expected, $result);
  5528. }
  5529. /**
  5530. * testMonth method
  5531. *
  5532. * Test generation of a month input.
  5533. */
  5534. public function testMonth(): void
  5535. {
  5536. $result = $this->Form->month('field', ['value' => '']);
  5537. $expected = [
  5538. 'input' => [
  5539. 'type' => 'month',
  5540. 'name' => 'field',
  5541. 'value' => '',
  5542. ],
  5543. ];
  5544. $this->assertHtml($expected, $result);
  5545. $this->View->setRequest(
  5546. $this->View->getRequest()->withData('release', '2050-02-10')
  5547. );
  5548. $this->Form->create();
  5549. $result = $this->Form->month('release');
  5550. $expected = [
  5551. 'input' => [
  5552. 'type' => 'month',
  5553. 'name' => 'release',
  5554. 'value' => '2050-02',
  5555. ],
  5556. ];
  5557. $this->assertHtml($expected, $result);
  5558. $this->View->setRequest(
  5559. $this->View->getRequest()->withData('release', '2050-03')
  5560. );
  5561. $this->Form->create();
  5562. $result = $this->Form->month('release');
  5563. $expected = [
  5564. 'input' => [
  5565. 'type' => 'month',
  5566. 'name' => 'release',
  5567. 'value' => '2050-03',
  5568. ],
  5569. ];
  5570. $this->assertHtml($expected, $result);
  5571. }
  5572. /**
  5573. * testYear method
  5574. *
  5575. * Test generation of a year input.
  5576. */
  5577. public function testYear(): void
  5578. {
  5579. $this->View->setRequest(
  5580. $this->View->getRequest()->withData('published', '2006')
  5581. );
  5582. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5583. $expected = [
  5584. ['select' => ['name' => 'field']],
  5585. ['option' => ['selected' => 'selected', 'value' => '']],
  5586. '/option',
  5587. ['option' => ['value' => '2007']],
  5588. '2007',
  5589. '/option',
  5590. ['option' => ['value' => '2006']],
  5591. '2006',
  5592. '/option',
  5593. '/select',
  5594. ];
  5595. $this->assertHtml($expected, $result);
  5596. $result = $this->Form->year('field', [
  5597. 'value' => '',
  5598. 'min' => 2006,
  5599. 'max' => 2007,
  5600. 'order' => 'asc',
  5601. ]);
  5602. $expected = [
  5603. ['select' => ['name' => 'field']],
  5604. ['option' => ['selected' => 'selected', 'value' => '']],
  5605. '/option',
  5606. ['option' => ['value' => '2006']],
  5607. '2006',
  5608. '/option',
  5609. ['option' => ['value' => '2007']],
  5610. '2007',
  5611. '/option',
  5612. '/select',
  5613. ];
  5614. $this->assertHtml($expected, $result);
  5615. $result = $this->Form->year('published', [
  5616. 'empty' => false,
  5617. 'min' => 2006,
  5618. 'max' => 2007,
  5619. ]);
  5620. $expected = [
  5621. ['select' => ['name' => 'published']],
  5622. ['option' => ['value' => '2007']],
  5623. '2007',
  5624. '/option',
  5625. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5626. '2006',
  5627. '/option',
  5628. '/select',
  5629. ];
  5630. $this->assertHtml($expected, $result);
  5631. $result = $this->Form->year('published', [
  5632. 'empty' => false,
  5633. 'value' => new Date('2008-01-12'),
  5634. 'min' => 2007,
  5635. 'max' => 2009,
  5636. ]);
  5637. $expected = [
  5638. ['select' => ['name' => 'published']],
  5639. ['option' => ['value' => '2009']],
  5640. '2009',
  5641. '/option',
  5642. ['option' => ['value' => '2008', 'selected' => 'selected']],
  5643. '2008',
  5644. '/option',
  5645. ['option' => ['value' => '2007']],
  5646. '2007',
  5647. '/option',
  5648. '/select',
  5649. ];
  5650. $this->assertHtml($expected, $result);
  5651. $result = $this->Form->year('published', [
  5652. 'empty' => 'Published on',
  5653. ]);
  5654. $this->assertStringContainsString('Published on', $result);
  5655. }
  5656. /**
  5657. * testControlYearPreEpoch method
  5658. *
  5659. * Test minYear being prior to the unix epoch.
  5660. */
  5661. public function testControlYearPreEpoch(): void
  5662. {
  5663. $start = date('Y') - 80;
  5664. $end = date('Y') - 18;
  5665. $result = $this->Form->control('birth_year', [
  5666. 'type' => 'year',
  5667. 'label' => 'Birth Year',
  5668. 'min' => $start,
  5669. 'max' => $end,
  5670. ]);
  5671. $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
  5672. $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
  5673. $this->assertStringNotContainsString('value="00">00', $result);
  5674. }
  5675. /**
  5676. * test control() datetime & required attributes
  5677. */
  5678. public function testControlDatetimeRequired(): void
  5679. {
  5680. $result = $this->Form->control('birthday', [
  5681. 'type' => 'date',
  5682. 'required' => true,
  5683. ]);
  5684. $this->assertStringContainsString(
  5685. '<input type="date" name="birthday" required="required"',
  5686. $result
  5687. );
  5688. }
  5689. /**
  5690. * testYearAutoExpandRange method
  5691. */
  5692. public function testYearAutoExpandRange(): void
  5693. {
  5694. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5695. $result = $this->Form->year('birthday');
  5696. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5697. $result = $matches[1];
  5698. $expected = range(date('Y') + 5, 1930);
  5699. $this->assertEquals($expected, $result);
  5700. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5701. $this->Form->create();
  5702. $result = $this->Form->year('release');
  5703. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5704. $result = $matches[1];
  5705. $expected = range(2050, date('Y') - 5);
  5706. $this->assertEquals($expected, $result);
  5707. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5708. $this->Form->create();
  5709. $result = $this->Form->year('release', [
  5710. 'min' => 1890,
  5711. 'max' => 1900,
  5712. ]);
  5713. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5714. $result = $matches[1];
  5715. $expected = range(1900, 1881);
  5716. $this->assertEquals($expected, $result);
  5717. }
  5718. /**
  5719. * test control placeholder + label
  5720. */
  5721. public function testControlLabelAndPlaceholder(): void
  5722. {
  5723. $this->Form->create($this->article);
  5724. $result = $this->Form->control('title', ['label' => 'Title', 'placeholder' => 'Add title']);
  5725. $expected = [
  5726. 'div' => ['class' => 'input text required'],
  5727. 'label' => ['for' => 'title'],
  5728. 'Title',
  5729. '/label',
  5730. 'input' => [
  5731. 'aria-required' => 'true',
  5732. 'type' => 'text',
  5733. 'required' => 'required',
  5734. 'placeholder' => 'Add title',
  5735. 'id' => 'title',
  5736. 'name' => 'title',
  5737. 'data-validity-message' => 'This field cannot be left empty',
  5738. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5739. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5740. ],
  5741. '/div',
  5742. ];
  5743. $this->assertHtml($expected, $result);
  5744. }
  5745. /**
  5746. * testControlLabelFalse method
  5747. *
  5748. * Test the label option being set to false.
  5749. */
  5750. public function testControlLabelFalse(): void
  5751. {
  5752. $this->Form->create($this->article);
  5753. $result = $this->Form->control('title', ['label' => false]);
  5754. $expected = [
  5755. 'div' => ['class' => 'input text required'],
  5756. 'input' => [
  5757. 'aria-required' => 'true',
  5758. 'type' => 'text',
  5759. 'required' => 'required',
  5760. 'id' => 'title',
  5761. 'name' => 'title',
  5762. 'data-validity-message' => 'This field cannot be left empty',
  5763. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5764. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5765. ],
  5766. '/div',
  5767. ];
  5768. $this->assertHtml($expected, $result);
  5769. $this->Form->create($this->article);
  5770. $result = $this->Form->control('title', ['label' => false, 'placeholder' => 'Add title']);
  5771. $expected['input'] += [
  5772. 'placeholder' => 'Add title',
  5773. 'aria-label' => 'Add title',
  5774. ];
  5775. $this->assertHtml($expected, $result);
  5776. }
  5777. /**
  5778. * testTextArea method
  5779. *
  5780. * Test generation of a textarea input.
  5781. */
  5782. public function testTextArea(): void
  5783. {
  5784. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5785. $result = $this->Form->textarea('field');
  5786. $expected = [
  5787. 'textarea' => ['name' => 'field', 'rows' => 5],
  5788. 'some test data',
  5789. '/textarea',
  5790. ];
  5791. $this->assertHtml($expected, $result);
  5792. $result = $this->Form->textarea('user.bio');
  5793. $expected = [
  5794. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5795. '/textarea',
  5796. ];
  5797. $this->assertHtml($expected, $result);
  5798. $this->View->setRequest($this->View->getRequest()
  5799. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5800. $this->Form->create();
  5801. $result = $this->Form->textarea('field');
  5802. $expected = [
  5803. 'textarea' => ['name' => 'field', 'rows' => 5],
  5804. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5805. '/textarea',
  5806. ];
  5807. $this->assertHtml($expected, $result);
  5808. $this->View->setRequest($this->View->getRequest()->withData(
  5809. 'Model.field',
  5810. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  5811. ));
  5812. $this->Form->create();
  5813. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5814. $expected = [
  5815. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  5816. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5817. '/textarea',
  5818. ];
  5819. $this->assertHtml($expected, $result);
  5820. $result = $this->Form->textarea('0.OtherModel.field');
  5821. $expected = [
  5822. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  5823. '/textarea',
  5824. ];
  5825. $this->assertHtml($expected, $result);
  5826. }
  5827. /**
  5828. * testTextAreaWithStupidCharacters method
  5829. *
  5830. * Test text area with non-ascii characters.
  5831. */
  5832. public function testTextAreaWithStupidCharacters(): void
  5833. {
  5834. $result = $this->Form->textarea('Post.content', [
  5835. 'value' => 'GREAT®',
  5836. 'rows' => '15',
  5837. 'cols' => '75',
  5838. ]);
  5839. $expected = [
  5840. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5841. 'GREAT®',
  5842. '/textarea',
  5843. ];
  5844. $this->assertHtml($expected, $result);
  5845. }
  5846. /**
  5847. * testTextAreaMaxLength method
  5848. *
  5849. * Test textareas maxlength read from schema.
  5850. */
  5851. public function testTextAreaMaxLength(): void
  5852. {
  5853. $this->Form->create([
  5854. 'schema' => [
  5855. 'stuff' => ['type' => 'string', 'length' => 10],
  5856. ],
  5857. ]);
  5858. $result = $this->Form->control('other', ['type' => 'textarea']);
  5859. $expected = [
  5860. 'div' => ['class' => 'input textarea'],
  5861. 'label' => ['for' => 'other'],
  5862. 'Other',
  5863. '/label',
  5864. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  5865. '/textarea',
  5866. '/div',
  5867. ];
  5868. $this->assertHtml($expected, $result);
  5869. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  5870. $expected = [
  5871. 'div' => ['class' => 'input textarea'],
  5872. 'label' => ['for' => 'stuff'],
  5873. 'Stuff',
  5874. '/label',
  5875. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  5876. '/textarea',
  5877. '/div',
  5878. ];
  5879. $this->assertHtml($expected, $result);
  5880. }
  5881. /**
  5882. * testHiddenField method
  5883. *
  5884. * Test generation of a hidden input.
  5885. */
  5886. public function testHidden(): void
  5887. {
  5888. $this->article['errors'] = [
  5889. 'field' => true,
  5890. ];
  5891. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  5892. $this->Form->create($this->article);
  5893. $result = $this->Form->hidden('field', ['id' => 'theID']);
  5894. $expected = [
  5895. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  5896. $this->assertHtml($expected, $result);
  5897. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5898. $expected = [
  5899. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  5900. ];
  5901. $this->assertHtml($expected, $result);
  5902. }
  5903. /**
  5904. * Test hidden() with various boolean values.
  5905. */
  5906. public function testHiddenBooleanValues(): void
  5907. {
  5908. $this->Form->create($this->article);
  5909. $result = $this->Form->hidden('field', ['value' => null]);
  5910. $expected = [
  5911. 'input' => ['type' => 'hidden', 'name' => 'field'],
  5912. ];
  5913. $this->assertHtml($expected, $result);
  5914. $result = $this->Form->hidden('field', ['value' => true]);
  5915. $expected = [
  5916. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '1'],
  5917. ];
  5918. $this->assertHtml($expected, $result);
  5919. $result = $this->Form->hidden('field', ['value' => false]);
  5920. $expected = [
  5921. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '0'],
  5922. ];
  5923. $this->assertHtml($expected, $result);
  5924. }
  5925. /**
  5926. * testFileUploadField method
  5927. *
  5928. * Test generation of a file upload input.
  5929. */
  5930. public function testFileUploadField(): void
  5931. {
  5932. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5933. $result = $this->Form->file('Model.upload');
  5934. $this->assertHtml($expected, $result);
  5935. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  5936. 'name' => '', 'type' => '', 'tmp_name' => '',
  5937. 'error' => 4, 'size' => 0,
  5938. ]));
  5939. $result = $this->Form->file('Model.upload');
  5940. $this->assertHtml($expected, $result);
  5941. $this->View->setRequest(
  5942. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  5943. );
  5944. $result = $this->Form->file('Model.upload');
  5945. $this->assertHtml($expected, $result);
  5946. }
  5947. /**
  5948. * testFileUploadOnOtherModel method
  5949. *
  5950. * Test File upload input on a model not used in create().
  5951. */
  5952. public function testFileUploadOnOtherModel(): void
  5953. {
  5954. $this->Form->create($this->article, ['type' => 'file']);
  5955. $result = $this->Form->file('ValidateProfile.city');
  5956. $expected = [
  5957. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  5958. ];
  5959. $this->assertHtml($expected, $result);
  5960. }
  5961. /**
  5962. * testButton method
  5963. *
  5964. * Test generation of a form button.
  5965. */
  5966. public function testButton(): void
  5967. {
  5968. $result = $this->Form->button('Hi');
  5969. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  5970. $this->assertHtml($expected, $result);
  5971. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'escapeTitle' => false]);
  5972. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  5973. $this->assertHtml($expected, $result);
  5974. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm', 'escapeTitle' => false]);
  5975. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  5976. $this->assertHtml($expected, $result);
  5977. $result = $this->Form->button('<Clear Form>', ['type' => 'reset']);
  5978. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  5979. $this->assertHtml($expected, $result);
  5980. $result = $this->Form->button('No type', ['type' => false]);
  5981. $expected = ['button' => [], 'No type', '/button'];
  5982. $this->assertHtml($expected, $result);
  5983. $result = $this->Form->button('Upload Text', [
  5984. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  5985. 'escape' => false,
  5986. ]);
  5987. $this->assertDoesNotMatchRegularExpression('/\&039/', $result);
  5988. }
  5989. /**
  5990. * testButtonUnlockedByDefault method
  5991. *
  5992. * Test that button() makes unlocked fields by default.
  5993. */
  5994. public function testButtonUnlockedByDefault(): void
  5995. {
  5996. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5997. $this->Form->create();
  5998. $this->Form->button('Save', ['name' => 'save']);
  5999. $this->Form->button('Clear');
  6000. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6001. $this->assertEquals(['save'], $result);
  6002. }
  6003. /**
  6004. * Test generation of a form button with confirm message.
  6005. */
  6006. public function testButtonWithConfirm(): void
  6007. {
  6008. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6009. $expected = ['button' => [
  6010. 'type' => 'submit',
  6011. 'data-confirm-message' => 'Confirm me!',
  6012. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6013. ], 'Hi', '/button'];
  6014. $this->assertHtml($expected, $result);
  6015. $result = $this->Form->button('Hi', ['escape' => false, 'confirm' => 'Confirm "me"!']);
  6016. $expected = ['button' => [
  6017. 'type' => 'submit',
  6018. 'data-confirm-message' => 'Confirm "me"!',
  6019. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6020. ], 'Hi', '/button'];
  6021. $this->assertHtml($expected, $result);
  6022. }
  6023. /**
  6024. * testPostButton method
  6025. */
  6026. public function testPostButton(): void
  6027. {
  6028. $result = $this->Form->postButton('Hi', '/controller/action');
  6029. $expected = [
  6030. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6031. 'button' => ['type' => 'submit'],
  6032. 'Hi',
  6033. '/button',
  6034. '/form',
  6035. ];
  6036. $this->assertHtml($expected, $result);
  6037. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6038. $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
  6039. }
  6040. /**
  6041. * testPostButtonMethodType method
  6042. */
  6043. public function testPostButtonMethodType(): void
  6044. {
  6045. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6046. $expected = [
  6047. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6048. 'div' => ['style' => 'display:none;'],
  6049. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6050. '/div',
  6051. 'button' => ['type' => 'submit'],
  6052. 'Hi',
  6053. '/button',
  6054. '/form',
  6055. ];
  6056. $this->assertHtml($expected, $result);
  6057. }
  6058. /**
  6059. * testPostButtonFormOptions method
  6060. */
  6061. public function testPostButtonFormOptions(): void
  6062. {
  6063. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6064. $expected = [
  6065. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6066. 'button' => ['type' => 'submit'],
  6067. 'Hi',
  6068. '/button',
  6069. '/form',
  6070. ];
  6071. $this->assertHtml($expected, $result);
  6072. }
  6073. /**
  6074. * testPostButtonNestedData method
  6075. *
  6076. * Test using postButton with N dimensional data.
  6077. */
  6078. public function testPostButtonNestedData(): void
  6079. {
  6080. $data = [
  6081. 'one' => [
  6082. 'two' => [
  6083. 3, 4, 5,
  6084. ],
  6085. ],
  6086. ];
  6087. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6088. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6089. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6090. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6091. }
  6092. /**
  6093. * testSecurePostButton method
  6094. *
  6095. * Test that postButton adds _Token fields.
  6096. */
  6097. public function testSecurePostButton(): void
  6098. {
  6099. $this->View->setRequest($this->View->getRequest()
  6100. ->withAttribute('csrfToken', 'testkey')
  6101. ->withAttribute('formTokenData', ['unlockedFields' => []]));
  6102. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6103. $tokenDebug = urlencode(json_encode([
  6104. '/posts/delete/1',
  6105. [],
  6106. [],
  6107. ]));
  6108. $expected = [
  6109. 'form' => [
  6110. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6111. ],
  6112. ['div' => ['style' => 'display:none;']],
  6113. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6114. '/div',
  6115. 'button' => ['type' => 'submit'],
  6116. 'Delete',
  6117. '/button',
  6118. ['div' => ['style' => 'display:none;']],
  6119. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6120. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6121. ['input' => [
  6122. 'type' => 'hidden',
  6123. 'name' => '_Token[debug]',
  6124. 'value' => $tokenDebug,
  6125. 'autocomplete' => 'off',
  6126. ]],
  6127. '/div',
  6128. '/form',
  6129. ];
  6130. $this->assertHtml($expected, $result);
  6131. }
  6132. /**
  6133. * testPostLink method
  6134. */
  6135. public function testPostLink(): void
  6136. {
  6137. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6138. $expected = [
  6139. 'form' => [
  6140. 'method' => 'post', 'action' => '/posts/delete/1',
  6141. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6142. ],
  6143. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6144. '/form',
  6145. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6146. 'Delete',
  6147. '/a',
  6148. ];
  6149. $this->assertHtml($expected, $result);
  6150. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6151. $expected = [
  6152. 'form' => [
  6153. 'method' => 'post', 'action' => '/posts/delete/1',
  6154. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6155. ],
  6156. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6157. '/form',
  6158. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6159. 'Delete',
  6160. '/a',
  6161. ];
  6162. $this->assertHtml($expected, $result);
  6163. $result = $this->Form->postLink(
  6164. 'Delete',
  6165. '/posts/delete/1',
  6166. ['target' => '_blank', 'class' => 'btn btn-danger']
  6167. );
  6168. $expected = [
  6169. 'form' => [
  6170. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6171. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6172. ],
  6173. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6174. '/form',
  6175. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6176. 'Delete',
  6177. '/a',
  6178. ];
  6179. $this->assertHtml($expected, $result);
  6180. }
  6181. /**
  6182. * testPostLinkWithConfirm method
  6183. *
  6184. * Test the confirm option for postLink().
  6185. */
  6186. public function testPostLinkWithConfirm(): void
  6187. {
  6188. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6189. $expected = [
  6190. 'form' => [
  6191. 'method' => 'post', 'action' => '/posts/delete/1',
  6192. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6193. ],
  6194. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6195. '/form',
  6196. 'a' => [
  6197. 'href' => '#',
  6198. 'data-confirm-message' => 'Confirm?',
  6199. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/',
  6200. ],
  6201. 'Delete',
  6202. '/a',
  6203. ];
  6204. $this->assertHtml($expected, $result);
  6205. $result = $this->Form->postLink(
  6206. 'Delete',
  6207. '/posts/delete/1',
  6208. ['confirm' => "'Confirm'\nthis \"deletion\"?"]
  6209. );
  6210. $expected = [
  6211. 'form' => [
  6212. 'method' => 'post', 'action' => '/posts/delete/1',
  6213. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6214. ],
  6215. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6216. '/form',
  6217. 'a' => [
  6218. 'href' => '#',
  6219. 'data-confirm-message' => "&#039;Confirm&#039;\nthis &quot;deletion&quot;?",
  6220. 'onclick' => "preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/",
  6221. ],
  6222. 'Delete',
  6223. '/a',
  6224. ];
  6225. $this->assertHtml($expected, $result);
  6226. $this->Form->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { $(\'form[name="{{formName}}"]\').submit();};']);
  6227. $result = $this->Form->postLink(
  6228. 'Delete',
  6229. '/posts/delete/1',
  6230. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  6231. );
  6232. $expected = [
  6233. 'form' => [
  6234. 'method' => 'post', 'action' => '/posts/delete/1',
  6235. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6236. ],
  6237. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6238. '/form',
  6239. 'a' => [
  6240. 'href' => '#',
  6241. 'data-confirm-message' => 'Confirm this deletion?',
  6242. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/',
  6243. ],
  6244. 'Delete',
  6245. '/a',
  6246. ];
  6247. $this->assertHtml($expected, $result);
  6248. }
  6249. /**
  6250. * testPostLinkWithQuery method
  6251. *
  6252. * Test postLink() with query string args.
  6253. */
  6254. public function testPostLinkWithQuery(): void
  6255. {
  6256. $result = $this->Form->postLink(
  6257. 'Delete',
  6258. ['controller' => 'Posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6259. );
  6260. $expected = [
  6261. 'form' => [
  6262. 'method' => 'post', 'action' => '/Posts/delete/1?a=b&amp;c=d',
  6263. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6264. ],
  6265. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6266. '/form',
  6267. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6268. 'Delete',
  6269. '/a',
  6270. ];
  6271. $this->assertHtml($expected, $result);
  6272. }
  6273. /**
  6274. * testPostLinkWithData method
  6275. *
  6276. * Test postLink with additional data.
  6277. */
  6278. public function testPostLinkWithData(): void
  6279. {
  6280. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6281. $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
  6282. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6283. $this->Form->create($entity);
  6284. $this->Form->end();
  6285. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6286. $this->assertStringContainsString(
  6287. '<input type="hidden" name="name" value="show"',
  6288. $result,
  6289. 'should not contain entity data.'
  6290. );
  6291. }
  6292. /**
  6293. * testPostLinkSecurityHash method
  6294. *
  6295. * Test that security hashes for postLink include the url.
  6296. */
  6297. public function testPostLinkSecurityHash(): void
  6298. {
  6299. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6300. $hash .= '%3Aid';
  6301. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6302. $result = $this->Form->postLink(
  6303. 'Delete',
  6304. '/posts/delete/1',
  6305. ['data' => ['id' => 1]]
  6306. );
  6307. $tokenDebug = urlencode(json_encode([
  6308. '/posts/delete/1',
  6309. [
  6310. 'id' => 1,
  6311. ],
  6312. [],
  6313. ]));
  6314. $expected = [
  6315. 'form' => [
  6316. 'method' => 'post', 'action' => '/posts/delete/1',
  6317. 'name', 'style' => 'display:none;',
  6318. ],
  6319. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6320. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6321. 'div' => ['style' => 'display:none;'],
  6322. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6323. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6324. ['input' => [
  6325. 'type' => 'hidden',
  6326. 'name' => '_Token[debug]',
  6327. 'value' => $tokenDebug,
  6328. 'autocomplete' => 'off',
  6329. ]],
  6330. '/div',
  6331. '/form',
  6332. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6333. 'Delete',
  6334. '/a',
  6335. ];
  6336. $this->assertHtml($expected, $result);
  6337. }
  6338. /**
  6339. * testPostLinkSecurityHashBlockMode method
  6340. *
  6341. * Test that postLink doesn't modify the fields in the containing form.
  6342. *
  6343. * postLink() calls inside open forms should not modify the field list
  6344. * for the form.
  6345. */
  6346. public function testPostLinkSecurityHashBlockMode(): void
  6347. {
  6348. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6349. $hash .= '%3A';
  6350. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6351. $this->Form->create(null, ['url' => ['action' => 'add']]);
  6352. $this->Form->control('title');
  6353. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6354. $result = $this->View->fetch('postLink');
  6355. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  6356. $this->assertEquals(['title'], $fields);
  6357. $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
  6358. $reflect = new ReflectionProperty($this->Form, '_lastAction');
  6359. $reflect->setAccessible(true);
  6360. $this->assertSame('/Articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
  6361. }
  6362. /**
  6363. * testPostLinkSecurityHashNoDebugMode method
  6364. *
  6365. * Test that security does not include debug token if debug is false.
  6366. */
  6367. public function testPostLinkSecurityHashNoDebugMode(): void
  6368. {
  6369. Configure::write('debug', false);
  6370. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6371. $hash .= '%3Aid';
  6372. $this->View->setRequest($this->View->getRequest()
  6373. ->withAttribute('formTokenData', ['key' => 'test']));
  6374. $result = $this->Form->postLink(
  6375. 'Delete',
  6376. '/posts/delete/1',
  6377. ['data' => ['id' => 1]]
  6378. );
  6379. $expected = [
  6380. 'form' => [
  6381. 'method' => 'post', 'action' => '/posts/delete/1',
  6382. 'name', 'style' => 'display:none;',
  6383. ],
  6384. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6385. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6386. 'div' => ['style' => 'display:none;'],
  6387. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6388. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6389. '/div',
  6390. '/form',
  6391. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6392. 'Delete',
  6393. '/a',
  6394. ];
  6395. $this->assertHtml($expected, $result);
  6396. }
  6397. /**
  6398. * testPostLinkNestedData method
  6399. *
  6400. * Test using postLink with N dimensional data.
  6401. */
  6402. public function testPostLinkNestedData(): void
  6403. {
  6404. $data = [
  6405. 'one' => [
  6406. 'two' => [
  6407. 3, 4, 5,
  6408. ],
  6409. ],
  6410. ];
  6411. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6412. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6413. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6414. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6415. }
  6416. /**
  6417. * testPostLinkAfterGetForm method
  6418. *
  6419. * Test creating postLinks after a GET form.
  6420. */
  6421. public function testPostLinkAfterGetForm(): void
  6422. {
  6423. $this->View->setRequest($this->View->getRequest()
  6424. ->withAttribute('csrfToken', 'testkey')
  6425. ->withAttribute('formTokenData', []));
  6426. $this->Form->create($this->article, ['type' => 'get']);
  6427. $this->Form->end();
  6428. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6429. $tokenDebug = urlencode(json_encode([
  6430. '/posts/delete/1',
  6431. [],
  6432. [],
  6433. ]));
  6434. $expected = [
  6435. 'form' => [
  6436. 'method' => 'post', 'action' => '/posts/delete/1',
  6437. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6438. ],
  6439. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6440. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6441. 'div' => ['style' => 'display:none;'],
  6442. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6443. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6444. ['input' => [
  6445. 'type' => 'hidden', 'name' => '_Token[debug]',
  6446. 'value' => $tokenDebug,
  6447. 'autocomplete' => 'off',
  6448. ]],
  6449. '/div',
  6450. '/form',
  6451. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6452. 'Delete',
  6453. '/a',
  6454. ];
  6455. $this->assertHtml($expected, $result);
  6456. }
  6457. /**
  6458. * testPostLinkFormBuffer method
  6459. *
  6460. * Test that postLink adds form tags to view block.
  6461. */
  6462. public function testPostLinkFormBuffer(): void
  6463. {
  6464. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6465. $expected = [
  6466. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6467. 'Delete',
  6468. '/a',
  6469. ];
  6470. $this->assertHtml($expected, $result);
  6471. $result = $this->View->fetch('postLink');
  6472. $expected = [
  6473. 'form' => [
  6474. 'method' => 'post', 'action' => '/posts/delete/1',
  6475. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6476. ],
  6477. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6478. '/form',
  6479. ];
  6480. $this->assertHtml($expected, $result);
  6481. $result = $this->Form->postLink(
  6482. 'Delete',
  6483. '/posts/delete/2',
  6484. ['block' => true, 'method' => 'DELETE']
  6485. );
  6486. $expected = [
  6487. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6488. 'Delete',
  6489. '/a',
  6490. ];
  6491. $this->assertHtml($expected, $result);
  6492. $result = $this->View->fetch('postLink');
  6493. $expected = [
  6494. 'form' => [
  6495. 'method' => 'post', 'action' => '/posts/delete/1',
  6496. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6497. ],
  6498. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6499. '/form',
  6500. [
  6501. 'form' => [
  6502. 'method' => 'post', 'action' => '/posts/delete/2',
  6503. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6504. ],
  6505. ],
  6506. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6507. '/form',
  6508. ];
  6509. $this->assertHtml($expected, $result);
  6510. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6511. $expected = [
  6512. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6513. 'Delete',
  6514. '/a',
  6515. ];
  6516. $this->assertHtml($expected, $result);
  6517. $result = $this->View->fetch('foobar');
  6518. $expected = [
  6519. 'form' => [
  6520. 'method' => 'post', 'action' => '/posts/delete/1',
  6521. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6522. ],
  6523. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6524. '/form',
  6525. ];
  6526. $this->assertHtml($expected, $result);
  6527. }
  6528. /**
  6529. * testSubmitButton method
  6530. */
  6531. public function testSubmitButton(): void
  6532. {
  6533. $result = $this->Form->submit('');
  6534. $expected = [
  6535. 'div' => ['class' => 'submit'],
  6536. 'input' => ['type' => 'submit', 'value' => ''],
  6537. '/div',
  6538. ];
  6539. $this->assertHtml($expected, $result);
  6540. $result = $this->Form->submit('Test Submit');
  6541. $expected = [
  6542. 'div' => ['class' => 'submit'],
  6543. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6544. '/div',
  6545. ];
  6546. $this->assertHtml($expected, $result);
  6547. $result = $this->Form->submit('Next >');
  6548. $expected = [
  6549. 'div' => ['class' => 'submit'],
  6550. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6551. '/div',
  6552. ];
  6553. $this->assertHtml($expected, $result);
  6554. $result = $this->Form->submit('Next >', ['escape' => false]);
  6555. $expected = [
  6556. 'div' => ['class' => 'submit'],
  6557. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6558. '/div',
  6559. ];
  6560. $this->assertHtml($expected, $result);
  6561. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6562. $expected = [
  6563. 'div' => ['class' => 'submit'],
  6564. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6565. '/div',
  6566. ];
  6567. $this->assertHtml($expected, $result);
  6568. }
  6569. /**
  6570. * testSubmitImage method
  6571. *
  6572. * Test image submit types.
  6573. */
  6574. public function testSubmitImage(): void
  6575. {
  6576. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6577. $expected = [
  6578. 'div' => ['class' => 'submit'],
  6579. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6580. '/div',
  6581. ];
  6582. $this->assertHtml($expected, $result);
  6583. $result = $this->Form->submit('/relative/cake.power.gif');
  6584. $expected = [
  6585. 'div' => ['class' => 'submit'],
  6586. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6587. '/div',
  6588. ];
  6589. $this->assertHtml($expected, $result);
  6590. $result = $this->Form->submit('cake.power.gif');
  6591. $expected = [
  6592. 'div' => ['class' => 'submit'],
  6593. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6594. '/div',
  6595. ];
  6596. $this->assertHtml($expected, $result);
  6597. $result = $this->Form->submit('Not.an.image');
  6598. $expected = [
  6599. 'div' => ['class' => 'submit'],
  6600. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6601. '/div',
  6602. ];
  6603. $this->assertHtml($expected, $result);
  6604. }
  6605. /**
  6606. * testSubmitUnlockedByDefault method
  6607. *
  6608. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6609. * be submitted at a time.
  6610. */
  6611. public function testSubmitUnlockedByDefault(): void
  6612. {
  6613. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6614. $this->Form->create();
  6615. $this->Form->submit('Go go');
  6616. $this->Form->submit('Save', ['name' => 'save']);
  6617. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6618. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6619. }
  6620. /**
  6621. * testSubmitImageTimestamp method
  6622. *
  6623. * Test submit image with timestamps.
  6624. */
  6625. public function testSubmitImageTimestamp(): void
  6626. {
  6627. Configure::write('Asset.timestamp', 'force');
  6628. $result = $this->Form->submit('cake.power.gif');
  6629. $expected = [
  6630. 'div' => ['class' => 'submit'],
  6631. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6632. '/div',
  6633. ];
  6634. $this->assertHtml($expected, $result);
  6635. }
  6636. /**
  6637. * testDateTimeWithGetForms method
  6638. *
  6639. * Test that datetime() works with GET style forms.
  6640. */
  6641. public function testDateTimeWithGetForms(): void
  6642. {
  6643. $this->Form->create($this->article, ['type' => 'get']);
  6644. $result = $this->Form->datetime('created');
  6645. $expected = [
  6646. 'input' => [
  6647. 'type' => 'datetime-local',
  6648. 'name' => 'created',
  6649. 'value' => '',
  6650. 'step' => '1',
  6651. ],
  6652. ];
  6653. $this->assertHtml($expected, $result);
  6654. $result = $this->Form->datetime('created', ['default' => true]);
  6655. $expected = [
  6656. 'input' => [
  6657. 'type' => 'datetime-local',
  6658. 'name' => 'created',
  6659. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  6660. 'step' => '1',
  6661. ],
  6662. ];
  6663. $this->assertHtml($expected, $result);
  6664. }
  6665. /**
  6666. * Provides fractional schema types
  6667. *
  6668. * @return array
  6669. */
  6670. public function fractionalTypeProvider(): array
  6671. {
  6672. return [
  6673. ['datetimefractional'],
  6674. ['timestampfractional'],
  6675. ['timestamptimezone'],
  6676. ];
  6677. }
  6678. /**
  6679. * testDateTimeWithFractional method
  6680. *
  6681. * Test that datetime() works with datetimefractional.
  6682. *
  6683. * @dataProvider fractionalTypeProvider
  6684. */
  6685. public function testDateTimeWithFractional(string $type): void
  6686. {
  6687. $this->Form->create([
  6688. 'schema' => [
  6689. 'created' => ['type' => $type],
  6690. ],
  6691. ]);
  6692. $result = $this->Form->datetime('created', [
  6693. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6694. ]);
  6695. $expected = [
  6696. 'input' => [
  6697. 'type' => 'datetime-local',
  6698. 'name' => 'created',
  6699. 'value' => '2019-09-27T02:52:43.123',
  6700. 'step' => '0.001',
  6701. ],
  6702. ];
  6703. $this->assertHtml($expected, $result);
  6704. }
  6705. /**
  6706. * testControlWithFractional method
  6707. *
  6708. * Test that control() works with datetimefractional.
  6709. *
  6710. * @dataProvider fractionalTypeProvider
  6711. */
  6712. public function testControlWithFractional(string $type): void
  6713. {
  6714. $this->Form->create([
  6715. 'schema' => [
  6716. 'created' => ['type' => $type],
  6717. ],
  6718. ]);
  6719. $result = $this->Form->control('created', [
  6720. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6721. ]);
  6722. $expected = [
  6723. 'div' => ['class' => 'input datetime'],
  6724. 'label' => ['for' => 'created'],
  6725. 'Created',
  6726. '/label',
  6727. 'input' => [
  6728. 'type' => 'datetime-local',
  6729. 'name' => 'created',
  6730. 'id' => 'created',
  6731. 'value' => '2019-09-27T02:52:43.123',
  6732. 'step' => '0.001',
  6733. ],
  6734. '/div',
  6735. ];
  6736. $this->assertHtml($expected, $result);
  6737. }
  6738. /**
  6739. * testForMagicControlNonExistentNotValidated method
  6740. */
  6741. public function testForMagicControlNonExistentNotValidated(): void
  6742. {
  6743. $this->Form->create($this->article);
  6744. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6745. $result = $this->Form->control('nonexistent_not_validated');
  6746. $expected = [
  6747. 'label' => ['for' => 'nonexistent-not-validated'],
  6748. 'Nonexistent Not Validated',
  6749. '/label',
  6750. 'input' => [
  6751. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6752. 'id' => 'nonexistent-not-validated',
  6753. ],
  6754. ];
  6755. $this->assertHtml($expected, $result);
  6756. $result = $this->Form->control('nonexistent_not_validated', [
  6757. 'val' => 'my value',
  6758. ]);
  6759. $expected = [
  6760. 'label' => ['for' => 'nonexistent-not-validated'],
  6761. 'Nonexistent Not Validated',
  6762. '/label',
  6763. 'input' => [
  6764. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6765. 'value' => 'my value', 'id' => 'nonexistent-not-validated',
  6766. ],
  6767. ];
  6768. $this->assertHtml($expected, $result);
  6769. $this->View->setRequest(
  6770. $this->View->getRequest()->withData('nonexistent_not_validated', 'CakePHP magic')
  6771. );
  6772. $this->Form->create($this->article);
  6773. $result = $this->Form->control('nonexistent_not_validated');
  6774. $expected = [
  6775. 'label' => ['for' => 'nonexistent-not-validated'],
  6776. 'Nonexistent Not Validated',
  6777. '/label',
  6778. 'input' => [
  6779. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6780. 'value' => 'CakePHP magic', 'id' => 'nonexistent-not-validated',
  6781. ],
  6782. ];
  6783. $this->assertHtml($expected, $result);
  6784. }
  6785. /**
  6786. * testFormMagicControlLabel method
  6787. */
  6788. public function testFormMagicControlLabel(): void
  6789. {
  6790. $this->getTableLocator()->get('Contacts', [
  6791. 'className' => ContactsTable::class,
  6792. ]);
  6793. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6794. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6795. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  6796. $expected = [
  6797. 'label' => ['for' => 'contacts-name'],
  6798. 'My label',
  6799. '/label',
  6800. 'input' => [
  6801. 'type' => 'text',
  6802. 'name' => 'Contacts[name]',
  6803. 'id' => 'contacts-name',
  6804. 'maxlength' => '255',
  6805. ],
  6806. ];
  6807. $this->assertHtml($expected, $result);
  6808. $result = $this->Form->control('name', [
  6809. 'label' => ['class' => 'mandatory'],
  6810. ]);
  6811. $expected = [
  6812. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6813. 'Name',
  6814. '/label',
  6815. 'input' => [
  6816. 'type' => 'text', 'name' => 'name',
  6817. 'id' => 'name', 'maxlength' => '255',
  6818. ],
  6819. ];
  6820. $this->assertHtml($expected, $result);
  6821. $result = $this->Form->control('name', [
  6822. 'div' => false,
  6823. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  6824. ]);
  6825. $expected = [
  6826. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6827. 'My label',
  6828. '/label',
  6829. 'input' => [
  6830. 'type' => 'text', 'name' => 'name',
  6831. 'id' => 'name', 'maxlength' => '255',
  6832. ],
  6833. ];
  6834. $this->assertHtml($expected, $result);
  6835. $result = $this->Form->control('Contact.name', [
  6836. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  6837. ]);
  6838. $expected = [
  6839. 'label' => ['for' => 'my_id'],
  6840. 'Name',
  6841. '/label',
  6842. 'input' => [
  6843. 'type' => 'text', 'name' => 'Contact[name]',
  6844. 'id' => 'my_id', 'maxlength' => '255',
  6845. ],
  6846. ];
  6847. $this->assertHtml($expected, $result);
  6848. $result = $this->Form->control('1.id');
  6849. $expected = ['input' => [
  6850. 'type' => 'hidden', 'name' => '1[id]',
  6851. 'id' => '1-id',
  6852. ]];
  6853. $this->assertHtml($expected, $result);
  6854. $result = $this->Form->control('1.name');
  6855. $expected = [
  6856. 'label' => ['for' => '1-name'],
  6857. 'Name',
  6858. '/label',
  6859. 'input' => [
  6860. 'type' => 'text', 'name' => '1[name]',
  6861. 'id' => '1-name', 'maxlength' => '255',
  6862. ],
  6863. ];
  6864. $this->assertHtml($expected, $result);
  6865. }
  6866. /**
  6867. * testFormEnd method
  6868. */
  6869. public function testFormEnd(): void
  6870. {
  6871. $this->assertSame('</form>', $this->Form->end());
  6872. }
  6873. /**
  6874. * testMultiRecordForm method
  6875. *
  6876. * Test the generation of fields for a multi record form.
  6877. */
  6878. public function testMultiRecordForm(): void
  6879. {
  6880. $articles = $this->getTableLocator()->get('Articles');
  6881. $articles->hasMany('Comments');
  6882. $comment = new Entity(['comment' => 'Value']);
  6883. $article = new Article(['comments' => [$comment]]);
  6884. $this->Form->create([$article]);
  6885. $result = $this->Form->control('0.comments.1.comment');
  6886. // phpcs:disable
  6887. $expected = [
  6888. 'div' => ['class' => 'input textarea'],
  6889. 'label' => ['for' => '0-comments-1-comment'],
  6890. 'Comment',
  6891. '/label',
  6892. 'textarea' => [
  6893. 'name',
  6894. 'id' => '0-comments-1-comment',
  6895. 'rows' => 5
  6896. ],
  6897. '/textarea',
  6898. '/div'
  6899. ];
  6900. // phpcs:enable
  6901. $this->assertHtml($expected, $result);
  6902. $result = $this->Form->control('0.comments.0.comment');
  6903. // phpcs:disable
  6904. $expected = [
  6905. 'div' => ['class' => 'input textarea'],
  6906. 'label' => ['for' => '0-comments-0-comment'],
  6907. 'Comment',
  6908. '/label',
  6909. 'textarea' => [
  6910. 'name',
  6911. 'id' => '0-comments-0-comment',
  6912. 'rows' => 5
  6913. ],
  6914. 'Value',
  6915. '/textarea',
  6916. '/div'
  6917. ];
  6918. // phpcs:enable
  6919. $this->assertHtml($expected, $result);
  6920. $comment->setError('comment', ['Not valid']);
  6921. $result = $this->Form->control('0.comments.0.comment');
  6922. // phpcs:disable
  6923. $expected = [
  6924. 'div' => ['class' => 'input textarea error'],
  6925. 'label' => ['for' => '0-comments-0-comment'],
  6926. 'Comment',
  6927. '/label',
  6928. 'textarea' => [
  6929. 'name',
  6930. 'class' => 'form-error',
  6931. 'id' => '0-comments-0-comment',
  6932. 'aria-invalid' => 'true',
  6933. 'aria-describedby' => '0-comments-0-comment-error',
  6934. 'rows' => 5
  6935. ],
  6936. 'Value',
  6937. '/textarea',
  6938. ['div' => ['class' => 'error-message', 'id' => '0-comments-0-comment-error']],
  6939. 'Not valid',
  6940. '/div',
  6941. '/div'
  6942. ];
  6943. // phpcs:enable
  6944. $this->assertHtml($expected, $result);
  6945. $this->getTableLocator()->get('Comments')
  6946. ->getValidator('default')
  6947. ->allowEmptyString('comment', null, false);
  6948. $result = $this->Form->control('0.comments.1.comment');
  6949. // phpcs:disable
  6950. $expected = [
  6951. 'div' => ['class' => 'input textarea required'],
  6952. 'label' => ['for' => '0-comments-1-comment'],
  6953. 'Comment',
  6954. '/label',
  6955. 'textarea' => [
  6956. 'name',
  6957. 'aria-required' => 'true',
  6958. 'required' => 'required',
  6959. 'id' => '0-comments-1-comment',
  6960. 'rows' => 5,
  6961. 'data-validity-message' => 'This field cannot be left empty',
  6962. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  6963. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  6964. ],
  6965. '/textarea',
  6966. '/div'
  6967. ];
  6968. // phpcs:enable
  6969. $this->assertHtml($expected, $result);
  6970. }
  6971. /**
  6972. * testHtml5Controls method
  6973. *
  6974. * Test that some html5 inputs + FormHelper::__call() work.
  6975. */
  6976. public function testHtml5Controls(): void
  6977. {
  6978. $result = $this->Form->email('User.email');
  6979. $expected = [
  6980. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  6981. ];
  6982. $this->assertHtml($expected, $result);
  6983. $result = $this->Form->search('User.query');
  6984. $expected = [
  6985. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  6986. ];
  6987. $this->assertHtml($expected, $result);
  6988. $result = $this->Form->search('User.query', ['value' => 'test']);
  6989. $expected = [
  6990. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  6991. ];
  6992. $this->assertHtml($expected, $result);
  6993. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  6994. $expected = [
  6995. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  6996. ];
  6997. $this->assertHtml($expected, $result);
  6998. }
  6999. /**
  7000. * testHtml5ControlWithControl method
  7001. *
  7002. * Test accessing html5 inputs through control().
  7003. */
  7004. public function testHtml5ControlWithControl(): void
  7005. {
  7006. $this->Form->create();
  7007. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7008. $result = $this->Form->control('website', [
  7009. 'type' => 'url',
  7010. 'val' => 'http://domain.tld',
  7011. 'label' => false,
  7012. ]);
  7013. $expected = [
  7014. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7015. ];
  7016. $this->assertHtml($expected, $result);
  7017. }
  7018. /**
  7019. * testHtml5ControlException method
  7020. *
  7021. * Test errors when field name is missing.
  7022. */
  7023. public function testHtml5ControlException(): void
  7024. {
  7025. $this->expectException(CakeException::class);
  7026. $this->Form->email();
  7027. }
  7028. /**
  7029. * tests fields that are required use custom validation messages
  7030. */
  7031. public function testHtml5ErrorMessage(): void
  7032. {
  7033. $this->Form->setConfig('autoSetCustomValidity', true);
  7034. $validator = (new Validator())
  7035. ->notEmptyString('email', 'Custom error message')
  7036. ->requirePresence('password')
  7037. ->alphaNumeric('password')
  7038. ->notBlank('phone');
  7039. $table = $this->getTableLocator()->get('Contacts', [
  7040. 'className' => ContactsTable::class,
  7041. ]);
  7042. $table->setValidator('default', $validator);
  7043. $contact = new Entity();
  7044. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7045. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7046. $result = $this->Form->control('password');
  7047. $expected = [
  7048. 'label' => ['for' => 'password'],
  7049. 'Password',
  7050. '/label',
  7051. 'input' => [
  7052. 'aria-required' => 'true',
  7053. 'id' => 'password',
  7054. 'name' => 'password',
  7055. 'type' => 'password',
  7056. 'value' => '',
  7057. 'required' => 'required',
  7058. 'data-validity-message' => 'This field cannot be left empty',
  7059. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7060. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7061. ],
  7062. ];
  7063. $this->assertHtml($expected, $result);
  7064. $result = $this->Form->control('phone');
  7065. $expected = [
  7066. 'label' => ['for' => 'phone'],
  7067. 'Phone',
  7068. '/label',
  7069. 'input' => [
  7070. 'aria-required' => 'true',
  7071. 'id' => 'phone',
  7072. 'name' => 'phone',
  7073. 'type' => 'tel',
  7074. 'value' => '',
  7075. 'maxlength' => 255,
  7076. 'required' => 'required',
  7077. 'data-validity-message' => 'This field cannot be left empty',
  7078. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7079. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7080. ],
  7081. ];
  7082. $this->assertHtml($expected, $result);
  7083. $result = $this->Form->control('email');
  7084. $expected = [
  7085. 'label' => ['for' => 'email'],
  7086. 'Email',
  7087. '/label',
  7088. 'input' => [
  7089. 'aria-required' => 'true',
  7090. 'id' => 'email',
  7091. 'name' => 'email',
  7092. 'type' => 'email',
  7093. 'value' => '',
  7094. 'maxlength' => 255,
  7095. 'required' => 'required',
  7096. 'data-validity-message' => 'Custom error message',
  7097. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7098. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7099. ],
  7100. ];
  7101. $this->assertHtml($expected, $result);
  7102. }
  7103. /**
  7104. * tests that custom validation messages are in templateVars
  7105. */
  7106. public function testHtml5ErrorMessageInTemplateVars(): void
  7107. {
  7108. $validator = (new Validator())
  7109. ->notEmptyString('email', 'Custom error "message" & entities')
  7110. ->requirePresence('password')
  7111. ->alphaNumeric('password')
  7112. ->notBlank('phone');
  7113. $table = $this->getTableLocator()->get('Contacts', [
  7114. 'className' => ContactsTable::class,
  7115. ]);
  7116. $table->setValidator('default', $validator);
  7117. $contact = new Entity();
  7118. $this->Form->setConfig('autoSetCustomValidity', false);
  7119. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7120. $this->Form->setTemplates([
  7121. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7122. 'inputContainer' => '{{content}}',
  7123. ]);
  7124. $result = $this->Form->control('password');
  7125. $expected = [
  7126. 'label' => ['for' => 'password'],
  7127. 'Password',
  7128. '/label',
  7129. 'input' => [
  7130. 'aria-required' => 'true',
  7131. 'id' => 'password',
  7132. 'name' => 'password',
  7133. 'type' => 'password',
  7134. 'value' => '',
  7135. 'required' => 'required',
  7136. 'data-message' => 'This field cannot be left empty',
  7137. ],
  7138. ];
  7139. $this->assertHtml($expected, $result);
  7140. $result = $this->Form->control('phone');
  7141. $expected = [
  7142. 'label' => ['for' => 'phone'],
  7143. 'Phone',
  7144. '/label',
  7145. 'input' => [
  7146. 'aria-required' => 'true',
  7147. 'id' => 'phone',
  7148. 'name' => 'phone',
  7149. 'type' => 'tel',
  7150. 'value' => '',
  7151. 'maxlength' => 255,
  7152. 'required' => 'required',
  7153. 'data-message' => 'This field cannot be left empty',
  7154. ],
  7155. ];
  7156. $this->assertHtml($expected, $result);
  7157. $result = $this->Form->control('email', [
  7158. 'templateVars' => [
  7159. 'custom' => 'data-custom="1"',
  7160. ],
  7161. ]);
  7162. $expected = [
  7163. 'label' => ['for' => 'email'],
  7164. 'Email',
  7165. '/label',
  7166. 'input' => [
  7167. 'aria-required' => 'true',
  7168. 'id' => 'email',
  7169. 'name' => 'email',
  7170. 'type' => 'email',
  7171. 'value' => '',
  7172. 'maxlength' => 255,
  7173. 'required' => 'required',
  7174. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7175. 'data-custom' => '1',
  7176. ],
  7177. ];
  7178. $this->assertHtml($expected, $result);
  7179. $this->Form->setConfig('autoSetCustomValidity', true);
  7180. }
  7181. /**
  7182. * testRequiredAttribute method
  7183. *
  7184. * Tests that formhelper sets required attributes.
  7185. */
  7186. public function testRequiredAttribute(): void
  7187. {
  7188. $this->article['required'] = [
  7189. 'title' => true,
  7190. 'body' => false,
  7191. ];
  7192. $this->Form->create($this->article);
  7193. $result = $this->Form->control('title');
  7194. $expected = [
  7195. 'div' => ['class' => 'input text required'],
  7196. 'label' => ['for' => 'title'],
  7197. 'Title',
  7198. '/label',
  7199. 'input' => [
  7200. 'type' => 'text',
  7201. 'name' => 'title',
  7202. 'id' => 'title',
  7203. 'aria-required' => 'true',
  7204. 'required' => 'required',
  7205. 'data-validity-message' => 'This field cannot be left empty',
  7206. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7207. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7208. ],
  7209. '/div',
  7210. ];
  7211. $this->assertHtml($expected, $result);
  7212. $result = $this->Form->control('title', ['required' => false]);
  7213. $this->assertStringNotContainsString('required', $result);
  7214. $result = $this->Form->control('body');
  7215. $expected = [
  7216. 'div' => ['class' => 'input text'],
  7217. 'label' => ['for' => 'body'],
  7218. 'Body',
  7219. '/label',
  7220. 'input' => [
  7221. 'type' => 'text',
  7222. 'name' => 'body',
  7223. 'id' => 'body',
  7224. ],
  7225. '/div',
  7226. ];
  7227. $this->assertHtml($expected, $result);
  7228. $result = $this->Form->control('body', ['required' => true]);
  7229. $this->assertStringContainsString('required', $result);
  7230. }
  7231. /**
  7232. * testControlsNotNested method
  7233. *
  7234. * Tests that it is possible to put inputs outside of the label.
  7235. */
  7236. public function testControlsNotNested(): void
  7237. {
  7238. $this->Form->setTemplates([
  7239. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7240. 'formGroup' => '{{input}}{{label}}',
  7241. ]);
  7242. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7243. $expected = [
  7244. 'div' => ['class' => 'input checkbox'],
  7245. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7246. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7247. 'label' => ['for' => 'foo'],
  7248. 'Foo',
  7249. '/label',
  7250. '/div',
  7251. ];
  7252. $this->assertHtml($expected, $result);
  7253. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7254. $expected = [
  7255. 'div' => ['class' => 'input checkbox'],
  7256. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7257. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7258. '/div',
  7259. ];
  7260. $this->assertHtml($expected, $result);
  7261. $result = $this->Form->control('confirm', [
  7262. 'type' => 'radio',
  7263. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7264. ]);
  7265. $expected = [
  7266. 'div' => ['class' => 'input radio'],
  7267. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7268. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7269. ['label' => ['for' => 'confirm-y']],
  7270. 'Yes',
  7271. '/label',
  7272. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7273. ['label' => ['for' => 'confirm-n']],
  7274. 'No',
  7275. '/label',
  7276. '<label',
  7277. 'Confirm',
  7278. '/label',
  7279. '/div',
  7280. ];
  7281. $this->assertHtml($expected, $result);
  7282. $result = $this->Form->select('category', ['1', '2'], [
  7283. 'multiple' => 'checkbox',
  7284. 'name' => 'fish',
  7285. ]);
  7286. $expected = [
  7287. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7288. ['div' => ['class' => 'checkbox']],
  7289. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7290. ['label' => ['for' => 'fish-0']],
  7291. '1',
  7292. '/label',
  7293. '/div',
  7294. ['div' => ['class' => 'checkbox']],
  7295. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7296. ['label' => ['for' => 'fish-1']],
  7297. '2',
  7298. '/label',
  7299. '/div',
  7300. ];
  7301. $this->assertHtml($expected, $result);
  7302. }
  7303. /**
  7304. * testControlContainerTemplates method
  7305. *
  7306. * Test that *Container templates are used by input.
  7307. */
  7308. public function testControlContainerTemplates(): void
  7309. {
  7310. $this->Form->setTemplates([
  7311. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7312. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7313. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7314. ]);
  7315. $this->article['errors'] = [
  7316. 'Article' => ['published' => 'error message'],
  7317. ];
  7318. $this->Form->create($this->article);
  7319. $result = $this->Form->control('accept', [
  7320. 'type' => 'checkbox',
  7321. ]);
  7322. $expected = [
  7323. 'div' => ['class' => 'check'],
  7324. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7325. 'label' => ['for' => 'accept'],
  7326. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7327. 'Accept',
  7328. '/label',
  7329. '/div',
  7330. ];
  7331. $this->assertHtml($expected, $result);
  7332. $result = $this->Form->control('accept', [
  7333. 'type' => 'radio',
  7334. 'options' => ['Y', 'N'],
  7335. ]);
  7336. $this->assertStringContainsString('<div class="rad">', $result);
  7337. $result = $this->Form->control('Article.published', [
  7338. 'type' => 'radio',
  7339. 'options' => ['Y', 'N'],
  7340. ]);
  7341. $this->assertStringContainsString('<div class="rad err">', $result);
  7342. }
  7343. /**
  7344. * testFormGroupTemplates method
  7345. *
  7346. * Test that *Container templates are used by input.
  7347. */
  7348. public function testFormGroupTemplates(): void
  7349. {
  7350. $this->Form->setTemplates([
  7351. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7352. ]);
  7353. $this->Form->create($this->article);
  7354. $result = $this->Form->control('accept', [
  7355. 'type' => 'radio',
  7356. 'options' => ['Y', 'N'],
  7357. ]);
  7358. $this->assertStringContainsString('<div class="radio">', $result);
  7359. }
  7360. /**
  7361. * testResetTemplates method
  7362. *
  7363. * Test resetting templates.
  7364. */
  7365. public function testResetTemplates(): void
  7366. {
  7367. $this->Form->setTemplates(['input' => '<input/>']);
  7368. $this->assertSame('<input/>', $this->Form->templater()->get('input'));
  7369. $this->Form->resetTemplates();
  7370. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7371. }
  7372. /**
  7373. * testContext method
  7374. *
  7375. * Test the context method.
  7376. */
  7377. public function testContext(): void
  7378. {
  7379. $result = $this->Form->context();
  7380. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7381. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7382. $this->assertSame($mock, $this->Form->context($mock));
  7383. $this->assertSame($mock, $this->Form->context());
  7384. }
  7385. /**
  7386. * testAutoDomId method
  7387. */
  7388. public function testAutoDomId(): void
  7389. {
  7390. $result = $this->Form->text('field', ['id' => true]);
  7391. $expected = [
  7392. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7393. ];
  7394. $this->assertHtml($expected, $result);
  7395. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7396. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7397. $expected = [
  7398. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7399. ['label' => ['for' => 'field-0']],
  7400. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7401. 'option A',
  7402. '/label',
  7403. ['label' => ['for' => 'field-1']],
  7404. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7405. 'option B',
  7406. '/label',
  7407. ];
  7408. $this->assertHtml($expected, $result);
  7409. $result = $this->Form->select(
  7410. 'multi_field',
  7411. ['first', 'second'],
  7412. ['multiple' => 'checkbox', 'id' => true]
  7413. );
  7414. $expected = [
  7415. 'input' => [
  7416. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '',
  7417. ],
  7418. ['div' => ['class' => 'checkbox']],
  7419. ['label' => ['for' => 'multi-field-0']],
  7420. ['input' => [
  7421. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7422. 'value' => '0', 'id' => 'multi-field-0',
  7423. ]],
  7424. 'first',
  7425. '/label',
  7426. '/div',
  7427. ['div' => ['class' => 'checkbox']],
  7428. ['label' => ['for' => 'multi-field-1']],
  7429. ['input' => [
  7430. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7431. 'value' => '1', 'id' => 'multi-field-1',
  7432. ]],
  7433. 'second',
  7434. '/label',
  7435. '/div',
  7436. ];
  7437. $this->assertHtml($expected, $result);
  7438. }
  7439. /**
  7440. * Test the basic setters and getters for value sources
  7441. */
  7442. public function testFormValueSourcesSettersGetters(): void
  7443. {
  7444. $this->View->setRequest($this->View->getRequest()
  7445. ->withData('id', '1')
  7446. ->withQueryParams(['id' => '2']));
  7447. $expected = ['data', 'context'];
  7448. $result = $this->Form->getValueSources();
  7449. $this->assertEquals($expected, $result);
  7450. $this->Form->setValueSources(['context']);
  7451. $result = $this->Form->getSourceValue('id');
  7452. $this->assertNull($result);
  7453. $this->Form->setValueSources('query');
  7454. $expected = ['query'];
  7455. $result = $this->Form->getValueSources();
  7456. $this->assertEquals($expected, $result);
  7457. $expected = '2';
  7458. $result = $this->Form->getSourceValue('id');
  7459. $this->assertSame($expected, $result);
  7460. $this->Form->setValueSources(['data']);
  7461. $expected = '1';
  7462. $result = $this->Form->getSourceValue('id');
  7463. $this->assertSame($expected, $result);
  7464. $this->Form->setValueSources(['query', 'data']);
  7465. $expected = '2';
  7466. $result = $this->Form->getSourceValue('id');
  7467. $this->assertSame($expected, $result);
  7468. }
  7469. public function testValueSourcesValidation(): void
  7470. {
  7471. $this->expectException(InvalidArgumentException::class);
  7472. $this->expectExceptionMessage('Invalid value source(s): invalid, foo. Valid values are: context, data, query');
  7473. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7474. }
  7475. /**
  7476. * Tests the different input rendering values based on sources values switching
  7477. */
  7478. public function testFormValueSourcesSingleSwitchRendering(): void
  7479. {
  7480. $articles = $this->getTableLocator()->get('Articles');
  7481. $article = new Article();
  7482. $articles->patchEntity($article, ['id' => '3']);
  7483. $this->Form->create($article);
  7484. $this->Form->setValueSources(['context']);
  7485. $result = $this->Form->control('id');
  7486. $expected = [
  7487. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7488. ];
  7489. $this->assertHtml($expected, $result);
  7490. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7491. $this->Form->setValueSources(['query']);
  7492. $result = $this->Form->control('id');
  7493. $expected = [
  7494. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7495. ];
  7496. $this->assertHtml($expected, $result);
  7497. $this->View->setRequest($this->View->getRequest()
  7498. ->withQueryParams(['id' => '5a'])
  7499. ->withData('id', '5b'));
  7500. $this->Form->setValueSources(['context']);
  7501. $this->Form->create($article);
  7502. $result = $this->Form->control('id');
  7503. $expected = [
  7504. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7505. ];
  7506. $this->assertHtml($expected, $result);
  7507. $this->Form->setValueSources(['data']);
  7508. $this->Form->create($article);
  7509. $result = $this->Form->control('id');
  7510. $expected = [
  7511. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7512. ];
  7513. $this->assertHtml($expected, $result);
  7514. $this->Form->setValueSources(['query']);
  7515. $result = $this->Form->control('id');
  7516. $expected = [
  7517. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7518. ];
  7519. $this->assertHtml($expected, $result);
  7520. }
  7521. /**
  7522. * Tests the different input rendering values based on sources values switching while supplying
  7523. * an entity (base context) and multiple sources (such as data, query)
  7524. */
  7525. public function testFormValueSourcesListSwitchRendering(): void
  7526. {
  7527. $articles = $this->getTableLocator()->get('Articles');
  7528. $article = new Article();
  7529. $articles->patchEntity($article, ['id' => '3']);
  7530. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7531. $this->Form->create($article);
  7532. $this->Form->setValueSources(['context', 'query']);
  7533. $result = $this->Form->control('id');
  7534. $expected = [
  7535. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7536. ];
  7537. $this->assertHtml($expected, $result);
  7538. $this->Form->setValueSources(['query', 'context']);
  7539. $result = $this->Form->control('id');
  7540. $expected = [
  7541. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7542. ];
  7543. $this->assertHtml($expected, $result);
  7544. $this->Form->setValueSources(['data', 'query', 'context']);
  7545. $result = $this->Form->control('id');
  7546. $expected = [
  7547. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7548. ];
  7549. $this->assertHtml($expected, $result);
  7550. $this->View->setRequest($this->View->getRequest()
  7551. ->withData('id', '8')
  7552. ->withQueryParams(['id' => '9']));
  7553. $this->Form->setValueSources(['data', 'query', 'context']);
  7554. $result = $this->Form->control('id');
  7555. $expected = [
  7556. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7557. ];
  7558. $this->assertHtml($expected, $result);
  7559. }
  7560. /**
  7561. * Test the different form input renderings based on values sources switchings through form options
  7562. */
  7563. public function testFormValueSourcesSwitchViaOptionsRendering(): void
  7564. {
  7565. $articles = $this->getTableLocator()->get('Articles');
  7566. $article = new Article();
  7567. $articles->patchEntity($article, ['id' => '3']);
  7568. $this->View->setRequest(
  7569. $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
  7570. );
  7571. $this->Form->create($article, ['valueSources' => 'query']);
  7572. $result = $this->Form->control('id');
  7573. $expected = [
  7574. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7575. ];
  7576. $this->assertHtml($expected, $result);
  7577. $result = $this->Form->getSourceValue('id');
  7578. $this->assertSame('5', $result);
  7579. $this->Form->setValueSources(['context']);
  7580. $this->Form->create($article, ['valueSources' => 'query']);
  7581. $result = $this->Form->control('id');
  7582. $expected = [
  7583. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7584. ];
  7585. $this->assertHtml($expected, $result);
  7586. $result = $this->Form->getSourceValue('id');
  7587. $this->assertSame('5', $result);
  7588. $this->Form->setValueSources(['query']);
  7589. $this->Form->create($article, ['valueSources' => 'data']);
  7590. $result = $this->Form->control('id');
  7591. $expected = [
  7592. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7593. ];
  7594. $this->assertHtml($expected, $result);
  7595. $result = $this->Form->getSourceValue('id');
  7596. $this->assertSame('4', $result);
  7597. $this->Form->setValueSources(['query']);
  7598. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7599. $result = $this->Form->control('id');
  7600. $expected = [
  7601. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7602. ];
  7603. $this->assertHtml($expected, $result);
  7604. $result = $this->Form->getSourceValue('id');
  7605. $this->assertSame(3, $result);
  7606. }
  7607. /**
  7608. * Test the different form input renderings based on values sources switchings through form options
  7609. */
  7610. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering(): void
  7611. {
  7612. $articles = $this->getTableLocator()->get('Articles');
  7613. $article = new Article();
  7614. $articles->patchEntity($article, ['id' => '3']);
  7615. $this->View->setRequest(
  7616. $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
  7617. );
  7618. $this->Form->setValueSources(['context'])
  7619. ->create($article, ['valueSources' => ['query', 'data']]);
  7620. $result = $this->Form->control('id');
  7621. $expected = [
  7622. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7623. ];
  7624. $this->assertHtml($expected, $result);
  7625. $result = $this->Form->getSourceValue('id');
  7626. $this->assertSame('11', $result);
  7627. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7628. $this->Form->setValueSources(['context'])
  7629. ->create($article, ['valueSources' => ['query', 'data']]);
  7630. $result = $this->Form->control('id');
  7631. $expected = [
  7632. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7633. ];
  7634. $this->assertHtml($expected, $result);
  7635. $result = $this->Form->getSourceValue('id');
  7636. $this->assertSame('10', $result);
  7637. }
  7638. /**
  7639. * Test the different form values sources resetting through From::end();
  7640. */
  7641. public function testFormValueSourcesResetViaEnd(): void
  7642. {
  7643. $expected = ['data', 'context'];
  7644. $result = $this->Form->getValueSources();
  7645. $this->assertEquals($expected, $result);
  7646. $expected = ['query', 'context', 'data'];
  7647. $this->Form->setValueSources(['query', 'context', 'data']);
  7648. $result = $this->Form->getValueSources();
  7649. $this->assertEquals($expected, $result);
  7650. $this->Form->create();
  7651. $result = $this->Form->getValueSources();
  7652. $this->assertEquals($expected, $result);
  7653. $this->Form->end();
  7654. $result = $this->Form->getValueSources();
  7655. $this->assertEquals(['data', 'context'], $result);
  7656. }
  7657. /**
  7658. * Test sources values defaults handling
  7659. */
  7660. public function testFormValueSourcesDefaults(): void
  7661. {
  7662. $this->View->setRequest(
  7663. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7664. );
  7665. $this->Form->create();
  7666. $result = $this->Form->password('password');
  7667. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7668. $this->assertHtml($expected, $result);
  7669. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7670. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7671. $this->assertHtml($expected, $result);
  7672. $this->Form->setValueSources('query');
  7673. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7674. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7675. $this->assertHtml($expected, $result);
  7676. $this->Form->setValueSources('data');
  7677. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7678. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7679. $this->assertHtml($expected, $result);
  7680. }
  7681. /**
  7682. * Test sources values schema defaults handling
  7683. */
  7684. public function testSourcesValueDoesntExistPassThrough(): void
  7685. {
  7686. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7687. $articles = $this->getTableLocator()->get('Articles');
  7688. $entity = $articles->newEmptyEntity();
  7689. $this->Form->create($entity);
  7690. $this->Form->setValueSources(['query', 'context']);
  7691. $result = $this->Form->getSourceValue('category');
  7692. $this->assertSame('sesame-cookies', $result);
  7693. $this->Form->setValueSources(['context', 'query']);
  7694. $result = $this->Form->getSourceValue('category');
  7695. $this->assertSame('sesame-cookies', $result);
  7696. }
  7697. /**
  7698. * testNestedLabelInput method
  7699. *
  7700. * Test the `nestedInput` parameter
  7701. */
  7702. public function testNestedLabelInput(): void
  7703. {
  7704. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7705. $expected = [
  7706. 'div' => ['class' => 'input text'],
  7707. 'label' => ['for' => 'foo'],
  7708. ['input' => [
  7709. 'type' => 'text',
  7710. 'name' => 'foo',
  7711. 'id' => 'foo',
  7712. ]],
  7713. 'Foo',
  7714. '/label',
  7715. '/div',
  7716. ];
  7717. $this->assertHtml($expected, $result);
  7718. }
  7719. /**
  7720. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7721. *
  7722. * This test makes sure `false` excludes the label from the render
  7723. */
  7724. public function testControlLabelManipulationDisableLabels(): void
  7725. {
  7726. $result = $this->Form->control('test', [
  7727. 'type' => 'radio',
  7728. 'options' => ['A', 'B'],
  7729. 'labelOptions' => false,
  7730. ]);
  7731. $expected = [
  7732. ['div' => ['class' => 'input radio']],
  7733. '<label',
  7734. 'Test',
  7735. '/label',
  7736. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7737. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7738. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7739. '/div',
  7740. ];
  7741. $this->assertHtml($expected, $result);
  7742. $result = $this->Form->control('checkbox1', [
  7743. 'label' => 'My checkboxes',
  7744. 'multiple' => 'checkbox',
  7745. 'type' => 'select',
  7746. 'options' => [
  7747. ['text' => 'First Checkbox', 'value' => 1],
  7748. ['text' => 'Second Checkbox', 'value' => 2],
  7749. ],
  7750. 'labelOptions' => false,
  7751. ]);
  7752. $expected = [
  7753. ['div' => ['class' => 'input select']],
  7754. ['label' => ['for' => 'checkbox1']],
  7755. 'My checkboxes',
  7756. '/label',
  7757. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  7758. ['div' => ['class' => 'checkbox']],
  7759. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7760. '/div',
  7761. ['div' => ['class' => 'checkbox']],
  7762. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7763. '/div',
  7764. '/div',
  7765. ];
  7766. $this->assertHtml($expected, $result);
  7767. }
  7768. /**
  7769. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7770. *
  7771. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7772. * added to the class if checked.
  7773. *
  7774. * Also checks to make sure any custom attributes are rendered correctly
  7775. */
  7776. public function testControlLabelManipulationRadios(): void
  7777. {
  7778. $result = $this->Form->control('test', [
  7779. 'type' => 'radio',
  7780. 'options' => ['A', 'B'],
  7781. 'labelOptions' => ['class' => 'custom-class'],
  7782. ]);
  7783. $expected = [
  7784. ['div' => ['class' => 'input radio']],
  7785. '<label',
  7786. 'Test',
  7787. '/label',
  7788. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7789. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7790. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7791. 'A',
  7792. '/label',
  7793. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  7794. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7795. 'B',
  7796. '/label',
  7797. '/div',
  7798. ];
  7799. $this->assertHtml($expected, $result);
  7800. $result = $this->Form->control('test', [
  7801. 'type' => 'radio',
  7802. 'options' => ['A', 'B'],
  7803. 'value' => 1,
  7804. 'labelOptions' => ['class' => 'custom-class'],
  7805. ]);
  7806. $expected = [
  7807. ['div' => ['class' => 'input radio']],
  7808. '<label',
  7809. 'Test',
  7810. '/label',
  7811. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7812. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7813. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7814. 'A',
  7815. '/label',
  7816. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  7817. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7818. 'B',
  7819. '/label',
  7820. '/div',
  7821. ];
  7822. $this->assertHtml($expected, $result);
  7823. $result = $this->Form->control('test', [
  7824. 'type' => 'radio',
  7825. 'options' => ['A', 'B'],
  7826. 'value' => 1,
  7827. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  7828. ]);
  7829. $expected = [
  7830. ['div' => ['class' => 'input radio']],
  7831. '<label',
  7832. 'Test',
  7833. '/label',
  7834. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7835. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  7836. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7837. 'A',
  7838. '/label',
  7839. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  7840. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7841. 'B',
  7842. '/label',
  7843. '/div',
  7844. ];
  7845. $this->assertHtml($expected, $result);
  7846. $result = $this->Form->radio('test', ['A', 'B'], [
  7847. 'label' => [
  7848. 'class' => ['custom-class', 'another-class'],
  7849. 'data-name' => 'bob',
  7850. ],
  7851. 'value' => 1,
  7852. ]);
  7853. $expected = [
  7854. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  7855. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  7856. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7857. 'A',
  7858. '/label',
  7859. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  7860. ['input' => [
  7861. 'type' => 'radio',
  7862. 'name' => 'test',
  7863. 'value' => '1',
  7864. 'id' => 'test-1',
  7865. 'checked' => 'checked',
  7866. ]],
  7867. 'B',
  7868. '/label',
  7869. ];
  7870. $this->assertHtml($expected, $result);
  7871. }
  7872. /**
  7873. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  7874. *
  7875. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7876. * added to the class if checked.
  7877. *
  7878. * Also checks to make sure any custom attributes are rendered correctly
  7879. */
  7880. public function testControlLabelManipulationCheckboxes(): void
  7881. {
  7882. $result = $this->Form->control('checkbox1', [
  7883. 'label' => 'My checkboxes',
  7884. 'multiple' => 'checkbox',
  7885. 'type' => 'select',
  7886. 'options' => [
  7887. ['text' => 'First Checkbox', 'value' => 1],
  7888. ['text' => 'Second Checkbox', 'value' => 2],
  7889. ],
  7890. 'labelOptions' => ['class' => 'custom-class'],
  7891. 'value' => ['1'],
  7892. ]);
  7893. $expected = [
  7894. ['div' => ['class' => 'input select']],
  7895. ['label' => ['for' => 'checkbox1']],
  7896. 'My checkboxes',
  7897. '/label',
  7898. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  7899. ['div' => ['class' => 'checkbox']],
  7900. ['label' => [
  7901. 'class' => 'custom-class selected',
  7902. 'for' => 'checkbox1-1',
  7903. ]],
  7904. ['input' => [
  7905. 'type' => 'checkbox',
  7906. 'name' => 'checkbox1[]',
  7907. 'value' => '1',
  7908. 'id' => 'checkbox1-1',
  7909. 'checked' => 'checked',
  7910. ]],
  7911. 'First Checkbox',
  7912. '/label',
  7913. '/div',
  7914. ['div' => ['class' => 'checkbox']],
  7915. ['label' => [
  7916. 'class' => 'custom-class',
  7917. 'for' => 'checkbox1-2',
  7918. ]],
  7919. ['input' => [
  7920. 'type' => 'checkbox',
  7921. 'name' => 'checkbox1[]',
  7922. 'value' => '2',
  7923. 'id' => 'checkbox1-2',
  7924. ]],
  7925. 'Second Checkbox',
  7926. '/label',
  7927. '/div',
  7928. '/div',
  7929. ];
  7930. $this->assertHtml($expected, $result);
  7931. $result = $this->Form->control('checkbox1', [
  7932. 'label' => 'My checkboxes',
  7933. 'multiple' => 'checkbox',
  7934. 'type' => 'select',
  7935. 'options' => [
  7936. ['text' => 'First Checkbox', 'value' => 1],
  7937. ['text' => 'Second Checkbox', 'value' => 2],
  7938. ],
  7939. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  7940. 'value' => ['1'],
  7941. ]);
  7942. $expected = [
  7943. ['div' => ['class' => 'input select']],
  7944. ['label' => ['for' => 'checkbox1']],
  7945. 'My checkboxes',
  7946. '/label',
  7947. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  7948. ['div' => ['class' => 'checkbox']],
  7949. ['label' => [
  7950. 'class' => 'custom-class another-class selected',
  7951. 'data-name' => 'bob',
  7952. 'for' => 'checkbox1-1',
  7953. ]],
  7954. ['input' => [
  7955. 'type' => 'checkbox',
  7956. 'name' => 'checkbox1[]',
  7957. 'value' => '1',
  7958. 'id' => 'checkbox1-1',
  7959. 'checked' => 'checked',
  7960. ]],
  7961. 'First Checkbox',
  7962. '/label',
  7963. '/div',
  7964. ['div' => ['class' => 'checkbox']],
  7965. ['label' => [
  7966. 'class' => 'custom-class another-class',
  7967. 'data-name' => 'bob',
  7968. 'for' => 'checkbox1-2',
  7969. ]],
  7970. ['input' => [
  7971. 'type' => 'checkbox',
  7972. 'name' => 'checkbox1[]',
  7973. 'value' => '2',
  7974. 'id' => 'checkbox1-2',
  7975. ]],
  7976. 'Second Checkbox',
  7977. '/label',
  7978. '/div',
  7979. '/div',
  7980. ];
  7981. $this->assertHtml($expected, $result);
  7982. }
  7983. /**
  7984. * testControlMaxLengthArrayContext method
  7985. *
  7986. * Test control() with maxlength attribute in Array Context.
  7987. */
  7988. public function testControlMaxLengthArrayContext(): void
  7989. {
  7990. $this->article['schema'] = [
  7991. 'title' => ['length' => 10],
  7992. ];
  7993. $this->Form->create($this->article);
  7994. $result = $this->Form->control('title');
  7995. $expected = [
  7996. 'div' => ['class'],
  7997. 'label' => ['for'],
  7998. 'Title',
  7999. '/label',
  8000. 'input' => [
  8001. 'aria-required' => 'true',
  8002. 'id',
  8003. 'name' => 'title',
  8004. 'type' => 'text',
  8005. 'required' => 'required',
  8006. 'maxlength' => 10,
  8007. 'data-validity-message' => 'This field cannot be left empty',
  8008. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8009. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8010. ],
  8011. '/div',
  8012. ];
  8013. $this->assertHtml($expected, $result);
  8014. }
  8015. /**
  8016. * testControlMaxLengthEntityContext method
  8017. *
  8018. * Test control() with maxlength attribute in Entity Context.
  8019. */
  8020. public function testControlMaxLengthEntityContext(): void
  8021. {
  8022. $this->article['schema']['title']['length'] = 45;
  8023. $validator = new Validator();
  8024. $validator->maxLength('title', 10);
  8025. $article = new EntityContext(
  8026. [
  8027. 'entity' => new Entity($this->article),
  8028. 'table' => new Table([
  8029. 'alias' => 'Articles',
  8030. 'schema' => $this->article['schema'],
  8031. 'validator' => $validator,
  8032. ]),
  8033. ]
  8034. );
  8035. $this->Form->create($article);
  8036. $result = $this->Form->control('title');
  8037. $expected = [
  8038. 'div' => ['class'],
  8039. 'label' => ['for'],
  8040. 'Title',
  8041. '/label',
  8042. 'input' => [
  8043. 'aria-required' => 'true',
  8044. 'id',
  8045. 'name' => 'title',
  8046. 'type' => 'text',
  8047. 'required' => 'required',
  8048. 'maxlength' => 10,
  8049. 'data-validity-message' => 'This field cannot be left empty',
  8050. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8051. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8052. ],
  8053. '/div',
  8054. ];
  8055. $this->assertHtml($expected, $result);
  8056. $this->article['schema']['title']['length'] = 45;
  8057. $validator = new Validator();
  8058. $validator->maxLength('title', 55);
  8059. $article = new EntityContext(
  8060. [
  8061. 'entity' => new Entity($this->article),
  8062. 'table' => new Table([
  8063. 'schema' => $this->article['schema'],
  8064. 'validator' => $validator,
  8065. 'alias' => 'Articles',
  8066. ]),
  8067. ]
  8068. );
  8069. $this->Form->create($article);
  8070. $result = $this->Form->control('title');
  8071. $expected = [
  8072. 'div' => ['class'],
  8073. 'label' => ['for'],
  8074. 'Title',
  8075. '/label',
  8076. 'input' => [
  8077. 'aria-required' => 'true',
  8078. 'id',
  8079. 'name' => 'title',
  8080. 'type' => 'text',
  8081. 'required' => 'required',
  8082. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8083. 'data-validity-message' => 'This field cannot be left empty',
  8084. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8085. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8086. ],
  8087. '/div',
  8088. ];
  8089. $this->assertHtml($expected, $result);
  8090. $this->article['schema']['title']['length'] = 45;
  8091. $validator = new Validator();
  8092. $validator->maxLength('title', 55);
  8093. $article = new EntityContext(
  8094. [
  8095. 'entity' => new Entity($this->article),
  8096. 'table' => new Table([
  8097. 'schema' => $this->article['schema'],
  8098. 'validator' => $validator,
  8099. 'alias' => 'Articles',
  8100. ]),
  8101. ]
  8102. );
  8103. $this->Form->create($article);
  8104. $result = $this->Form->control('title', ['maxlength' => 10]);
  8105. $expected = [
  8106. 'div' => ['class'],
  8107. 'label' => ['for'],
  8108. 'Title',
  8109. '/label',
  8110. 'input' => [
  8111. 'aria-required' => 'true',
  8112. 'id',
  8113. 'name' => 'title',
  8114. 'type' => 'text',
  8115. 'required' => 'required',
  8116. 'maxlength' => 10, // Length set in options should take highest precedence.
  8117. 'data-validity-message' => 'This field cannot be left empty',
  8118. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8119. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8120. ],
  8121. '/div',
  8122. ];
  8123. $this->assertHtml($expected, $result);
  8124. }
  8125. /**
  8126. * testControlMinMaxLengthEntityContext method
  8127. *
  8128. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8129. */
  8130. public function testControlMinMaxLengthEntityContext(): void
  8131. {
  8132. $validator = new Validator();
  8133. $validator->maxLength('title', 10);
  8134. $article = new EntityContext(
  8135. [
  8136. 'entity' => new Entity($this->article),
  8137. 'table' => new Table([
  8138. 'alias' => 'Articles',
  8139. 'schema' => $this->article['schema'],
  8140. 'validator' => $validator,
  8141. ]),
  8142. ]
  8143. );
  8144. $this->Form->create($article);
  8145. $result = $this->Form->control('title');
  8146. $expected = [
  8147. 'div' => ['class'],
  8148. 'label' => ['for'],
  8149. 'Title',
  8150. '/label',
  8151. 'input' => [
  8152. 'aria-required' => 'true',
  8153. 'id',
  8154. 'name' => 'title',
  8155. 'type' => 'text',
  8156. 'required' => 'required',
  8157. 'maxlength' => 10,
  8158. 'data-validity-message' => 'This field cannot be left empty',
  8159. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8160. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8161. ],
  8162. '/div',
  8163. ];
  8164. $this->assertHtml($expected, $result);
  8165. }
  8166. /**
  8167. * testControlMaxLengthFormContext method
  8168. *
  8169. * Test control() with maxlength attribute in Form Context.
  8170. */
  8171. public function testControlMaxLengthFormContext(): void
  8172. {
  8173. $validator = new Validator();
  8174. $validator->maxLength('title', 10);
  8175. $form = new Form();
  8176. $form->setValidator('default', $validator);
  8177. $this->Form->create($form);
  8178. $result = $this->Form->control('title');
  8179. $expected = [
  8180. 'div' => ['class'],
  8181. 'label' => ['for'],
  8182. 'Title',
  8183. '/label',
  8184. 'input' => [
  8185. 'aria-required' => 'true',
  8186. 'id',
  8187. 'name' => 'title',
  8188. 'type' => 'text',
  8189. 'required' => 'required',
  8190. 'maxlength' => 10,
  8191. 'data-validity-message' => 'This field cannot be left empty',
  8192. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8193. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8194. ],
  8195. '/div',
  8196. ];
  8197. $this->assertHtml($expected, $result);
  8198. }
  8199. }