FormHelperTest.php 295 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948
  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 Cake\Collection\Collection;
  18. use Cake\Core\Configure;
  19. use Cake\Form\Form;
  20. use Cake\Http\ServerRequest;
  21. use Cake\I18n\Date;
  22. use Cake\ORM\Entity;
  23. use Cake\ORM\Table;
  24. use Cake\Routing\Router;
  25. use Cake\TestSuite\TestCase;
  26. use Cake\Utility\Security;
  27. use Cake\Validation\Validator;
  28. use Cake\View\Form\EntityContext;
  29. use Cake\View\Helper\FormHelper;
  30. use Cake\View\View;
  31. use Cake\View\Widget\WidgetLocator;
  32. use ReflectionProperty;
  33. use TestApp\Model\Entity\Article;
  34. use TestApp\Model\Table\ContactsTable;
  35. use TestApp\Model\Table\ValidateUsersTable;
  36. /**
  37. * FormHelperTest class
  38. *
  39. * @property \Cake\View\Helper\FormHelper $Form
  40. * @property \Cake\View\View $View
  41. */
  42. class FormHelperTest extends TestCase
  43. {
  44. /**
  45. * Fixtures to be used
  46. *
  47. * @var array
  48. */
  49. public $fixtures = ['core.Articles', 'core.Comments'];
  50. /**
  51. * Do not load the fixtures by default
  52. *
  53. * @var bool
  54. */
  55. public $autoFixtures = false;
  56. /**
  57. * @var array
  58. */
  59. protected $article = [];
  60. /**
  61. * setUp method
  62. *
  63. * @return void
  64. */
  65. public function setUp(): void
  66. {
  67. parent::setUp();
  68. Configure::write('Config.language', 'eng');
  69. Configure::write('App.base', '');
  70. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  71. $request = new ServerRequest([
  72. 'webroot' => '',
  73. 'base' => '',
  74. 'url' => '/articles/add',
  75. 'params' => [
  76. 'controller' => 'articles',
  77. 'action' => 'add',
  78. ],
  79. ]);
  80. $this->View = new View($request);
  81. $this->Form = new FormHelper($this->View);
  82. $this->dateRegex = [
  83. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  84. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  85. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  86. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  87. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  88. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  89. ];
  90. $this->article = [
  91. 'schema' => [
  92. 'id' => ['type' => 'integer'],
  93. 'author_id' => ['type' => 'integer', 'null' => true],
  94. 'title' => ['type' => 'string', 'null' => true],
  95. 'body' => 'text',
  96. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  97. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  98. ],
  99. 'required' => [
  100. 'author_id' => true,
  101. 'title' => true,
  102. ],
  103. ];
  104. Security::setSalt('foo!');
  105. Router::connect('/:controller', ['action' => 'index']);
  106. Router::connect('/:controller/:action/*');
  107. }
  108. /**
  109. * tearDown method
  110. *
  111. * @return void
  112. */
  113. public function tearDown(): void
  114. {
  115. parent::tearDown();
  116. unset($this->Form, $this->Controller, $this->View);
  117. $this->getTableLocator()->clear();
  118. }
  119. /**
  120. * Test construct() with the templates option.
  121. *
  122. * @return void
  123. */
  124. public function testConstructTemplatesFile()
  125. {
  126. $helper = new FormHelper($this->View, [
  127. 'templates' => 'htmlhelper_tags',
  128. ]);
  129. $result = $helper->control('name');
  130. $this->assertStringContainsString('<input', $result);
  131. }
  132. /**
  133. * Test that when specifying custom widgets the config array for that widget
  134. * is overwritten instead of merged.
  135. *
  136. * @return void
  137. */
  138. public function testConstructWithWidgets()
  139. {
  140. $config = [
  141. 'widgets' => [
  142. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  143. ],
  144. ];
  145. $helper = new FormHelper($this->View, $config);
  146. $locator = $helper->getWidgetLocator();
  147. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  148. }
  149. /**
  150. * Test that when specifying custom widgets config file and it should be
  151. * added to widgets array. WidgetLocator will load widgets in constructor.
  152. *
  153. * @return void
  154. */
  155. public function testConstructWithWidgetsConfig()
  156. {
  157. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  158. $locator = $helper->getWidgetLocator();
  159. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  160. }
  161. /**
  162. * Test setting the widget locator
  163. *
  164. * @return void
  165. */
  166. public function testSetAndGetWidgetLocator()
  167. {
  168. $helper = new FormHelper($this->View);
  169. $locator = new WidgetLocator($helper->templater(), $this->View);
  170. $helper->setWidgetLocator($locator);
  171. $this->assertSame($locator, $helper->getWidgetLocator());
  172. }
  173. /**
  174. * Test overridding grouped input types which controls generation of "for"
  175. * attribute of labels.
  176. *
  177. * @return void
  178. */
  179. public function testConstructWithGroupedInputTypes()
  180. {
  181. $helper = new FormHelper($this->View, [
  182. 'groupedInputTypes' => ['radio'],
  183. ]);
  184. $result = $helper->control('when', ['type' => 'datetime-local']);
  185. $this->assertStringContainsString('<label for="when">When</label>', $result);
  186. }
  187. /**
  188. * Test registering a new widget class and rendering it.
  189. *
  190. * @return void
  191. */
  192. public function testAddWidgetAndRenderWidget()
  193. {
  194. $data = [
  195. 'val' => 1,
  196. ];
  197. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  198. $this->Form->addWidget('test', $mock);
  199. $mock->expects($this->once())
  200. ->method('render')
  201. ->with($data)
  202. ->will($this->returnValue('HTML'));
  203. $result = $this->Form->widget('test', $data);
  204. $this->assertSame('HTML', $result);
  205. }
  206. /**
  207. * Test that secureFields() of widget is called after calling render(),
  208. * not before.
  209. *
  210. * @return void
  211. */
  212. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  213. {
  214. $data = [
  215. 'val' => 1,
  216. 'name' => 'test',
  217. ];
  218. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  219. $this->Form->addWidget('test', $mock);
  220. $mock->expects($this->at(0))
  221. ->method('render')
  222. ->with($data)
  223. ->will($this->returnValue('HTML'));
  224. $mock->expects($this->at(1))
  225. ->method('secureFields')
  226. ->with($data)
  227. ->will($this->returnValue(['test']));
  228. $result = $this->Form->widget('test', $data + ['secure' => true]);
  229. $this->assertSame('HTML', $result);
  230. }
  231. /**
  232. * Test that empty string is not added to secure fields list when
  233. * rendering input widget without name.
  234. *
  235. * @return void
  236. */
  237. public function testRenderingWidgetWithEmptyName()
  238. {
  239. $this->assertEquals([], $this->Form->fields);
  240. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  241. $this->assertSame('<select name=""></select>', $result);
  242. $this->assertEquals([], $this->Form->fields);
  243. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  244. $this->assertSame('<select name="0"></select>', $result);
  245. $this->assertEquals(['0'], $this->Form->fields);
  246. }
  247. /**
  248. * Test registering an invalid widget class.
  249. *
  250. * @return void
  251. */
  252. public function testAddWidgetInvalid()
  253. {
  254. $this->expectException(\RuntimeException::class);
  255. $mock = new \StdClass();
  256. $this->Form->addWidget('test', $mock);
  257. $this->Form->widget('test');
  258. }
  259. /**
  260. * Test adding a new context class.
  261. *
  262. * @return void
  263. */
  264. public function testAddContextProvider()
  265. {
  266. $context = 'My data';
  267. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  268. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  269. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  270. $this->assertEquals($context, $data['entity']);
  271. return $stub;
  272. });
  273. $this->Form->create($context);
  274. $result = $this->Form->context();
  275. $this->assertSame($stub, $result);
  276. }
  277. /**
  278. * Test replacing a context class.
  279. *
  280. * @return void
  281. */
  282. public function testAddContextProviderReplace()
  283. {
  284. $entity = new Article();
  285. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  286. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  287. return $stub;
  288. });
  289. $this->Form->create($entity);
  290. $result = $this->Form->context();
  291. $this->assertSame($stub, $result);
  292. }
  293. /**
  294. * Test overriding a context class.
  295. *
  296. * @return void
  297. */
  298. public function testAddContextProviderAdd()
  299. {
  300. $entity = new Article();
  301. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  302. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  303. if ($data['entity'] instanceof Entity) {
  304. return $stub;
  305. }
  306. });
  307. $this->Form->create($entity);
  308. $result = $this->Form->context();
  309. $this->assertSame($stub, $result);
  310. }
  311. /**
  312. * Test adding an invalid context class.
  313. *
  314. * @return void
  315. */
  316. public function testAddContextProviderInvalid()
  317. {
  318. $this->expectException(\RuntimeException::class);
  319. $this->expectExceptionMessage('Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.');
  320. $context = 'My data';
  321. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  322. return new \StdClass();
  323. });
  324. $this->Form->create($context);
  325. }
  326. /**
  327. * Provides context options for create().
  328. *
  329. * @return array
  330. */
  331. public function contextSelectionProvider()
  332. {
  333. $entity = new Article();
  334. $collection = new Collection([$entity]);
  335. $emptyCollection = new Collection([]);
  336. $arrayObject = new \ArrayObject([]);
  337. $data = [
  338. 'schema' => [
  339. 'title' => ['type' => 'string'],
  340. ],
  341. ];
  342. $form = new Form();
  343. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  344. return [
  345. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  346. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  347. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  348. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  349. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  350. 'form' => [$form, 'Cake\View\Form\FormContext'],
  351. 'none' => [null, 'Cake\View\Form\NullContext'],
  352. 'false' => [false, 'Cake\View\Form\NullContext'],
  353. 'custom' => [$custom, get_class($custom)],
  354. ];
  355. }
  356. /**
  357. * Test default context selection in create()
  358. *
  359. * @dataProvider contextSelectionProvider
  360. * @return void
  361. */
  362. public function testCreateContextSelectionBuiltIn($data, $class)
  363. {
  364. $this->loadFixtures('Articles');
  365. $this->Form->create($data);
  366. $this->assertInstanceOf($class, $this->Form->context());
  367. }
  368. /**
  369. * Data provider for type option.
  370. *
  371. * @return array
  372. */
  373. public static function requestTypeProvider()
  374. {
  375. return [
  376. // type, method, override
  377. ['post', 'post', 'POST'],
  378. ['put', 'post', 'PUT'],
  379. ['patch', 'post', 'PATCH'],
  380. ['delete', 'post', 'DELETE'],
  381. ];
  382. }
  383. /**
  384. * Test creating file forms.
  385. *
  386. * @return void
  387. */
  388. public function testCreateFile()
  389. {
  390. $encoding = strtolower(Configure::read('App.encoding'));
  391. $result = $this->Form->create(false, ['type' => 'file']);
  392. $expected = [
  393. 'form' => [
  394. 'method' => 'post', 'action' => '/articles/add',
  395. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  396. ],
  397. 'div' => ['style' => 'display:none;'],
  398. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  399. '/div',
  400. ];
  401. $this->assertHtml($expected, $result);
  402. }
  403. /**
  404. * Test creating GET forms.
  405. *
  406. * @return void
  407. */
  408. public function testCreateGet()
  409. {
  410. $encoding = strtolower(Configure::read('App.encoding'));
  411. $result = $this->Form->create(false, ['type' => 'get']);
  412. $expected = ['form' => [
  413. 'method' => 'get', 'action' => '/articles/add',
  414. 'accept-charset' => $encoding,
  415. ]];
  416. $this->assertHtml($expected, $result);
  417. }
  418. /**
  419. * Test explicit method/enctype options.
  420. *
  421. * Explicit method overwrites inferred method from 'type'
  422. *
  423. * @return void
  424. */
  425. public function testCreateExplicitMethodEnctype()
  426. {
  427. $encoding = strtolower(Configure::read('App.encoding'));
  428. $result = $this->Form->create(false, [
  429. 'type' => 'get',
  430. 'method' => 'put',
  431. 'enctype' => 'multipart/form-data',
  432. ]);
  433. $expected = ['form' => [
  434. 'method' => 'put',
  435. 'action' => '/articles/add',
  436. 'enctype' => 'multipart/form-data',
  437. 'accept-charset' => $encoding,
  438. ]];
  439. $this->assertHtml($expected, $result);
  440. }
  441. /**
  442. * Test create() with the templates option.
  443. *
  444. * @return void
  445. */
  446. public function testCreateTemplatesArray()
  447. {
  448. $result = $this->Form->create($this->article, [
  449. 'templates' => [
  450. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  451. ],
  452. ]);
  453. $expected = [
  454. 'form' => [
  455. 'class' => 'form-horizontal',
  456. 'method' => 'post',
  457. 'action' => '/articles/add',
  458. 'accept-charset' => 'utf-8',
  459. ],
  460. ];
  461. $this->assertHtml($expected, $result);
  462. }
  463. /**
  464. * Test create() with the templates option.
  465. *
  466. * @return void
  467. */
  468. public function testCreateTemplatesFile()
  469. {
  470. $result = $this->Form->create($this->article, [
  471. 'templates' => 'htmlhelper_tags',
  472. ]);
  473. $expected = [
  474. 'start form',
  475. 'div' => ['class' => 'hidden'],
  476. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  477. '/div',
  478. ];
  479. $this->assertHtml($expected, $result);
  480. }
  481. /**
  482. * Test that create() and end() restore templates.
  483. *
  484. * @return void
  485. */
  486. public function testCreateEndRestoreTemplates()
  487. {
  488. $this->Form->create($this->article, [
  489. 'templates' => ['input' => 'custom input element'],
  490. ]);
  491. $this->Form->end();
  492. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  493. }
  494. /**
  495. * Test using template vars in various templates used by control() method.
  496. *
  497. * @return void
  498. */
  499. public function testControlTemplateVars()
  500. {
  501. $result = $this->Form->control('text', [
  502. 'templates' => [
  503. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  504. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  505. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  506. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  507. ],
  508. 'templateVars' => [
  509. 'forinput' => 'in-input',
  510. 'forlabel' => 'in-label',
  511. 'forgroup' => 'in-group',
  512. 'forcontainer' => 'in-container',
  513. ],
  514. ]);
  515. $expected = [
  516. 'div' => ['class'],
  517. 'label' => ['for'],
  518. 'Text in-label',
  519. '/label',
  520. 'in-group',
  521. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  522. 'in-container',
  523. '/div',
  524. ];
  525. $this->assertHtml($expected, $result);
  526. }
  527. /**
  528. * Test ensuring template variables work in template files loaded
  529. * during control().
  530. *
  531. * @return void
  532. */
  533. public function testControlTemplatesFromFile()
  534. {
  535. $result = $this->Form->control('title', [
  536. 'templates' => 'test_templates',
  537. 'templateVars' => [
  538. 'forcontainer' => 'container-data',
  539. ],
  540. ]);
  541. $expected = [
  542. 'div' => ['class'],
  543. 'label' => ['for'],
  544. 'Title',
  545. '/label',
  546. 'input' => ['name', 'type' => 'text', 'id'],
  547. 'container-data',
  548. '/div',
  549. ];
  550. $this->assertHtml($expected, $result);
  551. }
  552. /**
  553. * Test using template vars in inputSubmit and submitContainer template.
  554. *
  555. * @return void
  556. */
  557. public function testSubmitTemplateVars()
  558. {
  559. $this->Form->setTemplates([
  560. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  561. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  562. ]);
  563. $result = $this->Form->submit('Submit', [
  564. 'templateVars' => [
  565. 'forinput' => 'in-input',
  566. 'forcontainer' => 'in-container',
  567. ],
  568. ]);
  569. $expected = [
  570. 'div' => ['class'],
  571. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  572. 'in-container',
  573. '/div',
  574. ];
  575. $this->assertHtml($expected, $result);
  576. }
  577. /**
  578. * test the create() method
  579. *
  580. * @dataProvider requestTypeProvider
  581. * @return void
  582. */
  583. public function testCreateTypeOptions($type, $method, $override)
  584. {
  585. $encoding = strtolower(Configure::read('App.encoding'));
  586. $result = $this->Form->create(false, ['type' => $type]);
  587. $expected = [
  588. 'form' => [
  589. 'method' => $method, 'action' => '/articles/add',
  590. 'accept-charset' => $encoding,
  591. ],
  592. 'div' => ['style' => 'display:none;'],
  593. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  594. '/div',
  595. ];
  596. $this->assertHtml($expected, $result);
  597. }
  598. /**
  599. * Test using template vars in Create (formStart template)
  600. *
  601. * @return void
  602. */
  603. public function testCreateTemplateVars()
  604. {
  605. $result = $this->Form->create($this->article, [
  606. 'templates' => [
  607. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  608. ],
  609. 'templateVars' => ['header' => 'headertext'],
  610. ]);
  611. $expected = [
  612. 'h4' => ['class'],
  613. 'headertext',
  614. '/h4',
  615. 'form' => [
  616. 'method' => 'post',
  617. 'action' => '/articles/add',
  618. 'accept-charset' => 'utf-8',
  619. ],
  620. ];
  621. $this->assertHtml($expected, $result);
  622. }
  623. /**
  624. * Test opening a form for an update operation.
  625. *
  626. * @return void
  627. */
  628. public function testCreateUpdateForm()
  629. {
  630. $encoding = strtolower(Configure::read('App.encoding'));
  631. $this->View->setRequest($this->View->getRequest()
  632. ->withRequestTarget('/articles/edit/1')
  633. ->withParam('action', 'edit'));
  634. $this->article['defaults']['id'] = 1;
  635. $result = $this->Form->create($this->article);
  636. $expected = [
  637. 'form' => [
  638. 'method' => 'post',
  639. 'action' => '/articles/edit/1',
  640. 'accept-charset' => $encoding,
  641. ],
  642. 'div' => ['style' => 'display:none;'],
  643. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  644. '/div',
  645. ];
  646. $this->assertHtml($expected, $result);
  647. }
  648. /**
  649. * test create() with automatic url generation
  650. *
  651. * @return void
  652. */
  653. public function testCreateAutoUrl()
  654. {
  655. $encoding = strtolower(Configure::read('App.encoding'));
  656. $this->View->setRequest($this->View->getRequest()
  657. ->withRequestTarget('/articles/delete/10')
  658. ->withParam('action', 'delete'));
  659. $result = $this->Form->create($this->article);
  660. $expected = [
  661. 'form' => [
  662. 'method' => 'post', 'action' => '/articles/delete/10',
  663. 'accept-charset' => $encoding,
  664. ],
  665. 'div' => ['style' => 'display:none;'],
  666. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  667. '/div',
  668. ];
  669. $this->assertHtml($expected, $result);
  670. $this->article['defaults'] = ['id' => 1];
  671. $this->View->setRequest($this->View->getRequest()
  672. ->withRequestTarget('/articles/edit/1')
  673. ->withParam('action', 'delete'));
  674. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  675. $expected = [
  676. 'form' => [
  677. 'method' => 'post',
  678. 'action' => '/articles/edit/1',
  679. 'accept-charset' => $encoding,
  680. ],
  681. 'div' => ['style' => 'display:none;'],
  682. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  683. '/div',
  684. ];
  685. $this->assertHtml($expected, $result);
  686. $this->View->setRequest($this->View->getRequest()
  687. ->withParam('action', 'add'));
  688. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  689. $expected = [
  690. 'form' => [
  691. 'method' => 'post',
  692. 'action' => '/articles/publish/1',
  693. 'accept-charset' => $encoding,
  694. ],
  695. 'div' => ['style' => 'display:none;'],
  696. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  697. '/div',
  698. ];
  699. $this->assertHtml($expected, $result);
  700. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  701. $expected = [
  702. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  703. 'div' => ['style' => 'display:none;'],
  704. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  705. '/div',
  706. ];
  707. $this->assertHtml($expected, $result);
  708. $this->View->setRequest($this->View->getRequest()
  709. ->withParam('controller', 'Pages'));
  710. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  711. $expected = [
  712. 'form' => [
  713. 'method' => 'post', 'action' => '/Pages/signup/1',
  714. 'accept-charset' => $encoding,
  715. ],
  716. 'div' => ['style' => 'display:none;'],
  717. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  718. '/div',
  719. ];
  720. $this->assertHtml($expected, $result);
  721. }
  722. /**
  723. * Test create() with no URL (no "action" attribute for <form> tag)
  724. *
  725. * @return void
  726. */
  727. public function testCreateNoUrl()
  728. {
  729. $result = $this->Form->create(false, ['url' => false]);
  730. $expected = [
  731. 'form' => [
  732. 'method' => 'post',
  733. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  734. ],
  735. 'div' => ['style' => 'display:none;'],
  736. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  737. '/div',
  738. ];
  739. $this->assertHtml($expected, $result);
  740. }
  741. /**
  742. * test create() with a custom route
  743. *
  744. * @return void
  745. */
  746. public function testCreateCustomRoute()
  747. {
  748. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  749. $encoding = strtolower(Configure::read('App.encoding'));
  750. $this->View->setRequest($this->View->getRequest()
  751. ->withParam('controller', 'users'));
  752. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  753. $expected = [
  754. 'form' => [
  755. 'method' => 'post', 'action' => '/login',
  756. 'accept-charset' => $encoding,
  757. ],
  758. 'div' => ['style' => 'display:none;'],
  759. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  760. '/div',
  761. ];
  762. $this->assertHtml($expected, $result);
  763. Router::connect(
  764. '/new-article',
  765. ['controller' => 'articles', 'action' => 'myaction'],
  766. ['_name' => 'my-route']
  767. );
  768. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  769. $expected = [
  770. 'form' => [
  771. 'method' => 'post', 'action' => '/new-article',
  772. 'accept-charset' => $encoding,
  773. ],
  774. 'div' => ['style' => 'display:none;'],
  775. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  776. '/div',
  777. ];
  778. $this->assertHtml($expected, $result);
  779. }
  780. /**
  781. * test automatic accept-charset overriding
  782. *
  783. * @return void
  784. */
  785. public function testCreateWithAcceptCharset()
  786. {
  787. $result = $this->Form->create(
  788. $this->article,
  789. [
  790. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  791. ]
  792. );
  793. $expected = [
  794. 'form' => [
  795. 'method' => 'post', 'action' => '/articles',
  796. 'accept-charset' => 'iso-8859-1',
  797. ],
  798. 'div' => ['style' => 'display:none;'],
  799. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  800. '/div',
  801. ];
  802. $this->assertHtml($expected, $result);
  803. }
  804. /**
  805. * Test base form URL when url param is passed with multiple parameters (&)
  806. *
  807. */
  808. public function testCreateQueryStringRequest()
  809. {
  810. $encoding = strtolower(Configure::read('App.encoding'));
  811. $result = $this->Form->create($this->article, [
  812. 'type' => 'post',
  813. 'escape' => false,
  814. 'url' => [
  815. 'controller' => 'controller',
  816. 'action' => 'action',
  817. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  818. ],
  819. ]);
  820. $expected = [
  821. 'form' => [
  822. 'method' => 'post',
  823. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  824. 'accept-charset' => $encoding,
  825. ],
  826. 'div' => ['style' => 'display:none;'],
  827. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  828. '/div',
  829. ];
  830. $this->assertHtml($expected, $result);
  831. $result = $this->Form->create($this->article, [
  832. 'type' => 'post',
  833. 'url' => [
  834. 'controller' => 'controller',
  835. 'action' => 'action',
  836. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  837. ],
  838. ]);
  839. $this->assertHtml($expected, $result);
  840. }
  841. /**
  842. * test that create() doesn't cause errors by multiple id's being in the primary key
  843. * as could happen with multiple select or checkboxes.
  844. *
  845. * @return void
  846. */
  847. public function testCreateWithMultipleIdInData()
  848. {
  849. $encoding = strtolower(Configure::read('App.encoding'));
  850. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  851. $result = $this->Form->create($this->article);
  852. $expected = [
  853. 'form' => [
  854. 'method' => 'post',
  855. 'action' => '/articles/add',
  856. 'accept-charset' => $encoding,
  857. ],
  858. 'div' => ['style' => 'display:none;'],
  859. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  860. '/div',
  861. ];
  862. $this->assertHtml($expected, $result);
  863. }
  864. /**
  865. * test that create() doesn't add in extra passed params.
  866. *
  867. * @return void
  868. */
  869. public function testCreatePassedArgs()
  870. {
  871. $encoding = strtolower(Configure::read('App.encoding'));
  872. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  873. $result = $this->Form->create($this->article, [
  874. 'type' => 'post',
  875. 'escape' => false,
  876. 'url' => [
  877. 'action' => 'edit',
  878. 'myparam',
  879. ],
  880. ]);
  881. $expected = [
  882. 'form' => [
  883. 'method' => 'post',
  884. 'action' => '/articles/edit/myparam',
  885. 'accept-charset' => $encoding,
  886. ],
  887. 'div' => ['style' => 'display:none;'],
  888. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  889. '/div',
  890. ];
  891. $this->assertHtml($expected, $result);
  892. }
  893. /**
  894. * test creating a get form, and get form inputs.
  895. *
  896. * @return void
  897. */
  898. public function testGetFormCreate()
  899. {
  900. $encoding = strtolower(Configure::read('App.encoding'));
  901. $result = $this->Form->create($this->article, ['type' => 'get']);
  902. $expected = ['form' => [
  903. 'method' => 'get', 'action' => '/articles/add',
  904. 'accept-charset' => $encoding,
  905. ]];
  906. $this->assertHtml($expected, $result);
  907. $result = $this->Form->text('title');
  908. $expected = ['input' => [
  909. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  910. ]];
  911. $this->assertHtml($expected, $result);
  912. $result = $this->Form->password('password');
  913. $expected = ['input' => [
  914. 'name' => 'password', 'type' => 'password',
  915. ]];
  916. $this->assertHtml($expected, $result);
  917. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  918. $result = $this->Form->text('user_form');
  919. $expected = ['input' => [
  920. 'name' => 'user_form', 'type' => 'text',
  921. ]];
  922. $this->assertHtml($expected, $result);
  923. }
  924. /**
  925. * test get form, and inputs when the model param is false
  926. *
  927. * @return void
  928. */
  929. public function testGetFormWithFalseModel()
  930. {
  931. $encoding = strtolower(Configure::read('App.encoding'));
  932. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'contact_test'));
  933. $result = $this->Form->create(false, [
  934. 'type' => 'get', 'url' => ['controller' => 'contact_test'],
  935. ]);
  936. $expected = ['form' => [
  937. 'method' => 'get', 'action' => '/contact_test/add',
  938. 'accept-charset' => $encoding,
  939. ]];
  940. $this->assertHtml($expected, $result);
  941. $result = $this->Form->text('reason');
  942. $expected = [
  943. 'input' => ['type' => 'text', 'name' => 'reason'],
  944. ];
  945. $this->assertHtml($expected, $result);
  946. }
  947. /**
  948. * testFormCreateWithSecurity method
  949. *
  950. * Test form->create() with security key.
  951. *
  952. * @return void
  953. */
  954. public function testCreateWithSecurity()
  955. {
  956. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  957. $encoding = strtolower(Configure::read('App.encoding'));
  958. $result = $this->Form->create($this->article, [
  959. 'url' => '/articles/publish',
  960. ]);
  961. $expected = [
  962. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  963. 'div' => ['style' => 'display:none;'],
  964. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  965. ['input' => [
  966. 'type' => 'hidden',
  967. 'name' => '_csrfToken',
  968. 'value' => 'testKey',
  969. 'autocomplete' => 'off',
  970. ]],
  971. '/div',
  972. ];
  973. $this->assertHtml($expected, $result);
  974. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  975. $expected['form']['id'] = 'MyForm';
  976. $this->assertHtml($expected, $result);
  977. }
  978. /**
  979. * testFormCreateGetNoSecurity method
  980. *
  981. * Test form->create() with no security key as its a get form
  982. *
  983. * @return void
  984. */
  985. public function testCreateEndGetNoSecurity()
  986. {
  987. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  988. $article = new Article();
  989. $result = $this->Form->create($article, [
  990. 'type' => 'get',
  991. 'url' => '/contacts/add',
  992. ]);
  993. $this->assertStringNotContainsString('testKey', $result);
  994. $result = $this->Form->end();
  995. $this->assertStringNotContainsString('testKey', $result);
  996. }
  997. /**
  998. * test that create() clears the fields property so it starts fresh
  999. *
  1000. * @return void
  1001. */
  1002. public function testCreateClearingFields()
  1003. {
  1004. $this->Form->fields = ['model_id'];
  1005. $this->Form->create($this->article);
  1006. $this->assertEquals([], $this->Form->fields);
  1007. }
  1008. /**
  1009. * Tests form hash generation with model-less data
  1010. *
  1011. * @return void
  1012. */
  1013. public function testValidateHashNoModel()
  1014. {
  1015. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1016. $fields = ['anything'];
  1017. $result = $this->Form->secure($fields);
  1018. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1019. $this->assertStringContainsString($hash, $result);
  1020. }
  1021. /**
  1022. * Tests that hidden fields generated for checkboxes don't get locked
  1023. *
  1024. * @return void
  1025. */
  1026. public function testNoCheckboxLocking()
  1027. {
  1028. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1029. $this->assertSame([], $this->Form->fields);
  1030. $this->Form->checkbox('check', ['value' => '1']);
  1031. $this->assertSame($this->Form->fields, ['check']);
  1032. }
  1033. /**
  1034. * testFormSecurityFields method
  1035. *
  1036. * Test generation of secure form hash generation.
  1037. *
  1038. * @return void
  1039. */
  1040. public function testFormSecurityFields()
  1041. {
  1042. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1043. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1044. $result = $this->Form->secure($fields);
  1045. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1046. $hash .= ':' . 'Model.valid';
  1047. $hash = urlencode($hash);
  1048. $tokenDebug = urlencode(json_encode([
  1049. '',
  1050. $fields,
  1051. [],
  1052. ]));
  1053. $expected = [
  1054. 'div' => ['style' => 'display:none;'],
  1055. ['input' => [
  1056. 'type' => 'hidden',
  1057. 'name' => '_Token[fields]',
  1058. 'value' => $hash,
  1059. 'autocomplete' => 'off',
  1060. ]],
  1061. ['input' => [
  1062. 'type' => 'hidden',
  1063. 'name' => '_Token[unlocked]',
  1064. 'value' => '',
  1065. 'autocomplete' => 'off',
  1066. ]],
  1067. ['input' => [
  1068. 'type' => 'hidden',
  1069. 'name' => '_Token[debug]',
  1070. 'value' => $tokenDebug,
  1071. 'autocomplete' => 'off',
  1072. ]],
  1073. '/div',
  1074. ];
  1075. $this->assertHtml($expected, $result);
  1076. }
  1077. /**
  1078. * testFormSecurityFields method
  1079. *
  1080. * Test debug token is not generated if debug is false
  1081. *
  1082. * @return void
  1083. */
  1084. public function testFormSecurityFieldsNoDebugMode()
  1085. {
  1086. Configure::write('debug', false);
  1087. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1088. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1089. $result = $this->Form->secure($fields);
  1090. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1091. $hash .= ':' . 'Model.valid';
  1092. $hash = urlencode($hash);
  1093. $expected = [
  1094. 'div' => ['style' => 'display:none;'],
  1095. ['input' => [
  1096. 'type' => 'hidden',
  1097. 'name' => '_Token[fields]',
  1098. 'autocomplete' => 'off',
  1099. 'value' => $hash,
  1100. ]],
  1101. ['input' => [
  1102. 'type' => 'hidden',
  1103. 'name' => '_Token[unlocked]',
  1104. 'autocomplete' => 'off',
  1105. 'value' => '',
  1106. ]],
  1107. '/div',
  1108. ];
  1109. $this->assertHtml($expected, $result);
  1110. }
  1111. /**
  1112. * Tests correct generation of number fields for smallint
  1113. *
  1114. * @return void
  1115. */
  1116. public function testTextFieldGenerationForSmallint()
  1117. {
  1118. $this->article['schema'] = [
  1119. 'foo' => [
  1120. 'type' => 'smallinteger',
  1121. 'null' => false,
  1122. 'default' => null,
  1123. 'length' => 10,
  1124. ],
  1125. ];
  1126. $this->Form->create($this->article);
  1127. $result = $this->Form->control('foo');
  1128. $this->assertStringContainsString('class="input number"', $result);
  1129. $this->assertStringContainsString('type="number"', $result);
  1130. }
  1131. /**
  1132. * Tests correct generation of number fields for tinyint
  1133. *
  1134. * @return void
  1135. */
  1136. public function testTextFieldGenerationForTinyint()
  1137. {
  1138. $this->article['schema'] = [
  1139. 'foo' => [
  1140. 'type' => 'tinyinteger',
  1141. 'null' => false,
  1142. 'default' => null,
  1143. 'length' => 10,
  1144. ],
  1145. ];
  1146. $this->Form->create($this->article);
  1147. $result = $this->Form->control('foo');
  1148. $this->assertStringContainsString('class="input number"', $result);
  1149. $this->assertStringContainsString('type="number"', $result);
  1150. }
  1151. /**
  1152. * Tests correct generation of number fields for double and float fields
  1153. *
  1154. * @return void
  1155. */
  1156. public function testTextFieldGenerationForFloats()
  1157. {
  1158. $this->article['schema'] = [
  1159. 'foo' => [
  1160. 'type' => 'float',
  1161. 'null' => false,
  1162. 'default' => null,
  1163. 'length' => 10,
  1164. ],
  1165. ];
  1166. $this->Form->create($this->article);
  1167. $result = $this->Form->control('foo');
  1168. $expected = [
  1169. 'div' => ['class' => 'input number'],
  1170. 'label' => ['for' => 'foo'],
  1171. 'Foo',
  1172. '/label',
  1173. ['input' => [
  1174. 'type' => 'number',
  1175. 'name' => 'foo',
  1176. 'id' => 'foo',
  1177. 'step' => 'any',
  1178. ]],
  1179. '/div',
  1180. ];
  1181. $this->assertHtml($expected, $result);
  1182. $result = $this->Form->control('foo', ['step' => 0.5]);
  1183. $expected = [
  1184. 'div' => ['class' => 'input number'],
  1185. 'label' => ['for' => 'foo'],
  1186. 'Foo',
  1187. '/label',
  1188. ['input' => [
  1189. 'type' => 'number',
  1190. 'name' => 'foo',
  1191. 'id' => 'foo',
  1192. 'step' => '0.5',
  1193. ]],
  1194. '/div',
  1195. ];
  1196. $this->assertHtml($expected, $result);
  1197. }
  1198. /**
  1199. * Tests correct generation of number fields for integer fields
  1200. *
  1201. * @return void
  1202. */
  1203. public function testTextFieldTypeNumberGenerationForIntegers()
  1204. {
  1205. $this->getTableLocator()->get('Contacts', [
  1206. 'className' => ContactsTable::class,
  1207. ]);
  1208. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1209. $result = $this->Form->control('age');
  1210. $expected = [
  1211. 'div' => ['class' => 'input number'],
  1212. 'label' => ['for' => 'age'],
  1213. 'Age',
  1214. '/label',
  1215. ['input' => [
  1216. 'type' => 'number', 'name' => 'age',
  1217. 'id' => 'age',
  1218. ]],
  1219. '/div',
  1220. ];
  1221. $this->assertHtml($expected, $result);
  1222. }
  1223. /**
  1224. * Tests correct generation of file upload fields for binary fields
  1225. *
  1226. * @return void
  1227. */
  1228. public function testFileUploadFieldTypeGenerationForBinaries()
  1229. {
  1230. $table = $this->getTableLocator()->get('Contacts', [
  1231. 'className' => ContactsTable::class,
  1232. ]);
  1233. $table->setSchema(['foo' => [
  1234. 'type' => 'binary',
  1235. 'null' => false,
  1236. 'default' => null,
  1237. 'length' => 1024,
  1238. ]]);
  1239. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1240. $result = $this->Form->control('foo');
  1241. $expected = [
  1242. 'div' => ['class' => 'input file'],
  1243. 'label' => ['for' => 'foo'],
  1244. 'Foo',
  1245. '/label',
  1246. ['input' => [
  1247. 'type' => 'file', 'name' => 'foo',
  1248. 'id' => 'foo',
  1249. ]],
  1250. '/div',
  1251. ];
  1252. $this->assertHtml($expected, $result);
  1253. }
  1254. /**
  1255. * testFormSecurityMultipleFields method
  1256. *
  1257. * Test secure() with multiple row form. Ensure hash is correct.
  1258. *
  1259. * @return void
  1260. */
  1261. public function testFormSecurityMultipleFields()
  1262. {
  1263. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1264. $fields = [
  1265. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1266. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1267. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1268. ];
  1269. $result = $this->Form->secure($fields);
  1270. $hash = '8670192c3f040bf58680479060b4755b7a5c3596' .
  1271. '%3AModel.0.hidden%7CModel.0.valid%7CModel.1.hidden%7CModel.1.valid';
  1272. $tokenDebug = urlencode(json_encode([
  1273. '',
  1274. $fields,
  1275. [],
  1276. ]));
  1277. $expected = [
  1278. 'div' => ['style' => 'display:none;'],
  1279. ['input' => [
  1280. 'type' => 'hidden',
  1281. 'name' => '_Token[fields]',
  1282. 'value' => $hash,
  1283. 'autocomplete' => 'off',
  1284. ]],
  1285. ['input' => [
  1286. 'type' => 'hidden',
  1287. 'name' => '_Token[unlocked]',
  1288. 'autocomplete' => 'off',
  1289. 'value' => '',
  1290. ]],
  1291. ['input' => [
  1292. 'type' => 'hidden',
  1293. 'name' => '_Token[debug]',
  1294. 'value' => $tokenDebug,
  1295. 'autocomplete' => 'off',
  1296. ]],
  1297. '/div',
  1298. ];
  1299. $this->assertHtml($expected, $result);
  1300. }
  1301. /**
  1302. * testFormSecurityMultipleSubmitButtons
  1303. *
  1304. * test form submit generation and ensure that _Token is only created on end()
  1305. *
  1306. * @return void
  1307. */
  1308. public function testFormSecurityMultipleSubmitButtons()
  1309. {
  1310. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1311. $this->Form->create($this->article);
  1312. $this->Form->text('Address.title');
  1313. $this->Form->text('Address.first_name');
  1314. $result = $this->Form->submit('Save', ['name' => 'save']);
  1315. $expected = [
  1316. 'div' => ['class' => 'submit'],
  1317. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1318. '/div',
  1319. ];
  1320. $this->assertHtml($expected, $result);
  1321. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1322. $expected = [
  1323. 'div' => ['class' => 'submit'],
  1324. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1325. '/div',
  1326. ];
  1327. $this->assertHtml($expected, $result);
  1328. $result = $this->Form->end();
  1329. $tokenDebug = urlencode(json_encode([
  1330. '/articles/add',
  1331. [
  1332. 'Address.title',
  1333. 'Address.first_name',
  1334. ],
  1335. ['save', 'cancel'],
  1336. ]));
  1337. $expected = [
  1338. 'div' => ['style' => 'display:none;'],
  1339. ['input' => [
  1340. 'type' => 'hidden',
  1341. 'name' => '_Token[fields]',
  1342. 'autocomplete',
  1343. 'value',
  1344. ]],
  1345. ['input' => [
  1346. 'type' => 'hidden',
  1347. 'name' => '_Token[unlocked]',
  1348. 'value' => 'cancel%7Csave',
  1349. 'autocomplete' => 'off',
  1350. ]],
  1351. ['input' => [
  1352. 'type' => 'hidden',
  1353. 'name' => '_Token[debug]',
  1354. 'value' => $tokenDebug,
  1355. 'autocomplete' => 'off',
  1356. ]],
  1357. '/div',
  1358. ];
  1359. $this->assertHtml($expected, $result);
  1360. }
  1361. /**
  1362. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1363. *
  1364. * @return void
  1365. */
  1366. public function testSecurityButtonNestedNamed()
  1367. {
  1368. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1369. $this->Form->create('Addresses');
  1370. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1371. $result = $this->Form->unlockField();
  1372. $this->assertEquals(['Address.button'], $result);
  1373. }
  1374. /**
  1375. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1376. *
  1377. * @return void
  1378. */
  1379. public function testSecuritySubmitNestedNamed()
  1380. {
  1381. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1382. $this->Form->create($this->article);
  1383. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1384. $result = $this->Form->unlockField();
  1385. $this->assertEquals(['Address.button'], $result);
  1386. }
  1387. /**
  1388. * Test that the correct fields are unlocked for image submits with no names.
  1389. *
  1390. * @return void
  1391. */
  1392. public function testSecuritySubmitImageNoName()
  1393. {
  1394. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1395. $this->Form->create(false);
  1396. $result = $this->Form->submit('save.png');
  1397. $expected = [
  1398. 'div' => ['class' => 'submit'],
  1399. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1400. '/div',
  1401. ];
  1402. $this->assertHtml($expected, $result);
  1403. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1404. }
  1405. /**
  1406. * Test that the correct fields are unlocked for image submits with names.
  1407. *
  1408. * @return void
  1409. */
  1410. public function testSecuritySubmitImageName()
  1411. {
  1412. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1413. $this->Form->create(null);
  1414. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1415. $expected = [
  1416. 'div' => ['class' => 'submit'],
  1417. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1418. '/div',
  1419. ];
  1420. $this->assertHtml($expected, $result);
  1421. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1422. }
  1423. /**
  1424. * testFormSecurityMultipleControlFields method
  1425. *
  1426. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1427. *
  1428. * @return void
  1429. */
  1430. public function testFormSecurityMultipleControlFields()
  1431. {
  1432. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1433. $this->Form->create();
  1434. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1435. $this->Form->control('Addresses.0.title');
  1436. $this->Form->control('Addresses.0.first_name');
  1437. $this->Form->control('Addresses.0.last_name');
  1438. $this->Form->control('Addresses.0.address');
  1439. $this->Form->control('Addresses.0.city');
  1440. $this->Form->control('Addresses.0.phone');
  1441. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1442. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1443. $this->Form->control('Addresses.1.title');
  1444. $this->Form->control('Addresses.1.first_name');
  1445. $this->Form->control('Addresses.1.last_name');
  1446. $this->Form->control('Addresses.1.address');
  1447. $this->Form->control('Addresses.1.city');
  1448. $this->Form->control('Addresses.1.phone');
  1449. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1450. $result = $this->Form->secure($this->Form->fields);
  1451. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1452. $tokenDebug = urlencode(json_encode([
  1453. '/articles/add',
  1454. [
  1455. 'Addresses.0.id' => '123456',
  1456. 'Addresses.0.title',
  1457. 'Addresses.0.first_name',
  1458. 'Addresses.0.last_name',
  1459. 'Addresses.0.address',
  1460. 'Addresses.0.city',
  1461. 'Addresses.0.phone',
  1462. 'Addresses.0.primary',
  1463. 'Addresses.1.id' => '654321',
  1464. 'Addresses.1.title',
  1465. 'Addresses.1.first_name',
  1466. 'Addresses.1.last_name',
  1467. 'Addresses.1.address',
  1468. 'Addresses.1.city',
  1469. 'Addresses.1.phone',
  1470. 'Addresses.1.primary',
  1471. ],
  1472. [],
  1473. ]));
  1474. $expected = [
  1475. 'div' => ['style' => 'display:none;'],
  1476. ['input' => [
  1477. 'type' => 'hidden',
  1478. 'name' => '_Token[fields]',
  1479. 'value' => $hash,
  1480. 'autocomplete' => 'off',
  1481. ]],
  1482. ['input' => [
  1483. 'type' => 'hidden',
  1484. 'name' => '_Token[unlocked]',
  1485. 'autocomplete' => 'off',
  1486. 'value' => '',
  1487. ]],
  1488. ['input' => [
  1489. 'type' => 'hidden',
  1490. 'name' => '_Token[debug]',
  1491. 'value' => $tokenDebug,
  1492. 'autocomplete' => 'off',
  1493. ]],
  1494. '/div',
  1495. ];
  1496. $this->assertHtml($expected, $result);
  1497. }
  1498. /**
  1499. * testFormSecurityArrayFields method
  1500. *
  1501. * Test form security with Model.field.0 style inputs.
  1502. *
  1503. * @return void
  1504. */
  1505. public function testFormSecurityArrayFields()
  1506. {
  1507. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1508. $this->Form->create();
  1509. $this->Form->text('Address.primary.1');
  1510. $this->assertSame('Address.primary', $this->Form->fields[0]);
  1511. $this->Form->text('Address.secondary.1.0');
  1512. $this->assertSame('Address.secondary', $this->Form->fields[1]);
  1513. }
  1514. /**
  1515. * testFormSecurityMultipleControlDisabledFields method
  1516. *
  1517. * Test secure form generation with multiple records and disabled fields.
  1518. *
  1519. * @return void
  1520. */
  1521. public function testFormSecurityMultipleControlDisabledFields()
  1522. {
  1523. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1524. 'unlockedFields' => ['first_name', 'address'],
  1525. ]));
  1526. $this->Form->create();
  1527. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1528. $this->Form->text('Addresses.0.title');
  1529. $this->Form->text('Addresses.0.first_name');
  1530. $this->Form->text('Addresses.0.last_name');
  1531. $this->Form->text('Addresses.0.address');
  1532. $this->Form->text('Addresses.0.city');
  1533. $this->Form->text('Addresses.0.phone');
  1534. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1535. $this->Form->text('Addresses.1.title');
  1536. $this->Form->text('Addresses.1.first_name');
  1537. $this->Form->text('Addresses.1.last_name');
  1538. $this->Form->text('Addresses.1.address');
  1539. $this->Form->text('Addresses.1.city');
  1540. $this->Form->text('Addresses.1.phone');
  1541. $result = $this->Form->secure($this->Form->fields);
  1542. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1543. $tokenDebug = urlencode(json_encode([
  1544. '/articles/add',
  1545. [
  1546. 'Addresses.0.id' => '123456',
  1547. 'Addresses.0.title',
  1548. 'Addresses.0.last_name',
  1549. 'Addresses.0.city',
  1550. 'Addresses.0.phone',
  1551. 'Addresses.1.id' => '654321',
  1552. 'Addresses.1.title',
  1553. 'Addresses.1.last_name',
  1554. 'Addresses.1.city',
  1555. 'Addresses.1.phone',
  1556. ],
  1557. [
  1558. 'first_name',
  1559. 'address',
  1560. ],
  1561. ]));
  1562. $expected = [
  1563. 'div' => ['style' => 'display:none;'],
  1564. ['input' => [
  1565. 'type' => 'hidden',
  1566. 'name' => '_Token[fields]',
  1567. 'autocomplete' => 'off',
  1568. 'value' => $hash,
  1569. ]],
  1570. ['input' => [
  1571. 'type' => 'hidden',
  1572. 'name' => '_Token[unlocked]',
  1573. 'autocomplete' => 'off',
  1574. 'value' => 'address%7Cfirst_name',
  1575. ]],
  1576. ['input' => [
  1577. 'type' => 'hidden',
  1578. 'name' => '_Token[debug]',
  1579. 'autocomplete' => 'off',
  1580. 'value' => $tokenDebug,
  1581. ]],
  1582. '/div',
  1583. ];
  1584. $this->assertHtml($expected, $result);
  1585. }
  1586. /**
  1587. * testFormSecurityControlDisabledFields method
  1588. *
  1589. * Test single record form with disabled fields.
  1590. *
  1591. * @return void
  1592. */
  1593. public function testFormSecurityControlUnlockedFields()
  1594. {
  1595. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1596. 'unlockedFields' => ['first_name', 'address'],
  1597. ]));
  1598. $this->Form->create();
  1599. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1600. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1601. $this->Form->text('Addresses.title');
  1602. $this->Form->text('Addresses.first_name');
  1603. $this->Form->text('Addresses.last_name');
  1604. $this->Form->text('Addresses.address');
  1605. $this->Form->text('Addresses.city');
  1606. $this->Form->text('Addresses.phone');
  1607. $result = $this->Form->fields;
  1608. $expected = [
  1609. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1610. 'Addresses.city', 'Addresses.phone',
  1611. ];
  1612. $this->assertEquals($expected, $result);
  1613. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1614. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1615. $tokenDebug = urlencode(json_encode([
  1616. '/articles/add',
  1617. [
  1618. 'Addresses.id' => '123456',
  1619. 'Addresses.title',
  1620. 'Addresses.last_name',
  1621. 'Addresses.city',
  1622. 'Addresses.phone',
  1623. ],
  1624. [
  1625. 'first_name',
  1626. 'address',
  1627. ],
  1628. ]));
  1629. $expected = [
  1630. 'div' => ['style' => 'display:none;'],
  1631. ['input' => [
  1632. 'type' => 'hidden',
  1633. 'name' => '_Token[fields]',
  1634. 'value' => $hash,
  1635. 'autocomplete' => 'off',
  1636. 'data-foo' => 'bar',
  1637. ]],
  1638. ['input' => [
  1639. 'type' => 'hidden',
  1640. 'name' => '_Token[unlocked]',
  1641. 'value' => 'address%7Cfirst_name',
  1642. 'autocomplete' => 'off',
  1643. 'data-foo' => 'bar',
  1644. ]],
  1645. ['input' => [
  1646. 'type' => 'hidden', 'name' => '_Token[debug]',
  1647. 'value' => $tokenDebug,
  1648. 'autocomplete' => 'off',
  1649. 'data-foo' => 'bar',
  1650. ]],
  1651. '/div',
  1652. ];
  1653. $this->assertHtml($expected, $result);
  1654. }
  1655. /**
  1656. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1657. *
  1658. * Test single record form with debugSecurity param.
  1659. *
  1660. * @return void
  1661. */
  1662. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1663. {
  1664. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1665. 'unlockedFields' => ['first_name', 'address'],
  1666. ]));
  1667. $this->Form->create();
  1668. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1669. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1670. $this->Form->text('Addresses.title');
  1671. $this->Form->text('Addresses.first_name');
  1672. $this->Form->text('Addresses.last_name');
  1673. $this->Form->text('Addresses.address');
  1674. $this->Form->text('Addresses.city');
  1675. $this->Form->text('Addresses.phone');
  1676. $result = $this->Form->fields;
  1677. $expected = [
  1678. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1679. 'Addresses.city', 'Addresses.phone',
  1680. ];
  1681. $this->assertEquals($expected, $result);
  1682. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1683. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1684. $tokenDebug = urlencode(json_encode([
  1685. '/articles/add',
  1686. [
  1687. 'Addresses.id' => '123456',
  1688. 'Addresses.title',
  1689. 'Addresses.last_name',
  1690. 'Addresses.city',
  1691. 'Addresses.phone',
  1692. ],
  1693. [
  1694. 'first_name',
  1695. 'address',
  1696. ],
  1697. ]));
  1698. $expected = [
  1699. 'div' => ['style' => 'display:none;'],
  1700. ['input' => [
  1701. 'type' => 'hidden',
  1702. 'name' => '_Token[fields]',
  1703. 'value' => $hash,
  1704. 'autocomplete' => 'off',
  1705. 'data-foo' => 'bar',
  1706. ]],
  1707. ['input' => [
  1708. 'type' => 'hidden',
  1709. 'name' => '_Token[unlocked]',
  1710. 'value' => 'address%7Cfirst_name',
  1711. 'autocomplete' => 'off',
  1712. 'data-foo' => 'bar',
  1713. ]],
  1714. ['input' => [
  1715. 'type' => 'hidden', 'name' => '_Token[debug]',
  1716. 'value' => $tokenDebug,
  1717. 'autocomplete' => 'off',
  1718. 'data-foo' => 'bar',
  1719. ]],
  1720. '/div',
  1721. ];
  1722. $this->assertHtml($expected, $result);
  1723. }
  1724. /**
  1725. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1726. *
  1727. * Debug is false, debugSecurity is true -> no debug
  1728. *
  1729. * @return void
  1730. */
  1731. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1732. {
  1733. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1734. 'unlockedFields' => ['first_name', 'address'],
  1735. ]));
  1736. $this->Form->create();
  1737. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1738. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1739. $this->Form->text('Addresses.title');
  1740. $this->Form->text('Addresses.first_name');
  1741. $this->Form->text('Addresses.last_name');
  1742. $this->Form->text('Addresses.address');
  1743. $this->Form->text('Addresses.city');
  1744. $this->Form->text('Addresses.phone');
  1745. $result = $this->Form->fields;
  1746. $expected = [
  1747. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1748. 'Addresses.city', 'Addresses.phone',
  1749. ];
  1750. $this->assertEquals($expected, $result);
  1751. Configure::write('debug', false);
  1752. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1753. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1754. $expected = [
  1755. 'div' => ['style' => 'display:none;'],
  1756. ['input' => [
  1757. 'type' => 'hidden',
  1758. 'name' => '_Token[fields]',
  1759. 'value' => $hash,
  1760. 'autocomplete' => 'off',
  1761. 'data-foo' => 'bar',
  1762. ]],
  1763. ['input' => [
  1764. 'type' => 'hidden',
  1765. 'name' => '_Token[unlocked]',
  1766. 'value' => 'address%7Cfirst_name',
  1767. 'autocomplete' => 'off',
  1768. 'data-foo' => 'bar',
  1769. ]],
  1770. '/div',
  1771. ];
  1772. $this->assertHtml($expected, $result);
  1773. }
  1774. /**
  1775. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1776. *
  1777. * Test single record form with debugSecurity param.
  1778. *
  1779. * @return void
  1780. */
  1781. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1782. {
  1783. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1784. 'unlockedFields' => ['first_name', 'address'],
  1785. ]));
  1786. $this->Form->create();
  1787. $this->assertEquals(
  1788. $this->View->getRequest()->getParam('_Token.unlockedFields'),
  1789. $this->Form->unlockField()
  1790. );
  1791. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1792. $this->Form->text('Addresses.title');
  1793. $this->Form->text('Addresses.first_name');
  1794. $this->Form->text('Addresses.last_name');
  1795. $this->Form->text('Addresses.address');
  1796. $this->Form->text('Addresses.city');
  1797. $this->Form->text('Addresses.phone');
  1798. $result = $this->Form->fields;
  1799. $expected = [
  1800. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1801. 'Addresses.city', 'Addresses.phone',
  1802. ];
  1803. $this->assertEquals($expected, $result);
  1804. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1805. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1806. $expected = [
  1807. 'div' => ['style' => 'display:none;'],
  1808. ['input' => [
  1809. 'type' => 'hidden',
  1810. 'name' => '_Token[fields]',
  1811. 'value' => $hash,
  1812. 'autocomplete' => 'off',
  1813. 'data-foo' => 'bar',
  1814. ]],
  1815. ['input' => [
  1816. 'type' => 'hidden',
  1817. 'name' => '_Token[unlocked]',
  1818. 'value' => 'address%7Cfirst_name',
  1819. 'autocomplete' => 'off',
  1820. 'data-foo' => 'bar',
  1821. ]],
  1822. '/div',
  1823. ];
  1824. $this->assertHtml($expected, $result);
  1825. }
  1826. /**
  1827. * testFormSecureWithCustomNameAttribute method
  1828. *
  1829. * Test securing inputs with custom name attributes.
  1830. *
  1831. * @return void
  1832. */
  1833. public function testFormSecureWithCustomNameAttribute()
  1834. {
  1835. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1836. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1837. $this->assertSame('User.custom', $this->Form->fields[0]);
  1838. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1839. $this->assertSame('User.custom.another.value', $this->Form->fields[1]);
  1840. }
  1841. /**
  1842. * testFormSecuredControl method
  1843. *
  1844. * Test generation of entire secure form, assertions made on control() output.
  1845. *
  1846. * @return void
  1847. */
  1848. public function testFormSecuredControl()
  1849. {
  1850. $this->View->setRequest($this->View->getRequest()
  1851. ->withParam('_Token', 'stuff')
  1852. ->withParam('_csrfToken', 'testKey'));
  1853. $this->article['schema'] = [
  1854. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1855. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1856. ];
  1857. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1858. $encoding = strtolower(Configure::read('App.encoding'));
  1859. $expected = [
  1860. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1861. 'div' => ['style' => 'display:none;'],
  1862. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1863. ['input' => [
  1864. 'type' => 'hidden',
  1865. 'name' => '_csrfToken',
  1866. 'value' => 'testKey',
  1867. 'autocomplete' => 'off',
  1868. ]],
  1869. '/div',
  1870. ];
  1871. $this->assertHtml($expected, $result);
  1872. $result = $this->Form->control('ratio');
  1873. $expected = [
  1874. 'div' => ['class'],
  1875. 'label' => ['for'],
  1876. 'Ratio',
  1877. '/label',
  1878. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1879. '/div',
  1880. ];
  1881. $this->assertHtml($expected, $result);
  1882. $result = $this->Form->control('population');
  1883. $expected = [
  1884. 'div' => ['class'],
  1885. 'label' => ['for'],
  1886. 'Population',
  1887. '/label',
  1888. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1889. '/div',
  1890. ];
  1891. $this->assertHtml($expected, $result);
  1892. $result = $this->Form->control('published', ['type' => 'text']);
  1893. $expected = [
  1894. 'div' => ['class' => 'input text'],
  1895. 'label' => ['for' => 'published'],
  1896. 'Published',
  1897. '/label',
  1898. ['input' => [
  1899. 'type' => 'text',
  1900. 'name' => 'published',
  1901. 'id' => 'published',
  1902. ]],
  1903. '/div',
  1904. ];
  1905. $this->assertHtml($expected, $result);
  1906. $result = $this->Form->control('other', ['type' => 'text']);
  1907. $expected = [
  1908. 'div' => ['class' => 'input text'],
  1909. 'label' => ['for' => 'other'],
  1910. 'Other',
  1911. '/label',
  1912. ['input' => [
  1913. 'type' => 'text',
  1914. 'name' => 'other',
  1915. 'id',
  1916. ]],
  1917. '/div',
  1918. ];
  1919. $this->assertHtml($expected, $result);
  1920. $result = $this->Form->hidden('stuff');
  1921. $expected = [
  1922. 'input' => [
  1923. 'type' => 'hidden',
  1924. 'name' => 'stuff',
  1925. ],
  1926. ];
  1927. $this->assertHtml($expected, $result);
  1928. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1929. $expected = ['input' => [
  1930. 'type' => 'hidden',
  1931. 'name' => 'hidden',
  1932. 'value' => '0',
  1933. ]];
  1934. $this->assertHtml($expected, $result);
  1935. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1936. $expected = [
  1937. 'div' => ['class' => 'input checkbox'],
  1938. ['input' => [
  1939. 'type' => 'hidden',
  1940. 'name' => 'something',
  1941. 'value' => '0',
  1942. ]],
  1943. 'label' => ['for' => 'something'],
  1944. ['input' => [
  1945. 'type' => 'checkbox',
  1946. 'name' => 'something',
  1947. 'value' => '1',
  1948. 'id' => 'something',
  1949. ]],
  1950. 'Something',
  1951. '/label',
  1952. '/div',
  1953. ];
  1954. $this->assertHtml($expected, $result);
  1955. $result = $this->Form->fields;
  1956. $expectedFields = [
  1957. 'ratio',
  1958. 'population',
  1959. 'published',
  1960. 'other',
  1961. 'stuff' => '',
  1962. 'hidden' => '0',
  1963. 'something',
  1964. ];
  1965. $this->assertEquals($expectedFields, $result);
  1966. $result = $this->Form->secure($this->Form->fields);
  1967. $tokenDebug = urlencode(json_encode([
  1968. '/articles/add',
  1969. $expectedFields,
  1970. [],
  1971. ]));
  1972. $expected = [
  1973. 'div' => ['style' => 'display:none;'],
  1974. ['input' => [
  1975. 'type' => 'hidden',
  1976. 'name' => '_Token[fields]',
  1977. 'value',
  1978. 'autocomplete',
  1979. ]],
  1980. ['input' => [
  1981. 'type' => 'hidden',
  1982. 'name' => '_Token[unlocked]',
  1983. 'value' => '',
  1984. 'autocomplete' => 'off',
  1985. ]],
  1986. ['input' => [
  1987. 'type' => 'hidden', 'name' => '_Token[debug]',
  1988. 'value' => $tokenDebug,
  1989. 'autocomplete' => 'off',
  1990. ]],
  1991. '/div',
  1992. ];
  1993. $this->assertHtml($expected, $result);
  1994. }
  1995. /**
  1996. * testSecuredControlCustomName method
  1997. *
  1998. * Test secured inputs with custom names.
  1999. *
  2000. * @return void
  2001. */
  2002. public function testSecuredControlCustomName()
  2003. {
  2004. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2005. $this->assertEquals([], $this->Form->fields);
  2006. $this->Form->text('text_input', [
  2007. 'name' => 'Option[General.default_role]',
  2008. ]);
  2009. $expected = ['Option.General.default_role'];
  2010. $this->assertEquals($expected, $this->Form->fields);
  2011. $this->Form->select('select_box', [1, 2], [
  2012. 'name' => 'Option[General.select_role]',
  2013. ]);
  2014. $expected[] = 'Option.General.select_role';
  2015. $this->assertEquals($expected, $this->Form->fields);
  2016. $this->Form->text('other.things[]');
  2017. $expected[] = 'other.things';
  2018. $this->assertEquals($expected, $this->Form->fields);
  2019. }
  2020. /**
  2021. * testSecuredControlDuplicate method
  2022. *
  2023. * Test that a hidden field followed by a visible field
  2024. * undoes the hidden field locking.
  2025. *
  2026. * @return void
  2027. */
  2028. public function testSecuredControlDuplicate()
  2029. {
  2030. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2031. $this->assertEquals([], $this->Form->fields);
  2032. $this->Form->control('text_val', [
  2033. 'type' => 'hidden',
  2034. 'value' => 'some text',
  2035. ]);
  2036. $expected = ['text_val' => 'some text'];
  2037. $this->assertEquals($expected, $this->Form->fields);
  2038. $this->Form->control('text_val', [
  2039. 'type' => 'text',
  2040. ]);
  2041. $expected = ['text_val'];
  2042. $this->assertEquals($expected, $this->Form->fields);
  2043. }
  2044. /**
  2045. * testFormSecuredFileControl method
  2046. *
  2047. * Tests that the correct keys are added to the field hash index.
  2048. *
  2049. * @return void
  2050. */
  2051. public function testFormSecuredFileControl()
  2052. {
  2053. $this->assertEquals([], $this->Form->fields);
  2054. $this->Form->file('Attachment.file');
  2055. $expected = [
  2056. 'Attachment.file.name', 'Attachment.file.type',
  2057. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2058. 'Attachment.file.size',
  2059. ];
  2060. $this->assertEquals($expected, $this->Form->fields);
  2061. }
  2062. /**
  2063. * testFormSecuredMultipleSelect method
  2064. *
  2065. * Test that multiple selects keys are added to field hash.
  2066. *
  2067. * @return void
  2068. */
  2069. public function testFormSecuredMultipleSelect()
  2070. {
  2071. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  2072. $this->assertEquals([], $this->Form->fields);
  2073. $options = ['1' => 'one', '2' => 'two'];
  2074. $this->Form->select('Model.select', $options);
  2075. $expected = ['Model.select'];
  2076. $this->assertEquals($expected, $this->Form->fields);
  2077. $this->Form->fields = [];
  2078. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2079. $this->assertEquals($expected, $this->Form->fields);
  2080. }
  2081. /**
  2082. * testFormSecuredRadio method
  2083. *
  2084. * @return void
  2085. */
  2086. public function testFormSecuredRadio()
  2087. {
  2088. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2089. $this->assertEquals([], $this->Form->fields);
  2090. $options = ['1' => 'option1', '2' => 'option2'];
  2091. $this->Form->radio('Test.test', $options);
  2092. $expected = ['Test.test'];
  2093. $this->assertEquals($expected, $this->Form->fields);
  2094. $this->Form->radio('Test.all', $options, [
  2095. 'disabled' => ['option1', 'option2'],
  2096. ]);
  2097. $expected = ['Test.test', 'Test.all' => ''];
  2098. $this->assertEquals($expected, $this->Form->fields);
  2099. $this->Form->radio('Test.some', $options, [
  2100. 'disabled' => ['option1'],
  2101. ]);
  2102. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2103. $this->assertEquals($expected, $this->Form->fields);
  2104. }
  2105. /**
  2106. * testFormSecuredAndDisabledNotAssoc method
  2107. *
  2108. * Test that when disabled is in a list based attribute array it works.
  2109. *
  2110. * @return void
  2111. */
  2112. public function testFormSecuredAndDisabledNotAssoc()
  2113. {
  2114. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2115. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2116. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2117. $this->Form->text('Model.text', ['disabled']);
  2118. $this->Form->textarea('Model.textarea', ['disabled']);
  2119. $this->Form->password('Model.password', ['disabled']);
  2120. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2121. $expected = [
  2122. 'Model.radio' => '',
  2123. ];
  2124. $this->assertEquals($expected, $this->Form->fields);
  2125. }
  2126. /**
  2127. * testFormSecuredAndDisabled method
  2128. *
  2129. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2130. * hashing.
  2131. *
  2132. * @return void
  2133. */
  2134. public function testFormSecuredAndDisabled()
  2135. {
  2136. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2137. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2138. $this->Form->text('Model.text', ['disabled' => true]);
  2139. $this->Form->password('Model.text', ['disabled' => true]);
  2140. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2141. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2142. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2143. $this->Form->year('Model.year', ['disabled' => true]);
  2144. $this->Form->month('Model.month', ['disabled' => true]);
  2145. $this->Form->day('Model.day', ['disabled' => true]);
  2146. $this->Form->hour('Model.hour', ['disabled' => true]);
  2147. $this->Form->minute('Model.minute', ['disabled' => true]);
  2148. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2149. $expected = [
  2150. 'Model.radio' => '',
  2151. ];
  2152. $this->assertEquals($expected, $this->Form->fields);
  2153. }
  2154. /**
  2155. * testDisableSecurityUsingForm method
  2156. *
  2157. * @return void
  2158. */
  2159. public function testDisableSecurityUsingForm()
  2160. {
  2161. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2162. 'disabledFields' => [],
  2163. ]));
  2164. $this->Form->create();
  2165. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2166. $this->Form->text('Addresses.title');
  2167. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2168. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2169. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2170. $result = $this->Form->fields;
  2171. $expected = [
  2172. 'Addresses.id' => '123456', 'Addresses.title',
  2173. ];
  2174. $this->assertEquals($expected, $result);
  2175. }
  2176. /**
  2177. * testUnlockFieldAddsToList method
  2178. *
  2179. * Test disableField.
  2180. *
  2181. * @return void
  2182. */
  2183. public function testUnlockFieldAddsToList()
  2184. {
  2185. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2186. 'unlockedFields' => [],
  2187. ]));
  2188. $this->Form->unlockField('Contact.name');
  2189. $this->Form->text('Contact.name');
  2190. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2191. $this->assertEquals([], $this->Form->fields);
  2192. }
  2193. /**
  2194. * testUnlockFieldRemovingFromFields method
  2195. *
  2196. * Test unlockField removing from fields array.
  2197. *
  2198. * @return void
  2199. */
  2200. public function testUnlockFieldRemovingFromFields()
  2201. {
  2202. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2203. 'unlockedFields' => [],
  2204. ]));
  2205. $this->Form->create($this->article);
  2206. $this->Form->hidden('Article.id', ['value' => 1]);
  2207. $this->Form->text('Article.title');
  2208. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2209. $this->assertContains('Article.title', $this->Form->fields, 'Field should be secured.');
  2210. $this->Form->unlockField('Article.title');
  2211. $this->Form->unlockField('Article.id');
  2212. $this->assertEquals([], $this->Form->fields);
  2213. }
  2214. /**
  2215. * testResetUnlockFields method
  2216. *
  2217. * Test reset unlockFields, when create new form.
  2218. *
  2219. * @return void
  2220. */
  2221. public function testResetUnlockFields()
  2222. {
  2223. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2224. 'key' => 'testKey',
  2225. 'unlockedFields' => [],
  2226. ]));
  2227. $this->Form->unlockField('Contact.id');
  2228. $this->Form->create('Contact');
  2229. $this->Form->hidden('Contact.id', ['value' => 1]);
  2230. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2231. $this->Form->end();
  2232. $this->Form->create('Contact');
  2233. $this->Form->hidden('Contact.id', ['value' => 1]);
  2234. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2235. }
  2236. /**
  2237. * testSecuredFormUrlIgnoresHost method
  2238. *
  2239. * Test that only the path + query elements of a form's URL show up in their hash.
  2240. *
  2241. * @return void
  2242. */
  2243. public function testSecuredFormUrlIgnoresHost()
  2244. {
  2245. $this->View->setRequest($this->View->getRequest()->withParam('_Token', ['key' => 'testKey']));
  2246. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2247. $this->Form->create($this->article, [
  2248. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2249. ]);
  2250. $result = $this->Form->secure();
  2251. $this->assertStringContainsString($expected, $result);
  2252. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2253. $result = $this->Form->secure();
  2254. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2255. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2256. $result = $this->Form->secure();
  2257. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2258. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2259. $result = $this->Form->secure();
  2260. $this->assertStringNotContainsString($expected, $result, 'URL is different');
  2261. }
  2262. /**
  2263. * testSecuredFormUrlHasHtmlAndIdentifier method
  2264. *
  2265. * Test that URL, HTML and identifier show up in their hashes.
  2266. *
  2267. * @return void
  2268. */
  2269. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2270. {
  2271. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2272. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2273. $this->Form->create($this->article, [
  2274. 'url' => [
  2275. 'controller' => 'articles',
  2276. 'action' => 'view',
  2277. '?' => [
  2278. 'page' => 1,
  2279. 'limit' => 10,
  2280. 'html' => '<>"',
  2281. ],
  2282. '#' => 'result',
  2283. ],
  2284. ]);
  2285. $result = $this->Form->secure();
  2286. $this->assertStringContainsString($expected, $result);
  2287. $this->Form->create($this->article, [
  2288. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2289. ]);
  2290. $result = $this->Form->secure();
  2291. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2292. $this->Form->create($this->article, [
  2293. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2294. ]);
  2295. $result = $this->Form->secure();
  2296. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2297. }
  2298. /**
  2299. * testErrorMessageDisplay method
  2300. *
  2301. * Test error message display.
  2302. *
  2303. * @return void
  2304. */
  2305. public function testErrorMessageDisplay()
  2306. {
  2307. $this->article['errors'] = [
  2308. 'Article' => [
  2309. 'title' => 'error message',
  2310. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2311. ],
  2312. ];
  2313. $this->Form->create($this->article);
  2314. $result = $this->Form->control('Article.title');
  2315. $expected = [
  2316. 'div' => ['class' => 'input text error'],
  2317. 'label' => ['for' => 'article-title'],
  2318. 'Title',
  2319. '/label',
  2320. 'input' => [
  2321. 'type' => 'text', 'name' => 'Article[title]',
  2322. 'id' => 'article-title', 'class' => 'form-error',
  2323. ],
  2324. ['div' => ['class' => 'error-message']],
  2325. 'error message',
  2326. '/div',
  2327. '/div',
  2328. ];
  2329. $this->assertHtml($expected, $result);
  2330. $result = $this->Form->control('Article.title', [
  2331. 'templates' => [
  2332. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2333. ],
  2334. ]);
  2335. $expected = [
  2336. 'div' => ['class' => 'input text error'],
  2337. 'label' => ['for' => 'article-title'],
  2338. 'Title',
  2339. '/label',
  2340. 'input' => [
  2341. 'type' => 'text', 'name' => 'Article[title]',
  2342. 'id' => 'article-title', 'class' => 'form-error',
  2343. ],
  2344. '/div',
  2345. ];
  2346. $this->assertHtml($expected, $result);
  2347. $result = $this->Form->control('Article.content');
  2348. $expected = [
  2349. 'div' => ['class' => 'input text error'],
  2350. 'label' => ['for' => 'article-content'],
  2351. 'Content',
  2352. '/label',
  2353. 'input' => [
  2354. 'type' => 'text', 'name' => 'Article[content]',
  2355. 'id' => 'article-content', 'class' => 'form-error',
  2356. ],
  2357. ['div' => ['class' => 'error-message']],
  2358. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2359. '/div',
  2360. '/div',
  2361. ];
  2362. $this->assertHtml($expected, $result);
  2363. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2364. $expected = [
  2365. 'div' => ['class' => 'input text error'],
  2366. 'label' => ['for' => 'article-content'],
  2367. 'Content',
  2368. '/label',
  2369. 'input' => [
  2370. 'type' => 'text', 'name' => 'Article[content]',
  2371. 'id' => 'article-content', 'class' => 'form-error',
  2372. ],
  2373. ['div' => ['class' => 'error-message']],
  2374. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2375. '/div',
  2376. '/div',
  2377. ];
  2378. $this->assertHtml($expected, $result);
  2379. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2380. $expected = [
  2381. 'div' => ['class' => 'input text error'],
  2382. 'label' => ['for' => 'article-content'],
  2383. 'Content',
  2384. '/label',
  2385. 'input' => [
  2386. 'type' => 'text', 'name' => 'Article[content]',
  2387. 'id' => 'article-content', 'class' => 'form-error',
  2388. ],
  2389. ['div' => ['class' => 'error-message']],
  2390. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2391. '/div',
  2392. '/div',
  2393. ];
  2394. $this->assertHtml($expected, $result);
  2395. }
  2396. /**
  2397. * testEmptyErrorValidation method
  2398. *
  2399. * Test validation errors, when validation message is an empty string.
  2400. *
  2401. * @return void
  2402. */
  2403. public function testEmptyErrorValidation()
  2404. {
  2405. $this->article['errors'] = [
  2406. 'Article' => ['title' => ''],
  2407. ];
  2408. $this->Form->create($this->article);
  2409. $result = $this->Form->control('Article.title');
  2410. $expected = [
  2411. 'div' => ['class' => 'input text error'],
  2412. 'label' => ['for' => 'article-title'],
  2413. 'Title',
  2414. '/label',
  2415. 'input' => [
  2416. 'type' => 'text', 'name' => 'Article[title]',
  2417. 'id' => 'article-title', 'class' => 'form-error',
  2418. ],
  2419. ['div' => ['class' => 'error-message']],
  2420. [],
  2421. '/div',
  2422. '/div',
  2423. ];
  2424. $this->assertHtml($expected, $result);
  2425. }
  2426. /**
  2427. * testEmptyControlErrorValidation method
  2428. *
  2429. * Test validation errors, when calling control() overriding validation message by an empty string.
  2430. *
  2431. * @return void
  2432. */
  2433. public function testEmptyControlErrorValidation()
  2434. {
  2435. $this->article['errors'] = [
  2436. 'Article' => ['title' => 'error message'],
  2437. ];
  2438. $this->Form->create($this->article);
  2439. $result = $this->Form->control('Article.title', ['error' => '']);
  2440. $expected = [
  2441. 'div' => ['class' => 'input text error'],
  2442. 'label' => ['for' => 'article-title'],
  2443. 'Title',
  2444. '/label',
  2445. 'input' => [
  2446. 'type' => 'text', 'name' => 'Article[title]',
  2447. 'id' => 'article-title', 'class' => 'form-error',
  2448. ],
  2449. ['div' => ['class' => 'error-message']],
  2450. [],
  2451. '/div',
  2452. '/div',
  2453. ];
  2454. $this->assertHtml($expected, $result);
  2455. }
  2456. /**
  2457. * testControlErrorMessage method
  2458. *
  2459. * Test validation errors, when calling control() overriding validation messages.
  2460. *
  2461. * @return void
  2462. */
  2463. public function testControlErrorMessage()
  2464. {
  2465. $this->article['errors'] = [
  2466. 'title' => ['error message'],
  2467. ];
  2468. $this->Form->create($this->article);
  2469. $result = $this->Form->control('title', [
  2470. 'error' => 'Custom error!',
  2471. ]);
  2472. $expected = [
  2473. 'div' => ['class' => 'input text required error'],
  2474. 'label' => ['for' => 'title'],
  2475. 'Title',
  2476. '/label',
  2477. 'input' => [
  2478. 'type' => 'text', 'name' => 'title',
  2479. 'id' => 'title', 'class' => 'form-error',
  2480. 'required' => 'required',
  2481. 'oninvalid' => 'this.setCustomValidity(&#039;This field is required&#039;)',
  2482. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  2483. ],
  2484. ['div' => ['class' => 'error-message']],
  2485. 'Custom error!',
  2486. '/div',
  2487. '/div',
  2488. ];
  2489. $this->assertHtml($expected, $result);
  2490. $result = $this->Form->control('title', [
  2491. 'error' => ['error message' => 'Custom error!'],
  2492. ]);
  2493. $expected = [
  2494. 'div' => ['class' => 'input text required error'],
  2495. 'label' => ['for' => 'title'],
  2496. 'Title',
  2497. '/label',
  2498. 'input' => [
  2499. 'type' => 'text',
  2500. 'name' => 'title',
  2501. 'id' => 'title',
  2502. 'class' => 'form-error',
  2503. 'required' => 'required',
  2504. 'oninvalid' => 'this.setCustomValidity(&#039;This field is required&#039;)',
  2505. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  2506. ],
  2507. ['div' => ['class' => 'error-message']],
  2508. 'Custom error!',
  2509. '/div',
  2510. '/div',
  2511. ];
  2512. $this->assertHtml($expected, $result);
  2513. }
  2514. /**
  2515. * testFormValidationAssociated method
  2516. *
  2517. * Tests displaying errors for nested entities.
  2518. *
  2519. * @return void
  2520. */
  2521. public function testFormValidationAssociated()
  2522. {
  2523. $nested = new Entity(['foo' => 'bar']);
  2524. $nested->setError('foo', ['not a valid bar']);
  2525. $entity = new Entity(['nested' => $nested]);
  2526. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2527. $result = $this->Form->error('nested.foo');
  2528. $this->assertSame('<div class="error-message">not a valid bar</div>', $result);
  2529. }
  2530. /**
  2531. * testFormValidationAssociatedSecondLevel method
  2532. *
  2533. * Test form error display with associated model.
  2534. *
  2535. * @return void
  2536. */
  2537. public function testFormValidationAssociatedSecondLevel()
  2538. {
  2539. $inner = new Entity(['bar' => 'baz']);
  2540. $nested = new Entity(['foo' => $inner]);
  2541. $entity = new Entity(['nested' => $nested]);
  2542. $inner->setError('bar', ['not a valid one']);
  2543. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2544. $result = $this->Form->error('nested.foo.bar');
  2545. $this->assertSame('<div class="error-message">not a valid one</div>', $result);
  2546. }
  2547. /**
  2548. * testFormValidationMultiRecord method
  2549. *
  2550. * Test form error display with multiple records.
  2551. *
  2552. * @return void
  2553. */
  2554. public function testFormValidationMultiRecord()
  2555. {
  2556. $one = new Entity();
  2557. $two = new Entity();
  2558. $this->getTableLocator()->get('Contacts', [
  2559. 'className' => ContactsTable::class,
  2560. ]);
  2561. $one->set('email', '');
  2562. $one->setError('email', ['invalid email']);
  2563. $two->set('name', '');
  2564. $two->setError('name', ['This is wrong']);
  2565. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2566. $result = $this->Form->control('0.email');
  2567. $expected = [
  2568. 'div' => ['class' => 'input email error'],
  2569. 'label' => ['for' => '0-email'],
  2570. 'Email',
  2571. '/label',
  2572. 'input' => [
  2573. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2574. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2575. ],
  2576. ['div' => ['class' => 'error-message']],
  2577. 'invalid email',
  2578. '/div',
  2579. '/div',
  2580. ];
  2581. $this->assertHtml($expected, $result);
  2582. $result = $this->Form->control('1.name');
  2583. $expected = [
  2584. 'div' => ['class' => 'input text error'],
  2585. 'label' => ['for' => '1-name'],
  2586. 'Name',
  2587. '/label',
  2588. 'input' => [
  2589. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2590. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2591. ],
  2592. ['div' => ['class' => 'error-message']],
  2593. 'This is wrong',
  2594. '/div',
  2595. '/div',
  2596. ];
  2597. $this->assertHtml($expected, $result);
  2598. }
  2599. /**
  2600. * testControl method
  2601. *
  2602. * Test various incarnations of control().
  2603. *
  2604. * @return void
  2605. */
  2606. public function testControl()
  2607. {
  2608. $this->getTableLocator()->get('ValidateUsers', [
  2609. 'className' => ValidateUsersTable::class,
  2610. ]);
  2611. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2612. $result = $this->Form->control('ValidateUsers.balance');
  2613. $expected = [
  2614. 'div' => ['class'],
  2615. 'label' => ['for'],
  2616. 'Balance',
  2617. '/label',
  2618. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2619. '/div',
  2620. ];
  2621. $this->assertHtml($expected, $result);
  2622. $result = $this->Form->control('ValidateUser.cost_decimal');
  2623. $expected = [
  2624. 'div' => ['class'],
  2625. 'label' => ['for'],
  2626. 'Cost Decimal',
  2627. '/label',
  2628. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2629. '/div',
  2630. ];
  2631. $this->assertHtml($expected, $result);
  2632. $result = $this->Form->control('ValidateUser.null_decimal');
  2633. $expected = [
  2634. 'div' => ['class'],
  2635. 'label' => ['for'],
  2636. 'Null Decimal',
  2637. '/label',
  2638. 'input' => ['name', 'type' => 'number', 'id'],
  2639. '/div',
  2640. ];
  2641. $this->assertHtml($expected, $result);
  2642. }
  2643. /**
  2644. * testControlCustomization method
  2645. *
  2646. * Tests the input method and passing custom options.
  2647. *
  2648. * @return void
  2649. */
  2650. public function testControlCustomization()
  2651. {
  2652. $this->getTableLocator()->get('Contacts', [
  2653. 'className' => ContactsTable::class,
  2654. ]);
  2655. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2656. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2657. $expected = [
  2658. 'div' => ['class' => 'input email'],
  2659. 'label' => ['for' => 'custom'],
  2660. 'Email',
  2661. '/label',
  2662. ['input' => [
  2663. 'type' => 'email', 'name' => 'Contact[email]',
  2664. 'id' => 'custom', 'maxlength' => 255,
  2665. ]],
  2666. '/div',
  2667. ];
  2668. $this->assertHtml($expected, $result);
  2669. $result = $this->Form->control('Contact.email', [
  2670. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2671. ]);
  2672. $expected = [
  2673. '<div',
  2674. 'label' => ['for' => 'contact-email'],
  2675. 'Email',
  2676. '/label',
  2677. ['input' => [
  2678. 'type' => 'email', 'name' => 'Contact[email]',
  2679. 'id' => 'contact-email', 'maxlength' => 255,
  2680. ]],
  2681. '/div',
  2682. ];
  2683. $this->assertHtml($expected, $result);
  2684. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2685. $expected = [
  2686. 'div' => ['class' => 'input text'],
  2687. 'label' => ['for' => 'contact-email'],
  2688. 'Email',
  2689. '/label',
  2690. ['input' => [
  2691. 'type' => 'text', 'name' => 'Contact[email]',
  2692. 'id' => 'contact-email', 'maxlength' => '255',
  2693. ]],
  2694. '/div',
  2695. ];
  2696. $this->assertHtml($expected, $result);
  2697. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2698. $expected = [
  2699. 'div' => ['class' => 'input text'],
  2700. 'label' => ['for' => 'contact-5-email'],
  2701. 'Email',
  2702. '/label',
  2703. ['input' => [
  2704. 'type' => 'text', 'name' => 'Contact[5][email]',
  2705. 'id' => 'contact-5-email', 'maxlength' => '255',
  2706. ]],
  2707. '/div',
  2708. ];
  2709. $this->assertHtml($expected, $result);
  2710. $result = $this->Form->control('Contact.password');
  2711. $expected = [
  2712. 'div' => ['class' => 'input password'],
  2713. 'label' => ['for' => 'contact-password'],
  2714. 'Password',
  2715. '/label',
  2716. ['input' => [
  2717. 'type' => 'password', 'name' => 'Contact[password]',
  2718. 'id' => 'contact-password',
  2719. ]],
  2720. '/div',
  2721. ];
  2722. $this->assertHtml($expected, $result);
  2723. $result = $this->Form->control('Contact.email', [
  2724. 'type' => 'file', 'class' => 'textbox',
  2725. ]);
  2726. $expected = [
  2727. 'div' => ['class' => 'input file'],
  2728. 'label' => ['for' => 'contact-email'],
  2729. 'Email',
  2730. '/label',
  2731. ['input' => [
  2732. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2733. 'id' => 'contact-email',
  2734. ]],
  2735. '/div',
  2736. ];
  2737. $this->assertHtml($expected, $result);
  2738. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2739. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2740. $result = $this->Form->control('phone');
  2741. $expected = [
  2742. 'div' => ['class' => 'input tel'],
  2743. 'label' => ['for' => 'phone'],
  2744. 'Phone',
  2745. '/label',
  2746. ['input' => [
  2747. 'type' => 'tel', 'name' => 'phone',
  2748. 'value' => 'Hello &amp; World &gt; weird chars',
  2749. 'id' => 'phone', 'maxlength' => 255,
  2750. ]],
  2751. '/div',
  2752. ];
  2753. $this->assertHtml($expected, $result);
  2754. $this->View->setRequest(
  2755. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2756. );
  2757. $this->Form->create();
  2758. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2759. $expected = [
  2760. 'div' => ['class' => 'input text'],
  2761. 'label' => ['for' => 'myId'],
  2762. 'Field',
  2763. '/label',
  2764. 'input' => [
  2765. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2766. 'value' => 'My value', 'id' => 'myId',
  2767. ],
  2768. '/div',
  2769. ];
  2770. $this->assertHtml($expected, $result);
  2771. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2772. $this->Form->create();
  2773. $entity->setError('field', 'Badness!');
  2774. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2775. $result = $this->Form->control('field');
  2776. $expected = [
  2777. 'div' => ['class' => 'input text error'],
  2778. 'label' => ['for' => 'field'],
  2779. 'Field',
  2780. '/label',
  2781. 'input' => [
  2782. 'type' => 'text', 'name' => 'field',
  2783. 'id' => 'field', 'class' => 'form-error',
  2784. ],
  2785. ['div' => ['class' => 'error-message']],
  2786. 'Badness!',
  2787. '/div',
  2788. '/div',
  2789. ];
  2790. $this->assertHtml($expected, $result);
  2791. $result = $this->Form->control('field', [
  2792. 'templates' => [
  2793. 'inputContainerError' => '{{content}}{{error}}',
  2794. 'error' => '<span class="error-message">{{content}}</span>',
  2795. ],
  2796. ]);
  2797. $expected = [
  2798. 'label' => ['for' => 'field'],
  2799. 'Field',
  2800. '/label',
  2801. 'input' => [
  2802. 'type' => 'text', 'name' => 'field',
  2803. 'id' => 'field', 'class' => 'form-error',
  2804. ],
  2805. ['span' => ['class' => 'error-message']],
  2806. 'Badness!',
  2807. '/span',
  2808. ];
  2809. $this->assertHtml($expected, $result);
  2810. $entity->setError('field', ['minLength'], true);
  2811. $result = $this->Form->control('field', [
  2812. 'error' => [
  2813. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2814. 'maxLength' => 'login too large',
  2815. ],
  2816. ]);
  2817. $expected = [
  2818. 'div' => ['class' => 'input text error'],
  2819. 'label' => ['for' => 'field'],
  2820. 'Field',
  2821. '/label',
  2822. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2823. ['div' => ['class' => 'error-message']],
  2824. 'Le login doit contenir au moins 2 caractères',
  2825. '/div',
  2826. '/div',
  2827. ];
  2828. $this->assertHtml($expected, $result);
  2829. $entity->setError('field', ['maxLength'], true);
  2830. $result = $this->Form->control('field', [
  2831. 'error' => [
  2832. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2833. 'maxLength' => 'login too large',
  2834. ],
  2835. ]);
  2836. $expected = [
  2837. 'div' => ['class' => 'input text error'],
  2838. 'label' => ['for' => 'field'],
  2839. 'Field',
  2840. '/label',
  2841. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2842. ['div' => ['class' => 'error-message']],
  2843. 'login too large',
  2844. '/div',
  2845. '/div',
  2846. ];
  2847. $this->assertHtml($expected, $result);
  2848. }
  2849. /**
  2850. * testControlWithTemplateFile method
  2851. *
  2852. * Test that control() accepts a template file.
  2853. *
  2854. * @return void
  2855. */
  2856. public function testControlWithTemplateFile()
  2857. {
  2858. $result = $this->Form->control('field', [
  2859. 'templates' => 'htmlhelper_tags',
  2860. ]);
  2861. $expected = [
  2862. 'label' => ['for' => 'field'],
  2863. 'Field',
  2864. '/label',
  2865. 'input' => [
  2866. 'type' => 'text', 'name' => 'field',
  2867. 'id' => 'field',
  2868. ],
  2869. ];
  2870. $this->assertHtml($expected, $result);
  2871. }
  2872. /**
  2873. * testNestedControlsEndWithBrackets method
  2874. *
  2875. * Test that nested inputs end with brackets.
  2876. *
  2877. * @return void
  2878. */
  2879. public function testNestedControlsEndWithBrackets()
  2880. {
  2881. $result = $this->Form->text('nested.text[]');
  2882. $expected = [
  2883. 'input' => [
  2884. 'type' => 'text', 'name' => 'nested[text][]',
  2885. ],
  2886. ];
  2887. $this->assertHtml($expected, $result);
  2888. $result = $this->Form->file('nested.file[]');
  2889. $expected = [
  2890. 'input' => [
  2891. 'type' => 'file', 'name' => 'nested[file][]',
  2892. ],
  2893. ];
  2894. $this->assertHtml($expected, $result);
  2895. }
  2896. /**
  2897. * testCreateIdPrefix method
  2898. *
  2899. * Test id prefix.
  2900. *
  2901. * @return void
  2902. */
  2903. public function testCreateIdPrefix()
  2904. {
  2905. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2906. $result = $this->Form->control('field');
  2907. $expected = [
  2908. 'div' => ['class' => 'input text'],
  2909. 'label' => ['for' => 'prefix-field'],
  2910. 'Field',
  2911. '/label',
  2912. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2913. '/div',
  2914. ];
  2915. $this->assertHtml($expected, $result);
  2916. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2917. $expected = [
  2918. 'div' => ['class' => 'input text'],
  2919. 'label' => ['for' => 'custom-id'],
  2920. 'Field',
  2921. '/label',
  2922. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2923. '/div',
  2924. ];
  2925. $this->assertHtml($expected, $result);
  2926. $result = $this->Form->radio('Model.field', ['option A']);
  2927. $expected = [
  2928. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2929. 'label' => ['for' => 'prefix-model-field-0'],
  2930. ['input' => [
  2931. 'type' => 'radio',
  2932. 'name' => 'Model[field]',
  2933. 'value' => '0',
  2934. 'id' => 'prefix-model-field-0',
  2935. ]],
  2936. 'option A',
  2937. '/label',
  2938. ];
  2939. $this->assertHtml($expected, $result);
  2940. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2941. $expected = [
  2942. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2943. 'label' => ['for' => 'prefix-model-field-0'],
  2944. ['input' => [
  2945. 'type' => 'radio',
  2946. 'name' => 'Model[field]',
  2947. 'value' => '0',
  2948. 'id' => 'prefix-model-field-0',
  2949. ]],
  2950. 'option A',
  2951. '/label',
  2952. ];
  2953. $this->assertHtml($expected, $result);
  2954. $result = $this->Form->select(
  2955. 'Model.multi_field',
  2956. ['first'],
  2957. ['multiple' => 'checkbox']
  2958. );
  2959. $expected = [
  2960. 'input' => [
  2961. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  2962. ],
  2963. ['div' => ['class' => 'checkbox']],
  2964. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2965. ['input' => [
  2966. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2967. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  2968. ]],
  2969. 'first',
  2970. '/label',
  2971. '/div',
  2972. ];
  2973. $this->assertHtml($expected, $result);
  2974. $this->Form->end();
  2975. $result = $this->Form->control('field');
  2976. $expected = [
  2977. 'div' => ['class' => 'input text'],
  2978. 'label' => ['for' => 'field'],
  2979. 'Field',
  2980. '/label',
  2981. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2982. '/div',
  2983. ];
  2984. $this->assertHtml($expected, $result);
  2985. }
  2986. /**
  2987. * testControlZero method
  2988. *
  2989. * Test that inputs with 0 can be created.
  2990. *
  2991. * @return void
  2992. */
  2993. public function testControlZero()
  2994. {
  2995. $this->getTableLocator()->get('Contacts', [
  2996. 'className' => ContactsTable::class,
  2997. ]);
  2998. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2999. $result = $this->Form->control('0');
  3000. $expected = [
  3001. 'div' => ['class' => 'input text'],
  3002. 'label' => ['for' => '0'], '/label',
  3003. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3004. '/div',
  3005. ];
  3006. $this->assertHtml($expected, $result);
  3007. }
  3008. /**
  3009. * testControlCheckbox method
  3010. *
  3011. * Test control() with checkbox creation.
  3012. *
  3013. * @return void
  3014. */
  3015. public function testControlCheckbox()
  3016. {
  3017. $articles = $this->getTableLocator()->get('Articles');
  3018. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3019. $article = $articles->newEmptyEntity();
  3020. $this->Form->create($article);
  3021. $result = $this->Form->control('Articles.active');
  3022. $expected = [
  3023. 'div' => ['class' => 'input checkbox'],
  3024. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3025. 'label' => ['for' => 'articles-active'],
  3026. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3027. 'Active',
  3028. '/label',
  3029. '/div',
  3030. ];
  3031. $this->assertHtml($expected, $result);
  3032. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3033. $expected = [
  3034. 'div' => ['class' => 'input checkbox'],
  3035. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3036. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3037. '/div',
  3038. ];
  3039. $this->assertHtml($expected, $result);
  3040. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3041. $expected = [
  3042. 'div' => ['class' => 'input checkbox'],
  3043. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3044. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3045. '/div',
  3046. ];
  3047. $this->assertHtml($expected, $result);
  3048. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3049. $expected = [
  3050. 'div' => ['class' => 'input checkbox'],
  3051. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3052. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3053. '/div',
  3054. ];
  3055. $this->assertHtml($expected, $result);
  3056. $result = $this->Form->control('Articles.disabled', [
  3057. 'label' => 'Disabled',
  3058. 'type' => 'checkbox',
  3059. 'data-foo' => 'disabled',
  3060. ]);
  3061. $expected = [
  3062. 'div' => ['class' => 'input checkbox'],
  3063. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3064. 'label' => ['for' => 'articles-disabled'],
  3065. ['input' => [
  3066. 'type' => 'checkbox',
  3067. 'name' => 'Articles[disabled]',
  3068. 'value' => '1',
  3069. 'id' => 'articles-disabled',
  3070. 'data-foo' => 'disabled',
  3071. ]],
  3072. 'Disabled',
  3073. '/label',
  3074. '/div',
  3075. ];
  3076. $this->assertHtml($expected, $result);
  3077. $result = $this->Form->control('Articles.confirm', [
  3078. 'label' => 'Confirm <b>me</b>!',
  3079. 'type' => 'checkbox',
  3080. 'escape' => false,
  3081. ]);
  3082. $expected = [
  3083. 'div' => ['class' => 'input checkbox'],
  3084. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3085. 'label' => ['for' => 'articles-confirm'],
  3086. ['input' => [
  3087. 'type' => 'checkbox',
  3088. 'name' => 'Articles[confirm]',
  3089. 'value' => '1',
  3090. 'id' => 'articles-confirm',
  3091. ]],
  3092. 'Confirm <b>me</b>!',
  3093. '/label',
  3094. '/div',
  3095. ];
  3096. $this->assertHtml($expected, $result);
  3097. }
  3098. /**
  3099. * testControlHidden method
  3100. *
  3101. * Test that control() does not create wrapping div and label tag for hidden fields.
  3102. *
  3103. * @return void
  3104. */
  3105. public function testControlHidden()
  3106. {
  3107. $this->getTableLocator()->get('ValidateUsers', [
  3108. 'className' => ValidateUsersTable::class,
  3109. ]);
  3110. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3111. $result = $this->Form->control('ValidateUser.id');
  3112. $expected = [
  3113. 'input' => ['name', 'type' => 'hidden', 'id'],
  3114. ];
  3115. $this->assertHtml($expected, $result);
  3116. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3117. $expected = [
  3118. 'input' => ['name', 'type' => 'hidden', 'id'],
  3119. ];
  3120. $this->assertHtml($expected, $result);
  3121. }
  3122. /**
  3123. * testControlDatetime method
  3124. *
  3125. * Test form->control() with datetime.
  3126. *
  3127. * @return void
  3128. */
  3129. public function testControlDatetime()
  3130. {
  3131. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3132. ->setMethods(['datetime'])
  3133. ->setConstructorArgs([new View()])
  3134. ->getMock();
  3135. $this->Form->expects($this->once())->method('datetime')
  3136. ->with('prueba', [
  3137. 'type' => 'datetime',
  3138. 'timeFormat' => 24,
  3139. 'minYear' => 2008,
  3140. 'maxYear' => 2011,
  3141. 'interval' => 15,
  3142. 'options' => null,
  3143. 'empty' => false,
  3144. 'id' => 'prueba',
  3145. 'required' => false,
  3146. 'templateVars' => [],
  3147. ])
  3148. ->will($this->returnValue('This is it!'));
  3149. $result = $this->Form->control('prueba', [
  3150. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3151. 'maxYear' => 2011, 'interval' => 15,
  3152. ]);
  3153. $expected = [
  3154. 'div' => ['class' => 'input datetime'],
  3155. '<label',
  3156. 'Prueba',
  3157. '/label',
  3158. 'This is it!',
  3159. '/div',
  3160. ];
  3161. $this->assertHtml($expected, $result);
  3162. }
  3163. /**
  3164. * testControlDatetimeIdPrefix method
  3165. *
  3166. * Test form->control() with datetime with id prefix.
  3167. *
  3168. * @return void
  3169. */
  3170. public function testControlDatetimeIdPrefix()
  3171. {
  3172. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3173. ->setMethods(['datetime'])
  3174. ->setConstructorArgs([new View()])
  3175. ->getMock();
  3176. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3177. $this->Form->expects($this->once())->method('datetime')
  3178. ->with('prueba', [
  3179. 'type' => 'datetime',
  3180. 'timeFormat' => 24,
  3181. 'minYear' => 2008,
  3182. 'maxYear' => 2011,
  3183. 'interval' => 15,
  3184. 'options' => null,
  3185. 'empty' => false,
  3186. 'id' => 'prefix-prueba',
  3187. 'required' => false,
  3188. 'templateVars' => [],
  3189. ])
  3190. ->will($this->returnValue('This is it!'));
  3191. $result = $this->Form->control('prueba', [
  3192. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3193. 'maxYear' => 2011, 'interval' => 15,
  3194. ]);
  3195. $expected = [
  3196. 'div' => ['class' => 'input datetime'],
  3197. '<label',
  3198. 'Prueba',
  3199. '/label',
  3200. 'This is it!',
  3201. '/div',
  3202. ];
  3203. $this->assertHtml($expected, $result);
  3204. }
  3205. /**
  3206. * testControlCheckboxWithDisabledElements method
  3207. *
  3208. * Test generating checkboxes with disabled elements.
  3209. *
  3210. * @return void
  3211. */
  3212. public function testControlCheckboxWithDisabledElements()
  3213. {
  3214. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3215. $result = $this->Form->control('Contact.multiple', [
  3216. 'multiple' => 'checkbox',
  3217. 'disabled' => 'disabled',
  3218. 'options' => $options,
  3219. ]);
  3220. $expected = [
  3221. ['div' => ['class' => 'input select']],
  3222. ['label' => ['for' => 'contact-multiple']],
  3223. 'Multiple',
  3224. '/label',
  3225. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3226. ['div' => ['class' => 'checkbox']],
  3227. ['label' => ['for' => 'contact-multiple-1']],
  3228. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3229. 'One',
  3230. '/label',
  3231. '/div',
  3232. ['div' => ['class' => 'checkbox']],
  3233. ['label' => ['for' => 'contact-multiple-2']],
  3234. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3235. 'Two',
  3236. '/label',
  3237. '/div',
  3238. ['div' => ['class' => 'checkbox']],
  3239. ['label' => ['for' => 'contact-multiple-3']],
  3240. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3241. 'Three',
  3242. '/label',
  3243. '/div',
  3244. '/div',
  3245. ];
  3246. $this->assertHtml($expected, $result);
  3247. // make sure 50 does only disable 50, and not 50f5c0cf
  3248. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3249. $disabled = [50];
  3250. $expected = [
  3251. ['div' => ['class' => 'input select']],
  3252. ['label' => ['for' => 'contact-multiple']],
  3253. 'Multiple',
  3254. '/label',
  3255. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3256. ['div' => ['class' => 'checkbox']],
  3257. ['label' => ['for' => 'contact-multiple-50']],
  3258. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3259. 'Fifty',
  3260. '/label',
  3261. '/div',
  3262. ['div' => ['class' => 'checkbox']],
  3263. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3264. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3265. 'Stringy',
  3266. '/label',
  3267. '/div',
  3268. '/div',
  3269. ];
  3270. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3271. $this->assertHtml($expected, $result);
  3272. }
  3273. /**
  3274. * testControlWithLeadingInteger method
  3275. *
  3276. * Test input name with leading integer, ensure attributes are generated correctly.
  3277. *
  3278. * @return void
  3279. */
  3280. public function testControlWithLeadingInteger()
  3281. {
  3282. $result = $this->Form->text('0.Node.title');
  3283. $expected = [
  3284. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3285. ];
  3286. $this->assertHtml($expected, $result);
  3287. }
  3288. /**
  3289. * testControlSelectType method
  3290. *
  3291. * Test form->control() with select type inputs.
  3292. *
  3293. * @return void
  3294. */
  3295. public function testControlSelectType()
  3296. {
  3297. $result = $this->Form->control(
  3298. 'email',
  3299. [
  3300. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3301. );
  3302. $expected = [
  3303. 'div' => ['class' => 'input select'],
  3304. 'label' => ['for' => 'email'],
  3305. 'Email',
  3306. '/label',
  3307. ['select' => ['name' => 'email', 'id' => 'email']],
  3308. ['option' => ['value' => '']],
  3309. '/option',
  3310. ['option' => ['value' => 'è']],
  3311. 'Firést',
  3312. '/option',
  3313. ['option' => ['value' => 'é']],
  3314. 'Secoènd',
  3315. '/option',
  3316. '/select',
  3317. '/div',
  3318. ];
  3319. $this->assertHtml($expected, $result);
  3320. $result = $this->Form->control(
  3321. 'email',
  3322. [
  3323. 'options' => ['First', 'Second'], 'empty' => true]
  3324. );
  3325. $expected = [
  3326. 'div' => ['class' => 'input select'],
  3327. 'label' => ['for' => 'email'],
  3328. 'Email',
  3329. '/label',
  3330. ['select' => ['name' => 'email', 'id' => 'email']],
  3331. ['option' => ['value' => '']],
  3332. '/option',
  3333. ['option' => ['value' => '0']],
  3334. 'First',
  3335. '/option',
  3336. ['option' => ['value' => '1']],
  3337. 'Second',
  3338. '/option',
  3339. '/select',
  3340. '/div',
  3341. ];
  3342. $this->assertHtml($expected, $result);
  3343. $result = $this->Form->control('email', [
  3344. 'type' => 'select',
  3345. 'options' => new \ArrayObject(['First', 'Second']),
  3346. 'empty' => true,
  3347. ]);
  3348. $this->assertHtml($expected, $result);
  3349. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3350. $this->View->setRequest(
  3351. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3352. );
  3353. $this->Form->create();
  3354. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3355. $expected = [
  3356. 'div' => ['class' => 'input select'],
  3357. 'label' => ['for' => 'model-user-id'],
  3358. 'User',
  3359. '/label',
  3360. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3361. ['option' => ['value' => '']],
  3362. '/option',
  3363. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3364. 'good',
  3365. '/option',
  3366. ['option' => ['value' => 'other']],
  3367. 'bad',
  3368. '/option',
  3369. '/select',
  3370. '/div',
  3371. ];
  3372. $this->assertHtml($expected, $result);
  3373. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3374. $this->View->setRequest(
  3375. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3376. );
  3377. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3378. $expected = [
  3379. 'div' => ['class' => 'input select'],
  3380. 'label' => ['for' => 'thing-user-id'],
  3381. 'User',
  3382. '/label',
  3383. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3384. ['option' => ['value' => '']],
  3385. 'Some Empty',
  3386. '/option',
  3387. ['option' => ['value' => 'value']],
  3388. 'good',
  3389. '/option',
  3390. ['option' => ['value' => 'other']],
  3391. 'bad',
  3392. '/option',
  3393. '/select',
  3394. '/div',
  3395. ];
  3396. $this->assertHtml($expected, $result);
  3397. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3398. $this->View->setRequest(
  3399. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3400. );
  3401. $this->Form->create();
  3402. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3403. $expected = [
  3404. 'div' => ['class' => 'input select'],
  3405. 'label' => ['for' => 'thing-user-id'],
  3406. 'User',
  3407. '/label',
  3408. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3409. ['option' => ['value' => '']],
  3410. 'Some Empty',
  3411. '/option',
  3412. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3413. 'good',
  3414. '/option',
  3415. ['option' => ['value' => 'other']],
  3416. 'bad',
  3417. '/option',
  3418. '/select',
  3419. '/div',
  3420. ];
  3421. $this->assertHtml($expected, $result);
  3422. $result = $this->Form->control('Publisher.id', [
  3423. 'label' => 'Publisher',
  3424. 'type' => 'select',
  3425. 'multiple' => 'checkbox',
  3426. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3427. ]);
  3428. $expected = [
  3429. ['div' => ['class' => 'input select']],
  3430. ['label' => ['for' => 'publisher-id']],
  3431. 'Publisher',
  3432. '/label',
  3433. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3434. ['div' => ['class' => 'checkbox']],
  3435. ['label' => ['for' => 'publisher-id-value-1']],
  3436. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3437. 'Label 1',
  3438. '/label',
  3439. '/div',
  3440. ['div' => ['class' => 'checkbox']],
  3441. ['label' => ['for' => 'publisher-id-value-2']],
  3442. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3443. 'Label 2',
  3444. '/label',
  3445. '/div',
  3446. '/div',
  3447. ];
  3448. $this->assertHtml($expected, $result);
  3449. }
  3450. /**
  3451. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3452. *
  3453. * Test that control() and a non standard primary key makes a hidden input by default.
  3454. *
  3455. * @return void
  3456. */
  3457. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3458. {
  3459. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3460. $this->Form->create($this->article);
  3461. $result = $this->Form->control('title');
  3462. $expected = [
  3463. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3464. ];
  3465. $this->assertHtml($expected, $result);
  3466. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3467. $this->Form->create($this->article);
  3468. $result = $this->Form->control('title');
  3469. $expected = [
  3470. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3471. ];
  3472. $this->assertHtml($expected, $result);
  3473. $result = $this->Form->control('body');
  3474. $expected = [
  3475. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3476. ];
  3477. $this->assertHtml($expected, $result);
  3478. }
  3479. /**
  3480. * testControlOverridingMagicSelectType method
  3481. *
  3482. * Test that overriding the magic select type widget is possible.
  3483. *
  3484. * @return void
  3485. */
  3486. public function testControlOverridingMagicSelectType()
  3487. {
  3488. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3489. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3490. $expected = [
  3491. 'div' => ['class' => 'input text'],
  3492. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3493. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3494. '/div',
  3495. ];
  3496. $this->assertHtml($expected, $result);
  3497. //Check that magic types still work for plural/singular vars
  3498. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3499. $result = $this->Form->control('Model.type');
  3500. $expected = [
  3501. 'div' => ['class' => 'input select'],
  3502. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3503. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3504. ['option' => ['value' => 'value']], 'good', '/option',
  3505. ['option' => ['value' => 'other']], 'bad', '/option',
  3506. '/select',
  3507. '/div',
  3508. ];
  3509. $this->assertHtml($expected, $result);
  3510. }
  3511. /**
  3512. * testControlMagicTypeDoesNotOverride method
  3513. *
  3514. * Test that inferred types do not override developer input.
  3515. *
  3516. * @return void
  3517. */
  3518. public function testControlMagicTypeDoesNotOverride()
  3519. {
  3520. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3521. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3522. $expected = [
  3523. 'div' => ['class' => 'input checkbox'],
  3524. ['input' => [
  3525. 'type' => 'hidden',
  3526. 'name' => 'Model[user]',
  3527. 'value' => 0,
  3528. ]],
  3529. 'label' => ['for' => 'model-user'],
  3530. ['input' => [
  3531. 'name' => 'Model[user]',
  3532. 'type' => 'checkbox',
  3533. 'id' => 'model-user',
  3534. 'value' => 1,
  3535. ]],
  3536. 'User',
  3537. '/label',
  3538. '/div',
  3539. ];
  3540. $this->assertHtml($expected, $result);
  3541. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3542. $options = [
  3543. 1 => 'blue',
  3544. 2 => 'red',
  3545. ];
  3546. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3547. $expected = [
  3548. 'div' => ['class' => 'input select'],
  3549. 'label' => ['for' => 'tags-ids'],
  3550. 'Tags',
  3551. '/label',
  3552. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3553. ['div' => ['class' => 'checkbox']],
  3554. ['label' => ['for' => 'tags-ids-1']],
  3555. ['input' => [
  3556. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3557. 'value' => '1', 'name' => 'tags[_ids][]',
  3558. ]],
  3559. 'blue',
  3560. '/label',
  3561. '/div',
  3562. ['div' => ['class' => 'checkbox']],
  3563. ['label' => ['for' => 'tags-ids-2']],
  3564. ['input' => [
  3565. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3566. 'value' => '2', 'name' => 'tags[_ids][]',
  3567. ]],
  3568. 'red',
  3569. '/label',
  3570. '/div',
  3571. '/div',
  3572. ];
  3573. $this->assertHtml($expected, $result);
  3574. }
  3575. /**
  3576. * testControlMagicSelectForTypeNumber method
  3577. *
  3578. * Test that magic control() selects are created for type=number.
  3579. *
  3580. * @return void
  3581. */
  3582. public function testControlMagicSelectForTypeNumber()
  3583. {
  3584. $this->getTableLocator()->get('ValidateUsers', [
  3585. 'className' => ValidateUsersTable::class,
  3586. ]);
  3587. $entity = new Entity(['balance' => 1]);
  3588. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3589. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3590. $result = $this->Form->control('balance');
  3591. $expected = [
  3592. 'div' => ['class' => 'input select'],
  3593. 'label' => ['for' => 'balance'],
  3594. 'Balance',
  3595. '/label',
  3596. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3597. ['option' => ['value' => '0']],
  3598. 'nothing',
  3599. '/option',
  3600. ['option' => ['value' => '1', 'selected' => 'selected']],
  3601. 'some',
  3602. '/option',
  3603. ['option' => ['value' => '100']],
  3604. 'a lot',
  3605. '/option',
  3606. '/select',
  3607. '/div',
  3608. ];
  3609. $this->assertHtml($expected, $result);
  3610. }
  3611. /**
  3612. * testInvalidControlTypeOption method
  3613. *
  3614. * Test invalid 'input' type option to control() function.
  3615. *
  3616. * @return void
  3617. */
  3618. public function testInvalidControlTypeOption()
  3619. {
  3620. $this->expectException(\RuntimeException::class);
  3621. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3622. $this->Form->control('text', ['type' => 'input']);
  3623. }
  3624. /**
  3625. * testControlMagicSelectChangeToRadio method
  3626. *
  3627. * Test that magic control() selects can easily be converted into radio types without error.
  3628. *
  3629. * @return void
  3630. */
  3631. public function testControlMagicSelectChangeToRadio()
  3632. {
  3633. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3634. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3635. $this->assertStringContainsString('input type="radio"', $result);
  3636. }
  3637. /**
  3638. * testFormControlSubmit method
  3639. *
  3640. * Test correct results for form::control() and type submit.
  3641. *
  3642. * @return void
  3643. */
  3644. public function testFormControlSubmit()
  3645. {
  3646. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3647. $expected = [
  3648. 'div' => ['class' => 'submit'],
  3649. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3650. '/div',
  3651. ];
  3652. $this->assertHtml($expected, $result);
  3653. }
  3654. /**
  3655. * testFormControls method
  3656. *
  3657. * Test correct results from Form::controls().
  3658. *
  3659. * @return void
  3660. */
  3661. public function testFormControlsLegendFieldset()
  3662. {
  3663. $this->Form->create($this->article);
  3664. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3665. $expected = [
  3666. '<fieldset',
  3667. '<legend',
  3668. 'The Legend',
  3669. '/legend',
  3670. '*/fieldset',
  3671. ];
  3672. $this->assertHtml($expected, $result);
  3673. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3674. $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
  3675. $this->assertStringContainsString('<fieldset>', $result);
  3676. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3677. $this->assertStringNotContainsString('<legend>', $result);
  3678. $this->assertStringNotContainsString('<fieldset>', $result);
  3679. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3680. $this->assertStringNotContainsString('<legend>', $result);
  3681. $this->assertStringNotContainsString('<fieldset>', $result);
  3682. $this->Form->create($this->article);
  3683. $this->View->setRequest($this->View->getRequest()
  3684. ->withParam('prefix', 'admin')
  3685. ->withParam('action', 'admin_edit')
  3686. ->withParam('controller', 'articles'));
  3687. $result = $this->Form->allControls();
  3688. $expected = [
  3689. '<fieldset',
  3690. '<legend',
  3691. 'New Article',
  3692. '/legend',
  3693. '*/fieldset',
  3694. ];
  3695. $this->assertHtml($expected, $result);
  3696. $this->Form->create($this->article);
  3697. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3698. $expected = [
  3699. '<fieldset',
  3700. '<legend',
  3701. 'The Legend',
  3702. '/legend',
  3703. '*/fieldset',
  3704. ];
  3705. $this->assertHtml($expected, $result);
  3706. $this->Form->create($this->article);
  3707. $result = $this->Form->allControls([], [
  3708. 'fieldset' => [
  3709. 'class' => 'some-class some-other-class',
  3710. 'disabled' => true,
  3711. 'data-param' => 'a-param',
  3712. ],
  3713. 'legend' => 'The Legend',
  3714. ]);
  3715. $expected = [
  3716. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3717. '<legend',
  3718. 'The Legend',
  3719. '/legend',
  3720. '*/fieldset',
  3721. ];
  3722. $this->assertHtml($expected, $result);
  3723. }
  3724. /**
  3725. * testFormControls method
  3726. *
  3727. * Test the controls() method.
  3728. *
  3729. * @return void
  3730. */
  3731. public function testFormControls()
  3732. {
  3733. $this->Form->create($this->article);
  3734. $result = $this->Form->allControls();
  3735. $expected = [
  3736. '<fieldset',
  3737. '<legend', 'New Article', '/legend',
  3738. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3739. ['div' => ['class' => 'input select required']],
  3740. '*/div',
  3741. ['div' => ['class' => 'input text required']],
  3742. '*/div',
  3743. ['div' => ['class' => 'input text']],
  3744. '*/div',
  3745. ['div' => ['class' => 'input text']],
  3746. '*/div',
  3747. '/fieldset',
  3748. ];
  3749. $this->assertHtml($expected, $result);
  3750. $result = $this->Form->allControls([
  3751. 'published' => ['type' => 'boolean'],
  3752. ]);
  3753. $expected = [
  3754. '<fieldset',
  3755. '<legend', 'New Article', '/legend',
  3756. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3757. ['div' => ['class' => 'input select required']],
  3758. '*/div',
  3759. ['div' => ['class' => 'input text required']],
  3760. '*/div',
  3761. ['div' => ['class' => 'input text']],
  3762. '*/div',
  3763. ['div' => ['class' => 'input boolean']],
  3764. '*/div',
  3765. '/fieldset',
  3766. ];
  3767. $this->assertHtml($expected, $result);
  3768. $this->Form->create($this->article);
  3769. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3770. $expected = [
  3771. 'fieldset' => [],
  3772. 'legend' => [],
  3773. 'Hello',
  3774. '/legend',
  3775. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3776. ['div' => ['class' => 'input select required']],
  3777. '*/div',
  3778. ['div' => ['class' => 'input text required']],
  3779. '*/div',
  3780. ['div' => ['class' => 'input text']],
  3781. '*/div',
  3782. ['div' => ['class' => 'input text']],
  3783. '*/div',
  3784. '/fieldset',
  3785. ];
  3786. $this->assertHtml($expected, $result);
  3787. $this->Form->create(false);
  3788. $expected = [
  3789. 'fieldset' => [],
  3790. ['div' => ['class' => 'input text']],
  3791. 'label' => ['for' => 'foo'],
  3792. 'Foo',
  3793. '/label',
  3794. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3795. '*/div',
  3796. '/fieldset',
  3797. ];
  3798. $result = $this->Form->allControls(
  3799. ['foo' => ['type' => 'text']],
  3800. ['legend' => false]
  3801. );
  3802. $this->assertHtml($expected, $result);
  3803. }
  3804. /**
  3805. * testFormControlsBlacklist method
  3806. *
  3807. * @return void
  3808. */
  3809. public function testFormControlsBlacklist()
  3810. {
  3811. $this->Form->create($this->article);
  3812. $result = $this->Form->allControls([
  3813. 'id' => false,
  3814. ]);
  3815. $expected = [
  3816. '<fieldset',
  3817. '<legend', 'New Article', '/legend',
  3818. ['div' => ['class' => 'input select required']],
  3819. '*/div',
  3820. ['div' => ['class' => 'input text required']],
  3821. '*/div',
  3822. ['div' => ['class' => 'input text']],
  3823. '*/div',
  3824. ['div' => ['class' => 'input text']],
  3825. '*/div',
  3826. '/fieldset',
  3827. ];
  3828. $this->assertHtml($expected, $result);
  3829. $this->Form->create($this->article);
  3830. $result = $this->Form->allControls([
  3831. 'id' => [],
  3832. ]);
  3833. $expected = [
  3834. '<fieldset',
  3835. '<legend', 'New Article', '/legend',
  3836. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3837. ['div' => ['class' => 'input select required']],
  3838. '*/div',
  3839. ['div' => ['class' => 'input text required']],
  3840. '*/div',
  3841. ['div' => ['class' => 'input text']],
  3842. '*/div',
  3843. ['div' => ['class' => 'input text']],
  3844. '*/div',
  3845. '/fieldset',
  3846. ];
  3847. $this->assertHtml($expected, $result, true);
  3848. }
  3849. /**
  3850. * testSelectAsCheckbox method
  3851. *
  3852. * Test multi-select widget with checkbox formatting.
  3853. *
  3854. * @return void
  3855. */
  3856. public function testSelectAsCheckbox()
  3857. {
  3858. $result = $this->Form->select(
  3859. 'Model.multi_field',
  3860. ['first', 'second', 'third'],
  3861. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3862. );
  3863. $expected = [
  3864. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3865. ['div' => ['class' => 'checkbox']],
  3866. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3867. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3868. 'first',
  3869. '/label',
  3870. '/div',
  3871. ['div' => ['class' => 'checkbox']],
  3872. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3873. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3874. 'second',
  3875. '/label',
  3876. '/div',
  3877. ['div' => ['class' => 'checkbox']],
  3878. ['label' => ['for' => 'model-multi-field-2']],
  3879. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3880. 'third',
  3881. '/label',
  3882. '/div',
  3883. ];
  3884. $this->assertHtml($expected, $result);
  3885. $result = $this->Form->select(
  3886. 'Model.multi_field',
  3887. ['1/2' => 'half'],
  3888. ['multiple' => 'checkbox']
  3889. );
  3890. $expected = [
  3891. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3892. ['div' => ['class' => 'checkbox']],
  3893. ['label' => ['for' => 'model-multi-field-1-2']],
  3894. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3895. 'half',
  3896. '/label',
  3897. '/div',
  3898. ];
  3899. $this->assertHtml($expected, $result);
  3900. }
  3901. /**
  3902. * testLabel method
  3903. *
  3904. * Test label generation.
  3905. *
  3906. * @return void
  3907. */
  3908. public function testLabel()
  3909. {
  3910. $result = $this->Form->label('Person.name');
  3911. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3912. $this->assertHtml($expected, $result);
  3913. $result = $this->Form->label('Person.name');
  3914. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3915. $this->assertHtml($expected, $result);
  3916. $result = $this->Form->label('Person.first_name');
  3917. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3918. $this->assertHtml($expected, $result);
  3919. $result = $this->Form->label('Person.first_name', 'Your first name');
  3920. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3921. $this->assertHtml($expected, $result);
  3922. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3923. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3924. $this->assertHtml($expected, $result);
  3925. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3926. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3927. $this->assertHtml($expected, $result);
  3928. $result = $this->Form->label('Person.first_name', '');
  3929. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3930. $this->assertHtml($expected, $result);
  3931. $result = $this->Form->label('Person.2.name', '');
  3932. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3933. $this->assertHtml($expected, $result);
  3934. }
  3935. /**
  3936. * testLabelContainControl method
  3937. *
  3938. * Test that label() can accept an input with the correct template vars.
  3939. *
  3940. * @return void
  3941. */
  3942. public function testLabelContainControl()
  3943. {
  3944. $this->Form->setTemplates([
  3945. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3946. ]);
  3947. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3948. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3949. ]);
  3950. $expected = [
  3951. 'label' => ['for' => 'person-accept-terms'],
  3952. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3953. 'Accept',
  3954. '/label',
  3955. ];
  3956. $this->assertHtml($expected, $result);
  3957. }
  3958. /**
  3959. * testTextbox method
  3960. *
  3961. * Test textbox element generation.
  3962. *
  3963. * @return void
  3964. */
  3965. public function testTextbox()
  3966. {
  3967. $result = $this->Form->text('Model.field');
  3968. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3969. $this->assertHtml($expected, $result);
  3970. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3971. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3972. $this->assertHtml($expected, $result);
  3973. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3974. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3975. $this->assertHtml($expected, $result);
  3976. }
  3977. /**
  3978. * testTextBoxDataAndError method
  3979. *
  3980. * Test that text() hooks up with request data and error fields.
  3981. *
  3982. * @return void
  3983. */
  3984. public function testTextBoxDataAndError()
  3985. {
  3986. $this->article['errors'] = [
  3987. 'Contact' => ['text' => 'wrong'],
  3988. ];
  3989. $this->View->setRequest($this->View->getRequest()
  3990. ->withData('Model.text', 'test <strong>HTML</strong> values')
  3991. ->withData('Contact.text', 'test'));
  3992. $this->Form->create($this->article);
  3993. $result = $this->Form->text('Model.text');
  3994. $expected = [
  3995. 'input' => [
  3996. 'type' => 'text',
  3997. 'name' => 'Model[text]',
  3998. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3999. ],
  4000. ];
  4001. $this->assertHtml($expected, $result);
  4002. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4003. $expected = [
  4004. 'input' => [
  4005. 'type' => 'text',
  4006. 'name' => 'Contact[text]',
  4007. 'value' => 'test',
  4008. 'id' => 'theID',
  4009. 'class' => 'form-error',
  4010. ],
  4011. ];
  4012. $this->assertHtml($expected, $result);
  4013. }
  4014. /**
  4015. * testDefaultValue method
  4016. *
  4017. * Test default value setting.
  4018. *
  4019. * @return void
  4020. */
  4021. public function testTextDefaultValue()
  4022. {
  4023. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  4024. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4025. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4026. $this->assertHtml($expected, $result);
  4027. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4028. $this->Form->create();
  4029. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4030. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4031. $this->assertHtml($expected, $result);
  4032. $Articles = $this->getTableLocator()->get('Articles');
  4033. $title = $Articles->getSchema()->getColumn('title');
  4034. $Articles->getSchema()->addColumn(
  4035. 'title',
  4036. ['default' => 'default title'] + $title
  4037. );
  4038. $entity = $Articles->newEmptyEntity();
  4039. $this->Form->create($entity);
  4040. // Get default value from schema
  4041. $result = $this->Form->text('title');
  4042. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4043. $this->assertHtml($expected, $result);
  4044. // Don't get value from schema
  4045. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4046. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4047. $this->assertHtml($expected, $result);
  4048. // Custom default value overrides default value from schema
  4049. $result = $this->Form->text('title', ['default' => 'override default']);
  4050. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4051. $this->assertHtml($expected, $result);
  4052. // Default value from schema is used only for new entities.
  4053. $entity->isNew(false);
  4054. $result = $this->Form->text('title');
  4055. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4056. $this->assertHtml($expected, $result);
  4057. }
  4058. /**
  4059. * testError method
  4060. *
  4061. * Test field error generation.
  4062. *
  4063. * @return void
  4064. */
  4065. public function testError()
  4066. {
  4067. $this->article['errors'] = [
  4068. 'Article' => ['field' => 'email'],
  4069. ];
  4070. $this->Form->create($this->article);
  4071. $result = $this->Form->error('Article.field');
  4072. $expected = [
  4073. ['div' => ['class' => 'error-message']],
  4074. 'email',
  4075. '/div',
  4076. ];
  4077. $this->assertHtml($expected, $result);
  4078. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4079. $expected = [
  4080. ['div' => ['class' => 'error-message']],
  4081. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4082. '/div',
  4083. ];
  4084. $this->assertHtml($expected, $result);
  4085. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4086. $expected = [
  4087. ['div' => ['class' => 'error-message']],
  4088. '<strong', 'Badness!', '/strong',
  4089. '/div',
  4090. ];
  4091. $this->assertHtml($expected, $result);
  4092. }
  4093. /**
  4094. * testErrorRuleName method
  4095. *
  4096. * Test error translation can use rule names for translating.
  4097. *
  4098. * @return void
  4099. */
  4100. public function testErrorRuleName()
  4101. {
  4102. $this->article['errors'] = [
  4103. 'Article' => [
  4104. 'field' => ['email' => 'Your email was not good'],
  4105. ],
  4106. ];
  4107. $this->Form->create($this->article);
  4108. $result = $this->Form->error('Article.field');
  4109. $expected = [
  4110. ['div' => ['class' => 'error-message']],
  4111. 'Your email was not good',
  4112. '/div',
  4113. ];
  4114. $this->assertHtml($expected, $result);
  4115. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4116. $expected = [
  4117. ['div' => ['class' => 'error-message']],
  4118. 'Email in use',
  4119. '/div',
  4120. ];
  4121. $this->assertHtml($expected, $result);
  4122. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4123. $expected = [
  4124. ['div' => ['class' => 'error-message']],
  4125. 'Email in use',
  4126. '/div',
  4127. ];
  4128. $this->assertHtml($expected, $result);
  4129. $result = $this->Form->error('Article.field', [
  4130. 'email' => 'Key is preferred',
  4131. 'Your email was not good' => 'Email in use',
  4132. ]);
  4133. $expected = [
  4134. ['div' => ['class' => 'error-message']],
  4135. 'Key is preferred',
  4136. '/div',
  4137. ];
  4138. $this->assertHtml($expected, $result);
  4139. }
  4140. /**
  4141. * testErrorMessages method
  4142. *
  4143. * Test error with nested lists.
  4144. *
  4145. * @return void
  4146. */
  4147. public function testErrorMessages()
  4148. {
  4149. $this->article['errors'] = [
  4150. 'Article' => ['field' => 'email'],
  4151. ];
  4152. $this->Form->create($this->article);
  4153. $result = $this->Form->error('Article.field', [
  4154. 'email' => 'No good!',
  4155. ]);
  4156. $expected = [
  4157. 'div' => ['class' => 'error-message'],
  4158. 'No good!',
  4159. '/div',
  4160. ];
  4161. $this->assertHtml($expected, $result);
  4162. }
  4163. /**
  4164. * testErrorMultipleMessages method
  4165. *
  4166. * Test error() with multiple messages.
  4167. *
  4168. * @return void
  4169. */
  4170. public function testErrorMultipleMessages()
  4171. {
  4172. $this->article['errors'] = [
  4173. 'field' => ['notBlank', 'email', 'Something else'],
  4174. ];
  4175. $this->Form->create($this->article);
  4176. $result = $this->Form->error('field', [
  4177. 'notBlank' => 'Cannot be empty',
  4178. 'email' => 'No good!',
  4179. ]);
  4180. $expected = [
  4181. 'div' => ['class' => 'error-message'],
  4182. 'ul' => [],
  4183. '<li', 'Cannot be empty', '/li',
  4184. '<li', 'No good!', '/li',
  4185. '<li', 'Something else', '/li',
  4186. '/ul',
  4187. '/div',
  4188. ];
  4189. $this->assertHtml($expected, $result);
  4190. }
  4191. /**
  4192. * testPassword method
  4193. *
  4194. * Test password element generation.
  4195. *
  4196. * @return void
  4197. */
  4198. public function testPassword()
  4199. {
  4200. $this->article['errors'] = [
  4201. 'Contact' => [
  4202. 'passwd' => 1,
  4203. ],
  4204. ];
  4205. $this->Form->create($this->article);
  4206. $result = $this->Form->password('Contact.field');
  4207. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4208. $this->assertHtml($expected, $result);
  4209. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4210. $this->Form->create($this->article);
  4211. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4212. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4213. $this->assertHtml($expected, $result);
  4214. }
  4215. /**
  4216. * testRadio method
  4217. *
  4218. * Test radio element set generation.
  4219. *
  4220. * @return void
  4221. */
  4222. public function testRadio()
  4223. {
  4224. $result = $this->Form->radio('Model.field', ['option A']);
  4225. $expected = [
  4226. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4227. 'label' => ['for' => 'model-field-0'],
  4228. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4229. 'option A',
  4230. '/label',
  4231. ];
  4232. $this->assertHtml($expected, $result);
  4233. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4234. $this->assertHtml($expected, $result);
  4235. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4236. $expected = [
  4237. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4238. ['label' => ['for' => 'model-field-0']],
  4239. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4240. 'option A',
  4241. '/label',
  4242. ['label' => ['for' => 'model-field-1']],
  4243. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4244. 'option B',
  4245. '/label',
  4246. ];
  4247. $this->assertHtml($expected, $result);
  4248. $result = $this->Form->radio(
  4249. 'Employee.gender',
  4250. ['male' => 'Male', 'female' => 'Female'],
  4251. ['form' => 'my-form']
  4252. );
  4253. $expected = [
  4254. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4255. ['label' => ['for' => 'employee-gender-male']],
  4256. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4257. 'Male',
  4258. '/label',
  4259. ['label' => ['for' => 'employee-gender-female']],
  4260. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4261. 'Female',
  4262. '/label',
  4263. ];
  4264. $this->assertHtml($expected, $result);
  4265. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4266. $expected = [
  4267. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4268. ['label' => ['for' => 'model-custom-0']],
  4269. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4270. 'option A',
  4271. '/label',
  4272. ['label' => ['for' => 'model-custom-1']],
  4273. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4274. 'option B',
  4275. '/label',
  4276. ];
  4277. $this->assertHtml($expected, $result);
  4278. $result = $this->Form->radio(
  4279. 'Employee.gender',
  4280. [
  4281. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4282. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4283. ]
  4284. );
  4285. $expected = [
  4286. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4287. ['label' => ['for' => 'employee-gender-male']],
  4288. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4289. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4290. 'Male',
  4291. '/label',
  4292. ['label' => ['for' => 'employee-gender-female']],
  4293. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4294. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4295. 'Female',
  4296. '/label',
  4297. ];
  4298. $this->assertHtml($expected, $result);
  4299. }
  4300. /**
  4301. * Test radio with complex options and empty disabled data.
  4302. *
  4303. * @return void
  4304. */
  4305. public function testRadioComplexDisabled()
  4306. {
  4307. $options = [
  4308. ['value' => 'r', 'text' => 'red'],
  4309. ['value' => 'b', 'text' => 'blue'],
  4310. ];
  4311. $attrs = ['disabled' => []];
  4312. $result = $this->Form->radio('Model.field', $options, $attrs);
  4313. $expected = [
  4314. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4315. ['label' => ['for' => 'model-field-r']],
  4316. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4317. 'red',
  4318. '/label',
  4319. ['label' => ['for' => 'model-field-b']],
  4320. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4321. 'blue',
  4322. '/label',
  4323. ];
  4324. $this->assertHtml($expected, $result);
  4325. $attrs = ['disabled' => ['r']];
  4326. $result = $this->Form->radio('Model.field', $options, $attrs);
  4327. $this->assertStringContainsString('disabled="disabled"', $result);
  4328. }
  4329. /**
  4330. * testRadioDefaultValue method
  4331. *
  4332. * Test default value setting on radio() method.
  4333. *
  4334. * @return void
  4335. */
  4336. public function testRadioDefaultValue()
  4337. {
  4338. $Articles = $this->getTableLocator()->get('Articles');
  4339. $title = $Articles->getSchema()->getColumn('title');
  4340. $Articles->getSchema()->addColumn(
  4341. 'title',
  4342. ['default' => '1'] + $title
  4343. );
  4344. $this->Form->create($Articles->newEmptyEntity());
  4345. $result = $this->Form->radio('title', ['option A', 'option B']);
  4346. $expected = [
  4347. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4348. ['label' => ['for' => 'title-0']],
  4349. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4350. 'option A',
  4351. '/label',
  4352. ['label' => ['for' => 'title-1']],
  4353. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4354. 'option B',
  4355. '/label',
  4356. ];
  4357. $this->assertHtml($expected, $result);
  4358. }
  4359. /**
  4360. * Test setting a hiddenField value on radio buttons.
  4361. *
  4362. * @return void
  4363. */
  4364. public function testRadioHiddenFieldValue()
  4365. {
  4366. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4367. $expected = [
  4368. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4369. 'label' => ['for' => 'title-0'],
  4370. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4371. 'option A',
  4372. '/label',
  4373. ];
  4374. $this->assertHtml($expected, $result);
  4375. }
  4376. /**
  4377. * testControlRadio method
  4378. *
  4379. * Test that input works with radio types.
  4380. *
  4381. * @return void
  4382. */
  4383. public function testControlRadio()
  4384. {
  4385. $result = $this->Form->control('test', [
  4386. 'type' => 'radio',
  4387. 'options' => ['A', 'B'],
  4388. ]);
  4389. $expected = [
  4390. ['div' => ['class' => 'input radio']],
  4391. '<label',
  4392. 'Test',
  4393. '/label',
  4394. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4395. ['label' => ['for' => 'test-0']],
  4396. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4397. 'A',
  4398. '/label',
  4399. ['label' => ['for' => 'test-1']],
  4400. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4401. 'B',
  4402. '/label',
  4403. '/div',
  4404. ];
  4405. $this->assertHtml($expected, $result);
  4406. $result = $this->Form->control('test', [
  4407. 'type' => 'radio',
  4408. 'options' => ['A', 'B'],
  4409. 'value' => '0',
  4410. ]);
  4411. $expected = [
  4412. ['div' => ['class' => 'input radio']],
  4413. '<label',
  4414. 'Test',
  4415. '/label',
  4416. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4417. ['label' => ['for' => 'test-0']],
  4418. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4419. 'A',
  4420. '/label',
  4421. ['label' => ['for' => 'test-1']],
  4422. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4423. 'B',
  4424. '/label',
  4425. '/div',
  4426. ];
  4427. $this->assertHtml($expected, $result);
  4428. $result = $this->Form->control('test', [
  4429. 'type' => 'radio',
  4430. 'options' => ['A', 'B'],
  4431. 'label' => false,
  4432. ]);
  4433. $expected = [
  4434. ['div' => ['class' => 'input radio']],
  4435. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4436. ['label' => ['for' => 'test-0']],
  4437. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4438. 'A',
  4439. '/label',
  4440. ['label' => ['for' => 'test-1']],
  4441. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4442. 'B',
  4443. '/label',
  4444. '/div',
  4445. ];
  4446. $this->assertHtml($expected, $result);
  4447. }
  4448. /**
  4449. * testRadioNoLabel method
  4450. *
  4451. * Test that radio() works with label = false.
  4452. *
  4453. * @return void
  4454. */
  4455. public function testRadioNoLabel()
  4456. {
  4457. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4458. $expected = [
  4459. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4460. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4461. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4462. ];
  4463. $this->assertHtml($expected, $result);
  4464. }
  4465. /**
  4466. * testRadioControlInsideLabel method
  4467. *
  4468. * Test generating radio input inside label ala twitter bootstrap.
  4469. *
  4470. * @return void
  4471. */
  4472. public function testRadioControlInsideLabel()
  4473. {
  4474. $this->Form->setTemplates([
  4475. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4476. 'radioWrapper' => '{{label}}',
  4477. ]);
  4478. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4479. // phpcs:disable
  4480. $expected = [
  4481. ['input' => [
  4482. 'type' => 'hidden',
  4483. 'name' => 'Model[field]',
  4484. 'value' => ''
  4485. ]],
  4486. ['label' => ['for' => 'model-field-0']],
  4487. ['input' => [
  4488. 'type' => 'radio',
  4489. 'name' => 'Model[field]',
  4490. 'value' => '0',
  4491. 'id' => 'model-field-0'
  4492. ]],
  4493. 'option A',
  4494. '/label',
  4495. ['label' => ['for' => 'model-field-1']],
  4496. ['input' => [
  4497. 'type' => 'radio',
  4498. 'name' => 'Model[field]',
  4499. 'value' => '1',
  4500. 'id' => 'model-field-1'
  4501. ]],
  4502. 'option B',
  4503. '/label',
  4504. ];
  4505. // phpcs:enable
  4506. $this->assertHtml($expected, $result);
  4507. }
  4508. /**
  4509. * testRadioHiddenControlDisabling method
  4510. *
  4511. * Test disabling the hidden input for radio buttons.
  4512. *
  4513. * @return void
  4514. */
  4515. public function testRadioHiddenControlDisabling()
  4516. {
  4517. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4518. $expected = [
  4519. 'label' => ['for' => 'model-1-field-0'],
  4520. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4521. 'option A',
  4522. '/label',
  4523. ];
  4524. $this->assertHtml($expected, $result);
  4525. }
  4526. /**
  4527. * testRadioOutOfRange method
  4528. *
  4529. * Test radio element set generation.
  4530. *
  4531. * @return void
  4532. */
  4533. public function testRadioOutOfRange()
  4534. {
  4535. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4536. $expected = [
  4537. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4538. 'label' => ['for' => 'model-field-v'],
  4539. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4540. 'value',
  4541. '/label',
  4542. ];
  4543. $this->assertHtml($expected, $result);
  4544. }
  4545. /**
  4546. * testSelect method
  4547. *
  4548. * Test select element generation.
  4549. *
  4550. * @return void
  4551. */
  4552. public function testSelect()
  4553. {
  4554. $result = $this->Form->select('Model.field', []);
  4555. $expected = [
  4556. 'select' => ['name' => 'Model[field]'],
  4557. '/select',
  4558. ];
  4559. $this->assertHtml($expected, $result);
  4560. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4561. $this->Form->create();
  4562. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4563. $expected = [
  4564. 'select' => ['name' => 'Model[field]'],
  4565. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4566. 'good',
  4567. '/option',
  4568. ['option' => ['value' => 'other']],
  4569. 'bad',
  4570. '/option',
  4571. '/select',
  4572. ];
  4573. $this->assertHtml($expected, $result);
  4574. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4575. $this->assertHtml($expected, $result);
  4576. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4577. $this->Form->create();
  4578. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4579. $expected = [
  4580. 'select' => ['name' => 'Model[field]'],
  4581. ['option' => ['value' => 'value']],
  4582. 'good',
  4583. '/option',
  4584. ['option' => ['value' => 'other']],
  4585. 'bad',
  4586. '/option',
  4587. '/select',
  4588. ];
  4589. $this->assertHtml($expected, $result);
  4590. $options = [
  4591. ['value' => 'first', 'text' => 'First'],
  4592. ['value' => 'first', 'text' => 'Another First'],
  4593. ];
  4594. $result = $this->Form->select(
  4595. 'Model.field',
  4596. $options,
  4597. ['escape' => false, 'empty' => false]
  4598. );
  4599. $expected = [
  4600. 'select' => ['name' => 'Model[field]'],
  4601. ['option' => ['value' => 'first']],
  4602. 'First',
  4603. '/option',
  4604. ['option' => ['value' => 'first']],
  4605. 'Another First',
  4606. '/option',
  4607. '/select',
  4608. ];
  4609. $this->assertHtml($expected, $result);
  4610. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4611. $this->Form->create();
  4612. $result = $this->Form->select(
  4613. 'Model.contact_id',
  4614. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4615. ['escape' => false, 'empty' => 'pick something']
  4616. );
  4617. $expected = [
  4618. 'select' => ['name' => 'Model[contact_id]'],
  4619. ['option' => ['value' => '']], 'pick something', '/option',
  4620. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4621. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4622. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4623. '/select',
  4624. ];
  4625. $this->assertHtml($expected, $result);
  4626. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4627. $this->Form->create();
  4628. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4629. $expected = [
  4630. 'select' => ['name' => 'Model[field]'],
  4631. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4632. ['option' => ['value' => '1']], 'Yes', '/option',
  4633. '/select',
  4634. ];
  4635. $this->assertHtml($expected, $result);
  4636. }
  4637. /**
  4638. * testSelectEscapeHtml method
  4639. *
  4640. * Test that select() escapes HTML.
  4641. *
  4642. * @return void
  4643. */
  4644. public function testSelectEscapeHtml()
  4645. {
  4646. $result = $this->Form->select(
  4647. 'Model.field',
  4648. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4649. ['empty' => false]
  4650. );
  4651. $expected = [
  4652. 'select' => ['name' => 'Model[field]'],
  4653. ['option' => ['value' => 'first']],
  4654. 'first &quot;html&quot; &lt;chars&gt;',
  4655. '/option',
  4656. ['option' => ['value' => 'second']],
  4657. 'value',
  4658. '/option',
  4659. '/select',
  4660. ];
  4661. $this->assertHtml($expected, $result);
  4662. $result = $this->Form->select(
  4663. 'Model.field',
  4664. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4665. ['escape' => false, 'empty' => false]
  4666. );
  4667. $expected = [
  4668. 'select' => ['name' => 'Model[field]'],
  4669. ['option' => ['value' => 'first']],
  4670. 'first "html" <chars>',
  4671. '/option',
  4672. ['option' => ['value' => 'second']],
  4673. 'value',
  4674. '/option',
  4675. '/select',
  4676. ];
  4677. $this->assertHtml($expected, $result);
  4678. }
  4679. /**
  4680. * testSelectRequired method
  4681. *
  4682. * Test select() with required and disabled attributes.
  4683. *
  4684. * @return void
  4685. */
  4686. public function testSelectRequired()
  4687. {
  4688. $this->article['required'] = [
  4689. 'user_id' => true,
  4690. ];
  4691. $this->Form->create($this->article);
  4692. $result = $this->Form->select('user_id', ['option A']);
  4693. $expected = [
  4694. 'select' => [
  4695. 'name' => 'user_id',
  4696. 'required' => 'required',
  4697. ],
  4698. ['option' => ['value' => '0']], 'option A', '/option',
  4699. '/select',
  4700. ];
  4701. $this->assertHtml($expected, $result);
  4702. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4703. $expected = [
  4704. 'select' => [
  4705. 'name' => 'user_id',
  4706. 'disabled' => 'disabled',
  4707. ],
  4708. ['option' => ['value' => '0']], 'option A', '/option',
  4709. '/select',
  4710. ];
  4711. $this->assertHtml($expected, $result);
  4712. }
  4713. /**
  4714. * testNestedSelect method
  4715. *
  4716. * Test select element generation with optgroups.
  4717. *
  4718. * @return void
  4719. */
  4720. public function testNestedSelect()
  4721. {
  4722. $result = $this->Form->select(
  4723. 'Model.field',
  4724. [1 => 'One', 2 => 'Two', 'Three' => [
  4725. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4726. ]],
  4727. ['empty' => false]
  4728. );
  4729. $expected = [
  4730. 'select' => ['name' => 'Model[field]'],
  4731. ['option' => ['value' => 1]],
  4732. 'One',
  4733. '/option',
  4734. ['option' => ['value' => 2]],
  4735. 'Two',
  4736. '/option',
  4737. ['optgroup' => ['label' => 'Three']],
  4738. ['option' => ['value' => 3]],
  4739. 'Three',
  4740. '/option',
  4741. ['option' => ['value' => 4]],
  4742. 'Four',
  4743. '/option',
  4744. ['option' => ['value' => 5]],
  4745. 'Five',
  4746. '/option',
  4747. '/optgroup',
  4748. '/select',
  4749. ];
  4750. $this->assertHtml($expected, $result);
  4751. }
  4752. /**
  4753. * testSelectMultiple method
  4754. *
  4755. * Test generation of multiple select elements.
  4756. *
  4757. * @return void
  4758. */
  4759. public function testSelectMultiple()
  4760. {
  4761. $options = ['first', 'second', 'third'];
  4762. $result = $this->Form->select(
  4763. 'Model.multi_field',
  4764. $options,
  4765. ['form' => 'my-form', 'multiple' => true]
  4766. );
  4767. $expected = [
  4768. 'input' => [
  4769. 'type' => 'hidden',
  4770. 'name' => 'Model[multi_field]',
  4771. 'value' => '',
  4772. 'form' => 'my-form',
  4773. ],
  4774. 'select' => [
  4775. 'name' => 'Model[multi_field][]',
  4776. 'multiple' => 'multiple',
  4777. 'form' => 'my-form',
  4778. ],
  4779. ['option' => ['value' => '0']],
  4780. 'first',
  4781. '/option',
  4782. ['option' => ['value' => '1']],
  4783. 'second',
  4784. '/option',
  4785. ['option' => ['value' => '2']],
  4786. 'third',
  4787. '/option',
  4788. '/select',
  4789. ];
  4790. $this->assertHtml($expected, $result);
  4791. $result = $this->Form->select(
  4792. 'Model.multi_field',
  4793. $options,
  4794. ['multiple' => 'multiple', 'form' => 'my-form']
  4795. );
  4796. $this->assertHtml($expected, $result);
  4797. $result = $this->Form->select(
  4798. 'Model.multi_field',
  4799. $options,
  4800. ['form' => 'my-form', 'multiple' => false]
  4801. );
  4802. $this->assertStringNotContainsString('multiple', $result);
  4803. }
  4804. /**
  4805. * testCheckboxZeroValue method
  4806. *
  4807. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4808. *
  4809. * @return void
  4810. */
  4811. public function testCheckboxZeroValue()
  4812. {
  4813. $result = $this->Form->control('User.get_spam', [
  4814. 'type' => 'checkbox',
  4815. 'value' => '0',
  4816. 'hiddenField' => '1',
  4817. ]);
  4818. $expected = [
  4819. 'div' => ['class' => 'input checkbox'],
  4820. 'label' => ['for' => 'user-get-spam'],
  4821. ['input' => [
  4822. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4823. 'value' => '1',
  4824. ]],
  4825. ['input' => [
  4826. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4827. 'value' => '0', 'id' => 'user-get-spam',
  4828. ]],
  4829. 'Get Spam',
  4830. '/label',
  4831. '/div',
  4832. ];
  4833. $this->assertHtml($expected, $result);
  4834. }
  4835. /**
  4836. * testHabtmSelectBox method
  4837. *
  4838. * Test generation of habtm select boxes.
  4839. *
  4840. * @return void
  4841. */
  4842. public function testHabtmSelectBox()
  4843. {
  4844. $this->loadFixtures('Articles');
  4845. $options = [
  4846. 1 => 'blue',
  4847. 2 => 'red',
  4848. 3 => 'green',
  4849. ];
  4850. $tags = [
  4851. new Entity(['id' => 1, 'name' => 'blue']),
  4852. new Entity(['id' => 3, 'name' => 'green']),
  4853. ];
  4854. $article = new Article(['tags' => $tags]);
  4855. $this->Form->create($article);
  4856. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4857. $expected = [
  4858. 'div' => ['class' => 'input select'],
  4859. 'label' => ['for' => 'tags-ids'],
  4860. 'Tags',
  4861. '/label',
  4862. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4863. 'select' => [
  4864. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4865. 'multiple' => 'multiple',
  4866. ],
  4867. ['option' => ['value' => '1', 'selected' => 'selected']],
  4868. 'blue',
  4869. '/option',
  4870. ['option' => ['value' => '2']],
  4871. 'red',
  4872. '/option',
  4873. ['option' => ['value' => '3', 'selected' => 'selected']],
  4874. 'green',
  4875. '/option',
  4876. '/select',
  4877. '/div',
  4878. ];
  4879. $this->assertHtml($expected, $result);
  4880. // make sure only 50 is selected, and not 50f5c0cf
  4881. $options = [
  4882. '1' => 'blue',
  4883. '50f5c0cf' => 'red',
  4884. '50' => 'green',
  4885. ];
  4886. $tags = [
  4887. new Entity(['id' => 1, 'name' => 'blue']),
  4888. new Entity(['id' => 50, 'name' => 'green']),
  4889. ];
  4890. $article = new Article(['tags' => $tags]);
  4891. $this->Form->create($article);
  4892. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4893. $expected = [
  4894. 'div' => ['class' => 'input select'],
  4895. 'label' => ['for' => 'tags-ids'],
  4896. 'Tags',
  4897. '/label',
  4898. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4899. 'select' => [
  4900. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4901. 'multiple' => 'multiple',
  4902. ],
  4903. ['option' => ['value' => '1', 'selected' => 'selected']],
  4904. 'blue',
  4905. '/option',
  4906. ['option' => ['value' => '50f5c0cf']],
  4907. 'red',
  4908. '/option',
  4909. ['option' => ['value' => '50', 'selected' => 'selected']],
  4910. 'green',
  4911. '/option',
  4912. '/select',
  4913. '/div',
  4914. ];
  4915. $this->assertHtml($expected, $result);
  4916. $spacecraft = [
  4917. 1 => 'Orion',
  4918. 2 => 'Helios',
  4919. ];
  4920. $this->View->set('spacecraft', $spacecraft);
  4921. $this->Form->create();
  4922. $result = $this->Form->control('spacecraft._ids');
  4923. $expected = [
  4924. 'div' => ['class' => 'input select'],
  4925. 'label' => ['for' => 'spacecraft-ids'],
  4926. 'Spacecraft',
  4927. '/label',
  4928. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4929. 'select' => [
  4930. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4931. 'multiple' => 'multiple',
  4932. ],
  4933. ['option' => ['value' => '1']],
  4934. 'Orion',
  4935. '/option',
  4936. ['option' => ['value' => '2']],
  4937. 'Helios',
  4938. '/option',
  4939. '/select',
  4940. '/div',
  4941. ];
  4942. $this->assertHtml($expected, $result);
  4943. }
  4944. /**
  4945. * testErrorsForBelongsToManySelect method
  4946. *
  4947. * Tests that errors for belongsToMany select fields are being
  4948. * picked up properly.
  4949. *
  4950. * @return void
  4951. */
  4952. public function testErrorsForBelongsToManySelect()
  4953. {
  4954. $spacecraft = [
  4955. 1 => 'Orion',
  4956. 2 => 'Helios',
  4957. ];
  4958. $this->View->set('spacecraft', $spacecraft);
  4959. $article = new Article();
  4960. $article->setError('spacecraft', ['Invalid']);
  4961. $this->Form->create($article);
  4962. $result = $this->Form->control('spacecraft._ids');
  4963. $expected = [
  4964. ['div' => ['class' => 'input select error']],
  4965. 'label' => ['for' => 'spacecraft-ids'],
  4966. 'Spacecraft',
  4967. '/label',
  4968. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4969. 'select' => [
  4970. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4971. 'multiple' => 'multiple',
  4972. ],
  4973. ['option' => ['value' => '1']],
  4974. 'Orion',
  4975. '/option',
  4976. ['option' => ['value' => '2']],
  4977. 'Helios',
  4978. '/option',
  4979. '/select',
  4980. ['div' => ['class' => 'error-message']],
  4981. 'Invalid',
  4982. '/div',
  4983. '/div',
  4984. ];
  4985. $this->assertHtml($expected, $result);
  4986. }
  4987. /**
  4988. * testSelectMultipleCheckboxes method
  4989. *
  4990. * Test generation of multi select elements in checkbox format.
  4991. *
  4992. * @return void
  4993. */
  4994. public function testSelectMultipleCheckboxes()
  4995. {
  4996. $result = $this->Form->select(
  4997. 'Model.multi_field',
  4998. ['first', 'second', 'third'],
  4999. ['multiple' => 'checkbox']
  5000. );
  5001. $expected = [
  5002. 'input' => [
  5003. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5004. ],
  5005. ['div' => ['class' => 'checkbox']],
  5006. ['label' => ['for' => 'model-multi-field-0']],
  5007. ['input' => [
  5008. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5009. 'value' => '0', 'id' => 'model-multi-field-0',
  5010. ]],
  5011. 'first',
  5012. '/label',
  5013. '/div',
  5014. ['div' => ['class' => 'checkbox']],
  5015. ['label' => ['for' => 'model-multi-field-1']],
  5016. ['input' => [
  5017. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5018. 'value' => '1', 'id' => 'model-multi-field-1',
  5019. ]],
  5020. 'second',
  5021. '/label',
  5022. '/div',
  5023. ['div' => ['class' => 'checkbox']],
  5024. ['label' => ['for' => 'model-multi-field-2']],
  5025. ['input' => [
  5026. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5027. 'value' => '2', 'id' => 'model-multi-field-2',
  5028. ]],
  5029. 'third',
  5030. '/label',
  5031. '/div',
  5032. ];
  5033. $this->assertHtml($expected, $result);
  5034. $result = $this->Form->select(
  5035. 'Model.multi_field',
  5036. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5037. ['multiple' => 'checkbox']
  5038. );
  5039. $expected = [
  5040. 'input' => [
  5041. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5042. ],
  5043. ['div' => ['class' => 'checkbox']],
  5044. ['label' => ['for' => 'model-multi-field-a+']],
  5045. ['input' => [
  5046. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5047. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  5048. ]],
  5049. 'first',
  5050. '/label',
  5051. '/div',
  5052. ['div' => ['class' => 'checkbox']],
  5053. ['label' => ['for' => 'model-multi-field-a++']],
  5054. ['input' => [
  5055. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5056. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  5057. ]],
  5058. 'second',
  5059. '/label',
  5060. '/div',
  5061. ['div' => ['class' => 'checkbox']],
  5062. ['label' => ['for' => 'model-multi-field-a+++']],
  5063. ['input' => [
  5064. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5065. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  5066. ]],
  5067. 'third',
  5068. '/label',
  5069. '/div',
  5070. ];
  5071. $this->assertHtml($expected, $result);
  5072. $result = $this->Form->select(
  5073. 'Model.multi_field',
  5074. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5075. ['multiple' => 'checkbox']
  5076. );
  5077. $expected = [
  5078. 'input' => [
  5079. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5080. ],
  5081. ['div' => ['class' => 'checkbox']],
  5082. ['label' => ['for' => 'model-multi-field-a-b']],
  5083. ['input' => [
  5084. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5085. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  5086. ]],
  5087. 'first',
  5088. '/label',
  5089. '/div',
  5090. ['div' => ['class' => 'checkbox']],
  5091. ['label' => ['for' => 'model-multi-field-a-b1']],
  5092. ['input' => [
  5093. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5094. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  5095. ]],
  5096. 'second',
  5097. '/label',
  5098. '/div',
  5099. ['div' => ['class' => 'checkbox']],
  5100. ['label' => ['for' => 'model-multi-field-a-b2']],
  5101. ['input' => [
  5102. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5103. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  5104. ]],
  5105. 'third',
  5106. '/label',
  5107. '/div',
  5108. ];
  5109. $this->assertHtml($expected, $result);
  5110. }
  5111. /**
  5112. * testSelectMultipleCheckboxRequestData method
  5113. *
  5114. * Ensure that multiCheckbox reads from the request data.
  5115. *
  5116. * @return void
  5117. */
  5118. public function testSelectMultipleCheckboxRequestData()
  5119. {
  5120. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5121. $result = $this->Form->select(
  5122. 'Model.tags',
  5123. ['1' => 'first', 'Array' => 'Array'],
  5124. ['multiple' => 'checkbox']
  5125. );
  5126. $expected = [
  5127. 'input' => [
  5128. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '',
  5129. ],
  5130. ['div' => ['class' => 'checkbox']],
  5131. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5132. ['input' => [
  5133. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5134. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5135. ]],
  5136. 'first',
  5137. '/label',
  5138. '/div',
  5139. ['div' => ['class' => 'checkbox']],
  5140. ['label' => ['for' => 'model-tags-array']],
  5141. ['input' => [
  5142. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5143. 'value' => 'Array', 'id' => 'model-tags-array',
  5144. ]],
  5145. 'Array',
  5146. '/label',
  5147. '/div',
  5148. ];
  5149. $this->assertHtml($expected, $result);
  5150. }
  5151. /**
  5152. * testSelectMultipleCheckboxSecurity method
  5153. *
  5154. * Checks the security hash array generated for multiple-input checkbox elements.
  5155. *
  5156. * @return void
  5157. */
  5158. public function testSelectMultipleCheckboxSecurity()
  5159. {
  5160. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  5161. $this->assertEquals([], $this->Form->fields);
  5162. $this->Form->select(
  5163. 'Model.multi_field',
  5164. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5165. ['multiple' => 'checkbox']
  5166. );
  5167. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5168. $result = $this->Form->secure($this->Form->fields);
  5169. $key = '8af36fb34e6f2ef8ba0eb473bb4365ec232f3fe5%3A';
  5170. $this->assertStringContainsString('"' . $key . '"', $result);
  5171. }
  5172. /**
  5173. * testSelectMultipleSecureWithNoOptions method
  5174. *
  5175. * Multiple select elements should always be secured as they always participate
  5176. * in the POST data.
  5177. *
  5178. * @return void
  5179. */
  5180. public function testSelectMultipleSecureWithNoOptions()
  5181. {
  5182. $this->assertEquals([], $this->Form->fields);
  5183. $this->Form->select(
  5184. 'Model.select',
  5185. [],
  5186. ['multiple' => true]
  5187. );
  5188. $this->assertEquals(['Model.select'], $this->Form->fields);
  5189. }
  5190. /**
  5191. * testSelectNoSecureWithNoOptions method
  5192. *
  5193. * When a select box has no options it should not be added to the fields list
  5194. * as it always fail post validation.
  5195. *
  5196. * @return void
  5197. */
  5198. public function testSelectNoSecureWithNoOptions()
  5199. {
  5200. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testkey'));
  5201. $this->assertEquals([], $this->Form->fields);
  5202. $this->Form->select(
  5203. 'Model.select',
  5204. []
  5205. );
  5206. $this->assertEquals([], $this->Form->fields);
  5207. $this->Form->select(
  5208. 'Model.user_id',
  5209. [],
  5210. ['empty' => true]
  5211. );
  5212. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5213. }
  5214. /**
  5215. * testControlMultipleCheckboxes method
  5216. *
  5217. * Test control() resulting in multi select elements being generated.
  5218. *
  5219. * @return void
  5220. */
  5221. public function testControlMultipleCheckboxes()
  5222. {
  5223. $result = $this->Form->control('Model.multi_field', [
  5224. 'options' => ['first', 'second', 'third'],
  5225. 'multiple' => 'checkbox',
  5226. ]);
  5227. $expected = [
  5228. ['div' => ['class' => 'input select']],
  5229. ['label' => ['for' => 'model-multi-field']],
  5230. 'Multi Field',
  5231. '/label',
  5232. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5233. ['div' => ['class' => 'checkbox']],
  5234. ['label' => ['for' => 'model-multi-field-0']],
  5235. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5236. 'first',
  5237. '/label',
  5238. '/div',
  5239. ['div' => ['class' => 'checkbox']],
  5240. ['label' => ['for' => 'model-multi-field-1']],
  5241. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5242. 'second',
  5243. '/label',
  5244. '/div',
  5245. ['div' => ['class' => 'checkbox']],
  5246. ['label' => ['for' => 'model-multi-field-2']],
  5247. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5248. 'third',
  5249. '/label',
  5250. '/div',
  5251. '/div',
  5252. ];
  5253. $this->assertHtml($expected, $result);
  5254. $result = $this->Form->control('Model.multi_field', [
  5255. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5256. 'multiple' => 'checkbox',
  5257. ]);
  5258. $expected = [
  5259. ['div' => ['class' => 'input select']],
  5260. ['label' => ['for' => 'model-multi-field']],
  5261. 'Multi Field',
  5262. '/label',
  5263. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5264. ['div' => ['class' => 'checkbox']],
  5265. ['label' => ['for' => 'model-multi-field-a']],
  5266. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5267. 'first',
  5268. '/label',
  5269. '/div',
  5270. ['div' => ['class' => 'checkbox']],
  5271. ['label' => ['for' => 'model-multi-field-b']],
  5272. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5273. 'second',
  5274. '/label',
  5275. '/div',
  5276. ['div' => ['class' => 'checkbox']],
  5277. ['label' => ['for' => 'model-multi-field-c']],
  5278. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5279. 'third',
  5280. '/label',
  5281. '/div',
  5282. '/div',
  5283. ];
  5284. $this->assertHtml($expected, $result);
  5285. }
  5286. /**
  5287. * testSelectHiddenFieldOmission method
  5288. *
  5289. * Test that select() with 'hiddenField' => false omits the hidden field.
  5290. *
  5291. * @return void
  5292. */
  5293. public function testSelectHiddenFieldOmission()
  5294. {
  5295. $result = $this->Form->select(
  5296. 'Model.multi_field',
  5297. ['first', 'second'],
  5298. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5299. );
  5300. $this->assertStringNotContainsString('type="hidden"', $result);
  5301. }
  5302. /**
  5303. * testSelectCheckboxMultipleOverrideName method
  5304. *
  5305. * Test that select() with multiple = checkbox works with overriding name attribute.
  5306. *
  5307. * @return void
  5308. */
  5309. public function testSelectCheckboxMultipleOverrideName()
  5310. {
  5311. $result = $this->Form->select('category', ['1', '2'], [
  5312. 'multiple' => 'checkbox',
  5313. 'name' => 'fish',
  5314. ]);
  5315. $expected = [
  5316. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5317. ['div' => ['class' => 'checkbox']],
  5318. ['label' => ['for' => 'fish-0']],
  5319. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5320. '1',
  5321. '/label',
  5322. '/div',
  5323. ['div' => ['class' => 'checkbox']],
  5324. ['label' => ['for' => 'fish-1']],
  5325. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5326. '2',
  5327. '/label',
  5328. '/div',
  5329. ];
  5330. $this->assertHtml($expected, $result);
  5331. $result = $this->Form->multiCheckbox(
  5332. 'category',
  5333. new Collection(['1', '2']),
  5334. ['name' => 'fish']
  5335. );
  5336. $this->assertHtml($expected, $result);
  5337. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5338. 'name' => 'fish',
  5339. ]);
  5340. $this->assertHtml($expected, $result);
  5341. }
  5342. /**
  5343. * testControlMultiCheckbox method
  5344. *
  5345. * Test that control() works with multicheckbox.
  5346. *
  5347. * @return void
  5348. */
  5349. public function testControlMultiCheckbox()
  5350. {
  5351. $result = $this->Form->control('category', [
  5352. 'type' => 'multicheckbox',
  5353. 'options' => ['1', '2'],
  5354. ]);
  5355. $expected = [
  5356. ['div' => ['class' => 'input multicheckbox']],
  5357. '<label',
  5358. 'Category',
  5359. '/label',
  5360. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5361. ['div' => ['class' => 'checkbox']],
  5362. ['label' => ['for' => 'category-0']],
  5363. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5364. '1',
  5365. '/label',
  5366. '/div',
  5367. ['div' => ['class' => 'checkbox']],
  5368. ['label' => ['for' => 'category-1']],
  5369. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5370. '2',
  5371. '/label',
  5372. '/div',
  5373. '/div',
  5374. ];
  5375. $this->assertHtml($expected, $result);
  5376. }
  5377. /**
  5378. * testCheckbox method
  5379. *
  5380. * Test generation of checkboxes.
  5381. *
  5382. * @return void
  5383. */
  5384. public function testCheckbox()
  5385. {
  5386. $result = $this->Form->checkbox('Model.field');
  5387. $expected = [
  5388. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5389. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5390. ];
  5391. $this->assertHtml($expected, $result);
  5392. $result = $this->Form->checkbox('Model.field', [
  5393. 'id' => 'theID',
  5394. 'value' => 'myvalue',
  5395. 'form' => 'my-form',
  5396. ]);
  5397. $expected = [
  5398. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5399. ['input' => [
  5400. 'type' => 'checkbox', 'name' => 'Model[field]',
  5401. 'value' => 'myvalue', 'id' => 'theID',
  5402. 'form' => 'my-form',
  5403. ]],
  5404. ];
  5405. $this->assertHtml($expected, $result);
  5406. }
  5407. /**
  5408. * testCheckboxDefaultValue method
  5409. *
  5410. * Test default value setting on checkbox() method.
  5411. *
  5412. * @return void
  5413. */
  5414. public function testCheckboxDefaultValue()
  5415. {
  5416. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5417. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5418. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5419. $this->assertHtml($expected, $result);
  5420. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5421. $this->Form->create();
  5422. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5423. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5424. $this->assertHtml($expected, $result);
  5425. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5426. $this->Form->create();
  5427. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5428. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5429. $this->assertHtml($expected, $result);
  5430. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5431. $this->Form->create();
  5432. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5433. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5434. $this->assertHtml($expected, $result);
  5435. $Articles = $this->getTableLocator()->get('Articles');
  5436. $Articles->getSchema()->addColumn(
  5437. 'published',
  5438. ['type' => 'boolean', 'null' => false, 'default' => true]
  5439. );
  5440. $this->Form->create($Articles->newEmptyEntity());
  5441. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5442. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5443. $this->assertHtml($expected, $result);
  5444. }
  5445. /**
  5446. * testCheckboxCheckedAndError method
  5447. *
  5448. * Test checkbox being checked or having errors.
  5449. *
  5450. * @return void
  5451. */
  5452. public function testCheckboxCheckedAndError()
  5453. {
  5454. $this->article['errors'] = [
  5455. 'published' => true,
  5456. ];
  5457. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5458. $this->Form->create($this->article);
  5459. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5460. $expected = [
  5461. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5462. ['input' => [
  5463. 'type' => 'checkbox',
  5464. 'name' => 'published',
  5465. 'value' => 'myvalue',
  5466. 'id' => 'theID',
  5467. 'checked' => 'checked',
  5468. 'class' => 'form-error',
  5469. ]],
  5470. ];
  5471. $this->assertHtml($expected, $result);
  5472. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5473. $this->Form->create($this->article);
  5474. $result = $this->Form->checkbox('published');
  5475. $expected = [
  5476. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5477. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5478. ];
  5479. $this->assertHtml($expected, $result);
  5480. }
  5481. /**
  5482. * testCheckboxCustomNameAttribute method
  5483. *
  5484. * Test checkbox() with a custom name attribute.
  5485. *
  5486. * @return void
  5487. */
  5488. public function testCheckboxCustomNameAttribute()
  5489. {
  5490. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5491. $expected = [
  5492. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5493. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5494. ];
  5495. $this->assertHtml($expected, $result);
  5496. }
  5497. /**
  5498. * testCheckboxHiddenField method
  5499. *
  5500. * Test that the hidden input for checkboxes can be omitted or set to a
  5501. * specific value.
  5502. *
  5503. * @return void
  5504. */
  5505. public function testCheckboxHiddenField()
  5506. {
  5507. $result = $this->Form->checkbox('UserForm.something', [
  5508. 'hiddenField' => false,
  5509. ]);
  5510. $expected = [
  5511. 'input' => [
  5512. 'type' => 'checkbox',
  5513. 'name' => 'UserForm[something]',
  5514. 'value' => '1',
  5515. ],
  5516. ];
  5517. $this->assertHtml($expected, $result);
  5518. $result = $this->Form->checkbox('UserForm.something', [
  5519. 'value' => 'Y',
  5520. 'hiddenField' => 'N',
  5521. ]);
  5522. $expected = [
  5523. ['input' => [
  5524. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5525. 'value' => 'N',
  5526. ]],
  5527. ['input' => [
  5528. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5529. 'value' => 'Y',
  5530. ]],
  5531. ];
  5532. $this->assertHtml($expected, $result);
  5533. }
  5534. /**
  5535. * testTime method
  5536. *
  5537. * Test the time type.
  5538. *
  5539. * @return void
  5540. */
  5541. public function testTime()
  5542. {
  5543. $result = $this->Form->time('start_time', [
  5544. 'value' => '2014-03-08 16:30:00',
  5545. ]);
  5546. $expected = [
  5547. 'input' => [
  5548. 'type' => 'time',
  5549. 'name' => 'start_time',
  5550. 'value' => '16:30:00',
  5551. ],
  5552. ];
  5553. $this->assertHtml($expected, $result);
  5554. }
  5555. /**
  5556. * testDate method
  5557. *
  5558. * Test the date type.
  5559. *
  5560. * @return void
  5561. */
  5562. public function testDate()
  5563. {
  5564. $result = $this->Form->date('start_day', [
  5565. 'value' => '2014-03-08',
  5566. ]);
  5567. $expected = [
  5568. 'input' => [
  5569. 'type' => 'date',
  5570. 'name' => 'start_day',
  5571. 'value' => '2014-03-08',
  5572. ],
  5573. ];
  5574. $this->assertHtml($expected, $result);
  5575. $result = $this->Form->date('start_day', [
  5576. 'value' => new Date('2014-03-08'),
  5577. ]);
  5578. $this->assertHtml($expected, $result);
  5579. }
  5580. /**
  5581. * testDateTime method
  5582. *
  5583. * @return void
  5584. */
  5585. public function testDateTime()
  5586. {
  5587. $result = $this->Form->dateTime('date', ['default' => true]);
  5588. $expected = [
  5589. 'input' => [
  5590. 'type' => 'datetime-local',
  5591. 'name' => 'date',
  5592. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}(:\d{2})?/',
  5593. ],
  5594. ];
  5595. $this->assertHtml($expected, $result);
  5596. }
  5597. /**
  5598. * testDateTimeSecured method
  5599. *
  5600. * Test that datetime fields are added to protected fields list.
  5601. *
  5602. * @return void
  5603. */
  5604. public function testDateTimeSecured()
  5605. {
  5606. $this->View->setRequest(
  5607. $this->View->getRequest()->withParam('_Token', ['unlockedFields' => []])
  5608. );
  5609. $this->Form->dateTime('date');
  5610. $expected = ['date'];
  5611. $this->assertEquals($expected, $this->Form->fields);
  5612. $this->Form->fields = [];
  5613. $this->Form->date('published');
  5614. $expected = ['published'];
  5615. $this->assertEquals($expected, $this->Form->fields);
  5616. }
  5617. /**
  5618. * testDateTimeSecuredDisabled method
  5619. *
  5620. * Test that datetime fields are added to protected fields list.
  5621. *
  5622. * @return void
  5623. */
  5624. public function testDateTimeSecuredDisabled()
  5625. {
  5626. $this->View->setRequest(
  5627. $this->View->getRequest()->withParam('_Token', ['unlockedFields' => []])
  5628. );
  5629. $this->Form->dateTime('date', ['secure' => false]);
  5630. $expected = [];
  5631. $this->assertEquals($expected, $this->Form->fields);
  5632. $this->Form->fields = [];
  5633. $this->Form->date('published', ['secure' => false]);
  5634. $expected = [];
  5635. $this->assertEquals($expected, $this->Form->fields);
  5636. }
  5637. /**
  5638. * testDatetimeWithDefault method
  5639. *
  5640. * Test that datetime() and default values work.
  5641. *
  5642. * @return void
  5643. */
  5644. public function testDatetimeWithDefault()
  5645. {
  5646. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5647. $expected = [
  5648. 'input' => [
  5649. 'type' => 'datetime-local',
  5650. 'name' => 'updated',
  5651. 'value' => '2009-06-01T11:15:30',
  5652. ],
  5653. ];
  5654. $this->assertHtml($expected, $result);
  5655. $result = $this->Form->dateTime('updated', [
  5656. 'default' => '2009-06-01 11:15:30',
  5657. ]);
  5658. $expected = [
  5659. 'input' => [
  5660. 'type' => 'datetime-local',
  5661. 'name' => 'updated',
  5662. 'value' => '2009-06-01T11:15:30',
  5663. ],
  5664. ];
  5665. $this->assertHtml($expected, $result);
  5666. }
  5667. /**
  5668. * testMonth method
  5669. *
  5670. * Test generation of a month input.
  5671. *
  5672. * @return void
  5673. */
  5674. public function testMonth()
  5675. {
  5676. $result = $this->Form->month('field', ['value' => '']);
  5677. $expected = [
  5678. 'input' => [
  5679. 'type' => 'month',
  5680. 'name' => 'field',
  5681. 'value' => '',
  5682. ],
  5683. ];
  5684. $this->assertHtml($expected, $result);
  5685. $this->View->setRequest(
  5686. $this->View->getRequest()->withData('release', '2050-02-10')
  5687. );
  5688. $this->Form->create();
  5689. $result = $this->Form->month('release');
  5690. $expected = [
  5691. 'input' => [
  5692. 'type' => 'month',
  5693. 'name' => 'release',
  5694. 'value' => '2050-02',
  5695. ],
  5696. ];
  5697. $this->assertHtml($expected, $result);
  5698. $this->View->setRequest(
  5699. $this->View->getRequest()->withData('release', '2050-03')
  5700. );
  5701. $this->Form->create();
  5702. $result = $this->Form->month('release');
  5703. $expected = [
  5704. 'input' => [
  5705. 'type' => 'month',
  5706. 'name' => 'release',
  5707. 'value' => '2050-03',
  5708. ],
  5709. ];
  5710. $this->assertHtml($expected, $result);
  5711. }
  5712. /**
  5713. * testYear method
  5714. *
  5715. * Test generation of a year input.
  5716. *
  5717. * @return void
  5718. */
  5719. public function testYear()
  5720. {
  5721. $this->View->setRequest(
  5722. $this->View->getRequest()->withData('published', '2006')
  5723. );
  5724. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5725. $expected = [
  5726. ['select' => ['name' => 'field']],
  5727. ['option' => ['selected' => 'selected', 'value' => '']],
  5728. '/option',
  5729. ['option' => ['value' => '2007']],
  5730. '2007',
  5731. '/option',
  5732. ['option' => ['value' => '2006']],
  5733. '2006',
  5734. '/option',
  5735. '/select',
  5736. ];
  5737. $this->assertHtml($expected, $result);
  5738. $result = $this->Form->year('field', [
  5739. 'value' => '',
  5740. 'min' => 2006,
  5741. 'max' => 2007,
  5742. 'order' => 'asc',
  5743. ]);
  5744. $expected = [
  5745. ['select' => ['name' => 'field']],
  5746. ['option' => ['selected' => 'selected', 'value' => '']],
  5747. '/option',
  5748. ['option' => ['value' => '2006']],
  5749. '2006',
  5750. '/option',
  5751. ['option' => ['value' => '2007']],
  5752. '2007',
  5753. '/option',
  5754. '/select',
  5755. ];
  5756. $this->assertHtml($expected, $result);
  5757. $result = $this->Form->year('published', [
  5758. 'empty' => false,
  5759. 'min' => 2006,
  5760. 'max' => 2007,
  5761. ]);
  5762. $expected = [
  5763. ['select' => ['name' => 'published']],
  5764. ['option' => ['value' => '2007']],
  5765. '2007',
  5766. '/option',
  5767. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5768. '2006',
  5769. '/option',
  5770. '/select',
  5771. ];
  5772. $this->assertHtml($expected, $result);
  5773. $result = $this->Form->year('published', [
  5774. 'empty' => 'Published on',
  5775. ]);
  5776. $this->assertStringContainsString('Published on', $result);
  5777. }
  5778. /**
  5779. * testControlYearPreEpoch method
  5780. *
  5781. * Test minYear being prior to the unix epoch.
  5782. *
  5783. * @return void
  5784. */
  5785. public function testControlYearPreEpoch()
  5786. {
  5787. $start = date('Y') - 80;
  5788. $end = date('Y') - 18;
  5789. $result = $this->Form->control('birth_year', [
  5790. 'type' => 'year',
  5791. 'label' => 'Birth Year',
  5792. 'min' => $start,
  5793. 'max' => $end,
  5794. ]);
  5795. $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
  5796. $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
  5797. $this->assertStringNotContainsString('value="00">00', $result);
  5798. }
  5799. /**
  5800. * test control() datetime & required attributes
  5801. *
  5802. * @return void
  5803. */
  5804. public function testControlDatetimeRequired()
  5805. {
  5806. $result = $this->Form->control('birthday', [
  5807. 'type' => 'date',
  5808. 'required' => true,
  5809. ]);
  5810. $this->assertStringContainsString(
  5811. '<input type="date" name="birthday" required="required"',
  5812. $result
  5813. );
  5814. }
  5815. /**
  5816. * testYearAutoExpandRange method
  5817. *
  5818. * @return void
  5819. */
  5820. public function testYearAutoExpandRange()
  5821. {
  5822. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5823. $result = $this->Form->year('birthday');
  5824. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5825. $result = $matches[1];
  5826. $expected = range(date('Y') + 5, 1930);
  5827. $this->assertEquals($expected, $result);
  5828. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5829. $this->Form->create();
  5830. $result = $this->Form->year('release');
  5831. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5832. $result = $matches[1];
  5833. $expected = range(2050, date('Y') - 5);
  5834. $this->assertEquals($expected, $result);
  5835. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5836. $this->Form->create();
  5837. $result = $this->Form->year('release', [
  5838. 'min' => 1890,
  5839. 'max' => 1900,
  5840. ]);
  5841. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5842. $result = $matches[1];
  5843. $expected = range(1900, 1881);
  5844. $this->assertEquals($expected, $result);
  5845. }
  5846. /**
  5847. * testControlLabelFalse method
  5848. *
  5849. * Test the label option being set to false.
  5850. *
  5851. * @return void
  5852. */
  5853. public function testControlLabelFalse()
  5854. {
  5855. $this->Form->create($this->article);
  5856. $result = $this->Form->control('title', ['label' => false]);
  5857. $expected = [
  5858. 'div' => ['class' => 'input text required'],
  5859. 'input' => [
  5860. 'type' => 'text',
  5861. 'required' => 'required',
  5862. 'id' => 'title',
  5863. 'name' => 'title',
  5864. 'oninvalid' => 'this.setCustomValidity(&#039;This field is required&#039;)',
  5865. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  5866. ],
  5867. '/div',
  5868. ];
  5869. $this->assertHtml($expected, $result);
  5870. }
  5871. /**
  5872. * testTextArea method
  5873. *
  5874. * Test generation of a textarea input.
  5875. *
  5876. * @return void
  5877. */
  5878. public function testTextArea()
  5879. {
  5880. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5881. $result = $this->Form->textarea('field');
  5882. $expected = [
  5883. 'textarea' => ['name' => 'field', 'rows' => 5],
  5884. 'some test data',
  5885. '/textarea',
  5886. ];
  5887. $this->assertHtml($expected, $result);
  5888. $result = $this->Form->textarea('user.bio');
  5889. $expected = [
  5890. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5891. '/textarea',
  5892. ];
  5893. $this->assertHtml($expected, $result);
  5894. $this->View->setRequest($this->View->getRequest()
  5895. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5896. $this->Form->create();
  5897. $result = $this->Form->textarea('field');
  5898. $expected = [
  5899. 'textarea' => ['name' => 'field', 'rows' => 5],
  5900. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5901. '/textarea',
  5902. ];
  5903. $this->assertHtml($expected, $result);
  5904. $this->View->setRequest($this->View->getRequest()->withData(
  5905. 'Model.field',
  5906. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  5907. ));
  5908. $this->Form->create();
  5909. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5910. $expected = [
  5911. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  5912. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5913. '/textarea',
  5914. ];
  5915. $this->assertHtml($expected, $result);
  5916. $result = $this->Form->textarea('0.OtherModel.field');
  5917. $expected = [
  5918. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  5919. '/textarea',
  5920. ];
  5921. $this->assertHtml($expected, $result);
  5922. }
  5923. /**
  5924. * testTextAreaWithStupidCharacters method
  5925. *
  5926. * Test text area with non-ascii characters.
  5927. *
  5928. * @return void
  5929. */
  5930. public function testTextAreaWithStupidCharacters()
  5931. {
  5932. $result = $this->Form->textarea('Post.content', [
  5933. 'value' => 'GREAT®',
  5934. 'rows' => '15',
  5935. 'cols' => '75',
  5936. ]);
  5937. $expected = [
  5938. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5939. 'GREAT®',
  5940. '/textarea',
  5941. ];
  5942. $this->assertHtml($expected, $result);
  5943. }
  5944. /**
  5945. * testTextAreaMaxLength method
  5946. *
  5947. * Test textareas maxlength read from schema.
  5948. *
  5949. * @return void
  5950. */
  5951. public function testTextAreaMaxLength()
  5952. {
  5953. $this->Form->create([
  5954. 'schema' => [
  5955. 'stuff' => ['type' => 'string', 'length' => 10],
  5956. ],
  5957. ]);
  5958. $result = $this->Form->control('other', ['type' => 'textarea']);
  5959. $expected = [
  5960. 'div' => ['class' => 'input textarea'],
  5961. 'label' => ['for' => 'other'],
  5962. 'Other',
  5963. '/label',
  5964. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  5965. '/textarea',
  5966. '/div',
  5967. ];
  5968. $this->assertHtml($expected, $result);
  5969. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  5970. $expected = [
  5971. 'div' => ['class' => 'input textarea'],
  5972. 'label' => ['for' => 'stuff'],
  5973. 'Stuff',
  5974. '/label',
  5975. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  5976. '/textarea',
  5977. '/div',
  5978. ];
  5979. $this->assertHtml($expected, $result);
  5980. }
  5981. /**
  5982. * testHiddenField method
  5983. *
  5984. * Test generation of a hidden input.
  5985. *
  5986. * @return void
  5987. */
  5988. public function testHiddenField()
  5989. {
  5990. $this->article['errors'] = [
  5991. 'field' => true,
  5992. ];
  5993. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  5994. $this->Form->create($this->article);
  5995. $result = $this->Form->hidden('field', ['id' => 'theID']);
  5996. $expected = [
  5997. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  5998. $this->assertHtml($expected, $result);
  5999. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6000. $expected = [
  6001. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  6002. ];
  6003. $this->assertHtml($expected, $result);
  6004. }
  6005. /**
  6006. * testFileUploadField method
  6007. *
  6008. * Test generation of a file upload input.
  6009. *
  6010. * @return void
  6011. */
  6012. public function testFileUploadField()
  6013. {
  6014. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6015. $result = $this->Form->file('Model.upload');
  6016. $this->assertHtml($expected, $result);
  6017. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  6018. 'name' => '', 'type' => '', 'tmp_name' => '',
  6019. 'error' => 4, 'size' => 0,
  6020. ]));
  6021. $result = $this->Form->file('Model.upload');
  6022. $this->assertHtml($expected, $result);
  6023. $this->View->setRequest(
  6024. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6025. );
  6026. $result = $this->Form->file('Model.upload');
  6027. $this->assertHtml($expected, $result);
  6028. }
  6029. /**
  6030. * testFileUploadOnOtherModel method
  6031. *
  6032. * Test File upload input on a model not used in create().
  6033. *
  6034. * @return void
  6035. */
  6036. public function testFileUploadOnOtherModel()
  6037. {
  6038. $this->Form->create($this->article, ['type' => 'file']);
  6039. $result = $this->Form->file('ValidateProfile.city');
  6040. $expected = [
  6041. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  6042. ];
  6043. $this->assertHtml($expected, $result);
  6044. }
  6045. /**
  6046. * testButton method
  6047. *
  6048. * Test generation of a form button.
  6049. *
  6050. * @return void
  6051. */
  6052. public function testButton()
  6053. {
  6054. $result = $this->Form->button('Hi');
  6055. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6056. $this->assertHtml($expected, $result);
  6057. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6058. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6059. $this->assertHtml($expected, $result);
  6060. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6061. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6062. $this->assertHtml($expected, $result);
  6063. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6064. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6065. $this->assertHtml($expected, $result);
  6066. $result = $this->Form->button('No type', ['type' => false]);
  6067. $expected = ['button' => [], 'No type', '/button'];
  6068. $this->assertHtml($expected, $result);
  6069. $result = $this->Form->button('Upload Text', [
  6070. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6071. 'escape' => false,
  6072. ]);
  6073. $this->assertNotRegExp('/\&039/', $result);
  6074. }
  6075. /**
  6076. * testButtonUnlockedByDefault method
  6077. *
  6078. * Test that button() makes unlocked fields by default.
  6079. *
  6080. * @return void
  6081. */
  6082. public function testButtonUnlockedByDefault()
  6083. {
  6084. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'secured'));
  6085. $this->Form->button('Save', ['name' => 'save']);
  6086. $this->Form->button('Clear');
  6087. $result = $this->Form->unlockField();
  6088. $this->assertEquals(['save'], $result);
  6089. }
  6090. /**
  6091. * Test generation of a form button with confirm message.
  6092. *
  6093. * @return void
  6094. */
  6095. public function testButtonWithConfirm()
  6096. {
  6097. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6098. $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm(&quot;Confirm me!&quot;)) { return true; } return false;'], 'Hi', '/button'];
  6099. $this->assertHtml($expected, $result);
  6100. }
  6101. /**
  6102. * testPostButton method
  6103. *
  6104. * @return void
  6105. */
  6106. public function testPostButton()
  6107. {
  6108. $result = $this->Form->postButton('Hi', '/controller/action');
  6109. $expected = [
  6110. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6111. 'div' => ['style' => 'display:none;'],
  6112. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6113. '/div',
  6114. 'button' => ['type' => 'submit'],
  6115. 'Hi',
  6116. '/button',
  6117. '/form',
  6118. ];
  6119. $this->assertHtml($expected, $result);
  6120. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6121. $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
  6122. }
  6123. /**
  6124. * testPostButtonMethodType method
  6125. *
  6126. * @return void
  6127. */
  6128. public function testPostButtonMethodType()
  6129. {
  6130. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6131. $expected = [
  6132. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6133. 'div' => ['style' => 'display:none;'],
  6134. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6135. '/div',
  6136. 'button' => ['type' => 'submit'],
  6137. 'Hi',
  6138. '/button',
  6139. '/form',
  6140. ];
  6141. $this->assertHtml($expected, $result);
  6142. }
  6143. /**
  6144. * testPostButtonFormOptions method
  6145. *
  6146. * @return void
  6147. */
  6148. public function testPostButtonFormOptions()
  6149. {
  6150. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6151. $expected = [
  6152. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6153. 'div' => ['style' => 'display:none;'],
  6154. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6155. '/div',
  6156. 'button' => ['type' => 'submit'],
  6157. 'Hi',
  6158. '/button',
  6159. '/form',
  6160. ];
  6161. $this->assertHtml($expected, $result);
  6162. }
  6163. /**
  6164. * testPostButtonNestedData method
  6165. *
  6166. * Test using postButton with N dimensional data.
  6167. *
  6168. * @return void
  6169. */
  6170. public function testPostButtonNestedData()
  6171. {
  6172. $data = [
  6173. 'one' => [
  6174. 'two' => [
  6175. 3, 4, 5,
  6176. ],
  6177. ],
  6178. ];
  6179. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6180. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6181. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6182. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6183. }
  6184. /**
  6185. * testSecurePostButton method
  6186. *
  6187. * Test that postButton adds _Token fields.
  6188. *
  6189. * @return void
  6190. */
  6191. public function testSecurePostButton()
  6192. {
  6193. $this->View->setRequest($this->View->getRequest()
  6194. ->withParam('_csrfToken', 'testkey')
  6195. ->withParam('_Token.unlockedFields', []));
  6196. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6197. $tokenDebug = urlencode(json_encode([
  6198. '/posts/delete/1',
  6199. [],
  6200. [],
  6201. ]));
  6202. $expected = [
  6203. 'form' => [
  6204. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6205. ],
  6206. ['div' => ['style' => 'display:none;']],
  6207. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6208. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6209. '/div',
  6210. 'button' => ['type' => 'submit'],
  6211. 'Delete',
  6212. '/button',
  6213. ['div' => ['style' => 'display:none;']],
  6214. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6215. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6216. ['input' => [
  6217. 'type' => 'hidden',
  6218. 'name' => '_Token[debug]',
  6219. 'value' => $tokenDebug,
  6220. 'autocomplete' => 'off',
  6221. ]],
  6222. '/div',
  6223. '/form',
  6224. ];
  6225. $this->assertHtml($expected, $result);
  6226. }
  6227. /**
  6228. * testPostLink method
  6229. *
  6230. * @return void
  6231. */
  6232. public function testPostLink()
  6233. {
  6234. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6235. $expected = [
  6236. 'form' => [
  6237. 'method' => 'post', 'action' => '/posts/delete/1',
  6238. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6239. ],
  6240. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6241. '/form',
  6242. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6243. 'Delete',
  6244. '/a',
  6245. ];
  6246. $this->assertHtml($expected, $result);
  6247. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6248. $expected = [
  6249. 'form' => [
  6250. 'method' => 'post', 'action' => '/posts/delete/1',
  6251. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6252. ],
  6253. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6254. '/form',
  6255. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6256. 'Delete',
  6257. '/a',
  6258. ];
  6259. $this->assertHtml($expected, $result);
  6260. $result = $this->Form->postLink(
  6261. 'Delete',
  6262. '/posts/delete/1',
  6263. ['target' => '_blank', 'class' => 'btn btn-danger']
  6264. );
  6265. $expected = [
  6266. 'form' => [
  6267. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6268. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6269. ],
  6270. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6271. '/form',
  6272. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6273. 'Delete',
  6274. '/a',
  6275. ];
  6276. $this->assertHtml($expected, $result);
  6277. }
  6278. /**
  6279. * testPostLinkWithConfirm method
  6280. *
  6281. * Test the confirm option for postLink().
  6282. *
  6283. * @return void
  6284. */
  6285. public function testPostLinkWithConfirm()
  6286. {
  6287. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6288. $expected = [
  6289. 'form' => [
  6290. 'method' => 'post', 'action' => '/posts/delete/1',
  6291. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6292. ],
  6293. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6294. '/form',
  6295. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6296. 'Delete',
  6297. '/a',
  6298. ];
  6299. $this->assertHtml($expected, $result);
  6300. $result = $this->Form->postLink(
  6301. 'Delete',
  6302. '/posts/delete/1',
  6303. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6304. );
  6305. $expected = [
  6306. 'form' => [
  6307. 'method' => 'post', 'action' => '/posts/delete/1',
  6308. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6309. ],
  6310. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6311. '/form',
  6312. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6313. 'Delete',
  6314. '/a',
  6315. ];
  6316. $this->assertHtml($expected, $result);
  6317. $this->Form->setTemplates(['confirmJs' => 'if (confirm({{confirmMessage}})) { $(\'form[name="{{formName}}"]\').submit();};']);
  6318. $result = $this->Form->postLink(
  6319. 'Delete',
  6320. '/posts/delete/1',
  6321. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  6322. );
  6323. $expected = [
  6324. 'form' => [
  6325. 'method' => 'post', 'action' => '/posts/delete/1',
  6326. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6327. ],
  6328. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6329. '/form',
  6330. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\("Confirm this deletion\?"\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/'],
  6331. 'Delete',
  6332. '/a',
  6333. ];
  6334. $this->assertHtml($expected, $result);
  6335. }
  6336. /**
  6337. * testPostLinkWithQuery method
  6338. *
  6339. * Test postLink() with query string args.
  6340. *
  6341. * @return void
  6342. */
  6343. public function testPostLinkWithQuery()
  6344. {
  6345. $result = $this->Form->postLink(
  6346. 'Delete',
  6347. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6348. );
  6349. $expected = [
  6350. 'form' => [
  6351. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6352. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6353. ],
  6354. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6355. '/form',
  6356. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6357. 'Delete',
  6358. '/a',
  6359. ];
  6360. $this->assertHtml($expected, $result);
  6361. }
  6362. /**
  6363. * testPostLinkWithData method
  6364. *
  6365. * Test postLink with additional data.
  6366. *
  6367. * @return void
  6368. */
  6369. public function testPostLinkWithData()
  6370. {
  6371. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6372. $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
  6373. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6374. $this->Form->create($entity);
  6375. $this->Form->end();
  6376. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6377. $this->assertStringContainsString(
  6378. '<input type="hidden" name="name" value="show"',
  6379. $result,
  6380. 'should not contain entity data.'
  6381. );
  6382. }
  6383. /**
  6384. * testPostLinkSecurityHash method
  6385. *
  6386. * Test that security hashes for postLink include the url.
  6387. *
  6388. * @return void
  6389. */
  6390. public function testPostLinkSecurityHash()
  6391. {
  6392. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6393. $hash .= '%3Aid';
  6394. $this->View->setRequest($this->View->getRequest()->withParam('_Token.key', 'test'));
  6395. $result = $this->Form->postLink(
  6396. 'Delete',
  6397. '/posts/delete/1',
  6398. ['data' => ['id' => 1]]
  6399. );
  6400. $tokenDebug = urlencode(json_encode([
  6401. '/posts/delete/1',
  6402. [
  6403. 'id' => 1,
  6404. ],
  6405. [],
  6406. ]));
  6407. $expected = [
  6408. 'form' => [
  6409. 'method' => 'post', 'action' => '/posts/delete/1',
  6410. 'name', 'style' => 'display:none;',
  6411. ],
  6412. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6413. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6414. 'div' => ['style' => 'display:none;'],
  6415. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6416. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6417. ['input' => [
  6418. 'type' => 'hidden',
  6419. 'name' => '_Token[debug]',
  6420. 'value' => $tokenDebug,
  6421. 'autocomplete' => 'off',
  6422. ]],
  6423. '/div',
  6424. '/form',
  6425. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6426. 'Delete',
  6427. '/a',
  6428. ];
  6429. $this->assertHtml($expected, $result);
  6430. }
  6431. /**
  6432. * testPostLinkSecurityHashBlockMode method
  6433. *
  6434. * Test that postLink doesn't modify the fields in the containing form.
  6435. *
  6436. * postLink() calls inside open forms should not modify the field list
  6437. * for the form.
  6438. *
  6439. * @return void
  6440. */
  6441. public function testPostLinkSecurityHashBlockMode()
  6442. {
  6443. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6444. $hash .= '%3A';
  6445. $this->View->setRequest($this->View->getRequest()->withParam('_Token.key', 'test'));
  6446. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  6447. $this->Form->control('title');
  6448. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6449. $result = $this->View->fetch('postLink');
  6450. $this->assertEquals(['title'], $this->Form->fields);
  6451. $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
  6452. $reflect = new ReflectionProperty($this->Form, '_lastAction');
  6453. $reflect->setAccessible(true);
  6454. $this->assertEquals('/articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
  6455. }
  6456. /**
  6457. * testPostLinkSecurityHashNoDebugMode method
  6458. *
  6459. * Test that security does not include debug token if debug is false.
  6460. *
  6461. * @return void
  6462. */
  6463. public function testPostLinkSecurityHashNoDebugMode()
  6464. {
  6465. Configure::write('debug', false);
  6466. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6467. $hash .= '%3Aid';
  6468. $this->View->setRequest($this->View->getRequest()
  6469. ->withParam('_Token.key', 'test'));
  6470. $result = $this->Form->postLink(
  6471. 'Delete',
  6472. '/posts/delete/1',
  6473. ['data' => ['id' => 1]]
  6474. );
  6475. $expected = [
  6476. 'form' => [
  6477. 'method' => 'post', 'action' => '/posts/delete/1',
  6478. 'name', 'style' => 'display:none;',
  6479. ],
  6480. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6481. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6482. 'div' => ['style' => 'display:none;'],
  6483. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6484. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6485. '/div',
  6486. '/form',
  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. }
  6493. /**
  6494. * testPostLinkNestedData method
  6495. *
  6496. * Test using postLink with N dimensional data.
  6497. *
  6498. * @return void
  6499. */
  6500. public function testPostLinkNestedData()
  6501. {
  6502. $data = [
  6503. 'one' => [
  6504. 'two' => [
  6505. 3, 4, 5,
  6506. ],
  6507. ],
  6508. ];
  6509. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6510. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6511. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6512. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6513. }
  6514. /**
  6515. * testPostLinkAfterGetForm method
  6516. *
  6517. * Test creating postLinks after a GET form.
  6518. *
  6519. * @return void
  6520. */
  6521. public function testPostLinkAfterGetForm()
  6522. {
  6523. $this->View->setRequest($this->View->getRequest()
  6524. ->withParam('_csrfToken', 'testkey')
  6525. ->withParam('_Token', 'val'));
  6526. $this->Form->create($this->article, ['type' => 'get']);
  6527. $this->Form->end();
  6528. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6529. $tokenDebug = urlencode(json_encode([
  6530. '/posts/delete/1',
  6531. [],
  6532. [],
  6533. ]));
  6534. $expected = [
  6535. 'form' => [
  6536. 'method' => 'post', 'action' => '/posts/delete/1',
  6537. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6538. ],
  6539. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6540. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6541. 'div' => ['style' => 'display:none;'],
  6542. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6543. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6544. ['input' => [
  6545. 'type' => 'hidden', 'name' => '_Token[debug]',
  6546. 'value' => $tokenDebug,
  6547. 'autocomplete' => 'off',
  6548. ]],
  6549. '/div',
  6550. '/form',
  6551. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6552. 'Delete',
  6553. '/a',
  6554. ];
  6555. $this->assertHtml($expected, $result);
  6556. }
  6557. /**
  6558. * testPostLinkFormBuffer method
  6559. *
  6560. * Test that postLink adds form tags to view block.
  6561. *
  6562. * @return void
  6563. */
  6564. public function testPostLinkFormBuffer()
  6565. {
  6566. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6567. $expected = [
  6568. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6569. 'Delete',
  6570. '/a',
  6571. ];
  6572. $this->assertHtml($expected, $result);
  6573. $result = $this->View->fetch('postLink');
  6574. $expected = [
  6575. 'form' => [
  6576. 'method' => 'post', 'action' => '/posts/delete/1',
  6577. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6578. ],
  6579. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6580. '/form',
  6581. ];
  6582. $this->assertHtml($expected, $result);
  6583. $result = $this->Form->postLink(
  6584. 'Delete',
  6585. '/posts/delete/2',
  6586. ['block' => true, 'method' => 'DELETE']
  6587. );
  6588. $expected = [
  6589. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6590. 'Delete',
  6591. '/a',
  6592. ];
  6593. $this->assertHtml($expected, $result);
  6594. $result = $this->View->fetch('postLink');
  6595. $expected = [
  6596. 'form' => [
  6597. 'method' => 'post', 'action' => '/posts/delete/1',
  6598. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6599. ],
  6600. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6601. '/form',
  6602. [
  6603. 'form' => [
  6604. 'method' => 'post', 'action' => '/posts/delete/2',
  6605. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6606. ],
  6607. ],
  6608. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6609. '/form',
  6610. ];
  6611. $this->assertHtml($expected, $result);
  6612. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6613. $expected = [
  6614. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6615. 'Delete',
  6616. '/a',
  6617. ];
  6618. $this->assertHtml($expected, $result);
  6619. $result = $this->View->fetch('foobar');
  6620. $expected = [
  6621. 'form' => [
  6622. 'method' => 'post', 'action' => '/posts/delete/1',
  6623. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6624. ],
  6625. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6626. '/form',
  6627. ];
  6628. $this->assertHtml($expected, $result);
  6629. }
  6630. /**
  6631. * testSubmitButton method
  6632. *
  6633. * @return void
  6634. */
  6635. public function testSubmitButton()
  6636. {
  6637. $result = $this->Form->submit('');
  6638. $expected = [
  6639. 'div' => ['class' => 'submit'],
  6640. 'input' => ['type' => 'submit', 'value' => ''],
  6641. '/div',
  6642. ];
  6643. $this->assertHtml($expected, $result);
  6644. $result = $this->Form->submit('Test Submit');
  6645. $expected = [
  6646. 'div' => ['class' => 'submit'],
  6647. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6648. '/div',
  6649. ];
  6650. $this->assertHtml($expected, $result);
  6651. $result = $this->Form->submit('Next >');
  6652. $expected = [
  6653. 'div' => ['class' => 'submit'],
  6654. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6655. '/div',
  6656. ];
  6657. $this->assertHtml($expected, $result);
  6658. $result = $this->Form->submit('Next >', ['escape' => false]);
  6659. $expected = [
  6660. 'div' => ['class' => 'submit'],
  6661. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6662. '/div',
  6663. ];
  6664. $this->assertHtml($expected, $result);
  6665. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6666. $expected = [
  6667. 'div' => ['class' => 'submit'],
  6668. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6669. '/div',
  6670. ];
  6671. $this->assertHtml($expected, $result);
  6672. }
  6673. /**
  6674. * testSubmitImage method
  6675. *
  6676. * Test image submit types.
  6677. *
  6678. * @return void
  6679. */
  6680. public function testSubmitImage()
  6681. {
  6682. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6683. $expected = [
  6684. 'div' => ['class' => 'submit'],
  6685. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6686. '/div',
  6687. ];
  6688. $this->assertHtml($expected, $result);
  6689. $result = $this->Form->submit('/relative/cake.power.gif');
  6690. $expected = [
  6691. 'div' => ['class' => 'submit'],
  6692. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6693. '/div',
  6694. ];
  6695. $this->assertHtml($expected, $result);
  6696. $result = $this->Form->submit('cake.power.gif');
  6697. $expected = [
  6698. 'div' => ['class' => 'submit'],
  6699. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6700. '/div',
  6701. ];
  6702. $this->assertHtml($expected, $result);
  6703. $result = $this->Form->submit('Not.an.image');
  6704. $expected = [
  6705. 'div' => ['class' => 'submit'],
  6706. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6707. '/div',
  6708. ];
  6709. $this->assertHtml($expected, $result);
  6710. }
  6711. /**
  6712. * testSubmitUnlockedByDefault method
  6713. *
  6714. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6715. * be submitted at a time.
  6716. *
  6717. * @return void
  6718. */
  6719. public function testSubmitUnlockedByDefault()
  6720. {
  6721. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'secured'));
  6722. $this->Form->submit('Go go');
  6723. $this->Form->submit('Save', ['name' => 'save']);
  6724. $result = $this->Form->unlockField();
  6725. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6726. }
  6727. /**
  6728. * testSubmitImageTimestamp method
  6729. *
  6730. * Test submit image with timestamps.
  6731. *
  6732. * @return void
  6733. */
  6734. public function testSubmitImageTimestamp()
  6735. {
  6736. Configure::write('Asset.timestamp', 'force');
  6737. $result = $this->Form->submit('cake.power.gif');
  6738. $expected = [
  6739. 'div' => ['class' => 'submit'],
  6740. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6741. '/div',
  6742. ];
  6743. $this->assertHtml($expected, $result);
  6744. }
  6745. /**
  6746. * testDateTimeWithGetForms method
  6747. *
  6748. * Test that datetime() works with GET style forms.
  6749. *
  6750. * @return void
  6751. */
  6752. public function testDateTimeWithGetForms()
  6753. {
  6754. $this->Form->create($this->article, ['type' => 'get']);
  6755. $result = $this->Form->datetime('created');
  6756. $expected = [
  6757. 'input' => [
  6758. 'type' => 'datetime-local',
  6759. 'name' => 'created',
  6760. 'value' => '',
  6761. ],
  6762. ];
  6763. $this->assertHtml($expected, $result);
  6764. $result = $this->Form->datetime('created', ['default' => true]);
  6765. $expected = [
  6766. 'input' => [
  6767. 'type' => 'datetime-local',
  6768. 'name' => 'created',
  6769. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}(:\d{2})?/',
  6770. ],
  6771. ];
  6772. $this->assertHtml($expected, $result);
  6773. }
  6774. /**
  6775. * testForMagicControlNonExistingNorValidated method
  6776. *
  6777. * @return void
  6778. */
  6779. public function testForMagicControlNonExistingNorValidated()
  6780. {
  6781. $this->Form->create($this->article);
  6782. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6783. $result = $this->Form->control('non_existing_nor_validated');
  6784. $expected = [
  6785. 'label' => ['for' => 'non-existing-nor-validated'],
  6786. 'Non Existing Nor Validated',
  6787. '/label',
  6788. 'input' => [
  6789. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6790. 'id' => 'non-existing-nor-validated',
  6791. ],
  6792. ];
  6793. $this->assertHtml($expected, $result);
  6794. $result = $this->Form->control('non_existing_nor_validated', [
  6795. 'val' => 'my value',
  6796. ]);
  6797. $expected = [
  6798. 'label' => ['for' => 'non-existing-nor-validated'],
  6799. 'Non Existing Nor Validated',
  6800. '/label',
  6801. 'input' => [
  6802. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6803. 'value' => 'my value', 'id' => 'non-existing-nor-validated',
  6804. ],
  6805. ];
  6806. $this->assertHtml($expected, $result);
  6807. $this->View->setRequest(
  6808. $this->View->getRequest()->withData('non_existing_nor_validated', 'CakePHP magic')
  6809. );
  6810. $this->Form->create($this->article);
  6811. $result = $this->Form->control('non_existing_nor_validated');
  6812. $expected = [
  6813. 'label' => ['for' => 'non-existing-nor-validated'],
  6814. 'Non Existing Nor Validated',
  6815. '/label',
  6816. 'input' => [
  6817. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6818. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated',
  6819. ],
  6820. ];
  6821. $this->assertHtml($expected, $result);
  6822. }
  6823. /**
  6824. * testFormMagicControlLabel method
  6825. *
  6826. * @return void
  6827. */
  6828. public function testFormMagicControlLabel()
  6829. {
  6830. $this->getTableLocator()->get('Contacts', [
  6831. 'className' => ContactsTable::class,
  6832. ]);
  6833. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6834. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6835. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  6836. $expected = [
  6837. 'label' => ['for' => 'contacts-name'],
  6838. 'My label',
  6839. '/label',
  6840. 'input' => [
  6841. 'type' => 'text', 'name' => 'Contacts[name]',
  6842. 'id' => 'contacts-name', 'maxlength' => '255',
  6843. ],
  6844. ];
  6845. $this->assertHtml($expected, $result);
  6846. $result = $this->Form->control('name', [
  6847. 'label' => ['class' => 'mandatory'],
  6848. ]);
  6849. $expected = [
  6850. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6851. 'Name',
  6852. '/label',
  6853. 'input' => [
  6854. 'type' => 'text', 'name' => 'name',
  6855. 'id' => 'name', 'maxlength' => '255',
  6856. ],
  6857. ];
  6858. $this->assertHtml($expected, $result);
  6859. $result = $this->Form->control('name', [
  6860. 'div' => false,
  6861. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  6862. ]);
  6863. $expected = [
  6864. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6865. 'My label',
  6866. '/label',
  6867. 'input' => [
  6868. 'type' => 'text', 'name' => 'name',
  6869. 'id' => 'name', 'maxlength' => '255',
  6870. ],
  6871. ];
  6872. $this->assertHtml($expected, $result);
  6873. $result = $this->Form->control('Contact.name', [
  6874. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  6875. ]);
  6876. $expected = [
  6877. 'label' => ['for' => 'my_id'],
  6878. 'Name',
  6879. '/label',
  6880. 'input' => [
  6881. 'type' => 'text', 'name' => 'Contact[name]',
  6882. 'id' => 'my_id', 'maxlength' => '255',
  6883. ],
  6884. ];
  6885. $this->assertHtml($expected, $result);
  6886. $result = $this->Form->control('1.id');
  6887. $expected = ['input' => [
  6888. 'type' => 'hidden', 'name' => '1[id]',
  6889. 'id' => '1-id',
  6890. ]];
  6891. $this->assertHtml($expected, $result);
  6892. $result = $this->Form->control('1.name');
  6893. $expected = [
  6894. 'label' => ['for' => '1-name'],
  6895. 'Name',
  6896. '/label',
  6897. 'input' => [
  6898. 'type' => 'text', 'name' => '1[name]',
  6899. 'id' => '1-name', 'maxlength' => '255',
  6900. ],
  6901. ];
  6902. $this->assertHtml($expected, $result);
  6903. }
  6904. /**
  6905. * testFormEnd method
  6906. *
  6907. * @return void
  6908. */
  6909. public function testFormEnd()
  6910. {
  6911. $this->assertSame('</form>', $this->Form->end());
  6912. }
  6913. /**
  6914. * testMultiRecordForm method
  6915. *
  6916. * Test the generation of fields for a multi record form.
  6917. *
  6918. * @return void
  6919. */
  6920. public function testMultiRecordForm()
  6921. {
  6922. $this->loadFixtures('Articles', 'Comments');
  6923. $articles = $this->getTableLocator()->get('Articles');
  6924. $articles->hasMany('Comments');
  6925. $comment = new Entity(['comment' => 'Value']);
  6926. $article = new Article(['comments' => [$comment]]);
  6927. $this->Form->create([$article]);
  6928. $result = $this->Form->control('0.comments.1.comment');
  6929. // phpcs:disable
  6930. $expected = [
  6931. 'div' => ['class' => 'input textarea'],
  6932. 'label' => ['for' => '0-comments-1-comment'],
  6933. 'Comment',
  6934. '/label',
  6935. 'textarea' => [
  6936. 'name',
  6937. 'id' => '0-comments-1-comment',
  6938. 'rows' => 5
  6939. ],
  6940. '/textarea',
  6941. '/div'
  6942. ];
  6943. // phpcs:enable
  6944. $this->assertHtml($expected, $result);
  6945. $result = $this->Form->control('0.comments.0.comment');
  6946. // phpcs:disable
  6947. $expected = [
  6948. 'div' => ['class' => 'input textarea'],
  6949. 'label' => ['for' => '0-comments-0-comment'],
  6950. 'Comment',
  6951. '/label',
  6952. 'textarea' => [
  6953. 'name',
  6954. 'id' => '0-comments-0-comment',
  6955. 'rows' => 5
  6956. ],
  6957. 'Value',
  6958. '/textarea',
  6959. '/div'
  6960. ];
  6961. // phpcs:enable
  6962. $this->assertHtml($expected, $result);
  6963. $comment->setError('comment', ['Not valid']);
  6964. $result = $this->Form->control('0.comments.0.comment');
  6965. // phpcs:disable
  6966. $expected = [
  6967. 'div' => ['class' => 'input textarea error'],
  6968. 'label' => ['for' => '0-comments-0-comment'],
  6969. 'Comment',
  6970. '/label',
  6971. 'textarea' => [
  6972. 'name',
  6973. 'class' => 'form-error',
  6974. 'id' => '0-comments-0-comment',
  6975. 'rows' => 5
  6976. ],
  6977. 'Value',
  6978. '/textarea',
  6979. ['div' => ['class' => 'error-message']],
  6980. 'Not valid',
  6981. '/div',
  6982. '/div'
  6983. ];
  6984. // phpcs:enable
  6985. $this->assertHtml($expected, $result);
  6986. $this->getTableLocator()->get('Comments')
  6987. ->getValidator('default')
  6988. ->allowEmpty('comment', false);
  6989. $result = $this->Form->control('0.comments.1.comment');
  6990. // phpcs:disable
  6991. $expected = [
  6992. 'div' => ['class' => 'input textarea required'],
  6993. 'label' => ['for' => '0-comments-1-comment'],
  6994. 'Comment',
  6995. '/label',
  6996. 'textarea' => [
  6997. 'name',
  6998. 'required' => 'required',
  6999. 'id' => '0-comments-1-comment',
  7000. 'rows' => 5,
  7001. 'oninvalid' => 'this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  7002. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7003. ],
  7004. '/textarea',
  7005. '/div'
  7006. ];
  7007. // phpcs:enable
  7008. $this->assertHtml($expected, $result);
  7009. }
  7010. /**
  7011. * testHtml5Controls method
  7012. *
  7013. * Test that some html5 inputs + FormHelper::__call() work.
  7014. *
  7015. * @return void
  7016. */
  7017. public function testHtml5Controls()
  7018. {
  7019. $result = $this->Form->email('User.email');
  7020. $expected = [
  7021. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  7022. ];
  7023. $this->assertHtml($expected, $result);
  7024. $result = $this->Form->search('User.query');
  7025. $expected = [
  7026. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  7027. ];
  7028. $this->assertHtml($expected, $result);
  7029. $result = $this->Form->search('User.query', ['value' => 'test']);
  7030. $expected = [
  7031. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  7032. ];
  7033. $this->assertHtml($expected, $result);
  7034. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7035. $expected = [
  7036. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  7037. ];
  7038. $this->assertHtml($expected, $result);
  7039. }
  7040. /**
  7041. * testHtml5ControlWithControl method
  7042. *
  7043. * Test accessing html5 inputs through control().
  7044. *
  7045. * @return void
  7046. */
  7047. public function testHtml5ControlWithControl()
  7048. {
  7049. $this->Form->create();
  7050. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7051. $result = $this->Form->control('website', [
  7052. 'type' => 'url',
  7053. 'val' => 'http://domain.tld',
  7054. 'label' => false,
  7055. ]);
  7056. $expected = [
  7057. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7058. ];
  7059. $this->assertHtml($expected, $result);
  7060. }
  7061. /**
  7062. * testHtml5ControlException method
  7063. *
  7064. * Test errors when field name is missing.
  7065. *
  7066. * @return void
  7067. */
  7068. public function testHtml5ControlException()
  7069. {
  7070. $this->expectException(\Cake\Core\Exception\Exception::class);
  7071. $this->Form->email();
  7072. }
  7073. /**
  7074. * tests fields that are required use custom validation messages
  7075. *
  7076. * @return void
  7077. */
  7078. public function testHtml5ErrorMessage()
  7079. {
  7080. $this->Form->setConfig('autoSetCustomValidity', true);
  7081. $validator = (new \Cake\Validation\Validator())
  7082. ->requirePresence('email', true, 'Custom error message')
  7083. ->requirePresence('password')
  7084. ->alphaNumeric('password')
  7085. ->notBlank('phone');
  7086. $table = $this->getTableLocator()->get('Contacts', [
  7087. 'className' => ContactsTable::class,
  7088. ]);
  7089. $table->setValidator('default', $validator);
  7090. $contact = new Entity();
  7091. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7092. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7093. $result = $this->Form->control('password');
  7094. $expected = [
  7095. 'label' => ['for' => 'password'],
  7096. 'Password',
  7097. '/label',
  7098. 'input' => [
  7099. 'id' => 'password',
  7100. 'name' => 'password',
  7101. 'type' => 'password',
  7102. 'value' => '',
  7103. 'required' => 'required',
  7104. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7105. 'oninvalid' => 'this.setCustomValidity(&#039;This field is required&#039;)',
  7106. ],
  7107. ];
  7108. $this->assertHtml($expected, $result);
  7109. $result = $this->Form->control('phone');
  7110. $expected = [
  7111. 'label' => ['for' => 'phone'],
  7112. 'Phone',
  7113. '/label',
  7114. 'input' => [
  7115. 'id' => 'phone',
  7116. 'name' => 'phone',
  7117. 'type' => 'tel',
  7118. 'value' => '',
  7119. 'maxlength' => 255,
  7120. 'required' => 'required',
  7121. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7122. 'oninvalid' => 'this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  7123. ],
  7124. ];
  7125. $this->assertHtml($expected, $result);
  7126. $result = $this->Form->control('email');
  7127. $expected = [
  7128. 'label' => ['for' => 'email'],
  7129. 'Email',
  7130. '/label',
  7131. 'input' => [
  7132. 'id' => 'email',
  7133. 'name' => 'email',
  7134. 'type' => 'email',
  7135. 'value' => '',
  7136. 'maxlength' => 255,
  7137. 'required' => 'required',
  7138. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7139. 'oninvalid' => 'this.setCustomValidity(&#039;Custom error message&#039;)',
  7140. ],
  7141. ];
  7142. $this->assertHtml($expected, $result);
  7143. }
  7144. /**
  7145. * tests that custom validation messages are in templateVars
  7146. *
  7147. * @return void
  7148. */
  7149. public function testHtml5ErrorMessageInTemplateVars()
  7150. {
  7151. $validator = (new \Cake\Validation\Validator())
  7152. ->requirePresence('email', true, 'Custom error "message" & entities')
  7153. ->requirePresence('password')
  7154. ->alphaNumeric('password')
  7155. ->notBlank('phone');
  7156. $table = $this->getTableLocator()->get('Contacts', [
  7157. 'className' => ContactsTable::class,
  7158. ]);
  7159. $table->setValidator('default', $validator);
  7160. $contact = new Entity();
  7161. $this->Form->setConfig('autoSetCustomValidity', false);
  7162. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7163. $this->Form->setTemplates([
  7164. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7165. 'inputContainer' => '{{content}}',
  7166. ]);
  7167. $result = $this->Form->control('password');
  7168. $expected = [
  7169. 'label' => ['for' => 'password'],
  7170. 'Password',
  7171. '/label',
  7172. 'input' => [
  7173. 'id' => 'password',
  7174. 'name' => 'password',
  7175. 'type' => 'password',
  7176. 'value' => '',
  7177. 'required' => 'required',
  7178. 'data-message' => 'This field is required',
  7179. ],
  7180. ];
  7181. $this->assertHtml($expected, $result);
  7182. $result = $this->Form->control('phone');
  7183. $expected = [
  7184. 'label' => ['for' => 'phone'],
  7185. 'Phone',
  7186. '/label',
  7187. 'input' => [
  7188. 'id' => 'phone',
  7189. 'name' => 'phone',
  7190. 'type' => 'tel',
  7191. 'value' => '',
  7192. 'maxlength' => 255,
  7193. 'required' => 'required',
  7194. 'data-message' => 'This field cannot be left empty',
  7195. ],
  7196. ];
  7197. $this->assertHtml($expected, $result);
  7198. $result = $this->Form->control('email', [
  7199. 'templateVars' => [
  7200. 'custom' => 'data-custom="1"',
  7201. ],
  7202. ]);
  7203. $expected = [
  7204. 'label' => ['for' => 'email'],
  7205. 'Email',
  7206. '/label',
  7207. 'input' => [
  7208. 'id' => 'email',
  7209. 'name' => 'email',
  7210. 'type' => 'email',
  7211. 'value' => '',
  7212. 'maxlength' => 255,
  7213. 'required' => 'required',
  7214. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7215. 'data-custom' => '1',
  7216. ],
  7217. ];
  7218. $this->assertHtml($expected, $result);
  7219. $this->Form->setConfig('autoSetCustomValidity', true);
  7220. }
  7221. /**
  7222. * testRequiredAttribute method
  7223. *
  7224. * Tests that formhelper sets required attributes.
  7225. *
  7226. * @return void
  7227. */
  7228. public function testRequiredAttribute()
  7229. {
  7230. $this->article['required'] = [
  7231. 'title' => true,
  7232. 'body' => false,
  7233. ];
  7234. $this->Form->create($this->article);
  7235. $result = $this->Form->control('title');
  7236. $expected = [
  7237. 'div' => ['class' => 'input text required'],
  7238. 'label' => ['for' => 'title'],
  7239. 'Title',
  7240. '/label',
  7241. 'input' => [
  7242. 'type' => 'text',
  7243. 'name' => 'title',
  7244. 'id' => 'title',
  7245. 'required' => 'required',
  7246. 'oninvalid' => 'this.setCustomValidity(&#039;This field is required&#039;)',
  7247. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7248. ],
  7249. '/div',
  7250. ];
  7251. $this->assertHtml($expected, $result);
  7252. $result = $this->Form->control('title', ['required' => false]);
  7253. $this->assertStringNotContainsString('required', $result);
  7254. $result = $this->Form->control('body');
  7255. $expected = [
  7256. 'div' => ['class' => 'input text'],
  7257. 'label' => ['for' => 'body'],
  7258. 'Body',
  7259. '/label',
  7260. 'input' => [
  7261. 'type' => 'text',
  7262. 'name' => 'body',
  7263. 'id' => 'body',
  7264. ],
  7265. '/div',
  7266. ];
  7267. $this->assertHtml($expected, $result);
  7268. $result = $this->Form->control('body', ['required' => true]);
  7269. $this->assertStringContainsString('required', $result);
  7270. }
  7271. /**
  7272. * testControlsNotNested method
  7273. *
  7274. * Tests that it is possible to put inputs outside of the label.
  7275. *
  7276. * @return void
  7277. */
  7278. public function testControlsNotNested()
  7279. {
  7280. $this->Form->setTemplates([
  7281. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7282. 'formGroup' => '{{input}}{{label}}',
  7283. ]);
  7284. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7285. $expected = [
  7286. 'div' => ['class' => 'input checkbox'],
  7287. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7288. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7289. 'label' => ['for' => 'foo'],
  7290. 'Foo',
  7291. '/label',
  7292. '/div',
  7293. ];
  7294. $this->assertHtml($expected, $result);
  7295. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7296. $expected = [
  7297. 'div' => ['class' => 'input checkbox'],
  7298. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7299. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7300. '/div',
  7301. ];
  7302. $this->assertHtml($expected, $result);
  7303. $result = $this->Form->control('confirm', [
  7304. 'type' => 'radio',
  7305. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7306. ]);
  7307. $expected = [
  7308. 'div' => ['class' => 'input radio'],
  7309. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7310. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7311. ['label' => ['for' => 'confirm-y']],
  7312. 'Yes',
  7313. '/label',
  7314. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7315. ['label' => ['for' => 'confirm-n']],
  7316. 'No',
  7317. '/label',
  7318. '<label',
  7319. 'Confirm',
  7320. '/label',
  7321. '/div',
  7322. ];
  7323. $this->assertHtml($expected, $result);
  7324. $result = $this->Form->select('category', ['1', '2'], [
  7325. 'multiple' => 'checkbox',
  7326. 'name' => 'fish',
  7327. ]);
  7328. $expected = [
  7329. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7330. ['div' => ['class' => 'checkbox']],
  7331. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7332. ['label' => ['for' => 'fish-0']],
  7333. '1',
  7334. '/label',
  7335. '/div',
  7336. ['div' => ['class' => 'checkbox']],
  7337. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7338. ['label' => ['for' => 'fish-1']],
  7339. '2',
  7340. '/label',
  7341. '/div',
  7342. ];
  7343. $this->assertHtml($expected, $result);
  7344. }
  7345. /**
  7346. * testControlContainerTemplates method
  7347. *
  7348. * Test that *Container templates are used by input.
  7349. *
  7350. * @return void
  7351. */
  7352. public function testControlContainerTemplates()
  7353. {
  7354. $this->Form->setTemplates([
  7355. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7356. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7357. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7358. ]);
  7359. $this->article['errors'] = [
  7360. 'Article' => ['published' => 'error message'],
  7361. ];
  7362. $this->Form->create($this->article);
  7363. $result = $this->Form->control('accept', [
  7364. 'type' => 'checkbox',
  7365. ]);
  7366. $expected = [
  7367. 'div' => ['class' => 'check'],
  7368. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7369. 'label' => ['for' => 'accept'],
  7370. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7371. 'Accept',
  7372. '/label',
  7373. '/div',
  7374. ];
  7375. $this->assertHtml($expected, $result);
  7376. $result = $this->Form->control('accept', [
  7377. 'type' => 'radio',
  7378. 'options' => ['Y', 'N'],
  7379. ]);
  7380. $this->assertStringContainsString('<div class="rad">', $result);
  7381. $result = $this->Form->control('Article.published', [
  7382. 'type' => 'radio',
  7383. 'options' => ['Y', 'N'],
  7384. ]);
  7385. $this->assertStringContainsString('<div class="rad err">', $result);
  7386. }
  7387. /**
  7388. * testFormGroupTemplates method
  7389. *
  7390. * Test that *Container templates are used by input.
  7391. *
  7392. * @return void
  7393. */
  7394. public function testFormGroupTemplates()
  7395. {
  7396. $this->Form->setTemplates([
  7397. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7398. ]);
  7399. $this->Form->create($this->article);
  7400. $result = $this->Form->control('accept', [
  7401. 'type' => 'radio',
  7402. 'options' => ['Y', 'N'],
  7403. ]);
  7404. $this->assertStringContainsString('<div class="radio">', $result);
  7405. }
  7406. /**
  7407. * testResetTemplates method
  7408. *
  7409. * Test resetting templates.
  7410. *
  7411. * @return void
  7412. */
  7413. public function testResetTemplates()
  7414. {
  7415. $this->Form->setTemplates(['input' => '<input/>']);
  7416. $this->assertSame('<input/>', $this->Form->templater()->get('input'));
  7417. $this->Form->resetTemplates();
  7418. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7419. }
  7420. /**
  7421. * testContext method
  7422. *
  7423. * Test the context method.
  7424. *
  7425. * @return void
  7426. */
  7427. public function testContext()
  7428. {
  7429. $result = $this->Form->context();
  7430. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7431. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7432. $this->assertSame($mock, $this->Form->context($mock));
  7433. $this->assertSame($mock, $this->Form->context());
  7434. }
  7435. /**
  7436. * testAutoDomId method
  7437. *
  7438. * @return void
  7439. */
  7440. public function testAutoDomId()
  7441. {
  7442. $result = $this->Form->text('field', ['id' => true]);
  7443. $expected = [
  7444. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7445. ];
  7446. $this->assertHtml($expected, $result);
  7447. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7448. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7449. $expected = [
  7450. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7451. ['label' => ['for' => 'field-0']],
  7452. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7453. 'option A',
  7454. '/label',
  7455. ['label' => ['for' => 'field-1']],
  7456. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7457. 'option B',
  7458. '/label',
  7459. ];
  7460. $this->assertHtml($expected, $result);
  7461. $result = $this->Form->select(
  7462. 'multi_field',
  7463. ['first', 'second'],
  7464. ['multiple' => 'checkbox', 'id' => true]
  7465. );
  7466. $expected = [
  7467. 'input' => [
  7468. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '',
  7469. ],
  7470. ['div' => ['class' => 'checkbox']],
  7471. ['label' => ['for' => 'multi-field-0']],
  7472. ['input' => [
  7473. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7474. 'value' => '0', 'id' => 'multi-field-0',
  7475. ]],
  7476. 'first',
  7477. '/label',
  7478. '/div',
  7479. ['div' => ['class' => 'checkbox']],
  7480. ['label' => ['for' => 'multi-field-1']],
  7481. ['input' => [
  7482. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7483. 'value' => '1', 'id' => 'multi-field-1',
  7484. ]],
  7485. 'second',
  7486. '/label',
  7487. '/div',
  7488. ];
  7489. $this->assertHtml($expected, $result);
  7490. }
  7491. /**
  7492. * Test the basic setters and getters for value sources
  7493. *
  7494. * @return void
  7495. */
  7496. public function testFormValueSourcesSettersGetters()
  7497. {
  7498. $this->View->setRequest($this->View->getRequest()
  7499. ->withData('id', '1')
  7500. ->withQueryParams(['id' => '2']));
  7501. $expected = ['context'];
  7502. $result = $this->Form->getValueSources();
  7503. $this->assertEquals($expected, $result);
  7504. $expected = ['query', 'data', 'context'];
  7505. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7506. $result = $this->Form->getValueSources();
  7507. $this->assertEquals($expected, $result);
  7508. $this->Form->setValueSources(['context']);
  7509. $expected = '1';
  7510. $result = $this->Form->getSourceValue('id');
  7511. $this->assertEquals($expected, $result);
  7512. $this->Form->setValueSources('query');
  7513. $expected = ['query'];
  7514. $result = $this->Form->getValueSources();
  7515. $this->assertEquals($expected, $result);
  7516. $expected = '2';
  7517. $result = $this->Form->getSourceValue('id');
  7518. $this->assertEquals($expected, $result);
  7519. $this->Form->setValueSources(['data']);
  7520. $expected = '1';
  7521. $result = $this->Form->getSourceValue('id');
  7522. $this->assertEquals($expected, $result);
  7523. $this->Form->setValueSources(['query', 'data']);
  7524. $expected = '2';
  7525. $result = $this->Form->getSourceValue('id');
  7526. $this->assertEquals($expected, $result);
  7527. }
  7528. /**
  7529. * Tests the different input rendering values based on sources values switching
  7530. *
  7531. * @return void
  7532. */
  7533. public function testFormValueSourcesSingleSwitchRendering()
  7534. {
  7535. $this->loadFixtures('Articles');
  7536. $articles = $this->getTableLocator()->get('Articles');
  7537. $article = new Article();
  7538. $articles->patchEntity($article, ['id' => '3']);
  7539. $this->Form->create($article);
  7540. $this->Form->setValueSources(['context']);
  7541. $result = $this->Form->control('id');
  7542. $expected = [
  7543. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7544. ];
  7545. $this->assertHtml($expected, $result);
  7546. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7547. $this->Form->setValueSources(['query']);
  7548. $result = $this->Form->control('id');
  7549. $expected = [
  7550. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7551. ];
  7552. $this->assertHtml($expected, $result);
  7553. $this->View->setRequest($this->View->getRequest()
  7554. ->withQueryParams(['id' => '5a'])
  7555. ->withData('id', '5b'));
  7556. $this->Form->setValueSources(['context']);
  7557. $this->Form->create($article);
  7558. $result = $this->Form->control('id');
  7559. $expected = [
  7560. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7561. ];
  7562. $this->assertHtml($expected, $result);
  7563. $this->Form->setValueSources(['data']);
  7564. $this->Form->create($article);
  7565. $result = $this->Form->control('id');
  7566. $expected = [
  7567. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7568. ];
  7569. $this->assertHtml($expected, $result);
  7570. $this->Form->setValueSources(['query']);
  7571. $result = $this->Form->control('id');
  7572. $expected = [
  7573. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7574. ];
  7575. $this->assertHtml($expected, $result);
  7576. }
  7577. /**
  7578. * Tests the different input rendering values based on sources values switching while supplying
  7579. * an entity (base context) and multiple sources (such as data, query)
  7580. *
  7581. * @return void
  7582. */
  7583. public function testFormValueSourcesListSwitchRendering()
  7584. {
  7585. $this->loadFixtures('Articles');
  7586. $articles = $this->getTableLocator()->get('Articles');
  7587. $article = new Article();
  7588. $articles->patchEntity($article, ['id' => '3']);
  7589. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7590. $this->Form->create($article);
  7591. $this->Form->setValueSources(['context', 'query']);
  7592. $result = $this->Form->control('id');
  7593. $expected = [
  7594. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7595. ];
  7596. $this->assertHtml($expected, $result);
  7597. $this->Form->setValueSources(['query', 'context']);
  7598. $result = $this->Form->control('id');
  7599. $expected = [
  7600. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7601. ];
  7602. $this->assertHtml($expected, $result);
  7603. $this->Form->setValueSources(['data', 'query', 'context']);
  7604. $result = $this->Form->control('id');
  7605. $expected = [
  7606. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7607. ];
  7608. $this->assertHtml($expected, $result);
  7609. $this->View->setRequest($this->View->getRequest()
  7610. ->withData('id', '8')
  7611. ->withQueryParams(['id' => '9']));
  7612. $this->Form->setValueSources(['data', 'query', 'context']);
  7613. $result = $this->Form->control('id');
  7614. $expected = [
  7615. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7616. ];
  7617. $this->assertHtml($expected, $result);
  7618. }
  7619. /**
  7620. * Test the different form input renderings based on values sources switchings through form options
  7621. *
  7622. * @return void
  7623. */
  7624. public function testFormValueSourcesSwitchViaOptionsRendering()
  7625. {
  7626. $this->loadFixtures('Articles');
  7627. $articles = $this->getTableLocator()->get('Articles');
  7628. $article = new Article();
  7629. $articles->patchEntity($article, ['id' => '3']);
  7630. $this->View->setRequest(
  7631. $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
  7632. );
  7633. $this->Form->create($article, ['valueSources' => 'query']);
  7634. $result = $this->Form->control('id');
  7635. $expected = [
  7636. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7637. ];
  7638. $this->assertHtml($expected, $result);
  7639. $result = $this->Form->getSourceValue('id');
  7640. $this->assertSame('5', $result);
  7641. $this->Form->setValueSources(['context']);
  7642. $this->Form->create($article, ['valueSources' => 'query']);
  7643. $result = $this->Form->control('id');
  7644. $expected = [
  7645. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7646. ];
  7647. $this->assertHtml($expected, $result);
  7648. $result = $this->Form->getSourceValue('id');
  7649. $this->assertSame('5', $result);
  7650. $this->Form->setValueSources(['query']);
  7651. $this->Form->create($article, ['valueSources' => 'data']);
  7652. $result = $this->Form->control('id');
  7653. $expected = [
  7654. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7655. ];
  7656. $this->assertHtml($expected, $result);
  7657. $result = $this->Form->getSourceValue('id');
  7658. $this->assertSame('4', $result);
  7659. $this->Form->setValueSources(['query']);
  7660. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7661. $result = $this->Form->control('id');
  7662. $expected = [
  7663. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7664. ];
  7665. $this->assertHtml($expected, $result);
  7666. $result = $this->Form->getSourceValue('id');
  7667. $this->assertSame('4', $result);
  7668. }
  7669. /**
  7670. * Test the different form input renderings based on values sources switchings through form options
  7671. *
  7672. * @return void
  7673. */
  7674. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  7675. {
  7676. $this->loadFixtures('Articles');
  7677. $articles = $this->getTableLocator()->get('Articles');
  7678. $article = new Article();
  7679. $articles->patchEntity($article, ['id' => '3']);
  7680. $this->View->setRequest(
  7681. $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
  7682. );
  7683. $this->Form->setValueSources(['context'])
  7684. ->create($article, ['valueSources' => ['query', 'data']]);
  7685. $result = $this->Form->control('id');
  7686. $expected = [
  7687. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7688. ];
  7689. $this->assertHtml($expected, $result);
  7690. $result = $this->Form->getSourceValue('id');
  7691. $this->assertSame('11', $result);
  7692. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7693. $this->Form->setValueSources(['context'])
  7694. ->create($article, ['valueSources' => ['query', 'data']]);
  7695. $result = $this->Form->control('id');
  7696. $expected = [
  7697. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7698. ];
  7699. $this->assertHtml($expected, $result);
  7700. $result = $this->Form->getSourceValue('id');
  7701. $this->assertSame('10', $result);
  7702. }
  7703. /**
  7704. * Test the different form values sources resetting through From::end();
  7705. *
  7706. * @return void
  7707. */
  7708. public function testFormValueSourcesResetViaEnd()
  7709. {
  7710. $expected = ['context'];
  7711. $result = $this->Form->getValueSources();
  7712. $this->assertEquals($expected, $result);
  7713. $expected = ['query', 'context', 'data'];
  7714. $this->Form->setValueSources(['query', 'context', 'data']);
  7715. $result = $this->Form->getValueSources();
  7716. $this->assertEquals($expected, $result);
  7717. $this->Form->create();
  7718. $result = $this->Form->getValueSources();
  7719. $this->assertEquals($expected, $result);
  7720. $this->Form->end();
  7721. $result = $this->Form->getValueSources();
  7722. $this->assertEquals(['context'], $result);
  7723. }
  7724. /**
  7725. * Test sources values defaults handling
  7726. *
  7727. * @return void
  7728. */
  7729. public function testFormValueSourcesDefaults()
  7730. {
  7731. $this->View->setRequest(
  7732. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7733. );
  7734. $this->Form->create();
  7735. $result = $this->Form->password('password');
  7736. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7737. $this->assertHtml($expected, $result);
  7738. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7739. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7740. $this->assertHtml($expected, $result);
  7741. $this->Form->setValueSources('query');
  7742. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7743. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7744. $this->assertHtml($expected, $result);
  7745. $this->Form->setValueSources('data');
  7746. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7747. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7748. $this->assertHtml($expected, $result);
  7749. }
  7750. /**
  7751. * Test sources values schema defaults handling
  7752. *
  7753. * @return void
  7754. */
  7755. public function testSourcesValueDoesntExistPassThrough()
  7756. {
  7757. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7758. $articles = $this->getTableLocator()->get('Articles');
  7759. $entity = $articles->newEmptyEntity();
  7760. $this->Form->create($entity);
  7761. $this->Form->setValueSources(['query', 'context']);
  7762. $result = $this->Form->getSourceValue('category');
  7763. $this->assertSame('sesame-cookies', $result);
  7764. $this->Form->setValueSources(['context', 'query']);
  7765. $result = $this->Form->getSourceValue('category');
  7766. $this->assertSame('sesame-cookies', $result);
  7767. }
  7768. /**
  7769. * testNestedLabelInput method
  7770. *
  7771. * Test the `nestedInput` parameter
  7772. *
  7773. * @return void
  7774. */
  7775. public function testNestedLabelInput()
  7776. {
  7777. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7778. $expected = [
  7779. 'div' => ['class' => 'input text'],
  7780. 'label' => ['for' => 'foo'],
  7781. ['input' => [
  7782. 'type' => 'text',
  7783. 'name' => 'foo',
  7784. 'id' => 'foo',
  7785. ]],
  7786. 'Foo',
  7787. '/label',
  7788. '/div',
  7789. ];
  7790. $this->assertHtml($expected, $result);
  7791. }
  7792. /**
  7793. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7794. *
  7795. * This test makes sure `false` excludes the label from the render
  7796. *
  7797. * @return void
  7798. */
  7799. public function testControlLabelManipulationDisableLabels()
  7800. {
  7801. $result = $this->Form->control('test', [
  7802. 'type' => 'radio',
  7803. 'options' => ['A', 'B'],
  7804. 'labelOptions' => false,
  7805. ]);
  7806. $expected = [
  7807. ['div' => ['class' => 'input radio']],
  7808. '<label',
  7809. 'Test',
  7810. '/label',
  7811. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7812. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7813. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7814. '/div',
  7815. ];
  7816. $this->assertHtml($expected, $result);
  7817. $result = $this->Form->control('checkbox1', [
  7818. 'label' => 'My checkboxes',
  7819. 'multiple' => 'checkbox',
  7820. 'type' => 'select',
  7821. 'options' => [
  7822. ['text' => 'First Checkbox', 'value' => 1],
  7823. ['text' => 'Second Checkbox', 'value' => 2],
  7824. ],
  7825. 'labelOptions' => false,
  7826. ]);
  7827. $expected = [
  7828. ['div' => ['class' => 'input select']],
  7829. ['label' => ['for' => 'checkbox1']],
  7830. 'My checkboxes',
  7831. '/label',
  7832. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  7833. ['div' => ['class' => 'checkbox']],
  7834. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7835. '/div',
  7836. ['div' => ['class' => 'checkbox']],
  7837. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7838. '/div',
  7839. '/div',
  7840. ];
  7841. $this->assertHtml($expected, $result);
  7842. }
  7843. /**
  7844. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7845. *
  7846. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7847. * added to the class if checked.
  7848. *
  7849. * Also checks to make sure any custom attributes are rendered correctly
  7850. *
  7851. * @return void
  7852. */
  7853. public function testControlLabelManipulationRadios()
  7854. {
  7855. $result = $this->Form->control('test', [
  7856. 'type' => 'radio',
  7857. 'options' => ['A', 'B'],
  7858. 'labelOptions' => ['class' => 'custom-class'],
  7859. ]);
  7860. $expected = [
  7861. ['div' => ['class' => 'input radio']],
  7862. '<label',
  7863. 'Test',
  7864. '/label',
  7865. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7866. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7867. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7868. 'A',
  7869. '/label',
  7870. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  7871. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7872. 'B',
  7873. '/label',
  7874. '/div',
  7875. ];
  7876. $this->assertHtml($expected, $result);
  7877. $result = $this->Form->control('test', [
  7878. 'type' => 'radio',
  7879. 'options' => ['A', 'B'],
  7880. 'value' => 1,
  7881. 'labelOptions' => ['class' => 'custom-class'],
  7882. ]);
  7883. $expected = [
  7884. ['div' => ['class' => 'input radio']],
  7885. '<label',
  7886. 'Test',
  7887. '/label',
  7888. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7889. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7890. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7891. 'A',
  7892. '/label',
  7893. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  7894. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7895. 'B',
  7896. '/label',
  7897. '/div',
  7898. ];
  7899. $this->assertHtml($expected, $result);
  7900. $result = $this->Form->control('test', [
  7901. 'type' => 'radio',
  7902. 'options' => ['A', 'B'],
  7903. 'value' => 1,
  7904. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  7905. ]);
  7906. $expected = [
  7907. ['div' => ['class' => 'input radio']],
  7908. '<label',
  7909. 'Test',
  7910. '/label',
  7911. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7912. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  7913. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7914. 'A',
  7915. '/label',
  7916. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  7917. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7918. 'B',
  7919. '/label',
  7920. '/div',
  7921. ];
  7922. $this->assertHtml($expected, $result);
  7923. $result = $this->Form->radio('test', ['A', 'B'], [
  7924. 'label' => [
  7925. 'class' => ['custom-class', 'another-class'],
  7926. 'data-name' => 'bob',
  7927. ],
  7928. 'value' => 1,
  7929. ]);
  7930. $expected = [
  7931. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  7932. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  7933. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7934. 'A',
  7935. '/label',
  7936. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  7937. ['input' => [
  7938. 'type' => 'radio',
  7939. 'name' => 'test',
  7940. 'value' => '1',
  7941. 'id' => 'test-1',
  7942. 'checked' => 'checked',
  7943. ]],
  7944. 'B',
  7945. '/label',
  7946. ];
  7947. $this->assertHtml($expected, $result);
  7948. }
  7949. /**
  7950. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  7951. *
  7952. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7953. * added to the class if checked.
  7954. *
  7955. * Also checks to make sure any custom attributes are rendered correctly
  7956. *
  7957. * @return void
  7958. */
  7959. public function testControlLabelManipulationCheckboxes()
  7960. {
  7961. $result = $this->Form->control('checkbox1', [
  7962. 'label' => 'My checkboxes',
  7963. 'multiple' => 'checkbox',
  7964. 'type' => 'select',
  7965. 'options' => [
  7966. ['text' => 'First Checkbox', 'value' => 1],
  7967. ['text' => 'Second Checkbox', 'value' => 2],
  7968. ],
  7969. 'labelOptions' => ['class' => 'custom-class'],
  7970. 'value' => ['1'],
  7971. ]);
  7972. $expected = [
  7973. ['div' => ['class' => 'input select']],
  7974. ['label' => ['for' => 'checkbox1']],
  7975. 'My checkboxes',
  7976. '/label',
  7977. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  7978. ['div' => ['class' => 'checkbox']],
  7979. ['label' => [
  7980. 'class' => 'custom-class selected',
  7981. 'for' => 'checkbox1-1',
  7982. ]],
  7983. ['input' => [
  7984. 'type' => 'checkbox',
  7985. 'name' => 'checkbox1[]',
  7986. 'value' => '1',
  7987. 'id' => 'checkbox1-1',
  7988. 'checked' => 'checked',
  7989. ]],
  7990. 'First Checkbox',
  7991. '/label',
  7992. '/div',
  7993. ['div' => ['class' => 'checkbox']],
  7994. ['label' => [
  7995. 'class' => 'custom-class',
  7996. 'for' => 'checkbox1-2',
  7997. ]],
  7998. ['input' => [
  7999. 'type' => 'checkbox',
  8000. 'name' => 'checkbox1[]',
  8001. 'value' => '2',
  8002. 'id' => 'checkbox1-2',
  8003. ]],
  8004. 'Second Checkbox',
  8005. '/label',
  8006. '/div',
  8007. '/div',
  8008. ];
  8009. $this->assertHtml($expected, $result);
  8010. $result = $this->Form->control('checkbox1', [
  8011. 'label' => 'My checkboxes',
  8012. 'multiple' => 'checkbox',
  8013. 'type' => 'select',
  8014. 'options' => [
  8015. ['text' => 'First Checkbox', 'value' => 1],
  8016. ['text' => 'Second Checkbox', 'value' => 2],
  8017. ],
  8018. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8019. 'value' => ['1'],
  8020. ]);
  8021. $expected = [
  8022. ['div' => ['class' => 'input select']],
  8023. ['label' => ['for' => 'checkbox1']],
  8024. 'My checkboxes',
  8025. '/label',
  8026. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8027. ['div' => ['class' => 'checkbox']],
  8028. ['label' => [
  8029. 'class' => 'custom-class another-class selected',
  8030. 'data-name' => 'bob',
  8031. 'for' => 'checkbox1-1',
  8032. ]],
  8033. ['input' => [
  8034. 'type' => 'checkbox',
  8035. 'name' => 'checkbox1[]',
  8036. 'value' => '1',
  8037. 'id' => 'checkbox1-1',
  8038. 'checked' => 'checked',
  8039. ]],
  8040. 'First Checkbox',
  8041. '/label',
  8042. '/div',
  8043. ['div' => ['class' => 'checkbox']],
  8044. ['label' => [
  8045. 'class' => 'custom-class another-class',
  8046. 'data-name' => 'bob',
  8047. 'for' => 'checkbox1-2',
  8048. ]],
  8049. ['input' => [
  8050. 'type' => 'checkbox',
  8051. 'name' => 'checkbox1[]',
  8052. 'value' => '2',
  8053. 'id' => 'checkbox1-2',
  8054. ]],
  8055. 'Second Checkbox',
  8056. '/label',
  8057. '/div',
  8058. '/div',
  8059. ];
  8060. $this->assertHtml($expected, $result);
  8061. }
  8062. /**
  8063. * testControlMaxLengthArrayContext method
  8064. *
  8065. * Test control() with maxlength attribute in Array Context.
  8066. *
  8067. * @return void
  8068. */
  8069. public function testControlMaxLengthArrayContext()
  8070. {
  8071. $this->article['schema'] = [
  8072. 'title' => ['length' => 10],
  8073. ];
  8074. $this->Form->create($this->article);
  8075. $result = $this->Form->control('title');
  8076. $expected = [
  8077. 'div' => ['class'],
  8078. 'label' => ['for'],
  8079. 'Title',
  8080. '/label',
  8081. 'input' => [
  8082. 'id',
  8083. 'name' => 'title',
  8084. 'type' => 'text',
  8085. 'required' => 'required',
  8086. 'maxlength' => 10,
  8087. 'oninvalid' => 'this.setCustomValidity(&#039;This field is required&#039;)',
  8088. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  8089. ],
  8090. '/div',
  8091. ];
  8092. $this->assertHtml($expected, $result);
  8093. }
  8094. /**
  8095. * testControlMaxLengthEntityContext method
  8096. *
  8097. * Test control() with maxlength attribute in Entity Context.
  8098. *
  8099. * @return void
  8100. */
  8101. public function testControlMaxLengthEntityContext()
  8102. {
  8103. $this->article['schema']['title']['length'] = 45;
  8104. $validator = new Validator();
  8105. $validator->maxLength('title', 10);
  8106. $article = new EntityContext(
  8107. new ServerRequest(),
  8108. [
  8109. 'entity' => new Entity($this->article),
  8110. 'table' => new Table([
  8111. 'alias' => 'Articles',
  8112. 'schema' => $this->article['schema'],
  8113. 'validator' => $validator,
  8114. ]),
  8115. ]
  8116. );
  8117. $this->Form->create($article);
  8118. $result = $this->Form->control('title');
  8119. $expected = [
  8120. 'div' => ['class'],
  8121. 'label' => ['for'],
  8122. 'Title',
  8123. '/label',
  8124. 'input' => [
  8125. 'id',
  8126. 'name' => 'title',
  8127. 'type' => 'text',
  8128. 'required' => 'required',
  8129. 'maxlength' => 10,
  8130. 'oninvalid' => 'this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  8131. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  8132. ],
  8133. '/div',
  8134. ];
  8135. $this->assertHtml($expected, $result);
  8136. $this->article['schema']['title']['length'] = 45;
  8137. $validator = new Validator();
  8138. $validator->maxLength('title', 55);
  8139. $article = new EntityContext(
  8140. new ServerRequest(),
  8141. [
  8142. 'entity' => new Entity($this->article),
  8143. 'table' => new Table([
  8144. 'schema' => $this->article['schema'],
  8145. 'validator' => $validator,
  8146. 'alias' => 'Articles',
  8147. ]),
  8148. ]
  8149. );
  8150. $this->Form->create($article);
  8151. $result = $this->Form->control('title');
  8152. $expected = [
  8153. 'div' => ['class'],
  8154. 'label' => ['for'],
  8155. 'Title',
  8156. '/label',
  8157. 'input' => [
  8158. 'id',
  8159. 'name' => 'title',
  8160. 'type' => 'text',
  8161. 'required' => 'required',
  8162. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8163. 'oninvalid' => 'this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  8164. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  8165. ],
  8166. '/div',
  8167. ];
  8168. $this->assertHtml($expected, $result);
  8169. $this->article['schema']['title']['length'] = 45;
  8170. $validator = new Validator();
  8171. $validator->maxLength('title', 55);
  8172. $article = new EntityContext(
  8173. new ServerRequest(),
  8174. [
  8175. 'entity' => new Entity($this->article),
  8176. 'table' => new Table([
  8177. 'schema' => $this->article['schema'],
  8178. 'validator' => $validator,
  8179. 'alias' => 'Articles',
  8180. ]),
  8181. ]
  8182. );
  8183. $this->Form->create($article);
  8184. $result = $this->Form->control('title', ['maxlength' => 10]);
  8185. $expected = [
  8186. 'div' => ['class'],
  8187. 'label' => ['for'],
  8188. 'Title',
  8189. '/label',
  8190. 'input' => [
  8191. 'id',
  8192. 'name' => 'title',
  8193. 'type' => 'text',
  8194. 'required' => 'required',
  8195. 'maxlength' => 10, // Length set in options should take highest precedence.
  8196. 'oninvalid' => 'this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  8197. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  8198. ],
  8199. '/div',
  8200. ];
  8201. $this->assertHtml($expected, $result);
  8202. }
  8203. /**
  8204. * testControlMinMaxLengthEntityContext method
  8205. *
  8206. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8207. *
  8208. * @return void
  8209. */
  8210. public function testControlMinMaxLengthEntityContext()
  8211. {
  8212. $validator = new Validator();
  8213. $validator->maxLength('title', 10);
  8214. $article = new EntityContext(
  8215. new ServerRequest(),
  8216. [
  8217. 'entity' => new Entity($this->article),
  8218. 'table' => new Table([
  8219. 'alias' => 'Articles',
  8220. 'schema' => $this->article['schema'],
  8221. 'validator' => $validator,
  8222. ]),
  8223. ]
  8224. );
  8225. $this->Form->create($article);
  8226. $result = $this->Form->control('title');
  8227. $expected = [
  8228. 'div' => ['class'],
  8229. 'label' => ['for'],
  8230. 'Title',
  8231. '/label',
  8232. 'input' => [
  8233. 'id',
  8234. 'name' => 'title',
  8235. 'type' => 'text',
  8236. 'required' => 'required',
  8237. 'maxlength' => 10,
  8238. 'oninvalid' => 'this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  8239. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  8240. ],
  8241. '/div',
  8242. ];
  8243. $this->assertHtml($expected, $result);
  8244. }
  8245. /**
  8246. * testControlMaxLengthFormContext method
  8247. *
  8248. * Test control() with maxlength attribute in Form Context.
  8249. *
  8250. * @return void
  8251. */
  8252. public function testControlMaxLengthFormContext()
  8253. {
  8254. $validator = new Validator();
  8255. $validator->maxLength('title', 10);
  8256. $form = new Form();
  8257. $form->setValidator('default', $validator);
  8258. $this->Form->create($form);
  8259. $result = $this->Form->control('title');
  8260. $expected = [
  8261. 'div' => ['class'],
  8262. 'label' => ['for'],
  8263. 'Title',
  8264. '/label',
  8265. 'input' => [
  8266. 'id',
  8267. 'name' => 'title',
  8268. 'type' => 'text',
  8269. 'required' => 'required',
  8270. 'maxlength' => 10,
  8271. 'oninvalid' => 'this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  8272. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  8273. ],
  8274. '/div',
  8275. ];
  8276. $this->assertHtml($expected, $result);
  8277. }
  8278. }